crontab.c revision 104326
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: head/usr.sbin/cron/crontab/crontab.c 104326 2002-10-01 22:59:11Z dd $"; 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 312311Sjkh#include "cron.h" 322311Sjkh#include <errno.h> 332311Sjkh#include <fcntl.h> 3469793Sobrien#include <paths.h> 352311Sjkh#include <sys/file.h> 362311Sjkh#include <sys/stat.h> 372311Sjkh#ifdef USE_UTIMES 382311Sjkh# include <sys/time.h> 392311Sjkh#else 402311Sjkh# include <time.h> 412311Sjkh# include <utime.h> 422311Sjkh#endif 432311Sjkh#if defined(POSIX) 442311Sjkh# include <locale.h> 452311Sjkh#endif 462311Sjkh 472311Sjkh 482311Sjkh#define NHEADER_LINES 3 492311Sjkh 502311Sjkh 512311Sjkhenum opt_t { opt_unknown, opt_list, opt_delete, opt_edit, opt_replace }; 522311Sjkh 532311Sjkh#if DEBUGGING 542311Sjkhstatic char *Options[] = { "???", "list", "delete", "edit", "replace" }; 552311Sjkh#endif 562311Sjkh 572311Sjkh 582311Sjkhstatic PID_T Pid; 592311Sjkhstatic char User[MAX_UNAME], RealUser[MAX_UNAME]; 602311Sjkhstatic char Filename[MAX_FNAME]; 612311Sjkhstatic FILE *NewCrontab; 622311Sjkhstatic int CheckErrorCount; 632311Sjkhstatic enum opt_t Option; 642311Sjkhstatic struct passwd *pw; 652311Sjkhstatic void list_cmd __P((void)), 662311Sjkh delete_cmd __P((void)), 672311Sjkh edit_cmd __P((void)), 682311Sjkh poke_daemon __P((void)), 692311Sjkh check_error __P((char *)), 702311Sjkh parse_args __P((int c, char *v[])); 712311Sjkhstatic int replace_cmd __P((void)); 722311Sjkh 732311Sjkh 742311Sjkhstatic void 752311Sjkhusage(msg) 762311Sjkh char *msg; 772311Sjkh{ 7829452Scharnier fprintf(stderr, "crontab: usage error: %s\n", msg); 7929452Scharnier fprintf(stderr, "%s\n%s\n", 8029452Scharnier "usage: crontab [-u user] file", 8129452Scharnier " crontab [-u user] { -e | -l | -r }"); 822311Sjkh exit(ERROR_EXIT); 832311Sjkh} 842311Sjkh 852311Sjkh 862311Sjkhint 872311Sjkhmain(argc, argv) 882311Sjkh int argc; 892311Sjkh char *argv[]; 902311Sjkh{ 912311Sjkh int exitstatus; 922311Sjkh 932311Sjkh Pid = getpid(); 942311Sjkh ProgramName = argv[0]; 952311Sjkh 962311Sjkh#if defined(POSIX) 972311Sjkh setlocale(LC_ALL, ""); 982311Sjkh#endif 992311Sjkh 1002311Sjkh#if defined(BSD) 1012311Sjkh setlinebuf(stderr); 1022311Sjkh#endif 1032311Sjkh parse_args(argc, argv); /* sets many globals, opens a file */ 1042311Sjkh set_cron_uid(); 1052311Sjkh set_cron_cwd(); 1062311Sjkh if (!allowed(User)) { 10729452Scharnier warnx("you (%s) are not allowed to use this program", User); 1082311Sjkh log_it(RealUser, Pid, "AUTH", "crontab command not allowed"); 1092311Sjkh exit(ERROR_EXIT); 1102311Sjkh } 1112311Sjkh exitstatus = OK_EXIT; 1122311Sjkh switch (Option) { 1132311Sjkh case opt_list: list_cmd(); 1142311Sjkh break; 1152311Sjkh case opt_delete: delete_cmd(); 1162311Sjkh break; 1172311Sjkh case opt_edit: edit_cmd(); 1182311Sjkh break; 1192311Sjkh case opt_replace: if (replace_cmd() < 0) 1202311Sjkh exitstatus = ERROR_EXIT; 1212311Sjkh break; 12229452Scharnier case opt_unknown: 12329452Scharnier break; 1242311Sjkh } 125104326Sdd exit(exitstatus); 1262311Sjkh /*NOTREACHED*/ 1272311Sjkh} 1282311Sjkh 1298857Srgrimes 1302311Sjkhstatic void 1312311Sjkhparse_args(argc, argv) 1322311Sjkh int argc; 1332311Sjkh char *argv[]; 1342311Sjkh{ 1352311Sjkh int argch; 1362311Sjkh 13729452Scharnier if (!(pw = getpwuid(getuid()))) 13829452Scharnier errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out"); 13920573Spst (void) strncpy(User, pw->pw_name, (sizeof User)-1); 14020573Spst User[(sizeof User)-1] = '\0'; 1412311Sjkh strcpy(RealUser, User); 1422311Sjkh Filename[0] = '\0'; 1432311Sjkh Option = opt_unknown; 14424428Simp while ((argch = getopt(argc, argv, "u:lerx:")) != -1) { 1452311Sjkh switch (argch) { 1462311Sjkh case 'x': 1472311Sjkh if (!set_debug_flags(optarg)) 1482311Sjkh usage("bad debug option"); 1492311Sjkh break; 1502311Sjkh case 'u': 1512311Sjkh if (getuid() != ROOT_UID) 15229452Scharnier errx(ERROR_EXIT, "must be privileged to use -u"); 1532311Sjkh if (!(pw = getpwnam(optarg))) 15429452Scharnier errx(ERROR_EXIT, "user `%s' unknown", optarg); 15520573Spst (void) strncpy(User, pw->pw_name, (sizeof User)-1); 15620573Spst User[(sizeof User)-1] = '\0'; 1572311Sjkh break; 1582311Sjkh case 'l': 1592311Sjkh if (Option != opt_unknown) 1602311Sjkh usage("only one operation permitted"); 1612311Sjkh Option = opt_list; 1622311Sjkh break; 1632311Sjkh case 'r': 1642311Sjkh if (Option != opt_unknown) 1652311Sjkh usage("only one operation permitted"); 1662311Sjkh Option = opt_delete; 1672311Sjkh break; 1682311Sjkh case 'e': 1692311Sjkh if (Option != opt_unknown) 1702311Sjkh usage("only one operation permitted"); 1712311Sjkh Option = opt_edit; 1722311Sjkh break; 1732311Sjkh default: 1742311Sjkh usage("unrecognized option"); 1752311Sjkh } 1762311Sjkh } 1772311Sjkh 1782311Sjkh endpwent(); 1792311Sjkh 1802311Sjkh if (Option != opt_unknown) { 1812311Sjkh if (argv[optind] != NULL) { 1822311Sjkh usage("no arguments permitted after this option"); 1832311Sjkh } 1842311Sjkh } else { 1852311Sjkh if (argv[optind] != NULL) { 1862311Sjkh Option = opt_replace; 18720573Spst (void) strncpy (Filename, argv[optind], (sizeof Filename)-1); 18820573Spst Filename[(sizeof Filename)-1] = '\0'; 18920573Spst 1902311Sjkh } else { 1912311Sjkh usage("file name must be specified for replace"); 1922311Sjkh } 1932311Sjkh } 1942311Sjkh 1952311Sjkh if (Option == opt_replace) { 1962311Sjkh /* we have to open the file here because we're going to 1972311Sjkh * chdir(2) into /var/cron before we get around to 1982311Sjkh * reading the file. 1992311Sjkh */ 2002311Sjkh if (!strcmp(Filename, "-")) { 2012311Sjkh NewCrontab = stdin; 2022311Sjkh } else { 2032311Sjkh /* relinquish the setuid status of the binary during 2042311Sjkh * the open, lest nonroot users read files they should 2052311Sjkh * not be able to read. we can't use access() here 2062311Sjkh * since there's a race condition. thanks go out to 2072311Sjkh * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting 2082311Sjkh * the race. 2092311Sjkh */ 2102311Sjkh 21129452Scharnier if (swap_uids() < OK) 21229452Scharnier err(ERROR_EXIT, "swapping uids"); 21329452Scharnier if (!(NewCrontab = fopen(Filename, "r"))) 21429452Scharnier err(ERROR_EXIT, "%s", Filename); 21529452Scharnier if (swap_uids() < OK) 21629452Scharnier err(ERROR_EXIT, "swapping uids back"); 2172311Sjkh } 2182311Sjkh } 2192311Sjkh 2202311Sjkh Debug(DMISC, ("user=%s, file=%s, option=%s\n", 2212311Sjkh User, Filename, Options[(int)Option])) 2222311Sjkh} 2232311Sjkh 2242311Sjkh 2252311Sjkhstatic void 2262311Sjkhlist_cmd() { 2272311Sjkh char n[MAX_FNAME]; 2282311Sjkh FILE *f; 22975456Speter int ch, x; 2302311Sjkh 2312311Sjkh log_it(RealUser, Pid, "LIST", User); 2322311Sjkh (void) sprintf(n, CRON_TAB(User)); 2332311Sjkh if (!(f = fopen(n, "r"))) { 2342311Sjkh if (errno == ENOENT) 23529452Scharnier errx(ERROR_EXIT, "no crontab for %s", User); 2362311Sjkh else 23729452Scharnier err(ERROR_EXIT, "%s", n); 2382311Sjkh } 2392311Sjkh 2402311Sjkh /* file is open. copy to stdout, close. 2412311Sjkh */ 2422311Sjkh Set_LineNum(1) 24375456Speter 24475456Speter /* ignore the top few comments since we probably put them there. 24575456Speter */ 24675456Speter for (x = 0; x < NHEADER_LINES; x++) { 24775456Speter ch = get_char(f); 24875456Speter if (EOF == ch) 24975456Speter break; 25075456Speter if ('#' != ch) { 25178321Speter putchar(ch); 25275456Speter break; 25375456Speter } 25475456Speter while (EOF != (ch = get_char(f))) 25575456Speter if (ch == '\n') 25675456Speter break; 25775456Speter if (EOF == ch) 25875456Speter break; 25975456Speter } 26075456Speter 2612311Sjkh while (EOF != (ch = get_char(f))) 2622311Sjkh putchar(ch); 2632311Sjkh fclose(f); 2642311Sjkh} 2652311Sjkh 2662311Sjkh 2672311Sjkhstatic void 2682311Sjkhdelete_cmd() { 2692311Sjkh char n[MAX_FNAME]; 27067127Spaul int ch, first; 2712311Sjkh 27267127Spaul if (isatty(STDIN_FILENO)) { 27367127Spaul (void)fprintf(stderr, "remove crontab for %s? ", User); 27467127Spaul first = ch = getchar(); 27567127Spaul while (ch != '\n' && ch != EOF) 27667127Spaul ch = getchar(); 27767127Spaul if (first != 'y' && first != 'Y') 27867127Spaul return; 27967127Spaul } 28067127Spaul 2812311Sjkh log_it(RealUser, Pid, "DELETE", User); 2822311Sjkh (void) sprintf(n, CRON_TAB(User)); 2832311Sjkh if (unlink(n)) { 2842311Sjkh if (errno == ENOENT) 28529452Scharnier errx(ERROR_EXIT, "no crontab for %s", User); 2862311Sjkh else 28729452Scharnier err(ERROR_EXIT, "%s", n); 2882311Sjkh } 2892311Sjkh poke_daemon(); 2902311Sjkh} 2912311Sjkh 2922311Sjkh 2932311Sjkhstatic void 2942311Sjkhcheck_error(msg) 2952311Sjkh char *msg; 2962311Sjkh{ 2972311Sjkh CheckErrorCount++; 2982311Sjkh fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg); 2992311Sjkh} 3002311Sjkh 3012311Sjkh 3022311Sjkhstatic void 3032311Sjkhedit_cmd() { 3042311Sjkh char n[MAX_FNAME], q[MAX_TEMPSTR], *editor; 3052311Sjkh FILE *f; 3062311Sjkh int ch, t, x; 30768388Sdwmalone struct stat statbuf, fsbuf; 3082311Sjkh time_t mtime; 3092311Sjkh WAIT_T waiter; 3102311Sjkh PID_T pid, xpid; 31120573Spst mode_t um; 3122311Sjkh 3132311Sjkh log_it(RealUser, Pid, "BEGIN EDIT", User); 3142311Sjkh (void) sprintf(n, CRON_TAB(User)); 3152311Sjkh if (!(f = fopen(n, "r"))) { 31629452Scharnier if (errno != ENOENT) 31729452Scharnier err(ERROR_EXIT, "%s", n); 31829452Scharnier warnx("no crontab for %s - using an empty one", User); 31969793Sobrien if (!(f = fopen(_PATH_DEVNULL, "r"))) 32069793Sobrien err(ERROR_EXIT, _PATH_DEVNULL); 3212311Sjkh } 3222311Sjkh 32320573Spst um = umask(077); 32420573Spst (void) sprintf(Filename, "/tmp/crontab.XXXXXXXXXX"); 32520573Spst if ((t = mkstemp(Filename)) == -1) { 32629452Scharnier warn("%s", Filename); 32720573Spst (void) umask(um); 3282311Sjkh goto fatal; 3292311Sjkh } 33020573Spst (void) umask(um); 3312311Sjkh#ifdef HAS_FCHOWN 3322311Sjkh if (fchown(t, getuid(), getgid()) < 0) { 3332311Sjkh#else 3342311Sjkh if (chown(Filename, getuid(), getgid()) < 0) { 3352311Sjkh#endif 33629452Scharnier warn("fchown"); 3372311Sjkh goto fatal; 3382311Sjkh } 33968388Sdwmalone if (!(NewCrontab = fdopen(t, "r+"))) { 34029452Scharnier warn("fdopen"); 3412311Sjkh goto fatal; 3422311Sjkh } 3432311Sjkh 3442311Sjkh Set_LineNum(1) 3452311Sjkh 3462311Sjkh /* ignore the top few comments since we probably put them there. 3472311Sjkh */ 3482311Sjkh for (x = 0; x < NHEADER_LINES; x++) { 3492311Sjkh ch = get_char(f); 3502311Sjkh if (EOF == ch) 3512311Sjkh break; 3522311Sjkh if ('#' != ch) { 3532311Sjkh putc(ch, NewCrontab); 3542311Sjkh break; 3552311Sjkh } 3562311Sjkh while (EOF != (ch = get_char(f))) 3572311Sjkh if (ch == '\n') 3582311Sjkh break; 3592311Sjkh if (EOF == ch) 3602311Sjkh break; 3612311Sjkh } 3622311Sjkh 3632311Sjkh /* copy the rest of the crontab (if any) to the temp file. 3642311Sjkh */ 3652311Sjkh if (EOF != ch) 3662311Sjkh while (EOF != (ch = get_char(f))) 3672311Sjkh putc(ch, NewCrontab); 3682311Sjkh fclose(f); 36968388Sdwmalone if (fflush(NewCrontab)) 37029452Scharnier err(ERROR_EXIT, "%s", Filename); 37168388Sdwmalone if (fstat(t, &fsbuf) < 0) { 37268388Sdwmalone warn("unable to fstat temp file"); 37368388Sdwmalone goto fatal; 37468388Sdwmalone } 3752311Sjkh again: 3765176Sache if (stat(Filename, &statbuf) < 0) { 37729452Scharnier warn("stat"); 3782311Sjkh fatal: unlink(Filename); 3792311Sjkh exit(ERROR_EXIT); 3802311Sjkh } 38168388Sdwmalone if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 38268388Sdwmalone errx(ERROR_EXIT, "temp file must be edited in place"); 3832311Sjkh mtime = statbuf.st_mtime; 3842311Sjkh 3852311Sjkh if ((!(editor = getenv("VISUAL"))) 3862311Sjkh && (!(editor = getenv("EDITOR"))) 3872311Sjkh ) { 3882311Sjkh editor = EDITOR; 3892311Sjkh } 3902311Sjkh 3912311Sjkh /* we still have the file open. editors will generally rewrite the 3922311Sjkh * original file rather than renaming/unlinking it and starting a 3932311Sjkh * new one; even backup files are supposed to be made by copying 3942311Sjkh * rather than by renaming. if some editor does not support this, 3952311Sjkh * then don't use it. the security problems are more severe if we 3962311Sjkh * close and reopen the file around the edit. 3972311Sjkh */ 3982311Sjkh 3992311Sjkh switch (pid = fork()) { 4002311Sjkh case -1: 40129452Scharnier warn("fork"); 4022311Sjkh goto fatal; 4032311Sjkh case 0: 4042311Sjkh /* child */ 40529452Scharnier if (setuid(getuid()) < 0) 40629452Scharnier err(ERROR_EXIT, "setuid(getuid())"); 40729452Scharnier if (chdir("/tmp") < 0) 40829452Scharnier err(ERROR_EXIT, "chdir(/tmp)"); 40929452Scharnier if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR) 41029452Scharnier errx(ERROR_EXIT, "editor or filename too long"); 41179452Sbrian execlp(editor, editor, Filename, (char *)NULL); 41229452Scharnier err(ERROR_EXIT, "%s", editor); 4132311Sjkh /*NOTREACHED*/ 4142311Sjkh default: 4152311Sjkh /* parent */ 4162311Sjkh break; 4172311Sjkh } 4182311Sjkh 4192311Sjkh /* parent */ 42015161Sscrappy { 42115161Sscrappy void (*f[4])(); 42215161Sscrappy f[0] = signal(SIGHUP, SIG_IGN); 42315161Sscrappy f[1] = signal(SIGINT, SIG_IGN); 42415161Sscrappy f[2] = signal(SIGTERM, SIG_IGN); 4252311Sjkh xpid = wait(&waiter); 42615161Sscrappy signal(SIGHUP, f[0]); 42715161Sscrappy signal(SIGINT, f[1]); 42815161Sscrappy signal(SIGTERM, f[2]); 42915161Sscrappy } 4302311Sjkh if (xpid != pid) { 43129452Scharnier warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor); 4322311Sjkh goto fatal; 4332311Sjkh } 4342311Sjkh if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) { 43529452Scharnier warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter)); 4362311Sjkh goto fatal; 4372311Sjkh } 4382311Sjkh if (WIFSIGNALED(waiter)) { 43929452Scharnier warnx("\"%s\" killed; signal %d (%score dumped)", 44029452Scharnier editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no "); 4412311Sjkh goto fatal; 4422311Sjkh } 4435176Sache if (stat(Filename, &statbuf) < 0) { 44429452Scharnier warn("stat"); 4452311Sjkh goto fatal; 4462311Sjkh } 44768388Sdwmalone if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino) 44868388Sdwmalone errx(ERROR_EXIT, "temp file must be edited in place"); 4492311Sjkh if (mtime == statbuf.st_mtime) { 45029452Scharnier warnx("no changes made to crontab"); 4512311Sjkh goto remove; 4522311Sjkh } 45329452Scharnier warnx("installing new crontab"); 4542311Sjkh switch (replace_cmd()) { 4552311Sjkh case 0: 4562311Sjkh break; 4572311Sjkh case -1: 4582311Sjkh for (;;) { 4592311Sjkh printf("Do you want to retry the same edit? "); 4602311Sjkh fflush(stdout); 4612311Sjkh q[0] = '\0'; 4622311Sjkh (void) fgets(q, sizeof q, stdin); 4632311Sjkh switch (islower(q[0]) ? q[0] : tolower(q[0])) { 4642311Sjkh case 'y': 4652311Sjkh goto again; 4662311Sjkh case 'n': 4672311Sjkh goto abandon; 4682311Sjkh default: 4692311Sjkh fprintf(stderr, "Enter Y or N\n"); 4702311Sjkh } 4712311Sjkh } 4722311Sjkh /*NOTREACHED*/ 4732311Sjkh case -2: 4742311Sjkh abandon: 47529452Scharnier warnx("edits left in %s", Filename); 4762311Sjkh goto done; 4772311Sjkh default: 47829452Scharnier warnx("panic: bad switch() in replace_cmd()"); 4792311Sjkh goto fatal; 4802311Sjkh } 4812311Sjkh remove: 4822311Sjkh unlink(Filename); 4832311Sjkh done: 4842311Sjkh log_it(RealUser, Pid, "END EDIT", User); 4852311Sjkh} 4862311Sjkh 4878857Srgrimes 4882311Sjkh/* returns 0 on success 4892311Sjkh * -1 on syntax error 4902311Sjkh * -2 on install error 4912311Sjkh */ 4922311Sjkhstatic int 4932311Sjkhreplace_cmd() { 4942311Sjkh char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME]; 4952311Sjkh FILE *tmp; 4962311Sjkh int ch, eof; 4972311Sjkh entry *e; 4982311Sjkh time_t now = time(NULL); 4992311Sjkh char **envp = env_init(); 5002311Sjkh 50120573Spst if (envp == NULL) { 50229452Scharnier warnx("cannot allocate memory"); 50320573Spst return (-2); 50420573Spst } 50520573Spst 5062311Sjkh (void) sprintf(n, "tmp.%d", Pid); 5072311Sjkh (void) sprintf(tn, CRON_TAB(n)); 5082311Sjkh if (!(tmp = fopen(tn, "w+"))) { 50929452Scharnier warn("%s", tn); 5102311Sjkh return (-2); 5112311Sjkh } 5122311Sjkh 5132311Sjkh /* write a signature at the top of the file. 5142311Sjkh * 5152311Sjkh * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code. 5162311Sjkh */ 5172311Sjkh fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n"); 5182311Sjkh fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now)); 5192311Sjkh fprintf(tmp, "# (Cron version -- %s)\n", rcsid); 5202311Sjkh 5212311Sjkh /* copy the crontab to the tmp 5222311Sjkh */ 52368388Sdwmalone rewind(NewCrontab); 5242311Sjkh Set_LineNum(1) 5252311Sjkh while (EOF != (ch = get_char(NewCrontab))) 5262311Sjkh putc(ch, tmp); 5272311Sjkh ftruncate(fileno(tmp), ftell(tmp)); 5282311Sjkh fflush(tmp); rewind(tmp); 5292311Sjkh 5302311Sjkh if (ferror(tmp)) { 53129452Scharnier warnx("error while writing new crontab to %s", tn); 5322311Sjkh fclose(tmp); unlink(tn); 5332311Sjkh return (-2); 5342311Sjkh } 5352311Sjkh 5362311Sjkh /* check the syntax of the file being installed. 5372311Sjkh */ 5382311Sjkh 5392311Sjkh /* BUG: was reporting errors after the EOF if there were any errors 5402311Sjkh * in the file proper -- kludged it by stopping after first error. 5412311Sjkh * vix 31mar87 5422311Sjkh */ 5432311Sjkh Set_LineNum(1 - NHEADER_LINES) 5442311Sjkh CheckErrorCount = 0; eof = FALSE; 5452311Sjkh while (!CheckErrorCount && !eof) { 5462311Sjkh switch (load_env(envstr, tmp)) { 5472311Sjkh case ERR: 5482311Sjkh eof = TRUE; 5492311Sjkh break; 5502311Sjkh case FALSE: 5512311Sjkh e = load_entry(tmp, check_error, pw, envp); 5522311Sjkh if (e) 5532311Sjkh free(e); 5542311Sjkh break; 5552311Sjkh case TRUE: 5562311Sjkh break; 5572311Sjkh } 5582311Sjkh } 5592311Sjkh 5602311Sjkh if (CheckErrorCount != 0) { 56129452Scharnier warnx("errors in crontab file, can't install"); 5622311Sjkh fclose(tmp); unlink(tn); 5632311Sjkh return (-1); 5642311Sjkh } 5652311Sjkh 5662311Sjkh#ifdef HAS_FCHOWN 5672311Sjkh if (fchown(fileno(tmp), ROOT_UID, -1) < OK) 5682311Sjkh#else 5692311Sjkh if (chown(tn, ROOT_UID, -1) < OK) 5702311Sjkh#endif 5712311Sjkh { 57229452Scharnier warn("chown"); 5732311Sjkh fclose(tmp); unlink(tn); 5742311Sjkh return (-2); 5752311Sjkh } 5762311Sjkh 5772311Sjkh#ifdef HAS_FCHMOD 5782311Sjkh if (fchmod(fileno(tmp), 0600) < OK) 5792311Sjkh#else 5802311Sjkh if (chmod(tn, 0600) < OK) 5812311Sjkh#endif 5822311Sjkh { 58329452Scharnier warn("chown"); 5842311Sjkh fclose(tmp); unlink(tn); 5852311Sjkh return (-2); 5862311Sjkh } 5872311Sjkh 5882311Sjkh if (fclose(tmp) == EOF) { 58929452Scharnier warn("fclose"); 5902311Sjkh unlink(tn); 5912311Sjkh return (-2); 5922311Sjkh } 5932311Sjkh 5942311Sjkh (void) sprintf(n, CRON_TAB(User)); 5952311Sjkh if (rename(tn, n)) { 59629452Scharnier warn("error renaming %s to %s", tn, n); 5972311Sjkh unlink(tn); 5982311Sjkh return (-2); 5992311Sjkh } 6002311Sjkh log_it(RealUser, Pid, "REPLACE", User); 6012311Sjkh 6022311Sjkh poke_daemon(); 6032311Sjkh 6042311Sjkh return (0); 6052311Sjkh} 6062311Sjkh 6072311Sjkh 6082311Sjkhstatic void 6092311Sjkhpoke_daemon() { 6102311Sjkh#ifdef USE_UTIMES 6112311Sjkh struct timeval tvs[2]; 6122311Sjkh struct timezone tz; 6132311Sjkh 6142311Sjkh (void) gettimeofday(&tvs[0], &tz); 6152311Sjkh tvs[1] = tvs[0]; 6162311Sjkh if (utimes(SPOOL_DIR, tvs) < OK) { 61729452Scharnier warn("can't update mtime on spooldir %s", SPOOL_DIR); 6182311Sjkh return; 6192311Sjkh } 6202311Sjkh#else 6212311Sjkh if (utime(SPOOL_DIR, NULL) < OK) { 62229452Scharnier warn("can't update mtime on spooldir %s", SPOOL_DIR); 6232311Sjkh return; 6242311Sjkh } 6252311Sjkh#endif /*USE_UTIMES*/ 6262311Sjkh} 627