Pre-2.0 release: Sync with HAMMER 64 - NFS and cross-device link fixes.
[dragonfly.git] / usr.sbin / cron / lib / entry.c
blobf52df881081f0e43a041dba839af0fee26708ac0
1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
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
11 * user.
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]
28 #include "cron.h"
29 #include <grp.h>
30 #ifdef LOGIN_CAP
31 #include <login_cap.h>
32 #endif
34 typedef enum ecode {
35 e_none, e_minute, e_hour, e_dom, e_month, e_dow,
36 e_cmd, e_timespec, e_username, e_group, e_mem
37 #ifdef LOGIN_CAP
38 , e_class
39 #endif
40 } ecode_e;
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[] =
49 "no error",
50 "bad minute",
51 "bad hour",
52 "bad day-of-month",
53 "bad month",
54 "bad day-of-week",
55 "bad command",
56 "bad time specifier",
57 "bad username",
58 "bad group name",
59 "out of memory",
60 #ifdef LOGIN_CAP
61 "bad class name",
62 #endif
66 void
67 free_entry(entry *e)
69 #ifdef LOGIN_CAP
70 if (e->class != NULL)
71 free(e->class);
72 #endif
73 if (e->cmd != NULL)
74 free(e->cmd);
75 if (e->envp != NULL)
76 env_free(e->envp);
77 free(e);
81 /* return NULL if eof or syntax error occurs;
82 * otherwise return a pointer to a new entry.
84 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.
93 * syntax:
94 * user crontab:
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;
101 entry *e;
102 int ch;
103 char cmd[MAX_COMMAND];
104 char envstr[MAX_ENVSTR];
105 char **prev_env;
107 Debug(DPARS, ("load_entry()...about to eat comments\n"))
109 skip_comments(file);
111 ch = get_char(file);
112 if (ch == EOF)
113 return NULL;
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));
122 if (e == NULL) {
123 warn("load_entry: calloc failed");
124 return NULL;
127 if (ch == '@') {
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)
138 * HINT
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);
148 bit_set(e->hour, 0);
149 bit_set(e->dom, 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);
156 bit_set(e->hour, 0);
157 bit_set(e->dom, 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);
164 bit_set(e->hour, 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));
168 bit_set(e->dow, 0);
169 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) {
170 Debug(DPARS, ("load_entry()...daily shortcut\n"))
171 bit_set(e->minute, 0);
172 bit_set(e->hour, 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));
183 } else {
184 ecode = e_timespec;
185 goto eof;
187 /* Advance past whitespace between shortcut and
188 * username/command.
190 Skip_Blanks(ch, file);
191 if (ch == EOF) {
192 ecode = e_cmd;
193 goto eof;
195 } else {
196 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
198 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
199 PPC_NULL, ch, file);
200 if (ch == EOF) {
201 ecode = e_minute;
202 goto eof;
205 /* hours
208 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
209 PPC_NULL, ch, file);
210 if (ch == EOF) {
211 ecode = e_hour;
212 goto eof;
215 /* DOM (days of month)
218 if (ch == '*')
219 e->flags |= DOM_STAR;
220 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
221 PPC_NULL, ch, file);
222 if (ch == EOF) {
223 ecode = e_dom;
224 goto eof;
227 /* month
230 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
231 MonthNames, ch, file);
232 if (ch == EOF) {
233 ecode = e_month;
234 goto eof;
237 /* DOW (days of week)
240 if (ch == '*')
241 e->flags |= DOW_STAR;
242 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
243 DowNames, ch, file);
244 if (ch == EOF) {
245 ecode = e_dow;
246 goto eof;
250 /* make sundays equivilent */
251 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
252 bit_set(e->dow, 0);
253 bit_set(e->dow, 7);
256 /* ch is the first character of a command, or a username */
257 unget_char(ch, file);
259 if (!pw) {
260 char *username = cmd; /* temp buffer */
261 char *s, *group;
262 struct group *grp;
263 #ifdef LOGIN_CAP
264 login_cap_t *lc;
265 #endif
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))
271 if (ch == EOF) {
272 ecode = e_cmd;
273 goto eof;
276 #ifdef LOGIN_CAP
277 if ((s = strrchr(username, '/')) != NULL) {
278 *s = '\0';
279 e->class = strdup(s + 1);
280 if (e->class == NULL)
281 warn("strdup(\"%s\")", s + 1);
282 } else {
283 e->class = strdup(RESOURCE_RC);
284 if (e->class == NULL)
285 warn("strdup(\"%s\")", RESOURCE_RC);
287 if (e->class == NULL) {
288 ecode = e_mem;
289 goto eof;
291 if ((lc = login_getclass(e->class)) == NULL) {
292 ecode = e_class;
293 goto eof;
295 login_close(lc);
296 #endif
297 grp = NULL;
298 if ((s = strrchr(username, ':')) != NULL) {
299 *s = '\0';
300 if ((grp = getgrnam(s + 1)) == NULL) {
301 ecode = e_group;
302 goto eof;
306 pw = getpwnam(username);
307 if (pw == NULL) {
308 ecode = e_username;
309 goto eof;
311 if (grp != NULL)
312 pw->pw_gid = grp->gr_gid;
313 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
314 #ifdef LOGIN_CAP
315 Debug(DPARS, ("load_entry()...class %s\n",e->class))
316 #endif
319 if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
320 ecode = e_username;
321 goto eof;
324 e->uid = pw->pw_uid;
325 e->gid = pw->pw_gid;
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) {
332 warn("env_copy");
333 ecode = e_mem;
334 goto eof;
336 if (!env_get("SHELL", e->envp)) {
337 prev_env = 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);
342 env_free(prev_env);
343 ecode = e_mem;
344 goto eof;
347 prev_env = e->envp;
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);
352 env_free(prev_env);
353 ecode = e_mem;
354 goto eof;
356 if (!env_get("PATH", e->envp)) {
357 prev_env = 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);
362 env_free(prev_env);
363 ecode = e_mem;
364 goto eof;
367 prev_env = e->envp;
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);
372 env_free(prev_env);
373 ecode = e_mem;
374 goto eof;
376 #if defined(BSD)
377 prev_env = e->envp;
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);
382 env_free(prev_env);
383 ecode = e_mem;
384 goto eof;
386 #endif
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...
399 if (ch == EOF) {
400 ecode = e_cmd;
401 goto eof;
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);
409 ecode = e_mem;
410 goto eof;
412 Debug(DPARS, ("load_entry()...returning successfully\n"))
414 /* success, fini, return pointer to the entry we just created...
416 return e;
418 eof:
419 free_entry(e);
420 if (ecode != e_none && error_func)
421 (*error_func)(ecodes[(int)ecode]);
422 while (ch != EOF && ch != '\n')
423 ch = get_char(file);
424 return NULL;
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
434 static char
435 get_list(bitstr_t *bits, int low, int high, char **names, int ch, FILE *file)
437 int done;
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
456 done = FALSE;
457 while (!done) {
458 ch = get_range(bits, low, high, names, ch, file);
459 if (ch == ',')
460 ch = get_char(file);
461 else
462 done = TRUE;
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))
472 return ch;
476 static char
477 get_range(bitstr_t *bits, int low, int high, char **names, int ch, FILE *file)
479 /* range = number | number "-" number [ "/" number ]
482 int i;
483 int num1, num2, num3;
485 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
487 if (ch == '*') {
488 /* '*' means "first-last" but can still be modified by /step
490 num1 = low;
491 num2 = high;
492 ch = get_char(file);
493 if (ch == EOF)
494 return EOF;
495 } else {
496 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
497 return EOF;
499 if (ch != '-') {
500 /* not a range, it's a single number.
502 if (EOF == set_element(bits, low, high, num1))
503 return EOF;
504 return ch;
505 } else {
506 /* eat the dash
508 ch = get_char(file);
509 if (ch == EOF)
510 return EOF;
512 /* get the number following the dash
514 ch = get_number(&num2, low, names, ch, file);
515 if (ch == EOF)
516 return EOF;
520 /* check for step size
522 if (ch == '/') {
523 /* eat the slash
525 ch = get_char(file);
526 if (ch == EOF)
527 return EOF;
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, PPC_NULL, ch, file);
535 if (ch == EOF || num3 == 0)
536 return EOF;
537 } else {
538 /* no step. default==1.
540 num3 = 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))
550 return EOF;
552 return ch;
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
560 * file source
562 static char
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
570 pc = temp;
571 len = 0;
572 all_digits = TRUE;
573 while (isalnum(ch)) {
574 if (++len >= MAX_TEMPSTR)
575 return EOF;
577 *pc++ = ch;
579 if (!isdigit(ch))
580 all_digits = FALSE;
582 ch = get_char(file);
584 *pc = '\0';
585 if (len == 0)
586 return EOF;
588 /* try to find the name in the name list
590 if (names) {
591 for (i = 0; names[i] != NULL; i++) {
592 Debug(DPARS|DEXT,
593 ("get_num, compare(%s,%s)\n", names[i], temp))
594 if (!strcasecmp(names[i], temp)) {
595 *numptr = i+low;
596 return ch;
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.
605 if (all_digits) {
606 *numptr = atoi(temp);
607 return ch;
610 return EOF;
614 static int
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)
620 return EOF;
622 bit_set(bits, (number-low));
623 return OK;