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