crontab.c revision 8857
138889Sjdp/* Copyright 1988,1990,1993,1994 by Paul Vixie 238889Sjdp * All rights reserved 338889Sjdp * 438889Sjdp * Distribute freely, except: don't remove my name from the source or 538889Sjdp * documentation (don't take credit for my work), mark your changes (don't 660484Sobrien * get me blamed for your possible bugs), don't alter or remove this 760484Sobrien * notice. May be sold if buildable source is provided to buyer. No 860484Sobrien * warrantee of any kind, express or implied, is included with this 938889Sjdp * software; use at your own risk, responsibility for damages (if any) to 1038889Sjdp * anyone resulting from the use of this software rests entirely with the 1138889Sjdp * user. 1238889Sjdp * 1338889Sjdp * Send bug reports, bug fixes, enhancements, requests, flames, etc., and 1438889Sjdp * I'll try to keep a version up to date. I can be reached as follows: 1538889Sjdp * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul 1638889Sjdp * From Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp 1738889Sjdp */ 1838889Sjdp 1960484Sobrien#if !defined(lint) && !defined(LINT) 2060484Sobrienstatic char rcsid[] = "$Id: crontab.c,v 1.2 1994/12/21 11:06:08 ache Exp $"; 2160484Sobrien#endif 2238889Sjdp 2338889Sjdp/* crontab - install and manage per-user crontab files 2438889Sjdp * vix 02may87 [RCS has the rest of the log] 2560484Sobrien * vix 26jan87 [original] 2660484Sobrien */ 2760484Sobrien 2860484Sobrien 2960484Sobrien#define MAIN_PROGRAM 3060484Sobrien 3138889Sjdp 3238889Sjdp#include "cron.h" 3338889Sjdp#include <errno.h> 3438889Sjdp#include <fcntl.h> 3538889Sjdp#include <sys/file.h> 3638889Sjdp#include <sys/stat.h> 3738889Sjdp#ifdef USE_UTIMES 3838889Sjdp# include <sys/time.h> 3938889Sjdp#else 4060484Sobrien# include <time.h> 4160484Sobrien# include <utime.h> 4260484Sobrien#endif 4338889Sjdp#if defined(POSIX) 4438889Sjdp# include <locale.h> 4538889Sjdp#endif 4660484Sobrien 4760484Sobrien 4838889Sjdp#define NHEADER_LINES 3 4960484Sobrien 5060484Sobrien 5138889Sjdpenum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; 5260484Sobrien 5360484Sobrien#if DEBUGGING 5438889Sjdpstatic char *Options[] = { "???", "list", "delete", "edit", "replace" }; 5560484Sobrien#endif 5660484Sobrien 5738889Sjdp 5860484Sobrienstatic PID_T Pid; 5960484Sobrienstatic char User[MAX_UNAME], RealUser[MAX_UNAME]; 6038889Sjdpstatic char Filename[MAX_FNAME]; 6160484Sobrienstatic FILE *NewCrontab; 6260484Sobrienstatic int CheckErrorCount; 6338889Sjdpstatic enum opt_t Option; 6460484Sobrienstatic struct passwd *pw; 6560484Sobrienstatic void list_cmd __P((void)), 6638889Sjdp delete_cmd __P((void)), 6760484Sobrien edit_cmd __P((void)), 6860484Sobrien poke_daemon __P((void)), 6938889Sjdp check_error __P((char *)), 7060484Sobrien parse_args __P((int c, char *v[])); 7160484Sobrienstatic int replace_cmd __P((void)); 7238889Sjdp 7360484Sobrien 7460484Sobrienstatic void 7538889Sjdpusage(msg) 7660484Sobrien char *msg; 7760484Sobrien{ 7838889Sjdp fprintf(stderr, "%s: usage error: %s\n", ProgramName, msg); 7960484Sobrien fprintf(stderr, "usage:\t%s [-u user] file\n", ProgramName); 8060484Sobrien fprintf(stderr, "\t%s [-u user] { -e | -l | -r }\n", ProgramName); 8138889Sjdp fprintf(stderr, "\t\t(default operation is replace, per 1003.2)\n"); 8260484Sobrien fprintf(stderr, "\t-e\t(edit user's crontab)\n"); 8360484Sobrien fprintf(stderr, "\t-l\t(list user's crontab)\n"); 8438889Sjdp fprintf(stderr, "\t-r\t(delete user's crontab)\n"); 8560484Sobrien exit(ERROR_EXIT); 8660484Sobrien} 8738889Sjdp 8860484Sobrien 8960484Sobrienint 9038889Sjdpmain(argc, argv) 9160484Sobrien int argc; 9260484Sobrien char *argv[]; 9338889Sjdp{ 9460484Sobrien int exitstatus; 9560484Sobrien 9638889Sjdp Pid = getpid(); 9760484Sobrien ProgramName = argv[0]; 9860484Sobrien 9938889Sjdp#if defined(POSIX) 10060484Sobrien setlocale(LC_ALL, ""); 10160484Sobrien#endif 10238889Sjdp 10360484Sobrien#if defined(BSD) 10460484Sobrien setlinebuf(stderr); 10538889Sjdp#endif 10660484Sobrien parse_args(argc, argv); /* sets many globals, opens a file */ 10760484Sobrien set_cron_uid(); 10838889Sjdp set_cron_cwd(); 10938889Sjdp if (!allowed(User)) { 11038889Sjdp fprintf(stderr, 11138889Sjdp "You (%s) are not allowed to use this program (%s)\n", 11260484Sobrien User, ProgramName); 11360484Sobrien fprintf(stderr, "See crontab(1) for more information\n"); 11460484Sobrien log_it(RealUser, Pid, "AUTH", "crontab command not allowed"); 11538889Sjdp exit(ERROR_EXIT); 11638889Sjdp } 11738889Sjdp exitstatus = OK_EXIT; 11838889Sjdp switch (Option) { 11938889Sjdp case opt_list: list_cmd(); 12038889Sjdp break; 12138889Sjdp case opt_delete: delete_cmd(); 12238889Sjdp break; 12338889Sjdp case opt_edit: edit_cmd(); 12438889Sjdp break; 12538889Sjdp case opt_replace: if (replace_cmd() < 0) 12638889Sjdp exitstatus = ERROR_EXIT; 12760484Sobrien break; 12860484Sobrien } 12960484Sobrien exit(0); 13038889Sjdp /*NOTREACHED*/ 13138889Sjdp} 13238889Sjdp 13338889Sjdp 13438889Sjdpstatic void 13538889Sjdpparse_args(argc, argv) 13660484Sobrien int argc; 13760484Sobrien char *argv[]; 13860484Sobrien{ 13938889Sjdp int argch; 14038889Sjdp 14160484Sobrien if (!(pw = getpwuid(getuid()))) { 14260484Sobrien fprintf(stderr, "%s: your UID isn't in the passwd file.\n", 14360484Sobrien ProgramName); 14460484Sobrien fprintf(stderr, "bailing out.\n"); 14560484Sobrien exit(ERROR_EXIT); 14660484Sobrien } 14760484Sobrien strcpy(User, pw->pw_name); 14860484Sobrien strcpy(RealUser, User); 14960484Sobrien Filename[0] = '\0'; 15060484Sobrien Option = opt_unknown; 15177298Sobrien while (EOF != (argch = getopt(argc, argv, "u:lerx:"))) { 15277298Sobrien switch (argch) { 15377298Sobrien case 'x': 15460484Sobrien if (!set_debug_flags(optarg)) 15560484Sobrien usage("bad debug option"); 15660484Sobrien break; 15760484Sobrien case 'u': 15860484Sobrien if (getuid() != ROOT_UID) 15960484Sobrien { 160130561Sobrien fprintf(stderr, 161130561Sobrien "must be privileged to use -u\n"); 162130561Sobrien exit(ERROR_EXIT); 163130561Sobrien } 164130561Sobrien if (!(pw = getpwnam(optarg))) 165130561Sobrien { 166130561Sobrien fprintf(stderr, "%s: user `%s' unknown\n", 167130561Sobrien ProgramName, optarg); 168130561Sobrien exit(ERROR_EXIT); 169130561Sobrien } 170130561Sobrien (void) strcpy(User, optarg); 171130561Sobrien break; 17278828Sobrien case 'l': 17378828Sobrien if (Option != opt_unknown) 17478828Sobrien usage("only one operation permitted"); 17560484Sobrien Option = opt_list; 17660484Sobrien break; 17760484Sobrien case 'r': 17860484Sobrien if (Option != opt_unknown) 17960484Sobrien usage("only one operation permitted"); 18060484Sobrien Option = opt_delete; 18160484Sobrien break; 18260484Sobrien case 'e': 18360484Sobrien if (Option != opt_unknown) 18460484Sobrien usage("only one operation permitted"); 18560484Sobrien Option = opt_edit; 18660484Sobrien break; 18760484Sobrien default: 18860484Sobrien usage("unrecognized option"); 18960484Sobrien } 19060484Sobrien } 19160484Sobrien 19260484Sobrien endpwent(); 19360484Sobrien 19460484Sobrien if (Option != opt_unknown) { 19560484Sobrien if (argv[optind] != NULL) { 19660484Sobrien usage("no arguments permitted after this option"); 19760484Sobrien } 19860484Sobrien } else { 19960484Sobrien if (argv[optind] != NULL) { 20060484Sobrien Option = opt_replace; 20160484Sobrien (void) strcpy (Filename, argv[optind]); 20260484Sobrien } else { 20360484Sobrien usage("file name must be specified for replace"); 20460484Sobrien } 20560484Sobrien } 20660484Sobrien 20760484Sobrien if (Option == opt_replace) { 20860484Sobrien /* we have to open the file here because we're going to 20960484Sobrien * chdir(2) into /var/cron before we get around to 21060484Sobrien * reading the file. 21160484Sobrien */ 21260484Sobrien if (!strcmp(Filename, "-")) { 21360484Sobrien NewCrontab = stdin; 21460484Sobrien } else { 21560484Sobrien /* relinquish the setuid status of the binary during 21660484Sobrien * the open, lest nonroot users read files they should 21760484Sobrien * not be able to read. we can't use access() here 21860484Sobrien * since there's a race condition. thanks go out to 21960484Sobrien * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting 22060484Sobrien * the race. 22160484Sobrien */ 22260484Sobrien 22360484Sobrien if (swap_uids() < OK) { 22460484Sobrien perror("swapping uids"); 22560484Sobrien exit(ERROR_EXIT); 22660484Sobrien } 22760484Sobrien if (!(NewCrontab = fopen(Filename, "r"))) { 22860484Sobrien perror(Filename); 22960484Sobrien exit(ERROR_EXIT); 23060484Sobrien } 23160484Sobrien if (swap_uids() < OK) { 23260484Sobrien perror("swapping uids back"); 23360484Sobrien exit(ERROR_EXIT); 23460484Sobrien } 23560484Sobrien } 23660484Sobrien } 23760484Sobrien 23860484Sobrien Debug(DMISC, ("user=%s, file=%s, option=%s\n", 23960484Sobrien User, Filename, Options[(int)Option])) 24060484Sobrien} 24160484Sobrien 24260484Sobrien 24360484Sobrienstatic void 24460484Sobrienlist_cmd() { 24560484Sobrien char n[MAX_FNAME]; 24660484Sobrien FILE *f; 24760484Sobrien int ch; 24860484Sobrien 24960484Sobrien log_it(RealUser, Pid, "LIST", User); 25060484Sobrien (void) sprintf(n, CRON_TAB(User)); 25160484Sobrien if (!(f = fopen(n, "r"))) { 25260484Sobrien if (errno == ENOENT) 25360484Sobrien fprintf(stderr, "no crontab for %s\n", User); 25460484Sobrien else 25560484Sobrien perror(n); 25660484Sobrien exit(ERROR_EXIT); 25760484Sobrien } 25860484Sobrien 25960484Sobrien /* file is open. copy to stdout, close. 26060484Sobrien */ 26160484Sobrien Set_LineNum(1) 26260484Sobrien while (EOF != (ch = get_char(f))) 26360484Sobrien putchar(ch); 26460484Sobrien fclose(f); 26560484Sobrien} 26660484Sobrien 26760484Sobrien 26860484Sobrienstatic void 26960484Sobriendelete_cmd() { 27060484Sobrien char n[MAX_FNAME]; 27160484Sobrien 27260484Sobrien log_it(RealUser, Pid, "DELETE", User); 27360484Sobrien (void) sprintf(n, CRON_TAB(User)); 27460484Sobrien if (unlink(n)) { 27560484Sobrien if (errno == ENOENT) 27660484Sobrien fprintf(stderr, "no crontab for %s\n", User); 27760484Sobrien else 27860484Sobrien perror(n); 27960484Sobrien exit(ERROR_EXIT); 28060484Sobrien } 28160484Sobrien poke_daemon(); 28260484Sobrien} 283 284 285static void 286check_error(msg) 287 char *msg; 288{ 289 CheckErrorCount++; 290 fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg); 291} 292 293 294static void 295edit_cmd() { 296 char n[MAX_FNAME], q[MAX_TEMPSTR], *editor; 297 FILE *f; 298 int ch, t, x; 299 struct stat statbuf; 300 time_t mtime; 301 WAIT_T waiter; 302 PID_T pid, xpid; 303 304 log_it(RealUser, Pid, "BEGIN EDIT", User); 305 (void) sprintf(n, CRON_TAB(User)); 306 if (!(f = fopen(n, "r"))) { 307 if (errno != ENOENT) { 308 perror(n); 309 exit(ERROR_EXIT); 310 } 311 fprintf(stderr, "no crontab for %s - using an empty one\n", 312 User); 313 if (!(f = fopen("/dev/null", "r"))) { 314 perror("/dev/null"); 315 exit(ERROR_EXIT); 316 } 317 } 318 319 (void) sprintf(Filename, "/tmp/crontab.%d", Pid); 320 if (-1 == (t = open(Filename, O_CREAT|O_EXCL|O_RDWR, 0600))) { 321 perror(Filename); 322 goto fatal; 323 } 324#ifdef HAS_FCHOWN 325 if (fchown(t, getuid(), getgid()) < 0) { 326#else 327 if (chown(Filename, getuid(), getgid()) < 0) { 328#endif 329 perror("fchown"); 330 goto fatal; 331 } 332 if (!(NewCrontab = fdopen(t, "w"))) { 333 perror("fdopen"); 334 goto fatal; 335 } 336 337 Set_LineNum(1) 338 339 /* ignore the top few comments since we probably put them there. 340 */ 341 for (x = 0; x < NHEADER_LINES; x++) { 342 ch = get_char(f); 343 if (EOF == ch) 344 break; 345 if ('#' != ch) { 346 putc(ch, NewCrontab); 347 break; 348 } 349 while (EOF != (ch = get_char(f))) 350 if (ch == '\n') 351 break; 352 if (EOF == ch) 353 break; 354 } 355 356 /* copy the rest of the crontab (if any) to the temp file. 357 */ 358 if (EOF != ch) 359 while (EOF != (ch = get_char(f))) 360 putc(ch, NewCrontab); 361 fclose(f); 362 if (fclose(NewCrontab)) { 363 perror(Filename); 364 exit(ERROR_EXIT); 365 } 366 again: 367 if (stat(Filename, &statbuf) < 0) { 368 perror("stat"); 369 fatal: unlink(Filename); 370 exit(ERROR_EXIT); 371 } 372 mtime = statbuf.st_mtime; 373 374 if ((!(editor = getenv("VISUAL"))) 375 && (!(editor = getenv("EDITOR"))) 376 ) { 377 editor = EDITOR; 378 } 379 380 /* we still have the file open. editors will generally rewrite the 381 * original file rather than renaming/unlinking it and starting a 382 * new one; even backup files are supposed to be made by copying 383 * rather than by renaming. if some editor does not support this, 384 * then don't use it. the security problems are more severe if we 385 * close and reopen the file around the edit. 386 */ 387 388 switch (pid = fork()) { 389 case -1: 390 perror("fork"); 391 goto fatal; 392 case 0: 393 /* child */ 394 if (setuid(getuid()) < 0) { 395 perror("setuid(getuid())"); 396 exit(ERROR_EXIT); 397 } 398 if (chdir("/tmp") < 0) { 399 perror("chdir(/tmp)"); 400 exit(ERROR_EXIT); 401 } 402 if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) { 403 fprintf(stderr, "%s: editor or filename too long\n", 404 ProgramName); 405 exit(ERROR_EXIT); 406 } 407 sprintf(q, "%s %s", editor, Filename); 408 execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", q, NULL); 409 perror(editor); 410 exit(ERROR_EXIT); 411 /*NOTREACHED*/ 412 default: 413 /* parent */ 414 break; 415 } 416 417 /* parent */ 418 xpid = wait(&waiter); 419 if (xpid != pid) { 420 fprintf(stderr, "%s: wrong PID (%d != %d) from \"%s\"\n", 421 ProgramName, xpid, pid, editor); 422 goto fatal; 423 } 424 if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) { 425 fprintf(stderr, "%s: \"%s\" exited with status %d\n", 426 ProgramName, editor, WEXITSTATUS(waiter)); 427 goto fatal; 428 } 429 if (WIFSIGNALED(waiter)) { 430 fprintf(stderr, 431 "%s: \"%s\" killed; signal %d (%score dumped)\n", 432 ProgramName, editor, WTERMSIG(waiter), 433 WCOREDUMP(waiter) ?"" :"no "); 434 goto fatal; 435 } 436 if (stat(Filename, &statbuf) < 0) { 437 perror("stat"); 438 goto fatal; 439 } 440 if (mtime == statbuf.st_mtime) { 441 fprintf(stderr, "%s: no changes made to crontab\n", 442 ProgramName); 443 goto remove; 444 } 445 fprintf(stderr, "%s: installing new crontab\n", ProgramName); 446 if (!(NewCrontab = fopen(Filename, "r"))) { 447 perror(Filename); 448 goto fatal; 449 } 450 switch (replace_cmd()) { 451 case 0: 452 break; 453 case -1: 454 for (;;) { 455 printf("Do you want to retry the same edit? "); 456 fflush(stdout); 457 q[0] = '\0'; 458 (void) fgets(q, sizeof q, stdin); 459 switch (islower(q[0]) ? q[0] : tolower(q[0])) { 460 case 'y': 461 goto again; 462 case 'n': 463 goto abandon; 464 default: 465 fprintf(stderr, "Enter Y or N\n"); 466 } 467 } 468 /*NOTREACHED*/ 469 case -2: 470 abandon: 471 fprintf(stderr, "%s: edits left in %s\n", 472 ProgramName, Filename); 473 goto done; 474 default: 475 fprintf(stderr, "%s: panic: bad switch() in replace_cmd()\n"); 476 goto fatal; 477 } 478 remove: 479 unlink(Filename); 480 done: 481 log_it(RealUser, Pid, "END EDIT", User); 482} 483 484 485/* returns 0 on success 486 * -1 on syntax error 487 * -2 on install error 488 */ 489static int 490replace_cmd() { 491 char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME]; 492 FILE *tmp; 493 int ch, eof; 494 entry *e; 495 time_t now = time(NULL); 496 char **envp = env_init(); 497 498 (void) sprintf(n, "tmp.%d", Pid); 499 (void) sprintf(tn, CRON_TAB(n)); 500 if (!(tmp = fopen(tn, "w+"))) { 501 perror(tn); 502 return (-2); 503 } 504 505 /* write a signature at the top of the file. 506 * 507 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code. 508 */ 509 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n"); 510 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now)); 511 fprintf(tmp, "# (Cron version -- %s)\n", rcsid); 512 513 /* copy the crontab to the tmp 514 */ 515 Set_LineNum(1) 516 while (EOF != (ch = get_char(NewCrontab))) 517 putc(ch, tmp); 518 fclose(NewCrontab); 519 ftruncate(fileno(tmp), ftell(tmp)); 520 fflush(tmp); rewind(tmp); 521 522 if (ferror(tmp)) { 523 fprintf(stderr, "%s: error while writing new crontab to %s\n", 524 ProgramName, tn); 525 fclose(tmp); unlink(tn); 526 return (-2); 527 } 528 529 /* check the syntax of the file being installed. 530 */ 531 532 /* BUG: was reporting errors after the EOF if there were any errors 533 * in the file proper -- kludged it by stopping after first error. 534 * vix 31mar87 535 */ 536 Set_LineNum(1 - NHEADER_LINES) 537 CheckErrorCount = 0; eof = FALSE; 538 while (!CheckErrorCount && !eof) { 539 switch (load_env(envstr, tmp)) { 540 case ERR: 541 eof = TRUE; 542 break; 543 case FALSE: 544 e = load_entry(tmp, check_error, pw, envp); 545 if (e) 546 free(e); 547 break; 548 case TRUE: 549 break; 550 } 551 } 552 553 if (CheckErrorCount != 0) { 554 fprintf(stderr, "errors in crontab file, can't install.\n"); 555 fclose(tmp); unlink(tn); 556 return (-1); 557 } 558 559#ifdef HAS_FCHOWN 560 if (fchown(fileno(tmp), ROOT_UID, -1) < OK) 561#else 562 if (chown(tn, ROOT_UID, -1) < OK) 563#endif 564 { 565 perror("chown"); 566 fclose(tmp); unlink(tn); 567 return (-2); 568 } 569 570#ifdef HAS_FCHMOD 571 if (fchmod(fileno(tmp), 0600) < OK) 572#else 573 if (chmod(tn, 0600) < OK) 574#endif 575 { 576 perror("chown"); 577 fclose(tmp); unlink(tn); 578 return (-2); 579 } 580 581 if (fclose(tmp) == EOF) { 582 perror("fclose"); 583 unlink(tn); 584 return (-2); 585 } 586 587 (void) sprintf(n, CRON_TAB(User)); 588 if (rename(tn, n)) { 589 fprintf(stderr, "%s: error renaming %s to %s\n", 590 ProgramName, tn, n); 591 perror("rename"); 592 unlink(tn); 593 return (-2); 594 } 595 log_it(RealUser, Pid, "REPLACE", User); 596 597 poke_daemon(); 598 599 return (0); 600} 601 602 603static void 604poke_daemon() { 605#ifdef USE_UTIMES 606 struct timeval tvs[2]; 607 struct timezone tz; 608 609 (void) gettimeofday(&tvs[0], &tz); 610 tvs[1] = tvs[0]; 611 if (utimes(SPOOL_DIR, tvs) < OK) { 612 fprintf(stderr, "crontab: can't update mtime on spooldir\n"); 613 perror(SPOOL_DIR); 614 return; 615 } 616#else 617 if (utime(SPOOL_DIR, NULL) < OK) { 618 fprintf(stderr, "crontab: can't update mtime on spooldir\n"); 619 perror(SPOOL_DIR); 620 return; 621 } 622#endif /*USE_UTIMES*/ 623} 624