1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/stat.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/wait.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <signal.h>
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <string.h>
41*0Sstevel@tonic-gate #include <fcntl.h>
42*0Sstevel@tonic-gate #include <ctype.h>
43*0Sstevel@tonic-gate #include <pwd.h>
44*0Sstevel@tonic-gate #include <unistd.h>
45*0Sstevel@tonic-gate #include <locale.h>
46*0Sstevel@tonic-gate #include <nl_types.h>
47*0Sstevel@tonic-gate #include <langinfo.h>
48*0Sstevel@tonic-gate #include <libintl.h>
49*0Sstevel@tonic-gate #include <security/pam_appl.h>
50*0Sstevel@tonic-gate #include "cron.h"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	TMPFILE		"_cron"		/* prefix for tmp file */
53*0Sstevel@tonic-gate #define	CRMODE		0600	/* mode for creating crontabs */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #define	BADCREATE	\
56*0Sstevel@tonic-gate 	"can't create your crontab file in the crontab directory."
57*0Sstevel@tonic-gate #define	BADOPEN		"can't open your crontab file."
58*0Sstevel@tonic-gate #define	BADSHELL	\
59*0Sstevel@tonic-gate 	"because your login shell isn't /usr/bin/sh, you can't use cron."
60*0Sstevel@tonic-gate #define	WARNSHELL	"warning: commands will be executed using /usr/bin/sh\n"
61*0Sstevel@tonic-gate #define	BADUSAGE	\
62*0Sstevel@tonic-gate 	"proper usage is: \n	crontab [file | -e | -l | -r ] [user]"
63*0Sstevel@tonic-gate #define	INVALIDUSER	"you are not a valid user (no entry in /etc/passwd)."
64*0Sstevel@tonic-gate #define	NOTALLOWED	"you are not authorized to use cron.  Sorry."
65*0Sstevel@tonic-gate #define	NOTROOT		\
66*0Sstevel@tonic-gate 	"you must be super-user to access another user's crontab file"
67*0Sstevel@tonic-gate #define	AUDITREJECT	"The audit context for your shell has not been set."
68*0Sstevel@tonic-gate #define	EOLN		"unexpected end of line."
69*0Sstevel@tonic-gate #define	UNEXPECT	"unexpected character found in line."
70*0Sstevel@tonic-gate #define	OUTOFBOUND	"number out of bounds."
71*0Sstevel@tonic-gate #define	ERRSFND		"errors detected in input, no crontab file generated."
72*0Sstevel@tonic-gate #define	ED_ERROR	\
73*0Sstevel@tonic-gate 	"     The editor indicates that an error occurred while you were\n"\
74*0Sstevel@tonic-gate 	"     editing the crontab data - usually a minor typing error.\n\n"
75*0Sstevel@tonic-gate #define	BADREAD		"error reading your crontab file"
76*0Sstevel@tonic-gate #define	ED_PROMPT	\
77*0Sstevel@tonic-gate 	"     Edit again, to ensure crontab information is intact (%c/%c)?\n"\
78*0Sstevel@tonic-gate 	"     ('%c' will discard edits.)"
79*0Sstevel@tonic-gate #define	NAMETOOLONG	"login name too long"
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate extern int	per_errno;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate extern int	audit_crontab_modify(char *, char *, int);
84*0Sstevel@tonic-gate extern int	audit_crontab_delete(char *, int);
85*0Sstevel@tonic-gate extern int	audit_crontab_not_allowed(uid_t, char *);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate int		err;
88*0Sstevel@tonic-gate int		cursor;
89*0Sstevel@tonic-gate char		*cf;
90*0Sstevel@tonic-gate char		*tnam;
91*0Sstevel@tonic-gate char		edtemp[5+13+1];
92*0Sstevel@tonic-gate char		line[CTLINESIZE];
93*0Sstevel@tonic-gate static		char	login[UNAMESIZE];
94*0Sstevel@tonic-gate static		char	yeschr;
95*0Sstevel@tonic-gate static		char	nochr;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate static int yes(void);
98*0Sstevel@tonic-gate static int next_field(int, int);
99*0Sstevel@tonic-gate static void catch(int);
100*0Sstevel@tonic-gate static void crabort(char *);
101*0Sstevel@tonic-gate static void cerror(char *);
102*0Sstevel@tonic-gate static void copycron(FILE *);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate main(argc, argv)
105*0Sstevel@tonic-gate int	argc;
106*0Sstevel@tonic-gate char	**argv;
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	int	c, r;
109*0Sstevel@tonic-gate 	int	rflag	= 0;
110*0Sstevel@tonic-gate 	int	lflag	= 0;
111*0Sstevel@tonic-gate 	int	eflag	= 0;
112*0Sstevel@tonic-gate 	int	errflg	= 0;
113*0Sstevel@tonic-gate 	char *pp;
114*0Sstevel@tonic-gate 	FILE *fp, *tmpfp;
115*0Sstevel@tonic-gate 	struct stat stbuf;
116*0Sstevel@tonic-gate 	struct passwd *pwp;
117*0Sstevel@tonic-gate 	time_t omodtime;
118*0Sstevel@tonic-gate 	char *editor;
119*0Sstevel@tonic-gate 	char buf[BUFSIZ];
120*0Sstevel@tonic-gate 	uid_t ruid;
121*0Sstevel@tonic-gate 	pid_t pid;
122*0Sstevel@tonic-gate 	int stat_loc;
123*0Sstevel@tonic-gate 	int ret;
124*0Sstevel@tonic-gate 	char real_login[UNAMESIZE];
125*0Sstevel@tonic-gate 	int tmpfd = -1;
126*0Sstevel@tonic-gate 	pam_handle_t *pamh;
127*0Sstevel@tonic-gate 	int pam_error;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
130*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
131*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
132*0Sstevel@tonic-gate #endif
133*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
134*0Sstevel@tonic-gate 	yeschr = *nl_langinfo(YESSTR);
135*0Sstevel@tonic-gate 	nochr = *nl_langinfo(NOSTR);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "elr")) != EOF)
138*0Sstevel@tonic-gate 		switch (c) {
139*0Sstevel@tonic-gate 			case 'e':
140*0Sstevel@tonic-gate 				eflag++;
141*0Sstevel@tonic-gate 				break;
142*0Sstevel@tonic-gate 			case 'l':
143*0Sstevel@tonic-gate 				lflag++;
144*0Sstevel@tonic-gate 				break;
145*0Sstevel@tonic-gate 			case 'r':
146*0Sstevel@tonic-gate 				rflag++;
147*0Sstevel@tonic-gate 				break;
148*0Sstevel@tonic-gate 			case '?':
149*0Sstevel@tonic-gate 				errflg++;
150*0Sstevel@tonic-gate 				break;
151*0Sstevel@tonic-gate 		}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (eflag + lflag + rflag > 1)
154*0Sstevel@tonic-gate 		errflg++;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	argc -= optind;
157*0Sstevel@tonic-gate 	argv += optind;
158*0Sstevel@tonic-gate 	if (errflg || argc > 1)
159*0Sstevel@tonic-gate 		crabort(BADUSAGE);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	ruid = getuid();
162*0Sstevel@tonic-gate 	if ((pwp = getpwuid(ruid)) == NULL)
163*0Sstevel@tonic-gate 		crabort(INVALIDUSER);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (strlcpy(real_login, pwp->pw_name, sizeof (real_login))
166*0Sstevel@tonic-gate 	    >= sizeof (real_login))
167*0Sstevel@tonic-gate 		crabort(NAMETOOLONG);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if ((eflag || lflag || rflag) && argc == 1) {
170*0Sstevel@tonic-gate 		if ((pwp = getpwnam(*argv)) == NULL)
171*0Sstevel@tonic-gate 			crabort(INVALIDUSER);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		if (!chkauthattr(CRONADMIN_AUTH, real_login)) {
174*0Sstevel@tonic-gate 			if (pwp->pw_uid != ruid)
175*0Sstevel@tonic-gate 				crabort(NOTROOT);
176*0Sstevel@tonic-gate 			else
177*0Sstevel@tonic-gate 				pp = getuser(ruid);
178*0Sstevel@tonic-gate 		} else
179*0Sstevel@tonic-gate 			pp = *argv++;
180*0Sstevel@tonic-gate 	} else {
181*0Sstevel@tonic-gate 		pp = getuser(ruid);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (pp == NULL) {
185*0Sstevel@tonic-gate 		if (per_errno == 2)
186*0Sstevel@tonic-gate 			crabort(BADSHELL);
187*0Sstevel@tonic-gate 		else
188*0Sstevel@tonic-gate 			crabort(INVALIDUSER);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 	if (strlcpy(login, pp, sizeof (login)) >= sizeof (login))
191*0Sstevel@tonic-gate 		crabort(NAMETOOLONG);
192*0Sstevel@tonic-gate 	if (!allowed(login, CRONALLOW, CRONDENY))
193*0Sstevel@tonic-gate 		crabort(NOTALLOWED);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	/* Do account validation check */
196*0Sstevel@tonic-gate 	pam_error = pam_start("cron", pp, NULL, &pamh);
197*0Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
198*0Sstevel@tonic-gate 		crabort((char *)pam_strerror(pamh, pam_error));
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 	pam_error = pam_acct_mgmt(pamh, PAM_SILENT);
201*0Sstevel@tonic-gate 	if (pam_error != PAM_SUCCESS) {
202*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning - Invalid account: "
203*0Sstevel@tonic-gate 		    "'%s' not allowed to execute cronjobs\n"), pp);
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 	(void) pam_end(pamh, PAM_SUCCESS);
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* check for unaudited shell */
209*0Sstevel@tonic-gate 	if (audit_crontab_not_allowed(ruid, pp))
210*0Sstevel@tonic-gate 		crabort(AUDITREJECT);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	cf = xmalloc(strlen(CRONDIR)+strlen(login)+2);
213*0Sstevel@tonic-gate 	strcat(strcat(strcpy(cf, CRONDIR), "/"), login);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (rflag) {
216*0Sstevel@tonic-gate 		r = unlink(cf);
217*0Sstevel@tonic-gate 		cron_sendmsg(DELETE, login, login, CRON);
218*0Sstevel@tonic-gate 		audit_crontab_delete(cf, r);
219*0Sstevel@tonic-gate 		exit(0);
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 	if (lflag) {
222*0Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL)
223*0Sstevel@tonic-gate 			crabort(BADOPEN);
224*0Sstevel@tonic-gate 		while (fgets(line, CTLINESIZE, fp) != NULL)
225*0Sstevel@tonic-gate 			fputs(line, stdout);
226*0Sstevel@tonic-gate 		fclose(fp);
227*0Sstevel@tonic-gate 		exit(0);
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	if (eflag) {
230*0Sstevel@tonic-gate 		if ((fp = fopen(cf, "r")) == NULL) {
231*0Sstevel@tonic-gate 			if (errno != ENOENT)
232*0Sstevel@tonic-gate 				crabort(BADOPEN);
233*0Sstevel@tonic-gate 		}
234*0Sstevel@tonic-gate 		(void) strcpy(edtemp, "/tmp/crontabXXXXXX");
235*0Sstevel@tonic-gate 		tmpfd = mkstemp(edtemp);
236*0Sstevel@tonic-gate 		if (fchown(tmpfd, ruid, -1) == -1) {
237*0Sstevel@tonic-gate 			(void) close(tmpfd);
238*0Sstevel@tonic-gate 			crabort("fchown of temporary file failed");
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 		(void) close(tmpfd);
241*0Sstevel@tonic-gate 		/*
242*0Sstevel@tonic-gate 		 * Fork off a child with user's permissions,
243*0Sstevel@tonic-gate 		 * to edit the crontab file
244*0Sstevel@tonic-gate 		 */
245*0Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)-1)
246*0Sstevel@tonic-gate 			crabort("fork failed");
247*0Sstevel@tonic-gate 		if (pid == 0) {		/* child process */
248*0Sstevel@tonic-gate 			/* give up super-user privileges. */
249*0Sstevel@tonic-gate 			setuid(ruid);
250*0Sstevel@tonic-gate 			if ((tmpfp = fopen(edtemp, "w")) == NULL)
251*0Sstevel@tonic-gate 				crabort("can't create temporary file");
252*0Sstevel@tonic-gate 			if (fp != NULL) {
253*0Sstevel@tonic-gate 				/*
254*0Sstevel@tonic-gate 				 * Copy user's crontab file to temporary file.
255*0Sstevel@tonic-gate 				 */
256*0Sstevel@tonic-gate 				while (fgets(line, CTLINESIZE, fp) != NULL) {
257*0Sstevel@tonic-gate 					fputs(line, tmpfp);
258*0Sstevel@tonic-gate 					if (ferror(tmpfp)) {
259*0Sstevel@tonic-gate 						fclose(fp);
260*0Sstevel@tonic-gate 						fclose(tmpfp);
261*0Sstevel@tonic-gate 						crabort("write error on"
262*0Sstevel@tonic-gate 						    "temporary file");
263*0Sstevel@tonic-gate 					}
264*0Sstevel@tonic-gate 				}
265*0Sstevel@tonic-gate 				if (ferror(fp)) {
266*0Sstevel@tonic-gate 					fclose(fp);
267*0Sstevel@tonic-gate 					fclose(tmpfp);
268*0Sstevel@tonic-gate 					crabort(BADREAD);
269*0Sstevel@tonic-gate 				}
270*0Sstevel@tonic-gate 				fclose(fp);
271*0Sstevel@tonic-gate 			}
272*0Sstevel@tonic-gate 			if (fclose(tmpfp) == EOF)
273*0Sstevel@tonic-gate 				crabort("write error on temporary file");
274*0Sstevel@tonic-gate 			if (stat(edtemp, &stbuf) < 0)
275*0Sstevel@tonic-gate 				crabort("can't stat temporary file");
276*0Sstevel@tonic-gate 			omodtime = stbuf.st_mtime;
277*0Sstevel@tonic-gate 			editor = getenv("VISUAL");
278*0Sstevel@tonic-gate 			if (editor == NULL)
279*0Sstevel@tonic-gate 				editor = getenv("EDITOR");
280*0Sstevel@tonic-gate 			if (editor == NULL)
281*0Sstevel@tonic-gate 				editor = "ed";
282*0Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf),
283*0Sstevel@tonic-gate 				"%s %s", editor, edtemp);
284*0Sstevel@tonic-gate 			sleep(1);
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 			while (1) {
287*0Sstevel@tonic-gate 				ret = system(buf);
288*0Sstevel@tonic-gate 				/* sanity checks */
289*0Sstevel@tonic-gate 				if ((tmpfp = fopen(edtemp, "r")) == NULL)
290*0Sstevel@tonic-gate 				    crabort("can't open temporary file");
291*0Sstevel@tonic-gate 				if (fstat(fileno(tmpfp), &stbuf) < 0)
292*0Sstevel@tonic-gate 				    crabort("can't stat temporary file");
293*0Sstevel@tonic-gate 				if (stbuf.st_size == 0)
294*0Sstevel@tonic-gate 				    crabort("temporary file empty");
295*0Sstevel@tonic-gate 				if (omodtime == stbuf.st_mtime) {
296*0Sstevel@tonic-gate 				    (void) unlink(edtemp);
297*0Sstevel@tonic-gate 				    fprintf(stderr, gettext(
298*0Sstevel@tonic-gate 					"The crontab file was not changed.\n"));
299*0Sstevel@tonic-gate 				    exit(1);
300*0Sstevel@tonic-gate 				}
301*0Sstevel@tonic-gate 				if ((ret) && (errno != EINTR)) {
302*0Sstevel@tonic-gate 				/*
303*0Sstevel@tonic-gate 				 * Some editors (like 'vi') can return
304*0Sstevel@tonic-gate 				 * a non-zero exit status even though
305*0Sstevel@tonic-gate 				 * everything is okay. Need to check.
306*0Sstevel@tonic-gate 				 */
307*0Sstevel@tonic-gate 				fprintf(stderr, gettext(ED_ERROR));
308*0Sstevel@tonic-gate 				fflush(stderr);
309*0Sstevel@tonic-gate 				if (isatty(fileno(stdin))) {
310*0Sstevel@tonic-gate 				    /* Interactive */
311*0Sstevel@tonic-gate 					fprintf(stdout, gettext(ED_PROMPT),
312*0Sstevel@tonic-gate 					    yeschr, nochr, nochr);
313*0Sstevel@tonic-gate 					fflush(stdout);
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 					if (yes()) {
316*0Sstevel@tonic-gate 						/* Edit again */
317*0Sstevel@tonic-gate 						continue;
318*0Sstevel@tonic-gate 					} else {
319*0Sstevel@tonic-gate 						/* Dump changes */
320*0Sstevel@tonic-gate 						(void) unlink(edtemp);
321*0Sstevel@tonic-gate 						exit(1);
322*0Sstevel@tonic-gate 					}
323*0Sstevel@tonic-gate 				} else {
324*0Sstevel@tonic-gate 				    /* Non-interactive, dump changes */
325*0Sstevel@tonic-gate 				    (void) unlink(edtemp);
326*0Sstevel@tonic-gate 				    exit(1);
327*0Sstevel@tonic-gate 				}
328*0Sstevel@tonic-gate 			}
329*0Sstevel@tonic-gate 			exit(0);
330*0Sstevel@tonic-gate 			} /* while (1) */
331*0Sstevel@tonic-gate 		}
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 		/* fix for 1125555 - ignore common signals while waiting */
334*0Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_IGN);
335*0Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_IGN);
336*0Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_IGN);
337*0Sstevel@tonic-gate 		(void) signal(SIGTERM, SIG_IGN);
338*0Sstevel@tonic-gate 		wait(&stat_loc);
339*0Sstevel@tonic-gate 		if ((stat_loc & 0xFF00) != 0)
340*0Sstevel@tonic-gate 			exit(1);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 		if ((seteuid(ruid) < 0) ||
343*0Sstevel@tonic-gate 		    ((tmpfp = fopen(edtemp, "r")) == NULL)) {
344*0Sstevel@tonic-gate 			fprintf(stderr, "crontab: %s: %s\n",
345*0Sstevel@tonic-gate 			    edtemp, errmsg(errno));
346*0Sstevel@tonic-gate 			(void) unlink(edtemp);
347*0Sstevel@tonic-gate 			exit(1);
348*0Sstevel@tonic-gate 		} else
349*0Sstevel@tonic-gate 			seteuid(0);
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 		copycron(tmpfp);
352*0Sstevel@tonic-gate 		(void) unlink(edtemp);
353*0Sstevel@tonic-gate 	} else {
354*0Sstevel@tonic-gate 		if (argc == 0)
355*0Sstevel@tonic-gate 			copycron(stdin);
356*0Sstevel@tonic-gate 		else if (seteuid(getuid()) != 0 || (fp = fopen(argv[0], "r"))
357*0Sstevel@tonic-gate 		    == NULL)
358*0Sstevel@tonic-gate 			crabort(BADOPEN);
359*0Sstevel@tonic-gate 		else {
360*0Sstevel@tonic-gate 			seteuid(0);
361*0Sstevel@tonic-gate 			copycron(fp);
362*0Sstevel@tonic-gate 		}
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate 	cron_sendmsg(ADD, login, login, CRON);
365*0Sstevel@tonic-gate /*
366*0Sstevel@tonic-gate  *	if (per_errno == 2)
367*0Sstevel@tonic-gate  *		fprintf(stderr, gettext(WARNSHELL));
368*0Sstevel@tonic-gate  */
369*0Sstevel@tonic-gate 	return (0);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate static void
373*0Sstevel@tonic-gate copycron(fp)
374*0Sstevel@tonic-gate FILE *fp;
375*0Sstevel@tonic-gate {
376*0Sstevel@tonic-gate 	FILE *tfp;
377*0Sstevel@tonic-gate 	char pid[6], *tnam_end;
378*0Sstevel@tonic-gate 	int t;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	sprintf(pid, "%-5d", getpid());
381*0Sstevel@tonic-gate 	tnam = xmalloc(strlen(CRONDIR)+strlen(TMPFILE)+7);
382*0Sstevel@tonic-gate 	strcat(strcat(strcat(strcpy(tnam, CRONDIR), "/"), TMPFILE), pid);
383*0Sstevel@tonic-gate 	/* cut trailing blanks */
384*0Sstevel@tonic-gate 	tnam_end = strchr(tnam, ' ');
385*0Sstevel@tonic-gate 	if (tnam_end != NULL)
386*0Sstevel@tonic-gate 		*tnam_end = 0;
387*0Sstevel@tonic-gate 	/* catch SIGINT, SIGHUP, SIGQUIT signals */
388*0Sstevel@tonic-gate 	if (signal(SIGINT, catch) == SIG_IGN)
389*0Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
390*0Sstevel@tonic-gate 	if (signal(SIGHUP, catch) == SIG_IGN) signal(SIGHUP, SIG_IGN);
391*0Sstevel@tonic-gate 	if (signal(SIGQUIT, catch) == SIG_IGN) signal(SIGQUIT, SIG_IGN);
392*0Sstevel@tonic-gate 	if (signal(SIGTERM, catch) == SIG_IGN) signal(SIGTERM, SIG_IGN);
393*0Sstevel@tonic-gate 	if ((t = creat(tnam, CRMODE)) == -1) crabort(BADCREATE);
394*0Sstevel@tonic-gate 	if ((tfp = fdopen(t, "w")) == NULL) {
395*0Sstevel@tonic-gate 		unlink(tnam);
396*0Sstevel@tonic-gate 		crabort(BADCREATE);
397*0Sstevel@tonic-gate 	}
398*0Sstevel@tonic-gate 	err = 0;	/* if errors found, err set to 1 */
399*0Sstevel@tonic-gate 	while (fgets(line, CTLINESIZE, fp) != NULL) {
400*0Sstevel@tonic-gate 		cursor = 0;
401*0Sstevel@tonic-gate 		while (line[cursor] == ' ' || line[cursor] == '\t')
402*0Sstevel@tonic-gate 			cursor++;
403*0Sstevel@tonic-gate 		/* fix for 1039689 - treat blank line like a comment */
404*0Sstevel@tonic-gate 		if (line[cursor] == '#' || line[cursor] == '\n')
405*0Sstevel@tonic-gate 			goto cont;
406*0Sstevel@tonic-gate 		if (next_field(0, 59)) continue;
407*0Sstevel@tonic-gate 		if (next_field(0, 23)) continue;
408*0Sstevel@tonic-gate 		if (next_field(1, 31)) continue;
409*0Sstevel@tonic-gate 		if (next_field(1, 12)) continue;
410*0Sstevel@tonic-gate 		if (next_field(0, 06)) continue;
411*0Sstevel@tonic-gate 		if (line[++cursor] == '\0') {
412*0Sstevel@tonic-gate 			cerror(EOLN);
413*0Sstevel@tonic-gate 			continue;
414*0Sstevel@tonic-gate 		}
415*0Sstevel@tonic-gate cont:
416*0Sstevel@tonic-gate 		if (fputs(line, tfp) == EOF) {
417*0Sstevel@tonic-gate 			unlink(tnam);
418*0Sstevel@tonic-gate 			crabort(BADCREATE);
419*0Sstevel@tonic-gate 		}
420*0Sstevel@tonic-gate 	}
421*0Sstevel@tonic-gate 	fclose(fp);
422*0Sstevel@tonic-gate 	fclose(tfp);
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	/* audit differences between old and new crontabs */
425*0Sstevel@tonic-gate 	audit_crontab_modify(cf, tnam, err);
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	if (!err) {
428*0Sstevel@tonic-gate 		/* make file tfp the new crontab */
429*0Sstevel@tonic-gate 		unlink(cf);
430*0Sstevel@tonic-gate 		if (link(tnam, cf) == -1) {
431*0Sstevel@tonic-gate 			unlink(tnam);
432*0Sstevel@tonic-gate 			crabort(BADCREATE);
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 	} else
435*0Sstevel@tonic-gate 		fprintf(stderr, "crontab: %s\n", gettext(ERRSFND));
436*0Sstevel@tonic-gate 	unlink(tnam);
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate static int
440*0Sstevel@tonic-gate next_field(lower, upper)
441*0Sstevel@tonic-gate int lower, upper;
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate 	int num, num2;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	while ((line[cursor] == ' ') || (line[cursor] == '\t')) cursor++;
446*0Sstevel@tonic-gate 	if (line[cursor] == '\0') {
447*0Sstevel@tonic-gate 		cerror(EOLN);
448*0Sstevel@tonic-gate 		return (1);
449*0Sstevel@tonic-gate 	}
450*0Sstevel@tonic-gate 	if (line[cursor] == '*') {
451*0Sstevel@tonic-gate 		cursor++;
452*0Sstevel@tonic-gate 		if ((line[cursor] != ' ') && (line[cursor] != '\t')) {
453*0Sstevel@tonic-gate 			cerror(UNEXPECT);
454*0Sstevel@tonic-gate 			return (1);
455*0Sstevel@tonic-gate 		}
456*0Sstevel@tonic-gate 		return (0);
457*0Sstevel@tonic-gate 	}
458*0Sstevel@tonic-gate 	while (TRUE) {
459*0Sstevel@tonic-gate 		if (!isdigit(line[cursor])) {
460*0Sstevel@tonic-gate 			cerror(UNEXPECT);
461*0Sstevel@tonic-gate 			return (1);
462*0Sstevel@tonic-gate 		}
463*0Sstevel@tonic-gate 		num = 0;
464*0Sstevel@tonic-gate 		do {
465*0Sstevel@tonic-gate 			num = num*10 + (line[cursor]-'0');
466*0Sstevel@tonic-gate 		} while (isdigit(line[++cursor]));
467*0Sstevel@tonic-gate 		if ((num < lower) || (num > upper)) {
468*0Sstevel@tonic-gate 			cerror(OUTOFBOUND);
469*0Sstevel@tonic-gate 			return (1);
470*0Sstevel@tonic-gate 		}
471*0Sstevel@tonic-gate 		if (line[cursor] == '-') {
472*0Sstevel@tonic-gate 			if (!isdigit(line[++cursor])) {
473*0Sstevel@tonic-gate 				cerror(UNEXPECT);
474*0Sstevel@tonic-gate 				return (1);
475*0Sstevel@tonic-gate 			}
476*0Sstevel@tonic-gate 			num2 = 0;
477*0Sstevel@tonic-gate 			do {
478*0Sstevel@tonic-gate 				num2 = num2*10 + (line[cursor]-'0');
479*0Sstevel@tonic-gate 			} while (isdigit(line[++cursor]));
480*0Sstevel@tonic-gate 			if ((num2 < lower) || (num2 > upper)) {
481*0Sstevel@tonic-gate 				cerror(OUTOFBOUND);
482*0Sstevel@tonic-gate 				return (1);
483*0Sstevel@tonic-gate 			}
484*0Sstevel@tonic-gate 		}
485*0Sstevel@tonic-gate 		if ((line[cursor] == ' ') || (line[cursor] == '\t')) break;
486*0Sstevel@tonic-gate 		if (line[cursor] == '\0') {
487*0Sstevel@tonic-gate 			cerror(EOLN);
488*0Sstevel@tonic-gate 			return (1);
489*0Sstevel@tonic-gate 		}
490*0Sstevel@tonic-gate 		if (line[cursor++] != ',') {
491*0Sstevel@tonic-gate 			cerror(UNEXPECT);
492*0Sstevel@tonic-gate 			return (1);
493*0Sstevel@tonic-gate 		}
494*0Sstevel@tonic-gate 	}
495*0Sstevel@tonic-gate 	return (0);
496*0Sstevel@tonic-gate }
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate static void
499*0Sstevel@tonic-gate cerror(msg)
500*0Sstevel@tonic-gate char *msg;
501*0Sstevel@tonic-gate {
502*0Sstevel@tonic-gate 	fprintf(stderr, gettext("%scrontab: error on previous line; %s\n"),
503*0Sstevel@tonic-gate 	    line, msg);
504*0Sstevel@tonic-gate 	err = 1;
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate static void
509*0Sstevel@tonic-gate catch(int x)
510*0Sstevel@tonic-gate {
511*0Sstevel@tonic-gate 	unlink(tnam);
512*0Sstevel@tonic-gate 	exit(1);
513*0Sstevel@tonic-gate }
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate static void
516*0Sstevel@tonic-gate crabort(msg)
517*0Sstevel@tonic-gate char *msg;
518*0Sstevel@tonic-gate {
519*0Sstevel@tonic-gate 	int sverrno;
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	if (strcmp(edtemp, "") != 0) {
522*0Sstevel@tonic-gate 		sverrno = errno;
523*0Sstevel@tonic-gate 		(void) unlink(edtemp);
524*0Sstevel@tonic-gate 		errno = sverrno;
525*0Sstevel@tonic-gate 	}
526*0Sstevel@tonic-gate 	if (tnam != NULL) {
527*0Sstevel@tonic-gate 		sverrno = errno;
528*0Sstevel@tonic-gate 		(void) unlink(tnam);
529*0Sstevel@tonic-gate 		errno = sverrno;
530*0Sstevel@tonic-gate 	}
531*0Sstevel@tonic-gate 	fprintf(stderr, "crontab: %s\n", gettext(msg));
532*0Sstevel@tonic-gate 	exit(1);
533*0Sstevel@tonic-gate }
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate static int
536*0Sstevel@tonic-gate yes(void)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	int	first_char;
539*0Sstevel@tonic-gate 	int	dummy_char;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	first_char = dummy_char = getchar();
542*0Sstevel@tonic-gate 	while ((dummy_char != '\n')	&&
543*0Sstevel@tonic-gate 	    (dummy_char != '\0')	&&
544*0Sstevel@tonic-gate 	    (dummy_char != EOF))
545*0Sstevel@tonic-gate 		dummy_char = getchar();
546*0Sstevel@tonic-gate 	return (first_char == yeschr);
547*0Sstevel@tonic-gate }
548