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