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
17 * $FreeBSD: src/usr.sbin/cron/lib/entry.c,v 1.9.2.5 2001/08/18 04:20:31 mikeh Exp $
20 /* vix 26jan87 [RCS'd; rest of log is in RCS file]
21 * vix 01jan87 [added line-level error recovery]
22 * vix 31dec86 [added /step to the from-to range, per bob@acornrc]
23 * vix 30dec86 [written]
30 #include <login_cap.h>
34 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
35 e_cmd, e_timespec, e_username, e_group, e_mem
41 static char get_list(bitstr_t *, int, int, char *[], int, FILE *),
42 get_range(bitstr_t *, int, int, char *[], int, FILE *),
43 get_number(int *, int, char *[], int, FILE *);
44 static int set_element(bitstr_t *, int, int, int);
46 static char *ecodes[] =
80 /* return NULL if eof or syntax error occurs;
81 * otherwise return a pointer to a new entry.
84 load_entry(FILE *file, void (*error_func)(), struct passwd *pw, char **envp)
86 /* this function reads one crontab entry -- the next -- from a file.
87 * it skips any leading blank lines, ignores comments, and returns
88 * EOF if for any reason the entry can't be read and parsed.
90 * the entry is also parsed here.
94 * minutes hours doms months dows cmd\n
95 * system crontab (/etc/crontab):
96 * minutes hours doms months dows USERNAME cmd\n
99 ecode_e ecode = e_none;
102 char cmd[MAX_COMMAND];
103 char envstr[MAX_ENVSTR];
106 Debug(DPARS, ("load_entry()...about to eat comments\n"))
114 /* ch is now the first useful character of a useful line.
115 * it may be an @special or it may be the first character
116 * of a list of minutes.
119 e = (entry *) calloc(sizeof(entry), sizeof(char));
122 warn("load_entry: calloc failed");
127 /* all of these should be flagged and load-limited; i.e.,
128 * instead of @hourly meaning "0 * * * *" it should mean
129 * "close to the front of every hour but not 'til the
130 * system load is low". Problems are: how do you know
131 * what "low" means? (save me from /etc/cron.conf!) and:
132 * how to guarantee low variance (how low is low?), which
133 * means how to we run roughly every hour -- seems like
134 * we need to keep a history or let the first hour set
135 * the schedule, which means we aren't load-limited
136 * anymore. too much for my overloaded brain. (vix, jan90)
139 Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
140 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
141 if (!strcmp("reboot", cmd)) {
142 Debug(DPARS, ("load_entry()...reboot shortcut\n"))
143 e->flags |= WHEN_REBOOT;
144 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
145 Debug(DPARS, ("load_entry()...yearly shortcut\n"))
146 bit_set(e->minute, 0);
149 bit_set(e->month, 0);
150 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
151 e->flags |= DOW_STAR;
152 } else if (!strcmp("monthly", cmd)) {
153 Debug(DPARS, ("load_entry()...monthly shortcut\n"))
154 bit_set(e->minute, 0);
157 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
158 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
159 e->flags |= DOW_STAR;
160 } else if (!strcmp("weekly", cmd)) {
161 Debug(DPARS, ("load_entry()...weekly shortcut\n"))
162 bit_set(e->minute, 0);
164 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
165 e->flags |= DOM_STAR;
166 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
168 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
169 Debug(DPARS, ("load_entry()...daily shortcut\n"))
170 bit_set(e->minute, 0);
172 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
173 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
174 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
175 } else if (!strcmp("hourly", cmd)) {
176 Debug(DPARS, ("load_entry()...hourly shortcut\n"))
177 bit_set(e->minute, 0);
178 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
179 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
180 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
181 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
186 /* Advance past whitespace between shortcut and
189 Skip_Blanks(ch, file);
195 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
197 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
207 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
214 /* DOM (days of month)
218 e->flags |= DOM_STAR;
219 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
229 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
230 MonthNames, ch, file);
236 /* DOW (days of week)
240 e->flags |= DOW_STAR;
241 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
249 /* make sundays equivilent */
250 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
255 /* ch is the first character of a command, or a username */
256 unget_char(ch, file);
259 char *username = cmd; /* temp buffer */
266 Debug(DPARS, ("load_entry()...about to parse username\n"))
267 ch = get_string(username, MAX_COMMAND, file, " \t");
269 Debug(DPARS, ("load_entry()...got %s\n",username))
276 if ((s = strrchr(username, '/')) != NULL) {
278 e->class = strdup(s + 1);
279 if (e->class == NULL)
280 warn("strdup(\"%s\")", s + 1);
282 e->class = strdup(RESOURCE_RC);
283 if (e->class == NULL)
284 warn("strdup(\"%s\")", RESOURCE_RC);
286 if (e->class == NULL) {
290 if ((lc = login_getclass(e->class)) == NULL) {
297 if ((s = strrchr(username, ':')) != NULL) {
299 if ((grp = getgrnam(s + 1)) == NULL) {
305 pw = getpwnam(username);
311 pw->pw_gid = grp->gr_gid;
312 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
314 Debug(DPARS, ("load_entry()...class %s\n",e->class))
318 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
326 /* copy and fix up environment. some variables are just defaults and
327 * others are overrides.
329 e->envp = env_copy(envp);
330 if (e->envp == NULL) {
335 if (!env_get("SHELL", e->envp)) {
337 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
338 e->envp = env_set(e->envp, envstr);
339 if (e->envp == NULL) {
340 warn("env_set(%s)", envstr);
347 sprintf(envstr, "HOME=%s", pw->pw_dir);
348 e->envp = env_set(e->envp, envstr);
349 if (e->envp == NULL) {
350 warn("env_set(%s)", envstr);
355 if (!env_get("PATH", e->envp)) {
357 sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
358 e->envp = env_set(e->envp, envstr);
359 if (e->envp == NULL) {
360 warn("env_set(%s)", envstr);
367 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
368 e->envp = env_set(e->envp, envstr);
369 if (e->envp == NULL) {
370 warn("env_set(%s)", envstr);
377 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
378 e->envp = env_set(e->envp, envstr);
379 if (e->envp == NULL) {
380 warn("env_set(%s)", envstr);
387 Debug(DPARS, ("load_entry()...about to parse command\n"))
389 /* Everything up to the next \n or EOF is part of the command...
390 * too bad we don't know in advance how long it will be, since we
391 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
392 * XXX - should use realloc().
394 ch = get_string(cmd, MAX_COMMAND, file, "\n");
396 /* a file without a \n before the EOF is rude, so we'll complain...
403 /* got the command in the 'cmd' string; save it in *e.
405 e->cmd = strdup(cmd);
406 if (e->cmd == NULL) {
407 warn("strdup(\"%s\")", cmd);
411 Debug(DPARS, ("load_entry()...returning successfully\n"))
413 /* success, fini, return pointer to the entry we just created...
419 if (ecode != e_none && error_func)
420 (*error_func)(ecodes[(int)ecode]);
421 while (ch != EOF && ch != '\n')
427 * bits; one bit per flag, default=FALSE
428 * low, high bounds, impl. offset for bitstr
429 * names NULL or *[] of names for these elements
430 * ch current character being processed
431 * file file being read
434 get_list(bitstr_t *bits, int low, int high, char **names, int ch, FILE *file)
438 /* we know that we point to a non-blank character here;
439 * must do a Skip_Blanks before we exit, so that the
440 * next call (or the code that picks up the cmd) can
441 * assume the same thing.
444 Debug(DPARS|DEXT, ("get_list()...entered\n"))
446 /* list = range {"," range}
449 /* clear the bit string, since the default is 'off'.
451 bit_nclear(bits, 0, (high-low+1));
453 /* process all ranges
457 ch = get_range(bits, low, high, names, ch, file);
464 /* exiting. skip to some blanks, then skip over the blanks.
466 Skip_Nonblanks(ch, file)
467 Skip_Blanks(ch, file)
469 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
476 get_range(bitstr_t *bits, int low, int high, char **names, int ch, FILE *file)
478 /* range = number | number "-" number [ "/" number ]
482 int num1, num2, num3;
484 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
487 /* '*' means "first-last" but can still be modified by /step
495 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
499 /* not a range, it's a single number.
501 if (EOF == set_element(bits, low, high, num1))
511 /* get the number following the dash
513 ch = get_number(&num2, low, names, ch, file);
519 /* check for step size
528 /* get the step size -- note: we don't pass the
529 * names here, because the number is not an
530 * element id, it's a step size. 'low' is
531 * sent as a 0 since there is no offset either.
533 ch = get_number(&num3, 0, NULL, ch, file);
534 if (ch == EOF || num3 == 0)
537 /* no step. default==1.
542 /* range. set all elements from num1 to num2, stepping
543 * by num3. (the step is a downward-compatible extension
544 * proposed conceptually by bob@acornrc, syntactically
545 * designed then implmented by paul vixie).
547 for (i = num1; i <= num2; i += num3)
548 if (EOF == set_element(bits, low, high, i))
555 * numptr where does the result go?
556 * low offset applied to result if symbolic enum used
557 * names symbolic names, if any, for enums
558 * ch current character
562 get_number(int *numptr, int low, char **names, int ch, FILE *file)
564 char temp[MAX_TEMPSTR], *pc;
565 int len, i, all_digits;
567 /* collect alphanumerics into our fixed-size temp array
572 while (isalnum(ch)) {
573 if (++len >= MAX_TEMPSTR)
587 /* try to find the name in the name list
590 for (i = 0; names[i] != NULL; i++) {
592 ("get_num, compare(%s,%s)\n", names[i], temp))
593 if (!strcasecmp(names[i], temp)) {
600 /* no name list specified, or there is one and our string isn't
601 * in it. either way: if it's all digits, use its magnitude.
602 * otherwise, it's an error.
605 *numptr = atoi(temp);
614 set_element(bitstr_t *bits, int low, int high, int number)
616 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
618 if (number < low || number > high)
621 bit_set(bits, (number-low));