crontab.c revision 184706
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 184706 2008-11-06 04:53:02Z matteo $";
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>
34135242Sdds#include <md5.h>
3569793Sobrien#include <paths.h>
362311Sjkh#include <sys/file.h>
372311Sjkh#include <sys/stat.h>
382311Sjkh#ifdef USE_UTIMES
392311Sjkh# include <sys/time.h>
402311Sjkh#else
412311Sjkh# include <time.h>
422311Sjkh# include <utime.h>
432311Sjkh#endif
442311Sjkh#if defined(POSIX)
452311Sjkh# include <locale.h>
462311Sjkh#endif
472311Sjkh
48135242Sdds#define MD5_SIZE 33
492311Sjkh#define NHEADER_LINES 3
502311Sjkh
512311Sjkh
522311Sjkhenum opt_t	{ opt_unknown, opt_list, opt_delete, opt_edit, opt_replace };
532311Sjkh
542311Sjkh#if DEBUGGING
552311Sjkhstatic char	*Options[] = { "???", "list", "delete", "edit", "replace" };
562311Sjkh#endif
572311Sjkh
582311Sjkh
592311Sjkhstatic	PID_T		Pid;
602311Sjkhstatic	char		User[MAX_UNAME], RealUser[MAX_UNAME];
612311Sjkhstatic	char		Filename[MAX_FNAME];
622311Sjkhstatic	FILE		*NewCrontab;
632311Sjkhstatic	int		CheckErrorCount;
642311Sjkhstatic	enum opt_t	Option;
652311Sjkhstatic	struct passwd	*pw;
66173412Skevlostatic	void		list_cmd(void),
67173412Skevlo			delete_cmd(void),
68173412Skevlo			edit_cmd(void),
69173412Skevlo			poke_daemon(void),
70173412Skevlo			check_error(char *),
71173412Skevlo			parse_args(int c, char *v[]);
72173412Skevlostatic	int		replace_cmd(void);
732311Sjkh
742311Sjkh
752311Sjkhstatic void
762311Sjkhusage(msg)
772311Sjkh	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
882311Sjkhmain(argc, argv)
892311Sjkh	int	argc;
902311Sjkh	char	*argv[];
912311Sjkh{
922311Sjkh	int	exitstatus;
932311Sjkh
942311Sjkh	Pid = getpid();
952311Sjkh	ProgramName = argv[0];
962311Sjkh
972311Sjkh#if defined(POSIX)
982311Sjkh	setlocale(LC_ALL, "");
992311Sjkh#endif
1002311Sjkh
1012311Sjkh#if defined(BSD)
1022311Sjkh	setlinebuf(stderr);
1032311Sjkh#endif
1042311Sjkh	parse_args(argc, argv);		/* sets many globals, opens a file */
1052311Sjkh	set_cron_uid();
1062311Sjkh	set_cron_cwd();
1072311Sjkh	if (!allowed(User)) {
10829452Scharnier		warnx("you (%s) are not allowed to use this program", User);
1092311Sjkh		log_it(RealUser, Pid, "AUTH", "crontab command not allowed");
1102311Sjkh		exit(ERROR_EXIT);
1112311Sjkh	}
1122311Sjkh	exitstatus = OK_EXIT;
1132311Sjkh	switch (Option) {
1142311Sjkh	case opt_list:		list_cmd();
1152311Sjkh				break;
1162311Sjkh	case opt_delete:	delete_cmd();
1172311Sjkh				break;
1182311Sjkh	case opt_edit:		edit_cmd();
1192311Sjkh				break;
1202311Sjkh	case opt_replace:	if (replace_cmd() < 0)
1212311Sjkh					exitstatus = ERROR_EXIT;
1222311Sjkh				break;
12329452Scharnier	case opt_unknown:
12429452Scharnier				break;
1252311Sjkh	}
126104326Sdd	exit(exitstatus);
1272311Sjkh	/*NOTREACHED*/
1282311Sjkh}
1292311Sjkh
1308857Srgrimes
1312311Sjkhstatic void
1322311Sjkhparse_args(argc, argv)
1332311Sjkh	int	argc;
1342311Sjkh	char	*argv[];
1352311Sjkh{
1362311Sjkh	int		argch;
137154166Sbrooks	char		resolved_path[PATH_MAX];
1382311Sjkh
13929452Scharnier	if (!(pw = getpwuid(getuid())))
14029452Scharnier		errx(ERROR_EXIT, "your UID isn't in the passwd file, bailing out");
141184706Smatteo	bzero(pw->pw_passwd, strlen(pw->pw_passwd));
14220573Spst	(void) strncpy(User, pw->pw_name, (sizeof User)-1);
14320573Spst	User[(sizeof User)-1] = '\0';
1442311Sjkh	strcpy(RealUser, User);
1452311Sjkh	Filename[0] = '\0';
1462311Sjkh	Option = opt_unknown;
14724428Simp	while ((argch = getopt(argc, argv, "u:lerx:")) != -1) {
1482311Sjkh		switch (argch) {
1492311Sjkh		case 'x':
1502311Sjkh			if (!set_debug_flags(optarg))
1512311Sjkh				usage("bad debug option");
1522311Sjkh			break;
1532311Sjkh		case 'u':
1542311Sjkh			if (getuid() != ROOT_UID)
15529452Scharnier				errx(ERROR_EXIT, "must be privileged to use -u");
1562311Sjkh			if (!(pw = getpwnam(optarg)))
15729452Scharnier				errx(ERROR_EXIT, "user `%s' unknown", optarg);
158184706Smatteo			bzero(pw->pw_passwd, strlen(pw->pw_passwd));
15920573Spst			(void) strncpy(User, pw->pw_name, (sizeof User)-1);
16020573Spst			User[(sizeof User)-1] = '\0';
1612311Sjkh			break;
1622311Sjkh		case 'l':
1632311Sjkh			if (Option != opt_unknown)
1642311Sjkh				usage("only one operation permitted");
1652311Sjkh			Option = opt_list;
1662311Sjkh			break;
1672311Sjkh		case 'r':
1682311Sjkh			if (Option != opt_unknown)
1692311Sjkh				usage("only one operation permitted");
1702311Sjkh			Option = opt_delete;
1712311Sjkh			break;
1722311Sjkh		case 'e':
1732311Sjkh			if (Option != opt_unknown)
1742311Sjkh				usage("only one operation permitted");
1752311Sjkh			Option = opt_edit;
1762311Sjkh			break;
1772311Sjkh		default:
1782311Sjkh			usage("unrecognized option");
1792311Sjkh		}
1802311Sjkh	}
1812311Sjkh
1822311Sjkh	endpwent();
1832311Sjkh
1842311Sjkh	if (Option != opt_unknown) {
1852311Sjkh		if (argv[optind] != NULL) {
1862311Sjkh			usage("no arguments permitted after this option");
1872311Sjkh		}
1882311Sjkh	} else {
1892311Sjkh		if (argv[optind] != NULL) {
1902311Sjkh			Option = opt_replace;
19120573Spst			(void) strncpy (Filename, argv[optind], (sizeof Filename)-1);
19220573Spst			Filename[(sizeof Filename)-1] = '\0';
19320573Spst
1942311Sjkh		} else {
1952311Sjkh			usage("file name must be specified for replace");
1962311Sjkh		}
1972311Sjkh	}
1982311Sjkh
1992311Sjkh	if (Option == opt_replace) {
2002311Sjkh		/* we have to open the file here because we're going to
2012311Sjkh		 * chdir(2) into /var/cron before we get around to
2022311Sjkh		 * reading the file.
2032311Sjkh		 */
2042311Sjkh		if (!strcmp(Filename, "-")) {
2052311Sjkh			NewCrontab = stdin;
206154166Sbrooks		} else if (realpath(Filename, resolved_path) != NULL &&
207161964Sru		    !strcmp(resolved_path, SYSCRONTAB)) {
208161964Sru			err(ERROR_EXIT, SYSCRONTAB " must be edited manually");
2092311Sjkh		} else {
2102311Sjkh			/* relinquish the setuid status of the binary during
2112311Sjkh			 * the open, lest nonroot users read files they should
2122311Sjkh			 * not be able to read.  we can't use access() here
2132311Sjkh			 * since there's a race condition.  thanks go out to
2142311Sjkh			 * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting
2152311Sjkh			 * the race.
2162311Sjkh			 */
2172311Sjkh
21829452Scharnier			if (swap_uids() < OK)
21929452Scharnier				err(ERROR_EXIT, "swapping uids");
22029452Scharnier			if (!(NewCrontab = fopen(Filename, "r")))
22129452Scharnier				err(ERROR_EXIT, "%s", Filename);
22229452Scharnier			if (swap_uids() < OK)
22329452Scharnier				err(ERROR_EXIT, "swapping uids back");
2242311Sjkh		}
2252311Sjkh	}
2262311Sjkh
2272311Sjkh	Debug(DMISC, ("user=%s, file=%s, option=%s\n",
2282311Sjkh		      User, Filename, Options[(int)Option]))
2292311Sjkh}
2302311Sjkh
231135174Sddsstatic void
232135174Sddscopy_file(FILE *in, FILE *out) {
233135174Sdds	int	x, ch;
2342311Sjkh
235135174Sdds	Set_LineNum(1)
236135174Sdds	/* ignore the top few comments since we probably put them there.
237135174Sdds	 */
238135174Sdds	for (x = 0;  x < NHEADER_LINES;  x++) {
239135174Sdds		ch = get_char(in);
240135174Sdds		if (EOF == ch)
241135174Sdds			break;
242135174Sdds		if ('#' != ch) {
243135174Sdds			putc(ch, out);
244135174Sdds			break;
245135174Sdds		}
246135174Sdds		while (EOF != (ch = get_char(in)))
247135174Sdds			if (ch == '\n')
248135174Sdds				break;
249135174Sdds		if (EOF == ch)
250135174Sdds			break;
251135174Sdds	}
252135174Sdds
253135174Sdds	/* copy the rest of the crontab (if any) to the output file.
254135174Sdds	 */
255135174Sdds	if (EOF != ch)
256135174Sdds		while (EOF != (ch = get_char(in)))
257135174Sdds			putc(ch, out);
258135174Sdds}
259135174Sdds
2602311Sjkhstatic void
2612311Sjkhlist_cmd() {
2622311Sjkh	char	n[MAX_FNAME];
2632311Sjkh	FILE	*f;
2642311Sjkh
2652311Sjkh	log_it(RealUser, Pid, "LIST", User);
2662311Sjkh	(void) sprintf(n, CRON_TAB(User));
2672311Sjkh	if (!(f = fopen(n, "r"))) {
2682311Sjkh		if (errno == ENOENT)
26929452Scharnier			errx(ERROR_EXIT, "no crontab for %s", User);
2702311Sjkh		else
27129452Scharnier			err(ERROR_EXIT, "%s", n);
2722311Sjkh	}
2732311Sjkh
2742311Sjkh	/* file is open. copy to stdout, close.
2752311Sjkh	 */
276135174Sdds	copy_file(f, stdout);
2772311Sjkh	fclose(f);
2782311Sjkh}
2792311Sjkh
2802311Sjkh
2812311Sjkhstatic void
2822311Sjkhdelete_cmd() {
2832311Sjkh	char	n[MAX_FNAME];
28467127Spaul	int ch, first;
2852311Sjkh
28667127Spaul	if (isatty(STDIN_FILENO)) {
28767127Spaul		(void)fprintf(stderr, "remove crontab for %s? ", User);
28867127Spaul		first = ch = getchar();
28967127Spaul		while (ch != '\n' && ch != EOF)
29067127Spaul			ch = getchar();
29167127Spaul		if (first != 'y' && first != 'Y')
29267127Spaul			return;
29367127Spaul	}
29467127Spaul
2952311Sjkh	log_it(RealUser, Pid, "DELETE", User);
2962311Sjkh	(void) sprintf(n, CRON_TAB(User));
2972311Sjkh	if (unlink(n)) {
2982311Sjkh		if (errno == ENOENT)
29929452Scharnier			errx(ERROR_EXIT, "no crontab for %s", User);
3002311Sjkh		else
30129452Scharnier			err(ERROR_EXIT, "%s", n);
3022311Sjkh	}
3032311Sjkh	poke_daemon();
3042311Sjkh}
3052311Sjkh
3062311Sjkh
3072311Sjkhstatic void
3082311Sjkhcheck_error(msg)
3092311Sjkh	char	*msg;
3102311Sjkh{
3112311Sjkh	CheckErrorCount++;
3122311Sjkh	fprintf(stderr, "\"%s\":%d: %s\n", Filename, LineNumber-1, msg);
3132311Sjkh}
3142311Sjkh
3152311Sjkh
3162311Sjkhstatic void
3172311Sjkhedit_cmd() {
3182311Sjkh	char		n[MAX_FNAME], q[MAX_TEMPSTR], *editor;
3192311Sjkh	FILE		*f;
320135174Sdds	int		t;
32168388Sdwmalone	struct stat	statbuf, fsbuf;
3222311Sjkh	WAIT_T		waiter;
3232311Sjkh	PID_T		pid, xpid;
32420573Spst	mode_t		um;
325135165Sdds	int		syntax_error = 0;
326135242Sdds	char		orig_md5[MD5_SIZE];
327135242Sdds	char		new_md5[MD5_SIZE];
3282311Sjkh
3292311Sjkh	log_it(RealUser, Pid, "BEGIN EDIT", User);
3302311Sjkh	(void) sprintf(n, CRON_TAB(User));
3312311Sjkh	if (!(f = fopen(n, "r"))) {
33229452Scharnier		if (errno != ENOENT)
33329452Scharnier			err(ERROR_EXIT, "%s", n);
33429452Scharnier		warnx("no crontab for %s - using an empty one", User);
33569793Sobrien		if (!(f = fopen(_PATH_DEVNULL, "r")))
33669793Sobrien			err(ERROR_EXIT, _PATH_DEVNULL);
3372311Sjkh	}
3382311Sjkh
33920573Spst	um = umask(077);
34020573Spst	(void) sprintf(Filename, "/tmp/crontab.XXXXXXXXXX");
34120573Spst	if ((t = mkstemp(Filename)) == -1) {
34229452Scharnier		warn("%s", Filename);
34320573Spst		(void) umask(um);
3442311Sjkh		goto fatal;
3452311Sjkh	}
34620573Spst	(void) umask(um);
3472311Sjkh#ifdef HAS_FCHOWN
3482311Sjkh	if (fchown(t, getuid(), getgid()) < 0) {
3492311Sjkh#else
3502311Sjkh	if (chown(Filename, getuid(), getgid()) < 0) {
3512311Sjkh#endif
35229452Scharnier		warn("fchown");
3532311Sjkh		goto fatal;
3542311Sjkh	}
35568388Sdwmalone	if (!(NewCrontab = fdopen(t, "r+"))) {
35629452Scharnier		warn("fdopen");
3572311Sjkh		goto fatal;
3582311Sjkh	}
3592311Sjkh
360135174Sdds	copy_file(f, NewCrontab);
3612311Sjkh	fclose(f);
36268388Sdwmalone	if (fflush(NewCrontab))
36329452Scharnier		err(ERROR_EXIT, "%s", Filename);
36468388Sdwmalone	if (fstat(t, &fsbuf) < 0) {
36568388Sdwmalone		warn("unable to fstat temp file");
36668388Sdwmalone		goto fatal;
36768388Sdwmalone	}
3682311Sjkh again:
3695176Sache	if (stat(Filename, &statbuf) < 0) {
37029452Scharnier		warn("stat");
3712311Sjkh fatal:		unlink(Filename);
3722311Sjkh		exit(ERROR_EXIT);
3732311Sjkh	}
37468388Sdwmalone	if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
37568388Sdwmalone		errx(ERROR_EXIT, "temp file must be edited in place");
376135242Sdds	if (MD5File(Filename, orig_md5) == NULL) {
377135242Sdds		warn("MD5");
378135242Sdds		goto fatal;
379135242Sdds	}
3802311Sjkh
3812311Sjkh	if ((!(editor = getenv("VISUAL")))
3822311Sjkh	 && (!(editor = getenv("EDITOR")))
3832311Sjkh	    ) {
3842311Sjkh		editor = EDITOR;
3852311Sjkh	}
3862311Sjkh
3872311Sjkh	/* we still have the file open.  editors will generally rewrite the
3882311Sjkh	 * original file rather than renaming/unlinking it and starting a
3892311Sjkh	 * new one; even backup files are supposed to be made by copying
3902311Sjkh	 * rather than by renaming.  if some editor does not support this,
3912311Sjkh	 * then don't use it.  the security problems are more severe if we
3922311Sjkh	 * close and reopen the file around the edit.
3932311Sjkh	 */
3942311Sjkh
3952311Sjkh	switch (pid = fork()) {
3962311Sjkh	case -1:
39729452Scharnier		warn("fork");
3982311Sjkh		goto fatal;
3992311Sjkh	case 0:
4002311Sjkh		/* child */
40129452Scharnier		if (setuid(getuid()) < 0)
40229452Scharnier			err(ERROR_EXIT, "setuid(getuid())");
40329452Scharnier		if (chdir("/tmp") < 0)
40429452Scharnier			err(ERROR_EXIT, "chdir(/tmp)");
40529452Scharnier		if (strlen(editor) + strlen(Filename) + 2 >= MAX_TEMPSTR)
40629452Scharnier			errx(ERROR_EXIT, "editor or filename too long");
40779452Sbrian		execlp(editor, editor, Filename, (char *)NULL);
40829452Scharnier		err(ERROR_EXIT, "%s", editor);
4092311Sjkh		/*NOTREACHED*/
4102311Sjkh	default:
4112311Sjkh		/* parent */
4122311Sjkh		break;
4132311Sjkh	}
4142311Sjkh
4152311Sjkh	/* parent */
41615161Sscrappy	{
41715161Sscrappy	void (*f[4])();
41815161Sscrappy	f[0] = signal(SIGHUP, SIG_IGN);
41915161Sscrappy	f[1] = signal(SIGINT, SIG_IGN);
42015161Sscrappy	f[2] = signal(SIGTERM, SIG_IGN);
4212311Sjkh	xpid = wait(&waiter);
42215161Sscrappy	signal(SIGHUP, f[0]);
42315161Sscrappy	signal(SIGINT, f[1]);
42415161Sscrappy	signal(SIGTERM, f[2]);
42515161Sscrappy	}
4262311Sjkh	if (xpid != pid) {
42729452Scharnier		warnx("wrong PID (%d != %d) from \"%s\"", xpid, pid, editor);
4282311Sjkh		goto fatal;
4292311Sjkh	}
4302311Sjkh	if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
43129452Scharnier		warnx("\"%s\" exited with status %d", editor, WEXITSTATUS(waiter));
4322311Sjkh		goto fatal;
4332311Sjkh	}
4342311Sjkh	if (WIFSIGNALED(waiter)) {
43529452Scharnier		warnx("\"%s\" killed; signal %d (%score dumped)",
43629452Scharnier			editor, WTERMSIG(waiter), WCOREDUMP(waiter) ?"" :"no ");
4372311Sjkh		goto fatal;
4382311Sjkh	}
4395176Sache	if (stat(Filename, &statbuf) < 0) {
44029452Scharnier		warn("stat");
4412311Sjkh		goto fatal;
4422311Sjkh	}
44368388Sdwmalone	if (statbuf.st_dev != fsbuf.st_dev || statbuf.st_ino != fsbuf.st_ino)
44468388Sdwmalone		errx(ERROR_EXIT, "temp file must be edited in place");
445135242Sdds	if (MD5File(Filename, new_md5) == NULL) {
446135242Sdds		warn("MD5");
447135242Sdds		goto fatal;
448135242Sdds	}
449135242Sdds	if (strcmp(orig_md5, new_md5) == 0 && !syntax_error) {
45029452Scharnier		warnx("no changes made to crontab");
4512311Sjkh		goto remove;
4522311Sjkh	}
45329452Scharnier	warnx("installing new crontab");
4542311Sjkh	switch (replace_cmd()) {
455135165Sdds	case 0:			/* Success */
4562311Sjkh		break;
457135165Sdds	case -1:		/* Syntax error */
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':
465135165Sdds				syntax_error = 1;
4662311Sjkh				goto again;
4672311Sjkh			case 'n':
4682311Sjkh				goto abandon;
4692311Sjkh			default:
4702311Sjkh				fprintf(stderr, "Enter Y or N\n");
4712311Sjkh			}
4722311Sjkh		}
4732311Sjkh		/*NOTREACHED*/
474135165Sdds	case -2:		/* Install error */
4752311Sjkh	abandon:
47629452Scharnier		warnx("edits left in %s", Filename);
4772311Sjkh		goto done;
4782311Sjkh	default:
47929452Scharnier		warnx("panic: bad switch() in replace_cmd()");
4802311Sjkh		goto fatal;
4812311Sjkh	}
4822311Sjkh remove:
4832311Sjkh	unlink(Filename);
4842311Sjkh done:
4852311Sjkh	log_it(RealUser, Pid, "END EDIT", User);
4862311Sjkh}
4872311Sjkh
4888857Srgrimes
4892311Sjkh/* returns	0	on success
4902311Sjkh *		-1	on syntax error
4912311Sjkh *		-2	on install error
4922311Sjkh */
4932311Sjkhstatic int
4942311Sjkhreplace_cmd() {
4952311Sjkh	char	n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
4962311Sjkh	FILE	*tmp;
4972311Sjkh	int	ch, eof;
4982311Sjkh	entry	*e;
4992311Sjkh	time_t	now = time(NULL);
5002311Sjkh	char	**envp = env_init();
5012311Sjkh
50220573Spst	if (envp == NULL) {
50329452Scharnier		warnx("cannot allocate memory");
50420573Spst		return (-2);
50520573Spst	}
50620573Spst
5072311Sjkh	(void) sprintf(n, "tmp.%d", Pid);
5082311Sjkh	(void) sprintf(tn, CRON_TAB(n));
5092311Sjkh	if (!(tmp = fopen(tn, "w+"))) {
51029452Scharnier		warn("%s", tn);
5112311Sjkh		return (-2);
5122311Sjkh	}
5132311Sjkh
5142311Sjkh	/* write a signature at the top of the file.
5152311Sjkh	 *
5162311Sjkh	 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
5172311Sjkh	 */
5182311Sjkh	fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
5192311Sjkh	fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
5202311Sjkh	fprintf(tmp, "# (Cron version -- %s)\n", rcsid);
5212311Sjkh
5222311Sjkh	/* copy the crontab to the tmp
5232311Sjkh	 */
52468388Sdwmalone	rewind(NewCrontab);
5252311Sjkh	Set_LineNum(1)
5262311Sjkh	while (EOF != (ch = get_char(NewCrontab)))
5272311Sjkh		putc(ch, tmp);
5282311Sjkh	ftruncate(fileno(tmp), ftell(tmp));
5292311Sjkh	fflush(tmp);  rewind(tmp);
5302311Sjkh
5312311Sjkh	if (ferror(tmp)) {
53229452Scharnier		warnx("error while writing new crontab to %s", tn);
5332311Sjkh		fclose(tmp);  unlink(tn);
5342311Sjkh		return (-2);
5352311Sjkh	}
5362311Sjkh
5372311Sjkh	/* check the syntax of the file being installed.
5382311Sjkh	 */
5392311Sjkh
5402311Sjkh	/* BUG: was reporting errors after the EOF if there were any errors
5412311Sjkh	 * in the file proper -- kludged it by stopping after first error.
5422311Sjkh	 *		vix 31mar87
5432311Sjkh	 */
5442311Sjkh	Set_LineNum(1 - NHEADER_LINES)
5452311Sjkh	CheckErrorCount = 0;  eof = FALSE;
5462311Sjkh	while (!CheckErrorCount && !eof) {
5472311Sjkh		switch (load_env(envstr, tmp)) {
5482311Sjkh		case ERR:
5492311Sjkh			eof = TRUE;
5502311Sjkh			break;
5512311Sjkh		case FALSE:
5522311Sjkh			e = load_entry(tmp, check_error, pw, envp);
5532311Sjkh			if (e)
5542311Sjkh				free(e);
5552311Sjkh			break;
5562311Sjkh		case TRUE:
5572311Sjkh			break;
5582311Sjkh		}
5592311Sjkh	}
5602311Sjkh
5612311Sjkh	if (CheckErrorCount != 0) {
56229452Scharnier		warnx("errors in crontab file, can't install");
5632311Sjkh		fclose(tmp);  unlink(tn);
5642311Sjkh		return (-1);
5652311Sjkh	}
5662311Sjkh
5672311Sjkh#ifdef HAS_FCHOWN
5682311Sjkh	if (fchown(fileno(tmp), ROOT_UID, -1) < OK)
5692311Sjkh#else
5702311Sjkh	if (chown(tn, ROOT_UID, -1) < OK)
5712311Sjkh#endif
5722311Sjkh	{
57329452Scharnier		warn("chown");
5742311Sjkh		fclose(tmp);  unlink(tn);
5752311Sjkh		return (-2);
5762311Sjkh	}
5772311Sjkh
5782311Sjkh#ifdef HAS_FCHMOD
5792311Sjkh	if (fchmod(fileno(tmp), 0600) < OK)
5802311Sjkh#else
5812311Sjkh	if (chmod(tn, 0600) < OK)
5822311Sjkh#endif
5832311Sjkh	{
58429452Scharnier		warn("chown");
5852311Sjkh		fclose(tmp);  unlink(tn);
5862311Sjkh		return (-2);
5872311Sjkh	}
5882311Sjkh
5892311Sjkh	if (fclose(tmp) == EOF) {
59029452Scharnier		warn("fclose");
5912311Sjkh		unlink(tn);
5922311Sjkh		return (-2);
5932311Sjkh	}
5942311Sjkh
5952311Sjkh	(void) sprintf(n, CRON_TAB(User));
5962311Sjkh	if (rename(tn, n)) {
59729452Scharnier		warn("error renaming %s to %s", tn, n);
5982311Sjkh		unlink(tn);
5992311Sjkh		return (-2);
6002311Sjkh	}
6012311Sjkh	log_it(RealUser, Pid, "REPLACE", User);
6022311Sjkh
6032311Sjkh	poke_daemon();
6042311Sjkh
6052311Sjkh	return (0);
6062311Sjkh}
6072311Sjkh
6082311Sjkh
6092311Sjkhstatic void
6102311Sjkhpoke_daemon() {
6112311Sjkh#ifdef USE_UTIMES
6122311Sjkh	struct timeval tvs[2];
6132311Sjkh	struct timezone tz;
6142311Sjkh
6152311Sjkh	(void) gettimeofday(&tvs[0], &tz);
6162311Sjkh	tvs[1] = tvs[0];
6172311Sjkh	if (utimes(SPOOL_DIR, tvs) < OK) {
61829452Scharnier		warn("can't update mtime on spooldir %s", SPOOL_DIR);
6192311Sjkh		return;
6202311Sjkh	}
6212311Sjkh#else
6222311Sjkh	if (utime(SPOOL_DIR, NULL) < OK) {
62329452Scharnier		warn("can't update mtime on spooldir %s", SPOOL_DIR);
6242311Sjkh		return;
6252311Sjkh	}
6262311Sjkh#endif /*USE_UTIMES*/
6272311Sjkh}
628