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 $
18 * $DragonFly: src/usr.sbin/cron/lib/entry.c,v 1.6 2004/07/05 15:29:49 dillon Exp $
21 /* vix 26jan87 [RCS'd; rest of log is in RCS file]
22 * vix 01jan87 [added line-level error recovery]
23 * vix 31dec86 [added /step to the from-to range, per bob@acornrc]
24 * vix 30dec86 [written]
31 #include <login_cap.h>
35 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
36 e_cmd, e_timespec, e_username, e_group, e_mem
42 static char get_list(bitstr_t *, int, int, char *[], int, FILE *),
43 get_range(bitstr_t *, int, int, char *[], int, FILE *),
44 get_number(int *, int, char *[], int, FILE *);
45 static int set_element(bitstr_t *, int, int, int);
47 static char *ecodes[] =
81 /* return NULL if eof or syntax error occurs;
82 * otherwise return a pointer to a new entry.
85 load_entry(FILE *file, void (*error_func)(), struct passwd *pw, char **envp)
87 /* this function reads one crontab entry -- the next -- from a file.
88 * it skips any leading blank lines, ignores comments, and returns
89 * EOF if for any reason the entry can't be read and parsed.
91 * the entry is also parsed here.
95 * minutes hours doms months dows cmd\n
96 * system crontab (/etc/crontab):
97 * minutes hours doms months dows USERNAME cmd\n
100 ecode_e ecode = e_none;
103 char cmd[MAX_COMMAND];
104 char envstr[MAX_ENVSTR];
107 Debug(DPARS, ("load_entry()...about to eat comments\n"))
115 /* ch is now the first useful character of a useful line.
116 * it may be an @special or it may be the first character
117 * of a list of minutes.
120 e = (entry *) calloc(sizeof(entry), sizeof(char));
123 warn("load_entry: calloc failed");
128 /* all of these should be flagged and load-limited; i.e.,
129 * instead of @hourly meaning "0 * * * *" it should mean
130 * "close to the front of every hour but not 'til the
131 * system load is low". Problems are: how do you know
132 * what "low" means? (save me from /etc/cron.conf!) and:
133 * how to guarantee low variance (how low is low?), which
134 * means how to we run roughly every hour -- seems like
135 * we need to keep a history or let the first hour set
136 * the schedule, which means we aren't load-limited
137 * anymore. too much for my overloaded brain. (vix, jan90)
140 Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
141 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
142 if (!strcmp("reboot", cmd)) {
143 Debug(DPARS, ("load_entry()...reboot shortcut\n"))
144 e->flags |= WHEN_REBOOT;
145 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
146 Debug(DPARS, ("load_entry()...yearly shortcut\n"))
147 bit_set(e->minute, 0);
150 bit_set(e->month, 0);
151 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
152 e->flags |= DOW_STAR;
153 } else if (!strcmp("monthly", cmd)) {
154 Debug(DPARS, ("load_entry()...monthly shortcut\n"))
155 bit_set(e->minute, 0);
158 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
159 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
160 e->flags |= DOW_STAR;
161 } else if (!strcmp("weekly", cmd)) {
162 Debug(DPARS, ("load_entry()...weekly shortcut\n"))
163 bit_set(e->minute, 0);
165 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
166 e->flags |= DOM_STAR;
167 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
169 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
170 Debug(DPARS, ("load_entry()...daily shortcut\n"))
171 bit_set(e->minute, 0);
173 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
174 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
175 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
176 } else if (!strcmp("hourly", cmd)) {
177 Debug(DPARS, ("load_entry()...hourly shortcut\n"))
178 bit_set(e->minute, 0);
179 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
180 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
181 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
182 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
187 /* Advance past whitespace between shortcut and
190 Skip_Blanks(ch, file);
196 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
198 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
208 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
215 /* DOM (days of month)
219 e->flags |= DOM_STAR;
220 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
230 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
231 MonthNames, ch, file);
237 /* DOW (days of week)
241 e->flags |= DOW_STAR;
242 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
250 /* make sundays equivilent */
251 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
256 /* ch is the first character of a command, or a username */
257 unget_char(ch, file);
260 char *username = cmd; /* temp buffer */
267 Debug(DPARS, ("load_entry()...about to parse username\n"))
268 ch = get_string(username, MAX_COMMAND, file, " \t");
270 Debug(DPARS, ("load_entry()...got %s\n",username))
277 if ((s = strrchr(username, '/')) != NULL) {
279 e->class = strdup(s + 1);
280 if (e->class == NULL)
281 warn("strdup(\"%s\")", s + 1);
283 e->class = strdup(RESOURCE_RC);
284 if (e->class == NULL)
285 warn("strdup(\"%s\")", RESOURCE_RC);
287 if (e->class == NULL) {
291 if ((lc = login_getclass(e->class)) == NULL) {
298 if ((s = strrchr(username, ':')) != NULL) {
300 if ((grp = getgrnam(s + 1)) == NULL) {
306 pw = getpwnam(username);
312 pw->pw_gid = grp->gr_gid;
313 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
315 Debug(DPARS, ("load_entry()...class %s\n",e->class))
319 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
327 /* copy and fix up environment. some variables are just defaults and
328 * others are overrides.
330 e->envp = env_copy(envp);
331 if (e->envp == NULL) {
336 if (!env_get("SHELL", e->envp)) {
338 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
339 e->envp = env_set(e->envp, envstr);
340 if (e->envp == NULL) {
341 warn("env_set(%s)", envstr);
348 sprintf(envstr, "HOME=%s", pw->pw_dir);
349 e->envp = env_set(e->envp, envstr);
350 if (e->envp == NULL) {
351 warn("env_set(%s)", envstr);
356 if (!env_get("PATH", e->envp)) {
358 sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
359 e->envp = env_set(e->envp, envstr);
360 if (e->envp == NULL) {
361 warn("env_set(%s)", envstr);
368 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
369 e->envp = env_set(e->envp, envstr);
370 if (e->envp == NULL) {
371 warn("env_set(%s)", envstr);
378 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
379 e->envp = env_set(e->envp, envstr);
380 if (e->envp == NULL) {
381 warn("env_set(%s)", envstr);
388 Debug(DPARS, ("load_entry()...about to parse command\n"))
390 /* Everything up to the next \n or EOF is part of the command...
391 * too bad we don't know in advance how long it will be, since we
392 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
393 * XXX - should use realloc().
395 ch = get_string(cmd, MAX_COMMAND, file, "\n");
397 /* a file without a \n before the EOF is rude, so we'll complain...
404 /* got the command in the 'cmd' string; save it in *e.
406 e->cmd = strdup(cmd);
407 if (e->cmd == NULL) {
408 warn("strdup(\"%d\")", cmd);
412 Debug(DPARS, ("load_entry()...returning successfully\n"))
414 /* success, fini, return pointer to the entry we just created...
420 if (ecode != e_none && error_func)
421 (*error_func)(ecodes[(int)ecode]);
422 while (ch != EOF && ch != '\n')
428 * bits; one bit per flag, default=FALSE
429 * low, high bounds, impl. offset for bitstr
430 * names NULL or *[] of names for these elements
431 * ch current character being processed
432 * file file being read
435 get_list(bitstr_t *bits, int low, int high, char **names, int ch, FILE *file)
439 /* we know that we point to a non-blank character here;
440 * must do a Skip_Blanks before we exit, so that the
441 * next call (or the code that picks up the cmd) can
442 * assume the same thing.
445 Debug(DPARS|DEXT, ("get_list()...entered\n"))
447 /* list = range {"," range}
450 /* clear the bit string, since the default is 'off'.
452 bit_nclear(bits, 0, (high-low+1));
454 /* process all ranges
458 ch = get_range(bits, low, high, names, ch, file);
465 /* exiting. skip to some blanks, then skip over the blanks.
467 Skip_Nonblanks(ch, file)
468 Skip_Blanks(ch, file)
470 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
477 get_range(bitstr_t *bits, int low, int high, char **names, int ch, FILE *file)
479 /* range = number | number "-" number [ "/" number ]
483 int num1, num2, num3;
485 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
488 /* '*' means "first-last" but can still be modified by /step
496 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
500 /* not a range, it's a single number.
502 if (EOF == set_element(bits, low, high, num1))
512 /* get the number following the dash
514 ch = get_number(&num2, low, names, ch, file);
520 /* check for step size
529 /* get the step size -- note: we don't pass the
530 * names here, because the number is not an
531 * element id, it's a step size. 'low' is
532 * sent as a 0 since there is no offset either.
534 ch = get_number(&num3, 0, NULL, ch, file);
535 if (ch == EOF || num3 == 0)
538 /* no step. default==1.
543 /* range. set all elements from num1 to num2, stepping
544 * by num3. (the step is a downward-compatible extension
545 * proposed conceptually by bob@acornrc, syntactically
546 * designed then implmented by paul vixie).
548 for (i = num1; i <= num2; i += num3)
549 if (EOF == set_element(bits, low, high, i))
556 * numptr where does the result go?
557 * low offset applied to result if symbolic enum used
558 * names symbolic names, if any, for enums
559 * ch current character
563 get_number(int *numptr, int low, char **names, int ch, FILE *file)
565 char temp[MAX_TEMPSTR], *pc;
566 int len, i, all_digits;
568 /* collect alphanumerics into our fixed-size temp array
573 while (isalnum(ch)) {
574 if (++len >= MAX_TEMPSTR)
588 /* try to find the name in the name list
591 for (i = 0; names[i] != NULL; i++) {
593 ("get_num, compare(%s,%s)\n", names[i], temp))
594 if (!strcasecmp(names[i], temp)) {
601 /* no name list specified, or there is one and our string isn't
602 * in it. either way: if it's all digits, use its magnitude.
603 * otherwise, it's an error.
606 *numptr = atoi(temp);
615 set_element(bitstr_t *bits, int low, int high, int number)
617 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
619 if (number < low || number > high)
622 bit_set(bits, (number-low));