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(struct portal_cred *pcr, char *key, char **v,
52 int kso __unused, int *fdp)
54 int fd[2]; /* Pipe endpoints. */
55 int caller_end; /* The pipe end we will use. */
56 int process_end; /* The pipe end the spawned process will use. */
57 int redirect_fd; /* The fd to redirect on the spawned process. */
58 char pbuf[MAXPATHLEN];
63 struct portal_cred save_area;
65 /* Validate open mode, and assign roles. */
66 if ((pcr->pcr_flag & FWRITE) && (pcr->pcr_flag & FREAD))
67 /* Don't allow both on a single fd. */
69 else if (pcr->pcr_flag & FREAD) {
71 * The caller reads from the pipe,
72 * the spawned process writes to it.
74 caller_end = READ_END;
75 process_end = WRITE_END;
76 redirect_fd = STDOUT_FILENO;
77 } else if (pcr->pcr_flag & FWRITE) {
79 * The caller writes to the pipe,
80 * the spawned process reads from it.
82 caller_end = WRITE_END;
83 process_end = READ_END;
84 redirect_fd = STDIN_FILENO;
88 /* Get and check command line. */
90 strcpy(pbuf+1, key + (v[1] ? strlen(v[1]) : 0));
91 argc = parse_argv(pbuf, NULL);
95 /* Swap priviledges. */
96 if (set_user_credentials(pcr, &save_area) < 0)
99 /* Redirect and spawn the specified process. */
100 fd[READ_END] = fd[WRITE_END] = -1;
109 default: /* Parent */
110 (void)close(fd[process_end]);
113 argv = (char **)malloc((argc + 1) * sizeof(char *));
116 "malloc: failed to get space for %d pointers",
120 parse_argv(pbuf, argv);
122 if (dup2(fd[process_end], redirect_fd) < 0) {
123 syslog(LOG_ERR, "dup2: %m");
126 (void)close(fd[caller_end]);
127 (void)close(fd[process_end]);
129 syslog(LOG_ERR, "errlog: %m");
132 if (execv(argv[0], argv) < 0) {
133 syslog(LOG_ERR, "execv(%s): %m", argv[0]);
140 /* Re-establish our priviledges. */
141 if (restore_credentials(&save_area) < 0)
144 /* Set return fd value. */
146 *fdp = fd[caller_end];
148 for (i = 0; i < 2; i++)
158 * Redirect stderr to the system log.
160 * Return -1 with errno set on error.
176 if ((f = fdopen(fd[READ_END], "r")) == NULL) {
177 syslog(LOG_ERR, "fdopen: %m");
180 (void)close(fd[WRITE_END]);
181 while (fgets(buff, sizeof(buff), f) != NULL)
182 syslog(LOG_ERR, "exec: %s", buff);
185 default: /* Parent */
186 if (dup2(fd[WRITE_END], STDERR_FILENO) < 0)
188 (void)close(fd[READ_END]);
189 (void)close(fd[WRITE_END]);
196 * Parse the args string as a space-separated argument vector.
197 * If argv is not NULL, split the string into its constituent
198 * components, and set argv to point to the beginning of each
199 * string component; NULL-terminating argv.
200 * Return the number of string components.
203 parse_argv(char *args, char **argv)
207 enum {WORD, SPACE} state = SPACE;
209 for (p = args; *p; p++)