16446Swnj #ifndef lint 2*16369Skarels static char sccsid[] = "@(#)rlogind.c 4.20 (Berkeley) 04/11/84"; 36446Swnj #endif 46446Swnj 5*16369Skarels /* 6*16369Skarels * remote login server: 7*16369Skarels * remuser\0 8*16369Skarels * locuser\0 9*16369Skarels * terminal type\0 10*16369Skarels * data 11*16369Skarels */ 12*16369Skarels 136446Swnj #include <stdio.h> 146446Swnj #include <sys/types.h> 156446Swnj #include <sys/stat.h> 166446Swnj #include <sys/socket.h> 1713554Ssam #include <sys/wait.h> 189208Ssam 199208Ssam #include <netinet/in.h> 209208Ssam 216446Swnj #include <errno.h> 226446Swnj #include <pwd.h> 236446Swnj #include <signal.h> 246446Swnj #include <sgtty.h> 256446Swnj #include <stdio.h> 268380Ssam #include <netdb.h> 276446Swnj 286446Swnj extern errno; 2910417Ssam int reapchild(); 306446Swnj struct passwd *getpwnam(); 3111345Ssam char *crypt(), *rindex(), *index(), *malloc(), *ntoa(); 32*16369Skarels 336446Swnj main(argc, argv) 346446Swnj int argc; 356446Swnj char **argv; 366446Swnj { 37*16369Skarels int options = 0, fromlen; 386446Swnj struct sockaddr_in from; 396446Swnj 40*16369Skarels fromlen = sizeof (from); 41*16369Skarels if (getpeername(0, &from, &fromlen) < 0) { 42*16369Skarels fprintf(stderr, "%s: ", argv[0]); 43*16369Skarels perror("getpeername"); 44*16369Skarels _exit(1); 458380Ssam } 46*16369Skarels if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) { 47*16369Skarels fprintf(stderr, "%s: ", argv[0]); 48*16369Skarels perror("setsockopt (SO_KEEPALIVE)"); 496446Swnj } 50*16369Skarels doit(0, &from); 516446Swnj } 526446Swnj 536446Swnj char locuser[32], remuser[32]; 546446Swnj char buf[BUFSIZ]; 556446Swnj int child; 566446Swnj int cleanup(); 576446Swnj int netf; 586446Swnj extern errno; 596446Swnj char *line; 606446Swnj 616446Swnj doit(f, fromp) 626446Swnj int f; 636446Swnj struct sockaddr_in *fromp; 646446Swnj { 658380Ssam char c; 669242Ssam int i, p, cc, t, pid; 676446Swnj int stop = TIOCPKT_DOSTOP; 688380Ssam register struct hostent *hp; 696446Swnj 706446Swnj alarm(60); 716446Swnj read(f, &c, 1); 726446Swnj if (c != 0) 736446Swnj exit(1); 746446Swnj alarm(0); 7516227Skarels fromp->sin_port = ntohs((u_short)fromp->sin_port); 768380Ssam hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 778380Ssam fromp->sin_family); 7811345Ssam if (hp == 0) { 7916227Skarels char buf[BUFSIZ]; 8011345Ssam 8111345Ssam fatal(f, sprintf(buf, "Host name for your address (%s) unknown", 8211345Ssam ntoa(fromp->sin_addr))); 8311345Ssam } 846446Swnj if (fromp->sin_family != AF_INET || 8516227Skarels fromp->sin_port >= IPPORT_RESERVED) 869242Ssam fatal(f, "Permission denied"); 876446Swnj write(f, "", 1); 886446Swnj for (c = 'p'; c <= 's'; c++) { 896446Swnj struct stat stb; 906446Swnj line = "/dev/ptyXX"; 916446Swnj line[strlen("/dev/pty")] = c; 926446Swnj line[strlen("/dev/ptyp")] = '0'; 936446Swnj if (stat(line, &stb) < 0) 946446Swnj break; 956446Swnj for (i = 0; i < 16; i++) { 966446Swnj line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 976446Swnj p = open(line, 2); 986446Swnj if (p > 0) 996446Swnj goto gotpty; 1006446Swnj } 1016446Swnj } 1029242Ssam fatal(f, "All network ports in use"); 1039242Ssam /*NOTREACHED*/ 1046446Swnj gotpty: 10516227Skarels netf = f; 1066446Swnj line[strlen("/dev/")] = 't'; 1076446Swnj #ifdef DEBUG 1086446Swnj { int tt = open("/dev/tty", 2); 1096446Swnj if (tt > 0) { 1106446Swnj ioctl(tt, TIOCNOTTY, 0); 1116446Swnj close(tt); 1126446Swnj } 1136446Swnj } 1146446Swnj #endif 1156446Swnj t = open(line, 2); 1169242Ssam if (t < 0) 1179242Ssam fatalperror(f, line, errno); 1186446Swnj { struct sgttyb b; 1196446Swnj gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 1206446Swnj } 1219242Ssam pid = fork(); 1229242Ssam if (pid < 0) 1239242Ssam fatalperror(f, "", errno); 1249242Ssam if (pid) { 1256446Swnj char pibuf[1024], fibuf[1024], *pbp, *fbp; 12616227Skarels register pcc = 0, fcc = 0; 12716227Skarels int on = 1; 1286446Swnj /* FILE *console = fopen("/dev/console", "w"); */ 1296446Swnj /* setbuf(console, 0); */ 1306446Swnj 1316446Swnj /* fprintf(console, "f %d p %d\r\n", f, p); */ 13216227Skarels close(t); 1336446Swnj ioctl(f, FIONBIO, &on); 1346446Swnj ioctl(p, FIONBIO, &on); 1356446Swnj ioctl(p, TIOCPKT, &on); 1366446Swnj signal(SIGTSTP, SIG_IGN); 13713024Ssam signal(SIGCHLD, cleanup); 1386446Swnj for (;;) { 1396446Swnj int ibits = 0, obits = 0; 1409242Ssam 1419242Ssam if (fcc) 1429242Ssam obits |= (1<<p); 1439242Ssam else 1449242Ssam ibits |= (1<<f); 1456446Swnj if (pcc >= 0) 1469242Ssam if (pcc) 1479242Ssam obits |= (1<<f); 1489242Ssam else 1499242Ssam ibits |= (1<<p); 1506446Swnj /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */ 15116227Skarels if (select(16, &ibits, &obits, 0, 0) < 0) { 15216227Skarels if (errno == EINTR) 15316227Skarels continue; 15416227Skarels fatalperror(f, "select", errno); 15516227Skarels } 1566446Swnj /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */ 1576446Swnj if (ibits == 0 && obits == 0) { 15816227Skarels /* shouldn't happen... */ 1596446Swnj sleep(5); 1606446Swnj continue; 1616446Swnj } 1626446Swnj if (ibits & (1<<f)) { 1636446Swnj fcc = read(f, fibuf, sizeof (fibuf)); 1646446Swnj /* fprintf(console, "%d from f\r\n", fcc); */ 1656446Swnj if (fcc < 0 && errno == EWOULDBLOCK) 1666446Swnj fcc = 0; 1676446Swnj else { 1686446Swnj if (fcc <= 0) 1696446Swnj break; 1706446Swnj fbp = fibuf; 1716446Swnj } 1726446Swnj } 1736446Swnj if (ibits & (1<<p)) { 1746446Swnj pcc = read(p, pibuf, sizeof (pibuf)); 1756446Swnj /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */ 1766446Swnj pbp = pibuf; 1776446Swnj if (pcc < 0 && errno == EWOULDBLOCK) 1786446Swnj pcc = 0; 1796446Swnj else if (pcc <= 0) 18016227Skarels break; 1816446Swnj else if (pibuf[0] == 0) 1826446Swnj pbp++, pcc--; 1836446Swnj else { 1846446Swnj if (pibuf[0]&(TIOCPKT_FLUSHWRITE| 1856446Swnj TIOCPKT_NOSTOP| 1866446Swnj TIOCPKT_DOSTOP)) { 18716227Skarels /* The following 3 lines do nothing. */ 1886446Swnj int nstop = pibuf[0] & 1896446Swnj (TIOCPKT_NOSTOP| 1906446Swnj TIOCPKT_DOSTOP); 1916446Swnj if (nstop) 1926446Swnj stop = nstop; 1936446Swnj pibuf[0] |= nstop; 19412898Ssam send(f,&pibuf[0],1,MSG_OOB); 1956446Swnj } 1966446Swnj pcc = 0; 1976446Swnj } 1986446Swnj } 1996446Swnj if ((obits & (1<<f)) && pcc > 0) { 2006446Swnj cc = write(f, pbp, pcc); 20116227Skarels if (cc < 0 && errno == EWOULDBLOCK) { 20216227Skarels /* also shouldn't happen */ 20316227Skarels sleep(5); 20416227Skarels continue; 20516227Skarels } 2066446Swnj /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */ 2076446Swnj if (cc > 0) { 2086446Swnj pcc -= cc; 2096446Swnj pbp += cc; 2106446Swnj } 2116446Swnj } 2126446Swnj if ((obits & (1<<p)) && fcc > 0) { 2136446Swnj cc = write(p, fbp, fcc); 2146446Swnj /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */ 2156446Swnj if (cc > 0) { 2166446Swnj fcc -= cc; 2176446Swnj fbp += cc; 2186446Swnj } 2196446Swnj } 2206446Swnj } 2216446Swnj cleanup(); 2226446Swnj } 2236446Swnj close(f); 2246446Swnj close(p); 2256446Swnj dup2(t, 0); 2266446Swnj dup2(t, 1); 2276446Swnj dup2(t, 2); 2286446Swnj close(t); 2298380Ssam execl("/bin/login", "login", "-r", hp->h_name, 0); 2309242Ssam fatalperror(2, "/bin/login", errno); 2319242Ssam /*NOTREACHED*/ 2326446Swnj } 2336446Swnj 2346446Swnj cleanup() 2356446Swnj { 2366446Swnj 2376446Swnj rmut(); 23810009Ssam vhangup(); /* XXX */ 23910192Ssam shutdown(netf, 2); 2406446Swnj kill(0, SIGKILL); 2416446Swnj exit(1); 2426446Swnj } 2436446Swnj 2449242Ssam fatal(f, msg) 2459242Ssam int f; 2469242Ssam char *msg; 2479242Ssam { 2489242Ssam char buf[BUFSIZ]; 2499242Ssam 2509242Ssam buf[0] = '\01'; /* error indicator */ 25113554Ssam (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 2529242Ssam (void) write(f, buf, strlen(buf)); 2539242Ssam exit(1); 2549242Ssam } 2559242Ssam 2569242Ssam fatalperror(f, msg, errno) 2579242Ssam int f; 2589242Ssam char *msg; 2599242Ssam int errno; 2609242Ssam { 2619242Ssam char buf[BUFSIZ]; 26216227Skarels extern int sys_nerr; 2639242Ssam extern char *sys_errlist[]; 2649242Ssam 26516227Skarels if ((unsigned) errno < sys_nerr) 26616227Skarels (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 26716227Skarels else 26816227Skarels (void) sprintf(buf, "%s: Error %d", msg, errno); 2699242Ssam fatal(f, buf); 2709242Ssam } 2719242Ssam 2726446Swnj #include <utmp.h> 2736446Swnj 2746446Swnj struct utmp wtmp; 2756446Swnj char wtmpf[] = "/usr/adm/wtmp"; 2766446Swnj char utmp[] = "/etc/utmp"; 2776446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 2786446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 2796446Swnj 2806446Swnj rmut() 2816446Swnj { 2826446Swnj register f; 2836446Swnj int found = 0; 2846446Swnj 2856446Swnj f = open(utmp, 2); 2866446Swnj if (f >= 0) { 2876446Swnj while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) { 2886446Swnj if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 2896446Swnj continue; 2906446Swnj lseek(f, -(long)sizeof(wtmp), 1); 2916446Swnj SCPYN(wtmp.ut_name, ""); 29212681Ssam SCPYN(wtmp.ut_host, ""); 2936446Swnj time(&wtmp.ut_time); 2946446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 2956446Swnj found++; 2966446Swnj } 2976446Swnj close(f); 2986446Swnj } 2996446Swnj if (found) { 3006446Swnj f = open(wtmpf, 1); 3016446Swnj if (f >= 0) { 3026446Swnj SCPYN(wtmp.ut_line, line+5); 3036446Swnj SCPYN(wtmp.ut_name, ""); 30412681Ssam SCPYN(wtmp.ut_host, ""); 3056446Swnj time(&wtmp.ut_time); 3066446Swnj lseek(f, (long)0, 2); 3076446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3086446Swnj close(f); 3096446Swnj } 3106446Swnj } 3116446Swnj chmod(line, 0666); 3126446Swnj chown(line, 0, 0); 3136446Swnj line[strlen("/dev/")] = 'p'; 3146446Swnj chmod(line, 0666); 3156446Swnj chown(line, 0, 0); 3166446Swnj } 31711345Ssam 31811345Ssam /* 31911345Ssam * Convert network-format internet address 32011345Ssam * to base 256 d.d.d.d representation. 32111345Ssam */ 32211345Ssam char * 32311345Ssam ntoa(in) 32411345Ssam struct in_addr in; 32511345Ssam { 32611345Ssam static char b[18]; 32711345Ssam register char *p; 32811345Ssam 32911345Ssam p = (char *)∈ 33011345Ssam #define UC(b) (((int)b)&0xff) 33111345Ssam sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 33211345Ssam return (b); 33311345Ssam } 334