16446Swnj #ifndef lint 2*10585Ssam static char sccsid[] = "@(#)rlogind.c 4.10 83/01/22"; 36446Swnj #endif 46446Swnj 56446Swnj #include <stdio.h> 66446Swnj #include <sys/types.h> 76446Swnj #include <sys/stat.h> 86446Swnj #include <sys/socket.h> 99208Ssam 109208Ssam #include <netinet/in.h> 119208Ssam 126446Swnj #include <errno.h> 136446Swnj #include <pwd.h> 146446Swnj #include <wait.h> 156446Swnj #include <signal.h> 166446Swnj #include <sgtty.h> 176446Swnj #include <stdio.h> 188380Ssam #include <netdb.h> 196446Swnj 206446Swnj extern errno; 2110417Ssam int reapchild(); 226446Swnj struct passwd *getpwnam(); 238380Ssam char *crypt(), *rindex(), *index(), *malloc(); 248380Ssam struct sockaddr_in sin = { AF_INET }; 256446Swnj /* 266446Swnj * remote login server: 276446Swnj * remuser\0 286446Swnj * locuser\0 296446Swnj * terminal type\0 306446Swnj * data 316446Swnj */ 326446Swnj main(argc, argv) 336446Swnj int argc; 346446Swnj char **argv; 356446Swnj { 3610417Ssam int f, options = SO_KEEPALIVE; 376446Swnj struct sockaddr_in from; 388380Ssam struct servent *sp; 396446Swnj 408380Ssam sp = getservbyname("login", "tcp"); 418380Ssam if (sp == 0) { 428380Ssam fprintf(stderr, "rlogind: tcp/rlogin: unknown service\n"); 438380Ssam exit(1); 448380Ssam } 456446Swnj #ifndef DEBUG 466446Swnj if (fork()) 476446Swnj exit(0); 486446Swnj for (f = 0; f < 10; f++) 496446Swnj (void) close(f); 506446Swnj (void) open("/", 0); 516446Swnj (void) dup2(0, 1); 526446Swnj (void) dup2(0, 2); 536446Swnj { int tt = open("/dev/tty", 2); 546446Swnj if (tt > 0) { 556446Swnj ioctl(tt, TIOCNOTTY, 0); 566446Swnj close(tt); 576446Swnj } 586446Swnj } 596446Swnj #endif 609961Ssam sin.sin_port = sp->s_port; 616446Swnj argc--, argv++; 629208Ssam if (argc > 0 && !strcmp(argv[0], "-d")) { 6310417Ssam options |= SO_DEBUG; 6410417Ssam argc--, argv++; 6510417Ssam } 6610417Ssam if (argc > 0) { 678380Ssam int port = atoi(argv[0]); 688380Ssam 698380Ssam if (port < 0) { 708380Ssam fprintf(stderr, "%s: bad port #\n", argv[0]); 718380Ssam exit(1); 728380Ssam } 739961Ssam sin.sin_port = htons((u_short)port); 748380Ssam argv++, argc--; 758380Ssam } 769259Ssam f = socket(AF_INET, SOCK_STREAM, 0, 0); 779208Ssam if (f < 0) { 789208Ssam perror("rlogind: socket"); 799208Ssam exit(1); 809208Ssam } 8110417Ssam if (options & SO_DEBUG) 8210417Ssam if (setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) 8310417Ssam perror("rlogind: setsockopt (SO_DEBUG)"); 8410417Ssam #ifdef notdef 8510417Ssam if (setsockopt(f, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) 8610417Ssam perror("rlogind: setsocktopt (SO_KEEPALIVE)"); 8710417Ssam #endif 889208Ssam if (bind(f, &sin, sizeof (sin), 0) < 0) { 899208Ssam perror("rlogind: bind"); 909208Ssam exit(1); 919208Ssam } 92*10585Ssam sigset(SIGCHLD, reapchild); 939208Ssam listen(f, 10); 946446Swnj for (;;) { 959208Ssam int s, len = sizeof (from); 969208Ssam 979208Ssam s = accept(f, &from, &len, 0); 989208Ssam if (s < 0) { 9910417Ssam if (errno == EINTR) 10010417Ssam continue; 1019242Ssam perror("rlogind: accept"); 1026446Swnj continue; 1036446Swnj } 1046446Swnj if (fork() == 0) 1059208Ssam doit(s, &from); 1069208Ssam close(s); 1076446Swnj } 1086446Swnj } 1096446Swnj 11010417Ssam reapchild() 11110417Ssam { 11210417Ssam union wait status; 11310417Ssam 11410417Ssam while (wait3(&status, WNOHANG, 0) > 0) 11510417Ssam ; 11610417Ssam } 11710417Ssam 1186446Swnj char locuser[32], remuser[32]; 1196446Swnj char buf[BUFSIZ]; 1206446Swnj int child; 1216446Swnj int cleanup(); 1226446Swnj int netf; 1236446Swnj extern errno; 1246446Swnj char *line; 1256446Swnj 1266446Swnj doit(f, fromp) 1276446Swnj int f; 1286446Swnj struct sockaddr_in *fromp; 1296446Swnj { 1308380Ssam char c; 1319242Ssam int i, p, cc, t, pid; 1326446Swnj int stop = TIOCPKT_DOSTOP; 1338380Ssam register struct hostent *hp; 1346446Swnj 1356446Swnj alarm(60); 1366446Swnj read(f, &c, 1); 1376446Swnj if (c != 0) 1386446Swnj exit(1); 1396446Swnj alarm(0); 1409961Ssam fromp->sin_port = htons((u_short)fromp->sin_port); 1418380Ssam hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 1428380Ssam fromp->sin_family); 1439242Ssam if (hp == 0) 1449242Ssam fatal(f, "Host name for your address unknown"); 1456446Swnj if (fromp->sin_family != AF_INET || 1466446Swnj fromp->sin_port >= IPPORT_RESERVED || 1479242Ssam hp == 0) 1489242Ssam fatal(f, "Permission denied"); 1496446Swnj write(f, "", 1); 1506446Swnj for (c = 'p'; c <= 's'; c++) { 1516446Swnj struct stat stb; 1526446Swnj line = "/dev/ptyXX"; 1536446Swnj line[strlen("/dev/pty")] = c; 1546446Swnj line[strlen("/dev/ptyp")] = '0'; 1556446Swnj if (stat(line, &stb) < 0) 1566446Swnj break; 1576446Swnj for (i = 0; i < 16; i++) { 1586446Swnj line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 1596446Swnj p = open(line, 2); 1606446Swnj if (p > 0) 1616446Swnj goto gotpty; 1626446Swnj } 1636446Swnj } 1649242Ssam fatal(f, "All network ports in use"); 1659242Ssam /*NOTREACHED*/ 1666446Swnj gotpty: 1676446Swnj dup2(f, 0); 1686446Swnj line[strlen("/dev/")] = 't'; 1696446Swnj #ifdef DEBUG 1706446Swnj { int tt = open("/dev/tty", 2); 1716446Swnj if (tt > 0) { 1726446Swnj ioctl(tt, TIOCNOTTY, 0); 1736446Swnj close(tt); 1746446Swnj } 1756446Swnj } 1766446Swnj #endif 1776446Swnj t = open(line, 2); 1789242Ssam if (t < 0) 1799242Ssam fatalperror(f, line, errno); 1806446Swnj { struct sgttyb b; 1816446Swnj gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 1826446Swnj } 1839242Ssam pid = fork(); 1849242Ssam if (pid < 0) 1859242Ssam fatalperror(f, "", errno); 1869242Ssam if (pid) { 1876446Swnj char pibuf[1024], fibuf[1024], *pbp, *fbp; 1886446Swnj int pcc = 0, fcc = 0, on = 1; 1896446Swnj /* FILE *console = fopen("/dev/console", "w"); */ 1906446Swnj /* setbuf(console, 0); */ 1916446Swnj 1926446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */ 1936446Swnj ioctl(f, FIONBIO, &on); 1946446Swnj ioctl(p, FIONBIO, &on); 1956446Swnj ioctl(p, TIOCPKT, &on); 1966446Swnj signal(SIGTSTP, SIG_IGN); 1976446Swnj sigset(SIGCHLD, cleanup); 1986446Swnj for (;;) { 1996446Swnj int ibits = 0, obits = 0; 2009242Ssam 2019242Ssam if (fcc) 2029242Ssam obits |= (1<<p); 2039242Ssam else 2049242Ssam ibits |= (1<<f); 2056446Swnj if (pcc >= 0) 2069242Ssam if (pcc) 2079242Ssam obits |= (1<<f); 2089242Ssam else 2099242Ssam ibits |= (1<<p); 2109242Ssam if (fcc < 0 && pcc < 0) 2119242Ssam break; 2126446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */ 2139208Ssam select(16, &ibits, &obits, 0, 0, 0); 2146446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */ 2156446Swnj if (ibits == 0 && obits == 0) { 2166446Swnj sleep(5); 2176446Swnj continue; 2186446Swnj } 2196446Swnj if (ibits & (1<<f)) { 2206446Swnj fcc = read(f, fibuf, sizeof (fibuf)); 2216446Swnj /* fprintf(console, "%d from f\r\n", fcc); */ 2226446Swnj if (fcc < 0 && errno == EWOULDBLOCK) 2236446Swnj fcc = 0; 2246446Swnj else { 2256446Swnj if (fcc <= 0) 2266446Swnj break; 2276446Swnj fbp = fibuf; 2286446Swnj } 2296446Swnj } 2306446Swnj if (ibits & (1<<p)) { 2316446Swnj pcc = read(p, pibuf, sizeof (pibuf)); 2326446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */ 2336446Swnj pbp = pibuf; 2346446Swnj if (pcc < 0 && errno == EWOULDBLOCK) 2356446Swnj pcc = 0; 2366446Swnj else if (pcc <= 0) 2376446Swnj pcc = -1; 2386446Swnj else if (pibuf[0] == 0) 2396446Swnj pbp++, pcc--; 2406446Swnj else { 2416446Swnj if (pibuf[0]&(TIOCPKT_FLUSHWRITE| 2426446Swnj TIOCPKT_NOSTOP| 2436446Swnj TIOCPKT_DOSTOP)) { 2446446Swnj int nstop = pibuf[0] & 2456446Swnj (TIOCPKT_NOSTOP| 2466446Swnj TIOCPKT_DOSTOP); 2476446Swnj if (nstop) 2486446Swnj stop = nstop; 2496446Swnj pibuf[0] |= nstop; 2509208Ssam send(f,&pibuf[0],1,SOF_OOB); 2516446Swnj } 2526446Swnj pcc = 0; 2536446Swnj } 2546446Swnj } 2556446Swnj if ((obits & (1<<f)) && pcc > 0) { 2566446Swnj cc = write(f, pbp, pcc); 2576446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */ 2586446Swnj if (cc > 0) { 2596446Swnj pcc -= cc; 2606446Swnj pbp += cc; 2616446Swnj } 2626446Swnj } 2636446Swnj if ((obits & (1<<p)) && fcc > 0) { 2646446Swnj cc = write(p, fbp, fcc); 2656446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */ 2666446Swnj if (cc > 0) { 2676446Swnj fcc -= cc; 2686446Swnj fbp += cc; 2696446Swnj } 2706446Swnj } 2716446Swnj } 2726446Swnj cleanup(); 2736446Swnj } 2746446Swnj close(f); 2756446Swnj close(p); 2766446Swnj dup2(t, 0); 2776446Swnj dup2(t, 1); 2786446Swnj dup2(t, 2); 2796446Swnj close(t); 2808380Ssam execl("/bin/login", "login", "-r", hp->h_name, 0); 2819242Ssam fatalperror(2, "/bin/login", errno); 2829242Ssam /*NOTREACHED*/ 2836446Swnj } 2846446Swnj 2856446Swnj cleanup() 2866446Swnj { 2876446Swnj 2886446Swnj rmut(); 28910009Ssam vhangup(); /* XXX */ 29010192Ssam shutdown(netf, 2); 2916446Swnj kill(0, SIGKILL); 2926446Swnj exit(1); 2936446Swnj } 2946446Swnj 2959242Ssam fatal(f, msg) 2969242Ssam int f; 2979242Ssam char *msg; 2989242Ssam { 2999242Ssam char buf[BUFSIZ]; 3009242Ssam 3019242Ssam buf[0] = '\01'; /* error indicator */ 3029242Ssam (void) sprintf(buf + 1, "rlogind: %s.\n", msg); 3039242Ssam (void) write(f, buf, strlen(buf)); 3049242Ssam exit(1); 3059242Ssam } 3069242Ssam 3079242Ssam fatalperror(f, msg, errno) 3089242Ssam int f; 3099242Ssam char *msg; 3109242Ssam int errno; 3119242Ssam { 3129242Ssam char buf[BUFSIZ]; 3139242Ssam extern char *sys_errlist[]; 3149242Ssam 3159242Ssam (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 3169242Ssam fatal(f, buf); 3179242Ssam } 3189242Ssam 3196446Swnj #include <utmp.h> 3206446Swnj 3216446Swnj struct utmp wtmp; 3226446Swnj char wtmpf[] = "/usr/adm/wtmp"; 3236446Swnj char utmp[] = "/etc/utmp"; 3246446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 3256446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 3266446Swnj 3276446Swnj rmut() 3286446Swnj { 3296446Swnj register f; 3306446Swnj int found = 0; 3316446Swnj 3326446Swnj f = open(utmp, 2); 3336446Swnj if (f >= 0) { 3346446Swnj while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) { 3356446Swnj if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 3366446Swnj continue; 3376446Swnj lseek(f, -(long)sizeof(wtmp), 1); 3386446Swnj SCPYN(wtmp.ut_name, ""); 3396446Swnj time(&wtmp.ut_time); 3406446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3416446Swnj found++; 3426446Swnj } 3436446Swnj close(f); 3446446Swnj } 3456446Swnj if (found) { 3466446Swnj f = open(wtmpf, 1); 3476446Swnj if (f >= 0) { 3486446Swnj SCPYN(wtmp.ut_line, line+5); 3496446Swnj SCPYN(wtmp.ut_name, ""); 3506446Swnj time(&wtmp.ut_time); 3516446Swnj lseek(f, (long)0, 2); 3526446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3536446Swnj close(f); 3546446Swnj } 3556446Swnj } 3566446Swnj chmod(line, 0666); 3576446Swnj chown(line, 0, 0); 3586446Swnj line[strlen("/dev/")] = 'p'; 3596446Swnj chmod(line, 0666); 3606446Swnj chown(line, 0, 0); 3616446Swnj } 362