[BACK]Return to at.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / at

Annotation of src/usr.bin/at/at.c, Revision 1.16

1.16    ! mickey      1: /*     $OpenBSD: at.c,v 1.15 1998/06/03 16:20:26 deraadt Exp $ */
1.1       deraadt     2: /*     $NetBSD: at.c,v 1.4 1995/03/25 18:13:31 glass Exp $     */
                      3:
                      4: /*
1.7       millert     5:  *  at.c : Put file into atrun queue
                      6:  *  Copyright (C) 1993, 1994  Thomas Koenig
1.1       deraadt     7:  *
1.7       millert     8:  *  Atrun & Atq modifications
                      9:  *  Copyright (C) 1993  David Parsons
1.1       deraadt    10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. The name of the author(s) may not be used to endorse or promote
                     17:  *    products derived from this software without specific prior written
                     18:  *    permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
                     21:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     22:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1.7       millert    23:  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1.1       deraadt    24:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     25:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     29:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31:
                     32: /* System Headers */
                     33: #include <sys/types.h>
1.7       millert    34: #include <sys/param.h>
1.1       deraadt    35: #include <sys/stat.h>
                     36: #include <sys/wait.h>
                     37: #include <ctype.h>
                     38: #include <dirent.h>
                     39: #include <errno.h>
                     40: #include <fcntl.h>
                     41: #include <pwd.h>
                     42: #include <signal.h>
                     43: #include <stddef.h>
                     44: #include <stdio.h>
                     45: #include <stdlib.h>
                     46: #include <string.h>
                     47: #include <time.h>
                     48: #include <unistd.h>
1.7       millert    49: #include <utmp.h>
                     50: #include <locale.h>
1.16    ! mickey     51: #include <err.h>
1.7       millert    52:
                     53: #if (MAXLOGNAME-1) > UT_NAMESIZE
                     54: #define LOGNAMESIZE UT_NAMESIZE
                     55: #else
                     56: #define LOGNAMESIZE (MAXLOGNAME-1)
                     57: #endif
1.1       deraadt    58:
                     59: /* Local headers */
                     60: #include "at.h"
                     61: #include "panic.h"
                     62: #include "parsetime.h"
1.7       millert    63: #include "perm.h"
1.1       deraadt    64: #include "pathnames.h"
                     65: #define MAIN
                     66: #include "privs.h"
                     67:
                     68: /* Macros */
                     69: #define ALARMC 10              /* Number of seconds to wait for timeout */
                     70:
                     71: #define TIMESIZE 50
                     72:
1.7       millert    73: enum { ATQ, ATRM, AT, BATCH, CAT };    /* what program we want to run */
                     74:
1.1       deraadt    75: /* File scope variables */
                     76: #ifndef lint
1.16    ! mickey     77: static char rcsid[] = "$OpenBSD: at.c,v 1.15 1998/06/03 16:20:26 deraadt Exp $";
1.1       deraadt    78: #endif
                     79:
                     80: char *no_export[] =
                     81: {
                     82:        "TERM", "TERMCAP", "DISPLAY", "_"
                     83: };
1.16    ! mickey     84: static int send_mail = 0;
1.1       deraadt    85:
                     86: /* External variables */
1.7       millert    87:
1.1       deraadt    88: extern char **environ;
                     89: int fcreated;
                     90: char *namep;
                     91: char atfile[FILENAME_MAX];
                     92:
1.7       millert    93: char *atinput = (char *)0;     /* where to get input from */
1.1       deraadt    94: char atqueue = 0;              /* which queue to examine for jobs (atq) */
                     95: char atverify = 0;             /* verify time instead of queuing job */
                     96:
                     97: /* Function declarations */
1.7       millert    98:
                     99: static void sigc       __P((int));
                    100: static void alarmc     __P((int));
1.1       deraadt   101: static char *cwdname   __P((void));
1.7       millert   102: static void writefile  __P((time_t, char));
1.1       deraadt   103: static void list_jobs  __P((void));
                    104:
                    105: /* Signal catching functions */
                    106:
                    107: static void
                    108: sigc(signo)
                    109:        int signo;
                    110: {
1.7       millert   111:        /* If the user presses ^C, remove the spool file and exit. */
1.1       deraadt   112:        if (fcreated) {
                    113:                PRIV_START
1.7       millert   114:                (void)unlink(atfile);
1.1       deraadt   115:                PRIV_END
                    116:        }
                    117:
                    118:        exit(EXIT_FAILURE);
                    119: }
                    120:
                    121: static void
                    122: alarmc(signo)
                    123:        int signo;
                    124: {
1.7       millert   125:        /* Time out after some seconds. */
1.1       deraadt   126:        panic("File locking timed out");
                    127: }
                    128:
                    129: /* Local functions */
                    130:
                    131: static char *
                    132: cwdname()
                    133: {
1.7       millert   134:        /*
                    135:         * Read in the current directory; the name will be overwritten on
                    136:         * subsequent calls.
                    137:         */
                    138:        static char path[MAXPATHLEN];
1.1       deraadt   139:
1.7       millert   140:        return (getcwd(path, sizeof(path)));
                    141: }
1.1       deraadt   142:
1.7       millert   143: static int
                    144: nextjob()
                    145: {
                    146:        int jobno;
                    147:        FILE *fid;
1.1       deraadt   148:
1.12      kstailey  149:        if ((fid = fopen(_PATH_SEQFILE, "r+")) != NULL) {
1.7       millert   150:                if (fscanf(fid, "%5x", &jobno) == 1) {
                    151:                        (void)rewind(fid);
                    152:                        jobno = (1+jobno) % 0xfffff;    /* 2^20 jobs enough? */
                    153:                        (void)fprintf(fid, "%05x\n", jobno);
                    154:                } else
                    155:                        jobno = EOF;
                    156:                (void)fclose(fid);
                    157:                return (jobno);
1.12      kstailey  158:        } else if ((fid = fopen(_PATH_SEQFILE, "w")) != NULL) {
1.7       millert   159:                (void)fprintf(fid, "%05x\n", jobno = 1);
                    160:                (void)fclose(fid);
                    161:                return (1);
1.1       deraadt   162:        }
1.7       millert   163:        return (EOF);
1.1       deraadt   164: }
                    165:
                    166: static void
                    167: writefile(runtimer, queue)
                    168:        time_t runtimer;
                    169:        char queue;
                    170: {
                    171:        /*
                    172:         * This does most of the work if at or batch are invoked for
                    173:         * writing a job.
                    174:         */
1.7       millert   175:        int jobno;
1.1       deraadt   176:        char *ap, *ppos, *mailname;
                    177:        struct passwd *pass_entry;
                    178:        struct stat statbuf;
                    179:        int fdes, lockdes, fd2;
                    180:        FILE *fp, *fpin;
                    181:        struct sigaction act;
                    182:        char **atenv;
                    183:        int ch;
                    184:        mode_t cmask;
                    185:        struct flock lock;
                    186:
1.7       millert   187:        (void)setlocale(LC_TIME, "");
                    188:
1.1       deraadt   189:        /*
                    190:         * Install the signal handler for SIGINT; terminate after removing the
                    191:         * spool file if necessary
                    192:         */
1.15      deraadt   193:        memset(&act, 0, sizeof act);
1.1       deraadt   194:        act.sa_handler = sigc;
                    195:        sigemptyset(&(act.sa_mask));
                    196:        act.sa_flags = 0;
                    197:
                    198:        sigaction(SIGINT, &act, NULL);
                    199:
1.7       millert   200:        (void)strcpy(atfile, _PATH_ATJOBS);
                    201:        ppos = atfile + strlen(atfile);
1.1       deraadt   202:
                    203:        /*
                    204:         * Loop over all possible file names for running something at this
1.7       millert   205:         * particular time, see if a file is there; the first empty slot at
                    206:         * any particular time is used.  Lock the file _PATH_LOCKFILE first
                    207:         * to make sure we're alone when doing this.
1.1       deraadt   208:         */
                    209:
                    210:        PRIV_START
                    211:
1.7       millert   212:        if ((lockdes = open(_PATH_LOCKFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0)
1.1       deraadt   213:                perr2("Cannot open lockfile ", _PATH_LOCKFILE);
                    214:
                    215:        lock.l_type = F_WRLCK;
                    216:        lock.l_whence = SEEK_SET;
                    217:        lock.l_start = 0;
                    218:        lock.l_len = 0;
                    219:
                    220:        act.sa_handler = alarmc;
                    221:        sigemptyset(&(act.sa_mask));
                    222:        act.sa_flags = 0;
                    223:
                    224:        /*
                    225:         * Set an alarm so a timeout occurs after ALARMC seconds, in case
                    226:         * something is seriously broken.
                    227:         */
                    228:        sigaction(SIGALRM, &act, NULL);
                    229:        alarm(ALARMC);
                    230:        fcntl(lockdes, F_SETLKW, &lock);
                    231:        alarm(0);
                    232:
1.7       millert   233:        if ((jobno = nextjob()) == EOF)
                    234:            perr("Cannot generate job number");
                    235:
1.8       millert   236:        (void)snprintf(ppos, sizeof(atfile) - (ppos - atfile),
                    237:            "%c%5x%8x", queue, jobno, (unsigned) (runtimer/60));
1.1       deraadt   238:
1.7       millert   239:        for (ap = ppos; *ap != '\0'; ap++)
                    240:                if (*ap == ' ')
                    241:                        *ap = '0';
                    242:
                    243:        if (stat(atfile, &statbuf) != 0)
                    244:                if (errno != ENOENT)
                    245:                        perr2("Cannot access ", _PATH_ATJOBS);
1.1       deraadt   246:
                    247:        /*
                    248:         * Create the file. The x bit is only going to be set after it has
                    249:         * been completely written out, to make sure it is not executed in
                    250:         * the meantime.  To make sure they do not get deleted, turn off
                    251:         * their r bit.  Yes, this is a kluge.
                    252:         */
                    253:        cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
1.8       millert   254:        if ((fdes = open(atfile, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR)) == -1)
1.1       deraadt   255:                perr("Cannot create atjob file");
                    256:
                    257:        if ((fd2 = dup(fdes)) < 0)
                    258:                perr("Error in dup() of job file");
                    259:
1.7       millert   260:        if (fchown(fd2, real_uid, real_gid) != 0)
1.1       deraadt   261:                perr("Cannot give away file");
                    262:
                    263:        PRIV_END
                    264:
                    265:        /*
                    266:         * We've successfully created the file; let's set the flag so it
                    267:         * gets removed in case of an interrupt or error.
                    268:         */
                    269:        fcreated = 1;
                    270:
                    271:        /* Now we can release the lock, so other people can access it */
                    272:        lock.l_type = F_UNLCK;
                    273:        lock.l_whence = SEEK_SET;
                    274:        lock.l_start = 0;
                    275:        lock.l_len = 0;
1.7       millert   276:        (void)fcntl(lockdes, F_SETLKW, &lock);
                    277:        (void)close(lockdes);
1.1       deraadt   278:
                    279:        if ((fp = fdopen(fdes, "w")) == NULL)
                    280:                panic("Cannot reopen atjob file");
                    281:
                    282:        /*
1.5       millert   283:         * Get the userid to mail to, first by trying getlogin(), which reads
                    284:         * /etc/utmp, then from $LOGNAME or $USER, finally from getpwuid().
1.1       deraadt   285:         */
                    286:        mailname = getlogin();
1.5       millert   287:        if (mailname == NULL && (mailname = getenv("LOGNAME")) == NULL)
                    288:                mailname = getenv("USER");
1.1       deraadt   289:
1.7       millert   290:        if ((mailname == NULL) || (mailname[0] == '\0') ||
                    291:            (strlen(mailname) > LOGNAMESIZE) || (getpwnam(mailname) == NULL)) {
                    292:                pass_entry = getpwuid(real_uid);
1.1       deraadt   293:                if (pass_entry != NULL)
                    294:                        mailname = pass_entry->pw_name;
                    295:        }
                    296:
1.13      kstailey  297:        if (atinput != NULL) {
1.1       deraadt   298:                fpin = freopen(atinput, "r", stdin);
                    299:                if (fpin == NULL)
                    300:                        perr("Cannot open input file");
                    301:        }
1.7       millert   302:        (void)fprintf(fp, "#!/bin/sh\n# atrun uid=%u gid=%u\n# mail %*s %d\n",
                    303:            real_uid, real_gid, LOGNAMESIZE, mailname, send_mail);
1.1       deraadt   304:
                    305:        /* Write out the umask at the time of invocation */
1.7       millert   306:        (void)fprintf(fp, "umask %o\n", cmask);
1.1       deraadt   307:
                    308:        /*
                    309:         * Write out the environment. Anything that may look like a special
                    310:         * character to the shell is quoted, except for \n, which is done
                    311:         * with a pair of "'s.  Dont't export the no_export list (such as
                    312:         * TERM or DISPLAY) because we don't want these.
                    313:         */
                    314:        for (atenv = environ; *atenv != NULL; atenv++) {
                    315:                int export = 1;
                    316:                char *eqp;
                    317:
                    318:                eqp = strchr(*atenv, '=');
                    319:                if (ap == NULL)
                    320:                        eqp = *atenv;
                    321:                else {
                    322:                        int i;
                    323:
                    324:                        for (i = 0;i < sizeof(no_export) /
                    325:                            sizeof(no_export[0]); i++) {
                    326:                                export = export
                    327:                                    && (strncmp(*atenv, no_export[i],
                    328:                                        (size_t) (eqp - *atenv)) != 0);
                    329:                        }
                    330:                        eqp++;
                    331:                }
                    332:
                    333:                if (export) {
1.7       millert   334:                        (void)fwrite(*atenv, sizeof(char), eqp - *atenv, fp);
1.1       deraadt   335:                        for (ap = eqp; *ap != '\0'; ap++) {
                    336:                                if (*ap == '\n')
1.7       millert   337:                                        (void)fprintf(fp, "\"\n\"");
1.1       deraadt   338:                                else {
1.7       millert   339:                                        if (!isalnum(*ap)) {
                    340:                                                switch (*ap) {
                    341:                                                case '%': case '/': case '{':
                    342:                                                case '[': case ']': case '=':
                    343:                                                case '}': case '@': case '+':
                    344:                                                case '#': case ',': case '.':
                    345:                                                case ':': case '-': case '_':
                    346:                                                        break;
                    347:                                                default:
                    348:                                                        (void)fputc('\\', fp);
                    349:                                                        break;
                    350:                                                }
                    351:                                        }
                    352:                                        (void)fputc(*ap, fp);
1.1       deraadt   353:                                }
                    354:                        }
1.7       millert   355:                        (void)fputs("; export ", fp);
                    356:                        (void)fwrite(*atenv, sizeof(char), eqp - *atenv - 1, fp);
                    357:                        (void)fputc('\n', fp);
                    358:                }
                    359:        }
                    360:        /*
                    361:         * Cd to the directory at the time and write out all the
                    362:         * commands the user supplies from stdin.
                    363:         */
                    364:        (void)fputs("cd ", fp);
                    365:        for (ap = cwdname(); *ap != '\0'; ap++) {
                    366:                if (*ap == '\n')
                    367:                        fprintf(fp, "\"\n\"");
                    368:                else {
                    369:                        if (*ap != '/' && !isalnum(*ap))
                    370:                                (void)fputc('\\', fp);
1.1       deraadt   371:
1.7       millert   372:                        (void)fputc(*ap, fp);
1.1       deraadt   373:                }
                    374:        }
                    375:        /*
1.7       millert   376:         * Test cd's exit status: die if the original directory has been
                    377:         * removed, become unreadable or whatever.
1.1       deraadt   378:         */
1.7       millert   379:        (void)fprintf(fp, " || {\n\t echo 'Execution directory inaccessible' >&2\n\t exit 1\n}\n");
1.1       deraadt   380:
1.3       millert   381:        if ((ch = getchar()) == EOF)
                    382:                panic("Input error");
                    383:
                    384:        do {
1.7       millert   385:                (void)fputc(ch, fp);
1.3       millert   386:        } while ((ch = getchar()) != EOF);
1.1       deraadt   387:
1.7       millert   388:        (void)fprintf(fp, "\n");
1.1       deraadt   389:        if (ferror(fp))
                    390:                panic("Output error");
                    391:
                    392:        if (ferror(stdin))
                    393:                panic("Input error");
                    394:
1.7       millert   395:        (void)fclose(fp);
1.1       deraadt   396:
                    397:        /*
                    398:         * Set the x bit so that we're ready to start executing
                    399:         */
                    400:        if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
                    401:                perr("Cannot give away file");
                    402:
1.7       millert   403:        (void)close(fd2);
                    404:        (void)fprintf(stderr, "Job %d will be executed using /bin/sh\n", jobno);
1.1       deraadt   405: }
                    406:
                    407: static void
                    408: list_jobs()
                    409: {
                    410:        /*
                    411:         * List all a user's jobs in the queue, by looping through
                    412:         * _PATH_ATJOBS, or everybody's if we are root
                    413:         */
                    414:        struct passwd *pw;
                    415:        DIR *spool;
                    416:        struct dirent *dirent;
                    417:        struct stat buf;
                    418:        struct tm runtime;
                    419:        unsigned long ctm;
                    420:        char queue;
1.7       millert   421:        int jobno;
1.1       deraadt   422:        time_t runtimer;
                    423:        char timestr[TIMESIZE];
                    424:        int first = 1;
                    425:
                    426:        PRIV_START
                    427:
1.7       millert   428:        if (chdir(_PATH_ATJOBS) != 0)
1.1       deraadt   429:                perr2("Cannot change to ", _PATH_ATJOBS);
                    430:
                    431:        if ((spool = opendir(".")) == NULL)
                    432:                perr2("Cannot open ", _PATH_ATJOBS);
                    433:
                    434:        /* Loop over every file in the directory */
                    435:        while ((dirent = readdir(spool)) != NULL) {
                    436:                if (stat(dirent->d_name, &buf) != 0)
                    437:                        perr2("Cannot stat in ", _PATH_ATJOBS);
                    438:
                    439:                /*
                    440:                 * See it's a regular file and has its x bit turned on and
                    441:                 * is the user's
                    442:                 */
                    443:                if (!S_ISREG(buf.st_mode)
                    444:                    || ((buf.st_uid != real_uid) && !(real_uid == 0))
                    445:                    || !(S_IXUSR & buf.st_mode || atverify))
                    446:                        continue;
                    447:
1.7       millert   448:                if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) != 3)
1.1       deraadt   449:                        continue;
                    450:
                    451:                if (atqueue && (queue != atqueue))
                    452:                        continue;
                    453:
                    454:                runtimer = 60 * (time_t) ctm;
                    455:                runtime = *localtime(&runtimer);
                    456:                strftime(timestr, TIMESIZE, "%X %x", &runtime);
                    457:                if (first) {
1.7       millert   458:                        (void)printf("Date\t\t\tOwner\tQueue\tJob#\n");
1.1       deraadt   459:                        first = 0;
                    460:                }
                    461:                pw = getpwuid(buf.st_uid);
                    462:
1.7       millert   463:                (void)printf("%s\t%s\t%c%s\t%d\n",
1.1       deraadt   464:                    timestr,
                    465:                    pw ? pw->pw_name : "???",
                    466:                    queue,
                    467:                    (S_IXUSR & buf.st_mode) ? "" : "(done)",
1.7       millert   468:                    jobno);
1.1       deraadt   469:        }
                    470:        PRIV_END
                    471: }
                    472:
                    473: static void
1.7       millert   474: process_jobs(argc, argv, what)
1.1       deraadt   475:        int argc;
                    476:        char **argv;
1.7       millert   477:        int what;
1.1       deraadt   478: {
                    479:        /* Delete every argument (job - ID) given */
                    480:        int i;
                    481:        struct stat buf;
1.7       millert   482:        DIR *spool;
                    483:        struct dirent *dirent;
                    484:        unsigned long ctm;
                    485:        char queue;
                    486:        int jobno;
1.9       millert   487:
1.1       deraadt   488:        PRIV_START
                    489:
1.7       millert   490:        if (chdir(_PATH_ATJOBS) != 0)
1.1       deraadt   491:                perr2("Cannot change to ", _PATH_ATJOBS);
                    492:
1.7       millert   493:        if ((spool = opendir(".")) == NULL)
                    494:                perr2("Cannot open ", _PATH_ATJOBS);
                    495:
                    496:        PRIV_END
                    497:
                    498:        /* Loop over every file in the directory */
                    499:        while((dirent = readdir(spool)) != NULL) {
                    500:
                    501:                PRIV_START
                    502:                if (stat(dirent->d_name, &buf) != 0)
                    503:                        perr2("Cannot stat in ", _PATH_ATJOBS);
                    504:                PRIV_END
                    505:
                    506:                if (sscanf(dirent->d_name, "%c%5x%8lx", &queue, &jobno, &ctm) !=3)
                    507:                        continue;
                    508:
                    509:                for (i = optind; i < argc; i++) {
                    510:                        if (atoi(argv[i]) == jobno) {
1.16    ! mickey    511:                                if ((buf.st_uid != real_uid) && !(real_uid == 0))
        !           512:                                        errx(EXIT_FAILURE,
        !           513:                                             "%s: Not owner\n", argv[i]);
1.7       millert   514:                                switch (what) {
                    515:                                case ATRM:
                    516:                                        PRIV_START
                    517:
                    518:                                        if (unlink(dirent->d_name) != 0)
                    519:                                                perr(dirent->d_name);
                    520:
                    521:                                        PRIV_END
                    522:
                    523:                                        break;
                    524:
                    525:                                case CAT:
                    526:                                        {
                    527:                                                FILE *fp;
                    528:                                                int ch;
                    529:
                    530:                                                PRIV_START
                    531:
                    532:                                                fp = fopen(dirent->d_name, "r");
                    533:
                    534:                                                PRIV_END
                    535:
                    536:                                                if (!fp)
                    537:                                                        perr("Cannot open file");
                    538:
                    539:                                                while((ch = getc(fp)) != EOF)
                    540:                                                        putchar(ch);
                    541:                                        }
                    542:                                        break;
                    543:
                    544:                                default:
1.16    ! mickey    545:                                        errx(EXIT_FAILURE,
        !           546:                                            "Internal error, process_jobs = %d",
1.7       millert   547:                                            what);
                    548:                                        break;
                    549:                                }
                    550:                        }
1.1       deraadt   551:                }
                    552:        }
                    553: }                              /* delete_jobs */
                    554:
                    555: /* Global functions */
                    556:
                    557: int
                    558: main(argc, argv)
                    559:        int argc;
                    560:        char **argv;
                    561: {
                    562:        int c;
1.7       millert   563:        char queue = DEFAULT_AT_QUEUE;
                    564:        char queue_set = 0;
1.1       deraadt   565:        char *pgm;
                    566:
                    567:        enum {
1.7       millert   568:                ATQ, ATRM, AT, BATCH, CAT
                    569:        };                              /* what program we want to run */
                    570:        int program = AT;               /* our default program */
                    571:        char *options = "q:f:mvldbVc";  /* default options for at */
                    572:        int disp_version = 0;
1.1       deraadt   573:        time_t timer;
                    574:
                    575:        RELINQUISH_PRIVS
                    576:
                    577:        /* Eat any leading paths */
                    578:        if ((pgm = strrchr(argv[0], '/')) == NULL)
                    579:                pgm = argv[0];
                    580:        else
                    581:                pgm++;
                    582:
                    583:        namep = pgm;
                    584:
                    585:        /* find out what this program is supposed to do */
                    586:        if (strcmp(pgm, "atq") == 0) {
                    587:                program = ATQ;
1.7       millert   588:                options = "q:vV";
1.1       deraadt   589:        } else if (strcmp(pgm, "atrm") == 0) {
                    590:                program = ATRM;
1.7       millert   591:                options = "V";
1.1       deraadt   592:        } else if (strcmp(pgm, "batch") == 0) {
                    593:                program = BATCH;
1.7       millert   594:                options = "f:q:mvV";
1.1       deraadt   595:        }
                    596:
                    597:        /* process whatever options we can process */
                    598:        opterr = 1;
1.6       millert   599:        while ((c = getopt(argc, argv, options)) != -1)
1.1       deraadt   600:                switch (c) {
                    601:                case 'v':       /* verify time settings */
                    602:                        atverify = 1;
                    603:                        break;
                    604:
                    605:                case 'm':       /* send mail when job is complete */
                    606:                        send_mail = 1;
                    607:                        break;
                    608:
                    609:                case 'f':
                    610:                        atinput = optarg;
                    611:                        break;
                    612:
                    613:                case 'q':       /* specify queue */
                    614:                        if (strlen(optarg) > 1)
                    615:                                usage();
                    616:
                    617:                        atqueue = queue = *optarg;
1.7       millert   618:                        if (!(islower(queue) || isupper(queue)))
1.1       deraadt   619:                                usage();
1.7       millert   620:
                    621:                        queue_set = 1;
                    622:                        break;
                    623:
                    624:                case 'd':
                    625:                        if (program != AT)
                    626:                                usage();
                    627:
                    628:                        program = ATRM;
                    629:                        options = "V";
                    630:                        break;
                    631:
                    632:                case 'l':
                    633:                        if (program != AT)
                    634:                                usage();
                    635:
                    636:                        program = ATQ;
                    637:                        options = "q:vV";
                    638:                        break;
                    639:
                    640:                case 'b':
                    641:                        if (program != AT)
                    642:                                usage();
                    643:
                    644:                        program = BATCH;
                    645:                        options = "f:q:mvV";
                    646:                        break;
                    647:
                    648:                case 'V':
                    649:                        disp_version = 1;
                    650:                        break;
                    651:
                    652:                case 'c':
                    653:                        program = CAT;
                    654:                        options = "";
1.1       deraadt   655:                        break;
                    656:
                    657:                default:
                    658:                        usage();
                    659:                        break;
                    660:                }
                    661:        /* end of options eating */
                    662:
1.7       millert   663:        if (disp_version)
                    664:                (void)fprintf(stderr, "%s version %.1f\n", namep, AT_VERSION);
                    665:
1.16    ! mickey    666:        if (!check_permission())
        !           667:                errx(EXIT_FAILURE, "You do not have permission to use %s.",
        !           668:                     namep);
1.7       millert   669:
1.1       deraadt   670:        /* select our program */
                    671:        switch (program) {
                    672:        case ATQ:
1.10      millert   673:                if (optind != argc)
                    674:                        usage();
1.1       deraadt   675:                list_jobs();
                    676:                break;
                    677:
                    678:        case ATRM:
1.7       millert   679:        case CAT:
1.10      millert   680:                if (optind == argc)
                    681:                        usage();
1.11      millert   682:                process_jobs(argc, argv, program);
1.1       deraadt   683:                break;
                    684:
                    685:        case AT:
                    686:                timer = parsetime(argc, argv);
                    687:                if (atverify) {
                    688:                        struct tm *tm = localtime(&timer);
1.7       millert   689:                        (void)fprintf(stderr, "%s\n", asctime(tm));
1.1       deraadt   690:                }
                    691:                writefile(timer, queue);
                    692:                break;
                    693:
                    694:        case BATCH:
1.7       millert   695:                if (queue_set)
                    696:                        queue = toupper(queue);
                    697:                else
                    698:                        queue = DEFAULT_BATCH_QUEUE;
                    699:
                    700:                if (argc > optind)
                    701:                        timer = parsetime(argc, argv);
                    702:                else
                    703:                        timer = time(NULL);
                    704:
                    705:                if (atverify) {
                    706:                        struct tm *tm = localtime(&timer);
                    707:                        (void)fprintf(stderr, "%s\n", asctime(tm));
                    708:                }
                    709:
                    710:                writefile(timer, queue);
1.1       deraadt   711:                break;
                    712:
                    713:        default:
                    714:                panic("Internal error");
                    715:                break;
                    716:        }
                    717:        exit(EXIT_SUCCESS);
                    718: }