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