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

unix.superglobalmegacorp.com

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