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