[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.19

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