do_command.c revision 23884
1/* Copyright 1988,1990,1993,1994 by Paul Vixie 2 * All rights reserved 3 * 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 11 * user. 12 * 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 16 */ 17 18#if !defined(lint) && !defined(LINT) 19static char rcsid[] = "$Id: do_command.c,v 1.10 1997/02/22 16:04:43 peter Exp $"; 20#endif 21 22 23#include "cron.h" 24#include <sys/signal.h> 25#if defined(sequent) 26# include <sys/universe.h> 27#endif 28#if defined(SYSLOG) 29# include <syslog.h> 30#endif 31#if defined(LOGIN_CAP) 32# include <login_cap.h> 33#endif 34 35 36static void child_process __P((entry *, user *)), 37 do_univ __P((user *)); 38 39 40void 41do_command(e, u) 42 entry *e; 43 user *u; 44{ 45 Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n", 46 getpid(), e->cmd, u->name, e->uid, e->gid)) 47 48 /* fork to become asynchronous -- parent process is done immediately, 49 * and continues to run the normal cron code, which means return to 50 * tick(). the child and grandchild don't leave this function, alive. 51 * 52 * vfork() is unsuitable, since we have much to do, and the parent 53 * needs to be able to run off and fork other processes. 54 */ 55 switch (fork()) { 56 case -1: 57 log_it("CRON",getpid(),"error","can't fork"); 58 break; 59 case 0: 60 /* child process */ 61 acquire_daemonlock(1); 62 child_process(e, u); 63 Debug(DPROC, ("[%d] child process done, exiting\n", getpid())) 64 _exit(OK_EXIT); 65 break; 66 default: 67 /* parent process */ 68 break; 69 } 70 Debug(DPROC, ("[%d] main process returning to work\n", getpid())) 71} 72 73 74static void 75child_process(e, u) 76 entry *e; 77 user *u; 78{ 79 int stdin_pipe[2], stdout_pipe[2]; 80 register char *input_data; 81 char *usernm, *mailto; 82 int children = 0; 83# if defined(LOGIN_CAP) 84 struct passwd *pwd; 85 login_cap_t *lc; 86# endif 87 88 Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd)) 89 90 /* mark ourselves as different to PS command watchers by upshifting 91 * our program name. This has no effect on some kernels. 92 */ 93 /*local*/{ 94 register char *pch; 95 96 for (pch = ProgramName; *pch; pch++) 97 *pch = MkUpper(*pch); 98 } 99 100 /* discover some useful and important environment settings 101 */ 102 usernm = env_get("LOGNAME", e->envp); 103 mailto = env_get("MAILTO", e->envp); 104 105#ifdef USE_SIGCHLD 106 /* our parent is watching for our death by catching SIGCHLD. we 107 * do not care to watch for our children's deaths this way -- we 108 * use wait() explictly. so we have to disable the signal (which 109 * was inherited from the parent). 110 */ 111 (void) signal(SIGCHLD, SIG_IGN); 112#else 113 /* on system-V systems, we are ignoring SIGCLD. we have to stop 114 * ignoring it now or the wait() in cron_pclose() won't work. 115 * because of this, we have to wait() for our children here, as well. 116 */ 117 (void) signal(SIGCLD, SIG_DFL); 118#endif /*BSD*/ 119 120 /* create some pipes to talk to our future child 121 */ 122 pipe(stdin_pipe); /* child's stdin */ 123 pipe(stdout_pipe); /* child's stdout */ 124 125 /* since we are a forked process, we can diddle the command string 126 * we were passed -- nobody else is going to use it again, right? 127 * 128 * if a % is present in the command, previous characters are the 129 * command, and subsequent characters are the additional input to 130 * the command. Subsequent %'s will be transformed into newlines, 131 * but that happens later. 132 * 133 * If there are escaped %'s, remove the escape character. 134 */ 135 /*local*/{ 136 register int escaped = FALSE; 137 register int ch; 138 register char *p; 139 140 for (input_data = p = e->cmd; ch = *input_data; 141 input_data++, p++) { 142 if (p != input_data) 143 *p = ch; 144 if (escaped) { 145 if (ch == '%' || ch == '\\') 146 *--p = ch; 147 escaped = FALSE; 148 continue; 149 } 150 if (ch == '\\') { 151 escaped = TRUE; 152 continue; 153 } 154 if (ch == '%') { 155 *input_data++ = '\0'; 156 break; 157 } 158 } 159 *p = '\0'; 160 } 161 162 /* fork again, this time so we can exec the user's command. 163 */ 164 switch (vfork()) { 165 case -1: 166 log_it("CRON",getpid(),"error","can't vfork"); 167 exit(ERROR_EXIT); 168 /*NOTREACHED*/ 169 case 0: 170 Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n", 171 getpid())) 172 173 /* write a log message. we've waited this long to do it 174 * because it was not until now that we knew the PID that 175 * the actual user command shell was going to get and the 176 * PID is part of the log message. 177 */ 178 /*local*/{ 179 char *x = mkprints((u_char *)e->cmd, strlen(e->cmd)); 180 181 log_it(usernm, getpid(), "CMD", x); 182 free(x); 183 } 184 185 /* that's the last thing we'll log. close the log files. 186 */ 187#ifdef SYSLOG 188 closelog(); 189#endif 190 191 /* get new pgrp, void tty, etc. 192 */ 193 (void) setsid(); 194 195 /* close the pipe ends that we won't use. this doesn't affect 196 * the parent, who has to read and write them; it keeps the 197 * kernel from recording us as a potential client TWICE -- 198 * which would keep it from sending SIGPIPE in otherwise 199 * appropriate circumstances. 200 */ 201 close(stdin_pipe[WRITE_PIPE]); 202 close(stdout_pipe[READ_PIPE]); 203 204 /* grandchild process. make std{in,out} be the ends of 205 * pipes opened by our daddy; make stderr go to stdout. 206 */ 207 close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN); 208 close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT); 209 close(STDERR); dup2(STDOUT, STDERR); 210 211 /* close the pipes we just dup'ed. The resources will remain. 212 */ 213 close(stdin_pipe[READ_PIPE]); 214 close(stdout_pipe[WRITE_PIPE]); 215 216 /* set our login universe. Do this in the grandchild 217 * so that the child can invoke /usr/lib/sendmail 218 * without surprises. 219 */ 220 do_univ(u); 221 222# if defined(LOGIN_CAP) 223 /* Set user's entire context, but skip the environment 224 * as cron provides a separate interface for this 225 */ 226 pwd = getpwuid(e->uid); 227 if (pwd) 228 lc = login_getclass(pwd); 229 else 230 lc = NULL; 231 if (lc && pwd) { 232 setusercontext(lc, pwd, e->uid, 233 LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)); 234 login_close(lc); 235 } else { 236 /* fall back to the old method */ 237# endif 238 /* set our directory, uid and gid. Set gid first, 239 * since once we set uid, we've lost root privledges. 240 */ 241 setgid(e->gid); 242# if defined(BSD) 243 initgroups(env_get("LOGNAME", e->envp), e->gid); 244# endif 245 setlogin(usernm); 246 setuid(e->uid); /* we aren't root after this..*/ 247#if defined(LOGIN_CAP) 248 } 249#endif 250 chdir(env_get("HOME", e->envp)); 251 252 /* exec the command. 253 */ 254 { 255 char *shell = env_get("SHELL", e->envp); 256 257# if DEBUGGING 258 if (DebugFlags & DTEST) { 259 fprintf(stderr, 260 "debug DTEST is on, not exec'ing command.\n"); 261 fprintf(stderr, 262 "\tcmd='%s' shell='%s'\n", e->cmd, shell); 263 _exit(OK_EXIT); 264 } 265# endif /*DEBUGGING*/ 266 execle(shell, shell, "-c", e->cmd, (char *)0, e->envp); 267 fprintf(stderr, "execl: couldn't exec `%s'\n", shell); 268 perror("execl"); 269 _exit(ERROR_EXIT); 270 } 271 break; 272 default: 273 /* parent process */ 274 break; 275 } 276 277 children++; 278 279 /* middle process, child of original cron, parent of process running 280 * the user's command. 281 */ 282 283 Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid())) 284 285 /* close the ends of the pipe that will only be referenced in the 286 * grandchild process... 287 */ 288 close(stdin_pipe[READ_PIPE]); 289 close(stdout_pipe[WRITE_PIPE]); 290 291 /* 292 * write, to the pipe connected to child's stdin, any input specified 293 * after a % in the crontab entry. while we copy, convert any 294 * additional %'s to newlines. when done, if some characters were 295 * written and the last one wasn't a newline, write a newline. 296 * 297 * Note that if the input data won't fit into one pipe buffer (2K 298 * or 4K on most BSD systems), and the child doesn't read its stdin, 299 * we would block here. thus we must fork again. 300 */ 301 302 if (*input_data && fork() == 0) { 303 register FILE *out = fdopen(stdin_pipe[WRITE_PIPE], "w"); 304 register int need_newline = FALSE; 305 register int escaped = FALSE; 306 register int ch; 307 308 Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid())) 309 310 /* close the pipe we don't use, since we inherited it and 311 * are part of its reference count now. 312 */ 313 close(stdout_pipe[READ_PIPE]); 314 315 /* translation: 316 * \% -> % 317 * % -> \n 318 * \x -> \x for all x != % 319 */ 320 while (ch = *input_data++) { 321 if (escaped) { 322 if (ch != '%') 323 putc('\\', out); 324 } else { 325 if (ch == '%') 326 ch = '\n'; 327 } 328 329 if (!(escaped = (ch == '\\'))) { 330 putc(ch, out); 331 need_newline = (ch != '\n'); 332 } 333 } 334 if (escaped) 335 putc('\\', out); 336 if (need_newline) 337 putc('\n', out); 338 339 /* close the pipe, causing an EOF condition. fclose causes 340 * stdin_pipe[WRITE_PIPE] to be closed, too. 341 */ 342 fclose(out); 343 344 Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid())) 345 exit(0); 346 } 347 348 /* close the pipe to the grandkiddie's stdin, since its wicked uncle 349 * ernie back there has it open and will close it when he's done. 350 */ 351 close(stdin_pipe[WRITE_PIPE]); 352 353 children++; 354 355 /* 356 * read output from the grandchild. it's stderr has been redirected to 357 * it's stdout, which has been redirected to our pipe. if there is any 358 * output, we'll be mailing it to the user whose crontab this is... 359 * when the grandchild exits, we'll get EOF. 360 */ 361 362 Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid())) 363 364 /*local*/{ 365 register FILE *in = fdopen(stdout_pipe[READ_PIPE], "r"); 366 register int ch = getc(in); 367 368 if (ch != EOF) { 369 register FILE *mail; 370 register int bytes = 1; 371 int status = 0; 372 373 Debug(DPROC|DEXT, 374 ("[%d] got data (%x:%c) from grandchild\n", 375 getpid(), ch, ch)) 376 377 /* get name of recipient. this is MAILTO if set to a 378 * valid local username; USER otherwise. 379 */ 380 if (mailto) { 381 /* MAILTO was present in the environment 382 */ 383 if (!*mailto) { 384 /* ... but it's empty. set to NULL 385 */ 386 mailto = NULL; 387 } 388 } else { 389 /* MAILTO not present, set to USER. 390 */ 391 mailto = usernm; 392 } 393 394 /* if we are supposed to be mailing, MAILTO will 395 * be non-NULL. only in this case should we set 396 * up the mail command and subjects and stuff... 397 */ 398 399 if (mailto) { 400 register char **env; 401 auto char mailcmd[MAX_COMMAND]; 402 auto char hostname[MAXHOSTNAMELEN]; 403 404 (void) gethostname(hostname, MAXHOSTNAMELEN); 405 (void) snprintf(mailcmd, sizeof(mailcmd), 406 MAILARGS, MAILCMD); 407 if (!(mail = cron_popen(mailcmd, "w"))) { 408 perror(MAILCMD); 409 (void) _exit(ERROR_EXIT); 410 } 411 fprintf(mail, "From: root (Cron Daemon)\n"); 412 fprintf(mail, "To: %s\n", mailto); 413 fprintf(mail, "Subject: Cron <%s@%s> %s\n", 414 usernm, first_word(hostname, "."), 415 e->cmd); 416# if defined(MAIL_DATE) 417 fprintf(mail, "Date: %s\n", 418 arpadate(&TargetTime)); 419# endif /* MAIL_DATE */ 420 for (env = e->envp; *env; env++) 421 fprintf(mail, "X-Cron-Env: <%s>\n", 422 *env); 423 fprintf(mail, "\n"); 424 425 /* this was the first char from the pipe 426 */ 427 putc(ch, mail); 428 } 429 430 /* we have to read the input pipe no matter whether 431 * we mail or not, but obviously we only write to 432 * mail pipe if we ARE mailing. 433 */ 434 435 while (EOF != (ch = getc(in))) { 436 bytes++; 437 if (mailto) 438 putc(ch, mail); 439 } 440 441 /* only close pipe if we opened it -- i.e., we're 442 * mailing... 443 */ 444 445 if (mailto) { 446 Debug(DPROC, ("[%d] closing pipe to mail\n", 447 getpid())) 448 /* Note: the pclose will probably see 449 * the termination of the grandchild 450 * in addition to the mail process, since 451 * it (the grandchild) is likely to exit 452 * after closing its stdout. 453 */ 454 status = cron_pclose(mail); 455 } 456 457 /* if there was output and we could not mail it, 458 * log the facts so the poor user can figure out 459 * what's going on. 460 */ 461 if (mailto && status) { 462 char buf[MAX_TEMPSTR]; 463 464 snprintf(buf, sizeof(buf), 465 "mailed %d byte%s of output but got status 0x%04x\n", 466 bytes, (bytes==1)?"":"s", 467 status); 468 log_it(usernm, getpid(), "MAIL", buf); 469 } 470 471 } /*if data from grandchild*/ 472 473 Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid())) 474 475 fclose(in); /* also closes stdout_pipe[READ_PIPE] */ 476 } 477 478 /* wait for children to die. 479 */ 480 for (; children > 0; children--) 481 { 482 WAIT_T waiter; 483 PID_T pid; 484 485 Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n", 486 getpid(), children)) 487 pid = wait(&waiter); 488 if (pid < OK) { 489 Debug(DPROC, ("[%d] no more grandchildren--mail written?\n", 490 getpid())) 491 break; 492 } 493 Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x", 494 getpid(), pid, WEXITSTATUS(waiter))) 495 if (WIFSIGNALED(waiter) && WCOREDUMP(waiter)) 496 Debug(DPROC, (", dumped core")) 497 Debug(DPROC, ("\n")) 498 } 499} 500 501 502static void 503do_univ(u) 504 user *u; 505{ 506#if defined(sequent) 507/* Dynix (Sequent) hack to put the user associated with 508 * the passed user structure into the ATT universe if 509 * necessary. We have to dig the gecos info out of 510 * the user's password entry to see if the magic 511 * "universe(att)" string is present. 512 */ 513 514 struct passwd *p; 515 char *s; 516 int i; 517 518 p = getpwuid(u->uid); 519 (void) endpwent(); 520 521 if (p == NULL) 522 return; 523 524 s = p->pw_gecos; 525 526 for (i = 0; i < 4; i++) 527 { 528 if ((s = strchr(s, ',')) == NULL) 529 return; 530 s++; 531 } 532 if (strcmp(s, "universe(att)")) 533 return; 534 535 (void) universe(U_ATT); 536#endif 537} 538