xref: /csrg-svn/libexec/telnetd/telnetd.c (revision 10587)
16295Sroot #ifndef lint
2*10587Ssam static char sccsid[] = "@(#)telnetd.c	4.17 83/01/22";
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 	}
107*10587Ssam 	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");
1226002Sroot 		else if (pid == 0)
1239218Ssam 			doit(s2);
1249218Ssam 		close(s2);
1256002Sroot 	}
1266002Sroot 	/*NOTREACHED*/
1276002Sroot }
1286002Sroot 
12910418Ssam reapchild()
13010418Ssam {
13110418Ssam 	union wait status;
13210418Ssam 
13310418Ssam 	while (wait3(&status, WNOHANG, 0) > 0)
13410418Ssam 		;
13510418Ssam }
13610418Ssam 
1376002Sroot int	cleanup();
1386002Sroot 
1396002Sroot /*
1406002Sroot  * Get a pty, scan input lines.
1416002Sroot  */
1426002Sroot doit(f)
1436002Sroot {
1446002Sroot 	char *cp = line;
1456002Sroot 	int i, p, cc, t;
1466002Sroot 	struct sgttyb b;
1476002Sroot 
1486002Sroot 	for (i = 0; i < 16; i++) {
1496002Sroot 		cp[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
1506002Sroot 		p = open(cp, 2);
1516002Sroot 		if (p > 0)
1526002Sroot 			goto gotpty;
1536002Sroot 	}
1549244Ssam 	fatal(f, "All network ports in use");
1559244Ssam 	/*NOTREACHED*/
1566002Sroot gotpty:
1576002Sroot 	dup2(f, 0);
1586002Sroot 	cp[strlen("/dev/")] = 't';
1596002Sroot 	t = open("/dev/tty", 2);
1606002Sroot 	if (t >= 0) {
1616002Sroot 		ioctl(t, TIOCNOTTY, 0);
1626002Sroot 		close(t);
1636002Sroot 	}
1646002Sroot 	t = open(cp, 2);
1659244Ssam 	if (t < 0)
1669244Ssam 		fatalperror(f, cp, errno);
1676002Sroot 	ioctl(t, TIOCGETP, &b);
1686388Ssam 	b.sg_flags = CRMOD|XTABS|ANYP;
1696002Sroot 	ioctl(t, TIOCSETP, &b);
1706388Ssam 	ioctl(p, TIOCGETP, &b);
1718379Ssam 	b.sg_flags &= ~ECHO;
1726388Ssam 	ioctl(p, TIOCSETP, &b);
1739244Ssam 	if ((i = fork()) < 0)
1749244Ssam 		fatalperror(f, "fork", errno);
1756002Sroot 	if (i)
1766002Sroot 		telnet(f, p);
1776002Sroot 	close(f);
1786002Sroot 	close(p);
1796002Sroot 	dup2(t, 0);
1806002Sroot 	dup2(t, 1);
1816002Sroot 	dup2(t, 2);
1826002Sroot 	close(t);
1836002Sroot 	execl("/bin/login", "telnet-login", 0);
1849244Ssam 	fatalperror(f, "/bin/login", errno);
1859244Ssam 	/*NOTREACHED*/
1869244Ssam }
1879244Ssam 
1889244Ssam fatal(f, msg)
1899244Ssam 	int f;
1909244Ssam 	char *msg;
1919244Ssam {
1929244Ssam 	char buf[BUFSIZ];
1939244Ssam 
1949244Ssam 	(void) sprintf(buf, "telnetd: %s.\n", msg);
1959244Ssam 	(void) write(f, buf, strlen(buf));
1966002Sroot 	exit(1);
1976002Sroot }
1986002Sroot 
1999244Ssam fatalperror(f, msg, errno)
2009244Ssam 	int f;
2019244Ssam 	char *msg;
2029244Ssam 	int errno;
2039244Ssam {
2049244Ssam 	char buf[BUFSIZ];
2059244Ssam 	extern char *sys_errlist[];
2069244Ssam 
2079244Ssam 	(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
2089244Ssam 	fatal(f, buf);
2099244Ssam }
2109244Ssam 
2116002Sroot /*
2126002Sroot  * Main loop.  Select from pty and network, and
2136002Sroot  * hand data to telnet receiver finite state machine.
2146002Sroot  */
2156002Sroot telnet(f, p)
2166002Sroot {
2176002Sroot 	int on = 1;
2186002Sroot 
2196002Sroot 	net = f, pty = p;
2206002Sroot 	ioctl(f, FIONBIO, &on);
2216002Sroot 	ioctl(p, FIONBIO, &on);
2226002Sroot 	signal(SIGTSTP, SIG_IGN);
2236002Sroot 	sigset(SIGCHLD, cleanup);
2246002Sroot 
2258379Ssam 	/*
2268379Ssam 	 * Request to do remote echo.
2278379Ssam 	 */
2288379Ssam 	dooption(TELOPT_ECHO);
2298379Ssam 	myopts[TELOPT_ECHO] = 1;
2306002Sroot 	for (;;) {
2316002Sroot 		int ibits = 0, obits = 0;
2326002Sroot 		register int c;
2336002Sroot 
2346002Sroot 		/*
2356002Sroot 		 * Never look for input if there's still
2366002Sroot 		 * stuff in the corresponding output buffer
2376002Sroot 		 */
2386002Sroot 		if (nfrontp - nbackp)
2396002Sroot 			obits |= (1 << f);
2406002Sroot 		else
2416002Sroot 			ibits |= (1 << p);
2426002Sroot 		if (pfrontp - pbackp)
2436002Sroot 			obits |= (1 << p);
2446002Sroot 		else
2456002Sroot 			ibits |= (1 << f);
2466002Sroot 		if (ncc < 0 && pcc < 0)
2476002Sroot 			break;
2489218Ssam 		select(16, &ibits, &obits, 0, 0);
2496002Sroot 		if (ibits == 0 && obits == 0) {
2506002Sroot 			sleep(5);
2516002Sroot 			continue;
2526002Sroot 		}
2536002Sroot 
2546002Sroot 		/*
2556002Sroot 		 * Something to read from the network...
2566002Sroot 		 */
2576002Sroot 		if (ibits & (1 << f)) {
2586002Sroot 			ncc = read(f, netibuf, BUFSIZ);
2596002Sroot 			if (ncc < 0 && errno == EWOULDBLOCK)
2606002Sroot 				ncc = 0;
2616002Sroot 			else {
2626002Sroot 				if (ncc <= 0)
2636002Sroot 					break;
2646002Sroot 				netip = netibuf;
2656002Sroot 			}
2666002Sroot 		}
2676002Sroot 
2686002Sroot 		/*
2696002Sroot 		 * Something to read from the pty...
2706002Sroot 		 */
2716002Sroot 		if (ibits & (1 << p)) {
2726002Sroot 			pcc = read(p, ptyibuf, BUFSIZ);
2736002Sroot 			if (pcc < 0 && errno == EWOULDBLOCK)
2746002Sroot 				pcc = 0;
2756002Sroot 			else {
2766002Sroot 				if (pcc <= 0)
2776002Sroot 					break;
2786002Sroot 				ptyip = ptyibuf;
2796002Sroot 			}
2806002Sroot 		}
2816002Sroot 
2826002Sroot 		while (pcc > 0) {
2836002Sroot 			if ((&netobuf[BUFSIZ] - nfrontp) < 2)
2846002Sroot 				break;
2856002Sroot 			c = *ptyip++ & 0377, pcc--;
2866002Sroot 			if (c == IAC)
2876002Sroot 				*nfrontp++ = c;
2886002Sroot 			*nfrontp++ = c;
2896002Sroot 		}
2906002Sroot 		if ((obits & (1 << f)) && (nfrontp - nbackp) > 0)
2916002Sroot 			netflush();
2926002Sroot 		if (ncc > 0)
2936002Sroot 			telrcv();
2946002Sroot 		if ((obits & (1 << p)) && (pfrontp - pbackp) > 0)
2956002Sroot 			ptyflush();
2966002Sroot 	}
2976002Sroot 	cleanup();
2986002Sroot }
2996002Sroot 
3006002Sroot /*
3016002Sroot  * State for recv fsm
3026002Sroot  */
3036002Sroot #define	TS_DATA		0	/* base state */
3046002Sroot #define	TS_IAC		1	/* look for double IAC's */
3056002Sroot #define	TS_CR		2	/* CR-LF ->'s CR */
3066002Sroot #define	TS_BEGINNEG	3	/* throw away begin's... */
3076002Sroot #define	TS_ENDNEG	4	/* ...end's (suboption negotiation) */
3086002Sroot #define	TS_WILL		5	/* will option negotiation */
3096002Sroot #define	TS_WONT		6	/* wont " */
3106002Sroot #define	TS_DO		7	/* do " */
3116002Sroot #define	TS_DONT		8	/* dont " */
3126002Sroot 
3136002Sroot telrcv()
3146002Sroot {
3156002Sroot 	register int c;
3166002Sroot 	static int state = TS_DATA;
3176002Sroot 	struct sgttyb b;
3186002Sroot 
3196002Sroot 	while (ncc > 0) {
3206002Sroot 		if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
3216002Sroot 			return;
3226002Sroot 		c = *netip++ & 0377, ncc--;
3236002Sroot 		switch (state) {
3246002Sroot 
3256002Sroot 		case TS_DATA:
3266002Sroot 			if (c == IAC) {
3276002Sroot 				state = TS_IAC;
3286002Sroot 				break;
3296002Sroot 			}
3306002Sroot 			if (inter > 0)
3316002Sroot 				break;
3326002Sroot 			*pfrontp++ = c;
3336002Sroot 			if (!myopts[TELOPT_BINARY] && c == '\r')
3346002Sroot 				state = TS_CR;
3356002Sroot 			break;
3366002Sroot 
3376002Sroot 		case TS_CR:
3386002Sroot 			if (c && c != '\n')
3396002Sroot 				*pfrontp++ = c;
3406002Sroot 			state = TS_DATA;
3416002Sroot 			break;
3426002Sroot 
3436002Sroot 		case TS_IAC:
3446002Sroot 			switch (c) {
3456002Sroot 
3466002Sroot 			/*
3476002Sroot 			 * Send the process on the pty side an
3486002Sroot 			 * interrupt.  Do this with a NULL or
3496002Sroot 			 * interrupt char; depending on the tty mode.
3506002Sroot 			 */
3516002Sroot 			case BREAK:
3526002Sroot 			case IP:
3536002Sroot 				interrupt();
3546002Sroot 				break;
3556002Sroot 
3566002Sroot 			/*
3576002Sroot 			 * Are You There?
3586002Sroot 			 */
3596002Sroot 			case AYT:
3606002Sroot 				*pfrontp++ = BELL;
3616002Sroot 				break;
3626002Sroot 
3636002Sroot 			/*
3646002Sroot 			 * Erase Character and
3656002Sroot 			 * Erase Line
3666002Sroot 			 */
3676002Sroot 			case EC:
3686002Sroot 			case EL:
3696002Sroot 				ptyflush();	/* half-hearted */
3706002Sroot 				ioctl(pty, TIOCGETP, &b);
3716002Sroot 				*pfrontp++ = (c == EC) ?
3726002Sroot 					b.sg_erase : b.sg_kill;
3736002Sroot 				break;
3746002Sroot 
3756002Sroot 			/*
3766002Sroot 			 * Check for urgent data...
3776002Sroot 			 */
3786002Sroot 			case DM:
3796002Sroot 				break;
3806002Sroot 
3816002Sroot 			/*
3826002Sroot 			 * Begin option subnegotiation...
3836002Sroot 			 */
3846002Sroot 			case SB:
3856002Sroot 				state = TS_BEGINNEG;
3866002Sroot 				continue;
3876002Sroot 
3886002Sroot 			case WILL:
3896002Sroot 			case WONT:
3906002Sroot 			case DO:
3916002Sroot 			case DONT:
3926002Sroot 				state = TS_WILL + (c - WILL);
3936002Sroot 				continue;
3946002Sroot 
3956002Sroot 			case IAC:
3966002Sroot 				*pfrontp++ = c;
3976002Sroot 				break;
3986002Sroot 			}
3996002Sroot 			state = TS_DATA;
4006002Sroot 			break;
4016002Sroot 
4026002Sroot 		case TS_BEGINNEG:
4036002Sroot 			if (c == IAC)
4046002Sroot 				state = TS_ENDNEG;
4056002Sroot 			break;
4066002Sroot 
4076002Sroot 		case TS_ENDNEG:
4086002Sroot 			state = c == SE ? TS_DATA : TS_BEGINNEG;
4096002Sroot 			break;
4106002Sroot 
4116002Sroot 		case TS_WILL:
4126002Sroot 			if (!hisopts[c])
4136002Sroot 				willoption(c);
4146002Sroot 			state = TS_DATA;
4156002Sroot 			continue;
4166002Sroot 
4176002Sroot 		case TS_WONT:
4186002Sroot 			if (hisopts[c])
4196002Sroot 				wontoption(c);
4206002Sroot 			state = TS_DATA;
4216002Sroot 			continue;
4226002Sroot 
4236002Sroot 		case TS_DO:
4246002Sroot 			if (!myopts[c])
4256002Sroot 				dooption(c);
4266002Sroot 			state = TS_DATA;
4276002Sroot 			continue;
4286002Sroot 
4296002Sroot 		case TS_DONT:
4306002Sroot 			if (myopts[c]) {
4316002Sroot 				myopts[c] = 0;
4326002Sroot 				sprintf(nfrontp, wont, c);
4338379Ssam 				nfrontp += sizeof (wont) - 2;
4346002Sroot 			}
4356002Sroot 			state = TS_DATA;
4366002Sroot 			continue;
4376002Sroot 
4386002Sroot 		default:
4399218Ssam 			printf("telnetd: panic state=%d\n", state);
4406002Sroot 			exit(1);
4416002Sroot 		}
4426002Sroot 	}
4436002Sroot }
4446002Sroot 
4456002Sroot willoption(option)
4466002Sroot 	int option;
4476002Sroot {
4486002Sroot 	char *fmt;
4496002Sroot 
4506002Sroot 	switch (option) {
4516002Sroot 
4526002Sroot 	case TELOPT_BINARY:
4536002Sroot 		mode(RAW, 0);
4546002Sroot 		goto common;
4556002Sroot 
4566002Sroot 	case TELOPT_ECHO:
4576002Sroot 		mode(0, ECHO|CRMOD);
4586002Sroot 		/*FALL THRU*/
4596002Sroot 
4606002Sroot 	case TELOPT_SGA:
4616002Sroot 	common:
4626002Sroot 		hisopts[option] = 1;
4636002Sroot 		fmt = doopt;
4646002Sroot 		break;
4656002Sroot 
4666002Sroot 	case TELOPT_TM:
4676002Sroot 		fmt = dont;
4686002Sroot 		break;
4696002Sroot 
4706002Sroot 	default:
4716002Sroot 		fmt = dont;
4726002Sroot 		break;
4736002Sroot 	}
4746023Ssam 	sprintf(nfrontp, fmt, option);
4758379Ssam 	nfrontp += sizeof (dont) - 2;
4766002Sroot }
4776002Sroot 
4786002Sroot wontoption(option)
4796002Sroot 	int option;
4806002Sroot {
4816002Sroot 	char *fmt;
4826002Sroot 
4836002Sroot 	switch (option) {
4846002Sroot 
4856002Sroot 	case TELOPT_ECHO:
4866002Sroot 		mode(ECHO|CRMOD, 0);
4876002Sroot 		goto common;
4886002Sroot 
4896002Sroot 	case TELOPT_BINARY:
4906002Sroot 		mode(0, RAW);
4916002Sroot 		/*FALL THRU*/
4926002Sroot 
4936002Sroot 	case TELOPT_SGA:
4946002Sroot 	common:
4956002Sroot 		hisopts[option] = 0;
4966002Sroot 		fmt = dont;
4976002Sroot 		break;
4986002Sroot 
4996002Sroot 	default:
5006002Sroot 		fmt = dont;
5016002Sroot 	}
5026002Sroot 	sprintf(nfrontp, fmt, option);
5038379Ssam 	nfrontp += sizeof (doopt) - 2;
5046002Sroot }
5056002Sroot 
5066002Sroot dooption(option)
5076002Sroot 	int option;
5086002Sroot {
5096002Sroot 	char *fmt;
5106002Sroot 
5116002Sroot 	switch (option) {
5126002Sroot 
5136002Sroot 	case TELOPT_TM:
5146002Sroot 		fmt = wont;
5156002Sroot 		break;
5166002Sroot 
5176002Sroot 	case TELOPT_ECHO:
5186002Sroot 		mode(ECHO|CRMOD, 0);
5196002Sroot 		goto common;
5206002Sroot 
5216002Sroot 	case TELOPT_BINARY:
5226002Sroot 		mode(RAW, 0);
5236002Sroot 		/*FALL THRU*/
5246002Sroot 
5256002Sroot 	case TELOPT_SGA:
5266002Sroot 	common:
5276002Sroot 		fmt = will;
5286002Sroot 		break;
5296002Sroot 
5306002Sroot 	default:
5316002Sroot 		fmt = wont;
5326002Sroot 		break;
5336002Sroot 	}
5346002Sroot 	sprintf(nfrontp, fmt, option);
5358379Ssam 	nfrontp += sizeof (doopt) - 2;
5366002Sroot }
5376002Sroot 
5386002Sroot mode(on, off)
5396002Sroot 	int on, off;
5406002Sroot {
5416002Sroot 	struct sgttyb b;
5426002Sroot 
5436002Sroot 	ptyflush();
5446002Sroot 	ioctl(pty, TIOCGETP, &b);
5456002Sroot 	b.sg_flags |= on;
5466002Sroot 	b.sg_flags &= ~off;
5476002Sroot 	ioctl(pty, TIOCSETP, &b);
5486002Sroot }
5496002Sroot 
5506002Sroot /*
5516002Sroot  * Send interrupt to process on other side of pty.
5526002Sroot  * If it is in raw mode, just write NULL;
5536002Sroot  * otherwise, write intr char.
5546002Sroot  */
5556002Sroot interrupt()
5566002Sroot {
5576002Sroot 	struct sgttyb b;
5586002Sroot 	struct tchars tchars;
5596002Sroot 
5606002Sroot 	ptyflush();	/* half-hearted */
5616002Sroot 	ioctl(pty, TIOCGETP, &b);
5626002Sroot 	if (b.sg_flags & RAW) {
5636002Sroot 		*pfrontp++ = '\0';
5646002Sroot 		return;
5656002Sroot 	}
5666002Sroot 	*pfrontp++ = ioctl(pty, TIOCGETC, &tchars) < 0 ?
5676002Sroot 		'\177' : tchars.t_intrc;
5686002Sroot }
5696002Sroot 
5706002Sroot ptyflush()
5716002Sroot {
5726002Sroot 	int n;
5736002Sroot 
5746002Sroot 	if ((n = pfrontp - pbackp) > 0)
5756002Sroot 		n = write(pty, pbackp, n);
5768346Ssam 	if (n < 0)
5778346Ssam 		return;
5786002Sroot 	pbackp += n;
5796002Sroot 	if (pbackp == pfrontp)
5806002Sroot 		pbackp = pfrontp = ptyobuf;
5816002Sroot }
5826002Sroot 
5836002Sroot netflush()
5846002Sroot {
5856002Sroot 	int n;
5866002Sroot 
5876002Sroot 	if ((n = nfrontp - nbackp) > 0)
5886002Sroot 		n = write(net, nbackp, n);
5898346Ssam 	if (n < 0) {
5908346Ssam 		if (errno == EWOULDBLOCK)
5918346Ssam 			return;
5928346Ssam 		/* should blow this guy away... */
5938346Ssam 		return;
5948346Ssam 	}
5956002Sroot 	nbackp += n;
5966002Sroot 	if (nbackp == nfrontp)
5976002Sroot 		nbackp = nfrontp = netobuf;
5986002Sroot }
5996002Sroot 
6006002Sroot cleanup()
6016002Sroot {
6026002Sroot 
6036002Sroot 	rmut();
60410008Ssam 	vhangup();	/* XXX */
60510191Ssam 	shutdown(net, 2);
6066002Sroot 	kill(0, SIGKILL);
6076002Sroot 	exit(1);
6086002Sroot }
6096002Sroot 
6106002Sroot #include <utmp.h>
6116002Sroot 
6126002Sroot struct	utmp wtmp;
6136002Sroot char	wtmpf[]	= "/usr/adm/wtmp";
6146002Sroot char	utmp[] = "/etc/utmp";
6158379Ssam #define SCPYN(a, b)	strncpy(a, b, sizeof (a))
6168379Ssam #define SCMPN(a, b)	strncmp(a, b, sizeof (a))
6176002Sroot 
6186002Sroot rmut()
6196002Sroot {
6206002Sroot 	register f;
6216002Sroot 	int found = 0;
6226002Sroot 
6236002Sroot 	f = open(utmp, 2);
6246002Sroot 	if (f >= 0) {
6258379Ssam 		while(read(f, (char *)&wtmp, sizeof (wtmp)) == sizeof (wtmp)) {
6266002Sroot 			if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
6276002Sroot 				continue;
6288379Ssam 			lseek(f, -(long)sizeof (wtmp), 1);
6296002Sroot 			SCPYN(wtmp.ut_name, "");
6306002Sroot 			time(&wtmp.ut_time);
6318379Ssam 			write(f, (char *)&wtmp, sizeof (wtmp));
6326002Sroot 			found++;
6336002Sroot 		}
6346002Sroot 		close(f);
6356002Sroot 	}
6366002Sroot 	if (found) {
6376002Sroot 		f = open(wtmpf, 1);
6386002Sroot 		if (f >= 0) {
6396002Sroot 			SCPYN(wtmp.ut_line, line+5);
6406002Sroot 			SCPYN(wtmp.ut_name, "");
6416002Sroot 			time(&wtmp.ut_time);
6426002Sroot 			lseek(f, (long)0, 2);
6438379Ssam 			write(f, (char *)&wtmp, sizeof (wtmp));
6446002Sroot 			close(f);
6456002Sroot 		}
6466002Sroot 	}
6476002Sroot 	chmod(line, 0666);
6486002Sroot 	chown(line, 0, 0);
6496002Sroot 	line[strlen("/dev/")] = 'p';
6506002Sroot 	chmod(line, 0666);
6516002Sroot 	chown(line, 0, 0);
6526002Sroot }
653