From dce143e354da414bdd5fa5fb0b9b488ac4221200 Mon Sep 17 00:00:00 2001
From: Thomas Guyot-Sionnest <dermoth@users.sourceforge.net>
Date: Mon, 24 Mar 2008 06:28:46 +0000
Subject: Replace broken usage of NAGIOS_CONFIG_PATH with a stub function (that
 will try to find a config file in the future...) Allow NULL locator (default
 file/section)

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1960 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 70da7f1..e318ec3 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -30,6 +30,9 @@
 #include "utils_base.h"
 #include <ctype.h>
 
+/* FIXME: N::P dies if section is not found */
+/* FIXME: N::P dies 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)
  */
@@ -45,16 +48,21 @@ typedef struct {
 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
 /* internal function that converts a single line into options format */
 static int add_option(FILE *f, np_arg_list **optlst);
+/* internal function to find default file */
+static char* default_file(void);
 
 /* parse_locator decomposes a string of the form
  * 	[stanza][@filename]
  * into its seperate parts
  */
 static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
-	size_t locator_len, stanza_len;
+	size_t locator_len=0, stanza_len=0;
 
-	locator_len=strlen(locator);
-	stanza_len=strcspn(locator, "@");
+	/* if locator is NULL we'll use default values */
+	if(locator){
+		locator_len=strlen(locator);
+		stanza_len=strcspn(locator, "@");
+	}
 	/* if a non-default stanza is provided */
 	if(stanza_len>0){
 		i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
@@ -65,7 +73,7 @@ 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=strdup(NP_DEFAULT_INI_PATH);
+		i->file=default_file();
 	} else {
 		i->file=strdup(&(locator[stanza_len+1]));
 	}
@@ -75,7 +83,7 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in
 	}
 }
 
-/* this is the externally visible function used by plugins */
+/* this is the externally visible function used by extra_opts */
 np_arg_list* np_get_defaults(const char *locator, const char *default_section){
 	FILE *inifile=NULL;
 	np_arg_list *defaults=NULL;
@@ -89,7 +97,7 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
 		} else {
 			inifile=fopen(i.file, "r");
 		}
-		if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
+		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);
@@ -287,3 +295,24 @@ static int add_option(FILE *f, np_arg_list **optlst){
 	return 0;
 }
 
+static char* default_file(void){
+	char *np_env=NULL;
+
+	/* FIXME: STUB */
+	return "";
+#if 0
+	if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
+		/* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
+		 * every PATHs defined (colon-separated).
+		 */
+	}
+	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
+}
+
diff --git a/lib/parse_ini.h b/lib/parse_ini.h
index fea745c..61149a2 100644
--- a/lib/parse_ini.h
+++ b/lib/parse_ini.h
@@ -13,10 +13,43 @@ typedef struct np_arg_el {
 	struct np_arg_el *next;
 } np_arg_list;
 
-/* NP_DEFAULT_INI_PATH: compile-time default location for ini file */
+/* NP_DEFAULT_INI_PATH: compile-time default location for ini file
 #ifndef NP_DEFAULT_INI_PATH
-# define NP_DEFAULT_INI_PATH "/etc/nagios-plugins/plugins.ini"
-#endif /* NP_DEFAULT_INI_PATH */
+# define NP_DEFAULT_INI_PATH "/etc/nagios-plugins.ini"
+#endif NP_DEFAULT_INI_PATH */
+
+/* Filenames (see below) */
+#ifndef NP_DEFAULT_INI_FILENAME1
+# define NP_DEFAULT_INI_FILENAME1 "plugins.ini"
+#endif /* NP_DEFAULT_INI_FILENAME1 */
+#ifndef NP_DEFAULT_INI_FILENAME2
+# define NP_DEFAULT_INI_FILENAME2 "nagios-plugins.ini"
+#endif /* NP_DEFAULT_INI_FILENAME2 */
+
+/* Config paths ending in nagios (search for NP_DEFAULT_INI_FILENAME1) */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH1
+# define NP_DEFAULT_INI_NAGIOS_PATH1 "/etc/nagios"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH1 */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH2
+# define NP_DEFAULT_INI_NAGIOS_PATH2 "/usr/local/nagios/etc"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH2 */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH3
+# define NP_DEFAULT_INI_NAGIOS_PATH3 "/usr/local/etc/nagios"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH3 */
+#ifndef NP_DEFAULT_INI_NAGIOS_PATH4
+# define NP_DEFAULT_INI_NAGIOS_PATH4 "/etc/opt/nagios"
+#endif /* NP_DEFAULT_INI_NAGIOS_PATH4 */
+
+/* Config paths not ending in nagios (search for NP_DEFAULT_INI_FILENAME2) */
+#ifndef NP_DEFAULT_INI_PATH1
+# define NP_DEFAULT_INI_PATH1 "/etc"
+#endif /* NP_DEFAULT_INI_PATH1 */
+#ifndef NP_DEFAULT_INI_PATH2
+# define NP_DEFAULT_INI_PATH2 "/usr/local/etc"
+#endif /* NP_DEFAULT_INI_PATH2 */
+#ifndef NP_DEFAULT_INI_PATH3
+# define NP_DEFAULT_INI_PATH3 "/etc/opt"
+#endif /* NP_DEFAULT_INI_PATH3 */
 
 /* np_load_defaults: load the default configuration (if present) for
  * a plugin from the ini file
-- 
cgit v0.10-9-g596f

