Home | History | Annotate | Line # | Download | only in libtelnet
auth.c revision 1.2
      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.2  mycroft /*static char sccsid[] = "from: @(#)auth.c	5.2 (Berkeley) 3/22/91";*/
     36  1.2  mycroft static char rcsid[] = "$Id: auth.c,v 1.2 1993/08/01 18:32:46 mycroft Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd /*
     40  1.1      cgd  * Copyright (C) 1990 by the Massachusetts Institute of Technology
     41  1.1      cgd  *
     42  1.1      cgd  * Export of this software from the United States of America is assumed
     43  1.1      cgd  * to require a specific license from the United States Government.
     44  1.1      cgd  * It is the responsibility of any person or organization contemplating
     45  1.1      cgd  * export to obtain such a license before exporting.
     46  1.1      cgd  *
     47  1.1      cgd  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
     48  1.1      cgd  * distribute this software and its documentation for any purpose and
     49  1.1      cgd  * without fee is hereby granted, provided that the above copyright
     50  1.1      cgd  * notice appear in all copies and that both that copyright notice and
     51  1.1      cgd  * this permission notice appear in supporting documentation, and that
     52  1.1      cgd  * the name of M.I.T. not be used in advertising or publicity pertaining
     53  1.1      cgd  * to distribution of the software without specific, written prior
     54  1.1      cgd  * permission.  M.I.T. makes no representations about the suitability of
     55  1.1      cgd  * this software for any purpose.  It is provided "as is" without express
     56  1.1      cgd  * or implied warranty.
     57  1.1      cgd  */
     58  1.1      cgd 
     59  1.1      cgd 
     60  1.1      cgd #if	defined(AUTHENTICATE)
     61  1.1      cgd #include <stdio.h>
     62  1.1      cgd #include <sys/types.h>
     63  1.1      cgd #include <signal.h>
     64  1.1      cgd #define	AUTH_NAMES
     65  1.1      cgd #include <arpa/telnet.h>
     66  1.1      cgd #ifdef	__STDC__
     67  1.1      cgd #include <stdlib.h>
     68  1.1      cgd #endif
     69  1.1      cgd #ifdef	NO_STRING_H
     70  1.1      cgd #include <strings.h>
     71  1.1      cgd #else
     72  1.1      cgd #include <string.h>
     73  1.1      cgd #endif
     74  1.1      cgd 
     75  1.1      cgd #include "encrypt.h"
     76  1.1      cgd #include "auth.h"
     77  1.1      cgd #include "misc-proto.h"
     78  1.1      cgd #include "auth-proto.h"
     79  1.1      cgd 
     80  1.1      cgd #define	typemask(x)		(1<<((x)-1))
     81  1.1      cgd 
     82  1.1      cgd int auth_debug_mode = 0;
     83  1.1      cgd static 	char	*Name = "Noname";
     84  1.1      cgd static	int	Server = 0;
     85  1.1      cgd static	Authenticator	*authenticated = 0;
     86  1.1      cgd static	int	authenticating = 0;
     87  1.1      cgd static	int	validuser = 0;
     88  1.1      cgd static	unsigned char	_auth_send_data[256];
     89  1.1      cgd static	unsigned char	*auth_send_data;
     90  1.1      cgd static	int	auth_send_cnt = 0;
     91  1.1      cgd 
     92  1.1      cgd /*
     93  1.1      cgd  * Authentication types supported.  Plese note that these are stored
     94  1.1      cgd  * in priority order, i.e. try the first one first.
     95  1.1      cgd  */
     96  1.1      cgd Authenticator authenticators[] = {
     97  1.1      cgd #ifdef	KRB5
     98  1.1      cgd 	{ AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL,
     99  1.1      cgd 				kerberos5_init,
    100  1.1      cgd 				kerberos5_send,
    101  1.1      cgd 				kerberos5_is,
    102  1.1      cgd 				kerberos5_reply,
    103  1.1      cgd 				kerberos5_status,
    104  1.1      cgd 				kerberos5_printsub },
    105  1.1      cgd 	{ AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
    106  1.1      cgd 				kerberos5_init,
    107  1.1      cgd 				kerberos5_send,
    108  1.1      cgd 				kerberos5_is,
    109  1.1      cgd 				kerberos5_reply,
    110  1.1      cgd 				kerberos5_status,
    111  1.1      cgd 				kerberos5_printsub },
    112  1.1      cgd #endif
    113  1.1      cgd #ifdef	KRB4
    114  1.1      cgd 	{ AUTHTYPE_KERBEROS_V4, AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL,
    115  1.1      cgd 				kerberos4_init,
    116  1.1      cgd 				kerberos4_send,
    117  1.1      cgd 				kerberos4_is,
    118  1.1      cgd 				kerberos4_reply,
    119  1.1      cgd 				kerberos4_status,
    120  1.1      cgd 				kerberos4_printsub },
    121  1.1      cgd 	{ AUTHTYPE_KERBEROS_V4, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
    122  1.1      cgd 				kerberos4_init,
    123  1.1      cgd 				kerberos4_send,
    124  1.1      cgd 				kerberos4_is,
    125  1.1      cgd 				kerberos4_reply,
    126  1.1      cgd 				kerberos4_status,
    127  1.1      cgd 				kerberos4_printsub },
    128  1.1      cgd #endif
    129  1.1      cgd 	{ 0, },
    130  1.1      cgd };
    131  1.1      cgd 
    132  1.1      cgd static Authenticator NoAuth = { 0 };
    133  1.1      cgd 
    134  1.1      cgd static int	i_support = 0;
    135  1.1      cgd static int	i_wont_support = 0;
    136  1.1      cgd 
    137  1.1      cgd 	Authenticator *
    138  1.1      cgd findauthenticator(type, way)
    139  1.1      cgd 	int type;
    140  1.1      cgd 	int way;
    141  1.1      cgd {
    142  1.1      cgd 	Authenticator *ap = authenticators;
    143  1.1      cgd 
    144  1.1      cgd 	while (ap->type && (ap->type != type || ap->way != way))
    145  1.1      cgd 		++ap;
    146  1.1      cgd 	return(ap->type ? ap : 0);
    147  1.1      cgd }
    148  1.1      cgd 
    149  1.1      cgd 	void
    150  1.1      cgd auth_init(name, server)
    151  1.1      cgd 	char *name;
    152  1.1      cgd 	int server;
    153  1.1      cgd {
    154  1.1      cgd 	Authenticator *ap = authenticators;
    155  1.1      cgd 
    156  1.1      cgd 	Server = server;
    157  1.1      cgd 	Name = name;
    158  1.1      cgd 
    159  1.1      cgd 	i_support = 0;
    160  1.1      cgd 	authenticated = 0;
    161  1.1      cgd 	authenticating = 0;
    162  1.1      cgd 	while (ap->type) {
    163  1.1      cgd 		if (!ap->init || (*ap->init)(ap, server)) {
    164  1.1      cgd 			i_support |= typemask(ap->type);
    165  1.1      cgd 			if (auth_debug_mode)
    166  1.1      cgd 				printf(">>>%s: I support auth type %d %d\r\n",
    167  1.1      cgd 					Name,
    168  1.1      cgd 					ap->type, ap->way);
    169  1.1      cgd 		}
    170  1.1      cgd 		++ap;
    171  1.1      cgd 	}
    172  1.1      cgd }
    173  1.1      cgd 
    174  1.1      cgd 	void
    175  1.1      cgd auth_disable_name(name)
    176  1.1      cgd 	char *name;
    177  1.1      cgd {
    178  1.1      cgd 	int x;
    179  1.1      cgd 	for (x = 0; x < AUTHTYPE_CNT; ++x) {
    180  1.1      cgd 		if (!strcasecmp(name, AUTHTYPE_NAME(x))) {
    181  1.1      cgd 			i_wont_support |= typemask(x);
    182  1.1      cgd 			break;
    183  1.1      cgd 		}
    184  1.1      cgd 	}
    185  1.1      cgd }
    186  1.1      cgd 
    187  1.1      cgd 	int
    188  1.1      cgd getauthmask(type, maskp)
    189  1.1      cgd 	char *type;
    190  1.1      cgd 	int *maskp;
    191  1.1      cgd {
    192  1.1      cgd 	register int x;
    193  1.1      cgd 
    194  1.1      cgd 	if (strcasecmp(type, AUTHTYPE_NAME(0))) {
    195  1.1      cgd 		*maskp = -1;
    196  1.1      cgd 		return(1);
    197  1.1      cgd 	}
    198  1.1      cgd 
    199  1.1      cgd 	for (x = 1; x < AUTHTYPE_CNT; ++x) {
    200  1.1      cgd 		if (!strcasecmp(type, AUTHTYPE_NAME(x))) {
    201  1.1      cgd 			*maskp = typemask(x);
    202  1.1      cgd 			return(1);
    203  1.1      cgd 		}
    204  1.1      cgd 	}
    205  1.1      cgd 	return(0);
    206  1.1      cgd }
    207  1.1      cgd 
    208  1.1      cgd 	int
    209  1.1      cgd auth_enable(type)
    210  1.1      cgd 	int type;
    211  1.1      cgd {
    212  1.1      cgd 	return(auth_onoff(type, 1));
    213  1.1      cgd }
    214  1.1      cgd 
    215  1.1      cgd 	int
    216  1.1      cgd auth_disable(type)
    217  1.1      cgd 	int type;
    218  1.1      cgd {
    219  1.1      cgd 	return(auth_onoff(type, 0));
    220  1.1      cgd }
    221  1.1      cgd 
    222  1.1      cgd 	int
    223  1.1      cgd auth_onoff(type, on)
    224  1.1      cgd 	char *type;
    225  1.1      cgd 	int on;
    226  1.1      cgd {
    227  1.1      cgd 	int mask = -1;
    228  1.1      cgd 	Authenticator *ap;
    229  1.1      cgd 
    230  1.1      cgd 	if (!strcasecmp(type, "?") || !strcasecmp(type, "help")) {
    231  1.1      cgd                 printf("auth %s 'type'\n", on ? "enable" : "disable");
    232  1.1      cgd 		printf("Where 'type' is one of:\n");
    233  1.1      cgd 		printf("\t%s\n", AUTHTYPE_NAME(0));
    234  1.1      cgd 		for (ap = authenticators; ap->type; ap++)
    235  1.1      cgd 			printf("\t%s\n", AUTHTYPE_NAME(ap->type));
    236  1.1      cgd 		return(0);
    237  1.1      cgd 	}
    238  1.1      cgd 
    239  1.1      cgd 	if (!getauthmask(type, &mask)) {
    240  1.1      cgd 		printf("%s: invalid authentication type\n", type);
    241  1.1      cgd 		return(0);
    242  1.1      cgd 	}
    243  1.1      cgd 	mask = getauthmask(type, &mask);
    244  1.1      cgd 	if (on)
    245  1.1      cgd 		i_wont_support &= ~mask;
    246  1.1      cgd 	else
    247  1.1      cgd 		i_wont_support |= mask;
    248  1.1      cgd 	return(1);
    249  1.1      cgd }
    250  1.1      cgd 
    251  1.1      cgd 	int
    252  1.1      cgd auth_togdebug(on)
    253  1.1      cgd 	int on;
    254  1.1      cgd {
    255  1.1      cgd 	if (on < 0)
    256  1.1      cgd 		auth_debug_mode ^= 1;
    257  1.1      cgd 	else
    258  1.1      cgd 		auth_debug_mode = on;
    259  1.1      cgd 	printf("auth debugging %s\n", auth_debug_mode ? "enabled" : "disabled");
    260  1.1      cgd 	return(1);
    261  1.1      cgd }
    262  1.1      cgd 
    263  1.1      cgd 	int
    264  1.1      cgd auth_status()
    265  1.1      cgd {
    266  1.1      cgd 	Authenticator *ap;
    267  1.1      cgd 
    268  1.1      cgd 	if (i_wont_support == -1)
    269  1.1      cgd 		printf("Authentication disabled\n");
    270  1.1      cgd 	else
    271  1.1      cgd 		printf("Authentication enabled\n");
    272  1.1      cgd 
    273  1.1      cgd 	for (ap = authenticators; ap->type; ap++)
    274  1.1      cgd 		printf("%s: %s\n", AUTHTYPE_NAME(ap->type),
    275  1.1      cgd 			(i_wont_support & typemask(ap->type)) ?
    276  1.1      cgd 					"disabled" : "enabled");
    277  1.1      cgd 	return(1);
    278  1.1      cgd }
    279  1.1      cgd 
    280  1.1      cgd /*
    281  1.1      cgd  * This routine is called by the server to start authentication
    282  1.1      cgd  * negotiation.
    283  1.1      cgd  */
    284  1.1      cgd 	void
    285  1.1      cgd auth_request()
    286  1.1      cgd {
    287  1.1      cgd 	static unsigned char str_request[64] = { IAC, SB,
    288  1.1      cgd 						 TELOPT_AUTHENTICATION,
    289  1.1      cgd 						 TELQUAL_SEND, };
    290  1.1      cgd 	Authenticator *ap = authenticators;
    291  1.1      cgd 	unsigned char *e = str_request + 4;
    292  1.1      cgd 
    293  1.1      cgd 	if (!authenticating) {
    294  1.1      cgd 		authenticating = 1;
    295  1.1      cgd 		while (ap->type) {
    296  1.1      cgd 			if (i_support & ~i_wont_support & typemask(ap->type)) {
    297  1.1      cgd 				if (auth_debug_mode) {
    298  1.1      cgd 					printf(">>>%s: Sending type %d %d\r\n",
    299  1.1      cgd 						Name, ap->type, ap->way);
    300  1.1      cgd 				}
    301  1.1      cgd 				*e++ = ap->type;
    302  1.1      cgd 				*e++ = ap->way;
    303  1.1      cgd 			}
    304  1.1      cgd 			++ap;
    305  1.1      cgd 		}
    306  1.1      cgd 		*e++ = IAC;
    307  1.1      cgd 		*e++ = SE;
    308  1.1      cgd 		net_write(str_request, e - str_request);
    309  1.1      cgd 		printsub('>', &str_request[2], e - str_request - 2);
    310  1.1      cgd 	}
    311  1.1      cgd }
    312  1.1      cgd 
    313  1.1      cgd /*
    314  1.1      cgd  * This is called when an AUTH SEND is received.
    315  1.1      cgd  * It should never arrive on the server side (as only the server can
    316  1.1      cgd  * send an AUTH SEND).
    317  1.1      cgd  * You should probably respond to it if you can...
    318  1.1      cgd  *
    319  1.1      cgd  * If you want to respond to the types out of order (i.e. even
    320  1.1      cgd  * if he sends  LOGIN KERBEROS and you support both, you respond
    321  1.1      cgd  * with KERBEROS instead of LOGIN (which is against what the
    322  1.1      cgd  * protocol says)) you will have to hack this code...
    323  1.1      cgd  */
    324  1.1      cgd 	void
    325  1.1      cgd auth_send(data, cnt)
    326  1.1      cgd 	unsigned char *data;
    327  1.1      cgd 	int cnt;
    328  1.1      cgd {
    329  1.1      cgd 	Authenticator *ap;
    330  1.1      cgd 	static unsigned char str_none[] = { IAC, SB, TELOPT_AUTHENTICATION,
    331  1.1      cgd 					    TELQUAL_IS, AUTHTYPE_NULL, 0,
    332  1.1      cgd 					    IAC, SE };
    333  1.1      cgd 	if (Server) {
    334  1.1      cgd 		if (auth_debug_mode) {
    335  1.1      cgd 			printf(">>>%s: auth_send called!\r\n", Name);
    336  1.1      cgd 		}
    337  1.1      cgd 		return;
    338  1.1      cgd 	}
    339  1.1      cgd 
    340  1.1      cgd 	if (auth_debug_mode) {
    341  1.1      cgd 		printf(">>>%s: auth_send got:", Name);
    342  1.1      cgd 		printd(data, cnt); printf("\r\n");
    343  1.1      cgd 	}
    344  1.1      cgd 
    345  1.1      cgd 	/*
    346  1.1      cgd 	 * Save the data, if it is new, so that we can continue looking
    347  1.1      cgd 	 * at it if the authorization we try doesn't work
    348  1.1      cgd 	 */
    349  1.1      cgd 	if (data < _auth_send_data ||
    350  1.1      cgd 	    data > _auth_send_data + sizeof(_auth_send_data)) {
    351  1.1      cgd 		auth_send_cnt = cnt > sizeof(_auth_send_data)
    352  1.1      cgd 					? sizeof(_auth_send_data)
    353  1.1      cgd 					: cnt;
    354  1.1      cgd 		bcopy((void *)data, (void *)_auth_send_data, auth_send_cnt);
    355  1.1      cgd 		auth_send_data = _auth_send_data;
    356  1.1      cgd 	} else {
    357  1.1      cgd 		/*
    358  1.1      cgd 		 * This is probably a no-op, but we just make sure
    359  1.1      cgd 		 */
    360  1.1      cgd 		auth_send_data = data;
    361  1.1      cgd 		auth_send_cnt = cnt;
    362  1.1      cgd 	}
    363  1.1      cgd 	while ((auth_send_cnt -= 2) >= 0) {
    364  1.1      cgd 		if (auth_debug_mode)
    365  1.1      cgd 			printf(">>>%s: He supports %d\r\n",
    366  1.1      cgd 				Name, *auth_send_data);
    367  1.1      cgd 		if ((i_support & ~i_wont_support) & typemask(*auth_send_data)) {
    368  1.1      cgd 			ap = findauthenticator(auth_send_data[0],
    369  1.1      cgd 					       auth_send_data[1]);
    370  1.1      cgd 			if (!ap) {
    371  1.1      cgd 				printf("Internal state error: cannot find authentication type %d a second time\r\n", *auth_send_data);
    372  1.1      cgd 			} else if (ap->send) {
    373  1.1      cgd 				if (auth_debug_mode)
    374  1.1      cgd 					printf(">>>%s: Trying %d %d\r\n",
    375  1.1      cgd 						Name, auth_send_data[0],
    376  1.1      cgd 							auth_send_data[1]);
    377  1.1      cgd 				if ((*ap->send)(ap)) {
    378  1.1      cgd 					/*
    379  1.1      cgd 					 * Okay, we found one we like
    380  1.1      cgd 					 * and did it.
    381  1.1      cgd 					 * we can go home now.
    382  1.1      cgd 					 */
    383  1.1      cgd 					if (auth_debug_mode)
    384  1.1      cgd 						printf(">>>%s: Using type %d\r\n",
    385  1.1      cgd 							Name, *auth_send_data);
    386  1.1      cgd 					auth_send_data += 2;
    387  1.1      cgd 					return;
    388  1.1      cgd 				}
    389  1.1      cgd 			}
    390  1.1      cgd 			/* else
    391  1.1      cgd 			 *	just continue on and look for the
    392  1.1      cgd 			 *	next one if we didn't do anything.
    393  1.1      cgd 			 */
    394  1.1      cgd 		}
    395  1.1      cgd 		auth_send_data += 2;
    396  1.1      cgd 	}
    397  1.1      cgd 	net_write(str_none, sizeof(str_none));
    398  1.1      cgd 	printsub('>', &str_none[2], sizeof(str_none) - 2);
    399  1.1      cgd 	if (auth_debug_mode)
    400  1.1      cgd 		printf(">>>%s: Sent failure message\r\n", Name);
    401  1.1      cgd 	auth_finished(0, AUTH_REJECT);
    402  1.1      cgd }
    403  1.1      cgd 
    404  1.1      cgd 	void
    405  1.1      cgd auth_send_retry()
    406  1.1      cgd {
    407  1.1      cgd 	/*
    408  1.1      cgd 	 * if auth_send_cnt <= 0 then auth_send will end up rejecting
    409  1.1      cgd 	 * the authentication and informing the other side of this.
    410  1.1      cgd 	 */
    411  1.1      cgd 	auth_send(auth_send_data, auth_send_cnt);
    412  1.1      cgd }
    413  1.1      cgd 
    414  1.1      cgd 	void
    415  1.1      cgd auth_is(data, cnt)
    416  1.1      cgd 	unsigned char *data;
    417  1.1      cgd 	int cnt;
    418  1.1      cgd {
    419  1.1      cgd 	Authenticator *ap;
    420  1.1      cgd 
    421  1.1      cgd 	if (cnt < 2)
    422  1.1      cgd 		return;
    423  1.1      cgd 
    424  1.1      cgd 	if (data[0] == AUTHTYPE_NULL) {
    425  1.1      cgd 		auth_finished(0, AUTH_REJECT);
    426  1.1      cgd 		return;
    427  1.1      cgd 	}
    428  1.1      cgd 
    429  1.1      cgd 	if (ap = findauthenticator(data[0], data[1])) {
    430  1.1      cgd 		if (ap->is)
    431  1.1      cgd 			(*ap->is)(ap, data+2, cnt-2);
    432  1.1      cgd 	} else if (auth_debug_mode)
    433  1.1      cgd 		printf(">>>%s: Invalid authentication in IS: %d\r\n",
    434  1.1      cgd 			Name, *data);
    435  1.1      cgd }
    436  1.1      cgd 
    437  1.1      cgd 	void
    438  1.1      cgd auth_reply(data, cnt)
    439  1.1      cgd 	unsigned char *data;
    440  1.1      cgd 	int cnt;
    441  1.1      cgd {
    442  1.1      cgd 	Authenticator *ap;
    443  1.1      cgd 
    444  1.1      cgd 	if (cnt < 2)
    445  1.1      cgd 		return;
    446  1.1      cgd 
    447  1.1      cgd 	if (ap = findauthenticator(data[0], data[1])) {
    448  1.1      cgd 		if (ap->reply)
    449  1.1      cgd 			(*ap->reply)(ap, data+2, cnt-2);
    450  1.1      cgd 	} else if (auth_debug_mode)
    451  1.1      cgd 		printf(">>>%s: Invalid authentication in SEND: %d\r\n",
    452  1.1      cgd 			Name, *data);
    453  1.1      cgd }
    454  1.1      cgd 
    455  1.1      cgd 	void
    456  1.1      cgd auth_name(data, cnt)
    457  1.1      cgd 	unsigned char *data;
    458  1.1      cgd 	int cnt;
    459  1.1      cgd {
    460  1.1      cgd 	Authenticator *ap;
    461  1.1      cgd 	unsigned char savename[256];
    462  1.1      cgd 
    463  1.1      cgd 	if (cnt < 1) {
    464  1.1      cgd 		if (auth_debug_mode)
    465  1.1      cgd 			printf(">>>%s: Empty name in NAME\r\n", Name);
    466  1.1      cgd 		return;
    467  1.1      cgd 	}
    468  1.1      cgd 	if (cnt > sizeof(savename) - 1) {
    469  1.1      cgd 		if (auth_debug_mode)
    470  1.1      cgd 			printf(">>>%s: Name in NAME (%d) exceeds %d length\r\n",
    471  1.1      cgd 					Name, cnt, sizeof(savename)-1);
    472  1.1      cgd 		return;
    473  1.1      cgd 	}
    474  1.1      cgd 	bcopy((void *)data, (void *)savename, cnt);
    475  1.1      cgd 	savename[cnt] = '\0';	/* Null terminate */
    476  1.1      cgd 	if (auth_debug_mode)
    477  1.1      cgd 		printf(">>>%s: Got NAME [%s]\r\n", Name, savename);
    478  1.1      cgd 	auth_encrypt_user(savename);
    479  1.1      cgd }
    480  1.1      cgd 
    481  1.1      cgd 	int
    482  1.1      cgd auth_sendname(cp, len)
    483  1.1      cgd 	unsigned char *cp;
    484  1.1      cgd 	int len;
    485  1.1      cgd {
    486  1.1      cgd 	static unsigned char str_request[256+6]
    487  1.1      cgd 			= { IAC, SB, TELOPT_AUTHENTICATION, TELQUAL_NAME, };
    488  1.1      cgd 	register unsigned char *e = str_request + 4;
    489  1.1      cgd 	register unsigned char *ee = &str_request[sizeof(str_request)-2];
    490  1.1      cgd 
    491  1.1      cgd 	while (--len >= 0) {
    492  1.1      cgd 		if ((*e++ = *cp++) == IAC)
    493  1.1      cgd 			*e++ = IAC;
    494  1.1      cgd 		if (e >= ee)
    495  1.1      cgd 			return(0);
    496  1.1      cgd 	}
    497  1.1      cgd 	*e++ = IAC;
    498  1.1      cgd 	*e++ = SE;
    499  1.1      cgd 	net_write(str_request, e - str_request);
    500  1.1      cgd 	printsub('>', &str_request[2], e - &str_request[2]);
    501  1.1      cgd 	return(1);
    502  1.1      cgd }
    503  1.1      cgd 
    504  1.1      cgd 	void
    505  1.1      cgd auth_finished(ap, result)
    506  1.1      cgd 	Authenticator *ap;
    507  1.1      cgd 	int result;
    508  1.1      cgd {
    509  1.1      cgd 	if (!(authenticated = ap))
    510  1.1      cgd 		authenticated = &NoAuth;
    511  1.1      cgd 	validuser = result;
    512  1.1      cgd }
    513  1.1      cgd 
    514  1.1      cgd 	/* ARGSUSED */
    515  1.1      cgd 	static void
    516  1.1      cgd auth_intr(sig)
    517  1.1      cgd 	int sig;
    518  1.1      cgd {
    519  1.1      cgd 	auth_finished(0, AUTH_REJECT);
    520  1.1      cgd }
    521  1.1      cgd 
    522  1.1      cgd 	int
    523  1.1      cgd auth_wait(name)
    524  1.1      cgd 	char *name;
    525  1.1      cgd {
    526  1.1      cgd 	if (auth_debug_mode)
    527  1.1      cgd 		printf(">>>%s: in auth_wait.\r\n", Name);
    528  1.1      cgd 
    529  1.1      cgd 	if (Server && !authenticating)
    530  1.1      cgd 		return(0);
    531  1.1      cgd 
    532  1.1      cgd 	(void) signal(SIGALRM, auth_intr);
    533  1.1      cgd 	alarm(30);
    534  1.1      cgd 	while (!authenticated)
    535  1.1      cgd 		if (telnet_spin())
    536  1.1      cgd 			break;
    537  1.1      cgd 	alarm(0);
    538  1.1      cgd 	(void) signal(SIGALRM, SIG_DFL);
    539  1.1      cgd 
    540  1.1      cgd 	/*
    541  1.1      cgd 	 * Now check to see if the user is valid or not
    542  1.1      cgd 	 */
    543  1.1      cgd 	if (!authenticated || authenticated == &NoAuth)
    544  1.1      cgd 		return(AUTH_REJECT);
    545  1.1      cgd 
    546  1.1      cgd 	if (validuser == AUTH_VALID)
    547  1.1      cgd 		validuser = AUTH_USER;
    548  1.1      cgd 
    549  1.1      cgd 	if (authenticated->status)
    550  1.1      cgd 		validuser = (*authenticated->status)(authenticated,
    551  1.1      cgd 						     name, validuser);
    552  1.1      cgd 	return(validuser);
    553  1.1      cgd }
    554  1.1      cgd 
    555  1.1      cgd 	void
    556  1.1      cgd auth_debug(mode)
    557  1.1      cgd 	int mode;
    558  1.1      cgd {
    559  1.1      cgd 	auth_debug_mode = mode;
    560  1.1      cgd }
    561  1.1      cgd 
    562  1.1      cgd 	void
    563  1.1      cgd auth_printsub(data, cnt, buf, buflen)
    564  1.1      cgd 	unsigned char *data, *buf;
    565  1.1      cgd 	int cnt, buflen;
    566  1.1      cgd {
    567  1.1      cgd 	Authenticator *ap;
    568  1.1      cgd 
    569  1.1      cgd 	if ((ap = findauthenticator(data[1], data[2])) && ap->printsub)
    570  1.1      cgd 		(*ap->printsub)(data, cnt, buf, buflen);
    571  1.1      cgd 	else
    572  1.1      cgd 		auth_gen_printsub(data, cnt, buf, buflen);
    573  1.1      cgd }
    574  1.1      cgd 
    575  1.1      cgd 	void
    576  1.1      cgd auth_gen_printsub(data, cnt, buf, buflen)
    577  1.1      cgd 	unsigned char *data, *buf;
    578  1.1      cgd 	int cnt, buflen;
    579  1.1      cgd {
    580  1.1      cgd 	register unsigned char *cp;
    581  1.1      cgd 	unsigned char tbuf[16];
    582  1.1      cgd 
    583  1.1      cgd 	cnt -= 3;
    584  1.1      cgd 	data += 3;
    585  1.1      cgd 	buf[buflen-1] = '\0';
    586  1.1      cgd 	buf[buflen-2] = '*';
    587  1.1      cgd 	buflen -= 2;
    588  1.1      cgd 	for (; cnt > 0; cnt--, data++) {
    589  1.1      cgd 		sprintf((char *)tbuf, " %d", *data);
    590  1.1      cgd 		for (cp = tbuf; *cp && buflen > 0; --buflen)
    591  1.1      cgd 			*buf++ = *cp++;
    592  1.1      cgd 		if (buflen <= 0)
    593  1.1      cgd 			return;
    594  1.1      cgd 	}
    595  1.1      cgd 	*buf = '\0';
    596  1.1      cgd }
    597  1.1      cgd #endif
    598