121595Sdist /* 235539Sbostic * Copyright (c) 1983 The Regents of the University of California. 335539Sbostic * All rights reserved. 435539Sbostic * 535539Sbostic * Redistribution and use in source and binary forms are permitted 635539Sbostic * provided that the above copyright notice and this paragraph are 735539Sbostic * duplicated in all such forms and that any documentation, 835539Sbostic * advertising materials, and other materials related to such 935539Sbostic * distribution and use acknowledge that the software was developed 1035539Sbostic * by the University of California, Berkeley. The name of the 1135539Sbostic * University may not be used to endorse or promote products derived 1235539Sbostic * from this software without specific prior written permission. 1335539Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435539Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535539Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621595Sdist */ 1721595Sdist 186444Swnj #ifndef lint 1921595Sdist char copyright[] = 2035539Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 2121595Sdist All rights reserved.\n"; 2235539Sbostic #endif /* not lint */ 236444Swnj 2421595Sdist #ifndef lint 2536511Skfall static char sccsid[] = "@(#)rlogin.c 5.12 (Berkeley) 9/19/88"; 2635539Sbostic #endif /* not lint */ 2721595Sdist 2812990Ssam /* 2912990Ssam * rlogin - remote login 3012990Ssam */ 3126981Skarels #include <sys/param.h> 3225424Skarels #include <sys/errno.h> 3324727Smckusick #include <sys/file.h> 346444Swnj #include <sys/socket.h> 3529729Smckusick #include <sys/time.h> 3629729Smckusick #include <sys/resource.h> 3713620Ssam #include <sys/wait.h> 389365Ssam 399207Ssam #include <netinet/in.h> 409365Ssam 419365Ssam #include <stdio.h> 429365Ssam #include <sgtty.h> 436444Swnj #include <errno.h> 446444Swnj #include <pwd.h> 459365Ssam #include <signal.h> 4625424Skarels #include <setjmp.h> 479365Ssam #include <netdb.h> 486444Swnj 4936511Skfall #ifdef KERBEROS 5036511Skfall #include <kerberos/krb.h> 5136511Skfall int encrypt = 0; 5236511Skfall char krb_realm[REALM_SZ]; 5336511Skfall CREDENTIALS cred; 5436511Skfall Key_schedule schedule; 5536512Skfall int use_kerberos = 1; 5636511Skfall #endif /* KERBEROS */ 5736511Skfall 5824726Smckusick # ifndef TIOCPKT_WINDOW 5924726Smckusick # define TIOCPKT_WINDOW 0x80 6024726Smckusick # endif TIOCPKT_WINDOW 6124726Smckusick 6229729Smckusick /* concession to sun */ 6329729Smckusick # ifndef SIGUSR1 6429729Smckusick # define SIGUSR1 30 6529729Smckusick # endif SIGUSR1 6629729Smckusick 6729729Smckusick char *index(), *rindex(), *malloc(), *getenv(), *strcat(), *strcpy(); 686444Swnj struct passwd *getpwuid(); 699365Ssam char *name; 706444Swnj int rem; 716444Swnj char cmdchar = '~'; 726444Swnj int eight; 7321583Sbloom int litout; 746444Swnj char *speeds[] = 756444Swnj { "0", "50", "75", "110", "134", "150", "200", "300", 766444Swnj "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" }; 7718358Ssam char term[256] = "network"; 789365Ssam extern int errno; 799365Ssam int lostpeer(); 8024726Smckusick int dosigwinch = 0; 8126981Skarels #ifndef sigmask 8226981Skarels #define sigmask(m) (1 << ((m)-1)) 8326981Skarels #endif 8426981Skarels #ifdef sun 8526981Skarels struct winsize { 8626981Skarels unsigned short ws_row, ws_col; 8726981Skarels unsigned short ws_xpixel, ws_ypixel; 8826981Skarels }; 8929729Smckusick #endif sun 9018358Ssam struct winsize winsize; 9124726Smckusick int sigwinch(), oob(); 926444Swnj 9329729Smckusick /* 9429729Smckusick * The following routine provides compatibility (such as it is) 9529729Smckusick * between 4.2BSD Suns and others. Suns have only a `ttysize', 9629729Smckusick * so we convert it to a winsize. 9729729Smckusick */ 9829729Smckusick #ifdef sun 9929729Smckusick int 10029729Smckusick get_window_size(fd, wp) 10129729Smckusick int fd; 10229729Smckusick struct winsize *wp; 10329729Smckusick { 10429729Smckusick struct ttysize ts; 10529729Smckusick int error; 10629729Smckusick 10729729Smckusick if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0) 10829729Smckusick return (error); 10929729Smckusick wp->ws_row = ts.ts_lines; 11029729Smckusick wp->ws_col = ts.ts_cols; 11129729Smckusick wp->ws_xpixel = 0; 11229729Smckusick wp->ws_ypixel = 0; 11329729Smckusick return (0); 11429729Smckusick } 11529729Smckusick #else sun 11629729Smckusick #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp) 11729729Smckusick #endif sun 11829729Smckusick 1196444Swnj main(argc, argv) 1206444Swnj int argc; 1216444Swnj char **argv; 1226444Swnj { 1239365Ssam char *host, *cp; 1246444Swnj struct sgttyb ttyb; 1256444Swnj struct passwd *pwd; 1269365Ssam struct servent *sp; 12724726Smckusick int uid, options = 0, oldmask; 12817449Slepreau int on = 1; 1296444Swnj 1306444Swnj host = rindex(argv[0], '/'); 1316444Swnj if (host) 1326444Swnj host++; 1336444Swnj else 1346444Swnj host = argv[0]; 1356444Swnj argv++, --argc; 1366444Swnj if (!strcmp(host, "rlogin")) 1376444Swnj host = *argv++, --argc; 1386444Swnj another: 13910839Ssam if (argc > 0 && !strcmp(*argv, "-d")) { 1406444Swnj argv++, argc--; 14110415Ssam options |= SO_DEBUG; 1426444Swnj goto another; 1436444Swnj } 14410839Ssam if (argc > 0 && !strcmp(*argv, "-l")) { 1456444Swnj argv++, argc--; 1466444Swnj if (argc == 0) 1476444Swnj goto usage; 1486444Swnj name = *argv++; argc--; 1496444Swnj goto another; 1506444Swnj } 15110839Ssam if (argc > 0 && !strncmp(*argv, "-e", 2)) { 1526444Swnj cmdchar = argv[0][2]; 1536444Swnj argv++, argc--; 1546444Swnj goto another; 1556444Swnj } 15610839Ssam if (argc > 0 && !strcmp(*argv, "-8")) { 1576444Swnj eight = 1; 1586444Swnj argv++, argc--; 1596444Swnj goto another; 1606444Swnj } 16121583Sbloom if (argc > 0 && !strcmp(*argv, "-L")) { 16221583Sbloom litout = 1; 16321583Sbloom argv++, argc--; 16421583Sbloom goto another; 16521583Sbloom } 16636511Skfall 16736511Skfall #ifdef KERBEROS 16836511Skfall if (argc > 0 && !strcmp(*argv, "-x")) { 16936511Skfall encrypt = 1; 17036511Skfall des_set_key(cred.session, schedule); 17136511Skfall argv++, argc--; 17236511Skfall goto another; 17336511Skfall } 17436524Skfall if (argc > 0 && !strcmp(*argv, "-k")) { 17536524Skfall argv++, argc--; 17636524Skfall if(argc <= 0 || (**argv == '-')) { 17736524Skfall fprintf(stderr, "-k option requires an argument\n"); 17836524Skfall exit(1); 17936524Skfall } 18036524Skfall strncpy(krb_realm, *argv, REALM_SZ); 18136524Skfall argv++, argc--; 18236524Skfall goto another; 18336524Skfall } 18436524Skfall 18536511Skfall #endif /* KERBEROS */ 18636511Skfall 1876444Swnj if (host == 0) 1886444Swnj goto usage; 1896444Swnj if (argc > 0) 1906444Swnj goto usage; 1916444Swnj pwd = getpwuid(getuid()); 1926444Swnj if (pwd == 0) { 1936444Swnj fprintf(stderr, "Who are you?\n"); 1946444Swnj exit(1); 1956444Swnj } 19636512Skfall #ifdef KERBEROS 19736512Skfall sp = getservbyname("klogin", "tcp"); 19836512Skfall if(sp == NULL) { 19936512Skfall use_kerberos = 0; 20036512Skfall old_warning("klogin service unknown"); 20136512Skfall sp = getservbyname("login", "tcp"); 20236512Skfall } 20336512Skfall #else 2049365Ssam sp = getservbyname("login", "tcp"); 20536512Skfall #endif 2069365Ssam if (sp == 0) { 2079365Ssam fprintf(stderr, "rlogin: login/tcp: unknown service\n"); 2089365Ssam exit(2); 2099365Ssam } 2109241Ssam cp = getenv("TERM"); 2119241Ssam if (cp) 21229729Smckusick (void) strcpy(term, cp); 21318358Ssam if (ioctl(0, TIOCGETP, &ttyb) == 0) { 21429729Smckusick (void) strcat(term, "/"); 21529729Smckusick (void) strcat(term, speeds[ttyb.sg_ospeed]); 2166444Swnj } 21729729Smckusick (void) get_window_size(0, &winsize); 21829729Smckusick (void) signal(SIGPIPE, lostpeer); 21929729Smckusick /* will use SIGUSR1 for window size hack, so hold it off */ 22029729Smckusick oldmask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1)); 22136511Skfall 22236511Skfall #ifdef KERBEROS 22336512Skfall try_connect: 22436512Skfall if(use_kerberos) { 22536512Skfall rem = KSUCCESS; 22636512Skfall if(krb_realm[0] == '\0') { 22736512Skfall rem = get_krbrlm(krb_realm, 1); 22836512Skfall } 22936512Skfall if(rem == KSUCCESS) { 23036512Skfall if(encrypt) { 23136512Skfall rem = krcmd_mutual( 23236512Skfall &host, sp->s_port, 23336512Skfall name ? name : pwd->pw_name, term, 23436512Skfall 0, krb_realm, 23536512Skfall &cred, schedule 23636512Skfall ); 23736512Skfall } else { 23836512Skfall rem = krcmd( 23936512Skfall &host, sp->s_port, 24036512Skfall name ? name : pwd->pw_name, term, 24136512Skfall 0, krb_realm 24236512Skfall ); 24336512Skfall } 24436511Skfall } else { 24536512Skfall fprintf( 24636512Skfall stderr, 24736512Skfall "rlogin: Kerberos error getting local realm %s\n", 24836512Skfall krb_err_txt[rem] 24936511Skfall ); 25036512Skfall exit(1); 25136511Skfall } 25236512Skfall if((rem < 0) && errno == ECONNREFUSED) { 25336512Skfall use_kerberos = 0; 25436512Skfall old_warning("remote host doesn't support Kerberos"); 25536512Skfall goto try_connect; 25636512Skfall } 25736511Skfall } else { 25836512Skfall if(encrypt) { 25936512Skfall fprintf(stderr, "The -x flag requires Kerberos authencation\n"); 26036512Skfall exit(1); 26136512Skfall } 26236512Skfall rem = rcmd(&host, sp->s_port, pwd->pw_name, 26336512Skfall name ? name : pwd->pw_name, term, 0); 26436511Skfall } 26536512Skfall #else 26636512Skfall rem = rcmd(&host, sp->s_port, pwd->pw_name, 26736512Skfall name ? name : pwd->pw_name, term, 0); 26836512Skfall #endif 26936511Skfall 27036511Skfall if(rem < 0) 27136511Skfall exit(1); 27236511Skfall 27310415Ssam if (options & SO_DEBUG && 27417449Slepreau setsockopt(rem, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)) < 0) 27510415Ssam perror("rlogin: setsockopt (SO_DEBUG)"); 2769365Ssam uid = getuid(); 2779365Ssam if (setuid(uid) < 0) { 2789365Ssam perror("rlogin: setuid"); 2799365Ssam exit(1); 2809365Ssam } 28124726Smckusick doit(oldmask); 2829365Ssam /*NOTREACHED*/ 2836444Swnj usage: 2846444Swnj fprintf(stderr, 28536511Skfall #ifdef KERBEROS 286*36592Skfall "usage: rlogin host [ -ex ] [ -l username ] [ -k realm ] [ -8 ] [ -L ] [ -x ]\n"); 28736511Skfall #else 28825341Smckusick "usage: rlogin host [ -ex ] [ -l username ] [ -8 ] [ -L ]\n"); 28936511Skfall #endif 2906444Swnj exit(1); 2916444Swnj } 2926444Swnj 2936444Swnj #define CRLF "\r\n" 2946444Swnj 2959365Ssam int child; 29611803Sedward int catchild(); 29729729Smckusick int copytochild(), writeroob(); 2989365Ssam 29913075Ssam int defflags, tabflag; 30021583Sbloom int deflflags; 30113075Ssam char deferase, defkill; 30213075Ssam struct tchars deftc; 30313075Ssam struct ltchars defltc; 30413075Ssam struct tchars notc = { -1, -1, -1, -1, -1, -1 }; 30513075Ssam struct ltchars noltc = { -1, -1, -1, -1, -1, -1 }; 3066444Swnj 30724726Smckusick doit(oldmask) 3086444Swnj { 3096444Swnj int exit(); 31013075Ssam struct sgttyb sb; 3116444Swnj 31229729Smckusick (void) ioctl(0, TIOCGETP, (char *)&sb); 31313075Ssam defflags = sb.sg_flags; 31412155Ssam tabflag = defflags & TBDELAY; 3159962Ssam defflags &= ECHO | CRMOD; 31613075Ssam deferase = sb.sg_erase; 31713075Ssam defkill = sb.sg_kill; 31829729Smckusick (void) ioctl(0, TIOCLGET, (char *)&deflflags); 31929729Smckusick (void) ioctl(0, TIOCGETC, (char *)&deftc); 32013075Ssam notc.t_startc = deftc.t_startc; 32113075Ssam notc.t_stopc = deftc.t_stopc; 32229729Smckusick (void) ioctl(0, TIOCGLTC, (char *)&defltc); 32329729Smckusick (void) signal(SIGINT, SIG_IGN); 32429729Smckusick setsignal(SIGHUP, exit); 32529729Smckusick setsignal(SIGQUIT, exit); 3269365Ssam child = fork(); 3279365Ssam if (child == -1) { 3289365Ssam perror("rlogin: fork"); 32925424Skarels done(1); 3309365Ssam } 3319365Ssam if (child == 0) { 33224726Smckusick mode(1); 33329729Smckusick if (reader(oldmask) == 0) { 33425424Skarels prf("Connection closed."); 33525424Skarels exit(0); 33625424Skarels } 33712155Ssam sleep(1); 33812155Ssam prf("\007Connection closed."); 3396444Swnj exit(3); 3406444Swnj } 34129729Smckusick 34229729Smckusick /* 34329729Smckusick * We may still own the socket, and may have a pending SIGURG 34429729Smckusick * (or might receive one soon) that we really want to send to 34529729Smckusick * the reader. Set a trap that simply copies such signals to 34629729Smckusick * the child. 34729729Smckusick */ 34829729Smckusick (void) signal(SIGURG, copytochild); 34929729Smckusick (void) signal(SIGUSR1, writeroob); 35029729Smckusick (void) sigsetmask(oldmask); 35129729Smckusick (void) signal(SIGCHLD, catchild); 3529365Ssam writer(); 35312155Ssam prf("Closed connection."); 35425424Skarels done(0); 3556444Swnj } 3566444Swnj 35729729Smckusick /* 35829729Smckusick * Trap a signal, unless it is being ignored. 35929729Smckusick */ 36029729Smckusick setsignal(sig, act) 36129729Smckusick int sig, (*act)(); 36229729Smckusick { 36329729Smckusick int omask = sigblock(sigmask(sig)); 36429729Smckusick 36529729Smckusick if (signal(sig, act) == SIG_IGN) 36629729Smckusick (void) signal(sig, SIG_IGN); 36729729Smckusick (void) sigsetmask(omask); 36829729Smckusick } 36929729Smckusick 37025424Skarels done(status) 37125424Skarels int status; 3726444Swnj { 37329729Smckusick int w; 3746444Swnj 3756444Swnj mode(0); 37629729Smckusick if (child > 0) { 37729729Smckusick /* make sure catchild does not snap it up */ 37829729Smckusick (void) signal(SIGCHLD, SIG_DFL); 37929729Smckusick if (kill(child, SIGKILL) >= 0) 38029729Smckusick while ((w = wait((union wait *)0)) > 0 && w != child) 38129729Smckusick /*void*/; 38229729Smckusick } 38325424Skarels exit(status); 3846444Swnj } 3856444Swnj 38624726Smckusick /* 38729729Smckusick * Copy SIGURGs to the child process. 38829729Smckusick */ 38929729Smckusick copytochild() 39029729Smckusick { 39129729Smckusick 39229729Smckusick (void) kill(child, SIGURG); 39329729Smckusick } 39429729Smckusick 39529729Smckusick /* 39624726Smckusick * This is called when the reader process gets the out-of-band (urgent) 39724726Smckusick * request to turn on the window-changing protocol. 39824726Smckusick */ 39924726Smckusick writeroob() 40024726Smckusick { 40124726Smckusick 40225341Smckusick if (dosigwinch == 0) { 40324919Smckusick sendwindow(); 40429729Smckusick (void) signal(SIGWINCH, sigwinch); 40525341Smckusick } 40624726Smckusick dosigwinch = 1; 40724726Smckusick } 40824726Smckusick 40911803Sedward catchild() 41011803Sedward { 41111803Sedward union wait status; 41211803Sedward int pid; 41311803Sedward 41411803Sedward again: 41529729Smckusick pid = wait3(&status, WNOHANG|WUNTRACED, (struct rusage *)0); 41611803Sedward if (pid == 0) 41711803Sedward return; 41811803Sedward /* 41911803Sedward * if the child (reader) dies, just quit 42011803Sedward */ 42111803Sedward if (pid < 0 || pid == child && !WIFSTOPPED(status)) 42229729Smckusick done((int)(status.w_termsig | status.w_retcode)); 42311803Sedward goto again; 42411803Sedward } 42511803Sedward 4266444Swnj /* 4279365Ssam * writer: write to remote: 0 -> line. 4289365Ssam * ~. terminate 4299365Ssam * ~^Z suspend rlogin process. 43010415Ssam * ~^Y suspend rlogin process, but leave reader alone. 4316444Swnj */ 4329365Ssam writer() 4336444Swnj { 43423530Sbloom char c; 43511803Sedward register n; 43623530Sbloom register bol = 1; /* beginning of line */ 43723530Sbloom register local = 0; 4386444Swnj 43911803Sedward for (;;) { 44011803Sedward n = read(0, &c, 1); 44118358Ssam if (n <= 0) { 44218358Ssam if (n < 0 && errno == EINTR) 44318358Ssam continue; 44411803Sedward break; 44518358Ssam } 4469365Ssam /* 4479365Ssam * If we're at the beginning of the line 4489365Ssam * and recognize a command character, then 4499365Ssam * we echo locally. Otherwise, characters 4509365Ssam * are echo'd remotely. If the command 4519365Ssam * character is doubled, this acts as a 4529365Ssam * force and local echo is suppressed. 4539365Ssam */ 45423530Sbloom if (bol) { 45523530Sbloom bol = 0; 45623530Sbloom if (c == cmdchar) { 45723530Sbloom bol = 0; 45823530Sbloom local = 1; 45923530Sbloom continue; 4606444Swnj } 46123530Sbloom } else if (local) { 46223530Sbloom local = 0; 46323530Sbloom if (c == '.' || c == deftc.t_eofc) { 46423530Sbloom echo(c); 46523530Sbloom break; 4666444Swnj } 46723530Sbloom if (c == defltc.t_suspc || c == defltc.t_dsuspc) { 46823530Sbloom bol = 1; 46923530Sbloom echo(c); 47023530Sbloom stop(c); 47123530Sbloom continue; 47223530Sbloom } 47336511Skfall if (c != cmdchar) { 47436511Skfall #ifdef KERBEROS 47536511Skfall if(encrypt) { 47636511Skfall (void) des_write( 47736511Skfall rem, 47836511Skfall &cmdchar, 47936511Skfall 1 48036511Skfall ); 48136511Skfall } else 48236511Skfall #endif 48336511Skfall (void) write( 48436511Skfall rem, 48536511Skfall &cmdchar, 48636511Skfall 1 48736511Skfall ); 48836511Skfall } 4896444Swnj } 49036511Skfall 49136511Skfall #ifdef KERBEROS 49236511Skfall if(encrypt) { 49336511Skfall if (des_write(rem, &c, 1) == 0) { 49436511Skfall prf("line gone"); 49536511Skfall break; 49636511Skfall } 49736511Skfall } else 49836511Skfall #endif 49936511Skfall if (write(rem, &c, 1) == 0) { 50036511Skfall prf("line gone"); 50136511Skfall break; 50236511Skfall } 50323530Sbloom bol = c == defkill || c == deftc.t_eofc || 50425424Skarels c == deftc.t_intrc || c == defltc.t_suspc || 50523530Sbloom c == '\r' || c == '\n'; 5066444Swnj } 5076444Swnj } 5086444Swnj 50923530Sbloom echo(c) 51023530Sbloom register char c; 51123530Sbloom { 51223530Sbloom char buf[8]; 51323530Sbloom register char *p = buf; 51423530Sbloom 51523530Sbloom c &= 0177; 51623530Sbloom *p++ = cmdchar; 51723530Sbloom if (c < ' ') { 51823530Sbloom *p++ = '^'; 51923530Sbloom *p++ = c + '@'; 52023530Sbloom } else if (c == 0177) { 52123530Sbloom *p++ = '^'; 52223530Sbloom *p++ = '?'; 52323530Sbloom } else 52423530Sbloom *p++ = c; 52523530Sbloom *p++ = '\r'; 52623530Sbloom *p++ = '\n'; 52729729Smckusick (void) write(1, buf, p - buf); 52823530Sbloom } 52923530Sbloom 53018358Ssam stop(cmdc) 53118358Ssam char cmdc; 53218358Ssam { 53318358Ssam mode(0); 53429729Smckusick (void) signal(SIGCHLD, SIG_IGN); 53529729Smckusick (void) kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP); 53629729Smckusick (void) signal(SIGCHLD, catchild); 53718358Ssam mode(1); 53818358Ssam sigwinch(); /* check for size changes */ 53918358Ssam } 54018358Ssam 54118358Ssam sigwinch() 54218358Ssam { 54318358Ssam struct winsize ws; 54418358Ssam 54529729Smckusick if (dosigwinch && get_window_size(0, &ws) == 0 && 54618358Ssam bcmp(&ws, &winsize, sizeof (ws))) { 54718358Ssam winsize = ws; 54824726Smckusick sendwindow(); 54918358Ssam } 55018358Ssam } 55118358Ssam 55224726Smckusick /* 55324726Smckusick * Send the window size to the server via the magic escape 55424726Smckusick */ 55524726Smckusick sendwindow() 55624726Smckusick { 55724726Smckusick char obuf[4 + sizeof (struct winsize)]; 55824726Smckusick struct winsize *wp = (struct winsize *)(obuf+4); 55924726Smckusick 56024726Smckusick obuf[0] = 0377; 56124726Smckusick obuf[1] = 0377; 56224726Smckusick obuf[2] = 's'; 56324726Smckusick obuf[3] = 's'; 56424726Smckusick wp->ws_row = htons(winsize.ws_row); 56524726Smckusick wp->ws_col = htons(winsize.ws_col); 56624726Smckusick wp->ws_xpixel = htons(winsize.ws_xpixel); 56724726Smckusick wp->ws_ypixel = htons(winsize.ws_ypixel); 56836511Skfall 56936511Skfall #ifdef KERBEROS 57036511Skfall if(encrypt) 57136511Skfall (void) des_write(rem, obuf, sizeof(obuf)); 57236511Skfall else 57336511Skfall #endif 57436511Skfall (void) write(rem, obuf, sizeof(obuf)); 57524726Smckusick } 57624726Smckusick 57725424Skarels /* 57825424Skarels * reader: read from remote: line -> 1 57925424Skarels */ 58025424Skarels #define READING 1 58125424Skarels #define WRITING 2 58225424Skarels 58325424Skarels char rcvbuf[8 * 1024]; 58425424Skarels int rcvcnt; 58525424Skarels int rcvstate; 58626981Skarels int ppid; 58725424Skarels jmp_buf rcvtop; 58825424Skarels 5896444Swnj oob() 5906444Swnj { 59125424Skarels int out = FWRITE, atmark, n; 59225424Skarels int rcvd = 0; 5939365Ssam char waste[BUFSIZ], mark; 59424726Smckusick struct sgttyb sb; 5956444Swnj 59625424Skarels while (recv(rem, &mark, 1, MSG_OOB) < 0) 59725424Skarels switch (errno) { 59825424Skarels 59925424Skarels case EWOULDBLOCK: 60025424Skarels /* 60125424Skarels * Urgent data not here yet. 60225424Skarels * It may not be possible to send it yet 60325424Skarels * if we are blocked for output 60425424Skarels * and our input buffer is full. 60525424Skarels */ 60625424Skarels if (rcvcnt < sizeof(rcvbuf)) { 60725424Skarels n = read(rem, rcvbuf + rcvcnt, 60825424Skarels sizeof(rcvbuf) - rcvcnt); 60925424Skarels if (n <= 0) 61025424Skarels return; 61125424Skarels rcvd += n; 61225424Skarels } else { 61325424Skarels n = read(rem, waste, sizeof(waste)); 61425424Skarels if (n <= 0) 61525424Skarels return; 61625424Skarels } 61725424Skarels continue; 61825424Skarels 61925424Skarels default: 62025424Skarels return; 6216444Swnj } 62225424Skarels if (mark & TIOCPKT_WINDOW) { 62324726Smckusick /* 62424726Smckusick * Let server know about window size changes 62524726Smckusick */ 62629729Smckusick (void) kill(ppid, SIGUSR1); 62724726Smckusick } 62825424Skarels if (!eight && (mark & TIOCPKT_NOSTOP)) { 62929729Smckusick (void) ioctl(0, TIOCGETP, (char *)&sb); 63024726Smckusick sb.sg_flags &= ~CBREAK; 63124726Smckusick sb.sg_flags |= RAW; 63229729Smckusick (void) ioctl(0, TIOCSETN, (char *)&sb); 63313075Ssam notc.t_stopc = -1; 63413075Ssam notc.t_startc = -1; 63529729Smckusick (void) ioctl(0, TIOCSETC, (char *)¬c); 6366444Swnj } 63725424Skarels if (!eight && (mark & TIOCPKT_DOSTOP)) { 63829729Smckusick (void) ioctl(0, TIOCGETP, (char *)&sb); 63924726Smckusick sb.sg_flags &= ~RAW; 64024726Smckusick sb.sg_flags |= CBREAK; 64129729Smckusick (void) ioctl(0, TIOCSETN, (char *)&sb); 64213075Ssam notc.t_stopc = deftc.t_stopc; 64313075Ssam notc.t_startc = deftc.t_startc; 64429729Smckusick (void) ioctl(0, TIOCSETC, (char *)¬c); 6456444Swnj } 64625424Skarels if (mark & TIOCPKT_FLUSHWRITE) { 64729729Smckusick (void) ioctl(1, TIOCFLUSH, (char *)&out); 64825424Skarels for (;;) { 64925424Skarels if (ioctl(rem, SIOCATMARK, &atmark) < 0) { 65025424Skarels perror("ioctl"); 65125424Skarels break; 65225424Skarels } 65325424Skarels if (atmark) 65425424Skarels break; 65525424Skarels n = read(rem, waste, sizeof (waste)); 65625424Skarels if (n <= 0) 65725424Skarels break; 65825424Skarels } 65925424Skarels /* 66025424Skarels * Don't want any pending data to be output, 66125424Skarels * so clear the recv buffer. 66225424Skarels * If we were hanging on a write when interrupted, 66325424Skarels * don't want it to restart. If we were reading, 66425424Skarels * restart anyway. 66525424Skarels */ 66625424Skarels rcvcnt = 0; 66725424Skarels longjmp(rcvtop, 1); 66825424Skarels } 66929729Smckusick 67025424Skarels /* 67129729Smckusick * oob does not do FLUSHREAD (alas!) 67229729Smckusick */ 67329729Smckusick 67429729Smckusick /* 67525424Skarels * If we filled the receive buffer while a read was pending, 67625424Skarels * longjmp to the top to restart appropriately. Don't abort 67725424Skarels * a pending write, however, or we won't know how much was written. 67825424Skarels */ 67925424Skarels if (rcvd && rcvstate == READING) 68025424Skarels longjmp(rcvtop, 1); 6816444Swnj } 6826444Swnj 6839365Ssam /* 6849365Ssam * reader: read from remote: line -> 1 6859365Ssam */ 68629729Smckusick reader(oldmask) 68729729Smckusick int oldmask; 6886444Swnj { 68926981Skarels #if !defined(BSD) || BSD < 43 69026981Skarels int pid = -getpid(); 69126981Skarels #else 69225424Skarels int pid = getpid(); 69326981Skarels #endif 69425424Skarels int n, remaining; 69525424Skarels char *bufp = rcvbuf; 6966444Swnj 69729729Smckusick (void) signal(SIGTTOU, SIG_IGN); 69829729Smckusick (void) signal(SIGURG, oob); 69926981Skarels ppid = getppid(); 70029729Smckusick (void) fcntl(rem, F_SETOWN, pid); 70125424Skarels (void) setjmp(rcvtop); 70229729Smckusick (void) sigsetmask(oldmask); 7036444Swnj for (;;) { 70425424Skarels while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) { 70525424Skarels rcvstate = WRITING; 70625424Skarels n = write(1, bufp, remaining); 70725424Skarels if (n < 0) { 70825424Skarels if (errno != EINTR) 70926189Slepreau return (-1); 71025424Skarels continue; 71125424Skarels } 71225424Skarels bufp += n; 71325424Skarels } 71425424Skarels bufp = rcvbuf; 71525424Skarels rcvcnt = 0; 71625424Skarels rcvstate = READING; 71736511Skfall 71836511Skfall #ifdef KERBEROS 71936511Skfall if(encrypt) 72036511Skfall rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf)); 72136511Skfall else 72236511Skfall #endif 72336511Skfall rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf)); 72425424Skarels if (rcvcnt == 0) 72525424Skarels return (0); 72625424Skarels if (rcvcnt < 0) { 7279365Ssam if (errno == EINTR) 7286444Swnj continue; 72926981Skarels perror("read"); 73025424Skarels return (-1); 7316444Swnj } 7326444Swnj } 7336444Swnj } 7346444Swnj 7356444Swnj mode(f) 7366444Swnj { 73713075Ssam struct tchars *tc; 73813075Ssam struct ltchars *ltc; 73913075Ssam struct sgttyb sb; 74021583Sbloom int lflags; 7419365Ssam 74229729Smckusick (void) ioctl(0, TIOCGETP, (char *)&sb); 74329729Smckusick (void) ioctl(0, TIOCLGET, (char *)&lflags); 7449962Ssam switch (f) { 7459962Ssam 7469962Ssam case 0: 74713075Ssam sb.sg_flags &= ~(CBREAK|RAW|TBDELAY); 74813075Ssam sb.sg_flags |= defflags|tabflag; 7499962Ssam tc = &deftc; 75013075Ssam ltc = &defltc; 75113075Ssam sb.sg_kill = defkill; 75213075Ssam sb.sg_erase = deferase; 75321583Sbloom lflags = deflflags; 7549962Ssam break; 7559962Ssam 7569962Ssam case 1: 75713075Ssam sb.sg_flags |= (eight ? RAW : CBREAK); 75813075Ssam sb.sg_flags &= ~defflags; 75912155Ssam /* preserve tab delays, but turn off XTABS */ 76013075Ssam if ((sb.sg_flags & TBDELAY) == XTABS) 76113075Ssam sb.sg_flags &= ~TBDELAY; 7629962Ssam tc = ¬c; 76313075Ssam ltc = &noltc; 76413075Ssam sb.sg_kill = sb.sg_erase = -1; 76521583Sbloom if (litout) 76621583Sbloom lflags |= LLITOUT; 7679962Ssam break; 7689962Ssam 7699962Ssam default: 7709962Ssam return; 7716444Swnj } 77229729Smckusick (void) ioctl(0, TIOCSLTC, (char *)ltc); 77329729Smckusick (void) ioctl(0, TIOCSETC, (char *)tc); 77429729Smckusick (void) ioctl(0, TIOCSETN, (char *)&sb); 77529729Smckusick (void) ioctl(0, TIOCLSET, (char *)&lflags); 7766444Swnj } 7776444Swnj 7789365Ssam /*VARARGS*/ 77924726Smckusick prf(f, a1, a2, a3, a4, a5) 7809365Ssam char *f; 7816444Swnj { 78229729Smckusick 78324726Smckusick fprintf(stderr, f, a1, a2, a3, a4, a5); 7846444Swnj fprintf(stderr, CRLF); 7856444Swnj } 7866444Swnj 7879365Ssam lostpeer() 7886444Swnj { 78929729Smckusick 79029729Smckusick (void) signal(SIGPIPE, SIG_IGN); 79112155Ssam prf("\007Connection closed."); 79225424Skarels done(1); 7936444Swnj } 79436512Skfall 79536512Skfall old_warning(str) 79636512Skfall char *str; 79736512Skfall { 79836512Skfall prf("Warning: %s, using standard rlogin", str); 79936512Skfall } 800