]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/cron/lib/entry.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / cron / lib / entry.c
1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2  * All rights reserved
3  *
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.
12  *
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
16  */
17
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
20   "$FreeBSD$";
21 #endif
22
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]
27  */
28
29
30 #include "cron.h"
31 #include <grp.h>
32 #ifdef LOGIN_CAP
33 #include <login_cap.h>
34 #endif
35
36 typedef enum ecode {
37         e_none, e_minute, e_hour, e_dom, e_month, e_dow,
38         e_cmd, e_timespec, e_username, e_group, e_mem
39 #ifdef LOGIN_CAP
40         , e_class
41 #endif
42 } ecode_e;
43
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);
48
49 static char *ecodes[] =
50         {
51                 "no error",
52                 "bad minute",
53                 "bad hour",
54                 "bad day-of-month",
55                 "bad month",
56                 "bad day-of-week",
57                 "bad command",
58                 "bad time specifier",
59                 "bad username",
60                 "bad group name",
61                 "out of memory",
62 #ifdef LOGIN_CAP
63                 "bad class name",
64 #endif
65         };
66
67
68 void
69 free_entry(e)
70         entry   *e;
71 {
72 #ifdef LOGIN_CAP
73         if (e->class != NULL)
74                 free(e->class);
75 #endif
76         if (e->cmd != NULL)
77                 free(e->cmd);
78         if (e->envp != NULL)
79                 env_free(e->envp);
80         free(e);
81 }
82
83
84 /* return NULL if eof or syntax error occurs;
85  * otherwise return a pointer to a new entry.
86  */
87 entry *
88 load_entry(file, error_func, pw, envp)
89         FILE            *file;
90         void            (*error_func)(char *);
91         struct passwd   *pw;
92         char            **envp;
93 {
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.
97          *
98          * the entry is also parsed here.
99          *
100          * syntax:
101          *   user crontab:
102          *      minutes hours doms months dows cmd\n
103          *   system crontab (/etc/crontab):
104          *      minutes hours doms months dows USERNAME cmd\n
105          */
106
107         ecode_e ecode = e_none;
108         entry   *e;
109         int     ch;
110         char    cmd[MAX_COMMAND];
111         char    envstr[MAX_ENVSTR];
112         char    **prev_env;
113
114         Debug(DPARS, ("load_entry()...about to eat comments\n"))
115
116         skip_comments(file);
117
118         ch = get_char(file);
119         if (ch == EOF)
120                 return NULL;
121
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.
125          */
126
127         e = (entry *) calloc(sizeof(entry), sizeof(char));
128
129         if (e == NULL) {
130                 warn("load_entry: calloc failed");
131                 return NULL;
132         }
133
134         if (ch == '@') {
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)
145                  * HINT
146                  */
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);
156                         bit_set(e->hour, 0);
157                         bit_set(e->dom, 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);
165                         bit_set(e->hour, 0);
166                         bit_set(e->dom, 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);
174                         bit_set(e->hour, 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));
178                         bit_set(e->dow, 0);
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);
183                         bit_set(e->hour, 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"))
205                         e->flags |= SEC_RES;
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));
212                 } else {
213                         ecode = e_timespec;
214                         goto eof;
215                 }
216                 /* Advance past whitespace between shortcut and
217                  * username/command.
218                  */
219                 Skip_Blanks(ch, file);
220                 if (ch == EOF) {
221                         ecode = e_cmd;
222                         goto eof;
223                 }
224         } else {
225                 Debug(DPARS, ("load_entry()...about to parse numerics\n"))
226                 bit_set(e->second, 0);
227
228                 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE,
229                               PPC_NULL, ch, file);
230                 if (ch == EOF) {
231                         ecode = e_minute;
232                         goto eof;
233                 }
234
235                 /* hours
236                  */
237
238                 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR,
239                               PPC_NULL, ch, file);
240                 if (ch == EOF) {
241                         ecode = e_hour;
242                         goto eof;
243                 }
244
245                 /* DOM (days of month)
246                  */
247
248                 if (ch == '*')
249                         e->flags |= DOM_STAR;
250                 ch = get_list(e->dom, FIRST_DOM, LAST_DOM,
251                               PPC_NULL, ch, file);
252                 if (ch == EOF) {
253                         ecode = e_dom;
254                         goto eof;
255                 }
256
257                 /* month
258                  */
259
260                 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH,
261                               MonthNames, ch, file);
262                 if (ch == EOF) {
263                         ecode = e_month;
264                         goto eof;
265                 }
266
267                 /* DOW (days of week)
268                  */
269
270                 if (ch == '*')
271                         e->flags |= DOW_STAR;
272                 ch = get_list(e->dow, FIRST_DOW, LAST_DOW,
273                               DowNames, ch, file);
274                 if (ch == EOF) {
275                         ecode = e_dow;
276                         goto eof;
277                 }
278         }
279
280         /* make sundays equivalent */
281         if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) {
282                 bit_set(e->dow, 0);
283                 bit_set(e->dow, 7);
284         }
285
286         /* ch is the first character of a command, or a username */
287         unget_char(ch, file);
288
289         if (!pw) {
290                 char            *username = cmd;        /* temp buffer */
291                 char            *s;
292                 struct group    *grp;
293 #ifdef LOGIN_CAP
294                 login_cap_t *lc;
295 #endif
296
297                 Debug(DPARS, ("load_entry()...about to parse username\n"))
298                 ch = get_string(username, MAX_COMMAND, file, " \t");
299
300                 Debug(DPARS, ("load_entry()...got %s\n",username))
301                 if (ch == EOF) {
302                         ecode = e_cmd;
303                         goto eof;
304                 }
305
306 #ifdef LOGIN_CAP
307                 if ((s = strrchr(username, '/')) != NULL) {
308                         *s = '\0';
309                         e->class = strdup(s + 1);
310                         if (e->class == NULL)
311                                 warn("strdup(\"%s\")", s + 1);
312                 } else {
313                         e->class = strdup(RESOURCE_RC);
314                         if (e->class == NULL)
315                                 warn("strdup(\"%s\")", RESOURCE_RC);
316                 }
317                 if (e->class == NULL) {
318                         ecode = e_mem;
319                         goto eof;
320                 }
321                 if ((lc = login_getclass(e->class)) == NULL) {
322                         ecode = e_class;
323                         goto eof;
324                 }
325                 login_close(lc);
326 #endif
327                 grp = NULL;
328                 if ((s = strrchr(username, ':')) != NULL) {
329                         *s = '\0';
330                         if ((grp = getgrnam(s + 1)) == NULL) {
331                                 ecode = e_group;
332                                 goto eof;
333                         }
334                 }
335
336                 pw = getpwnam(username);
337                 if (pw == NULL) {
338                         ecode = e_username;
339                         goto eof;
340                 }
341                 if (grp != NULL)
342                         pw->pw_gid = grp->gr_gid;
343                 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid))
344 #ifdef LOGIN_CAP
345                 Debug(DPARS, ("load_entry()...class %s\n",e->class))
346 #endif
347         }
348
349 #ifndef PAM     /* PAM takes care of account expiration by itself */
350         if (pw->pw_expire && time(NULL) >= pw->pw_expire) {
351                 ecode = e_username;
352                 goto eof;
353         }
354 #endif /* !PAM */
355
356         e->uid = pw->pw_uid;
357         e->gid = pw->pw_gid;
358
359         /* copy and fix up environment.  some variables are just defaults and
360          * others are overrides.
361          */
362         e->envp = env_copy(envp);
363         if (e->envp == NULL) {
364                 warn("env_copy");
365                 ecode = e_mem;
366                 goto eof;
367         }
368         if (!env_get("SHELL", e->envp)) {
369                 prev_env = 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);
374                         env_free(prev_env);
375                         ecode = e_mem;
376                         goto eof;
377                 }
378         }
379         if (!env_get("HOME", e->envp)) {
380                 prev_env = 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);
385                         env_free(prev_env);
386                         ecode = e_mem;
387                         goto eof;
388                 }
389         }
390         if (!env_get("PATH", e->envp)) {
391                 prev_env = 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);
396                         env_free(prev_env);
397                         ecode = e_mem;
398                         goto eof;
399                 }
400         }
401         prev_env = e->envp;
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);
406                 env_free(prev_env);
407                 ecode = e_mem;
408                 goto eof;
409         }
410 #if defined(BSD)
411         prev_env = e->envp;
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);
416                 env_free(prev_env);
417                 ecode = e_mem;
418                 goto eof;
419         }
420 #endif
421
422         Debug(DPARS, ("load_entry()...about to parse command\n"))
423
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().
428          */
429         ch = get_string(cmd, MAX_COMMAND, file, "\n");
430
431         /* a file without a \n before the EOF is rude, so we'll complain...
432          */
433         if (ch == EOF) {
434                 ecode = e_cmd;
435                 goto eof;
436         }
437
438         /* got the command in the 'cmd' string; save it in *e.
439          */
440         e->cmd = strdup(cmd);
441         if (e->cmd == NULL) {
442                 warn("strdup(\"%s\")", cmd);
443                 ecode = e_mem;
444                 goto eof;
445         }
446         Debug(DPARS, ("load_entry()...returning successfully\n"))
447
448         /* success, fini, return pointer to the entry we just created...
449          */
450         return e;
451
452  eof:
453         free_entry(e);
454         if (ecode != e_none && error_func)
455                 (*error_func)(ecodes[(int)ecode]);
456         while (ch != EOF && ch != '\n')
457                 ch = get_char(file);
458         return NULL;
459 }
460
461
462 static char
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 */
469 {
470         register int    done;
471
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.
476          */
477
478         Debug(DPARS|DEXT, ("get_list()...entered\n"))
479
480         /* list = range {"," range}
481          */
482
483         /* clear the bit string, since the default is 'off'.
484          */
485         bit_nclear(bits, 0, (high-low+1));
486
487         /* process all ranges
488          */
489         done = FALSE;
490         while (!done) {
491                 ch = get_range(bits, low, high, names, ch, file);
492                 if (ch == ',')
493                         ch = get_char(file);
494                 else
495                         done = TRUE;
496         }
497
498         /* exiting.  skip to some blanks, then skip over the blanks.
499          */
500         Skip_Nonblanks(ch, file)
501         Skip_Blanks(ch, file)
502
503         Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch))
504
505         return ch;
506 }
507
508
509 static char
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 */
516 {
517         /* range = number | number "-" number [ "/" number ]
518          */
519
520         register int    i;
521         auto int        num1, num2, num3;
522
523         Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
524
525         if (ch == '*') {
526                 /* '*' means "first-last" but can still be modified by /step
527                  */
528                 num1 = low;
529                 num2 = high;
530                 ch = get_char(file);
531                 if (ch == EOF)
532                         return EOF;
533         } else {
534                 if (EOF == (ch = get_number(&num1, low, names, ch, file)))
535                         return EOF;
536
537                 if (ch == '/')
538                         num2 = high;
539                 else if (ch != '-') {
540                         /* not a range, it's a single number.
541                          */
542                         if (EOF == set_element(bits, low, high, num1))
543                                 return EOF;
544                         return ch;
545                 } else {
546                         /* eat the dash
547                          */
548                         ch = get_char(file);
549                         if (ch == EOF)
550                                 return EOF;
551
552                         /* get the number following the dash
553                          */
554                         ch = get_number(&num2, low, names, ch, file);
555                         if (ch == EOF)
556                                 return EOF;
557                 }
558         }
559
560         /* check for step size
561          */
562         if (ch == '/') {
563                 /* eat the slash
564                  */
565                 ch = get_char(file);
566                 if (ch == EOF)
567                         return EOF;
568
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.
573                  */
574                 ch = get_number(&num3, 0, PPC_NULL, ch, file);
575                 if (ch == EOF || num3 == 0)
576                         return EOF;
577         } else {
578                 /* no step.  default==1.
579                  */
580                 num3 = 1;
581         }
582
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).
587          */
588         for (i = num1;  i <= num2;  i += num3)
589                 if (EOF == set_element(bits, low, high, i))
590                         return EOF;
591
592         return ch;
593 }
594
595
596 static char
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 */
603 {
604         char    temp[MAX_TEMPSTR], *pc;
605         int     len, i, all_digits;
606
607         /* collect alphanumerics into our fixed-size temp array
608          */
609         pc = temp;
610         len = 0;
611         all_digits = TRUE;
612         while (isalnum(ch)) {
613                 if (++len >= MAX_TEMPSTR)
614                         return EOF;
615
616                 *pc++ = ch;
617
618                 if (!isdigit(ch))
619                         all_digits = FALSE;
620
621                 ch = get_char(file);
622         }
623         *pc = '\0';
624         if (len == 0)
625             return (EOF);
626
627         /* try to find the name in the name list
628          */
629         if (names) {
630                 for (i = 0;  names[i] != NULL;  i++) {
631                         Debug(DPARS|DEXT,
632                                 ("get_num, compare(%s,%s)\n", names[i], temp))
633                         if (!strcasecmp(names[i], temp)) {
634                                 *numptr = i+low;
635                                 return ch;
636                         }
637                 }
638         }
639
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.
643          */
644         if (all_digits) {
645                 *numptr = atoi(temp);
646                 return ch;
647         }
648
649         return EOF;
650 }
651
652
653 static int
654 set_element(bits, low, high, number)
655         bitstr_t        *bits;          /* one bit per flag, default=FALSE */
656         int             low;
657         int             high;
658         int             number;
659 {
660         Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number))
661
662         if (number < low || number > high)
663                 return EOF;
664
665         bit_set(bits, (number-low));
666         return OK;
667 }