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