xref: /freebsd/usr.sbin/cron/cron/do_command.c (revision 89c7bb561344767c6e3fa925553675145b1540f7)
184f33deaSJordan K. Hubbard /* Copyright 1988,1990,1993,1994 by Paul Vixie
284f33deaSJordan K. Hubbard  * All rights reserved
384f33deaSJordan K. Hubbard  *
484f33deaSJordan K. Hubbard  * Distribute freely, except: don't remove my name from the source or
584f33deaSJordan K. Hubbard  * documentation (don't take credit for my work), mark your changes (don't
684f33deaSJordan K. Hubbard  * get me blamed for your possible bugs), don't alter or remove this
784f33deaSJordan K. Hubbard  * notice.  May be sold if buildable source is provided to buyer.  No
884f33deaSJordan K. Hubbard  * warrantee of any kind, express or implied, is included with this
984f33deaSJordan K. Hubbard  * software; use at your own risk, responsibility for damages (if any) to
1084f33deaSJordan K. Hubbard  * anyone resulting from the use of this software rests entirely with the
1184f33deaSJordan K. Hubbard  * user.
1284f33deaSJordan K. Hubbard  *
1384f33deaSJordan K. Hubbard  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
1484f33deaSJordan K. Hubbard  * I'll try to keep a version up to date.  I can be reached as follows:
1584f33deaSJordan K. Hubbard  * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
1684f33deaSJordan K. Hubbard  */
1784f33deaSJordan K. Hubbard 
1884f33deaSJordan K. Hubbard #if !defined(lint) && !defined(LINT)
19401e6468SPhilippe Charnier static const char rcsid[] =
2097d92980SPeter Wemm   "$FreeBSD$";
2184f33deaSJordan K. Hubbard #endif
2284f33deaSJordan K. Hubbard 
2384f33deaSJordan K. Hubbard 
2484f33deaSJordan K. Hubbard #include "cron.h"
2584f33deaSJordan K. Hubbard #include <sys/signal.h>
2684f33deaSJordan K. Hubbard #if defined(sequent)
2784f33deaSJordan K. Hubbard # include <sys/universe.h>
2884f33deaSJordan K. Hubbard #endif
2984f33deaSJordan K. Hubbard #if defined(SYSLOG)
3084f33deaSJordan K. Hubbard # include <syslog.h>
3184f33deaSJordan K. Hubbard #endif
32b25b7bc1SDavid Nugent #if defined(LOGIN_CAP)
33b25b7bc1SDavid Nugent # include <login_cap.h>
34b25b7bc1SDavid Nugent #endif
35997c6eefSYaroslav Tykhiy #ifdef PAM
36997c6eefSYaroslav Tykhiy # include <security/pam_appl.h>
37997c6eefSYaroslav Tykhiy # include <security/openpam.h>
38997c6eefSYaroslav Tykhiy #endif
3984f33deaSJordan K. Hubbard 
4084f33deaSJordan K. Hubbard 
411709a13cSKyle Evans static void		child_process(entry *, user *);
4284f33deaSJordan K. Hubbard 
435b80de23SKyle Evans static WAIT_T		wait_on_child(PID_T, const char *);
4484f33deaSJordan K. Hubbard 
457466dbd6SKyle Evans extern char	*environ;
467466dbd6SKyle Evans 
4784f33deaSJordan K. Hubbard void
4884f33deaSJordan K. Hubbard do_command(e, u)
4984f33deaSJordan K. Hubbard 	entry	*e;
5084f33deaSJordan K. Hubbard 	user	*u;
5184f33deaSJordan K. Hubbard {
52a08d12d3SGleb Smirnoff 	pid_t pid;
53a08d12d3SGleb Smirnoff 
5484f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
5584f33deaSJordan K. Hubbard 		getpid(), e->cmd, u->name, e->uid, e->gid))
5684f33deaSJordan K. Hubbard 
5784f33deaSJordan K. Hubbard 	/* fork to become asynchronous -- parent process is done immediately,
5884f33deaSJordan K. Hubbard 	 * and continues to run the normal cron code, which means return to
5984f33deaSJordan K. Hubbard 	 * tick().  the child and grandchild don't leave this function, alive.
6084f33deaSJordan K. Hubbard 	 */
61a08d12d3SGleb Smirnoff 	switch ((pid = fork())) {
6284f33deaSJordan K. Hubbard 	case -1:
6384f33deaSJordan K. Hubbard 		log_it("CRON",getpid(),"error","can't fork");
64a08d12d3SGleb Smirnoff 		if (e->flags & INTERVAL)
65a08d12d3SGleb Smirnoff 			e->lastexit = time(NULL);
6684f33deaSJordan K. Hubbard 		break;
6784f33deaSJordan K. Hubbard 	case 0:
6884f33deaSJordan K. Hubbard 		/* child process */
6978735592SPawel Jakub Dawidek 		pidfile_close(pfh);
7084f33deaSJordan K. Hubbard 		child_process(e, u);
7184f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
7284f33deaSJordan K. Hubbard 		_exit(OK_EXIT);
7384f33deaSJordan K. Hubbard 		break;
7484f33deaSJordan K. Hubbard 	default:
7584f33deaSJordan K. Hubbard 		/* parent process */
76a08d12d3SGleb Smirnoff 		Debug(DPROC, ("[%d] main process forked child #%d, "
77a08d12d3SGleb Smirnoff 		    "returning to work\n", getpid(), pid))
78a08d12d3SGleb Smirnoff 		if (e->flags & INTERVAL) {
79a08d12d3SGleb Smirnoff 			e->lastexit = 0;
80a08d12d3SGleb Smirnoff 			e->child = pid;
81a08d12d3SGleb Smirnoff 		}
8284f33deaSJordan K. Hubbard 		break;
8384f33deaSJordan K. Hubbard 	}
8484f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
8584f33deaSJordan K. Hubbard }
8684f33deaSJordan K. Hubbard 
8784f33deaSJordan K. Hubbard 
8884f33deaSJordan K. Hubbard static void
8984f33deaSJordan K. Hubbard child_process(e, u)
9084f33deaSJordan K. Hubbard 	entry	*e;
9184f33deaSJordan K. Hubbard 	user	*u;
9284f33deaSJordan K. Hubbard {
9384f33deaSJordan K. Hubbard 	int		stdin_pipe[2], stdout_pipe[2];
9484f33deaSJordan K. Hubbard 	register char	*input_data;
9512455a9eSKyle Evans 	char		*usernm, *mailto, *mailfrom;
965b80de23SKyle Evans 	PID_T		jobpid, stdinjob, mailpid;
975b80de23SKyle Evans 	register FILE	*mail;
985b80de23SKyle Evans 	register int	bytes = 1;
995b80de23SKyle Evans 	int		status = 0;
100*89c7bb56SKyle Evans 	const char	*homedir = NULL;
101b25b7bc1SDavid Nugent # if defined(LOGIN_CAP)
102c00d650fSPeter Wemm 	struct passwd	*pwd;
1030435c150SAndrey A. Chernov 	login_cap_t *lc;
104b25b7bc1SDavid Nugent # endif
10584f33deaSJordan K. Hubbard 
10684f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
10784f33deaSJordan K. Hubbard 
10884f33deaSJordan K. Hubbard 	/* mark ourselves as different to PS command watchers by upshifting
10984f33deaSJordan K. Hubbard 	 * our program name.  This has no effect on some kernels.
11084f33deaSJordan K. Hubbard 	 */
111c7517d5aSPeter Wemm 	setproctitle("running job");
11284f33deaSJordan K. Hubbard 
11384f33deaSJordan K. Hubbard 	/* discover some useful and important environment settings
11484f33deaSJordan K. Hubbard 	 */
11584f33deaSJordan K. Hubbard 	usernm = env_get("LOGNAME", e->envp);
11684f33deaSJordan K. Hubbard 	mailto = env_get("MAILTO", e->envp);
11712455a9eSKyle Evans 	mailfrom = env_get("MAILFROM", e->envp);
11884f33deaSJordan K. Hubbard 
119997c6eefSYaroslav Tykhiy #ifdef PAM
120997c6eefSYaroslav Tykhiy 	/* use PAM to see if the user's account is available,
121997c6eefSYaroslav Tykhiy 	 * i.e., not locked or expired or whatever.  skip this
122997c6eefSYaroslav Tykhiy 	 * for system tasks from /etc/crontab -- they can run
123997c6eefSYaroslav Tykhiy 	 * as any user.
124997c6eefSYaroslav Tykhiy 	 */
125997c6eefSYaroslav Tykhiy 	if (strcmp(u->name, SYS_NAME)) {	/* not equal */
126997c6eefSYaroslav Tykhiy 		pam_handle_t *pamh = NULL;
127997c6eefSYaroslav Tykhiy 		int pam_err;
128997c6eefSYaroslav Tykhiy 		struct pam_conv pamc = {
129997c6eefSYaroslav Tykhiy 			.conv = openpam_nullconv,
130997c6eefSYaroslav Tykhiy 			.appdata_ptr = NULL
131587d674eSPedro F. Giffuni 		};
132997c6eefSYaroslav Tykhiy 
133997c6eefSYaroslav Tykhiy 		Debug(DPROC, ("[%d] checking account with PAM\n", getpid()))
134997c6eefSYaroslav Tykhiy 
135997c6eefSYaroslav Tykhiy 		/* u->name keeps crontab owner name while LOGNAME is the name
136997c6eefSYaroslav Tykhiy 		 * of user to run command on behalf of.  they should be the
137997c6eefSYaroslav Tykhiy 		 * same for a task from a per-user crontab.
138997c6eefSYaroslav Tykhiy 		 */
139997c6eefSYaroslav Tykhiy 		if (strcmp(u->name, usernm)) {
140997c6eefSYaroslav Tykhiy 			log_it(usernm, getpid(), "username ambiguity", u->name);
141997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
142997c6eefSYaroslav Tykhiy 		}
143997c6eefSYaroslav Tykhiy 
144997c6eefSYaroslav Tykhiy 		pam_err = pam_start("cron", usernm, &pamc, &pamh);
145997c6eefSYaroslav Tykhiy 		if (pam_err != PAM_SUCCESS) {
146997c6eefSYaroslav Tykhiy 			log_it("CRON", getpid(), "error", "can't start PAM");
147997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
148997c6eefSYaroslav Tykhiy 		}
149997c6eefSYaroslav Tykhiy 
150997c6eefSYaroslav Tykhiy 		pam_err = pam_acct_mgmt(pamh, PAM_SILENT);
151997c6eefSYaroslav Tykhiy 		/* Expired password shouldn't prevent the job from running. */
152997c6eefSYaroslav Tykhiy 		if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD) {
153997c6eefSYaroslav Tykhiy 			log_it(usernm, getpid(), "USER", "account unavailable");
154997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
155997c6eefSYaroslav Tykhiy 		}
156997c6eefSYaroslav Tykhiy 
157997c6eefSYaroslav Tykhiy 		pam_end(pamh, pam_err);
158997c6eefSYaroslav Tykhiy 	}
159997c6eefSYaroslav Tykhiy #endif
160997c6eefSYaroslav Tykhiy 
16184f33deaSJordan K. Hubbard #ifdef USE_SIGCHLD
16284f33deaSJordan K. Hubbard 	/* our parent is watching for our death by catching SIGCHLD.  we
16384f33deaSJordan K. Hubbard 	 * do not care to watch for our children's deaths this way -- we
1643df5ecacSUlrich Spörlein 	 * use wait() explicitly.  so we have to disable the signal (which
16584f33deaSJordan K. Hubbard 	 * was inherited from the parent).
16684f33deaSJordan K. Hubbard 	 */
1675e4a74ecSAndrey A. Chernov 	(void) signal(SIGCHLD, SIG_DFL);
16884f33deaSJordan K. Hubbard #else
16984f33deaSJordan K. Hubbard 	/* on system-V systems, we are ignoring SIGCLD.  we have to stop
17084f33deaSJordan K. Hubbard 	 * ignoring it now or the wait() in cron_pclose() won't work.
17184f33deaSJordan K. Hubbard 	 * because of this, we have to wait() for our children here, as well.
17284f33deaSJordan K. Hubbard 	 */
17384f33deaSJordan K. Hubbard 	(void) signal(SIGCLD, SIG_DFL);
17484f33deaSJordan K. Hubbard #endif /*BSD*/
17584f33deaSJordan K. Hubbard 
17684f33deaSJordan K. Hubbard 	/* create some pipes to talk to our future child
17784f33deaSJordan K. Hubbard 	 */
178d9850281SPedro F. Giffuni 	if (pipe(stdin_pipe) != 0 || pipe(stdout_pipe) != 0) {
179d9850281SPedro F. Giffuni 		log_it("CRON", getpid(), "error", "can't pipe");
180d9850281SPedro F. Giffuni 		exit(ERROR_EXIT);
181d9850281SPedro F. Giffuni 	}
18284f33deaSJordan K. Hubbard 
18384f33deaSJordan K. Hubbard 	/* since we are a forked process, we can diddle the command string
18484f33deaSJordan K. Hubbard 	 * we were passed -- nobody else is going to use it again, right?
18584f33deaSJordan K. Hubbard 	 *
18684f33deaSJordan K. Hubbard 	 * if a % is present in the command, previous characters are the
18784f33deaSJordan K. Hubbard 	 * command, and subsequent characters are the additional input to
18884f33deaSJordan K. Hubbard 	 * the command.  Subsequent %'s will be transformed into newlines,
18984f33deaSJordan K. Hubbard 	 * but that happens later.
19086ed6de3SJoerg Wunsch 	 *
19186ed6de3SJoerg Wunsch 	 * If there are escaped %'s, remove the escape character.
19284f33deaSJordan K. Hubbard 	 */
19384f33deaSJordan K. Hubbard 	/*local*/{
19484f33deaSJordan K. Hubbard 		register int escaped = FALSE;
19584f33deaSJordan K. Hubbard 		register int ch;
19686ed6de3SJoerg Wunsch 		register char *p;
19784f33deaSJordan K. Hubbard 
198401e6468SPhilippe Charnier 		for (input_data = p = e->cmd; (ch = *input_data);
19986ed6de3SJoerg Wunsch 		     input_data++, p++) {
20086ed6de3SJoerg Wunsch 			if (p != input_data)
20186ed6de3SJoerg Wunsch 			    *p = ch;
20284f33deaSJordan K. Hubbard 			if (escaped) {
20386ed6de3SJoerg Wunsch 				if (ch == '%' || ch == '\\')
20486ed6de3SJoerg Wunsch 					*--p = ch;
20584f33deaSJordan K. Hubbard 				escaped = FALSE;
20684f33deaSJordan K. Hubbard 				continue;
20784f33deaSJordan K. Hubbard 			}
20884f33deaSJordan K. Hubbard 			if (ch == '\\') {
20984f33deaSJordan K. Hubbard 				escaped = TRUE;
21084f33deaSJordan K. Hubbard 				continue;
21184f33deaSJordan K. Hubbard 			}
21284f33deaSJordan K. Hubbard 			if (ch == '%') {
21384f33deaSJordan K. Hubbard 				*input_data++ = '\0';
21484f33deaSJordan K. Hubbard 				break;
21584f33deaSJordan K. Hubbard 			}
21684f33deaSJordan K. Hubbard 		}
21786ed6de3SJoerg Wunsch 		*p = '\0';
21884f33deaSJordan K. Hubbard 	}
21984f33deaSJordan K. Hubbard 
22084f33deaSJordan K. Hubbard 	/* fork again, this time so we can exec the user's command.
22184f33deaSJordan K. Hubbard 	 */
2229b367233SKyle Evans 	switch (jobpid = fork()) {
22384f33deaSJordan K. Hubbard 	case -1:
2249b367233SKyle Evans 		log_it("CRON",getpid(),"error","can't fork");
22584f33deaSJordan K. Hubbard 		exit(ERROR_EXIT);
22684f33deaSJordan K. Hubbard 		/*NOTREACHED*/
22784f33deaSJordan K. Hubbard 	case 0:
2289b367233SKyle Evans 		Debug(DPROC, ("[%d] grandchild process fork()'ed\n",
22984f33deaSJordan K. Hubbard 			      getpid()))
23084f33deaSJordan K. Hubbard 
231f5896bafSYaroslav Tykhiy 		if (e->uid == ROOT_UID)
232f5896bafSYaroslav Tykhiy 			Jitter = RootJitter;
233f5896bafSYaroslav Tykhiy 		if (Jitter != 0) {
234f5896bafSYaroslav Tykhiy 			srandom(getpid());
235f5896bafSYaroslav Tykhiy 			sleep(random() % Jitter);
236f5896bafSYaroslav Tykhiy 		}
237f5896bafSYaroslav Tykhiy 
23884f33deaSJordan K. Hubbard 		/* write a log message.  we've waited this long to do it
23984f33deaSJordan K. Hubbard 		 * because it was not until now that we knew the PID that
24084f33deaSJordan K. Hubbard 		 * the actual user command shell was going to get and the
24184f33deaSJordan K. Hubbard 		 * PID is part of the log message.
24284f33deaSJordan K. Hubbard 		 */
2435b80de23SKyle Evans 		if ((e->flags & DONT_LOG) == 0) {
24484f33deaSJordan K. Hubbard 			char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
24584f33deaSJordan K. Hubbard 
24684f33deaSJordan K. Hubbard 			log_it(usernm, getpid(), "CMD", x);
24784f33deaSJordan K. Hubbard 			free(x);
24884f33deaSJordan K. Hubbard 		}
24984f33deaSJordan K. Hubbard 
25084f33deaSJordan K. Hubbard 		/* that's the last thing we'll log.  close the log files.
25184f33deaSJordan K. Hubbard 		 */
25284f33deaSJordan K. Hubbard #ifdef SYSLOG
25384f33deaSJordan K. Hubbard 		closelog();
25484f33deaSJordan K. Hubbard #endif
25584f33deaSJordan K. Hubbard 
25684f33deaSJordan K. Hubbard 		/* get new pgrp, void tty, etc.
25784f33deaSJordan K. Hubbard 		 */
25884f33deaSJordan K. Hubbard 		(void) setsid();
25984f33deaSJordan K. Hubbard 
26084f33deaSJordan K. Hubbard 		/* close the pipe ends that we won't use.  this doesn't affect
26184f33deaSJordan K. Hubbard 		 * the parent, who has to read and write them; it keeps the
26284f33deaSJordan K. Hubbard 		 * kernel from recording us as a potential client TWICE --
26384f33deaSJordan K. Hubbard 		 * which would keep it from sending SIGPIPE in otherwise
26484f33deaSJordan K. Hubbard 		 * appropriate circumstances.
26584f33deaSJordan K. Hubbard 		 */
26684f33deaSJordan K. Hubbard 		close(stdin_pipe[WRITE_PIPE]);
26784f33deaSJordan K. Hubbard 		close(stdout_pipe[READ_PIPE]);
26884f33deaSJordan K. Hubbard 
26984f33deaSJordan K. Hubbard 		/* grandchild process.  make std{in,out} be the ends of
27084f33deaSJordan K. Hubbard 		 * pipes opened by our daddy; make stderr go to stdout.
27184f33deaSJordan K. Hubbard 		 */
27284f33deaSJordan K. Hubbard 		close(STDIN);	dup2(stdin_pipe[READ_PIPE], STDIN);
27384f33deaSJordan K. Hubbard 		close(STDOUT);	dup2(stdout_pipe[WRITE_PIPE], STDOUT);
27484f33deaSJordan K. Hubbard 		close(STDERR);	dup2(STDOUT, STDERR);
27584f33deaSJordan K. Hubbard 
27684f33deaSJordan K. Hubbard 		/* close the pipes we just dup'ed.  The resources will remain.
27784f33deaSJordan K. Hubbard 		 */
27884f33deaSJordan K. Hubbard 		close(stdin_pipe[READ_PIPE]);
27984f33deaSJordan K. Hubbard 		close(stdout_pipe[WRITE_PIPE]);
28084f33deaSJordan K. Hubbard 
2817466dbd6SKyle Evans 		environ = NULL;
2827466dbd6SKyle Evans 
283b25b7bc1SDavid Nugent # if defined(LOGIN_CAP)
2847466dbd6SKyle Evans 		/* Set user's entire context, but note that PATH will
2857466dbd6SKyle Evans 		 * be overridden later
286b25b7bc1SDavid Nugent 		 */
2870435c150SAndrey A. Chernov 		if ((pwd = getpwnam(usernm)) == NULL)
288c00d650fSPeter Wemm 			pwd = getpwuid(e->uid);
2890435c150SAndrey A. Chernov 		lc = NULL;
2900435c150SAndrey A. Chernov 		if (pwd != NULL) {
291*89c7bb56SKyle Evans 			if (pwd->pw_dir != NULL
292*89c7bb56SKyle Evans 			    && pwd->pw_dir[0] != '\0') {
293*89c7bb56SKyle Evans 				homedir = strdup(pwd->pw_dir);
294*89c7bb56SKyle Evans 				if (homedir == NULL) {
295*89c7bb56SKyle Evans 					warn("strdup");
296*89c7bb56SKyle Evans 					_exit(ERROR_EXIT);
297*89c7bb56SKyle Evans 				}
298*89c7bb56SKyle Evans 			}
2990435c150SAndrey A. Chernov 			pwd->pw_gid = e->gid;
3000435c150SAndrey A. Chernov 			if (e->class != NULL)
3010435c150SAndrey A. Chernov 				lc = login_getclass(e->class);
3020435c150SAndrey A. Chernov 		}
3039efe2eccSPeter Wemm 		if (pwd &&
3040435c150SAndrey A. Chernov 		    setusercontext(lc, pwd, e->uid,
3057466dbd6SKyle Evans 			    LOGIN_SETALL) == 0)
3069efe2eccSPeter Wemm 			(void) endpwent();
3079efe2eccSPeter Wemm 		else {
308c00d650fSPeter Wemm 			/* fall back to the old method */
3099efe2eccSPeter Wemm 			(void) endpwent();
310c00d650fSPeter Wemm # endif
311c00d650fSPeter Wemm 			/* set our directory, uid and gid.  Set gid first,
312708f27a1SMaxim Konovalov 			 * since once we set uid, we've lost root privileges.
31384f33deaSJordan K. Hubbard 			 */
314bb0aa1a5SMaxim Konovalov 			if (setgid(e->gid) != 0) {
315bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
316bb0aa1a5SMaxim Konovalov 				    "error", "setgid failed");
3179b367233SKyle Evans 				_exit(ERROR_EXIT);
318bb0aa1a5SMaxim Konovalov 			}
31984f33deaSJordan K. Hubbard # if defined(BSD)
320bb0aa1a5SMaxim Konovalov 			if (initgroups(usernm, e->gid) != 0) {
321bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
322bb0aa1a5SMaxim Konovalov 				    "error", "initgroups failed");
3239b367233SKyle Evans 				_exit(ERROR_EXIT);
324bb0aa1a5SMaxim Konovalov 			}
32584f33deaSJordan K. Hubbard # endif
326bb0aa1a5SMaxim Konovalov 			if (setlogin(usernm) != 0) {
327bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
328bb0aa1a5SMaxim Konovalov 				    "error", "setlogin failed");
3299b367233SKyle Evans 				_exit(ERROR_EXIT);
330bb0aa1a5SMaxim Konovalov 			}
331bb0aa1a5SMaxim Konovalov 			if (setuid(e->uid) != 0) {
332bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
333bb0aa1a5SMaxim Konovalov 				    "error", "setuid failed");
3349b367233SKyle Evans 				_exit(ERROR_EXIT);
335bb0aa1a5SMaxim Konovalov 			}
336bb0aa1a5SMaxim Konovalov 			/* we aren't root after this..*/
337c00d650fSPeter Wemm #if defined(LOGIN_CAP)
338c00d650fSPeter Wemm 		}
3393d99cebfSAndrey A. Chernov 		if (lc != NULL)
3403d99cebfSAndrey A. Chernov 			login_close(lc);
341b25b7bc1SDavid Nugent #endif
34284f33deaSJordan K. Hubbard 
343*89c7bb56SKyle Evans 		/* For compatibility, we chdir to the value of HOME if it was
344*89c7bb56SKyle Evans 		 * specified explicitly in the crontab file, but not if it was
345*89c7bb56SKyle Evans 		 * set in the environment by some other mechanism. We chdir to
346*89c7bb56SKyle Evans 		 * the homedir given by the pw entry otherwise.
347*89c7bb56SKyle Evans 		 *
348*89c7bb56SKyle Evans 		 * If !LOGIN_CAP, then HOME is always set in e->envp.
349*89c7bb56SKyle Evans 		 *
350*89c7bb56SKyle Evans 		 * XXX: probably should also consult PAM.
351*89c7bb56SKyle Evans 		 */
352*89c7bb56SKyle Evans 		{
353*89c7bb56SKyle Evans 			char	*new_home = env_get("HOME", e->envp);
354*89c7bb56SKyle Evans 			if (new_home != NULL && new_home[0] != '\0')
355*89c7bb56SKyle Evans 				chdir(new_home);
356*89c7bb56SKyle Evans 			else if (homedir != NULL)
357*89c7bb56SKyle Evans 				chdir(homedir);
358*89c7bb56SKyle Evans 			else
359*89c7bb56SKyle Evans 				chdir("/");
360*89c7bb56SKyle Evans 		}
361*89c7bb56SKyle Evans 
362*89c7bb56SKyle Evans 		/* exec the command. Note that SHELL is not respected from
363*89c7bb56SKyle Evans 		 * either login.conf or pw_shell, only an explicit setting
364*89c7bb56SKyle Evans 		 * in the crontab. (default of _PATH_BSHELL is supplied when
365*89c7bb56SKyle Evans 		 * setting up the entry)
36684f33deaSJordan K. Hubbard 		 */
36784f33deaSJordan K. Hubbard 		{
36884f33deaSJordan K. Hubbard 			char	*shell = env_get("SHELL", e->envp);
3697466dbd6SKyle Evans 			char	**p;
3707466dbd6SKyle Evans 
371*89c7bb56SKyle Evans 			/* Apply the environment from the entry, overriding
372*89c7bb56SKyle Evans 			 * existing values (this will always set LOGNAME and
373*89c7bb56SKyle Evans 			 * SHELL). putenv should not fail unless malloc does.
3747466dbd6SKyle Evans 			 */
3757466dbd6SKyle Evans 			for (p = e->envp; *p; ++p) {
3767466dbd6SKyle Evans 				if (putenv(*p) != 0) {
3777466dbd6SKyle Evans 					warn("putenv");
3787466dbd6SKyle Evans 					_exit(ERROR_EXIT);
3797466dbd6SKyle Evans 				}
3807466dbd6SKyle Evans 			}
38184f33deaSJordan K. Hubbard 
382*89c7bb56SKyle Evans 			/* HOME in login.conf overrides pw, and HOME in the
383*89c7bb56SKyle Evans 			 * crontab overrides both. So set pw's value only if
384*89c7bb56SKyle Evans 			 * nothing was already set (overwrite==0).
385*89c7bb56SKyle Evans 			 */
386*89c7bb56SKyle Evans 			if (homedir != NULL
387*89c7bb56SKyle Evans 			    && setenv("HOME", homedir, 0) < 0) {
388*89c7bb56SKyle Evans 				warn("setenv(HOME)");
389*89c7bb56SKyle Evans 				_exit(ERROR_EXIT);
390*89c7bb56SKyle Evans 			}
391*89c7bb56SKyle Evans 
392*89c7bb56SKyle Evans 			/* PATH in login.conf is respected, but the crontab
393*89c7bb56SKyle Evans 			 * overrides; set a default value only if nothing
394*89c7bb56SKyle Evans 			 * already set.
395*89c7bb56SKyle Evans 			 */
396*89c7bb56SKyle Evans 			if (setenv("PATH", _PATH_DEFPATH, 0) < 0) {
397*89c7bb56SKyle Evans 				warn("setenv(PATH)");
398*89c7bb56SKyle Evans 				_exit(ERROR_EXIT);
399*89c7bb56SKyle Evans 			}
400*89c7bb56SKyle Evans 
40184f33deaSJordan K. Hubbard # if DEBUGGING
40284f33deaSJordan K. Hubbard 			if (DebugFlags & DTEST) {
40384f33deaSJordan K. Hubbard 				fprintf(stderr,
40484f33deaSJordan K. Hubbard 				"debug DTEST is on, not exec'ing command.\n");
40584f33deaSJordan K. Hubbard 				fprintf(stderr,
40684f33deaSJordan K. Hubbard 				"\tcmd='%s' shell='%s'\n", e->cmd, shell);
40784f33deaSJordan K. Hubbard 				_exit(OK_EXIT);
40884f33deaSJordan K. Hubbard 			}
40984f33deaSJordan K. Hubbard # endif /*DEBUGGING*/
4107466dbd6SKyle Evans 			execl(shell, shell, "-c", e->cmd, (char *)NULL);
4117466dbd6SKyle Evans 			warn("execl: couldn't exec `%s'", shell);
41284f33deaSJordan K. Hubbard 			_exit(ERROR_EXIT);
41384f33deaSJordan K. Hubbard 		}
41484f33deaSJordan K. Hubbard 		break;
41584f33deaSJordan K. Hubbard 	default:
41684f33deaSJordan K. Hubbard 		/* parent process */
41784f33deaSJordan K. Hubbard 		break;
41884f33deaSJordan K. Hubbard 	}
41984f33deaSJordan K. Hubbard 
42084f33deaSJordan K. Hubbard 	/* middle process, child of original cron, parent of process running
42184f33deaSJordan K. Hubbard 	 * the user's command.
42284f33deaSJordan K. Hubbard 	 */
42384f33deaSJordan K. Hubbard 
42484f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
42584f33deaSJordan K. Hubbard 
42684f33deaSJordan K. Hubbard 	/* close the ends of the pipe that will only be referenced in the
42784f33deaSJordan K. Hubbard 	 * grandchild process...
42884f33deaSJordan K. Hubbard 	 */
42984f33deaSJordan K. Hubbard 	close(stdin_pipe[READ_PIPE]);
43084f33deaSJordan K. Hubbard 	close(stdout_pipe[WRITE_PIPE]);
43184f33deaSJordan K. Hubbard 
43284f33deaSJordan K. Hubbard 	/*
43384f33deaSJordan K. Hubbard 	 * write, to the pipe connected to child's stdin, any input specified
43484f33deaSJordan K. Hubbard 	 * after a % in the crontab entry.  while we copy, convert any
43584f33deaSJordan K. Hubbard 	 * additional %'s to newlines.  when done, if some characters were
43684f33deaSJordan K. Hubbard 	 * written and the last one wasn't a newline, write a newline.
43784f33deaSJordan K. Hubbard 	 *
43884f33deaSJordan K. Hubbard 	 * Note that if the input data won't fit into one pipe buffer (2K
43984f33deaSJordan K. Hubbard 	 * or 4K on most BSD systems), and the child doesn't read its stdin,
44084f33deaSJordan K. Hubbard 	 * we would block here.  thus we must fork again.
44184f33deaSJordan K. Hubbard 	 */
44284f33deaSJordan K. Hubbard 
4435b80de23SKyle Evans 	if (*input_data && (stdinjob = fork()) == 0) {
44484f33deaSJordan K. Hubbard 		register FILE	*out = fdopen(stdin_pipe[WRITE_PIPE], "w");
44584f33deaSJordan K. Hubbard 		register int	need_newline = FALSE;
44684f33deaSJordan K. Hubbard 		register int	escaped = FALSE;
44784f33deaSJordan K. Hubbard 		register int	ch;
44884f33deaSJordan K. Hubbard 
44964ae78cbSGuy Helmer 		if (out == NULL) {
45064ae78cbSGuy Helmer 			warn("fdopen failed in child2");
45164ae78cbSGuy Helmer 			_exit(ERROR_EXIT);
45264ae78cbSGuy Helmer 		}
45364ae78cbSGuy Helmer 
45484f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
45584f33deaSJordan K. Hubbard 
45684f33deaSJordan K. Hubbard 		/* close the pipe we don't use, since we inherited it and
45784f33deaSJordan K. Hubbard 		 * are part of its reference count now.
45884f33deaSJordan K. Hubbard 		 */
45984f33deaSJordan K. Hubbard 		close(stdout_pipe[READ_PIPE]);
46084f33deaSJordan K. Hubbard 
46184f33deaSJordan K. Hubbard 		/* translation:
46284f33deaSJordan K. Hubbard 		 *	\% -> %
46384f33deaSJordan K. Hubbard 		 *	%  -> \n
46484f33deaSJordan K. Hubbard 		 *	\x -> \x	for all x != %
46584f33deaSJordan K. Hubbard 		 */
466401e6468SPhilippe Charnier 		while ((ch = *input_data++)) {
46784f33deaSJordan K. Hubbard 			if (escaped) {
46884f33deaSJordan K. Hubbard 				if (ch != '%')
46984f33deaSJordan K. Hubbard 					putc('\\', out);
47084f33deaSJordan K. Hubbard 			} else {
47184f33deaSJordan K. Hubbard 				if (ch == '%')
47284f33deaSJordan K. Hubbard 					ch = '\n';
47384f33deaSJordan K. Hubbard 			}
47484f33deaSJordan K. Hubbard 
47584f33deaSJordan K. Hubbard 			if (!(escaped = (ch == '\\'))) {
47684f33deaSJordan K. Hubbard 				putc(ch, out);
47784f33deaSJordan K. Hubbard 				need_newline = (ch != '\n');
47884f33deaSJordan K. Hubbard 			}
47984f33deaSJordan K. Hubbard 		}
48084f33deaSJordan K. Hubbard 		if (escaped)
48184f33deaSJordan K. Hubbard 			putc('\\', out);
48284f33deaSJordan K. Hubbard 		if (need_newline)
48384f33deaSJordan K. Hubbard 			putc('\n', out);
48484f33deaSJordan K. Hubbard 
48584f33deaSJordan K. Hubbard 		/* close the pipe, causing an EOF condition.  fclose causes
48684f33deaSJordan K. Hubbard 		 * stdin_pipe[WRITE_PIPE] to be closed, too.
48784f33deaSJordan K. Hubbard 		 */
48884f33deaSJordan K. Hubbard 		fclose(out);
48984f33deaSJordan K. Hubbard 
49084f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
49184f33deaSJordan K. Hubbard 		exit(0);
49284f33deaSJordan K. Hubbard 	}
49384f33deaSJordan K. Hubbard 
49484f33deaSJordan K. Hubbard 	/* close the pipe to the grandkiddie's stdin, since its wicked uncle
49584f33deaSJordan K. Hubbard 	 * ernie back there has it open and will close it when he's done.
49684f33deaSJordan K. Hubbard 	 */
49784f33deaSJordan K. Hubbard 	close(stdin_pipe[WRITE_PIPE]);
49884f33deaSJordan K. Hubbard 
49984f33deaSJordan K. Hubbard 	/*
50084f33deaSJordan K. Hubbard 	 * read output from the grandchild.  it's stderr has been redirected to
50184f33deaSJordan K. Hubbard 	 * it's stdout, which has been redirected to our pipe.  if there is any
50284f33deaSJordan K. Hubbard 	 * output, we'll be mailing it to the user whose crontab this is...
50384f33deaSJordan K. Hubbard 	 * when the grandchild exits, we'll get EOF.
50484f33deaSJordan K. Hubbard 	 */
50584f33deaSJordan K. Hubbard 
50684f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
50784f33deaSJordan K. Hubbard 
50884f33deaSJordan K. Hubbard 	/*local*/{
50984f33deaSJordan K. Hubbard 		register FILE	*in = fdopen(stdout_pipe[READ_PIPE], "r");
5109be756b5SMike Silbersack 		register int	ch;
51184f33deaSJordan K. Hubbard 
51264ae78cbSGuy Helmer 		if (in == NULL) {
51364ae78cbSGuy Helmer 			warn("fdopen failed in child");
51464ae78cbSGuy Helmer 			_exit(ERROR_EXIT);
51564ae78cbSGuy Helmer 		}
51664ae78cbSGuy Helmer 
5176795e26bSKyle Evans 		mail = NULL;
5186795e26bSKyle Evans 
5199be756b5SMike Silbersack 		ch = getc(in);
52084f33deaSJordan K. Hubbard 		if (ch != EOF) {
52184f33deaSJordan K. Hubbard 			Debug(DPROC|DEXT,
52284f33deaSJordan K. Hubbard 				("[%d] got data (%x:%c) from grandchild\n",
52384f33deaSJordan K. Hubbard 					getpid(), ch, ch))
52484f33deaSJordan K. Hubbard 
52584f33deaSJordan K. Hubbard 			/* get name of recipient.  this is MAILTO if set to a
52684f33deaSJordan K. Hubbard 			 * valid local username; USER otherwise.
52784f33deaSJordan K. Hubbard 			 */
528b75634d2SDmitry Morozovsky 			if (mailto == NULL) {
529b75634d2SDmitry Morozovsky 				/* MAILTO not present, set to USER,
530b75634d2SDmitry Morozovsky 				 * unless globally overriden.
53184f33deaSJordan K. Hubbard 				 */
532b75634d2SDmitry Morozovsky 				if (defmailto)
533b75634d2SDmitry Morozovsky 					mailto = defmailto;
534b75634d2SDmitry Morozovsky 				else
53584f33deaSJordan K. Hubbard 					mailto = usernm;
53684f33deaSJordan K. Hubbard 			}
5371168e5f1SDmitry Morozovsky 			if (mailto && *mailto == '\0')
5381168e5f1SDmitry Morozovsky 				mailto = NULL;
53984f33deaSJordan K. Hubbard 
54084f33deaSJordan K. Hubbard 			/* if we are supposed to be mailing, MAILTO will
54184f33deaSJordan K. Hubbard 			 * be non-NULL.  only in this case should we set
54284f33deaSJordan K. Hubbard 			 * up the mail command and subjects and stuff...
54384f33deaSJordan K. Hubbard 			 */
54484f33deaSJordan K. Hubbard 
5451168e5f1SDmitry Morozovsky 			if (mailto) {
54684f33deaSJordan K. Hubbard 				register char	**env;
54784f33deaSJordan K. Hubbard 				auto char	mailcmd[MAX_COMMAND];
54884f33deaSJordan K. Hubbard 				auto char	hostname[MAXHOSTNAMELEN];
54984f33deaSJordan K. Hubbard 
550713c03d5SPeter Wemm 				if (gethostname(hostname, MAXHOSTNAMELEN) == -1)
551713c03d5SPeter Wemm 					hostname[0] = '\0';
552713c03d5SPeter Wemm 				hostname[sizeof(hostname) - 1] = '\0';
553bdddbd2fSPaul Traina 				(void) snprintf(mailcmd, sizeof(mailcmd),
554bdddbd2fSPaul Traina 					       MAILARGS, MAILCMD);
5555b80de23SKyle Evans 				if (!(mail = cron_popen(mailcmd, "w", e, &mailpid))) {
556401e6468SPhilippe Charnier 					warn("%s", MAILCMD);
55784f33deaSJordan K. Hubbard 					(void) _exit(ERROR_EXIT);
55884f33deaSJordan K. Hubbard 				}
55912455a9eSKyle Evans 				if (mailfrom == NULL || *mailfrom == '\0')
560713c03d5SPeter Wemm 					fprintf(mail, "From: Cron Daemon <%s@%s>\n",
561713c03d5SPeter Wemm 					    usernm, hostname);
56212455a9eSKyle Evans 				else
56312455a9eSKyle Evans 					fprintf(mail, "From: Cron Daemon <%s>\n",
56412455a9eSKyle Evans 					    mailfrom);
56584f33deaSJordan K. Hubbard 				fprintf(mail, "To: %s\n", mailto);
56684f33deaSJordan K. Hubbard 				fprintf(mail, "Subject: Cron <%s@%s> %s\n",
56784f33deaSJordan K. Hubbard 					usernm, first_word(hostname, "."),
56884f33deaSJordan K. Hubbard 					e->cmd);
56984f33deaSJordan K. Hubbard # if defined(MAIL_DATE)
57084f33deaSJordan K. Hubbard 				fprintf(mail, "Date: %s\n",
57184f33deaSJordan K. Hubbard 					arpadate(&TargetTime));
57284f33deaSJordan K. Hubbard # endif /* MAIL_DATE */
57384f33deaSJordan K. Hubbard 				for (env = e->envp;  *env;  env++)
57484f33deaSJordan K. Hubbard 					fprintf(mail, "X-Cron-Env: <%s>\n",
57584f33deaSJordan K. Hubbard 						*env);
57684f33deaSJordan K. Hubbard 				fprintf(mail, "\n");
57784f33deaSJordan K. Hubbard 
57884f33deaSJordan K. Hubbard 				/* this was the first char from the pipe
57984f33deaSJordan K. Hubbard 				 */
58084f33deaSJordan K. Hubbard 				putc(ch, mail);
58184f33deaSJordan K. Hubbard 			}
58284f33deaSJordan K. Hubbard 
58384f33deaSJordan K. Hubbard 			/* we have to read the input pipe no matter whether
58484f33deaSJordan K. Hubbard 			 * we mail or not, but obviously we only write to
58584f33deaSJordan K. Hubbard 			 * mail pipe if we ARE mailing.
58684f33deaSJordan K. Hubbard 			 */
58784f33deaSJordan K. Hubbard 
58884f33deaSJordan K. Hubbard 			while (EOF != (ch = getc(in))) {
58984f33deaSJordan K. Hubbard 				bytes++;
5906795e26bSKyle Evans 				if (mail)
59184f33deaSJordan K. Hubbard 					putc(ch, mail);
59284f33deaSJordan K. Hubbard 			}
5935b80de23SKyle Evans 		}
5945b80de23SKyle Evans 		/*if data from grandchild*/
5955b80de23SKyle Evans 
5965b80de23SKyle Evans 		Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
5975b80de23SKyle Evans 
5985b80de23SKyle Evans 		/* also closes stdout_pipe[READ_PIPE] */
5995b80de23SKyle Evans 		fclose(in);
6005b80de23SKyle Evans 	}
6015b80de23SKyle Evans 
6025b80de23SKyle Evans 	/* wait for children to die.
6035b80de23SKyle Evans 	 */
6045b80de23SKyle Evans 	if (jobpid > 0) {
6055b80de23SKyle Evans 		WAIT_T	waiter;
6065b80de23SKyle Evans 
6075b80de23SKyle Evans 		waiter = wait_on_child(jobpid, "grandchild command job");
6085b80de23SKyle Evans 
6095b80de23SKyle Evans 		/* If everything went well, and -n was set, _and_ we have mail,
6105b80de23SKyle Evans 		 * we won't be mailing... so shoot the messenger!
6115b80de23SKyle Evans 		 */
6125b80de23SKyle Evans 		if (WIFEXITED(waiter) && WEXITSTATUS(waiter) == 0
6135b80de23SKyle Evans 		    && (e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR
6146795e26bSKyle Evans 		    && mail) {
6155b80de23SKyle Evans 			Debug(DPROC, ("[%d] %s executed successfully, mail suppressed\n",
6165b80de23SKyle Evans 				getpid(), "grandchild command job"))
6175b80de23SKyle Evans 			kill(mailpid, SIGKILL);
6185b80de23SKyle Evans 			(void)fclose(mail);
6196795e26bSKyle Evans 			mail = NULL;
6205b80de23SKyle Evans 		}
6215b80de23SKyle Evans 
62284f33deaSJordan K. Hubbard 
62384f33deaSJordan K. Hubbard 		/* only close pipe if we opened it -- i.e., we're
62484f33deaSJordan K. Hubbard 		 * mailing...
62584f33deaSJordan K. Hubbard 		 */
62684f33deaSJordan K. Hubbard 
6276795e26bSKyle Evans 		if (mail) {
62884f33deaSJordan K. Hubbard 			Debug(DPROC, ("[%d] closing pipe to mail\n",
62984f33deaSJordan K. Hubbard 				getpid()))
63084f33deaSJordan K. Hubbard 			/* Note: the pclose will probably see
63184f33deaSJordan K. Hubbard 			 * the termination of the grandchild
63284f33deaSJordan K. Hubbard 			 * in addition to the mail process, since
63384f33deaSJordan K. Hubbard 			 * it (the grandchild) is likely to exit
63484f33deaSJordan K. Hubbard 			 * after closing its stdout.
63584f33deaSJordan K. Hubbard 			 */
63684f33deaSJordan K. Hubbard 			status = cron_pclose(mail);
63784f33deaSJordan K. Hubbard 
63884f33deaSJordan K. Hubbard 			/* if there was output and we could not mail it,
63984f33deaSJordan K. Hubbard 			 * log the facts so the poor user can figure out
64084f33deaSJordan K. Hubbard 			 * what's going on.
64184f33deaSJordan K. Hubbard 			 */
6425b80de23SKyle Evans 			if (status) {
64384f33deaSJordan K. Hubbard 				char buf[MAX_TEMPSTR];
64484f33deaSJordan K. Hubbard 
645bdddbd2fSPaul Traina 				snprintf(buf, sizeof(buf),
64684f33deaSJordan K. Hubbard 			"mailed %d byte%s of output but got status 0x%04x\n",
64784f33deaSJordan K. Hubbard 					bytes, (bytes==1)?"":"s",
64884f33deaSJordan K. Hubbard 					status);
64984f33deaSJordan K. Hubbard 				log_it(usernm, getpid(), "MAIL", buf);
65084f33deaSJordan K. Hubbard 			}
6515b80de23SKyle Evans 		}
65284f33deaSJordan K. Hubbard 	}
65384f33deaSJordan K. Hubbard 
6545b80de23SKyle Evans 	if (*input_data && stdinjob > 0)
6555b80de23SKyle Evans 		wait_on_child(stdinjob, "grandchild stdinjob");
6565b80de23SKyle Evans }
6575b80de23SKyle Evans 
6585b80de23SKyle Evans static WAIT_T
6595b80de23SKyle Evans wait_on_child(PID_T childpid, const char *name) {
66084f33deaSJordan K. Hubbard 	WAIT_T	waiter;
66184f33deaSJordan K. Hubbard 	PID_T	pid;
66284f33deaSJordan K. Hubbard 
6635b80de23SKyle Evans 	Debug(DPROC, ("[%d] waiting for %s (%d) to finish\n",
6645b80de23SKyle Evans 		getpid(), name, childpid))
6655b80de23SKyle Evans 
6665b80de23SKyle Evans #ifdef POSIX
6675b80de23SKyle Evans 	while ((pid = waitpid(childpid, &waiter, 0)) < 0 && errno == EINTR)
6685b80de23SKyle Evans #else
6695b80de23SKyle Evans 	while ((pid = wait4(childpid, &waiter, 0, NULL)) < 0 && errno == EINTR)
6705b80de23SKyle Evans #endif
6715b80de23SKyle Evans 		;
6725b80de23SKyle Evans 
6735b80de23SKyle Evans 	if (pid < OK)
6745b80de23SKyle Evans 		return waiter;
6755b80de23SKyle Evans 
6765b80de23SKyle Evans 	Debug(DPROC, ("[%d] %s (%d) finished, status=%04x",
6775b80de23SKyle Evans 		getpid(), name, pid, WEXITSTATUS(waiter)))
67884f33deaSJordan K. Hubbard 	if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
67984f33deaSJordan K. Hubbard 		Debug(DPROC, (", dumped core"))
68084f33deaSJordan K. Hubbard 	Debug(DPROC, ("\n"))
6815b80de23SKyle Evans 
6825b80de23SKyle Evans 	return waiter;
68384f33deaSJordan K. Hubbard }
684