xref: /freebsd/usr.bin/at/at.c (revision aa0a1e58)
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/param.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/wait.h>
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #ifndef __FreeBSD__
46 #include <getopt.h>
47 #endif
48 #ifdef __FreeBSD__
49 #include <locale.h>
50 #endif
51 #include <pwd.h>
52 #include <signal.h>
53 #include <stddef.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 
60 /* Local headers */
61 
62 #include "at.h"
63 #include "panic.h"
64 #include "parsetime.h"
65 #include "perm.h"
66 
67 #define MAIN
68 #include "privs.h"
69 
70 /* Macros */
71 
72 #ifndef ATJOB_DIR
73 #define ATJOB_DIR "/usr/spool/atjobs/"
74 #endif
75 
76 #ifndef LFILE
77 #define LFILE ATJOB_DIR ".lockfile"
78 #endif
79 
80 #ifndef ATJOB_MX
81 #define ATJOB_MX 255
82 #endif
83 
84 #define ALARMC 10 /* Number of seconds to wait for timeout */
85 
86 #define SIZE 255
87 #define TIMESIZE 50
88 
89 enum { ATQ, ATRM, AT, BATCH, CAT };	/* what program we want to run */
90 
91 /* File scope variables */
92 
93 const char *no_export[] =
94 {
95     "TERM", "TERMCAP", "DISPLAY", "_"
96 } ;
97 static int send_mail = 0;
98 
99 /* External variables */
100 
101 extern char **environ;
102 int fcreated;
103 char atfile[] = ATJOB_DIR "12345678901234";
104 
105 char *atinput = (char*)0;	/* where to get input from */
106 char atqueue = 0;		/* which queue to examine for jobs (atq) */
107 char atverify = 0;		/* verify time instead of queuing job */
108 char *namep;
109 
110 /* Function declarations */
111 
112 static void sigc(int signo);
113 static void alarmc(int signo);
114 static char *cwdname(void);
115 static void writefile(time_t runtimer, char queue);
116 static void list_jobs(long *, int);
117 static long nextjob(void);
118 static time_t ttime(const char *arg);
119 static int in_job_list(long, long *, int);
120 static long *get_job_list(int, char *[], int *);
121 
122 /* Signal catching functions */
123 
124 static void sigc(int signo __unused)
125 {
126 /* If the user presses ^C, remove the spool file and exit
127  */
128     if (fcreated)
129     {
130 	PRIV_START
131 	    unlink(atfile);
132 	PRIV_END
133     }
134 
135     _exit(EXIT_FAILURE);
136 }
137 
138 static void alarmc(int signo __unused)
139 {
140     char buf[1024];
141 
142     /* Time out after some seconds. */
143     strlcpy(buf, namep, sizeof(buf));
144     strlcat(buf, ": file locking timed out\n", sizeof(buf));
145     write(STDERR_FILENO, buf, strlen(buf));
146     sigc(0);
147 }
148 
149 /* Local functions */
150 
151 static char *cwdname(void)
152 {
153 /* Read in the current directory; the name will be overwritten on
154  * subsequent calls.
155  */
156     static char *ptr = NULL;
157     static size_t size = SIZE;
158 
159     if (ptr == NULL)
160 	if ((ptr = malloc(size)) == NULL)
161 	    errx(EXIT_FAILURE, "virtual memory exhausted");
162 
163     while (1)
164     {
165 	if (ptr == NULL)
166 	    panic("out of memory");
167 
168 	if (getcwd(ptr, size-1) != NULL)
169 	    return ptr;
170 
171 	if (errno != ERANGE)
172 	    perr("cannot get directory");
173 
174 	free (ptr);
175 	size += SIZE;
176 	if ((ptr = malloc(size)) == NULL)
177 	    errx(EXIT_FAILURE, "virtual memory exhausted");
178     }
179 }
180 
181 static long
182 nextjob(void)
183 {
184     long jobno;
185     FILE *fid;
186 
187     if ((fid = fopen(ATJOB_DIR ".SEQ", "r+")) != NULL) {
188 	if (fscanf(fid, "%5lx", &jobno) == 1) {
189 	    rewind(fid);
190 	    jobno = (1+jobno) % 0xfffff;	/* 2^20 jobs enough? */
191 	    fprintf(fid, "%05lx\n", jobno);
192 	}
193 	else
194 	    jobno = EOF;
195 	fclose(fid);
196 	return jobno;
197     }
198     else if ((fid = fopen(ATJOB_DIR ".SEQ", "w")) != NULL) {
199 	fprintf(fid, "%05lx\n", jobno = 1);
200 	fclose(fid);
201 	return 1;
202     }
203     return EOF;
204 }
205 
206 static void
207 writefile(time_t runtimer, char queue)
208 {
209 /* This does most of the work if at or batch are invoked for writing a job.
210  */
211     long jobno;
212     char *ap, *ppos, *mailname;
213     struct passwd *pass_entry;
214     struct stat statbuf;
215     int fdes, lockdes, fd2;
216     FILE *fp, *fpin;
217     struct sigaction act;
218     char **atenv;
219     int ch;
220     mode_t cmask;
221     struct flock lock;
222 
223 #ifdef __FreeBSD__
224     (void) setlocale(LC_TIME, "");
225 #endif
226 
227 /* Install the signal handler for SIGINT; terminate after removing the
228  * spool file if necessary
229  */
230     act.sa_handler = sigc;
231     sigemptyset(&(act.sa_mask));
232     act.sa_flags = 0;
233 
234     sigaction(SIGINT, &act, NULL);
235 
236     ppos = atfile + strlen(ATJOB_DIR);
237 
238     /* Loop over all possible file names for running something at this
239      * particular time, see if a file is there; the first empty slot at any
240      * particular time is used.  Lock the file LFILE first to make sure
241      * we're alone when doing this.
242      */
243 
244     PRIV_START
245 
246     if ((lockdes = open(LFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0)
247 	perr("cannot open lockfile " LFILE);
248 
249     lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0;
250     lock.l_len = 0;
251 
252     act.sa_handler = alarmc;
253     sigemptyset(&(act.sa_mask));
254     act.sa_flags = 0;
255 
256     /* Set an alarm so a timeout occurs after ALARMC seconds, in case
257      * something is seriously broken.
258      */
259     sigaction(SIGALRM, &act, NULL);
260     alarm(ALARMC);
261     fcntl(lockdes, F_SETLKW, &lock);
262     alarm(0);
263 
264     if ((jobno = nextjob()) == EOF)
265 	perr("cannot generate job number");
266 
267     sprintf(ppos, "%c%5lx%8lx", queue,
268 	    jobno, (unsigned long) (runtimer/60));
269 
270     for(ap=ppos; *ap != '\0'; ap ++)
271 	if (*ap == ' ')
272 	    *ap = '0';
273 
274     if (stat(atfile, &statbuf) != 0)
275 	if (errno != ENOENT)
276 	    perr("cannot access " ATJOB_DIR);
277 
278     /* Create the file. The x bit is only going to be set after it has
279      * been completely written out, to make sure it is not executed in the
280      * meantime.  To make sure they do not get deleted, turn off their r
281      * bit.  Yes, this is a kluge.
282      */
283     cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
284     if ((fdes = creat(atfile, O_WRONLY)) == -1)
285 	perr("cannot create atjob file");
286 
287     if ((fd2 = dup(fdes)) <0)
288 	perr("error in dup() of job file");
289 
290     if(fchown(fd2, real_uid, real_gid) != 0)
291 	perr("cannot give away file");
292 
293     PRIV_END
294 
295     /* We no longer need suid root; now we just need to be able to write
296      * to the directory, if necessary.
297      */
298 
299     REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
300 
301     /* We've successfully created the file; let's set the flag so it
302      * gets removed in case of an interrupt or error.
303      */
304     fcreated = 1;
305 
306     /* Now we can release the lock, so other people can access it
307      */
308     lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0;
309     lock.l_len = 0;
310     fcntl(lockdes, F_SETLKW, &lock);
311     close(lockdes);
312 
313     if((fp = fdopen(fdes, "w")) == NULL)
314 	panic("cannot reopen atjob file");
315 
316     /* Get the userid to mail to, first by trying getlogin(),
317      * then from LOGNAME, finally from getpwuid().
318      */
319     mailname = getlogin();
320     if (mailname == NULL)
321 	mailname = getenv("LOGNAME");
322 
323     if ((mailname == NULL) || (mailname[0] == '\0')
324 	|| (strlen(mailname) >= MAXLOGNAME) || (getpwnam(mailname)==NULL))
325     {
326 	pass_entry = getpwuid(real_uid);
327 	if (pass_entry != NULL)
328 	    mailname = pass_entry->pw_name;
329     }
330 
331     if (atinput != (char *) NULL)
332     {
333 	fpin = freopen(atinput, "r", stdin);
334 	if (fpin == NULL)
335 	    perr("cannot open input file");
336     }
337     fprintf(fp, "#!/bin/sh\n# atrun uid=%ld gid=%ld\n# mail %*s %d\n",
338 	(long) real_uid, (long) real_gid, MAXLOGNAME - 1, mailname,
339 	send_mail);
340 
341     /* Write out the umask at the time of invocation
342      */
343     fprintf(fp, "umask %lo\n", (unsigned long) cmask);
344 
345     /* Write out the environment. Anything that may look like a
346      * special character to the shell is quoted, except for \n, which is
347      * done with a pair of "'s.  Don't export the no_export list (such
348      * as TERM or DISPLAY) because we don't want these.
349      */
350     for (atenv= environ; *atenv != NULL; atenv++)
351     {
352 	int export = 1;
353 	char *eqp;
354 
355 	eqp = strchr(*atenv, '=');
356 	if (ap == NULL)
357 	    eqp = *atenv;
358 	else
359 	{
360 	    size_t i;
361 	    for (i=0; i<sizeof(no_export)/sizeof(no_export[0]); i++)
362 	    {
363 		export = export
364 		    && (strncmp(*atenv, no_export[i],
365 				(size_t) (eqp-*atenv)) != 0);
366 	    }
367 	    eqp++;
368 	}
369 
370 	if (export)
371 	{
372 	    fwrite(*atenv, sizeof(char), eqp-*atenv, fp);
373 	    for(ap = eqp;*ap != '\0'; ap++)
374 	    {
375 		if (*ap == '\n')
376 		    fprintf(fp, "\"\n\"");
377 		else
378 		{
379 		    if (!isalnum(*ap)) {
380 			switch (*ap) {
381 			  case '%': case '/': case '{': case '[':
382 			  case ']': case '=': case '}': case '@':
383 			  case '+': case '#': case ',': case '.':
384 			  case ':': case '-': case '_':
385 			    break;
386 			  default:
387 			    fputc('\\', fp);
388 			    break;
389 			}
390 		    }
391 		    fputc(*ap, fp);
392 		}
393 	    }
394 	    fputs("; export ", fp);
395 	    fwrite(*atenv, sizeof(char), eqp-*atenv -1, fp);
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     struct stat buf;
537     DIR *spool;
538     struct dirent *dirent;
539     unsigned long ctm;
540     char queue;
541     long jobno;
542 
543     PRIV_START
544 
545     if (chdir(ATJOB_DIR) != 0)
546 	perr("cannot change to " ATJOB_DIR);
547 
548     if ((spool = opendir(".")) == NULL)
549 	perr("cannot open " ATJOB_DIR);
550 
551     PRIV_END
552 
553     /*	Loop over every file in the directory
554      */
555     while((dirent = readdir(spool)) != NULL) {
556 
557 	PRIV_START
558 	if (stat(dirent->d_name, &buf) != 0)
559 	    perr("cannot stat in " ATJOB_DIR);
560 	PRIV_END
561 
562 	if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3)
563 	    continue;
564 
565 	for (i=optind; i < argc; i++) {
566 	    if (atoi(argv[i]) == jobno) {
567 		if ((buf.st_uid != real_uid) && !(real_uid == 0))
568 		    errx(EXIT_FAILURE, "%s: not owner", argv[i]);
569 		switch (what) {
570 		  case ATRM:
571 
572 		    PRIV_START
573 
574 		    if (unlink(dirent->d_name) != 0)
575 		        perr(dirent->d_name);
576 
577 		    PRIV_END
578 
579 		    break;
580 
581 		  case CAT:
582 		    {
583 			FILE *fp;
584 			int ch;
585 
586 			PRIV_START
587 
588 			fp = fopen(dirent->d_name,"r");
589 
590 			PRIV_END
591 
592 			if (!fp) {
593 			    perr("cannot open file");
594 			}
595 			while((ch = getc(fp)) != EOF) {
596 			    putchar(ch);
597 			}
598 			fclose(fp);
599 		    }
600 		    break;
601 
602 		  default:
603 		    errx(EXIT_FAILURE, "internal error, process_jobs = %d",
604 			what);
605 	        }
606 	    }
607 	}
608     }
609     closedir(spool);
610 } /* delete_jobs */
611 
612 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
613 
614 static time_t
615 ttime(const char *arg)
616 {
617     /*
618      * This is pretty much a copy of stime_arg1() from touch.c.  I changed
619      * the return value and the argument list because it's more convenient
620      * (IMO) to do everything in one place. - Joe Halpin
621      */
622     struct timeval tv[2];
623     time_t now;
624     struct tm *t;
625     int yearset;
626     char *p;
627 
628     if (gettimeofday(&tv[0], NULL))
629 	panic("Cannot get current time");
630 
631     /* Start with the current time. */
632     now = tv[0].tv_sec;
633     if ((t = localtime(&now)) == NULL)
634 	panic("localtime");
635     /* [[CC]YY]MMDDhhmm[.SS] */
636     if ((p = strchr(arg, '.')) == NULL)
637 	t->tm_sec = 0;		/* Seconds defaults to 0. */
638     else {
639 	if (strlen(p + 1) != 2)
640 	    goto terr;
641 	*p++ = '\0';
642 	t->tm_sec = ATOI2(p);
643     }
644 
645     yearset = 0;
646     switch(strlen(arg)) {
647     case 12:			/* CCYYMMDDhhmm */
648 	t->tm_year = ATOI2(arg);
649 	t->tm_year *= 100;
650 	yearset = 1;
651 	/* FALLTHROUGH */
652     case 10:			/* YYMMDDhhmm */
653 	if (yearset) {
654 	    yearset = ATOI2(arg);
655 	    t->tm_year += yearset;
656 	} else {
657 	    yearset = ATOI2(arg);
658 	    t->tm_year = yearset + 2000;
659 	}
660 	t->tm_year -= 1900;	/* Convert to UNIX time. */
661 	/* FALLTHROUGH */
662     case 8:				/* MMDDhhmm */
663 	t->tm_mon = ATOI2(arg);
664 	--t->tm_mon;		/* Convert from 01-12 to 00-11 */
665 	t->tm_mday = ATOI2(arg);
666 	t->tm_hour = ATOI2(arg);
667 	t->tm_min = ATOI2(arg);
668 	break;
669     default:
670 	goto terr;
671     }
672 
673     t->tm_isdst = -1;		/* Figure out DST. */
674     tv[0].tv_sec = tv[1].tv_sec = mktime(t);
675     if (tv[0].tv_sec != -1)
676 	return tv[0].tv_sec;
677     else
678 terr:
679 	panic(
680 	   "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
681 }
682 
683 static long *
684 get_job_list(int argc, char *argv[], int *joblen)
685 {
686     int i, len;
687     long *joblist;
688     char *ep;
689 
690     joblist = NULL;
691     len = argc;
692     if (len > 0) {
693 	if ((joblist = malloc(len * sizeof(*joblist))) == NULL)
694 	    panic("out of memory");
695 
696 	for (i = 0; i < argc; i++) {
697 	    errno = 0;
698 	    if ((joblist[i] = strtol(argv[i], &ep, 10)) < 0 ||
699 		ep == argv[i] || *ep != '\0' || errno)
700 		panic("invalid job number");
701 	}
702     }
703 
704     *joblen = len;
705     return joblist;
706 }
707 
708 int
709 main(int argc, char **argv)
710 {
711     int c;
712     char queue = DEFAULT_AT_QUEUE;
713     char queue_set = 0;
714     char *pgm;
715 
716     int program = AT;			/* our default program */
717     const char *options = "q:f:t:rmvldbc"; /* default options for at */
718     time_t timer;
719     long *joblist;
720     int joblen;
721 
722     joblist = NULL;
723     joblen = 0;
724     timer = -1;
725     RELINQUISH_PRIVS
726 
727     /* Eat any leading paths
728      */
729     if ((pgm = strrchr(argv[0], '/')) == NULL)
730 	pgm = argv[0];
731     else
732         pgm++;
733 
734     namep = pgm;
735 
736     /* find out what this program is supposed to do
737      */
738     if (strcmp(pgm, "atq") == 0) {
739 	program = ATQ;
740 	options = "q:v";
741     }
742     else if (strcmp(pgm, "atrm") == 0) {
743 	program = ATRM;
744 	options = "";
745     }
746     else if (strcmp(pgm, "batch") == 0) {
747 	program = BATCH;
748 	options = "f:q:mv";
749     }
750 
751     /* process whatever options we can process
752      */
753     opterr=1;
754     while ((c=getopt(argc, argv, options)) != -1)
755 	switch (c) {
756 	case 'v':   /* verify time settings */
757 	    atverify = 1;
758 	    break;
759 
760 	case 'm':   /* send mail when job is complete */
761 	    send_mail = 1;
762 	    break;
763 
764 	case 'f':
765 	    atinput = optarg;
766 	    break;
767 
768 	case 'q':    /* specify queue */
769 	    if (strlen(optarg) > 1)
770 		usage();
771 
772 	    atqueue = queue = *optarg;
773 	    if (!(islower(queue)||isupper(queue)))
774 		usage();
775 
776 	    queue_set = 1;
777 	    break;
778 
779 	case 'd':
780 	    warnx("-d is deprecated; use -r instead");
781 	    /* fall through to 'r' */
782 
783 	case 'r':
784 	    if (program != AT)
785 		usage();
786 
787 	    program = ATRM;
788 	    options = "";
789 	    break;
790 
791 	case 't':
792 	    if (program != AT)
793 		usage();
794 	    timer = ttime(optarg);
795 	    break;
796 
797 	case 'l':
798 	    if (program != AT)
799 		usage();
800 
801 	    program = ATQ;
802 	    options = "q:";
803 	    break;
804 
805 	case 'b':
806 	    if (program != AT)
807 		usage();
808 
809 	    program = BATCH;
810 	    options = "f:q:mv";
811 	    break;
812 
813 	case 'c':
814 	    program = CAT;
815 	    options = "";
816 	    break;
817 
818 	default:
819 	    usage();
820 	    break;
821 	}
822     /* end of options eating
823      */
824 
825     /* select our program
826      */
827     if(!check_permission())
828 	errx(EXIT_FAILURE, "you do not have permission to use this program");
829     switch (program) {
830     case ATQ:
831 
832 	REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
833 
834 	if (queue_set == 0)
835 	    joblist = get_job_list(argc - optind, argv + optind, &joblen);
836 	list_jobs(joblist, joblen);
837 	break;
838 
839     case ATRM:
840 
841 	REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
842 
843 	process_jobs(argc, argv, ATRM);
844 	break;
845 
846     case CAT:
847 
848 	process_jobs(argc, argv, CAT);
849 	break;
850 
851     case AT:
852 	/*
853 	 * If timer is > -1, then the user gave the time with -t.  In that
854 	 * case, it's already been set. If not, set it now.
855 	 */
856 	if (timer == -1)
857 	    timer = parsetime(argc, argv);
858 
859 	if (atverify)
860 	{
861 	    struct tm *tm = localtime(&timer);
862 	    fprintf(stderr, "%s\n", asctime(tm));
863 	}
864 	writefile(timer, queue);
865 	break;
866 
867     case BATCH:
868 	if (queue_set)
869 	    queue = toupper(queue);
870 	else
871 	    queue = DEFAULT_BATCH_QUEUE;
872 
873 	if (argc > optind)
874 	    timer = parsetime(argc, argv);
875 	else
876 	    timer = time(NULL);
877 
878 	if (atverify)
879 	{
880 	    struct tm *tm = localtime(&timer);
881 	    fprintf(stderr, "%s\n", asctime(tm));
882 	}
883 
884         writefile(timer, queue);
885 	break;
886 
887     default:
888 	panic("internal error");
889 	break;
890     }
891     exit(EXIT_SUCCESS);
892 }
893