144346Skarels /* 240858Sbostic * Copyright (c) 1983, 1990 The Regents of the University of California. 335539Sbostic * All rights reserved. 435539Sbostic * 542763Sbostic * %sccs.include.redist.c% 621595Sdist */ 721595Sdist 86444Swnj #ifndef lint 921595Sdist char copyright[] = 1040858Sbostic "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\ 1121595Sdist All rights reserved.\n"; 1235539Sbostic #endif /* not lint */ 136444Swnj 1421595Sdist #ifndef lint 15*46850Sbostic static char sccsid[] = "@(#)rlogin.c 5.33 (Berkeley) 03/01/91"; 1635539Sbostic #endif /* not lint */ 1721595Sdist 1812990Ssam /* 1940858Sbostic * $Source: mit/rlogin/RCS/rlogin.c,v $ 2042763Sbostic * $Header: mit/rlogin/RCS/rlogin.c,v 5.2 89/07/26 12:11:21 kfall 2142763Sbostic * Exp Locker: kfall $ 2240858Sbostic */ 2340858Sbostic 2440858Sbostic /* 2512990Ssam * rlogin - remote login 2612990Ssam */ 2726981Skarels #include <sys/param.h> 2824727Smckusick #include <sys/file.h> 296444Swnj #include <sys/socket.h> 3040858Sbostic #include <sys/signal.h> 3129729Smckusick #include <sys/time.h> 3229729Smckusick #include <sys/resource.h> 3313620Ssam #include <sys/wait.h> 349365Ssam 359207Ssam #include <netinet/in.h> 3644346Skarels #include <netinet/in_systm.h> 3744346Skarels #include <netinet/ip.h> 3840858Sbostic #include <netdb.h> 399365Ssam 409365Ssam #include <sgtty.h> 4140858Sbostic #include <setjmp.h> 4245063Sbostic #include <varargs.h> 436444Swnj #include <errno.h> 446444Swnj #include <pwd.h> 4540858Sbostic #include <stdio.h> 4640858Sbostic #include <unistd.h> 4742058Sbostic #include <string.h> 486444Swnj 4940858Sbostic #ifdef KERBEROS 5041760Skfall #include <kerberosIV/des.h> 5140683Sbostic #include <kerberosIV/krb.h> 5236511Skfall 5340858Sbostic CREDENTIALS cred; 5440858Sbostic Key_schedule schedule; 55*46850Sbostic int use_kerberos = 1, doencrypt; 5640858Sbostic char dst_realm_buf[REALM_SZ], *dest_realm = NULL; 5740858Sbostic extern char *krb_realmofhost(); 5840858Sbostic #endif 5924726Smckusick 6040858Sbostic #ifndef TIOCPKT_WINDOW 6140858Sbostic #define TIOCPKT_WINDOW 0x80 6240858Sbostic #endif 6329729Smckusick 6440858Sbostic /* concession to Sun */ 6540858Sbostic #ifndef SIGUSR1 6640858Sbostic #define SIGUSR1 30 6726981Skarels #endif 6840858Sbostic 6940858Sbostic extern int errno; 7040858Sbostic int eight, litout, rem; 7145063Sbostic 7245063Sbostic int noescape; 7345063Sbostic u_char escapechar = '~'; 7445063Sbostic 7540858Sbostic char *speeds[] = { 7640858Sbostic "0", "50", "75", "110", "134", "150", "200", "300", "600", "1200", 7740858Sbostic "1800", "2400", "4800", "9600", "19200", "38400" 7840858Sbostic }; 7940858Sbostic 8026981Skarels #ifdef sun 8126981Skarels struct winsize { 8226981Skarels unsigned short ws_row, ws_col; 8326981Skarels unsigned short ws_xpixel, ws_ypixel; 8426981Skarels }; 8540858Sbostic #endif 8618358Ssam struct winsize winsize; 876444Swnj 8840858Sbostic #ifndef sun 8940858Sbostic #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp) 9040858Sbostic #endif 9129729Smckusick 9240858Sbostic void exit(); 9329729Smckusick 946444Swnj main(argc, argv) 956444Swnj int argc; 966444Swnj char **argv; 976444Swnj { 9840858Sbostic extern char *optarg; 9940858Sbostic extern int optind; 10040858Sbostic struct passwd *pw; 10140858Sbostic struct servent *sp; 1026444Swnj struct sgttyb ttyb; 10340858Sbostic long omask; 10440858Sbostic int argoff, ch, dflag, one, uid; 10540858Sbostic char *host, *p, *user, term[1024]; 10640858Sbostic void lostpeer(); 10745063Sbostic u_char getescape(); 10840858Sbostic char *getenv(); 1096444Swnj 11040858Sbostic argoff = dflag = 0; 11140858Sbostic one = 1; 11240858Sbostic host = user = NULL; 11340858Sbostic 11440858Sbostic if (p = rindex(argv[0], '/')) 11540858Sbostic ++p; 1166444Swnj else 11740858Sbostic p = argv[0]; 11840858Sbostic 11940858Sbostic if (strcmp(p, "rlogin")) 12040858Sbostic host = p; 12140858Sbostic 12240858Sbostic /* handle "rlogin host flags" */ 12340858Sbostic if (!host && argc > 2 && argv[1][0] != '-') { 12440858Sbostic host = argv[1]; 12540858Sbostic argoff = 1; 1266444Swnj } 12736511Skfall 12840858Sbostic #ifdef KERBEROS 12945063Sbostic #define OPTIONS "8EKLde:k:l:x" 13040858Sbostic #else 13145063Sbostic #define OPTIONS "8EKLde:l:" 13240858Sbostic #endif 13340858Sbostic while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != EOF) 13440858Sbostic switch(ch) { 13540858Sbostic case '8': 13640858Sbostic eight = 1; 13740858Sbostic break; 13845063Sbostic case 'E': 13945063Sbostic noescape = 1; 14045063Sbostic break; 14140868Sbostic case 'K': 14240868Sbostic #ifdef KERBEROS 14340868Sbostic use_kerberos = 0; 14440868Sbostic #endif 14540868Sbostic break; 14640858Sbostic case 'L': 14740858Sbostic litout = 1; 14840858Sbostic break; 14940858Sbostic case 'd': 15040858Sbostic dflag = 1; 15140858Sbostic break; 15240858Sbostic case 'e': 15345063Sbostic escapechar = getescape(optarg); 15440858Sbostic break; 15540858Sbostic #ifdef KERBEROS 15640858Sbostic case 'k': 15740858Sbostic dest_realm = dst_realm_buf; 15840858Sbostic (void)strncpy(dest_realm, optarg, REALM_SZ); 15940858Sbostic break; 16040858Sbostic #endif 16140858Sbostic case 'l': 16240858Sbostic user = optarg; 16340858Sbostic break; 164*46850Sbostic #ifdef CRYPT 165*46850Sbostic #ifdef KERBEROS 166*46850Sbostic case 'x': 167*46850Sbostic doencrypt = 1; 168*46850Sbostic des_set_key(cred.session, schedule); 169*46850Sbostic break; 170*46850Sbostic #endif 171*46850Sbostic #endif 17240858Sbostic case '?': 17340858Sbostic default: 17440858Sbostic usage(); 17536524Skfall } 17640858Sbostic optind += argoff; 17740858Sbostic argc -= optind; 17840858Sbostic argv += optind; 17936524Skfall 18040858Sbostic /* if haven't gotten a host yet, do so */ 18140858Sbostic if (!host && !(host = *argv++)) 18240858Sbostic usage(); 18336511Skfall 18440858Sbostic if (*argv) 18540858Sbostic usage(); 18640858Sbostic 18740858Sbostic if (!(pw = getpwuid(uid = getuid()))) { 18840858Sbostic (void)fprintf(stderr, "rlogin: unknown user id.\n"); 1896444Swnj exit(1); 1906444Swnj } 19140858Sbostic if (!user) 19240858Sbostic user = pw->pw_name; 19340858Sbostic 19440868Sbostic sp = NULL; 19540858Sbostic #ifdef KERBEROS 19640868Sbostic if (use_kerberos) { 197*46850Sbostic sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp"); 19840868Sbostic if (sp == NULL) { 19940868Sbostic use_kerberos = 0; 20040868Sbostic warning("can't get entry for %s/tcp service", 201*46850Sbostic doencrypt ? "eklogin" : "klogin"); 20240868Sbostic } 20336512Skfall } 20436512Skfall #endif 20540868Sbostic if (sp == NULL) 20640868Sbostic sp = getservbyname("login", "tcp"); 20740858Sbostic if (sp == NULL) { 20840858Sbostic (void)fprintf(stderr, "rlogin: login/tcp: unknown service.\n"); 20940858Sbostic exit(1); 2109365Ssam } 21140858Sbostic 21240858Sbostic (void)strcpy(term, (p = getenv("TERM")) ? p : "network"); 21318358Ssam if (ioctl(0, TIOCGETP, &ttyb) == 0) { 21440858Sbostic (void)strcat(term, "/"); 21540858Sbostic (void)strcat(term, speeds[ttyb.sg_ospeed]); 2166444Swnj } 21740858Sbostic 21840858Sbostic (void)get_window_size(0, &winsize); 21940858Sbostic 22040858Sbostic (void)signal(SIGPIPE, lostpeer); 22129729Smckusick /* will use SIGUSR1 for window size hack, so hold it off */ 22240858Sbostic omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1)); 22336511Skfall 22440858Sbostic #ifdef KERBEROS 22536512Skfall try_connect: 22640858Sbostic if (use_kerberos) { 22736512Skfall rem = KSUCCESS; 22840858Sbostic errno = 0; 22938728Skfall if (dest_realm == NULL) 23038728Skfall dest_realm = krb_realmofhost(host); 23138728Skfall 232*46850Sbostic #ifdef CRYPT 233*46850Sbostic if (doencrypt) 234*46850Sbostic rem = krcmd_mutual(&host, sp->s_port, user, term, 0, 235*46850Sbostic dest_realm, &cred, schedule); 236*46850Sbostic else 237*46850Sbostic #endif /* CRYPT */ 23840858Sbostic rem = krcmd(&host, sp->s_port, user, term, 0, 23940858Sbostic dest_realm); 24038728Skfall if (rem < 0) { 24136512Skfall use_kerberos = 0; 24236628Skfall sp = getservbyname("login", "tcp"); 24340858Sbostic if (sp == NULL) { 24440858Sbostic (void)fprintf(stderr, 24540858Sbostic "rlogin: unknown service login/tcp.\n"); 24636628Skfall exit(1); 24736628Skfall } 24838728Skfall if (errno == ECONNREFUSED) 24940858Sbostic warning("remote host doesn't support Kerberos"); 25038728Skfall if (errno == ENOENT) 25140858Sbostic warning("can't provide Kerberos auth data"); 25236512Skfall goto try_connect; 25336512Skfall } 25436511Skfall } else { 255*46850Sbostic #ifdef CRYPT 256*46850Sbostic if (doencrypt) { 257*46850Sbostic (void)fprintf(stderr, 258*46850Sbostic "rlogin: the -x flag requires Kerberos authentication.\n"); 259*46850Sbostic exit(1); 260*46850Sbostic } 261*46850Sbostic #endif /* CRYPT */ 26240858Sbostic rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0); 26336511Skfall } 26436512Skfall #else 26540858Sbostic rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0); 26645256Smckusick #endif /* KERBEROS */ 26736511Skfall 26840858Sbostic if (rem < 0) 26936511Skfall exit(1); 27036511Skfall 27140858Sbostic if (dflag && 27240858Sbostic setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0) 27340858Sbostic (void)fprintf(stderr, "rlogin: setsockopt: %s.\n", 27440858Sbostic strerror(errno)); 27544346Skarels one = IPTOS_LOWDELAY; 27644346Skarels if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0) 27744346Skarels perror("rlogin: setsockopt TOS (ignored)"); 27840858Sbostic 27940858Sbostic (void)setuid(uid); 28040858Sbostic doit(omask); 2819365Ssam /*NOTREACHED*/ 2826444Swnj } 2836444Swnj 28440858Sbostic int child, defflags, deflflags, tabflag; 28540858Sbostic char deferase, defkill; 28640858Sbostic struct tchars deftc; 28740858Sbostic struct ltchars defltc; 28840858Sbostic struct tchars notc = { -1, -1, -1, -1, -1, -1 }; 28940858Sbostic struct ltchars noltc = { -1, -1, -1, -1, -1, -1 }; 2906444Swnj 29140858Sbostic doit(omask) 29240858Sbostic long omask; 2936444Swnj { 29413075Ssam struct sgttyb sb; 29540858Sbostic void catch_child(), copytochild(), exit(), writeroob(); 2966444Swnj 29740858Sbostic (void)ioctl(0, TIOCGETP, (char *)&sb); 29813075Ssam defflags = sb.sg_flags; 29912155Ssam tabflag = defflags & TBDELAY; 3009962Ssam defflags &= ECHO | CRMOD; 30113075Ssam deferase = sb.sg_erase; 30213075Ssam defkill = sb.sg_kill; 30340858Sbostic (void)ioctl(0, TIOCLGET, (char *)&deflflags); 30440858Sbostic (void)ioctl(0, TIOCGETC, (char *)&deftc); 30513075Ssam notc.t_startc = deftc.t_startc; 30613075Ssam notc.t_stopc = deftc.t_stopc; 30740858Sbostic (void)ioctl(0, TIOCGLTC, (char *)&defltc); 30840858Sbostic (void)signal(SIGINT, SIG_IGN); 30929729Smckusick setsignal(SIGHUP, exit); 31029729Smckusick setsignal(SIGQUIT, exit); 3119365Ssam child = fork(); 3129365Ssam if (child == -1) { 31340858Sbostic (void)fprintf(stderr, "rlogin: fork: %s.\n", strerror(errno)); 31425424Skarels done(1); 3159365Ssam } 3169365Ssam if (child == 0) { 31724726Smckusick mode(1); 31840858Sbostic if (reader(omask) == 0) { 31940858Sbostic msg("connection closed."); 32025424Skarels exit(0); 32125424Skarels } 32212155Ssam sleep(1); 32340858Sbostic msg("\007connection closed."); 32440858Sbostic exit(1); 3256444Swnj } 32629729Smckusick 32729729Smckusick /* 32840858Sbostic * We may still own the socket, and may have a pending SIGURG (or might 32940858Sbostic * receive one soon) that we really want to send to the reader. Set a 33040858Sbostic * trap that simply copies such signals to the child. 33129729Smckusick */ 33240858Sbostic (void)signal(SIGURG, copytochild); 33340858Sbostic (void)signal(SIGUSR1, writeroob); 33440858Sbostic (void)sigsetmask(omask); 33540858Sbostic (void)signal(SIGCHLD, catch_child); 3369365Ssam writer(); 33740858Sbostic msg("closed connection."); 33825424Skarels done(0); 3396444Swnj } 3406444Swnj 34140858Sbostic /* trap a signal, unless it is being ignored. */ 34229729Smckusick setsignal(sig, act) 34340858Sbostic int sig; 34440858Sbostic void (*act)(); 34529729Smckusick { 34629729Smckusick int omask = sigblock(sigmask(sig)); 34729729Smckusick 34829729Smckusick if (signal(sig, act) == SIG_IGN) 34940858Sbostic (void)signal(sig, SIG_IGN); 35040858Sbostic (void)sigsetmask(omask); 35129729Smckusick } 35229729Smckusick 35325424Skarels done(status) 35425424Skarels int status; 3556444Swnj { 356*46850Sbostic int w, wstatus; 3576444Swnj 3586444Swnj mode(0); 35929729Smckusick if (child > 0) { 36040858Sbostic /* make sure catch_child does not snap it up */ 36140858Sbostic (void)signal(SIGCHLD, SIG_DFL); 36229729Smckusick if (kill(child, SIGKILL) >= 0) 363*46850Sbostic while ((w = wait(&wstatus)) > 0 && w != child); 36429729Smckusick } 36525424Skarels exit(status); 3666444Swnj } 3676444Swnj 36840858Sbostic int dosigwinch; 369*46850Sbostic void sigwinch(); 37029729Smckusick 37129729Smckusick /* 37224726Smckusick * This is called when the reader process gets the out-of-band (urgent) 37324726Smckusick * request to turn on the window-changing protocol. 37424726Smckusick */ 37540858Sbostic void 37624726Smckusick writeroob() 37724726Smckusick { 37825341Smckusick if (dosigwinch == 0) { 37924919Smckusick sendwindow(); 38040858Sbostic (void)signal(SIGWINCH, sigwinch); 38125341Smckusick } 38224726Smckusick dosigwinch = 1; 38324726Smckusick } 38424726Smckusick 38540858Sbostic void 38640858Sbostic catch_child() 38711803Sedward { 38811803Sedward union wait status; 38911803Sedward int pid; 39011803Sedward 39140858Sbostic for (;;) { 392*46850Sbostic pid = wait3((int *)&status, 393*46850Sbostic WNOHANG|WUNTRACED, (struct rusage *)0); 39440858Sbostic if (pid == 0) 39540858Sbostic return; 39640858Sbostic /* if the child (reader) dies, just quit */ 39740858Sbostic if (pid < 0 || pid == child && !WIFSTOPPED(status)) 39840858Sbostic done((int)(status.w_termsig | status.w_retcode)); 39940858Sbostic } 40040858Sbostic /* NOTREACHED */ 40111803Sedward } 40211803Sedward 4036444Swnj /* 4049365Ssam * writer: write to remote: 0 -> line. 40545063Sbostic * ~. terminate 40645063Sbostic * ~^Z suspend rlogin process. 40745063Sbostic * ~<delayed-suspend char> suspend rlogin process, but leave reader alone. 4086444Swnj */ 4099365Ssam writer() 4106444Swnj { 41145063Sbostic register int bol, local, n; 41223530Sbloom char c; 4136444Swnj 41440858Sbostic bol = 1; /* beginning of line */ 41540858Sbostic local = 0; 41611803Sedward for (;;) { 41740858Sbostic n = read(STDIN_FILENO, &c, 1); 41818358Ssam if (n <= 0) { 41918358Ssam if (n < 0 && errno == EINTR) 42018358Ssam continue; 42111803Sedward break; 42218358Ssam } 4239365Ssam /* 42440858Sbostic * If we're at the beginning of the line and recognize a 42540858Sbostic * command character, then we echo locally. Otherwise, 42640858Sbostic * characters are echo'd remotely. If the command character 42740858Sbostic * is doubled, this acts as a force and local echo is 42840858Sbostic * suppressed. 4299365Ssam */ 43023530Sbloom if (bol) { 43123530Sbloom bol = 0; 43245063Sbostic if (!noescape && c == escapechar) { 43323530Sbloom local = 1; 43423530Sbloom continue; 4356444Swnj } 43623530Sbloom } else if (local) { 43723530Sbloom local = 0; 43823530Sbloom if (c == '.' || c == deftc.t_eofc) { 43923530Sbloom echo(c); 44023530Sbloom break; 4416444Swnj } 44223530Sbloom if (c == defltc.t_suspc || c == defltc.t_dsuspc) { 44323530Sbloom bol = 1; 44423530Sbloom echo(c); 44523530Sbloom stop(c); 44623530Sbloom continue; 44723530Sbloom } 44845063Sbostic if (c != escapechar) 449*46850Sbostic #ifdef CRYPT 450*46850Sbostic #ifdef KERBEROS 451*46850Sbostic if (doencrypt) 452*46850Sbostic (void)des_write(rem, &escapechar, 1); 453*46850Sbostic else 454*46850Sbostic #endif 455*46850Sbostic #endif 45645063Sbostic (void)write(rem, &escapechar, 1); 4576444Swnj } 45836511Skfall 459*46850Sbostic #ifdef CRYPT 460*46850Sbostic #ifdef KERBEROS 461*46850Sbostic if (doencrypt) { 462*46850Sbostic if (des_write(rem, &c, 1) == 0) { 463*46850Sbostic msg("line gone"); 464*46850Sbostic break; 465*46850Sbostic } 466*46850Sbostic } else 467*46850Sbostic #endif 468*46850Sbostic #endif 46936511Skfall if (write(rem, &c, 1) == 0) { 47040858Sbostic msg("line gone"); 47136511Skfall break; 47236511Skfall } 47323530Sbloom bol = c == defkill || c == deftc.t_eofc || 47425424Skarels c == deftc.t_intrc || c == defltc.t_suspc || 47523530Sbloom c == '\r' || c == '\n'; 4766444Swnj } 4776444Swnj } 4786444Swnj 47923530Sbloom echo(c) 48023530Sbloom register char c; 48123530Sbloom { 48240858Sbostic register char *p; 48323530Sbloom char buf[8]; 48423530Sbloom 48540858Sbostic p = buf; 48623530Sbloom c &= 0177; 48745063Sbostic *p++ = escapechar; 48823530Sbloom if (c < ' ') { 48923530Sbloom *p++ = '^'; 49023530Sbloom *p++ = c + '@'; 49123530Sbloom } else if (c == 0177) { 49223530Sbloom *p++ = '^'; 49323530Sbloom *p++ = '?'; 49423530Sbloom } else 49523530Sbloom *p++ = c; 49623530Sbloom *p++ = '\r'; 49723530Sbloom *p++ = '\n'; 49845063Sbostic (void)write(STDOUT_FILENO, buf, p - buf); 49923530Sbloom } 50023530Sbloom 50118358Ssam stop(cmdc) 50218358Ssam char cmdc; 50318358Ssam { 50418358Ssam mode(0); 50540858Sbostic (void)signal(SIGCHLD, SIG_IGN); 50640858Sbostic (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP); 50740858Sbostic (void)signal(SIGCHLD, catch_child); 50818358Ssam mode(1); 50918358Ssam sigwinch(); /* check for size changes */ 51018358Ssam } 51118358Ssam 51240858Sbostic void 51318358Ssam sigwinch() 51418358Ssam { 51518358Ssam struct winsize ws; 51618358Ssam 51729729Smckusick if (dosigwinch && get_window_size(0, &ws) == 0 && 51840858Sbostic bcmp(&ws, &winsize, sizeof(ws))) { 51918358Ssam winsize = ws; 52024726Smckusick sendwindow(); 52118358Ssam } 52218358Ssam } 52318358Ssam 52424726Smckusick /* 52524726Smckusick * Send the window size to the server via the magic escape 52624726Smckusick */ 52724726Smckusick sendwindow() 52824726Smckusick { 52940858Sbostic struct winsize *wp; 53024726Smckusick char obuf[4 + sizeof (struct winsize)]; 53124726Smckusick 53240858Sbostic wp = (struct winsize *)(obuf+4); 53324726Smckusick obuf[0] = 0377; 53424726Smckusick obuf[1] = 0377; 53524726Smckusick obuf[2] = 's'; 53624726Smckusick obuf[3] = 's'; 53724726Smckusick wp->ws_row = htons(winsize.ws_row); 53824726Smckusick wp->ws_col = htons(winsize.ws_col); 53924726Smckusick wp->ws_xpixel = htons(winsize.ws_xpixel); 54024726Smckusick wp->ws_ypixel = htons(winsize.ws_ypixel); 54136511Skfall 542*46850Sbostic #ifdef CRYPT 543*46850Sbostic #ifdef KERBEROS 544*46850Sbostic if(doencrypt) 545*46850Sbostic (void)des_write(rem, obuf, sizeof(obuf)); 546*46850Sbostic else 547*46850Sbostic #endif 548*46850Sbostic #endif 54940858Sbostic (void)write(rem, obuf, sizeof(obuf)); 55024726Smckusick } 55124726Smckusick 55225424Skarels /* 55325424Skarels * reader: read from remote: line -> 1 55425424Skarels */ 55525424Skarels #define READING 1 55625424Skarels #define WRITING 2 55725424Skarels 55840858Sbostic jmp_buf rcvtop; 55940858Sbostic int ppid, rcvcnt, rcvstate; 56040858Sbostic char rcvbuf[8 * 1024]; 56125424Skarels 56240858Sbostic void 5636444Swnj oob() 5646444Swnj { 56540858Sbostic struct sgttyb sb; 56640858Sbostic int atmark, n, out, rcvd; 5679365Ssam char waste[BUFSIZ], mark; 5686444Swnj 56942230Sbostic out = O_RDWR; 57040858Sbostic rcvd = 0; 57125424Skarels while (recv(rem, &mark, 1, MSG_OOB) < 0) 57225424Skarels switch (errno) { 57325424Skarels case EWOULDBLOCK: 57425424Skarels /* 57540858Sbostic * Urgent data not here yet. It may not be possible 57640858Sbostic * to send it yet if we are blocked for output and 57740858Sbostic * our input buffer is full. 57825424Skarels */ 57925424Skarels if (rcvcnt < sizeof(rcvbuf)) { 58025424Skarels n = read(rem, rcvbuf + rcvcnt, 58140858Sbostic sizeof(rcvbuf) - rcvcnt); 58225424Skarels if (n <= 0) 58325424Skarels return; 58425424Skarels rcvd += n; 58525424Skarels } else { 58625424Skarels n = read(rem, waste, sizeof(waste)); 58725424Skarels if (n <= 0) 58825424Skarels return; 58925424Skarels } 59025424Skarels continue; 59125424Skarels default: 59225424Skarels return; 5936444Swnj } 59425424Skarels if (mark & TIOCPKT_WINDOW) { 59540858Sbostic /* Let server know about window size changes */ 59640858Sbostic (void)kill(ppid, SIGUSR1); 59724726Smckusick } 59825424Skarels if (!eight && (mark & TIOCPKT_NOSTOP)) { 59940858Sbostic (void)ioctl(0, TIOCGETP, (char *)&sb); 60024726Smckusick sb.sg_flags &= ~CBREAK; 60124726Smckusick sb.sg_flags |= RAW; 60240858Sbostic (void)ioctl(0, TIOCSETN, (char *)&sb); 60313075Ssam notc.t_stopc = -1; 60413075Ssam notc.t_startc = -1; 60540858Sbostic (void)ioctl(0, TIOCSETC, (char *)¬c); 6066444Swnj } 60725424Skarels if (!eight && (mark & TIOCPKT_DOSTOP)) { 60840858Sbostic (void)ioctl(0, TIOCGETP, (char *)&sb); 60924726Smckusick sb.sg_flags &= ~RAW; 61024726Smckusick sb.sg_flags |= CBREAK; 61140858Sbostic (void)ioctl(0, TIOCSETN, (char *)&sb); 61213075Ssam notc.t_stopc = deftc.t_stopc; 61313075Ssam notc.t_startc = deftc.t_startc; 61440858Sbostic (void)ioctl(0, TIOCSETC, (char *)¬c); 6156444Swnj } 61625424Skarels if (mark & TIOCPKT_FLUSHWRITE) { 61740858Sbostic (void)ioctl(1, TIOCFLUSH, (char *)&out); 61825424Skarels for (;;) { 61925424Skarels if (ioctl(rem, SIOCATMARK, &atmark) < 0) { 62040858Sbostic (void)fprintf(stderr, "rlogin: ioctl: %s.\n", 62140858Sbostic strerror(errno)); 62225424Skarels break; 62325424Skarels } 62425424Skarels if (atmark) 62525424Skarels break; 62625424Skarels n = read(rem, waste, sizeof (waste)); 62725424Skarels if (n <= 0) 62825424Skarels break; 62925424Skarels } 63025424Skarels /* 63140858Sbostic * Don't want any pending data to be output, so clear the recv 63240858Sbostic * buffer. If we were hanging on a write when interrupted, 63340858Sbostic * don't want it to restart. If we were reading, restart 63440858Sbostic * anyway. 63525424Skarels */ 63625424Skarels rcvcnt = 0; 63725424Skarels longjmp(rcvtop, 1); 63825424Skarels } 63929729Smckusick 64040858Sbostic /* oob does not do FLUSHREAD (alas!) */ 64129729Smckusick 64229729Smckusick /* 64340858Sbostic * If we filled the receive buffer while a read was pending, longjmp 64440858Sbostic * to the top to restart appropriately. Don't abort a pending write, 64540858Sbostic * however, or we won't know how much was written. 64625424Skarels */ 64725424Skarels if (rcvd && rcvstate == READING) 64825424Skarels longjmp(rcvtop, 1); 6496444Swnj } 6506444Swnj 65140858Sbostic /* reader: read from remote: line -> 1 */ 65240858Sbostic reader(omask) 65340858Sbostic int omask; 6546444Swnj { 65540858Sbostic void oob(); 65640858Sbostic 65726981Skarels #if !defined(BSD) || BSD < 43 65826981Skarels int pid = -getpid(); 65926981Skarels #else 66025424Skarels int pid = getpid(); 66126981Skarels #endif 66225424Skarels int n, remaining; 66325424Skarels char *bufp = rcvbuf; 6646444Swnj 66540858Sbostic (void)signal(SIGTTOU, SIG_IGN); 66640858Sbostic (void)signal(SIGURG, oob); 66726981Skarels ppid = getppid(); 66840858Sbostic (void)fcntl(rem, F_SETOWN, pid); 66940858Sbostic (void)setjmp(rcvtop); 67040858Sbostic (void)sigsetmask(omask); 6716444Swnj for (;;) { 67225424Skarels while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) { 67325424Skarels rcvstate = WRITING; 67445063Sbostic n = write(STDOUT_FILENO, bufp, remaining); 67525424Skarels if (n < 0) { 67625424Skarels if (errno != EINTR) 67740858Sbostic return(-1); 67825424Skarels continue; 67925424Skarels } 68025424Skarels bufp += n; 68125424Skarels } 68225424Skarels bufp = rcvbuf; 68325424Skarels rcvcnt = 0; 68425424Skarels rcvstate = READING; 68536511Skfall 686*46850Sbostic #ifdef CRYPT 687*46850Sbostic #ifdef KERBEROS 688*46850Sbostic if (doencrypt) 689*46850Sbostic rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf)); 690*46850Sbostic else 691*46850Sbostic #endif 692*46850Sbostic #endif 69336511Skfall rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf)); 69425424Skarels if (rcvcnt == 0) 69525424Skarels return (0); 69625424Skarels if (rcvcnt < 0) { 6979365Ssam if (errno == EINTR) 6986444Swnj continue; 69940858Sbostic (void)fprintf(stderr, "rlogin: read: %s.\n", 70040858Sbostic strerror(errno)); 70140858Sbostic return(-1); 7026444Swnj } 7036444Swnj } 7046444Swnj } 7056444Swnj 7066444Swnj mode(f) 7076444Swnj { 70813075Ssam struct ltchars *ltc; 70913075Ssam struct sgttyb sb; 71040858Sbostic struct tchars *tc; 71140858Sbostic int lflags; 7129365Ssam 71340858Sbostic (void)ioctl(0, TIOCGETP, (char *)&sb); 71440858Sbostic (void)ioctl(0, TIOCLGET, (char *)&lflags); 71540858Sbostic switch(f) { 7169962Ssam case 0: 71713075Ssam sb.sg_flags &= ~(CBREAK|RAW|TBDELAY); 71813075Ssam sb.sg_flags |= defflags|tabflag; 7199962Ssam tc = &deftc; 72013075Ssam ltc = &defltc; 72113075Ssam sb.sg_kill = defkill; 72213075Ssam sb.sg_erase = deferase; 72321583Sbloom lflags = deflflags; 7249962Ssam break; 7259962Ssam case 1: 72613075Ssam sb.sg_flags |= (eight ? RAW : CBREAK); 72713075Ssam sb.sg_flags &= ~defflags; 72812155Ssam /* preserve tab delays, but turn off XTABS */ 72913075Ssam if ((sb.sg_flags & TBDELAY) == XTABS) 73013075Ssam sb.sg_flags &= ~TBDELAY; 7319962Ssam tc = ¬c; 73213075Ssam ltc = &noltc; 73313075Ssam sb.sg_kill = sb.sg_erase = -1; 73421583Sbloom if (litout) 73521583Sbloom lflags |= LLITOUT; 7369962Ssam break; 7379962Ssam default: 7389962Ssam return; 7396444Swnj } 74040858Sbostic (void)ioctl(0, TIOCSLTC, (char *)ltc); 74140858Sbostic (void)ioctl(0, TIOCSETC, (char *)tc); 74240858Sbostic (void)ioctl(0, TIOCSETN, (char *)&sb); 74340858Sbostic (void)ioctl(0, TIOCLSET, (char *)&lflags); 7446444Swnj } 7456444Swnj 74640858Sbostic void 74740858Sbostic lostpeer() 7486444Swnj { 74940858Sbostic (void)signal(SIGPIPE, SIG_IGN); 75040858Sbostic msg("\007connection closed."); 75140858Sbostic done(1); 75240858Sbostic } 75329729Smckusick 75440858Sbostic /* copy SIGURGs to the child process. */ 75540858Sbostic void 75640858Sbostic copytochild() 75740858Sbostic { 75840858Sbostic (void)kill(child, SIGURG); 7596444Swnj } 7606444Swnj 76140858Sbostic msg(str) 76240858Sbostic char *str; 7636444Swnj { 76440858Sbostic (void)fprintf(stderr, "rlogin: %s\r\n", str); 76540858Sbostic } 76629729Smckusick 76740858Sbostic #ifdef KERBEROS 76840858Sbostic /* VARARGS */ 76940858Sbostic warning(va_alist) 77040858Sbostic va_dcl 77140858Sbostic { 77240858Sbostic va_list ap; 77340858Sbostic char *fmt; 77440858Sbostic 77540858Sbostic (void)fprintf(stderr, "rlogin: warning, using standard rlogin: "); 77640858Sbostic va_start(ap); 77740858Sbostic fmt = va_arg(ap, char *); 77840858Sbostic vfprintf(stderr, fmt, ap); 77940858Sbostic va_end(ap); 78040858Sbostic (void)fprintf(stderr, ".\n"); 7816444Swnj } 78240858Sbostic #endif 78336512Skfall 78440858Sbostic usage() 78536512Skfall { 78640858Sbostic (void)fprintf(stderr, 78740858Sbostic "usage: rlogin [ -%s]%s[-e char] [ -l username ] host\n", 78840858Sbostic #ifdef KERBEROS 789*46850Sbostic #ifdef CRYPT 790*46850Sbostic "8ELx", " [-k realm] "); 791*46850Sbostic #else 79245256Smckusick "8EL", " [-k realm] "); 793*46850Sbostic #endif 79445256Smckusick #else 79545063Sbostic "8EL", " "); 79640858Sbostic #endif 79740858Sbostic exit(1); 79836512Skfall } 79940858Sbostic 80040858Sbostic /* 80140858Sbostic * The following routine provides compatibility (such as it is) between 4.2BSD 80240858Sbostic * Suns and others. Suns have only a `ttysize', so we convert it to a winsize. 80340858Sbostic */ 80440858Sbostic #ifdef sun 80540858Sbostic get_window_size(fd, wp) 80640858Sbostic int fd; 80740858Sbostic struct winsize *wp; 80840858Sbostic { 80940858Sbostic struct ttysize ts; 81040858Sbostic int error; 81140858Sbostic 81240858Sbostic if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0) 81340858Sbostic return(error); 81440858Sbostic wp->ws_row = ts.ts_lines; 81540858Sbostic wp->ws_col = ts.ts_cols; 81640858Sbostic wp->ws_xpixel = 0; 81740858Sbostic wp->ws_ypixel = 0; 81840858Sbostic return(0); 81940858Sbostic } 82040858Sbostic #endif 82145063Sbostic 82245063Sbostic u_char 82345063Sbostic getescape(p) 82445063Sbostic register char *p; 82545063Sbostic { 82645063Sbostic long val; 82745063Sbostic int len; 82845063Sbostic 82945063Sbostic if ((len = strlen(p)) == 1) /* use any single char, including '\' */ 83045063Sbostic return((u_char)*p); 83145063Sbostic /* otherwise, \nnn */ 83245063Sbostic if (*p == '\\' && len >= 2 && len <= 4) { 83345063Sbostic val = strtol(++p, (char **)NULL, 8); 83445063Sbostic for (;;) { 83545063Sbostic if (!*++p) 83645063Sbostic return((u_char)val); 83745063Sbostic if (*p < '0' || *p > '8') 83845063Sbostic break; 83945063Sbostic } 84045063Sbostic } 84145063Sbostic msg("illegal option value -- e"); 84245063Sbostic usage(); 84345063Sbostic /* NOTREACHED */ 84445063Sbostic } 845