16446Swnj #ifndef lint 2*11222Ssam static char sccsid[] = "@(#)rlogind.c 4.11 83/02/21"; 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 } 9210585Ssam 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 } 104*11222Ssam if (fork() == 0) { 105*11222Ssam signal(SIGCHLD, SIG_IGN); 1069208Ssam doit(s, &from); 107*11222Ssam } 1089208Ssam close(s); 1096446Swnj } 1106446Swnj } 1116446Swnj 11210417Ssam reapchild() 11310417Ssam { 11410417Ssam union wait status; 11510417Ssam 11610417Ssam while (wait3(&status, WNOHANG, 0) > 0) 11710417Ssam ; 11810417Ssam } 11910417Ssam 1206446Swnj char locuser[32], remuser[32]; 1216446Swnj char buf[BUFSIZ]; 1226446Swnj int child; 1236446Swnj int cleanup(); 1246446Swnj int netf; 1256446Swnj extern errno; 1266446Swnj char *line; 1276446Swnj 1286446Swnj doit(f, fromp) 1296446Swnj int f; 1306446Swnj struct sockaddr_in *fromp; 1316446Swnj { 1328380Ssam char c; 1339242Ssam int i, p, cc, t, pid; 1346446Swnj int stop = TIOCPKT_DOSTOP; 1358380Ssam register struct hostent *hp; 1366446Swnj 1376446Swnj alarm(60); 1386446Swnj read(f, &c, 1); 1396446Swnj if (c != 0) 1406446Swnj exit(1); 1416446Swnj alarm(0); 1429961Ssam fromp->sin_port = htons((u_short)fromp->sin_port); 1438380Ssam hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 1448380Ssam fromp->sin_family); 1459242Ssam if (hp == 0) 1469242Ssam fatal(f, "Host name for your address unknown"); 1476446Swnj if (fromp->sin_family != AF_INET || 1486446Swnj fromp->sin_port >= IPPORT_RESERVED || 1499242Ssam hp == 0) 1509242Ssam fatal(f, "Permission denied"); 1516446Swnj write(f, "", 1); 1526446Swnj for (c = 'p'; c <= 's'; c++) { 1536446Swnj struct stat stb; 1546446Swnj line = "/dev/ptyXX"; 1556446Swnj line[strlen("/dev/pty")] = c; 1566446Swnj line[strlen("/dev/ptyp")] = '0'; 1576446Swnj if (stat(line, &stb) < 0) 1586446Swnj break; 1596446Swnj for (i = 0; i < 16; i++) { 1606446Swnj line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 1616446Swnj p = open(line, 2); 1626446Swnj if (p > 0) 1636446Swnj goto gotpty; 1646446Swnj } 1656446Swnj } 1669242Ssam fatal(f, "All network ports in use"); 1679242Ssam /*NOTREACHED*/ 1686446Swnj gotpty: 1696446Swnj dup2(f, 0); 1706446Swnj line[strlen("/dev/")] = 't'; 1716446Swnj #ifdef DEBUG 1726446Swnj { int tt = open("/dev/tty", 2); 1736446Swnj if (tt > 0) { 1746446Swnj ioctl(tt, TIOCNOTTY, 0); 1756446Swnj close(tt); 1766446Swnj } 1776446Swnj } 1786446Swnj #endif 1796446Swnj t = open(line, 2); 1809242Ssam if (t < 0) 1819242Ssam fatalperror(f, line, errno); 1826446Swnj { struct sgttyb b; 1836446Swnj gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 1846446Swnj } 1859242Ssam pid = fork(); 1869242Ssam if (pid < 0) 1879242Ssam fatalperror(f, "", errno); 1889242Ssam if (pid) { 1896446Swnj char pibuf[1024], fibuf[1024], *pbp, *fbp; 1906446Swnj int pcc = 0, fcc = 0, on = 1; 1916446Swnj /* FILE *console = fopen("/dev/console", "w"); */ 1926446Swnj /* setbuf(console, 0); */ 1936446Swnj 1946446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */ 1956446Swnj ioctl(f, FIONBIO, &on); 1966446Swnj ioctl(p, FIONBIO, &on); 1976446Swnj ioctl(p, TIOCPKT, &on); 1986446Swnj signal(SIGTSTP, SIG_IGN); 1996446Swnj sigset(SIGCHLD, cleanup); 2006446Swnj for (;;) { 2016446Swnj int ibits = 0, obits = 0; 2029242Ssam 2039242Ssam if (fcc) 2049242Ssam obits |= (1<<p); 2059242Ssam else 2069242Ssam ibits |= (1<<f); 2076446Swnj if (pcc >= 0) 2089242Ssam if (pcc) 2099242Ssam obits |= (1<<f); 2109242Ssam else 2119242Ssam ibits |= (1<<p); 2129242Ssam if (fcc < 0 && pcc < 0) 2139242Ssam break; 2146446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */ 2159208Ssam select(16, &ibits, &obits, 0, 0, 0); 2166446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */ 2176446Swnj if (ibits == 0 && obits == 0) { 2186446Swnj sleep(5); 2196446Swnj continue; 2206446Swnj } 2216446Swnj if (ibits & (1<<f)) { 2226446Swnj fcc = read(f, fibuf, sizeof (fibuf)); 2236446Swnj /* fprintf(console, "%d from f\r\n", fcc); */ 2246446Swnj if (fcc < 0 && errno == EWOULDBLOCK) 2256446Swnj fcc = 0; 2266446Swnj else { 2276446Swnj if (fcc <= 0) 2286446Swnj break; 2296446Swnj fbp = fibuf; 2306446Swnj } 2316446Swnj } 2326446Swnj if (ibits & (1<<p)) { 2336446Swnj pcc = read(p, pibuf, sizeof (pibuf)); 2346446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */ 2356446Swnj pbp = pibuf; 2366446Swnj if (pcc < 0 && errno == EWOULDBLOCK) 2376446Swnj pcc = 0; 2386446Swnj else if (pcc <= 0) 2396446Swnj pcc = -1; 2406446Swnj else if (pibuf[0] == 0) 2416446Swnj pbp++, pcc--; 2426446Swnj else { 2436446Swnj if (pibuf[0]&(TIOCPKT_FLUSHWRITE| 2446446Swnj TIOCPKT_NOSTOP| 2456446Swnj TIOCPKT_DOSTOP)) { 2466446Swnj int nstop = pibuf[0] & 2476446Swnj (TIOCPKT_NOSTOP| 2486446Swnj TIOCPKT_DOSTOP); 2496446Swnj if (nstop) 2506446Swnj stop = nstop; 2516446Swnj pibuf[0] |= nstop; 2529208Ssam send(f,&pibuf[0],1,SOF_OOB); 2536446Swnj } 2546446Swnj pcc = 0; 2556446Swnj } 2566446Swnj } 2576446Swnj if ((obits & (1<<f)) && pcc > 0) { 2586446Swnj cc = write(f, pbp, pcc); 2596446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */ 2606446Swnj if (cc > 0) { 2616446Swnj pcc -= cc; 2626446Swnj pbp += cc; 2636446Swnj } 2646446Swnj } 2656446Swnj if ((obits & (1<<p)) && fcc > 0) { 2666446Swnj cc = write(p, fbp, fcc); 2676446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */ 2686446Swnj if (cc > 0) { 2696446Swnj fcc -= cc; 2706446Swnj fbp += cc; 2716446Swnj } 2726446Swnj } 2736446Swnj } 2746446Swnj cleanup(); 2756446Swnj } 2766446Swnj close(f); 2776446Swnj close(p); 2786446Swnj dup2(t, 0); 2796446Swnj dup2(t, 1); 2806446Swnj dup2(t, 2); 2816446Swnj close(t); 2828380Ssam execl("/bin/login", "login", "-r", hp->h_name, 0); 2839242Ssam fatalperror(2, "/bin/login", errno); 2849242Ssam /*NOTREACHED*/ 2856446Swnj } 2866446Swnj 2876446Swnj cleanup() 2886446Swnj { 2896446Swnj 2906446Swnj rmut(); 29110009Ssam vhangup(); /* XXX */ 29210192Ssam shutdown(netf, 2); 2936446Swnj kill(0, SIGKILL); 2946446Swnj exit(1); 2956446Swnj } 2966446Swnj 2979242Ssam fatal(f, msg) 2989242Ssam int f; 2999242Ssam char *msg; 3009242Ssam { 3019242Ssam char buf[BUFSIZ]; 3029242Ssam 3039242Ssam buf[0] = '\01'; /* error indicator */ 3049242Ssam (void) sprintf(buf + 1, "rlogind: %s.\n", msg); 3059242Ssam (void) write(f, buf, strlen(buf)); 3069242Ssam exit(1); 3079242Ssam } 3089242Ssam 3099242Ssam fatalperror(f, msg, errno) 3109242Ssam int f; 3119242Ssam char *msg; 3129242Ssam int errno; 3139242Ssam { 3149242Ssam char buf[BUFSIZ]; 3159242Ssam extern char *sys_errlist[]; 3169242Ssam 3179242Ssam (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 3189242Ssam fatal(f, buf); 3199242Ssam } 3209242Ssam 3216446Swnj #include <utmp.h> 3226446Swnj 3236446Swnj struct utmp wtmp; 3246446Swnj char wtmpf[] = "/usr/adm/wtmp"; 3256446Swnj char utmp[] = "/etc/utmp"; 3266446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 3276446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 3286446Swnj 3296446Swnj rmut() 3306446Swnj { 3316446Swnj register f; 3326446Swnj int found = 0; 3336446Swnj 3346446Swnj f = open(utmp, 2); 3356446Swnj if (f >= 0) { 3366446Swnj while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) { 3376446Swnj if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 3386446Swnj continue; 3396446Swnj lseek(f, -(long)sizeof(wtmp), 1); 3406446Swnj SCPYN(wtmp.ut_name, ""); 3416446Swnj time(&wtmp.ut_time); 3426446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3436446Swnj found++; 3446446Swnj } 3456446Swnj close(f); 3466446Swnj } 3476446Swnj if (found) { 3486446Swnj f = open(wtmpf, 1); 3496446Swnj if (f >= 0) { 3506446Swnj SCPYN(wtmp.ut_line, line+5); 3516446Swnj SCPYN(wtmp.ut_name, ""); 3526446Swnj time(&wtmp.ut_time); 3536446Swnj lseek(f, (long)0, 2); 3546446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3556446Swnj close(f); 3566446Swnj } 3576446Swnj } 3586446Swnj chmod(line, 0666); 3596446Swnj chown(line, 0, 0); 3606446Swnj line[strlen("/dev/")] = 'p'; 3616446Swnj chmod(line, 0666); 3626446Swnj chown(line, 0, 0); 3636446Swnj } 364