parse_ini.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 functions to find default file */
  63. static char *default_file(void);
  64. static char *default_file_in_path(void);
  65. /*
  66. * Parse_locator decomposes a string of the form
  67. * [stanza][@filename]
  68. * into its seperate parts.
  69. */
  70. static void
  71. parse_locator(const char *locator, const char *def_stanza, np_ini_info *i)
  72. {
  73. size_t locator_len = 0, stanza_len = 0;
  74. /* if locator is NULL we'll use default values */
  75. if (locator != NULL) {
  76. locator_len = strlen(locator);
  77. stanza_len = strcspn(locator, "@");
  78. }
  79. /* if a non-default stanza is provided */
  80. if (stanza_len > 0) {
  81. i->stanza = malloc(sizeof(char) * (stanza_len + 1));
  82. strncpy(i->stanza, locator, stanza_len);
  83. i->stanza[stanza_len] = '\0';
  84. } else /* otherwise we use the default stanza */
  85. i->stanza = strdup(def_stanza);
  86. if (i->stanza == NULL)
  87. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  88. /* check whether there's an @file part */
  89. i->file = stanza_len == locator_len
  90. ? default_file()
  91. : strdup(&(locator[stanza_len + 1]));
  92. if (i->file == NULL || i->file[0] == '\0')
  93. die(STATE_UNKNOWN,
  94. _("Cannot find config file in any standard location.\n"));
  95. }
  96. /*
  97. * This is the externally visible function used by extra_opts.
  98. */
  99. np_arg_list *
  100. np_get_defaults(const char *locator, const char *default_section)
  101. {
  102. FILE *inifile = NULL;
  103. np_arg_list *defaults = NULL;
  104. np_ini_info i;
  105. parse_locator(locator, default_section, &i);
  106. if (strcmp(i.file, "-") == 0)
  107. inifile = stdin;
  108. else
  109. inifile = fopen(i.file, "r");
  110. if (inifile == NULL)
  111. die(STATE_UNKNOWN, "%s\n", _("Can't read config file"));
  112. if (read_defaults(inifile, i.stanza, &defaults) == FALSE)
  113. die(STATE_UNKNOWN,
  114. _("Invalid section '%s' in config file '%s'\n"), i.stanza,
  115. i.file);
  116. free(i.file);
  117. if (inifile != stdin)
  118. fclose(inifile);
  119. free(i.stanza);
  120. return defaults;
  121. }
  122. /*
  123. * The read_defaults() function is where the meat of the parsing takes place.
  124. *
  125. * Note that this may be called by a setuid binary, so we need to
  126. * be extra careful about user-supplied input (i.e. avoiding possible
  127. * format string vulnerabilities, etc).
  128. */
  129. static int
  130. read_defaults(FILE *f, const char *stanza, np_arg_list **opts)
  131. {
  132. int c, status = FALSE;
  133. size_t i, stanza_len;
  134. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA;
  135. stanza_len = strlen(stanza);
  136. /* our little stanza-parsing state machine */
  137. while ((c = fgetc(f)) != EOF) {
  138. /* gobble up leading whitespace */
  139. if (isspace(c))
  140. continue;
  141. switch (c) {
  142. /* globble up coment lines */
  143. case ';':
  144. case '#':
  145. GOBBLE_TO(f, c, '\n');
  146. break;
  147. /* start of a stanza, check to see if it matches */
  148. case '[':
  149. stanzastate = WRONGSTANZA;
  150. for (i = 0; i < stanza_len; i++) {
  151. c = fgetc(f);
  152. /* strip leading whitespace */
  153. if (i == 0)
  154. for (; isspace(c); c = fgetc(f))
  155. continue;
  156. /* nope, read to the end of the line */
  157. if (c != stanza[i]) {
  158. GOBBLE_TO(f, c, '\n');
  159. break;
  160. }
  161. }
  162. /* if it matched up to here and the next char is ']'... */
  163. if (i == stanza_len) {
  164. c = fgetc(f);
  165. /* strip trailing whitespace */
  166. for (; isspace(c); c = fgetc(f))
  167. continue;
  168. if (c == ']')
  169. stanzastate = RIGHTSTANZA;
  170. }
  171. break;
  172. /* otherwise, we're in the body of a stanza or a parse error */
  173. default:
  174. switch (stanzastate) {
  175. /* we never found the start of the first stanza, so
  176. * we're dealing with a config error
  177. */
  178. case NOSTANZA:
  179. die(STATE_UNKNOWN, "%s\n",
  180. _("Config file error"));
  181. /* we're in a stanza, but for a different plugin */
  182. case WRONGSTANZA:
  183. GOBBLE_TO(f, c, '\n');
  184. break;
  185. /* okay, this is where we start taking the config */
  186. case RIGHTSTANZA:
  187. ungetc(c, f);
  188. if (add_option(f, opts)) {
  189. die(STATE_UNKNOWN, "%s\n",
  190. _("Config file error"));
  191. }
  192. status = TRUE;
  193. break;
  194. }
  195. break;
  196. }
  197. }
  198. return status;
  199. }
  200. /*
  201. * Read one line of input in the format
  202. * ^option[[:space:]]*(=[[:space:]]*value)?
  203. * and create it as a cmdline argument
  204. * --option[=value]
  205. * appending it to the linked list optbuf.
  206. */
  207. static int
  208. add_option(FILE *f, np_arg_list **optlst)
  209. {
  210. np_arg_list *opttmp = *optlst, *optnew;
  211. char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL;
  212. char *eqptr = NULL, *valptr = NULL, *valend = NULL;
  213. short done_reading = 0, equals = 0, value = 0;
  214. size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0;
  215. size_t opt_len = 0, val_len = 0;
  216. /* read one line from the file */
  217. while (!done_reading) {
  218. /* grow if necessary */
  219. if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) {
  220. linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz;
  221. linebuf = realloc(linebuf, linebuf_sz);
  222. if (linebuf == NULL)
  223. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  224. }
  225. if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL)
  226. done_reading = 1;
  227. else {
  228. read_pos = strlen(linebuf);
  229. if (linebuf[read_pos - 1] == '\n') {
  230. linebuf[--read_pos] = '\0';
  231. done_reading = 1;
  232. }
  233. }
  234. }
  235. lineend = &linebuf[read_pos];
  236. /* all that to read one line, isn't C fun? :) now comes the parsing :/ */
  237. /* skip leading whitespace */
  238. for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++)
  239. continue;
  240. /* continue to '=' or EOL, watching for spaces that might precede it */
  241. for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) {
  242. if (isspace(*eqptr) && optend == NULL)
  243. optend = eqptr;
  244. else
  245. optend = NULL;
  246. }
  247. if (optend == NULL)
  248. optend = eqptr;
  249. --optend;
  250. /* ^[[:space:]]*=foo is a syntax error */
  251. if (optptr == eqptr)
  252. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  253. /* continue from '=' to start of value or EOL */
  254. for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr);
  255. valptr++)
  256. continue;
  257. /* continue to the end of value */
  258. for (valend = valptr; valend < lineend; valend++)
  259. continue;
  260. --valend;
  261. /* finally trim off trailing spaces */
  262. for (; isspace(*valend); valend--)
  263. continue;
  264. /* calculate the length of "--foo" */
  265. opt_len = (size_t)(1 + optend - optptr);
  266. /* 1-character params needs only one dash */
  267. if (opt_len == 1)
  268. cfg_len = 1 + (opt_len);
  269. else
  270. cfg_len = 2 + (opt_len);
  271. /* if valptr<lineend then we have to also allocate space for "=bar" */
  272. if (valptr < lineend) {
  273. equals = value = 1;
  274. val_len = (size_t)(1 + valend - valptr);
  275. cfg_len += 1 + val_len;
  276. }
  277. /* if valptr==valend then we have "=" but no "bar" */
  278. else if (valptr == lineend) {
  279. equals = 1;
  280. cfg_len += 1;
  281. }
  282. /* a line with no equal sign isn't valid */
  283. if (equals == 0)
  284. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  285. /* okay, now we have all the info we need, so we create a new np_arg_list
  286. * element and set the argument...
  287. */
  288. optnew = malloc(sizeof(np_arg_list));
  289. optnew->next = NULL;
  290. read_pos = 0;
  291. optnew->arg = malloc(cfg_len + 1);
  292. /* 1-character params needs only one dash */
  293. if (opt_len == 1) {
  294. strncpy(&optnew->arg[read_pos], "-", 1);
  295. read_pos += 1;
  296. } else {
  297. strncpy(&optnew->arg[read_pos], "--", 2);
  298. read_pos += 2;
  299. }
  300. strncpy(&optnew->arg[read_pos], optptr, opt_len);
  301. read_pos += opt_len;
  302. if (value) {
  303. optnew->arg[read_pos++] = '=';
  304. strncpy(&optnew->arg[read_pos], valptr, val_len);
  305. read_pos += val_len;
  306. }
  307. optnew->arg[read_pos] = '\0';
  308. /* ...and put that to the end of the list */
  309. if (*optlst == NULL)
  310. *optlst = optnew;
  311. else {
  312. while (opttmp->next != NULL)
  313. opttmp = opttmp->next;
  314. opttmp->next = optnew;
  315. }
  316. free(linebuf);
  317. return 0;
  318. }
  319. static char *
  320. default_file(void)
  321. {
  322. char **p, *ini_file;
  323. if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL ||
  324. (ini_file = default_file_in_path()) != NULL)
  325. return ini_file;
  326. for (p = default_ini_path_names; *p != NULL; p++)
  327. if (access(*p, F_OK) == 0)
  328. return *p;
  329. return NULL;
  330. }
  331. static char *
  332. default_file_in_path(void)
  333. {
  334. char *config_path, **file;
  335. char *dir, *ini_file, *tokens;
  336. if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL)
  337. return NULL;
  338. /* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */
  339. if ((tokens = strdup(config_path)) == NULL)
  340. die(STATE_UNKNOWN, _("Insufficient Memory"));
  341. for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) {
  342. for (file = default_ini_file_names; *file != NULL; file++) {
  343. if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0)
  344. die(STATE_UNKNOWN, _("Insufficient Memory"));
  345. if (access(ini_file, F_OK) == 0) {
  346. free(tokens);
  347. return ini_file;
  348. }
  349. }
  350. }
  351. free(tokens);
  352. return NULL;
  353. }