1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
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
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 <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
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]
33 #include <login_cap.h>
37 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
38 e_cmd, e_timespec, e_username, e_group, e_option,
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);
50 static char *ecodes[] =
86 /* return NULL if eof or syntax error occurs;
87 * otherwise return a pointer to a new entry.
90 load_entry(file, error_func, pw, envp)
92 void (*error_func)(char *);
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.
100 * the entry is also parsed here.
104 * minutes hours doms months dows cmd\n
105 * system crontab (/etc/crontab):
106 * minutes hours doms months dows USERNAME cmd\n
109 ecode_e ecode = e_none;
112 char cmd[MAX_COMMAND];
113 char envstr[MAX_ENVSTR];
116 Debug(DPARS, ("load_entry()...about to eat comments\n"))
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.
129 e = (entry *) calloc(sizeof(entry), sizeof(char));
132 warn("load_entry: calloc failed");
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)
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);
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);
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);
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));
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);
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"))
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 &&
220 Debug(DPARS, ("load_entry()... %ld seconds "
221 "since last run\n", interval))
222 e->interval = interval;
228 /* Advance past whitespace between shortcut and
231 Skip_Blanks(ch, file);
237 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
238 bit_set(e->second, 0);
240 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
250 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
257 /* DOM (days of month)
261 e->flags |= DOM_STAR;
262 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
272 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
273 MonthNames, ch, file);
279 /* DOW (days of week)
283 e->flags |= DOW_STAR;
284 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
292 /* make sundays equivalent */
293 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
298 /* ch is the first character of a command, or a username */
299 unget_char(ch, file);
302 char *username = cmd; /* temp buffer */
309 Debug(DPARS, ("load_entry()...about to parse username\n"))
310 ch = get_string(username, MAX_COMMAND, file, " \t");
312 Debug(DPARS, ("load_entry()...got %s\n",username))
319 if ((s = strrchr(username, '/')) != NULL) {
321 e->class = strdup(s + 1);
322 if (e->class == NULL)
323 warn("strdup(\"%s\")", s + 1);
325 e->class = strdup(RESOURCE_RC);
326 if (e->class == NULL)
327 warn("strdup(\"%s\")", RESOURCE_RC);
329 if (e->class == NULL) {
333 if ((lc = login_getclass(e->class)) == NULL) {
340 if ((s = strrchr(username, ':')) != NULL) {
342 if ((grp = getgrnam(s + 1)) == NULL) {
348 pw = getpwnam(username);
354 pw->pw_gid = grp->gr_gid;
355 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
357 Debug(DPARS, ("load_entry()...class %s\n",e->class))
361 #ifndef PAM /* PAM takes care of account expiration by itself */
362 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
371 /* copy and fix up environment. some variables are just defaults and
372 * others are overrides.
374 e->envp = env_copy(envp);
375 if (e->envp == NULL) {
380 if (!env_get("SHELL", e->envp)) {
382 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
383 e->envp = env_set(e->envp, envstr);
384 if (e->envp == NULL) {
385 warn("env_set(%s)", envstr);
391 if (!env_get("HOME", e->envp)) {
393 sprintf(envstr, "HOME=%s", pw->pw_dir);
394 e->envp = env_set(e->envp, envstr);
395 if (e->envp == NULL) {
396 warn("env_set(%s)", envstr);
402 if (!env_get("PATH", e->envp)) {
404 sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
405 e->envp = env_set(e->envp, envstr);
406 if (e->envp == NULL) {
407 warn("env_set(%s)", envstr);
414 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
415 e->envp = env_set(e->envp, envstr);
416 if (e->envp == NULL) {
417 warn("env_set(%s)", envstr);
424 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
425 e->envp = env_set(e->envp, envstr);
426 if (e->envp == NULL) {
427 warn("env_set(%s)", envstr);
434 Debug(DPARS, ("load_entry()...checking for command options\n"))
439 Debug(DPARS|DEXT, ("load_entry()...expecting option\n"))
440 switch (ch = get_char(file)) {
442 Debug(DPARS|DEXT, ("load_entry()...got MAIL_WHEN_ERR ('n') option\n"))
443 /* only allow the user to set the option once */
444 if ((e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR) {
445 Debug(DPARS|DEXT, ("load_entry()...duplicate MAIL_WHEN_ERR ('n') option\n"))
449 e->flags |= MAIL_WHEN_ERR;
452 Debug(DPARS|DEXT, ("load_entry()...got DONT_LOG ('q') option\n"))
453 /* only allow the user to set the option once */
454 if ((e->flags & DONT_LOG) == DONT_LOG) {
455 Debug(DPARS|DEXT, ("load_entry()...duplicate DONT_LOG ('q') option\n"))
459 e->flags |= DONT_LOG;
462 Debug(DPARS|DEXT, ("load_entry()...invalid option '%c'\n", ch))
467 if (ch!='\t' && ch!=' ') {
472 Skip_Blanks(ch, file)
473 if (ch == EOF || ch == '\n') {
479 unget_char(ch, file);
481 Debug(DPARS, ("load_entry()...about to parse command\n"))
483 /* Everything up to the next \n or EOF is part of the command...
484 * too bad we don't know in advance how long it will be, since we
485 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
486 * XXX - should use realloc().
488 ch = get_string(cmd, MAX_COMMAND, file, "\n");
490 /* a file without a \n before the EOF is rude, so we'll complain...
497 /* got the command in the 'cmd' string; save it in *e.
499 e->cmd = strdup(cmd);
500 if (e->cmd == NULL) {
501 warn("strdup(\"%s\")", cmd);
505 Debug(DPARS, ("load_entry()...returning successfully\n"))
507 /* success, fini, return pointer to the entry we just created...
513 if (ecode != e_none && error_func)
514 (*error_func)(ecodes[(int)ecode]);
515 while (ch != EOF && ch != '\n')
522 get_list(bits, low, high, names, ch, file)
523 bitstr_t *bits; /* one bit per flag, default=FALSE */
524 int low, high; /* bounds, impl. offset for bitstr */
525 char *names[]; /* NULL or *[] of names for these elements */
526 int ch; /* current character being processed */
527 FILE *file; /* file being read */
531 /* we know that we point to a non-blank character here;
532 * must do a Skip_Blanks before we exit, so that the
533 * next call (or the code that picks up the cmd) can
534 * assume the same thing.
537 Debug(DPARS|DEXT, ("get_list()...entered\n"))
539 /* list = range {"," range}
542 /* clear the bit string, since the default is 'off'.
544 bit_nclear(bits, 0, (high-low+1));
546 /* process all ranges
550 ch = get_range(bits, low, high, names, ch, file);
557 /* exiting. skip to some blanks, then skip over the blanks.
559 Skip_Nonblanks(ch, file)
560 Skip_Blanks(ch, file)
562 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
569 get_range(bits, low, high, names, ch, file)
570 bitstr_t *bits; /* one bit per flag, default=FALSE */
571 int low, high; /* bounds, impl. offset for bitstr */
572 char *names[]; /* NULL or names of elements */
573 int ch; /* current character being processed */
574 FILE *file; /* file being read */
576 /* range = number | number "-" number [ "/" number ]
580 auto int num1, num2, num3;
582 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
585 /* '*' means "first-last" but can still be modified by /step
593 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
598 else if (ch != '-') {
599 /* not a range, it's a single number.
601 if (EOF == set_element(bits, low, high, num1))
611 /* get the number following the dash
613 ch = get_number(&num2, low, names, ch, file);
619 /* check for step size
628 /* get the step size -- note: we don't pass the
629 * names here, because the number is not an
630 * element id, it's a step size. 'low' is
631 * sent as a 0 since there is no offset either.
633 ch = get_number(&num3, 0, PPC_NULL, ch, file);
634 if (ch == EOF || num3 == 0)
637 /* no step. default==1.
642 /* range. set all elements from num1 to num2, stepping
643 * by num3. (the step is a downward-compatible extension
644 * proposed conceptually by bob@acornrc, syntactically
645 * designed then implmented by paul vixie).
647 for (i = num1; i <= num2; i += num3)
648 if (EOF == set_element(bits, low, high, i))
656 get_number(numptr, low, names, ch, file)
657 int *numptr; /* where does the result go? */
658 int low; /* offset applied to result if symbolic enum used */
659 char *names[]; /* symbolic names, if any, for enums */
660 int ch; /* current character */
661 FILE *file; /* source */
663 char temp[MAX_TEMPSTR], *pc;
664 int len, i, all_digits;
666 /* collect alphanumerics into our fixed-size temp array
671 while (isalnum(ch)) {
672 if (++len >= MAX_TEMPSTR)
686 /* try to find the name in the name list
689 for (i = 0; names[i] != NULL; i++) {
691 ("get_num, compare(%s,%s)\n", names[i], temp))
692 if (!strcasecmp(names[i], temp)) {
699 /* no name list specified, or there is one and our string isn't
700 * in it. either way: if it's all digits, use its magnitude.
701 * otherwise, it's an error.
704 *numptr = atoi(temp);
713 set_element(bits, low, high, number)
714 bitstr_t *bits; /* one bit per flag, default=FALSE */
719 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
721 if (number < low || number > high)
724 bit_set(bits, (number-low));