Imported Upstream version 3.3.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     pid_t pid;
85     int i, inpipe[2], outpipe[2], errpipe[2], passwdpipe[2];
86     char number[NUM_STR_SIZE];
87     char **arg;
88     char *e;
89     char **env;
90     char *cmdline;
91     char *quoted;
92     char **newenv;
93     char *passwdvar = NULL;
94     int  *passwdfd = NULL;
95
96     /*
97      * Log the command line and count the args.
98      */
99     if ((pipedef & PASSWD_PIPE) != 0) {
100         passwdvar = *my_argv++;
101         passwdfd  = (int *)*my_argv++;
102     }
103     memset(inpipe, -1, SIZEOF(inpipe));
104     memset(outpipe, -1, SIZEOF(outpipe));
105     memset(errpipe, -1, SIZEOF(errpipe));
106     memset(passwdpipe, -1, SIZEOF(passwdpipe));
107
108     cmdline = stralloc(prog);
109     for(arg = my_argv; *arg != NULL; arg++) {
110         if (*arg != skip_argument) {
111             quoted = quote_string(*arg);
112             cmdline = vstrextend(&cmdline, " ", quoted, NULL);
113             amfree(quoted);
114         }
115     }
116     dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
117
118     /*
119      * Create the pipes
120      */
121     if ((pipedef & STDIN_PIPE) != 0) {
122         if(pipe(inpipe) == -1) {
123             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
124             /*NOTREACHED*/
125         }
126     }
127     if ((pipedef & STDOUT_PIPE) != 0) {
128         if(pipe(outpipe) == -1) {
129             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
130             /*NOTREACHED*/
131         }
132     }
133     if ((pipedef & STDERR_PIPE) != 0) {
134         if(pipe(errpipe) == -1) {
135             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
136             /*NOTREACHED*/
137         }
138     }
139     if ((pipedef & PASSWD_PIPE) != 0) {
140         if(pipe(passwdpipe) == -1) {
141             error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
142             /*NOTREACHED*/
143         }
144     }
145
146     /*
147      * Fork and set up the return or run the program.
148      */
149     switch(pid = fork()) {
150     case -1:
151         e = strerror(errno);
152         error(_("error [fork %s: %s]"), prog, e);
153         /*NOTREACHED*/
154
155     default:    /* parent process */
156         if ((pipedef & STDIN_PIPE) != 0) {
157             aclose(inpipe[0]);          /* close input side of pipe */
158             *stdinfd = inpipe[1];
159         }
160         if ((pipedef & STDOUT_PIPE) != 0) {
161             aclose(outpipe[1]);         /* close output side of pipe */
162             *stdoutfd = outpipe[0];
163         }
164         if ((pipedef & STDERR_PIPE) != 0) {
165             aclose(errpipe[1]);         /* close output side of pipe */
166             *stderrfd = errpipe[0];
167         }
168         if ((pipedef & PASSWD_PIPE) != 0) {
169             aclose(passwdpipe[0]);      /* close input side of pipe */
170             *passwdfd = passwdpipe[1];
171         }
172         break;
173     case 0:             /* child process */
174         debug_dup_stderr_to_debug();
175         if ((pipedef & STDIN_PIPE) != 0) {
176             aclose(inpipe[1]);          /* close output side of pipe */
177         } else {
178             inpipe[0] = *stdinfd;
179         }
180         if ((pipedef & STDOUT_PIPE) != 0) {
181             aclose(outpipe[0]);         /* close input side of pipe */
182         } else {
183             outpipe[1] = *stdoutfd;
184         }
185         if ((pipedef & STDERR_PIPE) != 0) {
186             aclose(errpipe[0]);         /* close input side of pipe */
187         } else {
188             errpipe[1] = *stderrfd;
189         }
190         if ((pipedef & PASSWD_PIPE) != 0) { 
191             aclose(passwdpipe[1]);      /* close output side of pipe */
192         }
193
194         /*
195          * Shift the pipes to the standard file descriptors as requested.
196          */
197         if(dup2(inpipe[0], 0) == -1) {
198             g_fprintf(stderr, "error [spawn %s: dup2 in: %s]", prog, strerror(errno));
199             exit(1);
200             /*NOTREACHED*/
201         }
202         if(dup2(outpipe[1], 1) == -1) {
203             g_fprintf(stderr, "error [spawn %s: dup2 out: %s]", prog, strerror(errno));
204             exit(1);
205             /*NOTREACHED*/
206         }
207         if(dup2(errpipe[1], 2) == -1) {
208             g_fprintf(stderr, "error [spawn %s: dup2 err: %s]", prog, strerror(errno));
209             exit(1);
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         } else {
237             /* if our real userid is zero, the child shouldn't inherit
238              * that, so drop privs permanently */
239             if (getuid() == 0 && !set_root_privs(-1)) {
240                 error(_("could not drop root privileges"));
241             }
242         }
243
244         execve(prog, my_argv, env);
245         e = strerror(errno);
246         error(_("error [exec %s: %s]"), prog, e);
247         /*NOTREACHED*/
248     }
249     amfree(cmdline);
250     return pid;
251 }