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
[4] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0 };
102 #define KRB_AUTH 0 /* Authentication data follows */
103 #define KRB_REJECT 1 /* Rejected (reason might follow) */
104 #define KRB_ACCEPT 2 /* Accepted */
105 #define KRB_RESPONSE 3 /* Response for mutual auth. */
107 #define KRB_FORWARD 4 /* Forwarded credentials follow */
108 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
109 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
111 static krb5_data auth
;
112 static krb5_ticket
*ticket
;
114 static krb5_context context
;
115 static krb5_auth_context auth_context
;
118 Data(Authenticator
*ap
, int type
, void *d
, int c
)
120 unsigned char *cd
= (unsigned char *)d
;
121 unsigned char *p0
, *p
;
122 size_t len
= sizeof(str_data
) + 3 + 2;
126 c
= strlen((char*)cd
);
128 for (p
= cd
; p
- cd
< c
; p
++, len
++)
136 memcpy(p0
, str_data
, sizeof(str_data
));
137 p
= p0
+ sizeof(str_data
);
139 if (auth_debug_mode
) {
140 printf("%s:%d: [%d] (%d)",
141 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
151 if ((*p
++ = *cd
++) == IAC
)
156 if (str_data
[3] == TELQUAL_IS
)
157 printsub('>', &p0
[2], len
- 2);
158 ret
= telnet_net_write(p0
, len
);
164 kerberos5_init(Authenticator
*ap
, int server
)
168 ret
= krb5_init_context(&context
);
173 krb5_kt_cursor cursor
;
175 ret
= krb5_kt_default(context
, &kt
);
179 ret
= krb5_kt_start_seq_get (context
, kt
, &cursor
);
181 krb5_kt_close (context
, kt
);
184 krb5_kt_end_seq_get (context
, kt
, &cursor
);
185 krb5_kt_close (context
, kt
);
187 str_data
[3] = TELQUAL_REPLY
;
189 str_data
[3] = TELQUAL_IS
;
195 kerberos5_send(char *name
, Authenticator
*ap
)
200 krb5_data cksum_data
;
203 if (!UserNameRequested
) {
204 if (auth_debug_mode
) {
205 printf("Kerberos V5: no user name supplied\r\n");
210 ret
= krb5_cc_default(context
, &ccache
);
212 if (auth_debug_mode
) {
213 printf("Kerberos V5: could not get default ccache: %s\r\n",
214 krb5_get_err_text (context
, ret
));
219 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
)
220 ap_opts
= AP_OPTS_MUTUAL_REQUIRED
;
224 ap_opts
|= AP_OPTS_USE_SUBKEY
;
226 ret
= krb5_auth_con_init (context
, &auth_context
);
228 if (auth_debug_mode
) {
229 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
230 krb5_get_err_text(context
, ret
));
235 ret
= krb5_auth_con_setaddrs_from_fd (context
,
239 if (auth_debug_mode
) {
240 printf ("Kerberos V5:"
241 " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
242 krb5_get_err_text(context
, ret
));
247 krb5_auth_con_setkeytype (context
, auth_context
, KEYTYPE_DES
);
252 cksum_data
.length
= sizeof(foo
);
253 cksum_data
.data
= foo
;
257 krb5_principal service
;
261 ret
= krb5_sname_to_principal (context
,
267 if (auth_debug_mode
) {
268 printf ("Kerberos V5:"
269 " krb5_sname_to_principal(%s) failed (%s)\r\n",
270 RemoteHostName
, krb5_get_err_text(context
, ret
));
274 ret
= krb5_unparse_name_fixed(context
, service
, sname
, sizeof(sname
));
276 if (auth_debug_mode
) {
277 printf ("Kerberos V5:"
278 " krb5_unparse_name_fixed failed (%s)\r\n",
279 krb5_get_err_text(context
, ret
));
283 printf("[ Trying %s (%s)... ]\r\n", name
, sname
);
284 ret
= krb5_mk_req_exact(context
, &auth_context
, ap_opts
,
286 &cksum_data
, ccache
, &auth
);
287 krb5_free_principal (context
, service
);
291 if (1 || auth_debug_mode
) {
292 printf("Kerberos V5: mk_req failed (%s)\r\n",
293 krb5_get_err_text(context
, ret
));
298 if (!auth_sendname((unsigned char *)UserNameRequested
,
299 strlen(UserNameRequested
))) {
301 printf("Not enough room for user name\r\n");
304 if (!Data(ap
, KRB_AUTH
, auth
.data
, auth
.length
)) {
306 printf("Not enough room for authentication data\r\n");
309 if (auth_debug_mode
) {
310 printf("Sent Kerberos V5 credentials to server\r\n");
316 kerberos5_send_mutual(Authenticator
*ap
)
318 return kerberos5_send("mutual KERBEROS5", ap
);
322 kerberos5_send_oneway(Authenticator
*ap
)
324 return kerberos5_send("KERBEROS5", ap
);
328 kerberos5_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
332 krb5_keyblock
*key_block
;
334 krb5_principal server
;
341 auth
.data
= (char *)data
;
346 ret
= krb5_auth_con_init (context
, &auth_context
);
348 Data(ap
, KRB_REJECT
, "krb5_auth_con_init failed", -1);
349 auth_finished(ap
, AUTH_REJECT
);
351 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
352 krb5_get_err_text(context
, ret
));
356 ret
= krb5_auth_con_setaddrs_from_fd (context
,
360 Data(ap
, KRB_REJECT
, "krb5_auth_con_setaddrs_from_fd failed", -1);
361 auth_finished(ap
, AUTH_REJECT
);
363 printf("Kerberos V5: "
364 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
365 krb5_get_err_text(context
, ret
));
369 ret
= krb5_sock_to_principal (context
,
375 Data(ap
, KRB_REJECT
, "krb5_sock_to_principal failed", -1);
376 auth_finished(ap
, AUTH_REJECT
);
378 printf("Kerberos V5: "
379 "krb5_sock_to_principal failed (%s)\r\n",
380 krb5_get_err_text(context
, ret
));
384 ret
= krb5_rd_req(context
,
392 krb5_free_principal (context
, server
);
397 "Read req failed: %s",
398 krb5_get_err_text(context
, ret
));
399 Data(ap
, KRB_REJECT
, errbuf
, -1);
401 printf("%s\r\n", errbuf
);
412 ret
= krb5_verify_authenticator_checksum(context
,
419 asprintf(&errbuf
, "Bad checksum: %s",
420 krb5_get_err_text(context
, ret
));
421 Data(ap
, KRB_REJECT
, errbuf
, -1);
423 printf ("%s\r\n", errbuf
);
428 ret
= krb5_auth_con_getremotesubkey (context
,
433 Data(ap
, KRB_REJECT
, "krb5_auth_con_getremotesubkey failed", -1);
434 auth_finished(ap
, AUTH_REJECT
);
436 printf("Kerberos V5: "
437 "krb5_auth_con_getremotesubkey failed (%s)\r\n",
438 krb5_get_err_text(context
, ret
));
442 if (key_block
== NULL
) {
443 ret
= krb5_auth_con_getkey(context
,
448 Data(ap
, KRB_REJECT
, "krb5_auth_con_getkey failed", -1);
449 auth_finished(ap
, AUTH_REJECT
);
451 printf("Kerberos V5: "
452 "krb5_auth_con_getkey failed (%s)\r\n",
453 krb5_get_err_text(context
, ret
));
456 if (key_block
== NULL
) {
457 Data(ap
, KRB_REJECT
, "no subkey received", -1);
458 auth_finished(ap
, AUTH_REJECT
);
460 printf("Kerberos V5: "
461 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
465 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
466 ret
= krb5_mk_rep(context
, auth_context
, &outbuf
);
469 "krb5_mk_rep failed", -1);
470 auth_finished(ap
, AUTH_REJECT
);
472 printf("Kerberos V5: "
473 "krb5_mk_rep failed (%s)\r\n",
474 krb5_get_err_text(context
, ret
));
477 Data(ap
, KRB_RESPONSE
, outbuf
.data
, outbuf
.length
);
479 if (krb5_unparse_name(context
, ticket
->client
, &name
))
482 if(UserNameRequested
&& krb5_kuserok(context
,
484 UserNameRequested
)) {
485 Data(ap
, KRB_ACCEPT
, name
, name
? -1 : 0);
486 if (auth_debug_mode
) {
487 printf("Kerberos5 identifies him as ``%s''\r\n",
491 if(key_block
->keytype
== ETYPE_DES_CBC_MD5
||
492 key_block
->keytype
== ETYPE_DES_CBC_MD4
||
493 key_block
->keytype
== ETYPE_DES_CBC_CRC
) {
498 skey
.data
= key_block
->keyvalue
.data
;
499 encrypt_session_key(&skey
, 0);
505 asprintf (&msg
, "user `%s' is not authorized to "
507 name
? name
: "<unknown>",
508 UserNameRequested
? UserNameRequested
: "<nobody>");
510 Data(ap
, KRB_REJECT
, NULL
, 0);
512 Data(ap
, KRB_REJECT
, (void *)msg
, -1);
515 auth_finished (ap
, AUTH_REJECT
);
516 krb5_free_keyblock_contents(context
, key_block
);
519 auth_finished(ap
, AUTH_USER
);
520 krb5_free_keyblock_contents(context
, key_block
);
525 char ccname
[1024]; /* XXX */
528 inbuf
.data
= (char *)data
;
531 pwd
= getpwnam (UserNameRequested
);
535 snprintf (ccname
, sizeof(ccname
),
536 "FILE:/tmp/krb5cc_%u", pwd
->pw_uid
);
538 ret
= krb5_cc_resolve (context
, ccname
, &ccache
);
541 printf ("Kerberos V5: could not get ccache: %s\r\n",
542 krb5_get_err_text(context
, ret
));
546 ret
= krb5_cc_initialize (context
,
551 printf ("Kerberos V5: could not init ccache: %s\r\n",
552 krb5_get_err_text(context
, ret
));
557 esetenv("KRB5CCNAME", ccname
, 1);
559 ret
= krb5_rd_cred2 (context
,
567 "Read forwarded creds failed: %s",
568 krb5_get_err_text (context
, ret
));
570 Data(ap
, KRB_FORWARD_REJECT
, NULL
, 0);
572 Data(ap
, KRB_FORWARD_REJECT
, errbuf
, -1);
574 printf("Could not read forwarded credentials: %s\r\n",
578 Data(ap
, KRB_FORWARD_ACCEPT
, 0, 0);
583 chown (ccname
+ 5, pwd
->pw_uid
, -1);
585 printf("Forwarded credentials obtained\r\n");
590 printf("Unknown Kerberos option %d\r\n", data
[-1]);
591 Data(ap
, KRB_REJECT
, 0, 0);
597 kerberos5_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
599 static int mutual_complete
= 0;
606 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
609 printf("[ Kerberos V5 refuses authentication ]\r\n");
615 krb5_keyblock
*keyblock
;
617 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
&&
619 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
624 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt
, data
);
626 printf("[ Kerberos V5 accepts you ]\r\n");
628 ret
= krb5_auth_con_getlocalsubkey (context
,
632 ret
= krb5_auth_con_getkey (context
,
636 printf("[ krb5_auth_con_getkey: %s ]\r\n",
637 krb5_get_err_text(context
, ret
));
644 skey
.data
= keyblock
->keyvalue
.data
;
645 encrypt_session_key(&skey
, 0);
646 krb5_free_keyblock_contents (context
, keyblock
);
647 auth_finished(ap
, AUTH_USER
);
648 if (forward_flags
& OPTS_FORWARD_CREDS
)
649 kerberos5_forward(ap
);
653 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
654 /* the rest of the reply should contain a krb_ap_rep */
655 krb5_ap_rep_enc_part
*reply
;
660 inbuf
.data
= (char *)data
;
662 ret
= krb5_rd_rep(context
, auth_context
, &inbuf
, &reply
);
664 printf("[ Mutual authentication failed: %s ]\r\n",
665 krb5_get_err_text (context
, ret
));
669 krb5_free_ap_rep_enc_part(context
, reply
);
673 case KRB_FORWARD_ACCEPT
:
674 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
676 case KRB_FORWARD_REJECT
:
677 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
682 printf("Unknown Kerberos option %d\r\n", data
[-1]);
688 kerberos5_status(Authenticator
*ap
, char *name
, size_t name_sz
, int level
)
690 if (level
< AUTH_USER
)
693 if (UserNameRequested
&&
694 krb5_kuserok(context
,
698 strlcpy(name
, UserNameRequested
, name_sz
);
707 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
708 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
711 kerberos5_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
715 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
719 case KRB_REJECT
: /* Rejected (reason might follow) */
720 strlcpy((char *)buf
, " REJECT ", buflen
);
723 case KRB_ACCEPT
: /* Accepted (name might follow) */
724 strlcpy((char *)buf
, " ACCEPT ", buflen
);
729 ADDC(buf
, buflen
, '"');
730 for (i
= 4; i
< cnt
; i
++)
731 ADDC(buf
, buflen
, data
[i
]);
732 ADDC(buf
, buflen
, '"');
733 ADDC(buf
, buflen
, '\0');
737 case KRB_AUTH
: /* Authentication data follows */
738 strlcpy((char *)buf
, " AUTH", buflen
);
742 strlcpy((char *)buf
, " RESPONSE", buflen
);
745 case KRB_FORWARD
: /* Forwarded credentials follow */
746 strlcpy((char *)buf
, " FORWARD", buflen
);
749 case KRB_FORWARD_ACCEPT
: /* Forwarded credentials accepted */
750 strlcpy((char *)buf
, " FORWARD_ACCEPT", buflen
);
753 case KRB_FORWARD_REJECT
: /* Forwarded credentials rejected */
754 /* (reason might follow) */
755 strlcpy((char *)buf
, " FORWARD_REJECT", buflen
);
759 snprintf((char*)buf
, buflen
, " %d (unknown)", data
[3]);
762 for (i
= 4; i
< cnt
; i
++) {
763 snprintf((char*)buf
, buflen
, " %d", data
[i
]);
771 kerberos5_forward(Authenticator
*ap
)
776 krb5_kdc_flags flags
;
778 krb5_principal principal
;
780 ret
= krb5_cc_default (context
, &ccache
);
783 printf ("KerberosV5: could not get default ccache: %s\r\n",
784 krb5_get_err_text (context
, ret
));
788 ret
= krb5_cc_get_principal (context
, ccache
, &principal
);
791 printf ("KerberosV5: could not get principal: %s\r\n",
792 krb5_get_err_text (context
, ret
));
796 memset (&creds
, 0, sizeof(creds
));
798 creds
.client
= principal
;
800 ret
= krb5_build_principal (context
,
802 strlen(principal
->realm
),
810 printf ("KerberosV5: could not get principal: %s\r\n",
811 krb5_get_err_text (context
, ret
));
815 creds
.times
.endtime
= 0;
818 flags
.b
.forwarded
= 1;
819 if (forward_flags
& OPTS_FORWARDABLE_CREDS
)
820 flags
.b
.forwardable
= 1;
822 ret
= krb5_get_forwarded_creds (context
,
831 printf ("Kerberos V5: error getting forwarded creds: %s\r\n",
832 krb5_get_err_text (context
, ret
));
836 if(!Data(ap
, KRB_FORWARD
, out_data
.data
, out_data
.length
)) {
838 printf("Not enough room for authentication data\r\n");
841 printf("Forwarded local Kerberos V5 credentials to server\r\n");
846 /* if this was a K5 authentication try and join a PAG for the user. */
848 kerberos5_dfspag(void)
851 dfspag
= krb5_dfs_pag(context
, dfsfwd
, ticket
->client
,
858 kerberos5_set_forward(int on
)
861 forward_flags
&= ~OPTS_FORWARD_CREDS
;
863 forward_flags
|= OPTS_FORWARD_CREDS
;
865 forward_flags
^= OPTS_FORWARD_CREDS
;
870 kerberos5_set_forwardable(int on
)
873 forward_flags
&= ~OPTS_FORWARDABLE_CREDS
;
875 forward_flags
|= OPTS_FORWARDABLE_CREDS
;
877 forward_flags
^= OPTS_FORWARDABLE_CREDS
;