2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 * Copyright (C) 1990 by the Massachusetts Institute of Technology
37 * Export of this software from the United States of America is assumed
38 * to require a specific license from the United States Government.
39 * It is the responsibility of any person or organization contemplating
40 * export to obtain such a license before exporting.
42 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
43 * distribute this software and its documentation for any purpose and
44 * without fee is hereby granted, provided that the above copyright
45 * notice appear in all copies and that both that copyright notice and
46 * this permission notice appear in supporting documentation, and that
47 * the name of M.I.T. not be used in advertising or publicity pertaining
48 * to distribution of the software without specific, written prior
49 * permission. M.I.T. makes no representations about the suitability of
50 * this software for any purpose. It is provided "as is" without express
51 * or implied warranty.
58 RCSID("$Id: kerberos.c 22071 2007-11-14 20:04:50Z lha $");
61 #ifdef HAVE_SYS_TYPES_H
62 #include <sys/types.h>
64 #ifdef HAVE_ARPA_TELNET_H
65 #include <arpa/telnet.h>
82 int kerberos4_cksum (unsigned char *, int);
83 extern int auth_debug_mode;
85 static unsigned char str_data[2048] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
86 AUTHTYPE_KERBEROS_V4, };
88 #define KRB_AUTH 0 /* Authentication data follows */
89 #define KRB_REJECT 1 /* Rejected (reason might follow) */
90 #define KRB_ACCEPT 2 /* Accepted */
91 #define KRB_CHALLENGE 3 /* Challenge for mutual auth. */
92 #define KRB_RESPONSE 4 /* Response for mutual auth. */
94 #define KRB_FORWARD 5 /* */
95 #define KRB_FORWARD_ACCEPT 6 /* */
96 #define KRB_FORWARD_REJECT 7 /* */
98 #define KRB_SERVICE_NAME "rcmd"
100 static KTEXT_ST auth;
101 static char name[ANAME_SZ];
102 static AUTH_DAT adat;
103 static des_cblock session_key;
104 static des_cblock cred_session;
105 static des_key_schedule sched;
106 static des_cblock challenge;
107 static int auth_done; /* XXX */
109 static int pack_cred(CREDENTIALS *cred, unsigned char *buf);
110 static int unpack_cred(unsigned char *buf, int len, CREDENTIALS *cred);
114 Data(Authenticator *ap, int type, const void *d, int c)
116 unsigned char *p = str_data + 4;
117 const unsigned char *cd = (const unsigned char *)d;
120 c = strlen((const char *)cd);
122 if (auth_debug_mode) {
123 printf("%s:%d: [%d] (%d)",
124 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
134 if ((*p++ = *cd++) == IAC)
139 if (str_data[3] == TELQUAL_IS)
140 printsub('>', &str_data[2], p - (&str_data[2]));
141 return(telnet_net_write(str_data, p - str_data));
145 kerberos4_init(Authenticator *ap, int server)
150 str_data[3] = TELQUAL_REPLY;
151 if ((fp = fopen(KEYFILE, "r")) == NULL)
155 str_data[3] = TELQUAL_IS;
160 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
161 int dst_realm_sz = REALM_SZ;
164 kerberos4_send(char *name, Authenticator *ap)
167 char instance[INST_SZ];
172 if (!UserNameRequested) {
173 if (auth_debug_mode) {
174 printf("Kerberos V4: no user name supplied\r\n");
179 memset(instance, 0, sizeof(instance));
182 krb_get_phost(RemoteHostName),
185 realm = dest_realm ? dest_realm : krb_realmofhost(RemoteHostName);
188 printf("Kerberos V4: no realm for %s\r\n", RemoteHostName);
191 printf("[ Trying %s (%s.%s@%s) ... ]\r\n", name,
192 KRB_SERVICE_NAME, instance, realm);
193 r = krb_mk_req(&auth, KRB_SERVICE_NAME, instance, realm, 0L);
195 printf("mk_req failed: %s\r\n", krb_get_err_text(r));
198 r = krb_get_cred(KRB_SERVICE_NAME, instance, realm, &cred);
200 printf("get_cred failed: %s\r\n", krb_get_err_text(r));
203 if (!auth_sendname((unsigned char*)UserNameRequested,
204 strlen(UserNameRequested))) {
206 printf("Not enough room for user name\r\n");
210 printf("Sent %d bytes of authentication data\r\n", auth.length);
211 if (!Data(ap, KRB_AUTH, (void *)auth.dat, auth.length)) {
213 printf("Not enough room for authentication data\r\n");
217 /* create challenge */
218 if ((ap->way & AUTH_HOW_MASK)==AUTH_HOW_MUTUAL) {
221 des_key_sched(&cred.session, sched);
222 memcpy (&cred_session, &cred.session, sizeof(cred_session));
224 des_init_random_number_generator(&cred.session);
226 des_new_random_key(&session_key);
227 des_ecb_encrypt(&session_key, &session_key, sched, 0);
228 des_ecb_encrypt(&session_key, &challenge, sched, 0);
232 Some CERT Advisory thinks this is a bad thing...
234 des_init_random_number_generator(&cred.session);
235 des_new_random_key(&challenge);
236 des_ecb_encrypt(&challenge, &session_key, sched, 1);
240 * Increment the challenge by 1, and encrypt it for
243 for (i = 7; i >= 0; --i)
244 if(++challenge[i] != 0) /* No carry! */
246 des_ecb_encrypt(&challenge, &challenge, sched, 1);
251 if (auth_debug_mode) {
252 printf("CK: %d:", kerberos4_cksum(auth.dat, auth.length));
253 printd(auth.dat, auth.length);
255 printf("Sent Kerberos V4 credentials to server\r\n");
260 kerberos4_send_mutual(Authenticator *ap)
262 return kerberos4_send("mutual KERBEROS4", ap);
266 kerberos4_send_oneway(Authenticator *ap)
268 return kerberos4_send("KERBEROS4", ap);
272 kerberos4_is(Authenticator *ap, unsigned char *data, int cnt)
274 struct sockaddr_in addr;
275 char realm[REALM_SZ];
276 char instance[INST_SZ];
284 if (krb_get_lrealm(realm, 1) != KSUCCESS) {
285 Data(ap, KRB_REJECT, (void *)"No local V4 Realm.", -1);
286 auth_finished(ap, AUTH_REJECT);
288 printf("No local realm\r\n");
291 memmove(auth.dat, data, auth.length = cnt);
292 if (auth_debug_mode) {
293 printf("Got %d bytes of authentication data\r\n", cnt);
294 printf("CK: %d:", kerberos4_cksum(auth.dat, auth.length));
295 printd(auth.dat, auth.length);
298 k_getsockinst(0, instance, sizeof(instance));
299 addr_len = sizeof(addr);
300 if(getpeername(0, (struct sockaddr *)&addr, &addr_len) < 0) {
302 printf("getpeername failed\r\n");
303 Data(ap, KRB_REJECT, "getpeername failed", -1);
304 auth_finished(ap, AUTH_REJECT);
307 if (addr.sin_family != AF_INET) {
309 printf("unknown address family: %d\r\n", addr.sin_family);
310 Data(ap, KRB_REJECT, "bad address family", -1);
311 auth_finished(ap, AUTH_REJECT);
315 r = krb_rd_req(&auth, KRB_SERVICE_NAME,
316 instance, addr.sin_addr.s_addr, &adat, "");
319 printf("Kerberos failed him as %s\r\n", name);
320 Data(ap, KRB_REJECT, (void *)krb_get_err_text(r), -1);
321 auth_finished(ap, AUTH_REJECT);
324 /* save the session key */
325 memmove(session_key, adat.session, sizeof(adat.session));
326 krb_kntoln(&adat, name);
328 if (UserNameRequested && !kuserok(&adat, UserNameRequested)){
330 struct passwd *pw = getpwnam(UserNameRequested);
333 snprintf(ts, sizeof(ts),
336 (unsigned)pw->pw_uid);
337 esetenv("KRBTKFILE", ts, 1);
340 syslog(LOG_INFO|LOG_AUTH,
341 "ROOT Kerberos login from %s on %s\n",
342 krb_unparse_name_long(adat.pname,
347 Data(ap, KRB_ACCEPT, NULL, 0);
352 ret = asprintf (&msg, "user `%s' is not authorized to "
354 krb_unparse_name_long(adat.pname,
357 UserNameRequested ? UserNameRequested : "<nobody>");
359 Data(ap, KRB_REJECT, NULL, 0);
361 Data(ap, KRB_REJECT, (void *)msg, -1);
364 auth_finished(ap, AUTH_REJECT);
367 auth_finished(ap, AUTH_USER);
372 Data(ap, KRB_RESPONSE, NULL, 0);
374 if(!VALIDKEY(session_key)){
375 Data(ap, KRB_RESPONSE, NULL, 0);
378 des_key_sched(&session_key, sched);
384 memmove(d_block, data, sizeof(d_block));
386 /* make a session key for encryption */
387 des_ecb_encrypt(&d_block, &session_key, sched, 1);
390 skey.data=session_key;
391 encrypt_session_key(&skey, 1);
393 /* decrypt challenge, add one and encrypt it */
394 des_ecb_encrypt(&d_block, &challenge, sched, 0);
395 for (i = 7; i >= 0; i--)
396 if(++challenge[i] != 0)
398 des_ecb_encrypt(&challenge, &challenge, sched, 1);
399 Data(ap, KRB_RESPONSE, (void *)challenge, sizeof(challenge));
407 unsigned char netcred[sizeof(CREDENTIALS)];
410 if(cnt > sizeof(cred))
413 memcpy (session_key, adat.session, sizeof(session_key));
414 des_set_key(&session_key, ks);
415 des_pcbc_encrypt((void*)data, (void*)netcred, cnt,
416 ks, &session_key, DES_DECRYPT);
417 unpack_cred(netcred, cnt, &cred);
419 if(strcmp(cred.service, KRB_TICKET_GRANTING_TICKET) ||
420 strncmp(cred.instance, cred.realm, sizeof(cred.instance)) ||
421 cred.lifetime < 0 || cred.lifetime > 255 ||
422 cred.kvno < 0 || cred.kvno > 255 ||
423 cred.issue_date < 0 ||
424 cred.issue_date > time(0) + CLOCK_SKEW ||
425 strncmp(cred.pname, adat.pname, sizeof(cred.pname)) ||
426 strncmp(cred.pinst, adat.pinst, sizeof(cred.pinst))){
427 Data(ap, KRB_FORWARD_REJECT, "Bad credentials", -1);
429 if((ret = tf_setup(&cred,
431 cred.pinst)) == KSUCCESS){
432 struct passwd *pw = getpwnam(UserNameRequested);
435 chown(tkt_string(), pw->pw_uid, pw->pw_gid);
436 Data(ap, KRB_FORWARD_ACCEPT, 0, 0);
438 Data(ap, KRB_FORWARD_REJECT,
439 krb_get_err_text(ret), -1);
443 memset(data, 0, cnt);
444 memset(&ks, 0, sizeof(ks));
445 memset(&cred, 0, sizeof(cred));
452 printf("Unknown Kerberos option %d\r\n", data[-1]);
453 Data(ap, KRB_REJECT, 0, 0);
459 kerberos4_reply(Authenticator *ap, unsigned char *data, int cnt)
467 if(auth_done){ /* XXX Ick! */
468 printf("[ Kerberos V4 received unknown opcode ]\r\n");
470 printf("[ Kerberos V4 refuses authentication ");
472 printf("because %.*s ", cnt, data);
478 printf("[ Kerberos V4 accepts you ]\r\n");
480 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
482 * Send over the encrypted challenge.
484 Data(ap, KRB_CHALLENGE, session_key,
485 sizeof(session_key));
486 des_ecb_encrypt(&session_key, &session_key, sched, 1);
489 skey.data = session_key;
490 encrypt_session_key(&skey, 0);
492 kerberos4_forward(ap, &cred_session);
496 auth_finished(ap, AUTH_USER);
499 /* make sure the response is correct */
500 if ((cnt != sizeof(des_cblock)) ||
501 (memcmp(data, challenge, sizeof(challenge)))){
502 printf("[ Kerberos V4 challenge failed!!! ]\r\n");
506 printf("[ Kerberos V4 challenge successful ]\r\n");
507 auth_finished(ap, AUTH_USER);
509 case KRB_FORWARD_ACCEPT:
510 printf("[ Kerberos V4 accepted forwarded credentials ]\r\n");
512 case KRB_FORWARD_REJECT:
513 printf("[ Kerberos V4 rejected forwarded credentials: `%.*s']\r\n",
518 printf("Unknown Kerberos option %d\r\n", data[-1]);
524 kerberos4_status(Authenticator *ap, char *name, size_t name_sz, int level)
526 if (level < AUTH_USER)
529 if (UserNameRequested && !kuserok(&adat, UserNameRequested)) {
530 strlcpy(name, UserNameRequested, name_sz);
536 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
537 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
540 kerberos4_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
544 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
548 case KRB_REJECT: /* Rejected (reason might follow) */
549 strlcpy((char *)buf, " REJECT ", buflen);
552 case KRB_ACCEPT: /* Accepted (name might follow) */
553 strlcpy((char *)buf, " ACCEPT ", buflen);
558 ADDC(buf, buflen, '"');
559 for (i = 4; i < cnt; i++)
560 ADDC(buf, buflen, data[i]);
561 ADDC(buf, buflen, '"');
562 ADDC(buf, buflen, '\0');
565 case KRB_AUTH: /* Authentication data follows */
566 strlcpy((char *)buf, " AUTH", buflen);
570 strlcpy((char *)buf, " CHALLENGE", buflen);
574 strlcpy((char *)buf, " RESPONSE", buflen);
578 snprintf((char*)buf, buflen, " %d (unknown)", data[3]);
581 for (i = 4; i < cnt; i++) {
582 snprintf((char*)buf, buflen, " %d", data[i]);
590 kerberos4_cksum(unsigned char *d, int n)
595 * A comment is probably needed here for those not
596 * well versed in the "C" language. Yes, this is
597 * supposed to be a "switch" with the body of the
598 * "switch" being a "while" statement. The whole
599 * purpose of the switch is to allow us to jump into
600 * the middle of the while() loop, and then not have
601 * to do any more switch()s.
603 * Some compilers will spit out a warning message
604 * about the loop not being entered at the top.
609 ck ^= (int)*d++ << 24;
612 ck ^= (int)*d++ << 16;
615 ck ^= (int)*d++ << 8;
625 pack_cred(CREDENTIALS *cred, unsigned char *buf)
627 unsigned char *p = buf;
629 memcpy (p, cred->service, ANAME_SZ);
631 memcpy (p, cred->instance, INST_SZ);
633 memcpy (p, cred->realm, REALM_SZ);
635 memcpy(p, cred->session, 8);
637 p += KRB_PUT_INT(cred->lifetime, p, 4, 4);
638 p += KRB_PUT_INT(cred->kvno, p, 4, 4);
639 p += KRB_PUT_INT(cred->ticket_st.length, p, 4, 4);
640 memcpy(p, cred->ticket_st.dat, cred->ticket_st.length);
641 p += cred->ticket_st.length;
642 p += KRB_PUT_INT(0, p, 4, 4);
643 p += KRB_PUT_INT(cred->issue_date, p, 4, 4);
644 memcpy (p, cred->pname, ANAME_SZ);
646 memcpy (p, cred->pinst, INST_SZ);
652 unpack_cred(unsigned char *buf, int len, CREDENTIALS *cred)
654 char *p = (char*)buf;
657 strncpy (cred->service, p, ANAME_SZ);
658 cred->service[ANAME_SZ - 1] = '\0';
660 strncpy (cred->instance, p, INST_SZ);
661 cred->instance[INST_SZ - 1] = '\0';
663 strncpy (cred->realm, p, REALM_SZ);
664 cred->realm[REALM_SZ - 1] = '\0';
667 memcpy(cred->session, p, 8);
669 p += krb_get_int(p, &tmp, 4, 0);
670 cred->lifetime = tmp;
671 p += krb_get_int(p, &tmp, 4, 0);
674 p += krb_get_int(p, &cred->ticket_st.length, 4, 0);
675 memcpy(cred->ticket_st.dat, p, cred->ticket_st.length);
676 p += cred->ticket_st.length;
677 p += krb_get_int(p, &tmp, 4, 0);
678 cred->ticket_st.mbz = 0;
679 p += krb_get_int(p, (uint32_t *)&cred->issue_date, 4, 0);
681 strncpy (cred->pname, p, ANAME_SZ);
682 cred->pname[ANAME_SZ - 1] = '\0';
684 strncpy (cred->pinst, p, INST_SZ);
685 cred->pinst[INST_SZ - 1] = '\0';
692 kerberos4_forward(Authenticator *ap, void *v)
694 des_cblock *key = (des_cblock *)v;
699 unsigned char netcred[sizeof(CREDENTIALS)];
702 realm = krb_realmofhost(RemoteHostName);
705 memset(&cred, 0, sizeof(cred));
706 ret = krb_get_cred(KRB_TICKET_GRANTING_TICKET,
712 des_set_key(key, ks);
713 len = pack_cred(&cred, netcred);
714 des_pcbc_encrypt((void*)netcred, (void*)netcred, len,
715 ks, key, DES_ENCRYPT);
716 memset(&ks, 0, sizeof(ks));
717 Data(ap, KRB_FORWARD, netcred, len);
718 memset(netcred, 0, sizeof(netcred));