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