crontab.c revision 184780
1151937Sjkim/* Copyright 1988,1990,1993,1994 by Paul Vixie 2151937Sjkim * All rights reserved 3151937Sjkim * 4167802Sjkim * Distribute freely, except: don't remove my name from the source or 5151937Sjkim * documentation (don't take credit for my work), mark your changes (don't 6151937Sjkim * get me blamed for your possible bugs), don't alter or remove this 7151937Sjkim * notice. May be sold if buildable source is provided to buyer. No 8151937Sjkim * warrantee of any kind, express or implied, is included with this 9151937Sjkim * software; use at your own risk, responsibility for damages (if any) to 10151937Sjkim * anyone resulting from the use of this software rests entirely with the 11151937Sjkim * user. 12167802Sjkim * 13151937Sjkim * Send bug reports, bug fixes, enhancements, requests, flames, etc., and 14151937Sjkim * I'll try to keep a version up to date. I can be reached as follows: 15151937Sjkim * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul 16151937Sjkim * From Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp 17151937Sjkim */ 18151937Sjkim 19151937Sjkim#if !defined(lint) && !defined(LINT) 20151937Sjkimstatic const char rcsid[] = 21151937Sjkim "$FreeBSD: head/usr.sbin/cron/crontab/crontab.c 184780 2008-11-09 07:34:11Z matteo $"; 22151937Sjkim#endif 23151937Sjkim 24151937Sjkim/* crontab - install and manage per-user crontab files 25151937Sjkim * vix 02may87 [RCS has the rest of the log] 26151937Sjkim * vix 26jan87 [original] 27151937Sjkim */ 28151937Sjkim 29151937Sjkim#define MAIN_PROGRAM 30151937Sjkim 31151937Sjkim#include "cron.h" 32151937Sjkim#include <errno.h> 33151937Sjkim#include <fcntl.h> 34151937Sjkim#include <md5.h> 35151937Sjkim#include <paths.h> 36151937Sjkim#include <sys/file.h> 37151937Sjkim#include <sys/stat.h> 38151937Sjkim#ifdef USE_UTIMES 39151937Sjkim# include <sys/time.h> 40151937Sjkim#else 41151937Sjkim# include <time.h> 42151937Sjkim# include <utime.h> 43151937Sjkim#endif 44151937Sjkim#if defined(POSIX) 45151937Sjkim# include <locale.h> 46151937Sjkim#endif 47151937Sjkim 48151937Sjkim#define MD5_SIZE 33 49151937Sjkim#define NHEADER_LINES 3 50151937Sjkim 51151937Sjkim 52151937Sjkimenum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; 53151937Sjkim 54151937Sjkim#if DEBUGGING 55151937Sjkimstatic char *Options[] = { "???", "list", "delete", "edit", "replace" }; 56151937Sjkim#endif 57151937Sjkim 58151937Sjkim 59151937Sjkimstatic PID_T Pid; 60151937Sjkimstatic char User[MAX_UNAME], RealUser[MAX_UNAME]; 61151937Sjkimstatic char Filename[MAX_FNAME]; 62151937Sjkimstatic FILE *NewCrontab; 63151937Sjkimstatic int CheckErrorCount; 64151937Sjkimstatic enum opt_t Option; 65151937Sjkimstatic struct passwd *pw; 66151937Sjkimstatic char *tmp_path; 67151937Sjkimstatic void list_cmd(void), 68151937Sjkim delete_cmd(void), 69151937Sjkim edit_cmd(void), 70151937Sjkim poke_daemon(void), 71151937Sjkim check_error(char *), 72151937Sjkim parse_args(int c, char *v[]); 73151937Sjkimstatic int replace_cmd(void); 74151937Sjkim 75151937Sjkim 76151937Sjkimstatic void 77151937Sjkimusage(msg) 78151937Sjkim char *msg; 79151937Sjkim{ 80151937Sjkim fprintf(stderr, "crontab: usage error: %s\n", msg); 81151937Sjkim fprintf(stderr, "%s\n%s\n", 82151937Sjkim "usage: crontab [-u user] file", 83151937Sjkim " crontab [-u user] { -e | -l | -r }"); 84151937Sjkim exit(ERROR_EXIT); 85151937Sjkim} 86151937Sjkim 87151937Sjkim 88151937Sjkimint 89151937Sjkimmain(argc, argv) 90151937Sjkim int argc; 91151937Sjkim char *argv[]; 92151937Sjkim{ 93151937Sjkim int exitstatus; 94151937Sjkim 95151937Sjkim Pid = getpid(); 96151937Sjkim ProgramName = argv[0]; 97151937Sjkim 98151937Sjkim#if defined(POSIX) 99151937Sjkim setlocale(LC_ALL, ""); 100151937Sjkim#endif 101151937Sjkim 102151937Sjkim#if defined(BSD) 103151937Sjkim setlinebuf(stderr); 104151937Sjkim#endif 105151937Sjkim parse_args(argc, argv); /* sets many globals, opens a file */ 106151937Sjkim set_cron_uid(); 107151937Sjkim set_cron_cwd(); 108151937Sjkim if (!allowed(User)) { 109151937Sjkim warnx("you (%s) are not allowed to use this program", User); 110151937Sjkim log_it(RealUser, Pid, "AUTH", "crontab command not allowed"); 111151937Sjkim exit(ERROR_EXIT); 112151937Sjkim } 113151937Sjkim exitstatus = OK_EXIT; 114151937Sjkim switch (Option) { 115151937Sjkim case opt_list: list_cmd(); 116151937Sjkim break; 117151937Sjkim case opt_delete: delete_cmd(); 118151937Sjkim break; 119151937Sjkim case opt_edit: edit_cmd(); 120151937Sjkim break; 121151937Sjkim case opt_replace: if (replace_cmd() < 0) 122151937Sjkim exitstatus = ERROR_EXIT; 123151937Sjkim break; 124151937Sjkim case opt_unknown: 125151937Sjkim break; 126151937Sjkim } 127151937Sjkim exit(exitstatus); 128151937Sjkim /*NOTREACHED*/ 129151937Sjkim} 130151937Sjkim 131151937Sjkim 132151937Sjkimstatic void 133151937Sjkimparse_args(argc, argv) 134151937Sjkim int argc; 135151937Sjkim char *argv[]; 136151937Sjkim{ 137151937Sjkim int argch; 138151937Sjkim char resolved_path[PATH_MAX]; 139151937Sjkim 140151937Sjkim if (!(pw = getpwuid(getuid()))) 141151937Sjkim errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out"); 142151937Sjkim bzero(pw->pw_passwd, strlen(pw->pw_passwd)); 143151937Sjkim (void) strncpy(User, pw->pw_name, (sizeof User)-1); 144151937Sjkim User[(sizeof User)-1] = '\0'; 145151937Sjkim strcpy(RealUser, User); 146151937Sjkim Filename[0] = '\0'; 147151937Sjkim Option = opt_unknown; 148151937Sjkim while ((argch = getopt(argc, argv, "u:lerx:")) != -1) { 149151937Sjkim switch (argch) { 150151937Sjkim case 'x': 151151937Sjkim if (!set_debug_flags(optarg)) 152151937Sjkim usage("bad debug option"); 153151937Sjkim break; 154151937Sjkim case 'u': 155151937Sjkim if (getuid() != ROOT_UID) 156151937Sjkim errx(ERROR_EXIT, "must be privileged to use -u"); 157151937Sjkim if (!(pw = getpwnam(optarg))) 158151937Sjkim errx(ERROR_EXIT, "user `%s' unknown", optarg); 159151937Sjkim bzero(pw->pw_passwd, strlen(pw->pw_passwd)); 160151937Sjkim (void) strncpy(User, pw->pw_name, (sizeof User)-1); 161151937Sjkim User[(sizeof User)-1] = '\0'; 162151937Sjkim break; 163151937Sjkim case 'l': 164151937Sjkim if (Option != opt_unknown) 165151937Sjkim usage("only one operation permitted"); 166151937Sjkim Option = opt_list; 167151937Sjkim break; 168151937Sjkim case 'r': 169151937Sjkim if (Option != opt_unknown) 170151937Sjkim usage("only one operation permitted"); 171167802Sjkim Option = opt_delete; 172151937Sjkim break; 173151937Sjkim case 'e': 174151937Sjkim if (Option != opt_unknown) 175151937Sjkim usage("only one operation permitted"); 176151937Sjkim Option = opt_edit; 177151937Sjkim break; 178151937Sjkim default: 179151937Sjkim usage("unrecognized option"); 180151937Sjkim } 181151937Sjkim } 182151937Sjkim 183151937Sjkim endpwent(); 184151937Sjkim 185151937Sjkim if (Option != opt_unknown) { 186151937Sjkim if (argv[optind] != NULL) { 187151937Sjkim usage("no arguments permitted after this option"); 188151937Sjkim } 189151937Sjkim } else { 190151937Sjkim if (argv[optind] != NULL) { 191151937Sjkim Option = opt_replace; 192151937Sjkim (void) strncpy (Filename, argv[optind], (sizeof Filename)-1); 193151937Sjkim Filename[(sizeof Filename)-1] = '\0'; 194151937Sjkim 195151937Sjkim } else { 196151937Sjkim usage("file name must be specified for replace"); 197151937Sjkim } 198151937Sjkim } 199151937Sjkim 200151937Sjkim if (Option == opt_replace) { 201151937Sjkim /* we have to open the file here because we're going to 202151937Sjkim * chdir(2) into /var/cron before we get around to 203151937Sjkim * reading the file. 204151937Sjkim */ 205151937Sjkim if (!strcmp(Filename, "-")) { 206151937Sjkim NewCrontab = stdin; 207151937Sjkim } else if (realpath(Filename, resolved_path) != NULL && 208151937Sjkim !strcmp(resolved_path, SYSCRONTAB)) { 209151937Sjkim err(ERROR_EXIT, SYSCRONTAB " must be edited manually"); 210151937Sjkim } else { 211151937Sjkim /* relinquish the setuid status of the binary during 212151937Sjkim * the open, lest nonroot users read files they should 213151937Sjkim * not be able to read. we can't use access() here 214151937Sjkim * since there's a race condition. thanks go out to 215151937Sjkim * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting 216151937Sjkim * the race. 217151937Sjkim */ 218151937Sjkim 219151937Sjkim if (swap_uids() < OK) 220151937Sjkim err(ERROR_EXIT, "swapping uids"); 221151937Sjkim if (!(NewCrontab = fopen(Filename, "r"))) 222151937Sjkim err(ERROR_EXIT, "%s", Filename); 223151937Sjkim if (swap_uids() < OK) 224151937Sjkim err(ERROR_EXIT, "swapping uids back"); 225151937Sjkim } 226151937Sjkim } 227151937Sjkim 228151937Sjkim Debug(DMISC, ("user=%s, file=%s, option=%s\n", 229151937Sjkim User, Filename, Options[(int)Option])) 230151937Sjkim} 231151937Sjkim 232151937Sjkimstatic void 233151937Sjkimcopy_file(FILE *in, FILE *out) { 234151937Sjkim int x, ch; 235151937Sjkim 236151937Sjkim Set_LineNum(1) 237151937Sjkim /* ignore the top few comments since we probably put them there. 238151937Sjkim */ 239151937Sjkim for (x = 0; x < NHEADER_LINES; x++) { 240151937Sjkim ch = get_char(in); 241151937Sjkim if (EOF == ch) 242151937Sjkim break; 243151937Sjkim if ('#' != ch) { 244151937Sjkim putc(ch, out); 245151937Sjkim break; 246151937Sjkim } 247151937Sjkim while (EOF != (ch = get_char(in))) 248151937Sjkim if (ch == '\n') 249151937Sjkim break; 250151937Sjkim if (EOF == ch) 251151937Sjkim break; 252151937Sjkim } 253151937Sjkim 254151937Sjkim /* copy the rest of the crontab (if any) to the output file. 255151937Sjkim */ 256151937Sjkim if (EOF != ch) 257151937Sjkim while (EOF != (ch = get_char(in))) 258151937Sjkim putc(ch, out); 259151937Sjkim} 260151937Sjkim 261151937Sjkimstatic void 262151937Sjkimlist_cmd() { 263151937Sjkim char n[MAX_FNAME]; 264151937Sjkim FILE *f; 265151937Sjkim 266151937Sjkim log_it(RealUser, Pid, "LIST", User); 267151937Sjkim (void) snprintf(n, sizeof(n), CRON_TAB(User)); 268151937Sjkim if (!(f = fopen(n, "r"))) { 269151937Sjkim if (errno == ENOENT) 270151937Sjkim errx(ERROR_EXIT, "no crontab for %s", User); 271151937Sjkim else 272151937Sjkim err(ERROR_EXIT, "%s", n); 273151937Sjkim } 274151937Sjkim 275151937Sjkim /* file is open. copy to stdout, close. 276151937Sjkim */ 277151937Sjkim copy_file(f, stdout); 278151937Sjkim fclose(f); 279151937Sjkim} 280151937Sjkim 281151937Sjkim 282151937Sjkimstatic void 283151937Sjkimdelete_cmd() { 284151937Sjkim char n[MAX_FNAME]; 285151937Sjkim int ch, first; 286151937Sjkim 287151937Sjkim if (isatty(STDIN_FILENO)) { 288151937Sjkim (void)fprintf(stderr, "remove crontab for %s? ", User); 289151937Sjkim first = ch = getchar(); 290151937Sjkim while (ch != '\n' && ch != EOF) 291151937Sjkim ch = getchar(); 292151937Sjkim if (first != 'y' && first != 'Y') 293151937Sjkim return; 294151937Sjkim } 295151937Sjkim 296151937Sjkim log_it(RealUser, Pid, "DELETE", User); 297151937Sjkim (void) snprintf(n, sizeof(n), CRON_TAB(User)); 298151937Sjkim if (unlink(n)) { 299151937Sjkim if (errno == ENOENT) 300151937Sjkim errx(ERROR_EXIT, "no crontab for %s", User); 301151937Sjkim else 302151937Sjkim err(ERROR_EXIT, "%s", n); 303151937Sjkim } 304151937Sjkim poke_daemon(); 305151937Sjkim} 306151937Sjkim 307151937Sjkim 308151937Sjkimstatic void 309151937Sjkimcheck_error(msg) 310151937Sjkim char *msg; 311151937Sjkim{ 312151937Sjkim CheckErrorCount++; 313151937Sjkim fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg); 314151937Sjkim} 315151937Sjkim 316151937Sjkim 317151937Sjkimstatic void 318151937Sjkimedit_cmd() { 319151937Sjkim char n[MAX_FNAME], q[MAX_TEMPSTR], *editor; 320151937Sjkim FILE *f; 321151937Sjkim int t; 322151937Sjkim struct stat statbuf, fsbuf; 323151937Sjkim WAIT_T waiter; 324151937Sjkim PID_T pid, xpid; 325151937Sjkim mode_t um; 326151937Sjkim int syntax_error = 0; 327151937Sjkim char orig_md5[MD5_SIZE]; 328151937Sjkim char new_md5[MD5_SIZE]; 329151937Sjkim 330151937Sjkim log_it(RealUser, Pid, "BEGIN EDIT", User); 331151937Sjkim (void) snprintf(n, sizeof(n), CRON_TAB(User)); 332151937Sjkim if (!(f = fopen(n, "r"))) { 333151937Sjkim if (errno != ENOENT) 334151937Sjkim err(ERROR_EXIT, "%s", n); 335167802Sjkim warnx("no crontab for %s - using an empty one", User); 336151937Sjkim if (!(f = fopen(_PATH_DEVNULL, "r"))) 337151937Sjkim err(ERROR_EXIT, _PATH_DEVNULL); 338151937Sjkim } 339151937Sjkim 340151937Sjkim um = umask(077); 341151937Sjkim (void) snprintf(Filename, sizeof(Filename), "/tmp/crontab.XXXXXXXXXX"); 342151937Sjkim if ((t = mkstemp(Filename)) == -1) { 343151937Sjkim warn("%s", Filename); 344151937Sjkim (void) umask(um); 345151937Sjkim goto fatal; 346151937Sjkim } 347151937Sjkim (void) umask(um); 348151937Sjkim#ifdef HAS_FCHOWN 349151937Sjkim if (fchown(t, getuid(), getgid()) < 0) { 350151937Sjkim#else 351151937Sjkim if (chown(Filename, getuid(), getgid()) < 0) { 352151937Sjkim#endif 353151937Sjkim warn("fchown"); 354151937Sjkim goto fatal; 355151937Sjkim } 356151937Sjkim if (!(NewCrontab = fdopen(t, "r+"))) { 357151937Sjkim warn("fdopen"); 358151937Sjkim goto fatal; 359151937Sjkim } 360151937Sjkim 361151937Sjkim copy_file(f, NewCrontab); 362151937Sjkim fclose(f); 363151937Sjkim if (fflush(NewCrontab)) 364151937Sjkim err(ERROR_EXIT, "%s", Filename); 365151937Sjkim if (fstat(t, &fsbuf) < 0) { 366151937Sjkim warn("unable to fstat temp file"); 367151937Sjkim goto fatal; 368151937Sjkim } 369151937Sjkim again: 370151937Sjkim if (stat(Filename, &statbuf) < 0) { 371151937Sjkim warn("stat"); 372151937Sjkim fatal: unlink(Filename); 373151937Sjkim exit(ERROR_EXIT); 374151937Sjkim } 375151937Sjkim if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 376151937Sjkim errx(ERROR_EXIT, "temp file must be edited in place"); 377151937Sjkim if (MD5File(Filename, orig_md5) == NULL) { 378151937Sjkim warn("MD5"); 379151937Sjkim goto fatal; 380151937Sjkim } 381151937Sjkim 382151937Sjkim if ((!(editor = getenv("VISUAL"))) 383151937Sjkim && (!(editor = getenv("EDITOR"))) 384151937Sjkim ) { 385151937Sjkim editor = EDITOR; 386151937Sjkim } 387151937Sjkim 388151937Sjkim /* we still have the file open. editors will generally rewrite the 389151937Sjkim * original file rather than renaming/unlinking it and starting a 390151937Sjkim * new one; even backup files are supposed to be made by copying 391151937Sjkim * rather than by renaming. if some editor does not support this, 392151937Sjkim * then don't use it. the security problems are more severe if we 393151937Sjkim * close and reopen the file around the edit. 394151937Sjkim */ 395151937Sjkim 396151937Sjkim switch (pid = fork()) { 397151937Sjkim case -1: 398151937Sjkim warn("fork"); 399 goto fatal; 400 case 0: 401 /* child */ 402 if (setuid(getuid()) < 0) 403 err(ERROR_EXIT, "setuid(getuid())"); 404 if (chdir("/tmp") < 0) 405 err(ERROR_EXIT, "chdir(/tmp)"); 406 if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) 407 errx(ERROR_EXIT, "editor or filename too long"); 408 execlp(editor, editor, Filename, (char *)NULL); 409 err(ERROR_EXIT, "%s", editor); 410 /*NOTREACHED*/ 411 default: 412 /* parent */ 413 break; 414 } 415 416 /* parent */ 417 { 418 void (*f[4])(); 419 f[0] = signal(SIGHUP, SIG_IGN); 420 f[1] = signal(SIGINT, SIG_IGN); 421 f[2] = signal(SIGTERM, SIG_IGN); 422 xpid = wait(&waiter); 423 signal(SIGHUP, f[0]); 424 signal(SIGINT, f[1]); 425 signal(SIGTERM, f[2]); 426 } 427 if (xpid != pid) { 428 warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor); 429 goto fatal; 430 } 431 if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) { 432 warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter)); 433 goto fatal; 434 } 435 if (WIFSIGNALED(waiter)) { 436 warnx("\"%s\" killed; signal %d (%score dumped)", 437 editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no "); 438 goto fatal; 439 } 440 if (stat(Filename, &statbuf) < 0) { 441 warn("stat"); 442 goto fatal; 443 } 444 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 445 errx(ERROR_EXIT, "temp file must be edited in place"); 446 if (MD5File(Filename, new_md5) == NULL) { 447 warn("MD5"); 448 goto fatal; 449 } 450 if (strcmp(orig_md5, new_md5) == 0 && !syntax_error) { 451 warnx("no changes made to crontab"); 452 goto remove; 453 } 454 warnx("installing new crontab"); 455 switch (replace_cmd()) { 456 case 0: /* Success */ 457 break; 458 case -1: /* Syntax error */ 459 for (;;) { 460 printf("Do you want to retry the same edit? "); 461 fflush(stdout); 462 q[0] = '\0'; 463 (void) fgets(q, sizeof q, stdin); 464 switch (islower(q[0]) ? q[0] : tolower(q[0])) { 465 case 'y': 466 syntax_error = 1; 467 goto again; 468 case 'n': 469 goto abandon; 470 default: 471 fprintf(stderr, "Enter Y or N\n"); 472 } 473 } 474 /*NOTREACHED*/ 475 case -2: /* Install error */ 476 abandon: 477 warnx("edits left in %s", Filename); 478 goto done; 479 default: 480 warnx("panic: bad switch() in replace_cmd()"); 481 goto fatal; 482 } 483 remove: 484 unlink(Filename); 485 done: 486 log_it(RealUser, Pid, "END EDIT", User); 487} 488 489 490void 491static remove_tmp(int sig) 492{ 493 if (tmp_path) { 494 unlink(tmp_path); 495 } 496 exit(ERROR_EXIT); 497} 498 499 500/* returns 0 on success 501 * -1 on syntax error 502 * -2 on install error 503 */ 504static int 505replace_cmd() { 506 char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME]; 507 FILE *tmp; 508 int ch, eof; 509 entry *e; 510 time_t now = time(NULL); 511 char **envp = env_init(); 512 void (*f[3])(); 513 514 if (envp == NULL) { 515 warnx("cannot allocate memory"); 516 return (-2); 517 } 518 519 (void) snprintf(n, sizeof(n), "tmp.%d", Pid); 520 (void) snprintf(tn, sizeof(n), CRON_TAB(n)); 521 522 /* Set up to remove the temp file if interrupted by a signal. */ 523 f[0] = signal(SIGHUP, remove_tmp); 524 f[1] = signal(SIGINT, remove_tmp); 525 f[2] = signal(SIGTERM, remove_tmp); 526 tmp_path = tn; 527 528 if (!(tmp = fopen(tn, "w+"))) { 529 warn("%s", tn); 530 return (-2); 531 } 532 533 /* write a signature at the top of the file. 534 * 535 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code. 536 */ 537 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n"); 538 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now)); 539 fprintf(tmp, "# (Cron version -- %s)\n", rcsid); 540 541 /* copy the crontab to the tmp 542 */ 543 rewind(NewCrontab); 544 Set_LineNum(1) 545 while (EOF != (ch = get_char(NewCrontab))) 546 putc(ch, tmp); 547 ftruncate(fileno(tmp), ftell(tmp)); 548 fflush(tmp); rewind(tmp); 549 550 if (ferror(tmp)) { 551 warnx("error while writing new crontab to %s", tn); 552 fclose(tmp); unlink(tn); 553 return (-2); 554 } 555 556 /* check the syntax of the file being installed. 557 */ 558 559 /* BUG: was reporting errors after the EOF if there were any errors 560 * in the file proper -- kludged it by stopping after first error. 561 * vix 31mar87 562 */ 563 Set_LineNum(1 - NHEADER_LINES) 564 CheckErrorCount = 0; eof = FALSE; 565 while (!CheckErrorCount && !eof) { 566 switch (load_env(envstr, tmp)) { 567 case ERR: 568 eof = TRUE; 569 break; 570 case FALSE: 571 e = load_entry(tmp, check_error, pw, envp); 572 if (e) 573 free(e); 574 break; 575 case TRUE: 576 break; 577 } 578 } 579 580 if (CheckErrorCount != 0) { 581 warnx("errors in crontab file, can't install"); 582 fclose(tmp); unlink(tn); 583 return (-1); 584 } 585 586#ifdef HAS_FCHOWN 587 if (fchown(fileno(tmp), ROOT_UID, -1) < OK) 588#else 589 if (chown(tn, ROOT_UID, -1) < OK) 590#endif 591 { 592 warn("chown"); 593 fclose(tmp); unlink(tn); 594 return (-2); 595 } 596 597#ifdef HAS_FCHMOD 598 if (fchmod(fileno(tmp), 0600) < OK) 599#else 600 if (chmod(tn, 0600) < OK) 601#endif 602 { 603 warn("chown"); 604 fclose(tmp); unlink(tn); 605 return (-2); 606 } 607 608 if (fclose(tmp) == EOF) { 609 warn("fclose"); 610 unlink(tn); 611 return (-2); 612 } 613 614 (void) snprintf(n, sizeof(n), CRON_TAB(User)); 615 if (rename(tn, n)) { 616 warn("error renaming %s to %s", tn, n); 617 unlink(tn); 618 return (-2); 619 } 620 621 /* Restore the default signal handlers. */ 622 tmp_path = NULL; 623 signal(SIGHUP, f[0]); 624 signal(SIGINT, f[1]); 625 signal(SIGTERM, f[2]); 626 627 log_it(RealUser, Pid, "REPLACE", User); 628 629 poke_daemon(); 630 631 return (0); 632} 633 634 635static void 636poke_daemon() { 637#ifdef USE_UTIMES 638 struct timeval tvs[2]; 639 struct timezone tz; 640 641 (void) gettimeofday(&tvs[0], &tz); 642 tvs[1] = tvs[0]; 643 if (utimes(SPOOL_DIR, tvs) < OK) { 644 warn("can't update mtime on spooldir %s", SPOOL_DIR); 645 return; 646 } 647#else 648 if (utime(SPOOL_DIR, NULL) < OK) { 649 warn("can't update mtime on spooldir %s", SPOOL_DIR); 650 return; 651 } 652#endif /*USE_UTIMES*/ 653} 654