diff options
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r-- | lib/parse_ini.c | 135 |
1 files changed, 53 insertions, 82 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 09c0dc4..1289aae 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 - 2024 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,20 @@ 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", | ||
45 | "plugins.ini", | ||
46 | "nagios-plugins.ini", | ||
47 | NULL | ||
48 | }; | ||
49 | 44 | ||
50 | static char *default_ini_path_names[] = { | 45 | static char *default_ini_path_names[] = { |
51 | "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", | 46 | "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", "/usr/local/etc/monitoring-plugins.ini", |
52 | "/usr/local/etc/monitoring-plugins.ini", | 47 | "/etc/monitoring-plugins/monitoring-plugins.ini", "/etc/monitoring-plugins.ini", |
53 | "/etc/monitoring-plugins/monitoring-plugins.ini", | ||
54 | "/etc/monitoring-plugins.ini", | ||
55 | /* deprecated path names (for backward compatibility): */ | 48 | /* deprecated path names (for backward compatibility): */ |
56 | "/etc/nagios/plugins.ini", | 49 | "/etc/nagios/plugins.ini", "/usr/local/nagios/etc/plugins.ini", "/usr/local/etc/nagios/plugins.ini", "/etc/opt/nagios/plugins.ini", |
57 | "/usr/local/nagios/etc/plugins.ini", | 50 | "/etc/nagios-plugins.ini", "/usr/local/etc/nagios-plugins.ini", "/etc/opt/nagios-plugins.ini", NULL}; |
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 | 51 | ||
66 | /* eat all characters from a FILE pointer until n is encountered */ | 52 | /* 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)) | 53 | #define GOBBLE_TO(f, c, n) \ |
54 | do { \ | ||
55 | (c) = fgetc((f)); \ | ||
56 | } while ((c) != EOF && (c) != (n)) | ||
68 | 57 | ||
69 | /* internal function that returns the constructed defaults options */ | 58 | /* internal function that returns the constructed defaults options */ |
70 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); | 59 | static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); |
@@ -81,9 +70,7 @@ static char *default_file_in_path(void); | |||
81 | * [stanza][@filename] | 70 | * [stanza][@filename] |
82 | * into its separate parts. | 71 | * into its separate parts. |
83 | */ | 72 | */ |
84 | static void | 73 | 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; | 74 | size_t locator_len = 0, stanza_len = 0; |
88 | 75 | ||
89 | /* if locator is NULL we'll use default values */ | 76 | /* if locator is NULL we'll use default values */ |
@@ -96,7 +83,7 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
96 | i->stanza = malloc(sizeof(char) * (stanza_len + 1)); | 83 | i->stanza = malloc(sizeof(char) * (stanza_len + 1)); |
97 | strncpy(i->stanza, locator, stanza_len); | 84 | strncpy(i->stanza, locator, stanza_len); |
98 | i->stanza[stanza_len] = '\0'; | 85 | i->stanza[stanza_len] = '\0'; |
99 | } else {/* otherwise we use the default stanza */ | 86 | } else { /* otherwise we use the default stanza */ |
100 | i->stanza = strdup(def_stanza); | 87 | i->stanza = strdup(def_stanza); |
101 | } | 88 | } |
102 | 89 | ||
@@ -105,7 +92,7 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
105 | 92 | ||
106 | /* check whether there's an @file part */ | 93 | /* check whether there's an @file part */ |
107 | if (stanza_len == locator_len) { | 94 | if (stanza_len == locator_len) { |
108 | i->file = default_file(); | 95 | i->file = default_file(); |
109 | i->file_string_on_heap = false; | 96 | i->file_string_on_heap = false; |
110 | } else { | 97 | } else { |
111 | i->file = strdup(&(locator[stanza_len + 1])); | 98 | i->file = strdup(&(locator[stanza_len + 1])); |
@@ -113,35 +100,28 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) | |||
113 | } | 100 | } |
114 | 101 | ||
115 | if (i->file == NULL || i->file[0] == '\0') | 102 | if (i->file == NULL || i->file[0] == '\0') |
116 | die(STATE_UNKNOWN, | 103 | die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); |
117 | _("Cannot find config file in any standard location.\n")); | ||
118 | } | 104 | } |
119 | 105 | ||
120 | /* | 106 | /* |
121 | * This is the externally visible function used by extra_opts. | 107 | * This is the externally visible function used by extra_opts. |
122 | */ | 108 | */ |
123 | np_arg_list * | 109 | 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; | 110 | FILE *inifile = NULL; |
127 | np_arg_list *defaults = NULL; | 111 | np_arg_list *defaults = NULL; |
128 | np_ini_info i; | 112 | np_ini_info i; |
129 | int is_suid_plugin = mp_suid(); | 113 | int is_suid_plugin = mp_suid(); |
130 | 114 | ||
131 | if (is_suid_plugin && idpriv_temp_drop() == -1) | 115 | if (is_suid_plugin && idpriv_temp_drop() == -1) |
132 | die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), | 116 | die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), strerror(errno)); |
133 | strerror(errno)); | ||
134 | 117 | ||
135 | parse_locator(locator, default_section, &i); | 118 | parse_locator(locator, default_section, &i); |
136 | inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); | 119 | inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); |
137 | 120 | ||
138 | if (inifile == NULL) | 121 | if (inifile == NULL) |
139 | die(STATE_UNKNOWN, _("Can't read config file: %s\n"), | 122 | die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); |
140 | strerror(errno)); | ||
141 | if (!read_defaults(inifile, i.stanza, &defaults)) | 123 | if (!read_defaults(inifile, i.stanza, &defaults)) |
142 | die(STATE_UNKNOWN, | 124 | 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 | 125 | ||
146 | if (i.file_string_on_heap) { | 126 | if (i.file_string_on_heap) { |
147 | free(i.file); | 127 | free(i.file); |
@@ -151,8 +131,7 @@ np_get_defaults(const char *locator, const char *default_section) | |||
151 | fclose(inifile); | 131 | fclose(inifile); |
152 | free(i.stanza); | 132 | free(i.stanza); |
153 | if (is_suid_plugin && idpriv_temp_restore() == -1) | 133 | if (is_suid_plugin && idpriv_temp_restore() == -1) |
154 | die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), | 134 | die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), strerror(errno)); |
155 | strerror(errno)); | ||
156 | 135 | ||
157 | return defaults; | 136 | return defaults; |
158 | } | 137 | } |
@@ -164,13 +143,15 @@ np_get_defaults(const char *locator, const char *default_section) | |||
164 | * be extra careful about user-supplied input (i.e. avoiding possible | 143 | * be extra careful about user-supplied input (i.e. avoiding possible |
165 | * format string vulnerabilities, etc). | 144 | * format string vulnerabilities, etc). |
166 | */ | 145 | */ |
167 | static int | 146 | 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; | 147 | int c = 0; |
171 | bool status = false; | 148 | bool status = false; |
172 | size_t i, stanza_len; | 149 | size_t i, stanza_len; |
173 | enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; | 150 | enum { |
151 | NOSTANZA, | ||
152 | WRONGSTANZA, | ||
153 | RIGHTSTANZA | ||
154 | } stanzastate = NOSTANZA; | ||
174 | 155 | ||
175 | stanza_len = strlen(stanza); | 156 | stanza_len = strlen(stanza); |
176 | 157 | ||
@@ -217,8 +198,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | |||
217 | * we're dealing with a config error | 198 | * we're dealing with a config error |
218 | */ | 199 | */ |
219 | case NOSTANZA: | 200 | case NOSTANZA: |
220 | die(STATE_UNKNOWN, "%s\n", | 201 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
221 | _("Config file error")); | ||
222 | /* we're in a stanza, but for a different plugin */ | 202 | /* we're in a stanza, but for a different plugin */ |
223 | case WRONGSTANZA: | 203 | case WRONGSTANZA: |
224 | GOBBLE_TO(f, c, '\n'); | 204 | GOBBLE_TO(f, c, '\n'); |
@@ -227,8 +207,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | |||
227 | case RIGHTSTANZA: | 207 | case RIGHTSTANZA: |
228 | ungetc(c, f); | 208 | ungetc(c, f); |
229 | if (add_option(f, opts)) { | 209 | if (add_option(f, opts)) { |
230 | die(STATE_UNKNOWN, "%s\n", | 210 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
231 | _("Config file error")); | ||
232 | } | 211 | } |
233 | status = true; | 212 | status = true; |
234 | break; | 213 | break; |
@@ -246,9 +225,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) | |||
246 | * --option[=value] | 225 | * --option[=value] |
247 | * appending it to the linked list optbuf. | 226 | * appending it to the linked list optbuf. |
248 | */ | 227 | */ |
249 | static int | 228 | 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; | 229 | np_arg_list *opttmp = *optlst, *optnew; |
253 | char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; | 230 | char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; |
254 | char *eqptr = NULL, *valptr = NULL, *valend = NULL; | 231 | char *eqptr = NULL, *valptr = NULL, *valend = NULL; |
@@ -295,8 +272,7 @@ add_option(FILE *f, np_arg_list **optlst) | |||
295 | if (optptr == eqptr) | 272 | if (optptr == eqptr) |
296 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); | 273 | die(STATE_UNKNOWN, "%s\n", _("Config file error")); |
297 | /* continue from '=' to start of value or EOL */ | 274 | /* continue from '=' to start of value or EOL */ |
298 | for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); | 275 | for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); valptr++) |
299 | valptr++) | ||
300 | continue; | 276 | continue; |
301 | /* continue to the end of value */ | 277 | /* continue to the end of value */ |
302 | for (valend = valptr; valend < lineend; valend++) | 278 | for (valend = valptr; valend < lineend; valend++) |
@@ -365,13 +341,10 @@ add_option(FILE *f, np_arg_list **optlst) | |||
365 | return 0; | 341 | return 0; |
366 | } | 342 | } |
367 | 343 | ||
368 | static char * | 344 | static char *default_file(void) { |
369 | default_file(void) | 345 | char *ini_file; |
370 | { | ||
371 | char *ini_file; | ||
372 | 346 | ||
373 | if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || | 347 | 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; | 348 | return ini_file; |
376 | } | 349 | } |
377 | 350 | ||
@@ -383,9 +356,7 @@ default_file(void) | |||
383 | return NULL; | 356 | return NULL; |
384 | } | 357 | } |
385 | 358 | ||
386 | static char * | 359 | static char *default_file_in_path(void) { |
387 | default_file_in_path(void) | ||
388 | { | ||
389 | char *config_path, **file; | 360 | char *config_path, **file; |
390 | char *dir, *ini_file, *tokens; | 361 | char *dir, *ini_file, *tokens; |
391 | 362 | ||