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 * From Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp 17 */ 18 19 #if !defined(lint) && !defined(LINT) 20 static const char rcsid[] = 21 "$FreeBSD$"; 22 #endif 23 24 /* crontab - install and manage per-user crontab files 25 * vix 02may87 [RCS has the rest of the log] 26 * vix 26jan87 [original] 27 */ 28 29 #define MAIN_PROGRAM 30 31 #include "cron.h" 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <md5.h> 35 #include <paths.h> 36 #include <sys/file.h> 37 #include <sys/stat.h> 38 #ifdef USE_UTIMES 39 # include <sys/time.h> 40 #else 41 # include <time.h> 42 # include <utime.h> 43 #endif 44 #if defined(POSIX) 45 # include <locale.h> 46 #endif 47 48 #define MD5_SIZE 33 49 #define NHEADER_LINES 3 50 51 52 enum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; 53 54 #if DEBUGGING 55 static char *Options[] = { "???", "list", "delete", "edit", "replace" }; 56 #endif 57 58 59 static PID_T Pid; 60 static char User[MAX_UNAME], RealUser[MAX_UNAME]; 61 static char Filename[MAX_FNAME]; 62 static FILE *NewCrontab; 63 static int CheckErrorCount; 64 static enum opt_t Option; 65 static struct passwd *pw; 66 static void list_cmd(void), 67 delete_cmd(void), 68 edit_cmd(void), 69 poke_daemon(void), 70 check_error(char *), 71 parse_args(int c, char *v[]); 72 static int replace_cmd(void); 73 74 75 static void 76 usage(char *msg) 77 { 78 fprintf(stderr, "crontab: usage error: %s\n", msg); 79 fprintf(stderr, "%s\n%s\n", 80 "usage: crontab [-u user] file", 81 " crontab [-u user] { -e | -l | -r }"); 82 exit(ERROR_EXIT); 83 } 84 85 86 int 87 main(int argc, char *argv[]) 88 { 89 int exitstatus; 90 91 Pid = getpid(); 92 ProgramName = argv[0]; 93 94 #if defined(POSIX) 95 setlocale(LC_ALL, ""); 96 #endif 97 98 #if defined(BSD) 99 setlinebuf(stderr); 100 #endif 101 parse_args(argc, argv); /* sets many globals, opens a file */ 102 set_cron_uid(); 103 set_cron_cwd(); 104 if (!allowed(User)) { 105 warnx("you (%s) are not allowed to use this program", User); 106 log_it(RealUser, Pid, "AUTH", "crontab command not allowed"); 107 exit(ERROR_EXIT); 108 } 109 exitstatus = OK_EXIT; 110 switch (Option) { 111 case opt_list: list_cmd(); 112 break; 113 case opt_delete: delete_cmd(); 114 break; 115 case opt_edit: edit_cmd(); 116 break; 117 case opt_replace: if (replace_cmd() < 0) 118 exitstatus = ERROR_EXIT; 119 break; 120 case opt_unknown: 121 break; 122 } 123 exit(exitstatus); 124 /*NOTREACHED*/ 125 } 126 127 128 static void 129 parse_args(argc, argv) 130 int argc; 131 char *argv[]; 132 { 133 int argch; 134 char resolved_path[PATH_MAX]; 135 136 if (!(pw = getpwuid(getuid()))) 137 errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out"); 138 bzero(pw->pw_passwd, strlen(pw->pw_passwd)); 139 (void) strncpy(User, pw->pw_name, (sizeof User)-1); 140 User[(sizeof User)-1] = '\0'; 141 strcpy(RealUser, User); 142 Filename[0] = '\0'; 143 Option = opt_unknown; 144 while ((argch = getopt(argc, argv, "u:lerx:")) != -1) { 145 switch (argch) { 146 case 'x': 147 if (!set_debug_flags(optarg)) 148 usage("bad debug option"); 149 break; 150 case 'u': 151 if (getuid() != ROOT_UID) 152 errx(ERROR_EXIT, "must be privileged to use -u"); 153 if (!(pw = getpwnam(optarg))) 154 errx(ERROR_EXIT, "user `%s' unknown", optarg); 155 bzero(pw->pw_passwd, strlen(pw->pw_passwd)); 156 (void) strncpy(User, pw->pw_name, (sizeof User)-1); 157 User[(sizeof User)-1] = '\0'; 158 break; 159 case 'l': 160 if (Option != opt_unknown) 161 usage("only one operation permitted"); 162 Option = opt_list; 163 break; 164 case 'r': 165 if (Option != opt_unknown) 166 usage("only one operation permitted"); 167 Option = opt_delete; 168 break; 169 case 'e': 170 if (Option != opt_unknown) 171 usage("only one operation permitted"); 172 Option = opt_edit; 173 break; 174 default: 175 usage("unrecognized option"); 176 } 177 } 178 179 endpwent(); 180 181 if (Option != opt_unknown) { 182 if (argv[optind] != NULL) { 183 usage("no arguments permitted after this option"); 184 } 185 } else { 186 if (argv[optind] != NULL) { 187 Option = opt_replace; 188 (void) strncpy (Filename, argv[optind], (sizeof Filename)-1); 189 Filename[(sizeof Filename)-1] = '\0'; 190 191 } else { 192 usage("file name must be specified for replace"); 193 } 194 } 195 196 if (Option == opt_replace) { 197 /* we have to open the file here because we're going to 198 * chdir(2) into /var/cron before we get around to 199 * reading the file. 200 */ 201 if (!strcmp(Filename, "-")) { 202 NewCrontab = stdin; 203 } else if (realpath(Filename, resolved_path) != NULL && 204 !strcmp(resolved_path, SYSCRONTAB)) { 205 err(ERROR_EXIT, SYSCRONTAB " must be edited manually"); 206 } else { 207 /* relinquish the setuid status of the binary during 208 * the open, lest nonroot users read files they should 209 * not be able to read. we can't use access() here 210 * since there's a race condition. thanks go out to 211 * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting 212 * the race. 213 */ 214 215 if (swap_uids() < OK) 216 err(ERROR_EXIT, "swapping uids"); 217 if (!(NewCrontab = fopen(Filename, "r"))) 218 err(ERROR_EXIT, "%s", Filename); 219 if (swap_uids_back() < OK) 220 err(ERROR_EXIT, "swapping uids back"); 221 } 222 } 223 224 Debug(DMISC, ("user=%s, file=%s, option=%s\n", 225 User, Filename, Options[(int)Option])) 226 } 227 228 static void 229 copy_file(FILE *in, FILE *out) { 230 int x, ch; 231 232 Set_LineNum(1) 233 /* ignore the top few comments since we probably put them there. 234 */ 235 for (x = 0; x < NHEADER_LINES; x++) { 236 ch = get_char(in); 237 if (EOF == ch) 238 break; 239 if ('#' != ch) { 240 putc(ch, out); 241 break; 242 } 243 while (EOF != (ch = get_char(in))) 244 if (ch == '\n') 245 break; 246 if (EOF == ch) 247 break; 248 } 249 250 /* copy the rest of the crontab (if any) to the output file. 251 */ 252 if (EOF != ch) 253 while (EOF != (ch = get_char(in))) 254 putc(ch, out); 255 } 256 257 static void 258 list_cmd() { 259 char n[MAX_FNAME]; 260 FILE *f; 261 262 log_it(RealUser, Pid, "LIST", User); 263 (void) snprintf(n, sizeof(n), CRON_TAB(User)); 264 if (!(f = fopen(n, "r"))) { 265 if (errno == ENOENT) 266 errx(ERROR_EXIT, "no crontab for %s", User); 267 else 268 err(ERROR_EXIT, "%s", n); 269 } 270 271 /* file is open. copy to stdout, close. 272 */ 273 copy_file(f, stdout); 274 fclose(f); 275 } 276 277 278 static void 279 delete_cmd() { 280 char n[MAX_FNAME]; 281 int ch, first; 282 283 if (isatty(STDIN_FILENO)) { 284 (void)fprintf(stderr, "remove crontab for %s? ", User); 285 first = ch = getchar(); 286 while (ch != '\n' && ch != EOF) 287 ch = getchar(); 288 if (first != 'y' && first != 'Y') 289 return; 290 } 291 292 log_it(RealUser, Pid, "DELETE", User); 293 (void) snprintf(n, sizeof(n), CRON_TAB(User)); 294 if (unlink(n)) { 295 if (errno == ENOENT) 296 errx(ERROR_EXIT, "no crontab for %s", User); 297 else 298 err(ERROR_EXIT, "%s", n); 299 } 300 poke_daemon(); 301 } 302 303 304 static void 305 check_error(msg) 306 char *msg; 307 { 308 CheckErrorCount++; 309 fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg); 310 } 311 312 313 static void 314 edit_cmd() { 315 char n[MAX_FNAME], q[MAX_TEMPSTR], *editor; 316 FILE *f; 317 int t; 318 struct stat statbuf, fsbuf; 319 WAIT_T waiter; 320 PID_T pid, xpid; 321 mode_t um; 322 int syntax_error = 0; 323 char orig_md5[MD5_SIZE]; 324 char new_md5[MD5_SIZE]; 325 326 log_it(RealUser, Pid, "BEGIN EDIT", User); 327 (void) snprintf(n, sizeof(n), CRON_TAB(User)); 328 if (!(f = fopen(n, "r"))) { 329 if (errno != ENOENT) 330 err(ERROR_EXIT, "%s", n); 331 warnx("no crontab for %s - using an empty one", User); 332 if (!(f = fopen(_PATH_DEVNULL, "r"))) 333 err(ERROR_EXIT, _PATH_DEVNULL); 334 } 335 336 um = umask(077); 337 (void) snprintf(Filename, sizeof(Filename), "/tmp/crontab.XXXXXXXXXX"); 338 if ((t = mkstemp(Filename)) == -1) { 339 warn("%s", Filename); 340 (void) umask(um); 341 goto fatal; 342 } 343 (void) umask(um); 344 #ifdef HAS_FCHOWN 345 if (fchown(t, getuid(), getgid()) < 0) { 346 #else 347 if (chown(Filename, getuid(), getgid()) < 0) { 348 #endif 349 warn("fchown"); 350 goto fatal; 351 } 352 if (!(NewCrontab = fdopen(t, "r+"))) { 353 warn("fdopen"); 354 goto fatal; 355 } 356 357 copy_file(f, NewCrontab); 358 fclose(f); 359 if (fflush(NewCrontab)) 360 err(ERROR_EXIT, "%s", Filename); 361 if (fstat(t, &fsbuf) < 0) { 362 warn("unable to fstat temp file"); 363 goto fatal; 364 } 365 again: 366 if (stat(Filename, &statbuf) < 0) { 367 warn("stat"); 368 fatal: unlink(Filename); 369 exit(ERROR_EXIT); 370 } 371 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 372 errx(ERROR_EXIT, "temp file must be edited in place"); 373 if (MD5File(Filename, orig_md5) == NULL) { 374 warn("MD5"); 375 goto fatal; 376 } 377 378 if ((!(editor = getenv("VISUAL"))) 379 && (!(editor = getenv("EDITOR"))) 380 ) { 381 editor = EDITOR; 382 } 383 384 /* we still have the file open. editors will generally rewrite the 385 * original file rather than renaming/unlinking it and starting a 386 * new one; even backup files are supposed to be made by copying 387 * rather than by renaming. if some editor does not support this, 388 * then don't use it. the security problems are more severe if we 389 * close and reopen the file around the edit. 390 */ 391 392 switch (pid = fork()) { 393 case -1: 394 warn("fork"); 395 goto fatal; 396 case 0: 397 /* child */ 398 if (setuid(getuid()) < 0) 399 err(ERROR_EXIT, "setuid(getuid())"); 400 if (chdir("/tmp") < 0) 401 err(ERROR_EXIT, "chdir(/tmp)"); 402 if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) 403 errx(ERROR_EXIT, "editor or filename too long"); 404 execlp(editor, editor, Filename, (char *)NULL); 405 err(ERROR_EXIT, "%s", editor); 406 /*NOTREACHED*/ 407 default: 408 /* parent */ 409 break; 410 } 411 412 /* parent */ 413 { 414 void (*sig[3])(int signal); 415 sig[0] = signal(SIGHUP, SIG_IGN); 416 sig[1] = signal(SIGINT, SIG_IGN); 417 sig[2] = signal(SIGTERM, SIG_IGN); 418 xpid = wait(&waiter); 419 signal(SIGHUP, sig[0]); 420 signal(SIGINT, sig[1]); 421 signal(SIGTERM, sig[2]); 422 } 423 if (xpid != pid) { 424 warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor); 425 goto fatal; 426 } 427 if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) { 428 warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter)); 429 goto fatal; 430 } 431 if (WIFSIGNALED(waiter)) { 432 warnx("\"%s\" killed; signal %d (%score dumped)", 433 editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no "); 434 goto fatal; 435 } 436 if (stat(Filename, &statbuf) < 0) { 437 warn("stat"); 438 goto fatal; 439 } 440 if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 441 errx(ERROR_EXIT, "temp file must be edited in place"); 442 if (MD5File(Filename, new_md5) == NULL) { 443 warn("MD5"); 444 goto fatal; 445 } 446 if (strcmp(orig_md5, new_md5) == 0 && !syntax_error) { 447 warnx("no changes made to crontab"); 448 goto remove; 449 } 450 warnx("installing new crontab"); 451 switch (replace_cmd()) { 452 case 0: /* Success */ 453 break; 454 case -1: /* Syntax error */ 455 for (;;) { 456 printf("Do you want to retry the same edit? "); 457 fflush(stdout); 458 q[0] = '\0'; 459 (void) fgets(q, sizeof q, stdin); 460 switch (islower(q[0]) ? q[0] : tolower(q[0])) { 461 case 'y': 462 syntax_error = 1; 463 goto again; 464 case 'n': 465 goto abandon; 466 default: 467 fprintf(stderr, "Enter Y or N\n"); 468 } 469 } 470 /*NOTREACHED*/ 471 case -2: /* Install error */ 472 abandon: 473 warnx("edits left in %s", Filename); 474 goto done; 475 default: 476 warnx("panic: bad switch() in replace_cmd()"); 477 goto fatal; 478 } 479 remove: 480 unlink(Filename); 481 done: 482 log_it(RealUser, Pid, "END EDIT", User); 483 } 484 485 486 /* returns 0 on success 487 * -1 on syntax error 488 * -2 on install error 489 */ 490 static int 491 replace_cmd() { 492 char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME]; 493 FILE *tmp; 494 int ch, eof; 495 entry *e; 496 time_t now = time(NULL); 497 char **envp = env_init(); 498 499 if (envp == NULL) { 500 warnx("cannot allocate memory"); 501 return (-2); 502 } 503 504 (void) snprintf(n, sizeof(n), "tmp.%d", Pid); 505 (void) snprintf(tn, sizeof(tn), CRON_TAB(n)); 506 507 if (!(tmp = fopen(tn, "w+"))) { 508 warn("%s", tn); 509 return (-2); 510 } 511 512 /* write a signature at the top of the file. 513 * 514 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code. 515 */ 516 fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n"); 517 fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now)); 518 fprintf(tmp, "# (Cron version -- %s)\n", rcsid); 519 520 /* copy the crontab to the tmp 521 */ 522 rewind(NewCrontab); 523 Set_LineNum(1) 524 while (EOF != (ch = get_char(NewCrontab))) 525 putc(ch, tmp); 526 ftruncate(fileno(tmp), ftell(tmp)); 527 fflush(tmp); rewind(tmp); 528 529 if (ferror(tmp)) { 530 warnx("error while writing new crontab to %s", tn); 531 fclose(tmp); unlink(tn); 532 return (-2); 533 } 534 535 /* check the syntax of the file being installed. 536 */ 537 538 /* BUG: was reporting errors after the EOF if there were any errors 539 * in the file proper -- kludged it by stopping after first error. 540 * vix 31mar87 541 */ 542 Set_LineNum(1 - NHEADER_LINES) 543 CheckErrorCount = 0; eof = FALSE; 544 while (!CheckErrorCount && !eof) { 545 switch (load_env(envstr, tmp)) { 546 case ERR: 547 eof = TRUE; 548 break; 549 case FALSE: 550 e = load_entry(tmp, check_error, pw, envp); 551 if (e) 552 free(e); 553 break; 554 case TRUE: 555 break; 556 } 557 } 558 559 if (CheckErrorCount != 0) { 560 warnx("errors in crontab file, can't install"); 561 fclose(tmp); unlink(tn); 562 return (-1); 563 } 564 565 #ifdef HAS_FCHOWN 566 if (fchown(fileno(tmp), ROOT_UID, -1) < OK) 567 #else 568 if (chown(tn, ROOT_UID, -1) < OK) 569 #endif 570 { 571 warn("chown"); 572 fclose(tmp); unlink(tn); 573 return (-2); 574 } 575 576 #ifdef HAS_FCHMOD 577 if (fchmod(fileno(tmp), 0600) < OK) 578 #else 579 if (chmod(tn, 0600) < OK) 580 #endif 581 { 582 warn("chown"); 583 fclose(tmp); unlink(tn); 584 return (-2); 585 } 586 587 if (fclose(tmp) == EOF) { 588 warn("fclose"); 589 unlink(tn); 590 return (-2); 591 } 592 593 (void) snprintf(n, sizeof(n), CRON_TAB(User)); 594 if (rename(tn, n)) { 595 warn("error renaming %s to %s", tn, n); 596 unlink(tn); 597 return (-2); 598 } 599 600 log_it(RealUser, Pid, "REPLACE", User); 601 602 poke_daemon(); 603 604 return (0); 605 } 606 607 608 static void 609 poke_daemon() { 610 #ifdef USE_UTIMES 611 struct timeval tvs[2]; 612 struct timezone tz; 613 614 (void) gettimeofday(&tvs[0], &tz); 615 tvs[1] = tvs[0]; 616 if (utimes(SPOOL_DIR, tvs) < OK) { 617 warn("can't update mtime on spooldir %s", SPOOL_DIR); 618 return; 619 } 620 #else 621 if (utime(SPOOL_DIR, NULL) < OK) { 622 warn("can't update mtime on spooldir %s", SPOOL_DIR); 623 return; 624 } 625 #endif /*USE_UTIMES*/ 626 } 627