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 39 #ifdef LOGIN_CAP 40 , e_class 41 #endif 42 } ecode_e; 43 44 static char get_list __P((bitstr_t *, int, int, char *[], int, FILE *)), 45 get_range __P((bitstr_t *, int, int, char *[], int, FILE *)), 46 get_number __P((int *, int, char *[], int, FILE *)); 47 static int set_element __P((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 #ifdef LOGIN_CAP 62 "bad class name", 63 #endif 64 }; 65 66 67 void 68 free_entry(e) 69 entry *e; 70 { 71 #ifdef LOGIN_CAP 72 if (e->class != NULL) 73 free(e->class); 74 #endif 75 free(e->cmd); 76 env_free(e->envp); 77 free(e); 78 } 79 80 81 /* return NULL if eof or syntax error occurs; 82 * otherwise return a pointer to a new entry. 83 */ 84 entry * 85 load_entry(file, error_func, pw, envp) 86 FILE *file; 87 void (*error_func)(); 88 struct passwd *pw; 89 char **envp; 90 { 91 /* this function reads one crontab entry -- the next -- from a file. 92 * it skips any leading blank lines, ignores comments, and returns 93 * EOF if for any reason the entry can't be read and parsed. 94 * 95 * the entry is also parsed here. 96 * 97 * syntax: 98 * user crontab: 99 * minutes hours doms months dows cmd\n 100 * system crontab (/etc/crontab): 101 * minutes hours doms months dows USERNAME cmd\n 102 */ 103 104 ecode_e ecode = e_none; 105 entry *e; 106 int ch; 107 char cmd[MAX_COMMAND]; 108 char envstr[MAX_ENVSTR]; 109 110 Debug(DPARS, ("load_entry()...about to eat comments\n")) 111 112 skip_comments(file); 113 114 ch = get_char(file); 115 if (ch == EOF) 116 return NULL; 117 118 /* ch is now the first useful character of a useful line. 119 * it may be an @special or it may be the first character 120 * of a list of minutes. 121 */ 122 123 e = (entry *) calloc(sizeof(entry), sizeof(char)); 124 125 if (ch == '@') { 126 /* all of these should be flagged and load-limited; i.e., 127 * instead of @hourly meaning "0 * * * *" it should mean 128 * "close to the front of every hour but not 'til the 129 * system load is low". Problems are: how do you know 130 * what "low" means? (save me from /etc/cron.conf!) and: 131 * how to guarantee low variance (how low is low?), which 132 * means how to we run roughly every hour -- seems like 133 * we need to keep a history or let the first hour set 134 * the schedule, which means we aren't load-limited 135 * anymore. too much for my overloaded brain. (vix, jan90) 136 * HINT 137 */ 138 ch = get_string(cmd, MAX_COMMAND, file, " \t\n"); 139 if (!strcmp("reboot", cmd)) { 140 e->flags |= WHEN_REBOOT; 141 } else if (!strcmp("yearly", cmd) || !strcmp("annually", cmd)){ 142 bit_set(e->minute, 0); 143 bit_set(e->hour, 0); 144 bit_set(e->dom, 0); 145 bit_set(e->month, 0); 146 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1)); 147 } else if (!strcmp("monthly", cmd)) { 148 bit_set(e->minute, 0); 149 bit_set(e->hour, 0); 150 bit_set(e->dom, 0); 151 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1)); 152 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1)); 153 } else if (!strcmp("weekly", cmd)) { 154 bit_set(e->minute, 0); 155 bit_set(e->hour, 0); 156 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1)); 157 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1)); 158 bit_set(e->dow, 0); 159 } else if (!strcmp("daily", cmd) || !strcmp("midnight", cmd)) { 160 bit_set(e->minute, 0); 161 bit_set(e->hour, 0); 162 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1)); 163 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1)); 164 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1)); 165 } else if (!strcmp("hourly", cmd)) { 166 bit_set(e->minute, 0); 167 bit_set(e->hour, (LAST_HOUR-FIRST_HOUR+1)); 168 bit_nset(e->dom, 0, (LAST_DOM-FIRST_DOM+1)); 169 bit_nset(e->month, 0, (LAST_MONTH-FIRST_MONTH+1)); 170 bit_nset(e->dow, 0, (LAST_DOW-FIRST_DOW+1)); 171 } else { 172 ecode = e_timespec; 173 goto eof; 174 } 175 } else { 176 Debug(DPARS, ("load_entry()...about to parse numerics\n")) 177 178 ch = get_list(e->minute, FIRST_MINUTE, LAST_MINUTE, 179 PPC_NULL, ch, file); 180 if (ch == EOF) { 181 ecode = e_minute; 182 goto eof; 183 } 184 185 /* hours 186 */ 187 188 ch = get_list(e->hour, FIRST_HOUR, LAST_HOUR, 189 PPC_NULL, ch, file); 190 if (ch == EOF) { 191 ecode = e_hour; 192 goto eof; 193 } 194 195 /* DOM (days of month) 196 */ 197 198 if (ch == '*') 199 e->flags |= DOM_STAR; 200 ch = get_list(e->dom, FIRST_DOM, LAST_DOM, 201 PPC_NULL, ch, file); 202 if (ch == EOF) { 203 ecode = e_dom; 204 goto eof; 205 } 206 207 /* month 208 */ 209 210 ch = get_list(e->month, FIRST_MONTH, LAST_MONTH, 211 MonthNames, ch, file); 212 if (ch == EOF) { 213 ecode = e_month; 214 goto eof; 215 } 216 217 /* DOW (days of week) 218 */ 219 220 if (ch == '*') 221 e->flags |= DOW_STAR; 222 ch = get_list(e->dow, FIRST_DOW, LAST_DOW, 223 DowNames, ch, file); 224 if (ch == EOF) { 225 ecode = e_dow; 226 goto eof; 227 } 228 } 229 230 /* make sundays equivilent */ 231 if (bit_test(e->dow, 0) || bit_test(e->dow, 7)) { 232 bit_set(e->dow, 0); 233 bit_set(e->dow, 7); 234 } 235 236 /* ch is the first character of a command, or a username */ 237 unget_char(ch, file); 238 239 if (!pw) { 240 char *username = cmd; /* temp buffer */ 241 char *s, *group; 242 struct group *grp; 243 244 Debug(DPARS, ("load_entry()...about to parse username\n")) 245 ch = get_string(username, MAX_COMMAND, file, " \t"); 246 247 Debug(DPARS, ("load_entry()...got %s\n",username)) 248 if (ch == EOF) { 249 ecode = e_cmd; 250 goto eof; 251 } 252 253 #ifdef LOGIN_CAP 254 if ((s = strrchr(username, '/')) != NULL) { 255 *s = '\0'; 256 e->class = strdup(s + 1); 257 } else 258 e->class = strdup(RESOURCE_RC); 259 if (login_getclass(e->class) == NULL) { 260 ecode = e_class; 261 goto eof; 262 } 263 #endif 264 grp = NULL; 265 if ((s = strrchr(username, ':')) != NULL) { 266 *s = '\0'; 267 if ((grp = getgrnam(s + 1)) == NULL) { 268 ecode = e_group; 269 goto eof; 270 } 271 } 272 273 pw = getpwnam(username); 274 if (pw == NULL) { 275 ecode = e_username; 276 goto eof; 277 } 278 if (grp != NULL) 279 pw->pw_gid = grp->gr_gid; 280 Debug(DPARS, ("load_entry()...uid %d, gid %d\n",pw->pw_uid,pw->pw_gid)) 281 #ifdef LOGIN_CAP 282 Debug(DPARS, ("load_entry()...class %s\n",e->class)) 283 #endif 284 } 285 286 if (pw->pw_expire && time(NULL) >= pw->pw_expire) { 287 ecode = e_username; 288 goto eof; 289 } 290 291 e->uid = pw->pw_uid; 292 e->gid = pw->pw_gid; 293 294 /* copy and fix up environment. some variables are just defaults and 295 * others are overrides. 296 */ 297 e->envp = env_copy(envp); 298 if (!env_get("SHELL", e->envp)) { 299 sprintf(envstr, "SHELL=%s", _PATH_BSHELL); 300 e->envp = env_set(e->envp, envstr); 301 } 302 sprintf(envstr, "HOME=%s", pw->pw_dir); 303 e->envp = env_set(e->envp, envstr); 304 if (!env_get("PATH", e->envp)) { 305 sprintf(envstr, "PATH=%s", _PATH_DEFPATH); 306 e->envp = env_set(e->envp, envstr); 307 } 308 sprintf(envstr, "%s=%s", "LOGNAME", pw->pw_name); 309 e->envp = env_set(e->envp, envstr); 310 #if defined(BSD) 311 sprintf(envstr, "%s=%s", "USER", pw->pw_name); 312 e->envp = env_set(e->envp, envstr); 313 #endif 314 315 Debug(DPARS, ("load_entry()...about to parse command\n")) 316 317 /* Everything up to the next \n or EOF is part of the command... 318 * too bad we don't know in advance how long it will be, since we 319 * need to malloc a string for it... so, we limit it to MAX_COMMAND. 320 * XXX - should use realloc(). 321 */ 322 ch = get_string(cmd, MAX_COMMAND, file, "\n"); 323 324 /* a file without a \n before the EOF is rude, so we'll complain... 325 */ 326 if (ch == EOF) { 327 ecode = e_cmd; 328 goto eof; 329 } 330 331 /* got the command in the 'cmd' string; save it in *e. 332 */ 333 e->cmd = strdup(cmd); 334 335 Debug(DPARS, ("load_entry()...returning successfully\n")) 336 337 /* success, fini, return pointer to the entry we just created... 338 */ 339 return e; 340 341 eof: 342 free(e); 343 if (ecode != e_none && error_func) 344 (*error_func)(ecodes[(int)ecode]); 345 while (ch != EOF && ch != '\n') 346 ch = get_char(file); 347 return NULL; 348 } 349 350 351 static char 352 get_list(bits, low, high, names, ch, file) 353 bitstr_t *bits; /* one bit per flag, default=FALSE */ 354 int low, high; /* bounds, impl. offset for bitstr */ 355 char *names[]; /* NULL or *[] of names for these elements */ 356 int ch; /* current character being processed */ 357 FILE *file; /* file being read */ 358 { 359 register int done; 360 361 /* we know that we point to a non-blank character here; 362 * must do a Skip_Blanks before we exit, so that the 363 * next call (or the code that picks up the cmd) can 364 * assume the same thing. 365 */ 366 367 Debug(DPARS|DEXT, ("get_list()...entered\n")) 368 369 /* list = range {"," range} 370 */ 371 372 /* clear the bit string, since the default is 'off'. 373 */ 374 bit_nclear(bits, 0, (high-low+1)); 375 376 /* process all ranges 377 */ 378 done = FALSE; 379 while (!done) { 380 ch = get_range(bits, low, high, names, ch, file); 381 if (ch == ',') 382 ch = get_char(file); 383 else 384 done = TRUE; 385 } 386 387 /* exiting. skip to some blanks, then skip over the blanks. 388 */ 389 Skip_Nonblanks(ch, file) 390 Skip_Blanks(ch, file) 391 392 Debug(DPARS|DEXT, ("get_list()...exiting w/ %02x\n", ch)) 393 394 return ch; 395 } 396 397 398 static char 399 get_range(bits, low, high, names, ch, file) 400 bitstr_t *bits; /* one bit per flag, default=FALSE */ 401 int low, high; /* bounds, impl. offset for bitstr */ 402 char *names[]; /* NULL or names of elements */ 403 int ch; /* current character being processed */ 404 FILE *file; /* file being read */ 405 { 406 /* range = number | number "-" number [ "/" number ] 407 */ 408 409 register int i; 410 auto int num1, num2, num3; 411 412 Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n")) 413 414 if (ch == '*') { 415 /* '*' means "first-last" but can still be modified by /step 416 */ 417 num1 = low; 418 num2 = high; 419 ch = get_char(file); 420 if (ch == EOF) 421 return EOF; 422 } else { 423 if (EOF == (ch = get_number(&num1, low, names, ch, file))) 424 return EOF; 425 426 if (ch != '-') { 427 /* not a range, it's a single number. 428 */ 429 if (EOF == set_element(bits, low, high, num1)) 430 return EOF; 431 return ch; 432 } else { 433 /* eat the dash 434 */ 435 ch = get_char(file); 436 if (ch == EOF) 437 return EOF; 438 439 /* get the number following the dash 440 */ 441 ch = get_number(&num2, low, names, ch, file); 442 if (ch == EOF) 443 return EOF; 444 } 445 } 446 447 /* check for step size 448 */ 449 if (ch == '/') { 450 /* eat the slash 451 */ 452 ch = get_char(file); 453 if (ch == EOF) 454 return EOF; 455 456 /* get the step size -- note: we don't pass the 457 * names here, because the number is not an 458 * element id, it's a step size. 'low' is 459 * sent as a 0 since there is no offset either. 460 */ 461 ch = get_number(&num3, 0, PPC_NULL, ch, file); 462 if (ch == EOF) 463 return EOF; 464 } else { 465 /* no step. default==1. 466 */ 467 num3 = 1; 468 } 469 470 /* range. set all elements from num1 to num2, stepping 471 * by num3. (the step is a downward-compatible extension 472 * proposed conceptually by bob@acornrc, syntactically 473 * designed then implmented by paul vixie). 474 */ 475 for (i = num1; i <= num2; i += num3) 476 if (EOF == set_element(bits, low, high, i)) 477 return EOF; 478 479 return ch; 480 } 481 482 483 static char 484 get_number(numptr, low, names, ch, file) 485 int *numptr; /* where does the result go? */ 486 int low; /* offset applied to result if symbolic enum used */ 487 char *names[]; /* symbolic names, if any, for enums */ 488 int ch; /* current character */ 489 FILE *file; /* source */ 490 { 491 char temp[MAX_TEMPSTR], *pc; 492 int len, i, all_digits; 493 494 /* collect alphanumerics into our fixed-size temp array 495 */ 496 pc = temp; 497 len = 0; 498 all_digits = TRUE; 499 while (isalnum(ch)) { 500 if (++len >= MAX_TEMPSTR) 501 return EOF; 502 503 *pc++ = ch; 504 505 if (!isdigit(ch)) 506 all_digits = FALSE; 507 508 ch = get_char(file); 509 } 510 *pc = '\0'; 511 512 /* try to find the name in the name list 513 */ 514 if (names) { 515 for (i = 0; names[i] != NULL; i++) { 516 Debug(DPARS|DEXT, 517 ("get_num, compare(%s,%s)\n", names[i], temp)) 518 if (!strcasecmp(names[i], temp)) { 519 *numptr = i+low; 520 return ch; 521 } 522 } 523 } 524 525 /* no name list specified, or there is one and our string isn't 526 * in it. either way: if it's all digits, use its magnitude. 527 * otherwise, it's an error. 528 */ 529 if (all_digits) { 530 *numptr = atoi(temp); 531 return ch; 532 } 533 534 return EOF; 535 } 536 537 538 static int 539 set_element(bits, low, high, number) 540 bitstr_t *bits; /* one bit per flag, default=FALSE */ 541 int low; 542 int high; 543 int number; 544 { 545 Debug(DPARS|DEXT, ("set_element(?,%d,%d,%d)\n", low, high, number)) 546 547 if (number < low || number > high) 548 return EOF; 549 550 bit_set(bits, (number-low)); 551 return OK; 552 } 553