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
33 * $FreeBSD: src/crypto/telnet/libtelnet/kerberos5.c,v 1.1.1.1.8.2 2003/04/24 19:13:59 nectar Exp $
34 * $DragonFly: src/crypto/telnet/libtelnet/kerberos5.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
38 * Copyright (C) 1990 by the Massachusetts Institute of Technology
40 * Export of this software from the United States of America may
41 * require a specific license from the United States Government.
42 * It is the responsibility of any person or organization contemplating
43 * export to obtain such a license before exporting.
45 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
46 * distribute this software and its documentation for any purpose and
47 * without fee is hereby granted, provided that the above copyright
48 * notice appear in all copies and that both that copyright notice and
49 * this permission notice appear in supporting documentation, and that
50 * the name of M.I.T. not be used in advertising or publicity pertaining
51 * to distribution of the software without specific, written prior
52 * permission. M.I.T. makes no representations about the suitability of
53 * this software for any purpose. It is provided "as is" without express
54 * or implied warranty.
59 #include <arpa/telnet.h>
67 #define Authenticator k5_Authenticator
75 int forward_flags
= 0; /* Flags get set in telnet/main.c on -f and -F */
77 /* These values need to be the same as those defined in telnet/main.c. */
78 /* Either define them in both places, or put in some common header file. */
79 #define OPTS_FORWARD_CREDS 0x00000002
80 #define OPTS_FORWARDABLE_CREDS 0x00000001
82 void kerberos5_forward (Authenticator
*);
84 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
85 AUTHTYPE_KERBEROS_V5
, };
87 #define KRB_AUTH 0 /* Authentication data follows */
88 #define KRB_REJECT 1 /* Rejected (reason might follow) */
89 #define KRB_ACCEPT 2 /* Accepted */
90 #define KRB_RESPONSE 3 /* Response for mutual auth. */
92 #define KRB_FORWARD 4 /* Forwarded credentials follow */
93 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
94 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
96 static krb5_data auth
;
97 static krb5_ticket
*ticket
;
99 static krb5_context context
;
100 static krb5_auth_context auth_context
;
103 Data(Authenticator
*ap
, int type
, const char *d
, int c
)
105 unsigned char *p
= str_data
+ 4;
106 const unsigned char *cd
= d
;
111 if (auth_debug_mode
) {
112 printf("%s:%d: [%d] (%d)",
113 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
123 if ((*p
++ = *cd
++) == IAC
)
128 if (str_data
[3] == TELQUAL_IS
)
129 printsub('>', &str_data
[2], p
- &str_data
[2]);
130 return(net_write(str_data
, p
- str_data
));
134 kerberos5_init(Authenticator
*ap __unused
, int server
)
138 ret
= krb5_init_context(&context
);
143 krb5_kt_cursor cursor
;
145 ret
= krb5_kt_default(context
, &kt
);
149 ret
= krb5_kt_start_seq_get (context
, kt
, &cursor
);
151 krb5_kt_close (context
, kt
);
154 krb5_kt_end_seq_get (context
, kt
, &cursor
);
155 krb5_kt_close (context
, kt
);
157 str_data
[3] = TELQUAL_REPLY
;
159 str_data
[3] = TELQUAL_IS
;
166 kerberos5_send(const char *name
, Authenticator
*ap
)
171 krb5_data cksum_data
;
174 if (!UserNameRequested
) {
175 if (auth_debug_mode
) {
176 printf("Kerberos V5: no user name supplied\r\n");
181 ret
= krb5_cc_default(context
, &ccache
);
183 if (auth_debug_mode
) {
184 printf("Kerberos V5: could not get default ccache: %s\r\n",
185 krb5_get_err_text (context
, ret
));
190 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
)
191 ap_opts
= AP_OPTS_MUTUAL_REQUIRED
;
194 ap_opts
|= AP_OPTS_USE_SUBKEY
;
196 ret
= krb5_auth_con_init (context
, &auth_context
);
198 if (auth_debug_mode
) {
199 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
200 krb5_get_err_text(context
, ret
));
205 ret
= krb5_auth_con_setaddrs_from_fd (context
,
209 if (auth_debug_mode
) {
210 printf ("Kerberos V5:"
211 " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
212 krb5_get_err_text(context
, ret
));
217 krb5_auth_con_setkeytype (context
, auth_context
, KEYTYPE_DES
);
222 cksum_data
.length
= sizeof(foo
);
223 cksum_data
.data
= foo
;
227 krb5_principal service
;
231 ret
= krb5_sname_to_principal (context
,
237 if (auth_debug_mode
) {
238 printf ("Kerberos V5:"
239 " krb5_sname_to_principal(%s) failed (%s)\r\n",
240 RemoteHostName
, krb5_get_err_text(context
, ret
));
244 ret
= krb5_unparse_name_fixed(context
, service
, sname
, sizeof(sname
));
246 if (auth_debug_mode
) {
247 printf ("Kerberos V5:"
248 " krb5_unparse_name_fixed failed (%s)\r\n",
249 krb5_get_err_text(context
, ret
));
253 printf("[ Trying %s (%s)... ]\r\n", name
, sname
);
254 ret
= krb5_mk_req_exact(context
, &auth_context
, ap_opts
,
256 &cksum_data
, ccache
, &auth
);
257 krb5_free_principal (context
, service
);
261 if (1 || auth_debug_mode
) {
262 printf("Kerberos V5: mk_req failed (%s)\r\n",
263 krb5_get_err_text(context
, ret
));
268 if (!auth_sendname((unsigned char *)UserNameRequested
,
269 strlen(UserNameRequested
))) {
271 printf("Not enough room for user name\r\n");
274 if (!Data(ap
, KRB_AUTH
, auth
.data
, auth
.length
)) {
276 printf("Not enough room for authentication data\r\n");
279 if (auth_debug_mode
) {
280 printf("Sent Kerberos V5 credentials to server\r\n");
286 kerberos5_send_mutual(Authenticator
*ap
)
288 return kerberos5_send("mutual KERBEROS5", ap
);
292 kerberos5_send_oneway(Authenticator
*ap
)
294 return kerberos5_send("KERBEROS5", ap
);
298 kerberos5_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
302 krb5_keyblock
*key_block
;
304 krb5_principal server
;
311 auth
.data
= (char *)data
;
316 ret
= krb5_auth_con_init (context
, &auth_context
);
318 Data(ap
, KRB_REJECT
, "krb5_auth_con_init failed", -1);
319 auth_finished(ap
, AUTH_REJECT
);
321 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
322 krb5_get_err_text(context
, ret
));
326 ret
= krb5_auth_con_setaddrs_from_fd (context
,
330 Data(ap
, KRB_REJECT
, "krb5_auth_con_setaddrs_from_fd failed", -1);
331 auth_finished(ap
, AUTH_REJECT
);
333 printf("Kerberos V5: "
334 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
335 krb5_get_err_text(context
, ret
));
339 ret
= krb5_sock_to_principal (context
,
345 Data(ap
, KRB_REJECT
, "krb5_sock_to_principal failed", -1);
346 auth_finished(ap
, AUTH_REJECT
);
348 printf("Kerberos V5: "
349 "krb5_sock_to_principal failed (%s)\r\n",
350 krb5_get_err_text(context
, ret
));
354 ret
= krb5_rd_req(context
,
362 krb5_free_principal (context
, server
);
367 "Read req failed: %s",
368 krb5_get_err_text(context
, ret
));
369 Data(ap
, KRB_REJECT
, errbuf
, -1);
371 printf("%s\r\n", errbuf
);
382 ret
= krb5_verify_authenticator_checksum(context
,
389 asprintf(&errbuf
, "Bad checksum: %s",
390 krb5_get_err_text(context
, ret
));
391 Data(ap
, KRB_REJECT
, errbuf
, -1);
393 printf ("%s\r\n", errbuf
);
398 ret
= krb5_auth_con_getremotesubkey (context
,
403 Data(ap
, KRB_REJECT
, "krb5_auth_con_getremotesubkey failed", -1);
404 auth_finished(ap
, AUTH_REJECT
);
406 printf("Kerberos V5: "
407 "krb5_auth_con_getremotesubkey failed (%s)\r\n",
408 krb5_get_err_text(context
, ret
));
412 if (key_block
== NULL
) {
413 ret
= krb5_auth_con_getkey(context
,
418 Data(ap
, KRB_REJECT
, "krb5_auth_con_getkey failed", -1);
419 auth_finished(ap
, AUTH_REJECT
);
421 printf("Kerberos V5: "
422 "krb5_auth_con_getkey failed (%s)\r\n",
423 krb5_get_err_text(context
, ret
));
426 if (key_block
== NULL
) {
427 Data(ap
, KRB_REJECT
, "no subkey received", -1);
428 auth_finished(ap
, AUTH_REJECT
);
430 printf("Kerberos V5: "
431 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
435 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
436 ret
= krb5_mk_rep(context
, auth_context
, &outbuf
);
439 "krb5_mk_rep failed", -1);
440 auth_finished(ap
, AUTH_REJECT
);
442 printf("Kerberos V5: "
443 "krb5_mk_rep failed (%s)\r\n",
444 krb5_get_err_text(context
, ret
));
447 Data(ap
, KRB_RESPONSE
, outbuf
.data
, outbuf
.length
);
449 if (krb5_unparse_name(context
, ticket
->client
, &name
))
452 if(UserNameRequested
&& krb5_kuserok(context
,
454 UserNameRequested
)) {
455 Data(ap
, KRB_ACCEPT
, name
, name
? -1 : 0);
456 if (auth_debug_mode
) {
457 printf("Kerberos5 identifies him as ``%s''\r\n",
461 if(key_block
->keytype
== ETYPE_DES_CBC_MD5
||
462 key_block
->keytype
== ETYPE_DES_CBC_MD4
||
463 key_block
->keytype
== ETYPE_DES_CBC_CRC
) {
468 skey
.data
= key_block
->keyvalue
.data
;
469 encrypt_session_key(&skey
, 0);
475 asprintf (&msg
, "user `%s' is not authorized to "
477 name
? name
: "<unknown>",
478 UserNameRequested
? UserNameRequested
: "<nobody>");
480 Data(ap
, KRB_REJECT
, NULL
, 0);
482 Data(ap
, KRB_REJECT
, (void *)msg
, -1);
485 auth_finished (ap
, AUTH_REJECT
);
486 krb5_free_keyblock_contents(context
, key_block
);
489 auth_finished(ap
, AUTH_USER
);
490 krb5_free_keyblock_contents(context
, key_block
);
495 char ccname
[1024]; /* XXX */
498 inbuf
.data
= (char *)data
;
501 pwd
= getpwnam (UserNameRequested
);
505 snprintf (ccname
, sizeof(ccname
),
506 "FILE:/tmp/krb5cc_%u", pwd
->pw_uid
);
508 ret
= krb5_cc_resolve (context
, ccname
, &ccache
);
511 printf ("Kerberos V5: could not get ccache: %s\r\n",
512 krb5_get_err_text(context
, ret
));
516 ret
= krb5_cc_initialize (context
,
521 printf ("Kerberos V5: could not init ccache: %s\r\n",
522 krb5_get_err_text(context
, ret
));
527 esetenv("KRB5CCNAME", ccname
, 1);
529 ret
= krb5_rd_cred2 (context
,
537 "Read forwarded creds failed: %s",
538 krb5_get_err_text (context
, ret
));
540 Data(ap
, KRB_FORWARD_REJECT
, NULL
, 0);
542 Data(ap
, KRB_FORWARD_REJECT
, errbuf
, -1);
544 printf("Could not read forwarded credentials: %s\r\n",
548 Data(ap
, KRB_FORWARD_ACCEPT
, 0, 0);
553 chown (ccname
+ 5, pwd
->pw_uid
, -1);
555 printf("Forwarded credentials obtained\r\n");
560 printf("Unknown Kerberos option %d\r\n", data
[-1]);
561 Data(ap
, KRB_REJECT
, 0, 0);
567 kerberos5_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
569 static int mutual_complete
= 0;
576 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
579 printf("[ Kerberos V5 refuses authentication ]\r\n");
585 krb5_keyblock
*keyblock
;
587 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
&&
589 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
594 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt
, data
);
596 printf("[ Kerberos V5 accepts you ]\r\n");
598 ret
= krb5_auth_con_getlocalsubkey (context
,
602 ret
= krb5_auth_con_getkey (context
,
606 printf("[ krb5_auth_con_getkey: %s ]\r\n",
607 krb5_get_err_text(context
, ret
));
614 skey
.data
= keyblock
->keyvalue
.data
;
615 encrypt_session_key(&skey
, 0);
616 krb5_free_keyblock_contents (context
, keyblock
);
617 auth_finished(ap
, AUTH_USER
);
618 if (forward_flags
& OPTS_FORWARD_CREDS
)
619 kerberos5_forward(ap
);
623 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
624 /* the rest of the reply should contain a krb_ap_rep */
625 krb5_ap_rep_enc_part
*reply
;
630 inbuf
.data
= (char *)data
;
632 ret
= krb5_rd_rep(context
, auth_context
, &inbuf
, &reply
);
634 printf("[ Mutual authentication failed: %s ]\r\n",
635 krb5_get_err_text (context
, ret
));
639 krb5_free_ap_rep_enc_part(context
, reply
);
643 case KRB_FORWARD_ACCEPT
:
644 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
646 case KRB_FORWARD_REJECT
:
647 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
652 printf("Unknown Kerberos option %d\r\n", data
[-1]);
658 kerberos5_status(Authenticator
*ap __unused
, char *name
, int level
)
660 if (level
< AUTH_USER
)
663 if (UserNameRequested
&&
664 krb5_kuserok(context
,
668 strcpy(name
, UserNameRequested
);
674 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
675 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
678 kerberos5_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
682 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
686 case KRB_REJECT
: /* Rejected (reason might follow) */
687 strlcpy((char *)buf
, " REJECT ", buflen
);
690 case KRB_ACCEPT
: /* Accepted (name might follow) */
691 strlcpy((char *)buf
, " ACCEPT ", buflen
);
696 ADDC(buf
, buflen
, '"');
697 for (i
= 4; i
< cnt
; i
++)
698 ADDC(buf
, buflen
, data
[i
]);
699 ADDC(buf
, buflen
, '"');
700 ADDC(buf
, buflen
, '\0');
704 case KRB_AUTH
: /* Authentication data follows */
705 strlcpy((char *)buf
, " AUTH", buflen
);
709 strlcpy((char *)buf
, " RESPONSE", buflen
);
712 case KRB_FORWARD
: /* Forwarded credentials follow */
713 strlcpy((char *)buf
, " FORWARD", buflen
);
716 case KRB_FORWARD_ACCEPT
: /* Forwarded credentials accepted */
717 strlcpy((char *)buf
, " FORWARD_ACCEPT", buflen
);
720 case KRB_FORWARD_REJECT
: /* Forwarded credentials rejected */
721 /* (reason might follow) */
722 strlcpy((char *)buf
, " FORWARD_REJECT", buflen
);
726 snprintf(buf
, buflen
, " %d (unknown)", data
[3]);
729 for (i
= 4; i
< cnt
; i
++) {
730 snprintf(buf
, buflen
, " %d", data
[i
]);
738 kerberos5_forward(Authenticator
*ap
)
743 krb5_kdc_flags flags
;
745 krb5_principal principal
;
747 ret
= krb5_cc_default (context
, &ccache
);
750 printf ("KerberosV5: could not get default ccache: %s\r\n",
751 krb5_get_err_text (context
, ret
));
755 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
758 printf ("KerberosV5: could not get principal: %s\r\n",
759 krb5_get_err_text (context
, ret
));
763 memset (&creds
, 0, sizeof(creds
));
765 creds
.client
= principal
;
767 ret
= krb5_build_principal (context
,
769 strlen(principal
->realm
),
777 printf ("KerberosV5: could not get principal: %s\r\n",
778 krb5_get_err_text (context
, ret
));
782 creds
.times
.endtime
= 0;
785 flags
.b
.forwarded
= 1;
786 if (forward_flags
& OPTS_FORWARDABLE_CREDS
)
787 flags
.b
.forwardable
= 1;
789 ret
= krb5_get_forwarded_creds (context
,
798 printf ("Kerberos V5: error getting forwarded creds: %s\r\n",
799 krb5_get_err_text (context
, ret
));
803 if(!Data(ap
, KRB_FORWARD
, out_data
.data
, out_data
.length
)) {
805 printf("Not enough room for authentication data\r\n");
808 printf("Forwarded local Kerberos V5 credentials to server\r\n");
813 /* if this was a K5 authentication try and join a PAG for the user. */
815 kerberos5_dfspag(void)
818 dfspag
= krb5_dfs_pag(context
, dfsfwd
, ticket
->client
,