1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
12 *
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <[email protected]> uunet!decwrl!vixie!paul
16 */
17
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
20 "$FreeBSD$";
21 #endif
22
23 /* vix 26jan87 [RCS'd; rest of log is in RCS file]
24 * vix 01jan87 [added line-level error recovery]
25 * vix 31dec86 [added /step to the from-to range, per bob@acornrc]
26 * vix 30dec86 [written]
27 */
28
29
30 #include "cron.h"
31 #include <grp.h>
32 #ifdef LOGIN_CAP
33 #include <login_cap.h>
34 #endif
35
36 typedef enum ecode {
37 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
38 e_cmd, e_timespec, e_username, e_group, e_option,
39 e_mem
40 #ifdef LOGIN_CAP
41 , e_class
42 #endif
43 } ecode_e;
44
45 static char get_list(bitstr_t *, int, int, char *[], int, FILE *),
46 get_range(bitstr_t *, int, int, char *[], int, FILE *),
47 get_number(int *, int, char *[], int, FILE *);
48 static int set_element(bitstr_t *, int, int, int);
49
50 static char *ecodes[] =
51 {
52 "no error",
53 "bad minute",
54 "bad hour",
55 "bad day-of-month",
56 "bad month",
57 "bad day-of-week",
58 "bad command",
59 "bad time specifier",
60 "bad username",
61 "bad group name",
62 "bad option",
63 "out of memory",
64 #ifdef LOGIN_CAP
65 "bad class name",
66 #endif
67 };
68
69
70 void
free_entry(e)71 free_entry(e)
72 entry *e;
73 {
74 #ifdef LOGIN_CAP
75 if (e->class != NULL)
76 free(e->class);
77 #endif
78 if (e->cmd != NULL)
79 free(e->cmd);
80 if (e->envp != NULL)
81 env_free(e->envp);
82 free(e);
83 }
84
85
86 /* return NULL if eof or syntax error occurs;
87 * otherwise return a pointer to a new entry.
88 */
89 entry *
load_entry(file,error_func,pw,envp)90 load_entry(file, error_func, pw, envp)
91 FILE *file;
92 void (*error_func)(char *);
93 struct passwd *pw;
94 char **envp;
95 {
96 /* this function reads one crontab entry -- the next -- from a file.
97 * it skips any leading blank lines, ignores comments, and returns
98 * EOF if for any reason the entry can't be read and parsed.
99 *
100 * the entry is also parsed here.
101 *
102 * syntax:
103 * user crontab:
104 * minutes hours doms months dows cmd\n
105 * system crontab (/etc/crontab):
106 * minutes hours doms months dows USERNAME cmd\n
107 */
108
109 ecode_e ecode = e_none;
110 entry *e;
111 int ch;
112 char cmd[MAX_COMMAND];
113 char envstr[MAX_ENVSTR];
114 char **prev_env;
115
116 Debug(DPARS, ("load_entry()...about to eat comments\n"))
117
118 skip_comments(file);
119
120 ch = get_char(file);
121 if (ch == EOF)
122 return NULL;
123
124 /* ch is now the first useful character of a useful line.
125 * it may be an @special or it may be the first character
126 * of a list of minutes.
127 */
128
129 e = (entry *) calloc(sizeof(entry), sizeof(char));
130
131 if (e == NULL) {
132 warn("load_entry: calloc failed");
133 return NULL;
134 }
135
136 if (ch == '@') {
137 long interval;
138 char *endptr;
139
140 /* all of these should be flagged and load-limited; i.e.,
141 * instead of @hourly meaning "0 * * * *" it should mean
142 * "close to the front of every hour but not 'til the
143 * system load is low". Problems are: how do you know
144 * what "low" means? (save me from /etc/cron.conf!) and:
145 * how to guarantee low variance (how low is low?), which
146 * means how to we run roughly every hour -- seems like
147 * we need to keep a history or let the first hour set
148 * the schedule, which means we aren't load-limited
149 * anymore. too much for my overloaded brain. (vix, jan90)
150 * HINT
151 */
152 Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
153 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
154 if (!strcmp("reboot", cmd)) {
155 Debug(DPARS, ("load_entry()...reboot shortcut\n"))
156 e->flags |= WHEN_REBOOT;
157 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
158 Debug(DPARS, ("load_entry()...yearly shortcut\n"))
159 bit_set(e->second, 0);
160 bit_set(e->minute, 0);
161 bit_set(e->hour, 0);
162 bit_set(e->dom, 0);
163 bit_set(e->month, 0);
164 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
165 e->flags |= DOW_STAR;
166 } else if (!strcmp("monthly", cmd)) {
167 Debug(DPARS, ("load_entry()...monthly shortcut\n"))
168 bit_set(e->second, 0);
169 bit_set(e->minute, 0);
170 bit_set(e->hour, 0);
171 bit_set(e->dom, 0);
172 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
173 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
174 e->flags |= DOW_STAR;
175 } else if (!strcmp("weekly", cmd)) {
176 Debug(DPARS, ("load_entry()...weekly shortcut\n"))
177 bit_set(e->second, 0);
178 bit_set(e->minute, 0);
179 bit_set(e->hour, 0);
180 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
181 e->flags |= DOM_STAR;
182 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
183 bit_set(e->dow, 0);
184 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
185 Debug(DPARS, ("load_entry()...daily shortcut\n"))
186 bit_set(e->second, 0);
187 bit_set(e->minute, 0);
188 bit_set(e->hour, 0);
189 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
190 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
191 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
192 } else if (!strcmp("hourly", cmd)) {
193 Debug(DPARS, ("load_entry()...hourly shortcut\n"))
194 bit_set(e->second, 0);
195 bit_set(e->minute, 0);
196 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
197 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
198 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
199 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
200 } else if (!strcmp("every_minute", cmd)) {
201 Debug(DPARS, ("load_entry()...every_minute shortcut\n"))
202 bit_set(e->second, 0);
203 bit_nset(e->minute, 0, (LAST_MINUTE-FIRST_MINUTE+1));
204 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
205 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
206 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
207 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
208 } else if (!strcmp("every_second", cmd)) {
209 Debug(DPARS, ("load_entry()...every_second shortcut\n"))
210 e->flags |= SEC_RES;
211 bit_nset(e->second, 0, (LAST_SECOND-FIRST_SECOND+1));
212 bit_nset(e->minute, 0, (LAST_MINUTE-FIRST_MINUTE+1));
213 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
214 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
215 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
216 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
217 } else if (*cmd != '\0' &&
218 (interval = strtol(cmd, &endptr, 10)) > 0 &&
219 *endptr == '\0') {
220 Debug(DPARS, ("load_entry()... %ld seconds "
221 "since last run\n", interval))
222 e->interval = interval;
223 e->flags = INTERVAL;
224 } else {
225 ecode = e_timespec;
226 goto eof;
227 }
228 /* Advance past whitespace between shortcut and
229 * username/command.
230 */
231 Skip_Blanks(ch, file);
232 if (ch == EOF) {
233 ecode = e_cmd;
234 goto eof;
235 }
236 } else {
237 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
238 bit_set(e->second, 0);
239
240 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
241 PPC_NULL, ch, file);
242 if (ch == EOF) {
243 ecode = e_minute;
244 goto eof;
245 }
246
247 /* hours
248 */
249
250 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
251 PPC_NULL, ch, file);
252 if (ch == EOF) {
253 ecode = e_hour;
254 goto eof;
255 }
256
257 /* DOM (days of month)
258 */
259
260 if (ch == '*')
261 e->flags |= DOM_STAR;
262 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
263 PPC_NULL, ch, file);
264 if (ch == EOF) {
265 ecode = e_dom;
266 goto eof;
267 }
268
269 /* month
270 */
271
272 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
273 MonthNames, ch, file);
274 if (ch == EOF) {
275 ecode = e_month;
276 goto eof;
277 }
278
279 /* DOW (days of week)
280 */
281
282 if (ch == '*')
283 e->flags |= DOW_STAR;
284 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
285 DowNames, ch, file);
286 if (ch == EOF) {
287 ecode = e_dow;
288 goto eof;
289 }
290 }
291
292 /* make sundays equivalent */
293 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
294 bit_set(e->dow, 0);
295 bit_set(e->dow, 7);
296 }
297
298 /* ch is the first character of a command, or a username */
299 unget_char(ch, file);
300
301 if (!pw) {
302 char *username = cmd; /* temp buffer */
303 char *s;
304 struct group *grp;
305 #ifdef LOGIN_CAP
306 login_cap_t *lc;
307 #endif
308
309 Debug(DPARS, ("load_entry()...about to parse username\n"))
310 ch = get_string(username, MAX_COMMAND, file, " \t");
311
312 Debug(DPARS, ("load_entry()...got %s\n",username))
313 if (ch == EOF) {
314 ecode = e_cmd;
315 goto eof;
316 }
317
318 /* need to have consumed blanks when checking options below */
319 Skip_Blanks(ch, file)
320 unget_char(ch, file);
321 #ifdef LOGIN_CAP
322 if ((s = strrchr(username, '/')) != NULL) {
323 *s = '\0';
324 e->class = strdup(s + 1);
325 if (e->class == NULL)
326 warn("strdup(\"%s\")", s + 1);
327 } else {
328 e->class = strdup(RESOURCE_RC);
329 if (e->class == NULL)
330 warn("strdup(\"%s\")", RESOURCE_RC);
331 }
332 if (e->class == NULL) {
333 ecode = e_mem;
334 goto eof;
335 }
336 if ((lc = login_getclass(e->class)) == NULL) {
337 ecode = e_class;
338 goto eof;
339 }
340 login_close(lc);
341 #endif
342 grp = NULL;
343 if ((s = strrchr(username, ':')) != NULL) {
344 *s = '\0';
345 if ((grp = getgrnam(s + 1)) == NULL) {
346 ecode = e_group;
347 goto eof;
348 }
349 }
350
351 pw = getpwnam(username);
352 if (pw == NULL) {
353 ecode = e_username;
354 goto eof;
355 }
356 if (grp != NULL)
357 pw->pw_gid = grp->gr_gid;
358 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
359 #ifdef LOGIN_CAP
360 Debug(DPARS, ("load_entry()...class %s\n",e->class))
361 #endif
362 }
363
364 #ifndef PAM /* PAM takes care of account expiration by itself */
365 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
366 ecode = e_username;
367 goto eof;
368 }
369 #endif /* !PAM */
370
371 e->uid = pw->pw_uid;
372 e->gid = pw->pw_gid;
373
374 /* copy and fix up environment. some variables are just defaults and
375 * others are overrides; we process only the overrides here, defaults
376 * are handled in do_command after login.conf is processed.
377 */
378 e->envp = env_copy(envp);
379 if (e->envp == NULL) {
380 warn("env_copy");
381 ecode = e_mem;
382 goto eof;
383 }
384 if (!env_get("SHELL", e->envp)) {
385 prev_env = e->envp;
386 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
387 e->envp = env_set(e->envp, envstr);
388 if (e->envp == NULL) {
389 warn("env_set(%s)", envstr);
390 env_free(prev_env);
391 ecode = e_mem;
392 goto eof;
393 }
394 }
395 /* If LOGIN_CAP, this is deferred to do_command where the login class
396 * is processed. If !LOGIN_CAP, do it here.
397 */
398 #ifndef LOGIN_CAP
399 if (!env_get("HOME", e->envp)) {
400 prev_env = e->envp;
401 sprintf(envstr, "HOME=%s", pw->pw_dir);
402 e->envp = env_set(e->envp, envstr);
403 if (e->envp == NULL) {
404 warn("env_set(%s)", envstr);
405 env_free(prev_env);
406 ecode = e_mem;
407 goto eof;
408 }
409 }
410 #endif
411 prev_env = e->envp;
412 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
413 e->envp = env_set(e->envp, envstr);
414 if (e->envp == NULL) {
415 warn("env_set(%s)", envstr);
416 env_free(prev_env);
417 ecode = e_mem;
418 goto eof;
419 }
420 #if defined(BSD)
421 prev_env = e->envp;
422 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
423 e->envp = env_set(e->envp, envstr);
424 if (e->envp == NULL) {
425 warn("env_set(%s)", envstr);
426 env_free(prev_env);
427 ecode = e_mem;
428 goto eof;
429 }
430 #endif
431
432 Debug(DPARS, ("load_entry()...checking for command options\n"))
433
434 ch = get_char(file);
435
436 while (ch == '-') {
437 Debug(DPARS|DEXT, ("load_entry()...expecting option\n"))
438 switch (ch = get_char(file)) {
439 case 'n':
440 Debug(DPARS|DEXT, ("load_entry()...got MAIL_WHEN_ERR ('n') option\n"))
441 /* only allow the user to set the option once */
442 if ((e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR) {
443 Debug(DPARS|DEXT, ("load_entry()...duplicate MAIL_WHEN_ERR ('n') option\n"))
444 ecode = e_option;
445 goto eof;
446 }
447 e->flags |= MAIL_WHEN_ERR;
448 break;
449 case 'q':
450 Debug(DPARS|DEXT, ("load_entry()...got DONT_LOG ('q') option\n"))
451 /* only allow the user to set the option once */
452 if ((e->flags & DONT_LOG) == DONT_LOG) {
453 Debug(DPARS|DEXT, ("load_entry()...duplicate DONT_LOG ('q') option\n"))
454 ecode = e_option;
455 goto eof;
456 }
457 e->flags |= DONT_LOG;
458 break;
459 default:
460 Debug(DPARS|DEXT, ("load_entry()...invalid option '%c'\n", ch))
461 ecode = e_option;
462 goto eof;
463 }
464 ch = get_char(file);
465 if (ch!='\t' && ch!=' ') {
466 ecode = e_option;
467 goto eof;
468 }
469
470 Skip_Blanks(ch, file)
471 if (ch == EOF || ch == '\n') {
472 ecode = e_cmd;
473 goto eof;
474 }
475 }
476
477 unget_char(ch, file);
478
479 Debug(DPARS, ("load_entry()...about to parse command\n"))
480
481 /* Everything up to the next \n or EOF is part of the command...
482 * too bad we don't know in advance how long it will be, since we
483 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
484 * XXX - should use realloc().
485 */
486 ch = get_string(cmd, MAX_COMMAND, file, "\n");
487
488 /* a file without a \n before the EOF is rude, so we'll complain...
489 */
490 if (ch == EOF) {
491 ecode = e_cmd;
492 goto eof;
493 }
494
495 /* got the command in the 'cmd' string; save it in *e.
496 */
497 e->cmd = strdup(cmd);
498 if (e->cmd == NULL) {
499 warn("strdup(\"%s\")", cmd);
500 ecode = e_mem;
501 goto eof;
502 }
503 Debug(DPARS, ("load_entry()...returning successfully\n"))
504
505 /* success, fini, return pointer to the entry we just created...
506 */
507 return e;
508
509 eof:
510 free_entry(e);
511 if (ecode != e_none && error_func)
512 (*error_func)(ecodes[(int)ecode]);
513 while (ch != EOF && ch != '\n')
514 ch = get_char(file);
515 return NULL;
516 }
517
518
519 static char
get_list(bits,low,high,names,ch,file)520 get_list(bits, low, high, names, ch, file)
521 bitstr_t *bits; /* one bit per flag, default=FALSE */
522 int low, high; /* bounds, impl. offset for bitstr */
523 char *names[]; /* NULL or *[] of names for these elements */
524 int ch; /* current character being processed */
525 FILE *file; /* file being read */
526 {
527 register int done;
528
529 /* we know that we point to a non-blank character here;
530 * must do a Skip_Blanks before we exit, so that the
531 * next call (or the code that picks up the cmd) can
532 * assume the same thing.
533 */
534
535 Debug(DPARS|DEXT, ("get_list()...entered\n"))
536
537 /* list = range {"," range}
538 */
539
540 /* clear the bit string, since the default is 'off'.
541 */
542 bit_nclear(bits, 0, (high-low+1));
543
544 /* process all ranges
545 */
546 done = FALSE;
547 while (!done) {
548 ch = get_range(bits, low, high, names, ch, file);
549 if (ch == ',')
550 ch = get_char(file);
551 else
552 done = TRUE;
553 }
554
555 /* exiting. skip to some blanks, then skip over the blanks.
556 */
557 Skip_Nonblanks(ch, file)
558 Skip_Blanks(ch, file)
559
560 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
561
562 return ch;
563 }
564
565
566 static char
get_range(bits,low,high,names,ch,file)567 get_range(bits, low, high, names, ch, file)
568 bitstr_t *bits; /* one bit per flag, default=FALSE */
569 int low, high; /* bounds, impl. offset for bitstr */
570 char *names[]; /* NULL or names of elements */
571 int ch; /* current character being processed */
572 FILE *file; /* file being read */
573 {
574 /* range = number | number "-" number [ "/" number ]
575 */
576
577 register int i;
578 auto int num1, num2, num3;
579
580 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
581
582 if (ch == '*') {
583 /* '*' means "first-last" but can still be modified by /step
584 */
585 num1 = low;
586 num2 = high;
587 ch = get_char(file);
588 if (ch == EOF)
589 return EOF;
590 } else {
591 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
592 return EOF;
593
594 if (ch == '/')
595 num2 = high;
596 else if (ch != '-') {
597 /* not a range, it's a single number.
598 */
599 if (EOF == set_element(bits, low, high, num1))
600 return EOF;
601 return ch;
602 } else {
603 /* eat the dash
604 */
605 ch = get_char(file);
606 if (ch == EOF)
607 return EOF;
608
609 /* get the number following the dash
610 */
611 ch = get_number(&num2, low, names, ch, file);
612 if (ch == EOF)
613 return EOF;
614 }
615 }
616
617 /* check for step size
618 */
619 if (ch == '/') {
620 /* eat the slash
621 */
622 ch = get_char(file);
623 if (ch == EOF)
624 return EOF;
625
626 /* get the step size -- note: we don't pass the
627 * names here, because the number is not an
628 * element id, it's a step size. 'low' is
629 * sent as a 0 since there is no offset either.
630 */
631 ch = get_number(&num3, 0, PPC_NULL, ch, file);
632 if (ch == EOF || num3 == 0)
633 return EOF;
634 } else {
635 /* no step. default==1.
636 */
637 num3 = 1;
638 }
639
640 /* range. set all elements from num1 to num2, stepping
641 * by num3. (the step is a downward-compatible extension
642 * proposed conceptually by bob@acornrc, syntactically
643 * designed then implmented by paul vixie).
644 */
645 for (i = num1; i <= num2; i += num3)
646 if (EOF == set_element(bits, low, high, i))
647 return EOF;
648
649 return ch;
650 }
651
652
653 static char
get_number(numptr,low,names,ch,file)654 get_number(numptr, low, names, ch, file)
655 int *numptr; /* where does the result go? */
656 int low; /* offset applied to result if symbolic enum used */
657 char *names[]; /* symbolic names, if any, for enums */
658 int ch; /* current character */
659 FILE *file; /* source */
660 {
661 char temp[MAX_TEMPSTR], *pc;
662 int len, i, all_digits;
663
664 /* collect alphanumerics into our fixed-size temp array
665 */
666 pc = temp;
667 len = 0;
668 all_digits = TRUE;
669 while (isalnum(ch)) {
670 if (++len >= MAX_TEMPSTR)
671 return EOF;
672
673 *pc++ = ch;
674
675 if (!isdigit(ch))
676 all_digits = FALSE;
677
678 ch = get_char(file);
679 }
680 *pc = '\0';
681 if (len == 0)
682 return (EOF);
683
684 /* try to find the name in the name list
685 */
686 if (names) {
687 for (i = 0; names[i] != NULL; i++) {
688 Debug(DPARS|DEXT,
689 ("get_num, compare(%s,%s)\n", names[i], temp))
690 if (!strcasecmp(names[i], temp)) {
691 *numptr = i+low;
692 return ch;
693 }
694 }
695 }
696
697 /* no name list specified, or there is one and our string isn't
698 * in it. either way: if it's all digits, use its magnitude.
699 * otherwise, it's an error.
700 */
701 if (all_digits) {
702 *numptr = atoi(temp);
703 return ch;
704 }
705
706 return EOF;
707 }
708
709
710 static int
set_element(bits,low,high,number)711 set_element(bits, low, high, number)
712 bitstr_t *bits; /* one bit per flag, default=FALSE */
713 int low;
714 int high;
715 int number;
716 {
717 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
718
719 if (number < low || number > high)
720 return EOF;
721
722 bit_set(bits, (number-low));
723 return OK;
724 }
725