1 /* $NetBSD: kerberos5.c,v 1.20 2014/04/26 22:10:40 joerg Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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.
53 #include <arpa/telnet.h>
61 #define Authenticator k5_Authenticator
64 /* #include <roken.h> */
72 int forward_flags
; /* Flags get set in telnet/main.c on -f and -F */
73 int got_forwarded_creds
;/* Tell telnetd to pass -F or -f to login. */
75 int require_hwpreauth
;
77 const char *get_krb5_err_text(krb5_context
, krb5_error_code
);
78 void kerberos5_forward(Authenticator
*);
80 static unsigned char str_data
[1024] = {IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
81 AUTHTYPE_KERBEROS_V5
,};
83 #define KRB_AUTH 0 /* Authentication data follows */
84 #define KRB_REJECT 1 /* Rejected (reason might follow) */
85 #define KRB_ACCEPT 2 /* Accepted */
86 #define KRB_RESPONSE 3 /* Response for mutual auth. */
88 #define KRB_FORWARD 4 /* Forwarded credentials follow */
89 #define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */
90 #define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */
92 static krb5_data auth
;
93 static krb5_ticket
*ticket
;
95 krb5_context telnet_context
;
96 static krb5_auth_context auth_context
;
99 Data(Authenticator
*ap
, int type
, const void *d
, int c
)
101 unsigned char *p
= str_data
+ 4;
102 const unsigned char *cd
= (const unsigned char *) d
;
107 if (auth_debug_mode
) {
108 printf("%s:%d: [%d] (%d)",
109 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
119 if ((*p
++ = *cd
++) == IAC
)
124 if (str_data
[3] == TELQUAL_IS
)
125 printsub('>', &str_data
[2], p
- &str_data
[2]);
126 return (telnet_net_write(str_data
, p
- str_data
));
130 get_krb5_err_text(krb5_context ctx
, krb5_error_code ret
)
132 static const char *str
= NULL
;
135 krb5_free_error_message(ctx
, str
);
137 str
= krb5_get_error_message(ctx
, ret
);
146 kerberos5_init(Authenticator
*ap
, int server
)
150 if (telnet_context
== 0) {
151 ret
= krb5_init_context(&telnet_context
);
158 krb5_kt_cursor cursor
;
160 ret
= krb5_kt_default(telnet_context
, &kt
);
164 ret
= krb5_kt_start_seq_get(telnet_context
, kt
, &cursor
);
166 krb5_kt_close(telnet_context
, kt
);
169 krb5_kt_end_seq_get(telnet_context
, kt
, &cursor
);
170 krb5_kt_close(telnet_context
, kt
);
172 str_data
[3] = TELQUAL_REPLY
;
174 str_data
[3] = TELQUAL_IS
;
179 kerberos5_send(Authenticator
*ap
)
184 krb5_data cksum_data
;
187 printf("[ Trying KERBEROS5 ... ]\r\n");
189 if (!UserNameRequested
) {
190 if (auth_debug_mode
) {
191 printf("Kerberos V5: no user name supplied\r\n");
195 ret
= krb5_cc_default(telnet_context
, &ccache
);
197 if (auth_debug_mode
) {
199 "Kerberos V5: could not get default ccache: %s\r\n",
200 get_krb5_err_text(telnet_context
, ret
));
204 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
)
205 ap_opts
= AP_OPTS_MUTUAL_REQUIRED
;
209 ap_opts
|= AP_OPTS_USE_SUBKEY
;
211 ret
= krb5_auth_con_init(telnet_context
, &auth_context
);
213 if (auth_debug_mode
) {
215 "Kerberos V5: krb5_auth_con_init failed: %s\r\n",
216 get_krb5_err_text(telnet_context
, ret
));
220 ret
= krb5_auth_con_setaddrs_from_fd(telnet_context
,
223 if (auth_debug_mode
) {
224 printf("Kerberos V5: "
225 "krb5_auth_con_setaddrs_from_fd failed: %s\r\n",
226 get_krb5_err_text(telnet_context
, ret
));
230 krb5_auth_con_setkeytype(telnet_context
, auth_context
,
236 cksum_data
.length
= sizeof(foo
);
237 cksum_data
.data
= foo
;
238 ret
= krb5_mk_req(telnet_context
, &auth_context
, ap_opts
, "host",
239 RemoteHostName
, &cksum_data
, ccache
, &auth
);
241 if (1 || auth_debug_mode
) {
242 printf("Kerberos V5: mk_req failed (%s)\r\n",
243 get_krb5_err_text(telnet_context
, ret
));
248 if (!auth_sendname((unsigned char *) UserNameRequested
,
249 strlen(UserNameRequested
))) {
251 printf("Not enough room for user name\r\n");
254 if (!Data(ap
, KRB_AUTH
, auth
.data
, auth
.length
)) {
256 printf("Not enough room for authentication data\r\n");
259 if (auth_debug_mode
) {
260 printf("Sent Kerberos V5 credentials to server\r\n");
266 kerberos5_is(Authenticator
* ap
, unsigned char *data
, int cnt
)
270 krb5_keyblock
*key_block
;
272 krb5_principal server
;
279 auth
.data
= (char *) data
;
284 ret
= krb5_auth_con_init(telnet_context
, &auth_context
);
286 Data(ap
, KRB_REJECT
, "krb5_auth_con_init failed", -1);
287 auth_finished(ap
, AUTH_REJECT
);
289 printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
290 get_krb5_err_text(telnet_context
, ret
));
293 ret
= krb5_auth_con_setaddrs_from_fd(telnet_context
,
294 auth_context
, &zero
);
296 Data(ap
, KRB_REJECT
, "krb5_auth_con_setaddrs_from_fd failed", -1);
297 auth_finished(ap
, AUTH_REJECT
);
299 printf("Kerberos V5: "
300 "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
301 get_krb5_err_text(telnet_context
, ret
));
304 ret
= krb5_sock_to_principal(telnet_context
, 0, "host",
305 KRB5_NT_SRV_HST
, &server
);
307 Data(ap
, KRB_REJECT
, "krb5_sock_to_principal failed", -1);
308 auth_finished(ap
, AUTH_REJECT
);
310 printf("Kerberos V5: "
311 "krb5_sock_to_principal failed (%s)\r\n",
312 get_krb5_err_text(telnet_context
, ret
));
315 ret
= krb5_rd_req(telnet_context
, &auth_context
, &auth
,
316 server
, NULL
, NULL
, &ticket
);
317 krb5_free_principal(telnet_context
, server
);
323 "Read req failed: %s",
324 get_krb5_err_text(telnet_context
, ret
));
325 Data(ap
, KRB_REJECT
, errbuf
, -1);
327 printf("%s\r\n", errbuf
);
336 ret
= krb5_verify_authenticator_checksum(telnet_context
,
337 auth_context
, foo
, sizeof(foo
));
341 asprintf(&errbuf
, "Bad checksum: %s",
342 get_krb5_err_text(telnet_context
, ret
));
343 Data(ap
, KRB_REJECT
, errbuf
, -1);
345 printf("%s\r\n", errbuf
);
350 ret
= krb5_auth_con_getremotesubkey(telnet_context
,
351 auth_context
, &key_block
);
354 Data(ap
, KRB_REJECT
, "krb5_auth_con_getremotesubkey failed", -1);
355 auth_finished(ap
, AUTH_REJECT
);
357 printf("Kerberos V5: "
358 "krb5_auth_con_getremotesubkey failed (%s)\r\n",
359 get_krb5_err_text(telnet_context
, ret
));
362 if (key_block
== NULL
) {
363 ret
= krb5_auth_con_getkey(telnet_context
,
368 Data(ap
, KRB_REJECT
, "krb5_auth_con_getkey failed", -1);
369 auth_finished(ap
, AUTH_REJECT
);
371 printf("Kerberos V5: "
372 "krb5_auth_con_getkey failed (%s)\r\n",
373 get_krb5_err_text(telnet_context
, ret
));
376 if (key_block
== NULL
) {
377 Data(ap
, KRB_REJECT
, "no subkey received", -1);
378 auth_finished(ap
, AUTH_REJECT
);
380 printf("Kerberos V5: "
381 "krb5_auth_con_getremotesubkey returned NULL key\r\n");
384 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
385 ret
= krb5_mk_rep(telnet_context
,
386 auth_context
, &outbuf
);
389 "krb5_mk_rep failed", -1);
390 auth_finished(ap
, AUTH_REJECT
);
392 printf("Kerberos V5: "
393 "krb5_mk_rep failed (%s)\r\n",
394 get_krb5_err_text(telnet_context
,
396 krb5_free_keyblock(telnet_context
, key_block
);
399 Data(ap
, KRB_RESPONSE
, outbuf
.data
, outbuf
.length
);
401 if (krb5_unparse_name(telnet_context
, ticket
->client
, &name
))
404 if (UserNameRequested
&& krb5_kuserok(telnet_context
,
405 ticket
->client
, UserNameRequested
)) {
406 Data(ap
, KRB_ACCEPT
, name
? name
: "", name
? -1 : 0);
407 if (auth_debug_mode
) {
408 printf("Kerberos5 identifies him as ``%s''\r\n",
411 if (key_block
->keytype
== ETYPE_DES_CBC_MD5
||
412 key_block
->keytype
== ETYPE_DES_CBC_MD4
||
413 key_block
->keytype
== ETYPE_DES_CBC_CRC
) {
418 skey
.data
= key_block
->keyvalue
.data
;
419 encrypt_session_key(&skey
, 0);
424 asprintf(&msg
, "user `%s' is not authorized to "
426 name
? name
: "<unknown>",
427 UserNameRequested
? UserNameRequested
: "<nobody>");
429 Data(ap
, KRB_REJECT
, NULL
, 0);
431 Data(ap
, KRB_REJECT
, (void *) msg
, -1);
434 auth_finished(ap
, AUTH_REJECT
);
435 krb5_free_keyblock(telnet_context
, key_block
);
438 auth_finished(ap
, AUTH_USER
);
439 krb5_free_keyblock(telnet_context
, key_block
);
442 struct passwd pws
, *pwd
;
444 char ccname
[1024]; /* XXX */
447 inbuf
.data
= (char *) data
;
450 if (getpwnam_r(UserNameRequested
, &pws
, pwbuf
,
451 sizeof(pwbuf
), &pwd
) != 0 || pwd
== NULL
)
454 snprintf(ccname
, sizeof(ccname
),
455 "FILE:/tmp/krb5cc_%u", pwd
->pw_uid
);
457 ret
= krb5_cc_resolve(telnet_context
, ccname
, &ccache
);
460 printf("Kerberos V5: could not get ccache: %s\r\n",
461 get_krb5_err_text(telnet_context
,
465 ret
= krb5_cc_initialize(telnet_context
, ccache
,
469 printf("Kerberos V5: could not init ccache: %s\r\n",
470 get_krb5_err_text(telnet_context
,
474 ret
= krb5_rd_cred2(telnet_context
, auth_context
,
480 "Read forwarded creds failed: %s",
481 get_krb5_err_text(telnet_context
, ret
));
483 Data(ap
, KRB_FORWARD_REJECT
, NULL
, 0);
485 Data(ap
, KRB_FORWARD_REJECT
, errbuf
, -1);
487 printf("Could not read forwarded credentials: %s\r\n",
491 Data(ap
, KRB_FORWARD_ACCEPT
, 0, 0);
492 chown(ccname
+ 5, pwd
->pw_uid
, -1);
494 printf("Forwarded credentials obtained\r\n");
499 printf("Unknown Kerberos option %d\r\n", data
[-1]);
500 Data(ap
, KRB_REJECT
, 0, 0);
506 kerberos5_reply(Authenticator
* ap
, unsigned char *data
, int cnt
)
508 static int mutual_complete
= 0;
515 printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
518 printf("[ Kerberos V5 refuses authentication ]\r\n");
524 krb5_keyblock
*keyblock
;
526 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
&&
528 printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
533 printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt
, data
);
535 printf("[ Kerberos V5 accepts you ]\r\n");
537 ret
= krb5_auth_con_getlocalsubkey(telnet_context
,
538 auth_context
, &keyblock
);
540 ret
= krb5_auth_con_getkey(telnet_context
,
541 auth_context
, &keyblock
);
543 printf("[ krb5_auth_con_getkey: %s ]\r\n",
544 get_krb5_err_text(telnet_context
, ret
));
550 skey
.data
= keyblock
->keyvalue
.data
;
551 encrypt_session_key(&skey
, 0);
552 krb5_free_keyblock(telnet_context
, keyblock
);
553 auth_finished(ap
, AUTH_USER
);
554 if (forward_flags
& OPTS_FORWARD_CREDS
)
555 kerberos5_forward(ap
);
559 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
560 /* the rest of the reply should contain a krb_ap_rep */
561 krb5_ap_rep_enc_part
*reply
;
566 inbuf
.data
= (char *) data
;
568 ret
= krb5_rd_rep(telnet_context
,
569 auth_context
, &inbuf
, &reply
);
571 printf("[ Mutual authentication failed: %s ]\r\n",
572 get_krb5_err_text(telnet_context
, ret
));
576 krb5_free_ap_rep_enc_part(telnet_context
, reply
);
580 case KRB_FORWARD_ACCEPT
:
581 printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
583 case KRB_FORWARD_REJECT
:
584 printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
589 printf("Unknown Kerberos option %d\r\n", data
[-1]);
595 kerberos5_status(Authenticator
*ap
, char *name
, size_t l
, int level
)
597 if (level
< AUTH_USER
)
600 if (UserNameRequested
&&
601 krb5_kuserok(telnet_context
, ticket
->client
, UserNameRequested
)) {
602 strlcpy(name
, UserNameRequested
, l
);
607 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
608 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
611 kerberos5_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
615 buf
[buflen
- 1] = '\0'; /* make sure its NULL terminated */
619 case KRB_REJECT
: /* Rejected (reason might follow) */
620 strlcpy((char *) buf
, " REJECT ", buflen
);
623 case KRB_ACCEPT
: /* Accepted (name might follow) */
624 strlcpy((char *) buf
, " ACCEPT ", buflen
);
629 ADDC(buf
, buflen
, '"');
630 for (i
= 4; i
< cnt
; i
++)
631 ADDC(buf
, buflen
, data
[i
]);
632 ADDC(buf
, buflen
, '"');
633 ADDC(buf
, buflen
, '\0');
637 case KRB_AUTH
: /* Authentication data follows */
638 strlcpy((char *) buf
, " AUTH", buflen
);
642 strlcpy((char *) buf
, " RESPONSE", buflen
);
645 case KRB_FORWARD
: /* Forwarded credentials follow */
646 strlcpy((char *) buf
, " FORWARD", buflen
);
649 case KRB_FORWARD_ACCEPT
: /* Forwarded credentials accepted */
650 strlcpy((char *) buf
, " FORWARD_ACCEPT", buflen
);
653 case KRB_FORWARD_REJECT
: /* Forwarded credentials rejected */
654 /* (reason might follow) */
655 strlcpy((char *) buf
, " FORWARD_REJECT", buflen
);
659 snprintf(buf
, buflen
, " %d (unknown)", data
[3]);
662 for (i
= 4; i
< cnt
; i
++) {
663 snprintf(buf
, buflen
, " %d", data
[i
]);
671 kerberos5_forward(Authenticator
* ap
)
676 krb5_kdc_flags flags
;
678 krb5_principal principal
;
680 ret
= krb5_cc_default(telnet_context
, &ccache
);
683 printf("KerberosV5: could not get default ccache: %s\r\n",
684 get_krb5_err_text(telnet_context
, ret
));
687 ret
= krb5_cc_get_principal(telnet_context
, ccache
, &principal
);
690 printf("KerberosV5: could not get principal: %s\r\n",
691 get_krb5_err_text(telnet_context
, ret
));
694 memset(&creds
, 0, sizeof(creds
));
696 creds
.client
= principal
;
698 ret
= krb5_build_principal(telnet_context
, &creds
.server
,
699 strlen(principal
->realm
), principal
->realm
, "krbtgt",
700 principal
->realm
, NULL
);
704 printf("KerberosV5: could not get principal: %s\r\n",
705 get_krb5_err_text(telnet_context
, ret
));
708 creds
.times
.endtime
= 0;
711 flags
.b
.forwarded
= 1;
712 if (forward_flags
& OPTS_FORWARDABLE_CREDS
)
713 flags
.b
.forwardable
= 1;
715 ret
= krb5_get_forwarded_creds(telnet_context
, auth_context
,
716 ccache
, flags
.i
, RemoteHostName
, &creds
, &out_data
);
719 printf("Kerberos V5: error getting forwarded creds: %s\r\n",
720 get_krb5_err_text(telnet_context
, ret
));
723 if (!Data(ap
, KRB_FORWARD
, out_data
.data
, out_data
.length
)) {
725 printf("Not enough room for authentication data\r\n");
728 printf("Forwarded local Kerberos V5 credentials to server\r\n");