Annotation of researchv9/ipc/src/bin/nrogin.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * RLOGIN
        !             3:  * Connect terminal to TCP/IP network
        !             4:  * Operation is line-at-a-time with remote echo.
        !             5:  */
        !             6: #include <stdio.h>
        !             7: #include <sys/param.h>
        !             8: #include <sys/types.h>
        !             9: #include <sgtty.h>
        !            10: #include <signal.h>
        !            11: #include <errno.h>
        !            12: #include <pwd.h>
        !            13: 
        !            14: #define        CEOT    04
        !            15: #define        NULL    0
        !            16: 
        !            17: 
        !            18: struct sgttyb  locvec, savloc;
        !            19: struct tchars savechars;
        !            20: struct ltchars lsavechars;
        !            21: struct ltchars nlchars = { -1, -1, -1, -1, -1, -1 };
        !            22: 
        !            23: int    rem;    /* remote descriptor */
        !            24: int    rembit; /* 1<<rem; for select */
        !            25: 
        !            26: fd_set rdfd_set;
        !            27: #define        NSELFD  (rem+1)
        !            28: 
        !            29: int    sigint();
        !            30: 
        !            31: char *mytty;
        !            32: SIG_TYP savint, savquit;
        !            33: char term[64] = "network";
        !            34: 
        !            35: intcatch(){    /* catch interrupts, turn them into rubouts */
        !            36:        signal(SIGINT, intcatch);
        !            37:        ioctl(rem, TIOCFLUSH, 0);
        !            38:        write(rem, &savechars.t_intrc, 1);
        !            39:        ioctl(rem, TIOCFLUSH, 0);
        !            40:        ioctl(0, TIOCFLUSH, 0);
        !            41:        FD_ZERO(rdfd_set);
        !            42: }
        !            43: 
        !            44: quitcatch(){   /* catch quits, turn them into FS's */
        !            45:        ioctl(0, TIOCFLUSH, 0);
        !            46:        signal(SIGQUIT, quitcatch);
        !            47:        write(rem, &savechars.t_quitc, 1);
        !            48: }
        !            49: 
        !            50: main(argc,argv)
        !            51:        char **argv;
        !            52: {
        !            53:        char *name=0, *host, *cp;
        !            54:        struct passwd *pwd;
        !            55:        extern struct passwd *getpwuid();
        !            56: 
        !            57:        ioctl(0, TIOCGETC, &savechars);
        !            58:        ioctl(0, TIOCGLTC, &lsavechars);
        !            59:        ioctl(0, TIOCGETP, &savloc);
        !            60:        locvec = savloc;
        !            61: 
        !            62:        argv++, --argc;
        !            63:        host = *argv++, --argc;
        !            64: another:
        !            65:        if (argc > 0 && !strcmp(*argv, "-l")) {
        !            66:                argv++, argc--;
        !            67:                if (argc == 0)
        !            68:                        goto usage;
        !            69:                name = *argv++; argc--;
        !            70:                goto another;
        !            71:        }
        !            72:        if (host == 0)
        !            73:                goto usage;
        !            74:        if (argc > 0)
        !            75:                goto usage;
        !            76:        pwd = getpwuid(getuid());
        !            77:        if (pwd == 0) {
        !            78:                fprintf(stderr, "Who are you?\n");
        !            79:                exit(1);
        !            80:        }
        !            81:        cp = (char *)getenv("TERM");
        !            82:        if (cp)
        !            83:                strcpy(term, cp);
        !            84: 
        !            85: 
        !            86:        /*
        !            87:         * request circuit to host.
        !            88:         */
        !            89:        rem = tcprcmd(host, "login", pwd->pw_name,
        !            90:                name ? name : pwd->pw_name, term, 0);
        !            91:        if (rem < 0) 
        !            92:                exit(1);
        !            93:        if(setuid(getuid()) < 0){
        !            94:                perror("setuid");
        !            95:                exit(1);
        !            96:        }
        !            97: 
        !            98:        savint = signal(SIGINT, intcatch);
        !            99:        savquit = signal(SIGQUIT, quitcatch);
        !           100: 
        !           101:        locvec.sg_flags &= ~(CRMOD|ECHO|XTABS);
        !           102:        locvec.sg_flags |= CBREAK;
        !           103:        ioctl(0, TIOCSETP, &locvec);
        !           104:        ioctl(0, TIOCSLTC, &nlchars);
        !           105: 
        !           106: 
        !           107:        /*
        !           108:         * main loop.
        !           109:         */
        !           110:        do; while(scan() != -1);
        !           111:        quit("select failed");
        !           112:        /*NOTREACHED*/
        !           113: usage:
        !           114:        fprintf(stderr, "Usage: rogin host [-l user]\n");
        !           115:        exit(1);
        !           116: }
        !           117: scan()
        !           118: {
        !           119:        extern errno;
        !           120: 
        !           121:        FD_ZERO(rdfd_set);
        !           122:     Loop:
        !           123:        FD_SET(0, rdfd_set);
        !           124:        FD_SET(rem, rdfd_set);
        !           125:        if(select(NSELFD, &rdfd_set, (fd_set *)0, 2000) == -1)
        !           126:                if(errno == EINTR)
        !           127:                        goto Loop;
        !           128:                else
        !           129:                        return -1;
        !           130:        if(FD_ISSET(0, rdfd_set))
        !           131:                keyboard();
        !           132:        if(FD_ISSET(rem, rdfd_set))
        !           133:                remote();
        !           134:        return 0;
        !           135: }
        !           136: 
        !           137: quit(s)
        !           138:        char *s;
        !           139: {
        !           140:        printf("rogin: %s\n\r", s);
        !           141:        ioctl(0, TIOCSETP, &savloc);
        !           142:        ioctl(0, TIOCSLTC, &lsavechars);
        !           143:        signal(SIGINT, SIG_DFL);
        !           144:        ioctl(rem, TIOCFLUSH, 0);
        !           145:        savloc.sg_ispeed = savloc.sg_ospeed = 0;        /* hangup */
        !           146:        ioctl(rem, TIOCSETP, &savloc);
        !           147:        close(rem);
        !           148:        exit(strcmp(s, "eof"));
        !           149: }
        !           150: 
        !           151: /*
        !           152:  * Scan data from keyboard, looking for escape lines.
        !           153:  */
        !           154: keyboard()
        !           155: {
        !           156:        register c;
        !           157:        register cc;
        !           158:        register char *bp;
        !           159:        register char *be, *obp;
        !           160:        char buf[1024];
        !           161:        static char line[128];
        !           162:        static char *linep = &line[0];
        !           163:        static col = 0;
        !           164:        long time();
        !           165: 
        !           166:        cc = read(0, buf, sizeof buf);
        !           167:        if(cc <0){
        !           168:                if(errno == EINTR)
        !           169:                        return;
        !           170:        }
        !           171:        if(cc <= 0)
        !           172:                quit("read error on file descriptor 0");
        !           173:        be = buf+cc;
        !           174:        bp = obp = buf;
        !           175:        while(bp < be) {
        !           176:                c = *bp++;
        !           177:                if (col==0 && c=='~') {
        !           178:                        *linep++ = c;
        !           179:                        col = 1;
        !           180:                        locvec.sg_flags |= savloc.sg_flags&(CRMOD|ECHO);
        !           181:                        ioctl(0, TIOCSETP, &locvec);
        !           182:                        write(1, linep-1, 1);
        !           183:                        continue;
        !           184:                }
        !           185:                col++;
        !           186:                if (c=='\r')
        !           187:                        c = '\n';
        !           188:                if (linep>line) {
        !           189:                        *linep++ = c;
        !           190:                        if (c==savloc.sg_kill)
        !           191:                                write(1, "\n", 1);
        !           192:                        if (c==savloc.sg_erase)
        !           193:                                linep -= 2;
        !           194:                        if (c==savloc.sg_kill || linep<=line) {
        !           195:                                linep = line;
        !           196:                                locvec.sg_flags &= ~(CRMOD|ECHO);
        !           197:                                ioctl(0, TIOCSETP, &locvec);
        !           198:                                col = 0;
        !           199:                                obp = bp;
        !           200:                        }
        !           201:                }
        !           202:                if (c=='\n') {
        !           203:                        col = 0;
        !           204:                        if (linep > line) {
        !           205:                                *linep = '\0';
        !           206:                                if (escape(line+1))
        !           207:                                        write(rem, line+1, linep-line-1);
        !           208:                                obp = bp;
        !           209:                                linep = line;
        !           210:                                locvec.sg_flags &= ~(CRMOD|ECHO);
        !           211:                                ioctl(0, TIOCSETP, &locvec);
        !           212:                        }
        !           213:                }
        !           214:        }
        !           215:        if (bp>obp && linep==line)
        !           216:                if(write(rem, obp, bp-obp) <= 0)
        !           217:                        quit("Eof");
        !           218: }
        !           219: 
        !           220: /*
        !           221:  * Send data from remote machine to standard output (trivial)
        !           222:  */
        !           223: remote(){
        !           224:        char buf[1024];
        !           225:        register n;
        !           226: 
        !           227:        n = read(rem, buf, sizeof buf);
        !           228:        if(n < 0 && errno == EINTR)
        !           229:                return;
        !           230:        if(n <= 0)
        !           231:                quit("Eof");
        !           232:        write(1, buf, n);
        !           233: }
        !           234: 
        !           235: escape(line)
        !           236:        register char *line;
        !           237: {
        !           238: 
        !           239:        switch(*line++) {
        !           240:        case '!':
        !           241:                cunix(line);
        !           242:                return(0);
        !           243: 
        !           244:        case '.':
        !           245:        case CEOT:
        !           246:                quit("eof");
        !           247: 
        !           248:        case 'b':
        !           249:                ioctl(rem, TIOCSBRK, 0);
        !           250:                return(0);
        !           251:        default:
        !           252:                return(1);
        !           253:        }
        !           254: }
        !           255: 
        !           256: cunix(prog)
        !           257: char *prog;
        !           258: {
        !           259:        register int upid;
        !           260:        int retcode;
        !           261: 
        !           262:        if ((upid = fork()) == 0) {
        !           263:                signal(SIGINT, savint);
        !           264:                signal(SIGQUIT, savquit);
        !           265:                ioctl(0, TIOCSETN, &savloc);
        !           266:                ioctl(0, TIOCSLTC, &lsavechars);
        !           267:                if (*prog == '\n')
        !           268:                        execl("/bin/sh", "sh", "-i", 0);
        !           269:                else
        !           270:                        execl("/bin/sh","sh","-c",prog,0);
        !           271:                exit(0100);
        !           272:        }
        !           273:        if (upid < 0) {
        !           274:                printf("can't fork\n");
        !           275:        } else {
        !           276:                while((wait(&retcode) !=upid))
        !           277:                        ;
        !           278:        }
        !           279:        signal(SIGINT, intcatch);
        !           280:        signal(SIGQUIT, quitcatch);
        !           281:        ioctl(0, TIOCSETN, &locvec);
        !           282:        ioctl(0, TIOCSLTC, &nlchars);
        !           283:        printf("!!\n");
        !           284: }
        !           285: 
        !           286: tcprcmd(host, port, locuser, remuser, cmd, fd2p)
        !           287: char *host, *port, *locuser, *remuser, *cmd;
        !           288: int *fd2p;
        !           289: {
        !           290:        char buf[1];
        !           291:        int fd;
        !           292:        char dialstr[128];
        !           293: 
        !           294:        sprintf(dialstr, "/cs/in!%s!%s", host, port);
        !           295:        fd = ipcopen(dialstr, "light");
        !           296:        if (fd<0) {
        !           297:                fprintf(stderr, "%s: connection failed\n", host);
        !           298:                goto bad;
        !           299:        }
        !           300: 
        !           301:        if(fd2p == 0)
        !           302:                write(fd, "", 1);
        !           303:        else
        !           304:                goto bad;       /* later */
        !           305: 
        !           306:        write(fd, locuser, strlen(locuser)+1);
        !           307:        write(fd, remuser, strlen(remuser)+1);
        !           308:        write(fd, cmd, strlen(cmd)+1);
        !           309: 
        !           310:        if(read(fd, buf, 1) != 1){
        !           311:                perror(host);
        !           312:                goto bad;
        !           313:        }
        !           314:        if(buf[0] != 0){
        !           315:                while(read(fd, buf, 1) == 1){
        !           316:                        write(2, buf, 1);
        !           317:                        if(buf[0] == '\n')
        !           318:                                break;
        !           319:                }
        !           320:                goto bad;
        !           321:        }
        !           322:        return(fd);
        !           323: bad:
        !           324:        close(fd);
        !           325:        return(-1);
        !           326: }

unix.superglobalmegacorp.com

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