1 /*****************************************************************************
3 * Monitoring Plugins parse_ini library
6 * Copyright (c) 2007 Monitoring Plugins Development Team
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.
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.
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/>.
22 *****************************************************************************/
25 #include "utils_base.h"
26 #include "parse_ini.h"
29 #include <sys/types.h>
33 /* np_ini_info contains the result of parsing a "locator" in the format
34 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
41 static char *default_ini_file_names
[] = {
42 "monitoring-plugins.ini",
48 static char *default_ini_path_names
[] = {
49 "/usr/local/etc/monitoring-plugins.ini",
50 "/etc/monitoring-plugins.ini",
51 /* Deprecated path names (for backward compatibility): */
52 "/etc/nagios/plugins.ini",
53 "/usr/local/nagios/etc/plugins.ini",
54 "/usr/local/etc/nagios/plugins.ini",
55 "/etc/opt/nagios/plugins.ini",
56 "/etc/nagios-plugins.ini",
57 "/usr/local/etc/nagios-plugins.ini",
58 "/etc/opt/nagios-plugins.ini",
62 /* eat all characters from a FILE pointer until n is encountered */
63 #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
65 /* internal function that returns the constructed defaults options */
66 static int read_defaults(FILE *f
, const char *stanza
, np_arg_list
**opts
);
68 /* internal function that converts a single line into options format */
69 static int add_option(FILE *f
, np_arg_list
**optlst
);
71 /* internal function to find default file */
72 static char *default_file(void);
74 /* parse_locator decomposes a string of the form
76 * into its seperate parts
79 parse_locator(const char *locator
, const char *def_stanza
, np_ini_info
*i
)
81 size_t locator_len
= 0, stanza_len
= 0;
83 /* if locator is NULL we'll use default values */
84 if (locator
!= NULL
) {
85 locator_len
= strlen(locator
);
86 stanza_len
= strcspn(locator
, "@");
88 /* if a non-default stanza is provided */
90 i
->stanza
= malloc(sizeof(char) * (stanza_len
+ 1));
91 strncpy(i
->stanza
, locator
, stanza_len
);
92 i
->stanza
[stanza_len
] = '\0';
93 } else /* otherwise we use the default stanza */
94 i
->stanza
= strdup(def_stanza
);
96 if (i
->stanza
== NULL
)
97 die(STATE_UNKNOWN
, _("malloc() failed!\n"));
99 /* check whether there's an @file part */
100 i
->file
= stanza_len
== locator_len
102 : strdup(&(locator
[stanza_len
+ 1]));
103 if (i
->file
== NULL
|| i
->file
[0] == '\0')
105 _("Cannot find config file in any standard location.\n"));
108 /* this is the externally visible function used by extra_opts */
110 np_get_defaults(const char *locator
, const char *default_section
)
112 FILE *inifile
= NULL
;
113 np_arg_list
*defaults
= NULL
;
116 parse_locator(locator
, default_section
, &i
);
117 if (strcmp(i
.file
, "-") == 0)
120 inifile
= fopen(i
.file
, "r");
123 die(STATE_UNKNOWN
, "%s\n", _("Can't read config file"));
124 if (read_defaults(inifile
, i
.stanza
, &defaults
) == FALSE
)
126 _("Invalid section '%s' in config file '%s'\n"), i
.stanza
,
130 if (inifile
!= stdin
)
136 /* read_defaults is where the meat of the parsing takes place.
138 * note that this may be called by a setuid binary, so we need to
139 * be extra careful about user-supplied input (i.e. avoiding possible
140 * format string vulnerabilities, etc)
143 read_defaults(FILE *f
, const char *stanza
, np_arg_list
**opts
)
145 int c
, status
= FALSE
;
146 size_t i
, stanza_len
;
147 enum { NOSTANZA
, WRONGSTANZA
, RIGHTSTANZA
} stanzastate
= NOSTANZA
;
149 stanza_len
= strlen(stanza
);
151 /* our little stanza-parsing state machine. */
152 while ((c
= fgetc(f
)) != EOF
) {
153 /* gobble up leading whitespace */
157 /* globble up coment lines */
160 GOBBLE_TO(f
, c
, '\n');
162 /* start of a stanza. check to see if it matches */
164 stanzastate
= WRONGSTANZA
;
165 for (i
= 0; i
< stanza_len
; i
++) {
167 /* Strip leading whitespace */
169 for (c
; isspace(c
); c
= fgetc(f
))
171 /* nope, read to the end of the line */
172 if (c
!= stanza
[i
]) {
173 GOBBLE_TO(f
, c
, '\n');
177 /* if it matched up to here and the next char is ']'... */
178 if (i
== stanza_len
) {
180 /* Strip trailing whitespace */
181 for (c
; isspace(c
); c
= fgetc(f
))
184 stanzastate
= RIGHTSTANZA
;
187 /* otherwise, we're in the body of a stanza or a parse error */
189 switch (stanzastate
) {
190 /* we never found the start of the first stanza, so
191 * we're dealing with a config error
194 die(STATE_UNKNOWN
, "%s\n",
195 _("Config file error"));
197 /* we're in a stanza, but for a different plugin */
199 GOBBLE_TO(f
, c
, '\n');
201 /* okay, this is where we start taking the config */
204 if (add_option(f
, opts
)) {
205 die(STATE_UNKNOWN
, "%s\n",
206 _("Config file error"));
218 * read one line of input in the format
219 * ^option[[:space:]]*(=[[:space:]]*value)?
220 * and creates it as a cmdline argument
222 * appending it to the linked list optbuf.
225 add_option(FILE *f
, np_arg_list
**optlst
)
227 np_arg_list
*opttmp
= *optlst
, *optnew
;
228 char *linebuf
= NULL
, *lineend
= NULL
, *optptr
= NULL
, *optend
= NULL
;
229 char *eqptr
= NULL
, *valptr
= NULL
, *spaceptr
= NULL
, *valend
= NULL
;
230 short done_reading
= 0, equals
= 0, value
= 0;
231 size_t cfg_len
= 0, read_sz
= 8, linebuf_sz
= 0, read_pos
= 0;
232 size_t opt_len
= 0, val_len
= 0;
234 /* read one line from the file */
235 while (!done_reading
) {
236 /* grow if necessary */
237 if (linebuf
== NULL
|| read_pos
+ read_sz
>= linebuf_sz
) {
238 linebuf_sz
= linebuf_sz
> 0 ? linebuf_sz
<< 1 : read_sz
;
239 linebuf
= realloc(linebuf
, linebuf_sz
);
241 die(STATE_UNKNOWN
, _("malloc() failed!\n"));
243 if (fgets(&linebuf
[read_pos
], read_sz
, f
) == NULL
)
246 read_pos
= strlen(linebuf
);
247 if (linebuf
[read_pos
- 1] == '\n') {
248 linebuf
[--read_pos
] = '\0';
253 lineend
= &linebuf
[read_pos
];
254 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
256 /* skip leading whitespace */
257 for (optptr
= linebuf
; optptr
< lineend
&& isspace(*optptr
); optptr
++)
259 /* continue to '=' or EOL, watching for spaces that might precede it */
260 for (eqptr
= optptr
; eqptr
< lineend
&& *eqptr
!= '='; eqptr
++) {
261 if (isspace(*eqptr
) && optend
== NULL
)
269 /* ^[[:space:]]*=foo is a syntax error */
271 die(STATE_UNKNOWN
, "%s\n", _("Config file error"));
272 /* continue from '=' to start of value or EOL */
273 for (valptr
= eqptr
+ 1; valptr
< lineend
&& isspace(*valptr
);
276 /* continue to the end of value */
277 for (valend
= valptr
; valend
< lineend
; valend
++)
280 /* Finally trim off trailing spaces */
281 for (valend
; isspace(*valend
); valend
--)
283 /* calculate the length of "--foo" */
284 opt_len
= 1 + optend
- optptr
;
285 /* 1-character params needs only one dash */
287 cfg_len
= 1 + (opt_len
);
289 cfg_len
= 2 + (opt_len
);
290 /* if valptr<lineend then we have to also allocate space for "=bar" */
291 if (valptr
< lineend
) {
293 val_len
= 1 + valend
- valptr
;
294 cfg_len
+= 1 + val_len
;
296 /* if valptr==valend then we have "=" but no "bar" */
297 else if (valptr
== lineend
) {
301 /* A line with no equal sign isn't valid */
303 die(STATE_UNKNOWN
, "%s\n", _("Config file error"));
305 /* okay, now we have all the info we need, so we create a new np_arg_list
306 * element and set the argument...
308 optnew
= malloc(sizeof(np_arg_list
));
312 optnew
->arg
= malloc(cfg_len
+ 1);
313 /* 1-character params needs only one dash */
315 strncpy(&optnew
->arg
[read_pos
], "-", 1);
318 strncpy(&optnew
->arg
[read_pos
], "--", 2);
321 strncpy(&optnew
->arg
[read_pos
], optptr
, opt_len
);
324 optnew
->arg
[read_pos
++] = '=';
325 strncpy(&optnew
->arg
[read_pos
], valptr
, val_len
);
328 optnew
->arg
[read_pos
] = '\0';
330 /* ...and put that to the end of the list */
334 while (opttmp
->next
!= NULL
)
335 opttmp
= opttmp
->next
;
336 opttmp
->next
= optnew
;
344 default_file_in_path(void)
346 char *config_path
, **file
;
347 char *dir
, *ini_file
, *tokens
;
349 if ((config_path
= getenv("NAGIOS_CONFIG_PATH")) == NULL
)
352 if ((tokens
= strdup(config_path
)) == NULL
)
353 die(STATE_UNKNOWN
, _("Insufficient Memory"));
354 for (dir
= strtok(tokens
, ":"); dir
!= NULL
; dir
= strtok(NULL
, ":")) {
355 for (file
= default_ini_file_names
; *file
!= NULL
; file
++) {
356 if ((asprintf(&ini_file
, "%s/%s", dir
, *file
)) < 0)
357 die(STATE_UNKNOWN
, _("Insufficient Memory"));
358 if (access(ini_file
, F_OK
) == 0) {
373 if ((ini_file
= getenv("MP_CONFIG_FILE")) != NULL
||
374 (ini_file
= default_file_in_path()) != NULL
)
376 for (p
= default_ini_path_names
; *p
!= NULL
; p
++)
377 if (access(*p
, F_OK
) == 0)