121174Sdist /* 221174Sdist * Copyright (c) 1983 Regents of the University of California. 321174Sdist * All rights reserved. The Berkeley software License Agreement 421174Sdist * specifies the terms and conditions for redistribution. 521174Sdist */ 621174Sdist 76446Swnj #ifndef lint 821174Sdist char copyright[] = 921174Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 1021174Sdist All rights reserved.\n"; 1121174Sdist #endif not lint 126446Swnj 1321174Sdist #ifndef lint 14*30600Smckusick static char sccsid[] = "@(#)rlogind.c 5.12 (Berkeley) 03/10/87"; 1521174Sdist #endif not lint 1621174Sdist 1716369Skarels /* 1816369Skarels * remote login server: 1916369Skarels * remuser\0 2016369Skarels * locuser\0 2118357Ssam * terminal info\0 2216369Skarels * data 2316369Skarels */ 2416369Skarels 256446Swnj #include <stdio.h> 266446Swnj #include <sys/types.h> 276446Swnj #include <sys/stat.h> 286446Swnj #include <sys/socket.h> 2913554Ssam #include <sys/wait.h> 3018357Ssam #include <sys/file.h> 319208Ssam 329208Ssam #include <netinet/in.h> 339208Ssam 346446Swnj #include <errno.h> 356446Swnj #include <pwd.h> 366446Swnj #include <signal.h> 376446Swnj #include <sgtty.h> 386446Swnj #include <stdio.h> 398380Ssam #include <netdb.h> 4017187Sralph #include <syslog.h> 4118357Ssam #include <strings.h> 426446Swnj 4324723Smckusick # ifndef TIOCPKT_WINDOW 4424723Smckusick # define TIOCPKT_WINDOW 0x80 4524723Smckusick # endif TIOCPKT_WINDOW 4624723Smckusick 476446Swnj extern errno; 4810417Ssam int reapchild(); 496446Swnj struct passwd *getpwnam(); 5024723Smckusick char *malloc(); 5116369Skarels 526446Swnj main(argc, argv) 536446Swnj int argc; 546446Swnj char **argv; 556446Swnj { 5617156Ssam int on = 1, options = 0, fromlen; 576446Swnj struct sockaddr_in from; 586446Swnj 5924855Seric openlog("rlogind", LOG_PID | LOG_AUTH, LOG_AUTH); 6016369Skarels fromlen = sizeof (from); 6116369Skarels if (getpeername(0, &from, &fromlen) < 0) { 6216369Skarels fprintf(stderr, "%s: ", argv[0]); 6316369Skarels perror("getpeername"); 6416369Skarels _exit(1); 658380Ssam } 6617156Ssam if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) { 6717187Sralph syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 686446Swnj } 6916369Skarels doit(0, &from); 706446Swnj } 716446Swnj 726446Swnj int child; 736446Swnj int cleanup(); 746446Swnj int netf; 756446Swnj extern errno; 766446Swnj char *line; 7724724Smckusick extern char *inet_ntoa(); 786446Swnj 7924889Smckusick struct winsize win = { 0, 0, 0, 0 }; 8024723Smckusick 8124889Smckusick 826446Swnj doit(f, fromp) 836446Swnj int f; 846446Swnj struct sockaddr_in *fromp; 856446Swnj { 8618357Ssam int i, p, t, pid, on = 1; 8718357Ssam register struct hostent *hp; 8824724Smckusick struct hostent hostent; 898380Ssam char c; 906446Swnj 916446Swnj alarm(60); 926446Swnj read(f, &c, 1); 936446Swnj if (c != 0) 946446Swnj exit(1); 956446Swnj alarm(0); 9616227Skarels fromp->sin_port = ntohs((u_short)fromp->sin_port); 978380Ssam hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 988380Ssam fromp->sin_family); 9911345Ssam if (hp == 0) { 10024724Smckusick /* 10124724Smckusick * Only the name is used below. 10224724Smckusick */ 10324724Smckusick hp = &hostent; 10424724Smckusick hp->h_name = inet_ntoa(fromp->sin_addr); 10511345Ssam } 1066446Swnj if (fromp->sin_family != AF_INET || 10716227Skarels fromp->sin_port >= IPPORT_RESERVED) 1089242Ssam fatal(f, "Permission denied"); 1096446Swnj write(f, "", 1); 1106446Swnj for (c = 'p'; c <= 's'; c++) { 1116446Swnj struct stat stb; 1126446Swnj line = "/dev/ptyXX"; 1136446Swnj line[strlen("/dev/pty")] = c; 1146446Swnj line[strlen("/dev/ptyp")] = '0'; 1156446Swnj if (stat(line, &stb) < 0) 1166446Swnj break; 1176446Swnj for (i = 0; i < 16; i++) { 1186446Swnj line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 1196446Swnj p = open(line, 2); 1206446Swnj if (p > 0) 1216446Swnj goto gotpty; 1226446Swnj } 1236446Swnj } 12424723Smckusick fatal(f, "Out of ptys"); 1259242Ssam /*NOTREACHED*/ 1266446Swnj gotpty: 12724889Smckusick (void) ioctl(p, TIOCSWINSZ, &win); 12816227Skarels netf = f; 1296446Swnj line[strlen("/dev/")] = 't'; 1306446Swnj #ifdef DEBUG 1316446Swnj { int tt = open("/dev/tty", 2); 1326446Swnj if (tt > 0) { 1336446Swnj ioctl(tt, TIOCNOTTY, 0); 1346446Swnj close(tt); 1356446Swnj } 1366446Swnj } 1376446Swnj #endif 1386446Swnj t = open(line, 2); 1399242Ssam if (t < 0) 1409242Ssam fatalperror(f, line, errno); 1416446Swnj { struct sgttyb b; 1426446Swnj gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 1436446Swnj } 1449242Ssam pid = fork(); 1459242Ssam if (pid < 0) 1469242Ssam fatalperror(f, "", errno); 14718357Ssam if (pid == 0) { 14818357Ssam close(f), close(p); 14918357Ssam dup2(t, 0), dup2(t, 1), dup2(t, 2); 15016227Skarels close(t); 15118357Ssam execl("/bin/login", "login", "-r", hp->h_name, 0); 15218357Ssam fatalperror(2, "/bin/login", errno); 15318357Ssam /*NOTREACHED*/ 15418357Ssam } 15518357Ssam close(t); 15618357Ssam ioctl(f, FIONBIO, &on); 15718357Ssam ioctl(p, FIONBIO, &on); 15818357Ssam ioctl(p, TIOCPKT, &on); 15918357Ssam signal(SIGTSTP, SIG_IGN); 16018357Ssam signal(SIGCHLD, cleanup); 16124724Smckusick setpgrp(0, 0); 16218357Ssam protocol(f, p); 163*30600Smckusick signal(SIGCHLD, SIG_IGN); 16418357Ssam cleanup(); 16518357Ssam } 1669242Ssam 16718357Ssam char magic[2] = { 0377, 0377 }; 16825423Skarels char oobdata[] = {TIOCPKT_WINDOW}; 16918357Ssam 17018357Ssam /* 17118357Ssam * Handle a "control" request (signaled by magic being present) 17218357Ssam * in the data stream. For now, we are only willing to handle 17318357Ssam * window size changes. 17418357Ssam */ 17518357Ssam control(pty, cp, n) 17618357Ssam int pty; 17718357Ssam char *cp; 17818357Ssam int n; 17918357Ssam { 18028705Smckusick struct winsize w; 18118357Ssam 18228705Smckusick if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 18318357Ssam return (0); 18425423Skarels oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 18528705Smckusick bcopy(cp+4, (char *)&w, sizeof(w)); 18628705Smckusick w.ws_row = ntohs(w.ws_row); 18728705Smckusick w.ws_col = ntohs(w.ws_col); 18828705Smckusick w.ws_xpixel = ntohs(w.ws_xpixel); 18928705Smckusick w.ws_ypixel = ntohs(w.ws_ypixel); 19028705Smckusick (void)ioctl(pty, TIOCSWINSZ, &w); 19128705Smckusick return (4+sizeof (w)); 19218357Ssam } 19318357Ssam 19418357Ssam /* 19518357Ssam * rlogin "protocol" machine. 19618357Ssam */ 19718357Ssam protocol(f, p) 19818357Ssam int f, p; 19918357Ssam { 20018357Ssam char pibuf[1024], fibuf[1024], *pbp, *fbp; 20118357Ssam register pcc = 0, fcc = 0; 20225423Skarels int cc; 20325740Skarels char cntl; 20418357Ssam 20518482Ssam /* 20618484Ssam * Must ignore SIGTTOU, otherwise we'll stop 20718484Ssam * when we try and set slave pty's window shape 20825423Skarels * (our controlling tty is the master pty). 20918482Ssam */ 21018484Ssam (void) signal(SIGTTOU, SIG_IGN); 21125423Skarels send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 21218357Ssam for (;;) { 21325740Skarels int ibits, obits, ebits; 21418357Ssam 21525740Skarels ibits = 0; 21625740Skarels obits = 0; 21718357Ssam if (fcc) 21818357Ssam obits |= (1<<p); 21918357Ssam else 22018357Ssam ibits |= (1<<f); 22118357Ssam if (pcc >= 0) 22218357Ssam if (pcc) 22318357Ssam obits |= (1<<f); 2249242Ssam else 22518357Ssam ibits |= (1<<p); 22625740Skarels ebits = (1<<p); 22725740Skarels if (select(16, &ibits, &obits, &ebits, 0) < 0) { 22818357Ssam if (errno == EINTR) 2296446Swnj continue; 23018357Ssam fatalperror(f, "select", errno); 23118357Ssam } 23225740Skarels if (ibits == 0 && obits == 0 && ebits == 0) { 23318357Ssam /* shouldn't happen... */ 23418357Ssam sleep(5); 23518357Ssam continue; 23618357Ssam } 23725740Skarels #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 23825740Skarels if (ebits & (1<<p)) { 23925740Skarels cc = read(p, &cntl, 1); 24025740Skarels if (cc == 1 && pkcontrol(cntl)) { 24125740Skarels cntl |= oobdata[0]; 24225740Skarels send(f, &cntl, 1, MSG_OOB); 24325740Skarels if (cntl & TIOCPKT_FLUSHWRITE) { 24425740Skarels pcc = 0; 24525740Skarels ibits &= ~(1<<p); 24625740Skarels } 24725740Skarels } 24825740Skarels } 24918357Ssam if (ibits & (1<<f)) { 25018357Ssam fcc = read(f, fibuf, sizeof (fibuf)); 25118357Ssam if (fcc < 0 && errno == EWOULDBLOCK) 25218357Ssam fcc = 0; 25318357Ssam else { 25418357Ssam register char *cp; 25518357Ssam int left, n; 25618357Ssam 25718357Ssam if (fcc <= 0) 25816227Skarels break; 25918357Ssam fbp = fibuf; 26024723Smckusick 26118357Ssam top: 26225423Skarels for (cp = fibuf; cp < fibuf+fcc-1; cp++) 26318357Ssam if (cp[0] == magic[0] && 26418357Ssam cp[1] == magic[1]) { 26518357Ssam left = fcc - (cp-fibuf); 26618357Ssam n = control(p, cp, left); 26718357Ssam if (n) { 26818357Ssam left -= n; 26918357Ssam if (left > 0) 27025423Skarels bcopy(cp+n, cp, left); 27118357Ssam fcc -= n; 27218357Ssam goto top; /* n^2 */ 27325423Skarels } 27425423Skarels } 27525423Skarels } 27625423Skarels } 27724723Smckusick 27824723Smckusick if ((obits & (1<<p)) && fcc > 0) { 27925423Skarels cc = write(p, fbp, fcc); 28024723Smckusick if (cc > 0) { 28124723Smckusick fcc -= cc; 28224723Smckusick fbp += cc; 2836446Swnj } 28418357Ssam } 28524723Smckusick 28618357Ssam if (ibits & (1<<p)) { 28718357Ssam pcc = read(p, pibuf, sizeof (pibuf)); 28818357Ssam pbp = pibuf; 28918357Ssam if (pcc < 0 && errno == EWOULDBLOCK) 29018357Ssam pcc = 0; 29118357Ssam else if (pcc <= 0) 29218357Ssam break; 29318357Ssam else if (pibuf[0] == 0) 29418357Ssam pbp++, pcc--; 29518357Ssam else { 29618357Ssam if (pkcontrol(pibuf[0])) { 29725423Skarels pibuf[0] |= oobdata[0]; 29818357Ssam send(f, &pibuf[0], 1, MSG_OOB); 29916227Skarels } 30018357Ssam pcc = 0; 3016446Swnj } 30218357Ssam } 30318357Ssam if ((obits & (1<<f)) && pcc > 0) { 30425423Skarels cc = write(f, pbp, pcc); 30525423Skarels if (cc < 0 && errno == EWOULDBLOCK) { 30625423Skarels /* also shouldn't happen */ 30725423Skarels sleep(5); 30825423Skarels continue; 30925423Skarels } 31018357Ssam if (cc > 0) { 31118357Ssam pcc -= cc; 31218357Ssam pbp += cc; 31318357Ssam } 3146446Swnj } 3156446Swnj } 3166446Swnj } 3176446Swnj 3186446Swnj cleanup() 3196446Swnj { 3206446Swnj 3216446Swnj rmut(); 32210009Ssam vhangup(); /* XXX */ 32310192Ssam shutdown(netf, 2); 3246446Swnj exit(1); 3256446Swnj } 3266446Swnj 3279242Ssam fatal(f, msg) 3289242Ssam int f; 3299242Ssam char *msg; 3309242Ssam { 3319242Ssam char buf[BUFSIZ]; 3329242Ssam 3339242Ssam buf[0] = '\01'; /* error indicator */ 33413554Ssam (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 3359242Ssam (void) write(f, buf, strlen(buf)); 3369242Ssam exit(1); 3379242Ssam } 3389242Ssam 3399242Ssam fatalperror(f, msg, errno) 3409242Ssam int f; 3419242Ssam char *msg; 3429242Ssam int errno; 3439242Ssam { 3449242Ssam char buf[BUFSIZ]; 34516227Skarels extern int sys_nerr; 3469242Ssam extern char *sys_errlist[]; 3479242Ssam 34818357Ssam if ((unsigned)errno < sys_nerr) 34916227Skarels (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 35016227Skarels else 35116227Skarels (void) sprintf(buf, "%s: Error %d", msg, errno); 3529242Ssam fatal(f, buf); 3539242Ssam } 3549242Ssam 3556446Swnj #include <utmp.h> 3566446Swnj 3576446Swnj struct utmp wtmp; 3586446Swnj char wtmpf[] = "/usr/adm/wtmp"; 35923566Sbloom char utmpf[] = "/etc/utmp"; 3606446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 3616446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 3626446Swnj 3636446Swnj rmut() 3646446Swnj { 3656446Swnj register f; 3666446Swnj int found = 0; 36723566Sbloom struct utmp *u, *utmp; 36823566Sbloom int nutmp; 36923566Sbloom struct stat statbf; 3706446Swnj 37123566Sbloom f = open(utmpf, O_RDWR); 3726446Swnj if (f >= 0) { 37323566Sbloom fstat(f, &statbf); 37423566Sbloom utmp = (struct utmp *)malloc(statbf.st_size); 37523566Sbloom if (!utmp) 37623566Sbloom syslog(LOG_ERR, "utmp malloc failed"); 37723566Sbloom if (statbf.st_size && utmp) { 37823566Sbloom nutmp = read(f, utmp, statbf.st_size); 37923566Sbloom nutmp /= sizeof(struct utmp); 38023566Sbloom 38123566Sbloom for (u = utmp ; u < &utmp[nutmp] ; u++) { 38223566Sbloom if (SCMPN(u->ut_line, line+5) || 38323566Sbloom u->ut_name[0]==0) 38423566Sbloom continue; 38523566Sbloom lseek(f, ((long)u)-((long)utmp), L_SET); 38623566Sbloom SCPYN(u->ut_name, ""); 38723566Sbloom SCPYN(u->ut_host, ""); 38823566Sbloom time(&u->ut_time); 38923566Sbloom write(f, (char *)u, sizeof(wtmp)); 39023566Sbloom found++; 39123566Sbloom } 3926446Swnj } 3936446Swnj close(f); 3946446Swnj } 3956446Swnj if (found) { 39623566Sbloom f = open(wtmpf, O_WRONLY|O_APPEND); 3976446Swnj if (f >= 0) { 3986446Swnj SCPYN(wtmp.ut_line, line+5); 3996446Swnj SCPYN(wtmp.ut_name, ""); 40012681Ssam SCPYN(wtmp.ut_host, ""); 4016446Swnj time(&wtmp.ut_time); 4026446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 4036446Swnj close(f); 4046446Swnj } 4056446Swnj } 4066446Swnj chmod(line, 0666); 4076446Swnj chown(line, 0, 0); 4086446Swnj line[strlen("/dev/")] = 'p'; 4096446Swnj chmod(line, 0666); 4106446Swnj chown(line, 0, 0); 4116446Swnj } 412