xref: /dragonfly/usr.bin/at/at.c (revision c03f08f3)
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  * $FreeBSD: src/usr.bin/at/at.c,v 1.29 2002/07/22 11:32:16 robert Exp $
29  * $DragonFly: src/usr.bin/at/at.c,v 1.8 2007/08/26 16:52:01 pavalos Exp $
30  */
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 #include <langinfo.h>
46 #include <locale.h>
47 #include <pwd.h>
48 #include <signal.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55 
56 /* Local headers */
57 
58 #include "at.h"
59 #include "panic.h"
60 #include "parsetime.h"
61 #include "perm.h"
62 
63 #define MAIN
64 #include "privs.h"
65 
66 /* Macros */
67 
68 #ifndef ATJOB_DIR
69 #define ATJOB_DIR "/usr/spool/atjobs/"
70 #endif
71 
72 #ifndef LFILE
73 #define LFILE ATJOB_DIR ".lockfile"
74 #endif
75 
76 #ifndef ATJOB_MX
77 #define ATJOB_MX 255
78 #endif
79 
80 #define ALARMC 10 /* Number of seconds to wait for timeout */
81 
82 #define SIZE 255
83 #define TIMESIZE 50
84 
85 enum { ATQ, ATRM, AT, BATCH, CAT };	/* what program we want to run */
86 
87 /* File scope variables */
88 
89 const char *no_export[] =
90 {
91     "TERM", "TERMCAP", "DISPLAY", "_"
92 } ;
93 static int send_mail = 0;
94 
95 /* External variables */
96 uid_t real_uid, effective_uid;
97 gid_t real_gid, effective_gid;
98 
99 extern char **environ;
100 int fcreated;
101 char atfile[sizeof(ATJOB_DIR) + 14] = ATJOB_DIR;
102 
103 char *atinput = NULL;		/* where to get input from */
104 char atqueue = 0;		/* which queue to examine for jobs (atq) */
105 char atverify = 0;		/* verify time instead of queuing job */
106 char *namep;
107 
108 /* Function declarations */
109 
110 static void sigc(int signo);
111 static void alarmc(int signo);
112 static char *cwdname(void);
113 static void writefile(time_t runtimer, char queue);
114 static void list_jobs(long *, int);
115 static long nextjob(void);
116 static time_t ttime(const char *arg);
117 static int in_job_list(long, long *, int);
118 static long *get_job_list(int, char *[], int *);
119 
120 /* Signal catching functions */
121 
122 static void
123 sigc(int signo __unused)
124 {
125 /* If the user presses ^C, remove the spool file and exit
126  */
127     if (fcreated)
128     {
129 	PRIV_START
130 	    unlink(atfile);
131 	PRIV_END
132     }
133 
134     _exit(EXIT_FAILURE);
135 }
136 
137 static void
138 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 *
152 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+")) != (FILE*)0) {
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")) != (FILE*)0) {
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     setlocale(LC_TIME, "");
225 
226 /* Install the signal handler for SIGINT; terminate after removing the
227  * spool file if necessary
228  */
229     act.sa_handler = sigc;
230     sigemptyset(&(act.sa_mask));
231     act.sa_flags = 0;
232 
233     sigaction(SIGINT, &act, NULL);
234 
235     /* Loop over all possible file names for running something at this
236      * particular time, see if a file is there; the first empty slot at any
237      * particular time is used.  Lock the file LFILE first to make sure
238      * we're alone when doing this.
239      */
240 
241     PRIV_START
242 
243     if ((lockdes = open(LFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0)
244 	perr("cannot open lockfile " LFILE);
245 
246     lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0;
247     lock.l_len = 0;
248 
249     act.sa_handler = alarmc;
250     sigemptyset(&(act.sa_mask));
251     act.sa_flags = 0;
252 
253     /* Set an alarm so a timeout occurs after ALARMC seconds, in case
254      * something is seriously broken.
255      */
256     sigaction(SIGALRM, &act, NULL);
257     alarm(ALARMC);
258     fcntl(lockdes, F_SETLKW, &lock);
259     alarm(0);
260 
261     if ((jobno = nextjob()) == EOF)
262 	perr("cannot generate job number");
263 
264     ppos = atfile + strlen(atfile);
265     snprintf(ppos, sizeof(atfile) - strlen(atfile), "%c%5lx%8lx", queue,
266 	     jobno, (unsigned long) (runtimer/60));
267 
268     for(ap=ppos; *ap != '\0'; ap ++)
269 	if (*ap == ' ')
270 	    *ap = '0';
271 
272     if (stat(atfile, &statbuf) != 0)
273 	if (errno != ENOENT)
274 	    perr("cannot access " ATJOB_DIR);
275 
276     /* Create the file. The x bit is only going to be set after it has
277      * been completely written out, to make sure it is not executed in the
278      * meantime.  To make sure they do not get deleted, turn off their r
279      * bit.  Yes, this is a kluge.
280      */
281     cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
282     if ((fdes = creat(atfile, O_WRONLY)) == -1)
283 	perr("cannot create atjob file");
284 
285     if ((fd2 = dup(fdes)) <0)
286 	perr("error in dup() of job file");
287 
288     if(fchown(fd2, real_uid, real_gid) != 0)
289 	perr("cannot give away file");
290 
291     PRIV_END
292 
293     /* We no longer need suid root; now we just need to be able to write
294      * to the directory, if necessary.
295      */
296 
297     REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
298 
299     /* We've successfully created the file; let's set the flag so it
300      * gets removed in case of an interrupt or error.
301      */
302     fcreated = 1;
303 
304     /* Now we can release the lock, so other people can access it
305      */
306     lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0;
307     lock.l_len = 0;
308     fcntl(lockdes, F_SETLKW, &lock);
309     close(lockdes);
310 
311     if((fp = fdopen(fdes, "w")) == NULL)
312 	panic("cannot reopen atjob file");
313 
314     /* Get the userid to mail to, first by trying getlogin(),
315      * then from LOGNAME, finally from getpwuid().
316      */
317     mailname = getlogin();
318     if (mailname == NULL)
319 	mailname = getenv("LOGNAME");
320 
321     if ((mailname == NULL) || (mailname[0] == '\0')
322 	|| (strlen(mailname) >= MAXLOGNAME) || (getpwnam(mailname)==NULL))
323     {
324 	pass_entry = getpwuid(real_uid);
325 	if (pass_entry != NULL)
326 	    mailname = pass_entry->pw_name;
327     }
328 
329     if (atinput != (char *) NULL)
330     {
331 	fpin = freopen(atinput, "r", stdin);
332 	if (fpin == NULL)
333 	    perr("cannot open input file");
334     }
335     fprintf(fp, "#!/bin/sh\n# atrun uid=%ld gid=%ld\n# mail %*s %d\n",
336 	(long) real_uid, (long) real_gid, MAXLOGNAME - 1, mailname,
337 	send_mail);
338 
339     /* Write out the umask at the time of invocation
340      */
341     fprintf(fp, "umask %lo\n", (unsigned long) cmask);
342 
343     /* Write out the environment. Anything that may look like a
344      * special character to the shell is quoted, except for \n, which is
345      * done with a pair of "'s.  Don't export the no_export list (such
346      * as TERM or DISPLAY) because we don't want these.
347      */
348     for (atenv= environ; *atenv != NULL; atenv++)
349     {
350 	int export = 1;
351 	char *eqp;
352 
353 	eqp = strchr(*atenv, '=');
354 	if (ap == NULL)
355 	    eqp = *atenv;
356 	else
357 	{
358 	    size_t i;
359 	    for (i=0; i<sizeof(no_export)/sizeof(no_export[0]); i++)
360 	    {
361 		export = export
362 		    && (strncmp(*atenv, no_export[i],
363 				(size_t) (eqp-*atenv)) != 0);
364 	    }
365 	    eqp++;
366 	}
367 
368 	if (export)
369 	{
370 	    fwrite(*atenv, sizeof(char), eqp-*atenv, fp);
371 	    for(ap = eqp;*ap != '\0'; ap++)
372 	    {
373 		if (*ap == '\n')
374 		    fprintf(fp, "\"\n\"");
375 		else
376 		{
377 		    if (!isalnum(*ap)) {
378 			switch (*ap) {
379 			  case '%': case '/': case '{': case '[':
380 			  case ']': case '=': case '}': case '@':
381 			  case '+': case '#': case ',': case '.':
382 			  case ':': case '-': case '_':
383 			    break;
384 			  default:
385 			    fputc('\\', fp);
386 			    break;
387 			}
388 		    }
389 		    fputc(*ap, fp);
390 		}
391 	    }
392 	    fputs("; export ", fp);
393 	    fwrite(*atenv, sizeof(char), eqp-*atenv -1, fp);
394 	    fputc('\n', fp);
395 
396 	}
397     }
398     /* Cd to the directory at the time and write out all the
399      * commands the user supplies from stdin.
400      */
401     fprintf(fp, "cd ");
402     for (ap = cwdname(); *ap != '\0'; ap++)
403     {
404 	if (*ap == '\n')
405 	    fprintf(fp, "\"\n\"");
406 	else
407 	{
408 	    if (*ap != '/' && !isalnum(*ap))
409 		fputc('\\', fp);
410 
411 	    fputc(*ap, fp);
412 	}
413     }
414     /* Test cd's exit status: die if the original directory has been
415      * removed, become unreadable or whatever
416      */
417     fprintf(fp, " || {\n\t echo 'Execution directory "
418 	        "inaccessible' >&2\n\t exit 1\n}\n");
419 
420     while((ch = getchar()) != EOF)
421 	fputc(ch, fp);
422 
423     fprintf(fp, "\n");
424     if (ferror(fp))
425 	panic("output error");
426 
427     if (ferror(stdin))
428 	panic("input error");
429 
430     fclose(fp);
431 
432     /* Set the x bit so that we're ready to start executing
433      */
434 
435     if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
436 	perr("cannot give away file");
437 
438     close(fd2);
439     fprintf(stderr, "Job %ld will be executed using /bin/sh\n", jobno);
440 }
441 
442 static int
443 in_job_list(long job, long *joblist, int len)
444 {
445     int i;
446 
447     for (i = 0; i < len; i++)
448 	if (job == joblist[i])
449 	    return 1;
450 
451     return 0;
452 }
453 
454 static void
455 list_jobs(long *joblist, int len)
456 {
457     /* List all a user's jobs in the queue, by looping through ATJOB_DIR,
458      * or everybody's if we are root
459      */
460     struct passwd *pw;
461     DIR *spool;
462     struct dirent *dirent;
463     struct stat buf;
464     struct tm runtime;
465     unsigned long ctm;
466     char queue;
467     long jobno;
468     time_t runtimer;
469     char timestr[TIMESIZE];
470     int first=1;
471 
472     setlocale(LC_TIME, "");
473 
474     PRIV_START
475 
476     if (chdir(ATJOB_DIR) != 0)
477 	perr("cannot change to " ATJOB_DIR);
478 
479     if ((spool = opendir(".")) == NULL)
480 	perr("cannot open " ATJOB_DIR);
481 
482     /*	Loop over every file in the directory
483      */
484     while((dirent = readdir(spool)) != NULL) {
485 	if (stat(dirent->d_name, &buf) != 0)
486 	    perr("cannot stat in " ATJOB_DIR);
487 
488 	/* See it's a regular file and has its x bit turned on and
489          * is the user's
490          */
491 	if (!S_ISREG(buf.st_mode)
492 	    || ((buf.st_uid != real_uid) && ! (real_uid == 0))
493 	    || !(S_IXUSR & buf.st_mode || atverify))
494 	    continue;
495 
496 	if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3)
497 	    continue;
498 
499 	/* If jobs are given, only list those jobs */
500 	if (joblist && !in_job_list(jobno, joblist, len))
501 	    continue;
502 
503 	if (atqueue && (queue != atqueue))
504 	    continue;
505 
506 	runtimer = 60*(time_t) ctm;
507 	runtime = *localtime(&runtimer);
508 	strftime(timestr, TIMESIZE, nl_langinfo(D_T_FMT), &runtime);
509 	if (first) {
510 	    printf("Date\t\t\t\tOwner\t\tQueue\tJob#\n");
511 	    first=0;
512 	}
513 	pw = getpwuid(buf.st_uid);
514 
515 	printf("%s\t%-16s%c%s\t%ld\n",
516 	       timestr,
517 	       pw ? pw->pw_name : "???",
518 	       queue,
519 	       (S_IXUSR & buf.st_mode) ? "":"(done)",
520 	       jobno);
521     }
522     closedir(spool);
523 
524     PRIV_END
525 }
526 
527 static void
528 process_jobs(int argc, char **argv, int what)
529 {
530     /* Delete every argument (job - ID) given
531      */
532     int i;
533     struct stat buf;
534     DIR *spool;
535     struct dirent *dirent;
536     unsigned long ctm;
537     char queue;
538     long jobno;
539 
540     PRIV_START
541 
542     if (chdir(ATJOB_DIR) != 0)
543 	perr("cannot change to " ATJOB_DIR);
544 
545     if ((spool = opendir(".")) == NULL)
546 	perr("cannot open " ATJOB_DIR);
547 
548     PRIV_END
549 
550     /*	Loop over every file in the directory
551      */
552     while((dirent = readdir(spool)) != NULL) {
553 
554 	PRIV_START
555 	if (stat(dirent->d_name, &buf) != 0)
556 	    perr("cannot stat in " ATJOB_DIR);
557 	PRIV_END
558 
559 	if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3)
560 	    continue;
561 
562 	for (i=optind; i < argc; i++) {
563 	    if (atoi(argv[i]) == jobno) {
564 		if ((buf.st_uid != real_uid) && !(real_uid == 0))
565 		    errx(EXIT_FAILURE, "%s: not owner", argv[i]);
566 		switch (what) {
567 		  case ATRM:
568 
569 		    PRIV_START
570 
571 		    if (unlink(dirent->d_name) != 0)
572 		        perr(dirent->d_name);
573 
574 		    PRIV_END
575 
576 		    break;
577 
578 		  case CAT:
579 		    {
580 			FILE *fp;
581 			int ch;
582 
583 			PRIV_START
584 
585 			fp = fopen(dirent->d_name,"r");
586 
587 			PRIV_END
588 
589 			if (!fp) {
590 			    perr("cannot open file");
591 			}
592 			while((ch = getc(fp)) != EOF) {
593 			    putchar(ch);
594 			}
595 			fclose(fp);
596 		    }
597 		    break;
598 
599 		  default:
600 		    errx(EXIT_FAILURE, "internal error, process_jobs = %d",
601 			what);
602 	        }
603 	    }
604 	}
605     }
606     closedir(spool);
607 } /* delete_jobs */
608 
609 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
610 
611 static time_t
612 ttime(const char *arg)
613 {
614     /*
615      * This is pretty much a copy of stime_arg1() from touch.c.  I changed
616      * the return value and the argument list because it's more convenient
617      * (IMO) to do everything in one place. - Joe Halpin
618      */
619     struct timeval tv[2];
620     time_t now;
621     struct tm *t;
622     int yearset;
623     char *p;
624 
625     if (gettimeofday(&tv[0], NULL))
626 	panic("Cannot get current time");
627 
628     /* Start with the current time. */
629     now = tv[0].tv_sec;
630     if ((t = localtime(&now)) == NULL)
631 	panic("localtime");
632     /* [[CC]YY]MMDDhhmm[.SS] */
633     if ((p = strchr(arg, '.')) == NULL)
634 	t->tm_sec = 0;		/* Seconds defaults to 0. */
635     else {
636 	if (strlen(p + 1) != 2)
637 	    goto terr;
638 	*p++ = '\0';
639 	t->tm_sec = ATOI2(p);
640     }
641 
642     yearset = 0;
643     switch(strlen(arg)) {
644     case 12:			/* CCYYMMDDhhmm */
645 	t->tm_year = ATOI2(arg);
646 	t->tm_year *= 100;
647 	yearset = 1;
648 	/* FALLTHROUGH */
649     case 10:			/* YYMMDDhhmm */
650 	if (yearset) {
651 	    yearset = ATOI2(arg);
652 	    t->tm_year += yearset;
653 	} else {
654 	    yearset = ATOI2(arg);
655 	    t->tm_year = yearset + 2000;
656 	}
657 	t->tm_year -= 1900;	/* Convert to UNIX time. */
658 	/* FALLTHROUGH */
659     case 8:				/* MMDDhhmm */
660 	t->tm_mon = ATOI2(arg);
661 	--t->tm_mon;		/* Convert from 01-12 to 00-11 */
662 	t->tm_mday = ATOI2(arg);
663 	t->tm_hour = ATOI2(arg);
664 	t->tm_min = ATOI2(arg);
665 	break;
666     default:
667 	goto terr;
668     }
669 
670     t->tm_isdst = -1;		/* Figure out DST. */
671     tv[0].tv_sec = tv[1].tv_sec = mktime(t);
672     if (tv[0].tv_sec != -1)
673 	return tv[0].tv_sec;
674     else
675 terr:
676 	panic(
677 	   "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
678 }
679 
680 static long *
681 get_job_list(int argc, char *argv[], int *joblen)
682 {
683     int i, len;
684     long *joblist;
685     char *ep;
686 
687     joblist = NULL;
688     len = argc;
689     if (len > 0) {
690 	if ((joblist = malloc(len * sizeof(*joblist))) == NULL)
691 	    panic("out of memory");
692 
693 	for (i = 0; i < argc; i++) {
694 	    errno = 0;
695 	    if ((joblist[i] = strtol(argv[i], &ep, 10)) < 0 ||
696 		ep == argv[i] || *ep != '\0' || errno)
697 		panic("invalid job number");
698 	}
699     }
700 
701     *joblen = len;
702     return joblist;
703 }
704 
705 int
706 main(int argc, char **argv)
707 {
708     int c;
709     char queue = DEFAULT_AT_QUEUE;
710     char queue_set = 0;
711     char *pgm;
712 
713     int program = AT;			/* our default program */
714     const char *options = "q:f:t:rmvldbc"; /* default options for at */
715     time_t timer;
716     long *joblist;
717     int joblen;
718 
719     joblist = NULL;
720     joblen = 0;
721     timer = -1;
722     RELINQUISH_PRIVS
723 
724     /* Eat any leading paths
725      */
726     if ((pgm = strrchr(argv[0], '/')) == NULL)
727 	pgm = argv[0];
728     else
729         pgm++;
730 
731     namep = pgm;
732 
733     /* find out what this program is supposed to do
734      */
735     if (strcmp(pgm, "atq") == 0) {
736 	program = ATQ;
737 	options = "q:v";
738     }
739     else if (strcmp(pgm, "atrm") == 0) {
740 	program = ATRM;
741 	options = "";
742     }
743     else if (strcmp(pgm, "batch") == 0) {
744 	program = BATCH;
745 	options = "f:q:mv";
746     }
747 
748     /* process whatever options we can process
749      */
750     opterr=1;
751     while ((c=getopt(argc, argv, options)) != -1)
752 	switch (c) {
753 	case 'v':   /* verify time settings */
754 	    atverify = 1;
755 	    break;
756 
757 	case 'm':   /* send mail when job is complete */
758 	    send_mail = 1;
759 	    break;
760 
761 	case 'f':
762 	    atinput = optarg;
763 	    break;
764 
765 	case 'q':    /* specify queue */
766 	    if (strlen(optarg) > 1)
767 		usage();
768 
769 	    atqueue = queue = *optarg;
770 	    if (!(islower(queue)||isupper(queue)))
771 		usage();
772 
773 	    queue_set = 1;
774 	    break;
775 
776 	case 'd':
777 	    warnx("-d is deprecated; use -r instead");
778 	    /* fall through to 'r' */
779 
780 	case 'r':
781 	    if (program != AT)
782 		usage();
783 
784 	    program = ATRM;
785 	    options = "";
786 	    break;
787 
788 	case 't':
789 	    if (program != AT)
790 		usage();
791 	    timer = ttime(optarg);
792 	    break;
793 
794 	case 'l':
795 	    if (program != AT)
796 		usage();
797 
798 	    program = ATQ;
799 	    options = "q:";
800 	    break;
801 
802 	case 'b':
803 	    if (program != AT)
804 		usage();
805 
806 	    program = BATCH;
807 	    options = "f:q:mv";
808 	    break;
809 
810 	case 'c':
811 	    program = CAT;
812 	    options = "";
813 	    break;
814 
815 	default:
816 	    usage();
817 	    break;
818 	}
819     /* end of options eating
820      */
821 
822     /* select our program
823      */
824     if(!check_permission())
825 	errx(EXIT_FAILURE, "you do not have permission to use this program");
826     switch (program) {
827     case ATQ:
828 
829 	REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
830 
831 	if (queue_set == 0)
832 	    joblist = get_job_list(argc - optind, argv + optind, &joblen);
833 	list_jobs(joblist, joblen);
834 	break;
835 
836     case ATRM:
837 
838 	REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
839 
840 	process_jobs(argc, argv, ATRM);
841 	break;
842 
843     case CAT:
844 
845 	process_jobs(argc, argv, CAT);
846 	break;
847 
848     case AT:
849 	/*
850 	 * If timer is > -1, then the user gave the time with -t.  In that
851 	 * case, it's already been set. If not, set it now.
852 	 */
853 	if (timer == -1)
854 	    timer = parsetime(argc, argv);
855 
856 	if (atverify)
857 	{
858 	    struct tm *tm = localtime(&timer);
859 	    fprintf(stderr, "%s\n", asctime(tm));
860 	}
861 	writefile(timer, queue);
862 	break;
863 
864     case BATCH:
865 	if (queue_set)
866 	    queue = toupper(queue);
867 	else
868 	    queue = DEFAULT_BATCH_QUEUE;
869 
870 	if (argc > optind)
871 	    timer = parsetime(argc, argv);
872 	else
873 	    timer = time(NULL);
874 
875 	if (atverify)
876 	{
877 	    struct tm *tm = localtime(&timer);
878 	    fprintf(stderr, "%s\n", asctime(tm));
879 	}
880 
881         writefile(timer, queue);
882 	break;
883 
884     default:
885 	panic("internal error");
886 	break;
887     }
888     exit(EXIT_SUCCESS);
889 }
890