at.c revision 24360
110154Sache/* 27767Sache * at.c : Put file into atrun queue 37767Sache * Copyright (C) 1993, 1994 Thomas Koenig 4941Snate * 57767Sache * Atrun & Atq modifications 67767Sache * Copyright (C) 1993 David Parsons 7941Snate * 8941Snate * Redistribution and use in source and binary forms, with or without 9941Snate * modification, are permitted provided that the following conditions 10941Snate * are met: 11941Snate * 1. Redistributions of source code must retain the above copyright 12941Snate * notice, this list of conditions and the following disclaimer. 13941Snate * 2. The name of the author(s) may not be used to endorse or promote 14941Snate * products derived from this software without specific prior written 15941Snate * permission. 16941Snate * 17941Snate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 18941Snate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19941Snate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2010154Sache * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 21941Snate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22941Snate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23941Snate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24941Snate * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25941Snate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26941Snate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27941Snate */ 28941Snate 29941Snate#define _USE_BSD 1 30941Snate 31941Snate/* System Headers */ 327767Sache 33941Snate#include <sys/types.h> 34941Snate#include <sys/stat.h> 35941Snate#include <sys/wait.h> 3622873Sdavidn#include <sys/param.h> 37941Snate#include <ctype.h> 38941Snate#include <dirent.h> 39941Snate#include <errno.h> 40941Snate#include <fcntl.h> 41941Snate#include <pwd.h> 42941Snate#include <signal.h> 43941Snate#include <stddef.h> 44941Snate#include <stdio.h> 45941Snate#include <stdlib.h> 46941Snate#include <string.h> 47941Snate#include <time.h> 48941Snate#include <unistd.h> 4922873Sdavidn#include <utmp.h> 507767Sache#ifndef __FreeBSD__ 517767Sache#include <getopt.h> 5211760Sache#else 5311760Sache#include <locale.h> 547767Sache#endif 55941Snate 5623318Sache#if (MAXLOGNAME-1) > UT_NAMESIZE 5722873Sdavidn#define LOGNAMESIZE UT_NAMESIZE 5822873Sdavidn#else 5923318Sache#define LOGNAMESIZE (MAXLOGNAME-1) 6022873Sdavidn#endif 6122873Sdavidn 62941Snate/* Local headers */ 637767Sache 64941Snate#include "at.h" 65941Snate#include "panic.h" 66941Snate#include "parsetime.h" 677767Sache#include "perm.h" 687767Sache 69941Snate#define MAIN 70941Snate#include "privs.h" 71941Snate 72941Snate/* Macros */ 73941Snate 7410154Sache#ifndef ATJOB_DIR 757767Sache#define ATJOB_DIR "/usr/spool/atjobs/" 767767Sache#endif 777767Sache 787767Sache#ifndef LFILE 797767Sache#define LFILE ATJOB_DIR ".lockfile" 807767Sache#endif 817767Sache 827767Sache#ifndef ATJOB_MX 837767Sache#define ATJOB_MX 255 847767Sache#endif 857767Sache 867767Sache#define ALARMC 10 /* Number of seconds to wait for timeout */ 877767Sache 88941Snate#define SIZE 255 89941Snate#define TIMESIZE 50 90941Snate 9110154Sacheenum { ATQ, ATRM, AT, BATCH, CAT }; /* what program we want to run */ 9210154Sache 93941Snate/* File scope variables */ 947767Sache 9524360Simpstatic char rcsid[] = "$Id: at.c,v 1.12 1997/03/03 07:58:00 ache Exp $"; 96941Snatechar *no_export[] = 97941Snate{ 987767Sache "TERM", "TERMCAP", "DISPLAY", "_" 997767Sache} ; 100941Snatestatic send_mail = 0; 101941Snate 102941Snate/* External variables */ 1037767Sache 104941Snateextern char **environ; 105941Snateint fcreated; 106941Snatechar *namep; 1077767Sachechar atfile[] = ATJOB_DIR "12345678901234"; 108941Snate 1097767Sachechar *atinput = (char*)0; /* where to get input from */ 110941Snatechar atqueue = 0; /* which queue to examine for jobs (atq) */ 111941Snatechar atverify = 0; /* verify time instead of queuing job */ 112941Snate 113941Snate/* Function declarations */ 114941Snate 1157767Sachestatic void sigc(int signo); 1167767Sachestatic void alarmc(int signo); 1177767Sachestatic char *cwdname(void); 1187767Sachestatic void writefile(time_t runtimer, char queue); 1197767Sachestatic void list_jobs(void); 1207767Sache 121941Snate/* Signal catching functions */ 122941Snate 1237767Sachestatic void sigc(int signo) 124941Snate{ 12510154Sache/* If the user presses ^C, remove the spool file and exit 126941Snate */ 1277767Sache if (fcreated) 1287767Sache { 1297767Sache PRIV_START 1307767Sache unlink(atfile); 1317767Sache PRIV_END 1327767Sache } 133941Snate 1347767Sache exit(EXIT_FAILURE); 135941Snate} 136941Snate 1377767Sachestatic void alarmc(int signo) 138941Snate{ 139941Snate/* Time out after some seconds 140941Snate */ 1417767Sache panic("File locking timed out"); 142941Snate} 143941Snate 144941Snate/* Local functions */ 145941Snate 1467767Sachestatic char *cwdname(void) 147941Snate{ 148941Snate/* Read in the current directory; the name will be overwritten on 149941Snate * subsequent calls. 150941Snate */ 1517767Sache static char *ptr = NULL; 1527767Sache static size_t size = SIZE; 153941Snate 1547767Sache if (ptr == NULL) 1557767Sache ptr = (char *) mymalloc(size); 1567767Sache 1577767Sache while (1) 1587767Sache { 159941Snate if (ptr == NULL) 1607767Sache panic("Out of memory"); 161941Snate 1627767Sache if (getcwd(ptr, size-1) != NULL) 1637767Sache return ptr; 16410154Sache 1657767Sache if (errno != ERANGE) 1667767Sache perr("Cannot get directory"); 16710154Sache 1687767Sache free (ptr); 1697767Sache size += SIZE; 1707767Sache ptr = (char *) mymalloc(size); 1717767Sache } 172941Snate} 173941Snate 17410154Sachestatic long 17510154Sachenextjob() 17610154Sache{ 17710154Sache long jobno; 17810154Sache FILE *fid; 17910154Sache 18010154Sache if ((fid = fopen(ATJOB_DIR ".SEQ", "r+")) != (FILE*)0) { 18110154Sache if (fscanf(fid, "%5lx", &jobno) == 1) { 18210154Sache rewind(fid); 18310154Sache jobno = (1+jobno) % 0xfffff; /* 2^20 jobs enough? */ 18410154Sache fprintf(fid, "%05lx\n", jobno); 18510154Sache } 18610154Sache else 18710154Sache jobno = EOF; 18810154Sache fclose(fid); 18910154Sache return jobno; 19010154Sache } 19110154Sache else if ((fid = fopen(ATJOB_DIR ".SEQ", "w")) != (FILE*)0) { 19210154Sache fprintf(fid, "%05lx\n", jobno = 1); 19310154Sache fclose(fid); 19410154Sache return 1; 19510154Sache } 19610154Sache return EOF; 19710154Sache} 19810154Sache 199941Snatestatic void 2007767Sachewritefile(time_t runtimer, char queue) 201941Snate{ 2027767Sache/* This does most of the work if at or batch are invoked for writing a job. 2037767Sache */ 20410154Sache long jobno; 2057767Sache char *ap, *ppos, *mailname; 2067767Sache struct passwd *pass_entry; 2077767Sache struct stat statbuf; 2087767Sache int fdes, lockdes, fd2; 2097767Sache FILE *fp, *fpin; 2107767Sache struct sigaction act; 2117767Sache char **atenv; 2127767Sache int ch; 2137767Sache mode_t cmask; 2147767Sache struct flock lock; 21510154Sache 21611760Sache#ifdef __FreeBSD__ 21711760Sache (void) setlocale(LC_TIME, ""); 21811760Sache#endif 21911760Sache 2207767Sache/* Install the signal handler for SIGINT; terminate after removing the 2217767Sache * spool file if necessary 2227767Sache */ 2237767Sache act.sa_handler = sigc; 2247767Sache sigemptyset(&(act.sa_mask)); 2257767Sache act.sa_flags = 0; 226941Snate 2277767Sache sigaction(SIGINT, &act, NULL); 228941Snate 2297767Sache ppos = atfile + strlen(ATJOB_DIR); 230941Snate 2317767Sache /* Loop over all possible file names for running something at this 2327767Sache * particular time, see if a file is there; the first empty slot at any 2337767Sache * particular time is used. Lock the file LFILE first to make sure 2347767Sache * we're alone when doing this. 2357767Sache */ 236941Snate 2377767Sache PRIV_START 238941Snate 2397767Sache if ((lockdes = open(LFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0) 2407767Sache perr("Cannot open lockfile " LFILE); 241941Snate 2427767Sache lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; 2437767Sache lock.l_len = 0; 244941Snate 2457767Sache act.sa_handler = alarmc; 2467767Sache sigemptyset(&(act.sa_mask)); 2477767Sache act.sa_flags = 0; 248941Snate 2497767Sache /* Set an alarm so a timeout occurs after ALARMC seconds, in case 2507767Sache * something is seriously broken. 2517767Sache */ 2527767Sache sigaction(SIGALRM, &act, NULL); 2537767Sache alarm(ALARMC); 2547767Sache fcntl(lockdes, F_SETLKW, &lock); 2557767Sache alarm(0); 256941Snate 25710154Sache if ((jobno = nextjob()) == EOF) 25810154Sache perr("Cannot generate job number"); 259941Snate 26010154Sache sprintf(ppos, "%c%5lx%8lx", queue, 26110154Sache jobno, (unsigned long) (runtimer/60)); 262941Snate 26310154Sache for(ap=ppos; *ap != '\0'; ap ++) 26410154Sache if (*ap == ' ') 26510154Sache *ap = '0'; 266941Snate 26710154Sache if (stat(atfile, &statbuf) != 0) 26810154Sache if (errno != ENOENT) 26910154Sache perr("Cannot access " ATJOB_DIR); 27010154Sache 2717767Sache /* Create the file. The x bit is only going to be set after it has 2727767Sache * been completely written out, to make sure it is not executed in the 2737767Sache * meantime. To make sure they do not get deleted, turn off their r 2747767Sache * bit. Yes, this is a kluge. 2757767Sache */ 2767767Sache cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR); 2777767Sache if ((fdes = creat(atfile, O_WRONLY)) == -1) 27810154Sache perr("Cannot create atjob file"); 279941Snate 2807767Sache if ((fd2 = dup(fdes)) <0) 2817767Sache perr("Error in dup() of job file"); 282941Snate 2837767Sache if(fchown(fd2, real_uid, real_gid) != 0) 2847767Sache perr("Cannot give away file"); 285941Snate 2867767Sache PRIV_END 287941Snate 2888112Sache /* We no longer need suid root; now we just need to be able to write 2898112Sache * to the directory, if necessary. 2908112Sache */ 2918112Sache 2928112Sache REDUCE_PRIV(DAEMON_UID, DAEMON_GID) 2938112Sache 29410154Sache /* We've successfully created the file; let's set the flag so it 2957767Sache * gets removed in case of an interrupt or error. 2967767Sache */ 2977767Sache fcreated = 1; 298941Snate 2997767Sache /* Now we can release the lock, so other people can access it 3007767Sache */ 3017767Sache lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; 3027767Sache lock.l_len = 0; 3037767Sache fcntl(lockdes, F_SETLKW, &lock); 3047767Sache close(lockdes); 305941Snate 3067767Sache if((fp = fdopen(fdes, "w")) == NULL) 3077767Sache panic("Cannot reopen atjob file"); 308941Snate 3097767Sache /* Get the userid to mail to, first by trying getlogin(), which reads 3107767Sache * /etc/utmp, then from LOGNAME, finally from getpwuid(). 3117767Sache */ 3127767Sache mailname = getlogin(); 3137767Sache if (mailname == NULL) 3147767Sache mailname = getenv("LOGNAME"); 315941Snate 31610154Sache if ((mailname == NULL) || (mailname[0] == '\0') 31722873Sdavidn || (strlen(mailname) > LOGNAMESIZE) || (getpwnam(mailname)==NULL)) 3187767Sache { 31910154Sache pass_entry = getpwuid(real_uid); 3207767Sache if (pass_entry != NULL) 3217767Sache mailname = pass_entry->pw_name; 3227767Sache } 323941Snate 3247767Sache if (atinput != (char *) NULL) 3257767Sache { 3267767Sache fpin = freopen(atinput, "r", stdin); 3277767Sache if (fpin == NULL) 3287767Sache perr("Cannot open input file"); 3297767Sache } 33022873Sdavidn fprintf(fp, "#!/bin/sh\n# atrun uid=%ld gid=%ld\n# mail %*s %d\n", 33122873Sdavidn (long) real_uid, (long) real_gid, LOGNAMESIZE, mailname, send_mail); 332941Snate 3337767Sache /* Write out the umask at the time of invocation 3347767Sache */ 3357767Sache fprintf(fp, "umask %lo\n", (unsigned long) cmask); 336941Snate 3377767Sache /* Write out the environment. Anything that may look like a 3387767Sache * special character to the shell is quoted, except for \n, which is 3397767Sache * done with a pair of "'s. Dont't export the no_export list (such 3407767Sache * as TERM or DISPLAY) because we don't want these. 3417767Sache */ 3427767Sache for (atenv= environ; *atenv != NULL; atenv++) 3437767Sache { 3447767Sache int export = 1; 3457767Sache char *eqp; 346941Snate 3477767Sache eqp = strchr(*atenv, '='); 3487767Sache if (ap == NULL) 3497767Sache eqp = *atenv; 3507767Sache else 3517767Sache { 3527767Sache int i; 3537767Sache for (i=0; i<sizeof(no_export)/sizeof(no_export[0]); i++) 3547767Sache { 3557767Sache export = export 35610154Sache && (strncmp(*atenv, no_export[i], 3577767Sache (size_t) (eqp-*atenv)) != 0); 3587767Sache } 3597767Sache eqp++; 3607767Sache } 361941Snate 3627767Sache if (export) 3637767Sache { 3647767Sache fwrite(*atenv, sizeof(char), eqp-*atenv, fp); 3657767Sache for(ap = eqp;*ap != '\0'; ap++) 3667767Sache { 3677767Sache if (*ap == '\n') 3687767Sache fprintf(fp, "\"\n\""); 3697767Sache else 3707767Sache { 37110154Sache if (!isalnum(*ap)) { 37210154Sache switch (*ap) { 37310154Sache case '%': case '/': case '{': case '[': 37410154Sache case ']': case '=': case '}': case '@': 37510154Sache case '+': case '#': case ',': case '.': 37610154Sache case ':': case '-': case '_': 37710154Sache break; 37810154Sache default: 37910154Sache fputc('\\', fp); 38010154Sache break; 38110154Sache } 38210154Sache } 3837767Sache fputc(*ap, fp); 384941Snate } 3857767Sache } 3867767Sache fputs("; export ", fp); 3877767Sache fwrite(*atenv, sizeof(char), eqp-*atenv -1, fp); 3887767Sache fputc('\n', fp); 38910154Sache 390941Snate } 39110154Sache } 3927767Sache /* Cd to the directory at the time and write out all the 3937767Sache * commands the user supplies from stdin. 3947767Sache */ 3957767Sache fprintf(fp, "cd "); 3967767Sache for (ap = cwdname(); *ap != '\0'; ap++) 3977767Sache { 3987767Sache if (*ap == '\n') 3997767Sache fprintf(fp, "\"\n\""); 4007767Sache else 4017767Sache { 4027767Sache if (*ap != '/' && !isalnum(*ap)) 4037767Sache fputc('\\', fp); 40410154Sache 4057767Sache fputc(*ap, fp); 4067767Sache } 4077767Sache } 4087767Sache /* Test cd's exit status: die if the original directory has been 4097767Sache * removed, become unreadable or whatever 4107767Sache */ 4117767Sache fprintf(fp, " || {\n\t echo 'Execution directory " 4127767Sache "inaccessible' >&2\n\t exit 1\n}\n"); 413941Snate 4147767Sache while((ch = getchar()) != EOF) 4157767Sache fputc(ch, fp); 416941Snate 4177767Sache fprintf(fp, "\n"); 4187767Sache if (ferror(fp)) 4197767Sache panic("Output error"); 42010154Sache 4217767Sache if (ferror(stdin)) 4227767Sache panic("Input error"); 423941Snate 4247767Sache fclose(fp); 425941Snate 4267767Sache /* Set the x bit so that we're ready to start executing 4277767Sache */ 428941Snate 4297767Sache if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0) 4307767Sache perr("Cannot give away file"); 431941Snate 4327767Sache close(fd2); 43310154Sache fprintf(stderr, "Job %ld will be executed using /bin/sh\n", jobno); 434941Snate} 435941Snate 436941Snatestatic void 437941Snatelist_jobs() 438941Snate{ 43910154Sache /* List all a user's jobs in the queue, by looping through ATJOB_DIR, 4407767Sache * or everybody's if we are root 4417767Sache */ 4427767Sache struct passwd *pw; 4437767Sache DIR *spool; 4447767Sache struct dirent *dirent; 4457767Sache struct stat buf; 4467767Sache struct tm runtime; 4477767Sache unsigned long ctm; 4487767Sache char queue; 44910154Sache long jobno; 4507767Sache time_t runtimer; 4517767Sache char timestr[TIMESIZE]; 4527767Sache int first=1; 453941Snate 4547767Sache PRIV_START 455941Snate 4567767Sache if (chdir(ATJOB_DIR) != 0) 4577767Sache perr("Cannot change to " ATJOB_DIR); 458941Snate 4597767Sache if ((spool = opendir(".")) == NULL) 4607767Sache perr("Cannot open " ATJOB_DIR); 461941Snate 46210154Sache /* Loop over every file in the directory 4637767Sache */ 4647767Sache while((dirent = readdir(spool)) != NULL) { 4657767Sache if (stat(dirent->d_name, &buf) != 0) 4667767Sache perr("Cannot stat in " ATJOB_DIR); 46710154Sache 4687767Sache /* See it's a regular file and has its x bit turned on and 4697767Sache * is the user's 4707767Sache */ 4717767Sache if (!S_ISREG(buf.st_mode) 4727767Sache || ((buf.st_uid != real_uid) && ! (real_uid == 0)) 4737767Sache || !(S_IXUSR & buf.st_mode || atverify)) 4747767Sache continue; 475941Snate 47610154Sache if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3) 4777767Sache continue; 478941Snate 4797767Sache if (atqueue && (queue != atqueue)) 4807767Sache continue; 481941Snate 4827767Sache runtimer = 60*(time_t) ctm; 4837767Sache runtime = *localtime(&runtimer); 4847767Sache strftime(timestr, TIMESIZE, "%X %x", &runtime); 4857767Sache if (first) { 4867767Sache printf("Date\t\t\tOwner\tQueue\tJob#\n"); 4877767Sache first=0; 4887767Sache } 4897767Sache pw = getpwuid(buf.st_uid); 490941Snate 49110154Sache printf("%s\t%s\t%c%s\t%ld\n", 49210154Sache timestr, 49310154Sache pw ? pw->pw_name : "???", 49410154Sache queue, 49510154Sache (S_IXUSR & buf.st_mode) ? "":"(done)", 49610154Sache jobno); 4977767Sache } 4987767Sache PRIV_END 499941Snate} 500941Snate 501941Snatestatic void 50210154Sacheprocess_jobs(int argc, char **argv, int what) 503941Snate{ 5047767Sache /* Delete every argument (job - ID) given 5057767Sache */ 5067767Sache int i; 5077767Sache struct stat buf; 50810154Sache DIR *spool; 50910154Sache struct dirent *dirent; 51010154Sache unsigned long ctm; 51110154Sache char queue; 51210154Sache long jobno; 513941Snate 5147767Sache PRIV_START 515941Snate 5167767Sache if (chdir(ATJOB_DIR) != 0) 5177767Sache perr("Cannot change to " ATJOB_DIR); 5188874Srgrimes 51910154Sache if ((spool = opendir(".")) == NULL) 52010154Sache perr("Cannot open " ATJOB_DIR); 52110154Sache 52210154Sache PRIV_END 52310154Sache 52410154Sache /* Loop over every file in the directory 52510154Sache */ 52610154Sache while((dirent = readdir(spool)) != NULL) { 52710154Sache 52810154Sache PRIV_START 52910154Sache if (stat(dirent->d_name, &buf) != 0) 53010154Sache perr("Cannot stat in " ATJOB_DIR); 53110154Sache PRIV_END 53210154Sache 53310154Sache if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3) 5347860Sache continue; 53510154Sache 53610154Sache for (i=optind; i < argc; i++) { 53710154Sache if (atoi(argv[i]) == jobno) { 53810154Sache if ((buf.st_uid != real_uid) && !(real_uid == 0)) { 53910154Sache fprintf(stderr, "%s: Not owner\n", argv[i]); 54010154Sache exit(EXIT_FAILURE); 54110154Sache } 54210154Sache switch (what) { 54310154Sache case ATRM: 54410154Sache 54510154Sache PRIV_START 54610154Sache 54710154Sache if (unlink(dirent->d_name) != 0) 54810154Sache perr(dirent->d_name); 54910154Sache 55010154Sache PRIV_END 55110154Sache 55210154Sache break; 55310154Sache 55410154Sache case CAT: 55510154Sache { 55610154Sache FILE *fp; 55710154Sache int ch; 55810154Sache 55910154Sache PRIV_START 56010154Sache 56110154Sache fp = fopen(dirent->d_name,"r"); 56210154Sache 56310154Sache PRIV_END 56410154Sache 56510154Sache if (!fp) { 56610154Sache perr("Cannot open file"); 56710154Sache } 56810154Sache while((ch = getc(fp)) != EOF) { 56910154Sache putchar(ch); 57010154Sache } 57110154Sache } 57210154Sache break; 57310154Sache 57410154Sache default: 57510154Sache fprintf(stderr, 57610154Sache "Internal error, process_jobs = %d\n",what); 57710154Sache exit(EXIT_FAILURE); 57810154Sache break; 57910154Sache } 58010154Sache } 5817860Sache } 5827767Sache } 5837767Sache} /* delete_jobs */ 584941Snate 585941Snate/* Global functions */ 586941Snate 5877767Sachevoid * 5887767Sachemymalloc(size_t n) 5897767Sache{ 5907767Sache void *p; 5917767Sache if ((p=malloc(n))==(void *)0) 5927767Sache { 5937767Sache fprintf(stderr,"Virtual memory exhausted\n"); 5947767Sache exit(EXIT_FAILURE); 5957767Sache } 5967767Sache return p; 5977767Sache} 5987767Sache 599941Snateint 6007767Sachemain(int argc, char **argv) 601941Snate{ 6027767Sache int c; 6037767Sache char queue = DEFAULT_AT_QUEUE; 6047767Sache char queue_set = 0; 6057767Sache char *pgm; 606941Snate 60710154Sache enum { ATQ, ATRM, AT, BATCH, CAT }; /* what program we want to run */ 6087767Sache int program = AT; /* our default program */ 60910154Sache char *options = "q:f:mvldbVc"; /* default options for at */ 6107767Sache int disp_version = 0; 6117767Sache time_t timer; 612941Snate 6137767Sache RELINQUISH_PRIVS 614941Snate 6157767Sache /* Eat any leading paths 6167767Sache */ 6177767Sache if ((pgm = strrchr(argv[0], '/')) == NULL) 6187767Sache pgm = argv[0]; 6197767Sache else 6207767Sache pgm++; 621941Snate 6227767Sache namep = pgm; 623941Snate 6247767Sache /* find out what this program is supposed to do 6257767Sache */ 6267767Sache if (strcmp(pgm, "atq") == 0) { 6277767Sache program = ATQ; 6287767Sache options = "q:vV"; 6297767Sache } 6307767Sache else if (strcmp(pgm, "atrm") == 0) { 6317767Sache program = ATRM; 6327767Sache options = "V"; 6337767Sache } 6347767Sache else if (strcmp(pgm, "batch") == 0) { 6357767Sache program = BATCH; 6367767Sache options = "f:q:mvV"; 6377767Sache } 638941Snate 6397767Sache /* process whatever options we can process 6407767Sache */ 6417767Sache opterr=1; 64224360Simp while ((c=getopt(argc, argv, options)) != -1) 6437767Sache switch (c) { 6447767Sache case 'v': /* verify time settings */ 6457767Sache atverify = 1; 6467767Sache break; 647941Snate 6487767Sache case 'm': /* send mail when job is complete */ 6497767Sache send_mail = 1; 6507767Sache break; 651941Snate 6527767Sache case 'f': 6537767Sache atinput = optarg; 6547767Sache break; 65510154Sache 6567767Sache case 'q': /* specify queue */ 6577767Sache if (strlen(optarg) > 1) 6587767Sache usage(); 659941Snate 6607767Sache atqueue = queue = *optarg; 6617767Sache if (!(islower(queue)||isupper(queue))) 6627767Sache usage(); 663941Snate 6647767Sache queue_set = 1; 6657767Sache break; 666941Snate 6677767Sache case 'd': 6687767Sache if (program != AT) 6697767Sache usage(); 670941Snate 6717767Sache program = ATRM; 6727767Sache options = "V"; 6737767Sache break; 674941Snate 6757767Sache case 'l': 6767767Sache if (program != AT) 6777767Sache usage(); 678941Snate 6797767Sache program = ATQ; 6807767Sache options = "q:vV"; 6817767Sache break; 682941Snate 6837767Sache case 'b': 6847767Sache if (program != AT) 6857767Sache usage(); 686941Snate 6877767Sache program = BATCH; 6887767Sache options = "f:q:mvV"; 6897767Sache break; 690941Snate 6917767Sache case 'V': 6927767Sache disp_version = 1; 6937767Sache break; 694941Snate 69510154Sache case 'c': 69610154Sache program = CAT; 69710154Sache options = ""; 69810154Sache break; 69910154Sache 7007767Sache default: 7017767Sache usage(); 7027767Sache break; 7037767Sache } 7047767Sache /* end of options eating 7057767Sache */ 706941Snate 7077767Sache if (disp_version) 7087767Sache fprintf(stderr, "at version " VERSION "\n" 7097767Sache "Bug reports to: ig25@rz.uni-karlsruhe.de (Thomas Koenig)\n"); 710941Snate 7117767Sache /* select our program 7127767Sache */ 7137767Sache if(!check_permission()) 7147767Sache { 7157767Sache fprintf(stderr, "You do not have permission to use %s.\n",namep); 7167767Sache exit(EXIT_FAILURE); 7177767Sache } 7187767Sache switch (program) { 7197767Sache case ATQ: 720941Snate 7217767Sache REDUCE_PRIV(DAEMON_UID, DAEMON_GID) 7227767Sache 7237767Sache list_jobs(); 7247767Sache break; 7257767Sache 7267767Sache case ATRM: 7277767Sache 7287767Sache REDUCE_PRIV(DAEMON_UID, DAEMON_GID) 7297767Sache 73010154Sache process_jobs(argc, argv, ATRM); 7317767Sache break; 7327767Sache 73310154Sache case CAT: 73410154Sache 73510154Sache process_jobs(argc, argv, CAT); 73610154Sache break; 73710154Sache 7387767Sache case AT: 7397767Sache timer = parsetime(argc, argv); 7407767Sache if (atverify) 7417767Sache { 7427767Sache struct tm *tm = localtime(&timer); 7437767Sache fprintf(stderr, "%s\n", asctime(tm)); 744941Snate } 7457767Sache writefile(timer, queue); 7467767Sache break; 7477767Sache 7487767Sache case BATCH: 7497767Sache if (queue_set) 7507767Sache queue = toupper(queue); 7517767Sache else 7527767Sache queue = DEFAULT_BATCH_QUEUE; 7537767Sache 7547767Sache if (argc > optind) 7557767Sache timer = parsetime(argc, argv); 7567767Sache else 7577767Sache timer = time(NULL); 75810154Sache 7597767Sache if (atverify) 7607767Sache { 7617767Sache struct tm *tm = localtime(&timer); 7627767Sache fprintf(stderr, "%s\n", asctime(tm)); 7637767Sache } 7647767Sache 7657767Sache writefile(timer, queue); 7667767Sache break; 7677767Sache 7687767Sache default: 7697767Sache panic("Internal error"); 7707767Sache break; 7717767Sache } 7727767Sache exit(EXIT_SUCCESS); 773941Snate} 774