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