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*25423Skarels static char sccsid[] = "@(#)rlogind.c 5.9 (Berkeley) 11/08/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 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); 16318357Ssam cleanup(); 16418357Ssam } 1659242Ssam 16618357Ssam char magic[2] = { 0377, 0377 }; 167*25423Skarels char oobdata[] = {TIOCPKT_WINDOW}; 16818357Ssam 16918357Ssam /* 17018357Ssam * Handle a "control" request (signaled by magic being present) 17118357Ssam * in the data stream. For now, we are only willing to handle 17218357Ssam * window size changes. 17318357Ssam */ 17418357Ssam control(pty, cp, n) 17518357Ssam int pty; 17618357Ssam char *cp; 17718357Ssam int n; 17818357Ssam { 17918357Ssam struct winsize *wp; 18018357Ssam 18118357Ssam if (n < 4+sizeof (*wp) || cp[2] != 's' || cp[3] != 's') 18218357Ssam return (0); 183*25423Skarels oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 18418357Ssam wp = (struct winsize *)(cp+4); 18518357Ssam wp->ws_row = ntohs(wp->ws_row); 18618357Ssam wp->ws_col = ntohs(wp->ws_col); 18718357Ssam wp->ws_xpixel = ntohs(wp->ws_xpixel); 18818357Ssam wp->ws_ypixel = ntohs(wp->ws_ypixel); 18918357Ssam (void)ioctl(pty, TIOCSWINSZ, wp); 19018357Ssam return (4+sizeof (*wp)); 19118357Ssam } 19218357Ssam 19318357Ssam /* 19418357Ssam * rlogin "protocol" machine. 19518357Ssam */ 19618357Ssam protocol(f, p) 19718357Ssam int f, p; 19818357Ssam { 19918357Ssam char pibuf[1024], fibuf[1024], *pbp, *fbp; 20018357Ssam register pcc = 0, fcc = 0; 201*25423Skarels int cc; 20218357Ssam 20318482Ssam /* 20418484Ssam * Must ignore SIGTTOU, otherwise we'll stop 20518484Ssam * when we try and set slave pty's window shape 206*25423Skarels * (our controlling tty is the master pty). 20718482Ssam */ 20818484Ssam (void) signal(SIGTTOU, SIG_IGN); 209*25423Skarels send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 21018357Ssam for (;;) { 21118357Ssam int ibits = 0, obits = 0; 21218357Ssam 21318357Ssam if (fcc) 21418357Ssam obits |= (1<<p); 21518357Ssam else 21618357Ssam ibits |= (1<<f); 21718357Ssam if (pcc >= 0) 21818357Ssam if (pcc) 21918357Ssam obits |= (1<<f); 2209242Ssam else 22118357Ssam ibits |= (1<<p); 22218357Ssam if (select(16, &ibits, &obits, 0, 0) < 0) { 22318357Ssam if (errno == EINTR) 2246446Swnj continue; 22518357Ssam fatalperror(f, "select", errno); 22618357Ssam } 22718357Ssam if (ibits == 0 && obits == 0) { 22818357Ssam /* shouldn't happen... */ 22918357Ssam sleep(5); 23018357Ssam continue; 23118357Ssam } 23218357Ssam if (ibits & (1<<f)) { 23318357Ssam fcc = read(f, fibuf, sizeof (fibuf)); 23418357Ssam if (fcc < 0 && errno == EWOULDBLOCK) 23518357Ssam fcc = 0; 23618357Ssam else { 23718357Ssam register char *cp; 23818357Ssam int left, n; 23918357Ssam 24018357Ssam if (fcc <= 0) 24116227Skarels break; 24218357Ssam fbp = fibuf; 24324723Smckusick 24418357Ssam top: 245*25423Skarels for (cp = fibuf; cp < fibuf+fcc-1; cp++) 24618357Ssam if (cp[0] == magic[0] && 24718357Ssam cp[1] == magic[1]) { 24818357Ssam left = fcc - (cp-fibuf); 24918357Ssam n = control(p, cp, left); 25018357Ssam if (n) { 25118357Ssam left -= n; 25218357Ssam if (left > 0) 253*25423Skarels bcopy(cp+n, cp, left); 25418357Ssam fcc -= n; 25518357Ssam goto top; /* n^2 */ 256*25423Skarels } 257*25423Skarels } 258*25423Skarels } 259*25423Skarels } 26024723Smckusick 26124723Smckusick if ((obits & (1<<p)) && fcc > 0) { 262*25423Skarels cc = write(p, fbp, fcc); 26324723Smckusick if (cc > 0) { 26424723Smckusick fcc -= cc; 26524723Smckusick fbp += cc; 2666446Swnj } 26718357Ssam } 26824723Smckusick 26918357Ssam if (ibits & (1<<p)) { 27018357Ssam pcc = read(p, pibuf, sizeof (pibuf)); 27118357Ssam pbp = pibuf; 27218357Ssam if (pcc < 0 && errno == EWOULDBLOCK) 27318357Ssam pcc = 0; 27418357Ssam else if (pcc <= 0) 27518357Ssam break; 27618357Ssam else if (pibuf[0] == 0) 27718357Ssam pbp++, pcc--; 27818357Ssam else { 27918357Ssam #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 28018357Ssam if (pkcontrol(pibuf[0])) { 281*25423Skarels pibuf[0] |= oobdata[0]; 28218357Ssam send(f, &pibuf[0], 1, MSG_OOB); 28316227Skarels } 28418357Ssam pcc = 0; 2856446Swnj } 28618357Ssam } 28718357Ssam if ((obits & (1<<f)) && pcc > 0) { 288*25423Skarels cc = write(f, pbp, pcc); 289*25423Skarels if (cc < 0 && errno == EWOULDBLOCK) { 290*25423Skarels /* also shouldn't happen */ 291*25423Skarels sleep(5); 292*25423Skarels continue; 293*25423Skarels } 29418357Ssam if (cc > 0) { 29518357Ssam pcc -= cc; 29618357Ssam pbp += cc; 29718357Ssam } 2986446Swnj } 2996446Swnj } 3006446Swnj } 3016446Swnj 3026446Swnj cleanup() 3036446Swnj { 3046446Swnj 3056446Swnj rmut(); 30610009Ssam vhangup(); /* XXX */ 30710192Ssam shutdown(netf, 2); 3086446Swnj exit(1); 3096446Swnj } 3106446Swnj 3119242Ssam fatal(f, msg) 3129242Ssam int f; 3139242Ssam char *msg; 3149242Ssam { 3159242Ssam char buf[BUFSIZ]; 3169242Ssam 3179242Ssam buf[0] = '\01'; /* error indicator */ 31813554Ssam (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 3199242Ssam (void) write(f, buf, strlen(buf)); 3209242Ssam exit(1); 3219242Ssam } 3229242Ssam 3239242Ssam fatalperror(f, msg, errno) 3249242Ssam int f; 3259242Ssam char *msg; 3269242Ssam int errno; 3279242Ssam { 3289242Ssam char buf[BUFSIZ]; 32916227Skarels extern int sys_nerr; 3309242Ssam extern char *sys_errlist[]; 3319242Ssam 33218357Ssam if ((unsigned)errno < sys_nerr) 33316227Skarels (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 33416227Skarels else 33516227Skarels (void) sprintf(buf, "%s: Error %d", msg, errno); 3369242Ssam fatal(f, buf); 3379242Ssam } 3389242Ssam 3396446Swnj #include <utmp.h> 3406446Swnj 3416446Swnj struct utmp wtmp; 3426446Swnj char wtmpf[] = "/usr/adm/wtmp"; 34323566Sbloom char utmpf[] = "/etc/utmp"; 3446446Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 3456446Swnj #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 3466446Swnj 3476446Swnj rmut() 3486446Swnj { 3496446Swnj register f; 3506446Swnj int found = 0; 35123566Sbloom struct utmp *u, *utmp; 35223566Sbloom int nutmp; 35323566Sbloom struct stat statbf; 3546446Swnj 35523566Sbloom f = open(utmpf, O_RDWR); 3566446Swnj if (f >= 0) { 35723566Sbloom fstat(f, &statbf); 35823566Sbloom utmp = (struct utmp *)malloc(statbf.st_size); 35923566Sbloom if (!utmp) 36023566Sbloom syslog(LOG_ERR, "utmp malloc failed"); 36123566Sbloom if (statbf.st_size && utmp) { 36223566Sbloom nutmp = read(f, utmp, statbf.st_size); 36323566Sbloom nutmp /= sizeof(struct utmp); 36423566Sbloom 36523566Sbloom for (u = utmp ; u < &utmp[nutmp] ; u++) { 36623566Sbloom if (SCMPN(u->ut_line, line+5) || 36723566Sbloom u->ut_name[0]==0) 36823566Sbloom continue; 36923566Sbloom lseek(f, ((long)u)-((long)utmp), L_SET); 37023566Sbloom SCPYN(u->ut_name, ""); 37123566Sbloom SCPYN(u->ut_host, ""); 37223566Sbloom time(&u->ut_time); 37323566Sbloom write(f, (char *)u, sizeof(wtmp)); 37423566Sbloom found++; 37523566Sbloom } 3766446Swnj } 3776446Swnj close(f); 3786446Swnj } 3796446Swnj if (found) { 38023566Sbloom f = open(wtmpf, O_WRONLY|O_APPEND); 3816446Swnj if (f >= 0) { 3826446Swnj SCPYN(wtmp.ut_line, line+5); 3836446Swnj SCPYN(wtmp.ut_name, ""); 38412681Ssam SCPYN(wtmp.ut_host, ""); 3856446Swnj time(&wtmp.ut_time); 3866446Swnj write(f, (char *)&wtmp, sizeof(wtmp)); 3876446Swnj close(f); 3886446Swnj } 3896446Swnj } 3906446Swnj chmod(line, 0666); 3916446Swnj chown(line, 0, 0); 3926446Swnj line[strlen("/dev/")] = 'p'; 3936446Swnj chmod(line, 0666); 3946446Swnj chown(line, 0, 0); 3956446Swnj } 396