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*24889Smckusick static char sccsid[] = "@(#)rlogind.c 5.7 (Berkeley) 09/17/85"; 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 79*24889Smckusick struct winsize win = { 0, 0, 0, 0 }; 8024723Smckusick 81*24889Smckusick 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: 127*24889Smckusick (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); 16318357Ssam cleanup(); 16418357Ssam } 1659242Ssam 16618357Ssam char magic[2] = { 0377, 0377 }; 16718357Ssam 16818357Ssam /* 16918357Ssam * Handle a "control" request (signaled by magic being present) 17018357Ssam * in the data stream. For now, we are only willing to handle 17118357Ssam * window size changes. 17218357Ssam */ 17318357Ssam control(pty, cp, n) 17418357Ssam int pty; 17518357Ssam char *cp; 17618357Ssam int n; 17718357Ssam { 17818357Ssam struct winsize *wp; 17918357Ssam 18018357Ssam if (n < 4+sizeof (*wp) || cp[2] != 's' || cp[3] != 's') 18118357Ssam return (0); 18218357Ssam wp = (struct winsize *)(cp+4); 18318357Ssam wp->ws_row = ntohs(wp->ws_row); 18418357Ssam wp->ws_col = ntohs(wp->ws_col); 18518357Ssam wp->ws_xpixel = ntohs(wp->ws_xpixel); 18618357Ssam wp->ws_ypixel = ntohs(wp->ws_ypixel); 18718357Ssam (void)ioctl(pty, TIOCSWINSZ, wp); 18818357Ssam return (4+sizeof (*wp)); 18918357Ssam } 19018357Ssam 19118357Ssam /* 19218357Ssam * rlogin "protocol" machine. 19318357Ssam */ 19418357Ssam protocol(f, p) 19518357Ssam int f, p; 19618357Ssam { 19718357Ssam char pibuf[1024], fibuf[1024], *pbp, *fbp; 19818357Ssam register pcc = 0, fcc = 0; 19924723Smckusick int cc, stop = TIOCPKT_DOSTOP, wsize; 20024723Smckusick static char oob[] = {TIOCPKT_WINDOW}; 20118357Ssam 20218482Ssam /* 20318484Ssam * Must ignore SIGTTOU, otherwise we'll stop 20418484Ssam * when we try and set slave pty's window shape 20518484Ssam * (our pgrp is that of the master pty). 20618482Ssam */ 20718484Ssam (void) signal(SIGTTOU, SIG_IGN); 20824723Smckusick send(f, oob, 1, MSG_OOB); /* indicate new rlogin */ 20918357Ssam for (;;) { 21018357Ssam int ibits = 0, obits = 0; 21118357Ssam 21218357Ssam if (fcc) 21318357Ssam obits |= (1<<p); 21418357Ssam else 21518357Ssam ibits |= (1<<f); 21618357Ssam if (pcc >= 0) 21718357Ssam if (pcc) 21818357Ssam obits |= (1<<f); 2199242Ssam else 22018357Ssam ibits |= (1<<p); 22118357Ssam if (select(16, &ibits, &obits, 0, 0) < 0) { 22218357Ssam if (errno == EINTR) 2236446Swnj continue; 22418357Ssam fatalperror(f, "select", errno); 22518357Ssam } 22618357Ssam if (ibits == 0 && obits == 0) { 22718357Ssam /* shouldn't happen... */ 22818357Ssam sleep(5); 22918357Ssam continue; 23018357Ssam } 23118357Ssam if (ibits & (1<<f)) { 23218357Ssam fcc = read(f, fibuf, sizeof (fibuf)); 23318357Ssam if (fcc < 0 && errno == EWOULDBLOCK) 23418357Ssam fcc = 0; 23518357Ssam else { 23618357Ssam register char *cp; 23718357Ssam int left, n; 23818357Ssam 23918357Ssam if (fcc <= 0) 24016227Skarels break; 24118357Ssam fbp = fibuf; 24224723Smckusick 24318357Ssam top: 24418357Ssam for (cp = fibuf; cp < fibuf+fcc; cp++) 24518357Ssam if (cp[0] == magic[0] && 24618357Ssam cp[1] == magic[1]) { 24718357Ssam left = fcc - (cp-fibuf); 24818357Ssam n = control(p, cp, left); 24918357Ssam if (n) { 25018357Ssam left -= n; 25118357Ssam if (left > 0) 25218357Ssam bcopy(cp, cp+n, left); 25318357Ssam fcc -= n; 25418357Ssam goto top; /* n^2 */ 25524723Smckusick } /* if (n) */ 25624723Smckusick } /* for (cp = ) */ 25724723Smckusick } /* else */ 25824723Smckusick } /* if (ibits & (1<<f)) */ 25924723Smckusick 26024723Smckusick if ((obits & (1<<p)) && fcc > 0) { 26124723Smckusick wsize = fcc; 26224723Smckusick do { 26324723Smckusick cc = write(p, fbp, wsize); 26424723Smckusick wsize /= 2; 26524723Smckusick } while (cc<0 && errno==EWOULDBLOCK && wsize); 26624723Smckusick if (cc > 0) { 26724723Smckusick fcc -= cc; 26824723Smckusick fbp += cc; 2696446Swnj } 27018357Ssam } 27124723Smckusick 27218357Ssam if (ibits & (1<<p)) { 27318357Ssam pcc = read(p, pibuf, sizeof (pibuf)); 27418357Ssam pbp = pibuf; 27518357Ssam if (pcc < 0 && errno == EWOULDBLOCK) 27618357Ssam pcc = 0; 27718357Ssam else if (pcc <= 0) 27818357Ssam break; 27918357Ssam else if (pibuf[0] == 0) 28018357Ssam pbp++, pcc--; 28118357Ssam else { 28218357Ssam #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 28324723Smckusick int out = FREAD; 28424723Smckusick 28518357Ssam if (pkcontrol(pibuf[0])) { 28618357Ssam /* The following 3 lines do nothing. */ 28718357Ssam int nstop = pibuf[0] & 28818357Ssam (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP); 28918357Ssam 29018357Ssam if (nstop) 29118357Ssam stop = nstop; 292*24889Smckusick pibuf[0] |= nstop | oob[0]; 29318357Ssam send(f, &pibuf[0], 1, MSG_OOB); 29424723Smckusick if (pibuf[0] & TIOCPKT_FLUSHWRITE) 29524723Smckusick ioctl(p, TIOCFLUSH, (char *)&out); 29624723Smckusick 29716227Skarels } 29818357Ssam pcc = 0; 2996446Swnj } 30018357Ssam } 30118357Ssam if ((obits & (1<<f)) && pcc > 0) { 30224723Smckusick wsize = pcc; 30324723Smckusick do { 30424723Smckusick cc = write(f, pbp, wsize); 30524723Smckusick wsize /= 2; 30624723Smckusick } while (cc<0 && errno==EWOULDBLOCK && wsize); 30718357Ssam if (cc > 0) { 30818357Ssam pcc -= cc; 30918357Ssam pbp += cc; 31018357Ssam } 3116446Swnj } 3126446Swnj } 3136446Swnj } 3146446Swnj 3156446Swnj cleanup() 3166446Swnj { 3176446Swnj 3186446Swnj rmut(); 31910009Ssam vhangup(); /* XXX */ 32010192Ssam shutdown(netf, 2); 3216446Swnj kill(0, SIGKILL); 3226446Swnj exit(1); 3236446Swnj } 3246446Swnj 3259242Ssam fatal(f, msg) 3269242Ssam int f; 3279242Ssam char *msg; 3289242Ssam { 3299242Ssam char buf[BUFSIZ]; 3309242Ssam 3319242Ssam buf[0] = '\01'; /* error indicator */ 33213554Ssam (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 3339242Ssam (void) write(f, buf, strlen(buf)); 3349242Ssam exit(1); 3359242Ssam } 3369242Ssam 3379242Ssam fatalperror(f, msg, errno) 3389242Ssam int f; 3399242Ssam char *msg; 3409242Ssam int errno; 3419242Ssam { 3429242Ssam char buf[BUFSIZ]; 34316227Skarels extern int sys_nerr; 3449242Ssam extern char *sys_errlist[]; 3459242Ssam 34618357Ssam if ((unsigned)errno < sys_nerr) 34716227Skarels (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 34816227Skarels else 34916227Skarels (void) sprintf(buf, "%s: Error %d", msg, errno); 3509242Ssam fatal(f, buf); 3519242Ssam } 3529242Ssam 3536446Swnj #include <utmp.h> 3546446Swnj 3556446Swnj struct utmp wtmp; 3566446Swnj char wtmpf[] = "/usr/adm/wtmp"; 35723566Sbloom char utmpf[] = "/etc/utmp"; 3586446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 3596446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 3606446Swnj 3616446Swnj rmut() 3626446Swnj { 3636446Swnj register f; 3646446Swnj int found = 0; 36523566Sbloom struct utmp *u, *utmp; 36623566Sbloom int nutmp; 36723566Sbloom struct stat statbf; 3686446Swnj 36923566Sbloom f = open(utmpf, O_RDWR); 3706446Swnj if (f >= 0) { 37123566Sbloom fstat(f, &statbf); 37223566Sbloom utmp = (struct utmp *)malloc(statbf.st_size); 37323566Sbloom if (!utmp) 37423566Sbloom syslog(LOG_ERR, "utmp malloc failed"); 37523566Sbloom if (statbf.st_size && utmp) { 37623566Sbloom nutmp = read(f, utmp, statbf.st_size); 37723566Sbloom nutmp /= sizeof(struct utmp); 37823566Sbloom 37923566Sbloom for (u = utmp ; u < &utmp[nutmp] ; u++) { 38023566Sbloom if (SCMPN(u->ut_line, line+5) || 38123566Sbloom u->ut_name[0]==0) 38223566Sbloom continue; 38323566Sbloom lseek(f, ((long)u)-((long)utmp), L_SET); 38423566Sbloom SCPYN(u->ut_name, ""); 38523566Sbloom SCPYN(u->ut_host, ""); 38623566Sbloom time(&u->ut_time); 38723566Sbloom write(f, (char *)u, sizeof(wtmp)); 38823566Sbloom found++; 38923566Sbloom } 3906446Swnj } 3916446Swnj close(f); 3926446Swnj } 3936446Swnj if (found) { 39423566Sbloom f = open(wtmpf, O_WRONLY|O_APPEND); 3956446Swnj if (f >= 0) { 3966446Swnj SCPYN(wtmp.ut_line, line+5); 3976446Swnj SCPYN(wtmp.ut_name, ""); 39812681Ssam SCPYN(wtmp.ut_host, ""); 3996446Swnj time(&wtmp.ut_time); 4006446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 4016446Swnj close(f); 4026446Swnj } 4036446Swnj } 4046446Swnj chmod(line, 0666); 4056446Swnj chown(line, 0, 0); 4066446Swnj line[strlen("/dev/")] = 'p'; 4076446Swnj chmod(line, 0666); 4086446Swnj chown(line, 0, 0); 4096446Swnj } 410