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 $
37 * Copyright (C) 1990 by the Massachusetts Institute of Technology
39 * Export of this software from the United States of America may
40 * require a specific license from the United States Government.
41 * It is the responsibility of any person or organization contemplating
42 * export to obtain such a license before exporting.
44 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
45 * distribute this software and its documentation for any purpose and
46 * without fee is hereby granted, provided that the above copyright
47 * notice appear in all copies and that both that copyright notice and
48 * this permission notice appear in supporting documentation, and that
49 * the name of M.I.T. not be used in advertising or publicity pertaining
50 * to distribution of the software without specific, written prior
51 * permission. M.I.T. makes no representations about the suitability of
52 * this software for any purpose. It is provided "as is" without express
53 * or implied warranty.
58 #include <arpa/telnet.h>
66 #define Authenticator k5_Authenticator
74 int forward_flags
= 0; /* Flags get set in telnet/main.c on -f and -F */
76 /* These values need to be the same as those defined in telnet/main.c. */
77 /* Either define them in both places, or put in some common header file. */
78 #define OPTS_FORWARD_CREDS 0x00000002
79 #define OPTS_FORWARDABLE_CREDS 0x00000001
81 void kerberos5_forward (Authenticator
*);
83 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
84 AUTHTYPE_KERBEROS_V5
, };
86 #define KRB_AUTH 0 /* Authentication data follows */
87 #define KRB_REJECT 1 /* Rejected (reason might follow) */
88 #define KRB_ACCEPT 2 /* Accepted */
89 #define KRB_RESPONSE 3 /* Response for mutual auth. */
91 #define KRB_FORWARD 4 /* Forwarded credentials follow */
92 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
93 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
95 static krb5_data auth
;
96 static krb5_ticket
*ticket
;
98 static krb5_context context
;
99 static krb5_auth_context auth_context
;
102 Data(Authenticator
*ap
, int type
, const char *d
, int c
)
104 unsigned char *p
= str_data
+ 4;
105 const unsigned char *cd
= d
;
110 if (auth_debug_mode
) {
111 printf("%s:%d: [%d] (%d)",
112 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
122 if ((*p
++ = *cd
++) == IAC
)
127 if (str_data
[3] == TELQUAL_IS
)
128 printsub('>', &str_data
[2], p
- &str_data
[2]);
129 return(net_write(str_data
, p
- str_data
));
133 kerberos5_init(Authenticator
*ap __unused
, int server
)
137 ret
= krb5_init_context(&context
);
142 krb5_kt_cursor cursor
;
144 ret
= krb5_kt_default(context
, &kt
);
148 ret
= krb5_kt_start_seq_get (context
, kt
, &cursor
);
150 krb5_kt_close (context
, kt
);
153 krb5_kt_end_seq_get (context
, kt
, &cursor
);
154 krb5_kt_close (context
, kt
);
156 str_data
[3] = TELQUAL_REPLY
;
158 str_data
[3] = TELQUAL_IS
;
165 kerberos5_send(const char *name
, Authenticator
*ap
)
170 krb5_data cksum_data
;
173 if (!UserNameRequested
) {
174 if (auth_debug_mode
) {
175 printf("Kerberos V5: no user name supplied\r\n");
180 ret
= krb5_cc_default(context
, &ccache
);
182 if (auth_debug_mode
) {
183 printf("Kerberos V5: could not get default ccache: %s\r\n",
184 krb5_get_err_text (context
, ret
));
189 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
)
190 ap_opts
= AP_OPTS_MUTUAL_REQUIRED
;
193 ap_opts
|= AP_OPTS_USE_SUBKEY
;
195 ret
= krb5_auth_con_init (context
, &auth_context
);
197 if (auth_debug_mode
) {
198 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
199 krb5_get_err_text(context
, ret
));
204 ret
= krb5_auth_con_setaddrs_from_fd (context
,
208 if (auth_debug_mode
) {
209 printf ("Kerberos V5:"
210 " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
211 krb5_get_err_text(context
, ret
));
216 krb5_auth_con_setkeytype (context
, auth_context
, KEYTYPE_DES
);
221 cksum_data
.length
= sizeof(foo
);
222 cksum_data
.data
= foo
;
226 krb5_principal service
;
230 ret
= krb5_sname_to_principal (context
,
236 if (auth_debug_mode
) {
237 printf ("Kerberos V5:"
238 " krb5_sname_to_principal(%s) failed (%s)\r\n",
239 RemoteHostName
, krb5_get_err_text(context
, ret
));
243 ret
= krb5_unparse_name_fixed(context
, service
, sname
, sizeof(sname
));
245 if (auth_debug_mode
) {
246 printf ("Kerberos V5:"
247 " krb5_unparse_name_fixed failed (%s)\r\n",
248 krb5_get_err_text(context
, ret
));
252 printf("[ Trying %s (%s)... ]\r\n", name
, sname
);
253 ret
= krb5_mk_req_exact(context
, &auth_context
, ap_opts
,
255 &cksum_data
, ccache
, &auth
);
256 krb5_free_principal (context
, service
);
260 if (1 || auth_debug_mode
) {
261 printf("Kerberos V5: mk_req failed (%s)\r\n",
262 krb5_get_err_text(context
, ret
));
267 if (!auth_sendname((unsigned char *)UserNameRequested
,
268 strlen(UserNameRequested
))) {
270 printf("Not enough room for user name\r\n");
273 if (!Data(ap
, KRB_AUTH
, auth
.data
, auth
.length
)) {
275 printf("Not enough room for authentication data\r\n");
278 if (auth_debug_mode
) {
279 printf("Sent Kerberos V5 credentials to server\r\n");
285 kerberos5_send_mutual(Authenticator
*ap
)
287 return kerberos5_send("mutual KERBEROS5", ap
);
291 kerberos5_send_oneway(Authenticator
*ap
)
293 return kerberos5_send("KERBEROS5", ap
);
297 kerberos5_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
301 krb5_keyblock
*key_block
;
303 krb5_principal server
;
310 auth
.data
= (char *)data
;
315 ret
= krb5_auth_con_init (context
, &auth_context
);
317 Data(ap
, KRB_REJECT
, "krb5_auth_con_init failed", -1);
318 auth_finished(ap
, AUTH_REJECT
);
320 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
321 krb5_get_err_text(context
, ret
));
325 ret
= krb5_auth_con_setaddrs_from_fd (context
,
329 Data(ap
, KRB_REJECT
, "krb5_auth_con_setaddrs_from_fd failed", -1);
330 auth_finished(ap
, AUTH_REJECT
);
332 printf("Kerberos V5: "
333 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
334 krb5_get_err_text(context
, ret
));
338 ret
= krb5_sock_to_principal (context
,
344 Data(ap
, KRB_REJECT
, "krb5_sock_to_principal failed", -1);
345 auth_finished(ap
, AUTH_REJECT
);
347 printf("Kerberos V5: "
348 "krb5_sock_to_principal failed (%s)\r\n",
349 krb5_get_err_text(context
, ret
));
353 ret
= krb5_rd_req(context
,
361 krb5_free_principal (context
, server
);
366 "Read req failed: %s",
367 krb5_get_err_text(context
, ret
));
368 Data(ap
, KRB_REJECT
, errbuf
, -1);
370 printf("%s\r\n", errbuf
);
381 ret
= krb5_verify_authenticator_checksum(context
,
388 asprintf(&errbuf
, "Bad checksum: %s",
389 krb5_get_err_text(context
, ret
));
390 Data(ap
, KRB_REJECT
, errbuf
, -1);
392 printf ("%s\r\n", errbuf
);
397 ret
= krb5_auth_con_getremotesubkey (context
,
402 Data(ap
, KRB_REJECT
, "krb5_auth_con_getremotesubkey failed", -1);
403 auth_finished(ap
, AUTH_REJECT
);
405 printf("Kerberos V5: "
406 "krb5_auth_con_getremotesubkey failed (%s)\r\n",
407 krb5_get_err_text(context
, ret
));
411 if (key_block
== NULL
) {
412 ret
= krb5_auth_con_getkey(context
,
417 Data(ap
, KRB_REJECT
, "krb5_auth_con_getkey failed", -1);
418 auth_finished(ap
, AUTH_REJECT
);
420 printf("Kerberos V5: "
421 "krb5_auth_con_getkey failed (%s)\r\n",
422 krb5_get_err_text(context
, ret
));
425 if (key_block
== NULL
) {
426 Data(ap
, KRB_REJECT
, "no subkey received", -1);
427 auth_finished(ap
, AUTH_REJECT
);
429 printf("Kerberos V5: "
430 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
434 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
435 ret
= krb5_mk_rep(context
, auth_context
, &outbuf
);
438 "krb5_mk_rep failed", -1);
439 auth_finished(ap
, AUTH_REJECT
);
441 printf("Kerberos V5: "
442 "krb5_mk_rep failed (%s)\r\n",
443 krb5_get_err_text(context
, ret
));
446 Data(ap
, KRB_RESPONSE
, outbuf
.data
, outbuf
.length
);
448 if (krb5_unparse_name(context
, ticket
->client
, &name
))
451 if(UserNameRequested
&& krb5_kuserok(context
,
453 UserNameRequested
)) {
454 Data(ap
, KRB_ACCEPT
, name
, name
? -1 : 0);
455 if (auth_debug_mode
) {
456 printf("Kerberos5 identifies him as ``%s''\r\n",
460 if(key_block
->keytype
== ETYPE_DES_CBC_MD5
||
461 key_block
->keytype
== ETYPE_DES_CBC_MD4
||
462 key_block
->keytype
== ETYPE_DES_CBC_CRC
) {
467 skey
.data
= key_block
->keyvalue
.data
;
468 encrypt_session_key(&skey
, 0);
474 asprintf (&msg
, "user `%s' is not authorized to "
476 name
? name
: "<unknown>",
477 UserNameRequested
? UserNameRequested
: "<nobody>");
479 Data(ap
, KRB_REJECT
, NULL
, 0);
481 Data(ap
, KRB_REJECT
, (void *)msg
, -1);
484 auth_finished (ap
, AUTH_REJECT
);
485 krb5_free_keyblock_contents(context
, key_block
);
488 auth_finished(ap
, AUTH_USER
);
489 krb5_free_keyblock_contents(context
, key_block
);
494 char ccname
[1024]; /* XXX */
497 inbuf
.data
= (char *)data
;
500 pwd
= getpwnam (UserNameRequested
);
504 snprintf (ccname
, sizeof(ccname
),
505 "FILE:/tmp/krb5cc_%u", pwd
->pw_uid
);
507 ret
= krb5_cc_resolve (context
, ccname
, &ccache
);
510 printf ("Kerberos V5: could not get ccache: %s\r\n",
511 krb5_get_err_text(context
, ret
));
515 ret
= krb5_cc_initialize (context
,
520 printf ("Kerberos V5: could not init ccache: %s\r\n",
521 krb5_get_err_text(context
, ret
));
526 esetenv("KRB5CCNAME", ccname
, 1);
528 ret
= krb5_rd_cred2 (context
,
536 "Read forwarded creds failed: %s",
537 krb5_get_err_text (context
, ret
));
539 Data(ap
, KRB_FORWARD_REJECT
, NULL
, 0);
541 Data(ap
, KRB_FORWARD_REJECT
, errbuf
, -1);
543 printf("Could not read forwarded credentials: %s\r\n",
547 Data(ap
, KRB_FORWARD_ACCEPT
, 0, 0);
552 chown (ccname
+ 5, pwd
->pw_uid
, -1);
554 printf("Forwarded credentials obtained\r\n");
559 printf("Unknown Kerberos option %d\r\n", data
[-1]);
560 Data(ap
, KRB_REJECT
, 0, 0);
566 kerberos5_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
568 static int mutual_complete
= 0;
575 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
578 printf("[ Kerberos V5 refuses authentication ]\r\n");
584 krb5_keyblock
*keyblock
;
586 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
&&
588 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
593 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt
, data
);
595 printf("[ Kerberos V5 accepts you ]\r\n");
597 ret
= krb5_auth_con_getlocalsubkey (context
,
601 ret
= krb5_auth_con_getkey (context
,
605 printf("[ krb5_auth_con_getkey: %s ]\r\n",
606 krb5_get_err_text(context
, ret
));
613 skey
.data
= keyblock
->keyvalue
.data
;
614 encrypt_session_key(&skey
, 0);
615 krb5_free_keyblock_contents (context
, keyblock
);
616 auth_finished(ap
, AUTH_USER
);
617 if (forward_flags
& OPTS_FORWARD_CREDS
)
618 kerberos5_forward(ap
);
622 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
623 /* the rest of the reply should contain a krb_ap_rep */
624 krb5_ap_rep_enc_part
*reply
;
629 inbuf
.data
= (char *)data
;
631 ret
= krb5_rd_rep(context
, auth_context
, &inbuf
, &reply
);
633 printf("[ Mutual authentication failed: %s ]\r\n",
634 krb5_get_err_text (context
, ret
));
638 krb5_free_ap_rep_enc_part(context
, reply
);
642 case KRB_FORWARD_ACCEPT
:
643 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
645 case KRB_FORWARD_REJECT
:
646 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
651 printf("Unknown Kerberos option %d\r\n", data
[-1]);
657 kerberos5_status(Authenticator
*ap __unused
, char *name
, int level
)
659 if (level
< AUTH_USER
)
662 if (UserNameRequested
&&
663 krb5_kuserok(context
,
667 strcpy(name
, UserNameRequested
);
673 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
674 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
677 kerberos5_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
681 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
685 case KRB_REJECT
: /* Rejected (reason might follow) */
686 strlcpy((char *)buf
, " REJECT ", buflen
);
689 case KRB_ACCEPT
: /* Accepted (name might follow) */
690 strlcpy((char *)buf
, " ACCEPT ", buflen
);
695 ADDC(buf
, buflen
, '"');
696 for (i
= 4; i
< cnt
; i
++)
697 ADDC(buf
, buflen
, data
[i
]);
698 ADDC(buf
, buflen
, '"');
699 ADDC(buf
, buflen
, '\0');
703 case KRB_AUTH
: /* Authentication data follows */
704 strlcpy((char *)buf
, " AUTH", buflen
);
708 strlcpy((char *)buf
, " RESPONSE", buflen
);
711 case KRB_FORWARD
: /* Forwarded credentials follow */
712 strlcpy((char *)buf
, " FORWARD", buflen
);
715 case KRB_FORWARD_ACCEPT
: /* Forwarded credentials accepted */
716 strlcpy((char *)buf
, " FORWARD_ACCEPT", buflen
);
719 case KRB_FORWARD_REJECT
: /* Forwarded credentials rejected */
720 /* (reason might follow) */
721 strlcpy((char *)buf
, " FORWARD_REJECT", buflen
);
725 snprintf(buf
, buflen
, " %d (unknown)", data
[3]);
728 for (i
= 4; i
< cnt
; i
++) {
729 snprintf(buf
, buflen
, " %d", data
[i
]);
737 kerberos5_forward(Authenticator
*ap
)
742 krb5_kdc_flags flags
;
744 krb5_principal principal
;
746 ret
= krb5_cc_default (context
, &ccache
);
749 printf ("KerberosV5: could not get default ccache: %s\r\n",
750 krb5_get_err_text (context
, ret
));
754 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
757 printf ("KerberosV5: could not get principal: %s\r\n",
758 krb5_get_err_text (context
, ret
));
762 memset (&creds
, 0, sizeof(creds
));
764 creds
.client
= principal
;
766 ret
= krb5_build_principal (context
,
768 strlen(principal
->realm
),
776 printf ("KerberosV5: could not get principal: %s\r\n",
777 krb5_get_err_text (context
, ret
));
781 creds
.times
.endtime
= 0;
784 flags
.b
.forwarded
= 1;
785 if (forward_flags
& OPTS_FORWARDABLE_CREDS
)
786 flags
.b
.forwardable
= 1;
788 ret
= krb5_get_forwarded_creds (context
,
797 printf ("Kerberos V5: error getting forwarded creds: %s\r\n",
798 krb5_get_err_text (context
, ret
));
802 if(!Data(ap
, KRB_FORWARD
, out_data
.data
, out_data
.length
)) {
804 printf("Not enough room for authentication data\r\n");
807 printf("Forwarded local Kerberos V5 credentials to server\r\n");
812 /* if this was a K5 authentication try and join a PAG for the user. */
814 kerberos5_dfspag(void)
817 dfspag
= krb5_dfs_pag(context
, dfsfwd
, ticket
->client
,