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 may
38 * 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.
54 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
60 #include <arpa/telnet.h>
68 #define Authenticator k5_Authenticator
76 int forward_flags = 0; /* Flags get set in telnet/main.c on -f and -F */
78 /* These values need to be the same as those defined in telnet/main.c. */
79 /* Either define them in both places, or put in some common header file. */
80 #define OPTS_FORWARD_CREDS 0x00000002
81 #define OPTS_FORWARDABLE_CREDS 0x00000001
83 void kerberos5_forward (Authenticator *);
85 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
86 AUTHTYPE_KERBEROS_V5, };
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_RESPONSE 3 /* Response for mutual auth. */
93 #define KRB_FORWARD 4 /* Forwarded credentials follow */
94 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
95 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
97 static krb5_data auth;
98 static krb5_ticket *ticket;
100 static krb5_context context;
101 static krb5_auth_context auth_context;
104 print_krb5_error(krb5_context context, krb5_error_code code, const char *msg)
106 const char *error_message;
108 error_message = krb5_get_error_message(context, code);
109 printf(msg, error_message);
110 krb5_free_error_message(context, error_message);
114 Data(Authenticator *ap, int type, const char *d, int c)
116 unsigned char *p = str_data + 4;
117 const unsigned char *cd = d;
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(net_write(str_data, p - str_data));
145 kerberos5_init(Authenticator *ap __unused, int server)
149 ret = krb5_init_context(&context);
154 krb5_kt_cursor cursor;
156 ret = krb5_kt_default(context, &kt);
160 ret = krb5_kt_start_seq_get (context, kt, &cursor);
162 krb5_kt_close (context, kt);
165 krb5_kt_end_seq_get (context, kt, &cursor);
166 krb5_kt_close (context, kt);
168 str_data[3] = TELQUAL_REPLY;
170 str_data[3] = TELQUAL_IS;
177 kerberos5_send(const char *name, Authenticator *ap)
182 krb5_data cksum_data;
185 if (!UserNameRequested) {
186 if (auth_debug_mode) {
187 printf("Kerberos V5: no user name supplied\r\n");
192 ret = krb5_cc_default(context, &ccache);
194 if (auth_debug_mode) {
195 print_krb5_error(context, ret, "Kerberos V5: could not get default ccache: %s\r\n");
200 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
201 ap_opts = AP_OPTS_MUTUAL_REQUIRED;
204 ap_opts |= AP_OPTS_USE_SUBKEY;
206 ret = krb5_auth_con_init (context, &auth_context);
208 if (auth_debug_mode) {
209 print_krb5_error(context, ret, "Kerberos V5: krb5_auth_con_init failed (%s)\r\n");
214 ret = krb5_auth_con_setaddrs_from_fd (context,
218 if (auth_debug_mode) {
219 print_krb5_error(context, ret, "Kerberos V5:"
220 " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n");
225 krb5_auth_con_setkeytype (context, auth_context, KEYTYPE_DES);
230 cksum_data.length = sizeof(foo);
231 cksum_data.data = foo;
235 krb5_principal service;
239 ret = krb5_sname_to_principal (context,
245 if (auth_debug_mode) {
248 err_str = krb5_get_error_message(context, ret);
249 printf("Kerberosr V5:"
250 " krb5_sname_to_principal(%s) failed (%s)\r\n",
251 RemoteHostName, err_str);
252 krb5_free_error_message(context, err_str);
256 ret = krb5_unparse_name_fixed(context, service, sname, sizeof(sname));
258 if (auth_debug_mode) {
259 print_krb5_error(context, ret, "Kerberos V5:"
260 " krb5_unparse_name_fixed failed (%s)\r\n");
264 printf("[ Trying %s (%s)... ]\r\n", name, sname);
265 ret = krb5_mk_req_exact(context, &auth_context, ap_opts,
267 &cksum_data, ccache, &auth);
268 krb5_free_principal (context, service);
272 if (1 || auth_debug_mode) {
273 print_krb5_error(context, ret, "Kerberos V5: mk_req failed (%s)\r\n");
278 if (!auth_sendname((unsigned char *)UserNameRequested,
279 strlen(UserNameRequested))) {
281 printf("Not enough room for user name\r\n");
284 if (!Data(ap, KRB_AUTH, auth.data, auth.length)) {
286 printf("Not enough room for authentication data\r\n");
289 if (auth_debug_mode) {
290 printf("Sent Kerberos V5 credentials to server\r\n");
296 kerberos5_send_mutual(Authenticator *ap)
298 return kerberos5_send("mutual KERBEROS5", ap);
302 kerberos5_send_oneway(Authenticator *ap)
304 return kerberos5_send("KERBEROS5", ap);
308 kerberos5_is(Authenticator *ap, unsigned char *data, int cnt)
312 krb5_keyblock *key_block;
314 krb5_principal server;
321 auth.data = (char *)data;
326 ret = krb5_auth_con_init (context, &auth_context);
328 Data(ap, KRB_REJECT, "krb5_auth_con_init failed", -1);
329 auth_finished(ap, AUTH_REJECT);
331 print_krb5_error(context, ret, "Kerberos V5: krb5_auth_con_init failed (%s)\r\n");
335 ret = krb5_auth_con_setaddrs_from_fd (context,
339 Data(ap, KRB_REJECT, "krb5_auth_con_setaddrs_from_fd failed", -1);
340 auth_finished(ap, AUTH_REJECT);
342 print_krb5_error(context, ret, "Kerberos V5: "
343 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n");
347 ret = krb5_sock_to_principal (context,
353 Data(ap, KRB_REJECT, "krb5_sock_to_principal failed", -1);
354 auth_finished(ap, AUTH_REJECT);
356 print_krb5_error(context, ret, "Kerberos V5: "
357 "krb5_sock_to_principal failed (%s)\r\n");
361 ret = krb5_rd_req(context,
369 krb5_free_principal (context, server);
374 err_str = krb5_get_error_message(context, ret);
376 "Read req failed: %s", err_str);
377 krb5_free_error_message(context, err_str);
378 Data(ap, KRB_REJECT, errbuf, -1);
380 printf("%s\r\n", errbuf);
391 ret = krb5_verify_authenticator_checksum(context,
400 err_str = krb5_get_error_message(context, ret);
401 asprintf(&errbuf, "Bad checksum: %s", err_str);
402 krb5_free_error_message(context, err_str);
403 Data(ap, KRB_REJECT, errbuf, -1);
405 printf ("%s\r\n", errbuf);
410 ret = krb5_auth_con_getremotesubkey (context,
415 Data(ap, KRB_REJECT, "krb5_auth_con_getremotesubkey failed", -1);
416 auth_finished(ap, AUTH_REJECT);
418 print_krb5_error(context, ret, "Kerberos V5: "
419 "krb5_auth_con_getremotesubkey failed (%s)\r\n");
423 if (key_block == NULL) {
424 ret = krb5_auth_con_getkey(context,
429 Data(ap, KRB_REJECT, "krb5_auth_con_getkey failed", -1);
430 auth_finished(ap, AUTH_REJECT);
432 print_krb5_error(context, ret, "Kerberos V5: "
433 "krb5_auth_con_getkey failed (%s)\r\n");
436 if (key_block == NULL) {
437 Data(ap, KRB_REJECT, "no subkey received", -1);
438 auth_finished(ap, AUTH_REJECT);
440 printf("Kerberos V5: "
441 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
445 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
446 ret = krb5_mk_rep(context, auth_context, &outbuf);
449 "krb5_mk_rep failed", -1);
450 auth_finished(ap, AUTH_REJECT);
452 print_krb5_error(context, ret, "Kerberos V5: "
453 "krb5_mk_rep failed (%s)\r\n");
456 Data(ap, KRB_RESPONSE, outbuf.data, outbuf.length);
458 if (krb5_unparse_name(context, ticket->client, &name))
461 if(UserNameRequested && krb5_kuserok(context,
463 UserNameRequested)) {
464 Data(ap, KRB_ACCEPT, name, name ? -1 : 0);
465 if (auth_debug_mode) {
466 printf("Kerberos5 identifies him as ``%s''\r\n",
470 if(key_block->keytype == ETYPE_DES_CBC_MD5 ||
471 key_block->keytype == ETYPE_DES_CBC_MD4 ||
472 key_block->keytype == ETYPE_DES_CBC_CRC) {
477 skey.data = key_block->keyvalue.data;
478 encrypt_session_key(&skey, 0);
484 asprintf (&msg, "user `%s' is not authorized to "
486 name ? name : "<unknown>",
487 UserNameRequested ? UserNameRequested : "<nobody>");
489 Data(ap, KRB_REJECT, NULL, 0);
491 Data(ap, KRB_REJECT, (void *)msg, -1);
494 auth_finished (ap, AUTH_REJECT);
495 krb5_free_keyblock_contents(context, key_block);
498 auth_finished(ap, AUTH_USER);
499 krb5_free_keyblock_contents(context, key_block);
504 char ccname[1024]; /* XXX */
507 inbuf.data = (char *)data;
510 pwd = getpwnam (UserNameRequested);
514 snprintf (ccname, sizeof(ccname),
515 "FILE:/tmp/krb5cc_%u", pwd->pw_uid);
517 ret = krb5_cc_resolve (context, ccname, &ccache);
520 print_krb5_error(context, ret, "Kerberos V5: could not get ccache: %s\r\n");
524 ret = krb5_cc_initialize (context,
529 print_krb5_error(context, ret, "Kerberos V5: could not init ccache: %s\r\n");
534 esetenv("KRB5CCNAME", ccname, 1);
536 ret = krb5_rd_cred2 (context,
544 err_str = krb5_get_error_message(context, ret);
546 "Read forwarded creds failed: %s", err_str);
547 krb5_free_error_message(context, err_str);
549 Data(ap, KRB_FORWARD_REJECT, NULL, 0);
551 Data(ap, KRB_FORWARD_REJECT, errbuf, -1);
553 printf("Could not read forwarded credentials: %s\r\n",
557 Data(ap, KRB_FORWARD_ACCEPT, 0, 0);
562 chown (ccname + 5, pwd->pw_uid, -1);
564 printf("Forwarded credentials obtained\r\n");
569 printf("Unknown Kerberos option %d\r\n", data[-1]);
570 Data(ap, KRB_REJECT, 0, 0);
576 kerberos5_reply(Authenticator *ap, unsigned char *data, int cnt)
578 static int mutual_complete = 0;
585 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
588 printf("[ Kerberos V5 refuses authentication ]\r\n");
594 krb5_keyblock *keyblock;
596 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
598 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
603 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt, data);
605 printf("[ Kerberos V5 accepts you ]\r\n");
607 ret = krb5_auth_con_getlocalsubkey (context,
611 ret = krb5_auth_con_getkey (context,
615 print_krb5_error(context, ret, "[ krb5_auth_con_getkey: %s ]\r\n");
622 skey.data = keyblock->keyvalue.data;
623 encrypt_session_key(&skey, 0);
624 krb5_free_keyblock_contents (context, keyblock);
625 auth_finished(ap, AUTH_USER);
626 if (forward_flags & OPTS_FORWARD_CREDS)
627 kerberos5_forward(ap);
631 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
632 /* the rest of the reply should contain a krb_ap_rep */
633 krb5_ap_rep_enc_part *reply;
638 inbuf.data = (char *)data;
640 ret = krb5_rd_rep(context, auth_context, &inbuf, &reply);
642 print_krb5_error(context, ret, "[ Mutual authentication failed: %s ]\r\n");
646 krb5_free_ap_rep_enc_part(context, reply);
650 case KRB_FORWARD_ACCEPT:
651 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
653 case KRB_FORWARD_REJECT:
654 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
659 printf("Unknown Kerberos option %d\r\n", data[-1]);
665 kerberos5_status(Authenticator *ap __unused, char *name, int level)
667 if (level < AUTH_USER)
670 if (UserNameRequested &&
671 krb5_kuserok(context,
675 strcpy(name, UserNameRequested);
681 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
682 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
685 kerberos5_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
689 buf[buflen-1] = '\0'; /* make sure its NULL terminated */
693 case KRB_REJECT: /* Rejected (reason might follow) */
694 strlcpy((char *)buf, " REJECT ", buflen);
697 case KRB_ACCEPT: /* Accepted (name might follow) */
698 strlcpy((char *)buf, " ACCEPT ", buflen);
703 ADDC(buf, buflen, '"');
704 for (i = 4; i < cnt; i++)
705 ADDC(buf, buflen, data[i]);
706 ADDC(buf, buflen, '"');
707 ADDC(buf, buflen, '\0');
711 case KRB_AUTH: /* Authentication data follows */
712 strlcpy((char *)buf, " AUTH", buflen);
716 strlcpy((char *)buf, " RESPONSE", buflen);
719 case KRB_FORWARD: /* Forwarded credentials follow */
720 strlcpy((char *)buf, " FORWARD", buflen);
723 case KRB_FORWARD_ACCEPT: /* Forwarded credentials accepted */
724 strlcpy((char *)buf, " FORWARD_ACCEPT", buflen);
727 case KRB_FORWARD_REJECT: /* Forwarded credentials rejected */
728 /* (reason might follow) */
729 strlcpy((char *)buf, " FORWARD_REJECT", buflen);
733 snprintf(buf, buflen, " %d (unknown)", data[3]);
736 for (i = 4; i < cnt; i++) {
737 snprintf(buf, buflen, " %d", data[i]);
745 kerberos5_forward(Authenticator *ap)
750 krb5_kdc_flags flags;
752 krb5_principal principal;
754 ret = krb5_cc_default (context, &ccache);
757 print_krb5_error(context, ret, "KerberosV5: could not get default ccache: %s\r\n");
761 ret = krb5_cc_get_principal (context, ccache, &principal);
764 print_krb5_error(context, ret, "KerberosV5: could not get principal: %s\r\n");
768 memset (&creds, 0, sizeof(creds));
770 creds.client = principal;
772 ret = krb5_build_principal (context,
774 strlen(principal->realm),
782 print_krb5_error(context, ret, "KerberosV5: could not get principal: %s\r\n");
786 creds.times.endtime = 0;
789 flags.b.forwarded = 1;
790 if (forward_flags & OPTS_FORWARDABLE_CREDS)
791 flags.b.forwardable = 1;
793 ret = krb5_get_forwarded_creds (context,
802 print_krb5_error(context, ret, "Kerberos V5: error getting forwarded creds: %s\r\n");
806 if(!Data(ap, KRB_FORWARD, out_data.data, out_data.length)) {
808 printf("Not enough room for authentication data\r\n");
811 printf("Forwarded local Kerberos V5 credentials to server\r\n");
816 /* if this was a K5 authentication try and join a PAG for the user. */
818 kerberos5_dfspag(void)
821 dfspag = krb5_dfs_pag(context, dfsfwd, ticket->client,