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