1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
25 #include <sys/signal.h>
27 # include <sys/universe.h>
32 #if defined(LOGIN_CAP)
33 # include <login_cap.h>
37 static void child_process __P((entry *, user *)),
38 do_univ __P((user *));
46 Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
47 getpid(), e->cmd, u->name, e->uid, e->gid))
49 /* fork to become asynchronous -- parent process is done immediately,
50 * and continues to run the normal cron code, which means return to
51 * tick(). the child and grandchild don't leave this function, alive.
53 * vfork() is unsuitable, since we have much to do, and the parent
54 * needs to be able to run off and fork other processes.
58 log_it("CRON",getpid(),"error","can't fork");
64 Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
71 Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
80 int stdin_pipe[2], stdout_pipe[2];
81 register char *input_data;
82 char *usernm, *mailto;
84 # if defined(LOGIN_CAP)
89 Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
91 /* mark ourselves as different to PS command watchers by upshifting
92 * our program name. This has no effect on some kernels.
94 setproctitle("running job");
96 /* discover some useful and important environment settings
98 usernm = env_get("LOGNAME", e->envp);
99 mailto = env_get("MAILTO", e->envp);
102 /* our parent is watching for our death by catching SIGCHLD. we
103 * do not care to watch for our children's deaths this way -- we
104 * use wait() explictly. so we have to disable the signal (which
105 * was inherited from the parent).
107 (void) signal(SIGCHLD, SIG_DFL);
109 /* on system-V systems, we are ignoring SIGCLD. we have to stop
110 * ignoring it now or the wait() in cron_pclose() won't work.
111 * because of this, we have to wait() for our children here, as well.
113 (void) signal(SIGCLD, SIG_DFL);
116 /* create some pipes to talk to our future child
118 pipe(stdin_pipe); /* child's stdin */
119 pipe(stdout_pipe); /* child's stdout */
121 /* since we are a forked process, we can diddle the command string
122 * we were passed -- nobody else is going to use it again, right?
124 * if a % is present in the command, previous characters are the
125 * command, and subsequent characters are the additional input to
126 * the command. Subsequent %'s will be transformed into newlines,
127 * but that happens later.
129 * If there are escaped %'s, remove the escape character.
132 register int escaped = FALSE;
136 for (input_data = p = e->cmd; (ch = *input_data);
141 if (ch == '%' || ch == '\\')
151 *input_data++ = '\0';
158 /* fork again, this time so we can exec the user's command.
162 log_it("CRON",getpid(),"error","can't vfork");
166 Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
169 if (e->uid == ROOT_UID)
173 sleep(random() % Jitter);
176 /* write a log message. we've waited this long to do it
177 * because it was not until now that we knew the PID that
178 * the actual user command shell was going to get and the
179 * PID is part of the log message.
182 char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
184 log_it(usernm, getpid(), "CMD", x);
188 /* that's the last thing we'll log. close the log files.
194 /* get new pgrp, void tty, etc.
198 /* close the pipe ends that we won't use. this doesn't affect
199 * the parent, who has to read and write them; it keeps the
200 * kernel from recording us as a potential client TWICE --
201 * which would keep it from sending SIGPIPE in otherwise
202 * appropriate circumstances.
204 close(stdin_pipe[WRITE_PIPE]);
205 close(stdout_pipe[READ_PIPE]);
207 /* grandchild process. make std{in,out} be the ends of
208 * pipes opened by our daddy; make stderr go to stdout.
210 close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN);
211 close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT);
212 close(STDERR); dup2(STDOUT, STDERR);
214 /* close the pipes we just dup'ed. The resources will remain.
216 close(stdin_pipe[READ_PIPE]);
217 close(stdout_pipe[WRITE_PIPE]);
219 /* set our login universe. Do this in the grandchild
220 * so that the child can invoke /usr/lib/sendmail
225 # if defined(LOGIN_CAP)
226 /* Set user's entire context, but skip the environment
227 * as cron provides a separate interface for this
229 if ((pwd = getpwnam(usernm)) == NULL)
230 pwd = getpwuid(e->uid);
233 pwd->pw_gid = e->gid;
234 if (e->class != NULL)
235 lc = login_getclass(e->class);
238 setusercontext(lc, pwd, e->uid,
239 LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
242 /* fall back to the old method */
245 /* set our directory, uid and gid. Set gid first,
246 * since once we set uid, we've lost root privledges.
248 if (setgid(e->gid) != 0) {
249 log_it(usernm, getpid(),
250 "error", "setgid failed");
254 if (initgroups(usernm, e->gid) != 0) {
255 log_it(usernm, getpid(),
256 "error", "initgroups failed");
260 if (setlogin(usernm) != 0) {
261 log_it(usernm, getpid(),
262 "error", "setlogin failed");
265 if (setuid(e->uid) != 0) {
266 log_it(usernm, getpid(),
267 "error", "setuid failed");
270 /* we aren't root after this..*/
271 #if defined(LOGIN_CAP)
276 chdir(env_get("HOME", e->envp));
281 char *shell = env_get("SHELL", e->envp);
284 if (DebugFlags & DTEST) {
286 "debug DTEST is on, not exec'ing command.\n");
288 "\tcmd='%s' shell='%s'\n", e->cmd, shell);
291 # endif /*DEBUGGING*/
292 execle(shell, shell, "-c", e->cmd, (char *)0, e->envp);
293 warn("execl: couldn't exec `%s'", shell);
304 /* middle process, child of original cron, parent of process running
305 * the user's command.
308 Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
310 /* close the ends of the pipe that will only be referenced in the
311 * grandchild process...
313 close(stdin_pipe[READ_PIPE]);
314 close(stdout_pipe[WRITE_PIPE]);
317 * write, to the pipe connected to child's stdin, any input specified
318 * after a % in the crontab entry. while we copy, convert any
319 * additional %'s to newlines. when done, if some characters were
320 * written and the last one wasn't a newline, write a newline.
322 * Note that if the input data won't fit into one pipe buffer (2K
323 * or 4K on most BSD systems), and the child doesn't read its stdin,
324 * we would block here. thus we must fork again.
327 if (*input_data && fork() == 0) {
328 register FILE *out = fdopen(stdin_pipe[WRITE_PIPE], "w");
329 register int need_newline = FALSE;
330 register int escaped = FALSE;
334 warn("fdopen failed in child2");
338 Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
340 /* close the pipe we don't use, since we inherited it and
341 * are part of its reference count now.
343 close(stdout_pipe[READ_PIPE]);
348 * \x -> \x for all x != %
350 while ((ch = *input_data++)) {
359 if (!(escaped = (ch == '\\'))) {
361 need_newline = (ch != '\n');
369 /* close the pipe, causing an EOF condition. fclose causes
370 * stdin_pipe[WRITE_PIPE] to be closed, too.
374 Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
378 /* close the pipe to the grandkiddie's stdin, since its wicked uncle
379 * ernie back there has it open and will close it when he's done.
381 close(stdin_pipe[WRITE_PIPE]);
386 * read output from the grandchild. it's stderr has been redirected to
387 * it's stdout, which has been redirected to our pipe. if there is any
388 * output, we'll be mailing it to the user whose crontab this is...
389 * when the grandchild exits, we'll get EOF.
392 Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
395 register FILE *in = fdopen(stdout_pipe[READ_PIPE], "r");
399 warn("fdopen failed in child");
406 register int bytes = 1;
410 ("[%d] got data (%x:%c) from grandchild\n",
413 /* get name of recipient. this is MAILTO if set to a
414 * valid local username; USER otherwise.
416 if (mailto == NULL) {
417 /* MAILTO not present, set to USER,
418 * unless globally overriden.
425 if (mailto && *mailto == '\0')
428 /* if we are supposed to be mailing, MAILTO will
429 * be non-NULL. only in this case should we set
430 * up the mail command and subjects and stuff...
435 auto char mailcmd[MAX_COMMAND];
436 auto char hostname[MAXHOSTNAMELEN];
438 (void) gethostname(hostname, MAXHOSTNAMELEN);
439 (void) snprintf(mailcmd, sizeof(mailcmd),
441 if (!(mail = cron_popen(mailcmd, "w", e))) {
443 (void) _exit(ERROR_EXIT);
445 fprintf(mail, "From: %s (Cron Daemon)\n", usernm);
446 fprintf(mail, "To: %s\n", mailto);
447 fprintf(mail, "Subject: Cron <%s@%s> %s\n",
448 usernm, first_word(hostname, "."),
450 # if defined(MAIL_DATE)
451 fprintf(mail, "Date: %s\n",
452 arpadate(&TargetTime));
453 # endif /* MAIL_DATE */
454 for (env = e->envp; *env; env++)
455 fprintf(mail, "X-Cron-Env: <%s>\n",
459 /* this was the first char from the pipe
464 /* we have to read the input pipe no matter whether
465 * we mail or not, but obviously we only write to
466 * mail pipe if we ARE mailing.
469 while (EOF != (ch = getc(in))) {
475 /* only close pipe if we opened it -- i.e., we're
480 Debug(DPROC, ("[%d] closing pipe to mail\n",
482 /* Note: the pclose will probably see
483 * the termination of the grandchild
484 * in addition to the mail process, since
485 * it (the grandchild) is likely to exit
486 * after closing its stdout.
488 status = cron_pclose(mail);
491 /* if there was output and we could not mail it,
492 * log the facts so the poor user can figure out
495 if (mailto && status) {
496 char buf[MAX_TEMPSTR];
498 snprintf(buf, sizeof(buf),
499 "mailed %d byte%s of output but got status 0x%04x\n",
500 bytes, (bytes==1)?"":"s",
502 log_it(usernm, getpid(), "MAIL", buf);
505 } /*if data from grandchild*/
507 Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
509 fclose(in); /* also closes stdout_pipe[READ_PIPE] */
512 /* wait for children to die.
514 for (; children > 0; children--)
519 Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n",
523 Debug(DPROC, ("[%d] no more grandchildren--mail written?\n",
527 Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x",
528 getpid(), pid, WEXITSTATUS(waiter)))
529 if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
530 Debug(DPROC, (", dumped core"))
541 /* Dynix (Sequent) hack to put the user associated with
542 * the passed user structure into the ATT universe if
543 * necessary. We have to dig the gecos info out of
544 * the user's password entry to see if the magic
545 * "universe(att)" string is present.
552 p = getpwuid(u->uid);
560 for (i = 0; i < 4; i++)
562 if ((s = strchr(s, ',')) == NULL)
566 if (strcmp(s, "universe(att)"))
569 (void) universe(U_ATT);