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 117*d9850281SPedro F. Giffuni } 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 */ 164*d9850281SPedro F. Giffuni if (pipe(stdin_pipe) != 0 || pipe(stdout_pipe) != 0) { 165*d9850281SPedro F. Giffuni log_it("CRON", getpid(), "error", "can't pipe"); 166*d9850281SPedro F. Giffuni exit(ERROR_EXIT); 167*d9850281SPedro F. Giffuni } 16884f33deaSJordan K. Hubbard 16984f33deaSJordan K. Hubbard /* since we are a forked process, we can diddle the command string 17084f33deaSJordan K. Hubbard * we were passed -- nobody else is going to use it again, right? 17184f33deaSJordan K. Hubbard * 17284f33deaSJordan K. Hubbard * if a % is present in the command, previous characters are the 17384f33deaSJordan K. Hubbard * command, and subsequent characters are the additional input to 17484f33deaSJordan K. Hubbard * the command. Subsequent %'s will be transformed into newlines, 17584f33deaSJordan K. Hubbard * but that happens later. 17686ed6de3SJoerg Wunsch * 17786ed6de3SJoerg Wunsch * If there are escaped %'s, remove the escape character. 17884f33deaSJordan K. Hubbard */ 17984f33deaSJordan K. Hubbard /*local*/{ 18084f33deaSJordan K. Hubbard register int escaped = FALSE; 18184f33deaSJordan K. Hubbard register int ch; 18286ed6de3SJoerg Wunsch register char *p; 18384f33deaSJordan K. Hubbard 184401e6468SPhilippe Charnier for (input_data = p = e->cmd; (ch = *input_data); 18586ed6de3SJoerg Wunsch input_data++, p++) { 18686ed6de3SJoerg Wunsch if (p != input_data) 18786ed6de3SJoerg Wunsch *p = ch; 18884f33deaSJordan K. Hubbard if (escaped) { 18986ed6de3SJoerg Wunsch if (ch == '%' || ch == '\\') 19086ed6de3SJoerg Wunsch *--p = ch; 19184f33deaSJordan K. Hubbard escaped = FALSE; 19284f33deaSJordan K. Hubbard continue; 19384f33deaSJordan K. Hubbard } 19484f33deaSJordan K. Hubbard if (ch == '\\') { 19584f33deaSJordan K. Hubbard escaped = TRUE; 19684f33deaSJordan K. Hubbard continue; 19784f33deaSJordan K. Hubbard } 19884f33deaSJordan K. Hubbard if (ch == '%') { 19984f33deaSJordan K. Hubbard *input_data++ = '\0'; 20084f33deaSJordan K. Hubbard break; 20184f33deaSJordan K. Hubbard } 20284f33deaSJordan K. Hubbard } 20386ed6de3SJoerg Wunsch *p = '\0'; 20484f33deaSJordan K. Hubbard } 20584f33deaSJordan K. Hubbard 20684f33deaSJordan K. Hubbard /* fork again, this time so we can exec the user's command. 20784f33deaSJordan K. Hubbard */ 20884f33deaSJordan K. Hubbard switch (vfork()) { 20984f33deaSJordan K. Hubbard case -1: 21084f33deaSJordan K. Hubbard log_it("CRON",getpid(),"error","can't vfork"); 21184f33deaSJordan K. Hubbard exit(ERROR_EXIT); 21284f33deaSJordan K. Hubbard /*NOTREACHED*/ 21384f33deaSJordan K. Hubbard case 0: 21484f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n", 21584f33deaSJordan K. Hubbard getpid())) 21684f33deaSJordan K. Hubbard 217f5896bafSYaroslav Tykhiy if (e->uid == ROOT_UID) 218f5896bafSYaroslav Tykhiy Jitter = RootJitter; 219f5896bafSYaroslav Tykhiy if (Jitter != 0) { 220f5896bafSYaroslav Tykhiy srandom(getpid()); 221f5896bafSYaroslav Tykhiy sleep(random() % Jitter); 222f5896bafSYaroslav Tykhiy } 223f5896bafSYaroslav Tykhiy 22484f33deaSJordan K. Hubbard /* write a log message. we've waited this long to do it 22584f33deaSJordan K. Hubbard * because it was not until now that we knew the PID that 22684f33deaSJordan K. Hubbard * the actual user command shell was going to get and the 22784f33deaSJordan K. Hubbard * PID is part of the log message. 22884f33deaSJordan K. Hubbard */ 22984f33deaSJordan K. Hubbard /*local*/{ 23084f33deaSJordan K. Hubbard char *x = mkprints((u_char *)e->cmd, strlen(e->cmd)); 23184f33deaSJordan K. Hubbard 23284f33deaSJordan K. Hubbard log_it(usernm, getpid(), "CMD", x); 23384f33deaSJordan K. Hubbard free(x); 23484f33deaSJordan K. Hubbard } 23584f33deaSJordan K. Hubbard 23684f33deaSJordan K. Hubbard /* that's the last thing we'll log. close the log files. 23784f33deaSJordan K. Hubbard */ 23884f33deaSJordan K. Hubbard #ifdef SYSLOG 23984f33deaSJordan K. Hubbard closelog(); 24084f33deaSJordan K. Hubbard #endif 24184f33deaSJordan K. Hubbard 24284f33deaSJordan K. Hubbard /* get new pgrp, void tty, etc. 24384f33deaSJordan K. Hubbard */ 24484f33deaSJordan K. Hubbard (void) setsid(); 24584f33deaSJordan K. Hubbard 24684f33deaSJordan K. Hubbard /* close the pipe ends that we won't use. this doesn't affect 24784f33deaSJordan K. Hubbard * the parent, who has to read and write them; it keeps the 24884f33deaSJordan K. Hubbard * kernel from recording us as a potential client TWICE -- 24984f33deaSJordan K. Hubbard * which would keep it from sending SIGPIPE in otherwise 25084f33deaSJordan K. Hubbard * appropriate circumstances. 25184f33deaSJordan K. Hubbard */ 25284f33deaSJordan K. Hubbard close(stdin_pipe[WRITE_PIPE]); 25384f33deaSJordan K. Hubbard close(stdout_pipe[READ_PIPE]); 25484f33deaSJordan K. Hubbard 25584f33deaSJordan K. Hubbard /* grandchild process. make std{in,out} be the ends of 25684f33deaSJordan K. Hubbard * pipes opened by our daddy; make stderr go to stdout. 25784f33deaSJordan K. Hubbard */ 25884f33deaSJordan K. Hubbard close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN); 25984f33deaSJordan K. Hubbard close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT); 26084f33deaSJordan K. Hubbard close(STDERR); dup2(STDOUT, STDERR); 26184f33deaSJordan K. Hubbard 26284f33deaSJordan K. Hubbard /* close the pipes we just dup'ed. The resources will remain. 26384f33deaSJordan K. Hubbard */ 26484f33deaSJordan K. Hubbard close(stdin_pipe[READ_PIPE]); 26584f33deaSJordan K. Hubbard close(stdout_pipe[WRITE_PIPE]); 26684f33deaSJordan K. Hubbard 26784f33deaSJordan K. Hubbard /* set our login universe. Do this in the grandchild 26884f33deaSJordan K. Hubbard * so that the child can invoke /usr/lib/sendmail 26984f33deaSJordan K. Hubbard * without surprises. 27084f33deaSJordan K. Hubbard */ 27184f33deaSJordan K. Hubbard do_univ(u); 27284f33deaSJordan K. Hubbard 273b25b7bc1SDavid Nugent # if defined(LOGIN_CAP) 274b25b7bc1SDavid Nugent /* Set user's entire context, but skip the environment 275b25b7bc1SDavid Nugent * as cron provides a separate interface for this 276b25b7bc1SDavid Nugent */ 2770435c150SAndrey A. Chernov if ((pwd = getpwnam(usernm)) == NULL) 278c00d650fSPeter Wemm pwd = getpwuid(e->uid); 2790435c150SAndrey A. Chernov lc = NULL; 2800435c150SAndrey A. Chernov if (pwd != NULL) { 2810435c150SAndrey A. Chernov pwd->pw_gid = e->gid; 2820435c150SAndrey A. Chernov if (e->class != NULL) 2830435c150SAndrey A. Chernov lc = login_getclass(e->class); 2840435c150SAndrey A. Chernov } 2859efe2eccSPeter Wemm if (pwd && 2860435c150SAndrey A. Chernov setusercontext(lc, pwd, e->uid, 2879efe2eccSPeter Wemm LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0) 2889efe2eccSPeter Wemm (void) endpwent(); 2899efe2eccSPeter Wemm else { 290c00d650fSPeter Wemm /* fall back to the old method */ 2919efe2eccSPeter Wemm (void) endpwent(); 292c00d650fSPeter Wemm # endif 293c00d650fSPeter Wemm /* set our directory, uid and gid. Set gid first, 294708f27a1SMaxim Konovalov * since once we set uid, we've lost root privileges. 29584f33deaSJordan K. Hubbard */ 296bb0aa1a5SMaxim Konovalov if (setgid(e->gid) != 0) { 297bb0aa1a5SMaxim Konovalov log_it(usernm, getpid(), 298bb0aa1a5SMaxim Konovalov "error", "setgid failed"); 299bb0aa1a5SMaxim Konovalov exit(ERROR_EXIT); 300bb0aa1a5SMaxim Konovalov } 30184f33deaSJordan K. Hubbard # if defined(BSD) 302bb0aa1a5SMaxim Konovalov if (initgroups(usernm, e->gid) != 0) { 303bb0aa1a5SMaxim Konovalov log_it(usernm, getpid(), 304bb0aa1a5SMaxim Konovalov "error", "initgroups failed"); 305bb0aa1a5SMaxim Konovalov exit(ERROR_EXIT); 306bb0aa1a5SMaxim Konovalov } 30784f33deaSJordan K. Hubbard # endif 308bb0aa1a5SMaxim Konovalov if (setlogin(usernm) != 0) { 309bb0aa1a5SMaxim Konovalov log_it(usernm, getpid(), 310bb0aa1a5SMaxim Konovalov "error", "setlogin failed"); 311bb0aa1a5SMaxim Konovalov exit(ERROR_EXIT); 312bb0aa1a5SMaxim Konovalov } 313bb0aa1a5SMaxim Konovalov if (setuid(e->uid) != 0) { 314bb0aa1a5SMaxim Konovalov log_it(usernm, getpid(), 315bb0aa1a5SMaxim Konovalov "error", "setuid failed"); 316bb0aa1a5SMaxim Konovalov exit(ERROR_EXIT); 317bb0aa1a5SMaxim Konovalov } 318bb0aa1a5SMaxim Konovalov /* we aren't root after this..*/ 319c00d650fSPeter Wemm #if defined(LOGIN_CAP) 320c00d650fSPeter Wemm } 3213d99cebfSAndrey A. Chernov if (lc != NULL) 3223d99cebfSAndrey A. Chernov login_close(lc); 323b25b7bc1SDavid Nugent #endif 324bdddbd2fSPaul Traina chdir(env_get("HOME", e->envp)); 32584f33deaSJordan K. Hubbard 32684f33deaSJordan K. Hubbard /* exec the command. 32784f33deaSJordan K. Hubbard */ 32884f33deaSJordan K. Hubbard { 32984f33deaSJordan K. Hubbard char *shell = env_get("SHELL", e->envp); 33084f33deaSJordan K. Hubbard 33184f33deaSJordan K. Hubbard # if DEBUGGING 33284f33deaSJordan K. Hubbard if (DebugFlags & DTEST) { 33384f33deaSJordan K. Hubbard fprintf(stderr, 33484f33deaSJordan K. Hubbard "debug DTEST is on, not exec'ing command.\n"); 33584f33deaSJordan K. Hubbard fprintf(stderr, 33684f33deaSJordan K. Hubbard "\tcmd='%s' shell='%s'\n", e->cmd, shell); 33784f33deaSJordan K. Hubbard _exit(OK_EXIT); 33884f33deaSJordan K. Hubbard } 33984f33deaSJordan K. Hubbard # endif /*DEBUGGING*/ 34084f33deaSJordan K. Hubbard execle(shell, shell, "-c", e->cmd, (char *)0, e->envp); 341401e6468SPhilippe Charnier warn("execl: couldn't exec `%s'", shell); 34284f33deaSJordan K. Hubbard _exit(ERROR_EXIT); 34384f33deaSJordan K. Hubbard } 34484f33deaSJordan K. Hubbard break; 34584f33deaSJordan K. Hubbard default: 34684f33deaSJordan K. Hubbard /* parent process */ 34784f33deaSJordan K. Hubbard break; 34884f33deaSJordan K. Hubbard } 34984f33deaSJordan K. Hubbard 35084f33deaSJordan K. Hubbard children++; 35184f33deaSJordan K. Hubbard 35284f33deaSJordan K. Hubbard /* middle process, child of original cron, parent of process running 35384f33deaSJordan K. Hubbard * the user's command. 35484f33deaSJordan K. Hubbard */ 35584f33deaSJordan K. Hubbard 35684f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid())) 35784f33deaSJordan K. Hubbard 35884f33deaSJordan K. Hubbard /* close the ends of the pipe that will only be referenced in the 35984f33deaSJordan K. Hubbard * grandchild process... 36084f33deaSJordan K. Hubbard */ 36184f33deaSJordan K. Hubbard close(stdin_pipe[READ_PIPE]); 36284f33deaSJordan K. Hubbard close(stdout_pipe[WRITE_PIPE]); 36384f33deaSJordan K. Hubbard 36484f33deaSJordan K. Hubbard /* 36584f33deaSJordan K. Hubbard * write, to the pipe connected to child's stdin, any input specified 36684f33deaSJordan K. Hubbard * after a % in the crontab entry. while we copy, convert any 36784f33deaSJordan K. Hubbard * additional %'s to newlines. when done, if some characters were 36884f33deaSJordan K. Hubbard * written and the last one wasn't a newline, write a newline. 36984f33deaSJordan K. Hubbard * 37084f33deaSJordan K. Hubbard * Note that if the input data won't fit into one pipe buffer (2K 37184f33deaSJordan K. Hubbard * or 4K on most BSD systems), and the child doesn't read its stdin, 37284f33deaSJordan K. Hubbard * we would block here. thus we must fork again. 37384f33deaSJordan K. Hubbard */ 37484f33deaSJordan K. Hubbard 37584f33deaSJordan K. Hubbard if (*input_data && fork() == 0) { 37684f33deaSJordan K. Hubbard register FILE *out = fdopen(stdin_pipe[WRITE_PIPE], "w"); 37784f33deaSJordan K. Hubbard register int need_newline = FALSE; 37884f33deaSJordan K. Hubbard register int escaped = FALSE; 37984f33deaSJordan K. Hubbard register int ch; 38084f33deaSJordan K. Hubbard 38164ae78cbSGuy Helmer if (out == NULL) { 38264ae78cbSGuy Helmer warn("fdopen failed in child2"); 38364ae78cbSGuy Helmer _exit(ERROR_EXIT); 38464ae78cbSGuy Helmer } 38564ae78cbSGuy Helmer 38684f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid())) 38784f33deaSJordan K. Hubbard 38884f33deaSJordan K. Hubbard /* close the pipe we don't use, since we inherited it and 38984f33deaSJordan K. Hubbard * are part of its reference count now. 39084f33deaSJordan K. Hubbard */ 39184f33deaSJordan K. Hubbard close(stdout_pipe[READ_PIPE]); 39284f33deaSJordan K. Hubbard 39384f33deaSJordan K. Hubbard /* translation: 39484f33deaSJordan K. Hubbard * \% -> % 39584f33deaSJordan K. Hubbard * % -> \n 39684f33deaSJordan K. Hubbard * \x -> \x for all x != % 39784f33deaSJordan K. Hubbard */ 398401e6468SPhilippe Charnier while ((ch = *input_data++)) { 39984f33deaSJordan K. Hubbard if (escaped) { 40084f33deaSJordan K. Hubbard if (ch != '%') 40184f33deaSJordan K. Hubbard putc('\\', out); 40284f33deaSJordan K. Hubbard } else { 40384f33deaSJordan K. Hubbard if (ch == '%') 40484f33deaSJordan K. Hubbard ch = '\n'; 40584f33deaSJordan K. Hubbard } 40684f33deaSJordan K. Hubbard 40784f33deaSJordan K. Hubbard if (!(escaped = (ch == '\\'))) { 40884f33deaSJordan K. Hubbard putc(ch, out); 40984f33deaSJordan K. Hubbard need_newline = (ch != '\n'); 41084f33deaSJordan K. Hubbard } 41184f33deaSJordan K. Hubbard } 41284f33deaSJordan K. Hubbard if (escaped) 41384f33deaSJordan K. Hubbard putc('\\', out); 41484f33deaSJordan K. Hubbard if (need_newline) 41584f33deaSJordan K. Hubbard putc('\n', out); 41684f33deaSJordan K. Hubbard 41784f33deaSJordan K. Hubbard /* close the pipe, causing an EOF condition. fclose causes 41884f33deaSJordan K. Hubbard * stdin_pipe[WRITE_PIPE] to be closed, too. 41984f33deaSJordan K. Hubbard */ 42084f33deaSJordan K. Hubbard fclose(out); 42184f33deaSJordan K. Hubbard 42284f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid())) 42384f33deaSJordan K. Hubbard exit(0); 42484f33deaSJordan K. Hubbard } 42584f33deaSJordan K. Hubbard 42684f33deaSJordan K. Hubbard /* close the pipe to the grandkiddie's stdin, since its wicked uncle 42784f33deaSJordan K. Hubbard * ernie back there has it open and will close it when he's done. 42884f33deaSJordan K. Hubbard */ 42984f33deaSJordan K. Hubbard close(stdin_pipe[WRITE_PIPE]); 43084f33deaSJordan K. Hubbard 43184f33deaSJordan K. Hubbard children++; 43284f33deaSJordan K. Hubbard 43384f33deaSJordan K. Hubbard /* 43484f33deaSJordan K. Hubbard * read output from the grandchild. it's stderr has been redirected to 43584f33deaSJordan K. Hubbard * it's stdout, which has been redirected to our pipe. if there is any 43684f33deaSJordan K. Hubbard * output, we'll be mailing it to the user whose crontab this is... 43784f33deaSJordan K. Hubbard * when the grandchild exits, we'll get EOF. 43884f33deaSJordan K. Hubbard */ 43984f33deaSJordan K. Hubbard 44084f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid())) 44184f33deaSJordan K. Hubbard 44284f33deaSJordan K. Hubbard /*local*/{ 44384f33deaSJordan K. Hubbard register FILE *in = fdopen(stdout_pipe[READ_PIPE], "r"); 4449be756b5SMike Silbersack register int ch; 44584f33deaSJordan K. Hubbard 44664ae78cbSGuy Helmer if (in == NULL) { 44764ae78cbSGuy Helmer warn("fdopen failed in child"); 44864ae78cbSGuy Helmer _exit(ERROR_EXIT); 44964ae78cbSGuy Helmer } 45064ae78cbSGuy Helmer 4519be756b5SMike Silbersack ch = getc(in); 45284f33deaSJordan K. Hubbard if (ch != EOF) { 45384f33deaSJordan K. Hubbard register FILE *mail; 45484f33deaSJordan K. Hubbard register int bytes = 1; 45584f33deaSJordan K. Hubbard int status = 0; 45684f33deaSJordan K. Hubbard 45784f33deaSJordan K. Hubbard Debug(DPROC|DEXT, 45884f33deaSJordan K. Hubbard ("[%d] got data (%x:%c) from grandchild\n", 45984f33deaSJordan K. Hubbard getpid(), ch, ch)) 46084f33deaSJordan K. Hubbard 46184f33deaSJordan K. Hubbard /* get name of recipient. this is MAILTO if set to a 46284f33deaSJordan K. Hubbard * valid local username; USER otherwise. 46384f33deaSJordan K. Hubbard */ 464b75634d2SDmitry Morozovsky if (mailto == NULL) { 465b75634d2SDmitry Morozovsky /* MAILTO not present, set to USER, 466b75634d2SDmitry Morozovsky * unless globally overriden. 46784f33deaSJordan K. Hubbard */ 468b75634d2SDmitry Morozovsky if (defmailto) 469b75634d2SDmitry Morozovsky mailto = defmailto; 470b75634d2SDmitry Morozovsky else 47184f33deaSJordan K. Hubbard mailto = usernm; 47284f33deaSJordan K. Hubbard } 4731168e5f1SDmitry Morozovsky if (mailto && *mailto == '\0') 4741168e5f1SDmitry Morozovsky mailto = NULL; 47584f33deaSJordan K. Hubbard 47684f33deaSJordan K. Hubbard /* if we are supposed to be mailing, MAILTO will 47784f33deaSJordan K. Hubbard * be non-NULL. only in this case should we set 47884f33deaSJordan K. Hubbard * up the mail command and subjects and stuff... 47984f33deaSJordan K. Hubbard */ 48084f33deaSJordan K. Hubbard 4811168e5f1SDmitry Morozovsky if (mailto) { 48284f33deaSJordan K. Hubbard register char **env; 48384f33deaSJordan K. Hubbard auto char mailcmd[MAX_COMMAND]; 48484f33deaSJordan K. Hubbard auto char hostname[MAXHOSTNAMELEN]; 48584f33deaSJordan K. Hubbard 486713c03d5SPeter Wemm if (gethostname(hostname, MAXHOSTNAMELEN) == -1) 487713c03d5SPeter Wemm hostname[0] = '\0'; 488713c03d5SPeter Wemm hostname[sizeof(hostname) - 1] = '\0'; 489bdddbd2fSPaul Traina (void) snprintf(mailcmd, sizeof(mailcmd), 490bdddbd2fSPaul Traina MAILARGS, MAILCMD); 491fe46d6a8SAndrey A. Chernov if (!(mail = cron_popen(mailcmd, "w", e))) { 492401e6468SPhilippe Charnier warn("%s", MAILCMD); 49384f33deaSJordan K. Hubbard (void) _exit(ERROR_EXIT); 49484f33deaSJordan K. Hubbard } 495713c03d5SPeter Wemm fprintf(mail, "From: Cron Daemon <%s@%s>\n", 496713c03d5SPeter Wemm usernm, hostname); 49784f33deaSJordan K. Hubbard fprintf(mail, "To: %s\n", mailto); 49884f33deaSJordan K. Hubbard fprintf(mail, "Subject: Cron <%s@%s> %s\n", 49984f33deaSJordan K. Hubbard usernm, first_word(hostname, "."), 50084f33deaSJordan K. Hubbard e->cmd); 50184f33deaSJordan K. Hubbard # if defined(MAIL_DATE) 50284f33deaSJordan K. Hubbard fprintf(mail, "Date: %s\n", 50384f33deaSJordan K. Hubbard arpadate(&TargetTime)); 50484f33deaSJordan K. Hubbard # endif /* MAIL_DATE */ 50584f33deaSJordan K. Hubbard for (env = e->envp; *env; env++) 50684f33deaSJordan K. Hubbard fprintf(mail, "X-Cron-Env: <%s>\n", 50784f33deaSJordan K. Hubbard *env); 50884f33deaSJordan K. Hubbard fprintf(mail, "\n"); 50984f33deaSJordan K. Hubbard 51084f33deaSJordan K. Hubbard /* this was the first char from the pipe 51184f33deaSJordan K. Hubbard */ 51284f33deaSJordan K. Hubbard putc(ch, mail); 51384f33deaSJordan K. Hubbard } 51484f33deaSJordan K. Hubbard 51584f33deaSJordan K. Hubbard /* we have to read the input pipe no matter whether 51684f33deaSJordan K. Hubbard * we mail or not, but obviously we only write to 51784f33deaSJordan K. Hubbard * mail pipe if we ARE mailing. 51884f33deaSJordan K. Hubbard */ 51984f33deaSJordan K. Hubbard 52084f33deaSJordan K. Hubbard while (EOF != (ch = getc(in))) { 52184f33deaSJordan K. Hubbard bytes++; 52284f33deaSJordan K. Hubbard if (mailto) 52384f33deaSJordan K. Hubbard putc(ch, mail); 52484f33deaSJordan K. Hubbard } 52584f33deaSJordan K. Hubbard 52684f33deaSJordan K. Hubbard /* only close pipe if we opened it -- i.e., we're 52784f33deaSJordan K. Hubbard * mailing... 52884f33deaSJordan K. Hubbard */ 52984f33deaSJordan K. Hubbard 53084f33deaSJordan K. Hubbard if (mailto) { 53184f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] closing pipe to mail\n", 53284f33deaSJordan K. Hubbard getpid())) 53384f33deaSJordan K. Hubbard /* Note: the pclose will probably see 53484f33deaSJordan K. Hubbard * the termination of the grandchild 53584f33deaSJordan K. Hubbard * in addition to the mail process, since 53684f33deaSJordan K. Hubbard * it (the grandchild) is likely to exit 53784f33deaSJordan K. Hubbard * after closing its stdout. 53884f33deaSJordan K. Hubbard */ 53984f33deaSJordan K. Hubbard status = cron_pclose(mail); 54084f33deaSJordan K. Hubbard } 54184f33deaSJordan K. Hubbard 54284f33deaSJordan K. Hubbard /* if there was output and we could not mail it, 54384f33deaSJordan K. Hubbard * log the facts so the poor user can figure out 54484f33deaSJordan K. Hubbard * what's going on. 54584f33deaSJordan K. Hubbard */ 54684f33deaSJordan K. Hubbard if (mailto && status) { 54784f33deaSJordan K. Hubbard char buf[MAX_TEMPSTR]; 54884f33deaSJordan K. Hubbard 549bdddbd2fSPaul Traina snprintf(buf, sizeof(buf), 55084f33deaSJordan K. Hubbard "mailed %d byte%s of output but got status 0x%04x\n", 55184f33deaSJordan K. Hubbard bytes, (bytes==1)?"":"s", 55284f33deaSJordan K. Hubbard status); 55384f33deaSJordan K. Hubbard log_it(usernm, getpid(), "MAIL", buf); 55484f33deaSJordan K. Hubbard } 55584f33deaSJordan K. Hubbard 55684f33deaSJordan K. Hubbard } /*if data from grandchild*/ 55784f33deaSJordan K. Hubbard 55884f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid())) 55984f33deaSJordan K. Hubbard 56084f33deaSJordan K. Hubbard fclose(in); /* also closes stdout_pipe[READ_PIPE] */ 56184f33deaSJordan K. Hubbard } 56284f33deaSJordan K. Hubbard 56384f33deaSJordan K. Hubbard /* wait for children to die. 56484f33deaSJordan K. Hubbard */ 56584f33deaSJordan K. Hubbard for (; children > 0; children--) 56684f33deaSJordan K. Hubbard { 56784f33deaSJordan K. Hubbard WAIT_T waiter; 56884f33deaSJordan K. Hubbard PID_T pid; 56984f33deaSJordan K. Hubbard 57084f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n", 57184f33deaSJordan K. Hubbard getpid(), children)) 57284f33deaSJordan K. Hubbard pid = wait(&waiter); 57384f33deaSJordan K. Hubbard if (pid < OK) { 57484f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] no more grandchildren--mail written?\n", 57584f33deaSJordan K. Hubbard getpid())) 57684f33deaSJordan K. Hubbard break; 57784f33deaSJordan K. Hubbard } 57884f33deaSJordan K. Hubbard Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x", 57984f33deaSJordan K. Hubbard getpid(), pid, WEXITSTATUS(waiter))) 58084f33deaSJordan K. Hubbard if (WIFSIGNALED(waiter) && WCOREDUMP(waiter)) 58184f33deaSJordan K. Hubbard Debug(DPROC, (", dumped core")) 58284f33deaSJordan K. Hubbard Debug(DPROC, ("\n")) 58384f33deaSJordan K. Hubbard } 58484f33deaSJordan K. Hubbard } 58584f33deaSJordan K. Hubbard 58684f33deaSJordan K. Hubbard 58784f33deaSJordan K. Hubbard static void 58884f33deaSJordan K. Hubbard do_univ(u) 58984f33deaSJordan K. Hubbard user *u; 59084f33deaSJordan K. Hubbard { 59184f33deaSJordan K. Hubbard #if defined(sequent) 59284f33deaSJordan K. Hubbard /* Dynix (Sequent) hack to put the user associated with 59384f33deaSJordan K. Hubbard * the passed user structure into the ATT universe if 59484f33deaSJordan K. Hubbard * necessary. We have to dig the gecos info out of 59584f33deaSJordan K. Hubbard * the user's password entry to see if the magic 59684f33deaSJordan K. Hubbard * "universe(att)" string is present. 59784f33deaSJordan K. Hubbard */ 59884f33deaSJordan K. Hubbard 59984f33deaSJordan K. Hubbard struct passwd *p; 60084f33deaSJordan K. Hubbard char *s; 60184f33deaSJordan K. Hubbard int i; 60284f33deaSJordan K. Hubbard 60384f33deaSJordan K. Hubbard p = getpwuid(u->uid); 60484f33deaSJordan K. Hubbard (void) endpwent(); 60584f33deaSJordan K. Hubbard 60684f33deaSJordan K. Hubbard if (p == NULL) 60784f33deaSJordan K. Hubbard return; 60884f33deaSJordan K. Hubbard 60984f33deaSJordan K. Hubbard s = p->pw_gecos; 61084f33deaSJordan K. Hubbard 61184f33deaSJordan K. Hubbard for (i = 0; i < 4; i++) 61284f33deaSJordan K. Hubbard { 61384f33deaSJordan K. Hubbard if ((s = strchr(s, ',')) == NULL) 61484f33deaSJordan K. Hubbard return; 61584f33deaSJordan K. Hubbard s++; 61684f33deaSJordan K. Hubbard } 61784f33deaSJordan K. Hubbard if (strcmp(s, "universe(att)")) 61884f33deaSJordan K. Hubbard return; 61984f33deaSJordan K. Hubbard 62084f33deaSJordan K. Hubbard (void) universe(U_ATT); 62184f33deaSJordan K. Hubbard #endif 62284f33deaSJordan K. Hubbard } 623