Code

Typos in comments
[nagiosplug.git] / lib / parse_ini.c
index b555316e0ab18b82838d403a360280f365a3478b..654452d8cd286dd6fbe0dfec4f5a3fd0052148a8 100644 (file)
 *****************************************************************************/
 
 #include "common.h"
-#include "parse_ini.h"
 #include "utils_base.h"
+#include "parse_ini.h"
 #include <ctype.h>
 
+#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)
  */
@@ -42,19 +48,26 @@ typedef struct {
 #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
 
 /* internal function that returns the constructed defaults options */
-static np_arg_list* read_defaults(FILE *f, const char *stanza);
+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);
+/* 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]
  * 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 +78,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 +88,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;
@@ -85,14 +98,16 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
        /* 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, _("Config file error"));
-               defaults=read_defaults(inifile, i.stanza);
+               if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
+               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;        
@@ -104,9 +119,8 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
  * be extra careful about user-supplied input (i.e. avoiding possible
  * format string vulnerabilities, etc)
  */
-static np_arg_list* read_defaults(FILE *f, const char *stanza){
-       int c;
-       np_arg_list *opts=NULL;
+static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
+       int c, status=FALSE;
        size_t i, stanza_len;
        enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
 
@@ -126,6 +140,8 @@ static np_arg_list* read_defaults(FILE *f, const char *stanza){
                                stanzastate=WRONGSTANZA;
                                for(i=0; i<stanza_len; i++){
                                        c=fgetc(f);
+                                       /* Strip leading whitespace */
+                                       if(i==0) for(c; isspace(c); c=fgetc(f));
                                        /* nope, read to the end of the line */
                                        if(c!=stanza[i]) {
                                                GOBBLE_TO(f, c, '\n');
@@ -135,6 +151,8 @@ static np_arg_list* read_defaults(FILE *f, const char *stanza){
                                /* if it matched up to here and the next char is ']'... */
                                if(i==stanza_len){
                                        c=fgetc(f);
+                                       /* Strip trailing whitespace */
+                                       for(c; isspace(c); c=fgetc(f));
                                        if(c==']') stanzastate=RIGHTSTANZA;
                                }
                                break;
@@ -154,15 +172,16 @@ static np_arg_list* read_defaults(FILE *f, const char *stanza){
                                        /* okay, this is where we start taking the config */
                                        case RIGHTSTANZA:
                                                ungetc(c, f);
-                                               if(add_option(f, &opts)){
+                                               if(add_option(f, opts)){
                                                        die(STATE_UNKNOWN, _("Config file error"));
                                                }
+                                               status=TRUE;
                                                break;
                                }
                                break;
                }
        }
-       return opts;
+       return status;
 }
 
 /*
@@ -223,7 +242,7 @@ static int add_option(FILE *f, np_arg_list **optlst){
        /* calculate the length of "--foo" */
        opt_len=1+optend-optptr;
        /* 1-character params needs only one dash */
-       if (opt_len==1)
+       if(opt_len==1)
                cfg_len=1+(opt_len);
        else
                cfg_len=2+(opt_len);
@@ -234,10 +253,12 @@ static int add_option(FILE *f, np_arg_list **optlst){
                cfg_len+=1+val_len;
        }
        /* if valptr==valend then we have "=" but no "bar" */
-       else if (valptr==lineend) {
+       else if(valptr==lineend) {
                equals=1;
                cfg_len+=1;
        }
+       /* A line with no equal sign isn't valid */
+       if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
 
        /* okay, now we have all the info we need, so we create a new np_arg_list
         * element and set the argument...
@@ -248,7 +269,7 @@ static int add_option(FILE *f, np_arg_list **optlst){
        read_pos=0;
        optnew->arg=(char *)malloc(cfg_len+1);
        /* 1-character params needs only one dash */
-       if (opt_len==1) {
+       if(opt_len==1) {
                strncpy(&optnew->arg[read_pos], "-", 1);
                read_pos+=1;
        } else {
@@ -256,17 +277,17 @@ static int add_option(FILE *f, np_arg_list **optlst){
                read_pos+=2;
        }
        strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
-       if(equals) optnew->arg[read_pos++]='=';
        if(value) {
+               optnew->arg[read_pos++]='=';
                strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
        }
        optnew->arg[read_pos]='\0';
 
        /* ...and put that to the end of the list */
-       if (*optlst==NULL) {
+       if(*optlst==NULL) {
                *optlst=optnew;
        }       else {
-               while (opttmp->next!=NULL) {
+               while(opttmp->next!=NULL) {
                        opttmp=opttmp->next;
                }
                opttmp->next = optnew;
@@ -276,3 +297,72 @@ static int add_option(FILE *f, np_arg_list **optlst){
        return 0;
 }
 
+static char* default_file(void){
+       struct stat sb;
+       char *np_env=NULL, *default_file=NULL;
+       char temp_file[MAX_INPUT_BUFFER];
+       size_t len;
+
+       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);
+       }
+
+       /* 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;
+}
+