masqmail-0.2

view src/peopen.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line source
1 /* This a snippet I found in sourceforge. I just changed the identing
2 style to my own and deleted the main function. */
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <sys/types.h>
11 #include "peopen.h"
12 #include "sysexits.h"
14 #include "masqmail.h"
16 static
17 void destroy_argv(char **arr)
18 {
19 char *p = arr[0];
20 int i = 0;
22 while(p){
23 free(p);
24 p = arr[i++];
25 }
26 free(arr);
27 }
29 static
30 char **create_argv(const char *cmd, int count)
31 {
32 char buf[strlen(cmd)+1];
33 char **arr, *q;
34 const char *p;
35 int i = 0;
37 arr = (char **)malloc(sizeof(char *) * count);
39 p = cmd;
40 while(*p && i < (count-1)){
41 while(*p && isspace(*p)) p++;
42 q = buf;
43 while(*p && !isspace(*p)) *q++ = *p++;
44 *q = 0;
45 arr[i++] = strdup(buf);
46 while(*p && isspace(*p)) p++;
47 }
48 arr[i] = NULL;
50 return arr;
51 }
53 FILE* peidopen(const char *command,
54 const char *type,
55 char *const envp [],
56 int *ret_pid,
57 uid_t uid, gid_t gid
58 )
59 {
60 enum { Read, Write } mode;
61 int pipe_fd [2];
62 pid_t pid;
64 if (command == NULL || type == NULL) {
65 errno = EINVAL;
66 return NULL;
67 }
69 if (strcmp (type, "r")) {
70 if (strcmp (type, "w")) {
71 errno = EINVAL;
72 return NULL;
73 } else
74 mode = Write;
75 } else
76 mode = Read;
78 if (pipe (pipe_fd) == -1)
79 return NULL;
81 switch (pid = fork ()) {
82 case 0: /* child thread */
84 {
85 int i, max_fd = sysconf(_SC_OPEN_MAX);
87 if(max_fd <= 0) max_fd = 64;
88 for(i = 0; i < max_fd; i++)
89 if((i != pipe_fd[0]) && (i != pipe_fd[1])) close(i);
90 }
91 if (close (pipe_fd [mode == Read ? 0 : 1]) != -1 &&
92 dup2 (pipe_fd [mode == Read ? 1 : 0], mode == Read ? STDOUT_FILENO : STDIN_FILENO) != -1) {
93 // char *argv [] = { "/bin/sh", "-c", (char*) command, NULL };
94 char **argv = create_argv(command, 10);
95 int ret;
97 if(uid != (uid_t)-1){
98 if((ret = seteuid(0)) != 0){
99 exit(EX_NOPERM);
100 }
101 }
102 if(gid != (gid_t)-1){
103 if((ret = setgid(gid)) != 0){
104 exit(EX_NOPERM);
105 }
106 }
107 if(uid != (uid_t)-1){
108 if((ret = setuid(uid)) != 0){
109 exit(EX_NOPERM);
110 }
111 }
112 execve (*argv, argv, envp);
113 }
115 _exit (errno);
117 default: /* parent thread */
118 *ret_pid = pid;
119 close (pipe_fd [mode == Read ? 1 : 0]);
120 return fdopen (pipe_fd [mode == Read ? 0 : 1], type);
122 case -1:
123 close (pipe_fd [0]);
124 close (pipe_fd [1]);
125 return NULL;
126 }
127 }
129 FILE* peopen(const char *command,
130 const char *type,
131 char *const envp [],
132 int *ret_pid
133 )
134 {
135 return peidopen(command, type, envp, ret_pid, -1 ,-1);
136 }