1*cce75673Smillert /* $OpenBSD: do_command.c,v 1.25 2003/07/30 20:20:01 millert Exp $ */ 2e134e629Smillert 3df930be7Sderaadt /* Copyright 1988,1990,1993,1994 by Paul Vixie 4df930be7Sderaadt * All rights reserved 5f454ebdeSmillert */ 6f454ebdeSmillert 7f454ebdeSmillert /* 8f454ebdeSmillert * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. 9df930be7Sderaadt * 10f454ebdeSmillert * Permission to use, copy, modify, and distribute this software for any 11f454ebdeSmillert * purpose with or without fee is hereby granted, provided that the above 12f454ebdeSmillert * copyright notice and this permission notice appear in all copies. 13df930be7Sderaadt * 14f454ebdeSmillert * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 15f454ebdeSmillert * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 16f454ebdeSmillert * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 17f454ebdeSmillert * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 18f454ebdeSmillert * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 19f454ebdeSmillert * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 20f454ebdeSmillert * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 21f454ebdeSmillert * SOFTWARE. 22df930be7Sderaadt */ 23df930be7Sderaadt 24df930be7Sderaadt #if !defined(lint) && !defined(LINT) 25*cce75673Smillert static char const rcsid[] = "$OpenBSD: do_command.c,v 1.25 2003/07/30 20:20:01 millert Exp $"; 26df930be7Sderaadt #endif 27df930be7Sderaadt 28df930be7Sderaadt #include "cron.h" 29df930be7Sderaadt 30f454ebdeSmillert static void child_process(entry *, user *); 31df930be7Sderaadt 32df930be7Sderaadt void 33f454ebdeSmillert do_command(entry *e, user *u) { 34b0ca7b2aSderaadt Debug(DPROC, ("[%ld] do_command(%s, (%s,%lu,%lu))\n", 35f454ebdeSmillert (long)getpid(), e->cmd, u->name, 362b139a93Smillert (u_long)e->pwd->pw_uid, (u_long)e->pwd->pw_gid)) 37df930be7Sderaadt 38df930be7Sderaadt /* fork to become asynchronous -- parent process is done immediately, 39df930be7Sderaadt * and continues to run the normal cron code, which means return to 40df930be7Sderaadt * tick(). the child and grandchild don't leave this function, alive. 41df930be7Sderaadt * 42df930be7Sderaadt * vfork() is unsuitable, since we have much to do, and the parent 43df930be7Sderaadt * needs to be able to run off and fork other processes. 44df930be7Sderaadt */ 45df930be7Sderaadt switch (fork()) { 46df930be7Sderaadt case -1: 47df930be7Sderaadt log_it("CRON", getpid(), "error", "can't fork"); 48df930be7Sderaadt break; 49df930be7Sderaadt case 0: 50df930be7Sderaadt /* child process */ 51df930be7Sderaadt acquire_daemonlock(1); 52df930be7Sderaadt child_process(e, u); 53f454ebdeSmillert Debug(DPROC, ("[%ld] child process done, exiting\n", 54f454ebdeSmillert (long)getpid())) 55df930be7Sderaadt _exit(OK_EXIT); 56df930be7Sderaadt break; 57df930be7Sderaadt default: 58df930be7Sderaadt /* parent process */ 59df930be7Sderaadt break; 60df930be7Sderaadt } 61f454ebdeSmillert Debug(DPROC, ("[%ld] main process returning to work\n",(long)getpid())) 62df930be7Sderaadt } 63df930be7Sderaadt 64df930be7Sderaadt static void 65f454ebdeSmillert child_process(entry *e, user *u) { 66df930be7Sderaadt int stdin_pipe[2], stdout_pipe[2]; 67e134e629Smillert char *input_data, *usernm, *mailto; 68d5919ef3Smillert int children = 0; 69df930be7Sderaadt 70f454ebdeSmillert Debug(DPROC, ("[%ld] child_process('%s')\n", (long)getpid(), e->cmd)) 719f52c269Sderaadt 720aec96c6Smillert /* mark ourselves as different to PS command watchers */ 730aec96c6Smillert setproctitle("running job"); 74df930be7Sderaadt 75df930be7Sderaadt /* discover some useful and important environment settings 76df930be7Sderaadt */ 772b139a93Smillert usernm = e->pwd->pw_name; 78df930be7Sderaadt mailto = env_get("MAILTO", e->envp); 79df930be7Sderaadt 80df930be7Sderaadt /* our parent is watching for our death by catching SIGCHLD. we 81df930be7Sderaadt * do not care to watch for our children's deaths this way -- we 82e4d25771Stodd * use wait() explicitly. so we have to reset the signal (which 83df930be7Sderaadt * was inherited from the parent). 84df930be7Sderaadt */ 855815f23fSmillert (void) signal(SIGCHLD, SIG_DFL); 86df930be7Sderaadt 87df930be7Sderaadt /* create some pipes to talk to our future child 88df930be7Sderaadt */ 89df930be7Sderaadt pipe(stdin_pipe); /* child's stdin */ 90df930be7Sderaadt pipe(stdout_pipe); /* child's stdout */ 91df930be7Sderaadt 92df930be7Sderaadt /* since we are a forked process, we can diddle the command string 93df930be7Sderaadt * we were passed -- nobody else is going to use it again, right? 94df930be7Sderaadt * 95df930be7Sderaadt * if a % is present in the command, previous characters are the 96df930be7Sderaadt * command, and subsequent characters are the additional input to 972b139a93Smillert * the command. An escaped % will have the escape character stripped 982b139a93Smillert * from it. Subsequent %'s will be transformed into newlines, 99df930be7Sderaadt * but that happens later. 100df930be7Sderaadt */ 101df930be7Sderaadt /*local*/{ 102f454ebdeSmillert int escaped = FALSE; 103f454ebdeSmillert int ch; 104f454ebdeSmillert char *p; 105df930be7Sderaadt 106f454ebdeSmillert for (input_data = p = e->cmd; 107f454ebdeSmillert (ch = *input_data) != '\0'; 108d94ebf94Sderaadt input_data++, p++) { 109d94ebf94Sderaadt if (p != input_data) 110d94ebf94Sderaadt *p = ch; 111df930be7Sderaadt if (escaped) { 11294cf10eeSmillert if (ch == '%') 113d94ebf94Sderaadt *--p = ch; 114df930be7Sderaadt escaped = FALSE; 115df930be7Sderaadt continue; 116df930be7Sderaadt } 117df930be7Sderaadt if (ch == '\\') { 118df930be7Sderaadt escaped = TRUE; 119df930be7Sderaadt continue; 120df930be7Sderaadt } 121df930be7Sderaadt if (ch == '%') { 122df930be7Sderaadt *input_data++ = '\0'; 123df930be7Sderaadt break; 124df930be7Sderaadt } 125df930be7Sderaadt } 126d94ebf94Sderaadt *p = '\0'; 127df930be7Sderaadt } 128df930be7Sderaadt 129df930be7Sderaadt /* fork again, this time so we can exec the user's command. 130df930be7Sderaadt */ 13114852038Smillert switch (fork()) { 132df930be7Sderaadt case -1: 1332b139a93Smillert log_it("CRON", getpid(), "error", "can't fork"); 134df930be7Sderaadt exit(ERROR_EXIT); 135df930be7Sderaadt /*NOTREACHED*/ 136df930be7Sderaadt case 0: 1372b139a93Smillert Debug(DPROC, ("[%ld] grandchild process fork()'ed\n", 138f454ebdeSmillert (long)getpid())) 139df930be7Sderaadt 140df930be7Sderaadt /* write a log message. we've waited this long to do it 141df930be7Sderaadt * because it was not until now that we knew the PID that 142df930be7Sderaadt * the actual user command shell was going to get and the 143df930be7Sderaadt * PID is part of the log message. 144df930be7Sderaadt */ 145f454ebdeSmillert if ((e->flags & DONT_LOG) == 0) { 146df930be7Sderaadt char *x = mkprints((u_char *)e->cmd, strlen(e->cmd)); 147df930be7Sderaadt 148df930be7Sderaadt log_it(usernm, getpid(), "CMD", x); 149df930be7Sderaadt free(x); 150df930be7Sderaadt } 151df930be7Sderaadt 152df930be7Sderaadt /* that's the last thing we'll log. close the log files. 153df930be7Sderaadt */ 154d8a30772Smillert log_close(); 155df930be7Sderaadt 156df930be7Sderaadt /* get new pgrp, void tty, etc. 157df930be7Sderaadt */ 158df930be7Sderaadt (void) setsid(); 159df930be7Sderaadt 160df930be7Sderaadt /* close the pipe ends that we won't use. this doesn't affect 161df930be7Sderaadt * the parent, who has to read and write them; it keeps the 162df930be7Sderaadt * kernel from recording us as a potential client TWICE -- 163df930be7Sderaadt * which would keep it from sending SIGPIPE in otherwise 164df930be7Sderaadt * appropriate circumstances. 165df930be7Sderaadt */ 166df930be7Sderaadt close(stdin_pipe[WRITE_PIPE]); 167df930be7Sderaadt close(stdout_pipe[READ_PIPE]); 168df930be7Sderaadt 169df930be7Sderaadt /* grandchild process. make std{in,out} be the ends of 170df930be7Sderaadt * pipes opened by our daddy; make stderr go to stdout. 171df930be7Sderaadt */ 17214852038Smillert if (stdin_pipe[READ_PIPE] != STDIN) { 17314852038Smillert dup2(stdin_pipe[READ_PIPE], STDIN); 174df930be7Sderaadt close(stdin_pipe[READ_PIPE]); 17514852038Smillert } 17614852038Smillert if (stdout_pipe[WRITE_PIPE] != STDOUT) { 17714852038Smillert dup2(stdout_pipe[WRITE_PIPE], STDOUT); 178a0f600ecSmillert close(stdout_pipe[WRITE_PIPE]); 17914852038Smillert } 18014852038Smillert dup2(STDOUT, STDERR); 181df930be7Sderaadt 182df930be7Sderaadt /* set our directory, uid and gid. Set gid first, since once 183df930be7Sderaadt * we set uid, we've lost root privledges. 184df930be7Sderaadt */ 185fed231abSmillert #ifdef LOGIN_CAP 186fed231abSmillert { 187b746b40dSmillert #ifdef BSD_AUTH 188b746b40dSmillert auth_session_t *as; 189b746b40dSmillert #endif 1902b139a93Smillert login_cap_t *lc; 1912b139a93Smillert char **p; 1922b139a93Smillert extern char **environ; 193fed231abSmillert 194fed231abSmillert /* XXX - should just pass in a login_cap_t * */ 1952b139a93Smillert if ((lc = login_getclass(e->pwd->pw_class)) == NULL) { 1962b139a93Smillert fprintf(stderr, 1972b139a93Smillert "unable to get login class for %s\n", 1982b139a93Smillert e->pwd->pw_name); 199fed231abSmillert _exit(ERROR_EXIT); 200fed231abSmillert } 2012b139a93Smillert if (setusercontext(lc, e->pwd, e->pwd->pw_uid, LOGIN_SETALL) < 0) { 202e134e629Smillert fprintf(stderr, 203e134e629Smillert "setusercontext failed for %s\n", 204e134e629Smillert e->pwd->pw_name); 205fed231abSmillert _exit(ERROR_EXIT); 206fed231abSmillert } 207f454ebdeSmillert #ifdef BSD_AUTH 208b746b40dSmillert as = auth_open(); 209b746b40dSmillert if (as == NULL || auth_setpwd(as, e->pwd) != 0) { 210b746b40dSmillert fprintf(stderr, "can't malloc\n"); 211b746b40dSmillert _exit(ERROR_EXIT); 212b746b40dSmillert } 213b746b40dSmillert if (auth_approval(as, lc, usernm, "cron") <= 0) { 2142b139a93Smillert fprintf(stderr, "approval failed for %s\n", 2152b139a93Smillert e->pwd->pw_name); 216f454ebdeSmillert _exit(ERROR_EXIT); 217f454ebdeSmillert } 218b746b40dSmillert auth_close(as); 219f454ebdeSmillert #endif /* BSD_AUTH */ 2202b139a93Smillert login_close(lc); 2212b139a93Smillert 222fed231abSmillert /* If no PATH specified in crontab file but 223f454ebdeSmillert * we just added one via login.conf, add it to 224fed231abSmillert * the crontab environment. 225fed231abSmillert */ 2262b139a93Smillert if (env_get("PATH", e->envp) == NULL && environ != NULL) { 2272b139a93Smillert for (p = environ; *p; p++) { 2282b139a93Smillert if (strncmp(*p, "PATH=", 5) == 0) { 2292b139a93Smillert e->envp = env_set(e->envp, *p); 2302b139a93Smillert break; 231fed231abSmillert } 232fed231abSmillert } 2332b139a93Smillert } 234f454ebdeSmillert } 235fed231abSmillert #else 2362b139a93Smillert setgid(e->pwd->pw_gid); 2372b139a93Smillert initgroups(usernm, e->pwd->pw_gid); 238e134e629Smillert #if (defined(BSD)) && (BSD >= 199103) 239bd5a2f8eSderaadt setlogin(usernm); 240e134e629Smillert #endif /* BSD */ 241a47aac79Smillert setuid(e->pwd->pw_uid); /* we aren't root after this... */ 242f454ebdeSmillert 243f454ebdeSmillert #endif /* LOGIN_CAP */ 244df930be7Sderaadt chdir(env_get("HOME", e->envp)); 245df930be7Sderaadt 246f454ebdeSmillert /* 247f454ebdeSmillert * Exec the command. 248df930be7Sderaadt */ 249df930be7Sderaadt { 250df930be7Sderaadt char *shell = env_get("SHELL", e->envp); 251df930be7Sderaadt 252df930be7Sderaadt # if DEBUGGING 253df930be7Sderaadt if (DebugFlags & DTEST) { 254df930be7Sderaadt fprintf(stderr, 255df930be7Sderaadt "debug DTEST is on, not exec'ing command.\n"); 256df930be7Sderaadt fprintf(stderr, 257df930be7Sderaadt "\tcmd='%s' shell='%s'\n", e->cmd, shell); 258df930be7Sderaadt _exit(OK_EXIT); 259df930be7Sderaadt } 260df930be7Sderaadt # endif /*DEBUGGING*/ 261*cce75673Smillert execle(shell, shell, "-c", e->cmd, (char *)NULL, e->envp); 262*cce75673Smillert fprintf(stderr, "execle: couldn't exec `%s'\n", shell); 263*cce75673Smillert perror("execle"); 264df930be7Sderaadt _exit(ERROR_EXIT); 265df930be7Sderaadt } 266df930be7Sderaadt break; 267df930be7Sderaadt default: 268df930be7Sderaadt /* parent process */ 269df930be7Sderaadt break; 270df930be7Sderaadt } 271df930be7Sderaadt 272df930be7Sderaadt children++; 273df930be7Sderaadt 274df930be7Sderaadt /* middle process, child of original cron, parent of process running 275df930be7Sderaadt * the user's command. 276df930be7Sderaadt */ 277df930be7Sderaadt 278f454ebdeSmillert Debug(DPROC, ("[%ld] child continues, closing pipes\n",(long)getpid())) 279df930be7Sderaadt 280df930be7Sderaadt /* close the ends of the pipe that will only be referenced in the 281df930be7Sderaadt * grandchild process... 282df930be7Sderaadt */ 283df930be7Sderaadt close(stdin_pipe[READ_PIPE]); 284df930be7Sderaadt close(stdout_pipe[WRITE_PIPE]); 285df930be7Sderaadt 286df930be7Sderaadt /* 287df930be7Sderaadt * write, to the pipe connected to child's stdin, any input specified 288df930be7Sderaadt * after a % in the crontab entry. while we copy, convert any 289df930be7Sderaadt * additional %'s to newlines. when done, if some characters were 290df930be7Sderaadt * written and the last one wasn't a newline, write a newline. 291df930be7Sderaadt * 292df930be7Sderaadt * Note that if the input data won't fit into one pipe buffer (2K 293df930be7Sderaadt * or 4K on most BSD systems), and the child doesn't read its stdin, 294df930be7Sderaadt * we would block here. thus we must fork again. 295df930be7Sderaadt */ 296df930be7Sderaadt 297df930be7Sderaadt if (*input_data && fork() == 0) { 298f454ebdeSmillert FILE *out = fdopen(stdin_pipe[WRITE_PIPE], "w"); 299f454ebdeSmillert int need_newline = FALSE; 300f454ebdeSmillert int escaped = FALSE; 301f454ebdeSmillert int ch; 302df930be7Sderaadt 303f454ebdeSmillert Debug(DPROC, ("[%ld] child2 sending data to grandchild\n", 304f454ebdeSmillert (long)getpid())) 305df930be7Sderaadt 306df930be7Sderaadt /* close the pipe we don't use, since we inherited it and 307df930be7Sderaadt * are part of its reference count now. 308df930be7Sderaadt */ 309df930be7Sderaadt close(stdout_pipe[READ_PIPE]); 310df930be7Sderaadt 311df930be7Sderaadt /* translation: 312df930be7Sderaadt * \% -> % 313df930be7Sderaadt * % -> \n 314df930be7Sderaadt * \x -> \x for all x != % 315df930be7Sderaadt */ 3169f52c269Sderaadt while ((ch = *input_data++) != '\0') { 317df930be7Sderaadt if (escaped) { 318df930be7Sderaadt if (ch != '%') 319df930be7Sderaadt putc('\\', out); 320df930be7Sderaadt } else { 321df930be7Sderaadt if (ch == '%') 322df930be7Sderaadt ch = '\n'; 323df930be7Sderaadt } 324df930be7Sderaadt 325df930be7Sderaadt if (!(escaped = (ch == '\\'))) { 326df930be7Sderaadt putc(ch, out); 327df930be7Sderaadt need_newline = (ch != '\n'); 328df930be7Sderaadt } 329df930be7Sderaadt } 330df930be7Sderaadt if (escaped) 331df930be7Sderaadt putc('\\', out); 332df930be7Sderaadt if (need_newline) 333df930be7Sderaadt putc('\n', out); 334df930be7Sderaadt 335df930be7Sderaadt /* close the pipe, causing an EOF condition. fclose causes 336df930be7Sderaadt * stdin_pipe[WRITE_PIPE] to be closed, too. 337df930be7Sderaadt */ 338df930be7Sderaadt fclose(out); 339df930be7Sderaadt 340f454ebdeSmillert Debug(DPROC, ("[%ld] child2 done sending to grandchild\n", 341f454ebdeSmillert (long)getpid())) 342df930be7Sderaadt exit(0); 343df930be7Sderaadt } 344df930be7Sderaadt 345df930be7Sderaadt /* close the pipe to the grandkiddie's stdin, since its wicked uncle 346df930be7Sderaadt * ernie back there has it open and will close it when he's done. 347df930be7Sderaadt */ 348df930be7Sderaadt close(stdin_pipe[WRITE_PIPE]); 349df930be7Sderaadt 350df930be7Sderaadt children++; 351df930be7Sderaadt 352df930be7Sderaadt /* 353df930be7Sderaadt * read output from the grandchild. it's stderr has been redirected to 354df930be7Sderaadt * it's stdout, which has been redirected to our pipe. if there is any 355df930be7Sderaadt * output, we'll be mailing it to the user whose crontab this is... 356df930be7Sderaadt * when the grandchild exits, we'll get EOF. 357df930be7Sderaadt */ 358df930be7Sderaadt 359f454ebdeSmillert Debug(DPROC, ("[%ld] child reading output from grandchild\n", 360f454ebdeSmillert (long)getpid())) 361df930be7Sderaadt 362df930be7Sderaadt /*local*/{ 363f454ebdeSmillert FILE *in = fdopen(stdout_pipe[READ_PIPE], "r"); 364f454ebdeSmillert int ch = getc(in); 365df930be7Sderaadt 366df930be7Sderaadt if (ch != EOF) { 367d5919ef3Smillert FILE *mail; 368f454ebdeSmillert int bytes = 1; 369df930be7Sderaadt int status = 0; 370df930be7Sderaadt 371df930be7Sderaadt Debug(DPROC|DEXT, 372f454ebdeSmillert ("[%ld] got data (%x:%c) from grandchild\n", 373f454ebdeSmillert (long)getpid(), ch, ch)) 374df930be7Sderaadt 375df930be7Sderaadt /* get name of recipient. this is MAILTO if set to a 376df930be7Sderaadt * valid local username; USER otherwise. 377df930be7Sderaadt */ 378e134e629Smillert if (mailto) { 379df930be7Sderaadt /* MAILTO was present in the environment 380df930be7Sderaadt */ 381df930be7Sderaadt if (!*mailto) { 382df930be7Sderaadt /* ... but it's empty. set to NULL 383df930be7Sderaadt */ 384df930be7Sderaadt mailto = NULL; 385df930be7Sderaadt } 386df930be7Sderaadt } else { 387df930be7Sderaadt /* MAILTO not present, set to USER. 388df930be7Sderaadt */ 389df930be7Sderaadt mailto = usernm; 390df930be7Sderaadt } 391df930be7Sderaadt 392df930be7Sderaadt /* if we are supposed to be mailing, MAILTO will 393df930be7Sderaadt * be non-NULL. only in this case should we set 394df930be7Sderaadt * up the mail command and subjects and stuff... 395df930be7Sderaadt */ 396df930be7Sderaadt 397e134e629Smillert if (mailto && safe_p(usernm, mailto)) { 398f454ebdeSmillert char **env; 399f454ebdeSmillert char mailcmd[MAX_COMMAND]; 400f454ebdeSmillert char hostname[MAXHOSTNAMELEN]; 401df930be7Sderaadt 4024af80cabSmpech gethostname(hostname, sizeof(hostname)); 403f454ebdeSmillert if (snprintf(mailcmd, sizeof mailcmd, MAILFMT, 404f454ebdeSmillert MAILARG) >= sizeof mailcmd) { 405f454ebdeSmillert fprintf(stderr, "mailcmd too long\n"); 406f454ebdeSmillert (void) _exit(ERROR_EXIT); 407f454ebdeSmillert } 4082b139a93Smillert if (!(mail = cron_popen(mailcmd, "w", e->pwd))) { 409f454ebdeSmillert perror(mailcmd); 410df930be7Sderaadt (void) _exit(ERROR_EXIT); 411df930be7Sderaadt } 412df930be7Sderaadt fprintf(mail, "From: root (Cron Daemon)\n"); 413df930be7Sderaadt fprintf(mail, "To: %s\n", mailto); 414df930be7Sderaadt fprintf(mail, "Subject: Cron <%s@%s> %s\n", 415df930be7Sderaadt usernm, first_word(hostname, "."), 416df930be7Sderaadt e->cmd); 417f454ebdeSmillert #ifdef MAIL_DATE 418df930be7Sderaadt fprintf(mail, "Date: %s\n", 419f326c411Sderaadt arpadate(&StartTime)); 420df930be7Sderaadt #endif /*MAIL_DATE*/ 421df930be7Sderaadt for (env = e->envp; *env; env++) 422df930be7Sderaadt fprintf(mail, "X-Cron-Env: <%s>\n", 423df930be7Sderaadt *env); 424df930be7Sderaadt fprintf(mail, "\n"); 425df930be7Sderaadt 426df930be7Sderaadt /* this was the first char from the pipe 427df930be7Sderaadt */ 428f454ebdeSmillert fputc(ch, mail); 429df930be7Sderaadt } 430df930be7Sderaadt 431df930be7Sderaadt /* we have to read the input pipe no matter whether 432df930be7Sderaadt * we mail or not, but obviously we only write to 433df930be7Sderaadt * mail pipe if we ARE mailing. 434df930be7Sderaadt */ 435df930be7Sderaadt 436df930be7Sderaadt while (EOF != (ch = getc(in))) { 437df930be7Sderaadt bytes++; 438df930be7Sderaadt if (mailto) 439f454ebdeSmillert fputc(ch, mail); 440df930be7Sderaadt } 441df930be7Sderaadt 442df930be7Sderaadt /* only close pipe if we opened it -- i.e., we're 443df930be7Sderaadt * mailing... 444df930be7Sderaadt */ 445df930be7Sderaadt 446df930be7Sderaadt if (mailto) { 447f454ebdeSmillert Debug(DPROC, ("[%ld] closing pipe to mail\n", 448f454ebdeSmillert (long)getpid())) 449df930be7Sderaadt /* Note: the pclose will probably see 450df930be7Sderaadt * the termination of the grandchild 451df930be7Sderaadt * in addition to the mail process, since 452df930be7Sderaadt * it (the grandchild) is likely to exit 453df930be7Sderaadt * after closing its stdout. 454df930be7Sderaadt */ 455df930be7Sderaadt status = cron_pclose(mail); 456df930be7Sderaadt } 457df930be7Sderaadt 458df930be7Sderaadt /* if there was output and we could not mail it, 459df930be7Sderaadt * log the facts so the poor user can figure out 460df930be7Sderaadt * what's going on. 461df930be7Sderaadt */ 462df930be7Sderaadt if (mailto && status) { 463df930be7Sderaadt char buf[MAX_TEMPSTR]; 464df930be7Sderaadt 465f5e9cd7eSderaadt snprintf(buf, sizeof buf, 466df930be7Sderaadt "mailed %d byte%s of output but got status 0x%04x\n", 467df930be7Sderaadt bytes, (bytes==1)?"":"s", 468df930be7Sderaadt status); 469df930be7Sderaadt log_it(usernm, getpid(), "MAIL", buf); 470df930be7Sderaadt } 471df930be7Sderaadt 472df930be7Sderaadt } /*if data from grandchild*/ 473df930be7Sderaadt 474f454ebdeSmillert Debug(DPROC, ("[%ld] got EOF from grandchild\n", 475f454ebdeSmillert (long)getpid())) 476df930be7Sderaadt 477df930be7Sderaadt fclose(in); /* also closes stdout_pipe[READ_PIPE] */ 478df930be7Sderaadt } 479df930be7Sderaadt 480df930be7Sderaadt /* wait for children to die. 481df930be7Sderaadt */ 482f454ebdeSmillert for (; children > 0; children--) { 483df930be7Sderaadt WAIT_T waiter; 484df930be7Sderaadt PID_T pid; 485df930be7Sderaadt 486f454ebdeSmillert Debug(DPROC, ("[%ld] waiting for grandchild #%d to finish\n", 487f454ebdeSmillert (long)getpid(), children)) 488e134e629Smillert while ((pid = wait(&waiter)) < OK && errno == EINTR) 489e134e629Smillert ; 490df930be7Sderaadt if (pid < OK) { 491f454ebdeSmillert Debug(DPROC, 492f454ebdeSmillert ("[%ld] no more grandchildren--mail written?\n", 493f454ebdeSmillert (long)getpid())) 494df930be7Sderaadt break; 495df930be7Sderaadt } 496f454ebdeSmillert Debug(DPROC, ("[%ld] grandchild #%ld finished, status=%04x", 497f454ebdeSmillert (long)getpid(), (long)pid, WEXITSTATUS(waiter))) 498df930be7Sderaadt if (WIFSIGNALED(waiter) && WCOREDUMP(waiter)) 499df930be7Sderaadt Debug(DPROC, (", dumped core")) 500df930be7Sderaadt Debug(DPROC, ("\n")) 501df930be7Sderaadt } 502df930be7Sderaadt } 503df930be7Sderaadt 5042b139a93Smillert int 505f454ebdeSmillert safe_p(const char *usernm, const char *s) { 506f454ebdeSmillert static const char safe_delim[] = "@!:%-.,"; /* conservative! */ 507f454ebdeSmillert const char *t; 508f454ebdeSmillert int ch, first; 509df930be7Sderaadt 510f454ebdeSmillert for (t = s, first = 1; (ch = *t++) != '\0'; first = 0) { 511f454ebdeSmillert if (isascii(ch) && isprint(ch) && 512f454ebdeSmillert (isalnum(ch) || (!first && strchr(safe_delim, ch)))) 513f454ebdeSmillert continue; 514f454ebdeSmillert log_it(usernm, getpid(), "UNSAFE", s); 515f454ebdeSmillert return (FALSE); 516df930be7Sderaadt } 517f454ebdeSmillert return (TRUE); 518df930be7Sderaadt } 519