Annotation of researchv8dc/cmd/inet/bin/rogin.c, revision 1.1.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:        host = (char *)rindex(argv[0], '/');
                     63:        if (host)
                     64:                host++;
                     65:        else
                     66:                host = argv[0];
                     67:        argv++, --argc;
                     68:        if (!strcmp(host, "rogin"))
                     69:                host = *argv++, --argc;
                     70: another:
                     71:        if (argc > 0 && !strcmp(*argv, "-l")) {
                     72:                argv++, argc--;
                     73:                if (argc == 0)
                     74:                        goto usage;
                     75:                name = *argv++; argc--;
                     76:                goto another;
                     77:        }
                     78:        if (host == 0)
                     79:                goto usage;
                     80:        if (argc > 0)
                     81:                goto usage;
                     82:        pwd = getpwuid(getuid());
                     83:        if (pwd == 0) {
                     84:                fprintf(stderr, "Who are you?\n");
                     85:                exit(1);
                     86:        }
                     87:        cp = (char *)getenv("TERM");
                     88:        if (cp)
                     89:                strcpy(term, cp);
                     90: 
                     91: 
                     92:        /*
                     93:         * request circuit to host.
                     94:         */
                     95:        rem = tcp_rcmd(host, "login", pwd->pw_name,
                     96:                name ? name : pwd->pw_name, term, 0);
                     97:        if (rem < 0) 
                     98:                exit(1);
                     99:        if(setuid(getuid()) < 0){
                    100:                perror("setuid");
                    101:                exit(1);
                    102:        }
                    103: 
                    104:        savint = signal(SIGINT, intcatch);
                    105:        savquit = signal(SIGQUIT, quitcatch);
                    106: 
                    107:        locvec.sg_flags &= ~(CRMOD|ECHO|XTABS);
                    108:        locvec.sg_flags |= CBREAK;
                    109:        ioctl(0, TIOCSETP, &locvec);
                    110:        ioctl(0, TIOCSLTC, &nlchars);
                    111: 
                    112: 
                    113:        /*
                    114:         * main loop.
                    115:         */
                    116:        do; while(scan() != -1);
                    117:        quit("select failed");
                    118:        /*NOTREACHED*/
                    119: usage:
                    120:        fprintf(stderr, "Usage: rogin host [-l user]\n");
                    121:        exit(1);
                    122: }
                    123: scan()
                    124: {
                    125:        extern errno;
                    126: 
                    127:        FD_ZERO(rdfd_set);
                    128:     Loop:
                    129:        FD_SET(0, rdfd_set);
                    130:        FD_SET(rem, rdfd_set);
                    131:        if(select(NSELFD, &rdfd_set, (fd_set *)0, 2000) == -1)
                    132:                if(errno == EINTR)
                    133:                        goto Loop;
                    134:                else
                    135:                        return -1;
                    136:        if(FD_ISSET(0, rdfd_set))
                    137:                keyboard();
                    138:        if(FD_ISSET(rem, rdfd_set))
                    139:                remote();
                    140:        return 0;
                    141: }
                    142: 
                    143: quit(s)
                    144:        char *s;
                    145: {
                    146:        printf("rogin: %s\n\r", s);
                    147:        ioctl(0, TIOCSETP, &savloc);
                    148:        ioctl(0, TIOCSLTC, &lsavechars);
                    149:        signal(SIGINT, SIG_DFL);
                    150:        ioctl(rem, TIOCFLUSH, 0);
                    151:        savloc.sg_ispeed = savloc.sg_ospeed = 0;        /* hangup */
                    152:        ioctl(rem, TIOCSETP, &savloc);
                    153:        close(rem);
                    154:        exit(strcmp(s, "eof"));
                    155: }
                    156: 
                    157: /*
                    158:  * Scan data from keyboard, looking for escape lines.
                    159:  */
                    160: keyboard()
                    161: {
                    162:        register c;
                    163:        register cc;
                    164:        register char *bp;
                    165:        register char *be, *obp;
                    166:        char buf[1024];
                    167:        static char line[128];
                    168:        static char *linep = &line[0];
                    169:        static col = 0;
                    170:        long time();
                    171: 
                    172:        cc = read(0, buf, sizeof buf);
                    173:        if(cc <0){
                    174:                if(errno == EINTR)
                    175:                        return;
                    176:        }
                    177:        if(cc <= 0)
                    178:                quit("read error on file descriptor 0");
                    179:        be = buf+cc;
                    180:        bp = obp = buf;
                    181:        while(bp < be) {
                    182:                c = *bp++;
                    183:                if (col==0 && c=='~') {
                    184:                        *linep++ = c;
                    185:                        col = 1;
                    186:                        locvec.sg_flags |= savloc.sg_flags&(CRMOD|ECHO);
                    187:                        ioctl(0, TIOCSETP, &locvec);
                    188:                        write(1, linep-1, 1);
                    189:                        continue;
                    190:                }
                    191:                col++;
                    192:                if (c=='\r')
                    193:                        c = '\n';
                    194:                if (linep>line) {
                    195:                        *linep++ = c;
                    196:                        if (c==savloc.sg_kill)
                    197:                                write(1, "\n", 1);
                    198:                        if (c==savloc.sg_erase)
                    199:                                linep -= 2;
                    200:                        if (c==savloc.sg_kill || linep<=line) {
                    201:                                linep = line;
                    202:                                locvec.sg_flags &= ~(CRMOD|ECHO);
                    203:                                ioctl(0, TIOCSETP, &locvec);
                    204:                                col = 0;
                    205:                                obp = bp;
                    206:                        }
                    207:                }
                    208:                if (c=='\n') {
                    209:                        col = 0;
                    210:                        if (linep > line) {
                    211:                                *linep = '\0';
                    212:                                if (escape(line+1))
                    213:                                        write(rem, line+1, linep-line-1);
                    214:                                obp = bp;
                    215:                                linep = line;
                    216:                                locvec.sg_flags &= ~(CRMOD|ECHO);
                    217:                                ioctl(0, TIOCSETP, &locvec);
                    218:                        }
                    219:                }
                    220:        }
                    221:        if (bp>obp && linep==line)
                    222:                if(write(rem, obp, bp-obp) <= 0)
                    223:                        quit("Eof");
                    224: }
                    225: 
                    226: /*
                    227:  * Send data from remote machine to standard output (trivial)
                    228:  */
                    229: remote(){
                    230:        char buf[1024];
                    231:        register n;
                    232: 
                    233:        n = read(rem, buf, sizeof buf);
                    234:        if(n < 0 && errno == EINTR)
                    235:                return;
                    236:        if(n <= 0)
                    237:                quit("Eof");
                    238:        write(1, buf, n);
                    239: }
                    240: 
                    241: escape(line)
                    242:        register char *line;
                    243: {
                    244: 
                    245:        switch(*line++) {
                    246:        case '!':
                    247:                cunix(line);
                    248:                return(0);
                    249: 
                    250:        case '.':
                    251:        case CEOT:
                    252:                quit("eof");
                    253: 
                    254:        case 'b':
                    255:                ioctl(rem, TIOCSBRK, 0);
                    256:                return(0);
                    257:        default:
                    258:                return(1);
                    259:        }
                    260: }
                    261: 
                    262: cunix(prog)
                    263: char *prog;
                    264: {
                    265:        register int upid;
                    266:        int retcode;
                    267: 
                    268:        if ((upid = fork()) == 0) {
                    269:                signal(SIGINT, savint);
                    270:                signal(SIGQUIT, savquit);
                    271:                ioctl(0, TIOCSETN, &savloc);
                    272:                ioctl(0, TIOCSLTC, &lsavechars);
                    273:                if (*prog == '\n')
                    274:                        execl("/bin/sh", "sh", "-i", 0);
                    275:                else
                    276:                        execl("/bin/sh","sh","-c",prog,0);
                    277:                exit(0100);
                    278:        }
                    279:        if (upid < 0) {
                    280:                printf("can't fork\n");
                    281:        } else {
                    282:                while((wait(&retcode) !=upid))
                    283:                        ;
                    284:        }
                    285:        signal(SIGINT, intcatch);
                    286:        signal(SIGQUIT, quitcatch);
                    287:        ioctl(0, TIOCSETN, &locvec);
                    288:        ioctl(0, TIOCSLTC, &nlchars);
                    289:        printf("!!\n");
                    290: }

unix.superglobalmegacorp.com

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