1 /* $OpenBSD: popen.c,v 1.20 2006/04/03 01:31:11 djm 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 /* this came out of the ftpd sources; it's been modified to avoid the 37 * globbing stuff since we don't need it. also execvp instead of execv. 38 */ 39 40 #ifndef lint 41 #if 0 42 static const sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; 43 #else 44 static const char rcsid[] = "$OpenBSD: popen.c,v 1.20 2006/04/03 01:31:11 djm Exp $"; 45 #endif 46 #endif /* not lint */ 47 48 #include "cron.h" 49 50 #define MAX_ARGV 100 51 #define MAX_GARGV 1000 52 53 /* 54 * Special version of popen which avoids call to shell. This ensures noone 55 * may create a pipe to a hidden program as a side effect of a list or dir 56 * command. 57 */ 58 static PID_T *pids; 59 static int fds; 60 61 FILE * 62 cron_popen(char *program, char *type, struct passwd *pw) { 63 char *cp; 64 FILE *iop; 65 int argc, pdes[2]; 66 PID_T pid; 67 char *argv[MAX_ARGV]; 68 69 if ((*type != 'r' && *type != 'w') || type[1] != '\0') 70 return (NULL); 71 72 if (!pids) { 73 if ((fds = sysconf(_SC_OPEN_MAX)) <= 0) 74 return (NULL); 75 if (!(pids = calloc(fds, sizeof(PID_T)))) 76 return (NULL); 77 } 78 if (pipe(pdes) < 0) 79 return (NULL); 80 81 /* break up string into pieces */ 82 for (argc = 0, cp = program; argc < MAX_ARGV - 1; cp = NULL) 83 if (!(argv[argc++] = strtok(cp, " \t\n"))) 84 break; 85 argv[MAX_ARGV-1] = NULL; 86 87 switch (pid = fork()) { 88 case -1: /* error */ 89 (void)close(pdes[0]); 90 (void)close(pdes[1]); 91 return (NULL); 92 /* NOTREACHED */ 93 case 0: /* child */ 94 if (pw) { 95 #ifdef LOGIN_CAP 96 if (setusercontext(0, pw, pw->pw_uid, LOGIN_SETALL) < 0) { 97 fprintf(stderr, 98 "setusercontext failed for %s\n", 99 pw->pw_name); 100 _exit(ERROR_EXIT); 101 } 102 #else 103 if (setgid(pw->pw_gid) < 0 || 104 initgroups(pw->pw_name, pw->pw_gid) < 0) { 105 fprintf(stderr, 106 "unable to set groups for %s\n", 107 pw->pw_name); 108 _exit(1); 109 } 110 #if (defined(BSD)) && (BSD >= 199103) 111 setlogin(pw->pw_name); 112 #endif /* BSD */ 113 if (setuid(pw->pw_uid)) { 114 fprintf(stderr, 115 "unable to set uid for %s\n", 116 pw->pw_name); 117 _exit(1); 118 } 119 #endif /* LOGIN_CAP */ 120 } 121 if (*type == 'r') { 122 if (pdes[1] != STDOUT) { 123 dup2(pdes[1], STDOUT); 124 (void)close(pdes[1]); 125 } 126 dup2(STDOUT, STDERR); /* stderr too! */ 127 (void)close(pdes[0]); 128 } else { 129 if (pdes[0] != STDIN) { 130 dup2(pdes[0], STDIN); 131 (void)close(pdes[0]); 132 } 133 (void)close(pdes[1]); 134 } 135 execvp(argv[0], argv); 136 _exit(1); 137 } 138 139 /* parent; assume fdopen can't fail... */ 140 if (*type == 'r') { 141 iop = fdopen(pdes[0], type); 142 (void)close(pdes[1]); 143 } else { 144 iop = fdopen(pdes[1], type); 145 (void)close(pdes[0]); 146 } 147 pids[fileno(iop)] = pid; 148 149 return (iop); 150 } 151 152 int 153 cron_pclose(FILE *iop) { 154 int fdes; 155 PID_T pid; 156 WAIT_T status; 157 sigset_t sigset, osigset; 158 159 /* 160 * pclose returns -1 if stream is not associated with a 161 * `popened' command, or, if already `pclosed'. 162 */ 163 if (pids == 0 || pids[fdes = fileno(iop)] == 0) 164 return (-1); 165 (void)fclose(iop); 166 sigemptyset(&sigset); 167 sigaddset(&sigset, SIGINT); 168 sigaddset(&sigset, SIGQUIT); 169 sigaddset(&sigset, SIGHUP); 170 sigprocmask(SIG_BLOCK, &sigset, &osigset); 171 while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR) 172 continue; 173 sigprocmask(SIG_SETMASK, &osigset, NULL); 174 pids[fdes] = 0; 175 if (pid < 0) 176 return (pid); 177 if (WIFEXITED(status)) 178 return (WEXITSTATUS(status)); 179 return (1); 180 } 181