crontab.c revision 135174
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 135174 2004-09-13 21:04:30Z dds $";
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
224135174Sddsstatic void
225135174Sddscopy_file(FILE *in, FILE *out) {
226135174Sdds	int	x, ch;
2272311Sjkh
228135174Sdds	Set_LineNum(1)
229135174Sdds	/* ignore the top few comments since we probably put them there.
230135174Sdds	 */
231135174Sdds	for (x = 0;  x < NHEADER_LINES;  x++) {
232135174Sdds		ch = get_char(in);
233135174Sdds		if (EOF == ch)
234135174Sdds			break;
235135174Sdds		if ('#' != ch) {
236135174Sdds			putc(ch, out);
237135174Sdds			break;
238135174Sdds		}
239135174Sdds		while (EOF != (ch = get_char(in)))
240135174Sdds			if (ch == '\n')
241135174Sdds				break;
242135174Sdds		if (EOF == ch)
243135174Sdds			break;
244135174Sdds	}
245135174Sdds
246135174Sdds	/* copy the rest of the crontab (if any) to the output file.
247135174Sdds	 */
248135174Sdds	if (EOF != ch)
249135174Sdds		while (EOF != (ch = get_char(in)))
250135174Sdds			putc(ch, out);
251135174Sdds}
252135174Sdds
2532311Sjkhstatic void
2542311Sjkhlist_cmd() {
2552311Sjkh	char	n[MAX_FNAME];
2562311Sjkh	FILE	*f;
2572311Sjkh
2582311Sjkh	log_it(RealUser, Pid, "LIST", User);
2592311Sjkh	(void) sprintf(n, CRON_TAB(User));
2602311Sjkh	if (!(f = fopen(n, "r"))) {
2612311Sjkh		if (errno == ENOENT)
26229452Scharnier			errx(ERROR_EXIT, "no crontab for %s", User);
2632311Sjkh		else
26429452Scharnier			err(ERROR_EXIT, "%s", n);
2652311Sjkh	}
2662311Sjkh
2672311Sjkh	/* file is open. copy to stdout, close.
2682311Sjkh	 */
269135174Sdds	copy_file(f, stdout);
2702311Sjkh	fclose(f);
2712311Sjkh}
2722311Sjkh
2732311Sjkh
2742311Sjkhstatic void
2752311Sjkhdelete_cmd() {
2762311Sjkh	char	n[MAX_FNAME];
27767127Spaul	int ch, first;
2782311Sjkh
27967127Spaul	if (isatty(STDIN_FILENO)) {
28067127Spaul		(void)fprintf(stderr, "remove crontab for %s? ", User);
28167127Spaul		first = ch = getchar();
28267127Spaul		while (ch != '\n' && ch != EOF)
28367127Spaul			ch = getchar();
28467127Spaul		if (first != 'y' && first != 'Y')
28567127Spaul			return;
28667127Spaul	}
28767127Spaul
2882311Sjkh	log_it(RealUser, Pid, "DELETE", User);
2892311Sjkh	(void) sprintf(n, CRON_TAB(User));
2902311Sjkh	if (unlink(n)) {
2912311Sjkh		if (errno == ENOENT)
29229452Scharnier			errx(ERROR_EXIT, "no crontab for %s", User);
2932311Sjkh		else
29429452Scharnier			err(ERROR_EXIT, "%s", n);
2952311Sjkh	}
2962311Sjkh	poke_daemon();
2972311Sjkh}
2982311Sjkh
2992311Sjkh
3002311Sjkhstatic void
3012311Sjkhcheck_error(msg)
3022311Sjkh	char	*msg;
3032311Sjkh{
3042311Sjkh	CheckErrorCount++;
3052311Sjkh	fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
3062311Sjkh}
3072311Sjkh
3082311Sjkh
3092311Sjkhstatic void
3102311Sjkhedit_cmd() {
3112311Sjkh	char		n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
3122311Sjkh	FILE		*f;
313135174Sdds	int		t;
31468388Sdwmalone	struct stat	statbuf, fsbuf;
3152311Sjkh	time_t		mtime;
3162311Sjkh	WAIT_T		waiter;
3172311Sjkh	PID_T		pid, xpid;
31820573Spst	mode_t		um;
319135165Sdds	int		syntax_error = 0;
3202311Sjkh
3212311Sjkh	log_it(RealUser, Pid, "BEGIN EDIT", User);
3222311Sjkh	(void) sprintf(n, CRON_TAB(User));
3232311Sjkh	if (!(f = fopen(n, "r"))) {
32429452Scharnier		if (errno != ENOENT)
32529452Scharnier			err(ERROR_EXIT, "%s", n);
32629452Scharnier		warnx("no crontab for %s - using an empty one", User);
32769793Sobrien		if (!(f = fopen(_PATH_DEVNULL, "r")))
32869793Sobrien			err(ERROR_EXIT, _PATH_DEVNULL);
3292311Sjkh	}
3302311Sjkh
33120573Spst	um = umask(077);
33220573Spst	(void) sprintf(Filename, "/tmp/crontab.XXXXXXXXXX");
33320573Spst	if ((t = mkstemp(Filename)) == -1) {
33429452Scharnier		warn("%s", Filename);
33520573Spst		(void) umask(um);
3362311Sjkh		goto fatal;
3372311Sjkh	}
33820573Spst	(void) umask(um);
3392311Sjkh#ifdef HAS_FCHOWN
3402311Sjkh	if (fchown(t, getuid(), getgid()) < 0) {
3412311Sjkh#else
3422311Sjkh	if (chown(Filename, getuid(), getgid()) < 0) {
3432311Sjkh#endif
34429452Scharnier		warn("fchown");
3452311Sjkh		goto fatal;
3462311Sjkh	}
34768388Sdwmalone	if (!(NewCrontab = fdopen(t, "r+"))) {
34829452Scharnier		warn("fdopen");
3492311Sjkh		goto fatal;
3502311Sjkh	}
3512311Sjkh
352135174Sdds	copy_file(f, NewCrontab);
3532311Sjkh	fclose(f);
35468388Sdwmalone	if (fflush(NewCrontab))
35529452Scharnier		err(ERROR_EXIT, "%s", Filename);
35668388Sdwmalone	if (fstat(t, &fsbuf) < 0) {
35768388Sdwmalone		warn("unable to fstat temp file");
35868388Sdwmalone		goto fatal;
35968388Sdwmalone	}
3602311Sjkh again:
3615176Sache	if (stat(Filename, &statbuf) < 0) {
36229452Scharnier		warn("stat");
3632311Sjkh fatal:		unlink(Filename);
3642311Sjkh		exit(ERROR_EXIT);
3652311Sjkh	}
36668388Sdwmalone	if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
36768388Sdwmalone		errx(ERROR_EXIT, "temp file must be edited in place");
3682311Sjkh	mtime = statbuf.st_mtime;
3692311Sjkh
3702311Sjkh	if ((!(editor = getenv("VISUAL")))
3712311Sjkh	 && (!(editor = getenv("EDITOR")))
3722311Sjkh	    ) {
3732311Sjkh		editor = EDITOR;
3742311Sjkh	}
3752311Sjkh
3762311Sjkh	/* we still have the file open.  editors will generally rewrite the
3772311Sjkh	 * original file rather than renaming/unlinking it and starting a
3782311Sjkh	 * new one; even backup files are supposed to be made by copying
3792311Sjkh	 * rather than by renaming.  if some editor does not support this,
3802311Sjkh	 * then don't use it.  the security problems are more severe if we
3812311Sjkh	 * close and reopen the file around the edit.
3822311Sjkh	 */
3832311Sjkh
3842311Sjkh	switch (pid = fork()) {
3852311Sjkh	case -1:
38629452Scharnier		warn("fork");
3872311Sjkh		goto fatal;
3882311Sjkh	case 0:
3892311Sjkh		/* child */
39029452Scharnier		if (setuid(getuid()) < 0)
39129452Scharnier			err(ERROR_EXIT, "setuid(getuid())");
39229452Scharnier		if (chdir("/tmp") < 0)
39329452Scharnier			err(ERROR_EXIT, "chdir(/tmp)");
39429452Scharnier		if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR)
39529452Scharnier			errx(ERROR_EXIT, "editor or filename too long");
39679452Sbrian		execlp(editor, editor, Filename, (char *)NULL);
39729452Scharnier		err(ERROR_EXIT, "%s", editor);
3982311Sjkh		/*NOTREACHED*/
3992311Sjkh	default:
4002311Sjkh		/* parent */
4012311Sjkh		break;
4022311Sjkh	}
4032311Sjkh
4042311Sjkh	/* parent */
40515161Sscrappy	{
40615161Sscrappy	void (*f[4])();
40715161Sscrappy	f[0] = signal(SIGHUP, SIG_IGN);
40815161Sscrappy	f[1] = signal(SIGINT, SIG_IGN);
40915161Sscrappy	f[2] = signal(SIGTERM, SIG_IGN);
4102311Sjkh	xpid = wait(&waiter);
41115161Sscrappy	signal(SIGHUP, f[0]);
41215161Sscrappy	signal(SIGINT, f[1]);
41315161Sscrappy	signal(SIGTERM, f[2]);
41415161Sscrappy	}
4152311Sjkh	if (xpid != pid) {
41629452Scharnier		warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor);
4172311Sjkh		goto fatal;
4182311Sjkh	}
4192311Sjkh	if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
42029452Scharnier		warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter));
4212311Sjkh		goto fatal;
4222311Sjkh	}
4232311Sjkh	if (WIFSIGNALED(waiter)) {
42429452Scharnier		warnx("\"%s\" killed; signal %d (%score dumped)",
42529452Scharnier			editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no ");
4262311Sjkh		goto fatal;
4272311Sjkh	}
4285176Sache	if (stat(Filename, &statbuf) < 0) {
42929452Scharnier		warn("stat");
4302311Sjkh		goto fatal;
4312311Sjkh	}
43268388Sdwmalone	if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
43368388Sdwmalone		errx(ERROR_EXIT, "temp file must be edited in place");
434135165Sdds	if (mtime == statbuf.st_mtime && !syntax_error) {
43529452Scharnier		warnx("no changes made to crontab");
4362311Sjkh		goto remove;
4372311Sjkh	}
43829452Scharnier	warnx("installing new crontab");
4392311Sjkh	switch (replace_cmd()) {
440135165Sdds	case 0:			/* Success */
4412311Sjkh		break;
442135165Sdds	case -1:		/* Syntax error */
4432311Sjkh		for (;;) {
4442311Sjkh			printf("Do you want to retry the same edit? ");
4452311Sjkh			fflush(stdout);
4462311Sjkh			q[0] = '\0';
4472311Sjkh			(void) fgets(q, sizeof q, stdin);
4482311Sjkh			switch (islower(q[0]) ? q[0] : tolower(q[0])) {
4492311Sjkh			case 'y':
450135165Sdds				syntax_error = 1;
4512311Sjkh				goto again;
4522311Sjkh			case 'n':
4532311Sjkh				goto abandon;
4542311Sjkh			default:
4552311Sjkh				fprintf(stderr, "Enter Y or N\n");
4562311Sjkh			}
4572311Sjkh		}
4582311Sjkh		/*NOTREACHED*/
459135165Sdds	case -2:		/* Install error */
4602311Sjkh	abandon:
46129452Scharnier		warnx("edits left in %s", Filename);
4622311Sjkh		goto done;
4632311Sjkh	default:
46429452Scharnier		warnx("panic: bad switch() in replace_cmd()");
4652311Sjkh		goto fatal;
4662311Sjkh	}
4672311Sjkh remove:
4682311Sjkh	unlink(Filename);
4692311Sjkh done:
4702311Sjkh	log_it(RealUser, Pid, "END EDIT", User);
4712311Sjkh}
4722311Sjkh
4738857Srgrimes
4742311Sjkh/* returns	0	on success
4752311Sjkh *		-1	on syntax error
4762311Sjkh *		-2	on install error
4772311Sjkh */
4782311Sjkhstatic int
4792311Sjkhreplace_cmd() {
4802311Sjkh	char	n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
4812311Sjkh	FILE	*tmp;
4822311Sjkh	int	ch, eof;
4832311Sjkh	entry	*e;
4842311Sjkh	time_t	now = time(NULL);
4852311Sjkh	char	**envp = env_init();
4862311Sjkh
48720573Spst	if (envp == NULL) {
48829452Scharnier		warnx("cannot allocate memory");
48920573Spst		return (-2);
49020573Spst	}
49120573Spst
4922311Sjkh	(void) sprintf(n, "tmp.%d", Pid);
4932311Sjkh	(void) sprintf(tn, CRON_TAB(n));
4942311Sjkh	if (!(tmp = fopen(tn, "w+"))) {
49529452Scharnier		warn("%s", tn);
4962311Sjkh		return (-2);
4972311Sjkh	}
4982311Sjkh
4992311Sjkh	/* write a signature at the top of the file.
5002311Sjkh	 *
5012311Sjkh	 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
5022311Sjkh	 */
5032311Sjkh	fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
5042311Sjkh	fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
5052311Sjkh	fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
5062311Sjkh
5072311Sjkh	/* copy the crontab to the tmp
5082311Sjkh	 */
50968388Sdwmalone	rewind(NewCrontab);
5102311Sjkh	Set_LineNum(1)
5112311Sjkh	while (EOF != (ch = get_char(NewCrontab)))
5122311Sjkh		putc(ch, tmp);
5132311Sjkh	ftruncate(fileno(tmp), ftell(tmp));
5142311Sjkh	fflush(tmp);  rewind(tmp);
5152311Sjkh
5162311Sjkh	if (ferror(tmp)) {
51729452Scharnier		warnx("error while writing new crontab to %s", tn);
5182311Sjkh		fclose(tmp);  unlink(tn);
5192311Sjkh		return (-2);
5202311Sjkh	}
5212311Sjkh
5222311Sjkh	/* check the syntax of the file being installed.
5232311Sjkh	 */
5242311Sjkh
5252311Sjkh	/* BUG: was reporting errors after the EOF if there were any errors
5262311Sjkh	 * in the file proper -- kludged it by stopping after first error.
5272311Sjkh	 *		vix 31mar87
5282311Sjkh	 */
5292311Sjkh	Set_LineNum(1 - NHEADER_LINES)
5302311Sjkh	CheckErrorCount = 0;  eof = FALSE;
5312311Sjkh	while (!CheckErrorCount && !eof) {
5322311Sjkh		switch (load_env(envstr, tmp)) {
5332311Sjkh		case ERR:
5342311Sjkh			eof = TRUE;
5352311Sjkh			break;
5362311Sjkh		case FALSE:
5372311Sjkh			e = load_entry(tmp, check_error, pw, envp);
5382311Sjkh			if (e)
5392311Sjkh				free(e);
5402311Sjkh			break;
5412311Sjkh		case TRUE:
5422311Sjkh			break;
5432311Sjkh		}
5442311Sjkh	}
5452311Sjkh
5462311Sjkh	if (CheckErrorCount != 0) {
54729452Scharnier		warnx("errors in crontab file, can't install");
5482311Sjkh		fclose(tmp);  unlink(tn);
5492311Sjkh		return (-1);
5502311Sjkh	}
5512311Sjkh
5522311Sjkh#ifdef HAS_FCHOWN
5532311Sjkh	if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
5542311Sjkh#else
5552311Sjkh	if (chown(tn, ROOT_UID, -1) < OK)
5562311Sjkh#endif
5572311Sjkh	{
55829452Scharnier		warn("chown");
5592311Sjkh		fclose(tmp);  unlink(tn);
5602311Sjkh		return (-2);
5612311Sjkh	}
5622311Sjkh
5632311Sjkh#ifdef HAS_FCHMOD
5642311Sjkh	if (fchmod(fileno(tmp), 0600) < OK)
5652311Sjkh#else
5662311Sjkh	if (chmod(tn, 0600) < OK)
5672311Sjkh#endif
5682311Sjkh	{
56929452Scharnier		warn("chown");
5702311Sjkh		fclose(tmp);  unlink(tn);
5712311Sjkh		return (-2);
5722311Sjkh	}
5732311Sjkh
5742311Sjkh	if (fclose(tmp) == EOF) {
57529452Scharnier		warn("fclose");
5762311Sjkh		unlink(tn);
5772311Sjkh		return (-2);
5782311Sjkh	}
5792311Sjkh
5802311Sjkh	(void) sprintf(n, CRON_TAB(User));
5812311Sjkh	if (rename(tn, n)) {
58229452Scharnier		warn("error renaming %s to %s", tn, n);
5832311Sjkh		unlink(tn);
5842311Sjkh		return (-2);
5852311Sjkh	}
5862311Sjkh	log_it(RealUser, Pid, "REPLACE", User);
5872311Sjkh
5882311Sjkh	poke_daemon();
5892311Sjkh
5902311Sjkh	return (0);
5912311Sjkh}
5922311Sjkh
5932311Sjkh
5942311Sjkhstatic void
5952311Sjkhpoke_daemon() {
5962311Sjkh#ifdef USE_UTIMES
5972311Sjkh	struct timeval tvs[2];
5982311Sjkh	struct timezone tz;
5992311Sjkh
6002311Sjkh	(void) gettimeofday(&tvs[0], &tz);
6012311Sjkh	tvs[1] = tvs[0];
6022311Sjkh	if (utimes(SPOOL_DIR, tvs) < OK) {
60329452Scharnier		warn("can't update mtime on spooldir %s", SPOOL_DIR);
6042311Sjkh		return;
6052311Sjkh	}
6062311Sjkh#else
6072311Sjkh	if (utime(SPOOL_DIR, NULL) < OK) {
60829452Scharnier		warn("can't update mtime on spooldir %s", SPOOL_DIR);
6092311Sjkh		return;
6102311Sjkh	}
6112311Sjkh#endif /*USE_UTIMES*/
6122311Sjkh}
613