Home | History | Annotate | Line # | Download | only in telnetd
telnetd.c revision 1.41
      1  1.41    itojun /*	$NetBSD: telnetd.c,v 1.41 2003/07/15 10:14:54 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.41    itojun __RCSID("$NetBSD: telnetd.c,v 1.41 2003/07/15 10:14:54 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.39    itojun #ifdef KRB5
     87  1.21   thorpej #define	Authenticator	k5_Authenticator
     88  1.21   thorpej #include <krb5.h>
     89  1.21   thorpej #undef	Authenticator
     90  1.21   thorpej #include <com_err.h>
     91  1.21   thorpej #endif
     92  1.21   thorpej 
     93  1.39    itojun #ifdef AUTHENTICATION
     94   1.1       cgd int	auth_level = 0;
     95   1.1       cgd #endif
     96  1.21   thorpej 
     97  1.21   thorpej #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
     98  1.21   thorpej #include <libtelnet/misc.h>
     99  1.21   thorpej #endif
    100  1.21   thorpej 
    101  1.39    itojun #ifdef SECURELOGIN
    102  1.15      dean int	require_secure_login = 0;
    103   1.1       cgd #endif
    104   1.1       cgd 
    105  1.25  christos extern int require_hwpreauth;
    106  1.25  christos #ifdef KRB5
    107  1.25  christos extern krb5_context telnet_context;
    108  1.25  christos #endif
    109   1.3       cgd int	registerd_host_only = 0;
    110   1.3       cgd 
    111   1.3       cgd 
    112   1.1       cgd /*
    113   1.1       cgd  * I/O data buffers,
    114   1.1       cgd  * pointers, and counters.
    115   1.1       cgd  */
    116   1.1       cgd char	ptyibuf[BUFSIZ], *ptyip = ptyibuf;
    117   1.1       cgd char	ptyibuf2[BUFSIZ];
    118   1.1       cgd 
    119   1.3       cgd 
    120   1.1       cgd int	hostinfo = 1;			/* do we print login banner? */
    121   1.1       cgd 
    122   1.1       cgd 
    123   1.1       cgd int debug = 0;
    124   1.1       cgd int keepalive = 1;
    125   1.6       tls char *gettyname = "default";
    126   1.1       cgd char *progname;
    127   1.1       cgd 
    128   1.9       mrg int main __P((int, char *[]));
    129   1.9       mrg void usage __P((void));
    130  1.41    itojun int getterminaltype __P((char *, size_t));
    131   1.9       mrg int getent __P((char *, char *));
    132  1.16    itojun void doit __P((struct sockaddr *));
    133   1.9       mrg void _gettermname __P((void));
    134   1.9       mrg int terminaltypeok __P((char *));
    135  1.26    itojun char *getstr __P((const char *, char **));
    136   1.1       cgd 
    137   1.3       cgd /*
    138   1.3       cgd  * The string to pass to getopt().  We do it this way so
    139   1.3       cgd  * that only the actual options that we support will be
    140   1.3       cgd  * passed off to getopt().
    141   1.3       cgd  */
    142   1.3       cgd char valid_opts[] = {
    143   1.6       tls 	'd', ':', 'g', ':', 'h', 'k', 'n', 'S', ':', 'u', ':', 'U',
    144  1.16    itojun 	'4', '6',
    145   1.3       cgd #ifdef	AUTHENTICATION
    146   1.3       cgd 	'a', ':', 'X', ':',
    147   1.3       cgd #endif
    148  1.21   thorpej #ifdef	ENCRYPTION
    149  1.21   thorpej 	'e', ':',
    150  1.21   thorpej #endif
    151   1.3       cgd #ifdef DIAGNOSTICS
    152   1.3       cgd 	'D', ':',
    153   1.3       cgd #endif
    154   1.3       cgd #ifdef	LINEMODE
    155   1.3       cgd 	'l',
    156   1.3       cgd #endif
    157  1.15      dean #ifdef	SECURELOGIN
    158   1.3       cgd 	's',
    159   1.3       cgd #endif
    160  1.21   thorpej #ifdef	KRB5
    161  1.21   thorpej 	'R', ':', 'H',
    162  1.21   thorpej #endif
    163   1.3       cgd 	'\0'
    164   1.3       cgd };
    165   1.3       cgd 
    166  1.16    itojun int family = AF_INET;
    167  1.16    itojun 
    168   1.9       mrg int
    169   1.1       cgd main(argc, argv)
    170  1.14   msaitoh 	int argc;
    171   1.1       cgd 	char *argv[];
    172   1.1       cgd {
    173  1.16    itojun 	struct sockaddr_storage from;
    174   1.1       cgd 	int on = 1, fromlen;
    175   1.1       cgd 	register int ch;
    176   1.1       cgd #if	defined(IPPROTO_IP) && defined(IP_TOS)
    177   1.1       cgd 	int tos = -1;
    178   1.1       cgd #endif
    179   1.1       cgd 
    180   1.1       cgd 	pfrontp = pbackp = ptyobuf;
    181   1.1       cgd 	netip = netibuf;
    182   1.1       cgd 	nfrontp = nbackp = netobuf;
    183  1.21   thorpej #ifdef	ENCRYPTION
    184  1.21   thorpej 	nclearto = 0;
    185  1.21   thorpej #endif	/* ENCRYPTION */
    186   1.1       cgd 
    187   1.1       cgd 	progname = *argv;
    188   1.1       cgd 
    189   1.1       cgd 
    190   1.7     enami 	while ((ch = getopt(argc, argv, valid_opts)) != -1) {
    191   1.8     enami 		switch (ch) {
    192   1.1       cgd 
    193   1.3       cgd #ifdef	AUTHENTICATION
    194   1.1       cgd 		case 'a':
    195   1.1       cgd 			/*
    196   1.1       cgd 			 * Check for required authentication level
    197   1.1       cgd 			 */
    198   1.1       cgd 			if (strcmp(optarg, "debug") == 0) {
    199   1.1       cgd 				auth_debug_mode = 1;
    200   1.1       cgd 			} else if (strcasecmp(optarg, "none") == 0) {
    201   1.1       cgd 				auth_level = 0;
    202   1.1       cgd 			} else if (strcasecmp(optarg, "other") == 0) {
    203   1.1       cgd 				auth_level = AUTH_OTHER;
    204   1.1       cgd 			} else if (strcasecmp(optarg, "user") == 0) {
    205   1.1       cgd 				auth_level = AUTH_USER;
    206   1.1       cgd 			} else if (strcasecmp(optarg, "valid") == 0) {
    207   1.1       cgd 				auth_level = AUTH_VALID;
    208   1.1       cgd 			} else if (strcasecmp(optarg, "off") == 0) {
    209   1.1       cgd 				/*
    210   1.1       cgd 				 * This hack turns off authentication
    211   1.1       cgd 				 */
    212   1.1       cgd 				auth_level = -1;
    213   1.1       cgd 			} else {
    214   1.1       cgd 				fprintf(stderr,
    215   1.1       cgd 			    "telnetd: unknown authorization level for -a\n");
    216   1.1       cgd 			}
    217   1.1       cgd 			break;
    218   1.3       cgd #endif	/* AUTHENTICATION */
    219   1.1       cgd 
    220   1.1       cgd 
    221   1.1       cgd 		case 'd':
    222   1.1       cgd 			if (strcmp(optarg, "ebug") == 0) {
    223   1.1       cgd 				debug++;
    224   1.1       cgd 				break;
    225   1.1       cgd 			}
    226   1.1       cgd 			usage();
    227   1.1       cgd 			/* NOTREACHED */
    228   1.1       cgd 			break;
    229   1.1       cgd 
    230   1.1       cgd #ifdef DIAGNOSTICS
    231   1.1       cgd 		case 'D':
    232   1.1       cgd 			/*
    233   1.1       cgd 			 * Check for desired diagnostics capabilities.
    234   1.1       cgd 			 */
    235   1.1       cgd 			if (!strcmp(optarg, "report")) {
    236   1.1       cgd 				diagnostic |= TD_REPORT|TD_OPTIONS;
    237   1.1       cgd 			} else if (!strcmp(optarg, "exercise")) {
    238   1.1       cgd 				diagnostic |= TD_EXERCISE;
    239   1.1       cgd 			} else if (!strcmp(optarg, "netdata")) {
    240   1.1       cgd 				diagnostic |= TD_NETDATA;
    241   1.1       cgd 			} else if (!strcmp(optarg, "ptydata")) {
    242   1.1       cgd 				diagnostic |= TD_PTYDATA;
    243   1.1       cgd 			} else if (!strcmp(optarg, "options")) {
    244   1.1       cgd 				diagnostic |= TD_OPTIONS;
    245   1.1       cgd 			} else {
    246   1.1       cgd 				usage();
    247   1.1       cgd 				/* NOT REACHED */
    248   1.1       cgd 			}
    249   1.1       cgd 			break;
    250   1.1       cgd #endif /* DIAGNOSTICS */
    251   1.1       cgd 
    252  1.21   thorpej #ifdef	ENCRYPTION
    253  1.21   thorpej 		case 'e':
    254  1.21   thorpej 			if (strcmp(optarg, "debug") == 0) {
    255  1.21   thorpej 				encrypt_debug_mode = 1;
    256  1.21   thorpej 				break;
    257  1.21   thorpej 			}
    258  1.21   thorpej 			usage();
    259  1.21   thorpej 			/* NOTREACHED */
    260  1.21   thorpej 			break;
    261  1.21   thorpej #endif	/* ENCRYPTION */
    262  1.21   thorpej 
    263   1.6       tls 		case 'g':
    264   1.6       tls 			gettyname = optarg;
    265   1.6       tls 			break;
    266   1.1       cgd 
    267   1.1       cgd 		case 'h':
    268   1.1       cgd 			hostinfo = 0;
    269   1.1       cgd 			break;
    270   1.1       cgd 
    271  1.21   thorpej #ifdef	KRB5
    272  1.21   thorpej 		case 'H':
    273  1.21   thorpej 		    {
    274  1.21   thorpej 			require_hwpreauth = 1;
    275  1.21   thorpej 			break;
    276  1.21   thorpej 		    }
    277  1.21   thorpej #endif	/* KRB5 */
    278  1.21   thorpej 
    279   1.1       cgd 
    280   1.1       cgd #ifdef	LINEMODE
    281   1.1       cgd 		case 'l':
    282   1.1       cgd 			alwayslinemode = 1;
    283   1.1       cgd 			break;
    284   1.1       cgd #endif	/* LINEMODE */
    285   1.1       cgd 
    286   1.3       cgd 		case 'k':
    287   1.3       cgd #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    288   1.3       cgd 			lmodetype = NO_AUTOKLUDGE;
    289   1.3       cgd #else
    290   1.3       cgd 			/* ignore -k option if built without kludge linemode */
    291   1.3       cgd #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    292   1.3       cgd 			break;
    293   1.3       cgd 
    294   1.1       cgd 		case 'n':
    295   1.1       cgd 			keepalive = 0;
    296   1.1       cgd 			break;
    297   1.1       cgd 
    298   1.1       cgd 
    299  1.21   thorpej #ifdef	KRB5
    300  1.21   thorpej 		case 'R':
    301  1.21   thorpej 		    {
    302  1.21   thorpej 			krb5_error_code retval;
    303  1.21   thorpej 
    304  1.21   thorpej 			if (telnet_context == 0) {
    305  1.21   thorpej 				retval = krb5_init_context(&telnet_context);
    306  1.21   thorpej 				if (retval) {
    307  1.21   thorpej 					com_err("telnetd", retval,
    308  1.21   thorpej 					    "while initializing krb5");
    309  1.21   thorpej 					exit(1);
    310  1.21   thorpej 				}
    311  1.21   thorpej 			}
    312  1.21   thorpej 			krb5_set_default_realm(telnet_context, optarg);
    313  1.21   thorpej 			break;
    314  1.21   thorpej 		    }
    315  1.21   thorpej #endif	/* KRB5 */
    316  1.21   thorpej 
    317  1.15      dean #ifdef	SECURELOGIN
    318   1.1       cgd 		case 's':
    319  1.15      dean 			/* Secure login required */
    320  1.15      dean 			require_secure_login = 1;
    321   1.1       cgd 			break;
    322  1.15      dean #endif	/* SECURELOGIN */
    323   1.1       cgd 		case 'S':
    324   1.1       cgd 			fprintf(stderr, "%s%s\n", "TOS option unavailable; ",
    325   1.1       cgd 						"-S flag not supported\n");
    326   1.1       cgd 			break;
    327   1.1       cgd 
    328   1.3       cgd 		case 'u':
    329  1.33  christos 			fprintf(stderr, "telnetd: -u option unneeded\n");
    330   1.3       cgd 			break;
    331   1.3       cgd 
    332   1.3       cgd 		case 'U':
    333   1.3       cgd 			registerd_host_only = 1;
    334   1.3       cgd 			break;
    335   1.3       cgd 
    336   1.3       cgd #ifdef	AUTHENTICATION
    337   1.1       cgd 		case 'X':
    338   1.1       cgd 			/*
    339   1.1       cgd 			 * Check for invalid authentication types
    340   1.1       cgd 			 */
    341   1.1       cgd 			auth_disable_name(optarg);
    342   1.1       cgd 			break;
    343   1.3       cgd #endif	/* AUTHENTICATION */
    344   1.1       cgd 
    345  1.16    itojun 		case '4':
    346  1.16    itojun 			family = AF_INET;
    347  1.16    itojun 			break;
    348  1.18   thorpej 
    349  1.16    itojun 		case '6':
    350  1.16    itojun 			family = AF_INET6;
    351  1.16    itojun 			break;
    352  1.18   thorpej 
    353   1.1       cgd 		default:
    354   1.3       cgd 			fprintf(stderr, "telnetd: %c: unknown option\n", ch);
    355   1.1       cgd 			/* FALLTHROUGH */
    356   1.1       cgd 		case '?':
    357   1.1       cgd 			usage();
    358   1.1       cgd 			/* NOTREACHED */
    359   1.1       cgd 		}
    360   1.1       cgd 	}
    361   1.1       cgd 
    362   1.1       cgd 	argc -= optind;
    363   1.1       cgd 	argv += optind;
    364   1.1       cgd 
    365   1.1       cgd 	if (debug) {
    366  1.16    itojun 	    int s, ns, foo, error;
    367  1.16    itojun 	    char *service = "telnet";
    368  1.16    itojun 	    struct addrinfo hints, *res;
    369   1.1       cgd 
    370   1.1       cgd 	    if (argc > 1) {
    371   1.1       cgd 		usage();
    372   1.1       cgd 		/* NOT REACHED */
    373  1.16    itojun 	    } else if (argc == 1)
    374  1.16    itojun 		service = *argv;
    375  1.16    itojun 
    376  1.16    itojun 	    memset(&hints, 0, sizeof(hints));
    377  1.16    itojun 	    hints.ai_flags = AI_PASSIVE;
    378  1.16    itojun 	    hints.ai_family = family;
    379  1.16    itojun 	    hints.ai_socktype = SOCK_STREAM;
    380  1.16    itojun 	    hints.ai_protocol = 0;
    381  1.16    itojun 	    error = getaddrinfo(NULL, service, &hints, &res);
    382  1.16    itojun 
    383  1.16    itojun 	    if (error) {
    384  1.22    itojun 		fprintf(stderr, "tcp/%s: %s\n", service, gai_strerror(error));
    385  1.22    itojun 		exit(1);
    386   1.1       cgd 	    }
    387   1.1       cgd 
    388  1.16    itojun 	    s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    389   1.1       cgd 	    if (s < 0) {
    390  1.28       wiz 		perror("telnetd: socket");
    391  1.22    itojun 		exit(1);
    392   1.1       cgd 	    }
    393   1.3       cgd 	    (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
    394   1.3       cgd 				(char *)&on, sizeof(on));
    395  1.16    itojun 	    if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
    396   1.1       cgd 		perror("bind");
    397   1.1       cgd 		exit(1);
    398   1.1       cgd 	    }
    399   1.1       cgd 	    if (listen(s, 1) < 0) {
    400   1.1       cgd 		perror("listen");
    401   1.1       cgd 		exit(1);
    402   1.1       cgd 	    }
    403  1.16    itojun 	    foo = res->ai_addrlen;
    404  1.16    itojun 	    ns = accept(s, res->ai_addr, &foo);
    405   1.1       cgd 	    if (ns < 0) {
    406   1.1       cgd 		perror("accept");
    407   1.1       cgd 		exit(1);
    408   1.1       cgd 	    }
    409   1.1       cgd 	    (void) dup2(ns, 0);
    410   1.1       cgd 	    (void) close(ns);
    411   1.1       cgd 	    (void) close(s);
    412   1.1       cgd 	} else if (argc > 0) {
    413   1.1       cgd 		usage();
    414   1.1       cgd 		/* NOT REACHED */
    415   1.1       cgd 	}
    416   1.1       cgd 
    417  1.24     lukem 	openlog("telnetd", LOG_PID, LOG_DAEMON);
    418   1.1       cgd 	fromlen = sizeof (from);
    419   1.1       cgd 	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
    420   1.1       cgd 		fprintf(stderr, "%s: ", progname);
    421   1.1       cgd 		perror("getpeername");
    422   1.1       cgd 		_exit(1);
    423   1.1       cgd 	}
    424   1.1       cgd 	if (keepalive &&
    425   1.3       cgd 	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE,
    426   1.3       cgd 			(char *)&on, sizeof (on)) < 0) {
    427   1.1       cgd 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
    428   1.1       cgd 	}
    429   1.1       cgd 
    430   1.1       cgd #if	defined(IPPROTO_IP) && defined(IP_TOS)
    431  1.19    itojun 	if (((struct sockaddr *)&from)->sa_family == AF_INET) {
    432   1.1       cgd 		if (tos < 0)
    433   1.1       cgd 			tos = 020;	/* Low Delay bit */
    434   1.1       cgd 		if (tos
    435   1.3       cgd 		   && (setsockopt(0, IPPROTO_IP, IP_TOS,
    436   1.3       cgd 				  (char *)&tos, sizeof(tos)) < 0)
    437   1.1       cgd 		   && (errno != ENOPROTOOPT) )
    438   1.1       cgd 			syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
    439   1.1       cgd 	}
    440   1.1       cgd #endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */
    441  1.16    itojun 
    442   1.1       cgd 	net = 0;
    443  1.16    itojun 	doit((struct sockaddr *)&from);
    444   1.1       cgd 	/* NOTREACHED */
    445   1.9       mrg #ifdef __GNUC__
    446   1.9       mrg 	exit(0);
    447   1.9       mrg #endif
    448   1.1       cgd }  /* end of main */
    449   1.1       cgd 
    450  1.39    itojun void
    451   1.1       cgd usage()
    452   1.1       cgd {
    453   1.1       cgd 	fprintf(stderr, "Usage: telnetd");
    454   1.3       cgd #ifdef	AUTHENTICATION
    455   1.3       cgd 	fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");
    456   1.1       cgd #endif
    457   1.1       cgd 	fprintf(stderr, " [-debug]");
    458   1.1       cgd #ifdef DIAGNOSTICS
    459   1.1       cgd 	fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");
    460   1.1       cgd #endif
    461  1.31       wiz #ifdef	ENCRYPTION
    462   1.1       cgd 	fprintf(stderr, " [-edebug]");
    463   1.1       cgd #endif
    464   1.1       cgd 	fprintf(stderr, " [-h]");
    465   1.3       cgd #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    466   1.3       cgd 	fprintf(stderr, " [-k]");
    467   1.3       cgd #endif
    468   1.1       cgd #ifdef LINEMODE
    469   1.1       cgd 	fprintf(stderr, " [-l]");
    470   1.1       cgd #endif
    471   1.1       cgd 	fprintf(stderr, " [-n]");
    472   1.3       cgd 	fprintf(stderr, "\n\t");
    473  1.15      dean #ifdef	SECURELOGIN
    474   1.1       cgd 	fprintf(stderr, " [-s]");
    475   1.1       cgd #endif
    476   1.3       cgd #ifdef	AUTHENTICATION
    477   1.1       cgd 	fprintf(stderr, " [-X auth-type]");
    478   1.1       cgd #endif
    479   1.3       cgd 	fprintf(stderr, " [-u utmp_hostname_length] [-U]");
    480   1.1       cgd 	fprintf(stderr, " [port]\n");
    481   1.1       cgd 	exit(1);
    482   1.1       cgd }
    483   1.1       cgd 
    484   1.1       cgd /*
    485   1.1       cgd  * getterminaltype
    486   1.1       cgd  *
    487   1.1       cgd  *	Ask the other end to send along its terminal type and speed.
    488   1.1       cgd  * Output is the variable terminaltype filled in.
    489   1.1       cgd  */
    490   1.3       cgd static unsigned char ttytype_sbbuf[] = {
    491   1.3       cgd 	IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE
    492   1.3       cgd };
    493   1.1       cgd 
    494  1.39    itojun int
    495  1.41    itojun getterminaltype(name, l)
    496   1.1       cgd     char *name;
    497  1.41    itojun     size_t l;
    498   1.1       cgd {
    499   1.1       cgd     int retval = -1;
    500   1.1       cgd 
    501   1.1       cgd     settimer(baseline);
    502  1.39    itojun #ifdef AUTHENTICATION
    503   1.1       cgd     /*
    504   1.1       cgd      * Handle the Authentication option before we do anything else.
    505   1.1       cgd      */
    506   1.1       cgd     send_do(TELOPT_AUTHENTICATION, 1);
    507   1.1       cgd     while (his_will_wont_is_changing(TELOPT_AUTHENTICATION))
    508   1.1       cgd 	ttloop();
    509   1.1       cgd     if (his_state_is_will(TELOPT_AUTHENTICATION)) {
    510  1.41    itojun 	retval = auth_wait(name, l);
    511   1.1       cgd     }
    512   1.1       cgd #endif
    513   1.1       cgd 
    514  1.21   thorpej #ifdef	ENCRYPTION
    515  1.21   thorpej     send_will(TELOPT_ENCRYPT, 1);
    516  1.21   thorpej #endif	/* ENCRYPTION */
    517   1.1       cgd     send_do(TELOPT_TTYPE, 1);
    518   1.1       cgd     send_do(TELOPT_TSPEED, 1);
    519   1.1       cgd     send_do(TELOPT_XDISPLOC, 1);
    520   1.3       cgd     send_do(TELOPT_NEW_ENVIRON, 1);
    521   1.3       cgd     send_do(TELOPT_OLD_ENVIRON, 1);
    522   1.1       cgd     while (
    523  1.21   thorpej #ifdef	ENCRYPTION
    524  1.21   thorpej 	   his_do_dont_is_changing(TELOPT_ENCRYPT) ||
    525  1.21   thorpej #endif	/* ENCRYPTION */
    526   1.1       cgd 	   his_will_wont_is_changing(TELOPT_TTYPE) ||
    527   1.1       cgd 	   his_will_wont_is_changing(TELOPT_TSPEED) ||
    528   1.1       cgd 	   his_will_wont_is_changing(TELOPT_XDISPLOC) ||
    529   1.3       cgd 	   his_will_wont_is_changing(TELOPT_NEW_ENVIRON) ||
    530   1.3       cgd 	   his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) {
    531   1.1       cgd 	ttloop();
    532   1.1       cgd     }
    533   1.1       cgd     if (his_state_is_will(TELOPT_TSPEED)) {
    534   1.3       cgd 	static unsigned char sb[] =
    535   1.3       cgd 			{ IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };
    536   1.1       cgd 
    537  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    538   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    539   1.1       cgd     }
    540  1.21   thorpej #ifdef	ENCRYPTION
    541  1.21   thorpej     /*
    542  1.21   thorpej      * Wait for the negotiation of what type of encryption we can
    543  1.21   thorpej      * send with.  If autoencrypt is not set, this will just return.
    544  1.21   thorpej      */
    545  1.21   thorpej     if (his_state_is_will(TELOPT_ENCRYPT)) {
    546  1.21   thorpej 	encrypt_wait();
    547  1.21   thorpej     }
    548  1.21   thorpej #endif	/* ENCRYPTION */
    549   1.1       cgd     if (his_state_is_will(TELOPT_XDISPLOC)) {
    550   1.3       cgd 	static unsigned char sb[] =
    551   1.3       cgd 			{ IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };
    552   1.3       cgd 
    553  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    554   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    555   1.3       cgd     }
    556   1.3       cgd     if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
    557   1.3       cgd 	static unsigned char sb[] =
    558   1.3       cgd 			{ IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };
    559   1.1       cgd 
    560  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    561   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    562   1.1       cgd     }
    563   1.3       cgd     else if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
    564   1.3       cgd 	static unsigned char sb[] =
    565   1.3       cgd 			{ IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };
    566   1.1       cgd 
    567  1.27       wiz 	output_datalen((const char *)sb, sizeof sb);
    568   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2););
    569   1.1       cgd     }
    570   1.1       cgd     if (his_state_is_will(TELOPT_TTYPE)) {
    571   1.1       cgd 
    572  1.27       wiz 	output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
    573   1.4       jtk 	DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
    574   1.4       jtk 					sizeof ttytype_sbbuf - 2););
    575   1.1       cgd     }
    576   1.1       cgd     if (his_state_is_will(TELOPT_TSPEED)) {
    577   1.1       cgd 	while (sequenceIs(tspeedsubopt, baseline))
    578   1.1       cgd 	    ttloop();
    579   1.1       cgd     }
    580   1.1       cgd     if (his_state_is_will(TELOPT_XDISPLOC)) {
    581   1.1       cgd 	while (sequenceIs(xdisplocsubopt, baseline))
    582   1.1       cgd 	    ttloop();
    583   1.1       cgd     }
    584   1.3       cgd     if (his_state_is_will(TELOPT_NEW_ENVIRON)) {
    585   1.1       cgd 	while (sequenceIs(environsubopt, baseline))
    586   1.1       cgd 	    ttloop();
    587   1.1       cgd     }
    588   1.3       cgd     if (his_state_is_will(TELOPT_OLD_ENVIRON)) {
    589   1.3       cgd 	while (sequenceIs(oenvironsubopt, baseline))
    590   1.3       cgd 	    ttloop();
    591   1.3       cgd     }
    592   1.1       cgd     if (his_state_is_will(TELOPT_TTYPE)) {
    593   1.1       cgd 	char first[256], last[256];
    594   1.1       cgd 
    595   1.1       cgd 	while (sequenceIs(ttypesubopt, baseline))
    596   1.1       cgd 	    ttloop();
    597   1.1       cgd 
    598   1.1       cgd 	/*
    599   1.1       cgd 	 * If the other side has already disabled the option, then
    600   1.1       cgd 	 * we have to just go with what we (might) have already gotten.
    601   1.1       cgd 	 */
    602   1.1       cgd 	if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) {
    603  1.37    itojun 	    (void) strlcpy(first, terminaltype, sizeof(first));
    604   1.1       cgd 	    for(;;) {
    605   1.1       cgd 		/*
    606   1.1       cgd 		 * Save the unknown name, and request the next name.
    607   1.1       cgd 		 */
    608  1.37    itojun 		(void) strlcpy(last, terminaltype, sizeof(last));
    609   1.1       cgd 		_gettermname();
    610   1.1       cgd 		if (terminaltypeok(terminaltype))
    611   1.1       cgd 		    break;
    612   1.1       cgd 		if ((strncmp(last, terminaltype, sizeof(last)) == 0) ||
    613   1.1       cgd 		    his_state_is_wont(TELOPT_TTYPE)) {
    614   1.1       cgd 		    /*
    615   1.1       cgd 		     * We've hit the end.  If this is the same as
    616   1.1       cgd 		     * the first name, just go with it.
    617   1.1       cgd 		     */
    618   1.1       cgd 		    if (strncmp(first, terminaltype, sizeof(first)) == 0)
    619   1.1       cgd 			break;
    620   1.1       cgd 		    /*
    621   1.1       cgd 		     * Get the terminal name one more time, so that
    622   1.1       cgd 		     * RFC1091 compliant telnets will cycle back to
    623   1.1       cgd 		     * the start of the list.
    624   1.1       cgd 		     */
    625   1.1       cgd 		     _gettermname();
    626  1.21   thorpej 		    if (strncmp(first, terminaltype, sizeof(first)) != 0) {
    627  1.37    itojun 			(void) strlcpy(terminaltype, first, sizeof(first));
    628  1.21   thorpej 		    }
    629   1.1       cgd 		    break;
    630   1.1       cgd 		}
    631   1.1       cgd 	    }
    632   1.1       cgd 	}
    633   1.1       cgd     }
    634   1.1       cgd     return(retval);
    635   1.1       cgd }  /* end of getterminaltype */
    636   1.1       cgd 
    637  1.39    itojun void
    638   1.1       cgd _gettermname()
    639   1.1       cgd {
    640   1.1       cgd     /*
    641   1.1       cgd      * If the client turned off the option,
    642   1.1       cgd      * we can't send another request, so we
    643   1.1       cgd      * just return.
    644   1.1       cgd      */
    645   1.1       cgd     if (his_state_is_wont(TELOPT_TTYPE))
    646   1.1       cgd 	return;
    647   1.1       cgd     settimer(baseline);
    648  1.27       wiz     output_datalen((const char *)ttytype_sbbuf, sizeof ttytype_sbbuf);
    649   1.4       jtk     DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2,
    650   1.4       jtk 					sizeof ttytype_sbbuf - 2););
    651   1.1       cgd     while (sequenceIs(ttypesubopt, baseline))
    652   1.1       cgd 	ttloop();
    653   1.1       cgd }
    654   1.1       cgd 
    655  1.39    itojun int
    656   1.1       cgd terminaltypeok(s)
    657   1.1       cgd     char *s;
    658   1.1       cgd {
    659   1.1       cgd     char buf[1024];
    660   1.1       cgd 
    661   1.1       cgd     if (terminaltype == NULL)
    662   1.1       cgd 	return(1);
    663   1.1       cgd 
    664   1.1       cgd     /*
    665   1.1       cgd      * tgetent() will return 1 if the type is known, and
    666   1.1       cgd      * 0 if it is not known.  If it returns -1, it couldn't
    667   1.1       cgd      * open the database.  But if we can't open the database,
    668   1.1       cgd      * it won't help to say we failed, because we won't be
    669   1.1       cgd      * able to verify anything else.  So, we treat -1 like 1.
    670   1.1       cgd      */
    671   1.1       cgd     if (tgetent(buf, s) == 0)
    672   1.1       cgd 	return(0);
    673   1.1       cgd     return(1);
    674   1.1       cgd }
    675   1.1       cgd 
    676   1.1       cgd char *hostname;
    677  1.13       mrg char host_name[MAXHOSTNAMELEN + 1];
    678  1.13       mrg char remote_host_name[MAXHOSTNAMELEN + 1];
    679   1.1       cgd 
    680  1.39    itojun extern void telnet __P((int, int));
    681   1.1       cgd 
    682   1.1       cgd /*
    683   1.1       cgd  * Get a pty, scan input lines.
    684   1.1       cgd  */
    685   1.9       mrg void
    686   1.1       cgd doit(who)
    687  1.16    itojun 	struct sockaddr *who;
    688   1.1       cgd {
    689   1.9       mrg 	char *host;
    690  1.16    itojun 	int error;
    691   1.1       cgd 	int level;
    692   1.3       cgd 	int ptynum;
    693  1.36  christos 	int flags;
    694   1.1       cgd 	char user_name[256];
    695   1.1       cgd 
    696   1.1       cgd 	/*
    697   1.1       cgd 	 * Find an available pty to use.
    698   1.1       cgd 	 */
    699   1.3       cgd 	pty = getpty(&ptynum);
    700   1.1       cgd 	if (pty < 0)
    701   1.1       cgd 		fatal(net, "All network ports in use");
    702   1.3       cgd 
    703  1.36  christos 	flags = registerd_host_only ? NI_NAMEREQD : 0;
    704  1.36  christos 
    705   1.1       cgd 	/* get name of connected client */
    706  1.32    itojun 	error = getnameinfo(who, who->sa_len, remote_host_name,
    707  1.36  christos 	    sizeof(remote_host_name), NULL, 0, flags);
    708  1.32    itojun 
    709  1.16    itojun 	if (error) {
    710   1.3       cgd 		fatal(net, "Couldn't resolve your address into a host name.\r\n\
    711   1.4       jtk 	 Please contact your net administrator");
    712   1.9       mrg #ifdef __GNUC__
    713   1.9       mrg 		host = NULL;	/* XXX gcc */
    714   1.9       mrg #endif
    715   1.3       cgd 	}
    716  1.16    itojun 
    717   1.1       cgd 	remote_host_name[sizeof(remote_host_name)-1] = 0;
    718   1.1       cgd 	host = remote_host_name;
    719   1.1       cgd 
    720  1.39    itojun 	(void)gethostname(host_name, sizeof(host_name));
    721  1.13       mrg 	host_name[sizeof(host_name) - 1] = '\0';
    722   1.1       cgd 	hostname = host_name;
    723   1.1       cgd 
    724  1.21   thorpej #if	defined(AUTHENTICATION) || defined(ENCRYPTION)
    725   1.1       cgd 	auth_encrypt_init(hostname, host, "TELNETD", 1);
    726   1.1       cgd #endif
    727   1.1       cgd 
    728   1.1       cgd 	init_env();
    729   1.1       cgd 	/*
    730   1.1       cgd 	 * get terminal type.
    731   1.1       cgd 	 */
    732   1.1       cgd 	*user_name = 0;
    733  1.41    itojun 	level = getterminaltype(user_name, sizeof(user_name));
    734   1.1       cgd 	setenv("TERM", terminaltype ? terminaltype : "network", 1);
    735   1.1       cgd 
    736   1.1       cgd 	/*
    737   1.1       cgd 	 * Start up the login process on the slave side of the terminal
    738   1.1       cgd 	 */
    739   1.1       cgd 	startslave(host, level, user_name);
    740   1.1       cgd 
    741   1.1       cgd 	telnet(net, pty);  /* begin server processing */
    742   1.1       cgd 	/*NOTREACHED*/
    743   1.1       cgd }  /* end of doit */
    744   1.1       cgd 
    745   1.1       cgd 
    746   1.1       cgd /*
    747   1.1       cgd  * Main loop.  Select from pty and network, and
    748   1.1       cgd  * hand data to telnet receiver finite state machine.
    749   1.1       cgd  */
    750  1.39    itojun void
    751   1.1       cgd telnet(f, p)
    752   1.1       cgd 	int f, p;
    753   1.1       cgd {
    754   1.1       cgd 	int on = 1;
    755   1.1       cgd #define	TABBUFSIZ	512
    756   1.1       cgd 	char	defent[TABBUFSIZ];
    757   1.1       cgd 	char	defstrs[TABBUFSIZ];
    758   1.1       cgd #undef	TABBUFSIZ
    759  1.20        ad 	char *HE, *HN, *IM, *IF, *ptyibuf2ptr;
    760  1.35   mycroft 	struct pollfd set[2];
    761   1.1       cgd 
    762   1.1       cgd 	/*
    763   1.1       cgd 	 * Initialize the slc mapping table.
    764   1.1       cgd 	 */
    765   1.1       cgd 	get_slc_defaults();
    766   1.1       cgd 
    767   1.1       cgd 	/*
    768   1.1       cgd 	 * Do some tests where it is desireable to wait for a response.
    769   1.1       cgd 	 * Rather than doing them slowly, one at a time, do them all
    770   1.1       cgd 	 * at once.
    771   1.1       cgd 	 */
    772   1.1       cgd 	if (my_state_is_wont(TELOPT_SGA))
    773   1.1       cgd 		send_will(TELOPT_SGA, 1);
    774   1.1       cgd 	/*
    775   1.1       cgd 	 * Is the client side a 4.2 (NOT 4.3) system?  We need to know this
    776   1.1       cgd 	 * because 4.2 clients are unable to deal with TCP urgent data.
    777   1.1       cgd 	 *
    778   1.1       cgd 	 * To find out, we send out a "DO ECHO".  If the remote system
    779   1.1       cgd 	 * answers "WILL ECHO" it is probably a 4.2 client, and we note
    780   1.1       cgd 	 * that fact ("WILL ECHO" ==> that the client will echo what
    781   1.1       cgd 	 * WE, the server, sends it; it does NOT mean that the client will
    782   1.1       cgd 	 * echo the terminal input).
    783   1.1       cgd 	 */
    784   1.1       cgd 	send_do(TELOPT_ECHO, 1);
    785   1.1       cgd 
    786   1.1       cgd #ifdef	LINEMODE
    787   1.1       cgd 	if (his_state_is_wont(TELOPT_LINEMODE)) {
    788   1.1       cgd 		/* Query the peer for linemode support by trying to negotiate
    789   1.1       cgd 		 * the linemode option.
    790   1.1       cgd 		 */
    791   1.1       cgd 		linemode = 0;
    792   1.1       cgd 		editmode = 0;
    793   1.1       cgd 		send_do(TELOPT_LINEMODE, 1);  /* send do linemode */
    794   1.1       cgd 	}
    795   1.1       cgd #endif	/* LINEMODE */
    796   1.1       cgd 
    797   1.1       cgd 	/*
    798   1.1       cgd 	 * Send along a couple of other options that we wish to negotiate.
    799   1.1       cgd 	 */
    800   1.1       cgd 	send_do(TELOPT_NAWS, 1);
    801   1.1       cgd 	send_will(TELOPT_STATUS, 1);
    802   1.3       cgd 	flowmode = 1;		/* default flow control state */
    803   1.3       cgd 	restartany = -1;	/* uninitialized... */
    804   1.1       cgd 	send_do(TELOPT_LFLOW, 1);
    805   1.1       cgd 
    806   1.1       cgd 	/*
    807   1.1       cgd 	 * Spin, waiting for a response from the DO ECHO.  However,
    808   1.1       cgd 	 * some REALLY DUMB telnets out there might not respond
    809   1.1       cgd 	 * to the DO ECHO.  So, we spin looking for NAWS, (most dumb
    810   1.1       cgd 	 * telnets so far seem to respond with WONT for a DO that
    811   1.1       cgd 	 * they don't understand...) because by the time we get the
    812   1.1       cgd 	 * response, it will already have processed the DO ECHO.
    813   1.1       cgd 	 * Kludge upon kludge.
    814   1.1       cgd 	 */
    815   1.1       cgd 	while (his_will_wont_is_changing(TELOPT_NAWS))
    816   1.1       cgd 		ttloop();
    817   1.1       cgd 
    818   1.1       cgd 	/*
    819   1.1       cgd 	 * But...
    820   1.1       cgd 	 * The client might have sent a WILL NAWS as part of its
    821   1.1       cgd 	 * startup code; if so, we'll be here before we get the
    822   1.1       cgd 	 * response to the DO ECHO.  We'll make the assumption
    823   1.1       cgd 	 * that any implementation that understands about NAWS
    824   1.1       cgd 	 * is a modern enough implementation that it will respond
    825   1.1       cgd 	 * to our DO ECHO request; hence we'll do another spin
    826   1.1       cgd 	 * waiting for the ECHO option to settle down, which is
    827   1.1       cgd 	 * what we wanted to do in the first place...
    828   1.1       cgd 	 */
    829   1.1       cgd 	if (his_want_state_is_will(TELOPT_ECHO) &&
    830   1.1       cgd 	    his_state_is_will(TELOPT_NAWS)) {
    831   1.1       cgd 		while (his_will_wont_is_changing(TELOPT_ECHO))
    832   1.1       cgd 			ttloop();
    833   1.1       cgd 	}
    834   1.1       cgd 	/*
    835   1.1       cgd 	 * On the off chance that the telnet client is broken and does not
    836   1.1       cgd 	 * respond to the DO ECHO we sent, (after all, we did send the
    837   1.1       cgd 	 * DO NAWS negotiation after the DO ECHO, and we won't get here
    838   1.1       cgd 	 * until a response to the DO NAWS comes back) simulate the
    839   1.1       cgd 	 * receipt of a will echo.  This will also send a WONT ECHO
    840   1.1       cgd 	 * to the client, since we assume that the client failed to
    841   1.1       cgd 	 * respond because it believes that it is already in DO ECHO
    842   1.1       cgd 	 * mode, which we do not want.
    843   1.1       cgd 	 */
    844   1.1       cgd 	if (his_want_state_is_will(TELOPT_ECHO)) {
    845   1.1       cgd 		DIAG(TD_OPTIONS,
    846  1.26    itojun 			{output_data("td: simulating recv\r\n");});
    847   1.1       cgd 		willoption(TELOPT_ECHO);
    848   1.1       cgd 	}
    849   1.1       cgd 
    850   1.1       cgd 	/*
    851   1.1       cgd 	 * Finally, to clean things up, we turn on our echo.  This
    852   1.1       cgd 	 * will break stupid 4.2 telnets out of local terminal echo.
    853   1.1       cgd 	 */
    854   1.1       cgd 
    855   1.1       cgd 	if (my_state_is_wont(TELOPT_ECHO))
    856   1.1       cgd 		send_will(TELOPT_ECHO, 1);
    857   1.1       cgd 
    858   1.1       cgd 	/*
    859   1.1       cgd 	 * Turn on packet mode
    860   1.1       cgd 	 */
    861   1.1       cgd 	(void) ioctl(p, TIOCPKT, (char *)&on);
    862   1.3       cgd 
    863   1.1       cgd #if	defined(LINEMODE) && defined(KLUDGELINEMODE)
    864   1.1       cgd 	/*
    865   1.1       cgd 	 * Continuing line mode support.  If client does not support
    866   1.1       cgd 	 * real linemode, attempt to negotiate kludge linemode by sending
    867   1.1       cgd 	 * the do timing mark sequence.
    868   1.1       cgd 	 */
    869   1.1       cgd 	if (lmodetype < REAL_LINEMODE)
    870   1.1       cgd 		send_do(TELOPT_TM, 1);
    871   1.1       cgd #endif	/* defined(LINEMODE) && defined(KLUDGELINEMODE) */
    872   1.1       cgd 
    873   1.1       cgd 	/*
    874   1.1       cgd 	 * Call telrcv() once to pick up anything received during
    875   1.1       cgd 	 * terminal type negotiation, 4.2/4.3 determination, and
    876   1.1       cgd 	 * linemode negotiation.
    877   1.1       cgd 	 */
    878   1.1       cgd 	telrcv();
    879   1.1       cgd 
    880   1.1       cgd 	(void) ioctl(f, FIONBIO, (char *)&on);
    881   1.1       cgd 	(void) ioctl(p, FIONBIO, (char *)&on);
    882   1.1       cgd 
    883  1.39    itojun 	(void) setsockopt(f, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof on);
    884   1.1       cgd 
    885   1.1       cgd 	(void) signal(SIGTSTP, SIG_IGN);
    886   1.1       cgd 	/*
    887   1.1       cgd 	 * Ignoring SIGTTOU keeps the kernel from blocking us
    888   1.1       cgd 	 * in ttioct() in /sys/tty.c.
    889   1.1       cgd 	 */
    890   1.1       cgd 	(void) signal(SIGTTOU, SIG_IGN);
    891   1.1       cgd 
    892   1.1       cgd 	(void) signal(SIGCHLD, cleanup);
    893   1.1       cgd 
    894   1.1       cgd 
    895   1.1       cgd 	{
    896   1.1       cgd 		register int t;
    897   1.1       cgd 		t = open(_PATH_TTY, O_RDWR);
    898   1.1       cgd 		if (t >= 0) {
    899   1.1       cgd 			(void) ioctl(t, TIOCNOTTY, (char *)0);
    900   1.1       cgd 			(void) close(t);
    901   1.1       cgd 		}
    902   1.1       cgd 	}
    903   1.1       cgd 
    904   1.1       cgd 
    905   1.1       cgd 	/*
    906   1.1       cgd 	 * Show banner that getty never gave.
    907   1.1       cgd 	 *
    908   1.1       cgd 	 * We put the banner in the pty input buffer.  This way, it
    909   1.1       cgd 	 * gets carriage return null processing, etc., just like all
    910   1.1       cgd 	 * other pty --> client data.
    911   1.1       cgd 	 */
    912   1.1       cgd 
    913   1.1       cgd 	if (getenv("USER"))
    914   1.1       cgd 		hostinfo = 0;
    915   1.1       cgd 
    916   1.6       tls 	if (getent(defent, gettyname) == 1) {
    917   1.1       cgd 		char *cp=defstrs;
    918   1.1       cgd 
    919   1.1       cgd 		HE = getstr("he", &cp);
    920   1.1       cgd 		HN = getstr("hn", &cp);
    921   1.1       cgd 		IM = getstr("im", &cp);
    922  1.20        ad 		IF = getstr("if", &cp);
    923   1.1       cgd 		if (HN && *HN)
    924  1.23  christos 			(void)strlcpy(host_name, HN, sizeof(host_name));
    925   1.1       cgd 		if (IM == 0)
    926   1.1       cgd 			IM = "";
    927   1.1       cgd 	} else {
    928   1.1       cgd 		IM = DEFAULT_IM;
    929   1.1       cgd 		HE = 0;
    930   1.1       cgd 	}
    931   1.1       cgd 	edithost(HE, host_name);
    932  1.20        ad 	ptyibuf2ptr = ptyibuf2;
    933  1.20        ad 	if (hostinfo) {
    934  1.20        ad 		if (IF)	{
    935  1.20        ad 			char buf[_POSIX2_LINE_MAX];
    936  1.20        ad 			FILE *fd;
    937  1.20        ad 
    938  1.20        ad 			if ((fd = fopen(IF, "r")) != NULL) {
    939  1.20        ad 				while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
    940  1.20        ad 					ptyibuf2ptr = putf(buf, ptyibuf2ptr);
    941  1.20        ad 				fclose(fd);
    942  1.20        ad 			}
    943  1.20        ad 		}
    944  1.20        ad 		if (*IM)
    945  1.20        ad 			ptyibuf2ptr = putf(IM, ptyibuf2ptr);
    946  1.20        ad 	}
    947   1.1       cgd 
    948   1.1       cgd 	if (pcc)
    949  1.20        ad 		strncpy(ptyibuf2ptr, ptyip, pcc+1);
    950   1.1       cgd 	ptyip = ptyibuf2;
    951   1.1       cgd 	pcc = strlen(ptyip);
    952   1.1       cgd #ifdef	LINEMODE
    953   1.1       cgd 	/*
    954   1.1       cgd 	 * Last check to make sure all our states are correct.
    955   1.1       cgd 	 */
    956   1.1       cgd 	init_termbuf();
    957   1.1       cgd 	localstat();
    958   1.1       cgd #endif	/* LINEMODE */
    959   1.1       cgd 
    960   1.1       cgd 	DIAG(TD_REPORT,
    961  1.26    itojun 		{output_data("td: Entering processing loop\r\n");});
    962   1.1       cgd 
    963   1.1       cgd 
    964  1.35   mycroft 	set[0].fd = f;
    965  1.35   mycroft 	set[1].fd = p;
    966   1.1       cgd 	for (;;) {
    967   1.1       cgd 		register int c;
    968   1.1       cgd 
    969   1.1       cgd 		if (ncc < 0 && pcc < 0)
    970   1.1       cgd 			break;
    971   1.1       cgd 
    972   1.1       cgd 		/*
    973   1.1       cgd 		 * Never look for input if there's still
    974   1.1       cgd 		 * stuff in the corresponding output buffer
    975   1.1       cgd 		 */
    976  1.34   mycroft 		set[0].events = 0;
    977  1.34   mycroft 		set[1].events = 0;
    978  1.34   mycroft 		if (nfrontp - nbackp || pcc > 0)
    979  1.34   mycroft 			set[0].events |= POLLOUT;
    980  1.34   mycroft 		else
    981  1.34   mycroft 			set[1].events |= POLLIN;
    982  1.34   mycroft 		if (pfrontp - pbackp || ncc > 0)
    983  1.34   mycroft 			set[1].events |= POLLOUT;
    984  1.34   mycroft 		else
    985  1.34   mycroft 			set[0].events |= POLLIN;
    986  1.34   mycroft 		if (!SYNCHing)
    987  1.34   mycroft 			set[0].events |= POLLPRI;
    988  1.34   mycroft 
    989  1.34   mycroft 		if ((c = poll(set, 2, INFTIM)) < 1) {
    990   1.1       cgd 			if (c == -1) {
    991   1.1       cgd 				if (errno == EINTR) {
    992   1.1       cgd 					continue;
    993   1.1       cgd 				}
    994   1.1       cgd 			}
    995   1.1       cgd 			sleep(5);
    996   1.1       cgd 			continue;
    997   1.1       cgd 		}
    998   1.1       cgd 
    999   1.1       cgd 		/*
   1000   1.1       cgd 		 * Any urgent data?
   1001   1.1       cgd 		 */
   1002  1.34   mycroft 		if (set[0].revents & POLLPRI) {
   1003   1.1       cgd 		    SYNCHing = 1;
   1004   1.1       cgd 		}
   1005   1.1       cgd 
   1006   1.1       cgd 		/*
   1007   1.1       cgd 		 * Something to read from the network...
   1008   1.1       cgd 		 */
   1009  1.34   mycroft 		if (set[0].revents && POLLIN) {
   1010  1.34   mycroft 		    ncc = read(f, netibuf, sizeof (netibuf));
   1011   1.1       cgd 		    if (ncc < 0 && errno == EWOULDBLOCK)
   1012   1.1       cgd 			ncc = 0;
   1013   1.1       cgd 		    else {
   1014   1.1       cgd 			if (ncc <= 0) {
   1015   1.1       cgd 			    break;
   1016   1.1       cgd 			}
   1017   1.1       cgd 			netip = netibuf;
   1018   1.1       cgd 		    }
   1019   1.1       cgd 		    DIAG((TD_REPORT | TD_NETDATA),
   1020  1.26    itojun 			    {output_data("td: netread %d chars\r\n", ncc);});
   1021   1.1       cgd 		    DIAG(TD_NETDATA, printdata("nd", netip, ncc));
   1022   1.1       cgd 		}
   1023   1.1       cgd 
   1024   1.1       cgd 		/*
   1025   1.1       cgd 		 * Something to read from the pty...
   1026   1.1       cgd 		 */
   1027  1.34   mycroft 		if (set[1].revents & POLLIN) {
   1028   1.1       cgd 			pcc = read(p, ptyibuf, BUFSIZ);
   1029   1.1       cgd 			/*
   1030   1.1       cgd 			 * On some systems, if we try to read something
   1031   1.1       cgd 			 * off the master side before the slave side is
   1032   1.1       cgd 			 * opened, we get EIO.
   1033   1.1       cgd 			 */
   1034   1.3       cgd 			if (pcc < 0 && (errno == EWOULDBLOCK ||
   1035   1.3       cgd 					errno == EAGAIN ||
   1036   1.3       cgd 					errno == EIO)) {
   1037   1.1       cgd 				pcc = 0;
   1038   1.1       cgd 			} else {
   1039   1.1       cgd 				if (pcc <= 0)
   1040   1.1       cgd 					break;
   1041   1.1       cgd #ifdef	LINEMODE
   1042   1.1       cgd 				/*
   1043   1.1       cgd 				 * If ioctl from pty, pass it through net
   1044   1.1       cgd 				 */
   1045   1.1       cgd 				if (ptyibuf[0] & TIOCPKT_IOCTL) {
   1046   1.1       cgd 					copy_termbuf(ptyibuf+1, pcc-1);
   1047   1.1       cgd 					localstat();
   1048   1.1       cgd 					pcc = 1;
   1049   1.1       cgd 				}
   1050   1.1       cgd #endif	/* LINEMODE */
   1051   1.1       cgd 				if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) {
   1052   1.1       cgd 					netclear();	/* clear buffer back */
   1053   1.1       cgd 					/*
   1054   1.1       cgd 					 * There are client telnets on some
   1055   1.1       cgd 					 * operating systems get screwed up
   1056   1.1       cgd 					 * royally if we send them urgent
   1057   1.1       cgd 					 * mode data.
   1058   1.1       cgd 					 */
   1059  1.26    itojun 					output_data("%c%c", IAC, DM);
   1060  1.26    itojun 					neturg = nfrontp - 1; /* off by one XXX */
   1061   1.4       jtk 					DIAG(TD_OPTIONS,
   1062   1.4       jtk 					    printoption("td: send IAC", DM));
   1063   1.1       cgd 				}
   1064   1.1       cgd 				if (his_state_is_will(TELOPT_LFLOW) &&
   1065   1.1       cgd 				    (ptyibuf[0] &
   1066   1.1       cgd 				     (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) {
   1067   1.3       cgd 					int newflow =
   1068   1.3       cgd 					    ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0;
   1069   1.3       cgd 					if (newflow != flowmode) {
   1070   1.3       cgd 						flowmode = newflow;
   1071  1.26    itojun 						(void) output_data(
   1072   1.3       cgd 							"%c%c%c%c%c%c",
   1073   1.3       cgd 							IAC, SB, TELOPT_LFLOW,
   1074   1.3       cgd 							flowmode ? LFLOW_ON
   1075   1.3       cgd 								 : LFLOW_OFF,
   1076   1.3       cgd 							IAC, SE);
   1077   1.4       jtk 						DIAG(TD_OPTIONS, printsub('>',
   1078  1.26    itojun 						    (unsigned char *)nfrontp - 4,
   1079   1.4       jtk 						    4););
   1080   1.3       cgd 					}
   1081   1.1       cgd 				}
   1082   1.1       cgd 				pcc--;
   1083   1.1       cgd 				ptyip = ptyibuf+1;
   1084   1.1       cgd 			}
   1085   1.1       cgd 		}
   1086   1.1       cgd 
   1087   1.1       cgd 		while (pcc > 0) {
   1088   1.1       cgd 			if ((&netobuf[BUFSIZ] - nfrontp) < 2)
   1089   1.1       cgd 				break;
   1090   1.1       cgd 			c = *ptyip++ & 0377, pcc--;
   1091   1.1       cgd 			if (c == IAC)
   1092  1.26    itojun 				output_data("%c", c);
   1093  1.26    itojun 			output_data("%c", c);
   1094   1.1       cgd 			if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) {
   1095   1.1       cgd 				if (pcc > 0 && ((*ptyip & 0377) == '\n')) {
   1096  1.26    itojun 					output_data("%c", *ptyip++ & 0377);
   1097   1.1       cgd 					pcc--;
   1098   1.1       cgd 				} else
   1099  1.26    itojun 					output_datalen("\0", 1);
   1100   1.1       cgd 			}
   1101   1.1       cgd 		}
   1102   1.1       cgd 
   1103  1.34   mycroft 		if (set[0].revents & POLLOUT && (nfrontp - nbackp) > 0)
   1104   1.1       cgd 			netflush();
   1105   1.1       cgd 		if (ncc > 0)
   1106   1.1       cgd 			telrcv();
   1107  1.34   mycroft 		if (set[1].revents & POLLOUT && (pfrontp - pbackp) > 0)
   1108   1.1       cgd 			ptyflush();
   1109   1.1       cgd 	}
   1110   1.1       cgd 	cleanup(0);
   1111   1.1       cgd }  /* end of telnet */
   1112   1.4       jtk 
   1113   1.1       cgd /*
   1114   1.1       cgd  * Send interrupt to process on other side of pty.
   1115   1.1       cgd  * If it is in raw mode, just write NULL;
   1116   1.1       cgd  * otherwise, write intr char.
   1117   1.1       cgd  */
   1118  1.39    itojun void
   1119   1.1       cgd interrupt()
   1120   1.1       cgd {
   1121   1.1       cgd 	ptyflush();	/* half-hearted */
   1122   1.1       cgd 
   1123  1.39    itojun 	(void) ioctl(pty, TIOCSIG, (char *)SIGINT);
   1124   1.1       cgd }
   1125   1.1       cgd 
   1126   1.1       cgd /*
   1127   1.1       cgd  * Send quit to process on other side of pty.
   1128   1.1       cgd  * If it is in raw mode, just write NULL;
   1129   1.1       cgd  * otherwise, write quit char.
   1130   1.1       cgd  */
   1131  1.39    itojun void
   1132   1.1       cgd sendbrk()
   1133   1.1       cgd {
   1134   1.1       cgd 	ptyflush();	/* half-hearted */
   1135  1.39    itojun 	(void) ioctl(pty, TIOCSIG, (char *)SIGQUIT);
   1136   1.1       cgd }
   1137   1.1       cgd 
   1138  1.39    itojun void
   1139   1.1       cgd sendsusp()
   1140   1.1       cgd {
   1141   1.1       cgd 	ptyflush();	/* half-hearted */
   1142  1.39    itojun 	(void) ioctl(pty, TIOCSIG, (char *)SIGTSTP);
   1143   1.1       cgd }
   1144   1.1       cgd 
   1145   1.1       cgd /*
   1146   1.1       cgd  * When we get an AYT, if ^T is enabled, use that.  Otherwise,
   1147   1.1       cgd  * just send back "[Yes]".
   1148   1.1       cgd  */
   1149  1.39    itojun void
   1150   1.1       cgd recv_ayt()
   1151   1.1       cgd {
   1152   1.1       cgd 	if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) {
   1153  1.39    itojun 		(void) ioctl(pty, TIOCSIG, (char *)SIGINFO);
   1154   1.1       cgd 		return;
   1155   1.1       cgd 	}
   1156  1.26    itojun 	(void) output_data("\r\n[Yes]\r\n");
   1157   1.1       cgd }
   1158   1.1       cgd 
   1159  1.39    itojun void
   1160   1.1       cgd doeof()
   1161   1.1       cgd {
   1162   1.1       cgd 	init_termbuf();
   1163   1.1       cgd 
   1164  1.39    itojun #if	defined(LINEMODE) && (VEOF == VMIN)
   1165   1.1       cgd 	if (!tty_isediting()) {
   1166   1.1       cgd 		extern char oldeofc;
   1167   1.1       cgd 		*pfrontp++ = oldeofc;
   1168   1.1       cgd 		return;
   1169   1.1       cgd 	}
   1170   1.1       cgd #endif
   1171   1.1       cgd 	*pfrontp++ = slctab[SLC_EOF].sptr ?
   1172   1.1       cgd 			(unsigned char)*slctab[SLC_EOF].sptr : '\004';
   1173   1.1       cgd }
   1174