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$";
26 #include "libradius.h"
37 # include <sys/wait.h>
40 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
43 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
49 * Execute a program on successful authentication.
50 * Return 0 if exec_wait == 0.
51 * Return the exit code of the called program if exec_wait != 0.
54 int radius_exec_program(const char *cmd, REQUEST *request,
55 int exec_wait, const char **user_msg)
58 static char message[256];
68 void (*oldsig)(int) = NULL;
71 * (hs) - Open a pipe for child/parent communication.
72 * - Reset the signal handler for SIGCHLD, so
73 * we have a chance to notice the dead child here and
74 * not in some signal handler.
75 * This has to be done for the exec_wait case only, since
76 * if we don't wait we aren't interested in any
81 radlog(L_ERR|L_CONS, "Couldn't open pipe: %m");
84 if ((oldsig = signal(SIGCHLD, SIG_DFL)) == SIG_ERR) {
85 radlog(L_ERR|L_CONS, "Can't reset SIGCHLD: %m");
90 if ((pid = fork()) == 0) {
99 radius_xlat2(answer, sizeof(answer), cmd, request);
103 * Log the command if we are debugging something
105 DEBUG("Exec-Program: %s", buf);
108 * Build vector list and execute.
110 p = strtok(buf, " \t");
113 p = strtok(NULL, " \t");
117 radlog(L_ERR, "Exec-Program: empty command line.");
122 if (close(pd[0]) != 0)
123 radlog(L_ERR|L_CONS, "Can't close pipe: %m");
124 if (dup2(pd[1], 1) != 1)
125 radlog(L_ERR|L_CONS, "Can't dup stdout: %m");
129 * Set up the environment variables.
130 * We're in the child, and it will exit in 4 lines
131 * anyhow, so memory allocation isn't an issue.
135 for (vp = request->packet->vps; vp->next; vp = vp->next) {
137 * Hmm... maybe we shouldn't pass the
138 * user's password in an environment
141 snprintf(buffer, sizeof(buffer), "%s=", vp->name);
142 for (p = buffer; *p != '='; p++) {
145 } else if (isalpha(*p)) {
151 vp_prints_value(buffer+n, sizeof(buffer) - n, vp, 1);
153 envp[envlen++] = strdup(buffer);
161 for(n = 32; n >= 3; n--)
164 execve(argv[0], argv, envp);
166 radlog(L_ERR, "Exec-Program: %s: %m", argv[0]);
174 radlog(L_ERR|L_CONS, "Couldn't fork: %m");
181 * (hs) Do we have a pipe?
182 * --> Close the write side of the pipe
186 if (pd[0] || pd[1]) {
187 if (close(pd[1]) != 0)
188 radlog(L_ERR|L_CONS, "Can't close pipe: %m");
191 * (hs) Read until we doesn't get any more
192 * or until the message is full.
195 left = sizeof(answer) - 1;
196 while ((n = read(pd[0], answer + done, left)) > 0) {
199 if (left <= 0) break;
204 * (hs) Make sure that the writer can't block
205 * while writing in a pipe that isn't read anymore.
211 * Parse the output, if any.
215 * For backwards compatibility, first check
216 * for plain text (user_msg).
219 n = userparse(answer, &vp);
220 if (vp) pairfree(&vp);
223 radlog(L_DBG, "Exec-Program-Wait: plaintext: %s", answer);
225 strncpy(message, answer, sizeof(message));
226 message[sizeof(message) - 1] = 0;
231 * HACK: Replace '\n' with ',' so that
232 * userparse() can parse the buffer in
233 * one go (the proper way would be to
234 * fix userparse(), but oh well).
236 for (p = answer; *p; p++) {
238 *p = comma ? ' ' : ',';
242 if (*p == ',') comma++;
246 * Replace any trailing comma by a NUL.
248 if (answer[strlen(answer) - 1] == ',')
249 answer[strlen(answer) - 1] = '\0';
251 radlog(L_DBG,"Exec-Program-Wait: value-pairs: %s", answer);
252 if (userparse(answer, &vp) != 0)
254 "Exec-Program-Wait: %s: unparsable reply", cmd);
256 pairmove(&request->reply->vps, &vp);
262 while(waitpid(pid, &status, 0) != pid)
266 * (hs) Now we let our cleanup_sig handler take care for
267 * all signals that will arise.
269 if (oldsig && (signal(SIGCHLD, oldsig) == SIG_ERR))
271 "Can't set SIGCHLD to the cleanup handler: %m");
272 sig_cleanup(SIGCHLD);
274 if (WIFEXITED(status)) {
275 status = WEXITSTATUS(status);
276 radlog(L_INFO, "Exec-Program: returned: %d", status);
279 radlog(L_ERR|L_CONS, "Exec-Program: Abnormal child exit (killed or coredump)");