1 /* $NetBSD: popen.c,v 1.2 2010/05/06 18:53:17 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software written by Ken Arnold and 8 * published in UNIX Review, Vol. 6, No. 8. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 */ 36 37 /* this came out of the ftpd sources; it's been modified to avoid the 38 * globbing stuff since we don't need it. also execvp instead of execv. 39 */ 40 41 #include <sys/cdefs.h> 42 #ifndef lint 43 #if 0 44 static sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; 45 static char rcsid[] = "Id: popen.c,v 1.6 2003/02/16 04:40:01 vixie Exp"; 46 #else 47 __RCSID("$NetBSD: popen.c,v 1.2 2010/05/06 18:53:17 christos Exp $"); 48 #endif 49 #endif /* not lint */ 50 51 #include "cron.h" 52 53 #define MAX_ARGV 100 54 #define MAX_GARGV 1000 55 56 /* 57 * Special version of popen which avoids call to shell. This ensures noone 58 * may create a pipe to a hidden program as a side effect of a list or dir 59 * command. 60 */ 61 static PID_T *pids; 62 static long fds; 63 64 FILE * 65 cron_popen(char *program, const char *type, struct passwd *pw) { 66 char *cp; 67 FILE *iop; 68 int argc, pdes[2]; 69 PID_T pid; 70 char *argv[MAX_ARGV]; 71 72 if ((*type != 'r' && *type != 'w') || type[1] != '\0') 73 return (NULL); 74 75 if (!pids) { 76 size_t len; 77 if ((fds = sysconf(_SC_OPEN_MAX)) <= 0) 78 return (NULL); 79 len = fds * sizeof(*pids); 80 if ((pids = malloc(len)) == NULL) 81 return (NULL); 82 (void)memset(pids, 0, len); 83 } 84 if (pipe(pdes) < 0) 85 return (NULL); 86 87 /* break up string into pieces */ 88 for (argc = 0, cp = program; argc < MAX_ARGV - 1; cp = NULL) 89 if (!(argv[argc++] = strtok(cp, " \t\n"))) 90 break; 91 argv[MAX_ARGV-1] = NULL; 92 93 switch (pid = vfork()) { 94 case -1: /* error */ 95 (void)close(pdes[0]); 96 (void)close(pdes[1]); 97 return (NULL); 98 /* NOTREACHED */ 99 case 0: /* child */ 100 if (pw) { 101 #ifdef LOGIN_CAP 102 if (setusercontext(0, pw, pw->pw_uid, LOGIN_SETALL) < 0) { 103 warnx("setusercontext failed for %s", 104 pw->pw_name); 105 _exit(ERROR_EXIT); 106 } 107 #else 108 if (setgid(pw->pw_gid) < 0 || 109 initgroups(pw->pw_name, pw->pw_gid) < 0) { 110 warnx("unable to set groups for %s", 111 pw->pw_name); 112 _exit(1); 113 } 114 #if (defined(BSD)) && (BSD >= 199103) 115 setlogin(pw->pw_name); 116 #endif /* BSD */ 117 if (setuid(pw->pw_uid)) { 118 warnx("unable to set uid for %s", 119 pw->pw_name); 120 _exit(1); 121 } 122 #endif /* LOGIN_CAP */ 123 } 124 if (*type == 'r') { 125 if (pdes[1] != STDOUT) { 126 (void)dup2(pdes[1], STDOUT); 127 (void)close(pdes[1]); 128 } 129 (void)dup2(STDOUT, STDERR); /* stderr too! */ 130 (void)close(pdes[0]); 131 } else { 132 if (pdes[0] != STDIN) { 133 (void)dup2(pdes[0], STDIN); 134 (void)close(pdes[0]); 135 } 136 (void)close(pdes[1]); 137 } 138 (void)execvp(argv[0], argv); 139 _exit(1); 140 } 141 142 /* parent; assume fdopen can't fail... */ 143 if (*type == 'r') { 144 iop = fdopen(pdes[0], type); 145 (void)close(pdes[1]); 146 } else { 147 iop = fdopen(pdes[1], type); 148 (void)close(pdes[0]); 149 } 150 pids[fileno(iop)] = pid; 151 152 return (iop); 153 } 154 155 int 156 cron_pclose(FILE *iop) { 157 int fdes; 158 PID_T pid; 159 WAIT_T status; 160 sigset_t sset, osset; 161 162 /* 163 * pclose returns -1 if stream is not associated with a 164 * `popened' command, or, if already `pclosed'. 165 */ 166 if (pids == 0 || pids[fdes = fileno(iop)] == 0) 167 return (-1); 168 (void)fclose(iop); 169 (void)sigemptyset(&sset); 170 (void)sigaddset(&sset, SIGINT); 171 (void)sigaddset(&sset, SIGQUIT); 172 (void)sigaddset(&sset, SIGHUP); 173 (void)sigprocmask(SIG_BLOCK, &sset, &osset); 174 while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR) 175 continue; 176 (void)sigprocmask(SIG_SETMASK, &osset, NULL); 177 pids[fdes] = 0; 178 if (pid < 0) 179 return (pid); 180 if (WIFEXITED(status)) 181 return (WEXITSTATUS(status)); 182 else 183 return WTERMSIG(status); 184 } 185