xref: /freebsd/usr.sbin/cron/cron/do_command.c (revision 713c03d57ac37f26c3f21be53606c222d68b9e4f)
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 
4484f33deaSJordan K. Hubbard 
4584f33deaSJordan K. Hubbard void
4684f33deaSJordan K. Hubbard do_command(e, u)
4784f33deaSJordan K. Hubbard 	entry	*e;
4884f33deaSJordan K. Hubbard 	user	*u;
4984f33deaSJordan K. Hubbard {
5084f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
5184f33deaSJordan K. Hubbard 		getpid(), e->cmd, u->name, e->uid, e->gid))
5284f33deaSJordan K. Hubbard 
5384f33deaSJordan K. Hubbard 	/* fork to become asynchronous -- parent process is done immediately,
5484f33deaSJordan K. Hubbard 	 * and continues to run the normal cron code, which means return to
5584f33deaSJordan K. Hubbard 	 * tick().  the child and grandchild don't leave this function, alive.
5684f33deaSJordan K. Hubbard 	 *
5784f33deaSJordan K. Hubbard 	 * vfork() is unsuitable, since we have much to do, and the parent
5884f33deaSJordan K. Hubbard 	 * needs to be able to run off and fork other processes.
5984f33deaSJordan K. Hubbard 	 */
6084f33deaSJordan K. Hubbard 	switch (fork()) {
6184f33deaSJordan K. Hubbard 	case -1:
6284f33deaSJordan K. Hubbard 		log_it("CRON",getpid(),"error","can't fork");
6384f33deaSJordan K. Hubbard 		break;
6484f33deaSJordan K. Hubbard 	case 0:
6584f33deaSJordan K. Hubbard 		/* child process */
6678735592SPawel Jakub Dawidek 		pidfile_close(pfh);
6784f33deaSJordan K. Hubbard 		child_process(e, u);
6884f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
6984f33deaSJordan K. Hubbard 		_exit(OK_EXIT);
7084f33deaSJordan K. Hubbard 		break;
7184f33deaSJordan K. Hubbard 	default:
7284f33deaSJordan K. Hubbard 		/* parent process */
7384f33deaSJordan K. Hubbard 		break;
7484f33deaSJordan K. Hubbard 	}
7584f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
7684f33deaSJordan K. Hubbard }
7784f33deaSJordan K. Hubbard 
7884f33deaSJordan K. Hubbard 
7984f33deaSJordan K. Hubbard static void
8084f33deaSJordan K. Hubbard child_process(e, u)
8184f33deaSJordan K. Hubbard 	entry	*e;
8284f33deaSJordan K. Hubbard 	user	*u;
8384f33deaSJordan K. Hubbard {
8484f33deaSJordan K. Hubbard 	int		stdin_pipe[2], stdout_pipe[2];
8584f33deaSJordan K. Hubbard 	register char	*input_data;
8684f33deaSJordan K. Hubbard 	char		*usernm, *mailto;
8784f33deaSJordan K. Hubbard 	int		children = 0;
88b25b7bc1SDavid Nugent # if defined(LOGIN_CAP)
89c00d650fSPeter Wemm 	struct passwd	*pwd;
900435c150SAndrey A. Chernov 	login_cap_t *lc;
91b25b7bc1SDavid Nugent # endif
9284f33deaSJordan K. Hubbard 
9384f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
9484f33deaSJordan K. Hubbard 
9584f33deaSJordan K. Hubbard 	/* mark ourselves as different to PS command watchers by upshifting
9684f33deaSJordan K. Hubbard 	 * our program name.  This has no effect on some kernels.
9784f33deaSJordan K. Hubbard 	 */
98c7517d5aSPeter Wemm 	setproctitle("running job");
9984f33deaSJordan K. Hubbard 
10084f33deaSJordan K. Hubbard 	/* discover some useful and important environment settings
10184f33deaSJordan K. Hubbard 	 */
10284f33deaSJordan K. Hubbard 	usernm = env_get("LOGNAME", e->envp);
10384f33deaSJordan K. Hubbard 	mailto = env_get("MAILTO", e->envp);
10484f33deaSJordan K. Hubbard 
105997c6eefSYaroslav Tykhiy #ifdef PAM
106997c6eefSYaroslav Tykhiy 	/* use PAM to see if the user's account is available,
107997c6eefSYaroslav Tykhiy 	 * i.e., not locked or expired or whatever.  skip this
108997c6eefSYaroslav Tykhiy 	 * for system tasks from /etc/crontab -- they can run
109997c6eefSYaroslav Tykhiy 	 * as any user.
110997c6eefSYaroslav Tykhiy 	 */
111997c6eefSYaroslav Tykhiy 	if (strcmp(u->name, SYS_NAME)) {	/* not equal */
112997c6eefSYaroslav Tykhiy 		pam_handle_t *pamh = NULL;
113997c6eefSYaroslav Tykhiy 		int pam_err;
114997c6eefSYaroslav Tykhiy 		struct pam_conv pamc = {
115997c6eefSYaroslav Tykhiy 			.conv = openpam_nullconv,
116997c6eefSYaroslav Tykhiy 			.appdata_ptr = NULL
117997c6eefSYaroslav Tykhiy 		};
118997c6eefSYaroslav Tykhiy 
119997c6eefSYaroslav Tykhiy 		Debug(DPROC, ("[%d] checking account with PAM\n", getpid()))
120997c6eefSYaroslav Tykhiy 
121997c6eefSYaroslav Tykhiy 		/* u->name keeps crontab owner name while LOGNAME is the name
122997c6eefSYaroslav Tykhiy 		 * of user to run command on behalf of.  they should be the
123997c6eefSYaroslav Tykhiy 		 * same for a task from a per-user crontab.
124997c6eefSYaroslav Tykhiy 		 */
125997c6eefSYaroslav Tykhiy 		if (strcmp(u->name, usernm)) {
126997c6eefSYaroslav Tykhiy 			log_it(usernm, getpid(), "username ambiguity", u->name);
127997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
128997c6eefSYaroslav Tykhiy 		}
129997c6eefSYaroslav Tykhiy 
130997c6eefSYaroslav Tykhiy 		pam_err = pam_start("cron", usernm, &pamc, &pamh);
131997c6eefSYaroslav Tykhiy 		if (pam_err != PAM_SUCCESS) {
132997c6eefSYaroslav Tykhiy 			log_it("CRON", getpid(), "error", "can't start PAM");
133997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
134997c6eefSYaroslav Tykhiy 		}
135997c6eefSYaroslav Tykhiy 
136997c6eefSYaroslav Tykhiy 		pam_err = pam_acct_mgmt(pamh, PAM_SILENT);
137997c6eefSYaroslav Tykhiy 		/* Expired password shouldn't prevent the job from running. */
138997c6eefSYaroslav Tykhiy 		if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD) {
139997c6eefSYaroslav Tykhiy 			log_it(usernm, getpid(), "USER", "account unavailable");
140997c6eefSYaroslav Tykhiy 			exit(ERROR_EXIT);
141997c6eefSYaroslav Tykhiy 		}
142997c6eefSYaroslav Tykhiy 
143997c6eefSYaroslav Tykhiy 		pam_end(pamh, pam_err);
144997c6eefSYaroslav Tykhiy 	}
145997c6eefSYaroslav Tykhiy #endif
146997c6eefSYaroslav Tykhiy 
14784f33deaSJordan K. Hubbard #ifdef USE_SIGCHLD
14884f33deaSJordan K. Hubbard 	/* our parent is watching for our death by catching SIGCHLD.  we
14984f33deaSJordan K. Hubbard 	 * do not care to watch for our children's deaths this way -- we
1503df5ecacSUlrich Spörlein 	 * use wait() explicitly.  so we have to disable the signal (which
15184f33deaSJordan K. Hubbard 	 * was inherited from the parent).
15284f33deaSJordan K. Hubbard 	 */
1535e4a74ecSAndrey A. Chernov 	(void) signal(SIGCHLD, SIG_DFL);
15484f33deaSJordan K. Hubbard #else
15584f33deaSJordan K. Hubbard 	/* on system-V systems, we are ignoring SIGCLD.  we have to stop
15684f33deaSJordan K. Hubbard 	 * ignoring it now or the wait() in cron_pclose() won't work.
15784f33deaSJordan K. Hubbard 	 * because of this, we have to wait() for our children here, as well.
15884f33deaSJordan K. Hubbard 	 */
15984f33deaSJordan K. Hubbard 	(void) signal(SIGCLD, SIG_DFL);
16084f33deaSJordan K. Hubbard #endif /*BSD*/
16184f33deaSJordan K. Hubbard 
16284f33deaSJordan K. Hubbard 	/* create some pipes to talk to our future child
16384f33deaSJordan K. Hubbard 	 */
16484f33deaSJordan K. Hubbard 	pipe(stdin_pipe);	/* child's stdin */
16584f33deaSJordan K. Hubbard 	pipe(stdout_pipe);	/* child's stdout */
16684f33deaSJordan K. Hubbard 
16784f33deaSJordan K. Hubbard 	/* since we are a forked process, we can diddle the command string
16884f33deaSJordan K. Hubbard 	 * we were passed -- nobody else is going to use it again, right?
16984f33deaSJordan K. Hubbard 	 *
17084f33deaSJordan K. Hubbard 	 * if a % is present in the command, previous characters are the
17184f33deaSJordan K. Hubbard 	 * command, and subsequent characters are the additional input to
17284f33deaSJordan K. Hubbard 	 * the command.  Subsequent %'s will be transformed into newlines,
17384f33deaSJordan K. Hubbard 	 * but that happens later.
17486ed6de3SJoerg Wunsch 	 *
17586ed6de3SJoerg Wunsch 	 * If there are escaped %'s, remove the escape character.
17684f33deaSJordan K. Hubbard 	 */
17784f33deaSJordan K. Hubbard 	/*local*/{
17884f33deaSJordan K. Hubbard 		register int escaped = FALSE;
17984f33deaSJordan K. Hubbard 		register int ch;
18086ed6de3SJoerg Wunsch 		register char *p;
18184f33deaSJordan K. Hubbard 
182401e6468SPhilippe Charnier 		for (input_data = p = e->cmd; (ch = *input_data);
18386ed6de3SJoerg Wunsch 		     input_data++, p++) {
18486ed6de3SJoerg Wunsch 			if (p != input_data)
18586ed6de3SJoerg Wunsch 			    *p = ch;
18684f33deaSJordan K. Hubbard 			if (escaped) {
18786ed6de3SJoerg Wunsch 				if (ch == '%' || ch == '\\')
18886ed6de3SJoerg Wunsch 					*--p = ch;
18984f33deaSJordan K. Hubbard 				escaped = FALSE;
19084f33deaSJordan K. Hubbard 				continue;
19184f33deaSJordan K. Hubbard 			}
19284f33deaSJordan K. Hubbard 			if (ch == '\\') {
19384f33deaSJordan K. Hubbard 				escaped = TRUE;
19484f33deaSJordan K. Hubbard 				continue;
19584f33deaSJordan K. Hubbard 			}
19684f33deaSJordan K. Hubbard 			if (ch == '%') {
19784f33deaSJordan K. Hubbard 				*input_data++ = '\0';
19884f33deaSJordan K. Hubbard 				break;
19984f33deaSJordan K. Hubbard 			}
20084f33deaSJordan K. Hubbard 		}
20186ed6de3SJoerg Wunsch 		*p = '\0';
20284f33deaSJordan K. Hubbard 	}
20384f33deaSJordan K. Hubbard 
20484f33deaSJordan K. Hubbard 	/* fork again, this time so we can exec the user's command.
20584f33deaSJordan K. Hubbard 	 */
20684f33deaSJordan K. Hubbard 	switch (vfork()) {
20784f33deaSJordan K. Hubbard 	case -1:
20884f33deaSJordan K. Hubbard 		log_it("CRON",getpid(),"error","can't vfork");
20984f33deaSJordan K. Hubbard 		exit(ERROR_EXIT);
21084f33deaSJordan K. Hubbard 		/*NOTREACHED*/
21184f33deaSJordan K. Hubbard 	case 0:
21284f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
21384f33deaSJordan K. Hubbard 			      getpid()))
21484f33deaSJordan K. Hubbard 
215f5896bafSYaroslav Tykhiy 		if (e->uid == ROOT_UID)
216f5896bafSYaroslav Tykhiy 			Jitter = RootJitter;
217f5896bafSYaroslav Tykhiy 		if (Jitter != 0) {
218f5896bafSYaroslav Tykhiy 			srandom(getpid());
219f5896bafSYaroslav Tykhiy 			sleep(random() % Jitter);
220f5896bafSYaroslav Tykhiy 		}
221f5896bafSYaroslav Tykhiy 
22284f33deaSJordan K. Hubbard 		/* write a log message.  we've waited this long to do it
22384f33deaSJordan K. Hubbard 		 * because it was not until now that we knew the PID that
22484f33deaSJordan K. Hubbard 		 * the actual user command shell was going to get and the
22584f33deaSJordan K. Hubbard 		 * PID is part of the log message.
22684f33deaSJordan K. Hubbard 		 */
22784f33deaSJordan K. Hubbard 		/*local*/{
22884f33deaSJordan K. Hubbard 			char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
22984f33deaSJordan K. Hubbard 
23084f33deaSJordan K. Hubbard 			log_it(usernm, getpid(), "CMD", x);
23184f33deaSJordan K. Hubbard 			free(x);
23284f33deaSJordan K. Hubbard 		}
23384f33deaSJordan K. Hubbard 
23484f33deaSJordan K. Hubbard 		/* that's the last thing we'll log.  close the log files.
23584f33deaSJordan K. Hubbard 		 */
23684f33deaSJordan K. Hubbard #ifdef SYSLOG
23784f33deaSJordan K. Hubbard 		closelog();
23884f33deaSJordan K. Hubbard #endif
23984f33deaSJordan K. Hubbard 
24084f33deaSJordan K. Hubbard 		/* get new pgrp, void tty, etc.
24184f33deaSJordan K. Hubbard 		 */
24284f33deaSJordan K. Hubbard 		(void) setsid();
24384f33deaSJordan K. Hubbard 
24484f33deaSJordan K. Hubbard 		/* close the pipe ends that we won't use.  this doesn't affect
24584f33deaSJordan K. Hubbard 		 * the parent, who has to read and write them; it keeps the
24684f33deaSJordan K. Hubbard 		 * kernel from recording us as a potential client TWICE --
24784f33deaSJordan K. Hubbard 		 * which would keep it from sending SIGPIPE in otherwise
24884f33deaSJordan K. Hubbard 		 * appropriate circumstances.
24984f33deaSJordan K. Hubbard 		 */
25084f33deaSJordan K. Hubbard 		close(stdin_pipe[WRITE_PIPE]);
25184f33deaSJordan K. Hubbard 		close(stdout_pipe[READ_PIPE]);
25284f33deaSJordan K. Hubbard 
25384f33deaSJordan K. Hubbard 		/* grandchild process.  make std{in,out} be the ends of
25484f33deaSJordan K. Hubbard 		 * pipes opened by our daddy; make stderr go to stdout.
25584f33deaSJordan K. Hubbard 		 */
25684f33deaSJordan K. Hubbard 		close(STDIN);	dup2(stdin_pipe[READ_PIPE], STDIN);
25784f33deaSJordan K. Hubbard 		close(STDOUT);	dup2(stdout_pipe[WRITE_PIPE], STDOUT);
25884f33deaSJordan K. Hubbard 		close(STDERR);	dup2(STDOUT, STDERR);
25984f33deaSJordan K. Hubbard 
26084f33deaSJordan K. Hubbard 		/* close the pipes we just dup'ed.  The resources will remain.
26184f33deaSJordan K. Hubbard 		 */
26284f33deaSJordan K. Hubbard 		close(stdin_pipe[READ_PIPE]);
26384f33deaSJordan K. Hubbard 		close(stdout_pipe[WRITE_PIPE]);
26484f33deaSJordan K. Hubbard 
26584f33deaSJordan K. Hubbard 		/* set our login universe.  Do this in the grandchild
26684f33deaSJordan K. Hubbard 		 * so that the child can invoke /usr/lib/sendmail
26784f33deaSJordan K. Hubbard 		 * without surprises.
26884f33deaSJordan K. Hubbard 		 */
26984f33deaSJordan K. Hubbard 		do_univ(u);
27084f33deaSJordan K. Hubbard 
271b25b7bc1SDavid Nugent # if defined(LOGIN_CAP)
272b25b7bc1SDavid Nugent 		/* Set user's entire context, but skip the environment
273b25b7bc1SDavid Nugent 		 * as cron provides a separate interface for this
274b25b7bc1SDavid Nugent 		 */
2750435c150SAndrey A. Chernov 		if ((pwd = getpwnam(usernm)) == NULL)
276c00d650fSPeter Wemm 			pwd = getpwuid(e->uid);
2770435c150SAndrey A. Chernov 		lc = NULL;
2780435c150SAndrey A. Chernov 		if (pwd != NULL) {
2790435c150SAndrey A. Chernov 			pwd->pw_gid = e->gid;
2800435c150SAndrey A. Chernov 			if (e->class != NULL)
2810435c150SAndrey A. Chernov 				lc = login_getclass(e->class);
2820435c150SAndrey A. Chernov 		}
2839efe2eccSPeter Wemm 		if (pwd &&
2840435c150SAndrey A. Chernov 		    setusercontext(lc, pwd, e->uid,
2859efe2eccSPeter Wemm 			    LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
2869efe2eccSPeter Wemm 			(void) endpwent();
2879efe2eccSPeter Wemm 		else {
288c00d650fSPeter Wemm 			/* fall back to the old method */
2899efe2eccSPeter Wemm 			(void) endpwent();
290c00d650fSPeter Wemm # endif
291c00d650fSPeter Wemm 			/* set our directory, uid and gid.  Set gid first,
292708f27a1SMaxim Konovalov 			 * since once we set uid, we've lost root privileges.
29384f33deaSJordan K. Hubbard 			 */
294bb0aa1a5SMaxim Konovalov 			if (setgid(e->gid) != 0) {
295bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
296bb0aa1a5SMaxim Konovalov 				    "error", "setgid failed");
297bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
298bb0aa1a5SMaxim Konovalov 			}
29984f33deaSJordan K. Hubbard # if defined(BSD)
300bb0aa1a5SMaxim Konovalov 			if (initgroups(usernm, e->gid) != 0) {
301bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
302bb0aa1a5SMaxim Konovalov 				    "error", "initgroups failed");
303bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
304bb0aa1a5SMaxim Konovalov 			}
30584f33deaSJordan K. Hubbard # endif
306bb0aa1a5SMaxim Konovalov 			if (setlogin(usernm) != 0) {
307bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
308bb0aa1a5SMaxim Konovalov 				    "error", "setlogin failed");
309bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
310bb0aa1a5SMaxim Konovalov 			}
311bb0aa1a5SMaxim Konovalov 			if (setuid(e->uid) != 0) {
312bb0aa1a5SMaxim Konovalov 				log_it(usernm, getpid(),
313bb0aa1a5SMaxim Konovalov 				    "error", "setuid failed");
314bb0aa1a5SMaxim Konovalov 				exit(ERROR_EXIT);
315bb0aa1a5SMaxim Konovalov 			}
316bb0aa1a5SMaxim Konovalov 			/* we aren't root after this..*/
317c00d650fSPeter Wemm #if defined(LOGIN_CAP)
318c00d650fSPeter Wemm 		}
3193d99cebfSAndrey A. Chernov 		if (lc != NULL)
3203d99cebfSAndrey A. Chernov 			login_close(lc);
321b25b7bc1SDavid Nugent #endif
322bdddbd2fSPaul Traina 		chdir(env_get("HOME", e->envp));
32384f33deaSJordan K. Hubbard 
32484f33deaSJordan K. Hubbard 		/* exec the command.
32584f33deaSJordan K. Hubbard 		 */
32684f33deaSJordan K. Hubbard 		{
32784f33deaSJordan K. Hubbard 			char	*shell = env_get("SHELL", e->envp);
32884f33deaSJordan K. Hubbard 
32984f33deaSJordan K. Hubbard # if DEBUGGING
33084f33deaSJordan K. Hubbard 			if (DebugFlags & DTEST) {
33184f33deaSJordan K. Hubbard 				fprintf(stderr,
33284f33deaSJordan K. Hubbard 				"debug DTEST is on, not exec'ing command.\n");
33384f33deaSJordan K. Hubbard 				fprintf(stderr,
33484f33deaSJordan K. Hubbard 				"\tcmd='%s' shell='%s'\n", e->cmd, shell);
33584f33deaSJordan K. Hubbard 				_exit(OK_EXIT);
33684f33deaSJordan K. Hubbard 			}
33784f33deaSJordan K. Hubbard # endif /*DEBUGGING*/
33884f33deaSJordan K. Hubbard 			execle(shell, shell, "-c", e->cmd, (char *)0, e->envp);
339401e6468SPhilippe Charnier 			warn("execl: couldn't exec `%s'", shell);
34084f33deaSJordan K. Hubbard 			_exit(ERROR_EXIT);
34184f33deaSJordan K. Hubbard 		}
34284f33deaSJordan K. Hubbard 		break;
34384f33deaSJordan K. Hubbard 	default:
34484f33deaSJordan K. Hubbard 		/* parent process */
34584f33deaSJordan K. Hubbard 		break;
34684f33deaSJordan K. Hubbard 	}
34784f33deaSJordan K. Hubbard 
34884f33deaSJordan K. Hubbard 	children++;
34984f33deaSJordan K. Hubbard 
35084f33deaSJordan K. Hubbard 	/* middle process, child of original cron, parent of process running
35184f33deaSJordan K. Hubbard 	 * the user's command.
35284f33deaSJordan K. Hubbard 	 */
35384f33deaSJordan K. Hubbard 
35484f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
35584f33deaSJordan K. Hubbard 
35684f33deaSJordan K. Hubbard 	/* close the ends of the pipe that will only be referenced in the
35784f33deaSJordan K. Hubbard 	 * grandchild process...
35884f33deaSJordan K. Hubbard 	 */
35984f33deaSJordan K. Hubbard 	close(stdin_pipe[READ_PIPE]);
36084f33deaSJordan K. Hubbard 	close(stdout_pipe[WRITE_PIPE]);
36184f33deaSJordan K. Hubbard 
36284f33deaSJordan K. Hubbard 	/*
36384f33deaSJordan K. Hubbard 	 * write, to the pipe connected to child's stdin, any input specified
36484f33deaSJordan K. Hubbard 	 * after a % in the crontab entry.  while we copy, convert any
36584f33deaSJordan K. Hubbard 	 * additional %'s to newlines.  when done, if some characters were
36684f33deaSJordan K. Hubbard 	 * written and the last one wasn't a newline, write a newline.
36784f33deaSJordan K. Hubbard 	 *
36884f33deaSJordan K. Hubbard 	 * Note that if the input data won't fit into one pipe buffer (2K
36984f33deaSJordan K. Hubbard 	 * or 4K on most BSD systems), and the child doesn't read its stdin,
37084f33deaSJordan K. Hubbard 	 * we would block here.  thus we must fork again.
37184f33deaSJordan K. Hubbard 	 */
37284f33deaSJordan K. Hubbard 
37384f33deaSJordan K. Hubbard 	if (*input_data && fork() == 0) {
37484f33deaSJordan K. Hubbard 		register FILE	*out = fdopen(stdin_pipe[WRITE_PIPE], "w");
37584f33deaSJordan K. Hubbard 		register int	need_newline = FALSE;
37684f33deaSJordan K. Hubbard 		register int	escaped = FALSE;
37784f33deaSJordan K. Hubbard 		register int	ch;
37884f33deaSJordan K. Hubbard 
37964ae78cbSGuy Helmer 		if (out == NULL) {
38064ae78cbSGuy Helmer 			warn("fdopen failed in child2");
38164ae78cbSGuy Helmer 			_exit(ERROR_EXIT);
38264ae78cbSGuy Helmer 		}
38364ae78cbSGuy Helmer 
38484f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
38584f33deaSJordan K. Hubbard 
38684f33deaSJordan K. Hubbard 		/* close the pipe we don't use, since we inherited it and
38784f33deaSJordan K. Hubbard 		 * are part of its reference count now.
38884f33deaSJordan K. Hubbard 		 */
38984f33deaSJordan K. Hubbard 		close(stdout_pipe[READ_PIPE]);
39084f33deaSJordan K. Hubbard 
39184f33deaSJordan K. Hubbard 		/* translation:
39284f33deaSJordan K. Hubbard 		 *	\% -> %
39384f33deaSJordan K. Hubbard 		 *	%  -> \n
39484f33deaSJordan K. Hubbard 		 *	\x -> \x	for all x != %
39584f33deaSJordan K. Hubbard 		 */
396401e6468SPhilippe Charnier 		while ((ch = *input_data++)) {
39784f33deaSJordan K. Hubbard 			if (escaped) {
39884f33deaSJordan K. Hubbard 				if (ch != '%')
39984f33deaSJordan K. Hubbard 					putc('\\', out);
40084f33deaSJordan K. Hubbard 			} else {
40184f33deaSJordan K. Hubbard 				if (ch == '%')
40284f33deaSJordan K. Hubbard 					ch = '\n';
40384f33deaSJordan K. Hubbard 			}
40484f33deaSJordan K. Hubbard 
40584f33deaSJordan K. Hubbard 			if (!(escaped = (ch == '\\'))) {
40684f33deaSJordan K. Hubbard 				putc(ch, out);
40784f33deaSJordan K. Hubbard 				need_newline = (ch != '\n');
40884f33deaSJordan K. Hubbard 			}
40984f33deaSJordan K. Hubbard 		}
41084f33deaSJordan K. Hubbard 		if (escaped)
41184f33deaSJordan K. Hubbard 			putc('\\', out);
41284f33deaSJordan K. Hubbard 		if (need_newline)
41384f33deaSJordan K. Hubbard 			putc('\n', out);
41484f33deaSJordan K. Hubbard 
41584f33deaSJordan K. Hubbard 		/* close the pipe, causing an EOF condition.  fclose causes
41684f33deaSJordan K. Hubbard 		 * stdin_pipe[WRITE_PIPE] to be closed, too.
41784f33deaSJordan K. Hubbard 		 */
41884f33deaSJordan K. Hubbard 		fclose(out);
41984f33deaSJordan K. Hubbard 
42084f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
42184f33deaSJordan K. Hubbard 		exit(0);
42284f33deaSJordan K. Hubbard 	}
42384f33deaSJordan K. Hubbard 
42484f33deaSJordan K. Hubbard 	/* close the pipe to the grandkiddie's stdin, since its wicked uncle
42584f33deaSJordan K. Hubbard 	 * ernie back there has it open and will close it when he's done.
42684f33deaSJordan K. Hubbard 	 */
42784f33deaSJordan K. Hubbard 	close(stdin_pipe[WRITE_PIPE]);
42884f33deaSJordan K. Hubbard 
42984f33deaSJordan K. Hubbard 	children++;
43084f33deaSJordan K. Hubbard 
43184f33deaSJordan K. Hubbard 	/*
43284f33deaSJordan K. Hubbard 	 * read output from the grandchild.  it's stderr has been redirected to
43384f33deaSJordan K. Hubbard 	 * it's stdout, which has been redirected to our pipe.  if there is any
43484f33deaSJordan K. Hubbard 	 * output, we'll be mailing it to the user whose crontab this is...
43584f33deaSJordan K. Hubbard 	 * when the grandchild exits, we'll get EOF.
43684f33deaSJordan K. Hubbard 	 */
43784f33deaSJordan K. Hubbard 
43884f33deaSJordan K. Hubbard 	Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
43984f33deaSJordan K. Hubbard 
44084f33deaSJordan K. Hubbard 	/*local*/{
44184f33deaSJordan K. Hubbard 		register FILE	*in = fdopen(stdout_pipe[READ_PIPE], "r");
4429be756b5SMike Silbersack 		register int	ch;
44384f33deaSJordan K. Hubbard 
44464ae78cbSGuy Helmer 		if (in == NULL) {
44564ae78cbSGuy Helmer 			warn("fdopen failed in child");
44664ae78cbSGuy Helmer 			_exit(ERROR_EXIT);
44764ae78cbSGuy Helmer 		}
44864ae78cbSGuy Helmer 
4499be756b5SMike Silbersack 		ch = getc(in);
45084f33deaSJordan K. Hubbard 		if (ch != EOF) {
45184f33deaSJordan K. Hubbard 			register FILE	*mail;
45284f33deaSJordan K. Hubbard 			register int	bytes = 1;
45384f33deaSJordan K. Hubbard 			int		status = 0;
45484f33deaSJordan K. Hubbard 
45584f33deaSJordan K. Hubbard 			Debug(DPROC|DEXT,
45684f33deaSJordan K. Hubbard 				("[%d] got data (%x:%c) from grandchild\n",
45784f33deaSJordan K. Hubbard 					getpid(), ch, ch))
45884f33deaSJordan K. Hubbard 
45984f33deaSJordan K. Hubbard 			/* get name of recipient.  this is MAILTO if set to a
46084f33deaSJordan K. Hubbard 			 * valid local username; USER otherwise.
46184f33deaSJordan K. Hubbard 			 */
462b75634d2SDmitry Morozovsky 			if (mailto == NULL) {
463b75634d2SDmitry Morozovsky 				/* MAILTO not present, set to USER,
464b75634d2SDmitry Morozovsky 				 * unless globally overriden.
46584f33deaSJordan K. Hubbard 				 */
466b75634d2SDmitry Morozovsky 				if (defmailto)
467b75634d2SDmitry Morozovsky 					mailto = defmailto;
468b75634d2SDmitry Morozovsky 				else
46984f33deaSJordan K. Hubbard 					mailto = usernm;
47084f33deaSJordan K. Hubbard 			}
4711168e5f1SDmitry Morozovsky 			if (mailto && *mailto == '\0')
4721168e5f1SDmitry Morozovsky 				mailto = NULL;
47384f33deaSJordan K. Hubbard 
47484f33deaSJordan K. Hubbard 			/* if we are supposed to be mailing, MAILTO will
47584f33deaSJordan K. Hubbard 			 * be non-NULL.  only in this case should we set
47684f33deaSJordan K. Hubbard 			 * up the mail command and subjects and stuff...
47784f33deaSJordan K. Hubbard 			 */
47884f33deaSJordan K. Hubbard 
4791168e5f1SDmitry Morozovsky 			if (mailto) {
48084f33deaSJordan K. Hubbard 				register char	**env;
48184f33deaSJordan K. Hubbard 				auto char	mailcmd[MAX_COMMAND];
48284f33deaSJordan K. Hubbard 				auto char	hostname[MAXHOSTNAMELEN];
48384f33deaSJordan K. Hubbard 
484*713c03d5SPeter Wemm 				if (gethostname(hostname, MAXHOSTNAMELEN) == -1)
485*713c03d5SPeter Wemm 					hostname[0] = '\0';
486*713c03d5SPeter Wemm 				hostname[sizeof(hostname) - 1] = '\0';
487bdddbd2fSPaul Traina 				(void) snprintf(mailcmd, sizeof(mailcmd),
488bdddbd2fSPaul Traina 					       MAILARGS, MAILCMD);
489fe46d6a8SAndrey A. Chernov 				if (!(mail = cron_popen(mailcmd, "w", e))) {
490401e6468SPhilippe Charnier 					warn("%s", MAILCMD);
49184f33deaSJordan K. Hubbard 					(void) _exit(ERROR_EXIT);
49284f33deaSJordan K. Hubbard 				}
493*713c03d5SPeter Wemm 				fprintf(mail, "From: Cron Daemon <%s@%s>\n",
494*713c03d5SPeter Wemm 					usernm, hostname);
49584f33deaSJordan K. Hubbard 				fprintf(mail, "To: %s\n", mailto);
49684f33deaSJordan K. Hubbard 				fprintf(mail, "Subject: Cron <%s@%s> %s\n",
49784f33deaSJordan K. Hubbard 					usernm, first_word(hostname, "."),
49884f33deaSJordan K. Hubbard 					e->cmd);
49984f33deaSJordan K. Hubbard # if defined(MAIL_DATE)
50084f33deaSJordan K. Hubbard 				fprintf(mail, "Date: %s\n",
50184f33deaSJordan K. Hubbard 					arpadate(&TargetTime));
50284f33deaSJordan K. Hubbard # endif /* MAIL_DATE */
50384f33deaSJordan K. Hubbard 				for (env = e->envp;  *env;  env++)
50484f33deaSJordan K. Hubbard 					fprintf(mail, "X-Cron-Env: <%s>\n",
50584f33deaSJordan K. Hubbard 						*env);
50684f33deaSJordan K. Hubbard 				fprintf(mail, "\n");
50784f33deaSJordan K. Hubbard 
50884f33deaSJordan K. Hubbard 				/* this was the first char from the pipe
50984f33deaSJordan K. Hubbard 				 */
51084f33deaSJordan K. Hubbard 				putc(ch, mail);
51184f33deaSJordan K. Hubbard 			}
51284f33deaSJordan K. Hubbard 
51384f33deaSJordan K. Hubbard 			/* we have to read the input pipe no matter whether
51484f33deaSJordan K. Hubbard 			 * we mail or not, but obviously we only write to
51584f33deaSJordan K. Hubbard 			 * mail pipe if we ARE mailing.
51684f33deaSJordan K. Hubbard 			 */
51784f33deaSJordan K. Hubbard 
51884f33deaSJordan K. Hubbard 			while (EOF != (ch = getc(in))) {
51984f33deaSJordan K. Hubbard 				bytes++;
52084f33deaSJordan K. Hubbard 				if (mailto)
52184f33deaSJordan K. Hubbard 					putc(ch, mail);
52284f33deaSJordan K. Hubbard 			}
52384f33deaSJordan K. Hubbard 
52484f33deaSJordan K. Hubbard 			/* only close pipe if we opened it -- i.e., we're
52584f33deaSJordan K. Hubbard 			 * mailing...
52684f33deaSJordan K. Hubbard 			 */
52784f33deaSJordan K. Hubbard 
52884f33deaSJordan K. Hubbard 			if (mailto) {
52984f33deaSJordan K. Hubbard 				Debug(DPROC, ("[%d] closing pipe to mail\n",
53084f33deaSJordan K. Hubbard 					getpid()))
53184f33deaSJordan K. Hubbard 				/* Note: the pclose will probably see
53284f33deaSJordan K. Hubbard 				 * the termination of the grandchild
53384f33deaSJordan K. Hubbard 				 * in addition to the mail process, since
53484f33deaSJordan K. Hubbard 				 * it (the grandchild) is likely to exit
53584f33deaSJordan K. Hubbard 				 * after closing its stdout.
53684f33deaSJordan K. Hubbard 				 */
53784f33deaSJordan K. Hubbard 				status = cron_pclose(mail);
53884f33deaSJordan K. Hubbard 			}
53984f33deaSJordan K. Hubbard 
54084f33deaSJordan K. Hubbard 			/* if there was output and we could not mail it,
54184f33deaSJordan K. Hubbard 			 * log the facts so the poor user can figure out
54284f33deaSJordan K. Hubbard 			 * what's going on.
54384f33deaSJordan K. Hubbard 			 */
54484f33deaSJordan K. Hubbard 			if (mailto && status) {
54584f33deaSJordan K. Hubbard 				char buf[MAX_TEMPSTR];
54684f33deaSJordan K. Hubbard 
547bdddbd2fSPaul Traina 				snprintf(buf, sizeof(buf),
54884f33deaSJordan K. Hubbard 			"mailed %d byte%s of output but got status 0x%04x\n",
54984f33deaSJordan K. Hubbard 					bytes, (bytes==1)?"":"s",
55084f33deaSJordan K. Hubbard 					status);
55184f33deaSJordan K. Hubbard 				log_it(usernm, getpid(), "MAIL", buf);
55284f33deaSJordan K. Hubbard 			}
55384f33deaSJordan K. Hubbard 
55484f33deaSJordan K. Hubbard 		} /*if data from grandchild*/
55584f33deaSJordan K. Hubbard 
55684f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
55784f33deaSJordan K. Hubbard 
55884f33deaSJordan K. Hubbard 		fclose(in);	/* also closes stdout_pipe[READ_PIPE] */
55984f33deaSJordan K. Hubbard 	}
56084f33deaSJordan K. Hubbard 
56184f33deaSJordan K. Hubbard 	/* wait for children to die.
56284f33deaSJordan K. Hubbard 	 */
56384f33deaSJordan K. Hubbard 	for (;  children > 0;  children--)
56484f33deaSJordan K. Hubbard 	{
56584f33deaSJordan K. Hubbard 		WAIT_T		waiter;
56684f33deaSJordan K. Hubbard 		PID_T		pid;
56784f33deaSJordan K. Hubbard 
56884f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n",
56984f33deaSJordan K. Hubbard 			getpid(), children))
57084f33deaSJordan K. Hubbard 		pid = wait(&waiter);
57184f33deaSJordan K. Hubbard 		if (pid < OK) {
57284f33deaSJordan K. Hubbard 			Debug(DPROC, ("[%d] no more grandchildren--mail written?\n",
57384f33deaSJordan K. Hubbard 				getpid()))
57484f33deaSJordan K. Hubbard 			break;
57584f33deaSJordan K. Hubbard 		}
57684f33deaSJordan K. Hubbard 		Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x",
57784f33deaSJordan K. Hubbard 			getpid(), pid, WEXITSTATUS(waiter)))
57884f33deaSJordan K. Hubbard 		if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
57984f33deaSJordan K. Hubbard 			Debug(DPROC, (", dumped core"))
58084f33deaSJordan K. Hubbard 		Debug(DPROC, ("\n"))
58184f33deaSJordan K. Hubbard 	}
58284f33deaSJordan K. Hubbard }
58384f33deaSJordan K. Hubbard 
58484f33deaSJordan K. Hubbard 
58584f33deaSJordan K. Hubbard static void
58684f33deaSJordan K. Hubbard do_univ(u)
58784f33deaSJordan K. Hubbard 	user	*u;
58884f33deaSJordan K. Hubbard {
58984f33deaSJordan K. Hubbard #if defined(sequent)
59084f33deaSJordan K. Hubbard /* Dynix (Sequent) hack to put the user associated with
59184f33deaSJordan K. Hubbard  * the passed user structure into the ATT universe if
59284f33deaSJordan K. Hubbard  * necessary.  We have to dig the gecos info out of
59384f33deaSJordan K. Hubbard  * the user's password entry to see if the magic
59484f33deaSJordan K. Hubbard  * "universe(att)" string is present.
59584f33deaSJordan K. Hubbard  */
59684f33deaSJordan K. Hubbard 
59784f33deaSJordan K. Hubbard 	struct	passwd	*p;
59884f33deaSJordan K. Hubbard 	char	*s;
59984f33deaSJordan K. Hubbard 	int	i;
60084f33deaSJordan K. Hubbard 
60184f33deaSJordan K. Hubbard 	p = getpwuid(u->uid);
60284f33deaSJordan K. Hubbard 	(void) endpwent();
60384f33deaSJordan K. Hubbard 
60484f33deaSJordan K. Hubbard 	if (p == NULL)
60584f33deaSJordan K. Hubbard 		return;
60684f33deaSJordan K. Hubbard 
60784f33deaSJordan K. Hubbard 	s = p->pw_gecos;
60884f33deaSJordan K. Hubbard 
60984f33deaSJordan K. Hubbard 	for (i = 0; i < 4; i++)
61084f33deaSJordan K. Hubbard 	{
61184f33deaSJordan K. Hubbard 		if ((s = strchr(s, ',')) == NULL)
61284f33deaSJordan K. Hubbard 			return;
61384f33deaSJordan K. Hubbard 		s++;
61484f33deaSJordan K. Hubbard 	}
61584f33deaSJordan K. Hubbard 	if (strcmp(s, "universe(att)"))
61684f33deaSJordan K. Hubbard 		return;
61784f33deaSJordan K. Hubbard 
61884f33deaSJordan K. Hubbard 	(void) universe(U_ATT);
61984f33deaSJordan K. Hubbard #endif
62084f33deaSJordan K. Hubbard }
621