Imported Upstream version 2.6.1
[debian/amanda] / common-src / pipespawn.c
1 #include "amanda.h"
2 #include "pipespawn.h"
3 #include "arglist.h"
4 #include "clock.h"
5 #include "util.h"
6
7 char skip_argument[1];
8
9 pid_t pipespawnv_passwd(char *prog, int pipedef, int need_root,
10                   int *stdinfd, int *stdoutfd, int *stderrfd,
11                   char **my_argv);
12
13
14 /*
15  * this used to be a function in it's own write but became a wrapper around
16  * pipespawnv to eliminate redundancy...
17  */
18 pid_t
19 pipespawn(
20     char *      prog,
21     int         pipedef,
22     int         need_root,
23     int *       stdinfd,
24     int *       stdoutfd,
25     int *       stderrfd,
26     ...)
27 {
28     va_list ap;
29     int argc = 0, i;
30     pid_t pid;
31     char **argv;
32
33     /* count args */
34     arglist_start(ap, stderrfd);
35     while(arglist_val(ap, char *) != NULL) {
36         argc++;
37     }
38     arglist_end(ap);
39
40     /*
41      * Create the argument vector.
42      */
43     arglist_start(ap, stderrfd);
44     argv = (char **)alloc((argc + 1) * SIZEOF(*argv));
45     i = 0;
46     while((argv[i] = arglist_val(ap, char *)) != NULL) {
47         if (argv[i] != skip_argument) {
48             i++;
49         }
50     }
51     arglist_end(ap);
52
53     pid = pipespawnv_passwd(prog, pipedef, need_root,
54                             stdinfd, stdoutfd, stderrfd, argv);
55     amfree(argv);
56     return pid;
57 }
58
59 pid_t
60 pipespawnv(
61     char *      prog,
62     int         pipedef,
63     int         need_root,
64     int *       stdinfd,
65     int *       stdoutfd,
66     int *       stderrfd,
67     char **     my_argv)
68 {
69     return pipespawnv_passwd(prog, pipedef, need_root,
70                              stdinfd, stdoutfd, stderrfd,
71         my_argv);
72 }
73
74 pid_t
75 pipespawnv_passwd(
76     char *      prog,
77     int         pipedef,
78     int         need_root,
79     int *       stdinfd,
80     int *       stdoutfd,
81     int *       stderrfd,
82     char **     my_argv)
83 {
84     int argc;
85     pid_t pid;
86     int i, inpipe[2], outpipe[2], errpipe[2], passwdpipe[2];
87     char number[NUM_STR_SIZE];
88     char **arg;
89     char *e;
90     char **env;
91     char *cmdline;
92     char *quoted;
93     char **newenv;
94     char *passwdvar = NULL;
95     int  *passwdfd = NULL;
96
97     /*
98      * Log the command line and count the args.
99      */
100     if ((pipedef & PASSWD_PIPE) != 0) {
101         passwdvar = *my_argv++;
102         passwdfd  = (int *)*my_argv++;
103     }
104     g_debug("pipespawnv: stdoutfd is %d", *stdoutfd);
105     memset(inpipe, -1, SIZEOF(inpipe));
106     memset(outpipe, -1, SIZEOF(outpipe));
107     memset(errpipe, -1, SIZEOF(errpipe));
108     memset(passwdpipe, -1, SIZEOF(passwdpipe));
109     argc = 0;
110
111     cmdline = stralloc(prog);
112     for(arg = my_argv; *arg != NULL; arg++) {
113         if (*arg != skip_argument) {
114             argc++;
115             quoted = quote_string(*arg);
116             cmdline = vstrextend(&cmdline, " ", quoted, NULL);
117             amfree(quoted);
118         }
119     }
120     dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
121
122     /*
123      * Create the pipes
124      */
125     if ((pipedef & STDIN_PIPE) != 0) {
126         if(pipe(inpipe) == -1) {
127             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
128             /*NOTREACHED*/
129         }
130     }
131     if ((pipedef & STDOUT_PIPE) != 0) {
132         if(pipe(outpipe) == -1) {
133             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
134             /*NOTREACHED*/
135         }
136     }
137     if ((pipedef & STDERR_PIPE) != 0) {
138         if(pipe(errpipe) == -1) {
139             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
140             /*NOTREACHED*/
141         }
142     }
143     if ((pipedef & PASSWD_PIPE) != 0) {
144         if(pipe(passwdpipe) == -1) {
145             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
146             /*NOTREACHED*/
147         }
148     }
149
150     /*
151      * Fork and set up the return or run the program.
152      */
153     switch(pid = fork()) {
154     case -1:
155         e = strerror(errno);
156         error(_("error [fork %s: %s]"), prog, e);
157         /*NOTREACHED*/
158
159     default:    /* parent process */
160         if ((pipedef & STDIN_PIPE) != 0) {
161             aclose(inpipe[0]);          /* close input side of pipe */
162             *stdinfd = inpipe[1];
163         }
164         if ((pipedef & STDOUT_PIPE) != 0) {
165             aclose(outpipe[1]);         /* close output side of pipe */
166             *stdoutfd = outpipe[0];
167         }
168         if ((pipedef & STDERR_PIPE) != 0) {
169             aclose(errpipe[1]);         /* close output side of pipe */
170             *stderrfd = errpipe[0];
171         }
172         if ((pipedef & PASSWD_PIPE) != 0) {
173             aclose(passwdpipe[0]);      /* close input side of pipe */
174             *passwdfd = passwdpipe[1];
175         }
176         break;
177     case 0:             /* child process */
178         if ((pipedef & STDIN_PIPE) != 0) {
179             aclose(inpipe[1]);          /* close output side of pipe */
180         } else {
181             inpipe[0] = *stdinfd;
182         }
183         if ((pipedef & STDOUT_PIPE) != 0) {
184             aclose(outpipe[0]);         /* close input side of pipe */
185         } else {
186             outpipe[1] = *stdoutfd;
187         }
188         if ((pipedef & STDERR_PIPE) != 0) {
189             aclose(errpipe[0]);         /* close input side of pipe */
190         } else {
191             errpipe[1] = *stderrfd;
192         }
193         if ((pipedef & PASSWD_PIPE) != 0) { 
194             aclose(passwdpipe[1]);      /* close output side of pipe */
195         }
196
197         /*
198          * Shift the pipes to the standard file descriptors as requested.
199          */
200         if(dup2(inpipe[0], 0) == -1) {
201             error(_("error [spawn %s: dup2 in: %s]"), prog, strerror(errno));
202             /*NOTREACHED*/
203         }
204         if(dup2(outpipe[1], 1) == -1) {
205             error(_("error [spawn %s: dup2 out: %s]"), prog, strerror(errno));
206             /*NOTREACHED*/
207         }
208         if(dup2(errpipe[1], 2) == -1) {
209             error(_("error [spawn %s: dup2 err: %s]"), prog, strerror(errno));
210             /*NOTREACHED*/
211         }
212
213         /*
214          * Get the "safe" environment.  If we are sending a password to
215          * the child via a pipe, add the environment variable for that.
216          */
217         env = safe_env();
218         if ((pipedef & PASSWD_PIPE) != 0) {
219             for (i = 0; env[i] != NULL; i++)
220                 (void)i; /* make lint happy and do nothing */   
221             newenv = (char **)alloc((i + 1 + 1) * SIZEOF(*newenv));
222             g_snprintf(number, SIZEOF(number), "%d", passwdpipe[0]);
223             newenv[0] = vstralloc(passwdvar, "=", number, NULL);
224             for(i = 0; env[i] != NULL; i++)
225                 newenv[i + 1] = env[i];
226             newenv[i + 1] = NULL;
227             amfree(env);
228             env = newenv;
229             safe_fd(passwdpipe[0], 1);
230         } else {
231             safe_fd(-1, 0);
232         }
233
234         if (need_root)
235             become_root();
236         execve(prog, my_argv, env);
237         e = strerror(errno);
238         error(_("error [exec %s: %s]"), prog, e);
239         /*NOTREACHED*/
240     }
241     amfree(cmdline);
242     return pid;
243 }