Debian Codegraph @

libtelnet/kerberos.c

raw
1 /*
2 Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1991, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * Copyright (C) 1990 by the Massachusetts Institute of Technology
50 *
51 * Export of this software from the United States of America is assumed
52 * to require a specific license from the United States Government.
53 * It is the responsibility of any person or organization contemplating
54 * export to obtain such a license before exporting.
55 *
56 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
57 * distribute this software and its documentation for any purpose and
58 * without fee is hereby granted, provided that the above copyright
59 * notice appear in all copies and that both that copyright notice and
60 * this permission notice appear in supporting documentation, and that
61 * the name of M.I.T. not be used in advertising or publicity pertaining
62 * to distribution of the software without specific, written prior
63 * permission. M.I.T. makes no representations about the suitability of
64 * this software for any purpose. It is provided "as is" without express
65 * or implied warranty.
66 */
67
68 #include <config.h>
69
70 #ifdef KRB4
71 # include <sys/types.h>
72 # include <arpa/telnet.h>
73 # include <stdio.h>
74 # ifdef HAVE_DES_H
75 # include <des.h> /* BSD wont include this in krb.h, so we do it here */
76 # endif
77 # ifdef HAVE_KRB_H
78 # include <krb.h>
79 # endif
80 # include <stdlib.h>
81 # include <string.h>
82
83 # include "encrypt.h"
84 # include "auth.h"
85 # include "misc.h"
86
87 int kerberos4_cksum (unsigned char *, int);
88 int krb_mk_req (KTEXT, char *, char *, char *, unsigned long);
89 int krb_rd_req (KTEXT, char *, char *, unsigned long, AUTH_DAT *, char *);
90 int krb_kntoln (AUTH_DAT *, char *);
91 int krb_get_cred (char *, char *, char *, CREDENTIALS *);
92 int krb_get_lrealm (char *, int);
93 int kuserok (AUTH_DAT *, char *);
94
95 extern auth_debug_mode;
96
97 /* Callback from consumer. */
98 extern void printsub (char, unsigned char *, int);
99
100 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
101 AUTHTYPE_KERBEROS_V4,
102 };
103
104 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
105 TELQUAL_NAME,
106 };
107
108 # define KRB_AUTH 0 /* Authentication data follows */
109 # define KRB_REJECT 1 /* Rejected (reason might follow) */
110 # define KRB_ACCEPT 2 /* Accepted */
111 # define KRB_CHALLENGE 3 /* Challenge for mutual auth. */
112 # define KRB_RESPONSE 4 /* Response for mutual auth. */
113
114 # define KRB_SERVICE_NAME "rcmd"
115
116 static KTEXT_ST auth;
117 static char name[ANAME_SZ];
118 static AUTH_DAT adat = { 0 };
119
120 # ifdef ENCRYPTION
121 static Block session_key = { 0 };
122
123 static Schedule sched;
124 static Block challenge = { 0 };
125 # endif/* ENCRYPTION */
126
127 static int
128 Data (ap, type, d, c)
129 TN_Authenticator *ap;
130 int type;
131 void *d;
132 int c;
133 {
134 unsigned char *p = str_data + 4;
135 unsigned char *cd = (unsigned char *) d;
136
137 if (c == -1)
138 c = strlen ((char *) cd);
139
140 if (auth_debug_mode)
141 {
142 printf ("%s:%d: [%d] (%d)",
143 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
144 str_data[3], type, c);
145 printd (d, c);
146 printf ("\r\n");
147 }
148 *p++ = ap->type;
149 *p++ = ap->way;
150 *p++ = type;
151 while (c-- > 0)
152 {
153 if ((*p++ = *cd++) == IAC)
154 *p++ = IAC;
155 }
156 *p++ = IAC;
157 *p++ = SE;
158 if (str_data[3] == TELQUAL_IS)
159 printsub ('>', &str_data[2], p - (&str_data[2]));
160 return (net_write (str_data, p - str_data));
161 }
162
163 int
164 kerberos4_init (ap, server)
165 TN_Authenticator *ap;
166 int server;
167 {
168 FILE *fp;
169
170 if (server)
171 {
172 str_data[3] = TELQUAL_REPLY;
173 if ((fp = fopen (KEYFILE, "r")) == NULL)
174 return (0);
175 fclose (fp);
176 }
177 else
178 {
179 str_data[3] = TELQUAL_IS;
180 }
181 return (1);
182 }
183
184 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
185 int dst_realm_sz = REALM_SZ;
186
187 int
188 kerberos4_send (ap)
189 TN_Authenticator *ap;
190 {
191 KTEXT_ST auth;
192 # ifdef ENCRYPTION
193 Block enckey;
194 # endif/* ENCRYPTION */
195 char instance[INST_SZ];
196 char *realm;
197 char *krb_realmofhost ();
198 char *krb_get_phost ();
199 CREDENTIALS cred;
200 int r;
201
202 printf ("[ Trying KERBEROS4 ... ]\n");
203 if (!UserNameRequested)
204 {
205 if (auth_debug_mode)
206 {
207 printf ("Kerberos V4: no user name supplied\r\n");
208 }
209 return (0);
210 }
211
212 memset (instance, 0, sizeof (instance));
213
214 if (realm = krb_get_phost (RemoteHostName))
215 strncpy (instance, realm, sizeof (instance));
216
217 instance[sizeof (instance) - 1] = '\0';
218
219 realm = dest_realm ? dest_realm : krb_realmofhost (RemoteHostName);
220
221 if (!realm)
222 {
223 printf ("Kerberos V4: no realm for %s\r\n", RemoteHostName);
224 return (0);
225 }
226 if (r = krb_mk_req (&auth, KRB_SERVICE_NAME, instance, realm, 0L))
227 {
228 printf ("mk_req failed: %s\r\n", krb_err_txt[r]);
229 return (0);
230 }
231 if (r = krb_get_cred (KRB_SERVICE_NAME, instance, realm, &cred))
232 {
233 printf ("get_cred failed: %s\r\n", krb_err_txt[r]);
234 return (0);
235 }
236 if (!auth_sendname (UserNameRequested, strlen (UserNameRequested)))
237 {
238 if (auth_debug_mode)
239 printf ("Not enough room for user name\r\n");
240 return (0);
241 }
242 if (auth_debug_mode)
243 printf ("Sent %d bytes of authentication data\r\n", auth.length);
244 if (!Data (ap, KRB_AUTH, (void *) auth.dat, auth.length))
245 {
246 if (auth_debug_mode)
247 printf ("Not enough room for authentication data\r\n");
248 return (0);
249 }
250 # ifdef ENCRYPTION
251 /*
252 * If we are doing mutual authentication, get set up to send
253 * the challenge, and verify it when the response comes back.
254 */
255 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
256 {
257 int i;
258
259 des_key_sched (cred.session, sched);
260 des_init_random_number_generator (cred.session);
261 des_new_random_key (session_key);
262 des_ecb_encrypt (session_key, session_key, sched, 0);
263 des_ecb_encrypt (session_key, challenge, sched, 0);
264 /*
265 * Increment the challenge by 1, and encrypt it for
266 * later comparison.
267 */
268 for (i = 7; i >= 0; --i)
269 {
270 int x;
271 x = (unsigned int) challenge[i] + 1;
272 challenge[i] = x; /* ignore overflow */
273 if (x < 256) /* if no overflow, all done */
274 break;
275 }
276 des_ecb_encrypt (challenge, challenge, sched, 1);
277 }
278 # endif/* ENCRYPTION */
279
280 if (auth_debug_mode)
281 {
282 printf ("CK: %d:", kerberos4_cksum (auth.dat, auth.length));
283 printd (auth.dat, auth.length);
284 printf ("\r\n");
285 printf ("Sent Kerberos V4 credentials to server\r\n");
286 }
287 return (1);
288 }
289
290 void
291 kerberos4_is (ap, data, cnt)
292 TN_Authenticator *ap;
293 unsigned char *data;
294 int cnt;
295 {
296 # ifdef ENCRYPTION
297 Session_Key skey;
298 Block datablock;
299 # endif/* ENCRYPTION */
300 char realm[REALM_SZ];
301 char instance[INST_SZ];
302 int r;
303
304 if (cnt-- < 1)
305 return;
306 switch (*data++)
307 {
308 case KRB_AUTH:
309 if (krb_get_lrealm (realm, 1) != KSUCCESS)
310 {
311 Data (ap, KRB_REJECT, (void *) "No local V4 Realm.", -1);
312 auth_finished (ap, AUTH_REJECT);
313 if (auth_debug_mode)
314 printf ("No local realm\r\n");
315 return;
316 }
317 memmove ((void *) auth.dat, (void *) data, auth.length = cnt);
318 if (auth_debug_mode)
319 {
320 printf ("Got %d bytes of authentication data\r\n", cnt);
321 printf ("CK: %d:", kerberos4_cksum (auth.dat, auth.length));
322 printd (auth.dat, auth.length);
323 printf ("\r\n");
324 }
325 instance[0] = '*';
326 instance[1] = 0;
327 if (r = krb_rd_req (&auth, KRB_SERVICE_NAME, instance, 0, &adat, ""))
328 {
329 if (auth_debug_mode)
330 printf ("Kerberos failed him as %s\r\n", name);
331 Data (ap, KRB_REJECT, (void *) krb_err_txt[r], -1);
332 auth_finished (ap, AUTH_REJECT);
333 return;
334 }
335 # ifdef ENCRYPTION
336 memmove ((void *) session_key, (void *) adat.session, sizeof (Block));
337 # endif/* ENCRYPTION */
338 krb_kntoln (&adat, name);
339
340 if (UserNameRequested && !kuserok (&adat, UserNameRequested))
341 Data (ap, KRB_ACCEPT, (void *) 0, 0);
342 else
343 Data (ap, KRB_REJECT, (void *) "user is not authorized", -1);
344 auth_finished (ap, AUTH_USER);
345 break;
346
347 case KRB_CHALLENGE:
348 # ifndef ENCRYPTION
349 Data (ap, KRB_RESPONSE, (void *) 0, 0);
350 # else/* ENCRYPTION */
351 if (!VALIDKEY (session_key))
352 {
353 /*
354 * We don't have a valid session key, so just
355 * send back a response with an empty session
356 * key.
357 */
358 Data (ap, KRB_RESPONSE, (void *) 0, 0);
359 break;
360 }
361
362 /*
363 * Initialize the random number generator since it's
364 * used later on by the encryption routine.
365 */
366 des_init_random_number_generator (session_key);
367 des_key_sched (session_key, sched);
368 memmove ((void *) datablock, (void *) data, sizeof (Block));
369 /*
370 * Take the received encrypted challenge, and encrypt
371 * it again to get a unique session_key for the
372 * ENCRYPT option.
373 */
374 des_ecb_encrypt (datablock, session_key, sched, 1);
375 skey.type = SK_DES;
376 skey.length = 8;
377 skey.data = session_key;
378 encrypt_session_key (&skey, 1);
379 /*
380 * Now decrypt the received encrypted challenge,
381 * increment by one, re-encrypt it and send it back.
382 */
383 des_ecb_encrypt (datablock, challenge, sched, 0);
384 for (r = 7; r >= 0; r--)
385 {
386 int t;
387 t = (unsigned int) challenge[r] + 1;
388 challenge[r] = t; /* ignore overflow */
389 if (t < 256) /* if no overflow, all done */
390 break;
391 }
392 des_ecb_encrypt (challenge, challenge, sched, 1);
393 Data (ap, KRB_RESPONSE, (void *) challenge, sizeof (challenge));
394 # endif/* ENCRYPTION */
395 break;
396
397 default:
398 if (auth_debug_mode)
399 printf ("Unknown Kerberos option %d\r\n", data[-1]);
400 Data (ap, KRB_REJECT, 0, 0);
401 break;
402 }
403 }
404
405 void
406 kerberos4_reply (ap, data, cnt)
407 TN_Authenticator *ap;
408 unsigned char *data;
409 int cnt;
410 {
411 # ifdef ENCRYPTION
412 Session_Key skey;
413 # endif/* ENCRYPTION */
414
415 if (cnt-- < 1)
416 return;
417 switch (*data++)
418 {
419 case KRB_REJECT:
420 if (cnt > 0)
421 {
422 printf ("[ Kerberos V4 refuses authentication because %.*s ]\r\n",
423 cnt, data);
424 }
425 else
426 printf ("[ Kerberos V4 refuses authentication ]\r\n");
427 auth_send_retry ();
428 return;
429 case KRB_ACCEPT:
430 printf ("[ Kerberos V4 accepts you ]\n");
431 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
432 {
433 /*
434 * Send over the encrypted challenge.
435 */
436 # ifndef ENCRYPTION
437 Data (ap, KRB_CHALLENGE, (void *) 0, 0);
438 # else/* ENCRYPTION */
439 Data (ap, KRB_CHALLENGE, (void *) session_key,
440 sizeof (session_key));
441 des_ecb_encrypt (session_key, session_key, sched, 1);
442 skey.type = SK_DES;
443 skey.length = 8;
444 skey.data = session_key;
445 encrypt_session_key (&skey, 0);
446 # endif/* ENCRYPTION */
447 return;
448 }
449 auth_finished (ap, AUTH_USER);
450 return;
451 case KRB_RESPONSE:
452 # ifdef ENCRYPTION
453 /*
454 * Verify that the response to the challenge is correct.
455 */
456 if ((cnt != sizeof (Block)) ||
457 (0 != memcmp ((void *) data, (void *) challenge,
458 sizeof (challenge))))
459 {
460 # endif/* ENCRYPTION */
461 printf ("[ Kerberos V4 challenge failed!!! ]\r\n");
462 auth_send_retry ();
463 return;
464 # ifdef ENCRYPTION
465 }
466 printf ("[ Kerberos V4 challenge successful ]\r\n");
467 auth_finished (ap, AUTH_USER);
468 # endif/* ENCRYPTION */
469 break;
470 default:
471 if (auth_debug_mode)
472 printf ("Unknown Kerberos option %d\r\n", data[-1]);
473 return;
474 }
475 }
476
477 int
478 kerberos4_status (ap, name, len, level)
479 TN_Authenticator *ap;
480 char *name;
481 size_t len;
482 int level;
483 {
484 if (level < AUTH_USER)
485 return (level);
486
487 if (UserNameRequested && !kuserok (&adat, UserNameRequested))
488 {
489 strncpy (name, UserNameRequested, len);
490 return (AUTH_VALID);
491 }
492 else
493 return (AUTH_USER);
494 }
495
496 # define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
497 # define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
498
499 void
500 kerberos4_printsub (data, cnt, buf, buflen)
501 unsigned char *data;
502 char *buf;
503 int cnt, buflen;
504 {
505 char lbuf[32];
506 int i;
507
508 buf[buflen - 1] = '\0'; /* make sure its NULL terminated */
509 buflen -= 1;
510
511 switch (data[3])
512 {
513 case KRB_REJECT: /* Rejected (reason might follow) */
514 strncpy ((char *) buf, " REJECT ", buflen);
515 goto common;
516
517 case KRB_ACCEPT: /* Accepted (name might follow) */
518 strncpy ((char *) buf, " ACCEPT ", buflen);
519 common:
520 BUMP (buf, buflen);
521 if (cnt <= 4)
522 break;
523 ADDC (buf, buflen, '"');
524 for (i = 4; i < cnt; i++)
525 ADDC (buf, buflen, data[i]);
526 ADDC (buf, buflen, '"');
527 ADDC (buf, buflen, '\0');
528 break;
529
530 case KRB_AUTH: /* Authentication data follows */
531 strncpy ((char *) buf, " AUTH", buflen);
532 goto common2;
533
534 case KRB_CHALLENGE:
535 strncpy ((char *) buf, " CHALLENGE", buflen);
536 goto common2;
537
538 case KRB_RESPONSE:
539 strncpy ((char *) buf, " RESPONSE", buflen);
540 goto common2;
541
542 default:
543 sprintf (lbuf, " %d (unknown)", data[3]);
544 strncpy ((char *) buf, lbuf, buflen);
545 common2:
546 BUMP (buf, buflen);
547 for (i = 4; i < cnt; i++)
548 {
549 sprintf (lbuf, " %d", data[i]);
550 strncpy ((char *) buf, lbuf, buflen);
551 BUMP (buf, buflen);
552 }
553 break;
554 }
555 }
556
557 int
558 kerberos4_cksum (d, n)
559 unsigned char *d;
560 int n;
561 {
562 int ck = 0;
563
564 /*
565 * A comment is probably needed here for those not
566 * well versed in the "C" language. Yes, this is
567 * supposed to be a "switch" with the body of the
568 * "switch" being a "while" statement. The whole
569 * purpose of the switch is to allow us to jump into
570 * the middle of the while() loop, and then not have
571 * to do any more switch()s.
572 *
573 * Some compilers will spit out a warning message
574 * about the loop not being entered at the top.
575 */
576 switch (n & 03)
577 while (n > 0)
578 {
579 case 0:
580 ck ^= (int) *d++ << 24;
581 --n;
582 case 3:
583 ck ^= (int) *d++ << 16;
584 --n;
585 case 2:
586 ck ^= (int) *d++ << 8;
587 --n;
588 case 1:
589 ck ^= (int) *d++;
590 --n;
591 }
592 return (ck);
593 }
594 #endif