Home | History | Annotate | Line # | Download | only in libtelnet
kerberos5.c revision 1.5
      1  1.5  thorpej /*	$NetBSD: kerberos5.c,v 1.5 2000/07/17 02:25:02 thorpej Exp $	*/
      2  1.4  thorpej 
      3  1.1      cgd /*-
      4  1.4  thorpej  * Copyright (c) 1991, 1993
      5  1.4  thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.1      cgd /*
     37  1.1      cgd  * Copyright (C) 1990 by the Massachusetts Institute of Technology
     38  1.1      cgd  *
     39  1.4  thorpej  * Export of this software from the United States of America may
     40  1.4  thorpej  * require a specific license from the United States Government.
     41  1.1      cgd  * It is the responsibility of any person or organization contemplating
     42  1.1      cgd  * export to obtain such a license before exporting.
     43  1.1      cgd  *
     44  1.1      cgd  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
     45  1.1      cgd  * distribute this software and its documentation for any purpose and
     46  1.1      cgd  * without fee is hereby granted, provided that the above copyright
     47  1.1      cgd  * notice appear in all copies and that both that copyright notice and
     48  1.1      cgd  * this permission notice appear in supporting documentation, and that
     49  1.1      cgd  * the name of M.I.T. not be used in advertising or publicity pertaining
     50  1.1      cgd  * to distribution of the software without specific, written prior
     51  1.1      cgd  * permission.  M.I.T. makes no representations about the suitability of
     52  1.1      cgd  * this software for any purpose.  It is provided "as is" without express
     53  1.1      cgd  * or implied warranty.
     54  1.1      cgd  */
     55  1.1      cgd 
     56  1.1      cgd #ifdef	KRB5
     57  1.1      cgd #include <arpa/telnet.h>
     58  1.1      cgd #include <stdio.h>
     59  1.4  thorpej #include <stdlib.h>
     60  1.4  thorpej #include <string.h>
     61  1.4  thorpej #include <unistd.h>
     62  1.1      cgd #include <netdb.h>
     63  1.1      cgd #include <ctype.h>
     64  1.4  thorpej #include <pwd.h>
     65  1.4  thorpej #define Authenticator k5_Authenticator
     66  1.4  thorpej #include <krb5.h>
     67  1.4  thorpej #undef Authenticator
     68  1.4  thorpej /* #include <roken.h> */
     69  1.1      cgd 
     70  1.1      cgd #include "encrypt.h"
     71  1.1      cgd #include "auth.h"
     72  1.1      cgd #include "misc.h"
     73  1.1      cgd 
     74  1.4  thorpej int forward_flags;	/* Flags get set in telnet/main.c on -f and -F */
     75  1.4  thorpej int got_forwarded_creds;/* Tell telnetd to pass -F or -f to login. */
     76  1.1      cgd 
     77  1.4  thorpej int require_hwpreauth;
     78  1.1      cgd 
     79  1.4  thorpej void kerberos5_forward(Authenticator *);
     80  1.4  thorpej 
     81  1.4  thorpej static unsigned char str_data[1024] = {IAC, SB, TELOPT_AUTHENTICATION, 0,
     82  1.4  thorpej 				       AUTHTYPE_KERBEROS_V5,};
     83  1.4  thorpej 
     84  1.4  thorpej #define	KRB_AUTH		0	/* Authentication data follows */
     85  1.4  thorpej #define	KRB_REJECT		1	/* Rejected (reason might follow) */
     86  1.4  thorpej #define	KRB_ACCEPT		2	/* Accepted */
     87  1.4  thorpej #define	KRB_RESPONSE		3	/* Response for mutual auth. */
     88  1.4  thorpej 
     89  1.4  thorpej #define KRB_FORWARD     	4	/* Forwarded credentials follow */
     90  1.4  thorpej #define KRB_FORWARD_ACCEPT     	5	/* Forwarded credentials accepted */
     91  1.4  thorpej #define KRB_FORWARD_REJECT     	6	/* Forwarded credentials rejected */
     92  1.4  thorpej 
     93  1.4  thorpej static krb5_data auth;
     94  1.4  thorpej static krb5_ticket *ticket;
     95  1.4  thorpej 
     96  1.4  thorpej krb5_context telnet_context;
     97  1.4  thorpej static krb5_auth_context auth_context;
     98  1.4  thorpej 
     99  1.4  thorpej static int
    100  1.4  thorpej Data(Authenticator *ap, int type, void *d, int c)
    101  1.1      cgd {
    102  1.4  thorpej 	unsigned char *p = str_data + 4;
    103  1.4  thorpej 	unsigned char *cd = (unsigned char *) d;
    104  1.1      cgd 
    105  1.1      cgd 	if (c == -1)
    106  1.4  thorpej 		c = strlen(cd);
    107  1.1      cgd 
    108  1.4  thorpej 	if (auth_debug_mode) {
    109  1.4  thorpej 		printf("%s:%d: [%d] (%d)",
    110  1.4  thorpej 		    str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
    111  1.4  thorpej 		    str_data[3],
    112  1.4  thorpej 		    type, c);
    113  1.4  thorpej 		printd(d, c);
    114  1.4  thorpej 		printf("\r\n");
    115  1.4  thorpej 	}
    116  1.1      cgd 	*p++ = ap->type;
    117  1.1      cgd 	*p++ = ap->way;
    118  1.1      cgd 	*p++ = type;
    119  1.4  thorpej 	while (c-- > 0) {
    120  1.4  thorpej 		if ((*p++ = *cd++) == IAC)
    121  1.4  thorpej 			*p++ = IAC;
    122  1.4  thorpej 	}
    123  1.4  thorpej 	*p++ = IAC;
    124  1.4  thorpej 	*p++ = SE;
    125  1.1      cgd 	if (str_data[3] == TELQUAL_IS)
    126  1.1      cgd 		printsub('>', &str_data[2], p - &str_data[2]);
    127  1.4  thorpej 	return (telnet_net_write(str_data, p - str_data));
    128  1.1      cgd }
    129  1.1      cgd 
    130  1.4  thorpej int
    131  1.4  thorpej kerberos5_init(Authenticator *ap, int server)
    132  1.1      cgd {
    133  1.5  thorpej 	krb5_error_code ret;
    134  1.4  thorpej 
    135  1.5  thorpej 	if (telnet_context == 0) {
    136  1.5  thorpej 		ret = krb5_init_context(&telnet_context);
    137  1.5  thorpej 		if (ret)
    138  1.5  thorpej 			return 0;
    139  1.5  thorpej 	}
    140  1.4  thorpej 
    141  1.4  thorpej 	if (server) {
    142  1.4  thorpej 		krb5_keytab kt;
    143  1.4  thorpej 		krb5_kt_cursor cursor;
    144  1.4  thorpej 
    145  1.4  thorpej 		ret = krb5_kt_default(telnet_context, &kt);
    146  1.4  thorpej 		if (ret)
    147  1.4  thorpej 			return 0;
    148  1.4  thorpej 
    149  1.4  thorpej 		ret = krb5_kt_start_seq_get(telnet_context, kt, &cursor);
    150  1.4  thorpej 		if (ret) {
    151  1.4  thorpej 			krb5_kt_close(telnet_context, kt);
    152  1.4  thorpej 			return 0;
    153  1.4  thorpej 		}
    154  1.4  thorpej 		krb5_kt_end_seq_get(telnet_context, kt, &cursor);
    155  1.4  thorpej 		krb5_kt_close(telnet_context, kt);
    156  1.4  thorpej 
    157  1.1      cgd 		str_data[3] = TELQUAL_REPLY;
    158  1.4  thorpej 	} else
    159  1.1      cgd 		str_data[3] = TELQUAL_IS;
    160  1.4  thorpej 	return (1);
    161  1.1      cgd }
    162  1.1      cgd 
    163  1.4  thorpej int
    164  1.4  thorpej kerberos5_send(Authenticator *ap)
    165  1.1      cgd {
    166  1.4  thorpej 	krb5_error_code ret;
    167  1.1      cgd 	krb5_ccache ccache;
    168  1.4  thorpej 	int ap_opts;
    169  1.4  thorpej 	krb5_data cksum_data;
    170  1.4  thorpej 	char foo[2];
    171  1.4  thorpej 	extern int net;
    172  1.1      cgd 
    173  1.4  thorpej 	printf("[ Trying KERBEROS5 ... ]\r\n");
    174  1.1      cgd 
    175  1.4  thorpej 	if (!UserNameRequested) {
    176  1.1      cgd 		if (auth_debug_mode) {
    177  1.4  thorpej 			printf("Kerberos V5: no user name supplied\r\n");
    178  1.1      cgd 		}
    179  1.4  thorpej 		return (0);
    180  1.1      cgd 	}
    181  1.4  thorpej 	ret = krb5_cc_default(telnet_context, &ccache);
    182  1.4  thorpej 	if (ret) {
    183  1.1      cgd 		if (auth_debug_mode) {
    184  1.4  thorpej 			printf(
    185  1.4  thorpej 			"Kerberos V5: could not get default ccache: %s\r\n",
    186  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    187  1.1      cgd 		}
    188  1.4  thorpej 		return (0);
    189  1.1      cgd 	}
    190  1.4  thorpej 	if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
    191  1.4  thorpej 		ap_opts = AP_OPTS_MUTUAL_REQUIRED;
    192  1.4  thorpej 	else
    193  1.4  thorpej 		ap_opts = 0;
    194  1.1      cgd 
    195  1.4  thorpej 	ret = krb5_auth_con_init(telnet_context, &auth_context);
    196  1.4  thorpej 	if (ret) {
    197  1.1      cgd 		if (auth_debug_mode) {
    198  1.4  thorpej 			printf(
    199  1.4  thorpej 			"Kerberos V5: krb5_auth_con_init failed: %s\r\n",
    200  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    201  1.4  thorpej 		}
    202  1.4  thorpej 		return (0);
    203  1.4  thorpej 	}
    204  1.4  thorpej 	ret = krb5_auth_con_setaddrs_from_fd(telnet_context,
    205  1.4  thorpej 	    auth_context, &net);
    206  1.4  thorpej 	if (ret) {
    207  1.4  thorpej 		if (auth_debug_mode) {
    208  1.4  thorpej 			printf("Kerberos V5: "
    209  1.4  thorpej 			    "krb5_auth_con_setaddrs_from_fd failed: %s\r\n",
    210  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    211  1.1      cgd 		}
    212  1.4  thorpej 		return (0);
    213  1.1      cgd 	}
    214  1.4  thorpej 	krb5_auth_setkeytype(telnet_context, auth_context, KEYTYPE_DES);
    215  1.1      cgd 
    216  1.4  thorpej 	foo[0] = ap->type;
    217  1.4  thorpej 	foo[1] = ap->way;
    218  1.1      cgd 
    219  1.4  thorpej 	cksum_data.length = sizeof(foo);
    220  1.4  thorpej 	cksum_data.data = foo;
    221  1.4  thorpej 	ret = krb5_mk_req(telnet_context, &auth_context, ap_opts, "host",
    222  1.4  thorpej 	    RemoteHostName, &cksum_data, ccache, &auth);
    223  1.4  thorpej 	if (ret) {
    224  1.4  thorpej 		if (1 || auth_debug_mode) {
    225  1.4  thorpej 			printf("Kerberos V5: mk_req failed (%s)\r\n",
    226  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    227  1.1      cgd 		}
    228  1.4  thorpej 		return (0);
    229  1.1      cgd 	}
    230  1.1      cgd 
    231  1.4  thorpej 	if (!auth_sendname((unsigned char *) UserNameRequested,
    232  1.4  thorpej 		strlen(UserNameRequested))) {
    233  1.4  thorpej 		if (auth_debug_mode)
    234  1.4  thorpej 			printf("Not enough room for user name\r\n");
    235  1.4  thorpej 		return (0);
    236  1.4  thorpej 	}
    237  1.1      cgd 	if (!Data(ap, KRB_AUTH, auth.data, auth.length)) {
    238  1.1      cgd 		if (auth_debug_mode)
    239  1.1      cgd 			printf("Not enough room for authentication data\r\n");
    240  1.4  thorpej 		return (0);
    241  1.1      cgd 	}
    242  1.1      cgd 	if (auth_debug_mode) {
    243  1.1      cgd 		printf("Sent Kerberos V5 credentials to server\r\n");
    244  1.1      cgd 	}
    245  1.4  thorpej 	return (1);
    246  1.1      cgd }
    247  1.1      cgd 
    248  1.4  thorpej void
    249  1.4  thorpej kerberos5_is(Authenticator * ap, unsigned char *data, int cnt)
    250  1.1      cgd {
    251  1.4  thorpej 	krb5_error_code ret;
    252  1.4  thorpej 	krb5_data outbuf;
    253  1.4  thorpej 	krb5_keyblock *key_block;
    254  1.1      cgd 	char *name;
    255  1.4  thorpej 	krb5_principal server;
    256  1.4  thorpej 	int zero = 0;
    257  1.1      cgd 
    258  1.1      cgd 	if (cnt-- < 1)
    259  1.1      cgd 		return;
    260  1.1      cgd 	switch (*data++) {
    261  1.1      cgd 	case KRB_AUTH:
    262  1.4  thorpej 		auth.data = (char *) data;
    263  1.1      cgd 		auth.length = cnt;
    264  1.1      cgd 
    265  1.4  thorpej 		auth_context = NULL;
    266  1.4  thorpej 
    267  1.4  thorpej 		ret = krb5_auth_con_init(telnet_context, &auth_context);
    268  1.4  thorpej 		if (ret) {
    269  1.4  thorpej 			Data(ap, KRB_REJECT, "krb5_auth_con_init failed", -1);
    270  1.4  thorpej 			auth_finished(ap, AUTH_REJECT);
    271  1.1      cgd 			if (auth_debug_mode)
    272  1.4  thorpej 				printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
    273  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    274  1.4  thorpej 			return;
    275  1.4  thorpej 		}
    276  1.4  thorpej 		ret = krb5_auth_con_setaddrs_from_fd(telnet_context,
    277  1.4  thorpej 		    auth_context, &zero);
    278  1.4  thorpej 		if (ret) {
    279  1.4  thorpej 			Data(ap, KRB_REJECT, "krb5_auth_con_setaddrs_from_fd failed", -1);
    280  1.1      cgd 			auth_finished(ap, AUTH_REJECT);
    281  1.4  thorpej 			if (auth_debug_mode)
    282  1.4  thorpej 				printf("Kerberos V5: "
    283  1.4  thorpej 				    "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
    284  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    285  1.1      cgd 			return;
    286  1.1      cgd 		}
    287  1.4  thorpej 		ret = krb5_sock_to_principal(telnet_context, 0, "host",
    288  1.4  thorpej 		    KRB5_NT_SRV_HST, &server);
    289  1.4  thorpej 		if (ret) {
    290  1.4  thorpej 			Data(ap, KRB_REJECT, "krb5_sock_to_principal failed", -1);
    291  1.4  thorpej 			auth_finished(ap, AUTH_REJECT);
    292  1.1      cgd 			if (auth_debug_mode)
    293  1.4  thorpej 				printf("Kerberos V5: "
    294  1.4  thorpej 				    "krb5_sock_to_principal failed (%s)\r\n",
    295  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    296  1.1      cgd 			return;
    297  1.1      cgd 		}
    298  1.4  thorpej 		ret = krb5_rd_req(telnet_context, &auth_context, &auth,
    299  1.4  thorpej 		    server, NULL, NULL, &ticket);
    300  1.4  thorpej 		krb5_free_principal(telnet_context, server);
    301  1.4  thorpej 
    302  1.4  thorpej 		if (ret) {
    303  1.4  thorpej 			char *errbuf;
    304  1.4  thorpej 
    305  1.4  thorpej 			asprintf(&errbuf,
    306  1.4  thorpej 			    "Read req failed: %s",
    307  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    308  1.4  thorpej 			Data(ap, KRB_REJECT, errbuf, -1);
    309  1.1      cgd 			if (auth_debug_mode)
    310  1.4  thorpej 				printf("%s\r\n", errbuf);
    311  1.4  thorpej 			free(errbuf);
    312  1.1      cgd 			return;
    313  1.4  thorpej 		} {
    314  1.4  thorpej 			char foo[2];
    315  1.4  thorpej 
    316  1.4  thorpej 			foo[0] = ap->type;
    317  1.4  thorpej 			foo[1] = ap->way;
    318  1.4  thorpej 
    319  1.4  thorpej 			ret = krb5_verify_authenticator_checksum(telnet_context,
    320  1.4  thorpej 			    auth_context, foo, sizeof(foo));
    321  1.4  thorpej 
    322  1.4  thorpej 			if (ret) {
    323  1.4  thorpej 				char *errbuf;
    324  1.4  thorpej 				asprintf(&errbuf, "Bad checksum: %s",
    325  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    326  1.4  thorpej 				Data(ap, KRB_REJECT, errbuf, -1);
    327  1.4  thorpej 				if (auth_debug_mode)
    328  1.4  thorpej 					printf("%s\r\n", errbuf);
    329  1.4  thorpej 				free(errbuf);
    330  1.4  thorpej 				return;
    331  1.4  thorpej 			}
    332  1.1      cgd 		}
    333  1.4  thorpej 		ret = krb5_auth_con_getremotesubkey(telnet_context,
    334  1.4  thorpej 		    auth_context, &key_block);
    335  1.1      cgd 
    336  1.4  thorpej 		if (ret) {
    337  1.4  thorpej 			Data(ap, KRB_REJECT, "krb5_auth_con_getremotesubkey failed", -1);
    338  1.4  thorpej 			auth_finished(ap, AUTH_REJECT);
    339  1.1      cgd 			if (auth_debug_mode)
    340  1.4  thorpej 				printf("Kerberos V5: "
    341  1.4  thorpej 				    "krb5_auth_con_getremotesubkey failed (%s)\r\n",
    342  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    343  1.1      cgd 			return;
    344  1.1      cgd 		}
    345  1.4  thorpej 		if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
    346  1.4  thorpej 			ret = krb5_mk_rep(telnet_context,
    347  1.4  thorpej 			    &auth_context, &outbuf);
    348  1.4  thorpej 			if (ret) {
    349  1.4  thorpej 				Data(ap, KRB_REJECT,
    350  1.4  thorpej 				    "krb5_mk_rep failed", -1);
    351  1.4  thorpej 				auth_finished(ap, AUTH_REJECT);
    352  1.4  thorpej 				if (auth_debug_mode)
    353  1.4  thorpej 					printf("Kerberos V5: "
    354  1.4  thorpej 					    "krb5_mk_rep failed (%s)\r\n",
    355  1.4  thorpej 					    krb5_get_err_text(telnet_context,
    356  1.4  thorpej 					    ret));
    357  1.4  thorpej 				return;
    358  1.4  thorpej 			}
    359  1.4  thorpej 			Data(ap, KRB_RESPONSE, outbuf.data, outbuf.length);
    360  1.4  thorpej 		}
    361  1.4  thorpej 		if (krb5_unparse_name(telnet_context, ticket->client, &name))
    362  1.1      cgd 			name = 0;
    363  1.4  thorpej 
    364  1.4  thorpej 		if (UserNameRequested && krb5_kuserok(telnet_context,
    365  1.4  thorpej 		    ticket->client, UserNameRequested)) {
    366  1.4  thorpej 			Data(ap, KRB_ACCEPT, name, name ? -1 : 0);
    367  1.4  thorpej 			if (auth_debug_mode) {
    368  1.4  thorpej 				printf("Kerberos5 identifies him as ``%s''\r\n",
    369  1.4  thorpej 				    name ? name : "");
    370  1.4  thorpej 			}
    371  1.4  thorpej 			if (key_block->keytype == ETYPE_DES_CBC_MD5 ||
    372  1.4  thorpej 			    key_block->keytype == ETYPE_DES_CBC_MD4 ||
    373  1.4  thorpej 			    key_block->keytype == ETYPE_DES_CBC_CRC) {
    374  1.4  thorpej 				Session_Key skey;
    375  1.4  thorpej 
    376  1.4  thorpej 				skey.type = SK_DES;
    377  1.4  thorpej 				skey.length = 8;
    378  1.4  thorpej 				skey.data = key_block->keyvalue.data;
    379  1.4  thorpej 				encrypt_session_key(&skey, 0);
    380  1.4  thorpej 			}
    381  1.4  thorpej 		} else {
    382  1.4  thorpej 			char *msg;
    383  1.4  thorpej 
    384  1.4  thorpej 			asprintf(&msg, "user `%s' is not authorized to "
    385  1.4  thorpej 			    "login as `%s'",
    386  1.4  thorpej 			    name ? name : "<unknown>",
    387  1.4  thorpej 			    UserNameRequested ? UserNameRequested : "<nobody>");
    388  1.4  thorpej 			if (msg == NULL)
    389  1.4  thorpej 				Data(ap, KRB_REJECT, NULL, 0);
    390  1.4  thorpej 			else {
    391  1.4  thorpej 				Data(ap, KRB_REJECT, (void *) msg, -1);
    392  1.4  thorpej 				free(msg);
    393  1.4  thorpej 			}
    394  1.4  thorpej 			auth_finished(ap, AUTH_REJECT);
    395  1.4  thorpej 			krb5_free_keyblock_contents(telnet_context, key_block);
    396  1.4  thorpej 			break;
    397  1.1      cgd 		}
    398  1.4  thorpej 		auth_finished(ap, AUTH_USER);
    399  1.4  thorpej 		krb5_free_keyblock_contents(telnet_context, key_block);
    400  1.4  thorpej 
    401  1.1      cgd 		break;
    402  1.4  thorpej 	case KRB_FORWARD:{
    403  1.4  thorpej 			struct passwd *pwd;
    404  1.4  thorpej 			char ccname[1024];	/* XXX */
    405  1.4  thorpej 			krb5_data inbuf;
    406  1.4  thorpej 			krb5_ccache ccache;
    407  1.4  thorpej 			inbuf.data = (char *) data;
    408  1.4  thorpej 			inbuf.length = cnt;
    409  1.4  thorpej 
    410  1.4  thorpej 			pwd = getpwnam(UserNameRequested);
    411  1.4  thorpej 			if (pwd == NULL)
    412  1.4  thorpej 				break;
    413  1.1      cgd 
    414  1.4  thorpej 			snprintf(ccname, sizeof(ccname),
    415  1.4  thorpej 			    "FILE:/tmp/krb5cc_%u", pwd->pw_uid);
    416  1.1      cgd 
    417  1.4  thorpej 			ret = krb5_cc_resolve(telnet_context, ccname, &ccache);
    418  1.4  thorpej 			if (ret) {
    419  1.4  thorpej 				if (auth_debug_mode)
    420  1.4  thorpej 					printf("Kerberos V5: could not get ccache: %s\r\n",
    421  1.4  thorpej 					    krb5_get_err_text(telnet_context,
    422  1.4  thorpej 					    ret));
    423  1.4  thorpej 				break;
    424  1.4  thorpej 			}
    425  1.4  thorpej 			ret = krb5_cc_initialize(telnet_context, ccache,
    426  1.4  thorpej 			    ticket->client);
    427  1.4  thorpej 			if (ret) {
    428  1.4  thorpej 				if (auth_debug_mode)
    429  1.4  thorpej 					printf("Kerberos V5: could not init ccache: %s\r\n",
    430  1.4  thorpej 					    krb5_get_err_text(telnet_context,
    431  1.4  thorpej 					        ret));
    432  1.1      cgd 				break;
    433  1.4  thorpej 			}
    434  1.4  thorpej 			ret = krb5_rd_cred(telnet_context, auth_context,
    435  1.4  thorpej 			    ccache, &inbuf);
    436  1.4  thorpej 			if (ret) {
    437  1.4  thorpej 				char *errbuf;
    438  1.4  thorpej 
    439  1.4  thorpej 				asprintf(&errbuf,
    440  1.4  thorpej 				    "Read forwarded creds failed: %s",
    441  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    442  1.4  thorpej 				if (errbuf == NULL)
    443  1.4  thorpej 					Data(ap, KRB_FORWARD_REJECT, NULL, 0);
    444  1.4  thorpej 				else
    445  1.4  thorpej 					Data(ap, KRB_FORWARD_REJECT, errbuf, -1);
    446  1.4  thorpej 				if (auth_debug_mode)
    447  1.4  thorpej 					printf("Could not read forwarded credentials: %s\r\n",
    448  1.4  thorpej 					    errbuf);
    449  1.4  thorpej 				free(errbuf);
    450  1.4  thorpej 			} else
    451  1.4  thorpej 				Data(ap, KRB_FORWARD_ACCEPT, 0, 0);
    452  1.4  thorpej 			chown(ccname + 5, pwd->pw_uid, -1);
    453  1.4  thorpej 			if (auth_debug_mode)
    454  1.4  thorpej 				printf("Forwarded credentials obtained\r\n");
    455  1.4  thorpej 			break;
    456  1.1      cgd 		}
    457  1.1      cgd 	default:
    458  1.1      cgd 		if (auth_debug_mode)
    459  1.1      cgd 			printf("Unknown Kerberos option %d\r\n", data[-1]);
    460  1.1      cgd 		Data(ap, KRB_REJECT, 0, 0);
    461  1.1      cgd 		break;
    462  1.1      cgd 	}
    463  1.1      cgd }
    464  1.1      cgd 
    465  1.4  thorpej void
    466  1.4  thorpej kerberos5_reply(Authenticator * ap, unsigned char *data, int cnt)
    467  1.1      cgd {
    468  1.4  thorpej 	static int mutual_complete = 0;
    469  1.1      cgd 
    470  1.1      cgd 	if (cnt-- < 1)
    471  1.1      cgd 		return;
    472  1.1      cgd 	switch (*data++) {
    473  1.1      cgd 	case KRB_REJECT:
    474  1.1      cgd 		if (cnt > 0) {
    475  1.1      cgd 			printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
    476  1.4  thorpej 			    cnt, data);
    477  1.1      cgd 		} else
    478  1.1      cgd 			printf("[ Kerberos V5 refuses authentication ]\r\n");
    479  1.1      cgd 		auth_send_retry();
    480  1.1      cgd 		return;
    481  1.4  thorpej 	case KRB_ACCEPT:{
    482  1.4  thorpej 			krb5_error_code ret;
    483  1.4  thorpej 			Session_Key skey;
    484  1.4  thorpej 			krb5_keyblock *keyblock;
    485  1.4  thorpej 
    486  1.4  thorpej 			if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
    487  1.4  thorpej 			    !mutual_complete) {
    488  1.4  thorpej 				printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
    489  1.4  thorpej 				auth_send_retry();
    490  1.4  thorpej 				return;
    491  1.4  thorpej 			}
    492  1.4  thorpej 			if (cnt)
    493  1.4  thorpej 				printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt, data);
    494  1.4  thorpej 			else
    495  1.4  thorpej 				printf("[ Kerberos V5 accepts you ]\r\n");
    496  1.4  thorpej 
    497  1.4  thorpej 			ret = krb5_auth_con_getlocalsubkey(telnet_context,
    498  1.4  thorpej 			    auth_context, &keyblock);
    499  1.4  thorpej 			if (ret)
    500  1.4  thorpej 				ret = krb5_auth_con_getkey(telnet_context,
    501  1.4  thorpej 				    auth_context, &keyblock);
    502  1.4  thorpej 			if (ret) {
    503  1.4  thorpej 				printf("[ krb5_auth_con_getkey: %s ]\r\n",
    504  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    505  1.4  thorpej 				auth_send_retry();
    506  1.4  thorpej 				return;
    507  1.4  thorpej 			}
    508  1.1      cgd 			skey.type = SK_DES;
    509  1.1      cgd 			skey.length = 8;
    510  1.4  thorpej 			skey.data = keyblock->keyvalue.data;
    511  1.1      cgd 			encrypt_session_key(&skey, 0);
    512  1.4  thorpej 			krb5_free_keyblock_contents(telnet_context, keyblock);
    513  1.4  thorpej 			auth_finished(ap, AUTH_USER);
    514  1.4  thorpej 			if (forward_flags & OPTS_FORWARD_CREDS)
    515  1.4  thorpej 				kerberos5_forward(ap);
    516  1.4  thorpej 			break;
    517  1.1      cgd 		}
    518  1.1      cgd 	case KRB_RESPONSE:
    519  1.4  thorpej 		if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
    520  1.4  thorpej 			/* the rest of the reply should contain a krb_ap_rep */
    521  1.4  thorpej 			krb5_ap_rep_enc_part *reply;
    522  1.4  thorpej 			krb5_data inbuf;
    523  1.4  thorpej 			krb5_error_code ret;
    524  1.4  thorpej 
    525  1.4  thorpej 			inbuf.length = cnt;
    526  1.4  thorpej 			inbuf.data = (char *) data;
    527  1.4  thorpej 
    528  1.4  thorpej 			ret = krb5_rd_rep(telnet_context,
    529  1.4  thorpej 			    auth_context, &inbuf, &reply);
    530  1.4  thorpej 			if (ret) {
    531  1.4  thorpej 				printf("[ Mutual authentication failed: %s ]\r\n",
    532  1.4  thorpej 				    krb5_get_err_text(telnet_context, ret));
    533  1.4  thorpej 				auth_send_retry();
    534  1.4  thorpej 				return;
    535  1.4  thorpej 			}
    536  1.4  thorpej 			krb5_free_ap_rep_enc_part(telnet_context, reply);
    537  1.4  thorpej 			mutual_complete = 1;
    538  1.1      cgd 		}
    539  1.4  thorpej 		return;
    540  1.4  thorpej 	case KRB_FORWARD_ACCEPT:
    541  1.4  thorpej 		printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
    542  1.4  thorpej 		return;
    543  1.4  thorpej 	case KRB_FORWARD_REJECT:
    544  1.4  thorpej 		printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
    545  1.4  thorpej 		    cnt, data);
    546  1.4  thorpej 		return;
    547  1.1      cgd 	default:
    548  1.1      cgd 		if (auth_debug_mode)
    549  1.1      cgd 			printf("Unknown Kerberos option %d\r\n", data[-1]);
    550  1.1      cgd 		return;
    551  1.1      cgd 	}
    552  1.1      cgd }
    553  1.1      cgd 
    554  1.4  thorpej int
    555  1.4  thorpej kerberos5_status(Authenticator *ap, char *name, int level)
    556  1.1      cgd {
    557  1.1      cgd 	if (level < AUTH_USER)
    558  1.4  thorpej 		return (level);
    559  1.1      cgd 
    560  1.1      cgd 	if (UserNameRequested &&
    561  1.4  thorpej 	    krb5_kuserok(telnet_context, ticket->client, UserNameRequested)) {
    562  1.1      cgd 		strcpy(name, UserNameRequested);
    563  1.4  thorpej 		return (AUTH_VALID);
    564  1.1      cgd 	} else
    565  1.4  thorpej 		return (AUTH_USER);
    566  1.1      cgd }
    567  1.1      cgd #define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
    568  1.4  thorpej #define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len);}
    569  1.1      cgd 
    570  1.4  thorpej void
    571  1.4  thorpej kerberos5_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
    572  1.1      cgd {
    573  1.4  thorpej 	int i;
    574  1.1      cgd 
    575  1.4  thorpej 	buf[buflen - 1] = '\0';	/* make sure its NULL terminated */
    576  1.1      cgd 	buflen -= 1;
    577  1.1      cgd 
    578  1.4  thorpej 	switch (data[3]) {
    579  1.4  thorpej 	case KRB_REJECT:	/* Rejected (reason might follow) */
    580  1.4  thorpej 		strlcpy((char *) buf, " REJECT ", buflen);
    581  1.1      cgd 		goto common;
    582  1.1      cgd 
    583  1.4  thorpej 	case KRB_ACCEPT:	/* Accepted (name might follow) */
    584  1.4  thorpej 		strlcpy((char *) buf, " ACCEPT ", buflen);
    585  1.4  thorpej common:
    586  1.1      cgd 		BUMP(buf, buflen);
    587  1.1      cgd 		if (cnt <= 4)
    588  1.1      cgd 			break;
    589  1.1      cgd 		ADDC(buf, buflen, '"');
    590  1.1      cgd 		for (i = 4; i < cnt; i++)
    591  1.1      cgd 			ADDC(buf, buflen, data[i]);
    592  1.1      cgd 		ADDC(buf, buflen, '"');
    593  1.1      cgd 		ADDC(buf, buflen, '\0');
    594  1.1      cgd 		break;
    595  1.1      cgd 
    596  1.4  thorpej 
    597  1.4  thorpej 	case KRB_AUTH:		/* Authentication data follows */
    598  1.4  thorpej 		strlcpy((char *) buf, " AUTH", buflen);
    599  1.4  thorpej 		goto common2;
    600  1.4  thorpej 
    601  1.4  thorpej 	case KRB_RESPONSE:
    602  1.4  thorpej 		strlcpy((char *) buf, " RESPONSE", buflen);
    603  1.1      cgd 		goto common2;
    604  1.1      cgd 
    605  1.4  thorpej 	case KRB_FORWARD:	/* Forwarded credentials follow */
    606  1.4  thorpej 		strlcpy((char *) buf, " FORWARD", buflen);
    607  1.1      cgd 		goto common2;
    608  1.1      cgd 
    609  1.4  thorpej 	case KRB_FORWARD_ACCEPT:	/* Forwarded credentials accepted */
    610  1.4  thorpej 		strlcpy((char *) buf, " FORWARD_ACCEPT", buflen);
    611  1.4  thorpej 		goto common2;
    612  1.4  thorpej 
    613  1.4  thorpej 	case KRB_FORWARD_REJECT:	/* Forwarded credentials rejected */
    614  1.4  thorpej 		/* (reason might follow) */
    615  1.4  thorpej 		strlcpy((char *) buf, " FORWARD_REJECT", buflen);
    616  1.1      cgd 		goto common2;
    617  1.1      cgd 
    618  1.1      cgd 	default:
    619  1.4  thorpej 		snprintf(buf, buflen, " %d (unknown)", data[3]);
    620  1.4  thorpej common2:
    621  1.1      cgd 		BUMP(buf, buflen);
    622  1.1      cgd 		for (i = 4; i < cnt; i++) {
    623  1.4  thorpej 			snprintf(buf, buflen, " %d", data[i]);
    624  1.1      cgd 			BUMP(buf, buflen);
    625  1.1      cgd 		}
    626  1.1      cgd 		break;
    627  1.1      cgd 	}
    628  1.1      cgd }
    629  1.4  thorpej 
    630  1.4  thorpej void
    631  1.4  thorpej kerberos5_forward(Authenticator * ap)
    632  1.4  thorpej {
    633  1.4  thorpej 	krb5_error_code ret;
    634  1.4  thorpej 	krb5_ccache ccache;
    635  1.4  thorpej 	krb5_creds creds;
    636  1.4  thorpej 	krb5_kdc_flags flags;
    637  1.4  thorpej 	krb5_data out_data;
    638  1.4  thorpej 	krb5_principal principal;
    639  1.4  thorpej 
    640  1.4  thorpej 	ret = krb5_cc_default(telnet_context, &ccache);
    641  1.4  thorpej 	if (ret) {
    642  1.4  thorpej 		if (auth_debug_mode)
    643  1.4  thorpej 			printf("KerberosV5: could not get default ccache: %s\r\n",
    644  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    645  1.4  thorpej 		return;
    646  1.4  thorpej 	}
    647  1.4  thorpej 	ret = krb5_cc_get_principal(telnet_context, ccache, &principal);
    648  1.4  thorpej 	if (ret) {
    649  1.4  thorpej 		if (auth_debug_mode)
    650  1.4  thorpej 			printf("KerberosV5: could not get principal: %s\r\n",
    651  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    652  1.4  thorpej 		return;
    653  1.4  thorpej 	}
    654  1.4  thorpej 	memset(&creds, 0, sizeof(creds));
    655  1.4  thorpej 
    656  1.4  thorpej 	creds.client = principal;
    657  1.4  thorpej 
    658  1.4  thorpej 	ret = krb5_build_principal(telnet_context, &creds.server,
    659  1.4  thorpej 	    strlen(principal->realm), principal->realm, "krbtgt",
    660  1.4  thorpej 	    principal->realm, NULL);
    661  1.4  thorpej 
    662  1.4  thorpej 	if (ret) {
    663  1.4  thorpej 		if (auth_debug_mode)
    664  1.4  thorpej 			printf("KerberosV5: could not get principal: %s\r\n",
    665  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    666  1.4  thorpej 		return;
    667  1.4  thorpej 	}
    668  1.4  thorpej 	creds.times.endtime = 0;
    669  1.4  thorpej 
    670  1.4  thorpej 	flags.i = 0;
    671  1.4  thorpej 	flags.b.forwarded = 1;
    672  1.4  thorpej 	if (forward_flags & OPTS_FORWARDABLE_CREDS)
    673  1.4  thorpej 		flags.b.forwardable = 1;
    674  1.4  thorpej 
    675  1.4  thorpej 	ret = krb5_get_forwarded_creds(telnet_context, auth_context,
    676  1.4  thorpej 	    ccache, flags.i, RemoteHostName, &creds, &out_data);
    677  1.4  thorpej 	if (ret) {
    678  1.4  thorpej 		if (auth_debug_mode)
    679  1.4  thorpej 			printf("Kerberos V5: error getting forwarded creds: %s\r\n",
    680  1.4  thorpej 			    krb5_get_err_text(telnet_context, ret));
    681  1.4  thorpej 		return;
    682  1.4  thorpej 	}
    683  1.4  thorpej 	if (!Data(ap, KRB_FORWARD, out_data.data, out_data.length)) {
    684  1.4  thorpej 		if (auth_debug_mode)
    685  1.4  thorpej 			printf("Not enough room for authentication data\r\n");
    686  1.4  thorpej 	} else {
    687  1.4  thorpej 		if (auth_debug_mode)
    688  1.4  thorpej 			printf("Forwarded local Kerberos V5 credentials to server\r\n");
    689  1.4  thorpej 	}
    690  1.4  thorpej }
    691  1.4  thorpej #endif /* KRB5 */
    692