2 * Copyright (C) 2005 Diomidis Spinellis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/syslog.h>
44 /* Usage conventions for the pipe's endpoints. */
48 static int errlog(void);
49 static int parse_argv(char *args, char **argv);
51 int portal_pipe(pcr, key, v, so, fdp)
52 struct portal_cred *pcr;
58 int fd[2]; /* Pipe endpoints. */
59 int caller_end; /* The pipe end we will use. */
60 int process_end; /* The pipe end the spawned process will use. */
61 int redirect_fd; /* The fd to redirect on the spawned process. */
62 char pbuf[MAXPATHLEN];
67 struct portal_cred save_area;
69 /* Validate open mode, and assign roles. */
70 if ((pcr->pcr_flag & FWRITE) && (pcr->pcr_flag & FREAD))
71 /* Don't allow both on a single fd. */
73 else if (pcr->pcr_flag & FREAD) {
75 * The caller reads from the pipe,
76 * the spawned process writes to it.
78 caller_end = READ_END;
79 process_end = WRITE_END;
80 redirect_fd = STDOUT_FILENO;
81 } else if (pcr->pcr_flag & FWRITE) {
83 * The caller writes to the pipe,
84 * the spawned process reads from it.
86 caller_end = WRITE_END;
87 process_end = READ_END;
88 redirect_fd = STDIN_FILENO;
92 /* Get and check command line. */
94 strcpy(pbuf+1, key + (v[1] ? strlen(v[1]) : 0));
95 argc = parse_argv(pbuf, NULL);
99 /* Swap priviledges. */
100 if (set_user_credentials(pcr, &save_area) < 0)
103 /* Redirect and spawn the specified process. */
104 fd[READ_END] = fd[WRITE_END] = -1;
113 default: /* Parent */
114 (void)close(fd[process_end]);
117 argv = (char **)malloc((argc + 1) * sizeof(char *));
120 "malloc: failed to get space for %d pointers",
124 parse_argv(pbuf, argv);
126 if (dup2(fd[process_end], redirect_fd) < 0) {
127 syslog(LOG_ERR, "dup2: %m");
130 (void)close(fd[caller_end]);
131 (void)close(fd[process_end]);
133 syslog(LOG_ERR, "errlog: %m");
136 if (execv(argv[0], argv) < 0) {
137 syslog(LOG_ERR, "execv(%s): %m", argv[0]);
144 /* Re-establish our priviledges. */
145 if (restore_credentials(&save_area) < 0)
148 /* Set return fd value. */
150 *fdp = fd[caller_end];
152 for (i = 0; i < 2; i++)
162 * Redirect stderr to the system log.
164 * Return -1 with errno set on error.
180 if ((f = fdopen(fd[READ_END], "r")) == NULL) {
181 syslog(LOG_ERR, "fdopen: %m");
184 (void)close(fd[WRITE_END]);
185 while (fgets(buff, sizeof(buff), f) != NULL)
186 syslog(LOG_ERR, "exec: %s", buff);
189 default: /* Parent */
190 if (dup2(fd[WRITE_END], STDERR_FILENO) < 0)
192 (void)close(fd[READ_END]);
193 (void)close(fd[WRITE_END]);
200 * Parse the args string as a space-separated argument vector.
201 * If argv is not NULL, split the string into its constituent
202 * components, and set argv to point to the beginning of each
203 * string component; NULL-terminating argv.
204 * Return the number of string components.
207 parse_argv(char *args, char **argv)
211 enum {WORD, SPACE} state = SPACE;
213 for (p = args; *p; p++)