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_mem
44 static char get_list(bitstr_t *, int, int, char *[], int, FILE *),
45 get_range(bitstr_t *, int, int, char *[], int, FILE *),
46 get_number(int *, int, char *[], int, FILE *);
47 static int set_element(bitstr_t *, int, int, int);
49 static char *ecodes[] =
84 /* return NULL if eof or syntax error occurs;
85 * otherwise return a pointer to a new entry.
88 load_entry(file, error_func, pw, envp)
90 void (*error_func)(char *);
94 /* this function reads one crontab entry -- the next -- from a file.
95 * it skips any leading blank lines, ignores comments, and returns
96 * EOF if for any reason the entry can't be read and parsed.
98 * the entry is also parsed here.
102 * minutes hours doms months dows cmd\n
103 * system crontab (/etc/crontab):
104 * minutes hours doms months dows USERNAME cmd\n
107 ecode_e ecode = e_none;
110 char cmd[MAX_COMMAND];
111 char envstr[MAX_ENVSTR];
114 Debug(DPARS, ("load_entry()...about to eat comments\n"))
122 /* ch is now the first useful character of a useful line.
123 * it may be an @special or it may be the first character
124 * of a list of minutes.
127 e = (entry *) calloc(sizeof(entry), sizeof(char));
130 warn("load_entry: calloc failed");
135 /* all of these should be flagged and load-limited; i.e.,
136 * instead of @hourly meaning "0 * * * *" it should mean
137 * "close to the front of every hour but not 'til the
138 * system load is low". Problems are: how do you know
139 * what "low" means? (save me from /etc/cron.conf!) and:
140 * how to guarantee low variance (how low is low?), which
141 * means how to we run roughly every hour -- seems like
142 * we need to keep a history or let the first hour set
143 * the schedule, which means we aren't load-limited
144 * anymore. too much for my overloaded brain. (vix, jan90)
147 Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
148 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
149 if (!strcmp("reboot", cmd)) {
150 Debug(DPARS, ("load_entry()...reboot shortcut\n"))
151 e->flags |= WHEN_REBOOT;
152 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
153 Debug(DPARS, ("load_entry()...yearly shortcut\n"))
154 bit_set(e->second, 0);
155 bit_set(e->minute, 0);
158 bit_set(e->month, 0);
159 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
160 e->flags |= DOW_STAR;
161 } else if (!strcmp("monthly", cmd)) {
162 Debug(DPARS, ("load_entry()...monthly shortcut\n"))
163 bit_set(e->second, 0);
164 bit_set(e->minute, 0);
167 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
168 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
169 e->flags |= DOW_STAR;
170 } else if (!strcmp("weekly", cmd)) {
171 Debug(DPARS, ("load_entry()...weekly shortcut\n"))
172 bit_set(e->second, 0);
173 bit_set(e->minute, 0);
175 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
176 e->flags |= DOM_STAR;
177 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
179 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
180 Debug(DPARS, ("load_entry()...daily shortcut\n"))
181 bit_set(e->second, 0);
182 bit_set(e->minute, 0);
184 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
185 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
186 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
187 } else if (!strcmp("hourly", cmd)) {
188 Debug(DPARS, ("load_entry()...hourly shortcut\n"))
189 bit_set(e->second, 0);
190 bit_set(e->minute, 0);
191 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
192 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
193 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
194 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
195 } else if (!strcmp("every_minute", cmd)) {
196 Debug(DPARS, ("load_entry()...every_minute shortcut\n"))
197 bit_set(e->second, 0);
198 bit_nset(e->minute, 0, (LAST_MINUTE-FIRST_MINUTE+1));
199 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
200 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
201 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
202 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
203 } else if (!strcmp("every_second", cmd)) {
204 Debug(DPARS, ("load_entry()...every_second shortcut\n"))
206 bit_nset(e->second, 0, (LAST_SECOND-FIRST_SECOND+1));
207 bit_nset(e->minute, 0, (LAST_MINUTE-FIRST_MINUTE+1));
208 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
209 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
210 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
211 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
216 /* Advance past whitespace between shortcut and
219 Skip_Blanks(ch, file);
225 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
226 bit_set(e->second, 0);
228 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
238 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
245 /* DOM (days of month)
249 e->flags |= DOM_STAR;
250 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
260 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
261 MonthNames, ch, file);
267 /* DOW (days of week)
271 e->flags |= DOW_STAR;
272 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
280 /* make sundays equivalent */
281 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
286 /* ch is the first character of a command, or a username */
287 unget_char(ch, file);
290 char *username = cmd; /* temp buffer */
297 Debug(DPARS, ("load_entry()...about to parse username\n"))
298 ch = get_string(username, MAX_COMMAND, file, " \t");
300 Debug(DPARS, ("load_entry()...got %s\n",username))
307 if ((s = strrchr(username, '/')) != NULL) {
309 e->class = strdup(s + 1);
310 if (e->class == NULL)
311 warn("strdup(\"%s\")", s + 1);
313 e->class = strdup(RESOURCE_RC);
314 if (e->class == NULL)
315 warn("strdup(\"%s\")", RESOURCE_RC);
317 if (e->class == NULL) {
321 if ((lc = login_getclass(e->class)) == NULL) {
328 if ((s = strrchr(username, ':')) != NULL) {
330 if ((grp = getgrnam(s + 1)) == NULL) {
336 pw = getpwnam(username);
342 pw->pw_gid = grp->gr_gid;
343 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
345 Debug(DPARS, ("load_entry()...class %s\n",e->class))
349 #ifndef PAM /* PAM takes care of account expiration by itself */
350 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
359 /* copy and fix up environment. some variables are just defaults and
360 * others are overrides.
362 e->envp = env_copy(envp);
363 if (e->envp == NULL) {
368 if (!env_get("SHELL", e->envp)) {
370 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
371 e->envp = env_set(e->envp, envstr);
372 if (e->envp == NULL) {
373 warn("env_set(%s)", envstr);
379 if (!env_get("HOME", e->envp)) {
381 sprintf(envstr, "HOME=%s", pw->pw_dir);
382 e->envp = env_set(e->envp, envstr);
383 if (e->envp == NULL) {
384 warn("env_set(%s)", envstr);
390 if (!env_get("PATH", e->envp)) {
392 sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
393 e->envp = env_set(e->envp, envstr);
394 if (e->envp == NULL) {
395 warn("env_set(%s)", envstr);
402 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
403 e->envp = env_set(e->envp, envstr);
404 if (e->envp == NULL) {
405 warn("env_set(%s)", envstr);
412 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
413 e->envp = env_set(e->envp, envstr);
414 if (e->envp == NULL) {
415 warn("env_set(%s)", envstr);
422 Debug(DPARS, ("load_entry()...about to parse command\n"))
424 /* Everything up to the next \n or EOF is part of the command...
425 * too bad we don't know in advance how long it will be, since we
426 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
427 * XXX - should use realloc().
429 ch = get_string(cmd, MAX_COMMAND, file, "\n");
431 /* a file without a \n before the EOF is rude, so we'll complain...
438 /* got the command in the 'cmd' string; save it in *e.
440 e->cmd = strdup(cmd);
441 if (e->cmd == NULL) {
442 warn("strdup(\"%s\")", cmd);
446 Debug(DPARS, ("load_entry()...returning successfully\n"))
448 /* success, fini, return pointer to the entry we just created...
454 if (ecode != e_none && error_func)
455 (*error_func)(ecodes[(int)ecode]);
456 while (ch != EOF && ch != '\n')
463 get_list(bits, low, high, names, ch, file)
464 bitstr_t *bits; /* one bit per flag, default=FALSE */
465 int low, high; /* bounds, impl. offset for bitstr */
466 char *names[]; /* NULL or *[] of names for these elements */
467 int ch; /* current character being processed */
468 FILE *file; /* file being read */
472 /* we know that we point to a non-blank character here;
473 * must do a Skip_Blanks before we exit, so that the
474 * next call (or the code that picks up the cmd) can
475 * assume the same thing.
478 Debug(DPARS|DEXT, ("get_list()...entered\n"))
480 /* list = range {"," range}
483 /* clear the bit string, since the default is 'off'.
485 bit_nclear(bits, 0, (high-low+1));
487 /* process all ranges
491 ch = get_range(bits, low, high, names, ch, file);
498 /* exiting. skip to some blanks, then skip over the blanks.
500 Skip_Nonblanks(ch, file)
501 Skip_Blanks(ch, file)
503 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
510 get_range(bits, low, high, names, ch, file)
511 bitstr_t *bits; /* one bit per flag, default=FALSE */
512 int low, high; /* bounds, impl. offset for bitstr */
513 char *names[]; /* NULL or names of elements */
514 int ch; /* current character being processed */
515 FILE *file; /* file being read */
517 /* range = number | number "-" number [ "/" number ]
521 auto int num1, num2, num3;
523 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
526 /* '*' means "first-last" but can still be modified by /step
534 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
539 else if (ch != '-') {
540 /* not a range, it's a single number.
542 if (EOF == set_element(bits, low, high, num1))
552 /* get the number following the dash
554 ch = get_number(&num2, low, names, ch, file);
560 /* check for step size
569 /* get the step size -- note: we don't pass the
570 * names here, because the number is not an
571 * element id, it's a step size. 'low' is
572 * sent as a 0 since there is no offset either.
574 ch = get_number(&num3, 0, PPC_NULL, ch, file);
575 if (ch == EOF || num3 == 0)
578 /* no step. default==1.
583 /* range. set all elements from num1 to num2, stepping
584 * by num3. (the step is a downward-compatible extension
585 * proposed conceptually by bob@acornrc, syntactically
586 * designed then implmented by paul vixie).
588 for (i = num1; i <= num2; i += num3)
589 if (EOF == set_element(bits, low, high, i))
597 get_number(numptr, low, names, ch, file)
598 int *numptr; /* where does the result go? */
599 int low; /* offset applied to result if symbolic enum used */
600 char *names[]; /* symbolic names, if any, for enums */
601 int ch; /* current character */
602 FILE *file; /* source */
604 char temp[MAX_TEMPSTR], *pc;
605 int len, i, all_digits;
607 /* collect alphanumerics into our fixed-size temp array
612 while (isalnum(ch)) {
613 if (++len >= MAX_TEMPSTR)
627 /* try to find the name in the name list
630 for (i = 0; names[i] != NULL; i++) {
632 ("get_num, compare(%s,%s)\n", names[i], temp))
633 if (!strcasecmp(names[i], temp)) {
640 /* no name list specified, or there is one and our string isn't
641 * in it. either way: if it's all digits, use its magnitude.
642 * otherwise, it's an error.
645 *numptr = atoi(temp);
654 set_element(bits, low, high, number)
655 bitstr_t *bits; /* one bit per flag, default=FALSE */
660 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
662 if (number < low || number > high)
665 bit_set(bits, (number-low));