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