diff options
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r-- | lib/parse_ini.c | 119 |
1 files changed, 49 insertions, 70 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 76953e9e..b6d80562 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c | |||
@@ -40,6 +40,23 @@ 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[] = { | ||
44 | "plugins.ini", | ||
45 | "nagios-plugins.ini", | ||
46 | NULL | ||
47 | }; | ||
48 | |||
49 | static char *default_ini_path_names[] = { | ||
50 | "/etc/nagios/plugins.ini", | ||
51 | "/usr/local/nagios/etc/plugins.ini", | ||
52 | "/usr/local/etc/nagios/plugins.ini", | ||
53 | "/etc/opt/nagios/plugins.ini", | ||
54 | "/etc/nagios-plugins.ini", | ||
55 | "/usr/local/etc/nagios-plugins.ini", | ||
56 | "/etc/opt/nagios-plugins.ini", | ||
57 | NULL | ||
58 | }; | ||
59 | |||
43 | /* eat all characters from a FILE pointer until n is encountered */ | 60 | /* eat all characters from a FILE pointer until n is encountered */ |
44 | #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n)) | 61 | #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n)) |
45 | 62 | ||
@@ -49,8 +66,6 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); | |||
49 | static int add_option(FILE *f, np_arg_list **optlst); | 66 | static int add_option(FILE *f, np_arg_list **optlst); |
50 | /* internal function to find default file */ | 67 | /* internal function to find default file */ |
51 | static char* default_file(void); | 68 | static char* default_file(void); |
52 | /* internal function to test files access */ | ||
53 | static int test_file(const char* env, int len, const char* file, char* temp_file); | ||
54 | 69 | ||
55 | /* parse_locator decomposes a string of the form | 70 | /* parse_locator decomposes a string of the form |
56 | * [stanza][@filename] | 71 | * [stanza][@filename] |
@@ -72,18 +87,17 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in | |||
72 | } else { /* otherwise we use the default stanza */ | 87 | } else { /* otherwise we use the default stanza */ |
73 | i->stanza=strdup(def_stanza); | 88 | i->stanza=strdup(def_stanza); |
74 | } | 89 | } |
90 | if(i->stanza==NULL){ | ||
91 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
92 | } | ||
75 | /* if there is no @file part */ | 93 | /* if there is no @file part */ |
76 | if(stanza_len==locator_len){ | 94 | if(stanza_len==locator_len){ |
77 | i->file=default_file(); | 95 | i->file=default_file(); |
78 | if(strcmp(i->file, "") == 0){ | ||
79 | die(STATE_UNKNOWN, _("Cannot find '%s' or '%s' in any standard location.\n"), NP_DEFAULT_INI_FILENAME1, NP_DEFAULT_INI_FILENAME2); | ||
80 | } | ||
81 | } else { | 96 | } else { |
82 | i->file=strdup(&(locator[stanza_len+1])); | 97 | i->file=strdup(&(locator[stanza_len+1])); |
83 | } | 98 | } |
84 | 99 | if(i->file==NULL || i->file[0]=='\0'){ | |
85 | if(i->file==NULL || i->stanza==NULL){ | 100 | die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); |
86 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
87 | } | 101 | } |
88 | } | 102 | } |
89 | 103 | ||
@@ -295,71 +309,36 @@ static int add_option(FILE *f, np_arg_list **optlst){ | |||
295 | return 0; | 309 | return 0; |
296 | } | 310 | } |
297 | 311 | ||
298 | static char* default_file(void){ | 312 | static char *default_file_in_path(void){ |
299 | struct stat sb; | 313 | char *config_path, **file; |
300 | char *np_env=NULL, *default_file=NULL; | 314 | char *dir, *ini_file, *tokens; |
301 | char temp_file[MAX_INPUT_BUFFER]; | 315 | |
302 | size_t len; | 316 | if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL) |
303 | 317 | return NULL; | |
304 | if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { | 318 | |
305 | /* skip any starting colon... */ | 319 | if((tokens=strdup(config_path))==NULL) |
306 | while(*np_env==':') np_env++; | 320 | die(STATE_UNKNOWN, _("Insufficient Memory")); |
307 | /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in | 321 | for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){ |
308 | * every PATHs defined (colon-separated). | 322 | for(file=default_ini_file_names; *file!=NULL; file++){ |
309 | */ | 323 | if((asprintf(&ini_file, "%s/%s", dir, *file))<0) |
310 | while((len=strcspn(np_env,":"))>0){ | 324 | die(STATE_UNKNOWN, _("Insufficient Memory")); |
311 | /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */ | 325 | if(access(ini_file, F_OK)==0){ |
312 | if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | 326 | free(tokens); |
313 | test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){ | 327 | return ini_file; |
314 | default_file=strdup(temp_file); | ||
315 | break; | ||
316 | } | 328 | } |
317 | 329 | } | |
318 | /* Move on to the next token */ | ||
319 | np_env+=len; | ||
320 | while(*np_env==':') np_env++; | ||
321 | } /* while(...) */ | ||
322 | } /* if(getenv("NAGIOS_CONFIG_PATH")) */ | ||
323 | |||
324 | /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */ | ||
325 | if(!default_file){ | ||
326 | if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
327 | test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
328 | test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
329 | test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1) | ||
330 | default_file=strdup(temp_file); | ||
331 | } | ||
332 | |||
333 | /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */ | ||
334 | if(!default_file){ | ||
335 | if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 || | ||
336 | test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 || | ||
337 | test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1) | ||
338 | default_file=strdup(temp_file); | ||
339 | } | 330 | } |
340 | 331 | free(tokens); | |
341 | /* Return default_file or empty string (should return NULL if we want plugins | 332 | return NULL; |
342 | * to die there)... | ||
343 | */ | ||
344 | if(default_file) | ||
345 | return default_file; | ||
346 | return ""; | ||
347 | } | 333 | } |
348 | 334 | ||
349 | /* put together len bytes from env and the filename and test for its | 335 | static char *default_file(void){ |
350 | * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed. | 336 | char **p, *ini_file; |
351 | */ | ||
352 | static int test_file(const char* env, int len, const char* file, char* temp_file){ | ||
353 | |||
354 | /* test if len + filelen + '/' + '\0' fits in temp_file */ | ||
355 | if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1; | ||
356 | |||
357 | strncpy(temp_file,env,len); | ||
358 | temp_file[len]='\0'; | ||
359 | strncat(temp_file,"/",len+1); | ||
360 | strncat(temp_file,file,len+strlen(file)+1); | ||
361 | 337 | ||
362 | if(access(temp_file, F_OK) == 0) return 1; | 338 | if((ini_file=default_file_in_path())!=NULL) |
363 | return 0; | 339 | return ini_file; |
340 | for(p=default_ini_path_names; *p!=NULL; p++) | ||
341 | if (access(*p, F_OK)==0) | ||
342 | return *p; | ||
343 | return NULL; | ||
364 | } | 344 | } |
365 | |||