12311Sjkh/* Copyright 1988,1990,1993,1994 by Paul Vixie 22311Sjkh * All rights reserved 32311Sjkh * 42311Sjkh * Distribute freely, except: don't remove my name from the source or 52311Sjkh * documentation (don't take credit for my work), mark your changes (don't 62311Sjkh * get me blamed for your possible bugs), don't alter or remove this 72311Sjkh * notice. May be sold if buildable source is provided to buyer. No 82311Sjkh * warrantee of any kind, express or implied, is included with this 92311Sjkh * software; use at your own risk, responsibility for damages (if any) to 102311Sjkh * anyone resulting from the use of this software rests entirely with the 112311Sjkh * user. 122311Sjkh * 132311Sjkh * Send bug reports, bug fixes, enhancements, requests, flames, etc., and 142311Sjkh * I'll try to keep a version up to date. I can be reached as follows: 152311Sjkh * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul 165176Sache * From Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp 172311Sjkh */ 182311Sjkh 192311Sjkh#if !defined(lint) && !defined(LINT) 2029452Scharnierstatic const char rcsid[] = 2150479Speter "$FreeBSD: releng/11.0/usr.sbin/cron/crontab/crontab.c 305427 2016-09-05 16:43:57Z emaste $"; 222311Sjkh#endif 232311Sjkh 242311Sjkh/* crontab - install and manage per-user crontab files 252311Sjkh * vix 02may87 [RCS has the rest of the log] 262311Sjkh * vix 26jan87 [original] 272311Sjkh */ 282311Sjkh 292311Sjkh#define MAIN_PROGRAM 302311Sjkh 31305427Semaste#include <sys/param.h> 322311Sjkh#include "cron.h" 332311Sjkh#include <errno.h> 342311Sjkh#include <fcntl.h> 35135242Sdds#include <md5.h> 3669793Sobrien#include <paths.h> 372311Sjkh#include <sys/file.h> 382311Sjkh#include <sys/stat.h> 392311Sjkh#ifdef USE_UTIMES 402311Sjkh# include <sys/time.h> 412311Sjkh#else 422311Sjkh# include <time.h> 432311Sjkh# include <utime.h> 442311Sjkh#endif 452311Sjkh#if defined(POSIX) 462311Sjkh# include <locale.h> 472311Sjkh#endif 482311Sjkh 49135242Sdds#define MD5_SIZE 33 502311Sjkh#define NHEADER_LINES 3 512311Sjkh 522311Sjkh 532311Sjkhenum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; 542311Sjkh 552311Sjkh#if DEBUGGING 562311Sjkhstatic char *Options[] = { "???", "list", "delete", "edit", "replace" }; 572311Sjkh#endif 582311Sjkh 592311Sjkh 602311Sjkhstatic PID_T Pid; 61305427Semastestatic char User[MAXLOGNAME], RealUser[MAXLOGNAME]; 622311Sjkhstatic char Filename[MAX_FNAME]; 632311Sjkhstatic FILE *NewCrontab; 642311Sjkhstatic int CheckErrorCount; 652311Sjkhstatic enum opt_t Option; 662311Sjkhstatic struct passwd *pw; 67173412Skevlostatic void list_cmd(void), 68173412Skevlo delete_cmd(void), 69173412Skevlo edit_cmd(void), 70173412Skevlo poke_daemon(void), 71173412Skevlo check_error(char *), 72173412Skevlo parse_args(int c, char *v[]); 73173412Skevlostatic int replace_cmd(void); 742311Sjkh 752311Sjkh 762311Sjkhstatic void 77184809Smatteousage(char *msg) 782311Sjkh{ 7929452Scharnier fprintf(stderr, "crontab: usage error: %s\n", msg); 8029452Scharnier fprintf(stderr, "%s\n%s\n", 8129452Scharnier "usage: crontab [-u user] file", 8229452Scharnier " crontab [-u user] { -e | -l | -r }"); 832311Sjkh exit(ERROR_EXIT); 842311Sjkh} 852311Sjkh 862311Sjkh 872311Sjkhint 88184809Smatteomain(int argc, char *argv[]) 892311Sjkh{ 902311Sjkh int exitstatus; 912311Sjkh 922311Sjkh Pid = getpid(); 932311Sjkh ProgramName = argv[0]; 942311Sjkh 952311Sjkh#if defined(POSIX) 962311Sjkh setlocale(LC_ALL, ""); 972311Sjkh#endif 982311Sjkh 992311Sjkh#if defined(BSD) 1002311Sjkh setlinebuf(stderr); 1012311Sjkh#endif 1022311Sjkh parse_args(argc, argv); /* sets many globals, opens a file */ 1032311Sjkh set_cron_uid(); 1042311Sjkh set_cron_cwd(); 1052311Sjkh if (!allowed(User)) { 10629452Scharnier warnx("you (%s) are not allowed to use this program", User); 1072311Sjkh log_it(RealUser, Pid, "AUTH", "crontab command not allowed"); 1082311Sjkh exit(ERROR_EXIT); 1092311Sjkh } 1102311Sjkh exitstatus = OK_EXIT; 1112311Sjkh switch (Option) { 1122311Sjkh case opt_list: list_cmd(); 1132311Sjkh break; 1142311Sjkh case opt_delete: delete_cmd(); 1152311Sjkh break; 1162311Sjkh case opt_edit: edit_cmd(); 1172311Sjkh break; 1182311Sjkh case opt_replace: if (replace_cmd() < 0) 1192311Sjkh exitstatus = ERROR_EXIT; 1202311Sjkh break; 12129452Scharnier case opt_unknown: 12229452Scharnier break; 1232311Sjkh } 124104326Sdd exit(exitstatus); 1252311Sjkh /*NOTREACHED*/ 1262311Sjkh} 1272311Sjkh 1288857Srgrimes 1292311Sjkhstatic void 1302311Sjkhparse_args(argc, argv) 1312311Sjkh int argc; 1322311Sjkh char *argv[]; 1332311Sjkh{ 1342311Sjkh int argch; 135154166Sbrooks char resolved_path[PATH_MAX]; 1362311Sjkh 13729452Scharnier if (!(pw = getpwuid(getuid()))) 13829452Scharnier errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out"); 139184706Smatteo bzero(pw->pw_passwd, strlen(pw->pw_passwd)); 14020573Spst (void) strncpy(User, pw->pw_name, (sizeof User)-1); 14120573Spst User[(sizeof User)-1] = '\0'; 1422311Sjkh strcpy(RealUser, User); 1432311Sjkh Filename[0] = '\0'; 1442311Sjkh Option = opt_unknown; 14524428Simp while ((argch = getopt(argc, argv, "u:lerx:")) != -1) { 1462311Sjkh switch (argch) { 1472311Sjkh case 'x': 1482311Sjkh if (!set_debug_flags(optarg)) 1492311Sjkh usage("bad debug option"); 1502311Sjkh break; 1512311Sjkh case 'u': 1522311Sjkh if (getuid() != ROOT_UID) 15329452Scharnier errx(ERROR_EXIT, "must be privileged to use -u"); 1542311Sjkh if (!(pw = getpwnam(optarg))) 15529452Scharnier errx(ERROR_EXIT, "user `%s' unknown", optarg); 156184706Smatteo bzero(pw->pw_passwd, strlen(pw->pw_passwd)); 15720573Spst (void) strncpy(User, pw->pw_name, (sizeof User)-1); 15820573Spst User[(sizeof User)-1] = '\0'; 1592311Sjkh break; 1602311Sjkh case 'l': 1612311Sjkh if (Option != opt_unknown) 1622311Sjkh usage("only one operation permitted"); 1632311Sjkh Option = opt_list; 1642311Sjkh break; 1652311Sjkh case 'r': 1662311Sjkh if (Option != opt_unknown) 1672311Sjkh usage("only one operation permitted"); 1682311Sjkh Option = opt_delete; 1692311Sjkh break; 1702311Sjkh case 'e': 1712311Sjkh if (Option != opt_unknown) 1722311Sjkh usage("only one operation permitted"); 1732311Sjkh Option = opt_edit; 1742311Sjkh break; 1752311Sjkh default: 1762311Sjkh usage("unrecognized option"); 1772311Sjkh } 1782311Sjkh } 1792311Sjkh 1802311Sjkh endpwent(); 1812311Sjkh 1822311Sjkh if (Option != opt_unknown) { 1832311Sjkh if (argv[optind] != NULL) { 1842311Sjkh usage("no arguments permitted after this option"); 1852311Sjkh } 1862311Sjkh } else { 1872311Sjkh if (argv[optind] != NULL) { 1882311Sjkh Option = opt_replace; 18920573Spst (void) strncpy (Filename, argv[optind], (sizeof Filename)-1); 19020573Spst Filename[(sizeof Filename)-1] = '\0'; 19120573Spst 1922311Sjkh } else { 1932311Sjkh usage("file name must be specified for replace"); 1942311Sjkh } 1952311Sjkh } 1962311Sjkh 1972311Sjkh if (Option == opt_replace) { 198232202Sdelphij /* relinquish the setuid status of the binary during 199232202Sdelphij * the open, lest nonroot users read files they should 200232202Sdelphij * not be able to read. we can't use access() here 201232202Sdelphij * since there's a race condition. thanks go out to 202232202Sdelphij * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting 203232202Sdelphij * the race. 204232202Sdelphij */ 205232202Sdelphij 206232202Sdelphij if (swap_uids() < OK) 207232202Sdelphij err(ERROR_EXIT, "swapping uids"); 208232202Sdelphij 2092311Sjkh /* we have to open the file here because we're going to 2102311Sjkh * chdir(2) into /var/cron before we get around to 2112311Sjkh * reading the file. 2122311Sjkh */ 2132311Sjkh if (!strcmp(Filename, "-")) { 2142311Sjkh NewCrontab = stdin; 215154166Sbrooks } else if (realpath(Filename, resolved_path) != NULL && 216161964Sru !strcmp(resolved_path, SYSCRONTAB)) { 217161964Sru err(ERROR_EXIT, SYSCRONTAB " must be edited manually"); 2182311Sjkh } else { 21929452Scharnier if (!(NewCrontab = fopen(Filename, "r"))) 22029452Scharnier err(ERROR_EXIT, "%s", Filename); 2212311Sjkh } 222232202Sdelphij if (swap_uids_back() < OK) 223232202Sdelphij err(ERROR_EXIT, "swapping uids back"); 2242311Sjkh } 2252311Sjkh 2262311Sjkh Debug(DMISC, ("user=%s, file=%s, option=%s\n", 2272311Sjkh User, Filename, Options[(int)Option])) 2282311Sjkh} 2292311Sjkh 230135174Sddsstatic void 231135174Sddscopy_file(FILE *in, FILE *out) { 232135174Sdds int x, ch; 2332311Sjkh 234135174Sdds Set_LineNum(1) 235135174Sdds /* ignore the top few comments since we probably put them there. 236135174Sdds */ 237135174Sdds for (x = 0; x < NHEADER_LINES; x++) { 238135174Sdds ch = get_char(in); 239135174Sdds if (EOF == ch) 240135174Sdds break; 241135174Sdds if ('#' != ch) { 242135174Sdds putc(ch, out); 243135174Sdds break; 244135174Sdds } 245135174Sdds while (EOF != (ch = get_char(in))) 246135174Sdds if (ch == '\n') 247135174Sdds break; 248135174Sdds if (EOF == ch) 249135174Sdds break; 250135174Sdds } 251135174Sdds 252135174Sdds /* copy the rest of the crontab (if any) to the output file. 253135174Sdds */ 254135174Sdds if (EOF != ch) 255135174Sdds while (EOF != (ch = get_char(in))) 256135174Sdds putc(ch, out); 257135174Sdds} 258135174Sdds 2592311Sjkhstatic void 2602311Sjkhlist_cmd() { 2612311Sjkh char n[MAX_FNAME]; 2622311Sjkh FILE *f; 2632311Sjkh 2642311Sjkh log_it(RealUser, Pid, "LIST", User); 265184779Smatteo (void) snprintf(n, sizeof(n), CRON_TAB(User)); 2662311Sjkh if (!(f = fopen(n, "r"))) { 2672311Sjkh if (errno == ENOENT) 26829452Scharnier errx(ERROR_EXIT, "no crontab for %s", User); 2692311Sjkh else 27029452Scharnier err(ERROR_EXIT, "%s", n); 2712311Sjkh } 2722311Sjkh 2732311Sjkh /* file is open. copy to stdout, close. 2742311Sjkh */ 275135174Sdds copy_file(f, stdout); 2762311Sjkh fclose(f); 2772311Sjkh} 2782311Sjkh 2792311Sjkh 2802311Sjkhstatic void 2812311Sjkhdelete_cmd() { 2822311Sjkh char n[MAX_FNAME]; 28367127Spaul int ch, first; 2842311Sjkh 28567127Spaul if (isatty(STDIN_FILENO)) { 28667127Spaul (void)fprintf(stderr, "remove crontab for %s? ", User); 28767127Spaul first = ch = getchar(); 28867127Spaul while (ch != '\n' && ch != EOF) 28967127Spaul ch = getchar(); 29067127Spaul if (first != 'y' && first != 'Y') 29167127Spaul return; 29267127Spaul } 29367127Spaul 2942311Sjkh log_it(RealUser, Pid, "DELETE", User); 295184779Smatteo (void) snprintf(n, sizeof(n), CRON_TAB(User)); 2962311Sjkh if (unlink(n)) { 2972311Sjkh if (errno == ENOENT) 29829452Scharnier errx(ERROR_EXIT, "no crontab for %s", User); 2992311Sjkh else 30029452Scharnier err(ERROR_EXIT, "%s", n); 3012311Sjkh } 3022311Sjkh poke_daemon(); 3032311Sjkh} 3042311Sjkh 3052311Sjkh 3062311Sjkhstatic void 3072311Sjkhcheck_error(msg) 3082311Sjkh char *msg; 3092311Sjkh{ 3102311Sjkh CheckErrorCount++; 3112311Sjkh fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg); 3122311Sjkh} 3132311Sjkh 3142311Sjkh 3152311Sjkhstatic void 3162311Sjkhedit_cmd() { 3172311Sjkh char n[MAX_FNAME], q[MAX_TEMPSTR], *editor; 3182311Sjkh FILE *f; 319135174Sdds int t; 32068388Sdwmalone struct stat statbuf, fsbuf; 3212311Sjkh WAIT_T waiter; 3222311Sjkh PID_T pid, xpid; 32320573Spst mode_t um; 324135165Sdds int syntax_error = 0; 325135242Sdds char orig_md5[MD5_SIZE]; 326135242Sdds char new_md5[MD5_SIZE]; 3272311Sjkh 3282311Sjkh log_it(RealUser, Pid, "BEGIN EDIT", User); 329184779Smatteo (void) snprintf(n, sizeof(n), CRON_TAB(User)); 3302311Sjkh if (!(f = fopen(n, "r"))) { 33129452Scharnier if (errno != ENOENT) 33229452Scharnier err(ERROR_EXIT, "%s", n); 33329452Scharnier warnx("no crontab for %s - using an empty one", User); 33469793Sobrien if (!(f = fopen(_PATH_DEVNULL, "r"))) 33569793Sobrien err(ERROR_EXIT, _PATH_DEVNULL); 3362311Sjkh } 3372311Sjkh 33820573Spst um = umask(077); 339184779Smatteo (void) snprintf(Filename, sizeof(Filename), "/tmp/crontab.XXXXXXXXXX"); 34020573Spst if ((t = mkstemp(Filename)) == -1) { 34129452Scharnier warn("%s", Filename); 34220573Spst (void) umask(um); 3432311Sjkh goto fatal; 3442311Sjkh } 34520573Spst (void) umask(um); 3462311Sjkh#ifdef HAS_FCHOWN 3472311Sjkh if (fchown(t, getuid(), getgid()) < 0) { 3482311Sjkh#else 3492311Sjkh if (chown(Filename, getuid(), getgid()) < 0) { 3502311Sjkh#endif 35129452Scharnier warn("fchown"); 3522311Sjkh goto fatal; 3532311Sjkh } 35468388Sdwmalone if (!(NewCrontab = fdopen(t, "r+"))) { 35529452Scharnier warn("fdopen"); 3562311Sjkh goto fatal; 3572311Sjkh } 3582311Sjkh 359135174Sdds copy_file(f, NewCrontab); 3602311Sjkh fclose(f); 36168388Sdwmalone if (fflush(NewCrontab)) 36229452Scharnier err(ERROR_EXIT, "%s", Filename); 36368388Sdwmalone if (fstat(t, &fsbuf) < 0) { 36468388Sdwmalone warn("unable to fstat temp file"); 36568388Sdwmalone goto fatal; 36668388Sdwmalone } 3672311Sjkh again: 368232202Sdelphij if (swap_uids() < OK) 369232202Sdelphij err(ERROR_EXIT, "swapping uids"); 3705176Sache if (stat(Filename, &statbuf) < 0) { 37129452Scharnier warn("stat"); 3722311Sjkh fatal: unlink(Filename); 3732311Sjkh exit(ERROR_EXIT); 3742311Sjkh } 375232202Sdelphij if (swap_uids_back() < OK) 376232202Sdelphij err(ERROR_EXIT, "swapping uids back"); 37768388Sdwmalone if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 37868388Sdwmalone errx(ERROR_EXIT, "temp file must be edited in place"); 379135242Sdds if (MD5File(Filename, orig_md5) == NULL) { 380135242Sdds warn("MD5"); 381135242Sdds goto fatal; 382135242Sdds } 3832311Sjkh 3842311Sjkh if ((!(editor = getenv("VISUAL"))) 3852311Sjkh && (!(editor = getenv("EDITOR"))) 3862311Sjkh ) { 3872311Sjkh editor = EDITOR; 3882311Sjkh } 3892311Sjkh 3902311Sjkh /* we still have the file open. editors will generally rewrite the 3912311Sjkh * original file rather than renaming/unlinking it and starting a 3922311Sjkh * new one; even backup files are supposed to be made by copying 3932311Sjkh * rather than by renaming. if some editor does not support this, 3942311Sjkh * then don't use it. the security problems are more severe if we 3952311Sjkh * close and reopen the file around the edit. 3962311Sjkh */ 3972311Sjkh 3982311Sjkh switch (pid = fork()) { 3992311Sjkh case -1: 40029452Scharnier warn("fork"); 4012311Sjkh goto fatal; 4022311Sjkh case 0: 4032311Sjkh /* child */ 40429452Scharnier if (setuid(getuid()) < 0) 40529452Scharnier err(ERROR_EXIT, "setuid(getuid())"); 40629452Scharnier if (chdir("/tmp") < 0) 40729452Scharnier err(ERROR_EXIT, "chdir(/tmp)"); 40829452Scharnier if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) 40929452Scharnier errx(ERROR_EXIT, "editor or filename too long"); 41079452Sbrian execlp(editor, editor, Filename, (char *)NULL); 41129452Scharnier err(ERROR_EXIT, "%s", editor); 4122311Sjkh /*NOTREACHED*/ 4132311Sjkh default: 4142311Sjkh /* parent */ 4152311Sjkh break; 4162311Sjkh } 4172311Sjkh 4182311Sjkh /* parent */ 41915161Sscrappy { 420184809Smatteo void (*sig[3])(int signal); 421184809Smatteo sig[0] = signal(SIGHUP, SIG_IGN); 422184809Smatteo sig[1] = signal(SIGINT, SIG_IGN); 423184809Smatteo sig[2] = signal(SIGTERM, SIG_IGN); 4242311Sjkh xpid = wait(&waiter); 425184809Smatteo signal(SIGHUP, sig[0]); 426184809Smatteo signal(SIGINT, sig[1]); 427184809Smatteo signal(SIGTERM, sig[2]); 42815161Sscrappy } 4292311Sjkh if (xpid != pid) { 43029452Scharnier warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor); 4312311Sjkh goto fatal; 4322311Sjkh } 4332311Sjkh if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) { 43429452Scharnier warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter)); 4352311Sjkh goto fatal; 4362311Sjkh } 4372311Sjkh if (WIFSIGNALED(waiter)) { 43829452Scharnier warnx("\"%s\" killed; signal %d (%score dumped)", 43929452Scharnier editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no "); 4402311Sjkh goto fatal; 4412311Sjkh } 442232202Sdelphij if (swap_uids() < OK) 443232202Sdelphij err(ERROR_EXIT, "swapping uids"); 4445176Sache if (stat(Filename, &statbuf) < 0) { 44529452Scharnier warn("stat"); 4462311Sjkh goto fatal; 4472311Sjkh } 44868388Sdwmalone if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 44968388Sdwmalone errx(ERROR_EXIT, "temp file must be edited in place"); 450135242Sdds if (MD5File(Filename, new_md5) == NULL) { 451135242Sdds warn("MD5"); 452135242Sdds goto fatal; 453135242Sdds } 454232202Sdelphij if (swap_uids_back() < OK) 455232202Sdelphij err(ERROR_EXIT, "swapping uids back"); 456135242Sdds if (strcmp(orig_md5, new_md5) == 0 && !syntax_error) { 45729452Scharnier warnx("no changes made to crontab"); 4582311Sjkh goto remove; 4592311Sjkh } 46029452Scharnier warnx("installing new crontab"); 4612311Sjkh switch (replace_cmd()) { 462135165Sdds case 0: /* Success */ 4632311Sjkh break; 464135165Sdds case -1: /* Syntax error */ 4652311Sjkh for (;;) { 4662311Sjkh printf("Do you want to retry the same edit? "); 4672311Sjkh fflush(stdout); 4682311Sjkh q[0] = '\0'; 4692311Sjkh (void) fgets(q, sizeof q, stdin); 4702311Sjkh switch (islower(q[0]) ? q[0] : tolower(q[0])) { 4712311Sjkh case 'y': 472135165Sdds syntax_error = 1; 4732311Sjkh goto again; 4742311Sjkh case 'n': 4752311Sjkh goto abandon; 4762311Sjkh default: 4772311Sjkh fprintf(stderr, "Enter Y or N\n"); 4782311Sjkh } 4792311Sjkh } 4802311Sjkh /*NOTREACHED*/ 481135165Sdds case -2: /* Install error */ 4822311Sjkh abandon: 48329452Scharnier warnx("edits left in %s", Filename); 4842311Sjkh goto done; 4852311Sjkh default: 48629452Scharnier warnx("panic: bad switch() in replace_cmd()"); 4872311Sjkh goto fatal; 4882311Sjkh } 4892311Sjkh remove: 4902311Sjkh unlink(Filename); 4912311Sjkh done: 4922311Sjkh log_it(RealUser, Pid, "END EDIT", User); 4932311Sjkh} 4942311Sjkh 4958857Srgrimes 4962311Sjkh/* returns 0 on success 4972311Sjkh * -1 on syntax error 4982311Sjkh * -2 on install error 4992311Sjkh */ 5002311Sjkhstatic int 5012311Sjkhreplace_cmd() { 5022311Sjkh char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME]; 5032311Sjkh FILE *tmp; 5042311Sjkh int ch, eof; 5052311Sjkh entry *e; 5062311Sjkh time_t now = time(NULL); 5072311Sjkh char **envp = env_init(); 5082311Sjkh 50920573Spst if (envp == NULL) { 51029452Scharnier warnx("cannot allocate memory"); 51120573Spst return (-2); 51220573Spst } 51320573Spst 514184779Smatteo (void) snprintf(n, sizeof(n), "tmp.%d", Pid); 515185041Smatteo (void) snprintf(tn, sizeof(tn), CRON_TAB(n)); 516184780Smatteo 5172311Sjkh if (!(tmp = fopen(tn, "w+"))) { 51829452Scharnier warn("%s", tn); 5192311Sjkh return (-2); 5202311Sjkh } 5212311Sjkh 5222311Sjkh /* write a signature at the top of the file. 5232311Sjkh * 5242311Sjkh * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code. 5252311Sjkh */ 5262311Sjkh fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n"); 5272311Sjkh fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now)); 5282311Sjkh fprintf(tmp, "# (Cron version -- %s)\n", rcsid); 5292311Sjkh 5302311Sjkh /* copy the crontab to the tmp 5312311Sjkh */ 53268388Sdwmalone rewind(NewCrontab); 5332311Sjkh Set_LineNum(1) 5342311Sjkh while (EOF != (ch = get_char(NewCrontab))) 5352311Sjkh putc(ch, tmp); 536295672Spfg ftruncate(fileno(tmp), ftello(tmp)); 5372311Sjkh fflush(tmp); rewind(tmp); 5382311Sjkh 5392311Sjkh if (ferror(tmp)) { 54029452Scharnier warnx("error while writing new crontab to %s", tn); 5412311Sjkh fclose(tmp); unlink(tn); 5422311Sjkh return (-2); 5432311Sjkh } 5442311Sjkh 5452311Sjkh /* check the syntax of the file being installed. 5462311Sjkh */ 5472311Sjkh 5482311Sjkh /* BUG: was reporting errors after the EOF if there were any errors 5492311Sjkh * in the file proper -- kludged it by stopping after first error. 5502311Sjkh * vix 31mar87 5512311Sjkh */ 5522311Sjkh Set_LineNum(1 - NHEADER_LINES) 5532311Sjkh CheckErrorCount = 0; eof = FALSE; 5542311Sjkh while (!CheckErrorCount && !eof) { 5552311Sjkh switch (load_env(envstr, tmp)) { 5562311Sjkh case ERR: 5572311Sjkh eof = TRUE; 5582311Sjkh break; 5592311Sjkh case FALSE: 5602311Sjkh e = load_entry(tmp, check_error, pw, envp); 5612311Sjkh if (e) 562292606Spfg free_entry(e); 5632311Sjkh break; 5642311Sjkh case TRUE: 5652311Sjkh break; 5662311Sjkh } 5672311Sjkh } 5682311Sjkh 5692311Sjkh if (CheckErrorCount != 0) { 57029452Scharnier warnx("errors in crontab file, can't install"); 5712311Sjkh fclose(tmp); unlink(tn); 5722311Sjkh return (-1); 5732311Sjkh } 5742311Sjkh 5752311Sjkh#ifdef HAS_FCHOWN 5762311Sjkh if (fchown(fileno(tmp), ROOT_UID, -1) < OK) 5772311Sjkh#else 5782311Sjkh if (chown(tn, ROOT_UID, -1) < OK) 5792311Sjkh#endif 5802311Sjkh { 58129452Scharnier warn("chown"); 5822311Sjkh fclose(tmp); unlink(tn); 5832311Sjkh return (-2); 5842311Sjkh } 5852311Sjkh 5862311Sjkh#ifdef HAS_FCHMOD 5872311Sjkh if (fchmod(fileno(tmp), 0600) < OK) 5882311Sjkh#else 5892311Sjkh if (chmod(tn, 0600) < OK) 5902311Sjkh#endif 5912311Sjkh { 59229452Scharnier warn("chown"); 5932311Sjkh fclose(tmp); unlink(tn); 5942311Sjkh return (-2); 5952311Sjkh } 5962311Sjkh 5972311Sjkh if (fclose(tmp) == EOF) { 59829452Scharnier warn("fclose"); 5992311Sjkh unlink(tn); 6002311Sjkh return (-2); 6012311Sjkh } 6022311Sjkh 603184779Smatteo (void) snprintf(n, sizeof(n), CRON_TAB(User)); 6042311Sjkh if (rename(tn, n)) { 60529452Scharnier warn("error renaming %s to %s", tn, n); 6062311Sjkh unlink(tn); 6072311Sjkh return (-2); 6082311Sjkh } 609184780Smatteo 6102311Sjkh log_it(RealUser, Pid, "REPLACE", User); 6112311Sjkh 612238024Sjhb /* 613238024Sjhb * Creating the 'tn' temp file has already updated the 614238024Sjhb * modification time of the spool directory. Sleep for a 615238024Sjhb * second to ensure that poke_daemon() sets a later 616238024Sjhb * modification time. Otherwise, this can race with the cron 617238024Sjhb * daemon scanning for updated crontabs. 618238024Sjhb */ 619238024Sjhb sleep(1); 620238024Sjhb 6212311Sjkh poke_daemon(); 6222311Sjkh 6232311Sjkh return (0); 6242311Sjkh} 6252311Sjkh 6262311Sjkh 6272311Sjkhstatic void 6282311Sjkhpoke_daemon() { 6292311Sjkh#ifdef USE_UTIMES 6302311Sjkh struct timeval tvs[2]; 6312311Sjkh 632239991Sed (void)gettimeofday(&tvs[0], NULL); 6332311Sjkh tvs[1] = tvs[0]; 6342311Sjkh if (utimes(SPOOL_DIR, tvs) < OK) { 63529452Scharnier warn("can't update mtime on spooldir %s", SPOOL_DIR); 6362311Sjkh return; 6372311Sjkh } 6382311Sjkh#else 6392311Sjkh if (utime(SPOOL_DIR, NULL) < OK) { 64029452Scharnier warn("can't update mtime on spooldir %s", SPOOL_DIR); 6412311Sjkh return; 6422311Sjkh } 6432311Sjkh#endif /*USE_UTIMES*/ 6442311Sjkh} 645