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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/crypto/telnet/libtelnet/kerberos5.c,v 1.1.1.1.8.2 2003/04/24 19:13:59 nectar Exp $
33 * Copyright (C) 1990 by the Massachusetts Institute of Technology
35 * Export of this software from the United States of America may
36 * require a specific license from the United States Government.
37 * It is the responsibility of any person or organization contemplating
38 * export to obtain such a license before exporting.
40 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
41 * distribute this software and its documentation for any purpose and
42 * without fee is hereby granted, provided that the above copyright
43 * notice appear in all copies and that both that copyright notice and
44 * this permission notice appear in supporting documentation, and that
45 * the name of M.I.T. not be used in advertising or publicity pertaining
46 * to distribution of the software without specific, written prior
47 * permission. M.I.T. makes no representations about the suitability of
48 * this software for any purpose. It is provided "as is" without express
49 * or implied warranty.
54 #include <arpa/telnet.h>
62 #define Authenticator k5_Authenticator
70 int forward_flags
= 0; /* Flags get set in telnet/main.c on -f and -F */
72 /* These values need to be the same as those defined in telnet/main.c. */
73 /* Either define them in both places, or put in some common header file. */
74 #define OPTS_FORWARD_CREDS 0x00000002
75 #define OPTS_FORWARDABLE_CREDS 0x00000001
77 void kerberos5_forward (Authenticator
*);
79 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
80 AUTHTYPE_KERBEROS_V5
, };
82 #define KRB_AUTH 0 /* Authentication data follows */
83 #define KRB_REJECT 1 /* Rejected (reason might follow) */
84 #define KRB_ACCEPT 2 /* Accepted */
85 #define KRB_RESPONSE 3 /* Response for mutual auth. */
87 #define KRB_FORWARD 4 /* Forwarded credentials follow */
88 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
89 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
91 static krb5_data auth
;
92 static krb5_ticket
*ticket
;
94 static krb5_context context
;
95 static krb5_auth_context auth_context
;
98 Data(Authenticator
*ap
, int type
, const char *d
, int c
)
100 unsigned char *p
= str_data
+ 4;
101 const unsigned char *cd
= d
;
106 if (auth_debug_mode
) {
107 printf("%s:%d: [%d] (%d)",
108 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
118 if ((*p
++ = *cd
++) == IAC
)
123 if (str_data
[3] == TELQUAL_IS
)
124 printsub('>', &str_data
[2], p
- &str_data
[2]);
125 return(net_write(str_data
, p
- str_data
));
129 kerberos5_init(Authenticator
*ap __unused
, int server
)
133 ret
= krb5_init_context(&context
);
138 krb5_kt_cursor cursor
;
140 ret
= krb5_kt_default(context
, &kt
);
144 ret
= krb5_kt_start_seq_get (context
, kt
, &cursor
);
146 krb5_kt_close (context
, kt
);
149 krb5_kt_end_seq_get (context
, kt
, &cursor
);
150 krb5_kt_close (context
, kt
);
152 str_data
[3] = TELQUAL_REPLY
;
154 str_data
[3] = TELQUAL_IS
;
161 kerberos5_send(const char *name
, Authenticator
*ap
)
166 krb5_data cksum_data
;
169 if (!UserNameRequested
) {
170 if (auth_debug_mode
) {
171 printf("Kerberos V5: no user name supplied\r\n");
176 ret
= krb5_cc_default(context
, &ccache
);
178 if (auth_debug_mode
) {
179 printf("Kerberos V5: could not get default ccache: %s\r\n",
180 krb5_get_err_text (context
, ret
));
185 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
)
186 ap_opts
= AP_OPTS_MUTUAL_REQUIRED
;
189 ap_opts
|= AP_OPTS_USE_SUBKEY
;
191 ret
= krb5_auth_con_init (context
, &auth_context
);
193 if (auth_debug_mode
) {
194 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
195 krb5_get_err_text(context
, ret
));
200 ret
= krb5_auth_con_setaddrs_from_fd (context
,
204 if (auth_debug_mode
) {
205 printf ("Kerberos V5:"
206 " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
207 krb5_get_err_text(context
, ret
));
212 krb5_auth_con_setkeytype (context
, auth_context
, KEYTYPE_DES
);
217 cksum_data
.length
= sizeof(foo
);
218 cksum_data
.data
= foo
;
222 krb5_principal service
;
226 ret
= krb5_sname_to_principal (context
,
232 if (auth_debug_mode
) {
233 printf ("Kerberos V5:"
234 " krb5_sname_to_principal(%s) failed (%s)\r\n",
235 RemoteHostName
, krb5_get_err_text(context
, ret
));
239 ret
= krb5_unparse_name_fixed(context
, service
, sname
, sizeof(sname
));
241 if (auth_debug_mode
) {
242 printf ("Kerberos V5:"
243 " krb5_unparse_name_fixed failed (%s)\r\n",
244 krb5_get_err_text(context
, ret
));
248 printf("[ Trying %s (%s)... ]\r\n", name
, sname
);
249 ret
= krb5_mk_req_exact(context
, &auth_context
, ap_opts
,
251 &cksum_data
, ccache
, &auth
);
252 krb5_free_principal (context
, service
);
256 if (1 || auth_debug_mode
) {
257 printf("Kerberos V5: mk_req failed (%s)\r\n",
258 krb5_get_err_text(context
, ret
));
263 if (!auth_sendname((unsigned char *)UserNameRequested
,
264 strlen(UserNameRequested
))) {
266 printf("Not enough room for user name\r\n");
269 if (!Data(ap
, KRB_AUTH
, auth
.data
, auth
.length
)) {
271 printf("Not enough room for authentication data\r\n");
274 if (auth_debug_mode
) {
275 printf("Sent Kerberos V5 credentials to server\r\n");
281 kerberos5_send_mutual(Authenticator
*ap
)
283 return kerberos5_send("mutual KERBEROS5", ap
);
287 kerberos5_send_oneway(Authenticator
*ap
)
289 return kerberos5_send("KERBEROS5", ap
);
293 kerberos5_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
297 krb5_keyblock
*key_block
;
299 krb5_principal server
;
306 auth
.data
= (char *)data
;
311 ret
= krb5_auth_con_init (context
, &auth_context
);
313 Data(ap
, KRB_REJECT
, "krb5_auth_con_init failed", -1);
314 auth_finished(ap
, AUTH_REJECT
);
316 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
317 krb5_get_err_text(context
, ret
));
321 ret
= krb5_auth_con_setaddrs_from_fd (context
,
325 Data(ap
, KRB_REJECT
, "krb5_auth_con_setaddrs_from_fd failed", -1);
326 auth_finished(ap
, AUTH_REJECT
);
328 printf("Kerberos V5: "
329 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
330 krb5_get_err_text(context
, ret
));
334 ret
= krb5_sock_to_principal (context
,
340 Data(ap
, KRB_REJECT
, "krb5_sock_to_principal failed", -1);
341 auth_finished(ap
, AUTH_REJECT
);
343 printf("Kerberos V5: "
344 "krb5_sock_to_principal failed (%s)\r\n",
345 krb5_get_err_text(context
, ret
));
349 ret
= krb5_rd_req(context
,
357 krb5_free_principal (context
, server
);
362 "Read req failed: %s",
363 krb5_get_err_text(context
, ret
));
364 Data(ap
, KRB_REJECT
, errbuf
, -1);
366 printf("%s\r\n", errbuf
);
377 ret
= krb5_verify_authenticator_checksum(context
,
384 asprintf(&errbuf
, "Bad checksum: %s",
385 krb5_get_err_text(context
, ret
));
386 Data(ap
, KRB_REJECT
, errbuf
, -1);
388 printf ("%s\r\n", errbuf
);
393 ret
= krb5_auth_con_getremotesubkey (context
,
398 Data(ap
, KRB_REJECT
, "krb5_auth_con_getremotesubkey failed", -1);
399 auth_finished(ap
, AUTH_REJECT
);
401 printf("Kerberos V5: "
402 "krb5_auth_con_getremotesubkey failed (%s)\r\n",
403 krb5_get_err_text(context
, ret
));
407 if (key_block
== NULL
) {
408 ret
= krb5_auth_con_getkey(context
,
413 Data(ap
, KRB_REJECT
, "krb5_auth_con_getkey failed", -1);
414 auth_finished(ap
, AUTH_REJECT
);
416 printf("Kerberos V5: "
417 "krb5_auth_con_getkey failed (%s)\r\n",
418 krb5_get_err_text(context
, ret
));
421 if (key_block
== NULL
) {
422 Data(ap
, KRB_REJECT
, "no subkey received", -1);
423 auth_finished(ap
, AUTH_REJECT
);
425 printf("Kerberos V5: "
426 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
430 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
431 ret
= krb5_mk_rep(context
, auth_context
, &outbuf
);
434 "krb5_mk_rep failed", -1);
435 auth_finished(ap
, AUTH_REJECT
);
437 printf("Kerberos V5: "
438 "krb5_mk_rep failed (%s)\r\n",
439 krb5_get_err_text(context
, ret
));
442 Data(ap
, KRB_RESPONSE
, outbuf
.data
, outbuf
.length
);
444 if (krb5_unparse_name(context
, ticket
->client
, &name
))
447 if(UserNameRequested
&& krb5_kuserok(context
,
449 UserNameRequested
)) {
450 Data(ap
, KRB_ACCEPT
, name
, name
? -1 : 0);
451 if (auth_debug_mode
) {
452 printf("Kerberos5 identifies him as ``%s''\r\n",
456 if(key_block
->keytype
== ETYPE_DES_CBC_MD5
||
457 key_block
->keytype
== ETYPE_DES_CBC_MD4
||
458 key_block
->keytype
== ETYPE_DES_CBC_CRC
) {
463 skey
.data
= key_block
->keyvalue
.data
;
464 encrypt_session_key(&skey
, 0);
470 asprintf (&msg
, "user `%s' is not authorized to "
472 name
? name
: "<unknown>",
473 UserNameRequested
? UserNameRequested
: "<nobody>");
475 Data(ap
, KRB_REJECT
, NULL
, 0);
477 Data(ap
, KRB_REJECT
, (void *)msg
, -1);
480 auth_finished (ap
, AUTH_REJECT
);
481 krb5_free_keyblock_contents(context
, key_block
);
484 auth_finished(ap
, AUTH_USER
);
485 krb5_free_keyblock_contents(context
, key_block
);
490 char ccname
[1024]; /* XXX */
493 inbuf
.data
= (char *)data
;
496 pwd
= getpwnam (UserNameRequested
);
500 snprintf (ccname
, sizeof(ccname
),
501 "FILE:/tmp/krb5cc_%u", pwd
->pw_uid
);
503 ret
= krb5_cc_resolve (context
, ccname
, &ccache
);
506 printf ("Kerberos V5: could not get ccache: %s\r\n",
507 krb5_get_err_text(context
, ret
));
511 ret
= krb5_cc_initialize (context
,
516 printf ("Kerberos V5: could not init ccache: %s\r\n",
517 krb5_get_err_text(context
, ret
));
522 esetenv("KRB5CCNAME", ccname
, 1);
524 ret
= krb5_rd_cred2 (context
,
532 "Read forwarded creds failed: %s",
533 krb5_get_err_text (context
, ret
));
535 Data(ap
, KRB_FORWARD_REJECT
, NULL
, 0);
537 Data(ap
, KRB_FORWARD_REJECT
, errbuf
, -1);
539 printf("Could not read forwarded credentials: %s\r\n",
543 Data(ap
, KRB_FORWARD_ACCEPT
, 0, 0);
548 chown (ccname
+ 5, pwd
->pw_uid
, -1);
550 printf("Forwarded credentials obtained\r\n");
555 printf("Unknown Kerberos option %d\r\n", data
[-1]);
556 Data(ap
, KRB_REJECT
, 0, 0);
562 kerberos5_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
564 static int mutual_complete
= 0;
571 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
574 printf("[ Kerberos V5 refuses authentication ]\r\n");
580 krb5_keyblock
*keyblock
;
582 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
&&
584 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
589 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt
, data
);
591 printf("[ Kerberos V5 accepts you ]\r\n");
593 ret
= krb5_auth_con_getlocalsubkey (context
,
597 ret
= krb5_auth_con_getkey (context
,
601 printf("[ krb5_auth_con_getkey: %s ]\r\n",
602 krb5_get_err_text(context
, ret
));
609 skey
.data
= keyblock
->keyvalue
.data
;
610 encrypt_session_key(&skey
, 0);
611 krb5_free_keyblock_contents (context
, keyblock
);
612 auth_finished(ap
, AUTH_USER
);
613 if (forward_flags
& OPTS_FORWARD_CREDS
)
614 kerberos5_forward(ap
);
618 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
619 /* the rest of the reply should contain a krb_ap_rep */
620 krb5_ap_rep_enc_part
*reply
;
625 inbuf
.data
= (char *)data
;
627 ret
= krb5_rd_rep(context
, auth_context
, &inbuf
, &reply
);
629 printf("[ Mutual authentication failed: %s ]\r\n",
630 krb5_get_err_text (context
, ret
));
634 krb5_free_ap_rep_enc_part(context
, reply
);
638 case KRB_FORWARD_ACCEPT
:
639 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
641 case KRB_FORWARD_REJECT
:
642 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
647 printf("Unknown Kerberos option %d\r\n", data
[-1]);
653 kerberos5_status(Authenticator
*ap __unused
, char *name
, int level
)
655 if (level
< AUTH_USER
)
658 if (UserNameRequested
&&
659 krb5_kuserok(context
,
663 strcpy(name
, UserNameRequested
);
669 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
670 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
673 kerberos5_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
677 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
681 case KRB_REJECT
: /* Rejected (reason might follow) */
682 strlcpy((char *)buf
, " REJECT ", buflen
);
685 case KRB_ACCEPT
: /* Accepted (name might follow) */
686 strlcpy((char *)buf
, " ACCEPT ", buflen
);
691 ADDC(buf
, buflen
, '"');
692 for (i
= 4; i
< cnt
; i
++)
693 ADDC(buf
, buflen
, data
[i
]);
694 ADDC(buf
, buflen
, '"');
695 ADDC(buf
, buflen
, '\0');
699 case KRB_AUTH
: /* Authentication data follows */
700 strlcpy((char *)buf
, " AUTH", buflen
);
704 strlcpy((char *)buf
, " RESPONSE", buflen
);
707 case KRB_FORWARD
: /* Forwarded credentials follow */
708 strlcpy((char *)buf
, " FORWARD", buflen
);
711 case KRB_FORWARD_ACCEPT
: /* Forwarded credentials accepted */
712 strlcpy((char *)buf
, " FORWARD_ACCEPT", buflen
);
715 case KRB_FORWARD_REJECT
: /* Forwarded credentials rejected */
716 /* (reason might follow) */
717 strlcpy((char *)buf
, " FORWARD_REJECT", buflen
);
721 snprintf(buf
, buflen
, " %d (unknown)", data
[3]);
724 for (i
= 4; i
< cnt
; i
++) {
725 snprintf(buf
, buflen
, " %d", data
[i
]);
733 kerberos5_forward(Authenticator
*ap
)
738 krb5_kdc_flags flags
;
740 krb5_principal principal
;
742 ret
= krb5_cc_default (context
, &ccache
);
745 printf ("KerberosV5: could not get default ccache: %s\r\n",
746 krb5_get_err_text (context
, ret
));
750 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
753 printf ("KerberosV5: could not get principal: %s\r\n",
754 krb5_get_err_text (context
, ret
));
758 memset (&creds
, 0, sizeof(creds
));
760 creds
.client
= principal
;
762 ret
= krb5_build_principal (context
,
764 strlen(principal
->realm
),
772 printf ("KerberosV5: could not get principal: %s\r\n",
773 krb5_get_err_text (context
, ret
));
777 creds
.times
.endtime
= 0;
780 flags
.b
.forwarded
= 1;
781 if (forward_flags
& OPTS_FORWARDABLE_CREDS
)
782 flags
.b
.forwardable
= 1;
784 ret
= krb5_get_forwarded_creds (context
,
793 printf ("Kerberos V5: error getting forwarded creds: %s\r\n",
794 krb5_get_err_text (context
, ret
));
798 if(!Data(ap
, KRB_FORWARD
, out_data
.data
, out_data
.length
)) {
800 printf("Not enough room for authentication data\r\n");
803 printf("Forwarded local Kerberos V5 credentials to server\r\n");
808 /* if this was a K5 authentication try and join a PAG for the user. */
810 kerberos5_dfspag(void)
813 dfspag
= krb5_dfs_pag(context
, dfsfwd
, ticket
->client
,