Index: head/usr.sbin/cron/cron/compat.h =================================================================== --- head/usr.sbin/cron/cron/compat.h (revision 357713) +++ head/usr.sbin/cron/cron/compat.h (revision 357714) @@ -1,140 +1,136 @@ /* Copyright 1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ /* * $FreeBSD$ */ #ifndef __P # ifdef __STDC__ # define __P(x) x # else # define __P(x) () # define const # endif #endif #if defined(UNIXPC) || defined(unixpc) # define UNIXPC 1 # define ATT 1 #endif #if defined(hpux) || defined(_hpux) || defined(__hpux) # define HPUX 1 # define seteuid(e) setresuid(-1,e,-1) # define setreuid(r,e) setresuid(r,e,-1) #endif #if defined(_IBMR2) # define AIX 1 #endif #if defined(__convex__) # define CONVEX 1 #endif #if defined(sgi) || defined(_sgi) || defined(__sgi) # define IRIX 1 /* IRIX 4 hdrs are broken: one cannot #include both * and because they disagree on system(), perror(). * Therefore we must zap the "const" keyword BEFORE including * either of them. */ # define const #endif #if defined(_UNICOS) # define UNICOS 1 #endif #ifndef POSIX # if (BSD >= 199103) || defined(__linux) || defined(ultrix) || defined(AIX) ||\ defined(HPUX) || defined(CONVEX) || defined(IRIX) # define POSIX # endif #endif #ifndef BSD # if defined(ultrix) # define BSD 198902 # endif #endif /*****************************************************************/ -#if !defined(BSD) && !defined(HPUX) && !defined(CONVEX) && !defined(__linux) -# define NEED_VFORK -#endif - #if (!defined(BSD) || (BSD < 198902)) && !defined(__linux) && \ !defined(IRIX) && !defined(NeXT) && !defined(HPUX) # define NEED_STRCASECMP #endif #if (!defined(BSD) || (BSD < 198911)) && !defined(__linux) &&\ !defined(IRIX) && !defined(UNICOS) && !defined(HPUX) # define NEED_STRDUP #endif #if (!defined(BSD) || (BSD < 198911)) && !defined(POSIX) && !defined(NeXT) # define NEED_STRERROR #endif #if defined(HPUX) || defined(AIX) || defined(UNIXPC) # define NEED_FLOCK #endif #ifndef POSIX # define NEED_SETSID #endif #if (defined(POSIX) && !defined(BSD)) && !defined(__linux) # define NEED_GETDTABLESIZE #endif #ifdef POSIX #include #ifdef _POSIX_SAVED_IDS # define HAVE_SAVED_UIDS #endif #endif #if !defined(ATT) && !defined(__linux) && !defined(IRIX) && !defined(UNICOS) # define USE_SIGCHLD #endif #if !defined(AIX) && !defined(UNICOS) # define SYS_TIME_H 1 #else # define SYS_TIME_H 0 #endif #if defined(BSD) && !defined(POSIX) # define USE_UTIMES #endif #if defined(AIX) || defined(HPUX) || defined(IRIX) # define NEED_SETENV #endif #if !defined(UNICOS) && !defined(UNIXPC) # define HAS_FCHOWN #endif #if !defined(UNICOS) && !defined(UNIXPC) # define HAS_FCHMOD #endif Index: head/usr.sbin/cron/cron/do_command.c =================================================================== --- head/usr.sbin/cron/cron/do_command.c (revision 357713) +++ head/usr.sbin/cron/cron/do_command.c (revision 357714) @@ -1,682 +1,679 @@ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ #if !defined(lint) && !defined(LINT) static const char rcsid[] = "$FreeBSD$"; #endif #include "cron.h" #include #if defined(sequent) # include #endif #if defined(SYSLOG) # include #endif #if defined(LOGIN_CAP) # include #endif #ifdef PAM # include # include #endif static void child_process(entry *, user *), do_univ(user *); static WAIT_T wait_on_child(PID_T, const char *); extern char *environ; void do_command(e, u) entry *e; user *u; { pid_t pid; Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n", getpid(), e->cmd, u->name, e->uid, e->gid)) /* fork to become asynchronous -- parent process is done immediately, * and continues to run the normal cron code, which means return to * tick(). the child and grandchild don't leave this function, alive. - * - * vfork() is unsuitable, since we have much to do, and the parent - * needs to be able to run off and fork other processes. */ switch ((pid = fork())) { case -1: log_it("CRON",getpid(),"error","can't fork"); if (e->flags & INTERVAL) e->lastexit = time(NULL); break; case 0: /* child process */ pidfile_close(pfh); child_process(e, u); Debug(DPROC, ("[%d] child process done, exiting\n", getpid())) _exit(OK_EXIT); break; default: /* parent process */ Debug(DPROC, ("[%d] main process forked child #%d, " "returning to work\n", getpid(), pid)) if (e->flags & INTERVAL) { e->lastexit = 0; e->child = pid; } break; } Debug(DPROC, ("[%d] main process returning to work\n", getpid())) } static void child_process(e, u) entry *e; user *u; { int stdin_pipe[2], stdout_pipe[2]; register char *input_data; char *usernm, *mailto, *mailfrom; PID_T jobpid, stdinjob, mailpid; register FILE *mail; register int bytes = 1; int status = 0; # if defined(LOGIN_CAP) struct passwd *pwd; login_cap_t *lc; # endif Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd)) /* mark ourselves as different to PS command watchers by upshifting * our program name. This has no effect on some kernels. */ setproctitle("running job"); /* discover some useful and important environment settings */ usernm = env_get("LOGNAME", e->envp); mailto = env_get("MAILTO", e->envp); mailfrom = env_get("MAILFROM", e->envp); #ifdef PAM /* use PAM to see if the user's account is available, * i.e., not locked or expired or whatever. skip this * for system tasks from /etc/crontab -- they can run * as any user. */ if (strcmp(u->name, SYS_NAME)) { /* not equal */ 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())) /* u->name keeps crontab owner name while LOGNAME is the name * of user to run command on behalf of. they should be the * same for a task from a per-user crontab. */ 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); /* Expired password shouldn't prevent the job from running. */ 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 /* our parent is watching for our death by catching SIGCHLD. we * do not care to watch for our children's deaths this way -- we * use wait() explicitly. so we have to disable the signal (which * was inherited from the parent). */ (void) signal(SIGCHLD, SIG_DFL); #else /* on system-V systems, we are ignoring SIGCLD. we have to stop * ignoring it now or the wait() in cron_pclose() won't work. * because of this, we have to wait() for our children here, as well. */ (void) signal(SIGCLD, SIG_DFL); #endif /*BSD*/ /* create some pipes to talk to our future child */ if (pipe(stdin_pipe) != 0 || pipe(stdout_pipe) != 0) { log_it("CRON", getpid(), "error", "can't pipe"); exit(ERROR_EXIT); } /* since we are a forked process, we can diddle the command string * we were passed -- nobody else is going to use it again, right? * * if a % is present in the command, previous characters are the * command, and subsequent characters are the additional input to * the command. Subsequent %'s will be transformed into newlines, * but that happens later. * * If there are escaped %'s, remove the escape character. */ /*local*/{ register int escaped = FALSE; register int ch; register 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'; } /* fork again, this time so we can exec the user's command. */ - switch (jobpid = vfork()) { + switch (jobpid = fork()) { case -1: - log_it("CRON",getpid(),"error","can't vfork"); + log_it("CRON",getpid(),"error","can't fork"); exit(ERROR_EXIT); /*NOTREACHED*/ case 0: - Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n", + Debug(DPROC, ("[%d] grandchild process fork()'ed\n", getpid())) if (e->uid == ROOT_UID) Jitter = RootJitter; if (Jitter != 0) { srandom(getpid()); sleep(random() % Jitter); } /* write a log message. we've waited this long to do it * because it was not until now that we knew the PID that * the actual user command shell was going to get and the * PID is part of the log message. */ if ((e->flags & DONT_LOG) == 0) { char *x = mkprints((u_char *)e->cmd, strlen(e->cmd)); log_it(usernm, getpid(), "CMD", x); free(x); } /* that's the last thing we'll log. close the log files. */ #ifdef SYSLOG closelog(); #endif /* get new pgrp, void tty, etc. */ (void) setsid(); /* close the pipe ends that we won't use. this doesn't affect * the parent, who has to read and write them; it keeps the * kernel from recording us as a potential client TWICE -- * which would keep it from sending SIGPIPE in otherwise * appropriate circumstances. */ close(stdin_pipe[WRITE_PIPE]); close(stdout_pipe[READ_PIPE]); /* grandchild process. make std{in,out} be the ends of * pipes opened by our daddy; make stderr go to stdout. */ close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN); close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT); close(STDERR); dup2(STDOUT, STDERR); /* close the pipes we just dup'ed. The resources will remain. */ close(stdin_pipe[READ_PIPE]); close(stdout_pipe[WRITE_PIPE]); /* set our login universe. Do this in the grandchild * so that the child can invoke /usr/lib/sendmail * without surprises. */ do_univ(u); environ = NULL; # if defined(LOGIN_CAP) /* Set user's entire context, but note that PATH will * be overridden later */ 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) == 0) (void) endpwent(); else { /* fall back to the old method */ (void) endpwent(); # endif /* set our directory, uid and gid. Set gid first, * since once we set uid, we've lost root privileges. */ if (setgid(e->gid) != 0) { log_it(usernm, getpid(), "error", "setgid failed"); - exit(ERROR_EXIT); + _exit(ERROR_EXIT); } # if defined(BSD) if (initgroups(usernm, e->gid) != 0) { log_it(usernm, getpid(), "error", "initgroups failed"); - exit(ERROR_EXIT); + _exit(ERROR_EXIT); } # endif if (setlogin(usernm) != 0) { log_it(usernm, getpid(), "error", "setlogin failed"); - exit(ERROR_EXIT); + _exit(ERROR_EXIT); } if (setuid(e->uid) != 0) { log_it(usernm, getpid(), "error", "setuid failed"); - exit(ERROR_EXIT); + _exit(ERROR_EXIT); } /* we aren't root after this..*/ #if defined(LOGIN_CAP) } if (lc != NULL) login_close(lc); #endif chdir(env_get("HOME", e->envp)); /* exec the command. */ { char *shell = env_get("SHELL", e->envp); char **p; /* Apply the environment from the entry, overriding existing * values (this will always set PATH, LOGNAME, etc.) putenv * should not fail unless malloc does. */ for (p = e->envp; *p; ++p) { if (putenv(*p) != 0) { warn("putenv"); _exit(ERROR_EXIT); } } # 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 /*DEBUGGING*/ execl(shell, shell, "-c", e->cmd, (char *)NULL); warn("execl: couldn't exec `%s'", shell); _exit(ERROR_EXIT); } break; default: /* parent process */ break; } /* middle process, child of original cron, parent of process running * the user's command. */ Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid())) /* close the ends of the pipe that will only be referenced in the * grandchild process... */ close(stdin_pipe[READ_PIPE]); close(stdout_pipe[WRITE_PIPE]); /* * write, to the pipe connected to child's stdin, any input specified * after a % in the crontab entry. while we copy, convert any * additional %'s to newlines. when done, if some characters were * written and the last one wasn't a newline, write a newline. * * Note that if the input data won't fit into one pipe buffer (2K * or 4K on most BSD systems), and the child doesn't read its stdin, * we would block here. thus we must fork again. */ if (*input_data && (stdinjob = fork()) == 0) { register FILE *out = fdopen(stdin_pipe[WRITE_PIPE], "w"); register int need_newline = FALSE; register int escaped = FALSE; register int ch; if (out == NULL) { warn("fdopen failed in child2"); _exit(ERROR_EXIT); } Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid())) /* close the pipe we don't use, since we inherited it and * are part of its reference count now. */ close(stdout_pipe[READ_PIPE]); /* translation: * \% -> % * % -> \n * \x -> \x for all x != % */ 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); /* close the pipe, causing an EOF condition. fclose causes * stdin_pipe[WRITE_PIPE] to be closed, too. */ fclose(out); Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid())) exit(0); } /* close the pipe to the grandkiddie's stdin, since its wicked uncle * ernie back there has it open and will close it when he's done. */ close(stdin_pipe[WRITE_PIPE]); /* * read output from the grandchild. it's stderr has been redirected to * it's stdout, which has been redirected to our pipe. if there is any * output, we'll be mailing it to the user whose crontab this is... * when the grandchild exits, we'll get EOF. */ Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid())) /*local*/{ register FILE *in = fdopen(stdout_pipe[READ_PIPE], "r"); register int ch; if (in == NULL) { warn("fdopen failed in child"); _exit(ERROR_EXIT); } mail = NULL; ch = getc(in); if (ch != EOF) { Debug(DPROC|DEXT, ("[%d] got data (%x:%c) from grandchild\n", getpid(), ch, ch)) /* get name of recipient. this is MAILTO if set to a * valid local username; USER otherwise. */ if (mailto == NULL) { /* MAILTO not present, set to USER, * unless globally overriden. */ if (defmailto) mailto = defmailto; else mailto = usernm; } if (mailto && *mailto == '\0') mailto = NULL; /* if we are supposed to be mailing, MAILTO will * be non-NULL. only in this case should we set * up the mail command and subjects and stuff... */ if (mailto) { register char **env; auto char mailcmd[MAX_COMMAND]; auto char hostname[MAXHOSTNAMELEN]; if (gethostname(hostname, MAXHOSTNAMELEN) == -1) hostname[0] = '\0'; hostname[sizeof(hostname) - 1] = '\0'; (void) snprintf(mailcmd, sizeof(mailcmd), MAILARGS, MAILCMD); if (!(mail = cron_popen(mailcmd, "w", e, &mailpid))) { warn("%s", MAILCMD); (void) _exit(ERROR_EXIT); } if (mailfrom == NULL || *mailfrom == '\0') fprintf(mail, "From: Cron Daemon <%s@%s>\n", usernm, hostname); else fprintf(mail, "From: Cron Daemon <%s>\n", mailfrom); 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 /* MAIL_DATE */ for (env = e->envp; *env; env++) fprintf(mail, "X-Cron-Env: <%s>\n", *env); fprintf(mail, "\n"); /* this was the first char from the pipe */ putc(ch, mail); } /* we have to read the input pipe no matter whether * we mail or not, but obviously we only write to * mail pipe if we ARE mailing. */ while (EOF != (ch = getc(in))) { bytes++; if (mail) putc(ch, mail); } } /*if data from grandchild*/ Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid())) /* also closes stdout_pipe[READ_PIPE] */ fclose(in); } /* wait for children to die. */ if (jobpid > 0) { WAIT_T waiter; waiter = wait_on_child(jobpid, "grandchild command job"); /* If everything went well, and -n was set, _and_ we have mail, * we won't be mailing... so shoot the messenger! */ if (WIFEXITED(waiter) && WEXITSTATUS(waiter) == 0 && (e->flags & MAIL_WHEN_ERR) == MAIL_WHEN_ERR && mail) { Debug(DPROC, ("[%d] %s executed successfully, mail suppressed\n", getpid(), "grandchild command job")) kill(mailpid, SIGKILL); (void)fclose(mail); mail = NULL; } /* only close pipe if we opened it -- i.e., we're * mailing... */ if (mail) { Debug(DPROC, ("[%d] closing pipe to mail\n", getpid())) /* Note: the pclose will probably see * the termination of the grandchild * in addition to the mail process, since * it (the grandchild) is likely to exit * after closing its stdout. */ status = cron_pclose(mail); /* if there was output and we could not mail it, * log the facts so the poor user can figure out * what's going on. */ if (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); } } } if (*input_data && stdinjob > 0) wait_on_child(stdinjob, "grandchild stdinjob"); } static WAIT_T wait_on_child(PID_T childpid, const char *name) { WAIT_T waiter; PID_T pid; Debug(DPROC, ("[%d] waiting for %s (%d) to finish\n", getpid(), name, childpid)) #ifdef POSIX while ((pid = waitpid(childpid, &waiter, 0)) < 0 && errno == EINTR) #else while ((pid = wait4(childpid, &waiter, 0, NULL)) < 0 && errno == EINTR) #endif ; if (pid < OK) return waiter; Debug(DPROC, ("[%d] %s (%d) finished, status=%04x", getpid(), name, pid, WEXITSTATUS(waiter))) if (WIFSIGNALED(waiter) && WCOREDUMP(waiter)) Debug(DPROC, (", dumped core")) Debug(DPROC, ("\n")) return waiter; } static void do_univ(u) user *u; { #if defined(sequent) /* Dynix (Sequent) hack to put the user associated with * the passed user structure into the ATT universe if * necessary. We have to dig the gecos info out of * the user's password entry to see if the magic * "universe(att)" string is present. */ struct passwd *p; char *s; int i; p = getpwuid(u->uid); (void) endpwent(); if (p == NULL) return; s = p->pw_gecos; for (i = 0; i < 4; i++) { if ((s = strchr(s, ',')) == NULL) return; s++; } if (strcmp(s, "universe(att)")) return; (void) universe(U_ATT); #endif } Index: head/usr.sbin/cron/cron/externs.h =================================================================== --- head/usr.sbin/cron/cron/externs.h (revision 357713) +++ head/usr.sbin/cron/cron/externs.h (revision 357714) @@ -1,147 +1,143 @@ /* $FreeBSD$ */ /* Copyright 1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ #if defined(POSIX) || defined(ATT) # include # include # include # include # define DIR_T struct dirent # define WAIT_T int # define WAIT_IS_INT 1 extern char *tzname[2]; # define TZONE(tm) tzname[(tm).tm_isdst] #endif #if defined(UNIXPC) # undef WAIT_T # undef WAIT_IS_INT # define WAIT_T union wait #endif #if defined(POSIX) # define SIG_T sig_t # define TIME_T time_t # define PID_T pid_t #endif #if defined(ATT) # define SIG_T void # define TIME_T long # define PID_T int #endif #if !defined(POSIX) && !defined(ATT) /* classic BSD */ extern time_t time(); extern unsigned sleep(); extern struct tm *localtime(); extern struct passwd *getpwnam(); extern int errno; extern void perror(), exit(), free(); extern char *getenv(), *strcpy(), *strchr(), *strtok(); extern void *malloc(), *realloc(); # define SIG_T void # define TIME_T long # define PID_T int # define WAIT_T union wait # define DIR_T struct direct # include # define TZONE(tm) (tm).tm_zone #endif /* getopt() isn't part of POSIX. some systems define it in anyway. * of those that do, some complain that our definition is different and some * do not. to add to the misery and confusion, some systems define getopt() * in ways that we cannot predict or comprehend, yet do not define the adjunct * external variables needed for the interface. */ #if (!defined(BSD) || (BSD < 198911)) && !defined(ATT) && !defined(UNICOS) int getopt(int, char * const *, const char *); #endif #if (!defined(BSD) || (BSD < 199103)) extern char *optarg; extern int optind, opterr, optopt; #endif #if WAIT_IS_INT # ifndef WEXITSTATUS # define WEXITSTATUS(x) (((x) >> 8) & 0xff) # endif # ifndef WTERMSIG # define WTERMSIG(x) ((x) & 0x7f) # endif # ifndef WCOREDUMP # define WCOREDUMP(x) ((x) & 0x80) # endif #else /*WAIT_IS_INT*/ # ifndef WEXITSTATUS # define WEXITSTATUS(x) ((x).w_retcode) # endif # ifndef WTERMSIG # define WTERMSIG(x) ((x).w_termsig) # endif # ifndef WCOREDUMP # define WCOREDUMP(x) ((x).w_coredump) # endif #endif /*WAIT_IS_INT*/ #ifndef WIFSIGNALED #define WIFSIGNALED(x) (WTERMSIG(x) != 0) #endif #ifndef WIFEXITED #define WIFEXITED(x) (WTERMSIG(x) == 0) #endif #ifdef NEED_STRCASECMP extern int strcasecmp(char *, char *); #endif #ifdef NEED_STRDUP extern char *strdup(char *); #endif #ifdef NEED_STRERROR extern char *strerror(int); #endif #ifdef NEED_FLOCK extern int flock(int, int); # define LOCK_SH 1 # define LOCK_EX 2 # define LOCK_NB 4 # define LOCK_UN 8 #endif #ifdef NEED_SETSID extern int setsid(void); #endif #ifdef NEED_GETDTABLESIZE extern int getdtablesize(void); #endif #ifdef NEED_SETENV extern int setenv(char *, char *, int); #endif - -#ifdef NEED_VFORK -extern PID_T vfork(void); -#endif Index: head/usr.sbin/cron/cron/popen.c =================================================================== --- head/usr.sbin/cron/cron/popen.c (revision 357713) +++ head/usr.sbin/cron/cron/popen.c (revision 357714) @@ -1,250 +1,250 @@ /* * Copyright (c) 1988 The Regents of the University of California. * All rights reserved. * * This code is derived from software written by Ken Arnold and * published in UNIX Review, Vol. 6, No. 8. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the University of California, Berkeley. The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * */ /* this came out of the ftpd sources; it's been modified to avoid the * globbing stuff since we don't need it. also execvp instead of execv. */ #ifndef lint #if 0 static char sccsid[] = "@(#)popen.c 5.7 (Berkeley) 2/14/89"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include "cron.h" #include #include #include #if defined(SYSLOG) # include #endif #if defined(LOGIN_CAP) # include #endif #define MAX_ARGS 100 #define WANT_GLOBBING 0 /* * Special version of popen which avoids call to shell. This insures no one * may create a pipe to a hidden program as a side effect of a list or dir * command. */ static PID_T *pids; static int fds; FILE * cron_popen(program, type, e, pidptr) char *program, *type; entry *e; PID_T *pidptr; { register char *cp; FILE *iop; int argc, pdes[2]; PID_T pid; char *usernm; char *argv[MAX_ARGS + 1]; # if defined(LOGIN_CAP) struct passwd *pwd; login_cap_t *lc; # endif #if WANT_GLOBBING char **pop, *vv[2]; int gargc; char *gargv[1000]; extern char **glob(), **copyblk(); #endif if ((*type != 'r' && *type != 'w') || type[1]) return(NULL); if (!pids) { if ((fds = getdtablesize()) <= 0) return(NULL); if (!(pids = calloc(fds, sizeof(PID_T)))) return(NULL); } if (pipe(pdes) < 0) return(NULL); /* break up string into pieces */ for (argc = 0, cp = program; argc < MAX_ARGS; cp = NULL) if (!(argv[argc++] = strtok(cp, " \t\n"))) break; argv[MAX_ARGS] = NULL; #if WANT_GLOBBING /* glob each piece */ gargv[0] = argv[0]; for (gargc = argc = 1; argv[argc]; argc++) { if (!(pop = glob(argv[argc]))) { /* globbing failed */ vv[0] = argv[argc]; vv[1] = NULL; pop = copyblk(vv); } argv[argc] = (char *)pop; /* save to free later */ while (*pop && gargc < 1000) gargv[gargc++] = *pop++; } gargv[gargc] = NULL; #endif iop = NULL; - switch(pid = vfork()) { + switch(pid = fork()) { case -1: /* error */ (void)close(pdes[0]); (void)close(pdes[1]); goto pfree; /* NOTREACHED */ case 0: /* child */ if (e != NULL) { #ifdef SYSLOG closelog(); #endif /* get new pgrp, void tty, etc. */ (void) setsid(); } if (*type == 'r') { /* Do not share our parent's stdin */ (void)close(0); (void)open(_PATH_DEVNULL, O_RDWR); if (pdes[1] != 1) { dup2(pdes[1], 1); dup2(pdes[1], 2); /* stderr, too! */ (void)close(pdes[1]); } (void)close(pdes[0]); } else { if (pdes[0] != 0) { dup2(pdes[0], 0); (void)close(pdes[0]); } /* Hack: stdout gets revoked */ (void)close(1); (void)open(_PATH_DEVNULL, O_RDWR); (void)close(2); (void)open(_PATH_DEVNULL, O_RDWR); (void)close(pdes[1]); } if (e != NULL) { /* Set user's entire context, but skip the environment * as cron provides a separate interface for this */ usernm = env_get("LOGNAME", e->envp); # 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) (void) endpwent(); else { /* fall back to the old method */ (void) endpwent(); # endif /* * Set our directory, uid and gid. Set gid * first since once we set uid, we've lost * root privileges. */ if (setgid(e->gid) != 0) _exit(ERROR_EXIT); # if defined(BSD) if (initgroups(usernm, e->gid) != 0) _exit(ERROR_EXIT); # endif if (setlogin(usernm) != 0) _exit(ERROR_EXIT); if (setuid(e->uid) != 0) _exit(ERROR_EXIT); /* we aren't root after this..*/ #if defined(LOGIN_CAP) } if (lc != NULL) login_close(lc); #endif chdir(env_get("HOME", e->envp)); } #if WANT_GLOBBING execvp(gargv[0], gargv); #else execvp(argv[0], argv); #endif _exit(1); } /* parent; assume fdopen can't fail... */ if (*type == 'r') { iop = fdopen(pdes[0], type); (void)close(pdes[1]); } else { iop = fdopen(pdes[1], type); (void)close(pdes[0]); } pids[fileno(iop)] = pid; pfree: #if WANT_GLOBBING for (argc = 1; argv[argc] != NULL; argc++) { /* blkfree((char **)argv[argc]); */ free((char *)argv[argc]); } #endif *pidptr = pid; return(iop); } int cron_pclose(iop) FILE *iop; { register int fdes; int omask; WAIT_T stat_loc; PID_T pid; /* * pclose returns -1 if stream is not associated with a * `popened' command, or, if already `pclosed'. */ if (pids == 0 || pids[fdes = fileno(iop)] == 0) return(-1); (void)fclose(iop); omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1) ; (void)sigsetmask(omask); pids[fdes] = 0; return (pid == -1 ? -1 : WEXITSTATUS(stat_loc)); } Index: head/usr.sbin/cron/lib/compat.c =================================================================== --- head/usr.sbin/cron/lib/compat.c (revision 357713) +++ head/usr.sbin/cron/lib/compat.c (revision 357714) @@ -1,237 +1,225 @@ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved * * Distribute freely, except: don't remove my name from the source or * documentation (don't take credit for my work), mark your changes (don't * get me blamed for your possible bugs), don't alter or remove this * notice. May be sold if buildable source is provided to buyer. No * warrantee of any kind, express or implied, is included with this * software; use at your own risk, responsibility for damages (if any) to * anyone resulting from the use of this software rests entirely with the * user. * * Send bug reports, bug fixes, enhancements, requests, flames, etc., and * I'll try to keep a version up to date. I can be reached as follows: * Paul Vixie uunet!decwrl!vixie!paul */ #if !defined(lint) && !defined(LINT) static char rcsid[] = "$FreeBSD$"; #endif /* vix 30dec93 [broke this out of misc.c - see RCS log for history] * vix 15jan87 [added TIOCNOTTY, thanks csg@pyramid] */ #include "cron.h" #ifdef NEED_GETDTABLESIZE # include #endif #if defined(NEED_SETSID) && defined(BSD) # include #endif #include #include -/* the code does not depend on any of vfork's - * side-effects; it just uses it as a quick - * fork-and-exec. - */ -#ifdef NEED_VFORK -PID_T -vfork() { - return (fork()); -} -#endif - - #ifdef NEED_STRDUP char * strdup(str) char *str; { char *temp; if ((temp = malloc(strlen(str) + 1)) == NULL) { errno = ENOMEM; return NULL; } (void) strcpy(temp, str); return temp; } #endif #ifdef NEED_STRERROR char * strerror(error) int error; { extern char *sys_errlist[]; extern int sys_nerr; static char buf[32]; if ((error <= sys_nerr) && (error > 0)) { return sys_errlist[error]; } sprintf(buf, "Unknown error: %d", error); return buf; } #endif #ifdef NEED_STRCASECMP int strcasecmp(left, right) char *left; char *right; { while (*left && (MkLower(*left) == MkLower(*right))) { left++; right++; } return MkLower(*left) - MkLower(*right); } #endif #ifdef NEED_SETSID int setsid() { int newpgrp; # if defined(BSD) int fd; # if defined(POSIX) newpgrp = setpgid((pid_t)0, getpid()); # else newpgrp = setpgrp(0, getpid()); # endif if ((fd = open(_PATH_TTY, 2)) >= 0) { (void) ioctl(fd, TIOCNOTTY, (char*)0); (void) close(fd); } # else /*BSD*/ newpgrp = setpgrp(); (void) close(STDIN); (void) open(_PATH_DEVNULL, 0); (void) close(STDOUT); (void) open(_PATH_DEVNULL, 1); (void) close(STDERR); (void) open(_PATH_DEVNULL, 2); # endif /*BSD*/ return newpgrp; } #endif /*NEED_SETSID*/ #ifdef NEED_GETDTABLESIZE int getdtablesize() { #ifdef _SC_OPEN_MAX return sysconf(_SC_OPEN_MAX); #else return _POSIX_OPEN_MAX; #endif } #endif #ifdef NEED_FLOCK /* The following flock() emulation snarfed intact *) from the HP-UX * "BSD to HP-UX porting tricks" maintained by * system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson)) * from the version "last updated: 11-Jan-1993" * Snarfage done by Jarkko Hietaniemi * *) well, almost, had to K&R the function entry, HPUX "cc" * does not grok ANSI function prototypes */ /* * flock (fd, operation) * * This routine performs some file locking like the BSD 'flock' * on the object described by the int file descriptor 'fd', * which must already be open. * * The operations that are available are: * * LOCK_SH - get a shared lock. * LOCK_EX - get an exclusive lock. * LOCK_NB - don't block (must be ORed with LOCK_SH or LOCK_EX). * LOCK_UN - release a lock. * * Return value: 0 if lock successful, -1 if failed. * * Note that whether the locks are enforced or advisory is * controlled by the presence or absence of the SETGID bit on * the executable. * * Note that there is no difference between shared and exclusive * locks, since the 'lockf' system call in SYSV doesn't make any * distinction. * * The file "" should be modified to contain the definitions * of the available operations, which must be added manually (see below * for the values). */ /* this code has been reformatted by vixie */ int flock(fd, operation) int fd; int operation; { int i; switch (operation) { case LOCK_SH: /* get a shared lock */ case LOCK_EX: /* get an exclusive lock */ i = lockf (fd, F_LOCK, 0); break; case LOCK_SH|LOCK_NB: /* get a non-blocking shared lock */ case LOCK_EX|LOCK_NB: /* get a non-blocking exclusive lock */ i = lockf (fd, F_TLOCK, 0); if (i == -1) if ((errno == EAGAIN) || (errno == EACCES)) errno = EWOULDBLOCK; break; case LOCK_UN: /* unlock */ i = lockf (fd, F_ULOCK, 0); break; default: /* can't decipher operation */ i = -1; errno = EINVAL; break; } return (i); } #endif /*NEED_FLOCK*/ #ifdef NEED_SETENV int setenv(name, value, overwrite) char *name, *value; int overwrite; { char *tmp; if (overwrite && getenv(name)) return -1; if (!(tmp = malloc(strlen(name) + strlen(value) + 2))) { errno = ENOMEM; return -1; } sprintf(tmp, "%s=%s", name, value); return putenv(tmp); /* intentionally orphan 'tmp' storage */ } #endif