Last stage of getting rlm_exec to work in multiple sections.
[freeradius.git] / src / main / exec.c
1 /*
2  * exec.c       Execute external programs.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2000  The FreeRADIUS server project
21  * Copyright 2000  Michael J. Hartwick <hartwick@hartwick.com>
22  */
23 static const char rcsid[] = "$Id$";
24
25 #include "autoconf.h"
26
27 #include <sys/file.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <signal.h>
34
35 #if HAVE_SYS_WAIT_H
36 #       include <sys/wait.h>
37 #endif
38 #ifndef WEXITSTATUS
39 #       define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
40 #endif
41 #ifndef WIFEXITED
42 #       define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
43 #endif
44
45 #include "radiusd.h"
46 #include "rad_assert.h"
47
48 /*
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.
52  *      Return -1 on fork/other errors in the parent process.
53  */
54 int radius_exec_program(const char *cmd, REQUEST *request,
55                         int exec_wait,
56                         char *user_msg, int msg_len,
57                         VALUE_PAIR *input_pairs,
58                         VALUE_PAIR **output_pairs)
59 {
60         VALUE_PAIR *vp;
61         char answer[4096];
62         char *argv[256];
63         char *buf, *p;
64         int pd[2];
65         pid_t pid, child_pid;
66         int argc = -1;
67         int comma = 0;
68         int status;
69         int n, left, done;
70
71         if (user_msg) *user_msg = '\0';
72         if (output_pairs) *output_pairs = NULL;
73
74         /*
75          *      Open a pipe for child/parent communication, if
76          *      necessary.
77          */
78         if (exec_wait) {
79                 if (pipe(pd) != 0) {
80                         radlog(L_ERR|L_CONS, "Couldn't open pipe: %s",
81                                strerror(errno));
82                         return -1;
83                 }
84         } else {
85                 /*
86                  *      We're not waiting, so we don't look for a
87                  *      message, or VP's.
88                  */
89                 user_msg = NULL;
90                 output_pairs = NULL;
91         }
92
93         /*
94          *      Do the translation (as the parent) of the command to
95          *      execute.  This MAY involve calling other modules, so
96          *      we want to do it in the parent.
97          */
98         radius_xlat(answer, sizeof(answer), cmd, request, NULL);
99         buf = answer;
100         
101         /*
102          *      Log the command if we are debugging something
103          */
104         DEBUG("Exec-Program: %s", buf);
105         
106         /*
107          *      Build vector list of arguments and execute.
108          *
109          *      FIXME: This parsing gets excited over spaces in
110          *      the translated strings, e.g. User-Name = "aa bb"
111          *      is passed as two seperate arguments, instead of one.
112          *
113          *      What we SHOULD do instead is to split the exec program
114          *      buffer first, and then do the translation on every
115          *      subsequent string.
116          */
117         p = strtok(buf, " \t");
118         if (p) do {
119                 argv[++argc] = p;
120                 p = strtok(NULL, " \t");
121         } while(p != NULL);
122
123         argv[++argc] = p;
124         if (argc == 0) {
125                 radlog(L_ERR, "Exec-Program: empty command line.");
126                 return -1;
127         }
128
129         if ((pid = rad_fork(exec_wait)) == 0) {
130 #define MAX_ENVP 1024
131                 int i, devnull;
132                 char *envp[MAX_ENVP];
133                 int envlen;
134                 char buffer[1024];
135
136                 /*      
137                  *      Child process.
138                  *
139                  *      We try to be fail-safe here.  So if ANYTHING
140                  *      goes wrong, we exit with status 1.
141                  */
142
143                 /*
144                  *      Open STDIN to /dev/null
145                  */
146                 devnull = open("/dev/null", O_RDWR);
147                 if (devnull < 0) {
148                         radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
149                                strerror(errno));
150                         exit(1);
151                 }
152                 dup2(devnull, STDIN_FILENO);
153
154                 /*
155                  *      Only massage the pipe handles if the parent
156                  *      has created them.
157                  */
158                 if (exec_wait) {
159                         /*
160                          *      pd[0] is the FD the child will read from,
161                          *      which we don't want.
162                          */
163                         if (close(pd[0]) != 0) {
164                                 radlog(L_ERR|L_CONS, "Can't close pipe: %s",
165                                        strerror(errno));
166                                 exit(1);
167                         }
168                         
169                         /*
170                          *      pd[1] is the FD that the child will write to,
171                          *      so we make it STDOUT.
172                          */
173                         if (dup2(pd[1], STDOUT_FILENO) != 1) {
174                                 radlog(L_ERR|L_CONS, "Can't dup stdout: %s",
175                                        strerror(errno));
176                                 exit(1);
177                         }
178
179                 } else {        /* no pipe, STDOUT should be /dev/null */
180                         dup2(devnull, STDOUT_FILENO);
181                 }
182
183                 /*
184                  *      If we're not debugging, then we can't do
185                  *      anything with the error messages, so we throw
186                  *      them away.
187                  *
188                  *      If we are debugging, then we want the error
189                  *      messages to go to the STDERR of the server.
190                  */
191                 if (debug_flag == 0) {
192                         dup2(devnull, STDERR_FILENO);
193                 }
194                 close(devnull);
195
196                 /*
197                  *      The server may have MANY FD's open.  We don't
198                  *      want to leave dangling FD's for the child process
199                  *      to play funky games with, so we close them.
200                  */
201                 for (i = 3; i < 256; i++) {
202                         close(i);
203                 }
204
205                 /*
206                  *      Set up the environment variables.
207                  *      We're in the child, and it will exit in 4 lines
208                  *      anyhow, so memory allocation isn't an issue.
209                  */
210                 envlen = 0;
211
212                 for (vp = input_pairs; vp->next; vp = vp->next) {
213                         /*
214                          *      Hmm... maybe we shouldn't pass the
215                          *      user's password in an environment
216                          *      variable...
217                          */
218                         snprintf(buffer, sizeof(buffer), "%s=", vp->name);
219                         for (p = buffer; *p != '='; p++) {
220                                 if (*p == '-') {
221                                         *p = '_';
222                                 } else if (isalpha((int) *p)) {
223                                         *p = toupper(*p);
224                                 }
225                         }
226
227                         n = strlen(buffer);
228                         vp_prints_value(buffer+n, sizeof(buffer) - n, vp, 1);
229
230                         envp[envlen++] = strdup(buffer);
231                 }
232                 envp[envlen] = NULL;
233
234                 execve(argv[0], argv, envp);
235
236                 radlog(L_ERR, "Exec-Program: FAILED to execute %s: %s",
237                        argv[0], strerror(errno));
238                 exit(1);
239         }
240
241         /*
242          *      Parent process.
243          */
244         if (pid < 0) {
245                 radlog(L_ERR|L_CONS, "Couldn't fork %s: %s",
246                        argv[0], strerror(errno));
247                 return -1;
248         }
249
250         /*
251          *      We're not waiting, exit, and ignore any child's
252          *      status.
253          */
254         if (!exec_wait) {
255                 return 0;
256         }
257
258         /*
259          *      Close the FD to which the child writes it's data.
260          *
261          *      If we can't close it, then we close pd[0], and return an
262          *      error.
263          */
264         if (close(pd[1]) != 0) {
265                 radlog(L_ERR|L_CONS, "Can't close pipe: %s", strerror(errno));
266                 close(pd[0]);
267                 return -1;
268         }
269
270         /*
271          *      Read from the pipe until we doesn't get any more or
272          *      until the message is full.
273          */
274         done = 0;
275         left = sizeof(answer) - 1;
276         while (1) {
277                 status = read(pd[0], answer + done, left);
278                 /*
279                  *      Nothing more to read: stop.
280                  */
281                 if (status == 0) {
282                         break;
283                 }
284
285                 /*
286                  *      Error: See if we have to continue.
287                  */
288                 if (status < 0) {
289                         /*
290                          *      We were interrupted: continue reading.
291                          */
292                         if (errno == EINTR) {
293                                 continue;
294                         }
295
296                         /*
297                          *      There was another error.  Most likely
298                          *      The child process has finished, and
299                          *      exited.
300                          */
301                         break;
302                 }
303
304                 done += status;
305                 left -= status;
306                 if (left <= 0) break;
307         }
308         answer[done] = 0;
309
310         /*
311          *      Make sure that the writer can't block while writing to
312          *      a pipe that no one is reading from anymore.
313          */
314         close(pd[0]);
315
316         DEBUG2("ANSWER %s", answer);
317
318         /*
319          *      Parse the output, if any.
320          */
321         if (done) {
322                 n = -1;
323                 if (output_pairs) {
324                         /*
325                          *      For backwards compatibility, first check
326                          *      for plain text (user_msg).
327                          */
328                         vp = NULL;
329                         n = userparse(answer, &vp);
330                         if (vp) {
331                                 pairfree(&vp);
332                         }
333                 }
334
335                 if (n < 0) {
336                         radlog(L_DBG, "Exec-Program-Wait: plaintext: %s", answer);
337                         if (user_msg) {
338                                 strNcpy(user_msg, answer, msg_len);
339                         }
340                 } else {
341                         /*
342                          *      HACK: Replace '\n' with ',' so that
343                          *      userparse() can parse the buffer in
344                          *      one go (the proper way would be to
345                          *      fix userparse(), but oh well).
346                          */
347                         for (p = answer; *p; p++) {
348                                 if (*p == '\n') {
349                                         *p = comma ? ' ' : ',';
350                                         p++;
351                                         comma = 0;
352                                 }
353                                 if (*p == ',') comma++;
354                         }
355
356                         /*
357                          *  Replace any trailing comma by a NUL.
358                          */
359                         if (answer[strlen(answer) - 1] == ',') {
360                                 answer[strlen(answer) - 1] = '\0';
361                         }
362
363                         radlog(L_DBG,"Exec-Program-Wait: value-pairs: %s", answer);
364                         if (userparse(answer, &vp) < 0) {
365                                 radlog(L_ERR, "Exec-Program-Wait: %s: unparsable reply", cmd);
366
367                         } else {
368                                 /*
369                                  *      Tell the caller about the value
370                                  *      pairs.
371                                  */
372                                 *output_pairs = vp;
373                         }
374                 } /* else the answer was a set of VP's, not a text message */
375         } /* else we didn't read anything from the child. */
376
377         /*
378          *      Call rad_waitpid (should map to waitpid on non-threaded
379          *      or single-server systems).
380          */
381         child_pid = rad_waitpid(pid, &status, 0);
382         if (child_pid == pid) {
383                 if (WIFEXITED(status)) {
384                         status = WEXITSTATUS(status);
385                         radlog(L_DBG, "Exec-Program: returned: %d", status);
386                         return status;
387                 }
388         }
389
390         radlog(L_ERR|L_CONS, "Exec-Program: Abnormal child exit");
391         return 1;
392 }