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 char rcsid[] = "$Id: crontab.c,v 1.4 1994/04/13 21:57:55 wollman 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 29 #define MAIN_PROGRAM 30 31 32 #include "cron.h" 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <sys/file.h> 36 #include <sys/stat.h> 37 #ifdef USE_UTIMES 38 # include <sys/time.h> 39 #else 40 # include <time.h> 41 # include <utime.h> 42 #endif 43 #if defined(POSIX) 44 # include <locale.h> 45 #endif 46 47 48 #define NHEADER_LINES 3 49 50 51 enum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; 52 53 #if DEBUGGING 54 static char *Options[] = { "???", "list", "delete", "edit", "replace" }; 55 #endif 56 57 58 static PID_T Pid; 59 static char User[MAX_UNAME], RealUser[MAX_UNAME]; 60 static char Filename[MAX_FNAME]; 61 static FILE *NewCrontab; 62 static int CheckErrorCount; 63 static enum opt_t Option; 64 static struct passwd *pw; 65 static void list_cmd __P((void)), 66 delete_cmd __P((void)), 67 edit_cmd __P((void)), 68 poke_daemon __P((void)), 69 check_error __P((char *)), 70 parse_args __P((int c, char *v[])); 71 static int replace_cmd __P((void)); 72 73 74 static void 75 usage(msg) 76 char *msg; 77 { 78 fprintf(stderr, "%s: usage error: %s\n", ProgramName, msg); 79 fprintf(stderr, "usage:\t%s [-u user] file\n", ProgramName); 80 fprintf(stderr, "\t%s [-u user] { -e | -l | -r }\n", ProgramName); 81 fprintf(stderr, "\t\t(default operation is replace, per 1003.2)\n"); 82 fprintf(stderr, "\t-e\t(edit user's crontab)\n"); 83 fprintf(stderr, "\t-l\t(list user's crontab)\n"); 84 fprintf(stderr, "\t-r\t(delete user's crontab)\n"); 85 exit(ERROR_EXIT); 86 } 87 88 89 int 90 main(argc, argv) 91 int argc; 92 char *argv[]; 93 { 94 int exitstatus; 95 96 Pid = getpid(); 97 ProgramName = argv[0]; 98 99 #if defined(POSIX) 100 setlocale(LC_ALL, ""); 101 #endif 102 103 #if defined(BSD) 104 setlinebuf(stderr); 105 #endif 106 parse_args(argc, argv); /* sets many globals, opens a file */ 107 set_cron_uid(); 108 set_cron_cwd(); 109 if (!allowed(User)) { 110 fprintf(stderr, 111 "You (%s) are not allowed to use this program (%s)\n", 112 User, ProgramName); 113 fprintf(stderr, "See crontab(1) for more information\n"); 114 log_it(RealUser, Pid, "AUTH", "crontab command not allowed"); 115 exit(ERROR_EXIT); 116 } 117 exitstatus = OK_EXIT; 118 switch (Option) { 119 case opt_list: list_cmd(); 120 break; 121 case opt_delete: delete_cmd(); 122 break; 123 case opt_edit: edit_cmd(); 124 break; 125 case opt_replace: if (replace_cmd() < 0) 126 exitstatus = ERROR_EXIT; 127 break; 128 } 129 exit(0); 130 /*NOTREACHED*/ 131 } 132 133 134 static void 135 parse_args(argc, argv) 136 int argc; 137 char *argv[]; 138 { 139 int argch; 140 141 if (!(pw = getpwuid(getuid()))) { 142 fprintf(stderr, "%s: your UID isn't in the passwd file.\n", 143 ProgramName); 144 fprintf(stderr, "bailing out.\n"); 145 exit(ERROR_EXIT); 146 } 147 strcpy(User, pw->pw_name); 148 strcpy(RealUser, User); 149 Filename[0] = '\0'; 150 Option = opt_unknown; 151 while (EOF != (argch = getopt(argc, argv, "u:lerx:"))) { 152 switch (argch) { 153 case 'x': 154 if (!set_debug_flags(optarg)) 155 usage("bad debug option"); 156 break; 157 case 'u': 158 if (getuid() != ROOT_UID) 159 { 160 fprintf(stderr, 161 "must be privileged to use -u\n"); 162 exit(ERROR_EXIT); 163 } 164 if (!(pw = getpwnam(optarg))) 165 { 166 fprintf(stderr, "%s: user `%s' unknown\n", 167 ProgramName, optarg); 168 exit(ERROR_EXIT); 169 } 170 (void) strcpy(User, optarg); 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) strcpy (Filename, argv[optind]); 202 } else { 203 usage("file name must be specified for replace"); 204 } 205 } 206 207 if (Option == opt_replace) { 208 /* we have to open the file here because we're going to 209 * chdir(2) into /var/cron before we get around to 210 * reading the file. 211 */ 212 if (!strcmp(Filename, "-")) { 213 NewCrontab = stdin; 214 } else { 215 /* relinquish the setuid status of the binary during 216 * the open, lest nonroot users read files they should 217 * not be able to read. we can't use access() here 218 * since there's a race condition. thanks go out to 219 * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting 220 * the race. 221 */ 222 223 if (swap_uids() < OK) { 224 perror("swapping uids"); 225 exit(ERROR_EXIT); 226 } 227 if (!(NewCrontab = fopen(Filename, "r"))) { 228 perror(Filename); 229 exit(ERROR_EXIT); 230 } 231 if (swap_uids() < OK) { 232 perror("swapping uids back"); 233 exit(ERROR_EXIT); 234 } 235 } 236 } 237 238 Debug(DMISC, ("user=%s, file=%s, option=%s\n", 239 User, Filename, Options[(int)Option])) 240 } 241 242 243 static void 244 list_cmd() { 245 char n[MAX_FNAME]; 246 FILE *f; 247 int ch; 248 249 log_it(RealUser, Pid, "LIST", User); 250 (void) sprintf(n, CRON_TAB(User)); 251 if (!(f = fopen(n, "r"))) { 252 if (errno == ENOENT) 253 fprintf(stderr, "no crontab for %s\n", User); 254 else 255 perror(n); 256 exit(ERROR_EXIT); 257 } 258 259 /* file is open. copy to stdout, close. 260 */ 261 Set_LineNum(1) 262 while (EOF != (ch = get_char(f))) 263 putchar(ch); 264 fclose(f); 265 } 266 267 268 static void 269 delete_cmd() { 270 char n[MAX_FNAME]; 271 272 log_it(RealUser, Pid, "DELETE", User); 273 (void) sprintf(n, CRON_TAB(User)); 274 if (unlink(n)) { 275 if (errno == ENOENT) 276 fprintf(stderr, "no crontab for %s\n", User); 277 else 278 perror(n); 279 exit(ERROR_EXIT); 280 } 281 poke_daemon(); 282 } 283 284 285 static void 286 check_error(msg) 287 char *msg; 288 { 289 CheckErrorCount++; 290 fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg); 291 } 292 293 294 static void 295 edit_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 */ 489 static int 490 replace_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 603 static void 604 poke_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