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

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

1.7     ! tholo       1: /*     $OpenBSD: rsh.c,v 1.6 1996/07/24 17:31:08 deraadt Exp $ */
1.4       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1983, 1990 The Regents of the University of California.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: char copyright[] =
                     38: "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
                     39:  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /*static char sccsid[] = "from: @(#)rsh.c      5.24 (Berkeley) 7/1/91";*/
1.7     ! tholo      44: static char rcsid[] = "$OpenBSD: rsh.c,v 1.6 1996/07/24 17:31:08 deraadt Exp $";
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include <sys/types.h>
                     48: #include <sys/socket.h>
                     49: #include <sys/ioctl.h>
                     50: #include <sys/file.h>
                     51:
                     52: #include <netinet/in.h>
                     53: #include <netdb.h>
                     54:
                     55: #include <pwd.h>
                     56: #include <signal.h>
                     57: #include <stdio.h>
                     58: #include <errno.h>
                     59: #include <string.h>
1.7     ! tholo      60: #if __STDC__
        !            61: #include <stdarg.h>
        !            62: #else
1.1       deraadt    63: #include <varargs.h>
1.7     ! tholo      64: #endif
1.1       deraadt    65: #include "pathnames.h"
                     66:
                     67: #ifdef KERBEROS
                     68: #include <kerberosIV/des.h>
                     69: #include <kerberosIV/krb.h>
                     70:
                     71: CREDENTIALS cred;
                     72: Key_schedule schedule;
                     73: int use_kerberos = 1, doencrypt;
                     74: char dst_realm_buf[REALM_SZ], *dest_realm;
1.7     ! tholo      75:
        !            76: void warning __P((const char *, ...));
1.1       deraadt    77: #endif
                     78:
                     79: /*
                     80:  * rsh - remote shell
                     81:  */
                     82: extern int errno;
                     83: int rfd2;
                     84:
                     85: main(argc, argv)
                     86:        int argc;
                     87:        char **argv;
                     88: {
                     89:        extern char *optarg;
                     90:        extern int optind;
                     91:        struct passwd *pw;
                     92:        struct servent *sp;
                     93:        long omask;
                     94:        int argoff, asrsh, ch, dflag, nflag, one, pid, rem, uid;
                     95:        register char *p;
                     96:        char *args, *host, *user, *copyargs();
                     97:        void sendsig();
                     98:
                     99:        argoff = asrsh = dflag = nflag = 0;
                    100:        one = 1;
                    101:        host = user = NULL;
                    102:
                    103:        /* if called as something other than "rsh", use it as the host name */
                    104:        if (p = rindex(argv[0], '/'))
                    105:                ++p;
                    106:        else
                    107:                p = argv[0];
                    108:        if (strcmp(p, "rsh"))
                    109:                host = p;
                    110:        else
                    111:                asrsh = 1;
                    112:
                    113:        /* handle "rsh host flags" */
                    114:        if (!host && argc > 2 && argv[1][0] != '-') {
                    115:                host = argv[1];
                    116:                argoff = 1;
                    117:        }
                    118:
                    119: #ifdef KERBEROS
                    120: #define        OPTIONS "8KLdek:l:nwx"
                    121: #else
                    122: #define        OPTIONS "8KLdel:nw"
                    123: #endif
                    124:        while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != EOF)
                    125:                switch(ch) {
                    126:                case 'K':
                    127: #ifdef KERBEROS
                    128:                        use_kerberos = 0;
                    129: #endif
                    130:                        break;
                    131:                case 'L':       /* -8Lew are ignored to allow rlogin aliases */
                    132:                case 'e':
                    133:                case 'w':
                    134:                case '8':
                    135:                        break;
                    136:                case 'd':
                    137:                        dflag = 1;
                    138:                        break;
                    139:                case 'l':
                    140:                        user = optarg;
                    141:                        break;
                    142: #ifdef KERBEROS
                    143:                case 'k':
                    144:                        dest_realm = dst_realm_buf;
                    145:                        strncpy(dest_realm, optarg, REALM_SZ);
                    146:                        break;
                    147: #endif
                    148:                case 'n':
                    149:                        nflag = 1;
                    150:                        break;
                    151: #ifdef KERBEROS
                    152:                case 'x':
                    153:                        doencrypt = 1;
1.3       tholo     154:                        desrw_set_key(&cred.session, schedule);
1.1       deraadt   155:                        break;
                    156: #endif
                    157:                case '?':
                    158:                default:
                    159:                        usage();
                    160:                }
                    161:        optind += argoff;
                    162:
                    163:        /* if haven't gotten a host yet, do so */
                    164:        if (!host && !(host = argv[optind++]))
                    165:                usage();
                    166:
                    167:        /* if no further arguments, must have been called as rlogin. */
                    168:        if (!argv[optind]) {
                    169:                if (asrsh)
                    170:                        *argv = "rlogin";
1.6       deraadt   171:                setuid(getuid());
1.1       deraadt   172:                execv(_PATH_RLOGIN, argv);
                    173:                (void)fprintf(stderr, "rsh: can't exec %s.\n", _PATH_RLOGIN);
                    174:                exit(1);
                    175:        }
                    176:
                    177:        argc -= optind;
                    178:        argv += optind;
                    179:
                    180:        if (!(pw = getpwuid(uid = getuid()))) {
                    181:                (void)fprintf(stderr, "rsh: unknown user id.\n");
                    182:                exit(1);
                    183:        }
                    184:        if (!user)
                    185:                user = pw->pw_name;
                    186:
                    187: #ifdef KERBEROS
                    188:        /* -x turns off -n */
                    189:        if (doencrypt)
                    190:                nflag = 0;
                    191: #endif
                    192:
                    193:        args = copyargs(argv);
                    194:
                    195:        sp = NULL;
                    196: #ifdef KERBEROS
                    197:        if (use_kerberos) {
                    198:                sp = getservbyname((doencrypt ? "ekshell" : "kshell"), "tcp");
                    199:                if (sp == NULL) {
                    200:                        use_kerberos = 0;
                    201:                        warning("can't get entry for %s/tcp service",
                    202:                            doencrypt ? "ekshell" : "kshell");
                    203:                }
                    204:        }
                    205: #endif
                    206:        if (sp == NULL)
                    207:                sp = getservbyname("shell", "tcp");
                    208:        if (sp == NULL) {
                    209:                (void)fprintf(stderr, "rsh: shell/tcp: unknown service.\n");
                    210:                exit(1);
                    211:        }
                    212:
                    213: #ifdef KERBEROS
                    214: try_connect:
                    215:        if (use_kerberos) {
                    216:                rem = KSUCCESS;
                    217:                errno = 0;
                    218:                if (dest_realm == NULL)
                    219:                        dest_realm = krb_realmofhost(host);
                    220:
                    221:                if (doencrypt)
                    222:                        rem = krcmd_mutual(&host, sp->s_port, user, args,
                    223:                            &rfd2, dest_realm, &cred, schedule);
                    224:                else
                    225:                        rem = krcmd(&host, sp->s_port, user, args, &rfd2,
                    226:                            dest_realm);
                    227:                if (rem < 0) {
                    228:                        use_kerberos = 0;
                    229:                        sp = getservbyname("shell", "tcp");
                    230:                        if (sp == NULL) {
                    231:                                (void)fprintf(stderr,
                    232:                                    "rsh: unknown service shell/tcp.\n");
                    233:                                exit(1);
                    234:                        }
                    235:                        if (errno == ECONNREFUSED)
                    236:                                warning("remote host doesn't support Kerberos");
                    237:                        if (errno == ENOENT)
                    238:                                warning("can't provide Kerberos auth data");
                    239:                        goto try_connect;
                    240:                }
                    241:        } else {
                    242:                if (doencrypt) {
                    243:                        (void)fprintf(stderr,
                    244:                            "rsh: the -x flag requires Kerberos authentication.\n");
                    245:                        exit(1);
                    246:                }
                    247:                rem = rcmd(&host, sp->s_port, pw->pw_name, user, args, &rfd2);
                    248:        }
                    249: #else
                    250:        rem = rcmd(&host, sp->s_port, pw->pw_name, user, args, &rfd2);
                    251: #endif
                    252:
                    253:        if (rem < 0)
                    254:                exit(1);
                    255:
                    256:        if (rfd2 < 0) {
                    257:                (void)fprintf(stderr, "rsh: can't establish stderr.\n");
                    258:                exit(1);
                    259:        }
                    260:        if (dflag) {
                    261:                if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
                    262:                    sizeof(one)) < 0)
                    263:                        (void)fprintf(stderr, "rsh: setsockopt: %s.\n",
                    264:                            strerror(errno));
                    265:                if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
                    266:                    sizeof(one)) < 0)
                    267:                        (void)fprintf(stderr, "rsh: setsockopt: %s.\n",
                    268:                            strerror(errno));
                    269:        }
                    270:
                    271:        (void)setuid(uid);
                    272:        omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGTERM));
                    273:        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                    274:                (void)signal(SIGINT, sendsig);
                    275:        if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
                    276:                (void)signal(SIGQUIT, sendsig);
                    277:        if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
                    278:                (void)signal(SIGTERM, sendsig);
                    279:
                    280:        if (!nflag) {
                    281:                pid = fork();
                    282:                if (pid < 0) {
                    283:                        (void)fprintf(stderr,
                    284:                            "rsh: fork: %s.\n", strerror(errno));
                    285:                        exit(1);
                    286:                }
                    287:        }
                    288:
                    289: #ifdef KERBEROS
                    290:        if (!doencrypt)
                    291: #endif
                    292:        {
                    293:                (void)ioctl(rfd2, FIONBIO, &one);
                    294:                (void)ioctl(rem, FIONBIO, &one);
                    295:        }
                    296:
                    297:        talk(nflag, omask, pid, rem);
                    298:
                    299:        if (!nflag)
                    300:                (void)kill(pid, SIGKILL);
                    301:        exit(0);
                    302: }
                    303:
                    304: talk(nflag, omask, pid, rem)
                    305:        int nflag, pid;
                    306:        long omask;
                    307:        register int rem;
                    308: {
                    309:        register int cc, wc;
                    310:        register char *bp;
                    311:        int readfrom, ready, rembits;
                    312:        char buf[BUFSIZ];
                    313:
                    314:        if (!nflag && pid == 0) {
                    315:                (void)close(rfd2);
                    316:
                    317: reread:                errno = 0;
                    318:                if ((cc = read(0, buf, sizeof buf)) <= 0)
                    319:                        goto done;
                    320:                bp = buf;
                    321:
                    322: rewrite:       rembits = 1 << rem;
                    323:                if (select(16, 0, &rembits, 0, 0) < 0) {
                    324:                        if (errno != EINTR) {
                    325:                                (void)fprintf(stderr,
                    326:                                    "rsh: select: %s.\n", strerror(errno));
                    327:                                exit(1);
                    328:                        }
                    329:                        goto rewrite;
                    330:                }
                    331:                if ((rembits & (1 << rem)) == 0)
                    332:                        goto rewrite;
                    333: #ifdef KERBEROS
                    334:                if (doencrypt)
                    335:                        wc = des_write(rem, bp, cc);
                    336:                else
                    337: #endif
                    338:                        wc = write(rem, bp, cc);
                    339:                if (wc < 0) {
                    340:                        if (errno == EWOULDBLOCK)
                    341:                                goto rewrite;
                    342:                        goto done;
                    343:                }
                    344:                bp += wc;
                    345:                cc -= wc;
                    346:                if (cc == 0)
                    347:                        goto reread;
                    348:                goto rewrite;
                    349: done:
                    350:                (void)shutdown(rem, 1);
                    351:                exit(0);
                    352:        }
                    353:
                    354:        (void)sigsetmask(omask);
                    355:        readfrom = (1 << rfd2) | (1 << rem);
                    356:        do {
                    357:                ready = readfrom;
                    358:                if (select(16, &ready, 0, 0, 0) < 0) {
                    359:                        if (errno != EINTR) {
                    360:                                (void)fprintf(stderr,
                    361:                                    "rsh: select: %s.\n", strerror(errno));
                    362:                                exit(1);
                    363:                        }
                    364:                        continue;
                    365:                }
                    366:                if (ready & (1 << rfd2)) {
                    367:                        errno = 0;
                    368: #ifdef KERBEROS
                    369:                        if (doencrypt)
                    370:                                cc = des_read(rfd2, buf, sizeof buf);
                    371:                        else
                    372: #endif
                    373:                                cc = read(rfd2, buf, sizeof buf);
                    374:                        if (cc <= 0) {
                    375:                                if (errno != EWOULDBLOCK)
                    376:                                        readfrom &= ~(1 << rfd2);
                    377:                        } else
                    378:                                (void)write(2, buf, cc);
                    379:                }
                    380:                if (ready & (1 << rem)) {
                    381:                        errno = 0;
                    382: #ifdef KERBEROS
                    383:                        if (doencrypt)
                    384:                                cc = des_read(rem, buf, sizeof buf);
                    385:                        else
                    386: #endif
                    387:                                cc = read(rem, buf, sizeof buf);
                    388:                        if (cc <= 0) {
                    389:                                if (errno != EWOULDBLOCK)
                    390:                                        readfrom &= ~(1 << rem);
                    391:                        } else
                    392:                                (void)write(1, buf, cc);
                    393:                }
                    394:        } while (readfrom);
                    395: }
                    396:
                    397: void
                    398: sendsig(signo)
                    399:        char signo;
                    400: {
                    401: #ifdef KERBEROS
                    402:        if (doencrypt)
                    403:                (void)des_write(rfd2, &signo, 1);
                    404:        else
                    405: #endif
                    406:                (void)write(rfd2, &signo, 1);
                    407: }
                    408:
                    409: #ifdef KERBEROS
                    410: /* VARARGS */
1.7     ! tholo     411: void
        !           412: #if __STDC__
        !           413: warning(const char *fmt, ...)
        !           414: #else
1.1       deraadt   415: warning(va_alist)
                    416: va_dcl
1.7     ! tholo     417: #endif
1.1       deraadt   418: {
                    419:        va_list ap;
1.7     ! tholo     420: #if !__STDC__
1.1       deraadt   421:        char *fmt;
1.7     ! tholo     422: #endif
        !           423:        char myrealm[REALM_SZ];
1.1       deraadt   424:
1.7     ! tholo     425:        if (krb_get_lrealm(myrealm, 0) != KSUCCESS)
        !           426:                return;
1.1       deraadt   427:        (void)fprintf(stderr, "rsh: warning, using standard rsh: ");
1.7     ! tholo     428: #if __STDC__
        !           429:        va_start(ap, fmt);
        !           430: #else
1.1       deraadt   431:        va_start(ap);
                    432:        fmt = va_arg(ap, char *);
1.7     ! tholo     433: #endif
1.1       deraadt   434:        vfprintf(stderr, fmt, ap);
                    435:        va_end(ap);
                    436:        (void)fprintf(stderr, ".\n");
                    437: }
                    438: #endif
                    439:
                    440: char *
                    441: copyargs(argv)
                    442:        char **argv;
                    443: {
                    444:        register int cc;
                    445:        register char **ap, *p;
                    446:        char *args, *malloc();
                    447:
                    448:        cc = 0;
                    449:        for (ap = argv; *ap; ++ap)
                    450:                cc += strlen(*ap) + 1;
                    451:        if (!(args = malloc((u_int)cc))) {
                    452:                (void)fprintf(stderr, "rsh: %s.\n", strerror(ENOMEM));
                    453:                exit(1);
                    454:        }
                    455:        for (p = args, ap = argv; *ap; ++ap) {
                    456:                (void)strcpy(p, *ap);
                    457:                for (p = strcpy(p, *ap); *p; ++p);
                    458:                if (ap[1])
                    459:                        *p++ = ' ';
                    460:        }
                    461:        return(args);
                    462: }
                    463:
                    464: usage()
                    465: {
                    466:        (void)fprintf(stderr,
                    467:            "usage: rsh [-nd%s]%s[-l login] host [command]\n",
                    468: #ifdef KERBEROS
                    469:            "x", " [-k realm] ");
                    470: #else
                    471:            "", " ");
                    472: #endif
                    473:        exit(1);
                    474: }