Annotation of 40BSD/cmd/uucp/uuxqt.c, revision 1.1.1.1

1.1       root        1: #include "uucp.h"
                      2: #include "uucpdefs.h"
                      3: #include <sys/types.h>
                      4: #include <sys/stat.h>
                      5: #include <sys/dir.h>
                      6: 
                      7: #define APPCMD(d) {\
                      8: char *p;\
                      9: for (p = d; *p != '\0';) *cmdp++ = *p++;\
                     10: *cmdp++ = ' ';\
                     11: *cmdp = '\0';}
                     12: 
                     13: /*
                     14:  *     uuxqt will execute commands set up by a uux command,
                     15:  *     usually from a remote machine - set by uucp.
                     16:  */
                     17: 
                     18: char *Cmds[] = {
                     19:        "rmail",
                     20:        "rnews",
                     21:        "vpr",
                     22:        "w",
                     23:        "fsend",
                     24:        "fget",
                     25:        "uusend",
                     26:        "cp",
                     27:        NULL
                     28:        };
                     29: #define PATH   "PATH=/bin:/usr/bin;"
                     30: /*  to remove restrictions from uuxqt
                     31:  *  define ALLOK 1
                     32:  *
                     33:  *  to add allowable commands, add to the list under Cmds[]
                     34:  */
                     35: 
                     36: main(argc, argv)
                     37: char *argv[];
                     38: {
                     39:        char xcmd[100];
                     40:        int cmdnok;
                     41:        char xfile[MAXFULLNAME], user[32], buf[BUFSIZ];
                     42:        char lbuf[30];
                     43:        char cfile[NAMESIZE], dfile[MAXFULLNAME];
                     44:        char file[NAMESIZE];
                     45:        char fin[MAXFULLNAME], sysout[NAMESIZE], fout[MAXFULLNAME];
                     46:        FILE *xfp, *dfp, *fp;
                     47:        char path[MAXFULLNAME];
                     48:        char cmd[BUFSIZ];
                     49:        char *cmdp, prm[MAXFULLNAME], *ptr;
                     50:        char *getprm();
                     51:        int uid, ret;
                     52:        int stcico = 0;
                     53:        char rnum[5];
                     54: 
                     55:        uucpname(Myname);
                     56:        Ofn = 1;
                     57:        Ifn = 0;
                     58:        while (argc>1 && argv[1][0] == '-') {
                     59:                switch(argv[1][1]){
                     60:                case 'x':
                     61:                        Debug = atoi(&argv[1][2]);
                     62:                        if (Debug <= 0)
                     63:                                Debug = 1;
                     64:                        break;
                     65:                default:
                     66:                        sprintf(stderr, "unknown flag %s\n", argv[1]);
                     67:                                break;
                     68:                }
                     69:                --argc;  argv++;
                     70:        }
                     71: 
                     72:        DEBUG(4, "\n\n** %s **\n", "START");
                     73:        chdir(Spool);
                     74:        strcpy(Wrkdir, Spool);
                     75:        uid = getuid();
                     76:        guinfo(uid, User, path);
                     77:        DEBUG(4, "User - %s\n", User);
                     78:        if (ulockf(X_LOCK, X_LOCKTIME) != 0)
                     79:                exit(0);
                     80: 
                     81:        DEBUG(4, "process %s\n", "");
                     82:        while (gtxfile(xfile) > 0) {
                     83:                DEBUG(4, "xfile - %s\n", xfile);
                     84: 
                     85:                xfp = fopen(xfile, "r");
                     86:                ASSERT(xfp != NULL, "CAN'T OPEN %s", xfile);
                     87: 
                     88:                /*  initialize to default  */
                     89:                strcpy(user, User);
                     90:                strcpy(fin, "/dev/null");
                     91:                strcpy(fout, "/dev/null");
                     92:                sprintf(sysout, "%.7s", Myname);
                     93:                while (fgets(buf, BUFSIZ, xfp) != NULL) {
                     94:                        switch (buf[0]) {
                     95:                        case X_USER:
                     96:                                sscanf(&buf[1], "%s%s", user, Rmtname);
                     97:                                break;
                     98:                        case X_STDIN:
                     99:                                sscanf(&buf[1], "%s", fin);
                    100:                                expfile(fin);
                    101:                                break;
                    102:                        case X_STDOUT:
                    103:                                sscanf(&buf[1], "%s%s", fout, sysout);
                    104:                                sysout[7] = '\0';
                    105:                                if (fout[0] != '~' || prefix(sysout, Myname))
                    106:                                        expfile(fout);
                    107:                                break;
                    108:                        case X_CMD:
                    109:                                strcpy(cmd, &buf[2]);
                    110:                                if (*(cmd + strlen(cmd) - 1) == '\n')
                    111:                                        *(cmd + strlen(cmd) - 1) = '\0';
                    112:                                break;
                    113:                        default:
                    114:                                break;
                    115:                        }
                    116:                }
                    117: 
                    118:                fclose(xfp);
                    119:                DEBUG(4, "fin - %s, ", fin);
                    120:                DEBUG(4, "fout - %s, ", fout);
                    121:                DEBUG(4, "sysout - %s, ", sysout);
                    122:                DEBUG(4, "user - %s\n", user);
                    123:                DEBUG(4, "cmd - %s\n", cmd);
                    124: 
                    125:                /*  command execution  */
                    126:                if (strcmp(fout, "/dev/null") == SAME)
                    127:                        strcpy(dfile,"/dev/null");
                    128:                else
                    129:                        gename(DATAPRE, sysout, 'O', dfile);
                    130: 
                    131:                /* expand file names where necessary */
                    132:                expfile(dfile);
                    133:                strcpy(buf, PATH);
                    134:                cmdp = buf + strlen(buf);
                    135:                ptr = cmd;
                    136:                xcmd[0] = '\0';
                    137:                cmdnok = 0;
                    138:                while ((ptr = getprm(ptr, prm)) != NULL) {
                    139:                        if (prm[0] == ';' || prm[0] == '^'
                    140:                          || prm[0] == '|') {
                    141:                                xcmd[0] = '\0';
                    142:                                APPCMD(prm);
                    143:                                continue;
                    144:                        }
                    145:                        if ((cmdnok = cmdok(xcmd, prm)) != 0) 
                    146:                                /*  command not valid  */
                    147:                                break;
                    148: 
                    149:                        if (prm[0] == '~')
                    150:                                expfile(prm);
                    151:                        APPCMD(prm);
                    152:                }
                    153:                if (cmdnok) {
                    154:                        sprintf(lbuf, "%s XQT DENIED", user);
                    155:                        logent(cmd, lbuf);
                    156:                        DEBUG(4, "bad command %s\n", prm);
                    157:                        notify(user, Rmtname, cmd, "DENIED");
                    158:                        goto rmfiles;
                    159:                }
                    160:                sprintf(lbuf, "%s XQT", user);
                    161:                logent(buf, lbuf);
                    162:                DEBUG(4, "cmd %s\n", buf);
                    163: 
                    164:                mvxfiles(xfile);
                    165:                chdir(XQTDIR);
                    166:                ret = shio(buf, fin, dfile, user);
                    167:                sprintf(rnum, "%d", ret);
                    168:                if (ret != 0
                    169:                  || strcmp(xcmd, "rmail") != SAME
                    170:                    && strcmp(xcmd, "rnews") != SAME)
                    171:                        notify(user, Rmtname, cmd, rnum);
                    172:                DEBUG(4, "exit cmd - %d\n", ret);
                    173:                chdir(Spool);
                    174:                rmxfiles(xfile);
                    175:                if (ret != 0) {
                    176:                        /*  exit status not zero */
                    177:                        dfp = fopen(dfile, "a");
                    178:                        ASSERT(dfp != NULL, "CAN'T OPEN %s", dfile);
                    179:                        fprintf(dfp, "exit status %d", ret);
                    180:                        fclose(dfp);
                    181:                }
                    182:                if (strcmp(fout, "/dev/null") != SAME) {
                    183:                        if (prefix(sysout, Myname)) {
                    184:                                xmv(dfile, fout);
                    185:                        }
                    186:                        else {
                    187:                                gename(CMDPRE, sysout, 'O', cfile);
                    188:                                fp = fopen(cfile, "w");
                    189:                                ASSERT(fp != NULL, "OPEN %s", cfile);
                    190:                                chmod(cfile, 0666);
                    191:                                fprintf(fp, "S %s %s %s - %s 0666\n",
                    192:                                dfile, fout, user, lastpart(dfile));
                    193:                                fclose(fp);
                    194:                        }
                    195:                }
                    196:        rmfiles:
                    197:                xfp = fopen(xfile, "r");
                    198:                ASSERT(xfp != NULL, "CAN'T OPEN %s", xfile);
                    199:                while (fgets(buf, BUFSIZ, xfp) != NULL) {
                    200:                        if (buf[0] != X_RQDFILE)
                    201:                                continue;
                    202:                        sscanf(&buf[1], "%s", file);
                    203:                        unlink(file);
                    204:                }
                    205:                unlink(xfile);
                    206:        }
                    207: 
                    208:        if (stcico)
                    209:                xuucico("");
                    210:        cleanup(0);
                    211: }
                    212: 
                    213: 
                    214: cleanup(code)
                    215: int code;
                    216: {
                    217:        logcls();
                    218:        rmlock(NULL);
                    219:        exit(code);
                    220: }
                    221: 
                    222: 
                    223: /*******
                    224:  *     gtxfile(file)   get a file to execute
                    225:  *     char *file;
                    226:  *
                    227:  *     return codes:  0 - no file  |  1 - file to execute
                    228:  */
                    229: 
                    230: gtxfile(file)
                    231: char *file;
                    232: {
                    233:        static FILE *pdir;
                    234:        char pre[2];
                    235: 
                    236:        if (pdir == NULL) {
                    237:                pdir = fopen(Spool, "r");
                    238:                ASSERT(pdir != NULL, "GTXFILE CAN'T OPEN %s", Spool);
                    239:        }
                    240: 
                    241:        pre[0] = XQTPRE;
                    242:        pre[1] = '\0';
                    243:        while (gnamef(pdir, file) != 0) {
                    244:                DEBUG(4, "file - %s\n", file);
                    245:                if (!prefix(pre, file))
                    246:                        continue;
                    247:                if (gotfiles(file))
                    248:                        /*  return file to execute */
                    249:                        return(1);
                    250:        }
                    251: 
                    252:        fclose(pdir);
                    253:        return(0);
                    254: }
                    255: 
                    256: 
                    257: /***
                    258:  *     gotfiles(file)          check for needed files
                    259:  *     char *file;
                    260:  *
                    261:  *     return codes:  0 - not ready  |  1 - all files ready
                    262:  */
                    263: 
                    264: gotfiles(file)
                    265: char *file;
                    266: {
                    267:        struct stat stbuf;
                    268:        FILE *fp;
                    269:        char buf[BUFSIZ], rqfile[MAXFULLNAME];
                    270: 
                    271:        fp = fopen(file, "r");
                    272:        if (fp == NULL)
                    273:                return(0);
                    274: 
                    275:        while (fgets(buf, BUFSIZ, fp) != NULL) {
                    276:                DEBUG(4, "%s\n", buf);
                    277:                if (buf[0] != X_RQDFILE)
                    278:                        continue;
                    279:                sscanf(&buf[1], "%s", rqfile);
                    280:                expfile(rqfile);
                    281:                if (stat(rqfile, &stbuf) == -1) {
                    282:                        fclose(fp);
                    283:                        return(0);
                    284:                }
                    285:        }
                    286: 
                    287:        fclose(fp);
                    288:        return(1);
                    289: }
                    290: 
                    291: 
                    292: /***
                    293:  *     rmxfiles(xfile)         remove execute files to x-directory
                    294:  *     char *xfile;
                    295:  *
                    296:  *     return codes - none
                    297:  */
                    298: 
                    299: rmxfiles(xfile)
                    300: char *xfile;
                    301: {
                    302:        FILE *fp;
                    303:        char buf[BUFSIZ], file[NAMESIZE], tfile[NAMESIZE];
                    304:        char tfull[MAXFULLNAME];
                    305: 
                    306:        if((fp = fopen(xfile, "r")) == NULL)
                    307:                return;
                    308: 
                    309:        while (fgets(buf, BUFSIZ, fp) != NULL) {
                    310:                if (buf[0] != X_RQDFILE)
                    311:                        continue;
                    312:                if (sscanf(&buf[1], "%s%s", file, tfile) < 2)
                    313:                        continue;
                    314:                sprintf(tfull, "%s/%s", XQTDIR, tfile);
                    315:                unlink(tfull);
                    316:        }
                    317:        fclose(fp);
                    318:        return;
                    319: }
                    320: 
                    321: 
                    322: /***
                    323:  *     mvxfiles(xfile)         move execute files to x-directory
                    324:  *     char *xfile;
                    325:  *
                    326:  *     return codes - none
                    327:  */
                    328: 
                    329: mvxfiles(xfile)
                    330: char *xfile;
                    331: {
                    332:        FILE *fp;
                    333:        char buf[BUFSIZ], ffile[MAXFULLNAME], tfile[NAMESIZE];
                    334:        char tfull[MAXFULLNAME];
                    335:        int ret;
                    336: 
                    337:        if((fp = fopen(xfile, "r")) == NULL)
                    338:                return;
                    339: 
                    340:        while (fgets(buf, BUFSIZ, fp) != NULL) {
                    341:                if (buf[0] != X_RQDFILE)
                    342:                        continue;
                    343:                if (sscanf(&buf[1], "%s%s", ffile, tfile) < 2)
                    344:                        continue;
                    345:                expfile(ffile);
                    346:                sprintf(tfull, "%s/%s", XQTDIR, tfile);
                    347:                unlink(tfull);
                    348:                ret = link(ffile, tfull);
                    349:                ASSERT(ret == 0, "LINK RET-%d", ret);
                    350:                unlink(ffile);
                    351:        }
                    352:        fclose(fp);
                    353:        return;
                    354: }
                    355: 
                    356: 
                    357: /***
                    358:  *     cmdok(xc, cmd)          check for valid command
                    359:  *                     *NOTE - side effect is to set xc to the
                    360:  *                             command to be executed.
                    361:  *     char *xc, *cmd;
                    362:  *
                    363:  *     return 0 - ok | 1 nok
                    364:  */
                    365: 
                    366: cmdok(xc, cmd)
                    367: char *xc, *cmd;
                    368: {
                    369:        char **ptr;
                    370: 
                    371:        if (xc[0] != '\0')
                    372:                return(0);
                    373: #ifndef ALLOK
                    374:        ptr = Cmds;
                    375:        while(*ptr != NULL) {
                    376:                if (strcmp(cmd, *ptr) == SAME)
                    377:                        break;
                    378:        ptr++;
                    379:        }
                    380:        if (*ptr == NULL)
                    381:                return(1);
                    382: #endif
                    383:        strcpy(xc, cmd);
                    384:        return(0);
                    385: }
                    386: 
                    387: 
                    388: /***
                    389:  *     notify  send mail to user giving execution results
                    390:  *     return code - none
                    391:  *     This program assumes new mail command - send remote mail
                    392:  */
                    393: 
                    394: notify(user, rmt, cmd, str)
                    395: char *user, *rmt, *cmd, *str;
                    396: {
                    397:        char text[100];
                    398:        char ruser[100];
                    399: 
                    400:        sprintf(text, "uuxqt cmd (%s) status (%s)", cmd, str);
                    401:        if (prefix(rmt, Myname))
                    402:                strcpy(ruser, user);
                    403:        else
                    404:                sprintf(ruser, "%s!%s", rmt, user);
                    405:        mailst(ruser, text);
                    406:        return;
                    407: }

unix.superglobalmegacorp.com

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