xref: /netbsd-src/lib/libtelnet/auth.c (revision 5e3df9b71ec2c331a9198a5567a78ee94d3f70de)
1*5e3df9b7Sandvar /*	$NetBSD: auth.c,v 1.25 2021/09/19 20:52:47 andvar Exp $	*/
20667d122Schristos 
361f28255Scgd /*-
450c0885eScgd  * Copyright (c) 1991, 1993
550c0885eScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
320a48c27cSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
340a48c27cSlukem #if 0
350a48c27cSlukem static char sccsid[] = "@(#)auth.c	8.3 (Berkeley) 5/30/95"
360a48c27cSlukem #else
37*5e3df9b7Sandvar __RCSID("$NetBSD: auth.c,v 1.25 2021/09/19 20:52:47 andvar Exp $");
380a48c27cSlukem #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd /*
4261f28255Scgd  * Copyright (C) 1990 by the Massachusetts Institute of Technology
4361f28255Scgd  *
4461f28255Scgd  * Export of this software from the United States of America is assumed
4561f28255Scgd  * to require a specific license from the United States Government.
4661f28255Scgd  * It is the responsibility of any person or organization contemplating
4761f28255Scgd  * export to obtain such a license before exporting.
4861f28255Scgd  *
4961f28255Scgd  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
5061f28255Scgd  * distribute this software and its documentation for any purpose and
5161f28255Scgd  * without fee is hereby granted, provided that the above copyright
5261f28255Scgd  * notice appear in all copies and that both that copyright notice and
5361f28255Scgd  * this permission notice appear in supporting documentation, and that
5461f28255Scgd  * the name of M.I.T. not be used in advertising or publicity pertaining
5561f28255Scgd  * to distribution of the software without specific, written prior
5661f28255Scgd  * permission.  M.I.T. makes no representations about the suitability of
5761f28255Scgd  * this software for any purpose.  It is provided "as is" without express
5861f28255Scgd  * or implied warranty.
5961f28255Scgd  */
6061f28255Scgd 
6161f28255Scgd 
627b8c272dSitojun #ifdef AUTHENTICATION
6361f28255Scgd #include <stdio.h>
6461f28255Scgd #include <sys/types.h>
6561f28255Scgd #include <signal.h>
6661f28255Scgd #define	AUTH_NAMES
6761f28255Scgd #include <arpa/telnet.h>
6861f28255Scgd #include <stdlib.h>
695c099b14Sthorpej #include <unistd.h>
7061f28255Scgd #ifdef	NO_STRING_H
7161f28255Scgd #include <strings.h>
7261f28255Scgd #else
7361f28255Scgd #include <string.h>
7461f28255Scgd #endif
7561f28255Scgd 
7661f28255Scgd #include "encrypt.h"
7761f28255Scgd #include "auth.h"
7861f28255Scgd #include "misc-proto.h"
7961f28255Scgd #include "auth-proto.h"
8061f28255Scgd 
8161f28255Scgd #define	typemask(x)		(1<<((x)-1))
8261f28255Scgd 
8361f28255Scgd int auth_debug_mode = 0;
846de775c2Smycroft static 	const char	*Name = "Noname";
8561f28255Scgd static	int	Server = 0;
8661f28255Scgd static	Authenticator	*authenticated = 0;
8761f28255Scgd static	int	authenticating = 0;
8861f28255Scgd static	int	validuser = 0;
8961f28255Scgd static	unsigned char	_auth_send_data[256];
9061f28255Scgd static	unsigned char	*auth_send_data;
9161f28255Scgd static	int	auth_send_cnt = 0;
9261f28255Scgd 
934fcf8685Sperry static void auth_intr(int);
945c099b14Sthorpej 
9561f28255Scgd /*
96*5e3df9b7Sandvar  * Authentication types supported.  Please note that these are stored
9761f28255Scgd  * in priority order, i.e. try the first one first.
9861f28255Scgd  */
9961f28255Scgd Authenticator authenticators[] = {
10061f28255Scgd #ifdef	KRB5
1015c099b14Sthorpej # ifdef	ENCRYPTION
1025c099b14Sthorpej 	{ AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL,
1035c099b14Sthorpej 				kerberos5_init,
1045c099b14Sthorpej 				kerberos5_send,
1055c099b14Sthorpej 				kerberos5_is,
1065c099b14Sthorpej 				kerberos5_reply,
1075c099b14Sthorpej 				kerberos5_status,
1085c099b14Sthorpej 				kerberos5_printsub },
1095c099b14Sthorpej # endif	/* ENCRYPTION */
11061f28255Scgd 	{ AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
11161f28255Scgd 				kerberos5_init,
11261f28255Scgd 				kerberos5_send,
11361f28255Scgd 				kerberos5_is,
11461f28255Scgd 				kerberos5_reply,
11561f28255Scgd 				kerberos5_status,
11661f28255Scgd 				kerberos5_printsub },
11761f28255Scgd #endif
118185393efSchristos #ifdef SRA
119185393efSchristos 	{ AUTHTYPE_SRA, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
120185393efSchristos 				sra_init,
121185393efSchristos 				sra_send,
122185393efSchristos 				sra_is,
123185393efSchristos 				sra_reply,
124185393efSchristos 				sra_status,
125185393efSchristos 				sra_printsub },
126185393efSchristos 
127185393efSchristos #endif
128185393efSchristos 	{ 0, 0, 0, 0, 0, 0, 0, 0 },
12961f28255Scgd };
13061f28255Scgd 
1316a6c8f61Schristos static Authenticator NoAuth = { .type = 0 };
13261f28255Scgd 
13361f28255Scgd static int	i_support = 0;
13461f28255Scgd static int	i_wont_support = 0;
13561f28255Scgd 
13661f28255Scgd Authenticator *
findauthenticator(int type,int way)137f9113d00Smatt findauthenticator(int type, int way)
13861f28255Scgd {
13961f28255Scgd 	Authenticator *ap = authenticators;
14061f28255Scgd 
14161f28255Scgd 	while (ap->type && (ap->type != type || ap->way != way))
14261f28255Scgd 		++ap;
14361f28255Scgd 	return(ap->type ? ap : 0);
14461f28255Scgd }
14561f28255Scgd 
14661f28255Scgd void
auth_init(const char * name,int server)147f9113d00Smatt auth_init(const char *name, int server)
14861f28255Scgd {
14961f28255Scgd 	Authenticator *ap = authenticators;
15061f28255Scgd 
15161f28255Scgd 	Server = server;
15261f28255Scgd 	Name = name;
15361f28255Scgd 
15461f28255Scgd 	i_support = 0;
15561f28255Scgd 	authenticated = 0;
15661f28255Scgd 	authenticating = 0;
15761f28255Scgd 	while (ap->type) {
15861f28255Scgd 		if (!ap->init || (*ap->init)(ap, server)) {
15961f28255Scgd 			i_support |= typemask(ap->type);
16061f28255Scgd 			if (auth_debug_mode)
16161f28255Scgd 				printf(">>>%s: I support auth type %d %d\r\n",
16261f28255Scgd 					Name,
16361f28255Scgd 					ap->type, ap->way);
16461f28255Scgd 		}
1650dcff754Sjtk 		else if (auth_debug_mode)
1660dcff754Sjtk 			printf(">>>%s: Init failed: auth type %d %d\r\n",
1670dcff754Sjtk 				Name, ap->type, ap->way);
16861f28255Scgd 		++ap;
16961f28255Scgd 	}
17061f28255Scgd }
17161f28255Scgd 
17261f28255Scgd void
auth_disable_name(char * name)173f9113d00Smatt auth_disable_name(char *name)
17461f28255Scgd {
17561f28255Scgd 	int x;
17661f28255Scgd 	for (x = 0; x < AUTHTYPE_CNT; ++x) {
177185393efSchristos 		if (AUTHTYPE_NAME(x) && !strcasecmp(name, AUTHTYPE_NAME(x))) {
17861f28255Scgd 			i_wont_support |= typemask(x);
17961f28255Scgd 			break;
18061f28255Scgd 		}
18161f28255Scgd 	}
18261f28255Scgd }
18361f28255Scgd 
18461f28255Scgd int
getauthmask(const char * type,int * maskp)18557aff589Schristos getauthmask(const char *type, int *maskp)
18661f28255Scgd {
18761f28255Scgd 	register int x;
18861f28255Scgd 
189185393efSchristos 	if (AUTHTYPE_NAME(0) && !strcasecmp(type, AUTHTYPE_NAME(0))) {
19061f28255Scgd 		*maskp = -1;
19161f28255Scgd 		return(1);
19261f28255Scgd 	}
19361f28255Scgd 
19461f28255Scgd 	for (x = 1; x < AUTHTYPE_CNT; ++x) {
195185393efSchristos 		if (AUTHTYPE_NAME(x) && !strcasecmp(type, AUTHTYPE_NAME(x))) {
19661f28255Scgd 			*maskp = typemask(x);
19761f28255Scgd 			return(1);
19861f28255Scgd 		}
19961f28255Scgd 	}
20061f28255Scgd 	return(0);
20161f28255Scgd }
20261f28255Scgd 
20361f28255Scgd int
auth_enable(const char * type)20457aff589Schristos auth_enable(const char *type)
20561f28255Scgd {
20661f28255Scgd 	return(auth_onoff(type, 1));
20761f28255Scgd }
20861f28255Scgd 
20961f28255Scgd int
auth_disable(const char * type)21057aff589Schristos auth_disable(const char *type)
21161f28255Scgd {
21261f28255Scgd 	return(auth_onoff(type, 0));
21361f28255Scgd }
21461f28255Scgd 
21561f28255Scgd int
auth_onoff(const char * type,int on)21657aff589Schristos auth_onoff(const char *type, int on)
21761f28255Scgd {
2180dcff754Sjtk 	int i, mask = -1;
21961f28255Scgd 	Authenticator *ap;
22061f28255Scgd 
22161f28255Scgd 	if (!strcasecmp(type, "?") || !strcasecmp(type, "help")) {
22261f28255Scgd 		printf("auth %s 'type'\n", on ? "enable" : "disable");
22361f28255Scgd 		printf("Where 'type' is one of:\n");
22461f28255Scgd 		printf("\t%s\n", AUTHTYPE_NAME(0));
2250dcff754Sjtk 		mask = 0;
2260dcff754Sjtk 		for (ap = authenticators; ap->type; ap++) {
2270dcff754Sjtk 			if ((mask & (i = typemask(ap->type))) != 0)
2280dcff754Sjtk 				continue;
2290dcff754Sjtk 			mask |= i;
23061f28255Scgd 			printf("\t%s\n", AUTHTYPE_NAME(ap->type));
2310dcff754Sjtk 		}
23261f28255Scgd 		return(0);
23361f28255Scgd 	}
23461f28255Scgd 
23561f28255Scgd 	if (!getauthmask(type, &mask)) {
23661f28255Scgd 		printf("%s: invalid authentication type\n", type);
23761f28255Scgd 		return(0);
23861f28255Scgd 	}
23961f28255Scgd 	if (on)
24061f28255Scgd 		i_wont_support &= ~mask;
24161f28255Scgd 	else
24261f28255Scgd 		i_wont_support |= mask;
24361f28255Scgd 	return(1);
24461f28255Scgd }
24561f28255Scgd 
24661f28255Scgd int
auth_togdebug(int on)247f9113d00Smatt auth_togdebug(int on)
24861f28255Scgd {
24961f28255Scgd 	if (on < 0)
25061f28255Scgd 		auth_debug_mode ^= 1;
25161f28255Scgd 	else
25261f28255Scgd 		auth_debug_mode = on;
25361f28255Scgd 	printf("auth debugging %s\n", auth_debug_mode ? "enabled" : "disabled");
25461f28255Scgd 	return(1);
25561f28255Scgd }
25661f28255Scgd 
25761f28255Scgd int
auth_status(const char * s)25857aff589Schristos auth_status(const char *s)
25961f28255Scgd {
26061f28255Scgd 	Authenticator *ap;
2610dcff754Sjtk 	int i, mask;
26261f28255Scgd 
26361f28255Scgd 	if (i_wont_support == -1)
26461f28255Scgd 		printf("Authentication disabled\n");
26561f28255Scgd 	else
26661f28255Scgd 		printf("Authentication enabled\n");
26761f28255Scgd 
2680dcff754Sjtk 	mask = 0;
2690dcff754Sjtk 	for (ap = authenticators; ap->type; ap++) {
2700dcff754Sjtk 		if ((mask & (i = typemask(ap->type))) != 0)
2710dcff754Sjtk 			continue;
2720dcff754Sjtk 		mask |= i;
27361f28255Scgd 		printf("%s: %s\n", AUTHTYPE_NAME(ap->type),
27461f28255Scgd 			(i_wont_support & typemask(ap->type)) ?
27561f28255Scgd 					"disabled" : "enabled");
2760dcff754Sjtk 	}
27761f28255Scgd 	return(1);
27861f28255Scgd }
27961f28255Scgd 
28061f28255Scgd /*
28161f28255Scgd  * This routine is called by the server to start authentication
28261f28255Scgd  * negotiation.
28361f28255Scgd  */
28461f28255Scgd void
auth_request(void)285f9113d00Smatt auth_request(void)
28661f28255Scgd {
28761f28255Scgd 	static unsigned char str_request[64] = { IAC, SB,
28861f28255Scgd 						 TELOPT_AUTHENTICATION,
28961f28255Scgd 						 TELQUAL_SEND, };
29061f28255Scgd 	Authenticator *ap = authenticators;
29161f28255Scgd 	unsigned char *e = str_request + 4;
29261f28255Scgd 
29361f28255Scgd 	if (!authenticating) {
29461f28255Scgd 		authenticating = 1;
29561f28255Scgd 		while (ap->type) {
29661f28255Scgd 			if (i_support & ~i_wont_support & typemask(ap->type)) {
29761f28255Scgd 				if (auth_debug_mode) {
29861f28255Scgd 					printf(">>>%s: Sending type %d %d\r\n",
29961f28255Scgd 						Name, ap->type, ap->way);
30061f28255Scgd 				}
30161f28255Scgd 				*e++ = ap->type;
30261f28255Scgd 				*e++ = ap->way;
30361f28255Scgd 			}
30461f28255Scgd 			++ap;
30561f28255Scgd 		}
30661f28255Scgd 		*e++ = IAC;
30761f28255Scgd 		*e++ = SE;
30806f19109Sthorpej 		telnet_net_write(str_request, e - str_request);
30961f28255Scgd 		printsub('>', &str_request[2], e - str_request - 2);
31061f28255Scgd 	}
31161f28255Scgd }
31261f28255Scgd 
31361f28255Scgd /*
31461f28255Scgd  * This is called when an AUTH SEND is received.
31561f28255Scgd  * It should never arrive on the server side (as only the server can
31661f28255Scgd  * send an AUTH SEND).
31761f28255Scgd  * You should probably respond to it if you can...
31861f28255Scgd  *
31961f28255Scgd  * If you want to respond to the types out of order (i.e. even
32061f28255Scgd  * if he sends  LOGIN KERBEROS and you support both, you respond
32161f28255Scgd  * with KERBEROS instead of LOGIN (which is against what the
32261f28255Scgd  * protocol says)) you will have to hack this code...
32361f28255Scgd  */
32461f28255Scgd void
auth_send(unsigned char * data,int cnt)325f9113d00Smatt auth_send(unsigned char *data, int cnt)
32661f28255Scgd {
32761f28255Scgd 	Authenticator *ap;
32861f28255Scgd 	static unsigned char str_none[] = { IAC, SB, TELOPT_AUTHENTICATION,
32961f28255Scgd 					    TELQUAL_IS, AUTHTYPE_NULL, 0,
33061f28255Scgd 					    IAC, SE };
33161f28255Scgd 	if (Server) {
33261f28255Scgd 		if (auth_debug_mode) {
33361f28255Scgd 			printf(">>>%s: auth_send called!\r\n", Name);
33461f28255Scgd 		}
33561f28255Scgd 		return;
33661f28255Scgd 	}
33761f28255Scgd 
33861f28255Scgd 	if (auth_debug_mode) {
33961f28255Scgd 		printf(">>>%s: auth_send got:", Name);
34061f28255Scgd 		printd(data, cnt); printf("\r\n");
34161f28255Scgd 	}
34261f28255Scgd 
34361f28255Scgd 	/*
34461f28255Scgd 	 * Save the data, if it is new, so that we can continue looking
34561f28255Scgd 	 * at it if the authorization we try doesn't work
34661f28255Scgd 	 */
34761f28255Scgd 	if (data < _auth_send_data ||
34861f28255Scgd 	    data > _auth_send_data + sizeof(_auth_send_data)) {
3496a6c8f61Schristos 		auth_send_cnt = (size_t)cnt > sizeof(_auth_send_data)
35061f28255Scgd 					? sizeof(_auth_send_data)
3516a6c8f61Schristos 					: (size_t)cnt;
3526a6c8f61Schristos 		memmove(_auth_send_data, data, auth_send_cnt);
35361f28255Scgd 		auth_send_data = _auth_send_data;
35461f28255Scgd 	} else {
35561f28255Scgd 		/*
35661f28255Scgd 		 * This is probably a no-op, but we just make sure
35761f28255Scgd 		 */
35861f28255Scgd 		auth_send_data = data;
35961f28255Scgd 		auth_send_cnt = cnt;
36061f28255Scgd 	}
36161f28255Scgd 	while ((auth_send_cnt -= 2) >= 0) {
36261f28255Scgd 		if (auth_debug_mode)
36361f28255Scgd 			printf(">>>%s: He supports %d\r\n",
36461f28255Scgd 				Name, *auth_send_data);
36561f28255Scgd 		if ((i_support & ~i_wont_support) & typemask(*auth_send_data)) {
36661f28255Scgd 			ap = findauthenticator(auth_send_data[0],
36761f28255Scgd 					       auth_send_data[1]);
36850c0885eScgd 			if (ap && ap->send) {
36961f28255Scgd 				if (auth_debug_mode)
37061f28255Scgd 					printf(">>>%s: Trying %d %d\r\n",
37161f28255Scgd 						Name, auth_send_data[0],
37261f28255Scgd 							auth_send_data[1]);
37361f28255Scgd 				if ((*ap->send)(ap)) {
37461f28255Scgd 					/*
37561f28255Scgd 					 * Okay, we found one we like
37661f28255Scgd 					 * and did it.
37761f28255Scgd 					 * we can go home now.
37861f28255Scgd 					 */
37961f28255Scgd 					if (auth_debug_mode)
38061f28255Scgd 						printf(">>>%s: Using type %d\r\n",
38161f28255Scgd 							Name, *auth_send_data);
38261f28255Scgd 					auth_send_data += 2;
38361f28255Scgd 					return;
38461f28255Scgd 				}
38561f28255Scgd 			}
38661f28255Scgd 			/* else
38761f28255Scgd 			 *	just continue on and look for the
38861f28255Scgd 			 *	next one if we didn't do anything.
38961f28255Scgd 			 */
39061f28255Scgd 		}
39161f28255Scgd 		auth_send_data += 2;
39261f28255Scgd 	}
39306f19109Sthorpej 	telnet_net_write(str_none, sizeof(str_none));
39461f28255Scgd 	printsub('>', &str_none[2], sizeof(str_none) - 2);
39561f28255Scgd 	if (auth_debug_mode)
39661f28255Scgd 		printf(">>>%s: Sent failure message\r\n", Name);
39761f28255Scgd 	auth_finished(0, AUTH_REJECT);
39850c0885eScgd #ifdef KANNAN
39950c0885eScgd 	/*
40050c0885eScgd 	 *  We requested strong authentication, however no mechanisms worked.
40150c0885eScgd 	 *  Therefore, exit on client end.
40250c0885eScgd 	 */
40350c0885eScgd 	printf("Unable to securely authenticate user ... exit\n");
40450c0885eScgd 	exit(0);
40550c0885eScgd #endif /* KANNAN */
40661f28255Scgd }
40761f28255Scgd 
40861f28255Scgd void
auth_send_retry(void)409f9113d00Smatt auth_send_retry(void)
41061f28255Scgd {
41161f28255Scgd 	/*
41261f28255Scgd 	 * if auth_send_cnt <= 0 then auth_send will end up rejecting
41361f28255Scgd 	 * the authentication and informing the other side of this.
41461f28255Scgd 	 */
41561f28255Scgd 	auth_send(auth_send_data, auth_send_cnt);
41661f28255Scgd }
41761f28255Scgd 
41861f28255Scgd void
auth_is(unsigned char * data,int cnt)419f9113d00Smatt auth_is(unsigned char *data, int cnt)
42061f28255Scgd {
42161f28255Scgd 	Authenticator *ap;
42261f28255Scgd 
42361f28255Scgd 	if (cnt < 2)
42461f28255Scgd 		return;
42561f28255Scgd 
42661f28255Scgd 	if (data[0] == AUTHTYPE_NULL) {
42761f28255Scgd 		auth_finished(0, AUTH_REJECT);
42861f28255Scgd 		return;
42961f28255Scgd 	}
43061f28255Scgd 
4315c099b14Sthorpej 	if ((ap = findauthenticator(data[0], data[1])) != NULL) {
43261f28255Scgd 		if (ap->is)
43361f28255Scgd 			(*ap->is)(ap, data+2, cnt-2);
43461f28255Scgd 	} else if (auth_debug_mode)
43561f28255Scgd 		printf(">>>%s: Invalid authentication in IS: %d\r\n",
43661f28255Scgd 			Name, *data);
43761f28255Scgd }
43861f28255Scgd 
43961f28255Scgd void
auth_reply(unsigned char * data,int cnt)440f9113d00Smatt auth_reply(unsigned char *data, int cnt)
44161f28255Scgd {
44261f28255Scgd 	Authenticator *ap;
44361f28255Scgd 
44461f28255Scgd 	if (cnt < 2)
44561f28255Scgd 		return;
44661f28255Scgd 
4475c099b14Sthorpej 	if ((ap = findauthenticator(data[0], data[1])) != NULL) {
44861f28255Scgd 		if (ap->reply)
44961f28255Scgd 			(*ap->reply)(ap, data+2, cnt-2);
45061f28255Scgd 	} else if (auth_debug_mode)
45161f28255Scgd 		printf(">>>%s: Invalid authentication in SEND: %d\r\n",
45261f28255Scgd 			Name, *data);
45361f28255Scgd }
45461f28255Scgd 
45561f28255Scgd void
auth_name(unsigned char * data,int cnt)456f9113d00Smatt auth_name(unsigned char *data, int cnt)
45761f28255Scgd {
45861f28255Scgd 	unsigned char savename[256];
45961f28255Scgd 
46061f28255Scgd 	if (cnt < 1) {
46161f28255Scgd 		if (auth_debug_mode)
46261f28255Scgd 			printf(">>>%s: Empty name in NAME\r\n", Name);
46361f28255Scgd 		return;
46461f28255Scgd 	}
4656a6c8f61Schristos 	if ((size_t)cnt > sizeof(savename) - 1) {
46661f28255Scgd 		if (auth_debug_mode)
46743e17572Sthorpej 			printf(">>>%s: Name in NAME (%d) exceeds %ld length\r\n",
46843e17572Sthorpej 					Name, cnt, (long)sizeof(savename)-1);
46961f28255Scgd 		return;
47061f28255Scgd 	}
4710dcff754Sjtk 	memmove((void *)savename, (void *)data, cnt);
47261f28255Scgd 	savename[cnt] = '\0';	/* Null terminate */
47361f28255Scgd 	if (auth_debug_mode)
47461f28255Scgd 		printf(">>>%s: Got NAME [%s]\r\n", Name, savename);
47561f28255Scgd 	auth_encrypt_user(savename);
47661f28255Scgd }
47761f28255Scgd 
47861f28255Scgd int
auth_sendname(unsigned char * cp,int len)479f9113d00Smatt auth_sendname(unsigned char *cp, int len)
48061f28255Scgd {
48161f28255Scgd 	static unsigned char str_request[256+6]
48261f28255Scgd 			= { IAC, SB, TELOPT_AUTHENTICATION, TELQUAL_NAME, };
48361f28255Scgd 	register unsigned char *e = str_request + 4;
48461f28255Scgd 	register unsigned char *ee = &str_request[sizeof(str_request)-2];
48561f28255Scgd 
48661f28255Scgd 	while (--len >= 0) {
48761f28255Scgd 		if ((*e++ = *cp++) == IAC)
48861f28255Scgd 			*e++ = IAC;
48961f28255Scgd 		if (e >= ee)
49061f28255Scgd 			return(0);
49161f28255Scgd 	}
49261f28255Scgd 	*e++ = IAC;
49361f28255Scgd 	*e++ = SE;
49406f19109Sthorpej 	telnet_net_write(str_request, e - str_request);
49561f28255Scgd 	printsub('>', &str_request[2], e - &str_request[2]);
49661f28255Scgd 	return(1);
49761f28255Scgd }
49861f28255Scgd 
49961f28255Scgd void
auth_finished(Authenticator * ap,int result)500f9113d00Smatt auth_finished(Authenticator *ap, int result)
50161f28255Scgd {
50261f28255Scgd 	if (!(authenticated = ap))
50361f28255Scgd 		authenticated = &NoAuth;
50461f28255Scgd 	validuser = result;
50561f28255Scgd }
50661f28255Scgd 
50761f28255Scgd 	/* ARGSUSED */
50861f28255Scgd static void
auth_intr(int sig)509f9113d00Smatt auth_intr(int sig)
51061f28255Scgd {
51161f28255Scgd 	auth_finished(0, AUTH_REJECT);
51261f28255Scgd }
51361f28255Scgd 
51461f28255Scgd int
auth_wait(char * name,size_t l)515f9113d00Smatt auth_wait(char *name, size_t l)
51661f28255Scgd {
51761f28255Scgd 	if (auth_debug_mode)
51861f28255Scgd 		printf(">>>%s: in auth_wait.\r\n", Name);
51961f28255Scgd 
52061f28255Scgd 	if (Server && !authenticating)
52161f28255Scgd 		return(0);
52261f28255Scgd 
52361f28255Scgd 	(void) signal(SIGALRM, auth_intr);
52461f28255Scgd 	alarm(30);
52561f28255Scgd 	while (!authenticated)
52661f28255Scgd 		if (telnet_spin())
52761f28255Scgd 			break;
52861f28255Scgd 	alarm(0);
52961f28255Scgd 	(void) signal(SIGALRM, SIG_DFL);
53061f28255Scgd 
53161f28255Scgd 	/*
53261f28255Scgd 	 * Now check to see if the user is valid or not
53361f28255Scgd 	 */
53461f28255Scgd 	if (!authenticated || authenticated == &NoAuth)
53561f28255Scgd 		return(AUTH_REJECT);
53661f28255Scgd 
53761f28255Scgd 	if (validuser == AUTH_VALID)
53861f28255Scgd 		validuser = AUTH_USER;
53961f28255Scgd 
54061f28255Scgd 	if (authenticated->status)
54161f28255Scgd 		validuser = (*authenticated->status)(authenticated,
542427af575Sitojun 						     name, l, validuser);
54361f28255Scgd 	return(validuser);
54461f28255Scgd }
54561f28255Scgd 
54661f28255Scgd void
auth_debug(int mode)547f9113d00Smatt auth_debug(int mode)
54861f28255Scgd {
54961f28255Scgd 	auth_debug_mode = mode;
55061f28255Scgd }
55161f28255Scgd 
55261f28255Scgd void
auth_printsub(unsigned char * data,int cnt,unsigned char * buf,int buflen)553f9113d00Smatt auth_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
55461f28255Scgd {
55561f28255Scgd 	Authenticator *ap;
55661f28255Scgd 
55761f28255Scgd 	if ((ap = findauthenticator(data[1], data[2])) && ap->printsub)
55861f28255Scgd 		(*ap->printsub)(data, cnt, buf, buflen);
55961f28255Scgd 	else
56061f28255Scgd 		auth_gen_printsub(data, cnt, buf, buflen);
56161f28255Scgd }
56261f28255Scgd 
56361f28255Scgd void
auth_gen_printsub(unsigned char * data,int cnt,unsigned char * buf,int buflen)564f9113d00Smatt auth_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
56561f28255Scgd {
56661f28255Scgd 	register unsigned char *cp;
56761f28255Scgd 	unsigned char tbuf[16];
56861f28255Scgd 
56961f28255Scgd 	cnt -= 3;
57061f28255Scgd 	data += 3;
57161f28255Scgd 	buf[buflen-1] = '\0';
57261f28255Scgd 	buf[buflen-2] = '*';
57361f28255Scgd 	buflen -= 2;
57461f28255Scgd 	for (; cnt > 0; cnt--, data++) {
575fabed9f7Sitojun 		snprintf((char *)tbuf, sizeof(tbuf), " %d", *data);
57661f28255Scgd 		for (cp = tbuf; *cp && buflen > 0; --buflen)
57761f28255Scgd 			*buf++ = *cp++;
57861f28255Scgd 		if (buflen <= 0)
57961f28255Scgd 			return;
58061f28255Scgd 	}
58161f28255Scgd 	*buf = '\0';
58261f28255Scgd }
58361f28255Scgd #endif
584