xref: /illumos-gate/usr/src/cmd/cron/crontab.c (revision 618372bc)
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
53010f05bSdp  * Common Development and Distribution License (the "License").
63010f05bSdp  * 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 /*
22b0d0a1c8SViswanathan Kannappan  * Copyright 2009 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	*/
2619803d09SToomas Soome /*	  All Rights Reserved	*/
277c478bd9Sstevel@tonic-gate 
286b734416SAndy Fiddaman /*
296863ede2SAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
30*618372bcSSebastian Wiedenroth  * Copyright 2022 Sebastian Wiedenroth
316b734416SAndy Fiddaman  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/wait.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <fcntl.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <locale.h>
477c478bd9Sstevel@tonic-gate #include <nl_types.h>
487c478bd9Sstevel@tonic-gate #include <langinfo.h>
497c478bd9Sstevel@tonic-gate #include <libintl.h>
507c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
515b08e637SChris Gerhard #include <limits.h>
525b08e637SChris Gerhard #include <libzoneinfo.h>
537c478bd9Sstevel@tonic-gate #include "cron.h"
543d63ea05Sas #include "getresponse.h"
557c478bd9Sstevel@tonic-gate 
56d61d085dScf #if defined(XPG4)
57d61d085dScf #define	VIPATH	"/usr/xpg4/bin/vi"
58d61d085dScf #elif defined(XPG6)
59d61d085dScf #define	VIPATH	"/usr/xpg6/bin/vi"
60d61d085dScf #else
61d61d085dScf #define	_XPG_NOTDEFINED
62d61d085dScf #define	VIPATH	"vi"
63d61d085dScf #endif
64d61d085dScf 
657c478bd9Sstevel@tonic-gate #define	TMPFILE		"_cron"		/* prefix for tmp file */
667c478bd9Sstevel@tonic-gate #define	CRMODE		0600	/* mode for creating crontabs */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	BADCREATE	\
697c478bd9Sstevel@tonic-gate 	"can't create your crontab file in the crontab directory."
707c478bd9Sstevel@tonic-gate #define	BADOPEN		"can't open your crontab file."
717c478bd9Sstevel@tonic-gate #define	BADSHELL	\
727c478bd9Sstevel@tonic-gate 	"because your login shell isn't /usr/bin/sh, you can't use cron."
737c478bd9Sstevel@tonic-gate #define	WARNSHELL	"warning: commands will be executed using /usr/bin/sh\n"
747c478bd9Sstevel@tonic-gate #define	BADUSAGE	\
753010f05bSdp 	"usage:\n"			\
766863ede2SAndy Fiddaman 	"\tcrontab [-u username] [file]\n"		\
776863ede2SAndy Fiddaman 	"\tcrontab [-u username] { -e | -l | -r }\n"	\
786863ede2SAndy Fiddaman 	"\tcrontab { -e | -l | -r } [username]"
797c478bd9Sstevel@tonic-gate #define	INVALIDUSER	"you are not a valid user (no entry in /etc/passwd)."
807c478bd9Sstevel@tonic-gate #define	NOTALLOWED	"you are not authorized to use cron.  Sorry."
817c478bd9Sstevel@tonic-gate #define	NOTROOT		\
827c478bd9Sstevel@tonic-gate 	"you must be super-user to access another user's crontab file"
837c478bd9Sstevel@tonic-gate #define	AUDITREJECT	"The audit context for your shell has not been set."
847c478bd9Sstevel@tonic-gate #define	EOLN		"unexpected end of line."
857c478bd9Sstevel@tonic-gate #define	UNEXPECT	"unexpected character found in line."
867c478bd9Sstevel@tonic-gate #define	OUTOFBOUND	"number out of bounds."
876b734416SAndy Fiddaman #define	OVERFLOW	"too many elements."
887c478bd9Sstevel@tonic-gate #define	ERRSFND		"errors detected in input, no crontab file generated."
897c478bd9Sstevel@tonic-gate #define	ED_ERROR	\
907c478bd9Sstevel@tonic-gate 	"     The editor indicates that an error occurred while you were\n"\
917c478bd9Sstevel@tonic-gate 	"     editing the crontab data - usually a minor typing error.\n\n"
927c478bd9Sstevel@tonic-gate #define	BADREAD		"error reading your crontab file"
937c478bd9Sstevel@tonic-gate #define	ED_PROMPT	\
943d63ea05Sas 	"     Edit again, to ensure crontab information is intact (%s/%s)?\n"\
953d63ea05Sas 	"     ('%s' will discard edits.)"
967c478bd9Sstevel@tonic-gate #define	NAMETOOLONG	"login name too long"
975b08e637SChris Gerhard #define	BAD_TZ	"Timezone unrecognized in: %s"
985b08e637SChris Gerhard #define	BAD_SHELL	"Invalid shell specified: %s"
995b08e637SChris Gerhard #define	BAD_HOME	"Unable to access directory: %s\t%s\n"
100*618372bcSSebastian Wiedenroth #define	BAD_RAND_DELAY	"Invalid delay: %s"
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate extern int	per_errno;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate extern int	audit_crontab_modify(char *, char *, int);
1057c478bd9Sstevel@tonic-gate extern int	audit_crontab_delete(char *, int);
1067c478bd9Sstevel@tonic-gate extern int	audit_crontab_not_allowed(uid_t, char *);
1077c478bd9Sstevel@tonic-gate 
108f95cbc2eSToomas Soome static int	err;
1097c478bd9Sstevel@tonic-gate int		cursor;
1107c478bd9Sstevel@tonic-gate char		*cf;
1117c478bd9Sstevel@tonic-gate char		*tnam;
1127c478bd9Sstevel@tonic-gate char		edtemp[5+13+1];
1137c478bd9Sstevel@tonic-gate char		line[CTLINESIZE];
1147c478bd9Sstevel@tonic-gate static		char	login[UNAMESIZE];
1157c478bd9Sstevel@tonic-gate 
1163d63ea05Sas static void	catch(int);
1173d63ea05Sas static void	crabort(char *);
1183d63ea05Sas static void	cerror(char *);
1193d63ea05Sas static void	copycron(FILE *);
1207c478bd9Sstevel@tonic-gate 
121032624d5Sbasabi int
main(int argc,char ** argv)122032624d5Sbasabi main(int argc, char **argv)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	int	c, r;
1257c478bd9Sstevel@tonic-gate 	int	rflag	= 0;
1267c478bd9Sstevel@tonic-gate 	int	lflag	= 0;
1277c478bd9Sstevel@tonic-gate 	int	eflag	= 0;
1287c478bd9Sstevel@tonic-gate 	int	errflg	= 0;
1297c478bd9Sstevel@tonic-gate 	char *pp;
1307c478bd9Sstevel@tonic-gate 	FILE *fp, *tmpfp;
1317c478bd9Sstevel@tonic-gate 	struct stat stbuf;
1327c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
1337c478bd9Sstevel@tonic-gate 	time_t omodtime;
1347c478bd9Sstevel@tonic-gate 	char *editor;
1357c478bd9Sstevel@tonic-gate 	uid_t ruid;
1367c478bd9Sstevel@tonic-gate 	pid_t pid;
1377c478bd9Sstevel@tonic-gate 	int stat_loc;
1387c478bd9Sstevel@tonic-gate 	int ret;
1397c478bd9Sstevel@tonic-gate 	char real_login[UNAMESIZE];
1406863ede2SAndy Fiddaman 	char *user = NULL;
1417c478bd9Sstevel@tonic-gate 	int tmpfd = -1;
1427c478bd9Sstevel@tonic-gate 	pam_handle_t *pamh;
1437c478bd9Sstevel@tonic-gate 	int pam_error;
14478ae324cSSumanth Naropanth 	char *buf;
14578ae324cSSumanth Naropanth 	size_t buflen;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1487c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1496863ede2SAndy Fiddaman #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1523d63ea05Sas 
1533d63ea05Sas 	if (init_yes() < 0) {
1543d63ea05Sas 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
1553d63ea05Sas 		    strerror(errno));
1563d63ea05Sas 		exit(1);
1573d63ea05Sas 	}
1587c478bd9Sstevel@tonic-gate 
1596863ede2SAndy Fiddaman 	while ((c = getopt(argc, argv, "elru:")) != EOF) {
1607c478bd9Sstevel@tonic-gate 		switch (c) {
1617c478bd9Sstevel@tonic-gate 			case 'e':
1627c478bd9Sstevel@tonic-gate 				eflag++;
1637c478bd9Sstevel@tonic-gate 				break;
1647c478bd9Sstevel@tonic-gate 			case 'l':
1657c478bd9Sstevel@tonic-gate 				lflag++;
1667c478bd9Sstevel@tonic-gate 				break;
1677c478bd9Sstevel@tonic-gate 			case 'r':
1687c478bd9Sstevel@tonic-gate 				rflag++;
1697c478bd9Sstevel@tonic-gate 				break;
1706863ede2SAndy Fiddaman 			case 'u':
1716863ede2SAndy Fiddaman 				user = optarg;
1726863ede2SAndy Fiddaman 				break;
1737c478bd9Sstevel@tonic-gate 			case '?':
1747c478bd9Sstevel@tonic-gate 				errflg++;
1757c478bd9Sstevel@tonic-gate 				break;
1767c478bd9Sstevel@tonic-gate 		}
1776863ede2SAndy Fiddaman 	}
1786863ede2SAndy Fiddaman 
1796863ede2SAndy Fiddaman 	argc -= optind;
1806863ede2SAndy Fiddaman 	argv += optind;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (eflag + lflag + rflag > 1)
1837c478bd9Sstevel@tonic-gate 		errflg++;
1847c478bd9Sstevel@tonic-gate 
1856863ede2SAndy Fiddaman 	if ((eflag || lflag || rflag) && argc > 0) {
1866863ede2SAndy Fiddaman 		if (user != NULL)
1876863ede2SAndy Fiddaman 			errflg++;
1886863ede2SAndy Fiddaman 		else
1896863ede2SAndy Fiddaman 			user = *argv;
1906863ede2SAndy Fiddaman 	}
1916863ede2SAndy Fiddaman 
1927c478bd9Sstevel@tonic-gate 	if (errflg || argc > 1)
1937c478bd9Sstevel@tonic-gate 		crabort(BADUSAGE);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	ruid = getuid();
1967c478bd9Sstevel@tonic-gate 	if ((pwp = getpwuid(ruid)) == NULL)
1977c478bd9Sstevel@tonic-gate 		crabort(INVALIDUSER);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (strlcpy(real_login, pwp->pw_name, sizeof (real_login))
2006863ede2SAndy Fiddaman 	    >= sizeof (real_login)) {
2017c478bd9Sstevel@tonic-gate 		crabort(NAMETOOLONG);
2026863ede2SAndy Fiddaman 	}
2037c478bd9Sstevel@tonic-gate 
2046863ede2SAndy Fiddaman 	if (user != NULL) {
2056863ede2SAndy Fiddaman 		if ((pwp = getpwnam(user)) == NULL)
2067c478bd9Sstevel@tonic-gate 			crabort(INVALIDUSER);
2077c478bd9Sstevel@tonic-gate 
208d1419d5aSNobutomo Nakano 		if (!cron_admin(real_login)) {
2097c478bd9Sstevel@tonic-gate 			if (pwp->pw_uid != ruid)
2107c478bd9Sstevel@tonic-gate 				crabort(NOTROOT);
2117c478bd9Sstevel@tonic-gate 			else
2127c478bd9Sstevel@tonic-gate 				pp = getuser(ruid);
2136863ede2SAndy Fiddaman 		} else {
2146863ede2SAndy Fiddaman 			pp = user;
2156863ede2SAndy Fiddaman 		}
2167c478bd9Sstevel@tonic-gate 	} else {
2177c478bd9Sstevel@tonic-gate 		pp = getuser(ruid);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
2217c478bd9Sstevel@tonic-gate 		if (per_errno == 2)
2227c478bd9Sstevel@tonic-gate 			crabort(BADSHELL);
2237c478bd9Sstevel@tonic-gate 		else
2247c478bd9Sstevel@tonic-gate 			crabort(INVALIDUSER);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	if (strlcpy(login, pp, sizeof (login)) >= sizeof (login))
2277c478bd9Sstevel@tonic-gate 		crabort(NAMETOOLONG);
2287c478bd9Sstevel@tonic-gate 	if (!allowed(login, CRONALLOW, CRONDENY))
2297c478bd9Sstevel@tonic-gate 		crabort(NOTALLOWED);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/* Do account validation check */
2327c478bd9Sstevel@tonic-gate 	pam_error = pam_start("cron", pp, NULL, &pamh);
2337c478bd9Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
2347c478bd9Sstevel@tonic-gate 		crabort((char *)pam_strerror(pamh, pam_error));
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	pam_error = pam_acct_mgmt(pamh, PAM_SILENT);
2377c478bd9Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
2387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning - Invalid account: "
2397c478bd9Sstevel@tonic-gate 		    "'%s' not allowed to execute cronjobs\n"), pp);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* check for unaudited shell */
2457c478bd9Sstevel@tonic-gate 	if (audit_crontab_not_allowed(ruid, pp))
2467c478bd9Sstevel@tonic-gate 		crabort(AUDITREJECT);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	cf = xmalloc(strlen(CRONDIR)+strlen(login)+2);
2497c478bd9Sstevel@tonic-gate 	strcat(strcat(strcpy(cf, CRONDIR), "/"), login);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (rflag) {
2527c478bd9Sstevel@tonic-gate 		r = unlink(cf);
2537c478bd9Sstevel@tonic-gate 		cron_sendmsg(DELETE, login, login, CRON);
2547c478bd9Sstevel@tonic-gate 		audit_crontab_delete(cf, r);
2557c478bd9Sstevel@tonic-gate 		exit(0);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 	if (lflag) {
2587c478bd9Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL)
2597c478bd9Sstevel@tonic-gate 			crabort(BADOPEN);
2607c478bd9Sstevel@tonic-gate 		while (fgets(line, CTLINESIZE, fp) != NULL)
2617c478bd9Sstevel@tonic-gate 			fputs(line, stdout);
2627c478bd9Sstevel@tonic-gate 		fclose(fp);
2637c478bd9Sstevel@tonic-gate 		exit(0);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	if (eflag) {
2667c478bd9Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL) {
2677c478bd9Sstevel@tonic-gate 			if (errno != ENOENT)
2687c478bd9Sstevel@tonic-gate 				crabort(BADOPEN);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		(void) strcpy(edtemp, "/tmp/crontabXXXXXX");
2717c478bd9Sstevel@tonic-gate 		tmpfd = mkstemp(edtemp);
2727c478bd9Sstevel@tonic-gate 		if (fchown(tmpfd, ruid, -1) == -1) {
2737c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
2747c478bd9Sstevel@tonic-gate 			crabort("fchown of temporary file failed");
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		(void) close(tmpfd);
2777c478bd9Sstevel@tonic-gate 		/*
2787c478bd9Sstevel@tonic-gate 		 * Fork off a child with user's permissions,
2797c478bd9Sstevel@tonic-gate 		 * to edit the crontab file
2807c478bd9Sstevel@tonic-gate 		 */
2817c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)-1)
2827c478bd9Sstevel@tonic-gate 			crabort("fork failed");
2837c478bd9Sstevel@tonic-gate 		if (pid == 0) {		/* child process */
2847c478bd9Sstevel@tonic-gate 			/* give up super-user privileges. */
2857c478bd9Sstevel@tonic-gate 			setuid(ruid);
2867c478bd9Sstevel@tonic-gate 			if ((tmpfp = fopen(edtemp, "w")) == NULL)
2877c478bd9Sstevel@tonic-gate 				crabort("can't create temporary file");
2887c478bd9Sstevel@tonic-gate 			if (fp != NULL) {
2897c478bd9Sstevel@tonic-gate 				/*
2907c478bd9Sstevel@tonic-gate 				 * Copy user's crontab file to temporary file.
2917c478bd9Sstevel@tonic-gate 				 */
2927c478bd9Sstevel@tonic-gate 				while (fgets(line, CTLINESIZE, fp) != NULL) {
2937c478bd9Sstevel@tonic-gate 					fputs(line, tmpfp);
2947c478bd9Sstevel@tonic-gate 					if (ferror(tmpfp)) {
2957c478bd9Sstevel@tonic-gate 						fclose(fp);
2967c478bd9Sstevel@tonic-gate 						fclose(tmpfp);
2977c478bd9Sstevel@tonic-gate 						crabort("write error on"
2987c478bd9Sstevel@tonic-gate 						    "temporary file");
2997c478bd9Sstevel@tonic-gate 					}
3007c478bd9Sstevel@tonic-gate 				}
3017c478bd9Sstevel@tonic-gate 				if (ferror(fp)) {
3027c478bd9Sstevel@tonic-gate 					fclose(fp);
3037c478bd9Sstevel@tonic-gate 					fclose(tmpfp);
3047c478bd9Sstevel@tonic-gate 					crabort(BADREAD);
3057c478bd9Sstevel@tonic-gate 				}
3067c478bd9Sstevel@tonic-gate 				fclose(fp);
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			if (fclose(tmpfp) == EOF)
3097c478bd9Sstevel@tonic-gate 				crabort("write error on temporary file");
3107c478bd9Sstevel@tonic-gate 			if (stat(edtemp, &stbuf) < 0)
3117c478bd9Sstevel@tonic-gate 				crabort("can't stat temporary file");
3127c478bd9Sstevel@tonic-gate 			omodtime = stbuf.st_mtime;
313d61d085dScf #ifdef _XPG_NOTDEFINED
3147c478bd9Sstevel@tonic-gate 			editor = getenv("VISUAL");
315d61d085dScf 			if (editor == NULL) {
316d61d085dScf #endif
3177c478bd9Sstevel@tonic-gate 				editor = getenv("EDITOR");
318d61d085dScf 				if (editor == NULL)
319d61d085dScf 					editor = VIPATH;
320d61d085dScf #ifdef _XPG_NOTDEFINED
321d61d085dScf 			}
322d61d085dScf #endif
32378ae324cSSumanth Naropanth 			buflen = strlen(editor) + strlen(edtemp) + 2;
32478ae324cSSumanth Naropanth 			buf = xmalloc(buflen);
32578ae324cSSumanth Naropanth 			(void) snprintf(buf, buflen, "%s %s", editor, edtemp);
3261c0be37bSsn 
3277c478bd9Sstevel@tonic-gate 			sleep(1);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 			while (1) {
33078ae324cSSumanth Naropanth 				ret = system(buf);
3311c0be37bSsn 
3327c478bd9Sstevel@tonic-gate 				/* sanity checks */
3337c478bd9Sstevel@tonic-gate 				if ((tmpfp = fopen(edtemp, "r")) == NULL)
3343d63ea05Sas 					crabort("can't open temporary file");
3357c478bd9Sstevel@tonic-gate 				if (fstat(fileno(tmpfp), &stbuf) < 0)
3363d63ea05Sas 					crabort("can't stat temporary file");
3377c478bd9Sstevel@tonic-gate 				if (stbuf.st_size == 0)
3383d63ea05Sas 					crabort("temporary file empty");
3397c478bd9Sstevel@tonic-gate 				if (omodtime == stbuf.st_mtime) {
3403d63ea05Sas 					(void) unlink(edtemp);
3413d63ea05Sas 					fprintf(stderr, gettext(
3423d63ea05Sas 					    "The crontab file was not"
3433d63ea05Sas 					    " changed.\n"));
3443d63ea05Sas 					exit(1);
3457c478bd9Sstevel@tonic-gate 				}
34678ae324cSSumanth Naropanth 				if ((ret) && (errno != EINTR)) {
3471c0be37bSsn 					/*
3481c0be37bSsn 					 * Some editors (like 'vi') can return
3491c0be37bSsn 					 * a non-zero exit status even though
3501c0be37bSsn 					 * everything is okay. Need to check.
3511c0be37bSsn 					 */
3521c0be37bSsn 					fprintf(stderr, gettext(ED_ERROR));
3531c0be37bSsn 					fflush(stderr);
3541c0be37bSsn 					if (isatty(fileno(stdin))) {
3551c0be37bSsn 						/* Interactive */
3561c0be37bSsn 						fprintf(stdout,
3571c0be37bSsn 						    gettext(ED_PROMPT),
3581c0be37bSsn 						    yesstr, nostr, nostr);
3591c0be37bSsn 						fflush(stdout);
3601c0be37bSsn 
3611c0be37bSsn 						if (yes()) {
3621c0be37bSsn 							/* Edit again */
3631c0be37bSsn 							continue;
3641c0be37bSsn 						} else {
3651c0be37bSsn 							/* Dump changes */
3661c0be37bSsn 							(void) unlink(edtemp);
3671c0be37bSsn 							exit(1);
3681c0be37bSsn 						}
3697c478bd9Sstevel@tonic-gate 					} else {
3701c0be37bSsn 						/*
3711c0be37bSsn 						 * Non-interactive, dump changes
3721c0be37bSsn 						 */
3737c478bd9Sstevel@tonic-gate 						(void) unlink(edtemp);
3747c478bd9Sstevel@tonic-gate 						exit(1);
3757c478bd9Sstevel@tonic-gate 					}
3767c478bd9Sstevel@tonic-gate 				}
3771c0be37bSsn 				exit(0);
3787c478bd9Sstevel@tonic-gate 			} /* while (1) */
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		/* fix for 1125555 - ignore common signals while waiting */
3827c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
3837c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
3847c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_IGN);
3857c478bd9Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
3867c478bd9Sstevel@tonic-gate 		wait(&stat_loc);
3877c478bd9Sstevel@tonic-gate 		if ((stat_loc & 0xFF00) != 0)
3887c478bd9Sstevel@tonic-gate 			exit(1);
3897c478bd9Sstevel@tonic-gate 
3901c0be37bSsn 		/*
3911c0be37bSsn 		 * unlink edtemp as 'ruid'. The file contents will be held
3921c0be37bSsn 		 * since we open the file descriptor 'tmpfp' before calling
3931c0be37bSsn 		 * unlink.
3941c0be37bSsn 		 */
3951c0be37bSsn 		if (((ret = seteuid(ruid)) < 0) ||
3961c0be37bSsn 		    ((tmpfp = fopen(edtemp, "r")) == NULL) ||
3971c0be37bSsn 		    (unlink(edtemp) == -1)) {
3987c478bd9Sstevel@tonic-gate 			fprintf(stderr, "crontab: %s: %s\n",
3997c478bd9Sstevel@tonic-gate 			    edtemp, errmsg(errno));
4001c0be37bSsn 			if ((ret < 0) || (tmpfp == NULL))
4011c0be37bSsn 				(void) unlink(edtemp);
4027c478bd9Sstevel@tonic-gate 			exit(1);
4037c478bd9Sstevel@tonic-gate 		} else
4047c478bd9Sstevel@tonic-gate 			seteuid(0);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		copycron(tmpfp);
4077c478bd9Sstevel@tonic-gate 	} else {
4087c478bd9Sstevel@tonic-gate 		if (argc == 0)
4097c478bd9Sstevel@tonic-gate 			copycron(stdin);
4107c478bd9Sstevel@tonic-gate 		else if (seteuid(getuid()) != 0 || (fp = fopen(argv[0], "r"))
4117c478bd9Sstevel@tonic-gate 		    == NULL)
4127c478bd9Sstevel@tonic-gate 			crabort(BADOPEN);
4137c478bd9Sstevel@tonic-gate 		else {
4147c478bd9Sstevel@tonic-gate 			seteuid(0);
4157c478bd9Sstevel@tonic-gate 			copycron(fp);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	cron_sendmsg(ADD, login, login, CRON);
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  *	if (per_errno == 2)
4217c478bd9Sstevel@tonic-gate  *		fprintf(stderr, gettext(WARNSHELL));
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate 	return (0);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static void
copycron(FILE * fp)42719803d09SToomas Soome copycron(FILE *fp)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	FILE *tfp;
4307c478bd9Sstevel@tonic-gate 	char pid[6], *tnam_end;
4317c478bd9Sstevel@tonic-gate 	int t;
4325b08e637SChris Gerhard 	char buf[LINE_MAX];
433*618372bcSSebastian Wiedenroth 	const char *errstr;
4346b734416SAndy Fiddaman 	cferror_t cferr;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	sprintf(pid, "%-5d", getpid());
4377c478bd9Sstevel@tonic-gate 	tnam = xmalloc(strlen(CRONDIR)+strlen(TMPFILE)+7);
4387c478bd9Sstevel@tonic-gate 	strcat(strcat(strcat(strcpy(tnam, CRONDIR), "/"), TMPFILE), pid);
4397c478bd9Sstevel@tonic-gate 	/* cut trailing blanks */
4407c478bd9Sstevel@tonic-gate 	tnam_end = strchr(tnam, ' ');
4417c478bd9Sstevel@tonic-gate 	if (tnam_end != NULL)
4427c478bd9Sstevel@tonic-gate 		*tnam_end = 0;
4437c478bd9Sstevel@tonic-gate 	/* catch SIGINT, SIGHUP, SIGQUIT signals */
4447c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, catch) == SIG_IGN)
4457c478bd9Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
4467c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, catch) == SIG_IGN) signal(SIGHUP, SIG_IGN);
4477c478bd9Sstevel@tonic-gate 	if (signal(SIGQUIT, catch) == SIG_IGN) signal(SIGQUIT, SIG_IGN);
4487c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, catch) == SIG_IGN) signal(SIGTERM, SIG_IGN);
4497c478bd9Sstevel@tonic-gate 	if ((t = creat(tnam, CRMODE)) == -1) crabort(BADCREATE);
4507c478bd9Sstevel@tonic-gate 	if ((tfp = fdopen(t, "w")) == NULL) {
4517c478bd9Sstevel@tonic-gate 		unlink(tnam);
4527c478bd9Sstevel@tonic-gate 		crabort(BADCREATE);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	err = 0;	/* if errors found, err set to 1 */
4557c478bd9Sstevel@tonic-gate 	while (fgets(line, CTLINESIZE, fp) != NULL) {
4567c478bd9Sstevel@tonic-gate 		cursor = 0;
4577c478bd9Sstevel@tonic-gate 		while (line[cursor] == ' ' || line[cursor] == '\t')
4587c478bd9Sstevel@tonic-gate 			cursor++;
4597c478bd9Sstevel@tonic-gate 		/* fix for 1039689 - treat blank line like a comment */
4607c478bd9Sstevel@tonic-gate 		if (line[cursor] == '#' || line[cursor] == '\n')
4617c478bd9Sstevel@tonic-gate 			goto cont;
4625b08e637SChris Gerhard 
4635b08e637SChris Gerhard 		if (strncmp(&line[cursor], ENV_TZ, strlen(ENV_TZ)) == 0) {
4645b08e637SChris Gerhard 			char *x;
4655b08e637SChris Gerhard 
466*618372bcSSebastian Wiedenroth 			(void) strncpy(buf, &line[cursor + strlen(ENV_TZ)],
4675b08e637SChris Gerhard 			    sizeof (buf));
4685b08e637SChris Gerhard 			if ((x = strchr(buf, '\n')) != NULL)
46919803d09SToomas Soome 				*x = '\0';
4705b08e637SChris Gerhard 
4715b08e637SChris Gerhard 			if (isvalid_tz(buf, NULL, _VTZ_ALL)) {
4725b08e637SChris Gerhard 				goto cont;
4735b08e637SChris Gerhard 			} else {
4745b08e637SChris Gerhard 				err = 1;
4755b08e637SChris Gerhard 				fprintf(stderr, BAD_TZ, &line[cursor]);
4765b08e637SChris Gerhard 				continue;
4775b08e637SChris Gerhard 			}
4785b08e637SChris Gerhard 		} else if (strncmp(&line[cursor], ENV_SHELL,
4795b08e637SChris Gerhard 		    strlen(ENV_SHELL)) == 0) {
4805b08e637SChris Gerhard 			char *x;
4815b08e637SChris Gerhard 
482*618372bcSSebastian Wiedenroth 			(void) strncpy(buf, &line[cursor + strlen(ENV_SHELL)],
4835b08e637SChris Gerhard 			    sizeof (buf));
4845b08e637SChris Gerhard 			if ((x = strchr(buf, '\n')) != NULL)
48519803d09SToomas Soome 				*x = '\0';
4865b08e637SChris Gerhard 
4875b08e637SChris Gerhard 			if (isvalid_shell(buf)) {
4885b08e637SChris Gerhard 				goto cont;
4895b08e637SChris Gerhard 			} else {
4905b08e637SChris Gerhard 				err = 1;
4915b08e637SChris Gerhard 				fprintf(stderr, BAD_SHELL, &line[cursor]);
4925b08e637SChris Gerhard 				continue;
4935b08e637SChris Gerhard 			}
4945b08e637SChris Gerhard 		} else if (strncmp(&line[cursor], ENV_HOME,
4955b08e637SChris Gerhard 		    strlen(ENV_HOME)) == 0) {
4965b08e637SChris Gerhard 			char *x;
4975b08e637SChris Gerhard 
498*618372bcSSebastian Wiedenroth 			(void) strncpy(buf, &line[cursor + strlen(ENV_HOME)],
4995b08e637SChris Gerhard 			    sizeof (buf));
5005b08e637SChris Gerhard 			if ((x = strchr(buf, '\n')) != NULL)
50119803d09SToomas Soome 				*x = '\0';
5025b08e637SChris Gerhard 			if (chdir(buf) == 0) {
5035b08e637SChris Gerhard 				goto cont;
5045b08e637SChris Gerhard 			} else {
5055b08e637SChris Gerhard 				err = 1;
5065b08e637SChris Gerhard 				fprintf(stderr, BAD_HOME, &line[cursor],
5075b08e637SChris Gerhard 				    strerror(errno));
5085b08e637SChris Gerhard 				continue;
5095b08e637SChris Gerhard 			}
510*618372bcSSebastian Wiedenroth 		} else if (strncmp(&line[cursor], ENV_RANDOM_DELAY,
511*618372bcSSebastian Wiedenroth 		    strlen(ENV_RANDOM_DELAY)) == 0) {
512*618372bcSSebastian Wiedenroth 			char *x;
513*618372bcSSebastian Wiedenroth 
514*618372bcSSebastian Wiedenroth 			(void) strncpy(buf,
515*618372bcSSebastian Wiedenroth 			    &line[cursor + strlen(ENV_RANDOM_DELAY)],
516*618372bcSSebastian Wiedenroth 			    sizeof (buf));
517*618372bcSSebastian Wiedenroth 			if ((x = strchr(buf, '\n')) != NULL)
518*618372bcSSebastian Wiedenroth 				*x = '\0';
519*618372bcSSebastian Wiedenroth 
520*618372bcSSebastian Wiedenroth 			(void) strtonum(buf, 0, UINT32_MAX / 60, &errstr);
521*618372bcSSebastian Wiedenroth 			if (errstr == NULL) {
522*618372bcSSebastian Wiedenroth 				goto cont;
523*618372bcSSebastian Wiedenroth 			} else {
524*618372bcSSebastian Wiedenroth 				err = 1;
525*618372bcSSebastian Wiedenroth 				fprintf(stderr, BAD_RAND_DELAY,
526*618372bcSSebastian Wiedenroth 				    &line[cursor], strerror(errno));
527*618372bcSSebastian Wiedenroth 				continue;
528*618372bcSSebastian Wiedenroth 			}
5295b08e637SChris Gerhard 		}
5305b08e637SChris Gerhard 
5316b734416SAndy Fiddaman 		if ((cferr = next_field(0, 59, line, &cursor, NULL)) != CFOK ||
5326b734416SAndy Fiddaman 		    (cferr = next_field(0, 23, line, &cursor, NULL)) != CFOK ||
5336b734416SAndy Fiddaman 		    (cferr = next_field(1, 31, line, &cursor, NULL)) != CFOK ||
5346b734416SAndy Fiddaman 		    (cferr = next_field(1, 12, line, &cursor, NULL)) != CFOK ||
5356b734416SAndy Fiddaman 		    (cferr = next_field(0, 6, line, &cursor, NULL)) != CFOK) {
5366b734416SAndy Fiddaman 			switch (cferr) {
5376b734416SAndy Fiddaman 			case CFEOLN:
5386b734416SAndy Fiddaman 				cerror(EOLN);
5396b734416SAndy Fiddaman 				break;
5406b734416SAndy Fiddaman 			case CFUNEXPECT:
5416b734416SAndy Fiddaman 				cerror(UNEXPECT);
5426b734416SAndy Fiddaman 				break;
5436b734416SAndy Fiddaman 			case CFOUTOFBOUND:
5446b734416SAndy Fiddaman 				cerror(OUTOFBOUND);
5456b734416SAndy Fiddaman 				break;
5466b734416SAndy Fiddaman 			case CFEOVERFLOW:
5476b734416SAndy Fiddaman 				cerror(OVERFLOW);
5486b734416SAndy Fiddaman 				break;
5496b734416SAndy Fiddaman 			case CFENOMEM:
5506b734416SAndy Fiddaman 				(void) fprintf(stderr, "Out of memory\n");
5516b734416SAndy Fiddaman 				exit(55);
5526b734416SAndy Fiddaman 				break;
5536b734416SAndy Fiddaman 			default:
5546b734416SAndy Fiddaman 				break;
5556b734416SAndy Fiddaman 			}
5566b734416SAndy Fiddaman 			continue;
5576b734416SAndy Fiddaman 		}
5586b734416SAndy Fiddaman 
5597c478bd9Sstevel@tonic-gate 		if (line[++cursor] == '\0') {
5607c478bd9Sstevel@tonic-gate 			cerror(EOLN);
5617c478bd9Sstevel@tonic-gate 			continue;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate cont:
5647c478bd9Sstevel@tonic-gate 		if (fputs(line, tfp) == EOF) {
5657c478bd9Sstevel@tonic-gate 			unlink(tnam);
5667c478bd9Sstevel@tonic-gate 			crabort(BADCREATE);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	fclose(fp);
5707c478bd9Sstevel@tonic-gate 	fclose(tfp);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/* audit differences between old and new crontabs */
5737c478bd9Sstevel@tonic-gate 	audit_crontab_modify(cf, tnam, err);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (!err) {
5767c478bd9Sstevel@tonic-gate 		/* make file tfp the new crontab */
5777c478bd9Sstevel@tonic-gate 		unlink(cf);
5787c478bd9Sstevel@tonic-gate 		if (link(tnam, cf) == -1) {
5797c478bd9Sstevel@tonic-gate 			unlink(tnam);
5807c478bd9Sstevel@tonic-gate 			crabort(BADCREATE);
5817c478bd9Sstevel@tonic-gate 		}
582b0d0a1c8SViswanathan Kannappan 	} else {
583b0d0a1c8SViswanathan Kannappan 		crabort(ERRSFND);
584b0d0a1c8SViswanathan Kannappan 	}
5857c478bd9Sstevel@tonic-gate 	unlink(tnam);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate static void
cerror(char * msg)58919803d09SToomas Soome cerror(char *msg)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("%scrontab: error on previous line; %s\n"),
5927c478bd9Sstevel@tonic-gate 	    line, msg);
5937c478bd9Sstevel@tonic-gate 	err = 1;
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static void
catch(int x)5987c478bd9Sstevel@tonic-gate catch(int x)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	unlink(tnam);
6017c478bd9Sstevel@tonic-gate 	exit(1);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static void
crabort(char * msg)60519803d09SToomas Soome crabort(char *msg)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	int sverrno;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (strcmp(edtemp, "") != 0) {
6107c478bd9Sstevel@tonic-gate 		sverrno = errno;
6117c478bd9Sstevel@tonic-gate 		(void) unlink(edtemp);
6127c478bd9Sstevel@tonic-gate 		errno = sverrno;
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 	if (tnam != NULL) {
6157c478bd9Sstevel@tonic-gate 		sverrno = errno;
6167c478bd9Sstevel@tonic-gate 		(void) unlink(tnam);
6177c478bd9Sstevel@tonic-gate 		errno = sverrno;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	fprintf(stderr, "crontab: %s\n", gettext(msg));
6207c478bd9Sstevel@tonic-gate 	exit(1);
6217c478bd9Sstevel@tonic-gate }
622