parse_ini.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*****************************************************************************
  2. *
  3. * Monitoring Plugins parse_ini library
  4. *
  5. * License: GPL
  6. * Copyright (c) 2007 Monitoring Plugins Development Team
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. *****************************************************************************/
  23. #include "common.h"
  24. #include "utils_base.h"
  25. #include "parse_ini.h"
  26. #include <ctype.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. /* np_ini_info contains the result of parsing a "locator" in the format
  31. * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
  32. */
  33. typedef struct {
  34. char *file;
  35. char *stanza;
  36. } np_ini_info;
  37. static char *default_ini_file_names[] = {
  38. "monitoring-plugins.ini",
  39. "plugins.ini",
  40. "nagios-plugins.ini",
  41. NULL
  42. };
  43. static char *default_ini_path_names[] = {
  44. "/usr/local/etc/monitoring-plugins.ini",
  45. "/etc/monitoring-plugins.ini",
  46. /* deprecated path names (for backward compatibility): */
  47. "/etc/nagios/plugins.ini",
  48. "/usr/local/nagios/etc/plugins.ini",
  49. "/usr/local/etc/nagios/plugins.ini",
  50. "/etc/opt/nagios/plugins.ini",
  51. "/etc/nagios-plugins.ini",
  52. "/usr/local/etc/nagios-plugins.ini",
  53. "/etc/opt/nagios-plugins.ini",
  54. NULL
  55. };
  56. /* eat all characters from a FILE pointer until n is encountered */
  57. #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
  58. /* internal function that returns the constructed defaults options */
  59. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
  60. /* internal function that converts a single line into options format */
  61. static int add_option(FILE *f, np_arg_list **optlst);
  62. /* internal function to find default file */
  63. static char *default_file(void);
  64. /*
  65. * Parse_locator decomposes a string of the form
  66. * [stanza][@filename]
  67. * into its seperate parts.
  68. */
  69. static void
  70. parse_locator(const char *locator, const char *def_stanza, np_ini_info *i)
  71. {
  72. size_t locator_len = 0, stanza_len = 0;
  73. /* if locator is NULL we'll use default values */
  74. if (locator != NULL) {
  75. locator_len = strlen(locator);
  76. stanza_len = strcspn(locator, "@");
  77. }
  78. /* if a non-default stanza is provided */
  79. if (stanza_len > 0) {
  80. i->stanza = malloc(sizeof(char) * (stanza_len + 1));
  81. strncpy(i->stanza, locator, stanza_len);
  82. i->stanza[stanza_len] = '\0';
  83. } else /* otherwise we use the default stanza */
  84. i->stanza = strdup(def_stanza);
  85. if (i->stanza == NULL)
  86. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  87. /* check whether there's an @file part */
  88. i->file = stanza_len == locator_len
  89. ? default_file()
  90. : strdup(&(locator[stanza_len + 1]));
  91. if (i->file == NULL || i->file[0] == '\0')
  92. die(STATE_UNKNOWN,
  93. _("Cannot find config file in any standard location.\n"));
  94. }
  95. /*
  96. * This is the externally visible function used by extra_opts.
  97. */
  98. np_arg_list *
  99. np_get_defaults(const char *locator, const char *default_section)
  100. {
  101. FILE *inifile = NULL;
  102. np_arg_list *defaults = NULL;
  103. np_ini_info i;
  104. parse_locator(locator, default_section, &i);
  105. if (strcmp(i.file, "-") == 0)
  106. inifile = stdin;
  107. else
  108. inifile = fopen(i.file, "r");
  109. if (inifile == NULL)
  110. die(STATE_UNKNOWN, "%s\n", _("Can't read config file"));
  111. if (read_defaults(inifile, i.stanza, &defaults) == FALSE)
  112. die(STATE_UNKNOWN,
  113. _("Invalid section '%s' in config file '%s'\n"), i.stanza,
  114. i.file);
  115. free(i.file);
  116. if (inifile != stdin)
  117. fclose(inifile);
  118. free(i.stanza);
  119. return defaults;
  120. }
  121. /*
  122. * The read_defaults() function is where the meat of the parsing takes place.
  123. *
  124. * Note that this may be called by a setuid binary, so we need to
  125. * be extra careful about user-supplied input (i.e. avoiding possible
  126. * format string vulnerabilities, etc).
  127. */
  128. static int
  129. read_defaults(FILE *f, const char *stanza, np_arg_list **opts)
  130. {
  131. int c, status = FALSE;
  132. size_t i, stanza_len;
  133. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA;
  134. stanza_len = strlen(stanza);
  135. /* our little stanza-parsing state machine */
  136. while ((c = fgetc(f)) != EOF) {
  137. /* gobble up leading whitespace */
  138. if (isspace(c))
  139. continue;
  140. switch (c) {
  141. /* globble up coment lines */
  142. case ';':
  143. case '#':
  144. GOBBLE_TO(f, c, '\n');
  145. break;
  146. /* start of a stanza, check to see if it matches */
  147. case '[':
  148. stanzastate = WRONGSTANZA;
  149. for (i = 0; i < stanza_len; i++) {
  150. c = fgetc(f);
  151. /* strip leading whitespace */
  152. if (i == 0)
  153. for (; isspace(c); c = fgetc(f))
  154. continue;
  155. /* nope, read to the end of the line */
  156. if (c != stanza[i]) {
  157. GOBBLE_TO(f, c, '\n');
  158. break;
  159. }
  160. }
  161. /* if it matched up to here and the next char is ']'... */
  162. if (i == stanza_len) {
  163. c = fgetc(f);
  164. /* strip trailing whitespace */
  165. for (; isspace(c); c = fgetc(f))
  166. continue;
  167. if (c == ']')
  168. stanzastate = RIGHTSTANZA;
  169. }
  170. break;
  171. /* otherwise, we're in the body of a stanza or a parse error */
  172. default:
  173. switch (stanzastate) {
  174. /* we never found the start of the first stanza, so
  175. * we're dealing with a config error
  176. */
  177. case NOSTANZA:
  178. die(STATE_UNKNOWN, "%s\n",
  179. _("Config file error"));
  180. /* we're in a stanza, but for a different plugin */
  181. case WRONGSTANZA:
  182. GOBBLE_TO(f, c, '\n');
  183. break;
  184. /* okay, this is where we start taking the config */
  185. case RIGHTSTANZA:
  186. ungetc(c, f);
  187. if (add_option(f, opts)) {
  188. die(STATE_UNKNOWN, "%s\n",
  189. _("Config file error"));
  190. }
  191. status = TRUE;
  192. break;
  193. }
  194. break;
  195. }
  196. }
  197. return status;
  198. }
  199. /*
  200. * Read one line of input in the format
  201. * ^option[[:space:]]*(=[[:space:]]*value)?
  202. * and create it as a cmdline argument
  203. * --option[=value]
  204. * appending it to the linked list optbuf.
  205. */
  206. static int
  207. add_option(FILE *f, np_arg_list **optlst)
  208. {
  209. np_arg_list *opttmp = *optlst, *optnew;
  210. char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL;
  211. char *eqptr = NULL, *valptr = NULL, *valend = NULL;
  212. short done_reading = 0, equals = 0, value = 0;
  213. size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0;
  214. size_t opt_len = 0, val_len = 0;
  215. /* read one line from the file */
  216. while (!done_reading) {
  217. /* grow if necessary */
  218. if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) {
  219. linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz;
  220. linebuf = realloc(linebuf, linebuf_sz);
  221. if (linebuf == NULL)
  222. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  223. }
  224. if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL)
  225. done_reading = 1;
  226. else {
  227. read_pos = strlen(linebuf);
  228. if (linebuf[read_pos - 1] == '\n') {
  229. linebuf[--read_pos] = '\0';
  230. done_reading = 1;
  231. }
  232. }
  233. }
  234. lineend = &linebuf[read_pos];
  235. /* all that to read one line, isn't C fun? :) now comes the parsing :/ */
  236. /* skip leading whitespace */
  237. for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++)
  238. continue;
  239. /* continue to '=' or EOL, watching for spaces that might precede it */
  240. for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) {
  241. if (isspace(*eqptr) && optend == NULL)
  242. optend = eqptr;
  243. else
  244. optend = NULL;
  245. }
  246. if (optend == NULL)
  247. optend = eqptr;
  248. --optend;
  249. /* ^[[:space:]]*=foo is a syntax error */
  250. if (optptr == eqptr)
  251. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  252. /* continue from '=' to start of value or EOL */
  253. for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr);
  254. valptr++)
  255. continue;
  256. /* continue to the end of value */
  257. for (valend = valptr; valend < lineend; valend++)
  258. continue;
  259. --valend;
  260. /* finally trim off trailing spaces */
  261. for (; isspace(*valend); valend--)
  262. continue;
  263. /* calculate the length of "--foo" */
  264. opt_len = (size_t)(1 + optend - optptr);
  265. /* 1-character params needs only one dash */
  266. if (opt_len == 1)
  267. cfg_len = 1 + (opt_len);
  268. else
  269. cfg_len = 2 + (opt_len);
  270. /* if valptr<lineend then we have to also allocate space for "=bar" */
  271. if (valptr < lineend) {
  272. equals = value = 1;
  273. val_len = (size_t)(1 + valend - valptr);
  274. cfg_len += 1 + val_len;
  275. }
  276. /* if valptr==valend then we have "=" but no "bar" */
  277. else if (valptr == lineend) {
  278. equals = 1;
  279. cfg_len += 1;
  280. }
  281. /* a line with no equal sign isn't valid */
  282. if (equals == 0)
  283. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  284. /* okay, now we have all the info we need, so we create a new np_arg_list
  285. * element and set the argument...
  286. */
  287. optnew = malloc(sizeof(np_arg_list));
  288. optnew->next = NULL;
  289. read_pos = 0;
  290. optnew->arg = malloc(cfg_len + 1);
  291. /* 1-character params needs only one dash */
  292. if (opt_len == 1) {
  293. strncpy(&optnew->arg[read_pos], "-", 1);
  294. read_pos += 1;
  295. } else {
  296. strncpy(&optnew->arg[read_pos], "--", 2);
  297. read_pos += 2;
  298. }
  299. strncpy(&optnew->arg[read_pos], optptr, opt_len);
  300. read_pos += opt_len;
  301. if (value) {
  302. optnew->arg[read_pos++] = '=';
  303. strncpy(&optnew->arg[read_pos], valptr, val_len);
  304. read_pos += val_len;
  305. }
  306. optnew->arg[read_pos] = '\0';
  307. /* ...and put that to the end of the list */
  308. if (*optlst == NULL)
  309. *optlst = optnew;
  310. else {
  311. while (opttmp->next != NULL)
  312. opttmp = opttmp->next;
  313. opttmp->next = optnew;
  314. }
  315. free(linebuf);
  316. return 0;
  317. }
  318. static char *
  319. default_file_in_path(void)
  320. {
  321. char *config_path, **file;
  322. char *dir, *ini_file, *tokens;
  323. if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL)
  324. return NULL;
  325. if ((tokens = strdup(config_path)) == NULL)
  326. die(STATE_UNKNOWN, _("Insufficient Memory"));
  327. for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) {
  328. for (file = default_ini_file_names; *file != NULL; file++) {
  329. if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0)
  330. die(STATE_UNKNOWN, _("Insufficient Memory"));
  331. if (access(ini_file, F_OK) == 0) {
  332. free(tokens);
  333. return ini_file;
  334. }
  335. }
  336. }
  337. free(tokens);
  338. return NULL;
  339. }
  340. static char *
  341. default_file(void)
  342. {
  343. char **p, *ini_file;
  344. if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL ||
  345. (ini_file = default_file_in_path()) != NULL)
  346. return ini_file;
  347. for (p = default_ini_path_names; *p != NULL; p++)
  348. if (access(*p, F_OK) == 0)
  349. return *p;
  350. return NULL;
  351. }