[BACK]Return to rlogin.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rlogin

Annotation of src/usr.bin/rlogin/rlogin.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: rlogin.c,v 1.6 1996/06/26 05:38:33 deraadt Exp $      */
1.1       deraadt     2: /*     $NetBSD: rlogin.c,v 1.8 1995/10/05 09:07:22 mycroft Exp $       */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1983, 1990, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)rlogin.c   8.1 (Berkeley) 6/6/93";
                     46: #else
1.7     ! deraadt    47: static char rcsid[] = "$OpenBSD: rlogin.c,v 1.6 1996/06/26 05:38:33 deraadt Exp $";
1.1       deraadt    48: #endif
                     49: #endif /* not lint */
                     50:
                     51: /*
                     52:  * rlogin - remote login
                     53:  */
                     54: #include <sys/param.h>
                     55: #include <sys/ioctl.h>
                     56: #include <sys/socket.h>
                     57: #include <sys/time.h>
                     58: #include <sys/resource.h>
                     59: #include <sys/wait.h>
                     60:
                     61: #include <netinet/in.h>
                     62: #include <netinet/in_systm.h>
                     63: #include <netinet/ip.h>
                     64:
                     65: #include <errno.h>
                     66: #include <fcntl.h>
                     67: #include <netdb.h>
                     68: #include <pwd.h>
                     69: #include <setjmp.h>
                     70: #include <termios.h>
                     71: #include <signal.h>
                     72: #include <stdio.h>
                     73: #include <stdlib.h>
                     74: #include <string.h>
                     75: #include <unistd.h>
                     76:
                     77: #ifdef __STDC__
                     78: #include <stdarg.h>
                     79: #else
                     80: #include <varargs.h>
                     81: #endif
                     82:
                     83: #ifdef KERBEROS
                     84: #include <kerberosIV/des.h>
                     85: #include <kerberosIV/krb.h>
                     86:
                     87: CREDENTIALS cred;
                     88: Key_schedule schedule;
                     89: int use_kerberos = 1, doencrypt;
                     90: char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
                     91: #endif
                     92:
                     93: #ifndef TIOCPKT_WINDOW
                     94: #define        TIOCPKT_WINDOW  0x80
                     95: #endif
                     96:
                     97: /* concession to Sun */
                     98: #ifndef SIGUSR1
                     99: #define        SIGUSR1 30
                    100: #endif
                    101:
                    102: #ifndef CCEQ
                    103: #define CCEQ(val, c)   (c == val ? val != _POSIX_VDISABLE : 0)
                    104: #endif
                    105:
                    106: int eight, rem;
                    107: struct termios deftty;
                    108:
                    109: int noescape;
                    110: u_char escapechar = '~';
                    111:
                    112: #ifdef OLDSUN
                    113: struct winsize {
                    114:        unsigned short ws_row, ws_col;
                    115:        unsigned short ws_xpixel, ws_ypixel;
                    116: };
                    117: #else
                    118: #define        get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
                    119: #endif
                    120: struct winsize winsize;
                    121:
                    122: void           catch_child __P((int));
                    123: void           copytochild __P((int));
                    124: __dead void    doit __P((long));
                    125: __dead void    done __P((int));
                    126: void           echo __P((char));
                    127: u_int          getescape __P((char *));
                    128: void           lostpeer __P((int));
                    129: void           mode __P((int));
                    130: void           msg __P((char *));
                    131: void           oob __P((int));
                    132: int            reader __P((int));
                    133: void           sendwindow __P((void));
                    134: void           setsignal __P((int));
                    135: void           sigwinch __P((int));
                    136: void           stop __P((int));
                    137: __dead void    usage __P((void));
                    138: void           writer __P((void));
                    139: void           writeroob __P((int));
                    140:
                    141: #ifdef KERBEROS
                    142: void           warning __P((const char *, ...));
                    143: #endif
                    144: #ifdef OLDSUN
                    145: int            get_window_size __P((int, struct winsize *));
                    146: #endif
                    147:
                    148: int
                    149: main(argc, argv)
                    150:        int argc;
                    151:        char *argv[];
                    152: {
                    153:        extern char *optarg;
                    154:        extern int optind;
                    155:        struct passwd *pw;
                    156:        struct servent *sp;
                    157:        struct termios tty;
                    158:        long omask;
                    159:        int argoff, ch, dflag, one, uid;
1.5       deraadt   160:        char *host, *p, *user, term[64];
1.1       deraadt   161:
                    162:        argoff = dflag = 0;
                    163:        one = 1;
                    164:        host = user = NULL;
                    165:
                    166:        if (p = rindex(argv[0], '/'))
                    167:                ++p;
                    168:        else
                    169:                p = argv[0];
                    170:
                    171:        if (strcmp(p, "rlogin"))
                    172:                host = p;
                    173:
                    174:        /* handle "rlogin host flags" */
                    175:        if (!host && argc > 2 && argv[1][0] != '-') {
                    176:                host = argv[1];
                    177:                argoff = 1;
                    178:        }
                    179:
                    180: #ifdef KERBEROS
                    181: #define        OPTIONS "8EKLde:k:l:x"
                    182: #else
                    183: #define        OPTIONS "8EKLde:l:"
                    184: #endif
                    185:        while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != EOF)
                    186:                switch(ch) {
                    187:                case '8':
                    188:                        eight = 1;
                    189:                        break;
                    190:                case 'E':
                    191:                        noescape = 1;
                    192:                        break;
                    193:                case 'K':
                    194: #ifdef KERBEROS
                    195:                        use_kerberos = 0;
                    196: #endif
                    197:                        break;
                    198:                case 'd':
                    199:                        dflag = 1;
                    200:                        break;
                    201:                case 'e':
                    202:                        noescape = 0;
                    203:                        escapechar = getescape(optarg);
                    204:                        break;
                    205: #ifdef KERBEROS
                    206:                case 'k':
                    207:                        dest_realm = dst_realm_buf;
                    208:                        (void)strncpy(dest_realm, optarg, REALM_SZ);
                    209:                        break;
                    210: #endif
                    211:                case 'l':
                    212:                        user = optarg;
                    213:                        break;
                    214: #ifdef KERBEROS
                    215:                case 'x':
                    216:                        doencrypt = 1;
1.3       tholo     217:                        desrw_set_key(&cred.session, schedule);
1.1       deraadt   218:                        break;
                    219: #endif
                    220:                case '?':
                    221:                default:
                    222:                        usage();
                    223:                }
                    224:        optind += argoff;
                    225:        argc -= optind;
                    226:        argv += optind;
                    227:
                    228:        /* if haven't gotten a host yet, do so */
                    229:        if (!host && !(host = *argv++))
                    230:                usage();
                    231:
                    232:        if (*argv)
                    233:                usage();
                    234:
                    235:        if (!(pw = getpwuid(uid = getuid()))) {
                    236:                (void)fprintf(stderr, "rlogin: unknown user id.\n");
                    237:                exit(1);
                    238:        }
                    239:        if (!user)
                    240:                user = pw->pw_name;
                    241:
                    242:        sp = NULL;
                    243: #ifdef KERBEROS
                    244:        if (use_kerberos) {
                    245:                sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
                    246:                if (sp == NULL) {
                    247:                        use_kerberos = 0;
                    248:                        warning("can't get entry for %s/tcp service",
                    249:                            doencrypt ? "eklogin" : "klogin");
                    250:                }
                    251:        }
                    252: #endif
                    253:        if (sp == NULL)
                    254:                sp = getservbyname("login", "tcp");
                    255:        if (sp == NULL) {
                    256:                (void)fprintf(stderr, "rlogin: login/tcp: unknown service.\n");
                    257:                exit(1);
                    258:        }
                    259:
1.4       deraadt   260:        (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
                    261:            sizeof(term) - 1);
                    262:        term[sizeof(term) - 1] = '\0';
                    263:
                    264:        /*
                    265:         * Add "/baud" only if there is room left; ie. do not send "/19"
                    266:         * for 19200 baud with a particularily long $TERM
                    267:         */
1.1       deraadt   268:        if (tcgetattr(0, &tty) == 0) {
1.4       deraadt   269:                char baud[20];          /* more than enough.. */
                    270:
                    271:                (void)sprintf(baud, "/%d", cfgetospeed(&tty));
                    272:                if (strlen(term) + strlen(baud) < sizeof(term) - 1)
                    273:                        (void)strcat(term, baud);
1.1       deraadt   274:        }
                    275:
                    276:        (void)get_window_size(0, &winsize);
                    277:
                    278:        (void)signal(SIGPIPE, lostpeer);
                    279:        /* will use SIGUSR1 for window size hack, so hold it off */
                    280:        omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
                    281:        /*
                    282:         * We set SIGURG and SIGUSR1 below so that an
                    283:         * incoming signal will be held pending rather than being
                    284:         * discarded. Note that these routines will be ready to get
                    285:         * a signal by the time that they are unblocked below.
                    286:         */
                    287:        (void)signal(SIGURG, copytochild);
                    288:        (void)signal(SIGUSR1, writeroob);
                    289:
                    290: #ifdef KERBEROS
                    291: try_connect:
                    292:        if (use_kerberos) {
                    293:                struct hostent *hp;
                    294:
                    295:                /* Fully qualify hostname (needed for krb_realmofhost). */
                    296:                hp = gethostbyname(host);
                    297:                if (hp != NULL && !(host = strdup(hp->h_name))) {
                    298:                        (void)fprintf(stderr, "rlogin: %s\n",
                    299:                            strerror(ENOMEM));
                    300:                        exit(1);
                    301:                }
                    302:
                    303:                rem = KSUCCESS;
                    304:                errno = 0;
                    305:                if (dest_realm == NULL)
                    306:                        dest_realm = krb_realmofhost(host);
                    307:
                    308:                if (doencrypt)
                    309:                        rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
                    310:                            dest_realm, &cred, schedule);
                    311:                else
                    312:                        rem = krcmd(&host, sp->s_port, user, term, 0,
                    313:                            dest_realm);
                    314:                if (rem < 0) {
                    315:                        use_kerberos = 0;
                    316:                        sp = getservbyname("login", "tcp");
                    317:                        if (sp == NULL) {
                    318:                                (void)fprintf(stderr,
                    319:                                    "rlogin: unknown service login/tcp.\n");
                    320:                                exit(1);
                    321:                        }
                    322:                        if (errno == ECONNREFUSED)
                    323:                                warning("remote host doesn't support Kerberos");
                    324:                        if (errno == ENOENT)
                    325:                                warning("can't provide Kerberos auth data");
                    326:                        goto try_connect;
                    327:                }
                    328:        } else {
                    329:                if (doencrypt) {
                    330:                        (void)fprintf(stderr,
                    331:                            "rlogin: the -x flag requires Kerberos authentication.\n");
                    332:                        exit(1);
                    333:                }
                    334:                rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
                    335:        }
                    336: #else
                    337:        rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
                    338: #endif /* KERBEROS */
                    339:
                    340:        if (rem < 0)
                    341:                exit(1);
                    342:
                    343:        if (dflag &&
                    344:            setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
                    345:                (void)fprintf(stderr, "rlogin: setsockopt: %s.\n",
                    346:                    strerror(errno));
                    347:        one = IPTOS_LOWDELAY;
                    348:        if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
                    349:                perror("rlogin: setsockopt TOS (ignored)");
                    350:
                    351:        (void)setuid(uid);
                    352:        doit(omask);
                    353:        /*NOTREACHED*/
                    354: }
                    355:
                    356: int child;
                    357:
                    358: void
                    359: doit(omask)
                    360:        long omask;
                    361: {
                    362:
                    363:        (void)signal(SIGINT, SIG_IGN);
                    364:        setsignal(SIGHUP);
                    365:        setsignal(SIGQUIT);
                    366:        mode(1);
                    367:        child = fork();
                    368:        if (child == -1) {
                    369:                (void)fprintf(stderr, "rlogin: fork: %s.\n", strerror(errno));
                    370:                done(1);
                    371:        }
                    372:        if (child == 0) {
                    373:                if (reader(omask) == 0) {
                    374:                        msg("connection closed.");
                    375:                        exit(0);
                    376:                }
                    377:                sleep(1);
                    378:                msg("\aconnection closed.");
                    379:                exit(1);
                    380:        }
                    381:
                    382:        /*
                    383:         * We may still own the socket, and may have a pending SIGURG (or might
                    384:         * receive one soon) that we really want to send to the reader.  When
                    385:         * one of these comes in, the trap copytochild simply copies such
                    386:         * signals to the child. We can now unblock SIGURG and SIGUSR1
                    387:         * that were set above.
                    388:         */
                    389:        (void)sigsetmask(omask);
                    390:        (void)signal(SIGCHLD, catch_child);
                    391:        writer();
                    392:        msg("closed connection.");
                    393:        done(0);
                    394: }
                    395:
                    396: /* trap a signal, unless it is being ignored. */
                    397: void
                    398: setsignal(sig)
                    399:        int sig;
                    400: {
                    401:        int omask = sigblock(sigmask(sig));
                    402:
                    403:        if (signal(sig, exit) == SIG_IGN)
                    404:                (void)signal(sig, SIG_IGN);
                    405:        (void)sigsetmask(omask);
                    406: }
                    407:
                    408: __dead void
                    409: done(status)
                    410:        int status;
                    411: {
                    412:        int w, wstatus;
                    413:
                    414:        mode(0);
                    415:        if (child > 0) {
                    416:                /* make sure catch_child does not snap it up */
                    417:                (void)signal(SIGCHLD, SIG_DFL);
                    418:                if (kill(child, SIGKILL) >= 0)
                    419:                        while ((w = wait(&wstatus)) > 0 && w != child);
                    420:        }
                    421:        exit(status);
                    422: }
                    423:
                    424: int dosigwinch;
                    425:
                    426: /*
                    427:  * This is called when the reader process gets the out-of-band (urgent)
                    428:  * request to turn on the window-changing protocol.
                    429:  */
                    430: void
                    431: writeroob(signo)
                    432:        int signo;
                    433: {
                    434:        if (dosigwinch == 0) {
                    435:                sendwindow();
                    436:                (void)signal(SIGWINCH, sigwinch);
                    437:        }
                    438:        dosigwinch = 1;
                    439: }
                    440:
                    441: void
                    442: catch_child(signo)
                    443:        int signo;
                    444: {
                    445:        union wait status;
                    446:        int pid;
                    447:
                    448:        for (;;) {
                    449:                pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
                    450:                if (pid == 0)
                    451:                        return;
                    452:                /* if the child (reader) dies, just quit */
                    453:                if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
                    454:                        done((int)(status.w_termsig | status.w_retcode));
                    455:        }
                    456:        /* NOTREACHED */
                    457: }
                    458:
                    459: /*
                    460:  * writer: write to remote: 0 -> line.
                    461:  * ~.                          terminate
                    462:  * ~^Z                         suspend rlogin process.
                    463:  * ~<delayed-suspend char>     suspend rlogin process, but leave reader alone.
                    464:  */
                    465: void
                    466: writer()
                    467: {
                    468:        register int bol, local, n;
                    469:        char c;
                    470:
                    471:        bol = 1;                        /* beginning of line */
                    472:        local = 0;
                    473:        for (;;) {
                    474:                n = read(STDIN_FILENO, &c, 1);
                    475:                if (n <= 0) {
                    476:                        if (n < 0 && errno == EINTR)
                    477:                                continue;
                    478:                        break;
                    479:                }
                    480:                /*
                    481:                 * If we're at the beginning of the line and recognize a
                    482:                 * command character, then we echo locally.  Otherwise,
                    483:                 * characters are echo'd remotely.  If the command character
                    484:                 * is doubled, this acts as a force and local echo is
                    485:                 * suppressed.
                    486:                 */
                    487:                if (bol) {
                    488:                        bol = 0;
                    489:                        if (!noescape && c == escapechar) {
                    490:                                local = 1;
                    491:                                continue;
                    492:                        }
                    493:                } else if (local) {
                    494:                        local = 0;
                    495:                        if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
                    496:                                echo(c);
                    497:                                break;
                    498:                        }
                    499:                        if (CCEQ(deftty.c_cc[VSUSP], c)) {
                    500:                                bol = 1;
                    501:                                echo(c);
                    502:                                stop(1);
                    503:                                continue;
                    504:                        }
                    505:                        if (CCEQ(deftty.c_cc[VDSUSP], c)) {
                    506:                                bol = 1;
                    507:                                echo(c);
                    508:                                stop(0);
                    509:                                continue;
                    510:                        }
                    511:                        if (c != escapechar)
                    512: #ifdef KERBEROS
                    513:                                if (doencrypt)
                    514:                                        (void)des_write(rem,
                    515:                                            (char *)&escapechar, 1);
                    516:                                else
                    517: #endif
                    518:                                        (void)write(rem, &escapechar, 1);
                    519:                }
                    520:
                    521: #ifdef KERBEROS
                    522:                if (doencrypt) {
                    523:                        if (des_write(rem, &c, 1) == 0) {
                    524:                                msg("line gone");
                    525:                                break;
                    526:                        }
                    527:                } else
                    528: #endif
                    529:                        if (write(rem, &c, 1) == 0) {
                    530:                                msg("line gone");
                    531:                                break;
                    532:                        }
                    533:                bol = CCEQ(deftty.c_cc[VKILL], c) ||
                    534:                    CCEQ(deftty.c_cc[VEOF], c) ||
                    535:                    CCEQ(deftty.c_cc[VINTR], c) ||
                    536:                    CCEQ(deftty.c_cc[VSUSP], c) ||
                    537:                    c == '\r' || c == '\n';
                    538:        }
                    539: }
                    540:
                    541: void
                    542: #if __STDC__
                    543: echo(register char c)
                    544: #else
                    545: echo(c)
                    546:        register char c;
                    547: #endif
                    548: {
                    549:        register char *p;
                    550:        char buf[8];
                    551:
                    552:        p = buf;
                    553:        c &= 0177;
                    554:        *p++ = escapechar;
                    555:        if (c < ' ') {
                    556:                *p++ = '^';
                    557:                *p++ = c + '@';
                    558:        } else if (c == 0177) {
                    559:                *p++ = '^';
                    560:                *p++ = '?';
                    561:        } else
                    562:                *p++ = c;
                    563:        *p++ = '\r';
                    564:        *p++ = '\n';
                    565:        (void)write(STDOUT_FILENO, buf, p - buf);
                    566: }
                    567:
                    568: void
                    569: stop(all)
                    570:        int all;
                    571: {
                    572:        mode(0);
                    573:        (void)signal(SIGCHLD, SIG_IGN);
                    574:        (void)kill(all ? 0 : getpid(), SIGTSTP);
                    575:        (void)signal(SIGCHLD, catch_child);
                    576:        mode(1);
                    577:        sigwinch(0);                    /* check for size changes */
                    578: }
                    579:
                    580: void
                    581: sigwinch(signo)
                    582:        int signo;
                    583: {
                    584:        struct winsize ws;
                    585:
                    586:        if (dosigwinch && get_window_size(0, &ws) == 0 &&
                    587:            bcmp(&ws, &winsize, sizeof(ws))) {
                    588:                winsize = ws;
                    589:                sendwindow();
                    590:        }
                    591: }
                    592:
                    593: /*
                    594:  * Send the window size to the server via the magic escape
                    595:  */
                    596: void
                    597: sendwindow()
                    598: {
                    599:        struct winsize *wp;
                    600:        char obuf[4 + sizeof (struct winsize)];
                    601:
                    602:        wp = (struct winsize *)(obuf+4);
                    603:        obuf[0] = 0377;
                    604:        obuf[1] = 0377;
                    605:        obuf[2] = 's';
                    606:        obuf[3] = 's';
                    607:        wp->ws_row = htons(winsize.ws_row);
                    608:        wp->ws_col = htons(winsize.ws_col);
                    609:        wp->ws_xpixel = htons(winsize.ws_xpixel);
                    610:        wp->ws_ypixel = htons(winsize.ws_ypixel);
                    611:
                    612: #ifdef KERBEROS
                    613:        if(doencrypt)
                    614:                (void)des_write(rem, obuf, sizeof(obuf));
                    615:        else
                    616: #endif
                    617:                (void)write(rem, obuf, sizeof(obuf));
                    618: }
                    619:
                    620: /*
                    621:  * reader: read from remote: line -> 1
                    622:  */
                    623: #define        READING 1
                    624: #define        WRITING 2
                    625:
                    626: jmp_buf rcvtop;
                    627: int ppid, rcvcnt, rcvstate;
                    628: char rcvbuf[8 * 1024];
                    629:
                    630: void
                    631: oob(signo)
                    632:        int signo;
                    633: {
                    634:        struct termios tty;
                    635:        int atmark, n, rcvd;
                    636:        char waste[BUFSIZ], mark;
                    637:
                    638:        rcvd = 0;
                    639:        while (recv(rem, &mark, 1, MSG_OOB) < 0) {
                    640:                switch (errno) {
                    641:                case EWOULDBLOCK:
                    642:                        /*
                    643:                         * Urgent data not here yet.  It may not be possible
                    644:                         * to send it yet if we are blocked for output and
                    645:                         * our input buffer is full.
                    646:                         */
                    647:                        if (rcvcnt < sizeof(rcvbuf)) {
                    648:                                n = read(rem, rcvbuf + rcvcnt,
                    649:                                    sizeof(rcvbuf) - rcvcnt);
                    650:                                if (n <= 0)
                    651:                                        return;
                    652:                                rcvd += n;
                    653:                        } else {
                    654:                                n = read(rem, waste, sizeof(waste));
                    655:                                if (n <= 0)
                    656:                                        return;
                    657:                        }
                    658:                        continue;
                    659:                default:
                    660:                        return;
                    661:                }
                    662:        }
                    663:        if (mark & TIOCPKT_WINDOW) {
                    664:                /* Let server know about window size changes */
                    665:                (void)kill(ppid, SIGUSR1);
                    666:        }
                    667:        if (!eight && (mark & TIOCPKT_NOSTOP)) {
                    668:                (void)tcgetattr(0, &tty);
                    669:                tty.c_iflag &= ~IXON;
                    670:                (void)tcsetattr(0, TCSANOW, &tty);
                    671:        }
                    672:        if (!eight && (mark & TIOCPKT_DOSTOP)) {
                    673:                (void)tcgetattr(0, &tty);
                    674:                tty.c_iflag |= (deftty.c_iflag & IXON);
                    675:                (void)tcsetattr(0, TCSANOW, &tty);
                    676:        }
                    677:        if (mark & TIOCPKT_FLUSHWRITE) {
                    678:                (void)tcflush(1, TCIOFLUSH);
                    679:                for (;;) {
                    680:                        if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
                    681:                                (void)fprintf(stderr, "rlogin: ioctl: %s.\n",
                    682:                                    strerror(errno));
                    683:                                break;
                    684:                        }
                    685:                        if (atmark)
                    686:                                break;
                    687:                        n = read(rem, waste, sizeof (waste));
                    688:                        if (n <= 0)
                    689:                                break;
                    690:                }
                    691:                /*
                    692:                 * Don't want any pending data to be output, so clear the recv
                    693:                 * buffer.  If we were hanging on a write when interrupted,
                    694:                 * don't want it to restart.  If we were reading, restart
                    695:                 * anyway.
                    696:                 */
                    697:                rcvcnt = 0;
                    698:                longjmp(rcvtop, 1);
                    699:        }
                    700:
                    701:        /* oob does not do FLUSHREAD (alas!) */
                    702:
                    703:        /*
                    704:         * If we filled the receive buffer while a read was pending, longjmp
                    705:         * to the top to restart appropriately.  Don't abort a pending write,
                    706:         * however, or we won't know how much was written.
                    707:         */
                    708:        if (rcvd && rcvstate == READING)
                    709:                longjmp(rcvtop, 1);
                    710: }
                    711:
                    712: /* reader: read from remote: line -> 1 */
                    713: int
                    714: reader(omask)
                    715:        int omask;
                    716: {
                    717:        int pid, n, remaining;
                    718:        char *bufp;
                    719:
                    720: #if BSD >= 43 || defined(SUNOS4)
                    721:        pid = getpid();         /* modern systems use positives for pid */
                    722: #else
                    723:        pid = -getpid();        /* old broken systems use negatives */
                    724: #endif
                    725:        (void)signal(SIGTTOU, SIG_IGN);
                    726:        (void)signal(SIGURG, oob);
                    727:        ppid = getppid();
                    728:        (void)fcntl(rem, F_SETOWN, pid);
                    729:        (void)setjmp(rcvtop);
                    730:        (void)sigsetmask(omask);
                    731:        bufp = rcvbuf;
                    732:        for (;;) {
                    733:                while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
                    734:                        rcvstate = WRITING;
                    735:                        n = write(STDOUT_FILENO, bufp, remaining);
                    736:                        if (n < 0) {
                    737:                                if (errno != EINTR)
                    738:                                        return (-1);
                    739:                                continue;
                    740:                        }
                    741:                        bufp += n;
                    742:                }
                    743:                bufp = rcvbuf;
                    744:                rcvcnt = 0;
                    745:                rcvstate = READING;
                    746:
                    747: #ifdef KERBEROS
                    748:                if (doencrypt)
                    749:                        rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
                    750:                else
                    751: #endif
                    752:                        rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
                    753:                if (rcvcnt == 0)
                    754:                        return (0);
                    755:                if (rcvcnt < 0) {
                    756:                        if (errno == EINTR)
                    757:                                continue;
                    758:                        (void)fprintf(stderr, "rlogin: read: %s.\n",
                    759:                            strerror(errno));
                    760:                        return (-1);
                    761:                }
                    762:        }
                    763: }
                    764:
                    765: void
                    766: mode(f)
                    767:        int f;
                    768: {
                    769:        struct termios tty;
                    770:
                    771:        switch (f) {
                    772:        case 0:
                    773:                (void)tcsetattr(0, TCSANOW, &deftty);
                    774:                break;
                    775:        case 1:
                    776:                (void)tcgetattr(0, &deftty);
                    777:                tty = deftty;
                    778:                /* This is loosely derived from sys/compat/tty_compat.c. */
                    779:                tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
                    780:                tty.c_iflag &= ~ICRNL;
                    781:                tty.c_oflag &= ~OPOST;
1.7     ! deraadt   782:                tty.c_cc[VMIN] = 1;
        !           783:                tty.c_cc[VTIME] = 0;
1.1       deraadt   784:                if (eight) {
                    785:                        tty.c_iflag &= IXOFF;
                    786:                        tty.c_cflag &= ~(CSIZE|PARENB);
                    787:                        tty.c_cflag |= CS8;
                    788:                }
                    789:                (void)tcsetattr(0, TCSANOW, &tty);
                    790:                break;
                    791:        default:
                    792:                return;
                    793:        }
                    794: }
                    795:
                    796: void
                    797: lostpeer(signo)
                    798:        int signo;
                    799: {
                    800:        (void)signal(SIGPIPE, SIG_IGN);
                    801:        msg("\aconnection closed.");
                    802:        done(1);
                    803: }
                    804:
                    805: /* copy SIGURGs to the child process. */
                    806: void
                    807: copytochild(signo)
                    808:        int signo;
                    809: {
                    810:        (void)kill(child, SIGURG);
                    811: }
                    812:
                    813: void
                    814: msg(str)
                    815:        char *str;
                    816: {
                    817:        (void)fprintf(stderr, "rlogin: %s\r\n", str);
                    818: }
                    819:
                    820: #ifdef KERBEROS
                    821: /* VARARGS */
                    822: void
                    823: #if __STDC__
                    824: warning(const char *fmt, ...)
                    825: #else
                    826: warning(fmt, va_alist)
                    827:        char *fmt;
                    828:        va_dcl
                    829: #endif
                    830: {
                    831:        va_list ap;
                    832:
                    833:        (void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
                    834: #ifdef __STDC__
                    835:        va_start(ap, fmt);
                    836: #else
                    837:        va_start(ap);
                    838: #endif
                    839:        vfprintf(stderr, fmt, ap);
                    840:        va_end(ap);
                    841:        (void)fprintf(stderr, ".\n");
                    842: }
                    843: #endif
                    844:
                    845: __dead void
                    846: usage()
                    847: {
                    848:        (void)fprintf(stderr,
                    849:            "usage: rlogin [ -%s]%s[-e char] [ -l username ] host\n",
                    850: #ifdef KERBEROS
                    851:            "8EKLx", " [-k realm] ");
                    852: #else
                    853:            "8EL", " ");
                    854: #endif
                    855:        exit(1);
                    856: }
                    857:
                    858: /*
                    859:  * The following routine provides compatibility (such as it is) between older
                    860:  * Suns and others.  Suns have only a `ttysize', so we convert it to a winsize.
                    861:  */
                    862: #ifdef OLDSUN
                    863: int
                    864: get_window_size(fd, wp)
                    865:        int fd;
                    866:        struct winsize *wp;
                    867: {
                    868:        struct ttysize ts;
                    869:        int error;
                    870:
                    871:        if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
                    872:                return (error);
                    873:        wp->ws_row = ts.ts_lines;
                    874:        wp->ws_col = ts.ts_cols;
                    875:        wp->ws_xpixel = 0;
                    876:        wp->ws_ypixel = 0;
                    877:        return (0);
                    878: }
                    879: #endif
                    880:
                    881: u_int
                    882: getescape(p)
                    883:        register char *p;
                    884: {
                    885:        long val;
                    886:        int len;
                    887:
                    888:        if ((len = strlen(p)) == 1)     /* use any single char, including '\' */
                    889:                return ((u_int)*p);
                    890:                                        /* otherwise, \nnn */
                    891:        if (*p == '\\' && len >= 2 && len <= 4) {
                    892:                val = strtol(++p, NULL, 8);
                    893:                for (;;) {
                    894:                        if (!*++p)
                    895:                                return ((u_int)val);
                    896:                        if (*p < '0' || *p > '8')
                    897:                                break;
                    898:                }
                    899:        }
                    900:        msg("illegal option value -- e");
                    901:        usage();
                    902:        /* NOTREACHED */
                    903: }