xref: /csrg-svn/libexec/telnetd/telnetd.c (revision 11221)
16295Sroot #ifndef lint
2*11221Ssam static char sccsid[] = "@(#)telnetd.c	4.18 83/02/21";
36295Sroot #endif
46295Sroot 
56002Sroot /*
66002Sroot  * Stripped-down telnet server.
76002Sroot  */
89218Ssam #include <sys/types.h>
99218Ssam #include <sys/socket.h>
109218Ssam 
119218Ssam #include <netinet/in.h>
129218Ssam 
136002Sroot #include <stdio.h>
146002Sroot #include <signal.h>
156002Sroot #include <errno.h>
166002Sroot #include <sgtty.h>
176002Sroot #include <wait.h>
188346Ssam #include <netdb.h>
199218Ssam 
206002Sroot #include "telnet.h"
216002Sroot 
226002Sroot #define	BELL		'\07'
236002Sroot 
246002Sroot char	hisopts[256];
256002Sroot char	myopts[256];
266002Sroot 
276002Sroot char	doopt[] = { IAC, DO, '%', 'c', 0 };
286002Sroot char	dont[] = { IAC, DONT, '%', 'c', 0 };
296002Sroot char	will[] = { IAC, WILL, '%', 'c', 0 };
306002Sroot char	wont[] = { IAC, WONT, '%', 'c', 0 };
316002Sroot 
326002Sroot /*
336002Sroot  * I/O data buffers, pointers, and counters.
346002Sroot  */
356002Sroot char	ptyibuf[BUFSIZ], *ptyip = ptyibuf;
366002Sroot char	ptyobuf[BUFSIZ], *pfrontp = ptyobuf, *pbackp = ptyobuf;
376002Sroot char	netibuf[BUFSIZ], *netip = netibuf;
386388Ssam char	netobuf[BUFSIZ], *nfrontp = netobuf, *nbackp = netobuf;
396002Sroot int	pcc, ncc;
406002Sroot 
416002Sroot int	pty, net;
426002Sroot int	inter;
4310418Ssam int	reapchild();
446002Sroot extern	int errno;
456002Sroot char	line[] = "/dev/ptyp0";
466002Sroot 
478346Ssam struct	sockaddr_in sin = { AF_INET };
486002Sroot 
496002Sroot main(argc, argv)
506002Sroot 	char *argv[];
516002Sroot {
5210418Ssam 	int s, pid, options;
538346Ssam 	struct servent *sp;
546002Sroot 
558346Ssam 	sp = getservbyname("telnet", "tcp");
568346Ssam 	if (sp == 0) {
578346Ssam 		fprintf(stderr, "telnetd: tcp/telnet: unknown service\n");
588346Ssam 		exit(1);
598346Ssam 	}
608346Ssam 	sin.sin_port = sp->s_port;
616002Sroot 	argc--, argv++;
6210418Ssam 	if (argc > 0 && !strcmp(*argv, "-d")) {
6310418Ssam 		options |= SO_DEBUG;
6410418Ssam 		argc--, argv++;
6510418Ssam 	}
668346Ssam 	if (argc > 0) {
678346Ssam 		sin.sin_port = atoi(*argv);
688346Ssam 		if (sin.sin_port <= 0) {
698346Ssam 			fprintf(stderr, "telnetd: %s: bad port #\n", *argv);
708346Ssam 			exit(1);
718346Ssam 		}
729969Ssam 		sin.sin_port = htons((u_short)sin.sin_port);
738346Ssam 	}
748456Ssam #ifndef DEBUG
758456Ssam 	if (fork())
768456Ssam 		exit(0);
778456Ssam 	for (s = 0; s < 10; s++)
788456Ssam 		(void) close(s);
798456Ssam 	(void) open("/", 0);
808456Ssam 	(void) dup2(0, 1);
818456Ssam 	(void) dup2(0, 2);
828456Ssam 	{ int tt = open("/dev/tty", 2);
838456Ssam 	  if (tt > 0) {
848456Ssam 		ioctl(tt, TIOCNOTTY, 0);
858456Ssam 		close(tt);
868456Ssam 	  }
878456Ssam 	}
888456Ssam #endif
899218Ssam again:
909288Ssam 	s = socket(AF_INET, SOCK_STREAM, 0, 0);
919218Ssam 	if (s < 0) {
929218Ssam 		perror("telnetd: socket");;
939218Ssam 		sleep(5);
949218Ssam 		goto again;
959218Ssam 	}
9610418Ssam 	if (options & SO_DEBUG)
9710418Ssam 		if (setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
9810418Ssam 			perror("telnetd: setsockopt (SO_DEBUG)");
9910418Ssam #ifdef notdef
10010418Ssam 	if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
10110418Ssam 		perror("telnetd: setsockopt (SO_KEEPALIVE)");
10210418Ssam #endif
1039218Ssam 	while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
1049218Ssam 		perror("telnetd: bind");
1059218Ssam 		sleep(5);
1069218Ssam 	}
10710587Ssam 	sigset(SIGCHLD, reapchild);
1089218Ssam 	listen(s, 10);
1096002Sroot 	for (;;) {
1109218Ssam 		int s2;
1119218Ssam 
1129244Ssam 		s2 = accept(s, (caddr_t)0, 0, 0);
1139218Ssam 		if (s2 < 0) {
11410418Ssam 			if (errno == EINTR)
11510418Ssam 				continue;
1169244Ssam 			perror("telnetd: accept");
1176002Sroot 			sleep(1);
1186002Sroot 			continue;
1196002Sroot 		}
1206002Sroot 		if ((pid = fork()) < 0)
1216002Sroot 			printf("Out of processes\n");
122*11221Ssam 		else if (pid == 0) {
123*11221Ssam 			signal(SIGCHLD, SIG_IGN);
1249218Ssam 			doit(s2);
125*11221Ssam 		}
1269218Ssam 		close(s2);
1276002Sroot 	}
1286002Sroot 	/*NOTREACHED*/
1296002Sroot }
1306002Sroot 
13110418Ssam reapchild()
13210418Ssam {
13310418Ssam 	union wait status;
13410418Ssam 
13510418Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
13610418Ssam 		;
13710418Ssam }
13810418Ssam 
1396002Sroot int	cleanup();
1406002Sroot 
1416002Sroot /*
1426002Sroot  * Get a pty, scan input lines.
1436002Sroot  */
1446002Sroot doit(f)
1456002Sroot {
1466002Sroot 	char *cp = line;
1476002Sroot 	int i, p, cc, t;
1486002Sroot 	struct sgttyb b;
1496002Sroot 
1506002Sroot 	for (i = 0; i < 16; i++) {
1516002Sroot 		cp[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1526002Sroot 		p = open(cp, 2);
1536002Sroot 		if (p > 0)
1546002Sroot 			goto gotpty;
1556002Sroot 	}
1569244Ssam 	fatal(f, "All network ports in use");
1579244Ssam 	/*NOTREACHED*/
1586002Sroot gotpty:
1596002Sroot 	dup2(f, 0);
1606002Sroot 	cp[strlen("/dev/")] = 't';
1616002Sroot 	t = open("/dev/tty", 2);
1626002Sroot 	if (t >= 0) {
1636002Sroot 		ioctl(t, TIOCNOTTY, 0);
1646002Sroot 		close(t);
1656002Sroot 	}
1666002Sroot 	t = open(cp, 2);
1679244Ssam 	if (t < 0)
1689244Ssam 		fatalperror(f, cp, errno);
1696002Sroot 	ioctl(t, TIOCGETP, &b);
1706388Ssam 	b.sg_flags = CRMOD|XTABS|ANYP;
1716002Sroot 	ioctl(t, TIOCSETP, &b);
1726388Ssam 	ioctl(p, TIOCGETP, &b);
1738379Ssam 	b.sg_flags &= ~ECHO;
1746388Ssam 	ioctl(p, TIOCSETP, &b);
1759244Ssam 	if ((i = fork()) < 0)
1769244Ssam 		fatalperror(f, "fork", errno);
1776002Sroot 	if (i)
1786002Sroot 		telnet(f, p);
1796002Sroot 	close(f);
1806002Sroot 	close(p);
1816002Sroot 	dup2(t, 0);
1826002Sroot 	dup2(t, 1);
1836002Sroot 	dup2(t, 2);
1846002Sroot 	close(t);
1856002Sroot 	execl("/bin/login", "telnet-login", 0);
1869244Ssam 	fatalperror(f, "/bin/login", errno);
1879244Ssam 	/*NOTREACHED*/
1889244Ssam }
1899244Ssam 
1909244Ssam fatal(f, msg)
1919244Ssam 	int f;
1929244Ssam 	char *msg;
1939244Ssam {
1949244Ssam 	char buf[BUFSIZ];
1959244Ssam 
1969244Ssam 	(void) sprintf(buf, "telnetd: %s.\n", msg);
1979244Ssam 	(void) write(f, buf, strlen(buf));
1986002Sroot 	exit(1);
1996002Sroot }
2006002Sroot 
2019244Ssam fatalperror(f, msg, errno)
2029244Ssam 	int f;
2039244Ssam 	char *msg;
2049244Ssam 	int errno;
2059244Ssam {
2069244Ssam 	char buf[BUFSIZ];
2079244Ssam 	extern char *sys_errlist[];
2089244Ssam 
2099244Ssam 	(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
2109244Ssam 	fatal(f, buf);
2119244Ssam }
2129244Ssam 
2136002Sroot /*
2146002Sroot  * Main loop.  Select from pty and network, and
2156002Sroot  * hand data to telnet receiver finite state machine.
2166002Sroot  */
2176002Sroot telnet(f, p)
2186002Sroot {
2196002Sroot 	int on = 1;
2206002Sroot 
2216002Sroot 	net = f, pty = p;
2226002Sroot 	ioctl(f, FIONBIO, &on);
2236002Sroot 	ioctl(p, FIONBIO, &on);
2246002Sroot 	signal(SIGTSTP, SIG_IGN);
2256002Sroot 	sigset(SIGCHLD, cleanup);
2266002Sroot 
2278379Ssam 	/*
2288379Ssam 	 * Request to do remote echo.
2298379Ssam 	 */
2308379Ssam 	dooption(TELOPT_ECHO);
2318379Ssam 	myopts[TELOPT_ECHO] = 1;
2326002Sroot 	for (;;) {
2336002Sroot 		int ibits = 0, obits = 0;
2346002Sroot 		register int c;
2356002Sroot 
2366002Sroot 		/*
2376002Sroot 		 * Never look for input if there's still
2386002Sroot 		 * stuff in the corresponding output buffer
2396002Sroot 		 */
2406002Sroot 		if (nfrontp - nbackp)
2416002Sroot 			obits |= (1 << f);
2426002Sroot 		else
2436002Sroot 			ibits |= (1 << p);
2446002Sroot 		if (pfrontp - pbackp)
2456002Sroot 			obits |= (1 << p);
2466002Sroot 		else
2476002Sroot 			ibits |= (1 << f);
2486002Sroot 		if (ncc < 0 && pcc < 0)
2496002Sroot 			break;
2509218Ssam 		select(16, &ibits, &obits, 0, 0);
2516002Sroot 		if (ibits == 0 && obits == 0) {
2526002Sroot 			sleep(5);
2536002Sroot 			continue;
2546002Sroot 		}
2556002Sroot 
2566002Sroot 		/*
2576002Sroot 		 * Something to read from the network...
2586002Sroot 		 */
2596002Sroot 		if (ibits & (1 << f)) {
2606002Sroot 			ncc = read(f, netibuf, BUFSIZ);
2616002Sroot 			if (ncc < 0 && errno == EWOULDBLOCK)
2626002Sroot 				ncc = 0;
2636002Sroot 			else {
2646002Sroot 				if (ncc <= 0)
2656002Sroot 					break;
2666002Sroot 				netip = netibuf;
2676002Sroot 			}
2686002Sroot 		}
2696002Sroot 
2706002Sroot 		/*
2716002Sroot 		 * Something to read from the pty...
2726002Sroot 		 */
2736002Sroot 		if (ibits & (1 << p)) {
2746002Sroot 			pcc = read(p, ptyibuf, BUFSIZ);
2756002Sroot 			if (pcc < 0 && errno == EWOULDBLOCK)
2766002Sroot 				pcc = 0;
2776002Sroot 			else {
2786002Sroot 				if (pcc <= 0)
2796002Sroot 					break;
2806002Sroot 				ptyip = ptyibuf;
2816002Sroot 			}
2826002Sroot 		}
2836002Sroot 
2846002Sroot 		while (pcc > 0) {
2856002Sroot 			if ((&netobuf[BUFSIZ] - nfrontp) < 2)
2866002Sroot 				break;
2876002Sroot 			c = *ptyip++ & 0377, pcc--;
2886002Sroot 			if (c == IAC)
2896002Sroot 				*nfrontp++ = c;
2906002Sroot 			*nfrontp++ = c;
2916002Sroot 		}
2926002Sroot 		if ((obits & (1 << f)) && (nfrontp - nbackp) > 0)
2936002Sroot 			netflush();
2946002Sroot 		if (ncc > 0)
2956002Sroot 			telrcv();
2966002Sroot 		if ((obits & (1 << p)) && (pfrontp - pbackp) > 0)
2976002Sroot 			ptyflush();
2986002Sroot 	}
2996002Sroot 	cleanup();
3006002Sroot }
3016002Sroot 
3026002Sroot /*
3036002Sroot  * State for recv fsm
3046002Sroot  */
3056002Sroot #define	TS_DATA		0	/* base state */
3066002Sroot #define	TS_IAC		1	/* look for double IAC's */
3076002Sroot #define	TS_CR		2	/* CR-LF ->'s CR */
3086002Sroot #define	TS_BEGINNEG	3	/* throw away begin's... */
3096002Sroot #define	TS_ENDNEG	4	/* ...end's (suboption negotiation) */
3106002Sroot #define	TS_WILL		5	/* will option negotiation */
3116002Sroot #define	TS_WONT		6	/* wont " */
3126002Sroot #define	TS_DO		7	/* do " */
3136002Sroot #define	TS_DONT		8	/* dont " */
3146002Sroot 
3156002Sroot telrcv()
3166002Sroot {
3176002Sroot 	register int c;
3186002Sroot 	static int state = TS_DATA;
3196002Sroot 	struct sgttyb b;
3206002Sroot 
3216002Sroot 	while (ncc > 0) {
3226002Sroot 		if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
3236002Sroot 			return;
3246002Sroot 		c = *netip++ & 0377, ncc--;
3256002Sroot 		switch (state) {
3266002Sroot 
3276002Sroot 		case TS_DATA:
3286002Sroot 			if (c == IAC) {
3296002Sroot 				state = TS_IAC;
3306002Sroot 				break;
3316002Sroot 			}
3326002Sroot 			if (inter > 0)
3336002Sroot 				break;
3346002Sroot 			*pfrontp++ = c;
3356002Sroot 			if (!myopts[TELOPT_BINARY] && c == '\r')
3366002Sroot 				state = TS_CR;
3376002Sroot 			break;
3386002Sroot 
3396002Sroot 		case TS_CR:
3406002Sroot 			if (c && c != '\n')
3416002Sroot 				*pfrontp++ = c;
3426002Sroot 			state = TS_DATA;
3436002Sroot 			break;
3446002Sroot 
3456002Sroot 		case TS_IAC:
3466002Sroot 			switch (c) {
3476002Sroot 
3486002Sroot 			/*
3496002Sroot 			 * Send the process on the pty side an
3506002Sroot 			 * interrupt.  Do this with a NULL or
3516002Sroot 			 * interrupt char; depending on the tty mode.
3526002Sroot 			 */
3536002Sroot 			case BREAK:
3546002Sroot 			case IP:
3556002Sroot 				interrupt();
3566002Sroot 				break;
3576002Sroot 
3586002Sroot 			/*
3596002Sroot 			 * Are You There?
3606002Sroot 			 */
3616002Sroot 			case AYT:
3626002Sroot 				*pfrontp++ = BELL;
3636002Sroot 				break;
3646002Sroot 
3656002Sroot 			/*
3666002Sroot 			 * Erase Character and
3676002Sroot 			 * Erase Line
3686002Sroot 			 */
3696002Sroot 			case EC:
3706002Sroot 			case EL:
3716002Sroot 				ptyflush();	/* half-hearted */
3726002Sroot 				ioctl(pty, TIOCGETP, &b);
3736002Sroot 				*pfrontp++ = (c == EC) ?
3746002Sroot 					b.sg_erase : b.sg_kill;
3756002Sroot 				break;
3766002Sroot 
3776002Sroot 			/*
3786002Sroot 			 * Check for urgent data...
3796002Sroot 			 */
3806002Sroot 			case DM:
3816002Sroot 				break;
3826002Sroot 
3836002Sroot 			/*
3846002Sroot 			 * Begin option subnegotiation...
3856002Sroot 			 */
3866002Sroot 			case SB:
3876002Sroot 				state = TS_BEGINNEG;
3886002Sroot 				continue;
3896002Sroot 
3906002Sroot 			case WILL:
3916002Sroot 			case WONT:
3926002Sroot 			case DO:
3936002Sroot 			case DONT:
3946002Sroot 				state = TS_WILL + (c - WILL);
3956002Sroot 				continue;
3966002Sroot 
3976002Sroot 			case IAC:
3986002Sroot 				*pfrontp++ = c;
3996002Sroot 				break;
4006002Sroot 			}
4016002Sroot 			state = TS_DATA;
4026002Sroot 			break;
4036002Sroot 
4046002Sroot 		case TS_BEGINNEG:
4056002Sroot 			if (c == IAC)
4066002Sroot 				state = TS_ENDNEG;
4076002Sroot 			break;
4086002Sroot 
4096002Sroot 		case TS_ENDNEG:
4106002Sroot 			state = c == SE ? TS_DATA : TS_BEGINNEG;
4116002Sroot 			break;
4126002Sroot 
4136002Sroot 		case TS_WILL:
4146002Sroot 			if (!hisopts[c])
4156002Sroot 				willoption(c);
4166002Sroot 			state = TS_DATA;
4176002Sroot 			continue;
4186002Sroot 
4196002Sroot 		case TS_WONT:
4206002Sroot 			if (hisopts[c])
4216002Sroot 				wontoption(c);
4226002Sroot 			state = TS_DATA;
4236002Sroot 			continue;
4246002Sroot 
4256002Sroot 		case TS_DO:
4266002Sroot 			if (!myopts[c])
4276002Sroot 				dooption(c);
4286002Sroot 			state = TS_DATA;
4296002Sroot 			continue;
4306002Sroot 
4316002Sroot 		case TS_DONT:
4326002Sroot 			if (myopts[c]) {
4336002Sroot 				myopts[c] = 0;
4346002Sroot 				sprintf(nfrontp, wont, c);
4358379Ssam 				nfrontp += sizeof (wont) - 2;
4366002Sroot 			}
4376002Sroot 			state = TS_DATA;
4386002Sroot 			continue;
4396002Sroot 
4406002Sroot 		default:
4419218Ssam 			printf("telnetd: panic state=%d\n", state);
4426002Sroot 			exit(1);
4436002Sroot 		}
4446002Sroot 	}
4456002Sroot }
4466002Sroot 
4476002Sroot willoption(option)
4486002Sroot 	int option;
4496002Sroot {
4506002Sroot 	char *fmt;
4516002Sroot 
4526002Sroot 	switch (option) {
4536002Sroot 
4546002Sroot 	case TELOPT_BINARY:
4556002Sroot 		mode(RAW, 0);
4566002Sroot 		goto common;
4576002Sroot 
4586002Sroot 	case TELOPT_ECHO:
4596002Sroot 		mode(0, ECHO|CRMOD);
4606002Sroot 		/*FALL THRU*/
4616002Sroot 
4626002Sroot 	case TELOPT_SGA:
4636002Sroot 	common:
4646002Sroot 		hisopts[option] = 1;
4656002Sroot 		fmt = doopt;
4666002Sroot 		break;
4676002Sroot 
4686002Sroot 	case TELOPT_TM:
4696002Sroot 		fmt = dont;
4706002Sroot 		break;
4716002Sroot 
4726002Sroot 	default:
4736002Sroot 		fmt = dont;
4746002Sroot 		break;
4756002Sroot 	}
4766023Ssam 	sprintf(nfrontp, fmt, option);
4778379Ssam 	nfrontp += sizeof (dont) - 2;
4786002Sroot }
4796002Sroot 
4806002Sroot wontoption(option)
4816002Sroot 	int option;
4826002Sroot {
4836002Sroot 	char *fmt;
4846002Sroot 
4856002Sroot 	switch (option) {
4866002Sroot 
4876002Sroot 	case TELOPT_ECHO:
4886002Sroot 		mode(ECHO|CRMOD, 0);
4896002Sroot 		goto common;
4906002Sroot 
4916002Sroot 	case TELOPT_BINARY:
4926002Sroot 		mode(0, RAW);
4936002Sroot 		/*FALL THRU*/
4946002Sroot 
4956002Sroot 	case TELOPT_SGA:
4966002Sroot 	common:
4976002Sroot 		hisopts[option] = 0;
4986002Sroot 		fmt = dont;
4996002Sroot 		break;
5006002Sroot 
5016002Sroot 	default:
5026002Sroot 		fmt = dont;
5036002Sroot 	}
5046002Sroot 	sprintf(nfrontp, fmt, option);
5058379Ssam 	nfrontp += sizeof (doopt) - 2;
5066002Sroot }
5076002Sroot 
5086002Sroot dooption(option)
5096002Sroot 	int option;
5106002Sroot {
5116002Sroot 	char *fmt;
5126002Sroot 
5136002Sroot 	switch (option) {
5146002Sroot 
5156002Sroot 	case TELOPT_TM:
5166002Sroot 		fmt = wont;
5176002Sroot 		break;
5186002Sroot 
5196002Sroot 	case TELOPT_ECHO:
5206002Sroot 		mode(ECHO|CRMOD, 0);
5216002Sroot 		goto common;
5226002Sroot 
5236002Sroot 	case TELOPT_BINARY:
5246002Sroot 		mode(RAW, 0);
5256002Sroot 		/*FALL THRU*/
5266002Sroot 
5276002Sroot 	case TELOPT_SGA:
5286002Sroot 	common:
5296002Sroot 		fmt = will;
5306002Sroot 		break;
5316002Sroot 
5326002Sroot 	default:
5336002Sroot 		fmt = wont;
5346002Sroot 		break;
5356002Sroot 	}
5366002Sroot 	sprintf(nfrontp, fmt, option);
5378379Ssam 	nfrontp += sizeof (doopt) - 2;
5386002Sroot }
5396002Sroot 
5406002Sroot mode(on, off)
5416002Sroot 	int on, off;
5426002Sroot {
5436002Sroot 	struct sgttyb b;
5446002Sroot 
5456002Sroot 	ptyflush();
5466002Sroot 	ioctl(pty, TIOCGETP, &b);
5476002Sroot 	b.sg_flags |= on;
5486002Sroot 	b.sg_flags &= ~off;
5496002Sroot 	ioctl(pty, TIOCSETP, &b);
5506002Sroot }
5516002Sroot 
5526002Sroot /*
5536002Sroot  * Send interrupt to process on other side of pty.
5546002Sroot  * If it is in raw mode, just write NULL;
5556002Sroot  * otherwise, write intr char.
5566002Sroot  */
5576002Sroot interrupt()
5586002Sroot {
5596002Sroot 	struct sgttyb b;
5606002Sroot 	struct tchars tchars;
5616002Sroot 
5626002Sroot 	ptyflush();	/* half-hearted */
5636002Sroot 	ioctl(pty, TIOCGETP, &b);
5646002Sroot 	if (b.sg_flags & RAW) {
5656002Sroot 		*pfrontp++ = '\0';
5666002Sroot 		return;
5676002Sroot 	}
5686002Sroot 	*pfrontp++ = ioctl(pty, TIOCGETC, &tchars) < 0 ?
5696002Sroot 		'\177' : tchars.t_intrc;
5706002Sroot }
5716002Sroot 
5726002Sroot ptyflush()
5736002Sroot {
5746002Sroot 	int n;
5756002Sroot 
5766002Sroot 	if ((n = pfrontp - pbackp) > 0)
5776002Sroot 		n = write(pty, pbackp, n);
5788346Ssam 	if (n < 0)
5798346Ssam 		return;
5806002Sroot 	pbackp += n;
5816002Sroot 	if (pbackp == pfrontp)
5826002Sroot 		pbackp = pfrontp = ptyobuf;
5836002Sroot }
5846002Sroot 
5856002Sroot netflush()
5866002Sroot {
5876002Sroot 	int n;
5886002Sroot 
5896002Sroot 	if ((n = nfrontp - nbackp) > 0)
5906002Sroot 		n = write(net, nbackp, n);
5918346Ssam 	if (n < 0) {
5928346Ssam 		if (errno == EWOULDBLOCK)
5938346Ssam 			return;
5948346Ssam 		/* should blow this guy away... */
5958346Ssam 		return;
5968346Ssam 	}
5976002Sroot 	nbackp += n;
5986002Sroot 	if (nbackp == nfrontp)
5996002Sroot 		nbackp = nfrontp = netobuf;
6006002Sroot }
6016002Sroot 
6026002Sroot cleanup()
6036002Sroot {
6046002Sroot 
6056002Sroot 	rmut();
60610008Ssam 	vhangup();	/* XXX */
60710191Ssam 	shutdown(net, 2);
6086002Sroot 	kill(0, SIGKILL);
6096002Sroot 	exit(1);
6106002Sroot }
6116002Sroot 
6126002Sroot #include <utmp.h>
6136002Sroot 
6146002Sroot struct	utmp wtmp;
6156002Sroot char	wtmpf[]	= "/usr/adm/wtmp";
6166002Sroot char	utmp[] = "/etc/utmp";
6178379Ssam #define SCPYN(a, b)	strncpy(a, b, sizeof (a))
6188379Ssam #define SCMPN(a, b)	strncmp(a, b, sizeof (a))
6196002Sroot 
6206002Sroot rmut()
6216002Sroot {
6226002Sroot 	register f;
6236002Sroot 	int found = 0;
6246002Sroot 
6256002Sroot 	f = open(utmp, 2);
6266002Sroot 	if (f >= 0) {
6278379Ssam 		while(read(f, (char *)&wtmp, sizeof (wtmp)) == sizeof (wtmp)) {
6286002Sroot 			if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
6296002Sroot 				continue;
6308379Ssam 			lseek(f, -(long)sizeof (wtmp), 1);
6316002Sroot 			SCPYN(wtmp.ut_name, "");
6326002Sroot 			time(&wtmp.ut_time);
6338379Ssam 			write(f, (char *)&wtmp, sizeof (wtmp));
6346002Sroot 			found++;
6356002Sroot 		}
6366002Sroot 		close(f);
6376002Sroot 	}
6386002Sroot 	if (found) {
6396002Sroot 		f = open(wtmpf, 1);
6406002Sroot 		if (f >= 0) {
6416002Sroot 			SCPYN(wtmp.ut_line, line+5);
6426002Sroot 			SCPYN(wtmp.ut_name, "");
6436002Sroot 			time(&wtmp.ut_time);
6446002Sroot 			lseek(f, (long)0, 2);
6458379Ssam 			write(f, (char *)&wtmp, sizeof (wtmp));
6466002Sroot 			close(f);
6476002Sroot 		}
6486002Sroot 	}
6496002Sroot 	chmod(line, 0666);
6506002Sroot 	chown(line, 0, 0);
6516002Sroot 	line[strlen("/dev/")] = 'p';
6526002Sroot 	chmod(line, 0666);
6536002Sroot 	chown(line, 0, 0);
6546002Sroot }
655