xref: /dragonfly/lib/libtelnet/krb4encpwd.c (revision 5868d2b9)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)krb4encpwd.c	8.3 (Berkeley) 5/30/95
34  * $FreeBSD: src/crypto/telnet/libtelnet/krb4encpwd.c,v 1.3.2.1 2002/04/13 10:59:07 markm Exp $
35  */
36 
37 #ifdef	KRB4_ENCPWD
38 /*
39  * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
40  * ALL RIGHTS RESERVED
41  *
42  * "Digital Equipment Corporation authorizes the reproduction,
43  * distribution and modification of this software subject to the following
44  * restrictions:
45  *
46  * 1.  Any partial or whole copy of this software, or any modification
47  * thereof, must include this copyright notice in its entirety.
48  *
49  * 2.  This software is supplied "as is" with no warranty of any kind,
50  * expressed or implied, for any purpose, including any warranty of fitness
51  * or merchantibility.  DIGITAL assumes no responsibility for the use or
52  * reliability of this software, nor promises to provide any form of
53  * support for it on any basis.
54  *
55  * 3.  Distribution of this software is authorized only if no profit or
56  * remuneration of any kind is received in exchange for such distribution.
57  *
58  * 4.  This software produces public key authentication certificates
59  * bearing an expiration date established by DIGITAL and RSA Data
60  * Security, Inc.  It may cease to generate certificates after the expiration
61  * date.  Any modification of this software that changes or defeats
62  * the expiration date or its effect is unauthorized.
63  *
64  * 5.  Software that will renew or extend the expiration date of
65  * authentication certificates produced by this software may be obtained
66  * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
67  * 94065, (415)595-8782, or from DIGITAL"
68  *
69  */
70 
71 #include <sys/types.h>
72 #include <openssl/des.h>
73 #include <arpa/telnet.h>
74 #include <krb.h>
75 #include <pwd.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 
80 #include "encrypt.h"
81 #include "auth.h"
82 #include "misc.h"
83 
84 int krb_mk_encpwd_req(KTEXT, char *, char *, char *, char *, char *, char *);
85 int krb_rd_encpwd_req(KTEXT, char *, char *, u_long, AUTH_DAT *, char *, char *, char *, char *);
86 
87 extern auth_debug_mode;
88 
89 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
90 			  		AUTHTYPE_KRB4_ENCPWD, };
91 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
92 					TELQUAL_NAME, };
93 
94 #define	KRB4_ENCPWD_AUTH	0	/* Authentication data follows */
95 #define	KRB4_ENCPWD_REJECT	1	/* Rejected (reason might follow) */
96 #define KRB4_ENCPWD_ACCEPT	2	/* Accepted */
97 #define	KRB4_ENCPWD_CHALLENGE	3	/* Challenge for mutual auth. */
98 #define	KRB4_ENCPWD_ACK		4	/* Acknowledge */
99 
100 #define KRB_SERVICE_NAME    "rcmd"
101 
102 static	KTEXT_ST auth;
103 static	char name[ANAME_SZ];
104 static	char user_passwd[ANAME_SZ];
105 static	AUTH_DAT adat = { 0 };
106 #ifdef	ENCRYPTION
107 static Block	session_key	= { 0 };
108 #endif	/* ENCRYPTION */
109 static char  challenge[REALM_SZ];
110 
111 	static int
112 Data(ap, type, d, c)
113 	Authenticator *ap;
114 	int type;
115 	void *d;
116 	int c;
117 {
118 	unsigned char *p = str_data + 4;
119 	unsigned char *cd = (unsigned char *)d;
120 
121 	if (c == -1)
122 		c = strlen((char *)cd);
123 
124 	if (0) {
125 		printf("%s:%d: [%d] (%d)",
126 			str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
127 			str_data[3],
128 			type, c);
129 		printd(d, c);
130 		printf("\r\n");
131 	}
132 	*p++ = ap->type;
133 	*p++ = ap->way;
134 	*p++ = type;
135 	while (c-- > 0) {
136 		if ((*p++ = *cd++) == IAC)
137 			*p++ = IAC;
138 	}
139 	*p++ = IAC;
140 	*p++ = SE;
141 	if (str_data[3] == TELQUAL_IS)
142 		printsub('>', &str_data[2], p - (&str_data[2]));
143 	return(net_write(str_data, p - str_data));
144 }
145 
146 	int
147 krb4encpwd_init(ap, server)
148 	Authenticator *ap;
149 	int server;
150 {
151 	char hostname[80], *cp, *realm;
152 	C_Block skey;
153 
154 	if (server) {
155 		str_data[3] = TELQUAL_REPLY;
156 	} else {
157 		str_data[3] = TELQUAL_IS;
158 		gethostname(hostname, sizeof(hostname));
159 		realm = krb_realmofhost(hostname);
160 		cp = strchr(hostname, '.');
161 		if (*cp != NULL) *cp = NULL;
162 		if (read_service_key(KRB_SERVICE_NAME, hostname, realm, 0,
163 					KEYFILE, (char *)skey)) {
164 		  return(0);
165 		}
166 	}
167 	return(1);
168 }
169 
170 	int
171 krb4encpwd_send(ap)
172 	Authenticator *ap;
173 {
174 
175 	printf("[ Trying KRB4ENCPWD ... ]\n");
176 	if (!UserNameRequested) {
177 		return(0);
178 	}
179 	if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
180 		return(0);
181 	}
182 
183 	if (!Data(ap, KRB4_ENCPWD_ACK, NULL, 0)) {
184 		return(0);
185 	}
186 
187 	return(1);
188 }
189 
190 	void
191 krb4encpwd_is(ap, data, cnt)
192 	Authenticator *ap;
193 	unsigned char *data;
194 	int cnt;
195 {
196 	Session_Key skey;
197 	Block datablock;
198 	char  r_passwd[ANAME_SZ], r_user[ANAME_SZ];
199 	char  lhostname[ANAME_SZ], *cp;
200 	int r;
201 	time_t now;
202 
203 	if (cnt-- < 1)
204 		return;
205 	switch (*data++) {
206 	case KRB4_ENCPWD_AUTH:
207 		memmove((void *)auth.dat, (void *)data, auth.length = cnt);
208 
209 		gethostname(lhostname, sizeof(lhostname));
210 		if ((cp = strchr(lhostname, '.')) != NULL)  *cp = '\0';
211 
212 		if (r = krb_rd_encpwd_req(&auth, KRB_SERVICE_NAME, lhostname, 0, &adat, NULL, challenge, r_user, r_passwd)) {
213 			Data(ap, KRB4_ENCPWD_REJECT, (void *)"Auth failed", -1);
214 			auth_finished(ap, AUTH_REJECT);
215 			return;
216 		}
217 		auth_encrypt_userpwd(r_passwd);
218 		if (passwdok(UserNameRequested, UserPassword) == 0) {
219 		  /*
220 		   *  illegal username and password
221 		   */
222 		  Data(ap, KRB4_ENCPWD_REJECT, (void *)"Illegal password", -1);
223 		  auth_finished(ap, AUTH_REJECT);
224 		  return;
225 		}
226 
227 		memmove((void *)session_key, (void *)adat.session, sizeof(Block));
228 		Data(ap, KRB4_ENCPWD_ACCEPT, (void *)0, 0);
229 		auth_finished(ap, AUTH_USER);
230 		break;
231 
232 	case KRB4_ENCPWD_CHALLENGE:
233 		/*
234 		 *  Take the received random challenge text and save
235 		 *  for future authentication.
236 		 */
237 		memmove((void *)challenge, (void *)data, sizeof(Block));
238 		break;
239 
240 
241 	case KRB4_ENCPWD_ACK:
242 		/*
243 		 *  Receive ack, if mutual then send random challenge
244 		 */
245 
246 		/*
247 		 * If we are doing mutual authentication, get set up to send
248 		 * the challenge, and verify it when the response comes back.
249 		 */
250 
251 		if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
252 		  register int i;
253 
254 		  time(&now);
255 		  sprintf(challenge, "%x", now);
256 		  Data(ap, KRB4_ENCPWD_CHALLENGE, (void *)challenge, strlen(challenge));
257 		}
258 		break;
259 
260 	default:
261 		Data(ap, KRB4_ENCPWD_REJECT, 0, 0);
262 		break;
263 	}
264 }
265 
266 
267 	void
268 krb4encpwd_reply(ap, data, cnt)
269 	Authenticator *ap;
270 	unsigned char *data;
271 	int cnt;
272 {
273 	Session_Key skey;
274 	KTEXT_ST krb_token;
275 	Block enckey;
276 	CREDENTIALS cred;
277 	int r;
278 	char	randchal[REALM_SZ], instance[ANAME_SZ], *cp;
279 	char	hostname[80], *realm;
280 
281 	if (cnt-- < 1)
282 		return;
283 	switch (*data++) {
284 	case KRB4_ENCPWD_REJECT:
285 		if (cnt > 0) {
286 			printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n",
287 				cnt, data);
288 		} else
289 			printf("[ KRB4_ENCPWD refuses authentication ]\r\n");
290 		auth_send_retry();
291 		return;
292 	case KRB4_ENCPWD_ACCEPT:
293 		printf("[ KRB4_ENCPWD accepts you ]\n");
294 		auth_finished(ap, AUTH_USER);
295 		return;
296 	case KRB4_ENCPWD_CHALLENGE:
297 		/*
298 		 * Verify that the response to the challenge is correct.
299 		 */
300 
301 		gethostname(hostname, sizeof(hostname));
302 		realm = krb_realmofhost(hostname);
303 		memmove((void *)challenge, (void *)data, cnt);
304 		memset(user_passwd, 0, sizeof(user_passwd));
305 		local_des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
306 		UserPassword = user_passwd;
307 		Challenge = challenge;
308 		strcpy(instance, RemoteHostName);
309 		if ((cp = strchr(instance, '.')) != NULL)  *cp = '\0';
310 
311 		if (r = krb_mk_encpwd_req(&krb_token, KRB_SERVICE_NAME, instance, realm, Challenge, UserNameRequested, user_passwd)) {
312 		  krb_token.length = 0;
313 		}
314 
315 		if (!Data(ap, KRB4_ENCPWD_AUTH, (void *)krb_token.dat, krb_token.length)) {
316 		  return;
317 		}
318 
319 		break;
320 
321 	default:
322 		return;
323 	}
324 }
325 
326 	int
327 krb4encpwd_status(ap, name, level)
328 	Authenticator *ap;
329 	char *name;
330 	int level;
331 {
332 
333 	if (level < AUTH_USER)
334 		return(level);
335 
336 	if (UserNameRequested && passwdok(UserNameRequested, UserPassword)) {
337 		strcpy(name, UserNameRequested);
338 		return(AUTH_VALID);
339 	} else {
340 		return(AUTH_USER);
341 	}
342 }
343 
344 #define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
345 #define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len);}
346 
347 	void
348 krb4encpwd_printsub(data, cnt, buf, buflen)
349 	unsigned char *data, *buf;
350 	int cnt, buflen;
351 {
352 	char lbuf[32];
353 	register int i;
354 
355 	buf[buflen-1] = '\0';		/* make sure its NULL terminated */
356 	buflen -= 1;
357 
358 	switch(data[3]) {
359 	case KRB4_ENCPWD_REJECT:	/* Rejected (reason might follow) */
360 		strncpy((char *)buf, " REJECT ", buflen);
361 		goto common;
362 
363 	case KRB4_ENCPWD_ACCEPT:	/* Accepted (name might follow) */
364 		strncpy((char *)buf, " ACCEPT ", buflen);
365 	common:
366 		BUMP(buf, buflen);
367 		if (cnt <= 4)
368 			break;
369 		ADDC(buf, buflen, '"');
370 		for (i = 4; i < cnt; i++)
371 			ADDC(buf, buflen, data[i]);
372 		ADDC(buf, buflen, '"');
373 		ADDC(buf, buflen, '\0');
374 		break;
375 
376 	case KRB4_ENCPWD_AUTH:		/* Authentication data follows */
377 		strncpy((char *)buf, " AUTH", buflen);
378 		goto common2;
379 
380 	case KRB4_ENCPWD_CHALLENGE:
381 		strncpy((char *)buf, " CHALLENGE", buflen);
382 		goto common2;
383 
384 	case KRB4_ENCPWD_ACK:
385 		strncpy((char *)buf, " ACK", buflen);
386 		goto common2;
387 
388 	default:
389 		sprintf(lbuf, " %d (unknown)", data[3]);
390 		strncpy((char *)buf, lbuf, buflen);
391 	common2:
392 		BUMP(buf, buflen);
393 		for (i = 4; i < cnt; i++) {
394 			sprintf(lbuf, " %d", data[i]);
395 			strncpy((char *)buf, lbuf, buflen);
396 			BUMP(buf, buflen);
397 		}
398 		break;
399 	}
400 }
401 
402 int passwdok(name, passwd)
403 char *name, *passwd;
404 {
405   char *crypt();
406   char *salt, *p;
407   struct passwd *pwd;
408   int   passwdok_status = 0;
409 
410   if (pwd = getpwnam(name))
411     salt = pwd->pw_passwd;
412   else salt = "xx";
413 
414   p = crypt(passwd, salt);
415 
416   if (pwd && !strcmp(p, pwd->pw_passwd)) {
417     passwdok_status = 1;
418   } else passwdok_status = 0;
419   return(passwdok_status);
420 }
421 
422 #endif
423