masqmail

diff src/peopen.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
line diff
     1.1 --- a/src/peopen.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/peopen.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -13,124 +13,123 @@
     1.4  
     1.5  #include "masqmail.h"
     1.6  
     1.7 -static
     1.8 -void destroy_argv(char **arr)
     1.9 +static void
    1.10 +destroy_argv(char **arr)
    1.11  {
    1.12 -  char *p = arr[0];
    1.13 -  int i = 0;
    1.14 +	char *p = arr[0];
    1.15 +	int i = 0;
    1.16  
    1.17 -  while(p){
    1.18 -    free(p);
    1.19 -    p = arr[i++];
    1.20 -  }
    1.21 -  free(arr);
    1.22 +	while (p) {
    1.23 +		free(p);
    1.24 +		p = arr[i++];
    1.25 +	}
    1.26 +	free(arr);
    1.27  }
    1.28  
    1.29 -static
    1.30 -char **create_argv(const char *cmd, int count)
    1.31 +static char**
    1.32 +create_argv(const char *cmd, int count)
    1.33  {
    1.34 -  char buf[strlen(cmd)+1];
    1.35 -  char **arr, *q;
    1.36 -  const char *p;
    1.37 -  int i = 0;
    1.38 +	char buf[strlen(cmd) + 1];
    1.39 +	char **arr, *q;
    1.40 +	const char *p;
    1.41 +	int i = 0;
    1.42  
    1.43 -  arr = (char **)malloc(sizeof(char *) * count);
    1.44 -  
    1.45 -  p = cmd;
    1.46 -  while(*p && i < (count-1)){
    1.47 -    while(*p && isspace(*p)) p++;
    1.48 -    q = buf;
    1.49 -    while(*p && !isspace(*p)) *q++ = *p++;
    1.50 -    *q = 0;
    1.51 -    arr[i++] = strdup(buf);
    1.52 -    while(*p && isspace(*p)) p++;
    1.53 -  }
    1.54 -  arr[i] = NULL;
    1.55 +	arr = (char **) malloc(sizeof(char *) * count);
    1.56  
    1.57 -  return arr;
    1.58 +	p = cmd;
    1.59 +	while (*p && i < (count - 1)) {
    1.60 +		while (*p && isspace(*p))
    1.61 +			p++;
    1.62 +		q = buf;
    1.63 +		while (*p && !isspace(*p))
    1.64 +			*q++ = *p++;
    1.65 +		*q = 0;
    1.66 +		arr[i++] = strdup(buf);
    1.67 +		while (*p && isspace(*p))
    1.68 +			p++;
    1.69 +	}
    1.70 +	arr[i] = NULL;
    1.71 +
    1.72 +	return arr;
    1.73  }
    1.74  
    1.75 -FILE* peidopen(const char	*command,
    1.76 -	       const char	*type,
    1.77 -	       char *const envp [],
    1.78 -	       int *ret_pid,
    1.79 -	       uid_t uid, gid_t gid
    1.80 -	     )
    1.81 +FILE*
    1.82 +peidopen(const char *command, const char *type, char *const envp[], int *ret_pid, uid_t uid, gid_t gid)
    1.83  {
    1.84 -  enum { Read, Write } mode;
    1.85 -  int pipe_fd [2];
    1.86 -  pid_t pid;
    1.87 -    
    1.88 -  if (command == NULL || type == NULL) {
    1.89 -    errno = EINVAL;
    1.90 -    return NULL;
    1.91 -  }
    1.92 +	enum { Read, Write } mode;
    1.93 +	int pipe_fd[2];
    1.94 +	pid_t pid;
    1.95  
    1.96 -  if (strcmp (type, "r")) {
    1.97 -    if (strcmp (type, "w")) {
    1.98 -      errno = EINVAL;
    1.99 -      return NULL;
   1.100 -    } else
   1.101 -      mode = Write;
   1.102 -  } else
   1.103 -    mode = Read;
   1.104 +	if (command == NULL || type == NULL) {
   1.105 +		errno = EINVAL;
   1.106 +		return NULL;
   1.107 +	}
   1.108  
   1.109 -  if (pipe (pipe_fd) == -1)
   1.110 -    return NULL;
   1.111 +	if (strcmp(type, "r")) {
   1.112 +		if (strcmp(type, "w")) {
   1.113 +			errno = EINVAL;
   1.114 +			return NULL;
   1.115 +		} else
   1.116 +			mode = Write;
   1.117 +	} else
   1.118 +		mode = Read;
   1.119  
   1.120 -  switch (pid = fork ()) {
   1.121 -  case 0: /* child thread */
   1.122 +	if (pipe(pipe_fd) == -1)
   1.123 +		return NULL;
   1.124  
   1.125 -    {
   1.126 -      int i, max_fd = sysconf(_SC_OPEN_MAX);
   1.127 -      
   1.128 -      if(max_fd <= 0) max_fd = 64;
   1.129 -      for(i = 0; i < max_fd; i++)
   1.130 -	if((i != pipe_fd[0]) && (i != pipe_fd[1])) close(i);
   1.131 -    }
   1.132 -    if (close (pipe_fd [mode == Read ? 0 : 1]) != -1 &&
   1.133 -	dup2 (pipe_fd [mode == Read ? 1 : 0], mode == Read ? STDOUT_FILENO : STDIN_FILENO) != -1) {
   1.134 -      //      char *argv [] = { "/bin/sh", "-c", (char*) command, NULL };
   1.135 -      char **argv = create_argv(command, 10);
   1.136 -      int ret;
   1.137 +	switch (pid = fork()) {
   1.138 +	case 0:  /* child thread */
   1.139  
   1.140 -      if(uid != (uid_t)-1){
   1.141 -	if((ret = seteuid(0)) != 0){
   1.142 -	  exit(EX_NOPERM);
   1.143 +		{
   1.144 +			int i, max_fd = sysconf(_SC_OPEN_MAX);
   1.145 +
   1.146 +			if (max_fd <= 0)
   1.147 +				max_fd = 64;
   1.148 +			for (i = 0; i < max_fd; i++)
   1.149 +				if ((i != pipe_fd[0]) && (i != pipe_fd[1]))
   1.150 +					close(i);
   1.151 +		}
   1.152 +		if (close(pipe_fd[mode == Read ? 0 : 1]) != -1 &&
   1.153 +			dup2(pipe_fd[mode == Read ? 1 : 0],
   1.154 +				 mode == Read ? STDOUT_FILENO : STDIN_FILENO) != -1) {
   1.155 +			//      char *argv [] = { "/bin/sh", "-c", (char*) command, NULL };
   1.156 +			char **argv = create_argv(command, 10);
   1.157 +			int ret;
   1.158 +
   1.159 +			if (uid != (uid_t) - 1) {
   1.160 +				if ((ret = seteuid(0)) != 0) {
   1.161 +					exit(EX_NOPERM);
   1.162 +				}
   1.163 +			}
   1.164 +			if (gid != (gid_t) - 1) {
   1.165 +				if ((ret = setgid(gid)) != 0) {
   1.166 +					exit(EX_NOPERM);
   1.167 +				}
   1.168 +			}
   1.169 +			if (uid != (uid_t) - 1) {
   1.170 +				if ((ret = setuid(uid)) != 0) {
   1.171 +					exit(EX_NOPERM);
   1.172 +				}
   1.173 +			}
   1.174 +			execve(*argv, argv, envp);
   1.175 +		}
   1.176 +
   1.177 +		_exit(errno);
   1.178 +
   1.179 +	default:  /* parent thread */
   1.180 +		*ret_pid = pid;
   1.181 +		close(pipe_fd[mode == Read ? 1 : 0]);
   1.182 +		return fdopen(pipe_fd[mode == Read ? 0 : 1], type);
   1.183 +
   1.184 +	case -1:
   1.185 +		close(pipe_fd[0]);
   1.186 +		close(pipe_fd[1]);
   1.187 +		return NULL;
   1.188  	}
   1.189 -      }
   1.190 -      if(gid != (gid_t)-1){
   1.191 -	if((ret = setgid(gid)) != 0){
   1.192 -	  exit(EX_NOPERM);
   1.193 -	}
   1.194 -      }
   1.195 -      if(uid != (uid_t)-1){
   1.196 -	if((ret = setuid(uid)) != 0){
   1.197 -	  exit(EX_NOPERM);
   1.198 -	}
   1.199 -      }
   1.200 -      execve (*argv, argv, envp);
   1.201 -    }
   1.202 -	    
   1.203 -    _exit (errno);
   1.204 -	    
   1.205 -  default: /* parent thread */
   1.206 -    *ret_pid = pid;
   1.207 -    close (pipe_fd [mode == Read ? 1 : 0]);
   1.208 -    return fdopen (pipe_fd [mode == Read ? 0 : 1], type);
   1.209 -	    
   1.210 -  case -1:
   1.211 -    close (pipe_fd [0]);
   1.212 -    close (pipe_fd [1]);
   1.213 -    return NULL;
   1.214 -  }
   1.215  }
   1.216  
   1.217 -FILE* peopen(const char	*command,
   1.218 -	     const char	*type,
   1.219 -	     char *const envp [],
   1.220 -	     int *ret_pid
   1.221 -	     )
   1.222 +FILE*
   1.223 +peopen(const char *command, const char *type, char *const envp[], int *ret_pid)
   1.224  {
   1.225 -  return peidopen(command, type, envp, ret_pid, -1 ,-1);
   1.226 +	return peidopen(command, type, envp, ret_pid, -1, -1);
   1.227  }