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
|
/* $OpenBSD: entry.c,v 1.9 2002/06/14 21:35:01 todd Exp $ */
/*
* Copyright 1988,1990,1993,1994 by Paul Vixie
* All rights reserved
*/
/*
* Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#if !defined(lint) && !defined(LINT)
static char rcsid[] = "$OpenBSD: entry.c,v 1.9 2002/06/14 21:35:01 todd Exp $";
#endif
/* vix 26jan87 [RCS'd; rest of log is in RCS file]
* vix 01jan87 [added line-level error recovery]
* vix 31dec86 [added /step to the from-to range, per bob@acornrc]
* vix 30dec86 [written]
*/
#include "cron.h"
typedef enum ecode {
e_none, e_minute, e_hour, e_dom, e_month, e_dow,
e_cmd, e_timespec, e_username, e_option
} ecode_e;
static const char *ecodes[] =
{
"no error",
"bad minute",
"bad hour",
"bad day-of-month",
"bad month",
"bad day-of-week",
"bad command",
"bad time specifier",
"bad username",
"bad option"
};
static char get_list(bitstr_t *, int, int, const char *[], int, FILE *),
get_range(bitstr_t *, int, int, const char *[], int, FILE *),
get_number(int *, int, const char *[], int, FILE *);
static int set_element(bitstr_t *, int, int, int);
void
free_entry(e)
entry *e;
{
free(e->cmd);
env_free(e->envp);
free(e);
}
/* return NULL if eof or syntax error occurs;
* otherwise return a pointer to a new entry.
*/
entry *
load_entry(file, error_func, pw, envp)
FILE *file;
void (*error_func)();
struct passwd *pw;
char **envp;
{
/* this function reads one crontab entry -- the next -- from a file.
* it skips any leading blank lines, ignores comments, and returns
* EOF if for any reason the entry can't be read and parsed.
*
* the entry is also parsed here.
*
* syntax:
* user crontab:
* minutes hours doms months dows cmd\n
* system crontab (/etc/crontab):
* minutes hours doms months dows USERNAME cmd\n
*/
ecode_e ecode = e_none;
entry *e;
int ch;
char cmd[MAX_COMMAND];
char envstr[MAX_ENVSTR];
char **tenvp;
Debug(DPARS, ("load_entry()...about to eat comments\n"))
skip_comments(file);
ch = get_char(file);
if (ch == EOF)
return NULL;
/* ch is now the first useful character of a useful line.
* it may be an @special or it may be the first character
* of a list of minutes.
*/
e = (entry *) calloc(sizeof(entry), sizeof(char));
if (ch == '@') {
/* all of these should be flagged and load-limited; i.e.,
* instead of @hourly meaning "0 * * * *" it should mean
* "close to the front of every hour but not 'til the
* system load is low". Problems are: how do you know
* what "low" means? (save me from /etc/cron.conf!) and:
* how to guarantee low variance (how low is low?), which
* means how to we run roughly every hour -- seems like
* we need to keep a history or let the first hour set
* the schedule, which means we aren't load-limited
* anymore. too much for my overloaded brain. (vix, jan90)
* HINT
*/
ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
if (!strcmp("reboot", cmd)) {
e->flags |= WHEN_REBOOT;
} else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_set(e->dom, 0);
bit_set(e->month, 0);
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
} else if (!strcmp("monthly", cmd)) {
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_set(e->dom, 0);
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
} else if (!strcmp("weekly", cmd)) {
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_set(e->dow, 0);
} else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
bit_set(e->minute, 0);
bit_set(e->hour, 0);
bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
} else if (!strcmp("hourly", cmd)) {
bit_set(e->minute, 0);
bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
e->flags |= HR_STAR;
} else {
ecode = e_timespec;
goto eof;
}
/* Advance past whitespace between shortcut and
* username/command.
*/
Skip_Blanks(ch, file);
if (ch == EOF) {
ecode = e_cmd;
goto eof;
}
} else {
Debug(DPARS, ("load_entry()...about to parse numerics\n"))
if (ch == '*')
e->flags |= MIN_STAR;
ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
PPC_NULL, ch, file);
if (ch == EOF) {
ecode = e_minute;
goto eof;
}
/* hours
*/
if (ch == '*')
e->flags |= HR_STAR;
ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
PPC_NULL, ch, file);
if (ch == EOF) {
ecode = e_hour;
goto eof;
}
/* DOM (days of month)
*/
if (ch == '*')
e->flags |= DOM_STAR;
ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
PPC_NULL, ch, file);
if (ch == EOF) {
ecode = e_dom;
goto eof;
}
/* month
*/
ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
MonthNames, ch, file);
if (ch == EOF) {
ecode = e_month;
goto eof;
}
/* DOW (days of week)
*/
if (ch == '*')
e->flags |= DOW_STAR;
ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
DowNames, ch, file);
if (ch == EOF) {
ecode = e_dow;
goto eof;
}
}
/* make sundays equivalent */
if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
bit_set(e->dow, 0);
bit_set(e->dow, 7);
}
/* ch is the first character of a command, or a username */
unget_char(ch, file);
if (!pw) {
char *username = cmd; /* temp buffer */
Debug(DPARS, ("load_entry()...about to parse username\n"))
ch = get_string(username, MAX_COMMAND, file, " \t");
Debug(DPARS, ("load_entry()...got %s\n",username))
if (ch == EOF) {
ecode = e_cmd;
goto eof;
}
pw = getpwnam(username);
if (pw == NULL) {
ecode = e_username;
goto eof;
}
Debug(DPARS, ("load_entry()...uid %ld, gid %ld\n",
(long)e->uid, (long)e->gid))
} else if (ch == '*') {
ecode = e_cmd;
goto eof;
}
e->uid = pw->pw_uid;
e->gid = pw->pw_gid;
/* copy and fix up environment. some variables are just defaults and
* others are overrides.
*/
if ((e->envp = env_copy(envp)) == NULL) {
ecode = e_none;
goto eof;
}
if (!env_get("SHELL", e->envp)) {
if (glue_strings(envstr, sizeof envstr, "SHELL",
_PATH_BSHELL, '=')) {
if ((tenvp = env_set(e->envp, envstr))) {
e->envp = tenvp;
} else {
ecode = e_none;
goto eof;
}
} else
log_it("CRON", getpid(), "error", "can't set SHELL");
}
if (!env_get("HOME", e->envp)) {
if (glue_strings(envstr, sizeof envstr, "HOME",
pw->pw_dir, '=')) {
if ((tenvp = env_set(e->envp, envstr))) {
e->envp = tenvp;
} else {
ecode = e_none;
goto eof;
}
} else
log_it("CRON", getpid(), "error", "can't set HOME");
}
#ifdef LOGIN_CAP
if (!env_get("PATH", e->envp)) {
if (glue_strings(envstr, sizeof envstr, "PATH",
_PATH_DEFPATH, '=')) {
if ((tenvp = env_set(e->envp, envstr))) {
e->envp = tenvp;
} else {
ecode = e_none;
goto eof;
}
} else
log_it("CRON", getpid(), "error", "can't set PATH");
}
#endif /* LOGIN_CAP */
if (glue_strings(envstr, sizeof envstr, "LOGNAME",
pw->pw_name, '=')) {
if ((tenvp = env_set(e->envp, envstr))) {
e->envp = tenvp;
} else {
ecode = e_none;
goto eof;
}
} else
log_it("CRON", getpid(), "error", "can't set LOGNAME");
#if defined(BSD)
if (glue_strings(envstr, sizeof envstr, "USER",
pw->pw_name, '=')) {
if ((tenvp = env_set(e->envp, envstr))) {
e->envp = tenvp;
} else {
ecode = e_none;
goto eof;
}
} else
log_it("CRON", getpid(), "error", "can't set USER");
#endif
Debug(DPARS, ("load_entry()...about to parse command\n"))
/* If the first character of the command is '-' it is a cron option.
*/
while ((ch = get_char(file)) == '-') {
switch (ch = get_char(file)) {
case 'q':
e->flags |= DONT_LOG;
Skip_Nonblanks(ch, file)
break;
default:
ecode = e_option;
goto eof;
}
Skip_Blanks(ch, file)
}
unget_char(ch, file);
/* Everything up to the next \n or EOF is part of the command...
* too bad we don't know in advance how long it will be, since we
* need to malloc a string for it... so, we limit it to MAX_COMMAND.
*/
ch = get_string(cmd, MAX_COMMAND, file, "\n");
/* a file without a \n before the EOF is rude, so we'll complain...
*/
if (ch == EOF) {
ecode = e_cmd;
goto eof;
}
/* got the command in the 'cmd' string; save it in *e.
*/
if ((e->cmd = strdup(cmd)) == NULL) {
ecode = e_none;
goto eof;
}
Debug(DPARS, ("load_entry()...returning successfully\n"))
/* success, fini, return pointer to the entry we just created...
*/
return e;
eof:
if (e->envp)
env_free(e->envp);
if (e->cmd)
free(e->cmd);
free(e);
if (ecode != e_none && error_func)
(*error_func)(ecodes[(int)ecode]);
while (ch != EOF && ch != '\n')
ch = get_char(file);
return NULL;
}
static char
get_list(bits, low, high, names, ch, file)
bitstr_t *bits; /* one bit per flag, default=FALSE */
int low, high; /* bounds, impl. offset for bitstr */
const char *names[]; /* NULL or *[] of names for these elements */
int ch; /* current character being processed */
FILE *file; /* file being read */
{
int done;
/* we know that we point to a non-blank character here;
* must do a Skip_Blanks before we exit, so that the
* next call (or the code that picks up the cmd) can
* assume the same thing.
*/
Debug(DPARS|DEXT, ("get_list()...entered\n"))
/* list = range {"," range}
*/
/* clear the bit string, since the default is 'off'.
*/
bit_nclear(bits, 0, (high-low+1));
/* process all ranges
*/
done = FALSE;
while (!done) {
ch = get_range(bits, low, high, names, ch, file);
if (ch == ',')
ch = get_char(file);
else
done = TRUE;
}
/* exiting. skip to some blanks, then skip over the blanks.
*/
Skip_Nonblanks(ch, file)
Skip_Blanks(ch, file)
Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
return ch;
}
static char
get_range(bits, low, high, names, ch, file)
bitstr_t *bits; /* one bit per flag, default=FALSE */
int low, high; /* bounds, impl. offset for bitstr */
const char *names[]; /* NULL or names of elements */
int ch; /* current character being processed */
FILE *file; /* file being read */
{
/* range = number | number "-" number [ "/" number ]
*/
int i, num1, num2, num3;
Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
if (ch == '*') {
/* '*' means "first-last" but can still be modified by /step
*/
num1 = low;
num2 = high;
ch = get_char(file);
if (ch == EOF)
return EOF;
} else {
if (EOF == (ch = get_number(&num1, low, names, ch, file)))
return EOF;
if (ch != '-') {
/* not a range, it's a single number.
*/
if (EOF == set_element(bits, low, high, num1))
return EOF;
return ch;
} else {
/* eat the dash
*/
ch = get_char(file);
if (ch == EOF)
return EOF;
/* get the number following the dash
*/
ch = get_number(&num2, low, names, ch, file);
if (ch == EOF)
return EOF;
}
}
/* check for step size
*/
if (ch == '/') {
/* eat the slash
*/
ch = get_char(file);
if (ch == EOF)
return EOF;
/* get the step size -- note: we don't pass the
* names here, because the number is not an
* element id, it's a step size. 'low' is
* sent as a 0 since there is no offset either.
*/
ch = get_number(&num3, 0, PPC_NULL, ch, file);
if (ch == EOF)
return EOF;
} else {
/* no step. default==1.
*/
num3 = 1;
}
/* range. set all elements from num1 to num2, stepping
* by num3. (the step is a downward-compatible extension
* proposed conceptually by bob@acornrc, syntactically
* designed then implemented by paul vixie).
*/
for (i = num1; i <= num2; i += num3)
if (EOF == set_element(bits, low, high, i))
return EOF;
return ch;
}
static char
get_number(numptr, low, names, ch, file)
int *numptr; /* where does the result go? */
int low; /* offset applied to enum result */
const char *names[]; /* symbolic names, if any, for enums */
int ch; /* current character */
FILE *file; /* source */
{
char temp[MAX_TEMPSTR], *pc;
int len, i, all_digits;
/* collect alphanumerics into our fixed-size temp array
*/
pc = temp;
len = 0;
all_digits = TRUE;
while (isalnum(ch)) {
if (++len >= MAX_TEMPSTR)
return EOF;
*pc++ = ch;
if (!isdigit(ch))
all_digits = FALSE;
ch = get_char(file);
}
*pc = '\0';
/* try to find the name in the name list
*/
if (names) {
for (i = 0; names[i] != NULL; i++) {
Debug(DPARS|DEXT,
("get_num, compare(%s,%s)\n", names[i], temp))
if (!strcasecmp(names[i], temp)) {
*numptr = i+low;
return ch;
}
}
}
/* no name list specified, or there is one and our string isn't
* in it. either way: if it's all digits, use its magnitude.
* otherwise, it's an error.
*/
if (all_digits) {
*numptr = atoi(temp);
return ch;
}
return EOF;
}
static int
set_element(bits, low, high, number)
bitstr_t *bits; /* one bit per flag, default=FALSE */
int low;
int high;
int number;
{
Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
if (number < low || number > high)
return EOF;
bit_set(bits, (number-low));
return OK;
}
|