2 * exec.c Execute external programs.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Michael J. Hartwick <hartwick@hartwick.com>
23 static const char rcsid[] = "$Id$";
36 # include <sys/wait.h>
39 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
42 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
46 #include "rad_assert.h"
48 #ifndef HAVE_PTHREAD_H
49 #define rad_fork(n) fork()
50 #define rad_waitpid waitpid
54 * Execute a program on successful authentication.
55 * Return 0 if exec_wait == 0.
56 * Return the exit code of the called program if exec_wait != 0.
57 * Return -1 on fork/other errors in the parent process.
59 int radius_exec_program(const char *cmd, REQUEST *request,
60 int exec_wait, const char **user_msg)
63 static char message[256];
75 * Open a pipe for child/parent communication, if
80 radlog(L_ERR|L_CONS, "Couldn't open pipe: %s",
87 * Do the translation (as the parent) of the command to
88 * execute. This MAY involve calling other modules, so
89 * we want to do it in the parent.
91 radius_xlat(answer, sizeof(answer), cmd, request, NULL);
95 * Log the command if we are debugging something
97 DEBUG("Exec-Program: %s", buf);
100 * Build vector list of arguments and execute.
102 p = strtok(buf, " \t");
105 p = strtok(NULL, " \t");
110 radlog(L_ERR, "Exec-Program: empty command line.");
114 if ((pid = rad_fork(exec_wait)) == 0) {
115 #define MAX_ENVP 1024
117 char *envp[MAX_ENVP];
124 * We try to be fail-safe here. So if ANYTHING
125 * goes wrong, we exit with status 1.
129 * Open STDIN to /dev/null
131 devnull = open("/dev/null", O_RDWR);
133 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
137 dup2(devnull, STDIN_FILENO);
140 * Only massage the pipe handles if the parent
145 * pd[0] is the FD the child will read from,
146 * which we don't want.
148 if (close(pd[0]) != 0) {
149 radlog(L_ERR|L_CONS, "Can't close pipe: %s",
155 * pd[1] is the FD that the child will write to,
156 * so we make it STDOUT.
158 if (dup2(pd[1], STDOUT_FILENO) != 1) {
159 radlog(L_ERR|L_CONS, "Can't dup stdout: %s",
164 } else { /* no pipe, STDOUT should be /dev/null */
165 dup2(devnull, STDOUT_FILENO);
169 * If we're not debugging, then we can't do
170 * anything with the error messages, so we throw
173 * If we are debugging, then we want the error
174 * messages to go to the STDERR of the server.
176 if (debug_flag == 0) {
177 dup2(devnull, STDERR_FILENO);
182 * The server may have MANY FD's open. We don't
183 * want to leave dangling FD's for the child process
184 * to play funky games with, so we close them.
186 for (i = 3; i < 256; i++) {
191 * Set up the environment variables.
192 * We're in the child, and it will exit in 4 lines
193 * anyhow, so memory allocation isn't an issue.
197 for (vp = request->packet->vps; vp->next; vp = vp->next) {
199 * Hmm... maybe we shouldn't pass the
200 * user's password in an environment
203 snprintf(buffer, sizeof(buffer), "%s=", vp->name);
204 for (p = buffer; *p != '='; p++) {
207 } else if (isalpha(*p)) {
213 vp_prints_value(buffer+n, sizeof(buffer) - n, vp, 1);
215 envp[envlen++] = strdup(buffer);
219 execve(argv[0], argv, envp);
221 radlog(L_ERR, "Exec-Program: FAILED to execute %s: %s",
222 argv[0], strerror(errno));
230 radlog(L_ERR|L_CONS, "Couldn't fork %s: %s",
231 argv[0], strerror(errno));
236 * We're not waiting, exit, and ignore any child's
244 * Close the FD to which the child writes it's data.
246 * If we can't close it, then we close pd[0], and return an
249 if (close(pd[1]) != 0) {
250 radlog(L_ERR|L_CONS, "Can't close pipe: %s", strerror(errno));
256 * Read from the pipe until we doesn't get any more or
257 * until the message is full.
260 left = sizeof(answer) - 1;
262 status = read(pd[0], answer + done, left);
264 * Nothing more to read: stop.
271 * Error: See if we have to continue.
275 * We were interrupted: continue reading.
277 if (errno == EINTR) {
282 * There was another error. Most likely
283 * The child process has finished, and
291 if (left <= 0) break;
296 * Make sure that the writer can't block while writing to
297 * a pipe that no one is reading from anymore.
302 * Parse the output, if any.
306 * For backwards compatibility, first check
307 * for plain text (user_msg).
310 n = userparse(answer, &vp);
316 radlog(L_DBG, "Exec-Program-Wait: plaintext: %s", answer);
318 strncpy(message, answer, sizeof(message));
319 message[sizeof(message) - 1] = 0;
324 * HACK: Replace '\n' with ',' so that
325 * userparse() can parse the buffer in
326 * one go (the proper way would be to
327 * fix userparse(), but oh well).
329 for (p = answer; *p; p++) {
331 *p = comma ? ' ' : ',';
335 if (*p == ',') comma++;
339 * Replace any trailing comma by a NUL.
341 if (answer[strlen(answer) - 1] == ',') {
342 answer[strlen(answer) - 1] = '\0';
345 radlog(L_DBG,"Exec-Program-Wait: value-pairs: %s", answer);
346 if (userparse(answer, &vp) < 0) {
347 radlog(L_ERR, "Exec-Program-Wait: %s: unparsable reply", cmd);
351 * Add the attributes to the reply.
353 pairmove(&request->reply->vps, &vp);
356 } /* else the answer was a set of VP's, not a text message */
357 } /* else we didn't read anything from the child. */
360 * Call rad_waitpid (should map to waitpid on non-threaded
361 * or single-server systems).
363 child_pid = rad_waitpid(pid, &status, 0);
364 rad_assert(child_pid == pid);
366 if (WIFEXITED(status)) {
367 status = WEXITSTATUS(status);
368 radlog(L_DBG, "Exec-Program: returned: %d", status);
371 radlog(L_ERR|L_CONS, "Exec-Program: Abnormal child exit (killed or coredump)");