9 pid_t pipespawnv_passwd(char *prog, int pipedef, int need_root,
10 int *stdinfd, int *stdoutfd, int *stderrfd,
15 * this used to be a function in it's own write but became a wrapper around
16 * pipespawnv to eliminate redundancy...
34 arglist_start(ap, stderrfd);
35 while(arglist_val(ap, char *) != NULL) {
41 * Create the argument vector.
43 arglist_start(ap, stderrfd);
44 argv = (char **)alloc((argc + 1) * SIZEOF(*argv));
46 while((argv[i] = arglist_val(ap, char *)) != NULL) {
47 if (argv[i] != skip_argument) {
53 pid = pipespawnv_passwd(prog, pipedef, need_root,
54 stdinfd, stdoutfd, stderrfd, argv);
69 return pipespawnv_passwd(prog, pipedef, need_root,
70 stdinfd, stdoutfd, stderrfd,
86 int i, inpipe[2], outpipe[2], errpipe[2], passwdpipe[2];
87 char number[NUM_STR_SIZE];
94 char *passwdvar = NULL;
98 * Log the command line and count the args.
100 if ((pipedef & PASSWD_PIPE) != 0) {
101 passwdvar = *my_argv++;
102 passwdfd = (int *)*my_argv++;
104 memset(inpipe, -1, SIZEOF(inpipe));
105 memset(outpipe, -1, SIZEOF(outpipe));
106 memset(errpipe, -1, SIZEOF(errpipe));
107 memset(passwdpipe, -1, SIZEOF(passwdpipe));
110 cmdline = stralloc(prog);
111 for(arg = my_argv; *arg != NULL; arg++) {
112 if (*arg != skip_argument) {
114 quoted = quote_string(*arg);
115 cmdline = vstrextend(&cmdline, " ", quoted, NULL);
119 dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
124 if ((pipedef & STDIN_PIPE) != 0) {
125 if(pipe(inpipe) == -1) {
126 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
130 if ((pipedef & STDOUT_PIPE) != 0) {
131 if(pipe(outpipe) == -1) {
132 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
136 if ((pipedef & STDERR_PIPE) != 0) {
137 if(pipe(errpipe) == -1) {
138 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
142 if ((pipedef & PASSWD_PIPE) != 0) {
143 if(pipe(passwdpipe) == -1) {
144 error(_("error [open pipe to %s: %s]"), prog, strerror(errno));
150 * Fork and set up the return or run the program.
152 switch(pid = fork()) {
155 error(_("error [fork %s: %s]"), prog, e);
158 default: /* parent process */
159 if ((pipedef & STDIN_PIPE) != 0) {
160 aclose(inpipe[0]); /* close input side of pipe */
161 *stdinfd = inpipe[1];
163 if ((pipedef & STDOUT_PIPE) != 0) {
164 aclose(outpipe[1]); /* close output side of pipe */
165 *stdoutfd = outpipe[0];
167 if ((pipedef & STDERR_PIPE) != 0) {
168 aclose(errpipe[1]); /* close output side of pipe */
169 *stderrfd = errpipe[0];
171 if ((pipedef & PASSWD_PIPE) != 0) {
172 aclose(passwdpipe[0]); /* close input side of pipe */
173 *passwdfd = passwdpipe[1];
176 case 0: /* child process */
177 debug_dup_stderr_to_debug();
178 if ((pipedef & STDIN_PIPE) != 0) {
179 aclose(inpipe[1]); /* close output side of pipe */
181 inpipe[0] = *stdinfd;
183 if ((pipedef & STDOUT_PIPE) != 0) {
184 aclose(outpipe[0]); /* close input side of pipe */
186 outpipe[1] = *stdoutfd;
188 if ((pipedef & STDERR_PIPE) != 0) {
189 aclose(errpipe[0]); /* close input side of pipe */
191 errpipe[1] = *stderrfd;
193 if ((pipedef & PASSWD_PIPE) != 0) {
194 aclose(passwdpipe[1]); /* close output side of pipe */
198 * Shift the pipes to the standard file descriptors as requested.
200 if(dup2(inpipe[0], 0) == -1) {
201 g_fprintf(stderr, "error [spawn %s: dup2 in: %s]", prog, strerror(errno));
205 if(dup2(outpipe[1], 1) == -1) {
206 g_fprintf(stderr, "error [spawn %s: dup2 out: %s]", prog, strerror(errno));
210 if(dup2(errpipe[1], 2) == -1) {
211 g_fprintf(stderr, "error [spawn %s: dup2 err: %s]", prog, strerror(errno));
217 * Get the "safe" environment. If we are sending a password to
218 * the child via a pipe, add the environment variable for that.
221 if ((pipedef & PASSWD_PIPE) != 0) {
222 for (i = 0; env[i] != NULL; i++)
223 (void)i; /* make lint happy and do nothing */
224 newenv = (char **)alloc((i + 1 + 1) * SIZEOF(*newenv));
225 g_snprintf(number, SIZEOF(number), "%d", passwdpipe[0]);
226 newenv[0] = vstralloc(passwdvar, "=", number, NULL);
227 for(i = 0; env[i] != NULL; i++)
228 newenv[i + 1] = env[i];
229 newenv[i + 1] = NULL;
232 safe_fd(passwdpipe[0], 1);
240 /* if our real userid is zero, the child shouldn't inherit
241 * that, so drop privs permanently */
242 if (getuid() == 0 && !set_root_privs(-1)) {
243 error(_("could not drop root privileges"));
247 execve(prog, my_argv, env);
249 error(_("error [exec %s: %s]"), prog, e);