xref: /csrg-svn/libexec/telnetd/telnetd.c (revision 26083)
121182Sdist /*
221182Sdist  * Copyright (c) 1983 Regents of the University of California.
321182Sdist  * All rights reserved.  The Berkeley software License Agreement
421182Sdist  * specifies the terms and conditions for redistribution.
521182Sdist  */
621182Sdist 
76295Sroot #ifndef lint
821182Sdist char copyright[] =
921182Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1021182Sdist  All rights reserved.\n";
1121182Sdist #endif not lint
126295Sroot 
1321182Sdist #ifndef lint
14*26083Slepreau static char sccsid[] = "@(#)telnetd.c	5.5 (Berkeley) 02/05/86";
1521182Sdist #endif not lint
1621182Sdist 
176002Sroot /*
186002Sroot  * Stripped-down telnet server.
196002Sroot  */
209218Ssam #include <sys/types.h>
219218Ssam #include <sys/socket.h>
2213608Ssam #include <sys/wait.h>
2317583Ssam #include <sys/file.h>
2420188Skarels #include <sys/stat.h>
259218Ssam 
269218Ssam #include <netinet/in.h>
279218Ssam 
2812216Ssam #include <arpa/telnet.h>
2912216Ssam 
306002Sroot #include <stdio.h>
316002Sroot #include <signal.h>
326002Sroot #include <errno.h>
336002Sroot #include <sgtty.h>
348346Ssam #include <netdb.h>
3517187Sralph #include <syslog.h>
369218Ssam 
3713798Ssam #define	BELL	'\07'
3823567Sbloom #define BANNER	"\r\n\r\n4.3 BSD UNIX (%s)\r\n\r\r\n\r%s"
396002Sroot 
406002Sroot char	hisopts[256];
416002Sroot char	myopts[256];
426002Sroot 
436002Sroot char	doopt[] = { IAC, DO, '%', 'c', 0 };
446002Sroot char	dont[] = { IAC, DONT, '%', 'c', 0 };
456002Sroot char	will[] = { IAC, WILL, '%', 'c', 0 };
466002Sroot char	wont[] = { IAC, WONT, '%', 'c', 0 };
476002Sroot 
486002Sroot /*
496002Sroot  * I/O data buffers, pointers, and counters.
506002Sroot  */
516002Sroot char	ptyibuf[BUFSIZ], *ptyip = ptyibuf;
526002Sroot char	ptyobuf[BUFSIZ], *pfrontp = ptyobuf, *pbackp = ptyobuf;
536002Sroot char	netibuf[BUFSIZ], *netip = netibuf;
546388Ssam char	netobuf[BUFSIZ], *nfrontp = netobuf, *nbackp = netobuf;
556002Sroot int	pcc, ncc;
566002Sroot 
576002Sroot int	pty, net;
586002Sroot int	inter;
5913799Ssam extern	char **environ;
606002Sroot extern	int errno;
6120188Skarels char	*line;
626002Sroot 
636002Sroot main(argc, argv)
646002Sroot 	char *argv[];
656002Sroot {
6616371Skarels 	struct sockaddr_in from;
6717156Ssam 	int on = 1, fromlen;
686002Sroot 
6924855Seric 	openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
7016371Skarels 	fromlen = sizeof (from);
7116371Skarels 	if (getpeername(0, &from, &fromlen) < 0) {
7216371Skarels 		fprintf(stderr, "%s: ", argv[0]);
7316371Skarels 		perror("getpeername");
7416371Skarels 		_exit(1);
758346Ssam 	}
7617156Ssam 	if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
7717187Sralph 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
7810418Ssam 	}
7916371Skarels 	doit(0, &from);
806002Sroot }
816002Sroot 
8213799Ssam char	*envinit[] = { "TERM=network", 0 };
836002Sroot int	cleanup();
846002Sroot 
856002Sroot /*
866002Sroot  * Get a pty, scan input lines.
876002Sroot  */
8812683Ssam doit(f, who)
8912683Ssam 	int f;
9012683Ssam 	struct sockaddr_in *who;
916002Sroot {
9220188Skarels 	char *host, *inet_ntoa();
9317583Ssam 	int i, p, t;
946002Sroot 	struct sgttyb b;
9512683Ssam 	struct hostent *hp;
9620188Skarels 	char c;
976002Sroot 
9820188Skarels 	for (c = 'p'; c <= 's'; c++) {
9920188Skarels 		struct stat stb;
10020188Skarels 
10120188Skarels 		line = "/dev/ptyXX";
10220188Skarels 		line[strlen("/dev/pty")] = c;
10320188Skarels 		line[strlen("/dev/ptyp")] = '0';
10420188Skarels 		if (stat(line, &stb) < 0)
10520188Skarels 			break;
10617583Ssam 		for (i = 0; i < 16; i++) {
10720188Skarels 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
10820188Skarels 			p = open(line, 2);
10917583Ssam 			if (p > 0)
11017583Ssam 				goto gotpty;
11117583Ssam 		}
1126002Sroot 	}
1139244Ssam 	fatal(f, "All network ports in use");
1149244Ssam 	/*NOTREACHED*/
1156002Sroot gotpty:
1166002Sroot 	dup2(f, 0);
11720188Skarels 	line[strlen("/dev/")] = 't';
11817583Ssam 	t = open("/dev/tty", O_RDWR);
1196002Sroot 	if (t >= 0) {
1206002Sroot 		ioctl(t, TIOCNOTTY, 0);
1216002Sroot 		close(t);
1226002Sroot 	}
12320188Skarels 	t = open(line, O_RDWR);
1249244Ssam 	if (t < 0)
12520188Skarels 		fatalperror(f, line, errno);
1266002Sroot 	ioctl(t, TIOCGETP, &b);
1276388Ssam 	b.sg_flags = CRMOD|XTABS|ANYP;
1286002Sroot 	ioctl(t, TIOCSETP, &b);
1296388Ssam 	ioctl(p, TIOCGETP, &b);
1308379Ssam 	b.sg_flags &= ~ECHO;
1316388Ssam 	ioctl(p, TIOCSETP, &b);
13212683Ssam 	hp = gethostbyaddr(&who->sin_addr, sizeof (struct in_addr),
13312683Ssam 		who->sin_family);
13412683Ssam 	if (hp)
13512683Ssam 		host = hp->h_name;
13612683Ssam 	else
13717444Sralph 		host = inet_ntoa(who->sin_addr);
1389244Ssam 	if ((i = fork()) < 0)
1399244Ssam 		fatalperror(f, "fork", errno);
1406002Sroot 	if (i)
1416002Sroot 		telnet(f, p);
1426002Sroot 	close(f);
1436002Sroot 	close(p);
1446002Sroot 	dup2(t, 0);
1456002Sroot 	dup2(t, 1);
1466002Sroot 	dup2(t, 2);
1476002Sroot 	close(t);
14813799Ssam 	environ = envinit;
14912713Ssam 	execl("/bin/login", "login", "-h", host, 0);
1509244Ssam 	fatalperror(f, "/bin/login", errno);
1519244Ssam 	/*NOTREACHED*/
1529244Ssam }
1539244Ssam 
1549244Ssam fatal(f, msg)
1559244Ssam 	int f;
1569244Ssam 	char *msg;
1579244Ssam {
1589244Ssam 	char buf[BUFSIZ];
1599244Ssam 
16017583Ssam 	(void) sprintf(buf, "telnetd: %s.\r\n", msg);
1619244Ssam 	(void) write(f, buf, strlen(buf));
1626002Sroot 	exit(1);
1636002Sroot }
1646002Sroot 
1659244Ssam fatalperror(f, msg, errno)
1669244Ssam 	int f;
1679244Ssam 	char *msg;
1689244Ssam 	int errno;
1699244Ssam {
1709244Ssam 	char buf[BUFSIZ];
1719244Ssam 	extern char *sys_errlist[];
1729244Ssam 
17317583Ssam 	(void) sprintf(buf, "%s: %s\r\n", msg, sys_errlist[errno]);
1749244Ssam 	fatal(f, buf);
1759244Ssam }
1769244Ssam 
1776002Sroot /*
1786002Sroot  * Main loop.  Select from pty and network, and
1796002Sroot  * hand data to telnet receiver finite state machine.
1806002Sroot  */
1816002Sroot telnet(f, p)
1826002Sroot {
1836002Sroot 	int on = 1;
18412713Ssam 	char hostname[32];
1856002Sroot 
1866002Sroot 	net = f, pty = p;
1876002Sroot 	ioctl(f, FIONBIO, &on);
1886002Sroot 	ioctl(p, FIONBIO, &on);
1896002Sroot 	signal(SIGTSTP, SIG_IGN);
19013028Ssam 	signal(SIGCHLD, cleanup);
191*26083Slepreau 	setpgrp(0, 0);
1926002Sroot 
1938379Ssam 	/*
1948379Ssam 	 * Request to do remote echo.
1958379Ssam 	 */
1968379Ssam 	dooption(TELOPT_ECHO);
1978379Ssam 	myopts[TELOPT_ECHO] = 1;
19812713Ssam 	/*
19912713Ssam 	 * Show banner that getty never gave.
20012713Ssam 	 */
20112713Ssam 	gethostname(hostname, sizeof (hostname));
20212713Ssam 	sprintf(nfrontp, BANNER, hostname, "");
20312713Ssam 	nfrontp += strlen(nfrontp);
2046002Sroot 	for (;;) {
2056002Sroot 		int ibits = 0, obits = 0;
2066002Sroot 		register int c;
2076002Sroot 
2086002Sroot 		/*
2096002Sroot 		 * Never look for input if there's still
2106002Sroot 		 * stuff in the corresponding output buffer
2116002Sroot 		 */
21217583Ssam 		if (nfrontp - nbackp || pcc > 0)
2136002Sroot 			obits |= (1 << f);
2146002Sroot 		else
2156002Sroot 			ibits |= (1 << p);
21617583Ssam 		if (pfrontp - pbackp || ncc > 0)
2176002Sroot 			obits |= (1 << p);
2186002Sroot 		else
2196002Sroot 			ibits |= (1 << f);
2206002Sroot 		if (ncc < 0 && pcc < 0)
2216002Sroot 			break;
2229218Ssam 		select(16, &ibits, &obits, 0, 0);
2236002Sroot 		if (ibits == 0 && obits == 0) {
2246002Sroot 			sleep(5);
2256002Sroot 			continue;
2266002Sroot 		}
2276002Sroot 
2286002Sroot 		/*
2296002Sroot 		 * Something to read from the network...
2306002Sroot 		 */
2316002Sroot 		if (ibits & (1 << f)) {
2326002Sroot 			ncc = read(f, netibuf, BUFSIZ);
2336002Sroot 			if (ncc < 0 && errno == EWOULDBLOCK)
2346002Sroot 				ncc = 0;
2356002Sroot 			else {
2366002Sroot 				if (ncc <= 0)
2376002Sroot 					break;
2386002Sroot 				netip = netibuf;
2396002Sroot 			}
2406002Sroot 		}
2416002Sroot 
2426002Sroot 		/*
2436002Sroot 		 * Something to read from the pty...
2446002Sroot 		 */
2456002Sroot 		if (ibits & (1 << p)) {
2466002Sroot 			pcc = read(p, ptyibuf, BUFSIZ);
2476002Sroot 			if (pcc < 0 && errno == EWOULDBLOCK)
2486002Sroot 				pcc = 0;
2496002Sroot 			else {
2506002Sroot 				if (pcc <= 0)
2516002Sroot 					break;
2526002Sroot 				ptyip = ptyibuf;
2536002Sroot 			}
2546002Sroot 		}
2556002Sroot 
2566002Sroot 		while (pcc > 0) {
2576002Sroot 			if ((&netobuf[BUFSIZ] - nfrontp) < 2)
2586002Sroot 				break;
2596002Sroot 			c = *ptyip++ & 0377, pcc--;
2606002Sroot 			if (c == IAC)
2616002Sroot 				*nfrontp++ = c;
2626002Sroot 			*nfrontp++ = c;
2636002Sroot 		}
2646002Sroot 		if ((obits & (1 << f)) && (nfrontp - nbackp) > 0)
2656002Sroot 			netflush();
2666002Sroot 		if (ncc > 0)
2676002Sroot 			telrcv();
2686002Sroot 		if ((obits & (1 << p)) && (pfrontp - pbackp) > 0)
2696002Sroot 			ptyflush();
2706002Sroot 	}
2716002Sroot 	cleanup();
2726002Sroot }
2736002Sroot 
2746002Sroot /*
2756002Sroot  * State for recv fsm
2766002Sroot  */
2776002Sroot #define	TS_DATA		0	/* base state */
2786002Sroot #define	TS_IAC		1	/* look for double IAC's */
2796002Sroot #define	TS_CR		2	/* CR-LF ->'s CR */
2806002Sroot #define	TS_BEGINNEG	3	/* throw away begin's... */
2816002Sroot #define	TS_ENDNEG	4	/* ...end's (suboption negotiation) */
2826002Sroot #define	TS_WILL		5	/* will option negotiation */
2836002Sroot #define	TS_WONT		6	/* wont " */
2846002Sroot #define	TS_DO		7	/* do " */
2856002Sroot #define	TS_DONT		8	/* dont " */
2866002Sroot 
2876002Sroot telrcv()
2886002Sroot {
2896002Sroot 	register int c;
2906002Sroot 	static int state = TS_DATA;
2916002Sroot 	struct sgttyb b;
2926002Sroot 
2936002Sroot 	while (ncc > 0) {
2946002Sroot 		if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
2956002Sroot 			return;
2966002Sroot 		c = *netip++ & 0377, ncc--;
2976002Sroot 		switch (state) {
2986002Sroot 
2996002Sroot 		case TS_DATA:
3006002Sroot 			if (c == IAC) {
3016002Sroot 				state = TS_IAC;
3026002Sroot 				break;
3036002Sroot 			}
3046002Sroot 			if (inter > 0)
3056002Sroot 				break;
3066002Sroot 			*pfrontp++ = c;
3076002Sroot 			if (!myopts[TELOPT_BINARY] && c == '\r')
3086002Sroot 				state = TS_CR;
3096002Sroot 			break;
3106002Sroot 
3116002Sroot 		case TS_CR:
3126002Sroot 			if (c && c != '\n')
3136002Sroot 				*pfrontp++ = c;
3146002Sroot 			state = TS_DATA;
3156002Sroot 			break;
3166002Sroot 
3176002Sroot 		case TS_IAC:
3186002Sroot 			switch (c) {
3196002Sroot 
3206002Sroot 			/*
3216002Sroot 			 * Send the process on the pty side an
3226002Sroot 			 * interrupt.  Do this with a NULL or
3236002Sroot 			 * interrupt char; depending on the tty mode.
3246002Sroot 			 */
3256002Sroot 			case BREAK:
3266002Sroot 			case IP:
3276002Sroot 				interrupt();
3286002Sroot 				break;
3296002Sroot 
3306002Sroot 			/*
3316002Sroot 			 * Are You There?
3326002Sroot 			 */
3336002Sroot 			case AYT:
33417583Ssam 				strcpy(nfrontp, "\r\n[Yes]\r\n");
33517583Ssam 				nfrontp += 9;
3366002Sroot 				break;
3376002Sroot 
3386002Sroot 			/*
3396002Sroot 			 * Erase Character and
3406002Sroot 			 * Erase Line
3416002Sroot 			 */
3426002Sroot 			case EC:
3436002Sroot 			case EL:
3446002Sroot 				ptyflush();	/* half-hearted */
3456002Sroot 				ioctl(pty, TIOCGETP, &b);
3466002Sroot 				*pfrontp++ = (c == EC) ?
3476002Sroot 					b.sg_erase : b.sg_kill;
3486002Sroot 				break;
3496002Sroot 
3506002Sroot 			/*
3516002Sroot 			 * Check for urgent data...
3526002Sroot 			 */
3536002Sroot 			case DM:
3546002Sroot 				break;
3556002Sroot 
3566002Sroot 			/*
3576002Sroot 			 * Begin option subnegotiation...
3586002Sroot 			 */
3596002Sroot 			case SB:
3606002Sroot 				state = TS_BEGINNEG;
3616002Sroot 				continue;
3626002Sroot 
3636002Sroot 			case WILL:
3646002Sroot 			case WONT:
3656002Sroot 			case DO:
3666002Sroot 			case DONT:
3676002Sroot 				state = TS_WILL + (c - WILL);
3686002Sroot 				continue;
3696002Sroot 
3706002Sroot 			case IAC:
3716002Sroot 				*pfrontp++ = c;
3726002Sroot 				break;
3736002Sroot 			}
3746002Sroot 			state = TS_DATA;
3756002Sroot 			break;
3766002Sroot 
3776002Sroot 		case TS_BEGINNEG:
3786002Sroot 			if (c == IAC)
3796002Sroot 				state = TS_ENDNEG;
3806002Sroot 			break;
3816002Sroot 
3826002Sroot 		case TS_ENDNEG:
3836002Sroot 			state = c == SE ? TS_DATA : TS_BEGINNEG;
3846002Sroot 			break;
3856002Sroot 
3866002Sroot 		case TS_WILL:
3876002Sroot 			if (!hisopts[c])
3886002Sroot 				willoption(c);
3896002Sroot 			state = TS_DATA;
3906002Sroot 			continue;
3916002Sroot 
3926002Sroot 		case TS_WONT:
3936002Sroot 			if (hisopts[c])
3946002Sroot 				wontoption(c);
3956002Sroot 			state = TS_DATA;
3966002Sroot 			continue;
3976002Sroot 
3986002Sroot 		case TS_DO:
3996002Sroot 			if (!myopts[c])
4006002Sroot 				dooption(c);
4016002Sroot 			state = TS_DATA;
4026002Sroot 			continue;
4036002Sroot 
4046002Sroot 		case TS_DONT:
4056002Sroot 			if (myopts[c]) {
4066002Sroot 				myopts[c] = 0;
4076002Sroot 				sprintf(nfrontp, wont, c);
4088379Ssam 				nfrontp += sizeof (wont) - 2;
4096002Sroot 			}
4106002Sroot 			state = TS_DATA;
4116002Sroot 			continue;
4126002Sroot 
4136002Sroot 		default:
4149218Ssam 			printf("telnetd: panic state=%d\n", state);
4156002Sroot 			exit(1);
4166002Sroot 		}
4176002Sroot 	}
4186002Sroot }
4196002Sroot 
4206002Sroot willoption(option)
4216002Sroot 	int option;
4226002Sroot {
4236002Sroot 	char *fmt;
4246002Sroot 
4256002Sroot 	switch (option) {
4266002Sroot 
4276002Sroot 	case TELOPT_BINARY:
4286002Sroot 		mode(RAW, 0);
4296002Sroot 		goto common;
4306002Sroot 
4316002Sroot 	case TELOPT_ECHO:
4326002Sroot 		mode(0, ECHO|CRMOD);
4336002Sroot 		/*FALL THRU*/
4346002Sroot 
4356002Sroot 	case TELOPT_SGA:
4366002Sroot 	common:
4376002Sroot 		hisopts[option] = 1;
4386002Sroot 		fmt = doopt;
4396002Sroot 		break;
4406002Sroot 
4416002Sroot 	case TELOPT_TM:
4426002Sroot 		fmt = dont;
4436002Sroot 		break;
4446002Sroot 
4456002Sroot 	default:
4466002Sroot 		fmt = dont;
4476002Sroot 		break;
4486002Sroot 	}
4496023Ssam 	sprintf(nfrontp, fmt, option);
4508379Ssam 	nfrontp += sizeof (dont) - 2;
4516002Sroot }
4526002Sroot 
4536002Sroot wontoption(option)
4546002Sroot 	int option;
4556002Sroot {
4566002Sroot 	char *fmt;
4576002Sroot 
4586002Sroot 	switch (option) {
4596002Sroot 
4606002Sroot 	case TELOPT_ECHO:
4616002Sroot 		mode(ECHO|CRMOD, 0);
4626002Sroot 		goto common;
4636002Sroot 
4646002Sroot 	case TELOPT_BINARY:
4656002Sroot 		mode(0, RAW);
4666002Sroot 		/*FALL THRU*/
4676002Sroot 
4686002Sroot 	case TELOPT_SGA:
4696002Sroot 	common:
4706002Sroot 		hisopts[option] = 0;
4716002Sroot 		fmt = dont;
4726002Sroot 		break;
4736002Sroot 
4746002Sroot 	default:
4756002Sroot 		fmt = dont;
4766002Sroot 	}
4776002Sroot 	sprintf(nfrontp, fmt, option);
4788379Ssam 	nfrontp += sizeof (doopt) - 2;
4796002Sroot }
4806002Sroot 
4816002Sroot dooption(option)
4826002Sroot 	int option;
4836002Sroot {
4846002Sroot 	char *fmt;
4856002Sroot 
4866002Sroot 	switch (option) {
4876002Sroot 
4886002Sroot 	case TELOPT_TM:
4896002Sroot 		fmt = wont;
4906002Sroot 		break;
4916002Sroot 
4926002Sroot 	case TELOPT_ECHO:
4936002Sroot 		mode(ECHO|CRMOD, 0);
4946002Sroot 		goto common;
4956002Sroot 
4966002Sroot 	case TELOPT_BINARY:
4976002Sroot 		mode(RAW, 0);
4986002Sroot 		/*FALL THRU*/
4996002Sroot 
5006002Sroot 	case TELOPT_SGA:
5016002Sroot 	common:
5026002Sroot 		fmt = will;
5036002Sroot 		break;
5046002Sroot 
5056002Sroot 	default:
5066002Sroot 		fmt = wont;
5076002Sroot 		break;
5086002Sroot 	}
5096002Sroot 	sprintf(nfrontp, fmt, option);
5108379Ssam 	nfrontp += sizeof (doopt) - 2;
5116002Sroot }
5126002Sroot 
5136002Sroot mode(on, off)
5146002Sroot 	int on, off;
5156002Sroot {
5166002Sroot 	struct sgttyb b;
5176002Sroot 
5186002Sroot 	ptyflush();
5196002Sroot 	ioctl(pty, TIOCGETP, &b);
5206002Sroot 	b.sg_flags |= on;
5216002Sroot 	b.sg_flags &= ~off;
5226002Sroot 	ioctl(pty, TIOCSETP, &b);
5236002Sroot }
5246002Sroot 
5256002Sroot /*
5266002Sroot  * Send interrupt to process on other side of pty.
5276002Sroot  * If it is in raw mode, just write NULL;
5286002Sroot  * otherwise, write intr char.
5296002Sroot  */
5306002Sroot interrupt()
5316002Sroot {
5326002Sroot 	struct sgttyb b;
5336002Sroot 	struct tchars tchars;
5346002Sroot 
5356002Sroot 	ptyflush();	/* half-hearted */
5366002Sroot 	ioctl(pty, TIOCGETP, &b);
5376002Sroot 	if (b.sg_flags & RAW) {
5386002Sroot 		*pfrontp++ = '\0';
5396002Sroot 		return;
5406002Sroot 	}
5416002Sroot 	*pfrontp++ = ioctl(pty, TIOCGETC, &tchars) < 0 ?
5426002Sroot 		'\177' : tchars.t_intrc;
5436002Sroot }
5446002Sroot 
5456002Sroot ptyflush()
5466002Sroot {
5476002Sroot 	int n;
5486002Sroot 
5496002Sroot 	if ((n = pfrontp - pbackp) > 0)
5506002Sroot 		n = write(pty, pbackp, n);
5518346Ssam 	if (n < 0)
5528346Ssam 		return;
5536002Sroot 	pbackp += n;
5546002Sroot 	if (pbackp == pfrontp)
5556002Sroot 		pbackp = pfrontp = ptyobuf;
5566002Sroot }
5576002Sroot 
5586002Sroot netflush()
5596002Sroot {
5606002Sroot 	int n;
5616002Sroot 
5626002Sroot 	if ((n = nfrontp - nbackp) > 0)
5636002Sroot 		n = write(net, nbackp, n);
5648346Ssam 	if (n < 0) {
5658346Ssam 		if (errno == EWOULDBLOCK)
5668346Ssam 			return;
5678346Ssam 		/* should blow this guy away... */
5688346Ssam 		return;
5698346Ssam 	}
5706002Sroot 	nbackp += n;
5716002Sroot 	if (nbackp == nfrontp)
5726002Sroot 		nbackp = nfrontp = netobuf;
5736002Sroot }
5746002Sroot 
5756002Sroot cleanup()
5766002Sroot {
5776002Sroot 
5786002Sroot 	rmut();
57910008Ssam 	vhangup();	/* XXX */
58010191Ssam 	shutdown(net, 2);
5816002Sroot 	exit(1);
5826002Sroot }
5836002Sroot 
5846002Sroot #include <utmp.h>
5856002Sroot 
5866002Sroot struct	utmp wtmp;
5876002Sroot char	wtmpf[]	= "/usr/adm/wtmp";
58823567Sbloom char	utmpf[] = "/etc/utmp";
58923567Sbloom #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
59023567Sbloom #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
5916002Sroot 
5926002Sroot rmut()
5936002Sroot {
5946002Sroot 	register f;
5956002Sroot 	int found = 0;
59623567Sbloom 	struct utmp *u, *utmp;
59723567Sbloom 	int nutmp;
59823567Sbloom 	struct stat statbf;
5996002Sroot 
60023567Sbloom 	f = open(utmpf, O_RDWR);
6016002Sroot 	if (f >= 0) {
60223567Sbloom 		fstat(f, &statbf);
60323567Sbloom 		utmp = (struct utmp *)malloc(statbf.st_size);
60423567Sbloom 		if (!utmp)
60523567Sbloom 			syslog(LOG_ERR, "utmp malloc failed");
60623567Sbloom 		if (statbf.st_size && utmp) {
60723567Sbloom 			nutmp = read(f, utmp, statbf.st_size);
60823567Sbloom 			nutmp /= sizeof(struct utmp);
60923567Sbloom 
61023567Sbloom 			for (u = utmp ; u < &utmp[nutmp] ; u++) {
61123567Sbloom 				if (SCMPN(u->ut_line, line+5) ||
61223567Sbloom 				    u->ut_name[0]==0)
61323567Sbloom 					continue;
61423567Sbloom 				lseek(f, ((long)u)-((long)utmp), L_SET);
61523567Sbloom 				SCPYN(u->ut_name, "");
61623567Sbloom 				SCPYN(u->ut_host, "");
61723567Sbloom 				time(&u->ut_time);
61823567Sbloom 				write(f, (char *)u, sizeof(wtmp));
61923567Sbloom 				found++;
62023567Sbloom 			}
6216002Sroot 		}
6226002Sroot 		close(f);
6236002Sroot 	}
6246002Sroot 	if (found) {
62517583Ssam 		f = open(wtmpf, O_WRONLY|O_APPEND);
6266002Sroot 		if (f >= 0) {
6276002Sroot 			SCPYN(wtmp.ut_line, line+5);
6286002Sroot 			SCPYN(wtmp.ut_name, "");
62912683Ssam 			SCPYN(wtmp.ut_host, "");
6306002Sroot 			time(&wtmp.ut_time);
63123567Sbloom 			write(f, (char *)&wtmp, sizeof(wtmp));
6326002Sroot 			close(f);
6336002Sroot 		}
6346002Sroot 	}
6356002Sroot 	chmod(line, 0666);
6366002Sroot 	chown(line, 0, 0);
6376002Sroot 	line[strlen("/dev/")] = 'p';
6386002Sroot 	chmod(line, 0666);
6396002Sroot 	chown(line, 0, 0);
6406002Sroot }
641