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; we process only the overrides here, defaults
373 * are handled in do_command after login.conf is processed.
375 e->envp = env_copy(envp);
376 if (e->envp == NULL) {
381 if (!env_get("SHELL", e->envp)) {
383 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
384 e->envp = env_set(e->envp, envstr);
385 if (e->envp == NULL) {
386 warn("env_set(%s)", envstr);
392 /* If LOGIN_CAP, this is deferred to do_command where the login class
393 * is processed. If !LOGIN_CAP, do it here.
396 if (!env_get("HOME", e->envp)) {
398 sprintf(envstr, "HOME=%s", pw->pw_dir);
399 e->envp = env_set(e->envp, envstr);
400 if (e->envp == NULL) {
401 warn("env_set(%s)", envstr);
409 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
410 e->envp = env_set(e->envp, envstr);
411 if (e->envp == NULL) {
412 warn("env_set(%s)", envstr);
419 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
420 e->envp = env_set(e->envp, envstr);
421 if (e->envp == NULL) {
422 warn("env_set(%s)", envstr);
429 Debug(DPARS, ("load_entry()...checking for command options\n"))
434 Debug(DPARS|DEXT, ("load_entry()...expecting option\n"))
435 switch (ch = get_char(file)) {
437 Debug(DPARS|DEXT, ("load_entry()...got MAIL_WHEN_ERR ('n') option\n"))
438 /* only allow the user to set the option once */
439 if ((e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR) {
440 Debug(DPARS|DEXT, ("load_entry()...duplicate MAIL_WHEN_ERR ('n') option\n"))
444 e->flags |= MAIL_WHEN_ERR;
447 Debug(DPARS|DEXT, ("load_entry()...got DONT_LOG ('q') option\n"))
448 /* only allow the user to set the option once */
449 if ((e->flags & DONT_LOG) == DONT_LOG) {
450 Debug(DPARS|DEXT, ("load_entry()...duplicate DONT_LOG ('q') option\n"))
454 e->flags |= DONT_LOG;
457 Debug(DPARS|DEXT, ("load_entry()...invalid option '%c'\n", ch))
462 if (ch!='\t' && ch!=' ') {
467 Skip_Blanks(ch, file)
468 if (ch == EOF || ch == '\n') {
474 unget_char(ch, file);
476 Debug(DPARS, ("load_entry()...about to parse command\n"))
478 /* Everything up to the next \n or EOF is part of the command...
479 * too bad we don't know in advance how long it will be, since we
480 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
481 * XXX - should use realloc().
483 ch = get_string(cmd, MAX_COMMAND, file, "\n");
485 /* a file without a \n before the EOF is rude, so we'll complain...
492 /* got the command in the 'cmd' string; save it in *e.
494 e->cmd = strdup(cmd);
495 if (e->cmd == NULL) {
496 warn("strdup(\"%s\")", cmd);
500 Debug(DPARS, ("load_entry()...returning successfully\n"))
502 /* success, fini, return pointer to the entry we just created...
508 if (ecode != e_none && error_func)
509 (*error_func)(ecodes[(int)ecode]);
510 while (ch != EOF && ch != '\n')
517 get_list(bits, low, high, names, ch, file)
518 bitstr_t *bits; /* one bit per flag, default=FALSE */
519 int low, high; /* bounds, impl. offset for bitstr */
520 char *names[]; /* NULL or *[] of names for these elements */
521 int ch; /* current character being processed */
522 FILE *file; /* file being read */
526 /* we know that we point to a non-blank character here;
527 * must do a Skip_Blanks before we exit, so that the
528 * next call (or the code that picks up the cmd) can
529 * assume the same thing.
532 Debug(DPARS|DEXT, ("get_list()...entered\n"))
534 /* list = range {"," range}
537 /* clear the bit string, since the default is 'off'.
539 bit_nclear(bits, 0, (high-low+1));
541 /* process all ranges
545 ch = get_range(bits, low, high, names, ch, file);
552 /* exiting. skip to some blanks, then skip over the blanks.
554 Skip_Nonblanks(ch, file)
555 Skip_Blanks(ch, file)
557 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
564 get_range(bits, low, high, names, ch, file)
565 bitstr_t *bits; /* one bit per flag, default=FALSE */
566 int low, high; /* bounds, impl. offset for bitstr */
567 char *names[]; /* NULL or names of elements */
568 int ch; /* current character being processed */
569 FILE *file; /* file being read */
571 /* range = number | number "-" number [ "/" number ]
575 auto int num1, num2, num3;
577 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
580 /* '*' means "first-last" but can still be modified by /step
588 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
593 else if (ch != '-') {
594 /* not a range, it's a single number.
596 if (EOF == set_element(bits, low, high, num1))
606 /* get the number following the dash
608 ch = get_number(&num2, low, names, ch, file);
614 /* check for step size
623 /* get the step size -- note: we don't pass the
624 * names here, because the number is not an
625 * element id, it's a step size. 'low' is
626 * sent as a 0 since there is no offset either.
628 ch = get_number(&num3, 0, PPC_NULL, ch, file);
629 if (ch == EOF || num3 == 0)
632 /* no step. default==1.
637 /* range. set all elements from num1 to num2, stepping
638 * by num3. (the step is a downward-compatible extension
639 * proposed conceptually by bob@acornrc, syntactically
640 * designed then implmented by paul vixie).
642 for (i = num1; i <= num2; i += num3)
643 if (EOF == set_element(bits, low, high, i))
651 get_number(numptr, low, names, ch, file)
652 int *numptr; /* where does the result go? */
653 int low; /* offset applied to result if symbolic enum used */
654 char *names[]; /* symbolic names, if any, for enums */
655 int ch; /* current character */
656 FILE *file; /* source */
658 char temp[MAX_TEMPSTR], *pc;
659 int len, i, all_digits;
661 /* collect alphanumerics into our fixed-size temp array
666 while (isalnum(ch)) {
667 if (++len >= MAX_TEMPSTR)
681 /* try to find the name in the name list
684 for (i = 0; names[i] != NULL; i++) {
686 ("get_num, compare(%s,%s)\n", names[i], temp))
687 if (!strcasecmp(names[i], temp)) {
694 /* no name list specified, or there is one and our string isn't
695 * in it. either way: if it's all digits, use its magnitude.
696 * otherwise, it's an error.
699 *numptr = atoi(temp);
708 set_element(bits, low, high, number)
709 bitstr_t *bits; /* one bit per flag, default=FALSE */
714 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
716 if (number < low || number > high)
719 bit_set(bits, (number-low));