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-2004,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/autoconf.h>
40 #ifdef HAVE_SYS_WAIT_H
41 # include <sys/wait.h>
44 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
47 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
50 #include <freeradius-devel/radiusd.h>
51 #include <freeradius-devel/rad_assert.h>
53 #define MAX_ARGV (256)
55 * Execute a program on successful authentication.
56 * Return 0 if exec_wait == 0.
57 * Return the exit code of the called program if exec_wait != 0.
58 * Return -1 on fork/other errors in the parent process.
60 int radius_exec_program(const char *cmd, REQUEST *request,
62 char *user_msg, int msg_len,
63 VALUE_PAIR *input_pairs,
64 VALUE_PAIR **output_pairs,
82 if (user_msg) *user_msg = '\0';
83 if (output_pairs) *output_pairs = NULL;
85 if (strlen(cmd) > (sizeof(mycmd) - 1)) {
86 radlog(L_ERR|L_CONS, "Command line is too long");
91 * Check for bad escapes.
93 if (cmd[strlen(cmd) - 1] == '\\') {
94 radlog(L_ERR|L_CONS, "Command line has final backslash, without a following character");
98 strlcpy(mycmd, cmd, sizeof(mycmd));
101 * Split the string into argv's BEFORE doing radius_xlat...
112 if ((*from == ' ') || (*from == '\t')) {
120 if (argc >= (MAX_ARGV - 1)) break;
123 * Copy the argv over to our buffer.
125 while (*from && (*from != ' ') && (*from != '\t')) {
126 if (to >= mycmd + sizeof(mycmd) - 1) {
127 return -1; /* ran out of space */
133 length = rad_copy_string(to, from);
135 radlog(L_ERR|L_CONS, "Invalid string passed as argument for external program");
143 if (from[1] == '{') {
146 length = rad_copy_variable(to, from);
148 radlog(L_ERR|L_CONS, "Invalid variable expansion passed as argument for external program");
153 } else { /* FIXME: catch %%{ ? */
161 } /* end of string, or found a space */
163 *(to++) = '\0'; /* terminate the string */
167 * We have to have SOMETHING, at least.
170 radlog(L_ERR, "Exec-Program: empty command line.");
175 * Expand each string, as appropriate.
178 left = sizeof(argv_buf);
179 for (i = 0; i < argc; i++) {
183 * Don't touch argv's which won't be translated.
185 if (strchr(argv[i], '%') == NULL) continue;
187 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
190 * Fail to be backwards compatible.
192 * It's yucky, but it won't break anything,
193 * and it won't cause security problems.
205 radlog(L_ERR, "Exec-Program: Ran out of space while expanding arguments.");
212 * Open a pipe for child/parent communication, if necessary.
216 radlog(L_ERR|L_CONS, "Couldn't open pipe: %s",
222 * We're not waiting, so we don't look for a
230 pid = rad_fork(); /* remember PID */
232 pid = fork(); /* don't wait */
236 #define MAX_ENVP 1024
238 char *envp[MAX_ENVP];
245 * We try to be fail-safe here. So if ANYTHING
246 * goes wrong, we exit with status 1.
250 * Open STDIN to /dev/null
252 devnull = open("/dev/null", O_RDWR);
254 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
258 dup2(devnull, STDIN_FILENO);
261 * Only massage the pipe handles if the parent
266 * pd[0] is the FD the child will read from,
267 * which we don't want.
269 if (close(pd[0]) != 0) {
270 radlog(L_ERR|L_CONS, "Can't close pipe: %s",
276 * pd[1] is the FD that the child will write to,
277 * so we make it STDOUT.
279 if (dup2(pd[1], STDOUT_FILENO) != 1) {
280 radlog(L_ERR|L_CONS, "Can't dup stdout: %s",
285 } else { /* no pipe, STDOUT should be /dev/null */
286 dup2(devnull, STDOUT_FILENO);
290 * If we're not debugging, then we can't do
291 * anything with the error messages, so we throw
294 * If we are debugging, then we want the error
295 * messages to go to the STDERR of the server.
297 if (debug_flag == 0) {
298 dup2(devnull, STDERR_FILENO);
303 * The server may have MANY FD's open. We don't
304 * want to leave dangling FD's for the child process
305 * to play funky games with, so we close them.
310 * Set up the environment variables.
311 * We're in the child, and it will exit in 4 lines
312 * anyhow, so memory allocation isn't an issue.
316 for (vp = input_pairs; vp != NULL; vp = vp->next) {
318 * Hmm... maybe we shouldn't pass the
319 * user's password in an environment
322 snprintf(buffer, sizeof(buffer), "%s=", vp->name);
324 for (p = buffer; *p != '='; p++) {
327 } else if (isalpha((int) *p)) {
334 vp_prints_value(buffer+n, sizeof(buffer) - n, vp, shell_escape);
336 envp[envlen++] = strdup(buffer);
339 * Don't add too many attributes.
341 if (envlen == (MAX_ENVP - 1)) break;
345 execve(argv[0], argv, envp);
346 radlog(L_ERR, "Exec-Program: FAILED to execute %s: %s",
347 argv[0], strerror(errno));
355 radlog(L_ERR|L_CONS, "Couldn't fork %s: %s",
356 argv[0], strerror(errno));
361 * We're not waiting, exit, and ignore any child's status.
368 * Close the FD to which the child writes it's data.
370 * If we can't close it, then we close pd[0], and return an
373 if (close(pd[1]) != 0) {
374 radlog(L_ERR|L_CONS, "Can't close pipe: %s", strerror(errno));
380 * Read from the pipe until we doesn't get any more or
381 * until the message is full.
384 left = sizeof(answer) - 1;
386 status = read(pd[0], answer + done, left);
388 * Nothing more to read: stop.
395 * Error: See if we have to continue.
399 * We were interrupted: continue reading.
401 if (errno == EINTR) {
406 * There was another error. Most likely
407 * The child process has finished, and
415 if (left <= 0) break;
420 * Make sure that the writer can't block while writing to
421 * a pipe that no one is reading from anymore.
425 DEBUG2("Exec-Program output: %s", answer);
428 * Parse the output, if any.
434 * For backwards compatibility, first check
435 * for plain text (user_msg).
438 n = userparse(answer, &vp);
444 if (n == T_OP_INVALID) {
445 DEBUG("Exec-Program-Wait: plaintext: %s", answer);
447 strlcpy(user_msg, answer, msg_len);
451 * HACK: Replace '\n' with ',' so that
452 * userparse() can parse the buffer in
453 * one go (the proper way would be to
454 * fix userparse(), but oh well).
456 for (p = answer; *p; p++) {
458 *p = comma ? ' ' : ',';
462 if (*p == ',') comma++;
466 * Replace any trailing comma by a NUL.
468 if (answer[strlen(answer) - 1] == ',') {
469 answer[strlen(answer) - 1] = '\0';
472 radlog(L_DBG,"Exec-Program-Wait: value-pairs: %s", answer);
473 if (userparse(answer, &vp) == T_OP_INVALID) {
474 radlog(L_ERR, "Exec-Program-Wait: %s: unparsable reply", cmd);
478 * Tell the caller about the value
483 } /* else the answer was a set of VP's, not a text message */
484 } /* else we didn't read anything from the child */
487 * Call rad_waitpid (should map to waitpid on non-threaded
488 * or single-server systems).
490 child_pid = rad_waitpid(pid, &status);
491 if (child_pid == 0) {
492 radlog(L_DBG, "Exec-Program: Timeout waiting for child");
496 if (child_pid == pid) {
497 if (WIFEXITED(status)) {
498 status = WEXITSTATUS(status);
499 radlog(L_DBG, "Exec-Program: returned: %d", status);
504 radlog(L_ERR|L_CONS, "Exec-Program: Abnormal child exit: %s",