From f02b3f6b2866e2ccb8cdfc2257c1f9a540ddc3e9 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 00:37:45 +0200 Subject: lib/parse_ini.[ch]: Simplify code Rewrite the code that looks up the INI configuration file path (used by the Extra-Opts feature) in order to improve readability. The behaviour should not have changed. --- lib/parse_ini.c | 119 +++++++++++++++++++++++--------------------------------- lib/parse_ini.h | 40 ------------------- 2 files changed, 49 insertions(+), 110 deletions(-) (limited to 'lib') 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 { char *stanza; } np_ini_info; +static char *default_ini_file_names[] = { + "plugins.ini", + "nagios-plugins.ini", + NULL +}; + +static char *default_ini_path_names[] = { + "/etc/nagios/plugins.ini", + "/usr/local/nagios/etc/plugins.ini", + "/usr/local/etc/nagios/plugins.ini", + "/etc/opt/nagios/plugins.ini", + "/etc/nagios-plugins.ini", + "/usr/local/etc/nagios-plugins.ini", + "/etc/opt/nagios-plugins.ini", + NULL +}; + /* eat all characters from a FILE pointer until n is encountered */ #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n)) @@ -49,8 +66,6 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); static int add_option(FILE *f, np_arg_list **optlst); /* internal function to find default file */ static char* default_file(void); -/* internal function to test files access */ -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] @@ -72,18 +87,17 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in } else { /* otherwise we use the default stanza */ i->stanza=strdup(def_stanza); } + if(i->stanza==NULL){ + die(STATE_UNKNOWN, _("malloc() failed!\n")); + } /* 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(i->file==NULL || i->file[0]=='\0'){ + die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); } } @@ -295,71 +309,36 @@ 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; +static char *default_file_in_path(void){ + char *config_path, **file; + char *dir, *ini_file, *tokens; + + if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL) + return NULL; + + if((tokens=strdup(config_path))==NULL) + die(STATE_UNKNOWN, _("Insufficient Memory")); + for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){ + for(file=default_ini_file_names; *file!=NULL; file++){ + if((asprintf(&ini_file, "%s/%s", dir, *file))<0) + die(STATE_UNKNOWN, _("Insufficient Memory")); + if(access(ini_file, F_OK)==0){ + free(tokens); + return ini_file; } - - /* 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 ""; + free(tokens); + return NULL; } -/* 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){ - - /* 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); +static char *default_file(void){ + char **p, *ini_file; - if(access(temp_file, F_OK) == 0) return 1; - return 0; + if((ini_file=default_file_in_path())!=NULL) + return ini_file; + for(p=default_ini_path_names; *p!=NULL; p++) + if (access(*p, F_OK)==0) + return *p; + return NULL; } - diff --git a/lib/parse_ini.h b/lib/parse_ini.h index a3a494ef..8b67ea34 100644 --- a/lib/parse_ini.h +++ b/lib/parse_ini.h @@ -13,46 +13,6 @@ typedef struct np_arg_el { struct np_arg_el *next; } np_arg_list; -/* FIXME: This is in plugins/common.c. Should be eventually moved to lib/ - * (although for this particular one a configure settings should be ideal) - */ -#ifndef MAX_INPUT_BUFFER -# define MAX_INPUT_BUFFER 8192 -#endif /* MAX_INPUT_BUFFER */ - -/* 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 v1.2.3-74-g34f1 From f94e95785cb15a0028be40019848ec05a8e208a8 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 00:39:38 +0200 Subject: lib/parse_ini.c: Read "monitoring-plugins.ini" Read "monitoring-plugins.ini" if that file exists, but fall back to reading "plugins.ini" or "nagios-plugins.ini" for backward compatibility. --- lib/parse_ini.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index b6d80562..e19af1bb 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -41,12 +41,16 @@ typedef struct { } np_ini_info; static char *default_ini_file_names[] = { + "monitoring-plugins.ini", "plugins.ini", "nagios-plugins.ini", NULL }; static char *default_ini_path_names[] = { + "/usr/local/etc/monitoring-plugins.ini", + "/etc/monitoring-plugins.ini", + /* Deprecated path names (for backward compatibility): */ "/etc/nagios/plugins.ini", "/usr/local/nagios/etc/plugins.ini", "/usr/local/etc/nagios/plugins.ini", -- cgit v1.2.3-74-g34f1 From fbe13d8f32dc0e3bb76e32ee690e6f15bcafb0f5 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 00:47:35 +0200 Subject: lib/parse_ini.c: Read $MP_CONFIG_FILE Read $MP_CONFIG_FILE if that variable is set in the environment. --- lib/parse_ini.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index e19af1bb..f352d78c 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -339,7 +339,8 @@ static char *default_file_in_path(void){ static char *default_file(void){ char **p, *ini_file; - if((ini_file=default_file_in_path())!=NULL) + if((ini_file=getenv("MP_CONFIG_FILE"))!=NULL || + (ini_file=default_file_in_path())!=NULL) return ini_file; for(p=default_ini_path_names; *p!=NULL; p++) if (access(*p, F_OK)==0) -- cgit v1.2.3-74-g34f1 From 95ed0a996c84d8df0485b2ffbadf6e92d3fef80f Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 00:50:48 +0200 Subject: lib/parse_ini.c: Remove outdated comment and code The lib/parse_ini.c:np_get_defaults() function now dies if no configuration file is found. --- lib/parse_ini.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index f352d78c..2e42df7c 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -30,8 +30,6 @@ #include #include -/* 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) */ @@ -112,20 +110,17 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){ np_ini_info i; parse_locator(locator, default_section, &i); - /* 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=stdin; - } else { - inifile=fopen(i.file, "r"); - } - if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("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!=stdin) fclose(inifile); + if(strcmp(i.file, "-")==0){ + inifile=stdin; + } else { + inifile=fopen(i.file, "r"); } + if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("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!=stdin) fclose(inifile); free(i.stanza); return defaults; } -- cgit v1.2.3-74-g34f1 From e2b816986926e91227fc151af99bcf6dd5f68e74 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 01:21:31 +0200 Subject: lib/parse_ini.c: Don't cast malloc(3) result There's no need to cast malloc(3)'s return value. --- lib/parse_ini.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 2e42df7c..51ad2c17 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -274,11 +274,11 @@ static int add_option(FILE *f, np_arg_list **optlst){ /* okay, now we have all the info we need, so we create a new np_arg_list * element and set the argument... */ - optnew=(np_arg_list *)malloc(sizeof(np_arg_list)); + optnew=malloc(sizeof(np_arg_list)); optnew->next=NULL; read_pos=0; - optnew->arg=(char *)malloc(cfg_len+1); + optnew->arg=malloc(cfg_len+1); /* 1-character params needs only one dash */ if(opt_len==1) { strncpy(&optnew->arg[read_pos], "-", 1); -- cgit v1.2.3-74-g34f1 From 11bfb0def2e216eece4b680eeb91a671099a46e5 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 10:37:10 +0200 Subject: lib/parse_ini.[ch]: Change code formatting Change the indentation and formatting of the code in lib/parse_ini.c. This breaks patches against that file and makes it harder to track its history, but it (hopefully) improves readability a lot. --- lib/parse_ini.c | 346 +++++++++++++++++++++++++++++++------------------------- lib/parse_ini.h | 2 +- 2 files changed, 192 insertions(+), 156 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 51ad2c17..a5b3d306 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -24,8 +24,8 @@ #include "common.h" #include "utils_base.h" #include "parse_ini.h" -#include +#include #include #include #include @@ -64,63 +64,71 @@ static char *default_ini_path_names[] = { /* internal function that returns the constructed defaults options */ 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); +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=0, stanza_len=0; +static void +parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) +{ + size_t locator_len = 0, stanza_len = 0; /* if locator is NULL we'll use default values */ - if(locator){ - locator_len=strlen(locator); - stanza_len=strcspn(locator, "@"); + if (locator != NULL) { + 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)); + if (stanza_len > 0) { + i->stanza = malloc(sizeof(char) * (stanza_len + 1)); strncpy(i->stanza, locator, stanza_len); - i->stanza[stanza_len]='\0'; - } else { /* otherwise we use the default stanza */ - i->stanza=strdup(def_stanza); - } - if(i->stanza==NULL){ + i->stanza[stanza_len] = '\0'; + } else /* otherwise we use the default stanza */ + i->stanza = strdup(def_stanza); + + if (i->stanza == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); - } - /* if there is no @file part */ - if(stanza_len==locator_len){ - i->file=default_file(); - } else { - i->file=strdup(&(locator[stanza_len+1])); - } - if(i->file==NULL || i->file[0]=='\0'){ - die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n")); - } + + /* check whether there's an @file part */ + i->file = stanza_len == locator_len + ? default_file() + : strdup(&(locator[stanza_len + 1])); + if (i->file == NULL || i->file[0] == '\0') + die(STATE_UNKNOWN, + _("Cannot find config file in any standard location.\n")); } /* 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; +np_arg_list * +np_get_defaults(const char *locator, const char *default_section) +{ + FILE *inifile = NULL; + np_arg_list *defaults = NULL; np_ini_info i; parse_locator(locator, default_section, &i); - if(strcmp(i.file, "-")==0){ - inifile=stdin; - } else { - inifile=fopen(i.file, "r"); - } - if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("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); + if (strcmp(i.file, "-") == 0) + inifile = stdin; + else + inifile = fopen(i.file, "r"); + + if (inifile == NULL) + die(STATE_UNKNOWN, "%s\n", _("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!=stdin) fclose(inifile); + if (inifile != stdin) + fclose(inifile); free(i.stanza); return defaults; } @@ -131,67 +139,76 @@ 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 int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){ - int c, status=FALSE; +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; + enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; - stanza_len=strlen(stanza); + stanza_len = strlen(stanza); /* our little stanza-parsing state machine. */ - while((c=fgetc(f))!=EOF){ + while ((c = fgetc(f)) != EOF) { /* gobble up leading whitespace */ - if(isspace(c)) continue; - switch(c){ + if (isspace(c)) + continue; + switch (c) { /* globble up coment lines */ - case ';': - case '#': - GOBBLE_TO(f, c, '\n'); - break; + case ';': + case '#': + GOBBLE_TO(f, c, '\n'); + break; /* start of a stanza. check to see if it matches */ - case '[': - stanzastate=WRONGSTANZA; - for(i=0; i= linebuf_sz){ - linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz; - linebuf=realloc(linebuf, linebuf_sz); - if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); + if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) { + linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz; + linebuf = realloc(linebuf, linebuf_sz); + if (linebuf == NULL) + die(STATE_UNKNOWN, _("malloc() failed!\n")); } - if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1; + if (fgets(&linebuf[read_pos], read_sz, f) == NULL) + done_reading = 1; else { - read_pos=strlen(linebuf); - if(linebuf[read_pos-1]=='\n') { - linebuf[--read_pos]='\0'; - done_reading=1; + read_pos = strlen(linebuf); + if (linebuf[read_pos - 1] == '\n') { + linebuf[--read_pos] = '\0'; + done_reading = 1; } } } - lineend=&linebuf[read_pos]; + lineend = &linebuf[read_pos]; /* all that to read one line. isn't C fun? :) now comes the parsing :/ */ /* skip leading whitespace */ - for(optptr=linebuf; optptrnext=NULL; + optnew = malloc(sizeof(np_arg_list)); + optnew->next = NULL; - read_pos=0; - optnew->arg=malloc(cfg_len+1); + read_pos = 0; + optnew->arg = 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; + read_pos += 1; } else { strncpy(&optnew->arg[read_pos], "--", 2); - read_pos+=2; + read_pos += 2; } - strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len; - if(value) { - optnew->arg[read_pos++]='='; - strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len; + strncpy(&optnew->arg[read_pos], optptr, opt_len); + read_pos += opt_len; + if (value) { + optnew->arg[read_pos++] = '='; + strncpy(&optnew->arg[read_pos], valptr, val_len); + read_pos += val_len; } - optnew->arg[read_pos]='\0'; + optnew->arg[read_pos] = '\0'; /* ...and put that to the end of the list */ - if(*optlst==NULL) { - *optlst=optnew; - } else { - while(opttmp->next!=NULL) { - opttmp=opttmp->next; - } + if (*optlst == NULL) + *optlst = optnew; + else { + while (opttmp->next != NULL) + opttmp = opttmp->next; opttmp->next = optnew; } @@ -308,20 +340,22 @@ static int add_option(FILE *f, np_arg_list **optlst){ return 0; } -static char *default_file_in_path(void){ +static char * +default_file_in_path(void) +{ char *config_path, **file; char *dir, *ini_file, *tokens; - if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL) + if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL) return NULL; - if((tokens=strdup(config_path))==NULL) + if ((tokens = strdup(config_path)) == NULL) die(STATE_UNKNOWN, _("Insufficient Memory")); - for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){ - for(file=default_ini_file_names; *file!=NULL; file++){ - if((asprintf(&ini_file, "%s/%s", dir, *file))<0) + for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) { + for (file = default_ini_file_names; *file != NULL; file++) { + if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0) die(STATE_UNKNOWN, _("Insufficient Memory")); - if(access(ini_file, F_OK)==0){ + if (access(ini_file, F_OK) == 0) { free(tokens); return ini_file; } @@ -331,14 +365,16 @@ static char *default_file_in_path(void){ return NULL; } -static char *default_file(void){ +static char * +default_file(void) +{ char **p, *ini_file; - if((ini_file=getenv("MP_CONFIG_FILE"))!=NULL || - (ini_file=default_file_in_path())!=NULL) + if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || + (ini_file = default_file_in_path()) != NULL) return ini_file; - for(p=default_ini_path_names; *p!=NULL; p++) - if (access(*p, F_OK)==0) + for (p = default_ini_path_names; *p != NULL; p++) + if (access(*p, F_OK) == 0) return *p; return NULL; } diff --git a/lib/parse_ini.h b/lib/parse_ini.h index 8b67ea34..e37601b5 100644 --- a/lib/parse_ini.h +++ b/lib/parse_ini.h @@ -16,7 +16,7 @@ typedef struct np_arg_el { /* np_load_defaults: load the default configuration (if present) for * a plugin from the ini file */ -np_arg_list* np_get_defaults(const char *locator, const char *default_section); +np_arg_list *np_get_defaults(const char *locator, const char *default_section); #endif /* _PARSE_INI_H_ */ -- cgit v1.2.3-74-g34f1 From f627b3f33bc16f7d5a3d4d56bc6d5c935fecb8d9 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 10:45:14 +0200 Subject: lib/parse_ini.c: Fix Clang warnings --- lib/parse_ini.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index a5b3d306..b33ce089 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -166,7 +166,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) c = fgetc(f); /* Strip leading whitespace */ if (i == 0) - for (c; isspace(c); c = fgetc(f)) + for (; isspace(c); c = fgetc(f)) continue; /* nope, read to the end of the line */ if (c != stanza[i]) { @@ -178,7 +178,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) if (i == stanza_len) { c = fgetc(f); /* Strip trailing whitespace */ - for (c; isspace(c); c = fgetc(f)) + for (; isspace(c); c = fgetc(f)) continue; if (c == ']') stanzastate = RIGHTSTANZA; @@ -193,7 +193,6 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) case NOSTANZA: die(STATE_UNKNOWN, "%s\n", _("Config file error")); - break; /* we're in a stanza, but for a different plugin */ case WRONGSTANZA: GOBBLE_TO(f, c, '\n'); @@ -226,7 +225,7 @@ add_option(FILE *f, np_arg_list **optlst) { np_arg_list *opttmp = *optlst, *optnew; char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; - char *eqptr = NULL, *valptr = NULL, *spaceptr = NULL, *valend = NULL; + char *eqptr = NULL, *valptr = NULL, *valend = NULL; short done_reading = 0, equals = 0, value = 0; size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0; size_t opt_len = 0, val_len = 0; @@ -240,7 +239,7 @@ add_option(FILE *f, np_arg_list **optlst) if (linebuf == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); } - if (fgets(&linebuf[read_pos], read_sz, f) == NULL) + if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL) done_reading = 1; else { read_pos = strlen(linebuf); @@ -278,10 +277,10 @@ add_option(FILE *f, np_arg_list **optlst) continue; --valend; /* Finally trim off trailing spaces */ - for (valend; isspace(*valend); valend--) + for (; isspace(*valend); valend--) continue; /* calculate the length of "--foo" */ - opt_len = 1 + optend - optptr; + opt_len = (size_t)(1 + optend - optptr); /* 1-character params needs only one dash */ if (opt_len == 1) cfg_len = 1 + (opt_len); @@ -290,7 +289,7 @@ add_option(FILE *f, np_arg_list **optlst) /* if valptr Date: Wed, 18 Jun 2014 10:52:09 +0200 Subject: lib/parse_ini.c: Cosmetic changes to comments --- lib/parse_ini.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index b33ce089..2e47e06d 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -48,7 +48,7 @@ static char *default_ini_file_names[] = { static char *default_ini_path_names[] = { "/usr/local/etc/monitoring-plugins.ini", "/etc/monitoring-plugins.ini", - /* Deprecated path names (for backward compatibility): */ + /* deprecated path names (for backward compatibility): */ "/etc/nagios/plugins.ini", "/usr/local/nagios/etc/plugins.ini", "/usr/local/etc/nagios/plugins.ini", @@ -71,9 +71,10 @@ 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 +/* + * Parse_locator decomposes a string of the form * [stanza][@filename] - * into its seperate parts + * into its seperate parts. */ static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) @@ -105,7 +106,9 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) _("Cannot find config file in any standard location.\n")); } -/* this is the externally visible function used by extra_opts */ +/* + * This is the externally visible function used by extra_opts. + */ np_arg_list * np_get_defaults(const char *locator, const char *default_section) { @@ -133,11 +136,12 @@ np_get_defaults(const char *locator, const char *default_section) return defaults; } -/* read_defaults is where the meat of the parsing takes place. +/* + * The read_defaults() function is where the meat of the parsing takes place. * - * note that this may be called by a setuid binary, so we need to + * Note that this may be called by a setuid binary, so we need to * be extra careful about user-supplied input (i.e. avoiding possible - * format string vulnerabilities, etc) + * format string vulnerabilities, etc). */ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) @@ -148,7 +152,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) stanza_len = strlen(stanza); - /* our little stanza-parsing state machine. */ + /* our little stanza-parsing state machine */ while ((c = fgetc(f)) != EOF) { /* gobble up leading whitespace */ if (isspace(c)) @@ -159,12 +163,12 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) case '#': GOBBLE_TO(f, c, '\n'); break; - /* start of a stanza. check to see if it matches */ + /* start of a stanza, check to see if it matches */ case '[': stanzastate = WRONGSTANZA; for (i = 0; i < stanza_len; i++) { c = fgetc(f); - /* Strip leading whitespace */ + /* strip leading whitespace */ if (i == 0) for (; isspace(c); c = fgetc(f)) continue; @@ -177,7 +181,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) /* if it matched up to here and the next char is ']'... */ if (i == stanza_len) { c = fgetc(f); - /* Strip trailing whitespace */ + /* strip trailing whitespace */ for (; isspace(c); c = fgetc(f)) continue; if (c == ']') @@ -214,9 +218,9 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) } /* - * read one line of input in the format + * Read one line of input in the format * ^option[[:space:]]*(=[[:space:]]*value)? - * and creates it as a cmdline argument + * and create it as a cmdline argument * --option[=value] * appending it to the linked list optbuf. */ @@ -250,7 +254,7 @@ add_option(FILE *f, np_arg_list **optlst) } } lineend = &linebuf[read_pos]; - /* all that to read one line. isn't C fun? :) now comes the parsing :/ */ + /* all that to read one line, isn't C fun? :) now comes the parsing :/ */ /* skip leading whitespace */ for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++) @@ -276,7 +280,7 @@ add_option(FILE *f, np_arg_list **optlst) for (valend = valptr; valend < lineend; valend++) continue; --valend; - /* Finally trim off trailing spaces */ + /* finally trim off trailing spaces */ for (; isspace(*valend); valend--) continue; /* calculate the length of "--foo" */ @@ -297,7 +301,7 @@ add_option(FILE *f, np_arg_list **optlst) equals = 1; cfg_len += 1; } - /* A line with no equal sign isn't valid */ + /* a line with no equal sign isn't valid */ if (equals == 0) die(STATE_UNKNOWN, "%s\n", _("Config file error")); -- cgit v1.2.3-74-g34f1 From 6da7dba782f37eafdec595acfc3445a56d445915 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 10:53:44 +0200 Subject: lib/parse_ini.c: Add comment on NAGIOS_CONFIG_PATH We might want to spit out a warning when NAGIOS_CONFIG_PATH is used. While at it, move the function that handles this environment variable to the bottom. --- lib/parse_ini.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 2e47e06d..ede0e5fe 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -68,8 +68,9 @@ 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 */ +/* internal functions to find default file */ static char *default_file(void); +static char *default_file_in_path(void); /* * Parse_locator decomposes a string of the form @@ -343,6 +344,20 @@ add_option(FILE *f, np_arg_list **optlst) return 0; } +static char * +default_file(void) +{ + char **p, *ini_file; + + if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || + (ini_file = default_file_in_path()) != NULL) + return ini_file; + for (p = default_ini_path_names; *p != NULL; p++) + if (access(*p, F_OK) == 0) + return *p; + return NULL; +} + static char * default_file_in_path(void) { @@ -351,6 +366,7 @@ default_file_in_path(void) if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL) return NULL; + /* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */ if ((tokens = strdup(config_path)) == NULL) die(STATE_UNKNOWN, _("Insufficient Memory")); @@ -367,17 +383,3 @@ default_file_in_path(void) free(tokens); return NULL; } - -static char * -default_file(void) -{ - char **p, *ini_file; - - if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || - (ini_file = default_file_in_path()) != NULL) - return ini_file; - for (p = default_ini_path_names; *p != NULL; p++) - if (access(*p, F_OK) == 0) - return *p; - return NULL; -} -- cgit v1.2.3-74-g34f1 From f0b22b37f9554fa230a355fe2a1e45e5b59630f2 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 18:39:32 +0200 Subject: lib/parse_ini.c: Search for INI file in subdirs Add two path names to the list of default INI file locations, as some users/distributions prefer to put configuration files into subdirectories. --- lib/parse_ini.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index ede0e5fe..cd3d8271 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -46,7 +46,9 @@ static char *default_ini_file_names[] = { }; static char *default_ini_path_names[] = { + "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini", "/usr/local/etc/monitoring-plugins.ini", + "/etc/monitoring-plugins/monitoring-plugins.ini", "/etc/monitoring-plugins.ini", /* deprecated path names (for backward compatibility): */ "/etc/nagios/plugins.ini", -- cgit v1.2.3-74-g34f1 From b81c10e00cc71bf1be90510114e410ed691dc266 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 23:09:02 +0200 Subject: lib/parse_ini.c: Cosmetic change Replace an "if" with the ternary operator. --- lib/parse_ini.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index cd3d8271..30b79d74 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -120,10 +120,7 @@ np_get_defaults(const char *locator, const char *default_section) np_ini_info i; parse_locator(locator, default_section, &i); - if (strcmp(i.file, "-") == 0) - inifile = stdin; - else - inifile = fopen(i.file, "r"); + inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); if (inifile == NULL) die(STATE_UNKNOWN, "%s\n", _("Can't read config file")); -- cgit v1.2.3-74-g34f1 From 2bf7647be60cd53d9e54fdcf970a90fe08797819 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 23:15:21 +0200 Subject: lib/parse_ini.c: Add newline to die() calls Our die() function doesn't append a newline character to the message. --- lib/parse_ini.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 30b79d74..447bd454 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -368,11 +368,11 @@ default_file_in_path(void) /* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */ if ((tokens = strdup(config_path)) == NULL) - die(STATE_UNKNOWN, _("Insufficient Memory")); + die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory")); for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) { for (file = default_ini_file_names; *file != NULL; file++) { if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0) - die(STATE_UNKNOWN, _("Insufficient Memory")); + die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory")); if (access(ini_file, F_OK) == 0) { free(tokens); return ini_file; -- cgit v1.2.3-74-g34f1 From 48025ff39c3a78b7805bf803ac96730cef53e15c Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 23:22:12 +0200 Subject: lib/parse_ini.c: Drop privileges for reading file Read the configuration file with privileges temporarily dropped if the code is used by a setuid plugin. --- lib/parse_ini.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 447bd454..86b94e7d 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -22,6 +22,7 @@ *****************************************************************************/ #include "common.h" +#include "idpriv.h" #include "utils_base.h" #include "parse_ini.h" @@ -118,6 +119,11 @@ np_get_defaults(const char *locator, const char *default_section) FILE *inifile = NULL; np_arg_list *defaults = NULL; np_ini_info i; + int is_suid_plugin = mp_suid(); + + if (is_suid_plugin && idpriv_temp_drop() == -1) + die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), + strerror(errno)); parse_locator(locator, default_section, &i); inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); @@ -133,6 +139,10 @@ np_get_defaults(const char *locator, const char *default_section) if (inifile != stdin) fclose(inifile); free(i.stanza); + if (is_suid_plugin && idpriv_temp_restore() == -1) + die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), + strerror(errno)); + return defaults; } -- cgit v1.2.3-74-g34f1 From dc0f25cf76397b13f39a1d0fc50e9174114478ca Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Wed, 18 Jun 2014 23:42:57 +0200 Subject: lib/parse_ini.c: Print proper read error message Print a useful error message if opening the configuration file fails. --- lib/parse_ini.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 86b94e7d..25abc89b 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -129,7 +129,8 @@ np_get_defaults(const char *locator, const char *default_section) inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); if (inifile == NULL) - die(STATE_UNKNOWN, "%s\n", _("Can't read config file")); + die(STATE_UNKNOWN, _("Can't read config file: %s\n"), + strerror(errno)); if (read_defaults(inifile, i.stanza, &defaults) == FALSE) die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, -- cgit v1.2.3-74-g34f1 From eb85a612a3321c57efbd672f8b11bfefbc659876 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Sat, 21 Jun 2014 15:15:44 +0200 Subject: Add UID to state retention file path Add the UID of the invoking user to the state retention file path. This helps solving permission issues when different users run the same plugin. --- NEWS | 2 ++ lib/tests/test_utils.c | 8 ++++++-- lib/utils_base.c | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/NEWS b/NEWS index 0a1ef950..4c511790 100644 --- a/NEWS +++ b/NEWS @@ -23,6 +23,8 @@ This file documents the major additions and syntax changes between releases. been disabled because they were broken State retention: the NAGIOS_PLUGIN_STATE_DIRECTORY environment variable has been renamed MP_STATE_PATH. The old variable will continue to work in v1.6.x + Add the UID of the invoking user to the state retention file path. This helps solving + permission issues when different users run the same plugin check_swap used to allow returning OK on a system without swap when only percent thresholds were used. This is no longer the case and one must now use -n/--no-swap= The Perl and Shell plugins now use the PATH specified via ./configure's --trusted-path diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index 356887d5..f35b7e27 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -21,6 +21,7 @@ #include "tap.h" +#include #include #include @@ -29,6 +30,7 @@ int main (int argc, char **argv) { + char state_path[1024]; range *range; double temp; thresholds *thresholds = NULL; @@ -345,9 +347,10 @@ main (int argc, char **argv) np_enable_state("allowedchars_in_keyname", 77); temp_state_key = this_monitoring_plugin->state; + sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid()); ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); - ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" ); + ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" ); /* Don't do this test just yet. Will die */ @@ -359,12 +362,13 @@ main (int argc, char **argv) np_enable_state("funnykeyname", 54); temp_state_key = this_monitoring_plugin->state; + sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid()); ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); - ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); + ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" ); ok( temp_state_key->data_version==54, "Version set" ); temp_state_data = np_state_read(); diff --git a/lib/utils_base.c b/lib/utils_base.c index 04c4b4f9..55d35fdd 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -489,7 +489,9 @@ void np_enable_state(char *keyname, int expected_data_version) { this_state->state_data=NULL; /* Calculate filename */ - asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_monitoring_plugin->plugin_name, this_state->name); + asprintf(&temp_filename, "%s/%lu/%s/%s", + _np_state_calculate_location_prefix(), (unsigned long)geteuid(), + this_monitoring_plugin->plugin_name, this_state->name); this_state->_filename=temp_filename; this_monitoring_plugin->state = this_state; -- cgit v1.2.3-74-g34f1 From 9123f6146c5dd3285d8fb78cf3a8cd52bad17ec1 Mon Sep 17 00:00:00 2001 From: Spenser Reinhardt Date: Mon, 23 Jun 2014 13:54:39 -0500 Subject: lib/utils_cmd.c - Free file descriptor Coverity 66502 - File descriptor fd in cmd_file_read is never closed, and thus file is left open after usage throughout runtime. - SR --- lib/utils_cmd.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib') diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 4c6d0be1..9e214bd4 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -390,6 +390,9 @@ cmd_file_read ( char *filename, output *out, int flags) if(out) out->lines = _cmd_fetch_output (fd, out, flags); + + if (close(fd) == -1) + die( STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno) ); return 0; } -- cgit v1.2.3-74-g34f1 From ba21e26443385dd283d08e0419ff6ff25fedd0e8 Mon Sep 17 00:00:00 2001 From: Jan Wagner Date: Sun, 6 Jul 2014 12:58:04 +0200 Subject: check_icmp/check_dhcp: disable check, if we are root As it is possible to use capabilities(7) on linux or solaris privileges for example, it is not necessary in all cases to have those binaries making use of setuid. --- lib/utils_base.c | 13 ------------- lib/utils_base.h | 3 --- plugins-root/check_dhcp.c | 3 --- plugins-root/check_icmp.c | 3 --- 4 files changed, 22 deletions(-) (limited to 'lib') diff --git a/lib/utils_base.c b/lib/utils_base.c index 55d35fdd..addf26bd 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -300,19 +300,6 @@ char *np_escaped_string (const char *string) { int np_check_if_root(void) { return (geteuid() == 0); } -int np_warn_if_not_root(void) { - int status = np_check_if_root(); - if(!status) { - printf(_("Warning: ")); - printf(_("This plugin must be either run as root or setuid root.\n")); - printf(_("To run as root, you can use a tool like sudo.\n")); - printf(_("To set the setuid permissions, use the command:\n")); - /* XXX could we use something like progname? */ - printf("\tchmod u+s yourpluginfile\n"); - } - return status; -} - /* * Extract the value from key/value pairs, or return NULL. The value returned * can be free()ed. diff --git a/lib/utils_base.h b/lib/utils_base.h index d69b0da1..42ae0c09 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -75,9 +75,6 @@ void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3))) /* a simple check to see if we're running as root. * returns zero on failure, nonzero on success */ int np_check_if_root(void); -/* and a helpful wrapper around that. it returns the same status - * code from the above function, in case it's helpful for testing */ -int np_warn_if_not_root(void); /* mp_suid() returns true if the real and effective uids differs, such as when * running a suid plugin */ diff --git a/plugins-root/check_dhcp.c b/plugins-root/check_dhcp.c index b69a10da..b874c555 100644 --- a/plugins-root/check_dhcp.c +++ b/plugins-root/check_dhcp.c @@ -270,9 +270,6 @@ int main(int argc, char **argv){ usage4 (_("Could not parse arguments")); } - /* this plugin almost certainly needs root permissions. */ - np_warn_if_not_root(); - /* create socket for DHCP communications */ dhcp_socket=create_dhcp_socket(); diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c index 4b4197d8..8b563e40 100644 --- a/plugins-root/check_icmp.c +++ b/plugins-root/check_icmp.c @@ -383,9 +383,6 @@ main(int argc, char **argv) bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); - /* print a helpful error message if geteuid != 0 */ - np_warn_if_not_root(); - /* we only need to be setsuid when we get the sockets, so do * that before pointer magic (esp. on network data) */ icmp_sockerrno = udp_sockerrno = tcp_sockerrno = sockets = 0; -- cgit v1.2.3-74-g34f1