import from HEAD
[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 <unistd.h>
34 #include <signal.h>
35
36 #ifdef HAVE_SYS_WAIT_H
37 #       include <sys/wait.h>
38 #endif
39 #ifndef WEXITSTATUS
40 #       define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
41 #endif
42 #ifndef WIFEXITED
43 #       define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
44 #endif
45
46 #include "radiusd.h"
47 #include "rad_assert.h"
48
49 /*
50  *      Copy a quoted string.
51  */
52 static int rad_copy_string(char *to, const char *from)
53 {
54         int length = 0;
55         char quote = *from;
56
57         do {
58                 if (*from == '\\') {
59                         *(to++) = *(from++);
60                         length++;
61                 }
62                 *(to++) = *(from++);
63                 length++;
64         } while (*from && (*from != quote));
65
66         if (*from != quote) return -1; /* not properly quoted */
67
68         *(to++) = quote;
69         length++;
70         *to = '\0';
71
72         return length;
73 }
74
75
76 /*
77  *      Copy a %{} string.
78  */
79 static int rad_copy_variable(char *to, const char *from)
80 {
81         int length = 0;
82         int sublen;
83
84         *(to++) = *(from++);
85         length++;
86
87         while (*from) {
88                 switch (*from) {
89                 case '"':
90                 case '\'':
91                         sublen = rad_copy_string(to, from);
92                         if (sublen < 0) return sublen;
93                         from += sublen;
94                         to += sublen;
95                         break;
96
97                 case '}':       /* end of variable expansion */
98                         *(to++) = *(from++);
99                         *to = '\0';
100                         length++;
101                         return length; /* proper end of variable */
102
103                 case '\\':
104                         *(to++) = *(from++);
105                         *(to++) = *(from++);
106                         length += 2;
107                         break;
108
109                 case '%':       /* start of variable expansion */
110                         if (from[1] == '{') {
111                                 *(to++) = *(from++);
112                                 length++;
113                                 
114                                 sublen = rad_copy_variable(to, from);
115                                 if (sublen < 0) return sublen;
116                                 from += sublen;
117                                 to += sublen;
118                                 length += sublen;
119                         } /* else FIXME: catch %%{ ?*/
120
121                         /* FALL-THROUGH */
122                         break;
123
124                 default:
125                         *(to++) = *(from++);
126                         length++;
127                         break;
128                 }
129         } /* loop over the input string */
130
131         /*
132          *      We ended the string before a trailing '}'
133          */
134
135         return -1;
136 }
137
138 #define MAX_ARGV (256)
139 /*
140  *      Execute a program on successful authentication.
141  *      Return 0 if exec_wait == 0.
142  *      Return the exit code of the called program if exec_wait != 0.
143  *      Return -1 on fork/other errors in the parent process.
144  */
145 int radius_exec_program(const char *cmd, REQUEST *request,
146                         int exec_wait,
147                         char *user_msg, int msg_len,
148                         VALUE_PAIR *input_pairs,
149                         VALUE_PAIR **output_pairs)
150 {
151         VALUE_PAIR *vp;
152         char mycmd[1024];
153         char answer[4096];
154         char argv_buf[4096];
155         char *argv[MAX_ARGV];
156         const char *from;
157         char *p, *to;
158         int pd[2];
159         pid_t pid, child_pid;
160         int argc = -1;
161         int comma = 0;
162         int status;
163         int i;
164         int n, left, done;
165
166         if (user_msg) *user_msg = '\0';
167         if (output_pairs) *output_pairs = NULL;
168
169         if (strlen(cmd) > (sizeof(mycmd) - 1)) {
170                 radlog(L_ERR|L_CONS, "Command line is too long");
171                 return -1;
172         }
173
174         /*
175          *      Check for bad escapes.
176          */
177         if (cmd[strlen(cmd) - 1] == '\\') {
178                 radlog(L_ERR|L_CONS, "Command line has final backslash, without a following character");
179                 return -1;
180         }
181
182         strNcpy(mycmd, cmd, sizeof(mycmd));
183
184         /*
185          *      Split the string into argv's BEFORE doing radius_xlat...
186          */
187         from = cmd;
188         to = mycmd;
189         argc = 0;
190         while (*from) {
191                 int length;
192
193                 /*
194                  *      Skip spaces.
195                  */
196                 if ((*from == ' ') || (*from == '\t')) {
197                         from++;
198                         continue;
199                 }
200
201                 argv[argc] = to;
202                 argc++;
203
204                 if (argc >= (MAX_ARGV - 1)) break;
205
206                 /*
207                  *      Copy the argv over to our buffer.
208                  */
209                 while (*from && (*from != ' ') && (*from != '\t')) {
210                         if (to >= mycmd + sizeof(mycmd) - 1) {
211                                 return -1; /* ran out of space */
212                         }
213
214                         switch (*from) {
215                         case '"':
216                         case '\'':
217                                 length = rad_copy_string(to, from);
218                                 if (length < 0) {
219                                         radlog(L_ERR|L_CONS, "Invalid string passed as argument for external program");
220                                         return -1;
221                                 }
222                                 from += length;
223                                 to += length;
224                                 break;
225
226                         case '%':
227                                 if (from[1] == '{') {
228                                         *(to++) = *(from++);
229
230                                         length = rad_copy_variable(to, from);
231                                         if (length < 0) {
232                                                 radlog(L_ERR|L_CONS, "Invalid variable expansion passed as argument for external program");
233                                                 return -1;
234                                         }
235                                         from += length;
236                                         to += length;
237                                 } else { /* FIXME: catch %%{ ? */
238                                         *(to++) = *(from++);
239                                 }
240                                 break;
241
242                         default:
243                                 *(to++) = *(from++);
244                         }
245                 } /* end of string, or found a space */
246
247                 *(to++) = '\0'; /* terminate the string */
248         }
249
250         /*
251          *      We have to have SOMETHING, at least.
252          */
253         if (argc <= 0) {
254                 radlog(L_ERR, "Exec-Program: empty command line.");
255                 return -1;
256         }
257
258         /*
259          *      Expand each string, as appropriate.
260          */
261         to = argv_buf;
262         left = sizeof(argv_buf);
263         for (i = 0; i < argc; i++) {
264                 int sublen;
265
266                 /*
267                  *      Don't touch argv's which won't be translated.
268                  */
269                 if (strchr(argv[i], '%') == NULL) continue;
270
271                 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
272                 if (sublen <= 0) {
273                         /*
274                          *      Fail to be backwards compatible.
275                          *
276                          *      It's yucky, but it won't break anything,
277                          *      and it won't cause security problems.
278                          */
279                         sublen = 0;
280                 }
281
282                 argv[i] = to;
283                 to += sublen;
284                 *(to++) = '\0';
285                 left -= sublen;
286                 left--;
287
288                 if (left <= 0) {
289                         radlog(L_ERR, "Exec-Program: Ran out of space while expanding arguments.");
290                         return -1;
291                 }
292         }
293         argv[argc] = NULL;
294
295         /*
296          *      Open a pipe for child/parent communication, if
297          *      necessary.
298          */
299         if (exec_wait) {
300                 if (pipe(pd) != 0) {
301                         radlog(L_ERR|L_CONS, "Couldn't open pipe: %s",
302                                strerror(errno));
303                         return -1;
304                 }
305         } else {
306                 /*
307                  *      We're not waiting, so we don't look for a
308                  *      message, or VP's.
309                  */
310                 user_msg = NULL;
311                 output_pairs = NULL;
312         }
313
314         if ((pid = rad_fork(exec_wait)) == 0) {
315 #define MAX_ENVP 1024
316                 int devnull;
317                 char *envp[MAX_ENVP];
318                 int envlen;
319                 char buffer[1024];
320
321                 /*
322                  *      Child process.
323                  *
324                  *      We try to be fail-safe here.  So if ANYTHING
325                  *      goes wrong, we exit with status 1.
326                  */
327
328                 /*
329                  *      Open STDIN to /dev/null
330                  */
331                 devnull = open("/dev/null", O_RDWR);
332                 if (devnull < 0) {
333                         radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
334                                strerror(errno));
335                         exit(1);
336                 }
337                 dup2(devnull, STDIN_FILENO);
338
339                 /*
340                  *      Only massage the pipe handles if the parent
341                  *      has created them.
342                  */
343                 if (exec_wait) {
344                         /*
345                          *      pd[0] is the FD the child will read from,
346                          *      which we don't want.
347                          */
348                         if (close(pd[0]) != 0) {
349                                 radlog(L_ERR|L_CONS, "Can't close pipe: %s",
350                                        strerror(errno));
351                                 exit(1);
352                         }
353
354                         /*
355                          *      pd[1] is the FD that the child will write to,
356                          *      so we make it STDOUT.
357                          */
358                         if (dup2(pd[1], STDOUT_FILENO) != 1) {
359                                 radlog(L_ERR|L_CONS, "Can't dup stdout: %s",
360                                        strerror(errno));
361                                 exit(1);
362                         }
363
364                 } else {        /* no pipe, STDOUT should be /dev/null */
365                         dup2(devnull, STDOUT_FILENO);
366                 }
367
368                 /*
369                  *      If we're not debugging, then we can't do
370                  *      anything with the error messages, so we throw
371                  *      them away.
372                  *
373                  *      If we are debugging, then we want the error
374                  *      messages to go to the STDERR of the server.
375                  */
376                 if (debug_flag == 0) {
377                         dup2(devnull, STDERR_FILENO);
378                 }
379                 close(devnull);
380
381                 /*
382                  *      The server may have MANY FD's open.  We don't
383                  *      want to leave dangling FD's for the child process
384                  *      to play funky games with, so we close them.
385                  */
386                 closefrom(3);
387
388                 /*
389                  *      Set up the environment variables.
390                  *      We're in the child, and it will exit in 4 lines
391                  *      anyhow, so memory allocation isn't an issue.
392                  */
393                 envlen = 0;
394
395                 for (vp = input_pairs; vp != NULL; vp = vp->next) {
396                         /*
397                          *      Hmm... maybe we shouldn't pass the
398                          *      user's password in an environment
399                          *      variable...
400                          */
401                         snprintf(buffer, sizeof(buffer), "%s=", vp->name);
402                         for (p = buffer; *p != '='; p++) {
403                                 if (*p == '-') {
404                                         *p = '_';
405                                 } else if (isalpha((int) *p)) {
406                                         *p = toupper(*p);
407                                 }
408                         }
409
410                         n = strlen(buffer);
411                         vp_prints_value(buffer+n, sizeof(buffer) - n, vp, 1);
412
413                         envp[envlen++] = strdup(buffer);
414
415                         /*
416                          *      Don't add too many attributes.
417                          */
418                         if (envlen == (MAX_ENVP - 1)) break;
419                 }
420                 envp[envlen] = NULL;
421                 execve(argv[0], argv, envp);
422                 radlog(L_ERR, "Exec-Program: FAILED to execute %s: %s",
423                        argv[0], strerror(errno));
424                 exit(1);
425         }
426
427         /*
428          *      Parent process.
429          */
430         if (pid < 0) {
431                 radlog(L_ERR|L_CONS, "Couldn't fork %s: %s",
432                        argv[0], strerror(errno));
433                 return -1;
434         }
435
436         /*
437          *      We're not waiting, exit, and ignore any child's
438          *      status.
439          */
440         if (!exec_wait) {
441                 return 0;
442         }
443
444         /*
445          *      Close the FD to which the child writes it's data.
446          *
447          *      If we can't close it, then we close pd[0], and return an
448          *      error.
449          */
450         if (close(pd[1]) != 0) {
451                 radlog(L_ERR|L_CONS, "Can't close pipe: %s", strerror(errno));
452                 close(pd[0]);
453                 return -1;
454         }
455
456         /*
457          *      Read from the pipe until we doesn't get any more or
458          *      until the message is full.
459          */
460         done = 0;
461         left = sizeof(answer) - 1;
462         while (1) {
463                 status = read(pd[0], answer + done, left);
464                 /*
465                  *      Nothing more to read: stop.
466                  */
467                 if (status == 0) {
468                         break;
469                 }
470
471                 /*
472                  *      Error: See if we have to continue.
473                  */
474                 if (status < 0) {
475                         /*
476                          *      We were interrupted: continue reading.
477                          */
478                         if (errno == EINTR) {
479                                 continue;
480                         }
481
482                         /*
483                          *      There was another error.  Most likely
484                          *      The child process has finished, and
485                          *      exited.
486                          */
487                         break;
488                 }
489
490                 done += status;
491                 left -= status;
492                 if (left <= 0) break;
493         }
494         answer[done] = 0;
495
496         /*
497          *      Make sure that the writer can't block while writing to
498          *      a pipe that no one is reading from anymore.
499          */
500         close(pd[0]);
501
502         DEBUG2("Exec-Program output: %s", answer);
503
504         /*
505          *      Parse the output, if any.
506          */
507         if (done) {
508                 n = T_INVALID;
509                 if (output_pairs) {
510                         /*
511                          *      For backwards compatibility, first check
512                          *      for plain text (user_msg).
513                          */
514                         vp = NULL;
515                         n = userparse(answer, &vp);
516                         if (vp) {
517                                 pairfree(&vp);
518                         }
519                 }
520
521                 if (n == T_INVALID) {
522                         radlog(L_DBG, "Exec-Program-Wait: plaintext: %s", answer);
523                         if (user_msg) {
524                                 strNcpy(user_msg, answer, msg_len);
525                         }
526                 } else {
527                         /*
528                          *      HACK: Replace '\n' with ',' so that
529                          *      userparse() can parse the buffer in
530                          *      one go (the proper way would be to
531                          *      fix userparse(), but oh well).
532                          */
533                         for (p = answer; *p; p++) {
534                                 if (*p == '\n') {
535                                         *p = comma ? ' ' : ',';
536                                         p++;
537                                         comma = 0;
538                                 }
539                                 if (*p == ',') comma++;
540                         }
541
542                         /*
543                          *  Replace any trailing comma by a NUL.
544                          */
545                         if (answer[strlen(answer) - 1] == ',') {
546                                 answer[strlen(answer) - 1] = '\0';
547                         }
548
549                         radlog(L_DBG,"Exec-Program-Wait: value-pairs: %s", answer);
550                         if (userparse(answer, &vp) == T_INVALID) {
551                                 radlog(L_ERR, "Exec-Program-Wait: %s: unparsable reply", cmd);
552
553                         } else {
554                                 /*
555                                  *      Tell the caller about the value
556                                  *      pairs.
557                                  */
558                                 *output_pairs = vp;
559                         }
560                 } /* else the answer was a set of VP's, not a text message */
561         } /* else we didn't read anything from the child. */
562
563         /*
564          *      Call rad_waitpid (should map to waitpid on non-threaded
565          *      or single-server systems).
566          */
567         child_pid = rad_waitpid(pid, &status, 0);
568         if (child_pid == pid) {
569                 if (WIFEXITED(status)) {
570                         status = WEXITSTATUS(status);
571                         radlog(L_DBG, "Exec-Program: returned: %d", status);
572                         return status;
573                 }
574         }
575
576         radlog(L_ERR|L_CONS, "Exec-Program: Abnormal child exit: %s",
577                strerror(errno));
578         return 1;
579 }