2 * Copyright 1988,1990,1993,1994 by Paul Vixie
7 * Copyright (c) 1997 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 #if !defined(lint) && !defined(LINT)
24 static const char rcsid[] =
25 "$Id: entry.c,v 1.3 1998/08/14 00:32:39 vixie Exp $";
28 /* vix 26jan87 [RCS'd; rest of log is in RCS file]
29 * vix 01jan87 [added line-level error recovery]
30 * vix 31dec86 [added /step to the from-to range, per bob@acornrc]
31 * vix 30dec86 [written]
38 #include <login_cap.h>
42 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
43 e_cmd, e_timespec, e_username, e_group, e_option,
50 static const char *ecodes[] =
69 static char get_list(bitstr_t *, int, int, const char *[], int, FILE *),
70 get_range(bitstr_t *, int, int, const char *[], int, FILE *),
71 get_number(int *, int, const char *[], int, FILE *);
72 static int set_element(bitstr_t *, int, int, int);
89 /* return NULL if eof or syntax error occurs;
90 * otherwise return a pointer to a new entry.
93 load_entry(FILE *file, void (*error_func)(const char *), struct passwd *pw,
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;
113 char cmd[MAX_COMMAND];
114 char envstr[MAX_ENVSTR];
117 Debug(DPARS, ("load_entry()...about to eat comments\n"))
125 /* ch is now the first useful character of a useful line.
126 * it may be an @special or it may be the first character
127 * of a list of minutes.
130 e = (entry *) calloc(sizeof(entry), sizeof(char));
133 warn("load_entry: calloc failed");
141 /* all of these should be flagged and load-limited; i.e.,
142 * instead of @hourly meaning "0 * * * *" it should mean
143 * "close to the front of every hour but not 'til the
144 * system load is low". Problems are: how do you know
145 * what "low" means? (save me from /etc/cron.conf!) and:
146 * how to guarantee low variance (how low is low?), which
147 * means how to we run roughly every hour -- seems like
148 * we need to keep a history or let the first hour set
149 * the schedule, which means we aren't load-limited
150 * anymore. too much for my overloaded brain. (vix, jan90)
153 Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
154 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
155 if (!strcmp("reboot", cmd)) {
156 Debug(DPARS, ("load_entry()...reboot shortcut\n"))
157 e->flags |= WHEN_REBOOT;
158 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
159 Debug(DPARS, ("load_entry()...yearly shortcut\n"))
160 bit_set(e->second, 0);
161 bit_set(e->minute, 0);
164 bit_set(e->month, 0);
165 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
166 e->flags |= DOW_STAR;
167 } else if (!strcmp("monthly", cmd)) {
168 Debug(DPARS, ("load_entry()...monthly shortcut\n"))
169 bit_set(e->second, 0);
170 bit_set(e->minute, 0);
173 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
174 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
175 e->flags |= DOW_STAR;
176 } else if (!strcmp("weekly", cmd)) {
177 Debug(DPARS, ("load_entry()...weekly shortcut\n"))
178 bit_set(e->second, 0);
179 bit_set(e->minute, 0);
181 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
182 e->flags |= DOM_STAR;
183 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
185 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
186 Debug(DPARS, ("load_entry()...daily shortcut\n"))
187 bit_set(e->second, 0);
188 bit_set(e->minute, 0);
190 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
191 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
192 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
193 } else if (!strcmp("hourly", cmd)) {
194 Debug(DPARS, ("load_entry()...hourly shortcut\n"))
195 bit_set(e->second, 0);
196 bit_set(e->minute, 0);
197 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
198 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
199 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
200 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
201 } else if (!strcmp("every_minute", cmd)) {
202 Debug(DPARS, ("load_entry()...every_minute shortcut\n"))
203 bit_set(e->second, 0);
204 bit_nset(e->minute, 0, (LAST_MINUTE-FIRST_MINUTE+1));
205 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
206 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
207 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
208 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
209 } else if (!strcmp("every_second", cmd)) {
210 Debug(DPARS, ("load_entry()...every_second shortcut\n"))
212 bit_nset(e->second, 0, (LAST_SECOND-FIRST_SECOND+1));
213 bit_nset(e->minute, 0, (LAST_MINUTE-FIRST_MINUTE+1));
214 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
215 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
216 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
217 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
218 } else if (*cmd != '\0' &&
219 (interval = strtol(cmd, &endptr, 10)) > 0 &&
221 Debug(DPARS, ("load_entry()... %ld seconds "
222 "since last run\n", interval))
223 e->interval = interval;
229 /* Advance past whitespace between shortcut and
232 Skip_Blanks(ch, file);
238 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
239 bit_set(e->second, 0);
241 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
251 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
258 /* DOM (days of month)
262 e->flags |= DOM_STAR;
263 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
273 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
274 MonthNames, ch, file);
280 /* DOW (days of week)
284 e->flags |= DOW_STAR;
285 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
293 /* make sundays equivalent */
294 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
299 /* ch is the first character of a command, or a username */
300 unget_char(ch, file);
303 char *username = cmd; /* temp buffer */
310 Debug(DPARS, ("load_entry()...about to parse username\n"))
311 ch = get_string(username, MAX_COMMAND, file, " \t");
313 Debug(DPARS, ("load_entry()...got %s\n",username))
319 /* need to have consumed blanks when checking options below */
320 Skip_Blanks(ch, file)
321 unget_char(ch, file);
323 if ((s = strrchr(username, '/')) != NULL) {
325 e->class = strdup(s + 1);
326 if (e->class == NULL)
327 warn("strdup(\"%s\")", s + 1);
329 e->class = strdup(RESOURCE_RC);
330 if (e->class == NULL)
331 warn("strdup(\"%s\")", RESOURCE_RC);
333 if (e->class == NULL) {
337 if ((lc = login_getclass(e->class)) == NULL) {
344 if ((s = strrchr(username, ':')) != NULL) {
346 if ((grp = getgrnam(s + 1)) == NULL) {
352 pw = getpwnam(username);
358 pw->pw_gid = grp->gr_gid;
359 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
361 Debug(DPARS, ("load_entry()...class %s\n",e->class))
365 #ifndef PAM /* PAM takes care of account expiration by itself */
366 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
375 /* copy and fix up environment. some variables are just defaults and
376 * others are overrides; we process only the overrides here, defaults
377 * are handled in do_command after login.conf is processed.
379 e->envp = env_copy(envp);
380 if (e->envp == NULL) {
385 if (!env_get("SHELL", e->envp)) {
387 e->envp = env_set(e->envp, "SHELL=" _PATH_BSHELL);
388 if (e->envp == NULL) {
389 warn("env_set(%s)", "SHELL=" _PATH_BSHELL);
395 /* If LOGIN_CAP, this is deferred to do_command where the login class
396 * is processed. If !LOGIN_CAP, do it here.
399 if (!env_get("HOME", e->envp)) {
401 len = snprintf(envstr, sizeof(envstr), "HOME=%s", pw->pw_dir);
402 if (len < (int)sizeof(envstr))
403 e->envp = env_set(e->envp, envstr);
404 if (len >= (int)sizeof(envstr) || e->envp == NULL) {
405 warn("env_set(%s)", envstr);
413 len = snprintf(envstr, sizeof(envstr), "LOGNAME=%s", pw->pw_name);
414 if (len < (int)sizeof(envstr))
415 e->envp = env_set(e->envp, envstr);
416 if (len >= (int)sizeof(envstr) || e->envp == NULL) {
417 warn("env_set(%s)", envstr);
424 len = snprintf(envstr, sizeof(envstr), "USER=%s", pw->pw_name);
425 if (len < (int)sizeof(envstr))
426 e->envp = env_set(e->envp, envstr);
427 if (len >= (int)sizeof(envstr) || e->envp == NULL) {
428 warn("env_set(%s)", envstr);
435 Debug(DPARS, ("load_entry()...checking for command options\n"))
440 Debug(DPARS|DEXT, ("load_entry()...expecting option\n"))
441 switch (ch = get_char(file)) {
443 Debug(DPARS|DEXT, ("load_entry()...got MAIL_WHEN_ERR ('n') option\n"))
444 /* only allow the user to set the option once */
445 if ((e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR) {
446 Debug(DPARS|DEXT, ("load_entry()...duplicate MAIL_WHEN_ERR ('n') option\n"))
450 e->flags |= MAIL_WHEN_ERR;
453 Debug(DPARS|DEXT, ("load_entry()...got DONT_LOG ('q') option\n"))
454 /* only allow the user to set the option once */
455 if ((e->flags & DONT_LOG) == DONT_LOG) {
456 Debug(DPARS|DEXT, ("load_entry()...duplicate DONT_LOG ('q') option\n"))
460 e->flags |= DONT_LOG;
463 Debug(DPARS|DEXT, ("load_entry()...invalid option '%c'\n", ch))
468 if (ch!='\t' && ch!=' ') {
473 Skip_Blanks(ch, file)
474 if (ch == EOF || ch == '\n') {
480 unget_char(ch, file);
482 Debug(DPARS, ("load_entry()...about to parse command\n"))
484 /* Everything up to the next \n or EOF is part of the command...
485 * too bad we don't know in advance how long it will be, since we
486 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
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 * bits one bit per flag, default=FALSE
523 * low, high bounds, impl. offset for bitstr
524 * names NULL or names for these elements
525 * ch current character being processed
526 * file file being read
529 get_list(bitstr_t *bits, int low, int high, const char *names[], int ch,
534 /* we know that we point to a non-blank character here;
535 * must do a Skip_Blanks before we exit, so that the
536 * next call (or the code that picks up the cmd) can
537 * assume the same thing.
540 Debug(DPARS|DEXT, ("get_list()...entered\n"))
542 /* list = range {"," range}
545 /* clear the bit string, since the default is 'off'.
547 bit_nclear(bits, 0, (high-low+1));
549 /* process all ranges
553 ch = get_range(bits, low, high, names, ch, file);
560 /* exiting. skip to some blanks, then skip over the blanks.
562 Skip_Nonblanks(ch, file)
563 Skip_Blanks(ch, file)
565 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
572 * bits one bit per flag, default=FALSE
573 * low, high bounds, impl. offset for bitstr
574 * names NULL or names for these elements
575 * ch current character being processed
576 * file file being read
579 get_range(bitstr_t *bits, int low, int high, const char *names[], int ch,
582 /* range = number | number "-" number [ "/" number ]
585 int i, num1, num2, num3;
587 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
590 /* '*' means "first-last" but can still be modified by /step
598 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
603 else if (ch != '-') {
604 /* not a range, it's a single number.
606 if (EOF == set_element(bits, low, high, num1))
616 /* get the number following the dash
618 ch = get_number(&num2, low, names, ch, file);
624 /* check for step size
633 /* get the step size -- note: we don't pass the
634 * names here, because the number is not an
635 * element id, it's a step size. 'low' is
636 * sent as a 0 since there is no offset either.
638 ch = get_number(&num3, 0, PPC_NULL, ch, file);
639 if (ch == EOF || num3 == 0)
642 /* no step. default==1.
647 /* range. set all elements from num1 to num2, stepping
648 * by num3. (the step is a downward-compatible extension
649 * proposed conceptually by bob@acornrc, syntactically
650 * designed then implemented by paul vixie).
652 for (i = num1; i <= num2; i += num3)
653 if (EOF == set_element(bits, low, high, i))
661 * numptr where does the result go?
662 * low offset applied to enum result
663 * names symbolic names, if any, for enums
664 * ch current character
668 get_number(int *numptr, int low, const char *names[], int ch, FILE *file)
670 char temp[MAX_TEMPSTR], *pc;
671 int len, i, all_digits;
673 /* collect alphanumerics into our fixed-size temp array
678 while (isalnum(ch)) {
679 if (++len >= MAX_TEMPSTR)
693 /* try to find the name in the name list
696 for (i = 0; names[i] != NULL; i++) {
698 ("get_num, compare(%s,%s)\n", names[i], temp))
699 if (!strcasecmp(names[i], temp)) {
706 /* no name list specified, or there is one and our string isn't
707 * in it. either way: if it's all digits, use its magnitude.
708 * otherwise, it's an error.
711 *numptr = atoi(temp);
720 set_element(bitstr_t *bits, int low, int high, int number)
722 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
724 if (number < low || number > high)
727 bit_set(bits, (number-low));