comparison 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
comparison
equal deleted inserted replaced
9:31cc8a89cb74 10:26e34ae9a3e3
11 #include "peopen.h" 11 #include "peopen.h"
12 #include "sysexits.h" 12 #include "sysexits.h"
13 13
14 #include "masqmail.h" 14 #include "masqmail.h"
15 15
16 static 16 static void
17 void destroy_argv(char **arr) 17 destroy_argv(char **arr)
18 { 18 {
19 char *p = arr[0]; 19 char *p = arr[0];
20 int i = 0; 20 int i = 0;
21 21
22 while(p){ 22 while (p) {
23 free(p); 23 free(p);
24 p = arr[i++]; 24 p = arr[i++];
25 } 25 }
26 free(arr); 26 free(arr);
27 } 27 }
28 28
29 static 29 static char**
30 char **create_argv(const char *cmd, int count) 30 create_argv(const char *cmd, int count)
31 { 31 {
32 char buf[strlen(cmd)+1]; 32 char buf[strlen(cmd) + 1];
33 char **arr, *q; 33 char **arr, *q;
34 const char *p; 34 const char *p;
35 int i = 0; 35 int i = 0;
36 36
37 arr = (char **)malloc(sizeof(char *) * count); 37 arr = (char **) malloc(sizeof(char *) * count);
38
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;
49 38
50 return arr; 39 p = cmd;
40 while (*p && i < (count - 1)) {
41 while (*p && isspace(*p))
42 p++;
43 q = buf;
44 while (*p && !isspace(*p))
45 *q++ = *p++;
46 *q = 0;
47 arr[i++] = strdup(buf);
48 while (*p && isspace(*p))
49 p++;
50 }
51 arr[i] = NULL;
52
53 return arr;
51 } 54 }
52 55
53 FILE* peidopen(const char *command, 56 FILE*
54 const char *type, 57 peidopen(const char *command, const char *type, char *const envp[], int *ret_pid, uid_t uid, gid_t gid)
55 char *const envp [],
56 int *ret_pid,
57 uid_t uid, gid_t gid
58 )
59 { 58 {
60 enum { Read, Write } mode; 59 enum { Read, Write } mode;
61 int pipe_fd [2]; 60 int pipe_fd[2];
62 pid_t pid; 61 pid_t pid;
63
64 if (command == NULL || type == NULL) {
65 errno = EINVAL;
66 return NULL;
67 }
68 62
69 if (strcmp (type, "r")) { 63 if (command == NULL || type == NULL) {
70 if (strcmp (type, "w")) { 64 errno = EINVAL;
71 errno = EINVAL; 65 return NULL;
72 return NULL; 66 }
73 } else
74 mode = Write;
75 } else
76 mode = Read;
77 67
78 if (pipe (pipe_fd) == -1) 68 if (strcmp(type, "r")) {
79 return NULL; 69 if (strcmp(type, "w")) {
70 errno = EINVAL;
71 return NULL;
72 } else
73 mode = Write;
74 } else
75 mode = Read;
80 76
81 switch (pid = fork ()) { 77 if (pipe(pipe_fd) == -1)
82 case 0: /* child thread */ 78 return NULL;
83 79
84 { 80 switch (pid = fork()) {
85 int i, max_fd = sysconf(_SC_OPEN_MAX); 81 case 0: /* child thread */
86
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;
96 82
97 if(uid != (uid_t)-1){ 83 {
98 if((ret = seteuid(0)) != 0){ 84 int i, max_fd = sysconf(_SC_OPEN_MAX);
99 exit(EX_NOPERM); 85
86 if (max_fd <= 0)
87 max_fd = 64;
88 for (i = 0; i < max_fd; i++)
89 if ((i != pipe_fd[0]) && (i != pipe_fd[1]))
90 close(i);
91 }
92 if (close(pipe_fd[mode == Read ? 0 : 1]) != -1 &&
93 dup2(pipe_fd[mode == Read ? 1 : 0],
94 mode == Read ? STDOUT_FILENO : STDIN_FILENO) != -1) {
95 // char *argv [] = { "/bin/sh", "-c", (char*) command, NULL };
96 char **argv = create_argv(command, 10);
97 int ret;
98
99 if (uid != (uid_t) - 1) {
100 if ((ret = seteuid(0)) != 0) {
101 exit(EX_NOPERM);
102 }
103 }
104 if (gid != (gid_t) - 1) {
105 if ((ret = setgid(gid)) != 0) {
106 exit(EX_NOPERM);
107 }
108 }
109 if (uid != (uid_t) - 1) {
110 if ((ret = setuid(uid)) != 0) {
111 exit(EX_NOPERM);
112 }
113 }
114 execve(*argv, argv, envp);
115 }
116
117 _exit(errno);
118
119 default: /* parent thread */
120 *ret_pid = pid;
121 close(pipe_fd[mode == Read ? 1 : 0]);
122 return fdopen(pipe_fd[mode == Read ? 0 : 1], type);
123
124 case -1:
125 close(pipe_fd[0]);
126 close(pipe_fd[1]);
127 return NULL;
100 } 128 }
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 }
114
115 _exit (errno);
116
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);
121
122 case -1:
123 close (pipe_fd [0]);
124 close (pipe_fd [1]);
125 return NULL;
126 }
127 } 129 }
128 130
129 FILE* peopen(const char *command, 131 FILE*
130 const char *type, 132 peopen(const char *command, const char *type, char *const envp[], int *ret_pid)
131 char *const envp [],
132 int *ret_pid
133 )
134 { 133 {
135 return peidopen(command, type, envp, ret_pid, -1 ,-1); 134 return peidopen(command, type, envp, ret_pid, -1, -1);
136 } 135 }