diff options
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r-- | lib/parse_ini.c | 134 |
1 files changed, 50 insertions, 84 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 09c0dc4f..150df7b2 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c | |||
@@ -1,25 +1,25 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring Plugins parse_ini library | 3 | * Monitoring Plugins parse_ini library |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2007 Monitoring Plugins Development Team | 6 | * Copyright (c) 2007 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * This program is free software: you can redistribute it and/or modify | 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 | 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 | 10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. | 11 | * (at your option) any later version. |
12 | * | 12 | * |
13 | * This program is distributed in the hope that it will be useful, | 13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. | 16 | * GNU General Public License for more details. |
17 | * | 17 | * |
18 | * You should have received a copy of the GNU General Public License | 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/>. | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * | 20 | * |
21 | * | 21 | * |
22 | *****************************************************************************/ | 22 | *****************************************************************************/ |
23 | 23 | ||
24 | #include "common.h" | 24 | #include "common.h" |
25 | #include "idpriv.h" | 25 | #include "idpriv.h" |
@@ -40,31 +40,19 @@ typedef struct { | |||
40 | char *stanza; | 40 | char *stanza; |
41 | } np_ini_info; | 41 | } np_ini_info; |
42 | 42 | ||
43 | static char *default_ini_file_names[] = { | 43 | static char *default_ini_file_names[] = {"monitoring-plugins.ini", "plugins.ini", "nagios-plugins.ini", NULL}; |
44 | "monitoring-plugins.ini", | 44 | |
45 | "plugins.ini", | 45 | static char *default_ini_path_names[] = {"/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", "/usr/local/etc/monitoring-plugins.ini", |
46 | "nagios-plugins.ini", | 46 | "/etc/monitoring-plugins/monitoring-plugins.ini", "/etc/monitoring-plugins.ini", |
47 | NULL | 47 | /* deprecated path names (for backward compatibility): */ |
48 | }; | 48 | "/etc/nagios/plugins.ini", "/usr/local/nagios/etc/plugins.ini", "/usr/local/etc/nagios/plugins.ini", "/etc/opt/nagios/plugins.ini", |
49 | 49 | "/etc/nagios-plugins.ini", "/usr/local/etc/nagios-plugins.ini", "/etc/opt/nagios-plugins.ini", NULL}; | |
50 | static char *default_ini_path_names[] = { | ||
51 | "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", | ||
52 | "/usr/local/etc/monitoring-plugins.ini", | ||
53 | "/etc/monitoring-plugins/monitoring-plugins.ini", | ||
54 | "/etc/monitoring-plugins.ini", | ||
55 | /* deprecated path names (for backward compatibility): */ | ||
56 | "/etc/nagios/plugins.ini", | ||
57 | "/usr/local/nagios/etc/plugins.ini", | ||
58 | "/usr/local/etc/nagios/plugins.ini", | ||
59 | "/etc/opt/nagios/plugins.ini", | ||
60 | "/etc/nagios-plugins.ini", | ||
61 | "/usr/local/etc/nagios-plugins.ini", | ||
62 | "/etc/opt/nagios-plugins.ini", | ||
63 | NULL | ||
64 | }; | ||
65 | 50 | ||
66 | /* eat all characters from a FILE pointer until n is encountered */ | 51 | /* eat all characters from a FILE pointer until n is encountered */ |
67 | #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n)) | 52 | #define GOBBLE_TO(f, c, n) \ |
53 | do { \ | ||
54 | (c) = fgetc((f)); \ | ||
55 | } while ((c) != EOF && (c) != (n)) | ||
68 | 56 | ||
69 | /* internal function that returns the constructed defaults options */ | 57 | /* internal function that returns the constructed defaults options */ |
70 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); | 58 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); |
@@ -81,9 +69,7 @@ static char *default_file_in_path(void); | |||
81 | * [stanza][@filename] | 69 | * [stanza][@filename] |
82 | * into its separate parts. | 70 | * into its separate parts. |
83 | */ | 71 | */ |
84 | static void | 72 | static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) { |
85 | parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | ||
86 | { | ||
87 | size_t locator_len = 0, stanza_len = 0; | 73 | size_t locator_len = 0, stanza_len = 0; |
88 | 74 | ||
89 | /* if locator is NULL we'll use default values */ | 75 | /* if locator is NULL we'll use default values */ |
@@ -96,7 +82,7 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
96 | i->stanza = malloc(sizeof(char) * (stanza_len + 1)); | 82 | i->stanza = malloc(sizeof(char) * (stanza_len + 1)); |
97 | strncpy(i->stanza, locator, stanza_len); | 83 | strncpy(i->stanza, locator, stanza_len); |
98 | i->stanza[stanza_len] = '\0'; | 84 | i->stanza[stanza_len] = '\0'; |
99 | } else {/* otherwise we use the default stanza */ | 85 | } else { /* otherwise we use the default stanza */ |
100 | i->stanza = strdup(def_stanza); | 86 | i->stanza = strdup(def_stanza); |
101 | } | 87 | } |
102 | 88 | ||
@@ -105,7 +91,7 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
105 | 91 | ||
106 | /* check whether there's an @file part */ | 92 | /* check whether there's an @file part */ |
107 | if (stanza_len == locator_len) { | 93 | if (stanza_len == locator_len) { |
108 | i->file = default_file(); | 94 | i->file = default_file(); |
109 | i->file_string_on_heap = false; | 95 | i->file_string_on_heap = false; |
110 | } else { | 96 | } else { |
111 | i->file = strdup(&(locator[stanza_len + 1])); | 97 | i->file = strdup(&(locator[stanza_len + 1])); |
@@ -113,35 +99,28 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
113 | } | 99 | } |
114 | 100 | ||
115 | if (i->file == NULL || i->file[0] == '\0') | 101 | if (i->file == NULL || i->file[0] == '\0') |
116 | die(STATE_UNKNOWN, | 102 | die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); |
117 | _("Cannot find config file in any standard location.\n")); | ||
118 | } | 103 | } |
119 | 104 | ||
120 | /* | 105 | /* |
121 | * This is the externally visible function used by extra_opts. | 106 | * This is the externally visible function used by extra_opts. |
122 | */ | 107 | */ |
123 | np_arg_list * | 108 | np_arg_list *np_get_defaults(const char *locator, const char *default_section) { |
124 | np_get_defaults(const char *locator, const char *default_section) | ||
125 | { | ||
126 | FILE *inifile = NULL; | 109 | FILE *inifile = NULL; |
127 | np_arg_list *defaults = NULL; | 110 | np_arg_list *defaults = NULL; |
128 | np_ini_info i; | 111 | np_ini_info i; |
129 | int is_suid_plugin = mp_suid(); | 112 | int is_suid_plugin = mp_suid(); |
130 | 113 | ||
131 | if (is_suid_plugin && idpriv_temp_drop() == -1) | 114 | if (is_suid_plugin && idpriv_temp_drop() == -1) |
132 | die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), | 115 | die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), strerror(errno)); |
133 | strerror(errno)); | ||
134 | 116 | ||
135 | parse_locator(locator, default_section, &i); | 117 | parse_locator(locator, default_section, &i); |
136 | inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); | 118 | inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); |
137 | 119 | ||
138 | if (inifile == NULL) | 120 | if (inifile == NULL) |
139 | die(STATE_UNKNOWN, _("Can't read config file: %s\n"), | 121 | die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); |
140 | strerror(errno)); | ||
141 | if (!read_defaults(inifile, i.stanza, &defaults)) | 122 | if (!read_defaults(inifile, i.stanza, &defaults)) |
142 | die(STATE_UNKNOWN, | 123 | die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); |
143 | _("Invalid section '%s' in config file '%s'\n"), i.stanza, | ||
144 | i.file); | ||
145 | 124 | ||
146 | if (i.file_string_on_heap) { | 125 | if (i.file_string_on_heap) { |
147 | free(i.file); | 126 | free(i.file); |
@@ -151,8 +130,7 @@ np_get_defaults(const char *locator, const char *default_section) | |||
151 | fclose(inifile); | 130 | fclose(inifile); |
152 | free(i.stanza); | 131 | free(i.stanza); |
153 | if (is_suid_plugin && idpriv_temp_restore() == -1) | 132 | if (is_suid_plugin && idpriv_temp_restore() == -1) |
154 | die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), | 133 | die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), strerror(errno)); |
155 | strerror(errno)); | ||
156 | 134 | ||
157 | return defaults; | 135 | return defaults; |
158 | } | 136 | } |
@@ -164,9 +142,7 @@ np_get_defaults(const char *locator, const char *default_section) | |||
164 | * be extra careful about user-supplied input (i.e. avoiding possible | 142 | * be extra careful about user-supplied input (i.e. avoiding possible |
165 | * format string vulnerabilities, etc). | 143 | * format string vulnerabilities, etc). |
166 | */ | 144 | */ |
167 | static int | 145 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { |
168 | read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | ||
169 | { | ||
170 | int c = 0; | 146 | int c = 0; |
171 | bool status = false; | 147 | bool status = false; |
172 | size_t i, stanza_len; | 148 | size_t i, stanza_len; |
@@ -217,8 +193,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | |||
217 | * we're dealing with a config error | 193 | * we're dealing with a config error |
218 | */ | 194 | */ |
219 | case NOSTANZA: | 195 | case NOSTANZA: |
220 | die(STATE_UNKNOWN, "%s\n", | 196 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
221 | _("Config file error")); | ||
222 | /* we're in a stanza, but for a different plugin */ | 197 | /* we're in a stanza, but for a different plugin */ |
223 | case WRONGSTANZA: | 198 | case WRONGSTANZA: |
224 | GOBBLE_TO(f, c, '\n'); | 199 | GOBBLE_TO(f, c, '\n'); |
@@ -227,8 +202,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | |||
227 | case RIGHTSTANZA: | 202 | case RIGHTSTANZA: |
228 | ungetc(c, f); | 203 | ungetc(c, f); |
229 | if (add_option(f, opts)) { | 204 | if (add_option(f, opts)) { |
230 | die(STATE_UNKNOWN, "%s\n", | 205 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
231 | _("Config file error")); | ||
232 | } | 206 | } |
233 | status = true; | 207 | status = true; |
234 | break; | 208 | break; |
@@ -246,9 +220,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | |||
246 | * --option[=value] | 220 | * --option[=value] |
247 | * appending it to the linked list optbuf. | 221 | * appending it to the linked list optbuf. |
248 | */ | 222 | */ |
249 | static int | 223 | static int add_option(FILE *f, np_arg_list **optlst) { |
250 | add_option(FILE *f, np_arg_list **optlst) | ||
251 | { | ||
252 | np_arg_list *opttmp = *optlst, *optnew; | 224 | np_arg_list *opttmp = *optlst, *optnew; |
253 | char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; | 225 | char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; |
254 | char *eqptr = NULL, *valptr = NULL, *valend = NULL; | 226 | char *eqptr = NULL, *valptr = NULL, *valend = NULL; |
@@ -295,8 +267,7 @@ add_option(FILE *f, np_arg_list **optlst) | |||
295 | if (optptr == eqptr) | 267 | if (optptr == eqptr) |
296 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); | 268 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
297 | /* continue from '=' to start of value or EOL */ | 269 | /* continue from '=' to start of value or EOL */ |
298 | for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); | 270 | for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); valptr++) |
299 | valptr++) | ||
300 | continue; | 271 | continue; |
301 | /* continue to the end of value */ | 272 | /* continue to the end of value */ |
302 | for (valend = valptr; valend < lineend; valend++) | 273 | for (valend = valptr; valend < lineend; valend++) |
@@ -365,13 +336,10 @@ add_option(FILE *f, np_arg_list **optlst) | |||
365 | return 0; | 336 | return 0; |
366 | } | 337 | } |
367 | 338 | ||
368 | static char * | 339 | static char *default_file(void) { |
369 | default_file(void) | 340 | char *ini_file; |
370 | { | ||
371 | char *ini_file; | ||
372 | 341 | ||
373 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || | 342 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || (ini_file = default_file_in_path()) != NULL) { |
374 | (ini_file = default_file_in_path()) != NULL) { | ||
375 | return ini_file; | 343 | return ini_file; |
376 | } | 344 | } |
377 | 345 | ||
@@ -383,9 +351,7 @@ default_file(void) | |||
383 | return NULL; | 351 | return NULL; |
384 | } | 352 | } |
385 | 353 | ||
386 | static char * | 354 | static char *default_file_in_path(void) { |
387 | default_file_in_path(void) | ||
388 | { | ||
389 | char *config_path, **file; | 355 | char *config_path, **file; |
390 | char *dir, *ini_file, *tokens; | 356 | char *dir, *ini_file, *tokens; |
391 | 357 | ||