#include "cron.h"
#include <sys/signal.h>
#if defined(SYSLOG)
# include <syslog.h>
#endif
#if defined(LOGIN_CAP)
# include <login_cap.h>
#endif
#ifdef PAM
# include <security/pam_appl.h>
# include <security/openpam.h>
#endif
static void child_process(entry *, user *);
void
do_command(entry *e, user *u)
{
Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
getpid(), e->cmd, u->name, e->uid, e->gid))
switch (fork()) {
case -1:
log_it("CRON",getpid(),"error","can't fork");
break;
case 0:
acquire_daemonlock(1);
child_process(e, u);
Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
_exit(OK_EXIT);
break;
default:
break;
}
Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
}
static void
child_process(entry *e, user *u)
{
int stdin_pipe[2], stdout_pipe[2];
int jitter;
char *input_data;
char *usernm, *mailto;
int children = 0;
# if defined(LOGIN_CAP)
struct passwd *pwd;
login_cap_t *lc;
# endif
Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
setproctitle("running job");
usernm = env_get("LOGNAME", e->envp);
mailto = env_get("MAILTO", e->envp);
#ifdef PAM
if (strcmp(u->name, SYS_NAME)) {
pam_handle_t *pamh = NULL;
int pam_err;
struct pam_conv pamc = {
.conv = openpam_nullconv,
.appdata_ptr = NULL
};
Debug(DPROC, ("[%d] checking account with PAM\n", getpid()))
if (strcmp(u->name, usernm)) {
log_it(usernm, getpid(), "username ambiguity", u->name);
exit(ERROR_EXIT);
}
pam_err = pam_start("cron", usernm, &pamc, &pamh);
if (pam_err != PAM_SUCCESS) {
log_it("CRON", getpid(), "error", "can't start PAM");
exit(ERROR_EXIT);
}
pam_err = pam_acct_mgmt(pamh, PAM_SILENT);
if (pam_err != PAM_SUCCESS && pam_err != PAM_NEW_AUTHTOK_REQD) {
log_it(usernm, getpid(), "USER", "account unavailable");
exit(ERROR_EXIT);
}
pam_end(pamh, pam_err);
}
#endif
#ifdef USE_SIGCHLD
signal(SIGCHLD, SIG_DFL);
#else
signal(SIGCLD, SIG_DFL);
#endif
pipe(stdin_pipe);
pipe(stdout_pipe);
{
int escaped = FALSE;
int ch;
char *p;
for (input_data = p = e->cmd; (ch = *input_data);
input_data++, p++) {
if (p != input_data)
*p = ch;
if (escaped) {
if (ch == '%' || ch == '\\')
*--p = ch;
escaped = FALSE;
continue;
}
if (ch == '\\') {
escaped = TRUE;
continue;
}
if (ch == '%') {
*input_data++ = '\0';
break;
}
}
*p = '\0';
}
switch (vfork()) {
case -1:
log_it("CRON",getpid(),"error","can't vfork");
exit(ERROR_EXIT);
case 0:
Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
getpid()))
jitter = (e->uid == ROOT_UID) ? RootJitter : Jitter;
if (jitter != 0) {
srandom(getpid());
sleep(random() % jitter);
}
{
char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
log_it(usernm, getpid(), "CMD", x);
free(x);
}
#ifdef SYSLOG
closelog();
#endif
setsid();
close(stdin_pipe[WRITE_PIPE]);
close(stdout_pipe[READ_PIPE]);
close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN);
close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT);
close(STDERR); dup2(STDOUT, STDERR);
close(stdin_pipe[READ_PIPE]);
close(stdout_pipe[WRITE_PIPE]);
# if defined(LOGIN_CAP)
if ((pwd = getpwnam(usernm)) == NULL)
pwd = getpwuid(e->uid);
lc = NULL;
if (pwd != NULL) {
pwd->pw_gid = e->gid;
if (e->class != NULL)
lc = login_getclass(e->class);
}
if (pwd &&
setusercontext(lc, pwd, e->uid,
LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
endpwent();
else {
endpwent();
# endif
setgid(e->gid);
# if defined(BSD)
initgroups(usernm, e->gid);
# endif
setlogin(usernm);
setuid(e->uid);
#if defined(LOGIN_CAP)
}
if (lc != NULL)
login_close(lc);
#endif
chdir(env_get("HOME", e->envp));
{
char *shell = env_get("SHELL", e->envp);
# if DEBUGGING
if (DebugFlags & DTEST) {
fprintf(stderr,
"debug DTEST is on, not exec'ing command.\n");
fprintf(stderr,
"\tcmd='%s' shell='%s'\n", e->cmd, shell);
_exit(OK_EXIT);
}
# endif
execle(shell, shell, "-c", e->cmd, NULL, e->envp);
warn("execl: couldn't exec `%s'", shell);
_exit(ERROR_EXIT);
}
break;
default:
break;
}
children++;
Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
close(stdin_pipe[READ_PIPE]);
close(stdout_pipe[WRITE_PIPE]);
if (*input_data && fork() == 0) {
FILE *out;
int need_newline = FALSE;
int escaped = FALSE;
int ch;
out = fdopen(stdin_pipe[WRITE_PIPE], "w");
if (out == NULL) {
warn("fdopen failed in child2");
_exit(ERROR_EXIT);
}
Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
close(stdout_pipe[READ_PIPE]);
while ((ch = *input_data++)) {
if (escaped) {
if (ch != '%')
putc('\\', out);
} else {
if (ch == '%')
ch = '\n';
}
if (!(escaped = (ch == '\\'))) {
putc(ch, out);
need_newline = (ch != '\n');
}
}
if (escaped)
putc('\\', out);
if (need_newline)
putc('\n', out);
fclose(out);
Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
exit(0);
}
close(stdin_pipe[WRITE_PIPE]);
children++;
Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
{
FILE *in;
int ch;
in = fdopen(stdout_pipe[READ_PIPE], "r");
if (in == NULL) {
warn("fdopen failed in child");
_exit(ERROR_EXIT);
}
ch = getc(in);
if (ch != EOF) {
FILE *mail;
int bytes = 1;
int status = 0;
Debug(DPROC|DEXT,
("[%d] got data (%x:%c) from grandchild\n",
getpid(), ch, ch))
if (mailto) {
if (!*mailto) {
mailto = NULL;
}
} else {
mailto = usernm;
}
if (mailto) {
char **env;
char mailcmd[MAX_COMMAND];
char hostname[MAXHOSTNAMELEN];
gethostname(hostname, MAXHOSTNAMELEN);
snprintf(mailcmd, sizeof(mailcmd),
MAILARGS, MAILCMD);
if (!(mail = cron_popen(mailcmd, "w", e))) {
warn("%s", MAILCMD);
_exit(ERROR_EXIT);
}
fprintf(mail, "From: %s (Cron Daemon)\n", usernm);
fprintf(mail, "To: %s\n", mailto);
fprintf(mail, "Subject: Cron <%s@%s> %s\n",
usernm, first_word(hostname, "."),
e->cmd);
# if defined(MAIL_DATE)
fprintf(mail, "Date: %s\n",
arpadate(&TargetTime));
# endif
for (env = e->envp; *env; env++)
fprintf(mail, "X-Cron-Env: <%s>\n",
*env);
fprintf(mail, "\n");
putc(ch, mail);
}
while (EOF != (ch = getc(in))) {
bytes++;
if (mailto)
putc(ch, mail);
}
if (mailto) {
Debug(DPROC, ("[%d] closing pipe to mail\n",
getpid()))
status = cron_pclose(mail);
}
if (mailto && status) {
char buf[MAX_TEMPSTR];
snprintf(buf, sizeof(buf),
"mailed %d byte%s of output but got status 0x%04x\n",
bytes, (bytes==1)?"":"s",
status);
log_it(usernm, getpid(), "MAIL", buf);
}
}
Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
fclose(in);
}
for (; children > 0; children--)
{
WAIT_T waiter;
PID_T pid;
Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n",
getpid(), children))
pid = wait(&waiter);
if (pid < OK) {
Debug(DPROC, ("[%d] no more grandchildren--mail written?\n",
getpid()))
break;
}
Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x",
getpid(), pid, WEXITSTATUS(waiter)))
if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
Debug(DPROC, (", dumped core"))
Debug(DPROC, ("\n"))
}
}