Home | History | Annotate | Line # | Download | only in libtelnet
kerberos5.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)kerberos5.c	5.2 (Berkeley) 3/22/91";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd /*
     39  1.1  cgd  * Copyright (C) 1990 by the Massachusetts Institute of Technology
     40  1.1  cgd  *
     41  1.1  cgd  * Export of this software from the United States of America is assumed
     42  1.1  cgd  * to require a specific license from the United States Government.
     43  1.1  cgd  * It is the responsibility of any person or organization contemplating
     44  1.1  cgd  * export to obtain such a license before exporting.
     45  1.1  cgd  *
     46  1.1  cgd  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
     47  1.1  cgd  * distribute this software and its documentation for any purpose and
     48  1.1  cgd  * without fee is hereby granted, provided that the above copyright
     49  1.1  cgd  * notice appear in all copies and that both that copyright notice and
     50  1.1  cgd  * this permission notice appear in supporting documentation, and that
     51  1.1  cgd  * the name of M.I.T. not be used in advertising or publicity pertaining
     52  1.1  cgd  * to distribution of the software without specific, written prior
     53  1.1  cgd  * permission.  M.I.T. makes no representations about the suitability of
     54  1.1  cgd  * this software for any purpose.  It is provided "as is" without express
     55  1.1  cgd  * or implied warranty.
     56  1.1  cgd  */
     57  1.1  cgd 
     58  1.1  cgd 
     59  1.1  cgd #ifdef	KRB5
     60  1.1  cgd #include <arpa/telnet.h>
     61  1.1  cgd #include <stdio.h>
     62  1.1  cgd #include <krb5/krb5.h>
     63  1.1  cgd #include <krb5/crc-32.h>
     64  1.1  cgd #include <krb5/libos-proto.h>
     65  1.1  cgd #include <netdb.h>
     66  1.1  cgd #include <ctype.h>
     67  1.1  cgd 
     68  1.1  cgd #ifdef	__STDC__
     69  1.1  cgd #include <stdlib.h>
     70  1.1  cgd #endif
     71  1.1  cgd #ifdef	NO_STRING_H
     72  1.1  cgd #include <strings.h>
     73  1.1  cgd #else
     74  1.1  cgd #include <string.h>
     75  1.1  cgd #endif
     76  1.1  cgd 
     77  1.1  cgd #include "encrypt.h"
     78  1.1  cgd #include "auth.h"
     79  1.1  cgd #include "misc.h"
     80  1.1  cgd 
     81  1.1  cgd extern auth_debug_mode;
     82  1.1  cgd 
     83  1.1  cgd char *malloc();
     84  1.1  cgd 
     85  1.1  cgd static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
     86  1.1  cgd 			  		AUTHTYPE_KERBEROS_V5, };
     87  1.1  cgd static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
     88  1.1  cgd 					TELQUAL_NAME, };
     89  1.1  cgd 
     90  1.1  cgd #define	KRB_AUTH	0		/* Authentication data follows */
     91  1.1  cgd #define	KRB_REJECT	1		/* Rejected (reason might follow) */
     92  1.1  cgd #define	KRB_ACCEPT	2		/* Accepted */
     93  1.1  cgd #define	KRB_CHALLANGE	3		/* Challange for mutual auth. */
     94  1.1  cgd #define	KRB_RESPONSE	4		/* Response for mutual auth. */
     95  1.1  cgd 
     96  1.1  cgd static	krb5_data auth;
     97  1.1  cgd 	/* telnetd gets session key from here */
     98  1.1  cgd static	krb5_tkt_authent *authdat = NULL;
     99  1.1  cgd 
    100  1.1  cgd #if	defined(ENCRYPT)
    101  1.1  cgd Block	session_key;
    102  1.1  cgd #endif
    103  1.1  cgd static Schedule sched;
    104  1.1  cgd static Block	challange;
    105  1.1  cgd 
    106  1.1  cgd 	static int
    107  1.1  cgd Data(ap, type, d, c)
    108  1.1  cgd 	Authenticator *ap;
    109  1.1  cgd 	int type;
    110  1.1  cgd 	void *d;
    111  1.1  cgd 	int c;
    112  1.1  cgd {
    113  1.1  cgd         unsigned char *p = str_data + 4;
    114  1.1  cgd 	unsigned char *cd = (unsigned char *)d;
    115  1.1  cgd 
    116  1.1  cgd 	if (c == -1)
    117  1.1  cgd 		c = strlen((char *)cd);
    118  1.1  cgd 
    119  1.1  cgd         if (auth_debug_mode) {
    120  1.1  cgd                 printf("%s:%d: [%d] (%d)",
    121  1.1  cgd                         str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
    122  1.1  cgd                         str_data[3],
    123  1.1  cgd                         type, c);
    124  1.1  cgd                 printd(d, c);
    125  1.1  cgd                 printf("\r\n");
    126  1.1  cgd         }
    127  1.1  cgd 	*p++ = ap->type;
    128  1.1  cgd 	*p++ = ap->way;
    129  1.1  cgd 	*p++ = type;
    130  1.1  cgd         while (c-- > 0) {
    131  1.1  cgd                 if ((*p++ = *cd++) == IAC)
    132  1.1  cgd                         *p++ = IAC;
    133  1.1  cgd         }
    134  1.1  cgd         *p++ = IAC;
    135  1.1  cgd         *p++ = SE;
    136  1.1  cgd 	if (str_data[3] == TELQUAL_IS)
    137  1.1  cgd 		printsub('>', &str_data[2], p - &str_data[2]);
    138  1.1  cgd         return(net_write(str_data, p - str_data));
    139  1.1  cgd }
    140  1.1  cgd 
    141  1.1  cgd 	int
    142  1.1  cgd kerberos5_init(ap, server)
    143  1.1  cgd 	Authenticator *ap;
    144  1.1  cgd 	int server;
    145  1.1  cgd {
    146  1.1  cgd 	if (server)
    147  1.1  cgd 		str_data[3] = TELQUAL_REPLY;
    148  1.1  cgd 	else
    149  1.1  cgd 		str_data[3] = TELQUAL_IS;
    150  1.1  cgd         krb5_init_ets();
    151  1.1  cgd 	return(1);
    152  1.1  cgd }
    153  1.1  cgd 
    154  1.1  cgd 	int
    155  1.1  cgd kerberos5_send(ap)
    156  1.1  cgd 	Authenticator *ap;
    157  1.1  cgd {
    158  1.1  cgd 	char **realms;
    159  1.1  cgd 	char *name;
    160  1.1  cgd 	char *p1, *p2;
    161  1.1  cgd 	krb5_checksum ksum;
    162  1.1  cgd 	krb5_octet sum[CRC32_CKSUM_LENGTH];
    163  1.1  cgd 	krb5_data *server[4];
    164  1.1  cgd 	krb5_data srvdata[3];
    165  1.1  cgd 	krb5_error_code r;
    166  1.1  cgd 	krb5_ccache ccache;
    167  1.1  cgd 	krb5_creds creds;		/* telnet gets session key from here */
    168  1.1  cgd 	extern krb5_flags krb5_kdc_default_options;
    169  1.1  cgd 
    170  1.1  cgd 	ksum.checksum_type = CKSUMTYPE_CRC32;
    171  1.1  cgd 	ksum.contents = sum;
    172  1.1  cgd 	ksum.length = sizeof(sum);
    173  1.1  cgd 	bzero((void *)sum, sizeof(sum));
    174  1.1  cgd 
    175  1.1  cgd         if (!UserNameRequested) {
    176  1.1  cgd                 if (auth_debug_mode) {
    177  1.1  cgd                         printf("Kerberos V5: no user name supplied\r\n");
    178  1.1  cgd                 }
    179  1.1  cgd                 return(0);
    180  1.1  cgd         }
    181  1.1  cgd 
    182  1.1  cgd 	if (r = krb5_cc_default(&ccache)) {
    183  1.1  cgd 		if (auth_debug_mode) {
    184  1.1  cgd 			printf("Kerberos V5: could not get default ccache\r\n");
    185  1.1  cgd 		}
    186  1.1  cgd 		return(0);
    187  1.1  cgd 	}
    188  1.1  cgd 
    189  1.1  cgd 	if ((name = malloc(strlen(RemoteHostName)+1)) == NULL) {
    190  1.1  cgd 		if (auth_debug_mode)
    191  1.1  cgd 			printf("Out of memory for hostname in Kerberos V5\r\n");
    192  1.1  cgd 		return(0);
    193  1.1  cgd 	}
    194  1.1  cgd 
    195  1.1  cgd 	if (r = krb5_get_host_realm(RemoteHostName, &realms)) {
    196  1.1  cgd 		if (auth_debug_mode)
    197  1.1  cgd 			printf("Kerberos V5: no realm for %s\r\n", RemoteHostName);
    198  1.1  cgd 		free(name);
    199  1.1  cgd 		return(0);
    200  1.1  cgd 	}
    201  1.1  cgd 
    202  1.1  cgd 	p1 = RemoteHostName;
    203  1.1  cgd 	p2 = name;
    204  1.1  cgd 
    205  1.1  cgd 	while (*p2 = *p1++) {
    206  1.1  cgd 		if (isupper(*p2))
    207  1.1  cgd 			*p2 |= 040;
    208  1.1  cgd 		++p2;
    209  1.1  cgd 	}
    210  1.1  cgd 
    211  1.1  cgd 	srvdata[0].data = realms[0];
    212  1.1  cgd 	srvdata[0].length = strlen(realms[0]);
    213  1.1  cgd 	srvdata[1].data = "rcmd";
    214  1.1  cgd 	srvdata[1].length = 4;
    215  1.1  cgd 	srvdata[2].data = name;
    216  1.1  cgd 	srvdata[2].length = p2 - name;
    217  1.1  cgd 
    218  1.1  cgd 	server[0] = &srvdata[0];
    219  1.1  cgd 	server[1] = &srvdata[1];
    220  1.1  cgd 	server[2] = &srvdata[2];
    221  1.1  cgd 	server[3] = 0;
    222  1.1  cgd 
    223  1.1  cgd 	bzero((char *)&creds, sizeof(creds));
    224  1.1  cgd 	creds.server = (krb5_principal)server;
    225  1.1  cgd 
    226  1.1  cgd 	if (r = krb5_cc_get_principal(ccache, &creds.client)) {
    227  1.1  cgd 		if (auth_debug_mode) {
    228  1.1  cgd 			printf("Keberos V5: failure on principal (%d)\r\n",
    229  1.1  cgd 				error_message(r));
    230  1.1  cgd 		}
    231  1.1  cgd 		free(name);
    232  1.1  cgd 		krb5_free_host_realm(realms);
    233  1.1  cgd 		return(0);
    234  1.1  cgd 	}
    235  1.1  cgd 
    236  1.1  cgd 	if (r = krb5_get_credentials(krb5_kdc_default_options, ccache, &creds)) {
    237  1.1  cgd 		if (auth_debug_mode) {
    238  1.1  cgd 			printf("Keberos V5: failure on credentials(%d)\r\n",r);
    239  1.1  cgd 		}
    240  1.1  cgd 		free(name);
    241  1.1  cgd 		krb5_free_host_realm(realms);
    242  1.1  cgd 		return(0);
    243  1.1  cgd 	}
    244  1.1  cgd 
    245  1.1  cgd 	r = krb5_mk_req_extended(0, &ksum, &creds.times,
    246  1.1  cgd 				 krb5_kdc_default_options,
    247  1.1  cgd 				 ccache, &creds, 0, &auth);
    248  1.1  cgd 
    249  1.1  cgd 	free(name);
    250  1.1  cgd 	krb5_free_host_realm(realms);
    251  1.1  cgd 	if (r) {
    252  1.1  cgd 		if (auth_debug_mode) {
    253  1.1  cgd 			printf("Keberos V5: mk_req failed\r\n");
    254  1.1  cgd 		}
    255  1.1  cgd 		return(0);
    256  1.1  cgd 	}
    257  1.1  cgd 
    258  1.1  cgd         if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
    259  1.1  cgd                 if (auth_debug_mode)
    260  1.1  cgd                         printf("Not enough room for user name\r\n");
    261  1.1  cgd                 return(0);
    262  1.1  cgd         }
    263  1.1  cgd 	if (!Data(ap, KRB_AUTH, auth.data, auth.length)) {
    264  1.1  cgd 		if (auth_debug_mode)
    265  1.1  cgd 			printf("Not enough room for authentication data\r\n");
    266  1.1  cgd 		return(0);
    267  1.1  cgd 	}
    268  1.1  cgd 	/*
    269  1.1  cgd 	 * If we are doing mutual authentication, get set up to send
    270  1.1  cgd 	 * the challange, and verify it when the response comes back.
    271  1.1  cgd 	 */
    272  1.1  cgd 	if (((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
    273  1.1  cgd 	    && (creds.keyblock.keytype == KEYTYPE_DES)) {
    274  1.1  cgd 		register int i;
    275  1.1  cgd 
    276  1.1  cgd 		des_key_sched(creds.keyblock.contents, sched);
    277  1.1  cgd 		des_set_random_generator_seed(creds.keyblock.contents);
    278  1.1  cgd 		des_new_random_key(challange);
    279  1.1  cgd 		des_ecb_encrypt(challange, session_key, sched, 1);
    280  1.1  cgd 		/*
    281  1.1  cgd 		 * Increment the challange by 1, and encrypt it for
    282  1.1  cgd 		 * later comparison.
    283  1.1  cgd 		 */
    284  1.1  cgd 		for (i = 7; i >= 0; --i) {
    285  1.1  cgd 			register int x;
    286  1.1  cgd 			x = (unsigned int)challange[i] + 1;
    287  1.1  cgd 			challange[i] = x;	/* ignore overflow */
    288  1.1  cgd 			if (x < 256)		/* if no overflow, all done */
    289  1.1  cgd 				break;
    290  1.1  cgd 		}
    291  1.1  cgd 		des_ecb_encrypt(challange, challange, sched, 1);
    292  1.1  cgd 	}
    293  1.1  cgd 
    294  1.1  cgd 	if (auth_debug_mode) {
    295  1.1  cgd 		printf("Sent Kerberos V5 credentials to server\r\n");
    296  1.1  cgd 	}
    297  1.1  cgd 	return(1);
    298  1.1  cgd }
    299  1.1  cgd 
    300  1.1  cgd 	void
    301  1.1  cgd kerberos5_is(ap, data, cnt)
    302  1.1  cgd 	Authenticator *ap;
    303  1.1  cgd 	unsigned char *data;
    304  1.1  cgd 	int cnt;
    305  1.1  cgd {
    306  1.1  cgd 	int r;
    307  1.1  cgd 	struct hostent *hp;
    308  1.1  cgd 	char *p1, *p2;
    309  1.1  cgd 	static char *realm = NULL;
    310  1.1  cgd 	krb5_data *server[4];
    311  1.1  cgd 	krb5_data srvdata[3];
    312  1.1  cgd         Session_Key skey;
    313  1.1  cgd 	char *name;
    314  1.1  cgd 	char *getenv();
    315  1.1  cgd 
    316  1.1  cgd 	if (cnt-- < 1)
    317  1.1  cgd 		return;
    318  1.1  cgd 	switch (*data++) {
    319  1.1  cgd 	case KRB_AUTH:
    320  1.1  cgd 		auth.data = (char *)data;
    321  1.1  cgd 		auth.length = cnt;
    322  1.1  cgd 
    323  1.1  cgd 		if (!(hp = gethostbyname(LocalHostName))) {
    324  1.1  cgd 			if (auth_debug_mode)
    325  1.1  cgd 				printf("Cannot resolve local host name\r\n");
    326  1.1  cgd 			Data(ap, KRB_REJECT, "Unknown local hostname.", -1);
    327  1.1  cgd 			auth_finished(ap, AUTH_REJECT);
    328  1.1  cgd 			return;
    329  1.1  cgd 		}
    330  1.1  cgd 
    331  1.1  cgd 		if (!realm && (krb5_get_default_realm(&realm))) {
    332  1.1  cgd 			if (auth_debug_mode)
    333  1.1  cgd 				printf("Could not get defualt realm\r\n");
    334  1.1  cgd 			Data(ap, KRB_REJECT, "Could not get default realm.", -1);
    335  1.1  cgd 			auth_finished(ap, AUTH_REJECT);
    336  1.1  cgd 			return;
    337  1.1  cgd 		}
    338  1.1  cgd 
    339  1.1  cgd 		if ((name = malloc(strlen(hp->h_name)+1)) == NULL) {
    340  1.1  cgd 			if (auth_debug_mode)
    341  1.1  cgd 				printf("Out of memory for hostname in Kerberos V5\r\n");
    342  1.1  cgd 			Data(ap, KRB_REJECT, "Out of memory.", -1);
    343  1.1  cgd 			auth_finished(ap, AUTH_REJECT);
    344  1.1  cgd 			return;
    345  1.1  cgd 		}
    346  1.1  cgd 
    347  1.1  cgd 		p1 = hp->h_name;
    348  1.1  cgd 		p2 = name;
    349  1.1  cgd 
    350  1.1  cgd 		while (*p2 = *p1++) {
    351  1.1  cgd 			if (isupper(*p2))
    352  1.1  cgd 				*p2 |= 040;
    353  1.1  cgd 			++p2;
    354  1.1  cgd 		}
    355  1.1  cgd 
    356  1.1  cgd 		srvdata[0].data = realm;
    357  1.1  cgd 		srvdata[0].length = strlen(realm);
    358  1.1  cgd 		srvdata[1].data = "rcmd";
    359  1.1  cgd 		srvdata[1].length = 4;
    360  1.1  cgd 		srvdata[2].data = name;
    361  1.1  cgd 		srvdata[2].length = p2 - name;
    362  1.1  cgd 
    363  1.1  cgd 		server[0] = &srvdata[0];
    364  1.1  cgd 		server[1] = &srvdata[1];
    365  1.1  cgd 		server[2] = &srvdata[2];
    366  1.1  cgd 		server[3] = 0;
    367  1.1  cgd 
    368  1.1  cgd 		if (authdat)
    369  1.1  cgd 			krb5_free_tkt_authent(authdat);
    370  1.1  cgd 		if (r = krb5_rd_req_simple(&auth, server, 0, &authdat)) {
    371  1.1  cgd 			char errbuf[128];
    372  1.1  cgd 
    373  1.1  cgd 			authdat = 0;
    374  1.1  cgd 			(void) strcpy(errbuf, "Read req failed: ");
    375  1.1  cgd 			(void) strcat(errbuf, error_message(r));
    376  1.1  cgd 			Data(ap, KRB_REJECT, errbuf, -1);
    377  1.1  cgd 			if (auth_debug_mode)
    378  1.1  cgd 				printf("%s\r\n", errbuf);
    379  1.1  cgd 			return;
    380  1.1  cgd 		}
    381  1.1  cgd 		free(name);
    382  1.1  cgd 		if (krb5_unparse_name(authdat->ticket->enc_part2 ->client,
    383  1.1  cgd 				      					&name))
    384  1.1  cgd 			name = 0;
    385  1.1  cgd 		Data(ap, KRB_ACCEPT, name, name ? -1 : 0);
    386  1.1  cgd 		if (auth_debug_mode) {
    387  1.1  cgd 			printf("Kerberos5 accepting him as ``%s''\r\n",
    388  1.1  cgd 							name ? name : "");
    389  1.1  cgd 		}
    390  1.1  cgd                 auth_finished(ap, AUTH_USER);
    391  1.1  cgd 		if (authdat->ticket->enc_part2->session->keytype != KEYTYPE_DES)
    392  1.1  cgd 			break;
    393  1.1  cgd 		bcopy((void *)authdat->ticket->enc_part2->session->contents,
    394  1.1  cgd 		      (void *)session_key, sizeof(Block));
    395  1.1  cgd 		break;
    396  1.1  cgd 
    397  1.1  cgd 	case KRB_CHALLANGE:
    398  1.1  cgd 		if (!VALIDKEY(session_key)) {
    399  1.1  cgd 			/*
    400  1.1  cgd 			 * We don't have a valid session key, so just
    401  1.1  cgd 			 * send back a response with an empty session
    402  1.1  cgd 			 * key.
    403  1.1  cgd 			 */
    404  1.1  cgd 			Data(ap, KRB_RESPONSE, (void *)0, 0);
    405  1.1  cgd 			break;
    406  1.1  cgd 		}
    407  1.1  cgd 
    408  1.1  cgd 		des_key_sched(session_key, sched);
    409  1.1  cgd 		bcopy((void *)data, (void *)datablock, sizeof(Block));
    410  1.1  cgd 		/*
    411  1.1  cgd 		 * Take the received encrypted challange, and encrypt
    412  1.1  cgd 		 * it again to get a unique session_key for the
    413  1.1  cgd 		 * ENCRYPT option.
    414  1.1  cgd 		 */
    415  1.1  cgd 		des_ecb_encrypt(datablock, session_key, sched, 1);
    416  1.1  cgd 		skey.type = SK_DES;
    417  1.1  cgd 		skey.length = 8;
    418  1.1  cgd 		skey.data = session_key;
    419  1.1  cgd 		encrypt_session_key(&skey, 1);
    420  1.1  cgd 		/*
    421  1.1  cgd 		 * Now decrypt the received encrypted challange,
    422  1.1  cgd 		 * increment by one, re-encrypt it and send it back.
    423  1.1  cgd 		 */
    424  1.1  cgd 		des_ecb_encrypt(datablock, challange, sched, 0);
    425  1.1  cgd 		for (r = 7; r >= 0; r++) {
    426  1.1  cgd 			register int t;
    427  1.1  cgd 			t = (unsigned int)challange[r] + 1;
    428  1.1  cgd 			challange[r] = t;	/* ignore overflow */
    429  1.1  cgd 			if (t < 256)		/* if no overflow, all done */
    430  1.1  cgd 				break;
    431  1.1  cgd 		}
    432  1.1  cgd 		des_ecb_encrypt(challange, challange, sched, 1);
    433  1.1  cgd 		Data(ap, KRB_RESPONSE, (void *)challange, sizeof(challange));
    434  1.1  cgd 		break;
    435  1.1  cgd 
    436  1.1  cgd 	default:
    437  1.1  cgd 		if (auth_debug_mode)
    438  1.1  cgd 			printf("Unknown Kerberos option %d\r\n", data[-1]);
    439  1.1  cgd 		Data(ap, KRB_REJECT, 0, 0);
    440  1.1  cgd 		break;
    441  1.1  cgd 	}
    442  1.1  cgd }
    443  1.1  cgd 
    444  1.1  cgd 	void
    445  1.1  cgd kerberos5_reply(ap, data, cnt)
    446  1.1  cgd 	Authenticator *ap;
    447  1.1  cgd 	unsigned char *data;
    448  1.1  cgd 	int cnt;
    449  1.1  cgd {
    450  1.1  cgd         Session_Key skey;
    451  1.1  cgd 
    452  1.1  cgd 	if (cnt-- < 1)
    453  1.1  cgd 		return;
    454  1.1  cgd 	switch (*data++) {
    455  1.1  cgd 	case KRB_REJECT:
    456  1.1  cgd 		if (cnt > 0) {
    457  1.1  cgd 			printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
    458  1.1  cgd 				cnt, data);
    459  1.1  cgd 		} else
    460  1.1  cgd 			printf("[ Kerberos V5 refuses authentication ]\r\n");
    461  1.1  cgd 		auth_send_retry();
    462  1.1  cgd 		return;
    463  1.1  cgd 	case KRB_ACCEPT:
    464  1.1  cgd 		printf("[ Kerberos V5 accepts you ]\n", cnt, data);
    465  1.1  cgd 		if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
    466  1.1  cgd 			/*
    467  1.1  cgd 			 * Send over the encrypted challange.
    468  1.1  cgd 		 	 */
    469  1.1  cgd 			Data(ap, KRB_CHALLANGE, (void *)session_key,
    470  1.1  cgd 						sizeof(session_key));
    471  1.1  cgd #if	defined(ENCRYPT)
    472  1.1  cgd 			des_ecb_encrypt(session_key, session_key, sched, 1);
    473  1.1  cgd 			skey.type = SK_DES;
    474  1.1  cgd 			skey.length = 8;
    475  1.1  cgd 			skey.data = session_key;
    476  1.1  cgd 			encrypt_session_key(&skey, 0);
    477  1.1  cgd #endif
    478  1.1  cgd 			return;
    479  1.1  cgd 		}
    480  1.1  cgd 		auth_finished(ap, AUTH_USER);
    481  1.1  cgd 		return;
    482  1.1  cgd 	case KRB_RESPONSE:
    483  1.1  cgd 		/*
    484  1.1  cgd 		 * Verify that the response to the challange is correct.
    485  1.1  cgd 		 */
    486  1.1  cgd 		if ((cnt != sizeof(Block)) ||
    487  1.1  cgd 		    (0 != memcmp((void *)data, (void *)challange,
    488  1.1  cgd 						sizeof(challange))))
    489  1.1  cgd 		{
    490  1.1  cgd 			printf("[ Kerberos V5 challange failed!!! ]\r\n");
    491  1.1  cgd 			auth_send_retry();
    492  1.1  cgd 			return;
    493  1.1  cgd 		}
    494  1.1  cgd 		printf("[ Kerberos V5 challange successful ]\r\n");
    495  1.1  cgd 		auth_finished(ap, AUTH_USER);
    496  1.1  cgd 		break;
    497  1.1  cgd 	default:
    498  1.1  cgd 		if (auth_debug_mode)
    499  1.1  cgd 			printf("Unknown Kerberos option %d\r\n", data[-1]);
    500  1.1  cgd 		return;
    501  1.1  cgd 	}
    502  1.1  cgd }
    503  1.1  cgd 
    504  1.1  cgd 	int
    505  1.1  cgd kerberos5_status(ap, name, level)
    506  1.1  cgd 	Authenticator *ap;
    507  1.1  cgd 	char *name;
    508  1.1  cgd 	int level;
    509  1.1  cgd {
    510  1.1  cgd 	if (level < AUTH_USER)
    511  1.1  cgd 		return(level);
    512  1.1  cgd 
    513  1.1  cgd 	if (UserNameRequested &&
    514  1.1  cgd 	    krb5_kuserok(authdat->ticket->enc_part2->client, UserNameRequested))
    515  1.1  cgd 	{
    516  1.1  cgd 		strcpy(name, UserNameRequested);
    517  1.1  cgd 		return(AUTH_VALID);
    518  1.1  cgd 	} else
    519  1.1  cgd 		return(AUTH_USER);
    520  1.1  cgd }
    521  1.1  cgd 
    522  1.1  cgd #define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
    523  1.1  cgd #define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len));}
    524  1.1  cgd 
    525  1.1  cgd 	void
    526  1.1  cgd kerberos5_printsub(data, cnt, buf, buflen)
    527  1.1  cgd 	unsigned char *data, *buf;
    528  1.1  cgd 	int cnt, buflen;
    529  1.1  cgd {
    530  1.1  cgd 	char lbuf[32];
    531  1.1  cgd 	register int i;
    532  1.1  cgd 
    533  1.1  cgd 	buf[buflen-1] = '\0';		/* make sure its NULL terminated */
    534  1.1  cgd 	buflen -= 1;
    535  1.1  cgd 
    536  1.1  cgd 	switch(data[3]) {
    537  1.1  cgd 	case KRB_REJECT:		/* Rejected (reason might follow) */
    538  1.1  cgd 		strncpy((char *)buf, " REJECT ", buflen);
    539  1.1  cgd 		goto common;
    540  1.1  cgd 
    541  1.1  cgd 	case KRB_ACCEPT:		/* Accepted (name might follow) */
    542  1.1  cgd 		strncpy((char *)buf, " ACCEPT ", buflen);
    543  1.1  cgd 	common:
    544  1.1  cgd 		BUMP(buf, buflen);
    545  1.1  cgd 		if (cnt <= 4)
    546  1.1  cgd 			break;
    547  1.1  cgd 		ADDC(buf, buflen, '"');
    548  1.1  cgd 		for (i = 4; i < cnt; i++)
    549  1.1  cgd 			ADDC(buf, buflen, data[i]);
    550  1.1  cgd 		ADDC(buf, buflen, '"');
    551  1.1  cgd 		ADDC(buf, buflen, '\0');
    552  1.1  cgd 		break;
    553  1.1  cgd 
    554  1.1  cgd 	case KRB_AUTH:			/* Authentication data follows */
    555  1.1  cgd 		strncpy((char *)buf, " AUTH", buflen);
    556  1.1  cgd 		goto common2;
    557  1.1  cgd 
    558  1.1  cgd 	case KRB_CHALLANGE:
    559  1.1  cgd 		strncpy((char *)buf, " CHALLANGE", buflen);
    560  1.1  cgd 		goto common2;
    561  1.1  cgd 
    562  1.1  cgd 	case KRB_RESPONSE:
    563  1.1  cgd 		strncpy((char *)buf, " RESPONSE", buflen);
    564  1.1  cgd 		goto common2;
    565  1.1  cgd 
    566  1.1  cgd 	default:
    567  1.1  cgd 		sprintf(lbuf, " %d (unknown)", data[3]);
    568  1.1  cgd 		strncpy((char *)buf, lbuf, buflen);
    569  1.1  cgd 	common2:
    570  1.1  cgd 		BUMP(buf, buflen);
    571  1.1  cgd 		for (i = 4; i < cnt; i++) {
    572  1.1  cgd 			sprintf(lbuf, " %d", data[i]);
    573  1.1  cgd 			strncpy((char *)buf, lbuf, buflen);
    574  1.1  cgd 			BUMP(buf, buflen);
    575  1.1  cgd 		}
    576  1.1  cgd 		break;
    577  1.1  cgd 	}
    578  1.1  cgd }
    579  1.1  cgd #endif
    580