Home | History | Annotate | Line # | Download | only in telnetd
sys_term.c revision 1.28
      1 /*	$NetBSD: sys_term.c,v 1.28 2001/08/24 00:14:04 wiz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)sys_term.c	8.4+1 (Berkeley) 5/30/95";
     40 #else
     41 __RCSID("$NetBSD: sys_term.c,v 1.28 2001/08/24 00:14:04 wiz Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "telnetd.h"
     46 #include "pathnames.h"
     47 
     48 #include <util.h>
     49 
     50 #include <sys/cdefs.h>
     51 #define P __P
     52 
     53 #if defined(CRAY) || defined(__hpux)
     54 # define PARENT_DOES_UTMP
     55 #endif
     56 
     57 #ifdef	NEWINIT
     58 #include <initreq.h>
     59 int	utmp_len = MAXHOSTNAMELEN;	/* sizeof(init_request.host) */
     60 #else	/* NEWINIT*/
     61 # ifdef	UTMPX
     62 # include <utmpx.h>
     63 struct	utmpx wtmp;
     64 # else
     65 # include <utmp.h>
     66 struct	utmp wtmp;
     67 # endif /* UTMPX */
     68 
     69 int	utmp_len = sizeof(wtmp.ut_host);
     70 # ifndef PARENT_DOES_UTMP
     71 char	wtmpf[]	= "/usr/adm/wtmp";
     72 char	utmpf[] = "/etc/utmp";
     73 # else /* PARENT_DOES_UTMP */
     74 char	wtmpf[]	= "/etc/wtmp";
     75 # endif /* PARENT_DOES_UTMP */
     76 
     77 # ifdef CRAY
     78 #include <tmpdir.h>
     79 #include <sys/wait.h>
     80 # endif	/* CRAY */
     81 #endif	/* NEWINIT */
     82 
     83 #ifdef	STREAMSPTY
     84 #include <sac.h>
     85 #include <sys/stropts.h>
     86 #endif
     87 
     88 #define SCPYN(a, b)	(void) strncpy(a, b, sizeof(a))
     89 #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
     90 
     91 #ifdef	STREAMS
     92 #include <sys/stream.h>
     93 #endif
     94 #ifdef __hpux
     95 #include <sys/resource.h>
     96 #include <sys/proc.h>
     97 #endif
     98 #ifdef	t_erase
     99 #undef	t_erase
    100 #undef	t_kill
    101 #undef	t_intrc
    102 #undef	t_quitc
    103 #undef	t_startc
    104 #undef	t_stopc
    105 #undef	t_eofc
    106 #undef	t_brkc
    107 #undef	t_suspc
    108 #undef	t_dsuspc
    109 #undef	t_rprntc
    110 #undef	t_flushc
    111 #undef	t_werasc
    112 #undef	t_lnextc
    113 #endif
    114 
    115 
    116 #ifndef	USE_TERMIO
    117 struct termbuf {
    118 	struct sgttyb sg;
    119 	struct tchars tc;
    120 	struct ltchars ltc;
    121 	int state;
    122 	int lflags;
    123 } termbuf, termbuf2;
    124 # define	cfsetospeed(tp, val)	(tp)->sg.sg_ospeed = (val)
    125 # define	cfsetispeed(tp, val)	(tp)->sg.sg_ispeed = (val)
    126 # define	cfgetospeed(tp)		(tp)->sg.sg_ospeed
    127 # define	cfgetispeed(tp)		(tp)->sg.sg_ispeed
    128 #else	/* USE_TERMIO */
    129 # ifdef	SYSV_TERMIO
    130 #	define termios termio
    131 # endif
    132 # ifndef	TCSANOW
    133 #  ifdef TCSETS
    134 #   define	TCSANOW		TCSETS
    135 #   define	TCSADRAIN	TCSETSW
    136 #   define	tcgetattr(f, t)	ioctl(f, TCGETS, (char *)t)
    137 #  else
    138 #   ifdef TCSETA
    139 #    define	TCSANOW		TCSETA
    140 #    define	TCSADRAIN	TCSETAW
    141 #    define	tcgetattr(f, t)	ioctl(f, TCGETA, (char *)t)
    142 #   else
    143 #    define	TCSANOW		TIOCSETA
    144 #    define	TCSADRAIN	TIOCSETAW
    145 #    define	tcgetattr(f, t)	ioctl(f, TIOCGETA, (char *)t)
    146 #   endif
    147 #  endif
    148 #  define	tcsetattr(f, a, t)	ioctl(f, a, t)
    149 #  define	cfsetospeed(tp, val)	(tp)->c_cflag &= ~CBAUD; \
    150 					(tp)->c_cflag |= (val)
    151 #  define	cfgetospeed(tp)		((tp)->c_cflag & CBAUD)
    152 #  ifdef CIBAUD
    153 #   define	cfsetispeed(tp, val)	(tp)->c_cflag &= ~CIBAUD; \
    154 					(tp)->c_cflag |= ((val)<<IBSHIFT)
    155 #   define	cfgetispeed(tp)		(((tp)->c_cflag & CIBAUD)>>IBSHIFT)
    156 #  else
    157 #   define	cfsetispeed(tp, val)	(tp)->c_cflag &= ~CBAUD; \
    158 					(tp)->c_cflag |= (val)
    159 #   define	cfgetispeed(tp)		((tp)->c_cflag & CBAUD)
    160 #  endif
    161 # endif /* TCSANOW */
    162 struct termios termbuf, termbuf2;	/* pty control structure */
    163 # ifdef  STREAMSPTY
    164 int ttyfd = -1;
    165 # endif
    166 #endif	/* USE_TERMIO */
    167 
    168 void getptyslave __P((void));
    169 int cleanopen __P((char *));
    170 char **addarg __P((char **, char *));
    171 void scrub_env __P((void));
    172 int getent __P((char *, char *));
    173 char *getstr __P((const char *, char **));
    174 #ifdef KRB5
    175 extern void kerberos5_cleanup __P((void));
    176 #endif
    177 
    178 /*
    179  * init_termbuf()
    180  * copy_termbuf(cp)
    181  * set_termbuf()
    182  *
    183  * These three routines are used to get and set the "termbuf" structure
    184  * to and from the kernel.  init_termbuf() gets the current settings.
    185  * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
    186  * set_termbuf() writes the structure into the kernel.
    187  */
    188 
    189 	void
    190 init_termbuf()
    191 {
    192 #ifndef	USE_TERMIO
    193 	(void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
    194 	(void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
    195 	(void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
    196 # ifdef	TIOCGSTATE
    197 	(void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
    198 # endif
    199 #else
    200 # ifdef  STREAMSPTY
    201 	(void) tcgetattr(ttyfd, &termbuf);
    202 # else
    203 	(void) tcgetattr(pty, &termbuf);
    204 # endif
    205 #endif
    206 	termbuf2 = termbuf;
    207 }
    208 
    209 #if	defined(LINEMODE) && defined(TIOCPKT_IOCTL)
    210 	void
    211 copy_termbuf(cp, len)
    212 	char *cp;
    213 	int len;
    214 {
    215 	if (len > sizeof(termbuf))
    216 		len = sizeof(termbuf);
    217 	memmove((char *)&termbuf, cp, len);
    218 	termbuf2 = termbuf;
    219 }
    220 #endif	/* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
    221 
    222 	void
    223 set_termbuf()
    224 {
    225 	/*
    226 	 * Only make the necessary changes.
    227 	 */
    228 #ifndef	USE_TERMIO
    229 	if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
    230 							sizeof(termbuf.sg)))
    231 		(void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
    232 	if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
    233 							sizeof(termbuf.tc)))
    234 		(void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
    235 	if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
    236 							sizeof(termbuf.ltc)))
    237 		(void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
    238 	if (termbuf.lflags != termbuf2.lflags)
    239 		(void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
    240 #else	/* USE_TERMIO */
    241 	if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
    242 # ifdef  STREAMSPTY
    243 		(void) tcsetattr(ttyfd, TCSANOW, &termbuf);
    244 # else
    245 		(void) tcsetattr(pty, TCSANOW, &termbuf);
    246 # endif
    247 #endif	/* USE_TERMIO */
    248 }
    249 
    250 
    251 /*
    252  * spcset(func, valp, valpp)
    253  *
    254  * This function takes various special characters (func), and
    255  * sets *valp to the current value of that character, and
    256  * *valpp to point to where in the "termbuf" structure that
    257  * value is kept.
    258  *
    259  * It returns the SLC_ level of support for this function.
    260  */
    261 
    262 #ifndef	USE_TERMIO
    263 	int
    264 spcset(func, valp, valpp)
    265 	int func;
    266 	cc_t *valp;
    267 	cc_t **valpp;
    268 {
    269 	switch(func) {
    270 	case SLC_EOF:
    271 		*valp = termbuf.tc.t_eofc;
    272 		*valpp = (cc_t *)&termbuf.tc.t_eofc;
    273 		return(SLC_VARIABLE);
    274 	case SLC_EC:
    275 		*valp = termbuf.sg.sg_erase;
    276 		*valpp = (cc_t *)&termbuf.sg.sg_erase;
    277 		return(SLC_VARIABLE);
    278 	case SLC_EL:
    279 		*valp = termbuf.sg.sg_kill;
    280 		*valpp = (cc_t *)&termbuf.sg.sg_kill;
    281 		return(SLC_VARIABLE);
    282 	case SLC_IP:
    283 		*valp = termbuf.tc.t_intrc;
    284 		*valpp = (cc_t *)&termbuf.tc.t_intrc;
    285 		return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
    286 	case SLC_ABORT:
    287 		*valp = termbuf.tc.t_quitc;
    288 		*valpp = (cc_t *)&termbuf.tc.t_quitc;
    289 		return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
    290 	case SLC_XON:
    291 		*valp = termbuf.tc.t_startc;
    292 		*valpp = (cc_t *)&termbuf.tc.t_startc;
    293 		return(SLC_VARIABLE);
    294 	case SLC_XOFF:
    295 		*valp = termbuf.tc.t_stopc;
    296 		*valpp = (cc_t *)&termbuf.tc.t_stopc;
    297 		return(SLC_VARIABLE);
    298 	case SLC_AO:
    299 		*valp = termbuf.ltc.t_flushc;
    300 		*valpp = (cc_t *)&termbuf.ltc.t_flushc;
    301 		return(SLC_VARIABLE);
    302 	case SLC_SUSP:
    303 		*valp = termbuf.ltc.t_suspc;
    304 		*valpp = (cc_t *)&termbuf.ltc.t_suspc;
    305 		return(SLC_VARIABLE);
    306 	case SLC_EW:
    307 		*valp = termbuf.ltc.t_werasc;
    308 		*valpp = (cc_t *)&termbuf.ltc.t_werasc;
    309 		return(SLC_VARIABLE);
    310 	case SLC_RP:
    311 		*valp = termbuf.ltc.t_rprntc;
    312 		*valpp = (cc_t *)&termbuf.ltc.t_rprntc;
    313 		return(SLC_VARIABLE);
    314 	case SLC_LNEXT:
    315 		*valp = termbuf.ltc.t_lnextc;
    316 		*valpp = (cc_t *)&termbuf.ltc.t_lnextc;
    317 		return(SLC_VARIABLE);
    318 	case SLC_FORW1:
    319 		*valp = termbuf.tc.t_brkc;
    320 		*valpp = (cc_t *)&termbuf.ltc.t_lnextc;
    321 		return(SLC_VARIABLE);
    322 	case SLC_BRK:
    323 	case SLC_SYNCH:
    324 	case SLC_AYT:
    325 	case SLC_EOR:
    326 		*valp = (cc_t)0;
    327 		*valpp = (cc_t *)0;
    328 		return(SLC_DEFAULT);
    329 	default:
    330 		*valp = (cc_t)0;
    331 		*valpp = (cc_t *)0;
    332 		return(SLC_NOSUPPORT);
    333 	}
    334 }
    335 
    336 #else	/* USE_TERMIO */
    337 
    338 	int
    339 spcset(func, valp, valpp)
    340 	int func;
    341 	cc_t *valp;
    342 	cc_t **valpp;
    343 {
    344 
    345 #define	setval(a, b)	*valp = termbuf.c_cc[a]; \
    346 			*valpp = &termbuf.c_cc[a]; \
    347 			return(b);
    348 #define	defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
    349 
    350 	switch(func) {
    351 	case SLC_EOF:
    352 		setval(VEOF, SLC_VARIABLE);
    353 	case SLC_EC:
    354 		setval(VERASE, SLC_VARIABLE);
    355 	case SLC_EL:
    356 		setval(VKILL, SLC_VARIABLE);
    357 	case SLC_IP:
    358 		setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
    359 	case SLC_ABORT:
    360 		setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
    361 	case SLC_XON:
    362 #ifdef	VSTART
    363 		setval(VSTART, SLC_VARIABLE);
    364 #else
    365 		defval(0x13);
    366 #endif
    367 	case SLC_XOFF:
    368 #ifdef	VSTOP
    369 		setval(VSTOP, SLC_VARIABLE);
    370 #else
    371 		defval(0x11);
    372 #endif
    373 	case SLC_EW:
    374 #ifdef	VWERASE
    375 		setval(VWERASE, SLC_VARIABLE);
    376 #else
    377 		defval(0);
    378 #endif
    379 	case SLC_RP:
    380 #ifdef	VREPRINT
    381 		setval(VREPRINT, SLC_VARIABLE);
    382 #else
    383 		defval(0);
    384 #endif
    385 	case SLC_LNEXT:
    386 #ifdef	VLNEXT
    387 		setval(VLNEXT, SLC_VARIABLE);
    388 #else
    389 		defval(0);
    390 #endif
    391 	case SLC_AO:
    392 #if	!defined(VDISCARD) && defined(VFLUSHO)
    393 # define VDISCARD VFLUSHO
    394 #endif
    395 #ifdef	VDISCARD
    396 		setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
    397 #else
    398 		defval(0);
    399 #endif
    400 	case SLC_SUSP:
    401 #ifdef	VSUSP
    402 		setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
    403 #else
    404 		defval(0);
    405 #endif
    406 #ifdef	VEOL
    407 	case SLC_FORW1:
    408 		setval(VEOL, SLC_VARIABLE);
    409 #endif
    410 #ifdef	VEOL2
    411 	case SLC_FORW2:
    412 		setval(VEOL2, SLC_VARIABLE);
    413 #endif
    414 	case SLC_AYT:
    415 #ifdef	VSTATUS
    416 		setval(VSTATUS, SLC_VARIABLE);
    417 #else
    418 		defval(0);
    419 #endif
    420 
    421 	case SLC_BRK:
    422 	case SLC_SYNCH:
    423 	case SLC_EOR:
    424 		defval(0);
    425 
    426 	default:
    427 		*valp = 0;
    428 		*valpp = 0;
    429 		return(SLC_NOSUPPORT);
    430 	}
    431 }
    432 #endif	/* USE_TERMIO */
    433 
    434 #ifdef CRAY
    435 /*
    436  * getnpty()
    437  *
    438  * Return the number of pty's configured into the system.
    439  */
    440 	int
    441 getnpty()
    442 {
    443 #ifdef _SC_CRAY_NPTY
    444 	int numptys;
    445 
    446 	if ((numptys = sysconf(_SC_CRAY_NPTY)) != -1)
    447 		return numptys;
    448 	else
    449 #endif /* _SC_CRAY_NPTY */
    450 		return 128;
    451 }
    452 #endif /* CRAY */
    453 
    454 #ifndef	convex
    455 /*
    456  * getpty()
    457  *
    458  * Allocate a pty.  As a side effect, the external character
    459  * array "line" contains the name of the slave side.
    460  *
    461  * Returns the file descriptor of the opened pty.
    462  */
    463 #ifndef	__GNUC__
    464 char *line = NULL16STR;
    465 #else
    466 static char Xline[] = NULL16STR;
    467 char *line = Xline;
    468 #endif
    469 #ifdef	CRAY
    470 char *myline = NULL16STR;
    471 #endif	/* CRAY */
    472 
    473 #ifdef OPENPTY_PTY
    474 
    475 static int ptyslavefd; /* for cleanopen() */
    476 
    477 int
    478 getpty(ptynum)
    479 int *ptynum;
    480 {
    481 	int ptyfd;
    482 
    483 	ptyfd = openpty(ptynum, &ptyslavefd, line, NULL, NULL);
    484 	if (ptyfd == 0)
    485 		return *ptynum;
    486 	ptyslavefd = -1;
    487 	return (-1);
    488 }
    489 #else /* ! OPENPTY_PTY */
    490 
    491 	int
    492 getpty(ptynum)
    493 int *ptynum;
    494 {
    495 	register int p;
    496 #ifdef	STREAMSPTY
    497 	int t;
    498 	char *ptsname();
    499 
    500 	p = open("/dev/ptmx", 2);
    501 	if (p > 0) {
    502 		grantpt(p);
    503 		unlockpt(p);
    504 		(void)strlcpy(line, ptsname(p), sizeof(NULL16STR));
    505 		return(p);
    506 	}
    507 
    508 #else	/* ! STREAMSPTY */
    509 #ifndef CRAY
    510 	register char *cp, *p1, *p2;
    511 	register int i;
    512 #if defined(sun) && defined(TIOCGPGRP) && BSD < 199207
    513 	int dummy;
    514 #endif
    515 
    516 #ifndef	__hpux
    517 	(void) sprintf(line, "/dev/ptyXX");
    518 	p1 = &line[8];
    519 	p2 = &line[9];
    520 #else
    521 	(void) sprintf(line, "/dev/ptym/ptyXX");
    522 	p1 = &line[13];
    523 	p2 = &line[14];
    524 #endif
    525 
    526 	for (cp = "pqrstuvwxyzPQRST"; *cp; cp++) {
    527 		struct stat stb;
    528 
    529 		*p1 = *cp;
    530 		*p2 = '0';
    531 		/*
    532 		 * This stat() check is just to keep us from
    533 		 * looping through all 256 combinations if there
    534 		 * aren't that many ptys available.
    535 		 */
    536 		if (stat(line, &stb) < 0)
    537 			break;
    538 		for (i = 0; i < 16; i++) {
    539 			*p2 = "0123456789abcdef"[i];
    540 			p = open(line, 2);
    541 			if (p > 0) {
    542 #ifndef	__hpux
    543 				line[5] = 't';
    544 #else
    545 				for (p1 = &line[8]; *p1; p1++)
    546 					*p1 = *(p1+1);
    547 				line[9] = 't';
    548 #endif
    549 				chown(line, 0, 0);
    550 				chmod(line, 0600);
    551 #if defined(sun) && defined(TIOCGPGRP) && BSD < 199207
    552 				if (ioctl(p, TIOCGPGRP, &dummy) == 0
    553 				    || errno != EIO) {
    554 					chmod(line, 0666);
    555 					close(p);
    556 					line[5] = 'p';
    557 				} else
    558 #endif /* defined(sun) && defined(TIOCGPGRP) && BSD < 199207 */
    559 					return(p);
    560 			}
    561 		}
    562 	}
    563 #else	/* CRAY */
    564 	extern lowpty, highpty;
    565 	struct stat sb;
    566 
    567 	for (*ptynum = lowpty; *ptynum <= highpty; (*ptynum)++) {
    568 		(void) sprintf(myline, "/dev/pty/%03d", *ptynum);
    569 		p = open(myline, 2);
    570 		if (p < 0)
    571 			continue;
    572 		(void) sprintf(line, "/dev/ttyp%03d", *ptynum);
    573 		/*
    574 		 * Here are some shenanigans to make sure that there
    575 		 * are no listeners lurking on the line.
    576 		 */
    577 		if(stat(line, &sb) < 0) {
    578 			(void) close(p);
    579 			continue;
    580 		}
    581 		if(sb.st_uid || sb.st_gid || sb.st_mode != 0600) {
    582 			chown(line, 0, 0);
    583 			chmod(line, 0600);
    584 			(void)close(p);
    585 			p = open(myline, 2);
    586 			if (p < 0)
    587 				continue;
    588 		}
    589 		/*
    590 		 * Now it should be safe...check for accessability.
    591 		 */
    592 		if (access(line, 6) == 0)
    593 			return(p);
    594 		else {
    595 			/* no tty side to pty so skip it */
    596 			(void) close(p);
    597 		}
    598 	}
    599 #endif	/* CRAY */
    600 #endif	/* STREAMSPTY */
    601 	return(-1);
    602 }
    603 #endif /* OPENPTY_PTY */
    604 #endif	/* convex */
    605 
    606 #ifdef	LINEMODE
    607 /*
    608  * tty_flowmode()	Find out if flow control is enabled or disabled.
    609  * tty_linemode()	Find out if linemode (external processing) is enabled.
    610  * tty_setlinemod(on)	Turn on/off linemode.
    611  * tty_isecho()		Find out if echoing is turned on.
    612  * tty_setecho(on)	Enable/disable character echoing.
    613  * tty_israw()		Find out if terminal is in RAW mode.
    614  * tty_binaryin(on)	Turn on/off BINARY on input.
    615  * tty_binaryout(on)	Turn on/off BINARY on output.
    616  * tty_isediting()	Find out if line editing is enabled.
    617  * tty_istrapsig()	Find out if signal trapping is enabled.
    618  * tty_setedit(on)	Turn on/off line editing.
    619  * tty_setsig(on)	Turn on/off signal trapping.
    620  * tty_issofttab()	Find out if tab expansion is enabled.
    621  * tty_setsofttab(on)	Turn on/off soft tab expansion.
    622  * tty_islitecho()	Find out if typed control chars are echoed literally
    623  * tty_setlitecho()	Turn on/off literal echo of control chars
    624  * tty_tspeed(val)	Set transmit speed to val.
    625  * tty_rspeed(val)	Set receive speed to val.
    626  */
    627 
    628 #ifdef convex
    629 static int linestate;
    630 #endif
    631 
    632 	int
    633 tty_linemode()
    634 {
    635 #ifndef convex
    636 #ifndef	USE_TERMIO
    637 	return(termbuf.state & TS_EXTPROC);
    638 #else
    639 	return(termbuf.c_lflag & EXTPROC);
    640 #endif
    641 #else
    642 	return(linestate);
    643 #endif
    644 }
    645 
    646 	void
    647 tty_setlinemode(on)
    648 	int on;
    649 {
    650 #ifdef	TIOCEXT
    651 # ifndef convex
    652 	set_termbuf();
    653 # else
    654 	linestate = on;
    655 # endif
    656 	(void) ioctl(pty, TIOCEXT, (char *)&on);
    657 # ifndef convex
    658 	init_termbuf();
    659 # endif
    660 #else	/* !TIOCEXT */
    661 # ifdef	EXTPROC
    662 	if (on)
    663 		termbuf.c_lflag |= EXTPROC;
    664 	else
    665 		termbuf.c_lflag &= ~EXTPROC;
    666 # endif
    667 #endif	/* TIOCEXT */
    668 }
    669 #endif	/* LINEMODE */
    670 
    671 	int
    672 tty_isecho()
    673 {
    674 #ifndef USE_TERMIO
    675 	return (termbuf.sg.sg_flags & ECHO);
    676 #else
    677 	return (termbuf.c_lflag & ECHO);
    678 #endif
    679 }
    680 
    681 	int
    682 tty_flowmode()
    683 {
    684 #ifndef USE_TERMIO
    685 	return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
    686 #else
    687 	return((termbuf.c_iflag & IXON) ? 1 : 0);
    688 #endif
    689 }
    690 
    691 	int
    692 tty_restartany()
    693 {
    694 #ifndef USE_TERMIO
    695 # ifdef	DECCTQ
    696 	return((termbuf.lflags & DECCTQ) ? 0 : 1);
    697 # else
    698 	return(-1);
    699 # endif
    700 #else
    701 	return((termbuf.c_iflag & IXANY) ? 1 : 0);
    702 #endif
    703 }
    704 
    705 	void
    706 tty_setecho(on)
    707 	int on;
    708 {
    709 #ifndef	USE_TERMIO
    710 	if (on)
    711 		termbuf.sg.sg_flags |= ECHO|CRMOD;
    712 	else
    713 		termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
    714 #else
    715 	if (on)
    716 		termbuf.c_lflag |= ECHO;
    717 	else
    718 		termbuf.c_lflag &= ~ECHO;
    719 #endif
    720 }
    721 
    722 	int
    723 tty_israw()
    724 {
    725 #ifndef USE_TERMIO
    726 	return(termbuf.sg.sg_flags & RAW);
    727 #else
    728 	return(!(termbuf.c_lflag & ICANON));
    729 #endif
    730 }
    731 
    732 #if	defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
    733 	int
    734 tty_setraw(on)
    735 {
    736 #  ifndef USE_TERMIO
    737 	if (on)
    738 		termbuf.sg.sg_flags |= RAW;
    739 	else
    740 		termbuf.sg.sg_flags &= ~RAW;
    741 #  else
    742 	if (on)
    743 		termbuf.c_lflag &= ~ICANON;
    744 	else
    745 		termbuf.c_lflag |= ICANON;
    746 #  endif
    747 }
    748 #endif
    749 
    750 	void
    751 tty_binaryin(on)
    752 	int on;
    753 {
    754 #ifndef	USE_TERMIO
    755 	if (on)
    756 		termbuf.lflags |= LPASS8;
    757 	else
    758 		termbuf.lflags &= ~LPASS8;
    759 #else
    760 	if (on) {
    761 		termbuf.c_iflag &= ~ISTRIP;
    762 	} else {
    763 		termbuf.c_iflag |= ISTRIP;
    764 	}
    765 #endif
    766 }
    767 
    768 	void
    769 tty_binaryout(on)
    770 	int on;
    771 {
    772 #ifndef	USE_TERMIO
    773 	if (on)
    774 		termbuf.lflags |= LLITOUT;
    775 	else
    776 		termbuf.lflags &= ~LLITOUT;
    777 #else
    778 	if (on) {
    779 		termbuf.c_cflag &= ~(CSIZE|PARENB);
    780 		termbuf.c_cflag |= CS8;
    781 		termbuf.c_oflag &= ~OPOST;
    782 	} else {
    783 		termbuf.c_cflag &= ~CSIZE;
    784 		termbuf.c_cflag |= CS7|PARENB;
    785 		termbuf.c_oflag |= OPOST;
    786 	}
    787 #endif
    788 }
    789 
    790 	int
    791 tty_isbinaryin()
    792 {
    793 #ifndef	USE_TERMIO
    794 	return(termbuf.lflags & LPASS8);
    795 #else
    796 	return(!(termbuf.c_iflag & ISTRIP));
    797 #endif
    798 }
    799 
    800 	int
    801 tty_isbinaryout()
    802 {
    803 #ifndef	USE_TERMIO
    804 	return(termbuf.lflags & LLITOUT);
    805 #else
    806 	return(!(termbuf.c_oflag&OPOST));
    807 #endif
    808 }
    809 
    810 #ifdef	LINEMODE
    811 	int
    812 tty_isediting()
    813 {
    814 #ifndef USE_TERMIO
    815 	return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
    816 #else
    817 	return(termbuf.c_lflag & ICANON);
    818 #endif
    819 }
    820 
    821 	int
    822 tty_istrapsig()
    823 {
    824 #ifndef USE_TERMIO
    825 	return(!(termbuf.sg.sg_flags&RAW));
    826 #else
    827 	return(termbuf.c_lflag & ISIG);
    828 #endif
    829 }
    830 
    831 	void
    832 tty_setedit(on)
    833 	int on;
    834 {
    835 #ifndef USE_TERMIO
    836 	if (on)
    837 		termbuf.sg.sg_flags &= ~CBREAK;
    838 	else
    839 		termbuf.sg.sg_flags |= CBREAK;
    840 #else
    841 	if (on)
    842 		termbuf.c_lflag |= ICANON;
    843 	else
    844 		termbuf.c_lflag &= ~ICANON;
    845 #endif
    846 }
    847 
    848 	void
    849 tty_setsig(on)
    850 	int on;
    851 {
    852 #ifndef	USE_TERMIO
    853 	if (on)
    854 		;
    855 #else
    856 	if (on)
    857 		termbuf.c_lflag |= ISIG;
    858 	else
    859 		termbuf.c_lflag &= ~ISIG;
    860 #endif
    861 }
    862 #endif	/* LINEMODE */
    863 
    864 	int
    865 tty_issofttab()
    866 {
    867 #ifndef	USE_TERMIO
    868 	return (termbuf.sg.sg_flags & XTABS);
    869 #else
    870 # ifdef	OXTABS
    871 	return (termbuf.c_oflag & OXTABS);
    872 # endif
    873 # ifdef	TABDLY
    874 	return ((termbuf.c_oflag & TABDLY) == TAB3);
    875 # endif
    876 #endif
    877 }
    878 
    879 	void
    880 tty_setsofttab(on)
    881 	int on;
    882 {
    883 #ifndef	USE_TERMIO
    884 	if (on)
    885 		termbuf.sg.sg_flags |= XTABS;
    886 	else
    887 		termbuf.sg.sg_flags &= ~XTABS;
    888 #else
    889 	if (on) {
    890 # ifdef	OXTABS
    891 		termbuf.c_oflag |= OXTABS;
    892 # endif
    893 # ifdef	TABDLY
    894 		termbuf.c_oflag &= ~TABDLY;
    895 		termbuf.c_oflag |= TAB3;
    896 # endif
    897 	} else {
    898 # ifdef	OXTABS
    899 		termbuf.c_oflag &= ~OXTABS;
    900 # endif
    901 # ifdef	TABDLY
    902 		termbuf.c_oflag &= ~TABDLY;
    903 		termbuf.c_oflag |= TAB0;
    904 # endif
    905 	}
    906 #endif
    907 }
    908 
    909 	int
    910 tty_islitecho()
    911 {
    912 #ifndef	USE_TERMIO
    913 	return (!(termbuf.lflags & LCTLECH));
    914 #else
    915 # ifdef	ECHOCTL
    916 	return (!(termbuf.c_lflag & ECHOCTL));
    917 # endif
    918 # ifdef	TCTLECH
    919 	return (!(termbuf.c_lflag & TCTLECH));
    920 # endif
    921 # if	!defined(ECHOCTL) && !defined(TCTLECH)
    922 	return (0);	/* assumes ctl chars are echoed '^x' */
    923 # endif
    924 #endif
    925 }
    926 
    927 	void
    928 tty_setlitecho(on)
    929 	int on;
    930 {
    931 #ifndef	USE_TERMIO
    932 	if (on)
    933 		termbuf.lflags &= ~LCTLECH;
    934 	else
    935 		termbuf.lflags |= LCTLECH;
    936 #else
    937 # ifdef	ECHOCTL
    938 	if (on)
    939 		termbuf.c_lflag &= ~ECHOCTL;
    940 	else
    941 		termbuf.c_lflag |= ECHOCTL;
    942 # endif
    943 # ifdef	TCTLECH
    944 	if (on)
    945 		termbuf.c_lflag &= ~TCTLECH;
    946 	else
    947 		termbuf.c_lflag |= TCTLECH;
    948 # endif
    949 #endif
    950 }
    951 
    952 	int
    953 tty_iscrnl()
    954 {
    955 #ifndef	USE_TERMIO
    956 	return (termbuf.sg.sg_flags & CRMOD);
    957 #else
    958 	return (termbuf.c_iflag & ICRNL);
    959 #endif
    960 }
    961 
    962 /*
    963  * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
    964  */
    965 #if B4800 != 4800
    966 #define	DECODE_BAUD
    967 #endif
    968 
    969 #ifdef	DECODE_BAUD
    970 
    971 /*
    972  * A table of available terminal speeds
    973  */
    974 struct termspeeds {
    975 	int	speed;
    976 	int	value;
    977 } termspeeds[] = {
    978 	{ 0,      B0 },      { 50,    B50 },    { 75,     B75 },
    979 	{ 110,    B110 },    { 134,   B134 },   { 150,    B150 },
    980 	{ 200,    B200 },    { 300,   B300 },   { 600,    B600 },
    981 	{ 1200,   B1200 },   { 1800,  B1800 },  { 2400,   B2400 },
    982 	{ 4800,   B4800 },
    983 #ifdef	B7200
    984 	{ 7200,  B7200 },
    985 #endif
    986 	{ 9600,   B9600 },
    987 #ifdef	B14400
    988 	{ 14400,  B14400 },
    989 #endif
    990 #ifdef	B19200
    991 	{ 19200,  B19200 },
    992 #endif
    993 #ifdef	B28800
    994 	{ 28800,  B28800 },
    995 #endif
    996 #ifdef	B38400
    997 	{ 38400,  B38400 },
    998 #endif
    999 #ifdef	B57600
   1000 	{ 57600,  B57600 },
   1001 #endif
   1002 #ifdef	B115200
   1003 	{ 115200, B115200 },
   1004 #endif
   1005 #ifdef	B230400
   1006 	{ 230400, B230400 },
   1007 #endif
   1008 	{ -1,     0 }
   1009 };
   1010 #endif	/* DECODE_BAUD */
   1011 
   1012 	void
   1013 tty_tspeed(val)
   1014 	int val;
   1015 {
   1016 #ifdef	DECODE_BAUD
   1017 	register struct termspeeds *tp;
   1018 
   1019 	for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
   1020 		;
   1021 	if (tp->speed == -1)	/* back up to last valid value */
   1022 		--tp;
   1023 	cfsetospeed(&termbuf, tp->value);
   1024 #else	/* DECODE_BAUD */
   1025 	cfsetospeed(&termbuf, val);
   1026 #endif	/* DECODE_BAUD */
   1027 }
   1028 
   1029 	void
   1030 tty_rspeed(val)
   1031 	int val;
   1032 {
   1033 #ifdef	DECODE_BAUD
   1034 	register struct termspeeds *tp;
   1035 
   1036 	for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
   1037 		;
   1038 	if (tp->speed == -1)	/* back up to last valid value */
   1039 		--tp;
   1040 	cfsetispeed(&termbuf, tp->value);
   1041 #else	/* DECODE_BAUD */
   1042 	cfsetispeed(&termbuf, val);
   1043 #endif	/* DECODE_BAUD */
   1044 }
   1045 
   1046 
   1047 #ifdef PARENT_DOES_UTMP
   1048 # ifndef NEWINIT
   1049 extern	struct utmp wtmp;
   1050 extern char wtmpf[];
   1051 # else	/* NEWINIT */
   1052 int	gotalarm;
   1053 
   1054 	/* ARGSUSED */
   1055 	void
   1056 nologinproc(sig)
   1057 	int sig;
   1058 {
   1059 	gotalarm++;
   1060 }
   1061 # endif	/* NEWINIT */
   1062 #endif /* PARENT_DOES_UTMP */
   1063 
   1064 #ifndef	NEWINIT
   1065 # ifdef PARENT_DOES_UTMP
   1066 extern void utmp_sig_init P((void));
   1067 extern void utmp_sig_reset P((void));
   1068 extern void utmp_sig_wait P((void));
   1069 extern void utmp_sig_notify P((int));
   1070 # endif /* PARENT_DOES_UTMP */
   1071 #endif
   1072 
   1073 /*
   1074  * getptyslave()
   1075  *
   1076  * Open the slave side of the pty, and do any initialization
   1077  * that is necessary.  The return value is a file descriptor
   1078  * for the slave side.
   1079  */
   1080 #if	!defined(CRAY) || !defined(NEWINIT)
   1081 extern int def_tspeed, def_rspeed;
   1082 # ifdef	TIOCGWINSZ
   1083 	extern int def_row, def_col;
   1084 # endif
   1085 #endif
   1086 
   1087     void
   1088 getptyslave()
   1089 {
   1090 	register int t = -1;
   1091 
   1092 #if	!defined(CRAY) || !defined(NEWINIT)
   1093 # ifdef	LINEMODE
   1094 	int waslm;
   1095 # endif
   1096 # ifdef	TIOCGWINSZ
   1097 	struct winsize ws;
   1098 # endif
   1099 	/*
   1100 	 * Opening the slave side may cause initilization of the
   1101 	 * kernel tty structure.  We need remember the state of
   1102 	 * 	if linemode was turned on
   1103 	 *	terminal window size
   1104 	 *	terminal speed
   1105 	 * so that we can re-set them if we need to.
   1106 	 */
   1107 # ifdef	LINEMODE
   1108 	waslm = tty_linemode();
   1109 # endif
   1110 
   1111 	/*
   1112 	 * Make sure that we don't have a controlling tty, and
   1113 	 * that we are the session (process group) leader.
   1114 	 */
   1115 # ifdef	TIOCNOTTY
   1116 	t = open(_PATH_TTY, O_RDWR);
   1117 	if (t >= 0) {
   1118 		(void) ioctl(t, TIOCNOTTY, (char *)0);
   1119 		(void) close(t);
   1120 	}
   1121 # endif
   1122 
   1123 
   1124 # ifdef PARENT_DOES_UTMP
   1125 	/*
   1126 	 * Wait for our parent to get the utmp stuff to get done.
   1127 	 */
   1128 	utmp_sig_wait();
   1129 # endif
   1130 
   1131 	t = cleanopen(line);
   1132 	if (t < 0)
   1133 		fatalperror(net, line);
   1134 
   1135 #ifdef  STREAMSPTY
   1136 #ifdef	USE_TERMIO
   1137 	ttyfd = t;
   1138 #endif
   1139 	if (ioctl(t, I_PUSH, "ptem") < 0)
   1140 		fatal(net, "I_PUSH ptem");
   1141 	if (ioctl(t, I_PUSH, "ldterm") < 0)
   1142 		fatal(net, "I_PUSH ldterm");
   1143 	if (ioctl(t, I_PUSH, "ttcompat") < 0)
   1144 		fatal(net, "I_PUSH ttcompat");
   1145 	if (ioctl(pty, I_PUSH, "pckt") < 0)
   1146 		fatal(net, "I_PUSH pckt");
   1147 #endif
   1148 
   1149 	/*
   1150 	 * set up the tty modes as we like them to be.
   1151 	 */
   1152 	init_termbuf();
   1153 # ifdef	TIOCGWINSZ
   1154 	if (def_row || def_col) {
   1155 		memset((char *)&ws, 0, sizeof(ws));
   1156 		ws.ws_col = def_col;
   1157 		ws.ws_row = def_row;
   1158 		(void)ioctl(t, TIOCSWINSZ, (char *)&ws);
   1159 	}
   1160 # endif
   1161 
   1162 	/*
   1163 	 * Settings for sgtty based systems
   1164 	 */
   1165 # ifndef	USE_TERMIO
   1166 	termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
   1167 # endif	/* USE_TERMIO */
   1168 
   1169 	/*
   1170 	 * Settings for UNICOS (and HPUX)
   1171 	 */
   1172 # if defined(CRAY) || defined(__hpux)
   1173 	termbuf.c_oflag = OPOST|ONLCR|TAB3;
   1174 	termbuf.c_iflag = IGNPAR|ISTRIP|ICRNL|IXON;
   1175 	termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
   1176 	termbuf.c_cflag = EXTB|HUPCL|CS8;
   1177 # endif
   1178 
   1179 	/*
   1180 	 * Settings for all other termios/termio based
   1181 	 * systems, other than 4.4BSD.  In 4.4BSD the
   1182 	 * kernel does the initial terminal setup.
   1183 	 */
   1184 # if defined(USE_TERMIO) && !(defined(CRAY) || defined(__hpux)) && (BSD <= 43)
   1185 #  ifndef	OXTABS
   1186 #   define OXTABS	0
   1187 #  endif
   1188 	termbuf.c_lflag |= ECHO;
   1189 	termbuf.c_oflag |= ONLCR|OXTABS;
   1190 	termbuf.c_iflag |= ICRNL;
   1191 	termbuf.c_iflag &= ~IXOFF;
   1192 # endif /* defined(USE_TERMIO) && !defined(CRAY) && (BSD <= 43) */
   1193 	tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
   1194 	tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
   1195 # ifdef	LINEMODE
   1196 	if (waslm)
   1197 		tty_setlinemode(1);
   1198 # endif	/* LINEMODE */
   1199 
   1200 	/*
   1201 	 * Set the tty modes, and make this our controlling tty.
   1202 	 */
   1203 	set_termbuf();
   1204 	if (login_tty(t) == -1)
   1205 		fatalperror(net, "login_tty");
   1206 #endif	/* !defined(CRAY) || !defined(NEWINIT) */
   1207 	if (net > 2)
   1208 		(void) close(net);
   1209 #if	defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
   1210 	/*
   1211 	 * Leave the pty open so that we can write out the rlogin
   1212 	 * protocol for /bin/login, if the authentication works.
   1213 	 */
   1214 #else
   1215 	if (pty > 2) {
   1216 		(void) close(pty);
   1217 		pty = -1;
   1218 	}
   1219 #endif
   1220 }
   1221 
   1222 #if	!defined(CRAY) || !defined(NEWINIT)
   1223 #ifndef	O_NOCTTY
   1224 #define	O_NOCTTY	0
   1225 #endif
   1226 /*
   1227  * Open the specified slave side of the pty,
   1228  * making sure that we have a clean tty.
   1229  */
   1230 	int
   1231 cleanopen(ttyline)
   1232 	char *ttyline;
   1233 {
   1234 #ifdef OPENPTY_PTY
   1235 	return ptyslavefd;
   1236 #else /* ! OPENPTY_PTY */
   1237 	register int t;
   1238 
   1239 #ifndef STREAMSPTY
   1240 	/*
   1241 	 * Make sure that other people can't open the
   1242 	 * slave side of the connection.
   1243 	 */
   1244 	(void) chown(ttyline, 0, 0);
   1245 	(void) chmod(ttyline, 0600);
   1246 #endif
   1247 
   1248 # if !defined(CRAY) && (BSD > 43)
   1249 	(void) revoke(ttyline);
   1250 # endif
   1251 
   1252 	t = open(ttyline, O_RDWR|O_NOCTTY);
   1253 
   1254 
   1255 	if (t < 0)
   1256 		return(-1);
   1257 
   1258 	/*
   1259 	 * Hangup anybody else using this ttyp, then reopen it for
   1260 	 * ourselves.
   1261 	 */
   1262 # if !(defined(CRAY) || defined(__hpux)) && (BSD <= 43) && !defined(STREAMSPTY)
   1263 	(void) signal(SIGHUP, SIG_IGN);
   1264 	vhangup();
   1265 	(void) signal(SIGHUP, SIG_DFL);
   1266 	t = open(ttyline, O_RDWR|O_NOCTTY);
   1267 	if (t < 0)
   1268 		return(-1);
   1269 # endif
   1270 # if	defined(CRAY)
   1271 	{
   1272 		register int i;
   1273 		i = open(ttyline, O_RDWR);
   1274 
   1275 		(void) close(t);
   1276 		if (i < 0)
   1277 			return(-1);
   1278 		t = i;
   1279 	}
   1280 # endif	/* defined(CRAY) */
   1281 	return(t);
   1282 #endif /* OPENPTY_PTY */
   1283 }
   1284 #endif	/* !defined(CRAY) || !defined(NEWINIT) */
   1285 
   1286 #if BSD <= 43
   1287 
   1288 	int
   1289 login_tty(t)
   1290 	int t;
   1291 {
   1292 	if (setsid() < 0) {
   1293 #ifdef ultrix
   1294 		/*
   1295 		 * The setsid() may have failed because we
   1296 		 * already have a pgrp == pid.  Zero out
   1297 		 * our pgrp and try again...
   1298 		 */
   1299 		if ((setpgrp(0, 0) < 0) || (setsid() < 0))
   1300 #endif
   1301 			fatalperror(net, "setsid()");
   1302 	}
   1303 # ifdef	TIOCSCTTY
   1304 	if (ioctl(t, TIOCSCTTY, (char *)0) < 0)
   1305 		fatalperror(net, "ioctl(sctty)");
   1306 #  if defined(CRAY)
   1307 	/*
   1308 	 * Close the hard fd to /dev/ttypXXX, and re-open through
   1309 	 * the indirect /dev/tty interface.
   1310 	 */
   1311 	close(t);
   1312 	if ((t = open("/dev/tty", O_RDWR)) < 0)
   1313 		fatalperror(net, "open(/dev/tty)");
   1314 #  endif
   1315 # else
   1316 	/*
   1317 	 * We get our controlling tty assigned as a side-effect
   1318 	 * of opening up a tty device.  But on BSD based systems,
   1319 	 * this only happens if our process group is zero.  The
   1320 	 * setsid() call above may have set our pgrp, so clear
   1321 	 * it out before opening the tty...
   1322 	 */
   1323 #  ifndef SOLARIS
   1324 	(void) setpgrp(0, 0);
   1325 #  else
   1326 	(void) setpgrp();
   1327 #  endif
   1328 	close(open(line, O_RDWR));
   1329 # endif
   1330 	if (t != 0)
   1331 		(void) dup2(t, 0);
   1332 	if (t != 1)
   1333 		(void) dup2(t, 1);
   1334 	if (t != 2)
   1335 		(void) dup2(t, 2);
   1336 	if (t > 2)
   1337 		close(t);
   1338 	return(0);
   1339 }
   1340 #endif	/* BSD <= 43 */
   1341 
   1342 #ifdef	NEWINIT
   1343 char *gen_id = "fe";
   1344 #endif
   1345 
   1346 /*
   1347  * startslave(host)
   1348  *
   1349  * Given a hostname, do whatever
   1350  * is necessary to startup the login process on the slave side of the pty.
   1351  */
   1352 
   1353 /* ARGSUSED */
   1354 	void
   1355 startslave(host, autologin, autoname)
   1356 	char *host;
   1357 	int autologin;
   1358 	char *autoname;
   1359 {
   1360 	register int i;
   1361 #ifdef	NEWINIT
   1362 	extern char *ptyip;
   1363 	struct init_request request;
   1364 	void nologinproc();
   1365 	register int n;
   1366 #endif	/* NEWINIT */
   1367 
   1368 #if	defined(AUTHENTICATION)
   1369 	if (!autoname || !autoname[0])
   1370 		autologin = 0;
   1371 
   1372 	if (autologin < auth_level) {
   1373 		fatal(net, "Authorization failed");
   1374 		exit(1);
   1375 	}
   1376 #endif
   1377 
   1378 #ifndef	NEWINIT
   1379 # ifdef	PARENT_DOES_UTMP
   1380 	utmp_sig_init();
   1381 # endif	/* PARENT_DOES_UTMP */
   1382 
   1383 	if ((i = fork()) < 0)
   1384 		fatalperror(net, "fork");
   1385 	if (i) {
   1386 # ifdef PARENT_DOES_UTMP
   1387 		/*
   1388 		 * Cray parent will create utmp entry for child and send
   1389 		 * signal to child to tell when done.  Child waits for signal
   1390 		 * before doing anything important.
   1391 		 */
   1392 		register int pid = i;
   1393 		void sigjob P((int));
   1394 
   1395 		setpgrp();
   1396 		utmp_sig_reset();		/* reset handler to default */
   1397 		/*
   1398 		 * Create utmp entry for child
   1399 		 */
   1400 		(void) time(&wtmp.ut_time);
   1401 		wtmp.ut_type = LOGIN_PROCESS;
   1402 		wtmp.ut_pid = pid;
   1403 		SCPYN(wtmp.ut_user, "LOGIN");
   1404 		SCPYN(wtmp.ut_host, host);
   1405 		SCPYN(wtmp.ut_line, line + sizeof("/dev/") - 1);
   1406 #ifndef	__hpux
   1407 		SCPYN(wtmp.ut_id, wtmp.ut_line+3);
   1408 #else
   1409 		SCPYN(wtmp.ut_id, wtmp.ut_line+7);
   1410 #endif
   1411 		pututline(&wtmp);
   1412 		endutent();
   1413 		if ((i = open(wtmpf, O_WRONLY|O_APPEND)) >= 0) {
   1414 			(void) write(i, (char *)&wtmp, sizeof(struct utmp));
   1415 			(void) close(i);
   1416 		}
   1417 #ifdef	CRAY
   1418 		{
   1419 		  struct sigaction act;
   1420 		  act.sa_handler = sigjob;
   1421 		  act.sa_mask = sigmask(SIGCHLD) | sigmask(WJSIGNAL);
   1422 		  act.sa_flags = 0;
   1423 		  (void) sigaction(WJSIGNAL, &act, 0);
   1424 		}
   1425 #endif
   1426 		utmp_sig_notify(pid);
   1427 # endif	/* PARENT_DOES_UTMP */
   1428 	} else {
   1429 		getptyslave();
   1430 		start_login(host, autologin, autoname);
   1431 		/*NOTREACHED*/
   1432 	}
   1433 #else	/* NEWINIT */
   1434 
   1435 	/*
   1436 	 * Init will start up login process if we ask nicely.  We only wait
   1437 	 * for it to start up and begin normal telnet operation.
   1438 	 */
   1439 	if ((i = open(INIT_FIFO, O_WRONLY)) < 0) {
   1440 		char tbuf[128];
   1441 
   1442 		(void)snprintf(tbuf, sizeof tbuf, "Can't open %s\n", INIT_FIFO);
   1443 		fatalperror(net, tbuf);
   1444 	}
   1445 	memset((char *)&request, 0, sizeof(request));
   1446 	request.magic = INIT_MAGIC;
   1447 	SCPYN(request.gen_id, gen_id);
   1448 	SCPYN(request.tty_id, &line[8]);
   1449 	SCPYN(request.host, host);
   1450 	SCPYN(request.term_type, terminaltype ? terminaltype : "network");
   1451 	request.signal = SIGCLD;
   1452 	request.pid = getpid();
   1453 #ifdef BFTPDAEMON
   1454 	/*
   1455 	 * Are we working as the bftp daemon?
   1456 	 */
   1457 	if (bftpd) {
   1458 		SCPYN(request.exec_name, BFTPPATH);
   1459 	}
   1460 #endif /* BFTPDAEMON */
   1461 	if (write(i, (char *)&request, sizeof(request)) < 0) {
   1462 		char tbuf[128];
   1463 
   1464 		(void)snprintf(tbuf, sizeof tbuf, "Can't write to %s\n", INIT_FIFO);
   1465 		fatalperror(net, tbuf);
   1466 	}
   1467 	(void) close(i);
   1468 	(void) signal(SIGALRM, nologinproc);
   1469 	for (i = 0; ; i++) {
   1470 		char tbuf[128];
   1471 
   1472 		alarm(15);
   1473 		n = read(pty, ptyip, BUFSIZ);
   1474 		if (i == 3 || n >= 0 || !gotalarm)
   1475 			break;
   1476 		gotalarm = 0;
   1477 		(void)snprintf(tbuf, sizeof tbuf,
   1478 		    "telnetd: waiting for /etc/init to start login process on %s\r\n", line);
   1479 		(void)write(net, tbuf, strlen(tbuf));
   1480 	}
   1481 	if (n < 0 && gotalarm)
   1482 		fatal(net, "/etc/init didn't start login process");
   1483 	pcc += n;
   1484 	alarm(0);
   1485 	(void) signal(SIGALRM, SIG_DFL);
   1486 
   1487 	return;
   1488 #endif	/* NEWINIT */
   1489 }
   1490 
   1491 char	*envinit[3];
   1492 
   1493 	void
   1494 init_env()
   1495 {
   1496 	char **envp;
   1497 
   1498 	envp = envinit;
   1499 	if ((*envp = getenv("TZ")))
   1500 		*envp++ -= 3;
   1501 #if	defined(CRAY) || defined(__hpux)
   1502 	else
   1503 		*envp++ = "TZ=GMT0";
   1504 #endif
   1505 	*envp = 0;
   1506 	environ = envinit;
   1507 }
   1508 
   1509 #ifndef	NEWINIT
   1510 
   1511 /*
   1512  * start_login(host)
   1513  *
   1514  * Assuming that we are now running as a child processes, this
   1515  * function will turn us into the login process.
   1516  */
   1517 extern char *gettyname;
   1518 
   1519 	void
   1520 start_login(host, autologin, name)
   1521 	char *host;
   1522 	int autologin;
   1523 	char *name;
   1524 {
   1525 	register char **argv;
   1526 #define	TABBUFSIZ	512
   1527 	char	defent[TABBUFSIZ];
   1528 	char	defstrs[TABBUFSIZ];
   1529 #undef	TABBUFSIZ
   1530 	const char *loginprog = NULL;
   1531 #ifdef	UTMPX
   1532 	register int pid = getpid();
   1533 	struct utmpx utmpx;
   1534 #endif
   1535 #ifdef SOLARIS
   1536 	char *term;
   1537 	char termnamebuf[64];
   1538 #endif
   1539 
   1540 #ifdef	UTMPX
   1541 	/*
   1542 	 * Create utmp entry for child
   1543 	 */
   1544 
   1545 	memset(&utmpx, 0, sizeof(utmpx));
   1546 	SCPYN(utmpx.ut_user, ".telnet");
   1547 	SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1);
   1548 	utmpx.ut_pid = pid;
   1549 	utmpx.ut_id[0] = 't';
   1550 	utmpx.ut_id[1] = 'n';
   1551 	utmpx.ut_id[2] = SC_WILDC;
   1552 	utmpx.ut_id[3] = SC_WILDC;
   1553 	utmpx.ut_type = LOGIN_PROCESS;
   1554 	(void) time(&utmpx.ut_tv.tv_sec);
   1555 	if (makeutx(&utmpx) == NULL)
   1556 		fatal(net, "makeutx failed");
   1557 #endif
   1558 
   1559 	scrub_env();
   1560 
   1561 	/*
   1562 	 * -h : pass on name of host.
   1563 	 *		WARNING:  -h is accepted by login if and only if
   1564 	 *			getuid() == 0.
   1565 	 * -p : don't clobber the environment (so terminal type stays set).
   1566 	 *
   1567 	 * -f : force this login, he has already been authenticated
   1568 	 */
   1569 	argv = addarg(0, "login");
   1570 
   1571 #if	!defined(NO_LOGIN_H)
   1572 
   1573 # if	defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
   1574 	/*
   1575 	 * Don't add the "-h host" option if we are going
   1576 	 * to be adding the "-r host" option down below...
   1577 	 */
   1578 	if ((auth_level < 0) || (autologin != AUTH_VALID))
   1579 # endif
   1580 	{
   1581 		argv = addarg(argv, "-h");
   1582 		argv = addarg(argv, host);
   1583 #ifdef	SOLARIS
   1584 		/*
   1585 		 * SVR4 version of -h takes TERM= as second arg, or -
   1586 		 */
   1587 		term = getenv("TERM");
   1588 		if (term == NULL || term[0] == 0) {
   1589 			term = "-";
   1590 		} else {
   1591 			(void)strlcpy(termnamebuf, "TERM=",
   1592 			    sizeof(termnamebuf));
   1593 			(void)strlcat(termnamebuf, term, sizeof(termnamebuf));
   1594 			term = termnamebuf;
   1595 		}
   1596 		argv = addarg(argv, term);
   1597 #endif
   1598 	}
   1599 #endif
   1600 #if	!defined(NO_LOGIN_P)
   1601 	argv = addarg(argv, "-p");
   1602 #endif
   1603 #ifdef	LINEMODE
   1604 	/*
   1605 	 * Set the environment variable "LINEMODE" to either
   1606 	 * "real" or "kludge" if we are operating in either
   1607 	 * real or kludge linemode.
   1608 	 */
   1609 	if (lmodetype == REAL_LINEMODE)
   1610 		setenv("LINEMODE", "real", 1);
   1611 # ifdef KLUDGELINEMODE
   1612 	else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
   1613 		setenv("LINEMODE", "kludge", 1);
   1614 # endif
   1615 #endif
   1616 #ifdef	BFTPDAEMON
   1617 	/*
   1618 	 * Are we working as the bftp daemon?  If so, then ask login
   1619 	 * to start bftp instead of shell.
   1620 	 */
   1621 	if (bftpd) {
   1622 		argv = addarg(argv, "-e");
   1623 		argv = addarg(argv, BFTPPATH);
   1624 	} else
   1625 #endif
   1626 #if	defined (SECURELOGIN)
   1627 	/*
   1628 	 * don't worry about the -f that might get sent.
   1629 	 * A -s is supposed to override it anyhow.
   1630 	 */
   1631 	if (require_secure_login)
   1632 		argv = addarg(argv, "-s");
   1633 #endif
   1634 #if	defined (AUTHENTICATION)
   1635 	if (auth_level >= 0 && autologin == AUTH_VALID) {
   1636 # if	!defined(NO_LOGIN_F)
   1637 #  if	defined(FORWARD)
   1638 		if (got_forwarded_creds)
   1639 			argv = addarg(argv, "-F");
   1640 		else
   1641 #  endif /* FORWARD */
   1642 		argv = addarg(argv, "-f");
   1643 		argv = addarg(argv, "--");
   1644 		argv = addarg(argv, name);
   1645 # else
   1646 #  if defined(LOGIN_R)
   1647 		/*
   1648 		 * We don't have support for "login -f", but we
   1649 		 * can fool /bin/login into thinking that we are
   1650 		 * rlogind, and allow us to log in without a
   1651 		 * password.  The rlogin protocol expects
   1652 		 *	local-user\0remote-user\0term/speed\0
   1653 		 */
   1654 
   1655 		if (pty > 2) {
   1656 			register char *cp;
   1657 			char speed[128];
   1658 			int isecho, israw, xpty, len;
   1659 			extern int def_rspeed;
   1660 #  ifndef LOGIN_HOST
   1661 			/*
   1662 			 * Tell login that we are coming from "localhost".
   1663 			 * If we passed in the real host name, then the
   1664 			 * user would have to allow .rhost access from
   1665 			 * every machine that they want authenticated
   1666 			 * access to work from, which sort of defeats
   1667 			 * the purpose of an authenticated login...
   1668 			 * So, we tell login that the session is coming
   1669 			 * from "localhost", and the user will only have
   1670 			 * to have "localhost" in their .rhost file.
   1671 			 */
   1672 #			define LOGIN_HOST "localhost"
   1673 #  endif
   1674 			argv = addarg(argv, "-r");
   1675 			argv = addarg(argv, LOGIN_HOST);
   1676 
   1677 			xpty = pty;
   1678 # ifndef  STREAMSPTY
   1679 			pty = 0;
   1680 # else
   1681 			ttyfd = 0;
   1682 # endif
   1683 			init_termbuf();
   1684 			isecho = tty_isecho();
   1685 			israw = tty_israw();
   1686 			if (isecho || !israw) {
   1687 				tty_setecho(0);		/* Turn off echo */
   1688 				tty_setraw(1);		/* Turn on raw */
   1689 				set_termbuf();
   1690 			}
   1691 			len = strlen(name)+1;
   1692 			write(xpty, name, len);
   1693 			write(xpty, name, len);
   1694 			sprintf(speed, "%s/%d", (cp = getenv("TERM")) ? cp : "",
   1695 				(def_rspeed > 0) ? def_rspeed : 9600);
   1696 			len = strlen(speed)+1;
   1697 			write(xpty, speed, len);
   1698 
   1699 			if (isecho || !israw) {
   1700 				init_termbuf();
   1701 				tty_setecho(isecho);
   1702 				tty_setraw(israw);
   1703 				set_termbuf();
   1704 				if (!israw) {
   1705 					/*
   1706 					 * Write a newline to ensure
   1707 					 * that login will be able to
   1708 					 * read the line...
   1709 					 */
   1710 					write(xpty, "\n", 1);
   1711 				}
   1712 			}
   1713 			pty = xpty;
   1714 		}
   1715 #  else
   1716 		argv = addarg(argv, "--");
   1717 		argv = addarg(argv, name);
   1718 #  endif
   1719 # endif
   1720 	} else
   1721 #endif
   1722 	if (getenv("USER")) {
   1723 		argv = addarg(argv, "--");
   1724 		argv = addarg(argv, getenv("USER"));
   1725 #if	defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
   1726 		{
   1727 			register char **cpp;
   1728 			for (cpp = environ; *cpp; cpp++)
   1729 				argv = addarg(argv, *cpp);
   1730 		}
   1731 #endif
   1732 		/*
   1733 		 * Assume that login will set the USER variable
   1734 		 * correctly.  For SysV systems, this means that
   1735 		 * USER will no longer be set, just LOGNAME by
   1736 		 * login.  (The problem is that if the auto-login
   1737 		 * fails, and the user then specifies a different
   1738 		 * account name, he can get logged in with both
   1739 		 * LOGNAME and USER in his environment, but the
   1740 		 * USER value will be wrong.
   1741 		 */
   1742 		unsetenv("USER");
   1743 	}
   1744 #ifdef	SOLARIS
   1745 	else {
   1746 		char **p;
   1747 
   1748 		argv = addarg(argv, "");	/* no login name */
   1749 		for (p = environ; *p; p++) {
   1750 			argv = addarg(argv, *p);
   1751 		}
   1752 	}
   1753 #endif	/* SOLARIS */
   1754 #if	defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
   1755 	if (pty > 2)
   1756 		close(pty);
   1757 #endif
   1758         if (getent(defent, gettyname) == 1) {
   1759                 char *cp = defstrs;
   1760 
   1761                 loginprog = getstr("lo", &cp);
   1762         }
   1763         if (loginprog == NULL)
   1764                 loginprog = _PATH_LOGIN;
   1765 	closelog();
   1766 	/*
   1767 	 * This sleep(1) is in here so that telnetd can
   1768 	 * finish up with the tty.  There's a race condition
   1769 	 * the login banner message gets lost...
   1770 	 */
   1771 	sleep(1);
   1772         execv(loginprog, argv);
   1773 
   1774         syslog(LOG_ERR, "%s: %m", loginprog);
   1775         fatalperror(net, loginprog);
   1776 	/*NOTREACHED*/
   1777 }
   1778 
   1779 	char **
   1780 addarg(argv, val)
   1781 	register char **argv;
   1782 	register char *val;
   1783 {
   1784 	register char **cpp;
   1785 
   1786 	if (argv == NULL) {
   1787 		/*
   1788 		 * 10 entries, a leading length, and a null
   1789 		 */
   1790 		argv = (char **)malloc(sizeof(*argv) * 12);
   1791 		if (argv == NULL)
   1792 			return(NULL);
   1793 		*argv++ = (char *)10;
   1794 		*argv = (char *)0;
   1795 	}
   1796 	for (cpp = argv; *cpp; cpp++)
   1797 		;
   1798 	if (cpp == &argv[(long)argv[-1]]) {
   1799 		--argv;
   1800 		*argv = (char *)((long)(*argv) + 10);
   1801 		argv = (char **)realloc(argv, sizeof(*argv) * ((long)(*argv) + 2));
   1802 		if (argv == NULL) {
   1803 			fatal(net, "not enough memory");
   1804 			/*NOTREACHED*/
   1805 		}
   1806 		argv++;
   1807 		cpp = &argv[(long)argv[-1] - 10];
   1808 	}
   1809 	*cpp++ = val;
   1810 	*cpp = 0;
   1811 	return(argv);
   1812 }
   1813 #endif	/* NEWINIT */
   1814 
   1815 /*
   1816  * scrub_env()
   1817  *
   1818  * We only accept the environment variables listed below.
   1819  */
   1820 
   1821 void
   1822 scrub_env()
   1823 {
   1824 	static const char *reject[] = {
   1825 		"TERMCAP=/",
   1826 		NULL
   1827 	};
   1828 
   1829 	static const char *acceptstr[] = {
   1830 		"XAUTH=", "XAUTHORITY=", "DISPLAY=",
   1831 		"TERM=",
   1832 		"EDITOR=",
   1833 		"PAGER=",
   1834 		"LOGNAME=",
   1835 		"POSIXLY_CORRECT=",
   1836 		"TERMCAP=",
   1837 		"PRINTER=",
   1838 		NULL
   1839 	};
   1840 
   1841 	char **cpp, **cpp2;
   1842 	const char **p;
   1843 
   1844 	for (cpp2 = cpp = environ; *cpp; cpp++) {
   1845 		int reject_it = 0;
   1846 
   1847 		for(p = reject; *p; p++)
   1848 			if(strncmp(*cpp, *p, strlen(*p)) == 0) {
   1849 				reject_it = 1;
   1850 				break;
   1851 			}
   1852 		if (reject_it)
   1853 			continue;
   1854 
   1855 		for(p = acceptstr; *p; p++)
   1856 			if(strncmp(*cpp, *p, strlen(*p)) == 0)
   1857 				break;
   1858 		if(*p != NULL)
   1859 			*cpp2++ = *cpp;
   1860 	}
   1861 	*cpp2 = NULL;
   1862 }
   1863 
   1864 /*
   1865  * cleanup()
   1866  *
   1867  * This is the routine to call when we are all through, to
   1868  * clean up anything that needs to be cleaned up.
   1869  */
   1870 	/* ARGSUSED */
   1871 	void
   1872 cleanup(sig)
   1873 	int sig;
   1874 {
   1875 #ifndef	PARENT_DOES_UTMP
   1876 # if (BSD > 43) || defined(convex)
   1877 	char *p, c;
   1878 
   1879 	p = line + sizeof("/dev/") - 1;
   1880 	if (logout(p))
   1881 		logwtmp(p, "", "");
   1882 	(void)chmod(line, 0666);
   1883 	(void)chown(line, 0, 0);
   1884 	c = *p; *p = 'p';
   1885 	(void)chmod(line, 0666);
   1886 	(void)chown(line, 0, 0);
   1887 	*p = c;
   1888 	if (ttyaction(line, "telnetd", "root"))
   1889 		syslog(LOG_ERR, "%s: ttyaction failed", line);
   1890 	(void) shutdown(net, 2);
   1891 	exit(1);
   1892 # else
   1893 	void rmut();
   1894 
   1895 	rmut();
   1896 	vhangup();	/* XXX */
   1897 	(void) shutdown(net, 2);
   1898 	exit(1);
   1899 # endif
   1900 #else	/* PARENT_DOES_UTMP */
   1901 # ifdef	NEWINIT
   1902 	(void) shutdown(net, 2);
   1903 	exit(1);
   1904 # else	/* NEWINIT */
   1905 #  ifdef CRAY
   1906 	static int incleanup = 0;
   1907 	register int t;
   1908 	int child_status; /* status of child process as returned by waitpid */
   1909 	int flags = WNOHANG|WUNTRACED;
   1910 
   1911 	/*
   1912 	 * 1: Pick up the zombie, if we are being called
   1913 	 *    as the signal handler.
   1914 	 * 2: If we are a nested cleanup(), return.
   1915 	 * 3: Try to clean up TMPDIR.
   1916 	 * 4: Fill in utmp with shutdown of process.
   1917 	 * 5: Close down the network and pty connections.
   1918 	 * 6: Finish up the TMPDIR cleanup, if needed.
   1919 	 */
   1920 	if (sig == SIGCHLD) {
   1921 		while (waitpid(-1, &child_status, flags) > 0)
   1922 			;	/* VOID */
   1923 		/* Check if the child process was stopped
   1924 		 * rather than exited.  We want cleanup only if
   1925 		 * the child has died.
   1926 		 */
   1927 		if (WIFSTOPPED(child_status)) {
   1928 			return;
   1929 		}
   1930 	}
   1931 	t = sigblock(sigmask(SIGCHLD));
   1932 	if (incleanup) {
   1933 		sigsetmask(t);
   1934 		return;
   1935 	}
   1936 	incleanup = 1;
   1937 	sigsetmask(t);
   1938 
   1939 	t = cleantmp(&wtmp);
   1940 	setutent();	/* just to make sure */
   1941 #  endif /* CRAY */
   1942 	rmut(line);
   1943 	close(pty);
   1944 #ifdef KRB5
   1945 	kerberos5_cleanup();
   1946 #endif
   1947 	(void) shutdown(net, 2);
   1948 #  ifdef CRAY
   1949 	if (t == 0)
   1950 		cleantmp(&wtmp);
   1951 #  endif /* CRAY */
   1952 	exit(1);
   1953 # endif	/* NEWINT */
   1954 #endif	/* PARENT_DOES_UTMP */
   1955 }
   1956 
   1957 #if defined(PARENT_DOES_UTMP) && !defined(NEWINIT)
   1958 /*
   1959  * _utmp_sig_rcv
   1960  * utmp_sig_init
   1961  * utmp_sig_wait
   1962  *	These three functions are used to coordinate the handling of
   1963  *	the utmp file between the server and the soon-to-be-login shell.
   1964  *	The server actually creates the utmp structure, the child calls
   1965  *	utmp_sig_wait(), until the server calls utmp_sig_notify() and
   1966  *	signals the future-login shell to proceed.
   1967  */
   1968 static int caught=0;		/* NZ when signal intercepted */
   1969 static void (*func)();		/* address of previous handler */
   1970 
   1971 	void
   1972 _utmp_sig_rcv(sig)
   1973 	int sig;
   1974 {
   1975 	caught = 1;
   1976 	(void) signal(SIGUSR1, func);
   1977 }
   1978 
   1979 	void
   1980 utmp_sig_init()
   1981 {
   1982 	/*
   1983 	 * register signal handler for UTMP creation
   1984 	 */
   1985 	if ((int)(func = signal(SIGUSR1, _utmp_sig_rcv)) == -1)
   1986 		fatalperror(net, "telnetd/signal");
   1987 }
   1988 
   1989 	void
   1990 utmp_sig_reset()
   1991 {
   1992 	(void) signal(SIGUSR1, func);	/* reset handler to default */
   1993 }
   1994 
   1995 # ifdef __hpux
   1996 # define sigoff() /* do nothing */
   1997 # define sigon() /* do nothing */
   1998 # endif
   1999 
   2000 	void
   2001 utmp_sig_wait()
   2002 {
   2003 	/*
   2004 	 * Wait for parent to write our utmp entry.
   2005 	 */
   2006 	sigoff();
   2007 	while (caught == 0) {
   2008 		pause();	/* wait until we get a signal (sigon) */
   2009 		sigoff();	/* turn off signals while we check caught */
   2010 	}
   2011 	sigon();		/* turn on signals again */
   2012 }
   2013 
   2014 	void
   2015 utmp_sig_notify(pid)
   2016 {
   2017 	kill(pid, SIGUSR1);
   2018 }
   2019 
   2020 # ifdef CRAY
   2021 static int gotsigjob = 0;
   2022 
   2023 	/*ARGSUSED*/
   2024 	void
   2025 sigjob(sig)
   2026 	int sig;
   2027 {
   2028 	register int jid;
   2029 	register struct jobtemp *jp;
   2030 
   2031 	while ((jid = waitjob(NULL)) != -1) {
   2032 		if (jid == 0) {
   2033 			return;
   2034 		}
   2035 		gotsigjob++;
   2036 		jobend(jid, NULL, NULL);
   2037 	}
   2038 }
   2039 
   2040 /*
   2041  *	jid_getutid:
   2042  *		called by jobend() before calling cleantmp()
   2043  *		to find the correct $TMPDIR to cleanup.
   2044  */
   2045 
   2046 	struct utmp *
   2047 jid_getutid(jid)
   2048 	int jid;
   2049 {
   2050 	struct utmp *cur = NULL;
   2051 
   2052 	setutent();	/* just to make sure */
   2053 	while (cur = getutent()) {
   2054 		if ( (cur->ut_type != NULL) && (jid == cur->ut_jid) ) {
   2055 			return(cur);
   2056 		}
   2057 	}
   2058 
   2059 	return(0);
   2060 }
   2061 
   2062 /*
   2063  * Clean up the TMPDIR that login created.
   2064  * The first time this is called we pick up the info
   2065  * from the utmp.  If the job has already gone away,
   2066  * then we'll clean up and be done.  If not, then
   2067  * when this is called the second time it will wait
   2068  * for the signal that the job is done.
   2069  */
   2070 	int
   2071 cleantmp(wtp)
   2072 	register struct utmp *wtp;
   2073 {
   2074 	struct utmp *utp;
   2075 	static int first = 1;
   2076 	register int mask, omask, ret;
   2077 	extern struct utmp *getutid P((const struct utmp *_Id));
   2078 
   2079 
   2080 	mask = sigmask(WJSIGNAL);
   2081 
   2082 	if (first == 0) {
   2083 		omask = sigblock(mask);
   2084 		while (gotsigjob == 0)
   2085 			sigpause(omask);
   2086 		return(1);
   2087 	}
   2088 	first = 0;
   2089 	setutent();	/* just to make sure */
   2090 
   2091 	utp = getutid(wtp);
   2092 	if (utp == 0) {
   2093 		syslog(LOG_WARNING,
   2094 		    "Can't get /etc/utmp entry to clean TMPDIR");
   2095 		return(-1);
   2096 	}
   2097 	/*
   2098 	 * Nothing to clean up if the user shell was never started.
   2099 	 */
   2100 	if (utp->ut_type != USER_PROCESS || utp->ut_jid == 0)
   2101 		return(1);
   2102 
   2103 	/*
   2104 	 * Block the WJSIGNAL while we are in jobend().
   2105 	 */
   2106 	omask = sigblock(mask);
   2107 	ret = jobend(utp->ut_jid, utp->ut_tpath, utp->ut_user);
   2108 	sigsetmask(omask);
   2109 	return(ret);
   2110 }
   2111 
   2112 	int
   2113 jobend(jid, path, user)
   2114 	register int jid;
   2115 	register char *path;
   2116 	register char *user;
   2117 {
   2118 	static int saved_jid = 0;
   2119 	static int pty_saved_jid = 0;
   2120 	static char saved_path[sizeof(wtmp.ut_tpath)+1];
   2121 	static char saved_user[sizeof(wtmp.ut_user)+1];
   2122 
   2123 	/*
   2124 	 * this little piece of code comes into play
   2125 	 * only when ptyreconnect is used to reconnect
   2126 	 * to an previous session.
   2127 	 *
   2128 	 * this is the only time when the
   2129 	 * "saved_jid != jid" code is executed.
   2130 	 */
   2131 
   2132 	if ( saved_jid && saved_jid != jid ) {
   2133 		if (!path) {	/* called from signal handler */
   2134 			pty_saved_jid = jid;
   2135 		} else {
   2136 			pty_saved_jid = saved_jid;
   2137 		}
   2138 	}
   2139 
   2140 	if (path) {
   2141 		strncpy(saved_path, path, sizeof(wtmp.ut_tpath));
   2142 		strncpy(saved_user, user, sizeof(wtmp.ut_user));
   2143 		saved_path[sizeof(saved_path)] = '\0';
   2144 		saved_user[sizeof(saved_user)] = '\0';
   2145 	}
   2146 	if (saved_jid == 0) {
   2147 		saved_jid = jid;
   2148 		return(0);
   2149 	}
   2150 
   2151 	/* if the jid has changed, get the correct entry from the utmp file */
   2152 
   2153 	if ( saved_jid != jid ) {
   2154 		struct utmp *utp = NULL;
   2155 		struct utmp *jid_getutid();
   2156 
   2157 		utp = jid_getutid(pty_saved_jid);
   2158 
   2159 		if (utp == 0) {
   2160 			syslog(LOG_WARNING,
   2161 			    "Can't get /etc/utmp entry to clean TMPDIR");
   2162 			return(-1);
   2163 		}
   2164 
   2165 		cleantmpdir(jid, utp->ut_tpath, utp->ut_user);
   2166 		return(1);
   2167 	}
   2168 
   2169 	cleantmpdir(jid, saved_path, saved_user);
   2170 	return(1);
   2171 }
   2172 
   2173 /*
   2174  * Fork a child process to clean up the TMPDIR
   2175  */
   2176 cleantmpdir(jid, tpath, user)
   2177 	register int jid;
   2178 	register char *tpath;
   2179 	register char *user;
   2180 {
   2181 	switch(fork()) {
   2182 	case -1:
   2183 		syslog(LOG_WARNING, "TMPDIR cleanup(%s): fork() failed: %m",
   2184 		    tpath);
   2185 		break;
   2186 	case 0:
   2187 		execl(CLEANTMPCMD, CLEANTMPCMD, user, tpath, NULL);
   2188 		syslog(LOG_ERR, "TMPDIR cleanup(%s): execl(%s) failed: %m",
   2189 		    tpath, CLEANTMPCMD);
   2190 		exit(1);
   2191 	default:
   2192 		/*
   2193 		 * Forget about child.  We will exit, and
   2194 		 * /etc/init will pick it up.
   2195 		 */
   2196 		break;
   2197 	}
   2198 }
   2199 # endif /* CRAY */
   2200 #endif	/* defined(PARENT_DOES_UTMP) && !defined(NEWINIT) */
   2201 
   2202 /*
   2203  * rmut()
   2204  *
   2205  * This is the function called by cleanup() to
   2206  * remove the utmp entry for this person.
   2207  */
   2208 
   2209 #ifdef	UTMPX
   2210 	void
   2211 rmut()
   2212 {
   2213 	register f;
   2214 	int found = 0;
   2215 	struct utmp *u, *utmp;
   2216 	int nutmp;
   2217 	struct stat statbf;
   2218 
   2219 	struct utmpx *utxp, utmpx;
   2220 
   2221 	/*
   2222 	 * This updates the utmpx and utmp entries and make a wtmp/x entry
   2223 	 */
   2224 
   2225 	SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1);
   2226 	utxp = getutxline(&utmpx);
   2227 	if (utxp) {
   2228 		utxp->ut_type = DEAD_PROCESS;
   2229 		utxp->ut_exit.e_termination = 0;
   2230 		utxp->ut_exit.e_exit = 0;
   2231 		(void) time(&utmpx.ut_tv.tv_sec);
   2232 		utmpx.ut_tv.tv_usec = 0;
   2233 		modutx(utxp);
   2234 	}
   2235 	endutxent();
   2236 }  /* end of rmut */
   2237 #endif
   2238 
   2239 #if	!defined(UTMPX) && !(defined(CRAY) || defined(__hpux)) && BSD <= 43
   2240 	void
   2241 rmut()
   2242 {
   2243 	register f;
   2244 	int found = 0;
   2245 	struct utmp *u, *utmp;
   2246 	int nutmp;
   2247 	struct stat statbf;
   2248 
   2249 	f = open(utmpf, O_RDWR);
   2250 	if (f >= 0) {
   2251 		(void) fstat(f, &statbf);
   2252 		utmp = (struct utmp *)malloc((unsigned)statbf.st_size);
   2253 		if (!utmp)
   2254 			syslog(LOG_WARNING, "utmp malloc failed");
   2255 		if (statbf.st_size && utmp) {
   2256 			nutmp = read(f, (char *)utmp, (int)statbf.st_size);
   2257 			nutmp /= sizeof(struct utmp);
   2258 
   2259 			for (u = utmp ; u < &utmp[nutmp] ; u++) {
   2260 				if (SCMPN(u->ut_line, line+5) ||
   2261 				    u->ut_name[0]==0)
   2262 					continue;
   2263 				(void)lseek(f, (off_t)((long)u)-((long)utmp),
   2264 				    SEEK_SET);
   2265 				SCPYN(u->ut_name, "");
   2266 				SCPYN(u->ut_host, "");
   2267 				(void) time(&u->ut_time);
   2268 				(void) write(f, (char *)u, sizeof(wtmp));
   2269 				found++;
   2270 			}
   2271 		}
   2272 		(void) close(f);
   2273 	}
   2274 	if (found) {
   2275 		f = open(wtmpf, O_WRONLY|O_APPEND);
   2276 		if (f >= 0) {
   2277 			SCPYN(wtmp.ut_line, line+5);
   2278 			SCPYN(wtmp.ut_name, "");
   2279 			SCPYN(wtmp.ut_host, "");
   2280 			(void) time(&wtmp.ut_time);
   2281 			(void) write(f, (char *)&wtmp, sizeof(wtmp));
   2282 			(void) close(f);
   2283 		}
   2284 	}
   2285 	(void) chmod(line, 0666);
   2286 	(void) chown(line, 0, 0);
   2287 	line[strlen("/dev/")] = 'p';
   2288 	(void) chmod(line, 0666);
   2289 	(void) chown(line, 0, 0);
   2290 }  /* end of rmut */
   2291 #endif	/* CRAY */
   2292 
   2293 #ifdef __hpux
   2294 rmut (line)
   2295 char *line;
   2296 {
   2297 	struct utmp utmp;
   2298 	struct utmp *utptr;
   2299 	int fd;			/* for /etc/wtmp */
   2300 
   2301 	utmp.ut_type = USER_PROCESS;
   2302 	(void) strncpy(utmp.ut_id, line+12, sizeof(utmp.ut_id));
   2303 	(void) setutent();
   2304 	utptr = getutid(&utmp);
   2305 	/* write it out only if it exists */
   2306 	if (utptr) {
   2307 		utptr->ut_type = DEAD_PROCESS;
   2308 		utptr->ut_time = time((long *) 0);
   2309 		(void) pututline(utptr);
   2310 		/* set wtmp entry if wtmp file exists */
   2311 		if ((fd = open(wtmpf, O_WRONLY | O_APPEND)) >= 0) {
   2312 			(void) write(fd, utptr, sizeof(utmp));
   2313 			(void) close(fd);
   2314 		}
   2315 	}
   2316 	(void) endutent();
   2317 
   2318 	(void) chmod(line, 0666);
   2319 	(void) chown(line, 0, 0);
   2320 	line[14] = line[13];
   2321 	line[13] = line[12];
   2322 	line[8] = 'm';
   2323 	line[9] = '/';
   2324 	line[10] = 'p';
   2325 	line[11] = 't';
   2326 	line[12] = 'y';
   2327 	(void) chmod(line, 0666);
   2328 	(void) chown(line, 0, 0);
   2329 }
   2330 #endif
   2331