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