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.3 2003/11/03 19:31:36 eirikn 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[] =
82 /* return NULL if eof or syntax error occurs;
83 * otherwise return a pointer to a new entry.
86 load_entry(file, error_func, pw, envp)
92 /* this function reads one crontab entry -- the next -- from a file.
93 * it skips any leading blank lines, ignores comments, and returns
94 * EOF if for any reason the entry can't be read and parsed.
96 * the entry is also parsed here.
100 * minutes hours doms months dows cmd\n
101 * system crontab (/etc/crontab):
102 * minutes hours doms months dows USERNAME cmd\n
105 ecode_e ecode = e_none;
108 char cmd[MAX_COMMAND];
109 char envstr[MAX_ENVSTR];
112 Debug(DPARS, ("load_entry()...about to eat comments\n"))
120 /* ch is now the first useful character of a useful line.
121 * it may be an @special or it may be the first character
122 * of a list of minutes.
125 e = (entry *) calloc(sizeof(entry), sizeof(char));
128 warn("load_entry: calloc failed");
133 /* all of these should be flagged and load-limited; i.e.,
134 * instead of @hourly meaning "0 * * * *" it should mean
135 * "close to the front of every hour but not 'til the
136 * system load is low". Problems are: how do you know
137 * what "low" means? (save me from /etc/cron.conf!) and:
138 * how to guarantee low variance (how low is low?), which
139 * means how to we run roughly every hour -- seems like
140 * we need to keep a history or let the first hour set
141 * the schedule, which means we aren't load-limited
142 * anymore. too much for my overloaded brain. (vix, jan90)
145 Debug(DPARS, ("load_entry()...about to test shortcuts\n"))
146 ch = get_string(cmd, MAX_COMMAND, file, " \t\n");
147 if (!strcmp("reboot", cmd)) {
148 Debug(DPARS, ("load_entry()...reboot shortcut\n"))
149 e->flags |= WHEN_REBOOT;
150 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){
151 Debug(DPARS, ("load_entry()...yearly shortcut\n"))
152 bit_set(e->minute, 0);
155 bit_set(e->month, 0);
156 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
157 e->flags |= DOW_STAR;
158 } else if (!strcmp("monthly", cmd)) {
159 Debug(DPARS, ("load_entry()...monthly shortcut\n"))
160 bit_set(e->minute, 0);
163 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
164 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
165 e->flags |= DOW_STAR;
166 } else if (!strcmp("weekly", cmd)) {
167 Debug(DPARS, ("load_entry()...weekly shortcut\n"))
168 bit_set(e->minute, 0);
170 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
171 e->flags |= DOM_STAR;
172 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
174 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
175 Debug(DPARS, ("load_entry()...daily shortcut\n"))
176 bit_set(e->minute, 0);
178 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
179 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
180 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
181 } else if (!strcmp("hourly", cmd)) {
182 Debug(DPARS, ("load_entry()...hourly shortcut\n"))
183 bit_set(e->minute, 0);
184 bit_nset(e->hour, 0, (LAST_HOUR-FIRST_HOUR+1));
185 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1));
186 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1));
187 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1));
192 /* Advance past whitespace between shortcut and
195 Skip_Blanks(ch, file);
201 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
203 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
213 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
220 /* DOM (days of month)
224 e->flags |= DOM_STAR;
225 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
235 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
236 MonthNames, ch, file);
242 /* DOW (days of week)
246 e->flags |= DOW_STAR;
247 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
255 /* make sundays equivilent */
256 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
261 /* ch is the first character of a command, or a username */
262 unget_char(ch, file);
265 char *username = cmd; /* temp buffer */
272 Debug(DPARS, ("load_entry()...about to parse username\n"))
273 ch = get_string(username, MAX_COMMAND, file, " \t");
275 Debug(DPARS, ("load_entry()...got %s\n",username))
282 if ((s = strrchr(username, '/')) != NULL) {
284 e->class = strdup(s + 1);
285 if (e->class == NULL)
286 warn("strdup(\"%s\")", s + 1);
288 e->class = strdup(RESOURCE_RC);
289 if (e->class == NULL)
290 warn("strdup(\"%s\")", RESOURCE_RC);
292 if (e->class == NULL) {
296 if ((lc = login_getclass(e->class)) == NULL) {
303 if ((s = strrchr(username, ':')) != NULL) {
305 if ((grp = getgrnam(s + 1)) == NULL) {
311 pw = getpwnam(username);
317 pw->pw_gid = grp->gr_gid;
318 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
320 Debug(DPARS, ("load_entry()...class %s\n",e->class))
324 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
332 /* copy and fix up environment. some variables are just defaults and
333 * others are overrides.
335 e->envp = env_copy(envp);
336 if (e->envp == NULL) {
341 if (!env_get("SHELL", e->envp)) {
343 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
344 e->envp = env_set(e->envp, envstr);
345 if (e->envp == NULL) {
346 warn("env_set(%s)", envstr);
353 sprintf(envstr, "HOME=%s", pw->pw_dir);
354 e->envp = env_set(e->envp, envstr);
355 if (e->envp == NULL) {
356 warn("env_set(%s)", envstr);
361 if (!env_get("PATH", e->envp)) {
363 sprintf(envstr, "PATH=%s", _PATH_DEFPATH);
364 e->envp = env_set(e->envp, envstr);
365 if (e->envp == NULL) {
366 warn("env_set(%s)", envstr);
373 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name);
374 e->envp = env_set(e->envp, envstr);
375 if (e->envp == NULL) {
376 warn("env_set(%s)", envstr);
383 sprintf(envstr, "%s=%s", "USER", pw->pw_name);
384 e->envp = env_set(e->envp, envstr);
385 if (e->envp == NULL) {
386 warn("env_set(%s)", envstr);
393 Debug(DPARS, ("load_entry()...about to parse command\n"))
395 /* Everything up to the next \n or EOF is part of the command...
396 * too bad we don't know in advance how long it will be, since we
397 * need to malloc a string for it... so, we limit it to MAX_COMMAND.
398 * XXX - should use realloc().
400 ch = get_string(cmd, MAX_COMMAND, file, "\n");
402 /* a file without a \n before the EOF is rude, so we'll complain...
409 /* got the command in the 'cmd' string; save it in *e.
411 e->cmd = strdup(cmd);
412 if (e->cmd == NULL) {
413 warn("strdup(\"%d\")", cmd);
417 Debug(DPARS, ("load_entry()...returning successfully\n"))
419 /* success, fini, return pointer to the entry we just created...
425 if (ecode != e_none && error_func)
426 (*error_func)(ecodes[(int)ecode]);
427 while (ch != EOF && ch != '\n')
434 get_list(bits, low, high, names, ch, file)
435 bitstr_t *bits; /* one bit per flag, default=FALSE */
436 int low, high; /* bounds, impl. offset for bitstr */
437 char *names[]; /* NULL or *[] of names for these elements */
438 int ch; /* current character being processed */
439 FILE *file; /* file being read */
443 /* we know that we point to a non-blank character here;
444 * must do a Skip_Blanks before we exit, so that the
445 * next call (or the code that picks up the cmd) can
446 * assume the same thing.
449 Debug(DPARS|DEXT, ("get_list()...entered\n"))
451 /* list = range {"," range}
454 /* clear the bit string, since the default is 'off'.
456 bit_nclear(bits, 0, (high-low+1));
458 /* process all ranges
462 ch = get_range(bits, low, high, names, ch, file);
469 /* exiting. skip to some blanks, then skip over the blanks.
471 Skip_Nonblanks(ch, file)
472 Skip_Blanks(ch, file)
474 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
481 get_range(bits, low, high, names, ch, file)
482 bitstr_t *bits; /* one bit per flag, default=FALSE */
483 int low, high; /* bounds, impl. offset for bitstr */
484 char *names[]; /* NULL or names of elements */
485 int ch; /* current character being processed */
486 FILE *file; /* file being read */
488 /* range = number | number "-" number [ "/" number ]
492 auto int num1, num2, num3;
494 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
497 /* '*' means "first-last" but can still be modified by /step
505 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
509 /* not a range, it's a single number.
511 if (EOF == set_element(bits, low, high, num1))
521 /* get the number following the dash
523 ch = get_number(&num2, low, names, ch, file);
529 /* check for step size
538 /* get the step size -- note: we don't pass the
539 * names here, because the number is not an
540 * element id, it's a step size. 'low' is
541 * sent as a 0 since there is no offset either.
543 ch = get_number(&num3, 0, PPC_NULL, ch, file);
547 /* no step. default==1.
552 /* range. set all elements from num1 to num2, stepping
553 * by num3. (the step is a downward-compatible extension
554 * proposed conceptually by bob@acornrc, syntactically
555 * designed then implmented by paul vixie).
557 for (i = num1; i <= num2; i += num3)
558 if (EOF == set_element(bits, low, high, i))
566 get_number(numptr, low, names, ch, file)
567 int *numptr; /* where does the result go? */
568 int low; /* offset applied to result if symbolic enum used */
569 char *names[]; /* symbolic names, if any, for enums */
570 int ch; /* current character */
571 FILE *file; /* source */
573 char temp[MAX_TEMPSTR], *pc;
574 int len, i, all_digits;
576 /* collect alphanumerics into our fixed-size temp array
581 while (isalnum(ch)) {
582 if (++len >= MAX_TEMPSTR)
594 /* try to find the name in the name list
597 for (i = 0; names[i] != NULL; i++) {
599 ("get_num, compare(%s,%s)\n", names[i], temp))
600 if (!strcasecmp(names[i], temp)) {
607 /* no name list specified, or there is one and our string isn't
608 * in it. either way: if it's all digits, use its magnitude.
609 * otherwise, it's an error.
612 *numptr = atoi(temp);
621 set_element(bits, low, high, number)
622 bitstr_t *bits; /* one bit per flag, default=FALSE */
627 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
629 if (number < low || number > high)
632 bit_set(bits, (number-low));