reduce scope of variable. Found by PVS-Studio
[freeradius.git] / src / main / exec.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16
17 /*
18  * $Id$
19  *
20  * @file exec.c
21  * @brief Execute external programs.
22  *
23  * @copyright 2000-2004,2006  The FreeRADIUS server project
24  */
25
26 RCSID("$Id$")
27
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
30
31 #include <sys/file.h>
32
33 #include <fcntl.h>
34 #include <ctype.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 #define MAX_ARGV (256)
47
48 #define USEC 1000000
49 static void tv_sub(struct timeval *end, struct timeval *start,
50                    struct timeval *elapsed)
51 {
52         elapsed->tv_sec = end->tv_sec - start->tv_sec;
53         if (elapsed->tv_sec > 0) {
54                 elapsed->tv_sec--;
55                 elapsed->tv_usec = USEC;
56         } else {
57                 elapsed->tv_usec = 0;
58         }
59         elapsed->tv_usec += end->tv_usec;
60         elapsed->tv_usec -= start->tv_usec;
61
62         if (elapsed->tv_usec >= USEC) {
63                 elapsed->tv_usec -= USEC;
64                 elapsed->tv_sec++;
65         }
66 }
67
68
69 /** Start a process
70  *
71  * @param cmd Command to execute. This is parsed into argv[] parts,
72  *      then each individual argv part is xlat'ed.
73  * @param request Current reuqest
74  * @param exec_wait set to 1 if you want to read from or write to child
75  * @param[in,out] input_fd pointer to int, receives the stdin file.
76  *      descriptor. Set to NULL and the child will have /dev/null on stdin
77  * @param[in,out] output_fd pinter to int, receives the stdout file
78  *      descriptor. Set to NULL and child will have /dev/null on stdout.
79  * @param input_pairs list of value pairs - these will be put into
80  *      the environment variables of the child.
81  * @param shell_escape values before passing them as arguments.
82  * @return PID of the child process, -1 on error.
83  */
84 pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
85                            int *input_fd, int *output_fd,
86                            VALUE_PAIR *input_pairs, bool shell_escape)
87 {
88 #ifndef __MINGW32__
89         VALUE_PAIR *vp;
90         int n;
91         int to_child[2] = {-1, -1};
92         int from_child[2] = {-1, -1};
93         pid_t pid;
94 #endif
95         int             argc;
96         int             i;
97         char const      **argv_p;
98         char            *argv[MAX_ARGV], **argv_start = argv;
99         char            argv_buf[4096];
100 #define MAX_ENVP 1024
101         char *envp[MAX_ENVP];
102         int envlen = 0;
103
104         /*
105          *      Stupid array decomposition...
106          *
107          *      If we do memcpy(&argv_p, &argv, sizeof(argv_p)) src ends up being a char **
108          *      pointing to the value of the first element.
109          */
110         memcpy(&argv_p, &argv_start, sizeof(argv_p));
111         argc = rad_expand_xlat(request, cmd, MAX_ARGV, argv_p, true, sizeof(argv_buf), argv_buf);
112         if (argc <= 0) {
113                 DEBUG("invalid command line '%s'.", cmd);
114                 return -1;
115         }
116
117
118 #ifndef NDEBUG
119         if (rad_debug_lvl > 2) {
120                 DEBUG3("executing cmd %s", cmd);
121                 for (i = 0; i < argc; i++) {
122                         DEBUG3("\t[%d] %s", i, argv[i]);
123                 }
124         }
125 #endif
126
127 #ifndef __MINGW32__
128         /*
129          *      Open a pipe for child/parent communication, if necessary.
130          */
131         if (exec_wait) {
132                 if (input_fd) {
133                         if (pipe(to_child) != 0) {
134                                 DEBUG("Couldn't open pipe to child: %s", fr_syserror(errno));
135                                 return -1;
136                         }
137                 }
138                 if (output_fd) {
139                         if (pipe(from_child) != 0) {
140                                 DEBUG("Couldn't open pipe from child: %s", fr_syserror(errno));
141                                 /* safe because these either need closing or are == -1 */
142                                 close(to_child[0]);
143                                 close(to_child[1]);
144                                 return -1;
145                         }
146                 }
147         }
148
149         envp[0] = NULL;
150
151         if (input_pairs) {
152                 vp_cursor_t cursor;
153                 char buffer[1024];
154
155                 /*
156                  *      Set up the environment variables in the
157                  *      parent, so we don't call libc functions that
158                  *      hold mutexes.  They might be locked when we fork,
159                  *      and will remain locked in the child.
160                  */
161                 for (vp = fr_cursor_init(&cursor, &input_pairs);
162                      vp;
163                      vp = fr_cursor_next(&cursor)) {
164                         /*
165                          *      Hmm... maybe we shouldn't pass the
166                          *      user's password in an environment
167                          *      variable...
168                          */
169                         snprintf(buffer, sizeof(buffer), "%s=", vp->da->name);
170                         if (shell_escape) {
171                                 char *p;
172
173                                 for (p = buffer; *p != '='; p++) {
174                                         if (*p == '-') {
175                                                 *p = '_';
176                                         } else if (isalpha((int) *p)) {
177                                                 *p = toupper(*p);
178                                         }
179                                 }
180                         }
181
182                         n = strlen(buffer);
183                         vp_prints_value(buffer + n, sizeof(buffer) - n, vp, shell_escape ? '"' : 0);
184
185                         envp[envlen++] = strdup(buffer);
186
187                         /*
188                          *      Don't add too many attributes.
189                          */
190                         if (envlen == (MAX_ENVP - 1)) break;
191
192                         /*
193                          *      NULL terminate for execve
194                          */
195                         envp[envlen] = NULL;
196                 }
197         }
198
199         if (exec_wait) {
200                 pid = rad_fork();       /* remember PID */
201         } else {
202                 pid = fork();           /* don't wait */
203         }
204
205         if (pid == 0) {
206                 int devnull;
207
208                 /*
209                  *      Child process.
210                  *
211                  *      We try to be fail-safe here. So if ANYTHING
212                  *      goes wrong, we exit with status 1.
213                  */
214
215                 /*
216                  *      Open STDIN to /dev/null
217                  */
218                 devnull = open("/dev/null", O_RDWR);
219                 if (devnull < 0) {
220                         DEBUG("Failed opening /dev/null: %s\n", fr_syserror(errno));
221
222                         /*
223                          *      Where the status code is interpreted as a module rcode
224                          *      one is subtracted from it, to allow 0 to equal success
225                          *
226                          *      2 is RLM_MODULE_FAIL + 1
227                          */
228                         exit(2);
229                 }
230
231                 /*
232                  *      Only massage the pipe handles if the parent
233                  *      has created them.
234                  */
235                 if (exec_wait) {
236                         if (input_fd) {
237                                 close(to_child[1]);
238                                 dup2(to_child[0], STDIN_FILENO);
239                         } else {
240                                 dup2(devnull, STDIN_FILENO);
241                         }
242
243                         if (output_fd) {
244                                 close(from_child[0]);
245                                 dup2(from_child[1], STDOUT_FILENO);
246                         } else {
247                                 dup2(devnull, STDOUT_FILENO);
248                         }
249
250                 } else {        /* no pipe, STDOUT should be /dev/null */
251                         dup2(devnull, STDIN_FILENO);
252                         dup2(devnull, STDOUT_FILENO);
253                 }
254
255                 /*
256                  *      If we're not debugging, then we can't do
257                  *      anything with the error messages, so we throw
258                  *      them away.
259                  *
260                  *      If we are debugging, then we want the error
261                  *      messages to go to the STDERR of the server.
262                  */
263                 if (rad_debug_lvl == 0) {
264                         dup2(devnull, STDERR_FILENO);
265                 }
266                 close(devnull);
267
268                 /*
269                  *      The server may have MANY FD's open.  We don't
270                  *      want to leave dangling FD's for the child process
271                  *      to play funky games with, so we close them.
272                  */
273                 closefrom(3);
274
275                 /*
276                  *      I swear the signature for execve is wrong and should
277                  *      take 'char const * const argv[]'.
278                  *
279                  *      Note: execve(), unlike system(), treats all the space
280                  *      delimited arguments as literals, so there's no need
281                  *      to perform additional escaping.
282                  */
283                 execve(argv[0], argv, envp);
284                 printf("Failed to execute \"%s\": %s", argv[0], fr_syserror(errno)); /* fork output will be captured */
285
286                 /*
287                  *      Where the status code is interpreted as a module rcode
288                  *      one is subtracted from it, to allow 0 to equal success
289                  *
290                  *      2 is RLM_MODULE_FAIL + 1
291                  */
292                 exit(2);
293         }
294
295         /*
296          *      Free child environment variables
297          */
298         for (i = 0; i < envlen; i++) {
299                 free(envp[i]);
300         }
301
302         /*
303          *      Parent process.
304          */
305         if (pid < 0) {
306                 DEBUG("Couldn't fork %s: %s", argv[0], fr_syserror(errno));
307                 if (exec_wait) {
308                         /* safe because these either need closing or are == -1 */
309                         close(to_child[0]);
310                         close(to_child[1]);
311                         close(from_child[0]);
312                         close(from_child[1]);
313                 }
314                 return -1;
315         }
316
317         /*
318          *      We're not waiting, exit, and ignore any child's status.
319          */
320         if (exec_wait) {
321                 /*
322                  *      Close the ends of the pipe(s) the child is using
323                  *      return the ends of the pipe(s) our caller wants
324                  *
325                  */
326                 if (input_fd) {
327                         *input_fd = to_child[1];
328                         close(to_child[0]);
329                 }
330                 if (output_fd) {
331                         *output_fd = from_child[0];
332                         close(from_child[1]);
333                 }
334         }
335
336         return pid;
337 #else
338         if (exec_wait) {
339                 DEBUG("Wait is not supported");
340                 return -1;
341         }
342
343         {
344                 /*
345                  *      The _spawn and _exec families of functions are
346                  *      found in Windows compiler libraries for
347                  *      portability from UNIX. There is a variety of
348                  *      functions, including the ability to pass
349                  *      either a list or array of parameters, to
350                  *      search in the PATH or otherwise, and whether
351                  *      or not to pass an environment (a set of
352                  *      environment variables). Using _spawn, you can
353                  *      also specify whether you want the new process
354                  *      to close your program (_P_OVERLAY), to wait
355                  *      until the new process is finished (_P_WAIT) or
356                  *      for the two to run concurrently (_P_NOWAIT).
357
358                  *      _spawn and _exec are useful for instances in
359                  *      which you have simple requirements for running
360                  *      the program, don't want the overhead of the
361                  *      Windows header file, or are interested
362                  *      primarily in portability.
363                  */
364
365                 /*
366                  *      FIXME: check return code... what is it?
367                  */
368                 _spawnve(_P_NOWAIT, argv[0], argv, envp);
369         }
370
371         return 0;
372 #endif
373 }
374
375 /** Read from the child process.
376  *
377  * @param fd file descriptor to read from.
378  * @param pid pid of child, will be reaped if it dies.
379  * @param timeout amount of time to wait, in seconds.
380  * @param answer buffer to write into.
381  * @param left length of buffer.
382  * @return -1 on error, or length of output.
383  */
384 int radius_readfrom_program(int fd, pid_t pid, int timeout,
385                             char *answer, int left)
386 {
387         int done = 0;
388 #ifndef __MINGW32__
389         int status;
390         struct timeval start;
391 #ifdef O_NONBLOCK
392         bool nonblock = true;
393 #endif
394
395 #ifdef O_NONBLOCK
396         /*
397          *      Try to set it non-blocking.
398          */
399         do {
400                 int flags;
401
402                 if ((flags = fcntl(fd, F_GETFL, NULL)) < 0)  {
403                         nonblock = false;
404                         break;
405                 }
406
407                 flags |= O_NONBLOCK;
408                 if( fcntl(fd, F_SETFL, flags) < 0) {
409                         nonblock = false;
410                         break;
411                 }
412         } while (0);
413 #endif
414
415
416         /*
417          *      Read from the pipe until we doesn't get any more or
418          *      until the message is full.
419          */
420         gettimeofday(&start, NULL);
421         while (1) {
422                 int rcode;
423                 fd_set fds;
424                 struct timeval when, elapsed, wake;
425
426                 FD_ZERO(&fds);
427                 FD_SET(fd, &fds);
428
429                 gettimeofday(&when, NULL);
430                 tv_sub(&when, &start, &elapsed);
431                 if (elapsed.tv_sec >= timeout) goto too_long;
432
433                 when.tv_sec = timeout;
434                 when.tv_usec = 0;
435                 tv_sub(&when, &elapsed, &wake);
436
437                 rcode = select(fd + 1, &fds, NULL, NULL, &wake);
438                 if (rcode == 0) {
439                 too_long:
440                         DEBUG("Child PID %u is taking too much time: forcing failure and killing child.", (unsigned int) pid);
441                         kill(pid, SIGTERM);
442                         close(fd); /* should give SIGPIPE to child, too */
443
444                         /*
445                          *      Clean up the child entry.
446                          */
447                         rad_waitpid(pid, &status);
448                         return -1;
449                 }
450                 if (rcode < 0) {
451                         if (errno == EINTR) continue;
452                         break;
453                 }
454
455 #ifdef O_NONBLOCK
456                 /*
457                  *      Read as many bytes as possible.  The kernel
458                  *      will return the number of bytes available.
459                  */
460                 if (nonblock) {
461                         status = read(fd, answer + done, left);
462                 } else
463 #endif
464                         /*
465                          *      There's at least 1 byte ready: read it.
466                          */
467                         status = read(fd, answer + done, 1);
468
469                 /*
470                  *      Nothing more to read: stop.
471                  */
472                 if (status == 0) {
473                         break;
474                 }
475
476                 /*
477                  *      Error: See if we have to continue.
478                  */
479                 if (status < 0) {
480                         /*
481                          *      We were interrupted: continue reading.
482                          */
483                         if (errno == EINTR) {
484                                 continue;
485                         }
486
487                         /*
488                          *      There was another error.  Most likely
489                          *      The child process has finished, and
490                          *      exited.
491                          */
492                         break;
493                 }
494
495                 done += status;
496                 left -= status;
497                 if (left <= 0) break;
498         }
499 #endif  /* __MINGW32__ */
500
501         /* Strip trailing new lines */
502         while ((done > 0) && (answer[done - 1] == '\n')) {
503                 answer[--done] = '\0';
504         }
505
506         return done;
507 }
508
509 /** Execute a program.
510  *
511  * @param[in,out] ctx to allocate new VALUE_PAIR (s) in.
512  * @param[out] out buffer to append plaintext (non valuepair) output.
513  * @param[in] outlen length of out buffer.
514  * @param[out] output_pairs list of value pairs - child stdout will be parsed and added into this list
515  *      of value pairs.
516  * @param[in] request Current request (may be NULL).
517  * @param[in] cmd Command to execute. This is parsed into argv[] parts, then each individual argv part
518  *      is xlat'ed.
519  * @param[in] input_pairs list of value pairs - these will be available in the environment of the child.
520  * @param[in] exec_wait set to 1 if you want to read from or write to child.
521  * @param[in] shell_escape values before passing them as arguments.
522  * @param[in] timeout amount of time to wait, in seconds.
523
524  * @return 0 if exec_wait==0, exit code if exec_wait!=0, -1 on error.
525  */
526 int radius_exec_program(TALLOC_CTX *ctx, char *out, size_t outlen, VALUE_PAIR **output_pairs,
527                         REQUEST *request, char const *cmd, VALUE_PAIR *input_pairs,
528                         bool exec_wait, bool shell_escape, int timeout)
529
530 {
531         pid_t pid;
532         int from_child;
533 #ifndef __MINGW32__
534         char *p;
535         pid_t child_pid;
536         int comma = 0;
537         int status, ret = 0;
538         ssize_t len;
539         char answer[4096];
540 #endif
541
542         RDEBUG2("Executing: %s:", cmd);
543
544         if (out) *out = '\0';
545
546         pid = radius_start_program(cmd, request, exec_wait, NULL, &from_child, input_pairs, shell_escape);
547         if (pid < 0) {
548                 return -1;
549         }
550
551         if (!exec_wait) {
552                 return 0;
553         }
554
555 #ifndef __MINGW32__
556         len = radius_readfrom_program(from_child, pid, timeout, answer, sizeof(answer));
557         if (len < 0) {
558                 /*
559                  *      Failure - radius_readfrom_program will
560                  *      have called close(from_child) for us
561                  */
562                 RERROR("Failed to read from child output");
563                 return -1;
564
565         }
566         answer[len] = '\0';
567
568         /*
569          *      Make sure that the writer can't block while writing to
570          *      a pipe that no one is reading from anymore.
571          */
572         close(from_child);
573
574         if (len == 0) {
575                 goto wait;
576         }
577
578         /*
579          *      Parse the output, if any.
580          */
581         if (output_pairs) {
582                 /*
583                  *      HACK: Replace '\n' with ',' so that
584                  *      fr_pair_list_afrom_str() can parse the buffer in
585                  *      one go (the proper way would be to
586                  *      fix fr_pair_list_afrom_str(), but oh well).
587                  */
588                 for (p = answer; *p; p++) {
589                         if (*p == '\n') {
590                                 *p = comma ? ' ' : ',';
591                                 p++;
592                                 comma = 0;
593                         }
594                         if (*p == ',') {
595                                 comma++;
596                         }
597                 }
598
599                 /*
600                  *      Replace any trailing comma by a NUL.
601                  */
602                 if (answer[len - 1] == ',') {
603                         answer[--len] = '\0';
604                 }
605
606                 if (fr_pair_list_afrom_str(ctx, answer, output_pairs) == T_INVALID) {
607                         RERROR("Failed parsing output from: %s: %s", cmd, fr_strerror());
608                         strlcpy(out, answer, len);
609                         ret = -1;
610                 }
611         /*
612          *      We've not been told to extract output pairs,
613          *      just copy the programs output to the out
614          *      buffer.
615          */
616
617         } else if (out) {
618                 strlcpy(out, answer, outlen);
619         }
620
621         /*
622          *      Call rad_waitpid (should map to waitpid on non-threaded
623          *      or single-server systems).
624          */
625 wait:
626         child_pid = rad_waitpid(pid, &status);
627         if (child_pid == 0) {
628                 RERROR("Timeout waiting for child");
629
630                 return -2;
631         }
632
633         if (child_pid == pid) {
634                 if (WIFEXITED(status)) {
635                         status = WEXITSTATUS(status);
636                         if ((status != 0) || (ret < 0)) {
637                                 RERROR("Program returned code (%d) and output '%s'", status, answer);
638                         } else {
639                                 RDEBUG2("Program returned code (%d) and output '%s'", status, answer);
640                         }
641
642                         return ret < 0 ? ret : status;
643                 }
644         }
645
646         RERROR("Abnormal child exit: %s", fr_syserror(errno));
647 #endif  /* __MINGW32__ */
648
649         return -1;
650 }