Annotation of src/usr.bin/at/at.c, Revision 1.66
1.66 ! deraadt 1: /* $OpenBSD: at.c,v 1.65 2015/08/20 22:32:41 deraadt Exp $ */
1.1 deraadt 2:
3: /*
1.7 millert 4: * at.c : Put file into atrun queue
5: * Copyright (C) 1993, 1994 Thomas Koenig
1.1 deraadt 6: *
1.7 millert 7: * Atrun & Atq modifications
8: * Copyright (C) 1993 David Parsons
1.1 deraadt 9: *
1.29 millert 10: * Traditional BSD behavior and other significant modifications
1.35 millert 11: * Copyright (C) 2002-2003 Todd C. Miller
1.29 millert 12: *
1.1 deraadt 13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. The name of the author(s) may not be used to endorse or promote
19: * products derived from this software without specific prior written
20: * permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1.7 millert 25: * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1.1 deraadt 26: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1.57 krw 29: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1.1 deraadt 30: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
33:
1.35 millert 34: #define MAIN_PROGRAM
1.1 deraadt 35:
1.35 millert 36: #include "cron.h"
1.1 deraadt 37: #include "at.h"
38: #include "privs.h"
1.35 millert 39: #include <limits.h>
1.1 deraadt 40:
41: #define ALARMC 10 /* Number of seconds to wait for timeout */
1.29 millert 42: #define TIMESIZE 50 /* Size of buffer passed to strftime() */
1.1 deraadt 43:
1.29 millert 44: /* Variables to remove from the job's environment. */
1.1 deraadt 45: char *no_export[] =
46: {
1.28 millert 47: "TERM", "TERMCAP", "DISPLAY", "_", "SHELLOPTS", "BASH_VERSINFO",
48: "EUID", "GROUPS", "PPID", "UID", "SSH_AUTH_SOCK", "SSH_AGENT_PID",
1.1 deraadt 49: };
1.7 millert 50:
1.27 millert 51: int program = AT; /* default program mode */
1.35 millert 52: char atfile[MAX_FNAME]; /* path to the at spool file */
1.29 millert 53: int fcreated; /* whether or not we created the file yet */
1.1 deraadt 54: char atqueue = 0; /* which queue to examine for jobs (atq) */
1.29 millert 55: char vflag = 0; /* show completed but unremoved jobs (atq) */
56: char force = 0; /* suppress errors (atrm) */
57: char interactive = 0; /* interactive mode (atrm) */
58: static int send_mail = 0; /* whether we are sending mail */
1.7 millert 59:
1.21 millert 60: static void sigc(int);
61: static void alarmc(int);
1.35 millert 62: static void writefile(const char *, time_t, char);
1.29 millert 63: static void list_jobs(int, char **, int, int);
1.48 millert 64: static time_t ttime(char *);
1.35 millert 65: static int check_permission(void);
1.46 cloder 66: static __dead void panic(const char *);
1.35 millert 67: static void perr(const char *);
68: static void perr2(const char *, const char *);
1.41 millert 69: static __dead void usage(void);
1.61 deraadt 70: static int rmok(long long);
1.35 millert 71: time_t parsetime(int, char **);
72:
73: /*
74: * Something fatal has happened, print error message and exit.
75: */
76: static __dead void
77: panic(const char *a)
78: {
79: (void)fprintf(stderr, "%s: %s\n", ProgramName, a);
80: if (fcreated) {
81: PRIV_START;
82: unlink(atfile);
83: PRIV_END;
84: }
85:
1.59 millert 86: exit(EXIT_FAILURE);
1.35 millert 87: }
88:
89: /*
90: * Two-parameter version of panic().
91: */
1.42 millert 92: static __dead void
1.35 millert 93: panic2(const char *a, const char *b)
94: {
95: (void)fprintf(stderr, "%s: %s%s\n", ProgramName, a, b);
96: if (fcreated) {
97: PRIV_START;
98: unlink(atfile);
99: PRIV_END;
100: }
101:
1.59 millert 102: exit(EXIT_FAILURE);
1.35 millert 103: }
104:
105: /*
106: * Some operating system error; print error message and exit.
107: */
108: static __dead void
109: perr(const char *a)
110: {
111: if (!force)
112: perror(a);
113: if (fcreated) {
114: PRIV_START;
115: unlink(atfile);
116: PRIV_END;
117: }
118:
1.59 millert 119: exit(EXIT_FAILURE);
1.35 millert 120: }
121:
122: /*
123: * Two-parameter version of perr().
124: */
1.42 millert 125: static __dead void
1.35 millert 126: perr2(const char *a, const char *b)
127: {
128: if (!force)
129: (void)fputs(a, stderr);
130: perr(b);
131: }
1.1 deraadt 132:
1.46 cloder 133: /* ARGSUSED */
1.42 millert 134: static void
1.26 millert 135: sigc(int signo)
1.1 deraadt 136: {
1.7 millert 137: /* If the user presses ^C, remove the spool file and exit. */
1.1 deraadt 138: if (fcreated) {
1.26 millert 139: PRIV_START;
1.7 millert 140: (void)unlink(atfile);
1.26 millert 141: PRIV_END;
1.1 deraadt 142: }
143:
1.59 millert 144: _exit(EXIT_FAILURE);
1.1 deraadt 145: }
146:
1.46 cloder 147: /* ARGSUSED */
1.42 millert 148: static void
1.26 millert 149: alarmc(int signo)
1.1 deraadt 150: {
1.35 millert 151: /* just return */
1.1 deraadt 152: }
153:
1.29 millert 154: static int
155: newjob(time_t runtimer, int queue)
156: {
157: int fd, i;
1.1 deraadt 158:
1.7 millert 159: /*
1.29 millert 160: * If we have a collision, try shifting the time by up to
161: * two minutes. Perhaps it would be better to try different
162: * queues instead...
1.7 millert 163: */
1.29 millert 164: for (i = 0; i < 120; i++) {
1.61 deraadt 165: snprintf(atfile, sizeof(atfile), "%s/%lld.%c", AT_DIR,
166: (long long)runtimer, queue);
1.29 millert 167: fd = open(atfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
168: if (fd >= 0)
169: return (fd);
1.31 millert 170: runtimer++;
1.29 millert 171: }
172: return (-1);
1.1 deraadt 173: }
174:
1.29 millert 175: /*
176: * This does most of the work if at or batch are invoked for
177: * writing a job.
178: */
1.1 deraadt 179: static void
1.35 millert 180: writefile(const char *cwd, time_t runtimer, char queue)
1.1 deraadt 181: {
1.35 millert 182: const char *ap;
183: char *mailname, *shell;
1.28 millert 184: char timestr[TIMESIZE];
1.1 deraadt 185: struct passwd *pass_entry;
1.28 millert 186: struct tm runtime;
1.1 deraadt 187: int fdes, lockdes, fd2;
1.55 deraadt 188: FILE *fp;
1.1 deraadt 189: struct sigaction act;
190: char **atenv;
191: int ch;
192: mode_t cmask;
1.29 millert 193: extern char **environ;
1.1 deraadt 194:
1.7 millert 195: (void)setlocale(LC_TIME, "");
196:
1.1 deraadt 197: /*
198: * Install the signal handler for SIGINT; terminate after removing the
199: * spool file if necessary
200: */
1.35 millert 201: bzero(&act, sizeof act);
1.1 deraadt 202: act.sa_handler = sigc;
1.29 millert 203: sigemptyset(&act.sa_mask);
1.1 deraadt 204: act.sa_flags = 0;
205: sigaction(SIGINT, &act, NULL);
206:
1.26 millert 207: PRIV_START;
1.1 deraadt 208:
1.35 millert 209: if ((lockdes = open(AT_DIR, O_RDONLY, 0)) < 0)
210: perr("Cannot open jobs dir");
211:
1.22 millert 212: /*
1.29 millert 213: * Lock the jobs dir so we don't have to worry about someone
214: * else grabbing a file name out from under us.
1.22 millert 215: * Set an alarm so we don't sleep forever waiting on the lock.
216: * If we don't succeed with ALARMC seconds, something is wrong...
217: */
1.35 millert 218: bzero(&act, sizeof act);
1.1 deraadt 219: act.sa_handler = alarmc;
1.29 millert 220: sigemptyset(&act.sa_mask);
1.35 millert 221: #ifdef SA_INTERRUPT
222: act.sa_flags = SA_INTERRUPT;
223: #endif
1.1 deraadt 224: sigaction(SIGALRM, &act, NULL);
225: alarm(ALARMC);
1.35 millert 226: ch = flock(lockdes, LOCK_EX);
1.1 deraadt 227: alarm(0);
1.35 millert 228: if (ch != 0)
229: panic("Unable to lock jobs dir");
1.22 millert 230:
1.1 deraadt 231: /*
232: * Create the file. The x bit is only going to be set after it has
233: * been completely written out, to make sure it is not executed in
234: * the meantime. To make sure they do not get deleted, turn off
235: * their r bit. Yes, this is a kluge.
236: */
237: cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
1.29 millert 238: if ((fdes = newjob(runtimer, queue)) == -1)
1.1 deraadt 239: perr("Cannot create atjob file");
240:
241: if ((fd2 = dup(fdes)) < 0)
242: perr("Error in dup() of job file");
243:
1.7 millert 244: if (fchown(fd2, real_uid, real_gid) != 0)
1.1 deraadt 245: perr("Cannot give away file");
246:
1.26 millert 247: PRIV_END;
1.1 deraadt 248:
249: /*
250: * We've successfully created the file; let's set the flag so it
251: * gets removed in case of an interrupt or error.
252: */
253: fcreated = 1;
254:
255: /* Now we can release the lock, so other people can access it */
1.7 millert 256: (void)close(lockdes);
1.1 deraadt 257:
258: if ((fp = fdopen(fdes, "w")) == NULL)
259: panic("Cannot reopen atjob file");
260:
261: /*
1.18 millert 262: * Get the userid to mail to, first by trying getlogin(), which asks
263: * the kernel, then from $LOGNAME or $USER, finally from getpwuid().
1.1 deraadt 264: */
265: mailname = getlogin();
1.5 millert 266: if (mailname == NULL && (mailname = getenv("LOGNAME")) == NULL)
267: mailname = getenv("USER");
1.1 deraadt 268:
1.7 millert 269: if ((mailname == NULL) || (mailname[0] == '\0') ||
1.35 millert 270: (strlen(mailname) > MAX_UNAME) || (getpwnam(mailname) == NULL)) {
1.7 millert 271: pass_entry = getpwuid(real_uid);
1.1 deraadt 272: if (pass_entry != NULL)
273: mailname = pass_entry->pw_name;
274: }
275:
1.28 millert 276: /*
277: * Get the shell to run the job under. First check $SHELL, falling
278: * back to the user's shell in the password database or, failing
279: * that, /bin/sh.
280: */
281: if ((shell = getenv("SHELL")) == NULL || *shell == '\0') {
282: pass_entry = getpwuid(real_uid);
283: if (pass_entry != NULL && *pass_entry->pw_shell != '\0')
284: shell = pass_entry->pw_shell;
285: else
286: shell = _PATH_BSHELL;
287: }
288:
1.42 millert 289: (void)fprintf(fp, "#!/bin/sh\n# atrun uid=%lu gid=%lu\n# mail %*s %d\n",
290: (unsigned long)real_uid, (unsigned long)real_gid,
291: MAX_UNAME, mailname, send_mail);
1.1 deraadt 292:
293: /* Write out the umask at the time of invocation */
1.7 millert 294: (void)fprintf(fp, "umask %o\n", cmask);
1.1 deraadt 295:
296: /*
297: * Write out the environment. Anything that may look like a special
298: * character to the shell is quoted, except for \n, which is done
1.44 jmc 299: * with a pair of "'s. Don't export the no_export list (such as
1.1 deraadt 300: * TERM or DISPLAY) because we don't want these.
301: */
302: for (atenv = environ; *atenv != NULL; atenv++) {
303: int export = 1;
304: char *eqp;
305:
306: eqp = strchr(*atenv, '=');
1.19 millert 307: if (eqp == NULL)
1.1 deraadt 308: eqp = *atenv;
309: else {
310: int i;
311:
312: for (i = 0;i < sizeof(no_export) /
313: sizeof(no_export[0]); i++) {
314: export = export
315: && (strncmp(*atenv, no_export[i],
316: (size_t) (eqp - *atenv)) != 0);
317: }
318: eqp++;
319: }
320:
321: if (export) {
1.63 millert 322: (void)fputs("export ", fp);
1.7 millert 323: (void)fwrite(*atenv, sizeof(char), eqp - *atenv, fp);
1.1 deraadt 324: for (ap = eqp; *ap != '\0'; ap++) {
325: if (*ap == '\n')
1.7 millert 326: (void)fprintf(fp, "\"\n\"");
1.1 deraadt 327: else {
1.62 deraadt 328: if (!isalnum((unsigned char)*ap)) {
1.7 millert 329: switch (*ap) {
330: case '%': case '/': case '{':
331: case '[': case ']': case '=':
332: case '}': case '@': case '+':
333: case '#': case ',': case '.':
334: case ':': case '-': case '_':
335: break;
336: default:
337: (void)fputc('\\', fp);
338: break;
339: }
340: }
341: (void)fputc(*ap, fp);
1.1 deraadt 342: }
343: }
1.7 millert 344: (void)fputc('\n', fp);
345: }
346: }
347: /*
348: * Cd to the directory at the time and write out all the
349: * commands the user supplies from stdin.
350: */
351: (void)fputs("cd ", fp);
1.35 millert 352: for (ap = cwd; *ap != '\0'; ap++) {
1.7 millert 353: if (*ap == '\n')
354: fprintf(fp, "\"\n\"");
355: else {
1.62 deraadt 356: if (*ap != '/' && !isalnum((unsigned char)*ap))
1.7 millert 357: (void)fputc('\\', fp);
1.1 deraadt 358:
1.7 millert 359: (void)fputc(*ap, fp);
1.1 deraadt 360: }
361: }
362: /*
1.7 millert 363: * Test cd's exit status: die if the original directory has been
364: * removed, become unreadable or whatever.
1.1 deraadt 365: */
1.29 millert 366: (void)fprintf(fp, " || {\n\t echo 'Execution directory inaccessible'"
367: " >&2\n\t exit 1\n}\n");
1.1 deraadt 368:
1.3 millert 369: if ((ch = getchar()) == EOF)
370: panic("Input error");
371:
1.28 millert 372: /* We want the job to run under the user's shell. */
373: fprintf(fp, "%s << '_END_OF_AT_JOB'\n", shell);
374:
1.3 millert 375: do {
1.7 millert 376: (void)fputc(ch, fp);
1.3 millert 377: } while ((ch = getchar()) != EOF);
1.1 deraadt 378:
1.28 millert 379: (void)fprintf(fp, "\n_END_OF_AT_JOB\n");
1.1 deraadt 380: if (ferror(fp))
381: panic("Output error");
382:
383: if (ferror(stdin))
384: panic("Input error");
385:
1.7 millert 386: (void)fclose(fp);
1.1 deraadt 387:
388: /*
389: * Set the x bit so that we're ready to start executing
390: */
391: if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
392: perr("Cannot give away file");
393:
1.7 millert 394: (void)close(fd2);
1.28 millert 395:
1.30 millert 396: /* Poke cron so it knows to reload the at spool. */
1.35 millert 397: PRIV_START;
398: poke_daemon(AT_DIR, RELOAD_AT);
399: PRIV_END;
1.30 millert 400:
1.28 millert 401: runtime = *localtime(&runtimer);
402: strftime(timestr, TIMESIZE, "%a %b %e %T %Y", &runtime);
403: (void)fprintf(stderr, "commands will be executed using %s\n", shell);
1.35 millert 404: (void)fprintf(stderr, "job %s at %s\n", &atfile[sizeof(AT_DIR)],
1.29 millert 405: timestr);
406: }
407:
408: /* Sort by creation time. */
409: static int
410: byctime(const void *v1, const void *v2)
411: {
1.46 cloder 412: const struct atjob *j1 = *(const struct atjob **)v1;
413: const struct atjob *j2 = *(const struct atjob **)v2;
1.29 millert 414:
415: return (j1->ctime - j2->ctime);
416: }
417:
418: /* Sort by job number (and thus execution time). */
419: static int
420: byjobno(const void *v1, const void *v2)
421: {
422: const struct atjob *j1 = *(struct atjob **)v1;
423: const struct atjob *j2 = *(struct atjob **)v2;
424:
425: if (j1->runtimer == j2->runtimer)
426: return (j1->queue - j2->queue);
427: return (j1->runtimer - j2->runtimer);
428: }
429:
430: static void
1.37 millert 431: print_job(struct atjob *job, int n, int shortformat)
1.29 millert 432: {
433: struct passwd *pw;
434: struct tm runtime;
435: char timestr[TIMESIZE];
436: static char *ranks[] = {
437: "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
438: };
439:
440: runtime = *localtime(&job->runtimer);
441: if (shortformat) {
442: strftime(timestr, TIMESIZE, "%a %b %e %T %Y", &runtime);
1.61 deraadt 443: (void)printf("%lld.%c\t%s\n", (long long)job->runtimer,
1.29 millert 444: job->queue, timestr);
445: } else {
1.37 millert 446: pw = getpwuid(job->uid);
1.29 millert 447: /* Rank hack shamelessly stolen from lpq */
448: if (n / 10 == 1)
449: printf("%3d%-5s", n,"th");
450: else
451: printf("%3d%-5s", n, ranks[n % 10]);
452: strftime(timestr, TIMESIZE, "%b %e, %Y %R", &runtime);
1.61 deraadt 453: (void)printf("%-21.18s%-11.8s%10lld.%c %c%s\n",
1.29 millert 454: timestr, pw ? pw->pw_name : "???",
1.61 deraadt 455: (long long)job->runtimer, job->queue, job->queue,
1.37 millert 456: (S_IXUSR & job->mode) ? "" : " (done)");
1.29 millert 457: }
1.1 deraadt 458: }
459:
1.29 millert 460: /*
461: * List all of a user's jobs in the queue, by looping through
1.35 millert 462: * AT_DIR, or all jobs if we are root. If argc is > 0, argv
1.29 millert 463: * contains the list of users whose jobs shall be displayed. By
464: * default, the list is sorted by execution date and queue. If
465: * csort is non-zero jobs will be sorted by creation/submission date.
466: */
1.1 deraadt 467: static void
1.29 millert 468: list_jobs(int argc, char **argv, int count_only, int csort)
1.1 deraadt 469: {
470: struct passwd *pw;
471: struct dirent *dirent;
1.40 tedu 472: struct atjob **atjobs, **newatjobs, *job;
1.29 millert 473: struct stat stbuf;
1.1 deraadt 474: time_t runtimer;
1.29 millert 475: uid_t *uids;
476: char queue, *ep;
477: DIR *spool;
1.54 moritz 478: int i, shortformat;
479: size_t numjobs, maxjobs;
1.29 millert 480:
481: if (argc) {
1.53 deraadt 482: if ((uids = calloc(sizeof(uid_t), argc)) == NULL)
1.35 millert 483: panic("Insufficient virtual memory");
1.29 millert 484:
485: for (i = 0; i < argc; i++) {
486: if ((pw = getpwnam(argv[i])) == NULL)
1.35 millert 487: panic2(argv[i], ": invalid user name");
1.29 millert 488: if (pw->pw_uid != real_uid && real_uid != 0)
1.35 millert 489: panic("Only the superuser may display other users' jobs");
1.29 millert 490: uids[i] = pw->pw_uid;
491: }
492: } else
493: uids = NULL;
494:
1.35 millert 495: shortformat = strcmp(ProgramName, "at") == 0;
1.1 deraadt 496:
1.26 millert 497: PRIV_START;
1.1 deraadt 498:
1.35 millert 499: if (chdir(AT_DIR) != 0)
500: perr2("Cannot change to ", AT_DIR);
1.1 deraadt 501:
502: if ((spool = opendir(".")) == NULL)
1.35 millert 503: perr2("Cannot open ", AT_DIR);
1.1 deraadt 504:
1.29 millert 505: PRIV_END;
506:
1.58 millert 507: if (fstat(dirfd(spool), &stbuf) != 0)
1.35 millert 508: perr2("Cannot stat ", AT_DIR);
1.29 millert 509:
510: /*
511: * The directory's link count should give us a good idea
512: * of how many files are in it. Fudge things a little just
513: * in case someone adds a job or two.
514: */
515: numjobs = 0;
516: maxjobs = stbuf.st_nlink + 4;
1.65 deraadt 517: atjobs = calloc(maxjobs, sizeof(struct atjob *));
1.29 millert 518: if (atjobs == NULL)
1.35 millert 519: panic("Insufficient virtual memory");
1.29 millert 520:
521: /* Loop over every file in the directory. */
1.1 deraadt 522: while ((dirent = readdir(spool)) != NULL) {
1.29 millert 523: PRIV_START;
524:
525: if (stat(dirent->d_name, &stbuf) != 0)
1.35 millert 526: perr2("Cannot stat in ", AT_DIR);
1.1 deraadt 527:
1.29 millert 528: PRIV_END;
529:
1.1 deraadt 530: /*
531: * See it's a regular file and has its x bit turned on and
532: * is the user's
533: */
1.29 millert 534: if (!S_ISREG(stbuf.st_mode)
535: || ((stbuf.st_uid != real_uid) && !(real_uid == 0))
536: || !(S_IXUSR & stbuf.st_mode || vflag))
1.1 deraadt 537: continue;
538:
1.61 deraadt 539: if (strtot(dirent->d_name, &ep, &runtimer) == -1)
540: continue;
1.62 deraadt 541: if (*ep != '.' || !isalpha((unsigned char)*(ep + 1)) ||
542: *(ep + 2) != '\0')
1.1 deraadt 543: continue;
1.29 millert 544: queue = *(ep + 1);
1.1 deraadt 545:
546: if (atqueue && (queue != atqueue))
547: continue;
548:
1.29 millert 549: /* Check against specified user(s). */
550: if (argc) {
551: for (i = 0; i < argc; i++) {
552: if (uids[0] == stbuf.st_uid)
553: break;
554: }
555: if (i == argc)
556: continue; /* user doesn't match */
557: }
558:
559: if (count_only) {
560: numjobs++;
561: continue;
562: }
563:
1.65 deraadt 564: job = malloc(sizeof(struct atjob));
1.29 millert 565: if (job == NULL)
1.35 millert 566: panic("Insufficient virtual memory");
1.29 millert 567: job->runtimer = runtimer;
568: job->ctime = stbuf.st_ctime;
1.37 millert 569: job->uid = stbuf.st_uid;
570: job->mode = stbuf.st_mode;
1.29 millert 571: job->queue = queue;
572: if (numjobs == maxjobs) {
1.54 moritz 573: size_t newjobs = maxjobs * 2;
1.64 doug 574: newatjobs = reallocarray(atjobs, newjobs, sizeof(job));
1.40 tedu 575: if (newatjobs == NULL)
1.35 millert 576: panic("Insufficient virtual memory");
1.40 tedu 577: atjobs = newatjobs;
578: maxjobs = newjobs;
1.29 millert 579: }
580: atjobs[numjobs++] = job;
581: }
582: free(uids);
1.45 robert 583: closedir(spool);
1.29 millert 584:
585: if (count_only || numjobs == 0) {
586: if (numjobs == 0 && !shortformat)
587: fprintf(stderr, "no files in queue.\n");
588: else if (count_only)
1.54 moritz 589: printf("%zu\n", numjobs);
1.29 millert 590: free(atjobs);
591: return;
592: }
593:
594: /* Sort by job run time or by job creation time. */
595: qsort(atjobs, numjobs, sizeof(struct atjob *),
596: csort ? byctime : byjobno);
597:
598: if (!shortformat)
599: (void)puts(" Rank Execution Date Owner "
600: "Job Queue");
601:
602: for (i = 0; i < numjobs; i++) {
1.37 millert 603: print_job(atjobs[i], i + 1, shortformat);
1.29 millert 604: free(atjobs[i]);
1.1 deraadt 605: }
1.29 millert 606: free(atjobs);
607: }
608:
609: static int
1.61 deraadt 610: rmok(long long job)
1.29 millert 611: {
612: int ch, junk;
613:
1.61 deraadt 614: printf("%lld: remove it? ", job);
1.29 millert 615: ch = getchar();
616: while ((junk = getchar()) != EOF && junk != '\n')
617: ;
618: return (ch == 'y' || ch == 'Y');
1.1 deraadt 619: }
620:
1.29 millert 621: /*
1.35 millert 622: * Loop through all jobs in AT_DIR and display or delete ones
1.29 millert 623: * that match argv (may be job or username), or all if argc == 0.
624: * Only the superuser may display/delete other people's jobs.
625: */
1.28 millert 626: static int
1.26 millert 627: process_jobs(int argc, char **argv, int what)
1.1 deraadt 628: {
1.29 millert 629: struct stat stbuf;
630: struct dirent *dirent;
631: struct passwd *pw;
632: time_t runtimer;
633: uid_t *uids;
1.46 cloder 634: char **jobs, *ep;
1.29 millert 635: long l;
636: FILE *fp;
1.7 millert 637: DIR *spool;
1.29 millert 638: int job_matches, jobs_len, uids_len;
1.30 millert 639: int error, i, ch, changed;
1.9 millert 640:
1.26 millert 641: PRIV_START;
1.1 deraadt 642:
1.35 millert 643: if (chdir(AT_DIR) != 0)
644: perr2("Cannot change to ", AT_DIR);
1.1 deraadt 645:
1.7 millert 646: if ((spool = opendir(".")) == NULL)
1.35 millert 647: perr2("Cannot open ", AT_DIR);
1.7 millert 648:
1.26 millert 649: PRIV_END;
1.7 millert 650:
1.29 millert 651: /* Convert argv into a list of jobs and uids. */
652: jobs = NULL;
653: uids = NULL;
654: jobs_len = uids_len = 0;
655: if (argc > 0) {
1.53 deraadt 656: if ((jobs = calloc(sizeof(char *), argc)) == NULL ||
657: (uids = calloc(sizeof(uid_t), argc)) == NULL)
1.35 millert 658: panic("Insufficient virtual memory");
1.29 millert 659:
660: for (i = 0; i < argc; i++) {
661: l = strtol(argv[i], &ep, 10);
1.62 deraadt 662: if (*ep == '.' && isalpha((unsigned char)*(ep + 1)) &&
1.29 millert 663: *(ep + 2) == '\0' && l > 0 && l < INT_MAX)
664: jobs[jobs_len++] = argv[i];
665: else if ((pw = getpwnam(argv[i])) != NULL) {
1.35 millert 666: if (real_uid != pw->pw_uid && real_uid != 0) {
667: fprintf(stderr, "%s: Only the superuser"
1.39 mpech 668: " may %s other users' jobs\n",
1.35 millert 669: ProgramName, what == ATRM
670: ? "remove" : "view");
1.59 millert 671: exit(EXIT_FAILURE);
1.35 millert 672: }
1.29 millert 673: uids[uids_len++] = pw->pw_uid;
674: } else
1.35 millert 675: panic2(argv[i], ": invalid user name");
1.29 millert 676: }
677: }
678:
1.7 millert 679: /* Loop over every file in the directory */
1.30 millert 680: changed = 0;
1.28 millert 681: while ((dirent = readdir(spool)) != NULL) {
1.7 millert 682:
1.26 millert 683: PRIV_START;
1.29 millert 684: if (stat(dirent->d_name, &stbuf) != 0)
1.35 millert 685: perr2("Cannot stat in ", AT_DIR);
1.26 millert 686: PRIV_END;
1.7 millert 687:
1.29 millert 688: if (stbuf.st_uid != real_uid && real_uid != 0)
1.7 millert 689: continue;
690:
1.61 deraadt 691: if (strtot(dirent->d_name, &ep, &runtimer) == -1)
692: continue;
1.62 deraadt 693: if (*ep != '.' || !isalpha((unsigned char)*(ep + 1)) ||
694: *(ep + 2) != '\0')
1.29 millert 695: continue;
1.7 millert 696:
1.29 millert 697: /* Check runtimer against argv; argc==0 means do all. */
698: job_matches = (argc == 0) ? 1 : 0;
699: if (!job_matches) {
700: for (i = 0; i < jobs_len; i++) {
1.36 millert 701: if (jobs[i] != NULL &&
702: strcmp(dirent->d_name, jobs[i]) == 0) {
1.29 millert 703: jobs[i] = NULL;
704: job_matches = 1;
705: break;
706: }
707: }
708: }
709: if (!job_matches) {
710: for (i = 0; i < uids_len; i++) {
711: if (uids[i] == stbuf.st_uid) {
712: job_matches = 1;
713: break;
714: }
715: }
716: }
717:
718: if (job_matches) {
719: switch (what) {
720: case ATRM:
721: PRIV_START;
722:
723: if (!interactive ||
724: (interactive && rmok(runtimer))) {
1.30 millert 725: if (unlink(dirent->d_name) == 0)
726: changed = 1;
727: else
1.7 millert 728: perr(dirent->d_name);
1.29 millert 729: if (!force && !interactive)
730: fprintf(stderr,
731: "%s removed\n",
732: dirent->d_name);
733: }
1.7 millert 734:
1.29 millert 735: PRIV_END;
1.7 millert 736:
1.29 millert 737: break;
1.7 millert 738:
1.29 millert 739: case CAT:
740: PRIV_START;
1.7 millert 741:
1.29 millert 742: fp = fopen(dirent->d_name, "r");
1.7 millert 743:
1.29 millert 744: PRIV_END;
1.7 millert 745:
1.29 millert 746: if (!fp)
747: perr("Cannot open file");
1.7 millert 748:
1.29 millert 749: while ((ch = getc(fp)) != EOF)
750: putchar(ch);
1.7 millert 751:
1.45 robert 752: fclose(fp);
1.29 millert 753: break;
1.7 millert 754:
1.29 millert 755: default:
1.35 millert 756: panic("Internal error");
1.29 millert 757: break;
1.7 millert 758: }
1.1 deraadt 759: }
760: }
1.45 robert 761: closedir(spool);
762:
1.29 millert 763: for (error = 0, i = 0; i < jobs_len; i++) {
764: if (jobs[i] != NULL) {
765: if (!force)
1.39 mpech 766: fprintf(stderr, "%s: %s: no such job\n",
1.35 millert 767: ProgramName, jobs[i]);
1.28 millert 768: error++;
769: }
770: }
1.29 millert 771: free(jobs);
772: free(uids);
773:
1.30 millert 774: /* If we modied the spool, poke cron so it knows to reload. */
1.35 millert 775: if (changed) {
776: PRIV_START;
777: if (chdir(CRONDIR) != 0)
778: perror(CRONDIR);
779: else
780: poke_daemon(AT_DIR, RELOAD_AT);
781: PRIV_END;
782: }
1.30 millert 783:
1.29 millert 784: return (error);
1.28 millert 785: }
1.1 deraadt 786:
1.25 millert 787: #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
788:
1.29 millert 789: /*
1.48 millert 790: * Adapted from date(1)
1.29 millert 791: */
1.25 millert 792: static time_t
1.48 millert 793: ttime(char *arg)
1.25 millert 794: {
1.48 millert 795: time_t now, then;
796: struct tm *lt;
1.25 millert 797: int yearset;
1.48 millert 798: char *dot, *p;
1.42 millert 799:
1.48 millert 800: if (time(&now) == (time_t)-1 || (lt = localtime(&now)) == NULL)
1.25 millert 801: panic("Cannot get current time");
1.42 millert 802:
1.48 millert 803: /* Valid date format is [[CC]YY]MMDDhhmm[.SS] */
804: for (p = arg, dot = NULL; *p != '\0'; p++) {
1.52 millert 805: if (*p == '.' && dot == NULL)
1.48 millert 806: dot = p;
807: else if (!isdigit((unsigned char)*p))
808: goto terr;
809: }
1.49 millert 810: if (dot == NULL)
811: lt->tm_sec = 0;
812: else {
1.48 millert 813: *dot++ = '\0';
814: if (strlen(dot) != 2)
815: goto terr;
1.50 millert 816: lt->tm_sec = ATOI2(dot);
1.48 millert 817: if (lt->tm_sec > 61) /* could be leap second */
1.25 millert 818: goto terr;
819: }
1.42 millert 820:
1.25 millert 821: yearset = 0;
822: switch(strlen(arg)) {
823: case 12: /* CCYYMMDDhhmm */
1.51 millert 824: lt->tm_year = ATOI2(arg) * 100;
825: lt->tm_year -= 1900; /* Convert to Unix time */
1.25 millert 826: yearset = 1;
827: /* FALLTHROUGH */
828: case 10: /* YYMMDDhhmm */
829: if (yearset) {
830: yearset = ATOI2(arg);
1.48 millert 831: lt->tm_year += yearset;
1.25 millert 832: } else {
833: yearset = ATOI2(arg);
1.60 guenther 834: /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
835: lt->tm_year = yearset;
836: if (yearset < 69)
837: lt->tm_year += 100;
1.25 millert 838: }
839: /* FALLTHROUGH */
840: case 8: /* MMDDhhmm */
1.48 millert 841: lt->tm_mon = ATOI2(arg);
842: if (lt->tm_mon > 12 || lt->tm_mon == 0)
843: goto terr;
844: --lt->tm_mon; /* Convert from 01-12 to 00-11 */
845: lt->tm_mday = ATOI2(arg);
846: if (lt->tm_mday > 31 || lt->tm_mday == 0)
847: goto terr;
848: lt->tm_hour = ATOI2(arg);
849: if (lt->tm_hour > 23)
850: goto terr;
851: lt->tm_min = ATOI2(arg);
852: if (lt->tm_min > 59)
853: goto terr;
1.25 millert 854: break;
855: default:
856: goto terr;
857: }
1.42 millert 858:
1.48 millert 859: lt->tm_isdst = -1; /* mktime will deduce DST. */
860: then = mktime(lt);
861: if (then == (time_t)-1) {
1.25 millert 862: terr:
1.48 millert 863: panic("illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
864: }
865: if (then < now)
866: panic("cannot schedule jobs in the past");
867: return (then);
1.30 millert 868: }
869:
1.35 millert 870: static int
871: check_permission(void)
872: {
873: int ok;
874: uid_t uid = geteuid();
875: struct passwd *pw;
1.30 millert 876:
1.35 millert 877: if ((pw = getpwuid(uid)) == NULL) {
878: perror("Cannot access password database");
1.59 millert 879: exit(EXIT_FAILURE);
1.35 millert 880: }
1.30 millert 881:
882: PRIV_START;
883:
1.35 millert 884: ok = allowed(pw->pw_name, AT_ALLOW, AT_DENY);
885:
886: PRIV_END;
1.30 millert 887:
1.35 millert 888: return (ok);
889: }
1.30 millert 890:
1.41 millert 891: static __dead void
1.35 millert 892: usage(void)
893: {
894: /* Print usage and exit. */
895: switch (program) {
896: case AT:
897: case CAT:
898: (void)fprintf(stderr,
1.47 jmc 899: "usage: at [-bm] [-f file] [-l [user ...]] [-q queue] "
900: "-t time_arg | timespec\n"
901: " at -c | -r job ...\n");
1.35 millert 902: break;
903: case ATQ:
904: (void)fprintf(stderr,
1.43 jmc 905: "usage: atq [-cnv] [-q queue] [name ...]\n");
1.35 millert 906: break;
907: case ATRM:
908: (void)fprintf(stderr,
909: "usage: atrm [-afi] [[job] [name] ...]\n");
910: break;
911: case BATCH:
912: (void)fprintf(stderr,
913: "usage: batch [-m] [-f file] [-q queue] [timespec]\n");
914: break;
915: }
1.59 millert 916: exit(EXIT_FAILURE);
1.25 millert 917: }
918:
1.1 deraadt 919: int
1.26 millert 920: main(int argc, char **argv)
1.1 deraadt 921: {
1.29 millert 922: time_t timer = -1;
1.55 deraadt 923: char *atinput = NULL; /* where to get input from */
1.7 millert 924: char queue = DEFAULT_AT_QUEUE;
925: char queue_set = 0;
1.25 millert 926: char *options = "q:f:t:bcdlmrv"; /* default options for at */
1.38 avsm 927: char cwd[PATH_MAX];
1.29 millert 928: int ch;
929: int aflag = 0;
930: int cflag = 0;
931: int nflag = 0;
1.66 ! deraadt 932:
! 933: if (pledge("stdio rpath wpath cpath fattr getpw unix flock id",
! 934: NULL) == -1)
! 935: perr("pledge");
1.41 millert 936:
937: if (argc < 1)
938: usage();
1.1 deraadt 939:
1.35 millert 940: if ((ProgramName = strrchr(argv[0], '/')) != NULL)
941: ProgramName++;
942: else
943: ProgramName = argv[0];
944:
1.26 millert 945: RELINQUISH_PRIVS;
1.1 deraadt 946:
947: /* find out what this program is supposed to do */
1.35 millert 948: if (strcmp(ProgramName, "atq") == 0) {
1.1 deraadt 949: program = ATQ;
1.29 millert 950: options = "cnvq:";
1.35 millert 951: } else if (strcmp(ProgramName, "atrm") == 0) {
1.1 deraadt 952: program = ATRM;
1.29 millert 953: options = "afi";
1.35 millert 954: } else if (strcmp(ProgramName, "batch") == 0) {
1.1 deraadt 955: program = BATCH;
1.24 millert 956: options = "f:q:mv";
1.1 deraadt 957: }
958:
959: /* process whatever options we can process */
1.29 millert 960: while ((ch = getopt(argc, argv, options)) != -1) {
961: switch (ch) {
962: case 'a':
963: aflag = 1;
964: break;
965:
966: case 'i':
967: interactive = 1;
968: force = 0;
969: break;
970:
971: case 'v': /* show completed but unremoved jobs */
972: /*
973: * This option is only useful when we are invoked
974: * as atq but we accept (and ignore) this flag in
975: * the other programs for backwards compatibility.
976: */
977: vflag = 1;
1.1 deraadt 978: break;
979:
980: case 'm': /* send mail when job is complete */
981: send_mail = 1;
982: break;
983:
984: case 'f':
1.29 millert 985: if (program == ATRM) {
986: force = 1;
987: interactive = 0;
988: } else
989: atinput = optarg;
1.1 deraadt 990: break;
991:
992: case 'q': /* specify queue */
993: if (strlen(optarg) > 1)
994: usage();
995:
996: atqueue = queue = *optarg;
1.62 deraadt 997: if (!(islower((unsigned char)queue) ||
998: isupper((unsigned char)queue)))
1.1 deraadt 999: usage();
1.7 millert 1000:
1001: queue_set = 1;
1002: break;
1003:
1.25 millert 1004: case 'd': /* for backwards compatibility */
1005: case 'r':
1.7 millert 1006: program = ATRM;
1.24 millert 1007: options = "";
1.7 millert 1008: break;
1009:
1.25 millert 1010: case 't':
1011: timer = ttime(optarg);
1012: break;
1013:
1.7 millert 1014: case 'l':
1015: program = ATQ;
1.29 millert 1016: options = "cnvq:";
1.7 millert 1017: break;
1018:
1019: case 'b':
1020: program = BATCH;
1.24 millert 1021: options = "f:q:mv";
1.7 millert 1022: break;
1023:
1024: case 'c':
1.29 millert 1025: if (program == ATQ) {
1026: cflag = 1;
1027: } else {
1028: program = CAT;
1029: options = "";
1030: }
1031: break;
1032:
1033: case 'n':
1034: nflag = 1;
1.1 deraadt 1035: break;
1036:
1037: default:
1038: usage();
1039: break;
1040: }
1.29 millert 1041: }
1042: argc -= optind;
1043: argv += optind;
1.55 deraadt 1044:
1045: switch (program) {
1046: case AT:
1047: case BATCH:
1048: if (atinput != NULL) {
1049: if (freopen(atinput, "r", stdin) == NULL)
1050: perr("Cannot open input file");
1051: }
1052: break;
1053: default:
1054: ;
1055: }
1.7 millert 1056:
1.35 millert 1057: if (getcwd(cwd, sizeof(cwd)) == NULL)
1058: perr("Cannot get current working directory");
1059:
1060: set_cron_cwd();
1061:
1.16 mickey 1062: if (!check_permission())
1.35 millert 1063: panic("You do not have permission to use at.");
1.7 millert 1064:
1.1 deraadt 1065: /* select our program */
1066: switch (program) {
1067: case ATQ:
1.29 millert 1068: list_jobs(argc, argv, nflag, cflag);
1.1 deraadt 1069: break;
1070:
1071: case ATRM:
1.7 millert 1072: case CAT:
1.29 millert 1073: if ((aflag && argc) || (!aflag && !argc))
1.10 millert 1074: usage();
1.28 millert 1075: exit(process_jobs(argc, argv, program));
1.1 deraadt 1076: break;
1077:
1078: case AT:
1.25 millert 1079: /* Time may have been specified via the -t flag. */
1.35 millert 1080: if (timer == -1) {
1081: if (argc == 0)
1082: usage();
1083: else if ((timer = parsetime(argc, argv)) == -1)
1.59 millert 1084: exit(EXIT_FAILURE);
1.35 millert 1085: }
1086: writefile(cwd, timer, queue);
1.1 deraadt 1087: break;
1088:
1089: case BATCH:
1.7 millert 1090: if (queue_set)
1.62 deraadt 1091: queue = toupper((unsigned char)queue);
1.7 millert 1092: else
1093: queue = DEFAULT_BATCH_QUEUE;
1094:
1.35 millert 1095: if (argc == 0)
1.7 millert 1096: timer = time(NULL);
1.35 millert 1097: else if ((timer = parsetime(argc, argv)) == -1)
1.59 millert 1098: exit(EXIT_FAILURE);
1.7 millert 1099:
1.35 millert 1100: writefile(cwd, timer, queue);
1.1 deraadt 1101: break;
1102:
1103: default:
1104: panic("Internal error");
1105: break;
1106: }
1.59 millert 1107: exit(EXIT_SUCCESS);
1.1 deraadt 1108: }