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