1 /*- 2 * at.c : Put file into atrun queue 3 * 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 * 6 * Copyright (C) 1993, 1994 Thomas Koenig 7 * 8 * Atrun & Atq modifications 9 * Copyright (C) 1993 David Parsons 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. The name of the author(s) may not be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #define _USE_BSD 1 36 37 /* System Headers */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 #include <sys/time.h> 42 #include <sys/wait.h> 43 #include <ctype.h> 44 #include <dirent.h> 45 #include <err.h> 46 #include <errno.h> 47 #include <fcntl.h> 48 #ifndef __FreeBSD__ 49 #include <getopt.h> 50 #endif 51 #ifdef __FreeBSD__ 52 #include <locale.h> 53 #endif 54 #include <pwd.h> 55 #include <signal.h> 56 #include <stddef.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <time.h> 61 #include <unistd.h> 62 63 /* Local headers */ 64 65 #include "at.h" 66 #include "panic.h" 67 #include "parsetime.h" 68 #include "perm.h" 69 70 #define MAIN 71 #include "privs.h" 72 73 /* Macros */ 74 75 #ifndef ATJOB_DIR 76 #define ATJOB_DIR "/usr/spool/atjobs/" 77 #endif 78 79 #ifndef LFILE 80 #define LFILE ATJOB_DIR ".lockfile" 81 #endif 82 83 #ifndef ATJOB_MX 84 #define ATJOB_MX 255 85 #endif 86 87 #define ALARMC 10 /* Number of seconds to wait for timeout */ 88 89 #define SIZE 255 90 #define TIMESIZE 50 91 92 enum { ATQ, ATRM, AT, BATCH, CAT }; /* what program we want to run */ 93 94 /* File scope variables */ 95 96 static const char *no_export[] = { 97 "TERM", "TERMCAP", "DISPLAY", "_" 98 }; 99 static int send_mail = 0; 100 static char *atinput = NULL; /* where to get input from */ 101 static char atqueue = 0; /* which queue to examine for jobs (atq) */ 102 103 /* External variables */ 104 105 extern char **environ; 106 int fcreated; 107 char atfile[] = ATJOB_DIR "12345678901234"; 108 char atverify = 0; /* verify time instead of queuing job */ 109 char *namep; 110 111 /* Function declarations */ 112 113 static void sigc(int signo); 114 static void alarmc(int signo); 115 static char *cwdname(void); 116 static void writefile(time_t runtimer, char queue); 117 static void list_jobs(long *, int); 118 static long nextjob(void); 119 static time_t ttime(const char *arg); 120 static int in_job_list(long, long *, int); 121 static long *get_job_list(int, char *[], int *); 122 123 /* Signal catching functions */ 124 125 static void sigc(int signo __unused) 126 { 127 /* If the user presses ^C, remove the spool file and exit 128 */ 129 if (fcreated) 130 { 131 PRIV_START 132 unlink(atfile); 133 PRIV_END 134 } 135 136 _exit(EXIT_FAILURE); 137 } 138 139 static void alarmc(int signo __unused) 140 { 141 char buf[1024]; 142 143 /* Time out after some seconds. */ 144 strlcpy(buf, namep, sizeof(buf)); 145 strlcat(buf, ": file locking timed out\n", sizeof(buf)); 146 write(STDERR_FILENO, buf, strlen(buf)); 147 sigc(0); 148 } 149 150 /* Local functions */ 151 152 static char *cwdname(void) 153 { 154 /* Read in the current directory; the name will be overwritten on 155 * subsequent calls. 156 */ 157 static char *ptr = NULL; 158 static size_t size = SIZE; 159 160 if (ptr == NULL) 161 if ((ptr = malloc(size)) == NULL) 162 errx(EXIT_FAILURE, "virtual memory exhausted"); 163 164 while (1) 165 { 166 if (ptr == NULL) 167 panic("out of memory"); 168 169 if (getcwd(ptr, size-1) != NULL) 170 return ptr; 171 172 if (errno != ERANGE) 173 perr("cannot get directory"); 174 175 free (ptr); 176 size += SIZE; 177 if ((ptr = malloc(size)) == NULL) 178 errx(EXIT_FAILURE, "virtual memory exhausted"); 179 } 180 } 181 182 static long 183 nextjob(void) 184 { 185 long jobno; 186 FILE *fid; 187 188 if ((fid = fopen(ATJOB_DIR ".SEQ", "r+")) != NULL) { 189 if (fscanf(fid, "%5lx", &jobno) == 1) { 190 rewind(fid); 191 jobno = (1+jobno) % 0xfffff; /* 2^20 jobs enough? */ 192 fprintf(fid, "%05lx\n", jobno); 193 } 194 else 195 jobno = EOF; 196 fclose(fid); 197 return jobno; 198 } 199 else if ((fid = fopen(ATJOB_DIR ".SEQ", "w")) != NULL) { 200 fprintf(fid, "%05lx\n", jobno = 1); 201 fclose(fid); 202 return 1; 203 } 204 return EOF; 205 } 206 207 static void 208 writefile(time_t runtimer, char queue) 209 { 210 /* This does most of the work if at or batch are invoked for writing a job. 211 */ 212 long jobno; 213 char *ap, *ppos, *mailname; 214 struct passwd *pass_entry; 215 struct stat statbuf; 216 int fdes, lockdes, fd2; 217 FILE *fp, *fpin; 218 struct sigaction act; 219 char **atenv; 220 int ch; 221 mode_t cmask; 222 struct flock lock; 223 224 #ifdef __FreeBSD__ 225 (void) setlocale(LC_TIME, ""); 226 #endif 227 228 /* Install the signal handler for SIGINT; terminate after removing the 229 * spool file if necessary 230 */ 231 act.sa_handler = sigc; 232 sigemptyset(&(act.sa_mask)); 233 act.sa_flags = 0; 234 235 sigaction(SIGINT, &act, NULL); 236 237 ppos = atfile + strlen(ATJOB_DIR); 238 239 /* Loop over all possible file names for running something at this 240 * particular time, see if a file is there; the first empty slot at any 241 * particular time is used. Lock the file LFILE first to make sure 242 * we're alone when doing this. 243 */ 244 245 PRIV_START 246 247 if ((lockdes = open(LFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0) 248 perr("cannot open lockfile " LFILE); 249 250 lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; 251 lock.l_len = 0; 252 253 act.sa_handler = alarmc; 254 sigemptyset(&(act.sa_mask)); 255 act.sa_flags = 0; 256 257 /* Set an alarm so a timeout occurs after ALARMC seconds, in case 258 * something is seriously broken. 259 */ 260 sigaction(SIGALRM, &act, NULL); 261 alarm(ALARMC); 262 fcntl(lockdes, F_SETLKW, &lock); 263 alarm(0); 264 265 if ((jobno = nextjob()) == EOF) 266 perr("cannot generate job number"); 267 268 sprintf(ppos, "%c%5lx%8lx", queue, 269 jobno, (unsigned long) (runtimer/60)); 270 271 for(ap=ppos; *ap != '\0'; ap ++) 272 if (*ap == ' ') 273 *ap = '0'; 274 275 if (stat(atfile, &statbuf) != 0) 276 if (errno != ENOENT) 277 perr("cannot access " ATJOB_DIR); 278 279 /* Create the file. The x bit is only going to be set after it has 280 * been completely written out, to make sure it is not executed in the 281 * meantime. To make sure they do not get deleted, turn off their r 282 * bit. Yes, this is a kluge. 283 */ 284 cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR); 285 if ((fdes = creat(atfile, O_WRONLY)) == -1) 286 perr("cannot create atjob file"); 287 288 if ((fd2 = dup(fdes)) <0) 289 perr("error in dup() of job file"); 290 291 if(fchown(fd2, real_uid, real_gid) != 0) 292 perr("cannot give away file"); 293 294 PRIV_END 295 296 /* We no longer need suid root; now we just need to be able to write 297 * to the directory, if necessary. 298 */ 299 300 REDUCE_PRIV(DAEMON_UID, DAEMON_GID) 301 302 /* We've successfully created the file; let's set the flag so it 303 * gets removed in case of an interrupt or error. 304 */ 305 fcreated = 1; 306 307 /* Now we can release the lock, so other people can access it 308 */ 309 lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; 310 lock.l_len = 0; 311 fcntl(lockdes, F_SETLKW, &lock); 312 close(lockdes); 313 314 if((fp = fdopen(fdes, "w")) == NULL) 315 panic("cannot reopen atjob file"); 316 317 /* Get the userid to mail to, first by trying getlogin(), 318 * then from LOGNAME, finally from getpwuid(). 319 */ 320 mailname = getlogin(); 321 if (mailname == NULL) 322 mailname = getenv("LOGNAME"); 323 324 if ((mailname == NULL) || (mailname[0] == '\0') 325 || (strlen(mailname) >= MAXLOGNAME) || (getpwnam(mailname)==NULL)) 326 { 327 pass_entry = getpwuid(real_uid); 328 if (pass_entry != NULL) 329 mailname = pass_entry->pw_name; 330 } 331 332 if (atinput != (char *) NULL) 333 { 334 fpin = freopen(atinput, "r", stdin); 335 if (fpin == NULL) 336 perr("cannot open input file"); 337 } 338 fprintf(fp, "#!/bin/sh\n# atrun uid=%ld gid=%ld\n# mail %*s %d\n", 339 (long) real_uid, (long) real_gid, MAXLOGNAME - 1, mailname, 340 send_mail); 341 342 /* Write out the umask at the time of invocation 343 */ 344 fprintf(fp, "umask %lo\n", (unsigned long) cmask); 345 346 /* Write out the environment. Anything that may look like a 347 * special character to the shell is quoted, except for \n, which is 348 * done with a pair of "'s. Don't export the no_export list (such 349 * as TERM or DISPLAY) because we don't want these. 350 */ 351 for (atenv= environ; *atenv != NULL; atenv++) 352 { 353 int export = 1; 354 char *eqp; 355 356 eqp = strchr(*atenv, '='); 357 if (eqp == NULL) 358 eqp = *atenv; 359 else 360 { 361 size_t i; 362 for (i = 0; i < nitems(no_export); i++) 363 { 364 export = export 365 && (strncmp(*atenv, no_export[i], 366 (size_t) (eqp-*atenv)) != 0); 367 } 368 eqp++; 369 } 370 371 if (export) 372 { 373 (void)fputs("export ", fp); 374 fwrite(*atenv, sizeof(char), eqp-*atenv, fp); 375 for(ap = eqp;*ap != '\0'; ap++) 376 { 377 if (*ap == '\n') 378 fprintf(fp, "\"\n\""); 379 else 380 { 381 if (!isalnum(*ap)) { 382 switch (*ap) { 383 case '%': case '/': case '{': case '[': 384 case ']': case '=': case '}': case '@': 385 case '+': case '#': case ',': case '.': 386 case ':': case '-': case '_': 387 break; 388 default: 389 fputc('\\', fp); 390 break; 391 } 392 } 393 fputc(*ap, fp); 394 } 395 } 396 fputc('\n', fp); 397 398 } 399 } 400 /* Cd to the directory at the time and write out all the 401 * commands the user supplies from stdin. 402 */ 403 fprintf(fp, "cd "); 404 for (ap = cwdname(); *ap != '\0'; ap++) 405 { 406 if (*ap == '\n') 407 fprintf(fp, "\"\n\""); 408 else 409 { 410 if (*ap != '/' && !isalnum(*ap)) 411 fputc('\\', fp); 412 413 fputc(*ap, fp); 414 } 415 } 416 /* Test cd's exit status: die if the original directory has been 417 * removed, become unreadable or whatever 418 */ 419 fprintf(fp, " || {\n\t echo 'Execution directory " 420 "inaccessible' >&2\n\t exit 1\n}\n"); 421 422 while((ch = getchar()) != EOF) 423 fputc(ch, fp); 424 425 fprintf(fp, "\n"); 426 if (ferror(fp)) 427 panic("output error"); 428 429 if (ferror(stdin)) 430 panic("input error"); 431 432 fclose(fp); 433 434 /* Set the x bit so that we're ready to start executing 435 */ 436 437 if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0) 438 perr("cannot give away file"); 439 440 close(fd2); 441 fprintf(stderr, "Job %ld will be executed using /bin/sh\n", jobno); 442 } 443 444 static int 445 in_job_list(long job, long *joblist, int len) 446 { 447 int i; 448 449 for (i = 0; i < len; i++) 450 if (job == joblist[i]) 451 return 1; 452 453 return 0; 454 } 455 456 static void 457 list_jobs(long *joblist, int len) 458 { 459 /* List all a user's jobs in the queue, by looping through ATJOB_DIR, 460 * or everybody's if we are root 461 */ 462 struct passwd *pw; 463 DIR *spool; 464 struct dirent *dirent; 465 struct stat buf; 466 struct tm runtime; 467 unsigned long ctm; 468 char queue; 469 long jobno; 470 time_t runtimer; 471 char timestr[TIMESIZE]; 472 int first=1; 473 474 #ifdef __FreeBSD__ 475 (void) setlocale(LC_TIME, ""); 476 #endif 477 478 PRIV_START 479 480 if (chdir(ATJOB_DIR) != 0) 481 perr("cannot change to " ATJOB_DIR); 482 483 if ((spool = opendir(".")) == NULL) 484 perr("cannot open " ATJOB_DIR); 485 486 /* Loop over every file in the directory 487 */ 488 while((dirent = readdir(spool)) != NULL) { 489 if (stat(dirent->d_name, &buf) != 0) 490 perr("cannot stat in " ATJOB_DIR); 491 492 /* See it's a regular file and has its x bit turned on and 493 * is the user's 494 */ 495 if (!S_ISREG(buf.st_mode) 496 || ((buf.st_uid != real_uid) && ! (real_uid == 0)) 497 || !(S_IXUSR & buf.st_mode || atverify)) 498 continue; 499 500 if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3) 501 continue; 502 503 /* If jobs are given, only list those jobs */ 504 if (joblist && !in_job_list(jobno, joblist, len)) 505 continue; 506 507 if (atqueue && (queue != atqueue)) 508 continue; 509 510 runtimer = 60*(time_t) ctm; 511 runtime = *localtime(&runtimer); 512 strftime(timestr, TIMESIZE, "%+", &runtime); 513 if (first) { 514 printf("Date\t\t\t\tOwner\t\tQueue\tJob#\n"); 515 first=0; 516 } 517 pw = getpwuid(buf.st_uid); 518 519 printf("%s\t%-16s%c%s\t%ld\n", 520 timestr, 521 pw ? pw->pw_name : "???", 522 queue, 523 (S_IXUSR & buf.st_mode) ? "":"(done)", 524 jobno); 525 } 526 PRIV_END 527 closedir(spool); 528 } 529 530 static void 531 process_jobs(int argc, char **argv, int what) 532 { 533 /* Delete every argument (job - ID) given 534 */ 535 int i; 536 int rc; 537 int nofJobs; 538 int nofDone; 539 int statErrno; 540 struct stat buf; 541 DIR *spool; 542 struct dirent *dirent; 543 unsigned long ctm; 544 char queue; 545 long jobno; 546 547 nofJobs = argc - optind; 548 nofDone = 0; 549 550 PRIV_START 551 552 if (chdir(ATJOB_DIR) != 0) 553 perr("cannot change to " ATJOB_DIR); 554 555 if ((spool = opendir(".")) == NULL) 556 perr("cannot open " ATJOB_DIR); 557 558 PRIV_END 559 560 /* Loop over every file in the directory 561 */ 562 while((dirent = readdir(spool)) != NULL) { 563 564 PRIV_START 565 rc = stat(dirent->d_name, &buf); 566 statErrno = errno; 567 PRIV_END 568 /* There's a race condition between readdir above and stat here: 569 * another atrm process could have removed the file from the spool 570 * directory under our nose. If this happens, stat will set errno to 571 * ENOENT, which we shouldn't treat as fatal. 572 */ 573 if (rc != 0) { 574 if (statErrno == ENOENT) 575 continue; 576 else 577 perr("cannot stat in " ATJOB_DIR); 578 } 579 580 if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3) 581 continue; 582 583 for (i=optind; i < argc; i++) { 584 if (atoi(argv[i]) == jobno) { 585 if ((buf.st_uid != real_uid) && !(real_uid == 0)) 586 errx(EXIT_FAILURE, "%s: not owner", argv[i]); 587 switch (what) { 588 case ATRM: 589 590 PRIV_START 591 592 if (unlink(dirent->d_name) != 0) 593 perr(dirent->d_name); 594 595 PRIV_END 596 597 break; 598 599 case CAT: 600 { 601 FILE *fp; 602 int ch; 603 604 PRIV_START 605 606 fp = fopen(dirent->d_name,"r"); 607 608 PRIV_END 609 610 if (!fp) { 611 perr("cannot open file"); 612 } 613 while((ch = getc(fp)) != EOF) { 614 putchar(ch); 615 } 616 fclose(fp); 617 } 618 break; 619 620 default: 621 errx(EXIT_FAILURE, "internal error, process_jobs = %d", 622 what); 623 } 624 625 /* All arguments have been processed 626 */ 627 if (++nofDone == nofJobs) 628 goto end; 629 } 630 } 631 } 632 end: 633 closedir(spool); 634 } /* delete_jobs */ 635 636 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 637 638 static time_t 639 ttime(const char *arg) 640 { 641 /* 642 * This is pretty much a copy of stime_arg1() from touch.c. I changed 643 * the return value and the argument list because it's more convenient 644 * (IMO) to do everything in one place. - Joe Halpin 645 */ 646 struct timeval tv[2]; 647 time_t now; 648 struct tm *t; 649 int yearset; 650 char *p; 651 652 if (gettimeofday(&tv[0], NULL)) 653 panic("Cannot get current time"); 654 655 /* Start with the current time. */ 656 now = tv[0].tv_sec; 657 if ((t = localtime(&now)) == NULL) 658 panic("localtime"); 659 /* [[CC]YY]MMDDhhmm[.SS] */ 660 if ((p = strchr(arg, '.')) == NULL) 661 t->tm_sec = 0; /* Seconds defaults to 0. */ 662 else { 663 if (strlen(p + 1) != 2) 664 goto terr; 665 *p++ = '\0'; 666 t->tm_sec = ATOI2(p); 667 } 668 669 yearset = 0; 670 switch(strlen(arg)) { 671 case 12: /* CCYYMMDDhhmm */ 672 t->tm_year = ATOI2(arg); 673 t->tm_year *= 100; 674 yearset = 1; 675 /* FALLTHROUGH */ 676 case 10: /* YYMMDDhhmm */ 677 if (yearset) { 678 yearset = ATOI2(arg); 679 t->tm_year += yearset; 680 } else { 681 yearset = ATOI2(arg); 682 t->tm_year = yearset + 2000; 683 } 684 t->tm_year -= 1900; /* Convert to UNIX time. */ 685 /* FALLTHROUGH */ 686 case 8: /* MMDDhhmm */ 687 t->tm_mon = ATOI2(arg); 688 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 689 t->tm_mday = ATOI2(arg); 690 t->tm_hour = ATOI2(arg); 691 t->tm_min = ATOI2(arg); 692 break; 693 default: 694 goto terr; 695 } 696 697 t->tm_isdst = -1; /* Figure out DST. */ 698 tv[0].tv_sec = tv[1].tv_sec = mktime(t); 699 if (tv[0].tv_sec != -1) 700 return tv[0].tv_sec; 701 else 702 terr: 703 panic( 704 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 705 } 706 707 static long * 708 get_job_list(int argc, char *argv[], int *joblen) 709 { 710 int i, len; 711 long *joblist; 712 char *ep; 713 714 joblist = NULL; 715 len = argc; 716 if (len > 0) { 717 if ((joblist = malloc(len * sizeof(*joblist))) == NULL) 718 panic("out of memory"); 719 720 for (i = 0; i < argc; i++) { 721 errno = 0; 722 if ((joblist[i] = strtol(argv[i], &ep, 10)) < 0 || 723 ep == argv[i] || *ep != '\0' || errno) 724 panic("invalid job number"); 725 } 726 } 727 728 *joblen = len; 729 return joblist; 730 } 731 732 int 733 main(int argc, char **argv) 734 { 735 int c; 736 char queue = DEFAULT_AT_QUEUE; 737 char queue_set = 0; 738 char *pgm; 739 740 int program = AT; /* our default program */ 741 const char *options = "q:f:t:rmvldbc"; /* default options for at */ 742 time_t timer; 743 long *joblist; 744 int joblen; 745 746 joblist = NULL; 747 joblen = 0; 748 timer = -1; 749 RELINQUISH_PRIVS 750 751 /* Eat any leading paths 752 */ 753 if ((pgm = strrchr(argv[0], '/')) == NULL) 754 pgm = argv[0]; 755 else 756 pgm++; 757 758 namep = pgm; 759 760 /* find out what this program is supposed to do 761 */ 762 if (strcmp(pgm, "atq") == 0) { 763 program = ATQ; 764 options = "q:v"; 765 } 766 else if (strcmp(pgm, "atrm") == 0) { 767 program = ATRM; 768 options = ""; 769 } 770 else if (strcmp(pgm, "batch") == 0) { 771 program = BATCH; 772 options = "f:q:mv"; 773 } 774 775 /* process whatever options we can process 776 */ 777 opterr=1; 778 while ((c=getopt(argc, argv, options)) != -1) 779 switch (c) { 780 case 'v': /* verify time settings */ 781 atverify = 1; 782 break; 783 784 case 'm': /* send mail when job is complete */ 785 send_mail = 1; 786 break; 787 788 case 'f': 789 atinput = optarg; 790 break; 791 792 case 'q': /* specify queue */ 793 if (strlen(optarg) > 1) 794 usage(); 795 796 atqueue = queue = *optarg; 797 if (!(islower(queue)||isupper(queue))) 798 usage(); 799 800 queue_set = 1; 801 break; 802 803 case 'd': 804 warnx("-d is deprecated; use -r instead"); 805 /* fall through to 'r' */ 806 807 case 'r': 808 if (program != AT) 809 usage(); 810 811 program = ATRM; 812 options = ""; 813 break; 814 815 case 't': 816 if (program != AT) 817 usage(); 818 timer = ttime(optarg); 819 break; 820 821 case 'l': 822 if (program != AT) 823 usage(); 824 825 program = ATQ; 826 options = "q:"; 827 break; 828 829 case 'b': 830 if (program != AT) 831 usage(); 832 833 program = BATCH; 834 options = "f:q:mv"; 835 break; 836 837 case 'c': 838 program = CAT; 839 options = ""; 840 break; 841 842 default: 843 usage(); 844 break; 845 } 846 /* end of options eating 847 */ 848 849 /* select our program 850 */ 851 if(!check_permission()) 852 errx(EXIT_FAILURE, "you do not have permission to use this program"); 853 switch (program) { 854 case ATQ: 855 856 REDUCE_PRIV(DAEMON_UID, DAEMON_GID) 857 858 if (queue_set == 0) 859 joblist = get_job_list(argc - optind, argv + optind, &joblen); 860 list_jobs(joblist, joblen); 861 break; 862 863 case ATRM: 864 865 REDUCE_PRIV(DAEMON_UID, DAEMON_GID) 866 867 process_jobs(argc, argv, ATRM); 868 break; 869 870 case CAT: 871 872 process_jobs(argc, argv, CAT); 873 break; 874 875 case AT: 876 /* 877 * If timer is > -1, then the user gave the time with -t. In that 878 * case, it's already been set. If not, set it now. 879 */ 880 if (timer == -1) 881 timer = parsetime(argc, argv); 882 883 if (atverify) 884 { 885 struct tm *tm = localtime(&timer); 886 fprintf(stderr, "%s\n", asctime(tm)); 887 } 888 writefile(timer, queue); 889 break; 890 891 case BATCH: 892 if (queue_set) 893 queue = toupper(queue); 894 else 895 queue = DEFAULT_BATCH_QUEUE; 896 897 if (argc > optind) 898 timer = parsetime(argc, argv); 899 else 900 timer = time(NULL); 901 902 if (atverify) 903 { 904 struct tm *tm = localtime(&timer); 905 fprintf(stderr, "%s\n", asctime(tm)); 906 } 907 908 writefile(timer, queue); 909 break; 910 911 default: 912 panic("internal error"); 913 break; 914 } 915 exit(EXIT_SUCCESS); 916 } 917