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.
60 #include <arpa/telnet.h>
68 #define Authenticator k5_Authenticator
87 int forward_flags
= 0; /* Flags get set in telnet/main.c on -f and -F */
92 /* These values need to be the same as those defined in telnet/main.c. */
93 /* Either define them in both places, or put in some common header file. */
94 #define OPTS_FORWARD_CREDS 0x00000002
95 #define OPTS_FORWARDABLE_CREDS 0x00000001
98 void kerberos5_forward (Authenticator
*);
100 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
101 AUTHTYPE_KERBEROS_V5
, };
103 #define KRB_AUTH 0 /* Authentication data follows */
104 #define KRB_REJECT 1 /* Rejected (reason might follow) */
105 #define KRB_ACCEPT 2 /* Accepted */
106 #define KRB_RESPONSE 3 /* Response for mutual auth. */
108 #define KRB_FORWARD 4 /* Forwarded credentials follow */
109 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
110 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
112 static krb5_data auth
;
113 static krb5_ticket
*ticket
;
115 static krb5_context context
;
116 static krb5_auth_context auth_context
;
119 Data(Authenticator
*ap
, int type
, void *d
, int c
)
121 unsigned char *p
= str_data
+ 4;
122 unsigned char *cd
= (unsigned char *)d
;
125 c
= strlen((char*)cd
);
127 if (auth_debug_mode
) {
128 printf("%s:%d: [%d] (%d)",
129 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
139 if ((*p
++ = *cd
++) == IAC
)
144 if (str_data
[3] == TELQUAL_IS
)
145 printsub('>', &str_data
[2], p
- &str_data
[2]);
146 return(telnet_net_write(str_data
, p
- str_data
));
150 kerberos5_init(Authenticator
*ap
, int server
)
154 ret
= krb5_init_context(&context
);
159 krb5_kt_cursor cursor
;
161 ret
= krb5_kt_default(context
, &kt
);
165 ret
= krb5_kt_start_seq_get (context
, kt
, &cursor
);
167 krb5_kt_close (context
, kt
);
170 krb5_kt_end_seq_get (context
, kt
, &cursor
);
171 krb5_kt_close (context
, kt
);
173 str_data
[3] = TELQUAL_REPLY
;
175 str_data
[3] = TELQUAL_IS
;
181 kerberos5_send(char *name
, Authenticator
*ap
)
186 krb5_data cksum_data
;
189 if (!UserNameRequested
) {
190 if (auth_debug_mode
) {
191 printf("Kerberos V5: no user name supplied\r\n");
196 ret
= krb5_cc_default(context
, &ccache
);
198 if (auth_debug_mode
) {
199 printf("Kerberos V5: could not get default ccache: %s\r\n",
200 krb5_get_err_text (context
, ret
));
205 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
)
206 ap_opts
= AP_OPTS_MUTUAL_REQUIRED
;
210 ap_opts
|= AP_OPTS_USE_SUBKEY
;
212 ret
= krb5_auth_con_init (context
, &auth_context
);
214 if (auth_debug_mode
) {
215 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
216 krb5_get_err_text(context
, ret
));
221 ret
= krb5_auth_con_setaddrs_from_fd (context
,
225 if (auth_debug_mode
) {
226 printf ("Kerberos V5:"
227 " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
228 krb5_get_err_text(context
, ret
));
233 krb5_auth_con_setkeytype (context
, auth_context
, KEYTYPE_DES
);
238 cksum_data
.length
= sizeof(foo
);
239 cksum_data
.data
= foo
;
243 krb5_principal service
;
247 ret
= krb5_sname_to_principal (context
,
253 if (auth_debug_mode
) {
254 printf ("Kerberos V5:"
255 " krb5_sname_to_principal(%s) failed (%s)\r\n",
256 RemoteHostName
, krb5_get_err_text(context
, ret
));
260 ret
= krb5_unparse_name_fixed(context
, service
, sname
, sizeof(sname
));
262 if (auth_debug_mode
) {
263 printf ("Kerberos V5:"
264 " krb5_unparse_name_fixed failed (%s)\r\n",
265 krb5_get_err_text(context
, ret
));
269 printf("[ Trying %s (%s)... ]\r\n", name
, sname
);
270 ret
= krb5_mk_req_exact(context
, &auth_context
, ap_opts
,
272 &cksum_data
, ccache
, &auth
);
273 krb5_free_principal (context
, service
);
277 if (1 || auth_debug_mode
) {
278 printf("Kerberos V5: mk_req failed (%s)\r\n",
279 krb5_get_err_text(context
, ret
));
284 if (!auth_sendname((unsigned char *)UserNameRequested
,
285 strlen(UserNameRequested
))) {
287 printf("Not enough room for user name\r\n");
290 if (!Data(ap
, KRB_AUTH
, auth
.data
, auth
.length
)) {
292 printf("Not enough room for authentication data\r\n");
295 if (auth_debug_mode
) {
296 printf("Sent Kerberos V5 credentials to server\r\n");
302 kerberos5_send_mutual(Authenticator
*ap
)
304 return kerberos5_send("mutual KERBEROS5", ap
);
308 kerberos5_send_oneway(Authenticator
*ap
)
310 return kerberos5_send("KERBEROS5", ap
);
314 kerberos5_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
318 krb5_keyblock
*key_block
;
320 krb5_principal server
;
327 auth
.data
= (char *)data
;
332 ret
= krb5_auth_con_init (context
, &auth_context
);
334 Data(ap
, KRB_REJECT
, "krb5_auth_con_init failed", -1);
335 auth_finished(ap
, AUTH_REJECT
);
337 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
338 krb5_get_err_text(context
, ret
));
342 ret
= krb5_auth_con_setaddrs_from_fd (context
,
346 Data(ap
, KRB_REJECT
, "krb5_auth_con_setaddrs_from_fd failed", -1);
347 auth_finished(ap
, AUTH_REJECT
);
349 printf("Kerberos V5: "
350 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
351 krb5_get_err_text(context
, ret
));
355 ret
= krb5_sock_to_principal (context
,
361 Data(ap
, KRB_REJECT
, "krb5_sock_to_principal failed", -1);
362 auth_finished(ap
, AUTH_REJECT
);
364 printf("Kerberos V5: "
365 "krb5_sock_to_principal failed (%s)\r\n",
366 krb5_get_err_text(context
, ret
));
370 ret
= krb5_rd_req(context
,
378 krb5_free_principal (context
, server
);
383 "Read req failed: %s",
384 krb5_get_err_text(context
, ret
));
385 Data(ap
, KRB_REJECT
, errbuf
, -1);
387 printf("%s\r\n", errbuf
);
398 ret
= krb5_verify_authenticator_checksum(context
,
405 asprintf(&errbuf
, "Bad checksum: %s",
406 krb5_get_err_text(context
, ret
));
407 Data(ap
, KRB_REJECT
, errbuf
, -1);
409 printf ("%s\r\n", errbuf
);
414 ret
= krb5_auth_con_getremotesubkey (context
,
419 Data(ap
, KRB_REJECT
, "krb5_auth_con_getremotesubkey failed", -1);
420 auth_finished(ap
, AUTH_REJECT
);
422 printf("Kerberos V5: "
423 "krb5_auth_con_getremotesubkey failed (%s)\r\n",
424 krb5_get_err_text(context
, ret
));
428 if (key_block
== NULL
) {
429 ret
= krb5_auth_con_getkey(context
,
434 Data(ap
, KRB_REJECT
, "krb5_auth_con_getkey failed", -1);
435 auth_finished(ap
, AUTH_REJECT
);
437 printf("Kerberos V5: "
438 "krb5_auth_con_getkey failed (%s)\r\n",
439 krb5_get_err_text(context
, ret
));
442 if (key_block
== NULL
) {
443 Data(ap
, KRB_REJECT
, "no subkey received", -1);
444 auth_finished(ap
, AUTH_REJECT
);
446 printf("Kerberos V5: "
447 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
451 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
452 ret
= krb5_mk_rep(context
, auth_context
, &outbuf
);
455 "krb5_mk_rep failed", -1);
456 auth_finished(ap
, AUTH_REJECT
);
458 printf("Kerberos V5: "
459 "krb5_mk_rep failed (%s)\r\n",
460 krb5_get_err_text(context
, ret
));
463 Data(ap
, KRB_RESPONSE
, outbuf
.data
, outbuf
.length
);
465 if (krb5_unparse_name(context
, ticket
->client
, &name
))
468 if(UserNameRequested
&& krb5_kuserok(context
,
470 UserNameRequested
)) {
471 Data(ap
, KRB_ACCEPT
, name
, name
? -1 : 0);
472 if (auth_debug_mode
) {
473 printf("Kerberos5 identifies him as ``%s''\r\n",
477 if(key_block
->keytype
== ETYPE_DES_CBC_MD5
||
478 key_block
->keytype
== ETYPE_DES_CBC_MD4
||
479 key_block
->keytype
== ETYPE_DES_CBC_CRC
) {
484 skey
.data
= key_block
->keyvalue
.data
;
485 encrypt_session_key(&skey
, 0);
491 asprintf (&msg
, "user `%s' is not authorized to "
493 name
? name
: "<unknown>",
494 UserNameRequested
? UserNameRequested
: "<nobody>");
496 Data(ap
, KRB_REJECT
, NULL
, 0);
498 Data(ap
, KRB_REJECT
, (void *)msg
, -1);
501 auth_finished (ap
, AUTH_REJECT
);
502 krb5_free_keyblock_contents(context
, key_block
);
505 auth_finished(ap
, AUTH_USER
);
506 krb5_free_keyblock_contents(context
, key_block
);
511 char ccname
[1024]; /* XXX */
514 inbuf
.data
= (char *)data
;
517 pwd
= getpwnam (UserNameRequested
);
521 snprintf (ccname
, sizeof(ccname
),
522 "FILE:/tmp/krb5cc_%u", pwd
->pw_uid
);
524 ret
= krb5_cc_resolve (context
, ccname
, &ccache
);
527 printf ("Kerberos V5: could not get ccache: %s\r\n",
528 krb5_get_err_text(context
, ret
));
532 ret
= krb5_cc_initialize (context
,
537 printf ("Kerberos V5: could not init ccache: %s\r\n",
538 krb5_get_err_text(context
, ret
));
543 esetenv("KRB5CCNAME", ccname
, 1);
545 ret
= krb5_rd_cred2 (context
,
553 "Read forwarded creds failed: %s",
554 krb5_get_err_text (context
, ret
));
556 Data(ap
, KRB_FORWARD_REJECT
, NULL
, 0);
558 Data(ap
, KRB_FORWARD_REJECT
, errbuf
, -1);
560 printf("Could not read forwarded credentials: %s\r\n",
564 Data(ap
, KRB_FORWARD_ACCEPT
, 0, 0);
569 chown (ccname
+ 5, pwd
->pw_uid
, -1);
571 printf("Forwarded credentials obtained\r\n");
576 printf("Unknown Kerberos option %d\r\n", data
[-1]);
577 Data(ap
, KRB_REJECT
, 0, 0);
583 kerberos5_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
585 static int mutual_complete
= 0;
592 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
595 printf("[ Kerberos V5 refuses authentication ]\r\n");
601 krb5_keyblock
*keyblock
;
603 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
&&
605 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
610 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt
, data
);
612 printf("[ Kerberos V5 accepts you ]\r\n");
614 ret
= krb5_auth_con_getlocalsubkey (context
,
618 ret
= krb5_auth_con_getkey (context
,
622 printf("[ krb5_auth_con_getkey: %s ]\r\n",
623 krb5_get_err_text(context
, ret
));
630 skey
.data
= keyblock
->keyvalue
.data
;
631 encrypt_session_key(&skey
, 0);
632 krb5_free_keyblock_contents (context
, keyblock
);
633 auth_finished(ap
, AUTH_USER
);
634 if (forward_flags
& OPTS_FORWARD_CREDS
)
635 kerberos5_forward(ap
);
639 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
640 /* the rest of the reply should contain a krb_ap_rep */
641 krb5_ap_rep_enc_part
*reply
;
646 inbuf
.data
= (char *)data
;
648 ret
= krb5_rd_rep(context
, auth_context
, &inbuf
, &reply
);
650 printf("[ Mutual authentication failed: %s ]\r\n",
651 krb5_get_err_text (context
, ret
));
655 krb5_free_ap_rep_enc_part(context
, reply
);
659 case KRB_FORWARD_ACCEPT
:
660 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
662 case KRB_FORWARD_REJECT
:
663 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
668 printf("Unknown Kerberos option %d\r\n", data
[-1]);
674 kerberos5_status(Authenticator
*ap
, char *name
, size_t name_sz
, int level
)
676 if (level
< AUTH_USER
)
679 if (UserNameRequested
&&
680 krb5_kuserok(context
,
684 strlcpy(name
, UserNameRequested
, name_sz
);
693 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
694 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
697 kerberos5_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
701 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
705 case KRB_REJECT
: /* Rejected (reason might follow) */
706 strlcpy((char *)buf
, " REJECT ", buflen
);
709 case KRB_ACCEPT
: /* Accepted (name might follow) */
710 strlcpy((char *)buf
, " ACCEPT ", buflen
);
715 ADDC(buf
, buflen
, '"');
716 for (i
= 4; i
< cnt
; i
++)
717 ADDC(buf
, buflen
, data
[i
]);
718 ADDC(buf
, buflen
, '"');
719 ADDC(buf
, buflen
, '\0');
723 case KRB_AUTH
: /* Authentication data follows */
724 strlcpy((char *)buf
, " AUTH", buflen
);
728 strlcpy((char *)buf
, " RESPONSE", buflen
);
731 case KRB_FORWARD
: /* Forwarded credentials follow */
732 strlcpy((char *)buf
, " FORWARD", buflen
);
735 case KRB_FORWARD_ACCEPT
: /* Forwarded credentials accepted */
736 strlcpy((char *)buf
, " FORWARD_ACCEPT", buflen
);
739 case KRB_FORWARD_REJECT
: /* Forwarded credentials rejected */
740 /* (reason might follow) */
741 strlcpy((char *)buf
, " FORWARD_REJECT", buflen
);
745 snprintf((char*)buf
, buflen
, " %d (unknown)", data
[3]);
748 for (i
= 4; i
< cnt
; i
++) {
749 snprintf((char*)buf
, buflen
, " %d", data
[i
]);
757 kerberos5_forward(Authenticator
*ap
)
762 krb5_kdc_flags flags
;
764 krb5_principal principal
;
766 ret
= krb5_cc_default (context
, &ccache
);
769 printf ("KerberosV5: could not get default ccache: %s\r\n",
770 krb5_get_err_text (context
, ret
));
774 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
777 printf ("KerberosV5: could not get principal: %s\r\n",
778 krb5_get_err_text (context
, ret
));
782 memset (&creds
, 0, sizeof(creds
));
784 creds
.client
= principal
;
786 ret
= krb5_build_principal (context
,
788 strlen(principal
->realm
),
796 printf ("KerberosV5: could not get principal: %s\r\n",
797 krb5_get_err_text (context
, ret
));
801 creds
.times
.endtime
= 0;
804 flags
.b
.forwarded
= 1;
805 if (forward_flags
& OPTS_FORWARDABLE_CREDS
)
806 flags
.b
.forwardable
= 1;
808 ret
= krb5_get_forwarded_creds (context
,
817 printf ("Kerberos V5: error getting forwarded creds: %s\r\n",
818 krb5_get_err_text (context
, ret
));
822 if(!Data(ap
, KRB_FORWARD
, out_data
.data
, out_data
.length
)) {
824 printf("Not enough room for authentication data\r\n");
827 printf("Forwarded local Kerberos V5 credentials to server\r\n");
832 /* if this was a K5 authentication try and join a PAG for the user. */
834 kerberos5_dfspag(void)
837 dfspag
= krb5_dfs_pag(context
, dfsfwd
, ticket
->client
,
844 kerberos5_set_forward(int on
)
847 forward_flags
&= ~OPTS_FORWARD_CREDS
;
849 forward_flags
|= OPTS_FORWARD_CREDS
;
851 forward_flags
^= OPTS_FORWARD_CREDS
;
856 kerberos5_set_forwardable(int on
)
859 forward_flags
&= ~OPTS_FORWARDABLE_CREDS
;
861 forward_flags
|= OPTS_FORWARDABLE_CREDS
;
863 forward_flags
^= OPTS_FORWARDABLE_CREDS
;