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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2001,2002,2003,2004 The FreeRADIUS server project
22 static const char rcsid[] = "$Id$";
24 #include <freeradius-devel/autoconf.h>
38 #ifdef HAVE_SYS_WAIT_H
39 # include <sys/wait.h>
42 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
45 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
48 #include <freeradius-devel/radiusd.h>
49 #include <freeradius-devel/rad_assert.h>
51 #define MAX_ARGV (256)
53 * Execute a program on successful authentication.
54 * Return 0 if exec_wait == 0.
55 * Return the exit code of the called program if exec_wait != 0.
56 * Return -1 on fork/other errors in the parent process.
58 int radius_exec_program(const char *cmd, REQUEST *request,
60 char *user_msg, int msg_len,
61 VALUE_PAIR *input_pairs,
62 VALUE_PAIR **output_pairs,
80 if (user_msg) *user_msg = '\0';
81 if (output_pairs) *output_pairs = NULL;
83 if (strlen(cmd) > (sizeof(mycmd) - 1)) {
84 radlog(L_ERR|L_CONS, "Command line is too long");
89 * Check for bad escapes.
91 if (cmd[strlen(cmd) - 1] == '\\') {
92 radlog(L_ERR|L_CONS, "Command line has final backslash, without a following character");
96 strNcpy(mycmd, cmd, sizeof(mycmd));
99 * Split the string into argv's BEFORE doing radius_xlat...
110 if ((*from == ' ') || (*from == '\t')) {
118 if (argc >= (MAX_ARGV - 1)) break;
121 * Copy the argv over to our buffer.
123 while (*from && (*from != ' ') && (*from != '\t')) {
124 if (to >= mycmd + sizeof(mycmd) - 1) {
125 return -1; /* ran out of space */
131 length = rad_copy_string(to, from);
133 radlog(L_ERR|L_CONS, "Invalid string passed as argument for external program");
141 if (from[1] == '{') {
144 length = rad_copy_variable(to, from);
146 radlog(L_ERR|L_CONS, "Invalid variable expansion passed as argument for external program");
151 } else { /* FIXME: catch %%{ ? */
159 } /* end of string, or found a space */
161 *(to++) = '\0'; /* terminate the string */
165 * We have to have SOMETHING, at least.
168 radlog(L_ERR, "Exec-Program: empty command line.");
173 * Expand each string, as appropriate.
176 left = sizeof(argv_buf);
177 for (i = 0; i < argc; i++) {
181 * Don't touch argv's which won't be translated.
183 if (strchr(argv[i], '%') == NULL) continue;
185 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
188 * Fail to be backwards compatible.
190 * It's yucky, but it won't break anything,
191 * and it won't cause security problems.
203 radlog(L_ERR, "Exec-Program: Ran out of space while expanding arguments.");
210 * Open a pipe for child/parent communication, if necessary.
214 radlog(L_ERR|L_CONS, "Couldn't open pipe: %s",
220 * We're not waiting, so we don't look for a
227 if ((pid = rad_fork(exec_wait)) == 0) {
228 #define MAX_ENVP 1024
230 char *envp[MAX_ENVP];
237 * We try to be fail-safe here. So if ANYTHING
238 * goes wrong, we exit with status 1.
242 * Open STDIN to /dev/null
244 devnull = open("/dev/null", O_RDWR);
246 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
250 dup2(devnull, STDIN_FILENO);
253 * Only massage the pipe handles if the parent
258 * pd[0] is the FD the child will read from,
259 * which we don't want.
261 if (close(pd[0]) != 0) {
262 radlog(L_ERR|L_CONS, "Can't close pipe: %s",
268 * pd[1] is the FD that the child will write to,
269 * so we make it STDOUT.
271 if (dup2(pd[1], STDOUT_FILENO) != 1) {
272 radlog(L_ERR|L_CONS, "Can't dup stdout: %s",
277 } else { /* no pipe, STDOUT should be /dev/null */
278 dup2(devnull, STDOUT_FILENO);
282 * If we're not debugging, then we can't do
283 * anything with the error messages, so we throw
286 * If we are debugging, then we want the error
287 * messages to go to the STDERR of the server.
289 if (debug_flag == 0) {
290 dup2(devnull, STDERR_FILENO);
295 * The server may have MANY FD's open. We don't
296 * want to leave dangling FD's for the child process
297 * to play funky games with, so we close them.
302 * Set up the environment variables.
303 * We're in the child, and it will exit in 4 lines
304 * anyhow, so memory allocation isn't an issue.
308 for (vp = input_pairs; vp != NULL; vp = vp->next) {
310 * Hmm... maybe we shouldn't pass the
311 * user's password in an environment
314 snprintf(buffer, sizeof(buffer), "%s=", vp->name);
316 for (p = buffer; *p != '='; p++) {
319 } else if (isalpha((int) *p)) {
326 vp_prints_value(buffer+n, sizeof(buffer) - n, vp, shell_escape);
328 envp[envlen++] = strdup(buffer);
331 * Don't add too many attributes.
333 if (envlen == (MAX_ENVP - 1)) break;
337 execve(argv[0], argv, envp);
338 radlog(L_ERR, "Exec-Program: FAILED to execute %s: %s",
339 argv[0], strerror(errno));
347 radlog(L_ERR|L_CONS, "Couldn't fork %s: %s",
348 argv[0], strerror(errno));
353 * We're not waiting, exit, and ignore any child's status.
360 * Close the FD to which the child writes it's data.
362 * If we can't close it, then we close pd[0], and return an
365 if (close(pd[1]) != 0) {
366 radlog(L_ERR|L_CONS, "Can't close pipe: %s", strerror(errno));
372 * Read from the pipe until we doesn't get any more or
373 * until the message is full.
376 left = sizeof(answer) - 1;
378 status = read(pd[0], answer + done, left);
380 * Nothing more to read: stop.
387 * Error: See if we have to continue.
391 * We were interrupted: continue reading.
393 if (errno == EINTR) {
398 * There was another error. Most likely
399 * The child process has finished, and
407 if (left <= 0) break;
412 * Make sure that the writer can't block while writing to
413 * a pipe that no one is reading from anymore.
417 DEBUG2("Exec-Program output: %s", answer);
420 * Parse the output, if any.
426 * For backwards compatibility, first check
427 * for plain text (user_msg).
430 n = userparse(answer, &vp);
436 if (n == T_OP_INVALID) {
437 DEBUG("Exec-Program-Wait: plaintext: %s", answer);
439 strNcpy(user_msg, answer, msg_len);
443 * HACK: Replace '\n' with ',' so that
444 * userparse() can parse the buffer in
445 * one go (the proper way would be to
446 * fix userparse(), but oh well).
448 for (p = answer; *p; p++) {
450 *p = comma ? ' ' : ',';
454 if (*p == ',') comma++;
458 * Replace any trailing comma by a NUL.
460 if (answer[strlen(answer) - 1] == ',') {
461 answer[strlen(answer) - 1] = '\0';
464 radlog(L_DBG,"Exec-Program-Wait: value-pairs: %s", answer);
465 if (userparse(answer, &vp) == T_OP_INVALID) {
466 radlog(L_ERR, "Exec-Program-Wait: %s: unparsable reply", cmd);
470 * Tell the caller about the value
475 } /* else the answer was a set of VP's, not a text message */
476 } /* else we didn't read anything from the child */
479 * Call rad_waitpid (should map to waitpid on non-threaded
480 * or single-server systems).
482 for (n = 0; n < 10; n++) {
483 child_pid = rad_waitpid(pid, &status, WNOHANG);
484 if (child_pid == pid) {
485 if (WIFEXITED(status)) {
486 status = WEXITSTATUS(status);
487 radlog(L_DBG, "Exec-Program: returned: %d", status);
492 radlog(L_ERR|L_CONS, "Exec-Program: Failed to catch child return code");
497 radlog(L_ERR|L_CONS, "Exec-Program: Child execution is taking too long. Giving up.");