parse_ini.c 11 KB

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