masqmail
view src/peopen.c @ 264:1e5e457dea18
comments and a small refactoring
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Fri, 03 Dec 2010 10:43:38 -0300 |
parents | 7cdd30429cc9 |
children | b27f66555ba8 |
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. -- oku
3 The functions destroy_argv() and create_argv() were added by oku.
4 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sysexits.h>
14 #include "peopen.h"
15 #include "masqmail.h"
17 static void
18 destroy_argv(char **arr)
19 {
20 char *p = arr[0];
21 int i = 0;
23 while (p) {
24 free(p);
25 p = arr[i++];
26 }
27 free(arr);
28 }
30 static char**
31 create_argv(const char *cmd, int count)
32 {
33 char buf[strlen(cmd) + 1];
34 char **arr, *q;
35 const char *p;
36 int i = 0;
38 arr = (char **) g_malloc(sizeof(char *) * count);
40 p = cmd;
41 while (*p && i < (count - 1)) {
42 while (*p && isspace(*p))
43 p++;
44 q = buf;
45 while (*p && !isspace(*p))
46 *q++ = *p++;
47 *q = '\0';
48 arr[i++] = strdup(buf);
49 while (*p && isspace(*p))
50 p++;
51 }
52 arr[i] = NULL;
54 return arr;
55 }
57 FILE*
58 peidopen(const char *command, const char *type, char *const envp[], int *ret_pid, uid_t uid, gid_t gid)
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)
88 max_fd = 64;
89 for (i = 0; i < max_fd; i++)
90 if ((i != pipe_fd[0]) && (i != pipe_fd[1]))
91 close(i);
92 }
93 if (close(pipe_fd[mode == Read ? 0 : 1]) != -1 &&
94 dup2(pipe_fd[mode == Read ? 1 : 0],
95 mode == Read ? STDOUT_FILENO : STDIN_FILENO) != -1) {
96 /* char *argv [] = { "/bin/sh", "-c", (char*) command, NULL }; */
97 char **argv = create_argv(command, 10);
98 int ret;
100 if (uid != (uid_t) - 1) {
101 if ((ret = seteuid(0)) != 0) {
102 exit(EX_NOPERM);
103 }
104 }
105 if (gid != (gid_t) - 1) {
106 if ((ret = setgid(gid)) != 0) {
107 exit(EX_NOPERM);
108 }
109 }
110 if (uid != (uid_t) - 1) {
111 if ((ret = setuid(uid)) != 0) {
112 exit(EX_NOPERM);
113 }
114 }
115 execve(*argv, argv, envp);
116 }
118 _exit(errno);
120 default: /* parent thread */
121 *ret_pid = pid;
122 close(pipe_fd[mode == Read ? 1 : 0]);
123 return fdopen(pipe_fd[mode == Read ? 0 : 1], type);
125 case -1:
126 close(pipe_fd[0]);
127 close(pipe_fd[1]);
128 return NULL;
129 }
130 }
132 FILE*
133 peopen(const char *command, const char *type, char *const envp[], int *ret_pid)
134 {
135 return peidopen(command, type, envp, ret_pid, -1, -1);
136 }