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