Annotation of researchv10dc/ipc/bin/rsh.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char sccsid[] = "@(#)rsh.c      4.8 83/06/10";
        !             3: #endif
        !             4: 
        !             5: #include <sys/param.h>
        !             6: #include <sys/file.h>
        !             7: 
        !             8: #include <stdio.h>
        !             9: #include <errno.h>
        !            10: #include <signal.h>
        !            11: #include <pwd.h>
        !            12: #include "config.h"
        !            13: 
        !            14: /*
        !            15:  * rsh - remote shell
        !            16:  */
        !            17: /* VARARGS */
        !            18: int    error();
        !            19: char   *index(), *rindex(), *malloc(), *getpass(), *sprintf(), *strcpy();
        !            20: 
        !            21: struct passwd *getpwuid();
        !            22: 
        !            23: int    errno;
        !            24: int    sendsig();
        !            25: 
        !            26: #define        mask(s) (1 << ((s) - 1))
        !            27: 
        !            28: main(argc, argv0)
        !            29:        int argc;
        !            30:        char **argv0;
        !            31: {
        !            32:        fd_set rdfds, fds;
        !            33:        int rem;
        !            34:        char *host, *cp, **ap, buf[BUFSIZ], *args, **argv = argv0, *user = 0;
        !            35:        register int n, wn;
        !            36:        register int cc;
        !            37:        int asrsh = 0;
        !            38:        struct passwd *pwd;
        !            39: 
        !            40:        host = rindex(argv[0], '/');
        !            41:        if (host)
        !            42:                host++;
        !            43:        else
        !            44:                host = argv[0];
        !            45:        argv++, --argc;
        !            46:        if (!strcmp(host, "rsh")) {
        !            47:                host = *argv++, --argc;
        !            48:                asrsh = 1;
        !            49:        }
        !            50: another:
        !            51:        if (argc > 0 && !strcmp(*argv, "-l")) {
        !            52:                argv++, argc--;
        !            53:                if (argc > 0)
        !            54:                        user = *argv++, argc--;
        !            55:                goto another;
        !            56:        }
        !            57:        if (argc > 0 && !strcmp(*argv, "-n")) {
        !            58:                argv++, argc--;
        !            59:                (void) close(0);
        !            60:                (void) open("/dev/null", 0);
        !            61:                goto another;
        !            62:        }
        !            63:        /*
        !            64:         * Ignore the -e flag to allow aliases with rogin
        !            65:         * to work
        !            66:         */
        !            67:        if (argc > 0 && !strncmp(*argv, "-e", 2)) {
        !            68:                argv++, argc--;
        !            69:                goto another;
        !            70:        }
        !            71:        if (host == 0)
        !            72:                goto usage;
        !            73: #ifdef broken
        !            74:        if (argv[0] == 0) {
        !            75:                if (asrsh)
        !            76:                        *argv0 = "rogin";
        !            77:                execv(RLOGIN, argv0);
        !            78:                perror(RLOGIN);
        !            79:                exit(1);
        !            80:        }
        !            81: #endif
        !            82:        pwd = getpwuid(getuid());
        !            83:        if (pwd == 0) {
        !            84:                fprintf(stderr, "who are you?\n");
        !            85:                exit(1);
        !            86:        }
        !            87:        cc = 0;
        !            88:        for (ap = argv; *ap; ap++)
        !            89:                cc += strlen(*ap) + 1;
        !            90:        cp = args = malloc(cc);
        !            91:        for (ap = argv; *ap; ap++) {
        !            92:                (void) strcpy(cp, *ap);
        !            93:                while (*cp)
        !            94:                        cp++;
        !            95:                if (ap[1])
        !            96:                        *cp++ = ' ';
        !            97:        }
        !            98: fprintf(stderr, "remote user = %s\n", user ? user : pwd->pw_name);
        !            99:        rem = tcp_rcmd(host, "shell", pwd->pw_name,
        !           100:            user ? user : pwd->pw_name, args, 0);
        !           101:        if (rem < 0)
        !           102:                exit(1);
        !           103:        setuid(getuid());
        !           104:        FD_ZERO(fds);
        !           105:        FD_SET(0, fds);
        !           106:        FD_SET(rem, fds);
        !           107:        while(1){
        !           108:                rdfds = fds;
        !           109:                if(select(NOFILE, &rdfds, 0, 20000) < 0){
        !           110:                        if(errno == EINTR)
        !           111:                                continue;
        !           112:                        else {
        !           113:                                perror("select");
        !           114:                                exit(1);
        !           115:                        }
        !           116:                }
        !           117:                if(FD_ISSET(0, rdfds)){
        !           118:                        n = read(0, buf, sizeof(buf));
        !           119:                        if(n < 0)
        !           120:                                break;
        !           121:                        if(n == 0){
        !           122:                                FD_CLR(0, fds);
        !           123:                                write(rem, "", 0);
        !           124:                        } else {
        !           125:                                if(write(rem, buf, n) != n)
        !           126:                                        break;
        !           127:                        }
        !           128:                }
        !           129:                if(FD_ISSET(rem, rdfds)){
        !           130:                        n = read(rem, buf, sizeof(buf));
        !           131:                        if(n <= 0) {
        !           132:                                break;
        !           133:                        }
        !           134:                        wn = write(1, buf, n);
        !           135:                        if (wn != n) {
        !           136:                                break;
        !           137:                        }
        !           138:                }
        !           139:        }
        !           140:        exit(0);
        !           141: usage:
        !           142:        fprintf(stderr,
        !           143:            "usage: rsh host [ -l login ] [ -p passwd ] command\n");
        !           144:        exit(1);
        !           145: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.