xref: /onnv-gate/usr/src/cmd/cron/crontab.c (revision 11115:bcfb2bb98fca)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51511Sdp  * Common Development and Distribution License (the "License").
61511Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228998SViswanathan.Kannappan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/wait.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <signal.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <ctype.h>
400Sstevel@tonic-gate #include <pwd.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <locale.h>
430Sstevel@tonic-gate #include <nl_types.h>
440Sstevel@tonic-gate #include <langinfo.h>
450Sstevel@tonic-gate #include <libintl.h>
460Sstevel@tonic-gate #include <security/pam_appl.h>
478439SChris.Gerhard@sun.com #include <limits.h>
488439SChris.Gerhard@sun.com #include <libzoneinfo.h>
490Sstevel@tonic-gate #include "cron.h"
504774Sas145665 #include "getresponse.h"
510Sstevel@tonic-gate 
521818Scf46844 #if defined(XPG4)
531818Scf46844 #define	VIPATH	"/usr/xpg4/bin/vi"
541818Scf46844 #elif defined(XPG6)
551818Scf46844 #define	VIPATH	"/usr/xpg6/bin/vi"
561818Scf46844 #else
571818Scf46844 #define	_XPG_NOTDEFINED
581818Scf46844 #define	VIPATH	"vi"
591818Scf46844 #endif
601818Scf46844 
610Sstevel@tonic-gate #define	TMPFILE		"_cron"		/* prefix for tmp file */
620Sstevel@tonic-gate #define	CRMODE		0600	/* mode for creating crontabs */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	BADCREATE	\
650Sstevel@tonic-gate 	"can't create your crontab file in the crontab directory."
660Sstevel@tonic-gate #define	BADOPEN		"can't open your crontab file."
670Sstevel@tonic-gate #define	BADSHELL	\
680Sstevel@tonic-gate 	"because your login shell isn't /usr/bin/sh, you can't use cron."
690Sstevel@tonic-gate #define	WARNSHELL	"warning: commands will be executed using /usr/bin/sh\n"
700Sstevel@tonic-gate #define	BADUSAGE	\
711511Sdp 	"usage:\n"			\
721511Sdp 	"\tcrontab [file]\n"		\
731511Sdp 	"\tcrontab -e [username]\n"	\
741511Sdp 	"\tcrontab -l [username]\n"	\
751511Sdp 	"\tcrontab -r [username]"
760Sstevel@tonic-gate #define	INVALIDUSER	"you are not a valid user (no entry in /etc/passwd)."
770Sstevel@tonic-gate #define	NOTALLOWED	"you are not authorized to use cron.  Sorry."
780Sstevel@tonic-gate #define	NOTROOT		\
790Sstevel@tonic-gate 	"you must be super-user to access another user's crontab file"
800Sstevel@tonic-gate #define	AUDITREJECT	"The audit context for your shell has not been set."
810Sstevel@tonic-gate #define	EOLN		"unexpected end of line."
820Sstevel@tonic-gate #define	UNEXPECT	"unexpected character found in line."
830Sstevel@tonic-gate #define	OUTOFBOUND	"number out of bounds."
840Sstevel@tonic-gate #define	ERRSFND		"errors detected in input, no crontab file generated."
850Sstevel@tonic-gate #define	ED_ERROR	\
860Sstevel@tonic-gate 	"     The editor indicates that an error occurred while you were\n"\
870Sstevel@tonic-gate 	"     editing the crontab data - usually a minor typing error.\n\n"
880Sstevel@tonic-gate #define	BADREAD		"error reading your crontab file"
890Sstevel@tonic-gate #define	ED_PROMPT	\
904774Sas145665 	"     Edit again, to ensure crontab information is intact (%s/%s)?\n"\
914774Sas145665 	"     ('%s' will discard edits.)"
920Sstevel@tonic-gate #define	NAMETOOLONG	"login name too long"
938439SChris.Gerhard@sun.com #define	BAD_TZ	"Timezone unrecognized in: %s"
948439SChris.Gerhard@sun.com #define	BAD_SHELL	"Invalid shell specified: %s"
958439SChris.Gerhard@sun.com #define	BAD_HOME	"Unable to access directory: %s\t%s\n"
960Sstevel@tonic-gate 
970Sstevel@tonic-gate extern int	per_errno;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate extern int	audit_crontab_modify(char *, char *, int);
1000Sstevel@tonic-gate extern int	audit_crontab_delete(char *, int);
1010Sstevel@tonic-gate extern int	audit_crontab_not_allowed(uid_t, char *);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int		err;
1040Sstevel@tonic-gate int		cursor;
1050Sstevel@tonic-gate char		*cf;
1060Sstevel@tonic-gate char		*tnam;
1070Sstevel@tonic-gate char		edtemp[5+13+1];
1080Sstevel@tonic-gate char		line[CTLINESIZE];
1090Sstevel@tonic-gate static		char	login[UNAMESIZE];
1100Sstevel@tonic-gate 
1114774Sas145665 static int	next_field(int, int);
1124774Sas145665 static void	catch(int);
1134774Sas145665 static void	crabort(char *);
1144774Sas145665 static void	cerror(char *);
1154774Sas145665 static void	copycron(FILE *);
1160Sstevel@tonic-gate 
117523Sbasabi int
main(int argc,char ** argv)118523Sbasabi main(int argc, char **argv)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int	c, r;
1210Sstevel@tonic-gate 	int	rflag	= 0;
1220Sstevel@tonic-gate 	int	lflag	= 0;
1230Sstevel@tonic-gate 	int	eflag	= 0;
1240Sstevel@tonic-gate 	int	errflg	= 0;
1250Sstevel@tonic-gate 	char *pp;
1260Sstevel@tonic-gate 	FILE *fp, *tmpfp;
1270Sstevel@tonic-gate 	struct stat stbuf;
1280Sstevel@tonic-gate 	struct passwd *pwp;
1290Sstevel@tonic-gate 	time_t omodtime;
1300Sstevel@tonic-gate 	char *editor;
1310Sstevel@tonic-gate 	uid_t ruid;
1320Sstevel@tonic-gate 	pid_t pid;
1330Sstevel@tonic-gate 	int stat_loc;
1340Sstevel@tonic-gate 	int ret;
1350Sstevel@tonic-gate 	char real_login[UNAMESIZE];
1360Sstevel@tonic-gate 	int tmpfd = -1;
1370Sstevel@tonic-gate 	pam_handle_t *pamh;
1380Sstevel@tonic-gate 	int pam_error;
1399182SSumanth.Naropanth@Sun.COM 	char *buf;
1409182SSumanth.Naropanth@Sun.COM 	size_t buflen;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1430Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1440Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1450Sstevel@tonic-gate #endif
1460Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1474774Sas145665 
1484774Sas145665 	if (init_yes() < 0) {
1494774Sas145665 		(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
1504774Sas145665 		    strerror(errno));
1514774Sas145665 		exit(1);
1524774Sas145665 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "elr")) != EOF)
1550Sstevel@tonic-gate 		switch (c) {
1560Sstevel@tonic-gate 			case 'e':
1570Sstevel@tonic-gate 				eflag++;
1580Sstevel@tonic-gate 				break;
1590Sstevel@tonic-gate 			case 'l':
1600Sstevel@tonic-gate 				lflag++;
1610Sstevel@tonic-gate 				break;
1620Sstevel@tonic-gate 			case 'r':
1630Sstevel@tonic-gate 				rflag++;
1640Sstevel@tonic-gate 				break;
1650Sstevel@tonic-gate 			case '?':
1660Sstevel@tonic-gate 				errflg++;
1670Sstevel@tonic-gate 				break;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (eflag + lflag + rflag > 1)
1710Sstevel@tonic-gate 		errflg++;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	argc -= optind;
1740Sstevel@tonic-gate 	argv += optind;
1750Sstevel@tonic-gate 	if (errflg || argc > 1)
1760Sstevel@tonic-gate 		crabort(BADUSAGE);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	ruid = getuid();
1790Sstevel@tonic-gate 	if ((pwp = getpwuid(ruid)) == NULL)
1800Sstevel@tonic-gate 		crabort(INVALIDUSER);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (strlcpy(real_login, pwp->pw_name, sizeof (real_login))
1830Sstevel@tonic-gate 	    >= sizeof (real_login))
1840Sstevel@tonic-gate 		crabort(NAMETOOLONG);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if ((eflag || lflag || rflag) && argc == 1) {
1870Sstevel@tonic-gate 		if ((pwp = getpwnam(*argv)) == NULL)
1880Sstevel@tonic-gate 			crabort(INVALIDUSER);
1890Sstevel@tonic-gate 
190*11115SNobutomo.Nakano@Sun.COM 		if (!cron_admin(real_login)) {
1910Sstevel@tonic-gate 			if (pwp->pw_uid != ruid)
1920Sstevel@tonic-gate 				crabort(NOTROOT);
1930Sstevel@tonic-gate 			else
1940Sstevel@tonic-gate 				pp = getuser(ruid);
1950Sstevel@tonic-gate 		} else
1960Sstevel@tonic-gate 			pp = *argv++;
1970Sstevel@tonic-gate 	} else {
1980Sstevel@tonic-gate 		pp = getuser(ruid);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (pp == NULL) {
2020Sstevel@tonic-gate 		if (per_errno == 2)
2030Sstevel@tonic-gate 			crabort(BADSHELL);
2040Sstevel@tonic-gate 		else
2050Sstevel@tonic-gate 			crabort(INVALIDUSER);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	if (strlcpy(login, pp, sizeof (login)) >= sizeof (login))
2080Sstevel@tonic-gate 		crabort(NAMETOOLONG);
2090Sstevel@tonic-gate 	if (!allowed(login, CRONALLOW, CRONDENY))
2100Sstevel@tonic-gate 		crabort(NOTALLOWED);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/* Do account validation check */
2130Sstevel@tonic-gate 	pam_error = pam_start("cron", pp, NULL, &pamh);
2140Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
2150Sstevel@tonic-gate 		crabort((char *)pam_strerror(pamh, pam_error));
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	pam_error = pam_acct_mgmt(pamh, PAM_SILENT);
2180Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
2190Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning - Invalid account: "
2200Sstevel@tonic-gate 		    "'%s' not allowed to execute cronjobs\n"), pp);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/* check for unaudited shell */
2260Sstevel@tonic-gate 	if (audit_crontab_not_allowed(ruid, pp))
2270Sstevel@tonic-gate 		crabort(AUDITREJECT);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	cf = xmalloc(strlen(CRONDIR)+strlen(login)+2);
2300Sstevel@tonic-gate 	strcat(strcat(strcpy(cf, CRONDIR), "/"), login);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (rflag) {
2330Sstevel@tonic-gate 		r = unlink(cf);
2340Sstevel@tonic-gate 		cron_sendmsg(DELETE, login, login, CRON);
2350Sstevel@tonic-gate 		audit_crontab_delete(cf, r);
2360Sstevel@tonic-gate 		exit(0);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	if (lflag) {
2390Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL)
2400Sstevel@tonic-gate 			crabort(BADOPEN);
2410Sstevel@tonic-gate 		while (fgets(line, CTLINESIZE, fp) != NULL)
2420Sstevel@tonic-gate 			fputs(line, stdout);
2430Sstevel@tonic-gate 		fclose(fp);
2440Sstevel@tonic-gate 		exit(0);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	if (eflag) {
2470Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL) {
2480Sstevel@tonic-gate 			if (errno != ENOENT)
2490Sstevel@tonic-gate 				crabort(BADOPEN);
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		(void) strcpy(edtemp, "/tmp/crontabXXXXXX");
2520Sstevel@tonic-gate 		tmpfd = mkstemp(edtemp);
2530Sstevel@tonic-gate 		if (fchown(tmpfd, ruid, -1) == -1) {
2540Sstevel@tonic-gate 			(void) close(tmpfd);
2550Sstevel@tonic-gate 			crabort("fchown of temporary file failed");
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 		(void) close(tmpfd);
2580Sstevel@tonic-gate 		/*
2590Sstevel@tonic-gate 		 * Fork off a child with user's permissions,
2600Sstevel@tonic-gate 		 * to edit the crontab file
2610Sstevel@tonic-gate 		 */
2620Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)-1)
2630Sstevel@tonic-gate 			crabort("fork failed");
2640Sstevel@tonic-gate 		if (pid == 0) {		/* child process */
2650Sstevel@tonic-gate 			/* give up super-user privileges. */
2660Sstevel@tonic-gate 			setuid(ruid);
2670Sstevel@tonic-gate 			if ((tmpfp = fopen(edtemp, "w")) == NULL)
2680Sstevel@tonic-gate 				crabort("can't create temporary file");
2690Sstevel@tonic-gate 			if (fp != NULL) {
2700Sstevel@tonic-gate 				/*
2710Sstevel@tonic-gate 				 * Copy user's crontab file to temporary file.
2720Sstevel@tonic-gate 				 */
2730Sstevel@tonic-gate 				while (fgets(line, CTLINESIZE, fp) != NULL) {
2740Sstevel@tonic-gate 					fputs(line, tmpfp);
2750Sstevel@tonic-gate 					if (ferror(tmpfp)) {
2760Sstevel@tonic-gate 						fclose(fp);
2770Sstevel@tonic-gate 						fclose(tmpfp);
2780Sstevel@tonic-gate 						crabort("write error on"
2790Sstevel@tonic-gate 						    "temporary file");
2800Sstevel@tonic-gate 					}
2810Sstevel@tonic-gate 				}
2820Sstevel@tonic-gate 				if (ferror(fp)) {
2830Sstevel@tonic-gate 					fclose(fp);
2840Sstevel@tonic-gate 					fclose(tmpfp);
2850Sstevel@tonic-gate 					crabort(BADREAD);
2860Sstevel@tonic-gate 				}
2870Sstevel@tonic-gate 				fclose(fp);
2880Sstevel@tonic-gate 			}
2890Sstevel@tonic-gate 			if (fclose(tmpfp) == EOF)
2900Sstevel@tonic-gate 				crabort("write error on temporary file");
2910Sstevel@tonic-gate 			if (stat(edtemp, &stbuf) < 0)
2920Sstevel@tonic-gate 				crabort("can't stat temporary file");
2930Sstevel@tonic-gate 			omodtime = stbuf.st_mtime;
2941818Scf46844 #ifdef _XPG_NOTDEFINED
2950Sstevel@tonic-gate 			editor = getenv("VISUAL");
2961818Scf46844 			if (editor == NULL) {
2971818Scf46844 #endif
2980Sstevel@tonic-gate 				editor = getenv("EDITOR");
2991818Scf46844 				if (editor == NULL)
3001818Scf46844 					editor = VIPATH;
3011818Scf46844 #ifdef _XPG_NOTDEFINED
3021818Scf46844 			}
3031818Scf46844 #endif
3049182SSumanth.Naropanth@Sun.COM 			buflen = strlen(editor) + strlen(edtemp) + 2;
3059182SSumanth.Naropanth@Sun.COM 			buf = xmalloc(buflen);
3069182SSumanth.Naropanth@Sun.COM 			(void) snprintf(buf, buflen, "%s %s", editor, edtemp);
3076760Ssn199410 
3080Sstevel@tonic-gate 			sleep(1);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 			while (1) {
3119182SSumanth.Naropanth@Sun.COM 				ret = system(buf);
3126760Ssn199410 
3130Sstevel@tonic-gate 				/* sanity checks */
3140Sstevel@tonic-gate 				if ((tmpfp = fopen(edtemp, "r")) == NULL)
3154774Sas145665 					crabort("can't open temporary file");
3160Sstevel@tonic-gate 				if (fstat(fileno(tmpfp), &stbuf) < 0)
3174774Sas145665 					crabort("can't stat temporary file");
3180Sstevel@tonic-gate 				if (stbuf.st_size == 0)
3194774Sas145665 					crabort("temporary file empty");
3200Sstevel@tonic-gate 				if (omodtime == stbuf.st_mtime) {
3214774Sas145665 					(void) unlink(edtemp);
3224774Sas145665 					fprintf(stderr, gettext(
3234774Sas145665 					    "The crontab file was not"
3244774Sas145665 					    " changed.\n"));
3254774Sas145665 					exit(1);
3260Sstevel@tonic-gate 				}
3279182SSumanth.Naropanth@Sun.COM 				if ((ret) && (errno != EINTR)) {
3286760Ssn199410 					/*
3296760Ssn199410 					 * Some editors (like 'vi') can return
3306760Ssn199410 					 * a non-zero exit status even though
3316760Ssn199410 					 * everything is okay. Need to check.
3326760Ssn199410 					 */
3336760Ssn199410 					fprintf(stderr, gettext(ED_ERROR));
3346760Ssn199410 					fflush(stderr);
3356760Ssn199410 					if (isatty(fileno(stdin))) {
3366760Ssn199410 						/* Interactive */
3376760Ssn199410 						fprintf(stdout,
3386760Ssn199410 						    gettext(ED_PROMPT),
3396760Ssn199410 						    yesstr, nostr, nostr);
3406760Ssn199410 						fflush(stdout);
3410Sstevel@tonic-gate 
3426760Ssn199410 						if (yes()) {
3436760Ssn199410 							/* Edit again */
3446760Ssn199410 							continue;
3456760Ssn199410 						} else {
3466760Ssn199410 							/* Dump changes */
3476760Ssn199410 							(void) unlink(edtemp);
3486760Ssn199410 							exit(1);
3496760Ssn199410 						}
3500Sstevel@tonic-gate 					} else {
3516760Ssn199410 						/*
3526760Ssn199410 						 * Non-interactive, dump changes
3536760Ssn199410 						 */
3540Sstevel@tonic-gate 						(void) unlink(edtemp);
3550Sstevel@tonic-gate 						exit(1);
3560Sstevel@tonic-gate 					}
3570Sstevel@tonic-gate 				}
3586760Ssn199410 				exit(0);
3590Sstevel@tonic-gate 			} /* while (1) */
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		/* fix for 1125555 - ignore common signals while waiting */
3630Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
3640Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
3650Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_IGN);
3660Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
3670Sstevel@tonic-gate 		wait(&stat_loc);
3680Sstevel@tonic-gate 		if ((stat_loc & 0xFF00) != 0)
3690Sstevel@tonic-gate 			exit(1);
3700Sstevel@tonic-gate 
3716760Ssn199410 		/*
3726760Ssn199410 		 * unlink edtemp as 'ruid'. The file contents will be held
3736760Ssn199410 		 * since we open the file descriptor 'tmpfp' before calling
3746760Ssn199410 		 * unlink.
3756760Ssn199410 		 */
3766760Ssn199410 		if (((ret = seteuid(ruid)) < 0) ||
3776760Ssn199410 		    ((tmpfp = fopen(edtemp, "r")) == NULL) ||
3786760Ssn199410 		    (unlink(edtemp) == -1)) {
3790Sstevel@tonic-gate 			fprintf(stderr, "crontab: %s: %s\n",
3800Sstevel@tonic-gate 			    edtemp, errmsg(errno));
3816760Ssn199410 			if ((ret < 0) || (tmpfp == NULL))
3826760Ssn199410 				(void) unlink(edtemp);
3830Sstevel@tonic-gate 			exit(1);
3840Sstevel@tonic-gate 		} else
3850Sstevel@tonic-gate 			seteuid(0);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		copycron(tmpfp);
3880Sstevel@tonic-gate 	} else {
3890Sstevel@tonic-gate 		if (argc == 0)
3900Sstevel@tonic-gate 			copycron(stdin);
3910Sstevel@tonic-gate 		else if (seteuid(getuid()) != 0 || (fp = fopen(argv[0], "r"))
3920Sstevel@tonic-gate 		    == NULL)
3930Sstevel@tonic-gate 			crabort(BADOPEN);
3940Sstevel@tonic-gate 		else {
3950Sstevel@tonic-gate 			seteuid(0);
3960Sstevel@tonic-gate 			copycron(fp);
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	cron_sendmsg(ADD, login, login, CRON);
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  *	if (per_errno == 2)
4020Sstevel@tonic-gate  *		fprintf(stderr, gettext(WARNSHELL));
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate 	return (0);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate static void
copycron(fp)4080Sstevel@tonic-gate copycron(fp)
4090Sstevel@tonic-gate FILE *fp;
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	FILE *tfp;
4120Sstevel@tonic-gate 	char pid[6], *tnam_end;
4130Sstevel@tonic-gate 	int t;
4148439SChris.Gerhard@sun.com 	char buf[LINE_MAX];
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	sprintf(pid, "%-5d", getpid());
4170Sstevel@tonic-gate 	tnam = xmalloc(strlen(CRONDIR)+strlen(TMPFILE)+7);
4180Sstevel@tonic-gate 	strcat(strcat(strcat(strcpy(tnam, CRONDIR), "/"), TMPFILE), pid);
4190Sstevel@tonic-gate 	/* cut trailing blanks */
4200Sstevel@tonic-gate 	tnam_end = strchr(tnam, ' ');
4210Sstevel@tonic-gate 	if (tnam_end != NULL)
4220Sstevel@tonic-gate 		*tnam_end = 0;
4230Sstevel@tonic-gate 	/* catch SIGINT, SIGHUP, SIGQUIT signals */
4240Sstevel@tonic-gate 	if (signal(SIGINT, catch) == SIG_IGN)
4250Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
4260Sstevel@tonic-gate 	if (signal(SIGHUP, catch) == SIG_IGN) signal(SIGHUP, SIG_IGN);
4270Sstevel@tonic-gate 	if (signal(SIGQUIT, catch) == SIG_IGN) signal(SIGQUIT, SIG_IGN);
4280Sstevel@tonic-gate 	if (signal(SIGTERM, catch) == SIG_IGN) signal(SIGTERM, SIG_IGN);
4290Sstevel@tonic-gate 	if ((t = creat(tnam, CRMODE)) == -1) crabort(BADCREATE);
4300Sstevel@tonic-gate 	if ((tfp = fdopen(t, "w")) == NULL) {
4310Sstevel@tonic-gate 		unlink(tnam);
4320Sstevel@tonic-gate 		crabort(BADCREATE);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	err = 0;	/* if errors found, err set to 1 */
4350Sstevel@tonic-gate 	while (fgets(line, CTLINESIZE, fp) != NULL) {
4360Sstevel@tonic-gate 		cursor = 0;
4370Sstevel@tonic-gate 		while (line[cursor] == ' ' || line[cursor] == '\t')
4380Sstevel@tonic-gate 			cursor++;
4390Sstevel@tonic-gate 		/* fix for 1039689 - treat blank line like a comment */
4400Sstevel@tonic-gate 		if (line[cursor] == '#' || line[cursor] == '\n')
4410Sstevel@tonic-gate 			goto cont;
4428439SChris.Gerhard@sun.com 
4438439SChris.Gerhard@sun.com 		if (strncmp(&line[cursor], ENV_TZ, strlen(ENV_TZ)) == 0) {
4448439SChris.Gerhard@sun.com 			char *x;
4458439SChris.Gerhard@sun.com 
4468439SChris.Gerhard@sun.com 			strncpy(buf, &line[cursor + strlen(ENV_TZ)],
4478439SChris.Gerhard@sun.com 			    sizeof (buf));
4488439SChris.Gerhard@sun.com 			if ((x = strchr(buf, '\n')) != NULL)
4498439SChris.Gerhard@sun.com 				*x = NULL;
4508439SChris.Gerhard@sun.com 
4518439SChris.Gerhard@sun.com 			if (isvalid_tz(buf, NULL, _VTZ_ALL)) {
4528439SChris.Gerhard@sun.com 				goto cont;
4538439SChris.Gerhard@sun.com 			} else {
4548439SChris.Gerhard@sun.com 				err = 1;
4558439SChris.Gerhard@sun.com 				fprintf(stderr, BAD_TZ, &line[cursor]);
4568439SChris.Gerhard@sun.com 				continue;
4578439SChris.Gerhard@sun.com 			}
4588439SChris.Gerhard@sun.com 		} else if (strncmp(&line[cursor], ENV_SHELL,
4598439SChris.Gerhard@sun.com 		    strlen(ENV_SHELL)) == 0) {
4608439SChris.Gerhard@sun.com 			char *x;
4618439SChris.Gerhard@sun.com 
4628439SChris.Gerhard@sun.com 			strncpy(buf, &line[cursor + strlen(ENV_SHELL)],
4638439SChris.Gerhard@sun.com 			    sizeof (buf));
4648439SChris.Gerhard@sun.com 			if ((x = strchr(buf, '\n')) != NULL)
4658439SChris.Gerhard@sun.com 				*x = NULL;
4668439SChris.Gerhard@sun.com 
4678439SChris.Gerhard@sun.com 			if (isvalid_shell(buf)) {
4688439SChris.Gerhard@sun.com 				goto cont;
4698439SChris.Gerhard@sun.com 			} else {
4708439SChris.Gerhard@sun.com 				err = 1;
4718439SChris.Gerhard@sun.com 				fprintf(stderr, BAD_SHELL, &line[cursor]);
4728439SChris.Gerhard@sun.com 				continue;
4738439SChris.Gerhard@sun.com 			}
4748439SChris.Gerhard@sun.com 		} else if (strncmp(&line[cursor], ENV_HOME,
4758439SChris.Gerhard@sun.com 		    strlen(ENV_HOME)) == 0) {
4768439SChris.Gerhard@sun.com 			char *x;
4778439SChris.Gerhard@sun.com 
4788439SChris.Gerhard@sun.com 			strncpy(buf, &line[cursor + strlen(ENV_HOME)],
4798439SChris.Gerhard@sun.com 			    sizeof (buf));
4808439SChris.Gerhard@sun.com 			if ((x = strchr(buf, '\n')) != NULL)
4818439SChris.Gerhard@sun.com 				*x = NULL;
4828439SChris.Gerhard@sun.com 			if (chdir(buf) == 0) {
4838439SChris.Gerhard@sun.com 				goto cont;
4848439SChris.Gerhard@sun.com 			} else {
4858439SChris.Gerhard@sun.com 				err = 1;
4868439SChris.Gerhard@sun.com 				fprintf(stderr, BAD_HOME, &line[cursor],
4878439SChris.Gerhard@sun.com 				    strerror(errno));
4888439SChris.Gerhard@sun.com 				continue;
4898439SChris.Gerhard@sun.com 			}
4908439SChris.Gerhard@sun.com 		}
4918439SChris.Gerhard@sun.com 
4920Sstevel@tonic-gate 		if (next_field(0, 59)) continue;
4930Sstevel@tonic-gate 		if (next_field(0, 23)) continue;
4940Sstevel@tonic-gate 		if (next_field(1, 31)) continue;
4950Sstevel@tonic-gate 		if (next_field(1, 12)) continue;
4960Sstevel@tonic-gate 		if (next_field(0, 06)) continue;
4970Sstevel@tonic-gate 		if (line[++cursor] == '\0') {
4980Sstevel@tonic-gate 			cerror(EOLN);
4990Sstevel@tonic-gate 			continue;
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate cont:
5020Sstevel@tonic-gate 		if (fputs(line, tfp) == EOF) {
5030Sstevel@tonic-gate 			unlink(tnam);
5040Sstevel@tonic-gate 			crabort(BADCREATE);
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 	fclose(fp);
5080Sstevel@tonic-gate 	fclose(tfp);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	/* audit differences between old and new crontabs */
5110Sstevel@tonic-gate 	audit_crontab_modify(cf, tnam, err);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (!err) {
5140Sstevel@tonic-gate 		/* make file tfp the new crontab */
5150Sstevel@tonic-gate 		unlink(cf);
5160Sstevel@tonic-gate 		if (link(tnam, cf) == -1) {
5170Sstevel@tonic-gate 			unlink(tnam);
5180Sstevel@tonic-gate 			crabort(BADCREATE);
5190Sstevel@tonic-gate 		}
5208998SViswanathan.Kannappan@Sun.COM 	} else {
5218998SViswanathan.Kannappan@Sun.COM 		crabort(ERRSFND);
5228998SViswanathan.Kannappan@Sun.COM 	}
5230Sstevel@tonic-gate 	unlink(tnam);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate static int
next_field(lower,upper)5270Sstevel@tonic-gate next_field(lower, upper)
5280Sstevel@tonic-gate int lower, upper;
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	int num, num2;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	while ((line[cursor] == ' ') || (line[cursor] == '\t')) cursor++;
5330Sstevel@tonic-gate 	if (line[cursor] == '\0') {
5340Sstevel@tonic-gate 		cerror(EOLN);
5350Sstevel@tonic-gate 		return (1);
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 	if (line[cursor] == '*') {
5380Sstevel@tonic-gate 		cursor++;
5390Sstevel@tonic-gate 		if ((line[cursor] != ' ') && (line[cursor] != '\t')) {
5400Sstevel@tonic-gate 			cerror(UNEXPECT);
5410Sstevel@tonic-gate 			return (1);
5420Sstevel@tonic-gate 		}
5430Sstevel@tonic-gate 		return (0);
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 	while (TRUE) {
5460Sstevel@tonic-gate 		if (!isdigit(line[cursor])) {
5470Sstevel@tonic-gate 			cerror(UNEXPECT);
5480Sstevel@tonic-gate 			return (1);
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		num = 0;
5510Sstevel@tonic-gate 		do {
5520Sstevel@tonic-gate 			num = num*10 + (line[cursor]-'0');
5530Sstevel@tonic-gate 		} while (isdigit(line[++cursor]));
5540Sstevel@tonic-gate 		if ((num < lower) || (num > upper)) {
5550Sstevel@tonic-gate 			cerror(OUTOFBOUND);
5560Sstevel@tonic-gate 			return (1);
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		if (line[cursor] == '-') {
5590Sstevel@tonic-gate 			if (!isdigit(line[++cursor])) {
5600Sstevel@tonic-gate 				cerror(UNEXPECT);
5610Sstevel@tonic-gate 				return (1);
5620Sstevel@tonic-gate 			}
5630Sstevel@tonic-gate 			num2 = 0;
5640Sstevel@tonic-gate 			do {
5650Sstevel@tonic-gate 				num2 = num2*10 + (line[cursor]-'0');
5660Sstevel@tonic-gate 			} while (isdigit(line[++cursor]));
5670Sstevel@tonic-gate 			if ((num2 < lower) || (num2 > upper)) {
5680Sstevel@tonic-gate 				cerror(OUTOFBOUND);
5690Sstevel@tonic-gate 				return (1);
5700Sstevel@tonic-gate 			}
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 		if ((line[cursor] == ' ') || (line[cursor] == '\t')) break;
5730Sstevel@tonic-gate 		if (line[cursor] == '\0') {
5740Sstevel@tonic-gate 			cerror(EOLN);
5750Sstevel@tonic-gate 			return (1);
5760Sstevel@tonic-gate 		}
5770Sstevel@tonic-gate 		if (line[cursor++] != ',') {
5780Sstevel@tonic-gate 			cerror(UNEXPECT);
5790Sstevel@tonic-gate 			return (1);
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 	return (0);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate static void
cerror(msg)5860Sstevel@tonic-gate cerror(msg)
5870Sstevel@tonic-gate char *msg;
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	fprintf(stderr, gettext("%scrontab: error on previous line; %s\n"),
5900Sstevel@tonic-gate 	    line, msg);
5910Sstevel@tonic-gate 	err = 1;
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate static void
catch(int x)5960Sstevel@tonic-gate catch(int x)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	unlink(tnam);
5990Sstevel@tonic-gate 	exit(1);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate static void
crabort(msg)6030Sstevel@tonic-gate crabort(msg)
6040Sstevel@tonic-gate char *msg;
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate 	int sverrno;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (strcmp(edtemp, "") != 0) {
6090Sstevel@tonic-gate 		sverrno = errno;
6100Sstevel@tonic-gate 		(void) unlink(edtemp);
6110Sstevel@tonic-gate 		errno = sverrno;
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 	if (tnam != NULL) {
6140Sstevel@tonic-gate 		sverrno = errno;
6150Sstevel@tonic-gate 		(void) unlink(tnam);
6160Sstevel@tonic-gate 		errno = sverrno;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	fprintf(stderr, "crontab: %s\n", gettext(msg));
6190Sstevel@tonic-gate 	exit(1);
6200Sstevel@tonic-gate }
621