1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/* popen.c secure popen: code mix from Richard Stevens and ntop source
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <sys/wait.h>
#include <limits.h>
#include <fcntl.h>
#include "ipband.h"
#define __SEC_POPEN_TOKEN " "
static pid_t *childpid = NULL; /* ptr to array allocated at run-time */
static int maxfd;
/* popen() substitute */
FILE * sec_popen(const char *cmd, const char *type) {
char **argv, *ptr, *strtokState;
char *cmdcpy = NULL;
int i, pfd[2];
pid_t pid;
FILE *fp;
if(cmd == NULL || cmd == "")
return(NULL);
if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
errno = EINVAL; /* required by POSIX.2 */
return(NULL);
}
if((cmdcpy = strdup(cmd)) == NULL)
return(NULL);
argv = NULL;
if((ptr = strtok_r(cmdcpy, __SEC_POPEN_TOKEN, &strtokState)) == NULL) {
free(cmdcpy);
return(NULL);
}
for(i = 0;; i++) {
if((argv = (char **)realloc(argv, (i+1) * sizeof(char*))) == NULL) {
free(cmdcpy);
return(NULL);
}
if((*(argv+i) = (char*)malloc((strlen(ptr)+1) * sizeof(char))) == NULL) {
free(cmdcpy);
return(NULL);
}
strcpy(argv[i], ptr);
if((ptr = strtok_r(NULL, __SEC_POPEN_TOKEN, &strtokState)) == NULL) {
if((argv = (char **) realloc(argv, (i+2) * sizeof(char*))) == NULL) {
free(cmdcpy);
return(NULL);
}
argv[i+1] = NULL;
break;
}
}
free(cmdcpy);
if (childpid == NULL) { /* first time through */
/* allocate zeroed out array for child pids */
maxfd = open_max();
if ( (childpid = calloc(maxfd, sizeof(pid_t))) == NULL)
return(NULL);
}
if (pipe(pfd) < 0)
return(NULL); /* errno set by pipe() */
if ( (pid = fork()) < 0)
return(NULL); /* errno set by fork() */
else if (pid == 0) { /* child */
if((getuid() != geteuid()) || (getgid() != getegid())) {
/* setuid binary, drop privileges */
if (setgid(getgid()) != 0 || setuid(getuid()) != 0)
err_sys("Error dropping privileges");
}
if (*type == 'r') {
close(pfd[0]);
if (pfd[1] != STDOUT_FILENO) {
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1]);
}
} else {
close(pfd[1]);
if (pfd[0] != STDIN_FILENO) {
dup2(pfd[0], STDIN_FILENO);
close(pfd[0]);
}
}
/* close all descriptors in childpid[] */
for (i = 0; i < maxfd; i++)
if (childpid[i] > 0)
close(i);
if(strchr(argv[0], '/') == NULL)
execvp(argv[0], argv); /* search in $PATH */
else
execv(argv[0], argv);
_exit(127);
} /* parent */
if (*type == 'r') {
close(pfd[1]);
if ( (fp = fdopen(pfd[0], type)) == NULL)
return(NULL);
} else {
close(pfd[0]);
if ( (fp = fdopen(pfd[1], type)) == NULL)
return(NULL);
}
childpid[fileno(fp)] = pid; /* remember child pid for this fd */
return(fp);
}
int sec_pclose(FILE *fp) {
int fd, stat;
pid_t pid;
if (childpid == NULL)
return(-1); /* popen() has never been called */
fd = fileno(fp);
if ( (pid = childpid[fd]) == 0)
return(-1); /* fp wasn't opened by popen() */
childpid[fd] = 0;
if (fclose(fp) == EOF)
return(-1);
while (waitpid(pid, &stat, 0) < 0)
if (errno != EINTR)
return(-1); /* error other than EINTR from waitpid() */
return(stat); /* return child's termination status */
}
/* Determine maximum number of open files */
#ifdef OPEN_MAX
static int openmax = OPEN_MAX;
#else
static int openmax = 0;
#endif
#define OPEN_MAX_GUESS 256 /* if OPEN_MAX is indeterminate */
/* we're not guaranteed this is adequate */
int open_max(void)
{
if (openmax == 0) { /* first time through */
errno = 0;
if ( (openmax = sysconf(_SC_OPEN_MAX)) < 0) {
if (errno == 0)
openmax = OPEN_MAX_GUESS; /* it's indeterminate */
else
err_sys("sysconf error for _SC_OPEN_MAX");
}
}
return(openmax);
}
|