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