diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index e318ec32155b99384d79c6e2d455effba92a6446..004396fe803378c18c501bb17743364b8cb6508b 100644 (file)
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
* License: GPL
* Copyright (c) 2007 Nagios Plugins Development Team
*
-* Last Modified: $Date$
-*
-*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
-* $Id$
*
*****************************************************************************/
#include "common.h"
-#include "parse_ini.h"
#include "utils_base.h"
+#include "parse_ini.h"
#include <ctype.h>
-/* FIXME: N::P dies if section is not found */
-/* FIXME: N::P dies if config file is not found */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+/* TODO: die like N::P if config file is not found */
/* np_ini_info contains the result of parsing a "locator" in the format
* [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
static int add_option(FILE *f, np_arg_list **optlst);
/* internal function to find default file */
static char* default_file(void);
+/* internal function to stat() files */
+static int test_file(const char* env, int len, const char* file, char* temp_file);
/* parse_locator decomposes a string of the form
* [stanza][@filename]
@@ -74,10 +75,13 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in
/* if there is no @file part */
if(stanza_len==locator_len){
i->file=default_file();
+ if(strcmp(i->file, "") == 0){
+ die(STATE_UNKNOWN, _("Cannot find '%s' or '%s' in any standard location.\n"), NP_DEFAULT_INI_FILENAME1, NP_DEFAULT_INI_FILENAME2);
+ }
} else {
i->file=strdup(&(locator[stanza_len+1]));
}
-
+
if(i->file==NULL || i->stanza==NULL){
die(STATE_UNKNOWN, _("malloc() failed!\n"));
}
/* if a file was specified or if we're using the default file */
if(i.file != NULL && strlen(i.file) > 0){
if(strcmp(i.file, "-")==0){
- inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
+ inifile=stdin;
} else {
inifile=fopen(i.file, "r");
}
if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
- if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
- /* We got nothing, try the default section */
- rewind(inifile);
- read_defaults(inifile, default_section, &defaults);
- }
+ if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
+ die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
free(i.file);
- if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
+ if(inifile!=stdin) fclose(inifile);
}
free(i.stanza);
- return defaults;
+ return defaults;
}
/* read_defaults is where the meat of the parsing takes place.
if(isspace(c)) continue;
switch(c){
/* globble up coment lines */
+ case ';':
case '#':
GOBBLE_TO(f, c, '\n');
break;
default:
switch(stanzastate){
/* we never found the start of the first stanza, so
- * we're dealing with a config error
+ * we're dealing with a config error
*/
case NOSTANZA:
die(STATE_UNKNOWN, _("Config file error"));
if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
/* continue from '=' to start of value or EOL */
for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
- /* continue to the end of value (FIXME: watching for trailing comments) */
- for(valend=valptr; valend<lineend; valend++)
- /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
- if(*valend=='#') break;
+ /* continue to the end of value */
+ for(valend=valptr; valend<lineend; valend++);
--valend;
/* Finally trim off trailing spaces */
for(valend; isspace(*valend); valend--);
}
static char* default_file(void){
- char *np_env=NULL;
+ struct stat sb;
+ char *np_env=NULL, *default_file=NULL;
+ char temp_file[MAX_INPUT_BUFFER];
+ size_t len;
- /* FIXME: STUB */
- return "";
-#if 0
if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
+ /* skip any starting colon... */
+ while(*np_env==':') np_env++;
/* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
* every PATHs defined (colon-separated).
*/
+ while((len=strcspn(np_env,":"))>0){
+ /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
+ if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+ test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
+ default_file=strdup(temp_file);
+ break;
+ }
+
+ /* Move on to the next token */
+ np_env+=len;
+ while(*np_env==':') np_env++;
+ } /* while(...) */
+ } /* if(getenv("NAGIOS_CONFIG_PATH")) */
+
+ /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
+ if(!default_file){
+ if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+ test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+ test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
+ test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
+ default_file=strdup(temp_file);
}
- if !file_found
- search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1;
- if !file_found
- search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2;
- if !file_found
- return empty string (or null if we want to die);
- return file name;
-#endif
+
+ /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
+ if(!default_file){
+ if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
+ test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
+ test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
+ default_file=strdup(temp_file);
+ }
+
+ /* Return default_file or empty string (should return NULL if we want plugins
+ * to die there)...
+ */
+ if(default_file)
+ return default_file;
+ return "";
+}
+
+/* put together len bytes from env and the filename and test for its
+ * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
+ */
+static int test_file(const char* env, int len, const char* file, char* temp_file){
+ struct stat sb;
+
+ /* test if len + filelen + '/' + '\0' fits in temp_file */
+ if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
+
+ strncpy(temp_file,env,len);
+ temp_file[len]='\0';
+ strncat(temp_file,"/",len+1);
+ strncat(temp_file,file,len+strlen(file)+1);
+
+ if(stat(temp_file, &sb) != -1) return 1;
+ return 0;
}