4
0

popen.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*****************************************************************************
  2. *
  3. * Nagios plugins popen
  4. *
  5. * License: GPL
  6. * Copyright (c) 2005-2007 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * A safe alternative to popen
  11. *
  12. * Provides spopen and spclose
  13. *
  14. * FILE * spopen(const char *);
  15. * int spclose(FILE *);
  16. *
  17. * Code taken with liitle modification from "Advanced Programming for the Unix
  18. * Environment" by W. Richard Stevens
  19. *
  20. * This is considered safe in that no shell is spawned, and the environment
  21. * and path passed to the exec'd program are essentially empty. (popen create
  22. * a shell and passes the environment to it).
  23. *
  24. *
  25. * This program is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License as published by
  27. * the Free Software Foundation, either version 3 of the License, or
  28. * (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  37. *
  38. *
  39. *****************************************************************************/
  40. #include "common.h"
  41. /* extern so plugin has pid to kill exec'd process on timeouts */
  42. extern int timeout_interval;
  43. extern pid_t *childpid;
  44. extern int *child_stderr_array;
  45. extern FILE *child_process;
  46. FILE *spopen (const char *);
  47. int spclose (FILE *);
  48. #ifdef REDHAT_SPOPEN_ERROR
  49. RETSIGTYPE popen_sigchld_handler (int);
  50. #endif
  51. RETSIGTYPE popen_timeout_alarm_handler (int);
  52. #include <stdarg.h> /* ANSI C header file */
  53. #include <fcntl.h>
  54. #include <limits.h>
  55. #include <sys/resource.h>
  56. #ifdef HAVE_SYS_WAIT_H
  57. #include <sys/wait.h>
  58. #endif
  59. #ifndef WEXITSTATUS
  60. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  61. #endif
  62. #ifndef WIFEXITED
  63. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  64. #endif
  65. /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
  66. #if defined(SIG_IGN) && !defined(SIG_ERR)
  67. #define SIG_ERR ((Sigfunc *)-1)
  68. #endif
  69. #define min(a,b) ((a) < (b) ? (a) : (b))
  70. #define max(a,b) ((a) > (b) ? (a) : (b))
  71. int open_max (void); /* {Prog openmax} */
  72. static void err_sys (const char *, ...) __attribute__((noreturn,format(printf, 1, 2)));
  73. char *rtrim (char *, const char *);
  74. char *pname = NULL; /* caller can set this from argv[0] */
  75. /*int *childerr = NULL;*//* ptr to array allocated at run-time */
  76. /*extern pid_t *childpid = NULL; *//* ptr to array allocated at run-time */
  77. static int maxfd; /* from our open_max(), {Prog openmax} */
  78. #ifdef REDHAT_SPOPEN_ERROR
  79. static volatile int childtermd = 0;
  80. #endif
  81. FILE *
  82. spopen (const char *cmdstring)
  83. {
  84. char *env[2];
  85. char *cmd = NULL;
  86. char **argv = NULL;
  87. char *str, *tmp;
  88. int argc;
  89. int i = 0, pfd[2], pfderr[2];
  90. pid_t pid;
  91. #ifdef RLIMIT_CORE
  92. /* do not leave core files */
  93. struct rlimit limit;
  94. getrlimit (RLIMIT_CORE, &limit);
  95. limit.rlim_cur = 0;
  96. setrlimit (RLIMIT_CORE, &limit);
  97. #endif
  98. env[0] = strdup("LC_ALL=C");
  99. env[1] = '\0';
  100. /* if no command was passed, return with no error */
  101. if (cmdstring == NULL)
  102. return (NULL);
  103. /* make copy of command string so strtok() doesn't silently modify it */
  104. /* (the calling program may want to access it later) */
  105. cmd = malloc (strlen (cmdstring) + 1);
  106. if (cmd == NULL)
  107. return NULL;
  108. strcpy (cmd, cmdstring);
  109. /* This is not a shell, so we don't handle "???" */
  110. if (strstr (cmdstring, "\""))
  111. return NULL;
  112. /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */
  113. if (strstr (cmdstring, " ' ") || strstr (cmdstring, "'''"))
  114. return NULL;
  115. /* there cannot be more args than characters */
  116. argc = strlen (cmdstring) + 1; /* add 1 for NULL termination */
  117. argv = malloc (sizeof(char*)*argc);
  118. if (argv == NULL) {
  119. printf ("%s\n", _("Could not malloc argv array in popen()"));
  120. return NULL;
  121. }
  122. /* loop to get arguments to command */
  123. while (cmd) {
  124. str = cmd;
  125. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  126. if (i >= argc - 2) {
  127. printf ("%s\n",_("CRITICAL - You need more args!!!"));
  128. return (NULL);
  129. }
  130. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  131. str++;
  132. if (!strstr (str, "'"))
  133. return NULL; /* balanced? */
  134. cmd = 1 + strstr (str, "'");
  135. str[strcspn (str, "'")] = 0;
  136. }
  137. else if (strcspn(str,"'") < strcspn (str, " \t\r\n")) {
  138. /* handle --option='foo bar' strings */
  139. tmp = str + strcspn(str, "'") + 1;
  140. if (!strstr (tmp, "'"))
  141. return NULL; /* balanced? */
  142. tmp += strcspn(tmp,"'") + 1;
  143. *tmp = 0;
  144. cmd = tmp + 1;
  145. } else {
  146. if (strpbrk (str, " \t\r\n")) {
  147. cmd = 1 + strpbrk (str, " \t\r\n");
  148. str[strcspn (str, " \t\r\n")] = 0;
  149. }
  150. else {
  151. cmd = NULL;
  152. }
  153. }
  154. if (cmd && strlen (cmd) == strspn (cmd, " \t\r\n"))
  155. cmd = NULL;
  156. argv[i++] = str;
  157. }
  158. argv[i] = NULL;
  159. if (childpid == NULL) { /* first time through */
  160. maxfd = open_max (); /* allocate zeroed out array for child pids */
  161. if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL)
  162. return (NULL);
  163. }
  164. if (child_stderr_array == NULL) { /* first time through */
  165. maxfd = open_max (); /* allocate zeroed out array for child pids */
  166. if ((child_stderr_array = calloc ((size_t)maxfd, sizeof (int))) == NULL)
  167. return (NULL);
  168. }
  169. if (pipe (pfd) < 0)
  170. return (NULL); /* errno set by pipe() */
  171. if (pipe (pfderr) < 0)
  172. return (NULL); /* errno set by pipe() */
  173. #ifdef REDHAT_SPOPEN_ERROR
  174. if (signal (SIGCHLD, popen_sigchld_handler) == SIG_ERR) {
  175. usage4 (_("Cannot catch SIGCHLD"));
  176. }
  177. #endif
  178. if ((pid = fork ()) < 0)
  179. return (NULL); /* errno set by fork() */
  180. else if (pid == 0) { /* child */
  181. close (pfd[0]);
  182. if (pfd[1] != STDOUT_FILENO) {
  183. dup2 (pfd[1], STDOUT_FILENO);
  184. close (pfd[1]);
  185. }
  186. close (pfderr[0]);
  187. if (pfderr[1] != STDERR_FILENO) {
  188. dup2 (pfderr[1], STDERR_FILENO);
  189. close (pfderr[1]);
  190. }
  191. /* close all descriptors in childpid[] */
  192. for (i = 0; i < maxfd; i++)
  193. if (childpid[i] > 0)
  194. close (i);
  195. execve (argv[0], argv, env);
  196. _exit (0);
  197. }
  198. close (pfd[1]); /* parent */
  199. if ((child_process = fdopen (pfd[0], "r")) == NULL)
  200. return (NULL);
  201. close (pfderr[1]);
  202. childpid[fileno (child_process)] = pid; /* remember child pid for this fd */
  203. child_stderr_array[fileno (child_process)] = pfderr[0]; /* remember STDERR */
  204. return (child_process);
  205. }
  206. int
  207. spclose (FILE * fp)
  208. {
  209. int fd, status;
  210. pid_t pid;
  211. if (childpid == NULL)
  212. return (1); /* popen() has never been called */
  213. fd = fileno (fp);
  214. if ((pid = childpid[fd]) == 0)
  215. return (1); /* fp wasn't opened by popen() */
  216. childpid[fd] = 0;
  217. if (fclose (fp) == EOF)
  218. return (1);
  219. #ifdef REDHAT_SPOPEN_ERROR
  220. while (!childtermd); /* wait until SIGCHLD */
  221. #endif
  222. while (waitpid (pid, &status, 0) < 0)
  223. if (errno != EINTR)
  224. return (1); /* error other than EINTR from waitpid() */
  225. if (WIFEXITED (status))
  226. return (WEXITSTATUS (status)); /* return child's termination status */
  227. return (1);
  228. }
  229. #ifdef OPEN_MAX
  230. static int openmax = OPEN_MAX;
  231. #else
  232. static int openmax = 0;
  233. #endif
  234. #define OPEN_MAX_GUESS 256 /* if OPEN_MAX is indeterminate */
  235. /* no guarantee this is adequate */
  236. #ifdef REDHAT_SPOPEN_ERROR
  237. RETSIGTYPE
  238. popen_sigchld_handler (int signo)
  239. {
  240. if (signo == SIGCHLD)
  241. childtermd = 1;
  242. }
  243. #endif
  244. RETSIGTYPE
  245. popen_timeout_alarm_handler (int signo)
  246. {
  247. int fh;
  248. if (signo == SIGALRM) {
  249. if (child_process != NULL) {
  250. fh=fileno (child_process);
  251. if(fh >= 0){
  252. kill (childpid[fh], SIGKILL);
  253. }
  254. printf (_("CRITICAL - Plugin timed out after %d seconds\n"),
  255. timeout_interval);
  256. } else {
  257. printf ("%s\n", _("CRITICAL - popen timeout received, but no child process"));
  258. }
  259. exit (STATE_CRITICAL);
  260. }
  261. }
  262. int
  263. open_max (void)
  264. {
  265. if (openmax == 0) { /* first time through */
  266. errno = 0;
  267. if ((openmax = sysconf (_SC_OPEN_MAX)) < 0) {
  268. if (errno == 0)
  269. openmax = OPEN_MAX_GUESS; /* it's indeterminate */
  270. else
  271. err_sys (_("sysconf error for _SC_OPEN_MAX"));
  272. }
  273. }
  274. return (openmax);
  275. }
  276. /* Fatal error related to a system call.
  277. * Print a message and die. */
  278. #define MAXLINE 2048
  279. static void
  280. err_sys (const char *fmt, ...)
  281. {
  282. int errnoflag = 1;
  283. int errno_save;
  284. char buf[MAXLINE];
  285. va_list ap;
  286. va_start (ap, fmt);
  287. /* err_doit (1, fmt, ap); */
  288. errno_save = errno; /* value caller might want printed */
  289. vsprintf (buf, fmt, ap);
  290. if (errnoflag)
  291. sprintf (buf + strlen (buf), ": %s", strerror (errno_save));
  292. strcat (buf, "\n");
  293. fflush (stdout); /* in case stdout and stderr are the same */
  294. fputs (buf, stderr);
  295. fflush (NULL); /* flushes all stdio output streams */
  296. va_end (ap);
  297. exit (1);
  298. }
  299. char *
  300. rtrim (char *str, const char *tok)
  301. {
  302. int i = 0;
  303. int j = sizeof (str);
  304. while (str != NULL && i < j) {
  305. if (*(str + i) == *tok) {
  306. sprintf (str + i, "%s", "\0");
  307. return str;
  308. }
  309. i++;
  310. }
  311. return str;
  312. }