16295Sroot #ifndef lint 2*13798Ssam static char sccsid[] = "@(#)telnetd.c 4.24 (Berkeley) 83/07/06"; 36295Sroot #endif 46295Sroot 56002Sroot /* 66002Sroot * Stripped-down telnet server. 76002Sroot */ 89218Ssam #include <sys/types.h> 99218Ssam #include <sys/socket.h> 1013608Ssam #include <sys/wait.h> 119218Ssam 129218Ssam #include <netinet/in.h> 139218Ssam 1412216Ssam #include <arpa/telnet.h> 1512216Ssam 166002Sroot #include <stdio.h> 176002Sroot #include <signal.h> 186002Sroot #include <errno.h> 196002Sroot #include <sgtty.h> 208346Ssam #include <netdb.h> 219218Ssam 22*13798Ssam #define BELL '\07' 23*13798Ssam #define BANNER "\r\n\r\n4.2 BSD UNIX (%s)\r\n\r\r\n\r%s" 246002Sroot 256002Sroot char hisopts[256]; 266002Sroot char myopts[256]; 276002Sroot 286002Sroot char doopt[] = { IAC, DO, '%', 'c', 0 }; 296002Sroot char dont[] = { IAC, DONT, '%', 'c', 0 }; 306002Sroot char will[] = { IAC, WILL, '%', 'c', 0 }; 316002Sroot char wont[] = { IAC, WONT, '%', 'c', 0 }; 326002Sroot 336002Sroot /* 346002Sroot * I/O data buffers, pointers, and counters. 356002Sroot */ 366002Sroot char ptyibuf[BUFSIZ], *ptyip = ptyibuf; 376002Sroot char ptyobuf[BUFSIZ], *pfrontp = ptyobuf, *pbackp = ptyobuf; 386002Sroot char netibuf[BUFSIZ], *netip = netibuf; 396388Ssam char netobuf[BUFSIZ], *nfrontp = netobuf, *nbackp = netobuf; 406002Sroot int pcc, ncc; 416002Sroot 426002Sroot int pty, net; 436002Sroot int inter; 4410418Ssam int reapchild(); 456002Sroot extern int errno; 466002Sroot char line[] = "/dev/ptyp0"; 476002Sroot 488346Ssam struct sockaddr_in sin = { AF_INET }; 496002Sroot 506002Sroot main(argc, argv) 516002Sroot char *argv[]; 526002Sroot { 5310418Ssam int s, pid, options; 548346Ssam struct servent *sp; 556002Sroot 568346Ssam sp = getservbyname("telnet", "tcp"); 578346Ssam if (sp == 0) { 588346Ssam fprintf(stderr, "telnetd: tcp/telnet: unknown service\n"); 598346Ssam exit(1); 608346Ssam } 618346Ssam sin.sin_port = sp->s_port; 626002Sroot argc--, argv++; 6310418Ssam if (argc > 0 && !strcmp(*argv, "-d")) { 6410418Ssam options |= SO_DEBUG; 6510418Ssam argc--, argv++; 6610418Ssam } 678346Ssam if (argc > 0) { 688346Ssam sin.sin_port = atoi(*argv); 698346Ssam if (sin.sin_port <= 0) { 708346Ssam fprintf(stderr, "telnetd: %s: bad port #\n", *argv); 718346Ssam exit(1); 728346Ssam } 739969Ssam sin.sin_port = htons((u_short)sin.sin_port); 748346Ssam } 758456Ssam #ifndef DEBUG 768456Ssam if (fork()) 778456Ssam exit(0); 788456Ssam for (s = 0; s < 10; s++) 798456Ssam (void) close(s); 808456Ssam (void) open("/", 0); 818456Ssam (void) dup2(0, 1); 828456Ssam (void) dup2(0, 2); 838456Ssam { int tt = open("/dev/tty", 2); 848456Ssam if (tt > 0) { 858456Ssam ioctl(tt, TIOCNOTTY, 0); 868456Ssam close(tt); 878456Ssam } 888456Ssam } 898456Ssam #endif 909218Ssam again: 919288Ssam s = socket(AF_INET, SOCK_STREAM, 0, 0); 929218Ssam if (s < 0) { 939218Ssam perror("telnetd: socket");; 949218Ssam sleep(5); 959218Ssam goto again; 969218Ssam } 9710418Ssam if (options & SO_DEBUG) 9810418Ssam if (setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) 9910418Ssam perror("telnetd: setsockopt (SO_DEBUG)"); 10010418Ssam if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) 10110418Ssam perror("telnetd: setsockopt (SO_KEEPALIVE)"); 1029218Ssam while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) { 1039218Ssam perror("telnetd: bind"); 1049218Ssam sleep(5); 1059218Ssam } 10613028Ssam signal(SIGCHLD, reapchild); 1079218Ssam listen(s, 10); 1086002Sroot for (;;) { 10912683Ssam struct sockaddr_in from; 11012683Ssam int s2, fromlen = sizeof (from); 1119218Ssam 11212683Ssam s2 = accept(s, (caddr_t)&from, &fromlen); 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"); 12211221Ssam else if (pid == 0) { 12311221Ssam signal(SIGCHLD, SIG_IGN); 12412683Ssam doit(s2, &from); 12511221Ssam } 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 */ 14412683Ssam doit(f, who) 14512683Ssam int f; 14612683Ssam struct sockaddr_in *who; 1476002Sroot { 14812683Ssam char *cp = line, *host, *ntoa(); 1496002Sroot int i, p, cc, t; 1506002Sroot struct sgttyb b; 15112683Ssam struct hostent *hp; 1526002Sroot 1536002Sroot for (i = 0; i < 16; i++) { 1546002Sroot cp[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 1556002Sroot p = open(cp, 2); 1566002Sroot if (p > 0) 1576002Sroot goto gotpty; 1586002Sroot } 1599244Ssam fatal(f, "All network ports in use"); 1609244Ssam /*NOTREACHED*/ 1616002Sroot gotpty: 1626002Sroot dup2(f, 0); 1636002Sroot cp[strlen("/dev/")] = 't'; 1646002Sroot t = open("/dev/tty", 2); 1656002Sroot if (t >= 0) { 1666002Sroot ioctl(t, TIOCNOTTY, 0); 1676002Sroot close(t); 1686002Sroot } 1696002Sroot t = open(cp, 2); 1709244Ssam if (t < 0) 1719244Ssam fatalperror(f, cp, errno); 1726002Sroot ioctl(t, TIOCGETP, &b); 1736388Ssam b.sg_flags = CRMOD|XTABS|ANYP; 1746002Sroot ioctl(t, TIOCSETP, &b); 1756388Ssam ioctl(p, TIOCGETP, &b); 1768379Ssam b.sg_flags &= ~ECHO; 1776388Ssam ioctl(p, TIOCSETP, &b); 17812683Ssam hp = gethostbyaddr(&who->sin_addr, sizeof (struct in_addr), 17912683Ssam who->sin_family); 18012683Ssam if (hp) 18112683Ssam host = hp->h_name; 18212683Ssam else 18312683Ssam host = ntoa(who->sin_addr); 1849244Ssam if ((i = fork()) < 0) 1859244Ssam fatalperror(f, "fork", errno); 1866002Sroot if (i) 1876002Sroot telnet(f, p); 1886002Sroot close(f); 1896002Sroot close(p); 1906002Sroot dup2(t, 0); 1916002Sroot dup2(t, 1); 1926002Sroot dup2(t, 2); 1936002Sroot close(t); 19412713Ssam execl("/bin/login", "login", "-h", host, 0); 1959244Ssam fatalperror(f, "/bin/login", errno); 1969244Ssam /*NOTREACHED*/ 1979244Ssam } 1989244Ssam 1999244Ssam fatal(f, msg) 2009244Ssam int f; 2019244Ssam char *msg; 2029244Ssam { 2039244Ssam char buf[BUFSIZ]; 2049244Ssam 2059244Ssam (void) sprintf(buf, "telnetd: %s.\n", msg); 2069244Ssam (void) write(f, buf, strlen(buf)); 2076002Sroot exit(1); 2086002Sroot } 2096002Sroot 2109244Ssam fatalperror(f, msg, errno) 2119244Ssam int f; 2129244Ssam char *msg; 2139244Ssam int errno; 2149244Ssam { 2159244Ssam char buf[BUFSIZ]; 2169244Ssam extern char *sys_errlist[]; 2179244Ssam 2189244Ssam (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 2199244Ssam fatal(f, buf); 2209244Ssam } 2219244Ssam 2226002Sroot /* 2236002Sroot * Main loop. Select from pty and network, and 2246002Sroot * hand data to telnet receiver finite state machine. 2256002Sroot */ 2266002Sroot telnet(f, p) 2276002Sroot { 2286002Sroot int on = 1; 22912713Ssam char hostname[32]; 2306002Sroot 2316002Sroot net = f, pty = p; 2326002Sroot ioctl(f, FIONBIO, &on); 2336002Sroot ioctl(p, FIONBIO, &on); 2346002Sroot signal(SIGTSTP, SIG_IGN); 23513028Ssam signal(SIGCHLD, cleanup); 2366002Sroot 2378379Ssam /* 2388379Ssam * Request to do remote echo. 2398379Ssam */ 2408379Ssam dooption(TELOPT_ECHO); 2418379Ssam myopts[TELOPT_ECHO] = 1; 24212713Ssam /* 24312713Ssam * Show banner that getty never gave. 24412713Ssam */ 24512713Ssam gethostname(hostname, sizeof (hostname)); 24612713Ssam sprintf(nfrontp, BANNER, hostname, ""); 24712713Ssam nfrontp += strlen(nfrontp); 2486002Sroot for (;;) { 2496002Sroot int ibits = 0, obits = 0; 2506002Sroot register int c; 2516002Sroot 2526002Sroot /* 2536002Sroot * Never look for input if there's still 2546002Sroot * stuff in the corresponding output buffer 2556002Sroot */ 2566002Sroot if (nfrontp - nbackp) 2576002Sroot obits |= (1 << f); 2586002Sroot else 2596002Sroot ibits |= (1 << p); 2606002Sroot if (pfrontp - pbackp) 2616002Sroot obits |= (1 << p); 2626002Sroot else 2636002Sroot ibits |= (1 << f); 2646002Sroot if (ncc < 0 && pcc < 0) 2656002Sroot break; 2669218Ssam select(16, &ibits, &obits, 0, 0); 2676002Sroot if (ibits == 0 && obits == 0) { 2686002Sroot sleep(5); 2696002Sroot continue; 2706002Sroot } 2716002Sroot 2726002Sroot /* 2736002Sroot * Something to read from the network... 2746002Sroot */ 2756002Sroot if (ibits & (1 << f)) { 2766002Sroot ncc = read(f, netibuf, BUFSIZ); 2776002Sroot if (ncc < 0 && errno == EWOULDBLOCK) 2786002Sroot ncc = 0; 2796002Sroot else { 2806002Sroot if (ncc <= 0) 2816002Sroot break; 2826002Sroot netip = netibuf; 2836002Sroot } 2846002Sroot } 2856002Sroot 2866002Sroot /* 2876002Sroot * Something to read from the pty... 2886002Sroot */ 2896002Sroot if (ibits & (1 << p)) { 2906002Sroot pcc = read(p, ptyibuf, BUFSIZ); 2916002Sroot if (pcc < 0 && errno == EWOULDBLOCK) 2926002Sroot pcc = 0; 2936002Sroot else { 2946002Sroot if (pcc <= 0) 2956002Sroot break; 2966002Sroot ptyip = ptyibuf; 2976002Sroot } 2986002Sroot } 2996002Sroot 3006002Sroot while (pcc > 0) { 3016002Sroot if ((&netobuf[BUFSIZ] - nfrontp) < 2) 3026002Sroot break; 3036002Sroot c = *ptyip++ & 0377, pcc--; 3046002Sroot if (c == IAC) 3056002Sroot *nfrontp++ = c; 3066002Sroot *nfrontp++ = c; 3076002Sroot } 3086002Sroot if ((obits & (1 << f)) && (nfrontp - nbackp) > 0) 3096002Sroot netflush(); 3106002Sroot if (ncc > 0) 3116002Sroot telrcv(); 3126002Sroot if ((obits & (1 << p)) && (pfrontp - pbackp) > 0) 3136002Sroot ptyflush(); 3146002Sroot } 3156002Sroot cleanup(); 3166002Sroot } 3176002Sroot 3186002Sroot /* 3196002Sroot * State for recv fsm 3206002Sroot */ 3216002Sroot #define TS_DATA 0 /* base state */ 3226002Sroot #define TS_IAC 1 /* look for double IAC's */ 3236002Sroot #define TS_CR 2 /* CR-LF ->'s CR */ 3246002Sroot #define TS_BEGINNEG 3 /* throw away begin's... */ 3256002Sroot #define TS_ENDNEG 4 /* ...end's (suboption negotiation) */ 3266002Sroot #define TS_WILL 5 /* will option negotiation */ 3276002Sroot #define TS_WONT 6 /* wont " */ 3286002Sroot #define TS_DO 7 /* do " */ 3296002Sroot #define TS_DONT 8 /* dont " */ 3306002Sroot 3316002Sroot telrcv() 3326002Sroot { 3336002Sroot register int c; 3346002Sroot static int state = TS_DATA; 3356002Sroot struct sgttyb b; 3366002Sroot 3376002Sroot while (ncc > 0) { 3386002Sroot if ((&ptyobuf[BUFSIZ] - pfrontp) < 2) 3396002Sroot return; 3406002Sroot c = *netip++ & 0377, ncc--; 3416002Sroot switch (state) { 3426002Sroot 3436002Sroot case TS_DATA: 3446002Sroot if (c == IAC) { 3456002Sroot state = TS_IAC; 3466002Sroot break; 3476002Sroot } 3486002Sroot if (inter > 0) 3496002Sroot break; 3506002Sroot *pfrontp++ = c; 3516002Sroot if (!myopts[TELOPT_BINARY] && c == '\r') 3526002Sroot state = TS_CR; 3536002Sroot break; 3546002Sroot 3556002Sroot case TS_CR: 3566002Sroot if (c && c != '\n') 3576002Sroot *pfrontp++ = c; 3586002Sroot state = TS_DATA; 3596002Sroot break; 3606002Sroot 3616002Sroot case TS_IAC: 3626002Sroot switch (c) { 3636002Sroot 3646002Sroot /* 3656002Sroot * Send the process on the pty side an 3666002Sroot * interrupt. Do this with a NULL or 3676002Sroot * interrupt char; depending on the tty mode. 3686002Sroot */ 3696002Sroot case BREAK: 3706002Sroot case IP: 3716002Sroot interrupt(); 3726002Sroot break; 3736002Sroot 3746002Sroot /* 3756002Sroot * Are You There? 3766002Sroot */ 3776002Sroot case AYT: 3786002Sroot *pfrontp++ = BELL; 3796002Sroot break; 3806002Sroot 3816002Sroot /* 3826002Sroot * Erase Character and 3836002Sroot * Erase Line 3846002Sroot */ 3856002Sroot case EC: 3866002Sroot case EL: 3876002Sroot ptyflush(); /* half-hearted */ 3886002Sroot ioctl(pty, TIOCGETP, &b); 3896002Sroot *pfrontp++ = (c == EC) ? 3906002Sroot b.sg_erase : b.sg_kill; 3916002Sroot break; 3926002Sroot 3936002Sroot /* 3946002Sroot * Check for urgent data... 3956002Sroot */ 3966002Sroot case DM: 3976002Sroot break; 3986002Sroot 3996002Sroot /* 4006002Sroot * Begin option subnegotiation... 4016002Sroot */ 4026002Sroot case SB: 4036002Sroot state = TS_BEGINNEG; 4046002Sroot continue; 4056002Sroot 4066002Sroot case WILL: 4076002Sroot case WONT: 4086002Sroot case DO: 4096002Sroot case DONT: 4106002Sroot state = TS_WILL + (c - WILL); 4116002Sroot continue; 4126002Sroot 4136002Sroot case IAC: 4146002Sroot *pfrontp++ = c; 4156002Sroot break; 4166002Sroot } 4176002Sroot state = TS_DATA; 4186002Sroot break; 4196002Sroot 4206002Sroot case TS_BEGINNEG: 4216002Sroot if (c == IAC) 4226002Sroot state = TS_ENDNEG; 4236002Sroot break; 4246002Sroot 4256002Sroot case TS_ENDNEG: 4266002Sroot state = c == SE ? TS_DATA : TS_BEGINNEG; 4276002Sroot break; 4286002Sroot 4296002Sroot case TS_WILL: 4306002Sroot if (!hisopts[c]) 4316002Sroot willoption(c); 4326002Sroot state = TS_DATA; 4336002Sroot continue; 4346002Sroot 4356002Sroot case TS_WONT: 4366002Sroot if (hisopts[c]) 4376002Sroot wontoption(c); 4386002Sroot state = TS_DATA; 4396002Sroot continue; 4406002Sroot 4416002Sroot case TS_DO: 4426002Sroot if (!myopts[c]) 4436002Sroot dooption(c); 4446002Sroot state = TS_DATA; 4456002Sroot continue; 4466002Sroot 4476002Sroot case TS_DONT: 4486002Sroot if (myopts[c]) { 4496002Sroot myopts[c] = 0; 4506002Sroot sprintf(nfrontp, wont, c); 4518379Ssam nfrontp += sizeof (wont) - 2; 4526002Sroot } 4536002Sroot state = TS_DATA; 4546002Sroot continue; 4556002Sroot 4566002Sroot default: 4579218Ssam printf("telnetd: panic state=%d\n", state); 4586002Sroot exit(1); 4596002Sroot } 4606002Sroot } 4616002Sroot } 4626002Sroot 4636002Sroot willoption(option) 4646002Sroot int option; 4656002Sroot { 4666002Sroot char *fmt; 4676002Sroot 4686002Sroot switch (option) { 4696002Sroot 4706002Sroot case TELOPT_BINARY: 4716002Sroot mode(RAW, 0); 4726002Sroot goto common; 4736002Sroot 4746002Sroot case TELOPT_ECHO: 4756002Sroot mode(0, ECHO|CRMOD); 4766002Sroot /*FALL THRU*/ 4776002Sroot 4786002Sroot case TELOPT_SGA: 4796002Sroot common: 4806002Sroot hisopts[option] = 1; 4816002Sroot fmt = doopt; 4826002Sroot break; 4836002Sroot 4846002Sroot case TELOPT_TM: 4856002Sroot fmt = dont; 4866002Sroot break; 4876002Sroot 4886002Sroot default: 4896002Sroot fmt = dont; 4906002Sroot break; 4916002Sroot } 4926023Ssam sprintf(nfrontp, fmt, option); 4938379Ssam nfrontp += sizeof (dont) - 2; 4946002Sroot } 4956002Sroot 4966002Sroot wontoption(option) 4976002Sroot int option; 4986002Sroot { 4996002Sroot char *fmt; 5006002Sroot 5016002Sroot switch (option) { 5026002Sroot 5036002Sroot case TELOPT_ECHO: 5046002Sroot mode(ECHO|CRMOD, 0); 5056002Sroot goto common; 5066002Sroot 5076002Sroot case TELOPT_BINARY: 5086002Sroot mode(0, RAW); 5096002Sroot /*FALL THRU*/ 5106002Sroot 5116002Sroot case TELOPT_SGA: 5126002Sroot common: 5136002Sroot hisopts[option] = 0; 5146002Sroot fmt = dont; 5156002Sroot break; 5166002Sroot 5176002Sroot default: 5186002Sroot fmt = dont; 5196002Sroot } 5206002Sroot sprintf(nfrontp, fmt, option); 5218379Ssam nfrontp += sizeof (doopt) - 2; 5226002Sroot } 5236002Sroot 5246002Sroot dooption(option) 5256002Sroot int option; 5266002Sroot { 5276002Sroot char *fmt; 5286002Sroot 5296002Sroot switch (option) { 5306002Sroot 5316002Sroot case TELOPT_TM: 5326002Sroot fmt = wont; 5336002Sroot break; 5346002Sroot 5356002Sroot case TELOPT_ECHO: 5366002Sroot mode(ECHO|CRMOD, 0); 5376002Sroot goto common; 5386002Sroot 5396002Sroot case TELOPT_BINARY: 5406002Sroot mode(RAW, 0); 5416002Sroot /*FALL THRU*/ 5426002Sroot 5436002Sroot case TELOPT_SGA: 5446002Sroot common: 5456002Sroot fmt = will; 5466002Sroot break; 5476002Sroot 5486002Sroot default: 5496002Sroot fmt = wont; 5506002Sroot break; 5516002Sroot } 5526002Sroot sprintf(nfrontp, fmt, option); 5538379Ssam nfrontp += sizeof (doopt) - 2; 5546002Sroot } 5556002Sroot 5566002Sroot mode(on, off) 5576002Sroot int on, off; 5586002Sroot { 5596002Sroot struct sgttyb b; 5606002Sroot 5616002Sroot ptyflush(); 5626002Sroot ioctl(pty, TIOCGETP, &b); 5636002Sroot b.sg_flags |= on; 5646002Sroot b.sg_flags &= ~off; 5656002Sroot ioctl(pty, TIOCSETP, &b); 5666002Sroot } 5676002Sroot 5686002Sroot /* 5696002Sroot * Send interrupt to process on other side of pty. 5706002Sroot * If it is in raw mode, just write NULL; 5716002Sroot * otherwise, write intr char. 5726002Sroot */ 5736002Sroot interrupt() 5746002Sroot { 5756002Sroot struct sgttyb b; 5766002Sroot struct tchars tchars; 5776002Sroot 5786002Sroot ptyflush(); /* half-hearted */ 5796002Sroot ioctl(pty, TIOCGETP, &b); 5806002Sroot if (b.sg_flags & RAW) { 5816002Sroot *pfrontp++ = '\0'; 5826002Sroot return; 5836002Sroot } 5846002Sroot *pfrontp++ = ioctl(pty, TIOCGETC, &tchars) < 0 ? 5856002Sroot '\177' : tchars.t_intrc; 5866002Sroot } 5876002Sroot 5886002Sroot ptyflush() 5896002Sroot { 5906002Sroot int n; 5916002Sroot 5926002Sroot if ((n = pfrontp - pbackp) > 0) 5936002Sroot n = write(pty, pbackp, n); 5948346Ssam if (n < 0) 5958346Ssam return; 5966002Sroot pbackp += n; 5976002Sroot if (pbackp == pfrontp) 5986002Sroot pbackp = pfrontp = ptyobuf; 5996002Sroot } 6006002Sroot 6016002Sroot netflush() 6026002Sroot { 6036002Sroot int n; 6046002Sroot 6056002Sroot if ((n = nfrontp - nbackp) > 0) 6066002Sroot n = write(net, nbackp, n); 6078346Ssam if (n < 0) { 6088346Ssam if (errno == EWOULDBLOCK) 6098346Ssam return; 6108346Ssam /* should blow this guy away... */ 6118346Ssam return; 6128346Ssam } 6136002Sroot nbackp += n; 6146002Sroot if (nbackp == nfrontp) 6156002Sroot nbackp = nfrontp = netobuf; 6166002Sroot } 6176002Sroot 6186002Sroot cleanup() 6196002Sroot { 6206002Sroot 6216002Sroot rmut(); 62210008Ssam vhangup(); /* XXX */ 62310191Ssam shutdown(net, 2); 6246002Sroot kill(0, SIGKILL); 6256002Sroot exit(1); 6266002Sroot } 6276002Sroot 6286002Sroot #include <utmp.h> 6296002Sroot 6306002Sroot struct utmp wtmp; 6316002Sroot char wtmpf[] = "/usr/adm/wtmp"; 6326002Sroot char utmp[] = "/etc/utmp"; 6338379Ssam #define SCPYN(a, b) strncpy(a, b, sizeof (a)) 6348379Ssam #define SCMPN(a, b) strncmp(a, b, sizeof (a)) 6356002Sroot 6366002Sroot rmut() 6376002Sroot { 6386002Sroot register f; 6396002Sroot int found = 0; 6406002Sroot 6416002Sroot f = open(utmp, 2); 6426002Sroot if (f >= 0) { 6438379Ssam while(read(f, (char *)&wtmp, sizeof (wtmp)) == sizeof (wtmp)) { 6446002Sroot if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 6456002Sroot continue; 6468379Ssam lseek(f, -(long)sizeof (wtmp), 1); 6476002Sroot SCPYN(wtmp.ut_name, ""); 64812683Ssam SCPYN(wtmp.ut_host, ""); 6496002Sroot time(&wtmp.ut_time); 6508379Ssam write(f, (char *)&wtmp, sizeof (wtmp)); 6516002Sroot found++; 6526002Sroot } 6536002Sroot close(f); 6546002Sroot } 6556002Sroot if (found) { 6566002Sroot f = open(wtmpf, 1); 6576002Sroot if (f >= 0) { 6586002Sroot SCPYN(wtmp.ut_line, line+5); 6596002Sroot SCPYN(wtmp.ut_name, ""); 66012683Ssam SCPYN(wtmp.ut_host, ""); 6616002Sroot time(&wtmp.ut_time); 6626002Sroot lseek(f, (long)0, 2); 6638379Ssam write(f, (char *)&wtmp, sizeof (wtmp)); 6646002Sroot close(f); 6656002Sroot } 6666002Sroot } 6676002Sroot chmod(line, 0666); 6686002Sroot chown(line, 0, 0); 6696002Sroot line[strlen("/dev/")] = 'p'; 6706002Sroot chmod(line, 0666); 6716002Sroot chown(line, 0, 0); 6726002Sroot } 67312683Ssam 67412683Ssam /* 67512683Ssam * Convert network-format internet address 67612683Ssam * to base 256 d.d.d.d representation. 67712683Ssam */ 67812683Ssam char * 67912683Ssam ntoa(in) 68012683Ssam struct in_addr in; 68112683Ssam { 68212683Ssam static char b[18]; 68312683Ssam register char *p; 68412683Ssam 68512683Ssam p = (char *)∈ 68612683Ssam #define UC(b) (((int)b)&0xff) 68712683Ssam sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 68812683Ssam return (b); 68912683Ssam } 690