xref: /illumos-gate/usr/src/cmd/cron/crontab.c (revision 3010f05b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3010f05bSdp  * Common Development and Distribution License (the "License").
6*3010f05bSdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3010f05bSdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/wait.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <pwd.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <nl_types.h>
467c478bd9Sstevel@tonic-gate #include <langinfo.h>
477c478bd9Sstevel@tonic-gate #include <libintl.h>
487c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
497c478bd9Sstevel@tonic-gate #include "cron.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	TMPFILE		"_cron"		/* prefix for tmp file */
527c478bd9Sstevel@tonic-gate #define	CRMODE		0600	/* mode for creating crontabs */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	BADCREATE	\
557c478bd9Sstevel@tonic-gate 	"can't create your crontab file in the crontab directory."
567c478bd9Sstevel@tonic-gate #define	BADOPEN		"can't open your crontab file."
577c478bd9Sstevel@tonic-gate #define	BADSHELL	\
587c478bd9Sstevel@tonic-gate 	"because your login shell isn't /usr/bin/sh, you can't use cron."
597c478bd9Sstevel@tonic-gate #define	WARNSHELL	"warning: commands will be executed using /usr/bin/sh\n"
607c478bd9Sstevel@tonic-gate #define	BADUSAGE	\
61*3010f05bSdp 	"usage:\n"			\
62*3010f05bSdp 	"\tcrontab [file]\n"		\
63*3010f05bSdp 	"\tcrontab -e [username]\n"	\
64*3010f05bSdp 	"\tcrontab -l [username]\n"	\
65*3010f05bSdp 	"\tcrontab -r [username]"
667c478bd9Sstevel@tonic-gate #define	INVALIDUSER	"you are not a valid user (no entry in /etc/passwd)."
677c478bd9Sstevel@tonic-gate #define	NOTALLOWED	"you are not authorized to use cron.  Sorry."
687c478bd9Sstevel@tonic-gate #define	NOTROOT		\
697c478bd9Sstevel@tonic-gate 	"you must be super-user to access another user's crontab file"
707c478bd9Sstevel@tonic-gate #define	AUDITREJECT	"The audit context for your shell has not been set."
717c478bd9Sstevel@tonic-gate #define	EOLN		"unexpected end of line."
727c478bd9Sstevel@tonic-gate #define	UNEXPECT	"unexpected character found in line."
737c478bd9Sstevel@tonic-gate #define	OUTOFBOUND	"number out of bounds."
747c478bd9Sstevel@tonic-gate #define	ERRSFND		"errors detected in input, no crontab file generated."
757c478bd9Sstevel@tonic-gate #define	ED_ERROR	\
767c478bd9Sstevel@tonic-gate 	"     The editor indicates that an error occurred while you were\n"\
777c478bd9Sstevel@tonic-gate 	"     editing the crontab data - usually a minor typing error.\n\n"
787c478bd9Sstevel@tonic-gate #define	BADREAD		"error reading your crontab file"
797c478bd9Sstevel@tonic-gate #define	ED_PROMPT	\
807c478bd9Sstevel@tonic-gate 	"     Edit again, to ensure crontab information is intact (%c/%c)?\n"\
817c478bd9Sstevel@tonic-gate 	"     ('%c' will discard edits.)"
827c478bd9Sstevel@tonic-gate #define	NAMETOOLONG	"login name too long"
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate extern int	per_errno;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate extern int	audit_crontab_modify(char *, char *, int);
877c478bd9Sstevel@tonic-gate extern int	audit_crontab_delete(char *, int);
887c478bd9Sstevel@tonic-gate extern int	audit_crontab_not_allowed(uid_t, char *);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int		err;
917c478bd9Sstevel@tonic-gate int		cursor;
927c478bd9Sstevel@tonic-gate char		*cf;
937c478bd9Sstevel@tonic-gate char		*tnam;
947c478bd9Sstevel@tonic-gate char		edtemp[5+13+1];
957c478bd9Sstevel@tonic-gate char		line[CTLINESIZE];
967c478bd9Sstevel@tonic-gate static		char	login[UNAMESIZE];
977c478bd9Sstevel@tonic-gate static		char	yeschr;
987c478bd9Sstevel@tonic-gate static		char	nochr;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int yes(void);
1017c478bd9Sstevel@tonic-gate static int next_field(int, int);
1027c478bd9Sstevel@tonic-gate static void catch(int);
1037c478bd9Sstevel@tonic-gate static void crabort(char *);
1047c478bd9Sstevel@tonic-gate static void cerror(char *);
1057c478bd9Sstevel@tonic-gate static void copycron(FILE *);
1067c478bd9Sstevel@tonic-gate 
107032624d5Sbasabi int
108032624d5Sbasabi main(int argc, char **argv)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	int	c, r;
1117c478bd9Sstevel@tonic-gate 	int	rflag	= 0;
1127c478bd9Sstevel@tonic-gate 	int	lflag	= 0;
1137c478bd9Sstevel@tonic-gate 	int	eflag	= 0;
1147c478bd9Sstevel@tonic-gate 	int	errflg	= 0;
1157c478bd9Sstevel@tonic-gate 	char *pp;
1167c478bd9Sstevel@tonic-gate 	FILE *fp, *tmpfp;
1177c478bd9Sstevel@tonic-gate 	struct stat stbuf;
1187c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
1197c478bd9Sstevel@tonic-gate 	time_t omodtime;
1207c478bd9Sstevel@tonic-gate 	char *editor;
1217c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
1227c478bd9Sstevel@tonic-gate 	uid_t ruid;
1237c478bd9Sstevel@tonic-gate 	pid_t pid;
1247c478bd9Sstevel@tonic-gate 	int stat_loc;
1257c478bd9Sstevel@tonic-gate 	int ret;
1267c478bd9Sstevel@tonic-gate 	char real_login[UNAMESIZE];
1277c478bd9Sstevel@tonic-gate 	int tmpfd = -1;
1287c478bd9Sstevel@tonic-gate 	pam_handle_t *pamh;
1297c478bd9Sstevel@tonic-gate 	int pam_error;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1327c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1337c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1367c478bd9Sstevel@tonic-gate 	yeschr = *nl_langinfo(YESSTR);
1377c478bd9Sstevel@tonic-gate 	nochr = *nl_langinfo(NOSTR);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "elr")) != EOF)
1407c478bd9Sstevel@tonic-gate 		switch (c) {
1417c478bd9Sstevel@tonic-gate 			case 'e':
1427c478bd9Sstevel@tonic-gate 				eflag++;
1437c478bd9Sstevel@tonic-gate 				break;
1447c478bd9Sstevel@tonic-gate 			case 'l':
1457c478bd9Sstevel@tonic-gate 				lflag++;
1467c478bd9Sstevel@tonic-gate 				break;
1477c478bd9Sstevel@tonic-gate 			case 'r':
1487c478bd9Sstevel@tonic-gate 				rflag++;
1497c478bd9Sstevel@tonic-gate 				break;
1507c478bd9Sstevel@tonic-gate 			case '?':
1517c478bd9Sstevel@tonic-gate 				errflg++;
1527c478bd9Sstevel@tonic-gate 				break;
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (eflag + lflag + rflag > 1)
1567c478bd9Sstevel@tonic-gate 		errflg++;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	argc -= optind;
1597c478bd9Sstevel@tonic-gate 	argv += optind;
1607c478bd9Sstevel@tonic-gate 	if (errflg || argc > 1)
1617c478bd9Sstevel@tonic-gate 		crabort(BADUSAGE);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	ruid = getuid();
1647c478bd9Sstevel@tonic-gate 	if ((pwp = getpwuid(ruid)) == NULL)
1657c478bd9Sstevel@tonic-gate 		crabort(INVALIDUSER);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (strlcpy(real_login, pwp->pw_name, sizeof (real_login))
1687c478bd9Sstevel@tonic-gate 	    >= sizeof (real_login))
1697c478bd9Sstevel@tonic-gate 		crabort(NAMETOOLONG);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if ((eflag || lflag || rflag) && argc == 1) {
1727c478bd9Sstevel@tonic-gate 		if ((pwp = getpwnam(*argv)) == NULL)
1737c478bd9Sstevel@tonic-gate 			crabort(INVALIDUSER);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (!chkauthattr(CRONADMIN_AUTH, real_login)) {
1767c478bd9Sstevel@tonic-gate 			if (pwp->pw_uid != ruid)
1777c478bd9Sstevel@tonic-gate 				crabort(NOTROOT);
1787c478bd9Sstevel@tonic-gate 			else
1797c478bd9Sstevel@tonic-gate 				pp = getuser(ruid);
1807c478bd9Sstevel@tonic-gate 		} else
1817c478bd9Sstevel@tonic-gate 			pp = *argv++;
1827c478bd9Sstevel@tonic-gate 	} else {
1837c478bd9Sstevel@tonic-gate 		pp = getuser(ruid);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
1877c478bd9Sstevel@tonic-gate 		if (per_errno == 2)
1887c478bd9Sstevel@tonic-gate 			crabort(BADSHELL);
1897c478bd9Sstevel@tonic-gate 		else
1907c478bd9Sstevel@tonic-gate 			crabort(INVALIDUSER);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if (strlcpy(login, pp, sizeof (login)) >= sizeof (login))
1937c478bd9Sstevel@tonic-gate 		crabort(NAMETOOLONG);
1947c478bd9Sstevel@tonic-gate 	if (!allowed(login, CRONALLOW, CRONDENY))
1957c478bd9Sstevel@tonic-gate 		crabort(NOTALLOWED);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Do account validation check */
1987c478bd9Sstevel@tonic-gate 	pam_error = pam_start("cron", pp, NULL, &pamh);
1997c478bd9Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
2007c478bd9Sstevel@tonic-gate 		crabort((char *)pam_strerror(pamh, pam_error));
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	pam_error = pam_acct_mgmt(pamh, PAM_SILENT);
2037c478bd9Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
2047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning - Invalid account: "
2057c478bd9Sstevel@tonic-gate 		    "'%s' not allowed to execute cronjobs\n"), pp);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* check for unaudited shell */
2117c478bd9Sstevel@tonic-gate 	if (audit_crontab_not_allowed(ruid, pp))
2127c478bd9Sstevel@tonic-gate 		crabort(AUDITREJECT);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	cf = xmalloc(strlen(CRONDIR)+strlen(login)+2);
2157c478bd9Sstevel@tonic-gate 	strcat(strcat(strcpy(cf, CRONDIR), "/"), login);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (rflag) {
2187c478bd9Sstevel@tonic-gate 		r = unlink(cf);
2197c478bd9Sstevel@tonic-gate 		cron_sendmsg(DELETE, login, login, CRON);
2207c478bd9Sstevel@tonic-gate 		audit_crontab_delete(cf, r);
2217c478bd9Sstevel@tonic-gate 		exit(0);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	if (lflag) {
2247c478bd9Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL)
2257c478bd9Sstevel@tonic-gate 			crabort(BADOPEN);
2267c478bd9Sstevel@tonic-gate 		while (fgets(line, CTLINESIZE, fp) != NULL)
2277c478bd9Sstevel@tonic-gate 			fputs(line, stdout);
2287c478bd9Sstevel@tonic-gate 		fclose(fp);
2297c478bd9Sstevel@tonic-gate 		exit(0);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	if (eflag) {
2327c478bd9Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL) {
2337c478bd9Sstevel@tonic-gate 			if (errno != ENOENT)
2347c478bd9Sstevel@tonic-gate 				crabort(BADOPEN);
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 		(void) strcpy(edtemp, "/tmp/crontabXXXXXX");
2377c478bd9Sstevel@tonic-gate 		tmpfd = mkstemp(edtemp);
2387c478bd9Sstevel@tonic-gate 		if (fchown(tmpfd, ruid, -1) == -1) {
2397c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
2407c478bd9Sstevel@tonic-gate 			crabort("fchown of temporary file failed");
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		(void) close(tmpfd);
2437c478bd9Sstevel@tonic-gate 		/*
2447c478bd9Sstevel@tonic-gate 		 * Fork off a child with user's permissions,
2457c478bd9Sstevel@tonic-gate 		 * to edit the crontab file
2467c478bd9Sstevel@tonic-gate 		 */
2477c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)-1)
2487c478bd9Sstevel@tonic-gate 			crabort("fork failed");
2497c478bd9Sstevel@tonic-gate 		if (pid == 0) {		/* child process */
2507c478bd9Sstevel@tonic-gate 			/* give up super-user privileges. */
2517c478bd9Sstevel@tonic-gate 			setuid(ruid);
2527c478bd9Sstevel@tonic-gate 			if ((tmpfp = fopen(edtemp, "w")) == NULL)
2537c478bd9Sstevel@tonic-gate 				crabort("can't create temporary file");
2547c478bd9Sstevel@tonic-gate 			if (fp != NULL) {
2557c478bd9Sstevel@tonic-gate 				/*
2567c478bd9Sstevel@tonic-gate 				 * Copy user's crontab file to temporary file.
2577c478bd9Sstevel@tonic-gate 				 */
2587c478bd9Sstevel@tonic-gate 				while (fgets(line, CTLINESIZE, fp) != NULL) {
2597c478bd9Sstevel@tonic-gate 					fputs(line, tmpfp);
2607c478bd9Sstevel@tonic-gate 					if (ferror(tmpfp)) {
2617c478bd9Sstevel@tonic-gate 						fclose(fp);
2627c478bd9Sstevel@tonic-gate 						fclose(tmpfp);
2637c478bd9Sstevel@tonic-gate 						crabort("write error on"
2647c478bd9Sstevel@tonic-gate 						    "temporary file");
2657c478bd9Sstevel@tonic-gate 					}
2667c478bd9Sstevel@tonic-gate 				}
2677c478bd9Sstevel@tonic-gate 				if (ferror(fp)) {
2687c478bd9Sstevel@tonic-gate 					fclose(fp);
2697c478bd9Sstevel@tonic-gate 					fclose(tmpfp);
2707c478bd9Sstevel@tonic-gate 					crabort(BADREAD);
2717c478bd9Sstevel@tonic-gate 				}
2727c478bd9Sstevel@tonic-gate 				fclose(fp);
2737c478bd9Sstevel@tonic-gate 			}
2747c478bd9Sstevel@tonic-gate 			if (fclose(tmpfp) == EOF)
2757c478bd9Sstevel@tonic-gate 				crabort("write error on temporary file");
2767c478bd9Sstevel@tonic-gate 			if (stat(edtemp, &stbuf) < 0)
2777c478bd9Sstevel@tonic-gate 				crabort("can't stat temporary file");
2787c478bd9Sstevel@tonic-gate 			omodtime = stbuf.st_mtime;
2797c478bd9Sstevel@tonic-gate 			editor = getenv("VISUAL");
2807c478bd9Sstevel@tonic-gate 			if (editor == NULL)
2817c478bd9Sstevel@tonic-gate 				editor = getenv("EDITOR");
2827c478bd9Sstevel@tonic-gate 			if (editor == NULL)
2837c478bd9Sstevel@tonic-gate 				editor = "ed";
2847c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf),
2857c478bd9Sstevel@tonic-gate 				"%s %s", editor, edtemp);
2867c478bd9Sstevel@tonic-gate 			sleep(1);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 			while (1) {
2897c478bd9Sstevel@tonic-gate 				ret = system(buf);
2907c478bd9Sstevel@tonic-gate 				/* sanity checks */
2917c478bd9Sstevel@tonic-gate 				if ((tmpfp = fopen(edtemp, "r")) == NULL)
2927c478bd9Sstevel@tonic-gate 				    crabort("can't open temporary file");
2937c478bd9Sstevel@tonic-gate 				if (fstat(fileno(tmpfp), &stbuf) < 0)
2947c478bd9Sstevel@tonic-gate 				    crabort("can't stat temporary file");
2957c478bd9Sstevel@tonic-gate 				if (stbuf.st_size == 0)
2967c478bd9Sstevel@tonic-gate 				    crabort("temporary file empty");
2977c478bd9Sstevel@tonic-gate 				if (omodtime == stbuf.st_mtime) {
2987c478bd9Sstevel@tonic-gate 				    (void) unlink(edtemp);
2997c478bd9Sstevel@tonic-gate 				    fprintf(stderr, gettext(
3007c478bd9Sstevel@tonic-gate 					"The crontab file was not changed.\n"));
3017c478bd9Sstevel@tonic-gate 				    exit(1);
3027c478bd9Sstevel@tonic-gate 				}
3037c478bd9Sstevel@tonic-gate 				if ((ret) && (errno != EINTR)) {
3047c478bd9Sstevel@tonic-gate 				/*
3057c478bd9Sstevel@tonic-gate 				 * Some editors (like 'vi') can return
3067c478bd9Sstevel@tonic-gate 				 * a non-zero exit status even though
3077c478bd9Sstevel@tonic-gate 				 * everything is okay. Need to check.
3087c478bd9Sstevel@tonic-gate 				 */
3097c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext(ED_ERROR));
3107c478bd9Sstevel@tonic-gate 				fflush(stderr);
3117c478bd9Sstevel@tonic-gate 				if (isatty(fileno(stdin))) {
3127c478bd9Sstevel@tonic-gate 				    /* Interactive */
3137c478bd9Sstevel@tonic-gate 					fprintf(stdout, gettext(ED_PROMPT),
3147c478bd9Sstevel@tonic-gate 					    yeschr, nochr, nochr);
3157c478bd9Sstevel@tonic-gate 					fflush(stdout);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 					if (yes()) {
3187c478bd9Sstevel@tonic-gate 						/* Edit again */
3197c478bd9Sstevel@tonic-gate 						continue;
3207c478bd9Sstevel@tonic-gate 					} else {
3217c478bd9Sstevel@tonic-gate 						/* Dump changes */
3227c478bd9Sstevel@tonic-gate 						(void) unlink(edtemp);
3237c478bd9Sstevel@tonic-gate 						exit(1);
3247c478bd9Sstevel@tonic-gate 					}
3257c478bd9Sstevel@tonic-gate 				} else {
3267c478bd9Sstevel@tonic-gate 				    /* Non-interactive, dump changes */
3277c478bd9Sstevel@tonic-gate 				    (void) unlink(edtemp);
3287c478bd9Sstevel@tonic-gate 				    exit(1);
3297c478bd9Sstevel@tonic-gate 				}
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			exit(0);
3327c478bd9Sstevel@tonic-gate 			} /* while (1) */
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		/* fix for 1125555 - ignore common signals while waiting */
3367c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
3377c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
3387c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_IGN);
3397c478bd9Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
3407c478bd9Sstevel@tonic-gate 		wait(&stat_loc);
3417c478bd9Sstevel@tonic-gate 		if ((stat_loc & 0xFF00) != 0)
3427c478bd9Sstevel@tonic-gate 			exit(1);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		if ((seteuid(ruid) < 0) ||
3457c478bd9Sstevel@tonic-gate 		    ((tmpfp = fopen(edtemp, "r")) == NULL)) {
3467c478bd9Sstevel@tonic-gate 			fprintf(stderr, "crontab: %s: %s\n",
3477c478bd9Sstevel@tonic-gate 			    edtemp, errmsg(errno));
3487c478bd9Sstevel@tonic-gate 			(void) unlink(edtemp);
3497c478bd9Sstevel@tonic-gate 			exit(1);
3507c478bd9Sstevel@tonic-gate 		} else
3517c478bd9Sstevel@tonic-gate 			seteuid(0);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		copycron(tmpfp);
3547c478bd9Sstevel@tonic-gate 		(void) unlink(edtemp);
3557c478bd9Sstevel@tonic-gate 	} else {
3567c478bd9Sstevel@tonic-gate 		if (argc == 0)
3577c478bd9Sstevel@tonic-gate 			copycron(stdin);
3587c478bd9Sstevel@tonic-gate 		else if (seteuid(getuid()) != 0 || (fp = fopen(argv[0], "r"))
3597c478bd9Sstevel@tonic-gate 		    == NULL)
3607c478bd9Sstevel@tonic-gate 			crabort(BADOPEN);
3617c478bd9Sstevel@tonic-gate 		else {
3627c478bd9Sstevel@tonic-gate 			seteuid(0);
3637c478bd9Sstevel@tonic-gate 			copycron(fp);
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	cron_sendmsg(ADD, login, login, CRON);
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate  *	if (per_errno == 2)
3697c478bd9Sstevel@tonic-gate  *		fprintf(stderr, gettext(WARNSHELL));
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate 	return (0);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static void
3757c478bd9Sstevel@tonic-gate copycron(fp)
3767c478bd9Sstevel@tonic-gate FILE *fp;
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	FILE *tfp;
3797c478bd9Sstevel@tonic-gate 	char pid[6], *tnam_end;
3807c478bd9Sstevel@tonic-gate 	int t;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	sprintf(pid, "%-5d", getpid());
3837c478bd9Sstevel@tonic-gate 	tnam = xmalloc(strlen(CRONDIR)+strlen(TMPFILE)+7);
3847c478bd9Sstevel@tonic-gate 	strcat(strcat(strcat(strcpy(tnam, CRONDIR), "/"), TMPFILE), pid);
3857c478bd9Sstevel@tonic-gate 	/* cut trailing blanks */
3867c478bd9Sstevel@tonic-gate 	tnam_end = strchr(tnam, ' ');
3877c478bd9Sstevel@tonic-gate 	if (tnam_end != NULL)
3887c478bd9Sstevel@tonic-gate 		*tnam_end = 0;
3897c478bd9Sstevel@tonic-gate 	/* catch SIGINT, SIGHUP, SIGQUIT signals */
3907c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, catch) == SIG_IGN)
3917c478bd9Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
3927c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, catch) == SIG_IGN) signal(SIGHUP, SIG_IGN);
3937c478bd9Sstevel@tonic-gate 	if (signal(SIGQUIT, catch) == SIG_IGN) signal(SIGQUIT, SIG_IGN);
3947c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, catch) == SIG_IGN) signal(SIGTERM, SIG_IGN);
3957c478bd9Sstevel@tonic-gate 	if ((t = creat(tnam, CRMODE)) == -1) crabort(BADCREATE);
3967c478bd9Sstevel@tonic-gate 	if ((tfp = fdopen(t, "w")) == NULL) {
3977c478bd9Sstevel@tonic-gate 		unlink(tnam);
3987c478bd9Sstevel@tonic-gate 		crabort(BADCREATE);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	err = 0;	/* if errors found, err set to 1 */
4017c478bd9Sstevel@tonic-gate 	while (fgets(line, CTLINESIZE, fp) != NULL) {
4027c478bd9Sstevel@tonic-gate 		cursor = 0;
4037c478bd9Sstevel@tonic-gate 		while (line[cursor] == ' ' || line[cursor] == '\t')
4047c478bd9Sstevel@tonic-gate 			cursor++;
4057c478bd9Sstevel@tonic-gate 		/* fix for 1039689 - treat blank line like a comment */
4067c478bd9Sstevel@tonic-gate 		if (line[cursor] == '#' || line[cursor] == '\n')
4077c478bd9Sstevel@tonic-gate 			goto cont;
4087c478bd9Sstevel@tonic-gate 		if (next_field(0, 59)) continue;
4097c478bd9Sstevel@tonic-gate 		if (next_field(0, 23)) continue;
4107c478bd9Sstevel@tonic-gate 		if (next_field(1, 31)) continue;
4117c478bd9Sstevel@tonic-gate 		if (next_field(1, 12)) continue;
4127c478bd9Sstevel@tonic-gate 		if (next_field(0, 06)) continue;
4137c478bd9Sstevel@tonic-gate 		if (line[++cursor] == '\0') {
4147c478bd9Sstevel@tonic-gate 			cerror(EOLN);
4157c478bd9Sstevel@tonic-gate 			continue;
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate cont:
4187c478bd9Sstevel@tonic-gate 		if (fputs(line, tfp) == EOF) {
4197c478bd9Sstevel@tonic-gate 			unlink(tnam);
4207c478bd9Sstevel@tonic-gate 			crabort(BADCREATE);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	fclose(fp);
4247c478bd9Sstevel@tonic-gate 	fclose(tfp);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/* audit differences between old and new crontabs */
4277c478bd9Sstevel@tonic-gate 	audit_crontab_modify(cf, tnam, err);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (!err) {
4307c478bd9Sstevel@tonic-gate 		/* make file tfp the new crontab */
4317c478bd9Sstevel@tonic-gate 		unlink(cf);
4327c478bd9Sstevel@tonic-gate 		if (link(tnam, cf) == -1) {
4337c478bd9Sstevel@tonic-gate 			unlink(tnam);
4347c478bd9Sstevel@tonic-gate 			crabort(BADCREATE);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 	} else
4377c478bd9Sstevel@tonic-gate 		fprintf(stderr, "crontab: %s\n", gettext(ERRSFND));
4387c478bd9Sstevel@tonic-gate 	unlink(tnam);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static int
4427c478bd9Sstevel@tonic-gate next_field(lower, upper)
4437c478bd9Sstevel@tonic-gate int lower, upper;
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	int num, num2;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	while ((line[cursor] == ' ') || (line[cursor] == '\t')) cursor++;
4487c478bd9Sstevel@tonic-gate 	if (line[cursor] == '\0') {
4497c478bd9Sstevel@tonic-gate 		cerror(EOLN);
4507c478bd9Sstevel@tonic-gate 		return (1);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	if (line[cursor] == '*') {
4537c478bd9Sstevel@tonic-gate 		cursor++;
4547c478bd9Sstevel@tonic-gate 		if ((line[cursor] != ' ') && (line[cursor] != '\t')) {
4557c478bd9Sstevel@tonic-gate 			cerror(UNEXPECT);
4567c478bd9Sstevel@tonic-gate 			return (1);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		return (0);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	while (TRUE) {
4617c478bd9Sstevel@tonic-gate 		if (!isdigit(line[cursor])) {
4627c478bd9Sstevel@tonic-gate 			cerror(UNEXPECT);
4637c478bd9Sstevel@tonic-gate 			return (1);
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 		num = 0;
4667c478bd9Sstevel@tonic-gate 		do {
4677c478bd9Sstevel@tonic-gate 			num = num*10 + (line[cursor]-'0');
4687c478bd9Sstevel@tonic-gate 		} while (isdigit(line[++cursor]));
4697c478bd9Sstevel@tonic-gate 		if ((num < lower) || (num > upper)) {
4707c478bd9Sstevel@tonic-gate 			cerror(OUTOFBOUND);
4717c478bd9Sstevel@tonic-gate 			return (1);
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 		if (line[cursor] == '-') {
4747c478bd9Sstevel@tonic-gate 			if (!isdigit(line[++cursor])) {
4757c478bd9Sstevel@tonic-gate 				cerror(UNEXPECT);
4767c478bd9Sstevel@tonic-gate 				return (1);
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 			num2 = 0;
4797c478bd9Sstevel@tonic-gate 			do {
4807c478bd9Sstevel@tonic-gate 				num2 = num2*10 + (line[cursor]-'0');
4817c478bd9Sstevel@tonic-gate 			} while (isdigit(line[++cursor]));
4827c478bd9Sstevel@tonic-gate 			if ((num2 < lower) || (num2 > upper)) {
4837c478bd9Sstevel@tonic-gate 				cerror(OUTOFBOUND);
4847c478bd9Sstevel@tonic-gate 				return (1);
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		if ((line[cursor] == ' ') || (line[cursor] == '\t')) break;
4887c478bd9Sstevel@tonic-gate 		if (line[cursor] == '\0') {
4897c478bd9Sstevel@tonic-gate 			cerror(EOLN);
4907c478bd9Sstevel@tonic-gate 			return (1);
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		if (line[cursor++] != ',') {
4937c478bd9Sstevel@tonic-gate 			cerror(UNEXPECT);
4947c478bd9Sstevel@tonic-gate 			return (1);
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 	return (0);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate static void
5017c478bd9Sstevel@tonic-gate cerror(msg)
5027c478bd9Sstevel@tonic-gate char *msg;
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%scrontab: error on previous line; %s\n"),
5057c478bd9Sstevel@tonic-gate 	    line, msg);
5067c478bd9Sstevel@tonic-gate 	err = 1;
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static void
5117c478bd9Sstevel@tonic-gate catch(int x)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	unlink(tnam);
5147c478bd9Sstevel@tonic-gate 	exit(1);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate static void
5187c478bd9Sstevel@tonic-gate crabort(msg)
5197c478bd9Sstevel@tonic-gate char *msg;
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	int sverrno;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (strcmp(edtemp, "") != 0) {
5247c478bd9Sstevel@tonic-gate 		sverrno = errno;
5257c478bd9Sstevel@tonic-gate 		(void) unlink(edtemp);
5267c478bd9Sstevel@tonic-gate 		errno = sverrno;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	if (tnam != NULL) {
5297c478bd9Sstevel@tonic-gate 		sverrno = errno;
5307c478bd9Sstevel@tonic-gate 		(void) unlink(tnam);
5317c478bd9Sstevel@tonic-gate 		errno = sverrno;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 	fprintf(stderr, "crontab: %s\n", gettext(msg));
5347c478bd9Sstevel@tonic-gate 	exit(1);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static int
5387c478bd9Sstevel@tonic-gate yes(void)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	int	first_char;
5417c478bd9Sstevel@tonic-gate 	int	dummy_char;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	first_char = dummy_char = getchar();
5447c478bd9Sstevel@tonic-gate 	while ((dummy_char != '\n')	&&
5457c478bd9Sstevel@tonic-gate 	    (dummy_char != '\0')	&&
5467c478bd9Sstevel@tonic-gate 	    (dummy_char != EOF))
5477c478bd9Sstevel@tonic-gate 		dummy_char = getchar();
5487c478bd9Sstevel@tonic-gate 	return (first_char == yeschr);
5497c478bd9Sstevel@tonic-gate }
550