16446Swnj #ifndef lint 2*12216Ssam static char sccsid[] = "@(#)rlogind.c 4.13 83/05/03"; 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(); 2311345Ssam char *crypt(), *rindex(), *index(), *malloc(), *ntoa(); 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 { 36*12216Ssam int f, options = 0; 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 if (setsockopt(f, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) 85*12216Ssam perror("rlogind: setsockopt (SO_KEEPALIVE)"); 869208Ssam if (bind(f, &sin, sizeof (sin), 0) < 0) { 879208Ssam perror("rlogind: bind"); 889208Ssam exit(1); 899208Ssam } 9010585Ssam sigset(SIGCHLD, reapchild); 919208Ssam listen(f, 10); 926446Swnj for (;;) { 939208Ssam int s, len = sizeof (from); 949208Ssam 959208Ssam s = accept(f, &from, &len, 0); 969208Ssam if (s < 0) { 9710417Ssam if (errno == EINTR) 9810417Ssam continue; 999242Ssam perror("rlogind: accept"); 1006446Swnj continue; 1016446Swnj } 10211222Ssam if (fork() == 0) { 10311222Ssam signal(SIGCHLD, SIG_IGN); 1049208Ssam doit(s, &from); 10511222Ssam } 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); 14311345Ssam if (hp == 0) { 14411345Ssam char buf[BUFSIZ], *cp = (char *)&fromp->sin_addr; 14511345Ssam 14611345Ssam fatal(f, sprintf(buf, "Host name for your address (%s) unknown", 14711345Ssam ntoa(fromp->sin_addr))); 14811345Ssam } 1496446Swnj if (fromp->sin_family != AF_INET || 1506446Swnj fromp->sin_port >= IPPORT_RESERVED || 1519242Ssam hp == 0) 1529242Ssam fatal(f, "Permission denied"); 1536446Swnj write(f, "", 1); 1546446Swnj for (c = 'p'; c <= 's'; c++) { 1556446Swnj struct stat stb; 1566446Swnj line = "/dev/ptyXX"; 1576446Swnj line[strlen("/dev/pty")] = c; 1586446Swnj line[strlen("/dev/ptyp")] = '0'; 1596446Swnj if (stat(line, &stb) < 0) 1606446Swnj break; 1616446Swnj for (i = 0; i < 16; i++) { 1626446Swnj line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 1636446Swnj p = open(line, 2); 1646446Swnj if (p > 0) 1656446Swnj goto gotpty; 1666446Swnj } 1676446Swnj } 1689242Ssam fatal(f, "All network ports in use"); 1699242Ssam /*NOTREACHED*/ 1706446Swnj gotpty: 1716446Swnj dup2(f, 0); 1726446Swnj line[strlen("/dev/")] = 't'; 1736446Swnj #ifdef DEBUG 1746446Swnj { int tt = open("/dev/tty", 2); 1756446Swnj if (tt > 0) { 1766446Swnj ioctl(tt, TIOCNOTTY, 0); 1776446Swnj close(tt); 1786446Swnj } 1796446Swnj } 1806446Swnj #endif 1816446Swnj t = open(line, 2); 1829242Ssam if (t < 0) 1839242Ssam fatalperror(f, line, errno); 1846446Swnj { struct sgttyb b; 1856446Swnj gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 1866446Swnj } 1879242Ssam pid = fork(); 1889242Ssam if (pid < 0) 1899242Ssam fatalperror(f, "", errno); 1909242Ssam if (pid) { 1916446Swnj char pibuf[1024], fibuf[1024], *pbp, *fbp; 1926446Swnj int pcc = 0, fcc = 0, on = 1; 1936446Swnj /* FILE *console = fopen("/dev/console", "w"); */ 1946446Swnj /* setbuf(console, 0); */ 1956446Swnj 1966446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */ 1976446Swnj ioctl(f, FIONBIO, &on); 1986446Swnj ioctl(p, FIONBIO, &on); 1996446Swnj ioctl(p, TIOCPKT, &on); 2006446Swnj signal(SIGTSTP, SIG_IGN); 2016446Swnj sigset(SIGCHLD, cleanup); 2026446Swnj for (;;) { 2036446Swnj int ibits = 0, obits = 0; 2049242Ssam 2059242Ssam if (fcc) 2069242Ssam obits |= (1<<p); 2079242Ssam else 2089242Ssam ibits |= (1<<f); 2096446Swnj if (pcc >= 0) 2109242Ssam if (pcc) 2119242Ssam obits |= (1<<f); 2129242Ssam else 2139242Ssam ibits |= (1<<p); 2149242Ssam if (fcc < 0 && pcc < 0) 2159242Ssam break; 2166446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */ 2179208Ssam select(16, &ibits, &obits, 0, 0, 0); 2186446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */ 2196446Swnj if (ibits == 0 && obits == 0) { 2206446Swnj sleep(5); 2216446Swnj continue; 2226446Swnj } 2236446Swnj if (ibits & (1<<f)) { 2246446Swnj fcc = read(f, fibuf, sizeof (fibuf)); 2256446Swnj /* fprintf(console, "%d from f\r\n", fcc); */ 2266446Swnj if (fcc < 0 && errno == EWOULDBLOCK) 2276446Swnj fcc = 0; 2286446Swnj else { 2296446Swnj if (fcc <= 0) 2306446Swnj break; 2316446Swnj fbp = fibuf; 2326446Swnj } 2336446Swnj } 2346446Swnj if (ibits & (1<<p)) { 2356446Swnj pcc = read(p, pibuf, sizeof (pibuf)); 2366446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */ 2376446Swnj pbp = pibuf; 2386446Swnj if (pcc < 0 && errno == EWOULDBLOCK) 2396446Swnj pcc = 0; 2406446Swnj else if (pcc <= 0) 2416446Swnj pcc = -1; 2426446Swnj else if (pibuf[0] == 0) 2436446Swnj pbp++, pcc--; 2446446Swnj else { 2456446Swnj if (pibuf[0]&(TIOCPKT_FLUSHWRITE| 2466446Swnj TIOCPKT_NOSTOP| 2476446Swnj TIOCPKT_DOSTOP)) { 2486446Swnj int nstop = pibuf[0] & 2496446Swnj (TIOCPKT_NOSTOP| 2506446Swnj TIOCPKT_DOSTOP); 2516446Swnj if (nstop) 2526446Swnj stop = nstop; 2536446Swnj pibuf[0] |= nstop; 2549208Ssam send(f,&pibuf[0],1,SOF_OOB); 2556446Swnj } 2566446Swnj pcc = 0; 2576446Swnj } 2586446Swnj } 2596446Swnj if ((obits & (1<<f)) && pcc > 0) { 2606446Swnj cc = write(f, pbp, pcc); 2616446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */ 2626446Swnj if (cc > 0) { 2636446Swnj pcc -= cc; 2646446Swnj pbp += cc; 2656446Swnj } 2666446Swnj } 2676446Swnj if ((obits & (1<<p)) && fcc > 0) { 2686446Swnj cc = write(p, fbp, fcc); 2696446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */ 2706446Swnj if (cc > 0) { 2716446Swnj fcc -= cc; 2726446Swnj fbp += cc; 2736446Swnj } 2746446Swnj } 2756446Swnj } 2766446Swnj cleanup(); 2776446Swnj } 2786446Swnj close(f); 2796446Swnj close(p); 2806446Swnj dup2(t, 0); 2816446Swnj dup2(t, 1); 2826446Swnj dup2(t, 2); 2836446Swnj close(t); 2848380Ssam execl("/bin/login", "login", "-r", hp->h_name, 0); 2859242Ssam fatalperror(2, "/bin/login", errno); 2869242Ssam /*NOTREACHED*/ 2876446Swnj } 2886446Swnj 2896446Swnj cleanup() 2906446Swnj { 2916446Swnj 2926446Swnj rmut(); 29310009Ssam vhangup(); /* XXX */ 29410192Ssam shutdown(netf, 2); 2956446Swnj kill(0, SIGKILL); 2966446Swnj exit(1); 2976446Swnj } 2986446Swnj 2999242Ssam fatal(f, msg) 3009242Ssam int f; 3019242Ssam char *msg; 3029242Ssam { 3039242Ssam char buf[BUFSIZ]; 3049242Ssam 3059242Ssam buf[0] = '\01'; /* error indicator */ 3069242Ssam (void) sprintf(buf + 1, "rlogind: %s.\n", msg); 3079242Ssam (void) write(f, buf, strlen(buf)); 3089242Ssam exit(1); 3099242Ssam } 3109242Ssam 3119242Ssam fatalperror(f, msg, errno) 3129242Ssam int f; 3139242Ssam char *msg; 3149242Ssam int errno; 3159242Ssam { 3169242Ssam char buf[BUFSIZ]; 3179242Ssam extern char *sys_errlist[]; 3189242Ssam 3199242Ssam (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 3209242Ssam fatal(f, buf); 3219242Ssam } 3229242Ssam 3236446Swnj #include <utmp.h> 3246446Swnj 3256446Swnj struct utmp wtmp; 3266446Swnj char wtmpf[] = "/usr/adm/wtmp"; 3276446Swnj char utmp[] = "/etc/utmp"; 3286446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 3296446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 3306446Swnj 3316446Swnj rmut() 3326446Swnj { 3336446Swnj register f; 3346446Swnj int found = 0; 3356446Swnj 3366446Swnj f = open(utmp, 2); 3376446Swnj if (f >= 0) { 3386446Swnj while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) { 3396446Swnj if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 3406446Swnj continue; 3416446Swnj lseek(f, -(long)sizeof(wtmp), 1); 3426446Swnj SCPYN(wtmp.ut_name, ""); 3436446Swnj time(&wtmp.ut_time); 3446446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3456446Swnj found++; 3466446Swnj } 3476446Swnj close(f); 3486446Swnj } 3496446Swnj if (found) { 3506446Swnj f = open(wtmpf, 1); 3516446Swnj if (f >= 0) { 3526446Swnj SCPYN(wtmp.ut_line, line+5); 3536446Swnj SCPYN(wtmp.ut_name, ""); 3546446Swnj time(&wtmp.ut_time); 3556446Swnj lseek(f, (long)0, 2); 3566446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3576446Swnj close(f); 3586446Swnj } 3596446Swnj } 3606446Swnj chmod(line, 0666); 3616446Swnj chown(line, 0, 0); 3626446Swnj line[strlen("/dev/")] = 'p'; 3636446Swnj chmod(line, 0666); 3646446Swnj chown(line, 0, 0); 3656446Swnj } 36611345Ssam 36711345Ssam /* 36811345Ssam * Convert network-format internet address 36911345Ssam * to base 256 d.d.d.d representation. 37011345Ssam */ 37111345Ssam char * 37211345Ssam ntoa(in) 37311345Ssam struct in_addr in; 37411345Ssam { 37511345Ssam static char b[18]; 37611345Ssam register char *p; 37711345Ssam 37811345Ssam p = (char *)∈ 37911345Ssam #define UC(b) (((int)b)&0xff) 38011345Ssam sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 38111345Ssam return (b); 38211345Ssam } 383