Home | History | Annotate | Line # | Download | only in telnetd
telnetd.c revision 1.32
      1  1.32    itojun /*	$NetBSD: telnetd.c,v 1.32 2002/01/31 07:54:50 itojun Exp $	*/
      2  1.17   thorpej 
      3  1.16    itojun /*
      4  1.16    itojun  * Copyright (C) 1997 and 1998 WIDE Project.
      5  1.16    itojun  * All rights reserved.
      6  1.16    itojun  *
      7  1.16    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.16    itojun  * modification, are permitted provided that the following conditions
      9  1.16    itojun  * are met:
     10  1.16    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.16    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.16    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.16    itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.16    itojun  *    documentation and/or other materials provided with the distribution.
     15  1.16    itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.16    itojun  *    may be used to endorse or promote products derived from this software
     17  1.16    itojun  *    without specific prior written permission.
     18  1.16    itojun  *
     19  1.16    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.16    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.16    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.16    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.16    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.16    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.16    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.16    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.16    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.16    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.16    itojun  * SUCH DAMAGE.
     30  1.16    itojun  */
     31  1.16    itojun 
     32   1.1       cgd /*
     33   1.3       cgd  * Copyright (c) 1989, 1993
     34   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
     35   1.1       cgd  *
     36   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37   1.1       cgd  * modification, are permitted provided that the following conditions
     38   1.1       cgd  * are met:
     39   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     45   1.1       cgd  *    must display the following acknowledgement:
     46   1.1       cgd  *	This product includes software developed by the University of
     47   1.1       cgd  *	California, Berkeley and its contributors.
     48   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     49   1.1       cgd  *    may be used to endorse or promote products derived from this software
     50   1.1       cgd  *    without specific prior written permission.
     51   1.1       cgd  *
     52   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1       cgd  * SUCH DAMAGE.
     63   1.1       cgd  */
     64   1.1       cgd 
     65   1.9       mrg #include <sys/cdefs.h>
     66   1.1       cgd #ifndef lint
     67   1.9       mrg __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     68   1.9       mrg 	The Regents of the University of California.  All rights reserved.\n");
     69   1.5   thorpej #if 0
     70   1.5   thorpej static char sccsid[] = "@(#)telnetd.c	8.4 (Berkeley) 5/30/95";
     71   1.5   thorpej #else
     72  1.32    itojun __RCSID("$NetBSD: telnetd.c,v 1.32 2002/01/31 07:54:50 itojun Exp $");
     73   1.5   thorpej #endif
     74   1.1       cgd #endif /* not lint */
     75   1.1       cgd 
     76   1.1       cgd #include "telnetd.h"
     77   1.1       cgd #include "pathnames.h"
     78   1.1       cgd 
     79   1.9       mrg #include <arpa/inet.h>
     80   1.9       mrg 
     81  1.16    itojun #include <err.h>
     82  1.10     lukem #include <termcap.h>
     83   1.9       mrg 
     84  1.20        ad #include <limits.h>
     85  1.20        ad 
     86   1.4       jtk #define P __P
     87   1.4       jtk 
     88   1.3       cgd #if	defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY)
     89   1.3       cgd /*
     90   1.3       cgd  * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can
     91   1.3       cgd  * use it to tell us to turn off all the socket security code,
     92   1.3       cgd  * since that is only used in UNICOS 7.0 and later.
     93   1.3       cgd  */
     94   1.3       cgd # undef _SC_CRAY_SECURE_SYS
     95   1.3       cgd #endif
     96   1.3       cgd 
     97   1.3       cgd #if	defined(_SC_CRAY_SECURE_SYS)
     98   1.3       cgd #include <sys/sysv.h>
     99   1.3       cgd #include <sys/secdev.h>
    100   1.3       cgd # ifdef SO_SEC_MULTI		/* 8.0 code */
    101   1.3       cgd #include <sys/secparm.h>
    102   1.3       cgd #include <sys/usrv.h>
    103   1.3       cgd # endif /* SO_SEC_MULTI */
    104   1.3       cgd int	secflag;
    105   1.3       cgd char	tty_dev[16];
    106   1.3       cgd struct	secdev dv;
    107   1.3       cgd struct	sysv sysv;
    108   1.3       cgd # ifdef SO_SEC_MULTI		/* 8.0 code */
    109   1.3       cgd struct	socksec ss;
    110   1.3       cgd # else /* SO_SEC_MULTI */	/* 7.0 code */
    111   1.3       cgd struct	socket_security ss;
    112   1.3       cgd # endif /* SO_SEC_MULTI */
    113   1.3       cgd #endif	/* _SC_CRAY_SECURE_SYS */
    114   1.3       cgd 
    115  1.21   thorpej #if	defined(KRB5)
    116  1.21   thorpej #define	Authenticator	k5_Authenticator
    117  1.21   thorpej #include <krb5.h>
    118  1.21   thorpej #undef	Authenticator
    119  1.21   thorpej #include <com_err.h>
    120  1.21   thorpej #endif
    121  1.21   thorpej 
    122   1.3       cgd #if	defined(AUTHENTICATION)
    123   1.1       cgd int	auth_level = 0;
    124   1.1       cgd #endif
    125  1.21   thorpej 
    126  1.21   thorpej #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
    127  1.21   thorpej #include <libtelnet/misc.h>
    128  1.21   thorpej #endif
    129  1.21   thorpej 
    130  1.15      dean #if	defined(SECURELOGIN)
    131  1.15      dean int	require_secure_login = 0;
    132   1.1       cgd #endif
    133   1.1       cgd 
    134   1.3       cgd extern	int utmp_len;
    135  1.25  christos extern int require_hwpreauth;
    136  1.25  christos #ifdef KRB5
    137  1.25  christos extern krb5_context telnet_context;
    138  1.25  christos #endif
    139   1.3       cgd int	registerd_host_only = 0;
    140   1.3       cgd 
    141   1.3       cgd #ifdef	STREAMSPTY
    142   1.3       cgd # include <stropts.h>
    143   1.3       cgd # include <termio.h>
    144   1.3       cgd /* make sure we don't get the bsd version */
    145   1.3       cgd # include "/usr/include/sys/tty.h"
    146   1.3       cgd # include <sys/ptyvar.h>
    147   1.3       cgd 
    148   1.3       cgd /*
    149   1.3       cgd  * Because of the way ptyibuf is used with streams messages, we need
    150  1.28       wiz  * ptyibuf+1 to be on a full-word boundary.  The following weirdness
    151   1.3       cgd  * is simply to make that happen.
    152   1.3       cgd  */
    153   1.3       cgd long	ptyibufbuf[BUFSIZ/sizeof(long)+1];
    154   1.3       cgd char	*ptyibuf = ((char *)&ptyibufbuf[1])-1;
    155   1.3       cgd char	*ptyip = ((char *)&ptyibufbuf[1])-1;
    156  1.20        ad char	ptyibuf2[BUFSIZ*4];
    157   1.3       cgd unsigned char ctlbuf[BUFSIZ];
    158   1.3       cgd struct	strbuf strbufc, strbufd;
    159   1.3       cgd 
    160   1.3       cgd int readstream();
    161   1.3       cgd 
    162   1.3       cgd #else	/* ! STREAMPTY */
    163   1.3       cgd 
    164   1.1       cgd /*
    165   1.1       cgd  * I/O data buffers,
    166   1.1       cgd  * pointers, and counters.
    167   1.1       cgd  */
    168   1.1       cgd char	ptyibuf[BUFSIZ], *ptyip = ptyibuf;
    169   1.1       cgd char	ptyibuf2[BUFSIZ];
    170   1.1       cgd 
    171   1.3       cgd #endif /* ! STREAMPTY */
    172   1.3       cgd 
    173   1.1       cgd int	hostinfo = 1;			/* do we print login banner? */
    174   1.1       cgd 
    175   1.1       cgd #ifdef	CRAY
    176   1.1       cgd extern int      newmap; /* nonzero if \n maps to ^M^J */
    177   1.1       cgd int	lowpty = 0, highpty;	/* low, high pty numbers */
    178   1.1       cgd #endif /* CRAY */
    179   1.1       cgd 
    180   1.1       cgd int debug = 0;
    181   1.1       cgd int keepalive = 1;
    182   1.6       tls char *gettyname = "default";
    183   1.1       cgd char *progname;
    184   1.1       cgd 
    185   1.9       mrg int main __P((int, char *[]));
    186   1.9       mrg void usage __P((void));
    187   1.9       mrg int getterminaltype __P((char *));
    188   1.9       mrg int getent __P((char *, char *));
    189  1.16    itojun void doit __P((struct sockaddr *));
    190   1.9       mrg void _gettermname __P((void));
    191   1.9       mrg int terminaltypeok __P((char *));
    192  1.26    itojun char *getstr __P((const char *, char **));
    193   1.1       cgd 
    194   1.3       cgd /*
    195   1.3       cgd  * The string to pass to getopt().  We do it this way so
    196   1.3       cgd  * that only the actual options that we support will be
    197   1.3       cgd  * passed off to getopt().
    198   1.3       cgd  */
    199   1.3       cgd char valid_opts[] = {
    200   1.6       tls 	'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
    201  1.16    itojun 	'4', '6',
    202   1.3       cgd #ifdef	AUTHENTICATION
    203   1.3       cgd 	'a', ':', 'X', ':',
    204   1.3       cgd #endif
    205   1.3       cgd #ifdef BFTPDAEMON
    206   1.3       cgd 	'B',
    207   1.3       cgd #endif
    208  1.21   thorpej #ifdef	ENCRYPTION
    209  1.21   thorpej 	'e', ':',
    210  1.21   thorpej #endif
    211   1.3       cgd #ifdef DIAGNOSTICS
    212   1.3       cgd 	'D', ':',
    213   1.3       cgd #endif
    214   1.3       cgd #ifdef	LINEMODE
    215   1.3       cgd 	'l',
    216   1.3       cgd #endif
    217   1.3       cgd #ifdef CRAY
    218   1.3       cgd 	'r', ':',
    219   1.3       cgd #endif
    220  1.15      dean #ifdef	SECURELOGIN
    221   1.3       cgd 	's',
    222   1.3       cgd #endif
    223  1.21   thorpej #ifdef	KRB5
    224  1.21   thorpej 	'R', ':', 'H',
    225  1.21   thorpej #endif
    226   1.3       cgd 	'\0'
    227   1.3       cgd };
    228   1.3       cgd 
    229  1.16    itojun int family = AF_INET;
    230  1.16    itojun 
    231   1.9       mrg int
    232   1.1       cgd main(argc, argv)
    233  1.14   msaitoh 	int argc;
    234   1.1       cgd 	char *argv[];
    235   1.1       cgd {
    236  1.16    itojun 	struct sockaddr_storage from;
    237   1.1       cgd 	int on = 1, fromlen;
    238   1.1       cgd 	register int ch;
    239   1.1       cgd #if	defined(IPPROTO_IP) && defined(IP_TOS)
    240   1.1       cgd 	int tos = -1;
    241   1.1       cgd #endif
    242   1.1       cgd 
    243   1.1       cgd 	pfrontp = pbackp = ptyobuf;
    244   1.1       cgd 	netip = netibuf;
    245   1.1       cgd 	nfrontp = nbackp = netobuf;
    246  1.21   thorpej #ifdef	ENCRYPTION
    247  1.21   thorpej 	nclearto = 0;
    248  1.21   thorpej #endif	/* ENCRYPTION */
    249   1.1       cgd 
    250   1.1       cgd 	progname = *argv;
    251   1.1       cgd 
    252   1.1       cgd #ifdef CRAY
    253   1.1       cgd 	/*
    254   1.1       cgd 	 * Get number of pty's before trying to process options,
    255   1.1       cgd 	 * which may include changing pty range.
    256   1.1       cgd 	 */
    257   1.1       cgd 	highpty = getnpty();
    258   1.1       cgd #endif /* CRAY */
    259   1.1       cgd 
    260   1.7     enami 	while ((ch = getopt(argc, argv, valid_opts)) != -1) {
    261   1.8     enami 		switch (ch) {
    262   1.1       cgd 
    263   1.3       cgd #ifdef	AUTHENTICATION
    264   1.1       cgd 		case 'a':
    265   1.1       cgd 			/*
    266   1.1       cgd 			 * Check for required authentication level
    267   1.1       cgd 			 */
    268   1.1       cgd 			if (strcmp(optarg, "debug") == 0) {
    269   1.1       cgd 				auth_debug_mode = 1;
    270   1.1       cgd 			} else if (strcasecmp(optarg, "none") == 0) {
    271   1.1       cgd 				auth_level = 0;
    272   1.1       cgd 			} else if (strcasecmp(optarg, "other") == 0) {
    273   1.1       cgd 				auth_level = AUTH_OTHER;
    274   1.1       cgd 			} else if (strcasecmp(optarg, "user") == 0) {
    275   1.1       cgd 				auth_level = AUTH_USER;
    276   1.1       cgd 			} else if (strcasecmp(optarg, "valid") == 0) {
    277   1.1       cgd 				auth_level = AUTH_VALID;
    278   1.1       cgd 			} else if (strcasecmp(optarg, "off") == 0) {
    279   1.1       cgd 				/*
    280   1.1       cgd 				 * This hack turns off authentication
    281   1.1       cgd 				 */
    282   1.1       cgd 				auth_level = -1;
    283   1.1       cgd 			} else {
    284   1.1       cgd 				fprintf(stderr,
    285   1.1       cgd 			    "telnetd: unknown authorization level for -a\n");
    286   1.1       cgd 			}
    287   1.1       cgd 			break;
    288   1.3       cgd #endif	/* AUTHENTICATION */
    289   1.1       cgd 
    290   1.1       cgd #ifdef BFTPDAEMON
    291   1.1       cgd 		case 'B':
    292   1.1       cgd 			bftpd++;
    293   1.1       cgd 			break;
    294   1.1       cgd #endif /* BFTPDAEMON */
    295   1.1       cgd 
    296   1.1       cgd 		case 'd':
    297   1.1       cgd 			if (strcmp(optarg, "ebug") == 0) {
    298   1.1       cgd 				debug++;
    299   1.1       cgd 				break;
    300   1.1       cgd 			}
    301   1.1       cgd 			usage();
    302   1.1       cgd 			/* NOTREACHED */
    303   1.1       cgd 			break;
    304   1.1       cgd 
    305   1.1       cgd #ifdef DIAGNOSTICS
    306   1.1       cgd 		case 'D':
    307   1.1       cgd 			/*
    308   1.1       cgd 			 * Check for desired diagnostics capabilities.
    309   1.1       cgd 			 */
    310   1.1       cgd 			if (!strcmp(optarg, "report")) {
    311   1.1       cgd 				diagnostic |= TD_REPORT|TD_OPTIONS;
    312   1.1       cgd 			} else if (!strcmp(optarg, "exercise")) {
    313   1.1       cgd 				diagnostic |= TD_EXERCISE;
    314   1.1       cgd 			} else if (!strcmp(optarg, "netdata")) {
    315   1.1       cgd 				diagnostic |= TD_NETDATA;
    316   1.1       cgd 			} else if (!strcmp(optarg, "ptydata")) {
    317   1.1       cgd 				diagnostic |= TD_PTYDATA;
    318   1.1       cgd 			} else if (!strcmp(optarg, "options")) {
    319   1.1       cgd 				diagnostic |= TD_OPTIONS;
    320   1.1       cgd 			} else {
    321   1.1       cgd 				usage();
    322   1.1       cgd 				/* NOT REACHED */
    323   1.1       cgd 			}
    324   1.1       cgd 			break;
    325   1.1       cgd #endif /* DIAGNOSTICS */
    326   1.1       cgd 
    327  1.21   thorpej #ifdef	ENCRYPTION
    328  1.21   thorpej 		case 'e':
    329  1.21   thorpej 			if (strcmp(optarg, "debug") == 0) {
    330  1.21   thorpej 				encrypt_debug_mode = 1;
    331  1.21   thorpej 				break;
    332  1.21   thorpej 			}
    333  1.21   thorpej 			usage();
    334  1.21   thorpej 			/* NOTREACHED */
    335  1.21   thorpej 			break;
    336  1.21   thorpej #endif	/* ENCRYPTION */
    337  1.21   thorpej 
    338   1.6       tls 		case 'g':
    339   1.6       tls 			gettyname = optarg;
    340   1.6       tls 			break;
    341   1.1       cgd 
    342   1.1       cgd 		case 'h':
    343   1.1       cgd 			hostinfo = 0;
    344   1.1       cgd 			break;
    345   1.1       cgd 
    346  1.21   thorpej #ifdef	KRB5
    347  1.21   thorpej 		case 'H':
    348  1.21   thorpej 		    {
    349  1.21   thorpej 			require_hwpreauth = 1;
    350  1.21   thorpej 			break;
    351  1.21   thorpej 		    }
    352  1.21   thorpej #endif	/* KRB5 */
    353  1.21   thorpej 
    354   1.1       cgd 
    355   1.1       cgd #ifdef	LINEMODE
    356   1.1       cgd 		case 'l':
    357   1.1       cgd 			alwayslinemode = 1;
    358   1.1       cgd 			break;
    359   1.1       cgd #endif	/* LINEMODE */
    360   1.1       cgd 
    361   1.3       cgd 		case 'k':
    362   1.3       cgd #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    363   1.3       cgd 			lmodetype = NO_AUTOKLUDGE;
    364   1.3       cgd #else
    365   1.3       cgd 			/* ignore -k option if built without kludge linemode */
    366   1.3       cgd #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    367   1.3       cgd 			break;
    368   1.3       cgd 
    369   1.1       cgd 		case 'n':
    370   1.1       cgd 			keepalive = 0;
    371   1.1       cgd 			break;
    372   1.1       cgd 
    373   1.1       cgd #ifdef CRAY
    374   1.1       cgd 		case 'r':
    375   1.1       cgd 		    {
    376   1.1       cgd 			char *strchr();
    377   1.1       cgd 			char *c;
    378   1.1       cgd 
    379   1.1       cgd 			/*
    380   1.1       cgd 			 * Allow the specification of alterations
    381   1.1       cgd 			 * to the pty search range.  It is legal to
    382   1.1       cgd 			 * specify only one, and not change the
    383   1.1       cgd 			 * other from its default.
    384   1.1       cgd 			 */
    385   1.1       cgd 			c = strchr(optarg, '-');
    386   1.1       cgd 			if (c) {
    387   1.1       cgd 				*c++ = '\0';
    388   1.1       cgd 				highpty = atoi(c);
    389   1.1       cgd 			}
    390   1.1       cgd 			if (*optarg != '\0')
    391   1.1       cgd 				lowpty = atoi(optarg);
    392   1.1       cgd 			if ((lowpty > highpty) || (lowpty < 0) ||
    393   1.1       cgd 							(highpty > 32767)) {
    394   1.1       cgd 				usage();
    395   1.1       cgd 				/* NOT REACHED */
    396   1.1       cgd 			}
    397   1.1       cgd 			break;
    398   1.1       cgd 		    }
    399   1.1       cgd #endif	/* CRAY */
    400   1.1       cgd 
    401  1.21   thorpej #ifdef	KRB5
    402  1.21   thorpej 		case 'R':
    403  1.21   thorpej 		    {
    404  1.21   thorpej 			krb5_error_code retval;
    405  1.21   thorpej 
    406  1.21   thorpej 			if (telnet_context == 0) {
    407  1.21   thorpej 				retval = krb5_init_context(&telnet_context);
    408  1.21   thorpej 				if (retval) {
    409  1.21   thorpej 					com_err("telnetd", retval,
    410  1.21   thorpej 					    "while initializing krb5");
    411  1.21   thorpej 					exit(1);
    412  1.21   thorpej 				}
    413  1.21   thorpej 			}
    414  1.21   thorpej 			krb5_set_default_realm(telnet_context, optarg);
    415  1.21   thorpej 			break;
    416  1.21   thorpej 		    }
    417  1.21   thorpej #endif	/* KRB5 */
    418  1.21   thorpej 
    419  1.15      dean #ifdef	SECURELOGIN
    420   1.1       cgd 		case 's':
    421  1.15      dean 			/* Secure login required */
    422  1.15      dean 			require_secure_login = 1;
    423   1.1       cgd 			break;
    424  1.15      dean #endif	/* SECURELOGIN */
    425   1.1       cgd 		case 'S':
    426   1.1       cgd #ifdef	HAS_GETTOS
    427   1.1       cgd 			if ((tos = parsetos(optarg, "tcp")) < 0)
    428   1.1       cgd 				fprintf(stderr, "%s%s%s\n",
    429   1.1       cgd 					"telnetd: Bad TOS argument '", optarg,
    430   1.1       cgd 					"'; will try to use default TOS");
    431   1.1       cgd #else
    432   1.1       cgd 			fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
    433   1.1       cgd 						"-S flag not supported\n");
    434   1.1       cgd #endif
    435   1.1       cgd 			break;
    436   1.1       cgd 
    437   1.3       cgd 		case 'u':
    438   1.3       cgd 			utmp_len = atoi(optarg);
    439   1.3       cgd 			break;
    440   1.3       cgd 
    441   1.3       cgd 		case 'U':
    442   1.3       cgd 			registerd_host_only = 1;
    443   1.3       cgd 			break;
    444   1.3       cgd 
    445   1.3       cgd #ifdef	AUTHENTICATION
    446   1.1       cgd 		case 'X':
    447   1.1       cgd 			/*
    448   1.1       cgd 			 * Check for invalid authentication types
    449   1.1       cgd 			 */
    450   1.1       cgd 			auth_disable_name(optarg);
    451   1.1       cgd 			break;
    452   1.3       cgd #endif	/* AUTHENTICATION */
    453   1.1       cgd 
    454  1.16    itojun 		case '4':
    455  1.16    itojun 			family = AF_INET;
    456  1.16    itojun 			break;
    457  1.18   thorpej 
    458  1.16    itojun 		case '6':
    459  1.16    itojun 			family = AF_INET6;
    460  1.16    itojun 			break;
    461  1.18   thorpej 
    462   1.1       cgd 		default:
    463   1.3       cgd 			fprintf(stderr, "telnetd: %c: unknown option\n", ch);
    464   1.1       cgd 			/* FALLTHROUGH */
    465   1.1       cgd 		case '?':
    466   1.1       cgd 			usage();
    467   1.1       cgd 			/* NOTREACHED */
    468   1.1       cgd 		}
    469   1.1       cgd 	}
    470   1.1       cgd 
    471   1.1       cgd 	argc -= optind;
    472   1.1       cgd 	argv += optind;
    473   1.1       cgd 
    474   1.1       cgd 	if (debug) {
    475  1.16    itojun 	    int s, ns, foo, error;
    476  1.16    itojun 	    char *service = "telnet";
    477  1.16    itojun 	    struct addrinfo hints, *res;
    478   1.1       cgd 
    479   1.1       cgd 	    if (argc > 1) {
    480   1.1       cgd 		usage();
    481   1.1       cgd 		/* NOT REACHED */
    482  1.16    itojun 	    } else if (argc == 1)
    483  1.16    itojun 		service = *argv;
    484  1.16    itojun 
    485  1.16    itojun 	    memset(&hints, 0, sizeof(hints));
    486  1.16    itojun 	    hints.ai_flags = AI_PASSIVE;
    487  1.16    itojun 	    hints.ai_family = family;
    488  1.16    itojun 	    hints.ai_socktype = SOCK_STREAM;
    489  1.16    itojun 	    hints.ai_protocol = 0;
    490  1.16    itojun 	    error = getaddrinfo(NULL, service, &hints, &res);
    491  1.16    itojun 
    492  1.16    itojun 	    if (error) {
    493  1.22    itojun 		fprintf(stderr, "tcp/%s: %s\n", service, gai_strerror(error));
    494  1.22    itojun 		exit(1);
    495   1.1       cgd 	    }
    496   1.1       cgd 
    497  1.16    itojun 	    s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    498   1.1       cgd 	    if (s < 0) {
    499  1.28       wiz 		perror("telnetd: socket");
    500  1.22    itojun 		exit(1);
    501   1.1       cgd 	    }
    502   1.3       cgd 	    (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
    503   1.3       cgd 				(char *)&on, sizeof(on));
    504  1.16    itojun 	    if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
    505   1.1       cgd 		perror("bind");
    506   1.1       cgd 		exit(1);
    507   1.1       cgd 	    }
    508   1.1       cgd 	    if (listen(s, 1) < 0) {
    509   1.1       cgd 		perror("listen");
    510   1.1       cgd 		exit(1);
    511   1.1       cgd 	    }
    512  1.16    itojun 	    foo = res->ai_addrlen;
    513  1.16    itojun 	    ns = accept(s, res->ai_addr, &foo);
    514   1.1       cgd 	    if (ns < 0) {
    515   1.1       cgd 		perror("accept");
    516   1.1       cgd 		exit(1);
    517   1.1       cgd 	    }
    518   1.1       cgd 	    (void) dup2(ns, 0);
    519   1.1       cgd 	    (void) close(ns);
    520   1.1       cgd 	    (void) close(s);
    521   1.1       cgd #ifdef convex
    522   1.1       cgd 	} else if (argc == 1) {
    523   1.1       cgd 		; /* VOID*/		/* Just ignore the host/port name */
    524   1.1       cgd #endif
    525   1.1       cgd 	} else if (argc > 0) {
    526   1.1       cgd 		usage();
    527   1.1       cgd 		/* NOT REACHED */
    528   1.1       cgd 	}
    529   1.1       cgd 
    530   1.3       cgd #if	defined(_SC_CRAY_SECURE_SYS)
    531   1.3       cgd 	secflag = sysconf(_SC_CRAY_SECURE_SYS);
    532   1.3       cgd 
    533   1.3       cgd 	/*
    534   1.3       cgd 	 *	Get socket's security label
    535   1.3       cgd 	 */
    536   1.3       cgd 	if (secflag)  {
    537   1.3       cgd 		int szss = sizeof(ss);
    538   1.3       cgd #ifdef SO_SEC_MULTI			/* 8.0 code */
    539   1.3       cgd 		int sock_multi;
    540   1.3       cgd 		int szi = sizeof(int);
    541   1.3       cgd #endif /* SO_SEC_MULTI */
    542   1.3       cgd 
    543   1.4       jtk 		memset((char *)&dv, 0, sizeof(dv));
    544   1.3       cgd 
    545   1.3       cgd 		if (getsysv(&sysv, sizeof(struct sysv)) != 0) {
    546   1.3       cgd 			perror("getsysv");
    547   1.3       cgd 			exit(1);
    548   1.3       cgd 		}
    549   1.3       cgd 
    550   1.3       cgd 		/*
    551   1.3       cgd 		 *	Get socket security label and set device values
    552   1.3       cgd 		 *	   {security label to be set on ttyp device}
    553   1.3       cgd 		 */
    554   1.3       cgd #ifdef SO_SEC_MULTI			/* 8.0 code */
    555   1.3       cgd 		if ((getsockopt(0, SOL_SOCKET, SO_SECURITY,
    556   1.3       cgd 			       (char *)&ss, &szss) < 0) ||
    557   1.3       cgd 		    (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI,
    558   1.3       cgd 				(char *)&sock_multi, &szi) < 0)) {
    559   1.3       cgd 			perror("getsockopt");
    560   1.3       cgd 			exit(1);
    561   1.3       cgd 		} else {
    562   1.3       cgd 			dv.dv_actlvl = ss.ss_actlabel.lt_level;
    563   1.3       cgd 			dv.dv_actcmp = ss.ss_actlabel.lt_compart;
    564   1.3       cgd 			if (!sock_multi) {
    565   1.3       cgd 				dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl;
    566   1.3       cgd 				dv.dv_valcmp = dv.dv_actcmp;
    567   1.3       cgd 			} else {
    568   1.3       cgd 				dv.dv_minlvl = ss.ss_minlabel.lt_level;
    569   1.3       cgd 				dv.dv_maxlvl = ss.ss_maxlabel.lt_level;
    570   1.3       cgd 				dv.dv_valcmp = ss.ss_maxlabel.lt_compart;
    571   1.3       cgd 			}
    572   1.3       cgd 			dv.dv_devflg = 0;
    573   1.3       cgd 		}
    574   1.3       cgd #else /* SO_SEC_MULTI */		/* 7.0 code */
    575   1.3       cgd 		if (getsockopt(0, SOL_SOCKET, SO_SECURITY,
    576   1.3       cgd 				(char *)&ss, &szss) >= 0) {
    577   1.3       cgd 			dv.dv_actlvl = ss.ss_slevel;
    578   1.3       cgd 			dv.dv_actcmp = ss.ss_compart;
    579   1.3       cgd 			dv.dv_minlvl = ss.ss_minlvl;
    580   1.3       cgd 			dv.dv_maxlvl = ss.ss_maxlvl;
    581   1.3       cgd 			dv.dv_valcmp = ss.ss_maxcmp;
    582   1.3       cgd 		}
    583   1.3       cgd #endif /* SO_SEC_MULTI */
    584   1.3       cgd 	}
    585   1.3       cgd #endif	/* _SC_CRAY_SECURE_SYS */
    586   1.3       cgd 
    587  1.24     lukem 	openlog("telnetd", LOG_PID, LOG_DAEMON);
    588   1.1       cgd 	fromlen = sizeof (from);
    589   1.1       cgd 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
    590   1.1       cgd 		fprintf(stderr, "%s: ", progname);
    591   1.1       cgd 		perror("getpeername");
    592   1.1       cgd 		_exit(1);
    593   1.1       cgd 	}
    594   1.1       cgd 	if (keepalive &&
    595   1.3       cgd 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
    596   1.3       cgd 			(char *)&on, sizeof (on)) < 0) {
    597   1.1       cgd 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    598   1.1       cgd 	}
    599   1.1       cgd 
    600   1.1       cgd #if	defined(IPPROTO_IP) && defined(IP_TOS)
    601  1.19    itojun 	if (((struct sockaddr *)&from)->sa_family == AF_INET) {
    602   1.1       cgd # if	defined(HAS_GETTOS)
    603   1.1       cgd 		struct tosent *tp;
    604   1.1       cgd 		if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
    605   1.1       cgd 			tos = tp->t_tos;
    606   1.1       cgd # endif
    607   1.1       cgd 		if (tos < 0)
    608   1.1       cgd 			tos = 020;	/* Low Delay bit */
    609   1.1       cgd 		if (tos
    610   1.3       cgd 		   && (setsockopt(0, IPPROTO_IP, IP_TOS,
    611   1.3       cgd 				  (char *)&tos, sizeof(tos)) < 0)
    612   1.1       cgd 		   && (errno != ENOPROTOOPT) )
    613   1.1       cgd 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    614   1.1       cgd 	}
    615   1.1       cgd #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
    616  1.16    itojun 
    617   1.1       cgd 	net = 0;
    618  1.16    itojun 	doit((struct sockaddr *)&from);
    619   1.1       cgd 	/* NOTREACHED */
    620   1.9       mrg #ifdef __GNUC__
    621   1.9       mrg 	exit(0);
    622   1.9       mrg #endif
    623   1.1       cgd }  /* end of main */
    624   1.1       cgd 
    625   1.1       cgd 	void
    626   1.1       cgd usage()
    627   1.1       cgd {
    628   1.1       cgd 	fprintf(stderr, "Usage: telnetd");
    629   1.3       cgd #ifdef	AUTHENTICATION
    630   1.3       cgd 	fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
    631   1.1       cgd #endif
    632   1.1       cgd #ifdef BFTPDAEMON
    633   1.1       cgd 	fprintf(stderr, " [-B]");
    634   1.1       cgd #endif
    635   1.1       cgd 	fprintf(stderr, " [-debug]");
    636   1.1       cgd #ifdef DIAGNOSTICS
    637   1.1       cgd 	fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
    638   1.1       cgd #endif
    639  1.31       wiz #ifdef	ENCRYPTION
    640   1.1       cgd 	fprintf(stderr, " [-edebug]");
    641   1.1       cgd #endif
    642   1.1       cgd 	fprintf(stderr, " [-h]");
    643   1.3       cgd #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    644   1.3       cgd 	fprintf(stderr, " [-k]");
    645   1.3       cgd #endif
    646   1.1       cgd #ifdef LINEMODE
    647   1.1       cgd 	fprintf(stderr, " [-l]");
    648   1.1       cgd #endif
    649   1.1       cgd 	fprintf(stderr, " [-n]");
    650   1.1       cgd #ifdef	CRAY
    651   1.1       cgd 	fprintf(stderr, " [-r[lowpty]-[highpty]]");
    652   1.1       cgd #endif
    653   1.3       cgd 	fprintf(stderr, "\n\t");
    654  1.15      dean #ifdef	SECURELOGIN
    655   1.1       cgd 	fprintf(stderr, " [-s]");
    656   1.1       cgd #endif
    657   1.3       cgd #ifdef	HAS_GETTOS
    658   1.3       cgd 	fprintf(stderr, " [-S tos]");
    659   1.3       cgd #endif
    660   1.3       cgd #ifdef	AUTHENTICATION
    661   1.1       cgd 	fprintf(stderr, " [-X auth-type]");
    662   1.1       cgd #endif
    663   1.3       cgd 	fprintf(stderr, " [-u utmp_hostname_length] [-U]");
    664   1.1       cgd 	fprintf(stderr, " [port]\n");
    665   1.1       cgd 	exit(1);
    666   1.1       cgd }
    667   1.1       cgd 
    668   1.1       cgd /*
    669   1.1       cgd  * getterminaltype
    670   1.1       cgd  *
    671   1.1       cgd  *	Ask the other end to send along its terminal type and speed.
    672   1.1       cgd  * Output is the variable terminaltype filled in.
    673   1.1       cgd  */
    674   1.3       cgd static unsigned char ttytype_sbbuf[] = {
    675   1.3       cgd 	IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
    676   1.3       cgd };
    677   1.1       cgd 
    678   1.1       cgd     int
    679   1.1       cgd getterminaltype(name)
    680   1.1       cgd     char *name;
    681   1.1       cgd {
    682   1.1       cgd     int retval = -1;
    683   1.1       cgd 
    684   1.1       cgd     settimer(baseline);
    685   1.3       cgd #if	defined(AUTHENTICATION)
    686   1.1       cgd     /*
    687   1.1       cgd      * Handle the Authentication option before we do anything else.
    688   1.1       cgd      */
    689   1.1       cgd     send_do(TELOPT_AUTHENTICATION, 1);
    690   1.1       cgd     while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
    691   1.1       cgd 	ttloop();
    692   1.1       cgd     if (his_state_is_will(TELOPT_AUTHENTICATION)) {
    693   1.1       cgd 	retval = auth_wait(name);
    694   1.1       cgd     }
    695   1.1       cgd #endif
    696   1.1       cgd 
    697  1.21   thorpej #ifdef	ENCRYPTION
    698  1.21   thorpej     send_will(TELOPT_ENCRYPT, 1);
    699  1.21   thorpej #endif	/* ENCRYPTION */
    700   1.1       cgd     send_do(TELOPT_TTYPE, 1);
    701   1.1       cgd     send_do(TELOPT_TSPEED, 1);
    702   1.1       cgd     send_do(TELOPT_XDISPLOC, 1);
    703   1.3       cgd     send_do(TELOPT_NEW_ENVIRON, 1);
    704   1.3       cgd     send_do(TELOPT_OLD_ENVIRON, 1);
    705   1.1       cgd     while (
    706  1.21   thorpej #ifdef	ENCRYPTION
    707  1.21   thorpej 	   his_do_dont_is_changing(TELOPT_ENCRYPT) ||
    708  1.21   thorpej #endif	/* ENCRYPTION */
    709   1.1       cgd 	   his_will_wont_is_changing(TELOPT_TTYPE) ||
    710   1.1       cgd 	   his_will_wont_is_changing(TELOPT_TSPEED) ||
    711   1.1       cgd 	   his_will_wont_is_changing(TELOPT_XDISPLOC) ||
    712   1.3       cgd 	   his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
    713   1.3       cgd 	   his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
    714   1.1       cgd 	ttloop();
    715   1.1       cgd     }
    716   1.1       cgd     if (his_state_is_will(TELOPT_TSPEED)) {
    717   1.3       cgd 	static unsigned char sb[] =
    718   1.3       cgd 			{ IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
    719   1.1       cgd 
    720  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    721   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    722   1.1       cgd     }
    723  1.21   thorpej #ifdef	ENCRYPTION
    724  1.21   thorpej     /*
    725  1.21   thorpej      * Wait for the negotiation of what type of encryption we can
    726  1.21   thorpej      * send with.  If autoencrypt is not set, this will just return.
    727  1.21   thorpej      */
    728  1.21   thorpej     if (his_state_is_will(TELOPT_ENCRYPT)) {
    729  1.21   thorpej 	encrypt_wait();
    730  1.21   thorpej     }
    731  1.21   thorpej #endif	/* ENCRYPTION */
    732   1.1       cgd     if (his_state_is_will(TELOPT_XDISPLOC)) {
    733   1.3       cgd 	static unsigned char sb[] =
    734   1.3       cgd 			{ IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
    735   1.3       cgd 
    736  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    737   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    738   1.3       cgd     }
    739   1.3       cgd     if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
    740   1.3       cgd 	static unsigned char sb[] =
    741   1.3       cgd 			{ IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
    742   1.1       cgd 
    743  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    744   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    745   1.1       cgd     }
    746   1.3       cgd     else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
    747   1.3       cgd 	static unsigned char sb[] =
    748   1.3       cgd 			{ IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
    749   1.1       cgd 
    750  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    751   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    752   1.1       cgd     }
    753   1.1       cgd     if (his_state_is_will(TELOPT_TTYPE)) {
    754   1.1       cgd 
    755  1.27       wiz 	output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
    756   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
    757   1.4       jtk 					sizeof ttytype_sbbuf - 2););
    758   1.1       cgd     }
    759   1.1       cgd     if (his_state_is_will(TELOPT_TSPEED)) {
    760   1.1       cgd 	while (sequenceIs(tspeedsubopt, baseline))
    761   1.1       cgd 	    ttloop();
    762   1.1       cgd     }
    763   1.1       cgd     if (his_state_is_will(TELOPT_XDISPLOC)) {
    764   1.1       cgd 	while (sequenceIs(xdisplocsubopt, baseline))
    765   1.1       cgd 	    ttloop();
    766   1.1       cgd     }
    767   1.3       cgd     if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
    768   1.1       cgd 	while (sequenceIs(environsubopt, baseline))
    769   1.1       cgd 	    ttloop();
    770   1.1       cgd     }
    771   1.3       cgd     if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
    772   1.3       cgd 	while (sequenceIs(oenvironsubopt, baseline))
    773   1.3       cgd 	    ttloop();
    774   1.3       cgd     }
    775   1.1       cgd     if (his_state_is_will(TELOPT_TTYPE)) {
    776   1.1       cgd 	char first[256], last[256];
    777   1.1       cgd 
    778   1.1       cgd 	while (sequenceIs(ttypesubopt, baseline))
    779   1.1       cgd 	    ttloop();
    780   1.1       cgd 
    781   1.1       cgd 	/*
    782   1.1       cgd 	 * If the other side has already disabled the option, then
    783   1.1       cgd 	 * we have to just go with what we (might) have already gotten.
    784   1.1       cgd 	 */
    785   1.1       cgd 	if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
    786  1.21   thorpej 	    (void) strncpy(first, terminaltype, sizeof(first) - 1);
    787  1.21   thorpej 	    first[sizeof(first) - 1] = '\0';
    788   1.1       cgd 	    for(;;) {
    789   1.1       cgd 		/*
    790   1.1       cgd 		 * Save the unknown name, and request the next name.
    791   1.1       cgd 		 */
    792  1.21   thorpej 		(void) strncpy(last, terminaltype, sizeof(last) - 1);
    793  1.21   thorpej 		last[sizeof(last) - 1] = '\0';
    794   1.1       cgd 		_gettermname();
    795   1.1       cgd 		if (terminaltypeok(terminaltype))
    796   1.1       cgd 		    break;
    797   1.1       cgd 		if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
    798   1.1       cgd 		    his_state_is_wont(TELOPT_TTYPE)) {
    799   1.1       cgd 		    /*
    800   1.1       cgd 		     * We've hit the end.  If this is the same as
    801   1.1       cgd 		     * the first name, just go with it.
    802   1.1       cgd 		     */
    803   1.1       cgd 		    if (strncmp(first, terminaltype, sizeof(first)) == 0)
    804   1.1       cgd 			break;
    805   1.1       cgd 		    /*
    806   1.1       cgd 		     * Get the terminal name one more time, so that
    807   1.1       cgd 		     * RFC1091 compliant telnets will cycle back to
    808   1.1       cgd 		     * the start of the list.
    809   1.1       cgd 		     */
    810   1.1       cgd 		     _gettermname();
    811  1.21   thorpej 		    if (strncmp(first, terminaltype, sizeof(first)) != 0) {
    812  1.21   thorpej 			(void) strncpy(terminaltype, first, sizeof(first) - 1);
    813  1.21   thorpej 			terminaltype[sizeof(terminaltype) - 1] = '\0';
    814  1.21   thorpej 		    }
    815   1.1       cgd 		    break;
    816   1.1       cgd 		}
    817   1.1       cgd 	    }
    818   1.1       cgd 	}
    819   1.1       cgd     }
    820   1.1       cgd     return(retval);
    821   1.1       cgd }  /* end of getterminaltype */
    822   1.1       cgd 
    823   1.1       cgd     void
    824   1.1       cgd _gettermname()
    825   1.1       cgd {
    826   1.1       cgd     /*
    827   1.1       cgd      * If the client turned off the option,
    828   1.1       cgd      * we can't send another request, so we
    829   1.1       cgd      * just return.
    830   1.1       cgd      */
    831   1.1       cgd     if (his_state_is_wont(TELOPT_TTYPE))
    832   1.1       cgd 	return;
    833   1.1       cgd     settimer(baseline);
    834  1.27       wiz     output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
    835   1.4       jtk     DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
    836   1.4       jtk 					sizeof ttytype_sbbuf - 2););
    837   1.1       cgd     while (sequenceIs(ttypesubopt, baseline))
    838   1.1       cgd 	ttloop();
    839   1.1       cgd }
    840   1.1       cgd 
    841   1.1       cgd     int
    842   1.1       cgd terminaltypeok(s)
    843   1.1       cgd     char *s;
    844   1.1       cgd {
    845   1.1       cgd     char buf[1024];
    846   1.1       cgd 
    847   1.1       cgd     if (terminaltype == NULL)
    848   1.1       cgd 	return(1);
    849   1.1       cgd 
    850   1.1       cgd     /*
    851   1.1       cgd      * tgetent() will return 1 if the type is known, and
    852   1.1       cgd      * 0 if it is not known.  If it returns -1, it couldn't
    853   1.1       cgd      * open the database.  But if we can't open the database,
    854   1.1       cgd      * it won't help to say we failed, because we won't be
    855   1.1       cgd      * able to verify anything else.  So, we treat -1 like 1.
    856   1.1       cgd      */
    857   1.1       cgd     if (tgetent(buf, s) == 0)
    858   1.1       cgd 	return(0);
    859   1.1       cgd     return(1);
    860   1.1       cgd }
    861   1.1       cgd 
    862   1.1       cgd #ifndef	MAXHOSTNAMELEN
    863   1.1       cgd #define	MAXHOSTNAMELEN 64
    864   1.1       cgd #endif	/* MAXHOSTNAMELEN */
    865   1.1       cgd 
    866   1.1       cgd char *hostname;
    867  1.13       mrg char host_name[MAXHOSTNAMELEN + 1];
    868  1.13       mrg char remote_host_name[MAXHOSTNAMELEN + 1];
    869   1.1       cgd 
    870   1.1       cgd #ifndef	convex
    871   1.1       cgd extern void telnet P((int, int));
    872   1.1       cgd #else
    873   1.1       cgd extern void telnet P((int, int, char *));
    874   1.1       cgd #endif
    875   1.1       cgd 
    876   1.1       cgd /*
    877   1.1       cgd  * Get a pty, scan input lines.
    878   1.1       cgd  */
    879   1.9       mrg void
    880   1.1       cgd doit(who)
    881  1.16    itojun 	struct sockaddr *who;
    882   1.1       cgd {
    883   1.9       mrg 	char *host;
    884  1.16    itojun 	int error;
    885   1.1       cgd 	int level;
    886   1.3       cgd 	int ptynum;
    887   1.1       cgd 	char user_name[256];
    888   1.1       cgd 
    889   1.1       cgd 	/*
    890   1.1       cgd 	 * Find an available pty to use.
    891   1.1       cgd 	 */
    892   1.1       cgd #ifndef	convex
    893   1.3       cgd 	pty = getpty(&ptynum);
    894   1.1       cgd 	if (pty < 0)
    895   1.1       cgd 		fatal(net, "All network ports in use");
    896   1.1       cgd #else
    897   1.1       cgd 	for (;;) {
    898   1.1       cgd 		char *lp;
    899   1.1       cgd 
    900   1.1       cgd 		if ((lp = getpty()) == NULL)
    901   1.1       cgd 			fatal(net, "Out of ptys");
    902   1.1       cgd 
    903   1.1       cgd 		if ((pty = open(lp, 2)) >= 0) {
    904  1.23  christos 			(void)strlcpy(line, lp, sizeof(NULL16STR));
    905   1.1       cgd 			line[5] = 't';
    906   1.1       cgd 			break;
    907   1.1       cgd 		}
    908   1.1       cgd 	}
    909   1.1       cgd #endif
    910   1.1       cgd 
    911   1.3       cgd #if	defined(_SC_CRAY_SECURE_SYS)
    912   1.3       cgd 	/*
    913   1.4       jtk 	 *	set ttyp line security label
    914   1.3       cgd 	 */
    915   1.3       cgd 	if (secflag) {
    916   1.3       cgd 		char slave_dev[16];
    917   1.3       cgd 
    918   1.3       cgd 		sprintf(tty_dev, "/dev/pty/%03d", ptynum);
    919   1.3       cgd 		if (setdevs(tty_dev, &dv) < 0)
    920   1.3       cgd 		 	fatal(net, "cannot set pty security");
    921   1.3       cgd 		sprintf(slave_dev, "/dev/ttyp%03d", ptynum);
    922   1.3       cgd 		if (setdevs(slave_dev, &dv) < 0)
    923   1.3       cgd 		 	fatal(net, "cannot set tty security");
    924   1.3       cgd 	}
    925   1.3       cgd #endif	/* _SC_CRAY_SECURE_SYS */
    926   1.3       cgd 
    927   1.1       cgd 	/* get name of connected client */
    928  1.32    itojun 	error = getnameinfo(who, who->sa_len, remote_host_name,
    929  1.32    itojun 	    sizeof(remote_host_name), NULL, 0, 0);
    930  1.32    itojun 
    931  1.32    itojun 	if (!error && strlen(remote_host_name) > utmp_len)
    932  1.32    itojun 		error = getnameinfo(who, who->sa_len, remote_host_name,
    933  1.32    itojun 		    sizeof(remote_host_name), NULL, 0, NI_NUMERICHOST);
    934   1.3       cgd 
    935  1.16    itojun 	if (error) {
    936   1.3       cgd 		fatal(net, "Couldn't resolve your address into a host name.\r\n\
    937   1.4       jtk 	 Please contact your net administrator");
    938   1.9       mrg #ifdef __GNUC__
    939   1.9       mrg 		host = NULL;	/* XXX gcc */
    940   1.9       mrg #endif
    941   1.3       cgd 	}
    942  1.16    itojun 
    943   1.1       cgd 	remote_host_name[sizeof(remote_host_name)-1] = 0;
    944   1.1       cgd 	host = remote_host_name;
    945   1.1       cgd 
    946  1.13       mrg 	(void)gethostname(host_name, sizeof (host_name));
    947  1.13       mrg 	host_name[sizeof(host_name) - 1] = '\0';
    948   1.1       cgd 	hostname = host_name;
    949   1.1       cgd 
    950  1.21   thorpej #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
    951   1.1       cgd 	auth_encrypt_init(hostname, host, "TELNETD", 1);
    952   1.1       cgd #endif
    953   1.1       cgd 
    954   1.1       cgd 	init_env();
    955   1.1       cgd 	/*
    956   1.1       cgd 	 * get terminal type.
    957   1.1       cgd 	 */
    958   1.1       cgd 	*user_name = 0;
    959   1.1       cgd 	level = getterminaltype(user_name);
    960   1.1       cgd 	setenv("TERM", terminaltype ? terminaltype : "network", 1);
    961   1.1       cgd 
    962   1.1       cgd 	/*
    963   1.1       cgd 	 * Start up the login process on the slave side of the terminal
    964   1.1       cgd 	 */
    965   1.1       cgd #ifndef	convex
    966   1.1       cgd 	startslave(host, level, user_name);
    967   1.1       cgd 
    968   1.3       cgd #if	defined(_SC_CRAY_SECURE_SYS)
    969   1.3       cgd 	if (secflag) {
    970   1.3       cgd 		if (setulvl(dv.dv_actlvl) < 0)
    971   1.3       cgd 			fatal(net,"cannot setulvl()");
    972   1.3       cgd 		if (setucmp(dv.dv_actcmp) < 0)
    973   1.3       cgd 			fatal(net, "cannot setucmp()");
    974   1.3       cgd 	}
    975   1.3       cgd #endif	/* _SC_CRAY_SECURE_SYS */
    976   1.3       cgd 
    977   1.1       cgd 	telnet(net, pty);  /* begin server processing */
    978   1.1       cgd #else
    979   1.1       cgd 	telnet(net, pty, host);
    980   1.1       cgd #endif
    981   1.1       cgd 	/*NOTREACHED*/
    982   1.1       cgd }  /* end of doit */
    983   1.1       cgd 
    984   1.1       cgd 
    985   1.1       cgd /*
    986   1.1       cgd  * Main loop.  Select from pty and network, and
    987   1.1       cgd  * hand data to telnet receiver finite state machine.
    988   1.1       cgd  */
    989   1.1       cgd 	void
    990   1.1       cgd #ifndef	convex
    991   1.1       cgd telnet(f, p)
    992   1.1       cgd #else
    993   1.1       cgd telnet(f, p, host)
    994   1.1       cgd #endif
    995   1.1       cgd 	int f, p;
    996   1.1       cgd #ifdef convex
    997   1.1       cgd 	char *host;
    998   1.1       cgd #endif
    999   1.1       cgd {
   1000   1.1       cgd 	int on = 1;
   1001   1.1       cgd #define	TABBUFSIZ	512
   1002   1.1       cgd 	char	defent[TABBUFSIZ];
   1003   1.1       cgd 	char	defstrs[TABBUFSIZ];
   1004   1.1       cgd #undef	TABBUFSIZ
   1005  1.20        ad 	char *HE, *HN, *IM, *IF, *ptyibuf2ptr;
   1006   1.4       jtk 	int nfd;
   1007   1.1       cgd 
   1008   1.1       cgd 	/*
   1009   1.1       cgd 	 * Initialize the slc mapping table.
   1010   1.1       cgd 	 */
   1011   1.1       cgd 	get_slc_defaults();
   1012   1.1       cgd 
   1013   1.1       cgd 	/*
   1014   1.1       cgd 	 * Do some tests where it is desireable to wait for a response.
   1015   1.1       cgd 	 * Rather than doing them slowly, one at a time, do them all
   1016   1.1       cgd 	 * at once.
   1017   1.1       cgd 	 */
   1018   1.1       cgd 	if (my_state_is_wont(TELOPT_SGA))
   1019   1.1       cgd 		send_will(TELOPT_SGA, 1);
   1020   1.1       cgd 	/*
   1021   1.1       cgd 	 * Is the client side a 4.2 (NOT 4.3) system?  We need to know this
   1022   1.1       cgd 	 * because 4.2 clients are unable to deal with TCP urgent data.
   1023   1.1       cgd 	 *
   1024   1.1       cgd 	 * To find out, we send out a "DO ECHO".  If the remote system
   1025   1.1       cgd 	 * answers "WILL ECHO" it is probably a 4.2 client, and we note
   1026   1.1       cgd 	 * that fact ("WILL ECHO" ==> that the client will echo what
   1027   1.1       cgd 	 * WE, the server, sends it; it does NOT mean that the client will
   1028   1.1       cgd 	 * echo the terminal input).
   1029   1.1       cgd 	 */
   1030   1.1       cgd 	send_do(TELOPT_ECHO, 1);
   1031   1.1       cgd 
   1032   1.1       cgd #ifdef	LINEMODE
   1033   1.1       cgd 	if (his_state_is_wont(TELOPT_LINEMODE)) {
   1034   1.1       cgd 		/* Query the peer for linemode support by trying to negotiate
   1035   1.1       cgd 		 * the linemode option.
   1036   1.1       cgd 		 */
   1037   1.1       cgd 		linemode = 0;
   1038   1.1       cgd 		editmode = 0;
   1039   1.1       cgd 		send_do(TELOPT_LINEMODE, 1);  /* send do linemode */
   1040   1.1       cgd 	}
   1041   1.1       cgd #endif	/* LINEMODE */
   1042   1.1       cgd 
   1043   1.1       cgd 	/*
   1044   1.1       cgd 	 * Send along a couple of other options that we wish to negotiate.
   1045   1.1       cgd 	 */
   1046   1.1       cgd 	send_do(TELOPT_NAWS, 1);
   1047   1.1       cgd 	send_will(TELOPT_STATUS, 1);
   1048   1.3       cgd 	flowmode = 1;		/* default flow control state */
   1049   1.3       cgd 	restartany = -1;	/* uninitialized... */
   1050   1.1       cgd 	send_do(TELOPT_LFLOW, 1);
   1051   1.1       cgd 
   1052   1.1       cgd 	/*
   1053   1.1       cgd 	 * Spin, waiting for a response from the DO ECHO.  However,
   1054   1.1       cgd 	 * some REALLY DUMB telnets out there might not respond
   1055   1.1       cgd 	 * to the DO ECHO.  So, we spin looking for NAWS, (most dumb
   1056   1.1       cgd 	 * telnets so far seem to respond with WONT for a DO that
   1057   1.1       cgd 	 * they don't understand...) because by the time we get the
   1058   1.1       cgd 	 * response, it will already have processed the DO ECHO.
   1059   1.1       cgd 	 * Kludge upon kludge.
   1060   1.1       cgd 	 */
   1061   1.1       cgd 	while (his_will_wont_is_changing(TELOPT_NAWS))
   1062   1.1       cgd 		ttloop();
   1063   1.1       cgd 
   1064   1.1       cgd 	/*
   1065   1.1       cgd 	 * But...
   1066   1.1       cgd 	 * The client might have sent a WILL NAWS as part of its
   1067   1.1       cgd 	 * startup code; if so, we'll be here before we get the
   1068   1.1       cgd 	 * response to the DO ECHO.  We'll make the assumption
   1069   1.1       cgd 	 * that any implementation that understands about NAWS
   1070   1.1       cgd 	 * is a modern enough implementation that it will respond
   1071   1.1       cgd 	 * to our DO ECHO request; hence we'll do another spin
   1072   1.1       cgd 	 * waiting for the ECHO option to settle down, which is
   1073   1.1       cgd 	 * what we wanted to do in the first place...
   1074   1.1       cgd 	 */
   1075   1.1       cgd 	if (his_want_state_is_will(TELOPT_ECHO) &&
   1076   1.1       cgd 	    his_state_is_will(TELOPT_NAWS)) {
   1077   1.1       cgd 		while (his_will_wont_is_changing(TELOPT_ECHO))
   1078   1.1       cgd 			ttloop();
   1079   1.1       cgd 	}
   1080   1.1       cgd 	/*
   1081   1.1       cgd 	 * On the off chance that the telnet client is broken and does not
   1082   1.1       cgd 	 * respond to the DO ECHO we sent, (after all, we did send the
   1083   1.1       cgd 	 * DO NAWS negotiation after the DO ECHO, and we won't get here
   1084   1.1       cgd 	 * until a response to the DO NAWS comes back) simulate the
   1085   1.1       cgd 	 * receipt of a will echo.  This will also send a WONT ECHO
   1086   1.1       cgd 	 * to the client, since we assume that the client failed to
   1087   1.1       cgd 	 * respond because it believes that it is already in DO ECHO
   1088   1.1       cgd 	 * mode, which we do not want.
   1089   1.1       cgd 	 */
   1090   1.1       cgd 	if (his_want_state_is_will(TELOPT_ECHO)) {
   1091   1.1       cgd 		DIAG(TD_OPTIONS,
   1092  1.26    itojun 			{output_data("td: simulating recv\r\n");});
   1093   1.1       cgd 		willoption(TELOPT_ECHO);
   1094   1.1       cgd 	}
   1095   1.1       cgd 
   1096   1.1       cgd 	/*
   1097   1.1       cgd 	 * Finally, to clean things up, we turn on our echo.  This
   1098   1.1       cgd 	 * will break stupid 4.2 telnets out of local terminal echo.
   1099   1.1       cgd 	 */
   1100   1.1       cgd 
   1101   1.1       cgd 	if (my_state_is_wont(TELOPT_ECHO))
   1102   1.1       cgd 		send_will(TELOPT_ECHO, 1);
   1103   1.1       cgd 
   1104   1.3       cgd #ifndef	STREAMSPTY
   1105   1.1       cgd 	/*
   1106   1.1       cgd 	 * Turn on packet mode
   1107   1.1       cgd 	 */
   1108   1.1       cgd 	(void) ioctl(p, TIOCPKT, (char *)&on);
   1109   1.3       cgd #endif
   1110   1.3       cgd 
   1111   1.1       cgd #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
   1112   1.1       cgd 	/*
   1113   1.1       cgd 	 * Continuing line mode support.  If client does not support
   1114   1.1       cgd 	 * real linemode, attempt to negotiate kludge linemode by sending
   1115   1.1       cgd 	 * the do timing mark sequence.
   1116   1.1       cgd 	 */
   1117   1.1       cgd 	if (lmodetype < REAL_LINEMODE)
   1118   1.1       cgd 		send_do(TELOPT_TM, 1);
   1119   1.1       cgd #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
   1120   1.1       cgd 
   1121   1.1       cgd 	/*
   1122   1.1       cgd 	 * Call telrcv() once to pick up anything received during
   1123   1.1       cgd 	 * terminal type negotiation, 4.2/4.3 determination, and
   1124   1.1       cgd 	 * linemode negotiation.
   1125   1.1       cgd 	 */
   1126   1.1       cgd 	telrcv();
   1127   1.1       cgd 
   1128   1.1       cgd 	(void) ioctl(f, FIONBIO, (char *)&on);
   1129   1.1       cgd 	(void) ioctl(p, FIONBIO, (char *)&on);
   1130   1.1       cgd 
   1131   1.1       cgd #if	defined(SO_OOBINLINE)
   1132   1.3       cgd 	(void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE,
   1133   1.3       cgd 				(char *)&on, sizeof on);
   1134   1.1       cgd #endif	/* defined(SO_OOBINLINE) */
   1135   1.1       cgd 
   1136   1.1       cgd #ifdef	SIGTSTP
   1137   1.1       cgd 	(void) signal(SIGTSTP, SIG_IGN);
   1138   1.1       cgd #endif
   1139   1.1       cgd #ifdef	SIGTTOU
   1140   1.1       cgd 	/*
   1141   1.1       cgd 	 * Ignoring SIGTTOU keeps the kernel from blocking us
   1142   1.1       cgd 	 * in ttioct() in /sys/tty.c.
   1143   1.1       cgd 	 */
   1144   1.1       cgd 	(void) signal(SIGTTOU, SIG_IGN);
   1145   1.1       cgd #endif
   1146   1.1       cgd 
   1147   1.1       cgd 	(void) signal(SIGCHLD, cleanup);
   1148   1.1       cgd 
   1149   1.1       cgd 
   1150   1.1       cgd #ifdef  TIOCNOTTY
   1151   1.1       cgd 	{
   1152   1.1       cgd 		register int t;
   1153   1.1       cgd 		t = open(_PATH_TTY, O_RDWR);
   1154   1.1       cgd 		if (t >= 0) {
   1155   1.1       cgd 			(void) ioctl(t, TIOCNOTTY, (char *)0);
   1156   1.1       cgd 			(void) close(t);
   1157   1.1       cgd 		}
   1158   1.1       cgd 	}
   1159   1.1       cgd #endif
   1160   1.1       cgd 
   1161   1.1       cgd 
   1162   1.1       cgd 	/*
   1163   1.1       cgd 	 * Show banner that getty never gave.
   1164   1.1       cgd 	 *
   1165   1.1       cgd 	 * We put the banner in the pty input buffer.  This way, it
   1166   1.1       cgd 	 * gets carriage return null processing, etc., just like all
   1167   1.1       cgd 	 * other pty --> client data.
   1168   1.1       cgd 	 */
   1169   1.1       cgd 
   1170   1.1       cgd 	if (getenv("USER"))
   1171   1.1       cgd 		hostinfo = 0;
   1172   1.1       cgd 
   1173   1.6       tls 	if (getent(defent, gettyname) == 1) {
   1174   1.1       cgd 		char *cp=defstrs;
   1175   1.1       cgd 
   1176   1.1       cgd 		HE = getstr("he", &cp);
   1177   1.1       cgd 		HN = getstr("hn", &cp);
   1178   1.1       cgd 		IM = getstr("im", &cp);
   1179  1.20        ad 		IF = getstr("if", &cp);
   1180   1.1       cgd 		if (HN && *HN)
   1181  1.23  christos 			(void)strlcpy(host_name, HN, sizeof(host_name));
   1182   1.1       cgd 		if (IM == 0)
   1183   1.1       cgd 			IM = "";
   1184   1.1       cgd 	} else {
   1185   1.1       cgd 		IM = DEFAULT_IM;
   1186   1.1       cgd 		HE = 0;
   1187   1.1       cgd 	}
   1188   1.1       cgd 	edithost(HE, host_name);
   1189  1.20        ad 	ptyibuf2ptr = ptyibuf2;
   1190  1.20        ad 	if (hostinfo) {
   1191  1.20        ad 		if (IF)	{
   1192  1.20        ad 			char buf[_POSIX2_LINE_MAX];
   1193  1.20        ad 			FILE *fd;
   1194  1.20        ad 
   1195  1.20        ad 			if ((fd = fopen(IF, "r")) != NULL) {
   1196  1.20        ad 				while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
   1197  1.20        ad 					ptyibuf2ptr = putf(buf, ptyibuf2ptr);
   1198  1.20        ad 				fclose(fd);
   1199  1.20        ad 			}
   1200  1.20        ad 		}
   1201  1.20        ad 		if (*IM)
   1202  1.20        ad 			ptyibuf2ptr = putf(IM, ptyibuf2ptr);
   1203  1.20        ad 	}
   1204   1.1       cgd 
   1205   1.1       cgd 	if (pcc)
   1206  1.20        ad 		strncpy(ptyibuf2ptr, ptyip, pcc+1);
   1207   1.1       cgd 	ptyip = ptyibuf2;
   1208   1.1       cgd 	pcc = strlen(ptyip);
   1209   1.1       cgd #ifdef	LINEMODE
   1210   1.1       cgd 	/*
   1211   1.1       cgd 	 * Last check to make sure all our states are correct.
   1212   1.1       cgd 	 */
   1213   1.1       cgd 	init_termbuf();
   1214   1.1       cgd 	localstat();
   1215   1.1       cgd #endif	/* LINEMODE */
   1216   1.1       cgd 
   1217   1.1       cgd 	DIAG(TD_REPORT,
   1218  1.26    itojun 		{output_data("td: Entering processing loop\r\n");});
   1219   1.1       cgd 
   1220   1.1       cgd #ifdef	convex
   1221   1.1       cgd 	startslave(host);
   1222   1.1       cgd #endif
   1223   1.1       cgd 
   1224   1.4       jtk 	nfd = ((f > p) ? f : p) + 1;
   1225   1.1       cgd 	for (;;) {
   1226   1.1       cgd 		fd_set ibits, obits, xbits;
   1227   1.1       cgd 		register int c;
   1228   1.1       cgd 
   1229   1.1       cgd 		if (ncc < 0 && pcc < 0)
   1230   1.1       cgd 			break;
   1231   1.1       cgd 
   1232   1.1       cgd 		FD_ZERO(&ibits);
   1233   1.1       cgd 		FD_ZERO(&obits);
   1234   1.1       cgd 		FD_ZERO(&xbits);
   1235  1.26    itojun 
   1236  1.26    itojun 		if (f >= FD_SETSIZE)
   1237  1.26    itojun 			fatal(net, "fd too large");
   1238  1.26    itojun 		if (p >= FD_SETSIZE)
   1239  1.26    itojun 			fatal(net, "fd too large");
   1240  1.26    itojun 
   1241   1.1       cgd 		/*
   1242   1.1       cgd 		 * Never look for input if there's still
   1243   1.1       cgd 		 * stuff in the corresponding output buffer
   1244   1.1       cgd 		 */
   1245   1.1       cgd 		if (nfrontp - nbackp || pcc > 0) {
   1246   1.1       cgd 			FD_SET(f, &obits);
   1247   1.1       cgd 		} else {
   1248   1.1       cgd 			FD_SET(p, &ibits);
   1249   1.1       cgd 		}
   1250   1.1       cgd 		if (pfrontp - pbackp || ncc > 0) {
   1251   1.1       cgd 			FD_SET(p, &obits);
   1252   1.1       cgd 		} else {
   1253   1.1       cgd 			FD_SET(f, &ibits);
   1254   1.1       cgd 		}
   1255   1.1       cgd 		if (!SYNCHing) {
   1256   1.1       cgd 			FD_SET(f, &xbits);
   1257   1.1       cgd 		}
   1258   1.4       jtk 		if ((c = select(nfd, &ibits, &obits, &xbits,
   1259   1.1       cgd 						(struct timeval *)0)) < 1) {
   1260   1.1       cgd 			if (c == -1) {
   1261   1.1       cgd 				if (errno == EINTR) {
   1262   1.1       cgd 					continue;
   1263   1.1       cgd 				}
   1264   1.1       cgd 			}
   1265   1.1       cgd 			sleep(5);
   1266   1.1       cgd 			continue;
   1267   1.1       cgd 		}
   1268   1.1       cgd 
   1269   1.1       cgd 		/*
   1270   1.1       cgd 		 * Any urgent data?
   1271   1.1       cgd 		 */
   1272   1.1       cgd 		if (FD_ISSET(net, &xbits)) {
   1273   1.1       cgd 		    SYNCHing = 1;
   1274   1.1       cgd 		}
   1275   1.1       cgd 
   1276   1.1       cgd 		/*
   1277   1.1       cgd 		 * Something to read from the network...
   1278   1.1       cgd 		 */
   1279   1.1       cgd 		if (FD_ISSET(net, &ibits)) {
   1280   1.1       cgd #if	!defined(SO_OOBINLINE)
   1281   1.1       cgd 			/*
   1282   1.1       cgd 			 * In 4.2 (and 4.3 beta) systems, the
   1283   1.1       cgd 			 * OOB indication and data handling in the kernel
   1284   1.1       cgd 			 * is such that if two separate TCP Urgent requests
   1285   1.1       cgd 			 * come in, one byte of TCP data will be overlaid.
   1286   1.1       cgd 			 * This is fatal for Telnet, but we try to live
   1287   1.1       cgd 			 * with it.
   1288   1.1       cgd 			 *
   1289   1.1       cgd 			 * In addition, in 4.2 (and...), a special protocol
   1290   1.1       cgd 			 * is needed to pick up the TCP Urgent data in
   1291   1.1       cgd 			 * the correct sequence.
   1292   1.1       cgd 			 *
   1293   1.1       cgd 			 * What we do is:  if we think we are in urgent
   1294   1.1       cgd 			 * mode, we look to see if we are "at the mark".
   1295   1.1       cgd 			 * If we are, we do an OOB receive.  If we run
   1296   1.1       cgd 			 * this twice, we will do the OOB receive twice,
   1297   1.1       cgd 			 * but the second will fail, since the second
   1298   1.1       cgd 			 * time we were "at the mark", but there wasn't
   1299   1.1       cgd 			 * any data there (the kernel doesn't reset
   1300   1.1       cgd 			 * "at the mark" until we do a normal read).
   1301   1.1       cgd 			 * Once we've read the OOB data, we go ahead
   1302   1.1       cgd 			 * and do normal reads.
   1303   1.1       cgd 			 *
   1304   1.1       cgd 			 * There is also another problem, which is that
   1305   1.1       cgd 			 * since the OOB byte we read doesn't put us
   1306   1.1       cgd 			 * out of OOB state, and since that byte is most
   1307   1.1       cgd 			 * likely the TELNET DM (data mark), we would
   1308   1.1       cgd 			 * stay in the TELNET SYNCH (SYNCHing) state.
   1309   1.1       cgd 			 * So, clocks to the rescue.  If we've "just"
   1310   1.1       cgd 			 * received a DM, then we test for the
   1311   1.1       cgd 			 * presence of OOB data when the receive OOB
   1312   1.1       cgd 			 * fails (and AFTER we did the normal mode read
   1313   1.1       cgd 			 * to clear "at the mark").
   1314   1.1       cgd 			 */
   1315   1.1       cgd 		    if (SYNCHing) {
   1316   1.1       cgd 			int atmark;
   1317   1.1       cgd 
   1318   1.1       cgd 			(void) ioctl(net, SIOCATMARK, (char *)&atmark);
   1319   1.1       cgd 			if (atmark) {
   1320   1.1       cgd 			    ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB);
   1321   1.1       cgd 			    if ((ncc == -1) && (errno == EINVAL)) {
   1322   1.1       cgd 				ncc = read(net, netibuf, sizeof (netibuf));
   1323   1.1       cgd 				if (sequenceIs(didnetreceive, gotDM)) {
   1324   1.1       cgd 				    SYNCHing = stilloob(net);
   1325   1.1       cgd 				}
   1326   1.1       cgd 			    }
   1327   1.1       cgd 			} else {
   1328   1.1       cgd 			    ncc = read(net, netibuf, sizeof (netibuf));
   1329   1.1       cgd 			}
   1330   1.1       cgd 		    } else {
   1331   1.1       cgd 			ncc = read(net, netibuf, sizeof (netibuf));
   1332   1.1       cgd 		    }
   1333   1.1       cgd 		    settimer(didnetreceive);
   1334   1.1       cgd #else	/* !defined(SO_OOBINLINE)) */
   1335   1.1       cgd 		    ncc = read(net, netibuf, sizeof (netibuf));
   1336   1.1       cgd #endif	/* !defined(SO_OOBINLINE)) */
   1337   1.1       cgd 		    if (ncc < 0 && errno == EWOULDBLOCK)
   1338   1.1       cgd 			ncc = 0;
   1339   1.1       cgd 		    else {
   1340   1.1       cgd 			if (ncc <= 0) {
   1341   1.1       cgd 			    break;
   1342   1.1       cgd 			}
   1343   1.1       cgd 			netip = netibuf;
   1344   1.1       cgd 		    }
   1345   1.1       cgd 		    DIAG((TD_REPORT | TD_NETDATA),
   1346  1.26    itojun 			    {output_data("td: netread %d chars\r\n", ncc);});
   1347   1.1       cgd 		    DIAG(TD_NETDATA, printdata("nd", netip, ncc));
   1348   1.1       cgd 		}
   1349   1.1       cgd 
   1350   1.1       cgd 		/*
   1351   1.1       cgd 		 * Something to read from the pty...
   1352   1.1       cgd 		 */
   1353   1.1       cgd 		if (FD_ISSET(p, &ibits)) {
   1354   1.3       cgd #ifndef	STREAMSPTY
   1355   1.1       cgd 			pcc = read(p, ptyibuf, BUFSIZ);
   1356   1.3       cgd #else
   1357   1.3       cgd 			pcc = readstream(p, ptyibuf, BUFSIZ);
   1358   1.3       cgd #endif
   1359   1.1       cgd 			/*
   1360   1.1       cgd 			 * On some systems, if we try to read something
   1361   1.1       cgd 			 * off the master side before the slave side is
   1362   1.1       cgd 			 * opened, we get EIO.
   1363   1.1       cgd 			 */
   1364   1.3       cgd 			if (pcc < 0 && (errno == EWOULDBLOCK ||
   1365   1.3       cgd #ifdef	EAGAIN
   1366   1.3       cgd 					errno == EAGAIN ||
   1367   1.3       cgd #endif
   1368   1.3       cgd 					errno == EIO)) {
   1369   1.1       cgd 				pcc = 0;
   1370   1.1       cgd 			} else {
   1371   1.1       cgd 				if (pcc <= 0)
   1372   1.1       cgd 					break;
   1373   1.1       cgd #ifdef	LINEMODE
   1374   1.1       cgd 				/*
   1375   1.1       cgd 				 * If ioctl from pty, pass it through net
   1376   1.1       cgd 				 */
   1377   1.1       cgd 				if (ptyibuf[0] & TIOCPKT_IOCTL) {
   1378   1.1       cgd 					copy_termbuf(ptyibuf+1, pcc-1);
   1379   1.1       cgd 					localstat();
   1380   1.1       cgd 					pcc = 1;
   1381   1.1       cgd 				}
   1382   1.1       cgd #endif	/* LINEMODE */
   1383   1.1       cgd 				if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
   1384   1.1       cgd 					netclear();	/* clear buffer back */
   1385   1.1       cgd #ifndef	NO_URGENT
   1386   1.1       cgd 					/*
   1387   1.1       cgd 					 * There are client telnets on some
   1388   1.1       cgd 					 * operating systems get screwed up
   1389   1.1       cgd 					 * royally if we send them urgent
   1390   1.1       cgd 					 * mode data.
   1391   1.1       cgd 					 */
   1392  1.26    itojun 					output_data("%c%c", IAC, DM);
   1393  1.26    itojun 					neturg = nfrontp - 1; /* off by one XXX */
   1394   1.4       jtk 					DIAG(TD_OPTIONS,
   1395   1.4       jtk 					    printoption("td: send IAC", DM));
   1396   1.4       jtk 
   1397   1.1       cgd #endif
   1398   1.1       cgd 				}
   1399   1.1       cgd 				if (his_state_is_will(TELOPT_LFLOW) &&
   1400   1.1       cgd 				    (ptyibuf[0] &
   1401   1.1       cgd 				     (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
   1402   1.3       cgd 					int newflow =
   1403   1.3       cgd 					    ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
   1404   1.3       cgd 					if (newflow != flowmode) {
   1405   1.3       cgd 						flowmode = newflow;
   1406  1.26    itojun 						(void) output_data(
   1407   1.3       cgd 							"%c%c%c%c%c%c",
   1408   1.3       cgd 							IAC, SB, TELOPT_LFLOW,
   1409   1.3       cgd 							flowmode ? LFLOW_ON
   1410   1.3       cgd 								 : LFLOW_OFF,
   1411   1.3       cgd 							IAC, SE);
   1412   1.4       jtk 						DIAG(TD_OPTIONS, printsub('>',
   1413  1.26    itojun 						    (unsigned char *)nfrontp - 4,
   1414   1.4       jtk 						    4););
   1415   1.3       cgd 					}
   1416   1.1       cgd 				}
   1417   1.1       cgd 				pcc--;
   1418   1.1       cgd 				ptyip = ptyibuf+1;
   1419   1.1       cgd 			}
   1420   1.1       cgd 		}
   1421   1.1       cgd 
   1422   1.1       cgd 		while (pcc > 0) {
   1423   1.1       cgd 			if ((&netobuf[BUFSIZ] - nfrontp) < 2)
   1424   1.1       cgd 				break;
   1425   1.1       cgd 			c = *ptyip++ & 0377, pcc--;
   1426   1.1       cgd 			if (c == IAC)
   1427  1.26    itojun 				output_data("%c", c);
   1428  1.26    itojun 			output_data("%c", c);
   1429   1.1       cgd 			if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
   1430   1.1       cgd 				if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
   1431  1.26    itojun 					output_data("%c", *ptyip++ & 0377);
   1432   1.1       cgd 					pcc--;
   1433   1.1       cgd 				} else
   1434  1.26    itojun 					output_datalen("\0", 1);
   1435   1.1       cgd 			}
   1436   1.1       cgd 		}
   1437   1.1       cgd 
   1438   1.1       cgd 		if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0)
   1439   1.1       cgd 			netflush();
   1440   1.1       cgd 		if (ncc > 0)
   1441   1.1       cgd 			telrcv();
   1442   1.1       cgd 		if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0)
   1443   1.1       cgd 			ptyflush();
   1444   1.1       cgd 	}
   1445   1.1       cgd 	cleanup(0);
   1446   1.1       cgd }  /* end of telnet */
   1447   1.4       jtk 
   1448   1.1       cgd #ifndef	TCSIG
   1449   1.1       cgd # ifdef	TIOCSIG
   1450   1.1       cgd #  define TCSIG TIOCSIG
   1451   1.1       cgd # endif
   1452   1.1       cgd #endif
   1453   1.3       cgd 
   1454   1.3       cgd #ifdef	STREAMSPTY
   1455   1.3       cgd 
   1456   1.3       cgd int flowison = -1;  /* current state of flow: -1 is unknown */
   1457   1.3       cgd 
   1458   1.3       cgd int readstream(p, ibuf, bufsize)
   1459   1.3       cgd 	int p;
   1460   1.3       cgd 	char *ibuf;
   1461   1.3       cgd 	int bufsize;
   1462   1.3       cgd {
   1463   1.3       cgd 	int flags = 0;
   1464   1.3       cgd 	int ret = 0;
   1465   1.3       cgd 	struct termios *tsp;
   1466   1.3       cgd 	struct termio *tp;
   1467   1.3       cgd 	struct iocblk *ip;
   1468   1.3       cgd 	char vstop, vstart;
   1469   1.3       cgd 	int ixon;
   1470   1.3       cgd 	int newflow;
   1471   1.3       cgd 
   1472   1.3       cgd 	strbufc.maxlen = BUFSIZ;
   1473   1.3       cgd 	strbufc.buf = (char *)ctlbuf;
   1474   1.3       cgd 	strbufd.maxlen = bufsize-1;
   1475   1.3       cgd 	strbufd.len = 0;
   1476   1.3       cgd 	strbufd.buf = ibuf+1;
   1477   1.3       cgd 	ibuf[0] = 0;
   1478   1.3       cgd 
   1479   1.3       cgd 	ret = getmsg(p, &strbufc, &strbufd, &flags);
   1480   1.3       cgd 	if (ret < 0)  /* error of some sort -- probably EAGAIN */
   1481   1.3       cgd 		return(-1);
   1482   1.3       cgd 
   1483   1.3       cgd 	if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) {
   1484   1.3       cgd 		/* data message */
   1485   1.3       cgd 		if (strbufd.len > 0) {			/* real data */
   1486   1.3       cgd 			return(strbufd.len + 1);	/* count header char */
   1487   1.3       cgd 		} else {
   1488   1.3       cgd 			/* nothing there */
   1489   1.3       cgd 			errno = EAGAIN;
   1490   1.3       cgd 			return(-1);
   1491   1.3       cgd 		}
   1492   1.3       cgd 	}
   1493   1.3       cgd 
   1494   1.3       cgd 	/*
   1495   1.3       cgd 	 * It's a control message.  Return 1, to look at the flag we set
   1496   1.3       cgd 	 */
   1497   1.3       cgd 
   1498   1.3       cgd 	switch (ctlbuf[0]) {
   1499   1.3       cgd 	case M_FLUSH:
   1500   1.3       cgd 		if (ibuf[1] & FLUSHW)
   1501   1.3       cgd 			ibuf[0] = TIOCPKT_FLUSHWRITE;
   1502   1.3       cgd 		return(1);
   1503   1.3       cgd 
   1504   1.3       cgd 	case M_IOCTL:
   1505   1.3       cgd 		ip = (struct iocblk *) (ibuf+1);
   1506   1.3       cgd 
   1507   1.3       cgd 		switch (ip->ioc_cmd) {
   1508   1.3       cgd 		case TCSETS:
   1509   1.3       cgd 		case TCSETSW:
   1510   1.3       cgd 		case TCSETSF:
   1511   1.3       cgd 			tsp = (struct termios *)
   1512   1.3       cgd 					(ibuf+1 + sizeof(struct iocblk));
   1513   1.3       cgd 			vstop = tsp->c_cc[VSTOP];
   1514   1.3       cgd 			vstart = tsp->c_cc[VSTART];
   1515   1.3       cgd 			ixon = tsp->c_iflag & IXON;
   1516   1.3       cgd 			break;
   1517   1.3       cgd 		case TCSETA:
   1518   1.3       cgd 		case TCSETAW:
   1519   1.3       cgd 		case TCSETAF:
   1520   1.3       cgd 			tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk));
   1521   1.3       cgd 			vstop = tp->c_cc[VSTOP];
   1522   1.3       cgd 			vstart = tp->c_cc[VSTART];
   1523   1.4       jtk 			ixon = tp->c_iflag & IXON;
   1524   1.3       cgd 			break;
   1525   1.3       cgd 		default:
   1526   1.3       cgd 			errno = EAGAIN;
   1527   1.3       cgd 			return(-1);
   1528   1.3       cgd 		}
   1529   1.3       cgd 
   1530   1.3       cgd 		newflow =  (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0;
   1531   1.3       cgd 		if (newflow != flowison) {  /* it's a change */
   1532   1.3       cgd 			flowison = newflow;
   1533   1.3       cgd 			ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP;
   1534   1.3       cgd 			return(1);
   1535   1.3       cgd 		}
   1536   1.3       cgd 	}
   1537   1.3       cgd 
   1538   1.3       cgd 	/* nothing worth doing anything about */
   1539   1.3       cgd 	errno = EAGAIN;
   1540   1.3       cgd 	return(-1);
   1541   1.3       cgd }
   1542   1.3       cgd #endif /* STREAMSPTY */
   1543   1.1       cgd 
   1544   1.1       cgd /*
   1545   1.1       cgd  * Send interrupt to process on other side of pty.
   1546   1.1       cgd  * If it is in raw mode, just write NULL;
   1547   1.1       cgd  * otherwise, write intr char.
   1548   1.1       cgd  */
   1549   1.1       cgd 	void
   1550   1.1       cgd interrupt()
   1551   1.1       cgd {
   1552   1.1       cgd 	ptyflush();	/* half-hearted */
   1553   1.1       cgd 
   1554   1.4       jtk #if defined(STREAMSPTY) && defined(TIOCSIGNAL)
   1555   1.4       jtk 	/* Streams PTY style ioctl to post a signal */
   1556   1.4       jtk 	{
   1557   1.4       jtk 		int sig = SIGINT;
   1558   1.4       jtk 		(void) ioctl(pty, TIOCSIGNAL, &sig);
   1559   1.4       jtk 		(void) ioctl(pty, I_FLUSH, FLUSHR);
   1560   1.4       jtk 	}
   1561   1.4       jtk #else
   1562   1.1       cgd #ifdef	TCSIG
   1563   1.1       cgd 	(void) ioctl(pty, TCSIG, (char *)SIGINT);
   1564   1.1       cgd #else	/* TCSIG */
   1565   1.1       cgd 	init_termbuf();
   1566   1.1       cgd 	*pfrontp++ = slctab[SLC_IP].sptr ?
   1567   1.1       cgd 			(unsigned char)*slctab[SLC_IP].sptr : '\177';
   1568   1.1       cgd #endif	/* TCSIG */
   1569   1.4       jtk #endif
   1570   1.1       cgd }
   1571   1.1       cgd 
   1572   1.1       cgd /*
   1573   1.1       cgd  * Send quit to process on other side of pty.
   1574   1.1       cgd  * If it is in raw mode, just write NULL;
   1575   1.1       cgd  * otherwise, write quit char.
   1576   1.1       cgd  */
   1577   1.1       cgd 	void
   1578   1.1       cgd sendbrk()
   1579   1.1       cgd {
   1580   1.1       cgd 	ptyflush();	/* half-hearted */
   1581   1.1       cgd #ifdef	TCSIG
   1582   1.1       cgd 	(void) ioctl(pty, TCSIG, (char *)SIGQUIT);
   1583   1.1       cgd #else	/* TCSIG */
   1584   1.1       cgd 	init_termbuf();
   1585   1.1       cgd 	*pfrontp++ = slctab[SLC_ABORT].sptr ?
   1586   1.1       cgd 			(unsigned char)*slctab[SLC_ABORT].sptr : '\034';
   1587   1.1       cgd #endif	/* TCSIG */
   1588   1.1       cgd }
   1589   1.1       cgd 
   1590   1.1       cgd 	void
   1591   1.1       cgd sendsusp()
   1592   1.1       cgd {
   1593   1.1       cgd #ifdef	SIGTSTP
   1594   1.1       cgd 	ptyflush();	/* half-hearted */
   1595   1.1       cgd # ifdef	TCSIG
   1596   1.1       cgd 	(void) ioctl(pty, TCSIG, (char *)SIGTSTP);
   1597   1.1       cgd # else	/* TCSIG */
   1598   1.1       cgd 	*pfrontp++ = slctab[SLC_SUSP].sptr ?
   1599   1.1       cgd 			(unsigned char)*slctab[SLC_SUSP].sptr : '\032';
   1600   1.1       cgd # endif	/* TCSIG */
   1601   1.1       cgd #endif	/* SIGTSTP */
   1602   1.1       cgd }
   1603   1.1       cgd 
   1604   1.1       cgd /*
   1605   1.1       cgd  * When we get an AYT, if ^T is enabled, use that.  Otherwise,
   1606   1.1       cgd  * just send back "[Yes]".
   1607   1.1       cgd  */
   1608   1.1       cgd 	void
   1609   1.1       cgd recv_ayt()
   1610   1.1       cgd {
   1611   1.1       cgd #if	defined(SIGINFO) && defined(TCSIG)
   1612   1.1       cgd 	if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
   1613   1.1       cgd 		(void) ioctl(pty, TCSIG, (char *)SIGINFO);
   1614   1.1       cgd 		return;
   1615   1.1       cgd 	}
   1616   1.1       cgd #endif
   1617  1.26    itojun 	(void) output_data("\r\n[Yes]\r\n");
   1618   1.1       cgd }
   1619   1.1       cgd 
   1620   1.1       cgd 	void
   1621   1.1       cgd doeof()
   1622   1.1       cgd {
   1623   1.1       cgd 	init_termbuf();
   1624   1.1       cgd 
   1625   1.1       cgd #if	defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN)
   1626   1.1       cgd 	if (!tty_isediting()) {
   1627   1.1       cgd 		extern char oldeofc;
   1628   1.1       cgd 		*pfrontp++ = oldeofc;
   1629   1.1       cgd 		return;
   1630   1.1       cgd 	}
   1631   1.1       cgd #endif
   1632   1.1       cgd 	*pfrontp++ = slctab[SLC_EOF].sptr ?
   1633   1.1       cgd 			(unsigned char)*slctab[SLC_EOF].sptr : '\004';
   1634   1.1       cgd }
   1635