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