xref: /freebsd/usr.sbin/cron/cron/do_command.c (revision 5b80de237b53fd14e3926dd58fc0b78be983ed84)
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 
41784bddbcSKevin Lo static void		child_process(entry *, user *),
42784bddbcSKevin Lo 			do_univ(user *);
4384f33deaSJordan K. Hubbard 
44*5b80de23SKyle Evans static WAIT_T		wait_on_child(PID_T, const char *);
4584f33deaSJordan K. Hubbard 
4684f33deaSJordan K. Hubbard void
4784f33deaSJordan K. Hubbard do_command(e, u)
4884f33deaSJordan K. Hubbard 	entry	*e;
4984f33deaSJordan K. Hubbard 	user	*u;
5084f33deaSJordan K. Hubbard {
51a08d12d3SGleb Smirnoff 	pid_t pid;
52a08d12d3SGleb Smirnoff 
5384f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
5484f33deaSJordan K. Hubbard 		getpid(), e->cmd, u->name, e->uid, e->gid))
5584f33deaSJordan K. Hubbard 
5684f33deaSJordan K. Hubbard 	/* fork to become asynchronous -- parent process is done immediately,
5784f33deaSJordan K. Hubbard 	 * and continues to run the normal cron code, which means return to
5884f33deaSJordan K. Hubbard 	 * tick().  the child and grandchild don't leave this function, alive.
5984f33deaSJordan K. Hubbard 	 *
6084f33deaSJordan K. Hubbard 	 * vfork() is unsuitable, since we have much to do, and the parent
6184f33deaSJordan K. Hubbard 	 * needs to be able to run off and fork other processes.
6284f33deaSJordan K. Hubbard 	 */
63a08d12d3SGleb Smirnoff 	switch ((pid = fork())) {
6484f33deaSJordan K. Hubbard 	case -1:
6584f33deaSJordan K. Hubbard 		log_it("CRON",getpid(),"error","can't fork");
66a08d12d3SGleb Smirnoff 		if (e->flags & INTERVAL)
67a08d12d3SGleb Smirnoff 			e->lastexit = time(NULL);
6884f33deaSJordan K. Hubbard 		break;
6984f33deaSJordan K. Hubbard 	case 0:
7084f33deaSJordan K. Hubbard 		/* child process */
7178735592SPawel Jakub Dawidek 		pidfile_close(pfh);
7284f33deaSJordan K. Hubbard 		child_process(e, u);
7384f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
7484f33deaSJordan K. Hubbard 		_exit(OK_EXIT);
7584f33deaSJordan K. Hubbard 		break;
7684f33deaSJordan K. Hubbard 	default:
7784f33deaSJordan K. Hubbard 		/* parent process */
78a08d12d3SGleb Smirnoff 		Debug(DPROC, ("[%d] main process forked child #%d, "
79a08d12d3SGleb Smirnoff 		    "returning to work\n", getpid(), pid))
80a08d12d3SGleb Smirnoff 		if (e->flags & INTERVAL) {
81a08d12d3SGleb Smirnoff 			e->lastexit = 0;
82a08d12d3SGleb Smirnoff 			e->child = pid;
83a08d12d3SGleb Smirnoff 		}
8484f33deaSJordan K. Hubbard 		break;
8584f33deaSJordan K. Hubbard 	}
8684f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
8784f33deaSJordan K. Hubbard }
8884f33deaSJordan K. Hubbard 
8984f33deaSJordan K. Hubbard 
9084f33deaSJordan K. Hubbard static void
9184f33deaSJordan K. Hubbard child_process(e, u)
9284f33deaSJordan K. Hubbard 	entry	*e;
9384f33deaSJordan K. Hubbard 	user	*u;
9484f33deaSJordan K. Hubbard {
9584f33deaSJordan K. Hubbard 	int		stdin_pipe[2], stdout_pipe[2];
9684f33deaSJordan K. Hubbard 	register char	*input_data;
9712455a9eSKyle Evans 	char		*usernm, *mailto, *mailfrom;
98*5b80de23SKyle Evans 	PID_T		jobpid, stdinjob, mailpid;
99*5b80de23SKyle Evans 	register FILE	*mail;
100*5b80de23SKyle Evans 	register int	bytes = 1;
101*5b80de23SKyle Evans 	int		status = 0;
102b25b7bc1SDavid Nugent # if defined(LOGIN_CAP)
103c00d650fSPeter Wemm 	struct passwd	*pwd;
1040435c150SAndrey A. Chernov 	login_cap_t *lc;
105b25b7bc1SDavid Nugent # endif
10684f33deaSJordan K. Hubbard 
10784f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
10884f33deaSJordan K. Hubbard 
10984f33deaSJordan K. Hubbard 	/* mark ourselves as different to PS command watchers by upshifting
11084f33deaSJordan K. Hubbard 	 * our program name.  This has no effect on some kernels.
11184f33deaSJordan K. Hubbard 	 */
112c7517d5aSPeter Wemm 	setproctitle("running job");
11384f33deaSJordan K. Hubbard 
11484f33deaSJordan K. Hubbard 	/* discover some useful and important environment settings
11584f33deaSJordan K. Hubbard 	 */
11684f33deaSJordan K. Hubbard 	usernm = env_get("LOGNAME", e->envp);
11784f33deaSJordan K. Hubbard 	mailto = env_get("MAILTO", e->envp);
11812455a9eSKyle Evans 	mailfrom = env_get("MAILFROM", e->envp);
11984f33deaSJordan K. Hubbard 
120997c6eefSYaroslav Tykhiy #ifdef PAM
121997c6eefSYaroslav Tykhiy 	/* use PAM to see if the user's account is available,
122997c6eefSYaroslav Tykhiy 	 * i.e., not locked or expired or whatever.  skip this
123997c6eefSYaroslav Tykhiy 	 * for system tasks from /etc/crontab -- they can run
124997c6eefSYaroslav Tykhiy 	 * as any user.
125997c6eefSYaroslav Tykhiy 	 */
126997c6eefSYaroslav Tykhiy 	if (strcmp(u->name, SYS_NAME)) {	/* not equal */
127997c6eefSYaroslav Tykhiy 		pam_handle_t *pamh = NULL;
128997c6eefSYaroslav Tykhiy 		int pam_err;
129997c6eefSYaroslav Tykhiy 		struct pam_conv pamc = {
130997c6eefSYaroslav Tykhiy 			.conv = openpam_nullconv,
131997c6eefSYaroslav Tykhiy 			.appdata_ptr = NULL
132587d674eSPedro F. Giffuni 		};
133997c6eefSYaroslav Tykhiy 
134997c6eefSYaroslav Tykhiy 		Debug(DPROC, ("[%d] checking account with PAM\n", getpid()))
135997c6eefSYaroslav Tykhiy 
136997c6eefSYaroslav Tykhiy 		/* u->name keeps crontab owner name while LOGNAME is the name
137997c6eefSYaroslav Tykhiy 		 * of user to run command on behalf of.  they should be the
138997c6eefSYaroslav Tykhiy 		 * same for a task from a per-user crontab.
139997c6eefSYaroslav Tykhiy 		 */
140997c6eefSYaroslav Tykhiy 		if (strcmp(u->name, usernm)) {
141997c6eefSYaroslav Tykhiy 			log_it(usernm, getpid(), "username ambiguity", u->name);
142997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
143997c6eefSYaroslav Tykhiy 		}
144997c6eefSYaroslav Tykhiy 
145997c6eefSYaroslav Tykhiy 		pam_err = pam_start("cron", usernm, &pamc, &pamh);
146997c6eefSYaroslav Tykhiy 		if (pam_err != PAM_SUCCESS) {
147997c6eefSYaroslav Tykhiy 			log_it("CRON", getpid(), "error", "can't start PAM");
148997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
149997c6eefSYaroslav Tykhiy 		}
150997c6eefSYaroslav Tykhiy 
151997c6eefSYaroslav Tykhiy 		pam_err = pam_acct_mgmt(pamh, PAM_SILENT);
152997c6eefSYaroslav Tykhiy 		/* Expired password shouldn't prevent the job from running. */
153997c6eefSYaroslav Tykhiy 		if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD) {
154997c6eefSYaroslav Tykhiy 			log_it(usernm, getpid(), "USER", "account unavailable");
155997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
156997c6eefSYaroslav Tykhiy 		}
157997c6eefSYaroslav Tykhiy 
158997c6eefSYaroslav Tykhiy 		pam_end(pamh, pam_err);
159997c6eefSYaroslav Tykhiy 	}
160997c6eefSYaroslav Tykhiy #endif
161997c6eefSYaroslav Tykhiy 
16284f33deaSJordan K. Hubbard #ifdef USE_SIGCHLD
16384f33deaSJordan K. Hubbard 	/* our parent is watching for our death by catching SIGCHLD.  we
16484f33deaSJordan K. Hubbard 	 * do not care to watch for our children's deaths this way -- we
1653df5ecacSUlrich Spörlein 	 * use wait() explicitly.  so we have to disable the signal (which
16684f33deaSJordan K. Hubbard 	 * was inherited from the parent).
16784f33deaSJordan K. Hubbard 	 */
1685e4a74ecSAndrey A. Chernov 	(void) signal(SIGCHLD, SIG_DFL);
16984f33deaSJordan K. Hubbard #else
17084f33deaSJordan K. Hubbard 	/* on system-V systems, we are ignoring SIGCLD.  we have to stop
17184f33deaSJordan K. Hubbard 	 * ignoring it now or the wait() in cron_pclose() won't work.
17284f33deaSJordan K. Hubbard 	 * because of this, we have to wait() for our children here, as well.
17384f33deaSJordan K. Hubbard 	 */
17484f33deaSJordan K. Hubbard 	(void) signal(SIGCLD, SIG_DFL);
17584f33deaSJordan K. Hubbard #endif /*BSD*/
17684f33deaSJordan K. Hubbard 
17784f33deaSJordan K. Hubbard 	/* create some pipes to talk to our future child
17884f33deaSJordan K. Hubbard 	 */
179d9850281SPedro F. Giffuni 	if (pipe(stdin_pipe) != 0 || pipe(stdout_pipe) != 0) {
180d9850281SPedro F. Giffuni 		log_it("CRON", getpid(), "error", "can't pipe");
181d9850281SPedro F. Giffuni 		exit(ERROR_EXIT);
182d9850281SPedro F. Giffuni 	}
18384f33deaSJordan K. Hubbard 
18484f33deaSJordan K. Hubbard 	/* since we are a forked process, we can diddle the command string
18584f33deaSJordan K. Hubbard 	 * we were passed -- nobody else is going to use it again, right?
18684f33deaSJordan K. Hubbard 	 *
18784f33deaSJordan K. Hubbard 	 * if a % is present in the command, previous characters are the
18884f33deaSJordan K. Hubbard 	 * command, and subsequent characters are the additional input to
18984f33deaSJordan K. Hubbard 	 * the command.  Subsequent %'s will be transformed into newlines,
19084f33deaSJordan K. Hubbard 	 * but that happens later.
19186ed6de3SJoerg Wunsch 	 *
19286ed6de3SJoerg Wunsch 	 * If there are escaped %'s, remove the escape character.
19384f33deaSJordan K. Hubbard 	 */
19484f33deaSJordan K. Hubbard 	/*local*/{
19584f33deaSJordan K. Hubbard 		register int escaped = FALSE;
19684f33deaSJordan K. Hubbard 		register int ch;
19786ed6de3SJoerg Wunsch 		register char *p;
19884f33deaSJordan K. Hubbard 
199401e6468SPhilippe Charnier 		for (input_data = p = e->cmd; (ch = *input_data);
20086ed6de3SJoerg Wunsch 		     input_data++, p++) {
20186ed6de3SJoerg Wunsch 			if (p != input_data)
20286ed6de3SJoerg Wunsch 			    *p = ch;
20384f33deaSJordan K. Hubbard 			if (escaped) {
20486ed6de3SJoerg Wunsch 				if (ch == '%' || ch == '\\')
20586ed6de3SJoerg Wunsch 					*--p = ch;
20684f33deaSJordan K. Hubbard 				escaped = FALSE;
20784f33deaSJordan K. Hubbard 				continue;
20884f33deaSJordan K. Hubbard 			}
20984f33deaSJordan K. Hubbard 			if (ch == '\\') {
21084f33deaSJordan K. Hubbard 				escaped = TRUE;
21184f33deaSJordan K. Hubbard 				continue;
21284f33deaSJordan K. Hubbard 			}
21384f33deaSJordan K. Hubbard 			if (ch == '%') {
21484f33deaSJordan K. Hubbard 				*input_data++ = '\0';
21584f33deaSJordan K. Hubbard 				break;
21684f33deaSJordan K. Hubbard 			}
21784f33deaSJordan K. Hubbard 		}
21886ed6de3SJoerg Wunsch 		*p = '\0';
21984f33deaSJordan K. Hubbard 	}
22084f33deaSJordan K. Hubbard 
22184f33deaSJordan K. Hubbard 	/* fork again, this time so we can exec the user's command.
22284f33deaSJordan K. Hubbard 	 */
223*5b80de23SKyle Evans 	switch (jobpid = vfork()) {
22484f33deaSJordan K. Hubbard 	case -1:
22584f33deaSJordan K. Hubbard 		log_it("CRON",getpid(),"error","can't vfork");
22684f33deaSJordan K. Hubbard 		exit(ERROR_EXIT);
22784f33deaSJordan K. Hubbard 		/*NOTREACHED*/
22884f33deaSJordan K. Hubbard 	case 0:
22984f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
23084f33deaSJordan K. Hubbard 			      getpid()))
23184f33deaSJordan K. Hubbard 
232f5896bafSYaroslav Tykhiy 		if (e->uid == ROOT_UID)
233f5896bafSYaroslav Tykhiy 			Jitter = RootJitter;
234f5896bafSYaroslav Tykhiy 		if (Jitter != 0) {
235f5896bafSYaroslav Tykhiy 			srandom(getpid());
236f5896bafSYaroslav Tykhiy 			sleep(random() % Jitter);
237f5896bafSYaroslav Tykhiy 		}
238f5896bafSYaroslav Tykhiy 
23984f33deaSJordan K. Hubbard 		/* write a log message.  we've waited this long to do it
24084f33deaSJordan K. Hubbard 		 * because it was not until now that we knew the PID that
24184f33deaSJordan K. Hubbard 		 * the actual user command shell was going to get and the
24284f33deaSJordan K. Hubbard 		 * PID is part of the log message.
24384f33deaSJordan K. Hubbard 		 */
244*5b80de23SKyle Evans 		if ((e->flags & DONT_LOG) == 0) {
24584f33deaSJordan K. Hubbard 			char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
24684f33deaSJordan K. Hubbard 
24784f33deaSJordan K. Hubbard 			log_it(usernm, getpid(), "CMD", x);
24884f33deaSJordan K. Hubbard 			free(x);
24984f33deaSJordan K. Hubbard 		}
25084f33deaSJordan K. Hubbard 
25184f33deaSJordan K. Hubbard 		/* that's the last thing we'll log.  close the log files.
25284f33deaSJordan K. Hubbard 		 */
25384f33deaSJordan K. Hubbard #ifdef SYSLOG
25484f33deaSJordan K. Hubbard 		closelog();
25584f33deaSJordan K. Hubbard #endif
25684f33deaSJordan K. Hubbard 
25784f33deaSJordan K. Hubbard 		/* get new pgrp, void tty, etc.
25884f33deaSJordan K. Hubbard 		 */
25984f33deaSJordan K. Hubbard 		(void) setsid();
26084f33deaSJordan K. Hubbard 
26184f33deaSJordan K. Hubbard 		/* close the pipe ends that we won't use.  this doesn't affect
26284f33deaSJordan K. Hubbard 		 * the parent, who has to read and write them; it keeps the
26384f33deaSJordan K. Hubbard 		 * kernel from recording us as a potential client TWICE --
26484f33deaSJordan K. Hubbard 		 * which would keep it from sending SIGPIPE in otherwise
26584f33deaSJordan K. Hubbard 		 * appropriate circumstances.
26684f33deaSJordan K. Hubbard 		 */
26784f33deaSJordan K. Hubbard 		close(stdin_pipe[WRITE_PIPE]);
26884f33deaSJordan K. Hubbard 		close(stdout_pipe[READ_PIPE]);
26984f33deaSJordan K. Hubbard 
27084f33deaSJordan K. Hubbard 		/* grandchild process.  make std{in,out} be the ends of
27184f33deaSJordan K. Hubbard 		 * pipes opened by our daddy; make stderr go to stdout.
27284f33deaSJordan K. Hubbard 		 */
27384f33deaSJordan K. Hubbard 		close(STDIN);	dup2(stdin_pipe[READ_PIPE], STDIN);
27484f33deaSJordan K. Hubbard 		close(STDOUT);	dup2(stdout_pipe[WRITE_PIPE], STDOUT);
27584f33deaSJordan K. Hubbard 		close(STDERR);	dup2(STDOUT, STDERR);
27684f33deaSJordan K. Hubbard 
27784f33deaSJordan K. Hubbard 		/* close the pipes we just dup'ed.  The resources will remain.
27884f33deaSJordan K. Hubbard 		 */
27984f33deaSJordan K. Hubbard 		close(stdin_pipe[READ_PIPE]);
28084f33deaSJordan K. Hubbard 		close(stdout_pipe[WRITE_PIPE]);
28184f33deaSJordan K. Hubbard 
28284f33deaSJordan K. Hubbard 		/* set our login universe.  Do this in the grandchild
28384f33deaSJordan K. Hubbard 		 * so that the child can invoke /usr/lib/sendmail
28484f33deaSJordan K. Hubbard 		 * without surprises.
28584f33deaSJordan K. Hubbard 		 */
28684f33deaSJordan K. Hubbard 		do_univ(u);
28784f33deaSJordan K. Hubbard 
288b25b7bc1SDavid Nugent # if defined(LOGIN_CAP)
289b25b7bc1SDavid Nugent 		/* Set user's entire context, but skip the environment
290b25b7bc1SDavid Nugent 		 * as cron provides a separate interface for this
291b25b7bc1SDavid Nugent 		 */
2920435c150SAndrey A. Chernov 		if ((pwd = getpwnam(usernm)) == NULL)
293c00d650fSPeter Wemm 			pwd = getpwuid(e->uid);
2940435c150SAndrey A. Chernov 		lc = NULL;
2950435c150SAndrey A. Chernov 		if (pwd != NULL) {
2960435c150SAndrey A. Chernov 			pwd->pw_gid = e->gid;
2970435c150SAndrey A. Chernov 			if (e->class != NULL)
2980435c150SAndrey A. Chernov 				lc = login_getclass(e->class);
2990435c150SAndrey A. Chernov 		}
3009efe2eccSPeter Wemm 		if (pwd &&
3010435c150SAndrey A. Chernov 		    setusercontext(lc, pwd, e->uid,
3029efe2eccSPeter Wemm 			    LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
3039efe2eccSPeter Wemm 			(void) endpwent();
3049efe2eccSPeter Wemm 		else {
305c00d650fSPeter Wemm 			/* fall back to the old method */
3069efe2eccSPeter Wemm 			(void) endpwent();
307c00d650fSPeter Wemm # endif
308c00d650fSPeter Wemm 			/* set our directory, uid and gid.  Set gid first,
309708f27a1SMaxim Konovalov 			 * since once we set uid, we've lost root privileges.
31084f33deaSJordan K. Hubbard 			 */
311bb0aa1a5SMaxim Konovalov 			if (setgid(e->gid) != 0) {
312bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
313bb0aa1a5SMaxim Konovalov 				    "error", "setgid failed");
314bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
315bb0aa1a5SMaxim Konovalov 			}
31684f33deaSJordan K. Hubbard # if defined(BSD)
317bb0aa1a5SMaxim Konovalov 			if (initgroups(usernm, e->gid) != 0) {
318bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
319bb0aa1a5SMaxim Konovalov 				    "error", "initgroups failed");
320bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
321bb0aa1a5SMaxim Konovalov 			}
32284f33deaSJordan K. Hubbard # endif
323bb0aa1a5SMaxim Konovalov 			if (setlogin(usernm) != 0) {
324bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
325bb0aa1a5SMaxim Konovalov 				    "error", "setlogin failed");
326bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
327bb0aa1a5SMaxim Konovalov 			}
328bb0aa1a5SMaxim Konovalov 			if (setuid(e->uid) != 0) {
329bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
330bb0aa1a5SMaxim Konovalov 				    "error", "setuid failed");
331bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
332bb0aa1a5SMaxim Konovalov 			}
333bb0aa1a5SMaxim Konovalov 			/* we aren't root after this..*/
334c00d650fSPeter Wemm #if defined(LOGIN_CAP)
335c00d650fSPeter Wemm 		}
3363d99cebfSAndrey A. Chernov 		if (lc != NULL)
3373d99cebfSAndrey A. Chernov 			login_close(lc);
338b25b7bc1SDavid Nugent #endif
339bdddbd2fSPaul Traina 		chdir(env_get("HOME", e->envp));
34084f33deaSJordan K. Hubbard 
34184f33deaSJordan K. Hubbard 		/* exec the command.
34284f33deaSJordan K. Hubbard 		 */
34384f33deaSJordan K. Hubbard 		{
34484f33deaSJordan K. Hubbard 			char	*shell = env_get("SHELL", e->envp);
34584f33deaSJordan K. Hubbard 
34684f33deaSJordan K. Hubbard # if DEBUGGING
34784f33deaSJordan K. Hubbard 			if (DebugFlags & DTEST) {
34884f33deaSJordan K. Hubbard 				fprintf(stderr,
34984f33deaSJordan K. Hubbard 				"debug DTEST is on, not exec'ing command.\n");
35084f33deaSJordan K. Hubbard 				fprintf(stderr,
35184f33deaSJordan K. Hubbard 				"\tcmd='%s' shell='%s'\n", e->cmd, shell);
35284f33deaSJordan K. Hubbard 				_exit(OK_EXIT);
35384f33deaSJordan K. Hubbard 			}
35484f33deaSJordan K. Hubbard # endif /*DEBUGGING*/
355b83c6ff5SPedro F. Giffuni 			execle(shell, shell, "-c", e->cmd, (char *)NULL,
356b83c6ff5SPedro F. Giffuni 			    e->envp);
357b83c6ff5SPedro F. Giffuni 			warn("execle: couldn't exec `%s'", shell);
35884f33deaSJordan K. Hubbard 			_exit(ERROR_EXIT);
35984f33deaSJordan K. Hubbard 		}
36084f33deaSJordan K. Hubbard 		break;
36184f33deaSJordan K. Hubbard 	default:
36284f33deaSJordan K. Hubbard 		/* parent process */
36384f33deaSJordan K. Hubbard 		break;
36484f33deaSJordan K. Hubbard 	}
36584f33deaSJordan K. Hubbard 
36684f33deaSJordan K. Hubbard 	/* middle process, child of original cron, parent of process running
36784f33deaSJordan K. Hubbard 	 * the user's command.
36884f33deaSJordan K. Hubbard 	 */
36984f33deaSJordan K. Hubbard 
37084f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
37184f33deaSJordan K. Hubbard 
37284f33deaSJordan K. Hubbard 	/* close the ends of the pipe that will only be referenced in the
37384f33deaSJordan K. Hubbard 	 * grandchild process...
37484f33deaSJordan K. Hubbard 	 */
37584f33deaSJordan K. Hubbard 	close(stdin_pipe[READ_PIPE]);
37684f33deaSJordan K. Hubbard 	close(stdout_pipe[WRITE_PIPE]);
37784f33deaSJordan K. Hubbard 
37884f33deaSJordan K. Hubbard 	/*
37984f33deaSJordan K. Hubbard 	 * write, to the pipe connected to child's stdin, any input specified
38084f33deaSJordan K. Hubbard 	 * after a % in the crontab entry.  while we copy, convert any
38184f33deaSJordan K. Hubbard 	 * additional %'s to newlines.  when done, if some characters were
38284f33deaSJordan K. Hubbard 	 * written and the last one wasn't a newline, write a newline.
38384f33deaSJordan K. Hubbard 	 *
38484f33deaSJordan K. Hubbard 	 * Note that if the input data won't fit into one pipe buffer (2K
38584f33deaSJordan K. Hubbard 	 * or 4K on most BSD systems), and the child doesn't read its stdin,
38684f33deaSJordan K. Hubbard 	 * we would block here.  thus we must fork again.
38784f33deaSJordan K. Hubbard 	 */
38884f33deaSJordan K. Hubbard 
389*5b80de23SKyle Evans 	if (*input_data && (stdinjob = fork()) == 0) {
39084f33deaSJordan K. Hubbard 		register FILE	*out = fdopen(stdin_pipe[WRITE_PIPE], "w");
39184f33deaSJordan K. Hubbard 		register int	need_newline = FALSE;
39284f33deaSJordan K. Hubbard 		register int	escaped = FALSE;
39384f33deaSJordan K. Hubbard 		register int	ch;
39484f33deaSJordan K. Hubbard 
39564ae78cbSGuy Helmer 		if (out == NULL) {
39664ae78cbSGuy Helmer 			warn("fdopen failed in child2");
39764ae78cbSGuy Helmer 			_exit(ERROR_EXIT);
39864ae78cbSGuy Helmer 		}
39964ae78cbSGuy Helmer 
40084f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
40184f33deaSJordan K. Hubbard 
40284f33deaSJordan K. Hubbard 		/* close the pipe we don't use, since we inherited it and
40384f33deaSJordan K. Hubbard 		 * are part of its reference count now.
40484f33deaSJordan K. Hubbard 		 */
40584f33deaSJordan K. Hubbard 		close(stdout_pipe[READ_PIPE]);
40684f33deaSJordan K. Hubbard 
40784f33deaSJordan K. Hubbard 		/* translation:
40884f33deaSJordan K. Hubbard 		 *	\% -> %
40984f33deaSJordan K. Hubbard 		 *	%  -> \n
41084f33deaSJordan K. Hubbard 		 *	\x -> \x	for all x != %
41184f33deaSJordan K. Hubbard 		 */
412401e6468SPhilippe Charnier 		while ((ch = *input_data++)) {
41384f33deaSJordan K. Hubbard 			if (escaped) {
41484f33deaSJordan K. Hubbard 				if (ch != '%')
41584f33deaSJordan K. Hubbard 					putc('\\', out);
41684f33deaSJordan K. Hubbard 			} else {
41784f33deaSJordan K. Hubbard 				if (ch == '%')
41884f33deaSJordan K. Hubbard 					ch = '\n';
41984f33deaSJordan K. Hubbard 			}
42084f33deaSJordan K. Hubbard 
42184f33deaSJordan K. Hubbard 			if (!(escaped = (ch == '\\'))) {
42284f33deaSJordan K. Hubbard 				putc(ch, out);
42384f33deaSJordan K. Hubbard 				need_newline = (ch != '\n');
42484f33deaSJordan K. Hubbard 			}
42584f33deaSJordan K. Hubbard 		}
42684f33deaSJordan K. Hubbard 		if (escaped)
42784f33deaSJordan K. Hubbard 			putc('\\', out);
42884f33deaSJordan K. Hubbard 		if (need_newline)
42984f33deaSJordan K. Hubbard 			putc('\n', out);
43084f33deaSJordan K. Hubbard 
43184f33deaSJordan K. Hubbard 		/* close the pipe, causing an EOF condition.  fclose causes
43284f33deaSJordan K. Hubbard 		 * stdin_pipe[WRITE_PIPE] to be closed, too.
43384f33deaSJordan K. Hubbard 		 */
43484f33deaSJordan K. Hubbard 		fclose(out);
43584f33deaSJordan K. Hubbard 
43684f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
43784f33deaSJordan K. Hubbard 		exit(0);
43884f33deaSJordan K. Hubbard 	}
43984f33deaSJordan K. Hubbard 
44084f33deaSJordan K. Hubbard 	/* close the pipe to the grandkiddie's stdin, since its wicked uncle
44184f33deaSJordan K. Hubbard 	 * ernie back there has it open and will close it when he's done.
44284f33deaSJordan K. Hubbard 	 */
44384f33deaSJordan K. Hubbard 	close(stdin_pipe[WRITE_PIPE]);
44484f33deaSJordan K. Hubbard 
44584f33deaSJordan K. Hubbard 	/*
44684f33deaSJordan K. Hubbard 	 * read output from the grandchild.  it's stderr has been redirected to
44784f33deaSJordan K. Hubbard 	 * it's stdout, which has been redirected to our pipe.  if there is any
44884f33deaSJordan K. Hubbard 	 * output, we'll be mailing it to the user whose crontab this is...
44984f33deaSJordan K. Hubbard 	 * when the grandchild exits, we'll get EOF.
45084f33deaSJordan K. Hubbard 	 */
45184f33deaSJordan K. Hubbard 
45284f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
45384f33deaSJordan K. Hubbard 
45484f33deaSJordan K. Hubbard 	/*local*/{
45584f33deaSJordan K. Hubbard 		register FILE	*in = fdopen(stdout_pipe[READ_PIPE], "r");
4569be756b5SMike Silbersack 		register int	ch;
45784f33deaSJordan K. Hubbard 
45864ae78cbSGuy Helmer 		if (in == NULL) {
45964ae78cbSGuy Helmer 			warn("fdopen failed in child");
46064ae78cbSGuy Helmer 			_exit(ERROR_EXIT);
46164ae78cbSGuy Helmer 		}
46264ae78cbSGuy Helmer 
4639be756b5SMike Silbersack 		ch = getc(in);
46484f33deaSJordan K. Hubbard 		if (ch != EOF) {
46584f33deaSJordan K. Hubbard 			Debug(DPROC|DEXT,
46684f33deaSJordan K. Hubbard 				("[%d] got data (%x:%c) from grandchild\n",
46784f33deaSJordan K. Hubbard 					getpid(), ch, ch))
46884f33deaSJordan K. Hubbard 
46984f33deaSJordan K. Hubbard 			/* get name of recipient.  this is MAILTO if set to a
47084f33deaSJordan K. Hubbard 			 * valid local username; USER otherwise.
47184f33deaSJordan K. Hubbard 			 */
472b75634d2SDmitry Morozovsky 			if (mailto == NULL) {
473b75634d2SDmitry Morozovsky 				/* MAILTO not present, set to USER,
474b75634d2SDmitry Morozovsky 				 * unless globally overriden.
47584f33deaSJordan K. Hubbard 				 */
476b75634d2SDmitry Morozovsky 				if (defmailto)
477b75634d2SDmitry Morozovsky 					mailto = defmailto;
478b75634d2SDmitry Morozovsky 				else
47984f33deaSJordan K. Hubbard 					mailto = usernm;
48084f33deaSJordan K. Hubbard 			}
4811168e5f1SDmitry Morozovsky 			if (mailto && *mailto == '\0')
4821168e5f1SDmitry Morozovsky 				mailto = NULL;
48384f33deaSJordan K. Hubbard 
48484f33deaSJordan K. Hubbard 			/* if we are supposed to be mailing, MAILTO will
48584f33deaSJordan K. Hubbard 			 * be non-NULL.  only in this case should we set
48684f33deaSJordan K. Hubbard 			 * up the mail command and subjects and stuff...
48784f33deaSJordan K. Hubbard 			 */
48884f33deaSJordan K. Hubbard 
4891168e5f1SDmitry Morozovsky 			if (mailto) {
49084f33deaSJordan K. Hubbard 				register char	**env;
49184f33deaSJordan K. Hubbard 				auto char	mailcmd[MAX_COMMAND];
49284f33deaSJordan K. Hubbard 				auto char	hostname[MAXHOSTNAMELEN];
49384f33deaSJordan K. Hubbard 
494713c03d5SPeter Wemm 				if (gethostname(hostname, MAXHOSTNAMELEN) == -1)
495713c03d5SPeter Wemm 					hostname[0] = '\0';
496713c03d5SPeter Wemm 				hostname[sizeof(hostname) - 1] = '\0';
497bdddbd2fSPaul Traina 				(void) snprintf(mailcmd, sizeof(mailcmd),
498bdddbd2fSPaul Traina 					       MAILARGS, MAILCMD);
499*5b80de23SKyle Evans 				if (!(mail = cron_popen(mailcmd, "w", e, &mailpid))) {
500401e6468SPhilippe Charnier 					warn("%s", MAILCMD);
50184f33deaSJordan K. Hubbard 					(void) _exit(ERROR_EXIT);
50284f33deaSJordan K. Hubbard 				}
50312455a9eSKyle Evans 				if (mailfrom == NULL || *mailfrom == '\0')
504713c03d5SPeter Wemm 					fprintf(mail, "From: Cron Daemon <%s@%s>\n",
505713c03d5SPeter Wemm 					    usernm, hostname);
50612455a9eSKyle Evans 				else
50712455a9eSKyle Evans 					fprintf(mail, "From: Cron Daemon <%s>\n",
50812455a9eSKyle Evans 					    mailfrom);
50984f33deaSJordan K. Hubbard 				fprintf(mail, "To: %s\n", mailto);
51084f33deaSJordan K. Hubbard 				fprintf(mail, "Subject: Cron <%s@%s> %s\n",
51184f33deaSJordan K. Hubbard 					usernm, first_word(hostname, "."),
51284f33deaSJordan K. Hubbard 					e->cmd);
51384f33deaSJordan K. Hubbard # if defined(MAIL_DATE)
51484f33deaSJordan K. Hubbard 				fprintf(mail, "Date: %s\n",
51584f33deaSJordan K. Hubbard 					arpadate(&TargetTime));
51684f33deaSJordan K. Hubbard # endif /* MAIL_DATE */
51784f33deaSJordan K. Hubbard 				for (env = e->envp;  *env;  env++)
51884f33deaSJordan K. Hubbard 					fprintf(mail, "X-Cron-Env: <%s>\n",
51984f33deaSJordan K. Hubbard 						*env);
52084f33deaSJordan K. Hubbard 				fprintf(mail, "\n");
52184f33deaSJordan K. Hubbard 
52284f33deaSJordan K. Hubbard 				/* this was the first char from the pipe
52384f33deaSJordan K. Hubbard 				 */
52484f33deaSJordan K. Hubbard 				putc(ch, mail);
52584f33deaSJordan K. Hubbard 			}
52684f33deaSJordan K. Hubbard 
52784f33deaSJordan K. Hubbard 			/* we have to read the input pipe no matter whether
52884f33deaSJordan K. Hubbard 			 * we mail or not, but obviously we only write to
52984f33deaSJordan K. Hubbard 			 * mail pipe if we ARE mailing.
53084f33deaSJordan K. Hubbard 			 */
53184f33deaSJordan K. Hubbard 
53284f33deaSJordan K. Hubbard 			while (EOF != (ch = getc(in))) {
53384f33deaSJordan K. Hubbard 				bytes++;
53484f33deaSJordan K. Hubbard 				if (mailto)
53584f33deaSJordan K. Hubbard 					putc(ch, mail);
53684f33deaSJordan K. Hubbard 			}
537*5b80de23SKyle Evans 		}
538*5b80de23SKyle Evans 		/*if data from grandchild*/
539*5b80de23SKyle Evans 
540*5b80de23SKyle Evans 		Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
541*5b80de23SKyle Evans 
542*5b80de23SKyle Evans 		/* also closes stdout_pipe[READ_PIPE] */
543*5b80de23SKyle Evans 		fclose(in);
544*5b80de23SKyle Evans 	}
545*5b80de23SKyle Evans 
546*5b80de23SKyle Evans 	/* wait for children to die.
547*5b80de23SKyle Evans 	 */
548*5b80de23SKyle Evans 	if (jobpid > 0) {
549*5b80de23SKyle Evans 		WAIT_T	waiter;
550*5b80de23SKyle Evans 
551*5b80de23SKyle Evans 		waiter = wait_on_child(jobpid, "grandchild command job");
552*5b80de23SKyle Evans 
553*5b80de23SKyle Evans 		/* If everything went well, and -n was set, _and_ we have mail,
554*5b80de23SKyle Evans 		 * we won't be mailing... so shoot the messenger!
555*5b80de23SKyle Evans 		 */
556*5b80de23SKyle Evans 		if (WIFEXITED(waiter) && WEXITSTATUS(waiter) == 0
557*5b80de23SKyle Evans 		    && (e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR
558*5b80de23SKyle Evans 		    && mailto) {
559*5b80de23SKyle Evans 			Debug(DPROC, ("[%d] %s executed successfully, mail suppressed\n",
560*5b80de23SKyle Evans 				getpid(), "grandchild command job"))
561*5b80de23SKyle Evans 			kill(mailpid, SIGKILL);
562*5b80de23SKyle Evans 			(void)fclose(mail);
563*5b80de23SKyle Evans 			mailto = NULL;
564*5b80de23SKyle Evans 		}
565*5b80de23SKyle Evans 
56684f33deaSJordan K. Hubbard 
56784f33deaSJordan K. Hubbard 		/* only close pipe if we opened it -- i.e., we're
56884f33deaSJordan K. Hubbard 		 * mailing...
56984f33deaSJordan K. Hubbard 		 */
57084f33deaSJordan K. Hubbard 
57184f33deaSJordan K. Hubbard 		if (mailto) {
57284f33deaSJordan K. Hubbard 			Debug(DPROC, ("[%d] closing pipe to mail\n",
57384f33deaSJordan K. Hubbard 				getpid()))
57484f33deaSJordan K. Hubbard 			/* Note: the pclose will probably see
57584f33deaSJordan K. Hubbard 			 * the termination of the grandchild
57684f33deaSJordan K. Hubbard 			 * in addition to the mail process, since
57784f33deaSJordan K. Hubbard 			 * it (the grandchild) is likely to exit
57884f33deaSJordan K. Hubbard 			 * after closing its stdout.
57984f33deaSJordan K. Hubbard 			 */
58084f33deaSJordan K. Hubbard 			status = cron_pclose(mail);
58184f33deaSJordan K. Hubbard 
58284f33deaSJordan K. Hubbard 			/* if there was output and we could not mail it,
58384f33deaSJordan K. Hubbard 			 * log the facts so the poor user can figure out
58484f33deaSJordan K. Hubbard 			 * what's going on.
58584f33deaSJordan K. Hubbard 			 */
586*5b80de23SKyle Evans 			if (status) {
58784f33deaSJordan K. Hubbard 				char buf[MAX_TEMPSTR];
58884f33deaSJordan K. Hubbard 
589bdddbd2fSPaul Traina 				snprintf(buf, sizeof(buf),
59084f33deaSJordan K. Hubbard 			"mailed %d byte%s of output but got status 0x%04x\n",
59184f33deaSJordan K. Hubbard 					bytes, (bytes==1)?"":"s",
59284f33deaSJordan K. Hubbard 					status);
59384f33deaSJordan K. Hubbard 				log_it(usernm, getpid(), "MAIL", buf);
59484f33deaSJordan K. Hubbard 			}
595*5b80de23SKyle Evans 		}
59684f33deaSJordan K. Hubbard 	}
59784f33deaSJordan K. Hubbard 
598*5b80de23SKyle Evans 	if (*input_data && stdinjob > 0)
599*5b80de23SKyle Evans 		wait_on_child(stdinjob, "grandchild stdinjob");
600*5b80de23SKyle Evans }
601*5b80de23SKyle Evans 
602*5b80de23SKyle Evans static WAIT_T
603*5b80de23SKyle Evans wait_on_child(PID_T childpid, const char *name) {
60484f33deaSJordan K. Hubbard 	WAIT_T	waiter;
60584f33deaSJordan K. Hubbard 	PID_T	pid;
60684f33deaSJordan K. Hubbard 
607*5b80de23SKyle Evans 	Debug(DPROC, ("[%d] waiting for %s (%d) to finish\n",
608*5b80de23SKyle Evans 		getpid(), name, childpid))
609*5b80de23SKyle Evans 
610*5b80de23SKyle Evans #ifdef POSIX
611*5b80de23SKyle Evans 	while ((pid = waitpid(childpid, &waiter, 0)) < 0 && errno == EINTR)
612*5b80de23SKyle Evans #else
613*5b80de23SKyle Evans 	while ((pid = wait4(childpid, &waiter, 0, NULL)) < 0 && errno == EINTR)
614*5b80de23SKyle Evans #endif
615*5b80de23SKyle Evans 		;
616*5b80de23SKyle Evans 
617*5b80de23SKyle Evans 	if (pid < OK)
618*5b80de23SKyle Evans 		return waiter;
619*5b80de23SKyle Evans 
620*5b80de23SKyle Evans 	Debug(DPROC, ("[%d] %s (%d) finished, status=%04x",
621*5b80de23SKyle Evans 		getpid(), name, pid, WEXITSTATUS(waiter)))
62284f33deaSJordan K. Hubbard 	if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
62384f33deaSJordan K. Hubbard 		Debug(DPROC, (", dumped core"))
62484f33deaSJordan K. Hubbard 	Debug(DPROC, ("\n"))
625*5b80de23SKyle Evans 
626*5b80de23SKyle Evans 	return waiter;
62784f33deaSJordan K. Hubbard }
62884f33deaSJordan K. Hubbard 
62984f33deaSJordan K. Hubbard 
63084f33deaSJordan K. Hubbard static void
63184f33deaSJordan K. Hubbard do_univ(u)
63284f33deaSJordan K. Hubbard 	user	*u;
63384f33deaSJordan K. Hubbard {
63484f33deaSJordan K. Hubbard #if defined(sequent)
63584f33deaSJordan K. Hubbard /* Dynix (Sequent) hack to put the user associated with
63684f33deaSJordan K. Hubbard  * the passed user structure into the ATT universe if
63784f33deaSJordan K. Hubbard  * necessary.  We have to dig the gecos info out of
63884f33deaSJordan K. Hubbard  * the user's password entry to see if the magic
63984f33deaSJordan K. Hubbard  * "universe(att)" string is present.
64084f33deaSJordan K. Hubbard  */
64184f33deaSJordan K. Hubbard 
64284f33deaSJordan K. Hubbard 	struct	passwd	*p;
64384f33deaSJordan K. Hubbard 	char	*s;
64484f33deaSJordan K. Hubbard 	int	i;
64584f33deaSJordan K. Hubbard 
64684f33deaSJordan K. Hubbard 	p = getpwuid(u->uid);
64784f33deaSJordan K. Hubbard 	(void) endpwent();
64884f33deaSJordan K. Hubbard 
64984f33deaSJordan K. Hubbard 	if (p == NULL)
65084f33deaSJordan K. Hubbard 		return;
65184f33deaSJordan K. Hubbard 
65284f33deaSJordan K. Hubbard 	s = p->pw_gecos;
65384f33deaSJordan K. Hubbard 
65484f33deaSJordan K. Hubbard 	for (i = 0; i < 4; i++)
65584f33deaSJordan K. Hubbard 	{
65684f33deaSJordan K. Hubbard 		if ((s = strchr(s, ',')) == NULL)
65784f33deaSJordan K. Hubbard 			return;
65884f33deaSJordan K. Hubbard 		s++;
65984f33deaSJordan K. Hubbard 	}
66084f33deaSJordan K. Hubbard 	if (strcmp(s, "universe(att)"))
66184f33deaSJordan K. Hubbard 		return;
66284f33deaSJordan K. Hubbard 
66384f33deaSJordan K. Hubbard 	(void) universe(U_ATT);
66484f33deaSJordan K. Hubbard #endif
66584f33deaSJordan K. Hubbard }
666