summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am11
-rw-r--r--lib/extra_opts.c4
-rw-r--r--lib/extra_opts.h2
-rw-r--r--lib/parse_ini.c497
-rw-r--r--lib/parse_ini.h44
-rw-r--r--lib/tests/Makefile.am7
-rwxr-xr-xlib/tests/test_base64.t2
-rw-r--r--lib/tests/test_cmd.t2
-rwxr-xr-xlib/tests/test_disk.t2
-rwxr-xr-xlib/tests/test_ini1.t2
-rwxr-xr-xlib/tests/test_ini3.t2
-rwxr-xr-xlib/tests/test_opts1.t2
-rwxr-xr-xlib/tests/test_opts2.t2
-rwxr-xr-xlib/tests/test_opts3.t2
-rwxr-xr-xlib/tests/test_tcp.t2
-rw-r--r--lib/tests/test_utils.c128
-rwxr-xr-xlib/tests/test_utils.t2
-rw-r--r--lib/utils_base.c136
-rw-r--r--lib/utils_base.h15
-rw-r--r--lib/utils_cmd.c7
-rw-r--r--lib/utils_cmd.h2
-rw-r--r--lib/utils_disk.c2
-rw-r--r--lib/utils_tcp.c2
23 files changed, 486 insertions, 391 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 99fa591a..01d73a64 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2,19 +2,18 @@
2 2
3SUBDIRS = . tests 3SUBDIRS = . tests
4 4
5noinst_LIBRARIES = libnagiosplug.a 5noinst_LIBRARIES = libmonitoringplug.a
6 6
7AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" 7AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \
8 -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
8 9
9libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c 10libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c
10EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h 11EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h
11 12
12if USE_PARSE_INI 13if USE_PARSE_INI
13libnagiosplug_a_SOURCES += parse_ini.c extra_opts.c 14libmonitoringplug_a_SOURCES += parse_ini.c extra_opts.c
14endif USE_PARSE_INI 15endif USE_PARSE_INI
15 16
16INCLUDES = -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
17
18test test-debug: 17test test-debug:
19 cd tests && make $@ 18 cd tests && make $@
20 19
diff --git a/lib/extra_opts.c b/lib/extra_opts.c
index 2939c7a1..f4d5e66a 100644
--- a/lib/extra_opts.c
+++ b/lib/extra_opts.c
@@ -1,9 +1,9 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios-plugins extra_opts library 3* Monitoring Plugins extra_opts library
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2007 Nagios Plugins Development Team 6* Copyright (c) 2007 Monitoring Plugins Development Team
7* 7*
8* This program is free software: you can redistribute it and/or modify 8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by 9* it under the terms of the GNU General Public License as published by
diff --git a/lib/extra_opts.h b/lib/extra_opts.h
index 4bb7b73b..8ff14a16 100644
--- a/lib/extra_opts.h
+++ b/lib/extra_opts.h
@@ -2,7 +2,7 @@
2#define _EXTRA_OPTS_H_ 2#define _EXTRA_OPTS_H_
3 3
4/* 4/*
5 * extra_opts.h: routines for loading nagios-plugin defaults from ini 5 * extra_opts.h: routines for loading monitoring-plugin defaults from ini
6 * configuration files. 6 * configuration files.
7 */ 7 */
8 8
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 004396fe..25abc89b 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -1,9 +1,9 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios-plugins parse_ini library 3* Monitoring Plugins parse_ini library
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2007 Nagios Plugins Development Team 6* Copyright (c) 2007 Monitoring Plugins Development Team
7* 7*
8* This program is free software: you can redistribute it and/or modify 8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by 9* it under the terms of the GNU General Public License as published by
@@ -22,16 +22,15 @@
22*****************************************************************************/ 22*****************************************************************************/
23 23
24#include "common.h" 24#include "common.h"
25#include "idpriv.h"
25#include "utils_base.h" 26#include "utils_base.h"
26#include "parse_ini.h" 27#include "parse_ini.h"
27#include <ctype.h>
28 28
29#include <ctype.h>
29#include <sys/types.h> 30#include <sys/types.h>
30#include <sys/stat.h> 31#include <sys/stat.h>
31#include <unistd.h> 32#include <unistd.h>
32 33
33/* TODO: die like N::P if config file is not found */
34
35/* np_ini_info contains the result of parsing a "locator" in the format 34/* np_ini_info contains the result of parsing a "locator" in the format
36 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example) 35 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
37 */ 36 */
@@ -40,254 +39,314 @@ typedef struct {
40 char *stanza; 39 char *stanza;
41} np_ini_info; 40} np_ini_info;
42 41
42static char *default_ini_file_names[] = {
43 "monitoring-plugins.ini",
44 "plugins.ini",
45 "nagios-plugins.ini",
46 NULL
47};
48
49static char *default_ini_path_names[] = {
50 "/usr/local/etc/monitoring-plugins/monitoring-plugins.ini",
51 "/usr/local/etc/monitoring-plugins.ini",
52 "/etc/monitoring-plugins/monitoring-plugins.ini",
53 "/etc/monitoring-plugins.ini",
54 /* deprecated path names (for backward compatibility): */
55 "/etc/nagios/plugins.ini",
56 "/usr/local/nagios/etc/plugins.ini",
57 "/usr/local/etc/nagios/plugins.ini",
58 "/etc/opt/nagios/plugins.ini",
59 "/etc/nagios-plugins.ini",
60 "/usr/local/etc/nagios-plugins.ini",
61 "/etc/opt/nagios-plugins.ini",
62 NULL
63};
64
43/* eat all characters from a FILE pointer until n is encountered */ 65/* 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)) 66#define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
45 67
46/* internal function that returns the constructed defaults options */ 68/* internal function that returns the constructed defaults options */
47static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); 69static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
70
48/* internal function that converts a single line into options format */ 71/* internal function that converts a single line into options format */
49static int add_option(FILE *f, np_arg_list **optlst); 72static int add_option(FILE *f, np_arg_list **optlst);
50/* internal function to find default file */
51static char* default_file(void);
52/* internal function to stat() files */
53static int test_file(const char* env, int len, const char* file, char* temp_file);
54 73
55/* parse_locator decomposes a string of the form 74/* internal functions to find default file */
75static char *default_file(void);
76static char *default_file_in_path(void);
77
78/*
79 * Parse_locator decomposes a string of the form
56 * [stanza][@filename] 80 * [stanza][@filename]
57 * into its seperate parts 81 * into its seperate parts.
58 */ 82 */
59static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){ 83static void
60 size_t locator_len=0, stanza_len=0; 84parse_locator(const char *locator, const char *def_stanza, np_ini_info *i)
85{
86 size_t locator_len = 0, stanza_len = 0;
61 87
62 /* if locator is NULL we'll use default values */ 88 /* if locator is NULL we'll use default values */
63 if(locator){ 89 if (locator != NULL) {
64 locator_len=strlen(locator); 90 locator_len = strlen(locator);
65 stanza_len=strcspn(locator, "@"); 91 stanza_len = strcspn(locator, "@");
66 } 92 }
67 /* if a non-default stanza is provided */ 93 /* if a non-default stanza is provided */
68 if(stanza_len>0){ 94 if (stanza_len > 0) {
69 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1)); 95 i->stanza = malloc(sizeof(char) * (stanza_len + 1));
70 strncpy(i->stanza, locator, stanza_len); 96 strncpy(i->stanza, locator, stanza_len);
71 i->stanza[stanza_len]='\0'; 97 i->stanza[stanza_len] = '\0';
72 } else { /* otherwise we use the default stanza */ 98 } else /* otherwise we use the default stanza */
73 i->stanza=strdup(def_stanza); 99 i->stanza = strdup(def_stanza);
74 }
75 /* if there is no @file part */
76 if(stanza_len==locator_len){
77 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 {
82 i->file=strdup(&(locator[stanza_len+1]));
83 }
84 100
85 if(i->file==NULL || i->stanza==NULL){ 101 if (i->stanza == NULL)
86 die(STATE_UNKNOWN, _("malloc() failed!\n")); 102 die(STATE_UNKNOWN, _("malloc() failed!\n"));
87 } 103
104 /* check whether there's an @file part */
105 i->file = stanza_len == locator_len
106 ? default_file()
107 : strdup(&(locator[stanza_len + 1]));
108 if (i->file == NULL || i->file[0] == '\0')
109 die(STATE_UNKNOWN,
110 _("Cannot find config file in any standard location.\n"));
88} 111}
89 112
90/* this is the externally visible function used by extra_opts */ 113/*
91np_arg_list* np_get_defaults(const char *locator, const char *default_section){ 114 * This is the externally visible function used by extra_opts.
92 FILE *inifile=NULL; 115 */
93 np_arg_list *defaults=NULL; 116np_arg_list *
117np_get_defaults(const char *locator, const char *default_section)
118{
119 FILE *inifile = NULL;
120 np_arg_list *defaults = NULL;
94 np_ini_info i; 121 np_ini_info i;
122 int is_suid_plugin = mp_suid();
95 123
96 parse_locator(locator, default_section, &i); 124 if (is_suid_plugin && idpriv_temp_drop() == -1)
97 /* if a file was specified or if we're using the default file */ 125 die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"),
98 if(i.file != NULL && strlen(i.file) > 0){ 126 strerror(errno));
99 if(strcmp(i.file, "-")==0){
100 inifile=stdin;
101 } else {
102 inifile=fopen(i.file, "r");
103 }
104 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
105 if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
106 die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
107 127
108 free(i.file); 128 parse_locator(locator, default_section, &i);
109 if(inifile!=stdin) fclose(inifile); 129 inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r");
110 } 130
131 if (inifile == NULL)
132 die(STATE_UNKNOWN, _("Can't read config file: %s\n"),
133 strerror(errno));
134 if (read_defaults(inifile, i.stanza, &defaults) == FALSE)
135 die(STATE_UNKNOWN,
136 _("Invalid section '%s' in config file '%s'\n"), i.stanza,
137 i.file);
138
139 free(i.file);
140 if (inifile != stdin)
141 fclose(inifile);
111 free(i.stanza); 142 free(i.stanza);
143 if (is_suid_plugin && idpriv_temp_restore() == -1)
144 die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"),
145 strerror(errno));
146
112 return defaults; 147 return defaults;
113} 148}
114 149
115/* read_defaults is where the meat of the parsing takes place. 150/*
151 * The read_defaults() function is where the meat of the parsing takes place.
116 * 152 *
117 * note that this may be called by a setuid binary, so we need to 153 * Note that this may be called by a setuid binary, so we need to
118 * be extra careful about user-supplied input (i.e. avoiding possible 154 * be extra careful about user-supplied input (i.e. avoiding possible
119 * format string vulnerabilities, etc) 155 * format string vulnerabilities, etc).
120 */ 156 */
121static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){ 157static int
122 int c, status=FALSE; 158read_defaults(FILE *f, const char *stanza, np_arg_list **opts)
159{
160 int c, status = FALSE;
123 size_t i, stanza_len; 161 size_t i, stanza_len;
124 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA; 162 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA;
125 163
126 stanza_len=strlen(stanza); 164 stanza_len = strlen(stanza);
127 165
128 /* our little stanza-parsing state machine. */ 166 /* our little stanza-parsing state machine */
129 while((c=fgetc(f))!=EOF){ 167 while ((c = fgetc(f)) != EOF) {
130 /* gobble up leading whitespace */ 168 /* gobble up leading whitespace */
131 if(isspace(c)) continue; 169 if (isspace(c))
132 switch(c){ 170 continue;
171 switch (c) {
133 /* globble up coment lines */ 172 /* globble up coment lines */
134 case ';': 173 case ';':
135 case '#': 174 case '#':
136 GOBBLE_TO(f, c, '\n'); 175 GOBBLE_TO(f, c, '\n');
137 break; 176 break;
138 /* start of a stanza. check to see if it matches */ 177 /* start of a stanza, check to see if it matches */
139 case '[': 178 case '[':
140 stanzastate=WRONGSTANZA; 179 stanzastate = WRONGSTANZA;
141 for(i=0; i<stanza_len; i++){ 180 for (i = 0; i < stanza_len; i++) {
142 c=fgetc(f); 181 c = fgetc(f);
143 /* Strip leading whitespace */ 182 /* strip leading whitespace */
144 if(i==0) for(c; isspace(c); c=fgetc(f)); 183 if (i == 0)
145 /* nope, read to the end of the line */ 184 for (; isspace(c); c = fgetc(f))
146 if(c!=stanza[i]) { 185 continue;
147 GOBBLE_TO(f, c, '\n'); 186 /* nope, read to the end of the line */
148 break; 187 if (c != stanza[i]) {
149 } 188 GOBBLE_TO(f, c, '\n');
150 } 189 break;
151 /* if it matched up to here and the next char is ']'... */
152 if(i==stanza_len){
153 c=fgetc(f);
154 /* Strip trailing whitespace */
155 for(c; isspace(c); c=fgetc(f));
156 if(c==']') stanzastate=RIGHTSTANZA;
157 } 190 }
158 break; 191 }
192 /* if it matched up to here and the next char is ']'... */
193 if (i == stanza_len) {
194 c = fgetc(f);
195 /* strip trailing whitespace */
196 for (; isspace(c); c = fgetc(f))
197 continue;
198 if (c == ']')
199 stanzastate = RIGHTSTANZA;
200 }
201 break;
159 /* otherwise, we're in the body of a stanza or a parse error */ 202 /* otherwise, we're in the body of a stanza or a parse error */
160 default: 203 default:
161 switch(stanzastate){ 204 switch (stanzastate) {
162 /* we never found the start of the first stanza, so 205 /* we never found the start of the first stanza, so
163 * we're dealing with a config error 206 * we're dealing with a config error
164 */ 207 */
165 case NOSTANZA: 208 case NOSTANZA:
166 die(STATE_UNKNOWN, _("Config file error")); 209 die(STATE_UNKNOWN, "%s\n",
167 break; 210 _("Config file error"));
168 /* we're in a stanza, but for a different plugin */ 211 /* we're in a stanza, but for a different plugin */
169 case WRONGSTANZA: 212 case WRONGSTANZA:
170 GOBBLE_TO(f, c, '\n'); 213 GOBBLE_TO(f, c, '\n');
171 break; 214 break;
172 /* okay, this is where we start taking the config */ 215 /* okay, this is where we start taking the config */
173 case RIGHTSTANZA: 216 case RIGHTSTANZA:
174 ungetc(c, f); 217 ungetc(c, f);
175 if(add_option(f, opts)){ 218 if (add_option(f, opts)) {
176 die(STATE_UNKNOWN, _("Config file error")); 219 die(STATE_UNKNOWN, "%s\n",
177 } 220 _("Config file error"));
178 status=TRUE;
179 break;
180 } 221 }
222 status = TRUE;
181 break; 223 break;
224 }
225 break;
182 } 226 }
183 } 227 }
184 return status; 228 return status;
185} 229}
186 230
187/* 231/*
188 * read one line of input in the format 232 * Read one line of input in the format
189 * ^option[[:space:]]*(=[[:space:]]*value)? 233 * ^option[[:space:]]*(=[[:space:]]*value)?
190 * and creates it as a cmdline argument 234 * and create it as a cmdline argument
191 * --option[=value] 235 * --option[=value]
192 * appending it to the linked list optbuf. 236 * appending it to the linked list optbuf.
193 */ 237 */
194static int add_option(FILE *f, np_arg_list **optlst){ 238static int
195 np_arg_list *opttmp=*optlst, *optnew; 239add_option(FILE *f, np_arg_list **optlst)
196 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL; 240{
197 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL; 241 np_arg_list *opttmp = *optlst, *optnew;
198 short done_reading=0, equals=0, value=0; 242 char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL;
199 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0; 243 char *eqptr = NULL, *valptr = NULL, *valend = NULL;
200 size_t opt_len=0, val_len=0; 244 short done_reading = 0, equals = 0, value = 0;
245 size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0;
246 size_t opt_len = 0, val_len = 0;
201 247
202 /* read one line from the file */ 248 /* read one line from the file */
203 while(!done_reading){ 249 while (!done_reading) {
204 /* grow if necessary */ 250 /* grow if necessary */
205 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){ 251 if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) {
206 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz; 252 linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz;
207 linebuf=realloc(linebuf, linebuf_sz); 253 linebuf = realloc(linebuf, linebuf_sz);
208 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); 254 if (linebuf == NULL)
255 die(STATE_UNKNOWN, _("malloc() failed!\n"));
209 } 256 }
210 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1; 257 if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL)
258 done_reading = 1;
211 else { 259 else {
212 read_pos=strlen(linebuf); 260 read_pos = strlen(linebuf);
213 if(linebuf[read_pos-1]=='\n') { 261 if (linebuf[read_pos - 1] == '\n') {
214 linebuf[--read_pos]='\0'; 262 linebuf[--read_pos] = '\0';
215 done_reading=1; 263 done_reading = 1;
216 } 264 }
217 } 265 }
218 } 266 }
219 lineend=&linebuf[read_pos]; 267 lineend = &linebuf[read_pos];
220 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */ 268 /* all that to read one line, isn't C fun? :) now comes the parsing :/ */
221 269
222 /* skip leading whitespace */ 270 /* skip leading whitespace */
223 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++); 271 for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++)
272 continue;
224 /* continue to '=' or EOL, watching for spaces that might precede it */ 273 /* continue to '=' or EOL, watching for spaces that might precede it */
225 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){ 274 for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) {
226 if(isspace(*eqptr) && optend==NULL) optend=eqptr; 275 if (isspace(*eqptr) && optend == NULL)
227 else optend=NULL; 276 optend = eqptr;
277 else
278 optend = NULL;
228 } 279 }
229 if(optend==NULL) optend=eqptr; 280 if (optend == NULL)
281 optend = eqptr;
230 --optend; 282 --optend;
231 /* ^[[:space:]]*=foo is a syntax error */ 283 /* ^[[:space:]]*=foo is a syntax error */
232 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n")); 284 if (optptr == eqptr)
285 die(STATE_UNKNOWN, "%s\n", _("Config file error"));
233 /* continue from '=' to start of value or EOL */ 286 /* continue from '=' to start of value or EOL */
234 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++); 287 for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr);
288 valptr++)
289 continue;
235 /* continue to the end of value */ 290 /* continue to the end of value */
236 for(valend=valptr; valend<lineend; valend++); 291 for (valend = valptr; valend < lineend; valend++)
292 continue;
237 --valend; 293 --valend;
238 /* Finally trim off trailing spaces */ 294 /* finally trim off trailing spaces */
239 for(valend; isspace(*valend); valend--); 295 for (; isspace(*valend); valend--)
296 continue;
240 /* calculate the length of "--foo" */ 297 /* calculate the length of "--foo" */
241 opt_len=1+optend-optptr; 298 opt_len = (size_t)(1 + optend - optptr);
242 /* 1-character params needs only one dash */ 299 /* 1-character params needs only one dash */
243 if(opt_len==1) 300 if (opt_len == 1)
244 cfg_len=1+(opt_len); 301 cfg_len = 1 + (opt_len);
245 else 302 else
246 cfg_len=2+(opt_len); 303 cfg_len = 2 + (opt_len);
247 /* if valptr<lineend then we have to also allocate space for "=bar" */ 304 /* if valptr<lineend then we have to also allocate space for "=bar" */
248 if(valptr<lineend) { 305 if (valptr < lineend) {
249 equals=value=1; 306 equals = value = 1;
250 val_len=1+valend-valptr; 307 val_len = (size_t)(1 + valend - valptr);
251 cfg_len+=1+val_len; 308 cfg_len += 1 + val_len;
252 } 309 }
253 /* if valptr==valend then we have "=" but no "bar" */ 310 /* if valptr==valend then we have "=" but no "bar" */
254 else if(valptr==lineend) { 311 else if (valptr == lineend) {
255 equals=1; 312 equals = 1;
256 cfg_len+=1; 313 cfg_len += 1;
257 } 314 }
258 /* A line with no equal sign isn't valid */ 315 /* a line with no equal sign isn't valid */
259 if(equals==0) die(STATE_UNKNOWN, _("Config file error\n")); 316 if (equals == 0)
317 die(STATE_UNKNOWN, "%s\n", _("Config file error"));
260 318
261 /* okay, now we have all the info we need, so we create a new np_arg_list 319 /* okay, now we have all the info we need, so we create a new np_arg_list
262 * element and set the argument... 320 * element and set the argument...
263 */ 321 */
264 optnew=(np_arg_list *)malloc(sizeof(np_arg_list)); 322 optnew = malloc(sizeof(np_arg_list));
265 optnew->next=NULL; 323 optnew->next = NULL;
266 324
267 read_pos=0; 325 read_pos = 0;
268 optnew->arg=(char *)malloc(cfg_len+1); 326 optnew->arg = malloc(cfg_len + 1);
269 /* 1-character params needs only one dash */ 327 /* 1-character params needs only one dash */
270 if(opt_len==1) { 328 if (opt_len == 1) {
271 strncpy(&optnew->arg[read_pos], "-", 1); 329 strncpy(&optnew->arg[read_pos], "-", 1);
272 read_pos+=1; 330 read_pos += 1;
273 } else { 331 } else {
274 strncpy(&optnew->arg[read_pos], "--", 2); 332 strncpy(&optnew->arg[read_pos], "--", 2);
275 read_pos+=2; 333 read_pos += 2;
276 } 334 }
277 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len; 335 strncpy(&optnew->arg[read_pos], optptr, opt_len);
278 if(value) { 336 read_pos += opt_len;
279 optnew->arg[read_pos++]='='; 337 if (value) {
280 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len; 338 optnew->arg[read_pos++] = '=';
339 strncpy(&optnew->arg[read_pos], valptr, val_len);
340 read_pos += val_len;
281 } 341 }
282 optnew->arg[read_pos]='\0'; 342 optnew->arg[read_pos] = '\0';
283 343
284 /* ...and put that to the end of the list */ 344 /* ...and put that to the end of the list */
285 if(*optlst==NULL) { 345 if (*optlst == NULL)
286 *optlst=optnew; 346 *optlst = optnew;
287 } else { 347 else {
288 while(opttmp->next!=NULL) { 348 while (opttmp->next != NULL)
289 opttmp=opttmp->next; 349 opttmp = opttmp->next;
290 }
291 opttmp->next = optnew; 350 opttmp->next = optnew;
292 } 351 }
293 352
@@ -295,72 +354,42 @@ static int add_option(FILE *f, np_arg_list **optlst){
295 return 0; 354 return 0;
296} 355}
297 356
298static char* default_file(void){ 357static char *
299 struct stat sb; 358default_file(void)
300 char *np_env=NULL, *default_file=NULL; 359{
301 char temp_file[MAX_INPUT_BUFFER]; 360 char **p, *ini_file;
302 size_t len; 361
303 362 if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL ||
304 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { 363 (ini_file = default_file_in_path()) != NULL)
305 /* skip any starting colon... */ 364 return ini_file;
306 while(*np_env==':') np_env++; 365 for (p = default_ini_path_names; *p != NULL; p++)
307 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in 366 if (access(*p, F_OK) == 0)
308 * every PATHs defined (colon-separated). 367 return *p;
309 */ 368 return NULL;
310 while((len=strcspn(np_env,":"))>0){
311 /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
312 if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
313 test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
314 default_file=strdup(temp_file);
315 break;
316 }
317
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 }
340
341 /* Return default_file or empty string (should return NULL if we want plugins
342 * to die there)...
343 */
344 if(default_file)
345 return default_file;
346 return "";
347} 369}
348 370
349/* put together len bytes from env and the filename and test for its 371static char *
350 * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed. 372default_file_in_path(void)
351 */ 373{
352static int test_file(const char* env, int len, const char* file, char* temp_file){ 374 char *config_path, **file;
353 struct stat sb; 375 char *dir, *ini_file, *tokens;
354 376
355 /* test if len + filelen + '/' + '\0' fits in temp_file */ 377 if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL)
356 if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1; 378 return NULL;
357 379 /* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */
358 strncpy(temp_file,env,len); 380
359 temp_file[len]='\0'; 381 if ((tokens = strdup(config_path)) == NULL)
360 strncat(temp_file,"/",len+1); 382 die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory"));
361 strncat(temp_file,file,len+strlen(file)+1); 383 for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) {
362 384 for (file = default_ini_file_names; *file != NULL; file++) {
363 if(stat(temp_file, &sb) != -1) return 1; 385 if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0)
364 return 0; 386 die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory"));
387 if (access(ini_file, F_OK) == 0) {
388 free(tokens);
389 return ini_file;
390 }
391 }
392 }
393 free(tokens);
394 return NULL;
365} 395}
366
diff --git a/lib/parse_ini.h b/lib/parse_ini.h
index e0ba8164..e37601b5 100644
--- a/lib/parse_ini.h
+++ b/lib/parse_ini.h
@@ -2,7 +2,7 @@
2#define _PARSE_INI_H_ 2#define _PARSE_INI_H_
3 3
4/* 4/*
5 * parse_ini.h: routines for loading nagios-plugin defaults from ini 5 * parse_ini.h: routines for loading monitoring-plugin defaults from ini
6 * configuration files. 6 * configuration files.
7 */ 7 */
8 8
@@ -13,50 +13,10 @@ typedef struct np_arg_el {
13 struct np_arg_el *next; 13 struct np_arg_el *next;
14} np_arg_list; 14} np_arg_list;
15 15
16/* FIXME: This is in plugins/common.c. Should be eventually moved to lib/
17 * (although for this particular one a configure settings should be ideal)
18 */
19#ifndef MAX_INPUT_BUFFER
20# define MAX_INPUT_BUFFER 8192
21#endif /* MAX_INPUT_BUFFER */
22
23/* Filenames (see below) */
24#ifndef NP_DEFAULT_INI_FILENAME1
25# define NP_DEFAULT_INI_FILENAME1 "plugins.ini"
26#endif /* NP_DEFAULT_INI_FILENAME1 */
27#ifndef NP_DEFAULT_INI_FILENAME2
28# define NP_DEFAULT_INI_FILENAME2 "nagios-plugins.ini"
29#endif /* NP_DEFAULT_INI_FILENAME2 */
30
31/* Config paths ending in nagios (search for NP_DEFAULT_INI_FILENAME1) */
32#ifndef NP_DEFAULT_INI_NAGIOS_PATH1
33# define NP_DEFAULT_INI_NAGIOS_PATH1 "/etc/nagios"
34#endif /* NP_DEFAULT_INI_NAGIOS_PATH1 */
35#ifndef NP_DEFAULT_INI_NAGIOS_PATH2
36# define NP_DEFAULT_INI_NAGIOS_PATH2 "/usr/local/nagios/etc"
37#endif /* NP_DEFAULT_INI_NAGIOS_PATH2 */
38#ifndef NP_DEFAULT_INI_NAGIOS_PATH3
39# define NP_DEFAULT_INI_NAGIOS_PATH3 "/usr/local/etc/nagios"
40#endif /* NP_DEFAULT_INI_NAGIOS_PATH3 */
41#ifndef NP_DEFAULT_INI_NAGIOS_PATH4
42# define NP_DEFAULT_INI_NAGIOS_PATH4 "/etc/opt/nagios"
43#endif /* NP_DEFAULT_INI_NAGIOS_PATH4 */
44
45/* Config paths not ending in nagios (search for NP_DEFAULT_INI_FILENAME2) */
46#ifndef NP_DEFAULT_INI_PATH1
47# define NP_DEFAULT_INI_PATH1 "/etc"
48#endif /* NP_DEFAULT_INI_PATH1 */
49#ifndef NP_DEFAULT_INI_PATH2
50# define NP_DEFAULT_INI_PATH2 "/usr/local/etc"
51#endif /* NP_DEFAULT_INI_PATH2 */
52#ifndef NP_DEFAULT_INI_PATH3
53# define NP_DEFAULT_INI_PATH3 "/etc/opt"
54#endif /* NP_DEFAULT_INI_PATH3 */
55
56/* np_load_defaults: load the default configuration (if present) for 16/* np_load_defaults: load the default configuration (if present) for
57 * a plugin from the ini file 17 * a plugin from the ini file
58 */ 18 */
59np_arg_list* np_get_defaults(const char *locator, const char *default_section); 19np_arg_list *np_get_defaults(const char *locator, const char *default_section);
60 20
61#endif /* _PARSE_INI_H_ */ 21#endif /* _PARSE_INI_H_ */
62 22
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am
index 1af5891a..a8d7adee 100644
--- a/lib/tests/Makefile.am
+++ b/lib/tests/Makefile.am
@@ -5,9 +5,8 @@ noinst_PROGRAMS = @EXTRA_TEST@
5TESTS = @EXTRA_TEST@ 5TESTS = @EXTRA_TEST@
6check_PROGRAMS = @EXTRA_TEST@ 6check_PROGRAMS = @EXTRA_TEST@
7 7
8AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" 8AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \
9 9 -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
10INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
11 10
12EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 11EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3
13 12
@@ -28,7 +27,7 @@ endif
28 27
29AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags) 28AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags)
30AM_LDFLAGS = $(tap_ldflags) -ltap 29AM_LDFLAGS = $(tap_ldflags) -ltap
31LDADD = $(top_srcdir)/lib/libnagiosplug.a $(top_srcdir)/gl/libgnu.a 30LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a
32 31
33SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c 32SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c
34 33
diff --git a/lib/tests/test_base64.t b/lib/tests/test_base64.t
index 4b9236f0..4ad64737 100755
--- a/lib/tests/test_base64.t
+++ b/lib/tests/test_base64.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_base64") { 3if (! -e "./test_base64") {
4 plan skip_all => "./test_base64 not compiled - please install tap library to test"; 4 plan skip_all => "./test_base64 not compiled - please enable libtap library to test";
5} 5}
6exec "./test_base64"; 6exec "./test_base64";
diff --git a/lib/tests/test_cmd.t b/lib/tests/test_cmd.t
index 4dd54ef9..c50dded8 100644
--- a/lib/tests/test_cmd.t
+++ b/lib/tests/test_cmd.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_cmd") { 3if (! -e "./test_cmd") {
4 plan skip_all => "./test_cmd not compiled - please install tap library to test"; 4 plan skip_all => "./test_cmd not compiled - please enable libtap library to test";
5} 5}
6exec "./test_cmd"; 6exec "./test_cmd";
diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t
index d32567a9..da84dfdf 100755
--- a/lib/tests/test_disk.t
+++ b/lib/tests/test_disk.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_disk") { 3if (! -e "./test_disk") {
4 plan skip_all => "./test_disk not compiled - please install tap library to test"; 4 plan skip_all => "./test_disk not compiled - please enable libtap library to test";
5} 5}
6exec "./test_disk"; 6exec "./test_disk";
diff --git a/lib/tests/test_ini1.t b/lib/tests/test_ini1.t
index d4810116..25baed91 100755
--- a/lib/tests/test_ini1.t
+++ b/lib/tests/test_ini1.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_ini1") { 3if (! -e "./test_ini1") {
4 plan skip_all => "./test_ini not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_ini not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6exec "./test_ini1"; 6exec "./test_ini1";
diff --git a/lib/tests/test_ini3.t b/lib/tests/test_ini3.t
index 4b0be4c6..a2ca94a4 100755
--- a/lib/tests/test_ini3.t
+++ b/lib/tests/test_ini3.t
@@ -4,7 +4,7 @@ use strict;
4use warnings; 4use warnings;
5 5
6if (! -e "./test_ini3") { 6if (! -e "./test_ini3") {
7 plan skip_all => "./test_ini not compiled - please install tap library and/or enable parse-ini to test"; 7 plan skip_all => "./test_ini not compiled - please enable libtap library and/or extra-opts to test";
8} 8}
9 9
10# array of argument arrays 10# array of argument arrays
diff --git a/lib/tests/test_opts1.t b/lib/tests/test_opts1.t
index 52eed942..1934f12b 100755
--- a/lib/tests/test_opts1.t
+++ b/lib/tests/test_opts1.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_opts1") { 3if (! -e "./test_opts1") {
4 plan skip_all => "./test_opts1 not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_opts1 not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6exec "./test_opts1"; 6exec "./test_opts1";
diff --git a/lib/tests/test_opts2.t b/lib/tests/test_opts2.t
index f7444f6f..59b9fdb8 100755
--- a/lib/tests/test_opts2.t
+++ b/lib/tests/test_opts2.t
@@ -1,7 +1,7 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_opts2") { 3if (! -e "./test_opts2") {
4 plan skip_all => "./test_opts2 not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_opts2 not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6$ENV{"NAGIOS_CONFIG_PATH"} = "."; 6$ENV{"NAGIOS_CONFIG_PATH"} = ".";
7exec "./test_opts2"; 7exec "./test_opts2";
diff --git a/lib/tests/test_opts3.t b/lib/tests/test_opts3.t
index fa7876d0..8d974ca0 100755
--- a/lib/tests/test_opts3.t
+++ b/lib/tests/test_opts3.t
@@ -4,7 +4,7 @@ use strict;
4use warnings; 4use warnings;
5 5
6if (! -e "./test_opts3") { 6if (! -e "./test_opts3") {
7 plan skip_all => "./test_opts3 not compiled - please install tap library and/or enable parse-ini to test"; 7 plan skip_all => "./test_opts3 not compiled - please enable libtap library and/or extra-opts to test";
8} 8}
9 9
10# array of argument arrays 10# array of argument arrays
diff --git a/lib/tests/test_tcp.t b/lib/tests/test_tcp.t
index 8f34b67b..e5e12fc8 100755
--- a/lib/tests/test_tcp.t
+++ b/lib/tests/test_tcp.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_tcp") { 3if (! -e "./test_tcp") {
4 plan skip_all => "./test_tcp not compiled - please install tap library to test"; 4 plan skip_all => "./test_tcp not compiled - please enable libtap library to test";
5} 5}
6exec "./test_tcp"; 6exec "./test_tcp";
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 79e1110a..f35b7e27 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -21,6 +21,7 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24#include <unistd.h>
24#include <sys/types.h> 25#include <sys/types.h>
25#include <sys/stat.h> 26#include <sys/stat.h>
26 27
@@ -29,31 +30,32 @@
29int 30int
30main (int argc, char **argv) 31main (int argc, char **argv)
31{ 32{
33 char state_path[1024];
32 range *range; 34 range *range;
33 double temp; 35 double temp;
34 thresholds *thresholds = NULL; 36 thresholds *thresholds = NULL;
35 int rc; 37 int i, rc;
36 char *temp_string; 38 char *temp_string;
37 state_key *temp_state_key = NULL; 39 state_key *temp_state_key = NULL;
38 state_data *temp_state_data; 40 state_data *temp_state_data;
39 time_t current_time; 41 time_t current_time;
40 42
41 plan_tests(150); 43 plan_tests(185);
42 44
43 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); 45 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised");
44 46
45 np_init( "check_test", argc, argv ); 47 np_init( "check_test", argc, argv );
46 48
47 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised"); 49 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised");
48 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); 50 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" );
49 51
50 ok( this_nagios_plugin->argc==argc, "Argc set" ); 52 ok( this_monitoring_plugin->argc==argc, "Argc set" );
51 ok( this_nagios_plugin->argv==argv, "Argv set" ); 53 ok( this_monitoring_plugin->argv==argv, "Argv set" );
52 54
53 np_set_args(0,0); 55 np_set_args(0,0);
54 56
55 ok( this_nagios_plugin->argc==0, "argc changed" ); 57 ok( this_monitoring_plugin->argc==0, "argc changed" );
56 ok( this_nagios_plugin->argv==0, "argv changed" ); 58 ok( this_monitoring_plugin->argv==0, "argv changed" );
57 59
58 np_set_args(argc, argv); 60 np_set_args(argc, argv);
59 61
@@ -182,6 +184,21 @@ main (int argc, char **argv)
182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 184 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 185 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
184 186
187 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20");
188 ok( rc == 0, "Thresholds ('-30:20', '-10:-2') set");
189 ok( thresholds->warning->start == -10, "Warning start set correctly");
190 ok( thresholds->warning->end == -2, "Warning end set correctly");
191 ok( thresholds->critical->start == -30, "Critical start set correctly");
192 ok( thresholds->critical->end == 20, "Critical end set correctly");
193 ok( get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical");
194 ok( get_status(-29, thresholds) == STATE_WARNING, "-29 - warning");
195 ok( get_status(-11, thresholds) == STATE_WARNING, "-11 - warning");
196 ok( get_status(-10, thresholds) == STATE_OK, "-10 - ok");
197 ok( get_status(-2, thresholds) == STATE_OK, "-2 - ok");
198 ok( get_status(-1, thresholds) == STATE_WARNING, "-1 - warning");
199 ok( get_status(19, thresholds) == STATE_WARNING, "19 - warning");
200 ok( get_status(21, thresholds) == STATE_CRITICAL, "21 - critical");
201
185 char *test; 202 char *test;
186 test = np_escaped_string("bob\\n"); 203 test = np_escaped_string("bob\\n");
187 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 204 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
@@ -296,23 +313,23 @@ main (int argc, char **argv)
296 diag( "You are probably running in wrong directory. Must run as ./test_utils" ); 313 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
297 314
298 315
299 this_nagios_plugin->argc=4; 316 this_monitoring_plugin->argc=4;
300 this_nagios_plugin->argv[0] = "./test_utils"; 317 this_monitoring_plugin->argv[0] = "./test_utils";
301 this_nagios_plugin->argv[1] = "here"; 318 this_monitoring_plugin->argv[1] = "here";
302 this_nagios_plugin->argv[2] = "--and"; 319 this_monitoring_plugin->argv[2] = "--and";
303 this_nagios_plugin->argv[3] = "now"; 320 this_monitoring_plugin->argv[3] = "now";
304 temp_string = (char *) _np_state_generate_key(); 321 temp_string = (char *) _np_state_generate_key();
305 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); 322 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
306 323
307 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 324 unsetenv("MP_STATE_PATH");
308 temp_string = (char *) _np_state_calculate_location_prefix(); 325 temp_string = (char *) _np_state_calculate_location_prefix();
309 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); 326 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
310 327
311 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); 328 setenv("MP_STATE_PATH", "", 1);
312 temp_string = (char *) _np_state_calculate_location_prefix(); 329 temp_string = (char *) _np_state_calculate_location_prefix();
313 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); 330 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
314 331
315 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); 332 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
316 temp_string = (char *) _np_state_calculate_location_prefix(); 333 temp_string = (char *) _np_state_calculate_location_prefix();
317 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); 334 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
318 335
@@ -320,36 +337,38 @@ main (int argc, char **argv)
320 337
321 ok(temp_state_key==NULL, "temp_state_key initially empty"); 338 ok(temp_state_key==NULL, "temp_state_key initially empty");
322 339
323 this_nagios_plugin->argc=1; 340 this_monitoring_plugin->argc=1;
324 this_nagios_plugin->argv[0] = "./test_utils"; 341 this_monitoring_plugin->argv[0] = "./test_utils";
325 np_enable_state(NULL, 51); 342 np_enable_state(NULL, 51);
326 temp_state_key = this_nagios_plugin->state; 343 temp_state_key = this_monitoring_plugin->state;
327 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 344 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
328 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); 345 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
329 346
330 347
331 np_enable_state("allowedchars_in_keyname", 77); 348 np_enable_state("allowedchars_in_keyname", 77);
332 temp_state_key = this_nagios_plugin->state; 349 temp_state_key = this_monitoring_plugin->state;
350 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid());
333 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 351 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
334 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); 352 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
335 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" ); 353 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
336 354
337 355
338 /* Don't do this test just yet. Will die */ 356 /* Don't do this test just yet. Will die */
339 /* 357 /*
340 np_enable_state("bad^chars$in@here", 77); 358 np_enable_state("bad^chars$in@here", 77);
341 temp_state_key = this_nagios_plugin->state; 359 temp_state_key = this_monitoring_plugin->state;
342 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); 360 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
343 */ 361 */
344 362
345 np_enable_state("funnykeyname", 54); 363 np_enable_state("funnykeyname", 54);
346 temp_state_key = this_nagios_plugin->state; 364 temp_state_key = this_monitoring_plugin->state;
365 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid());
347 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 366 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
348 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 367 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
349 368
350 369
351 370
352 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); 371 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
353 ok( temp_state_key->data_version==54, "Version set" ); 372 ok( temp_state_key->data_version==54, "Version set" );
354 373
355 temp_state_data = np_state_read(); 374 temp_state_data = np_state_read();
@@ -367,9 +386,9 @@ main (int argc, char **argv)
367 386
368 temp_state_key->_filename="var/statefile"; 387 temp_state_key->_filename="var/statefile";
369 temp_state_data = np_state_read(); 388 temp_state_data = np_state_read();
370 ok( this_nagios_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not"); 389 ok( this_monitoring_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not");
371 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" ); 390 ok( this_monitoring_plugin->state->state_data->time==1234567890, "Got time" );
372 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); 391 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" );
373 392
374 temp_state_key->data_version=53; 393 temp_state_key->data_version=53;
375 temp_state_data = np_state_read(); 394 temp_state_data = np_state_read();
@@ -379,7 +398,7 @@ main (int argc, char **argv)
379 temp_state_key->_filename="var/nonexistant"; 398 temp_state_key->_filename="var/nonexistant";
380 temp_state_data = np_state_read(); 399 temp_state_data = np_state_read();
381 ok( temp_state_data==NULL, "Missing file gives NULL" ); 400 ok( temp_state_data==NULL, "Missing file gives NULL" );
382 ok( this_nagios_plugin->state->state_data==NULL, "No state information" ); 401 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" );
383 402
384 temp_state_key->_filename="var/oldformat"; 403 temp_state_key->_filename="var/oldformat";
385 temp_state_data = np_state_read(); 404 temp_state_data = np_state_read();
@@ -426,7 +445,7 @@ main (int argc, char **argv)
426 temp_state_data = np_state_read(); 445 temp_state_data = np_state_read();
427 /* Check time is set to current_time */ 446 /* Check time is set to current_time */
428 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); 447 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
429 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); 448 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
430 449
431 450
432 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 451 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
@@ -438,7 +457,54 @@ main (int argc, char **argv)
438 457
439 np_cleanup(); 458 np_cleanup();
440 459
441 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" ); 460 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin");
461
462 ok(mp_suid() == FALSE, "Test aren't suid");
463
464 /* base states with random case */
465 char *states[] = {
466 "Ok",
467 "wArnINg",
468 "cRiTIcaL",
469 "UnKNoWN",
470 NULL
471 };
472
473 for (i=0; states[i]!=NULL; i++) {
474 /* out of the random case states, create the lower and upper versions + numeric string one */
475 char *statelower = strdup(states[i]);
476 char *stateupper = strdup(states[i]);
477 char statenum[2];
478 char *temp_ptr;
479 for (temp_ptr = statelower; *temp_ptr; temp_ptr++) {
480 *temp_ptr = tolower(*temp_ptr);
481 }
482 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
483 *temp_ptr = toupper(*temp_ptr);
484 }
485 snprintf(statenum, 2, "%i", i);
486
487 /* Base test names, we'll append the state string */
488 char testname[64] = "Translate state string: ";
489 int tlen = strlen(testname);
490
491 strcpy(testname+tlen, states[i]);
492 ok(i==mp_translate_state(states[i]), testname);
493
494 strcpy(testname+tlen, statelower);
495 ok(i==mp_translate_state(statelower), testname);
496
497 strcpy(testname+tlen, stateupper);
498 ok(i==mp_translate_state(stateupper), testname);
499
500 strcpy(testname+tlen, statenum);
501 ok(i==mp_translate_state(statenum), testname);
502 }
503 ok(ERROR==mp_translate_state("warningfewgw"), "Translate state string with garbage");
504 ok(ERROR==mp_translate_state("00"), "Translate state string: bad numeric string 1");
505 ok(ERROR==mp_translate_state("01"), "Translate state string: bad numeric string 2");
506 ok(ERROR==mp_translate_state("10"), "Translate state string: bad numeric string 3");
507 ok(ERROR==mp_translate_state(""), "Translate state string: empty string");
442 508
443 return exit_status(); 509 return exit_status();
444} 510}
diff --git a/lib/tests/test_utils.t b/lib/tests/test_utils.t
index 152eb718..e2a9311e 100755
--- a/lib/tests/test_utils.t
+++ b/lib/tests/test_utils.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_utils") { 3if (! -e "./test_utils") {
4 plan skip_all => "./test_utils not compiled - please install tap library to test"; 4 plan skip_all => "./test_utils not compiled - please enable libtap library to test";
5} 5}
6exec "./test_utils"; 6exec "./test_utils";
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 54463e92..55d35fdd 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -3,7 +3,7 @@
3* utils_base.c 3* utils_base.c
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2006 Nagios Plugins Development Team 6* Copyright (c) 2006 Monitoring Plugins Development Team
7* 7*
8* Library of useful functions for plugins 8* Library of useful functions for plugins
9* 9*
@@ -30,56 +30,58 @@
30#include <ctype.h> 30#include <ctype.h>
31#include <fcntl.h> 31#include <fcntl.h>
32#include <sys/stat.h> 32#include <sys/stat.h>
33#include <unistd.h>
34#include <sys/types.h>
33 35
34#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } 36#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
35 37
36nagios_plugin *this_nagios_plugin=NULL; 38monitoring_plugin *this_monitoring_plugin=NULL;
37 39
38int _np_state_read_file(FILE *); 40int _np_state_read_file(FILE *);
39 41
40void np_init( char *plugin_name, int argc, char **argv ) { 42void np_init( char *plugin_name, int argc, char **argv ) {
41 if (this_nagios_plugin==NULL) { 43 if (this_monitoring_plugin==NULL) {
42 this_nagios_plugin = calloc(1, sizeof(nagios_plugin)); 44 this_monitoring_plugin = calloc(1, sizeof(monitoring_plugin));
43 if (this_nagios_plugin==NULL) { 45 if (this_monitoring_plugin==NULL) {
44 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 46 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
45 strerror(errno)); 47 strerror(errno));
46 } 48 }
47 this_nagios_plugin->plugin_name = strdup(plugin_name); 49 this_monitoring_plugin->plugin_name = strdup(plugin_name);
48 if (this_nagios_plugin->plugin_name==NULL) 50 if (this_monitoring_plugin->plugin_name==NULL)
49 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 51 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
50 this_nagios_plugin->argc = argc; 52 this_monitoring_plugin->argc = argc;
51 this_nagios_plugin->argv = argv; 53 this_monitoring_plugin->argv = argv;
52 } 54 }
53} 55}
54 56
55void np_set_args( int argc, char **argv ) { 57void np_set_args( int argc, char **argv ) {
56 if (this_nagios_plugin==NULL) 58 if (this_monitoring_plugin==NULL)
57 die(STATE_UNKNOWN, _("This requires np_init to be called")); 59 die(STATE_UNKNOWN, _("This requires np_init to be called"));
58 60
59 this_nagios_plugin->argc = argc; 61 this_monitoring_plugin->argc = argc;
60 this_nagios_plugin->argv = argv; 62 this_monitoring_plugin->argv = argv;
61} 63}
62 64
63 65
64void np_cleanup() { 66void np_cleanup() {
65 if (this_nagios_plugin!=NULL) { 67 if (this_monitoring_plugin!=NULL) {
66 if(this_nagios_plugin->state!=NULL) { 68 if(this_monitoring_plugin->state!=NULL) {
67 if(this_nagios_plugin->state->state_data) { 69 if(this_monitoring_plugin->state->state_data) {
68 np_free(this_nagios_plugin->state->state_data->data); 70 np_free(this_monitoring_plugin->state->state_data->data);
69 np_free(this_nagios_plugin->state->state_data); 71 np_free(this_monitoring_plugin->state->state_data);
70 } 72 }
71 np_free(this_nagios_plugin->state->name); 73 np_free(this_monitoring_plugin->state->name);
72 np_free(this_nagios_plugin->state); 74 np_free(this_monitoring_plugin->state);
73 } 75 }
74 np_free(this_nagios_plugin->plugin_name); 76 np_free(this_monitoring_plugin->plugin_name);
75 np_free(this_nagios_plugin); 77 np_free(this_monitoring_plugin);
76 } 78 }
77 this_nagios_plugin=NULL; 79 this_monitoring_plugin=NULL;
78} 80}
79 81
80/* Hidden function to get a pointer to this_nagios_plugin for testing */ 82/* Hidden function to get a pointer to this_monitoring_plugin for testing */
81void _get_nagios_plugin( nagios_plugin **pointer ){ 83void _get_monitoring_plugin( monitoring_plugin **pointer ){
82 *pointer = this_nagios_plugin; 84 *pointer = this_monitoring_plugin;
83} 85}
84 86
85void 87void
@@ -89,7 +91,7 @@ die (int result, const char *fmt, ...)
89 va_start (ap, fmt); 91 va_start (ap, fmt);
90 vprintf (fmt, ap); 92 vprintf (fmt, ap);
91 va_end (ap); 93 va_end (ap);
92 if(this_nagios_plugin!=NULL) { 94 if(this_monitoring_plugin!=NULL) {
93 np_cleanup(); 95 np_cleanup();
94 } 96 }
95 exit (result); 97 exit (result);
@@ -367,6 +369,23 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
367 return value; 369 return value;
368} 370}
369 371
372
373/*
374 * Read a string representing a state (ok, warning... or numeric: 0, 1) and
375 * return the corresponding STATE_ value or ERROR)
376 */
377int mp_translate_state (char *state_text) {
378 if (!strcasecmp(state_text,"OK") || !strcmp(state_text,"0"))
379 return STATE_OK;
380 if (!strcasecmp(state_text,"WARNING") || !strcmp(state_text,"1"))
381 return STATE_WARNING;
382 if (!strcasecmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
383 return STATE_CRITICAL;
384 if (!strcasecmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
385 return STATE_UNKNOWN;
386 return ERROR;
387}
388
370/* 389/*
371 * Returns a string to use as a keyname, based on an md5 hash of argv, thus 390 * Returns a string to use as a keyname, based on an md5 hash of argv, thus
372 * hopefully a unique key per service/plugin invocation. Use the extra-opts 391 * hopefully a unique key per service/plugin invocation. Use the extra-opts
@@ -375,14 +394,14 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
375char *_np_state_generate_key() { 394char *_np_state_generate_key() {
376 struct sha1_ctx ctx; 395 struct sha1_ctx ctx;
377 int i; 396 int i;
378 char **argv = this_nagios_plugin->argv; 397 char **argv = this_monitoring_plugin->argv;
379 unsigned char result[20]; 398 unsigned char result[20];
380 char keyname[41]; 399 char keyname[41];
381 char *p=NULL; 400 char *p=NULL;
382 401
383 sha1_init_ctx(&ctx); 402 sha1_init_ctx(&ctx);
384 403
385 for(i=0; i<this_nagios_plugin->argc; i++) { 404 for(i=0; i<this_monitoring_plugin->argc; i++) {
386 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); 405 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
387 } 406 }
388 407
@@ -401,9 +420,9 @@ char *_np_state_generate_key() {
401} 420}
402 421
403void _cleanup_state_data() { 422void _cleanup_state_data() {
404 if (this_nagios_plugin->state->state_data!=NULL) { 423 if (this_monitoring_plugin->state->state_data!=NULL) {
405 np_free(this_nagios_plugin->state->state_data->data); 424 np_free(this_monitoring_plugin->state->state_data->data);
406 np_free(this_nagios_plugin->state->state_data); 425 np_free(this_monitoring_plugin->state->state_data);
407 } 426 }
408} 427}
409 428
@@ -415,9 +434,18 @@ void _cleanup_state_data() {
415char* _np_state_calculate_location_prefix(){ 434char* _np_state_calculate_location_prefix(){
416 char *env_dir; 435 char *env_dir;
417 436
418 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 437 /* Do not allow passing MP_STATE_PATH in setuid plugins
419 if(env_dir && env_dir[0] != '\0') 438 * for security reasons */
420 return env_dir; 439 if (mp_suid() == FALSE) {
440 env_dir = getenv("MP_STATE_PATH");
441 if(env_dir && env_dir[0] != '\0')
442 return env_dir;
443 /* This is the former ENV, for backward-compatibility */
444 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
445 if(env_dir && env_dir[0] != '\0')
446 return env_dir;
447 }
448
421 return NP_STATE_DIR_PREFIX; 449 return NP_STATE_DIR_PREFIX;
422} 450}
423 451
@@ -432,7 +460,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
432 char *temp_keyname = NULL; 460 char *temp_keyname = NULL;
433 char *p=NULL; 461 char *p=NULL;
434 462
435 if(this_nagios_plugin==NULL) 463 if(this_monitoring_plugin==NULL)
436 die(STATE_UNKNOWN, _("This requires np_init to be called")); 464 die(STATE_UNKNOWN, _("This requires np_init to be called"));
437 465
438 this_state = (state_key *) calloc(1, sizeof(state_key)); 466 this_state = (state_key *) calloc(1, sizeof(state_key));
@@ -456,15 +484,17 @@ void np_enable_state(char *keyname, int expected_data_version) {
456 p++; 484 p++;
457 } 485 }
458 this_state->name=temp_keyname; 486 this_state->name=temp_keyname;
459 this_state->plugin_name=this_nagios_plugin->plugin_name; 487 this_state->plugin_name=this_monitoring_plugin->plugin_name;
460 this_state->data_version=expected_data_version; 488 this_state->data_version=expected_data_version;
461 this_state->state_data=NULL; 489 this_state->state_data=NULL;
462 490
463 /* Calculate filename */ 491 /* Calculate filename */
464 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name); 492 asprintf(&temp_filename, "%s/%lu/%s/%s",
493 _np_state_calculate_location_prefix(), (unsigned long)geteuid(),
494 this_monitoring_plugin->plugin_name, this_state->name);
465 this_state->_filename=temp_filename; 495 this_state->_filename=temp_filename;
466 496
467 this_nagios_plugin->state = this_state; 497 this_monitoring_plugin->state = this_state;
468} 498}
469 499
470/* 500/*
@@ -479,11 +509,11 @@ state_data *np_state_read() {
479 FILE *statefile; 509 FILE *statefile;
480 int rc = FALSE; 510 int rc = FALSE;
481 511
482 if(this_nagios_plugin==NULL) 512 if(this_monitoring_plugin==NULL)
483 die(STATE_UNKNOWN, _("This requires np_init to be called")); 513 die(STATE_UNKNOWN, _("This requires np_init to be called"));
484 514
485 /* Open file. If this fails, no previous state found */ 515 /* Open file. If this fails, no previous state found */
486 statefile = fopen( this_nagios_plugin->state->_filename, "r" ); 516 statefile = fopen( this_monitoring_plugin->state->_filename, "r" );
487 if(statefile!=NULL) { 517 if(statefile!=NULL) {
488 518
489 this_state_data = (state_data *) calloc(1, sizeof(state_data)); 519 this_state_data = (state_data *) calloc(1, sizeof(state_data));
@@ -492,7 +522,7 @@ state_data *np_state_read() {
492 strerror(errno)); 522 strerror(errno));
493 523
494 this_state_data->data=NULL; 524 this_state_data->data=NULL;
495 this_nagios_plugin->state->state_data = this_state_data; 525 this_monitoring_plugin->state->state_data = this_state_data;
496 526
497 rc = _np_state_read_file(statefile); 527 rc = _np_state_read_file(statefile);
498 528
@@ -503,10 +533,10 @@ state_data *np_state_read() {
503 _cleanup_state_data(); 533 _cleanup_state_data();
504 } 534 }
505 535
506 return this_nagios_plugin->state->state_data; 536 return this_monitoring_plugin->state->state_data;
507} 537}
508 538
509/* 539/*
510 * Read the state file 540 * Read the state file
511 */ 541 */
512int _np_state_read_file(FILE *f) { 542int _np_state_read_file(FILE *f) {
@@ -544,7 +574,7 @@ int _np_state_read_file(FILE *f) {
544 break; 574 break;
545 case STATE_DATA_VERSION: 575 case STATE_DATA_VERSION:
546 i=atoi(line); 576 i=atoi(line);
547 if(i != this_nagios_plugin->state->data_version) 577 if(i != this_monitoring_plugin->state->data_version)
548 failure++; 578 failure++;
549 else 579 else
550 expected=STATE_DATA_TIME; 580 expected=STATE_DATA_TIME;
@@ -555,13 +585,13 @@ int _np_state_read_file(FILE *f) {
555 if(data_time > current_time) 585 if(data_time > current_time)
556 failure++; 586 failure++;
557 else { 587 else {
558 this_nagios_plugin->state->state_data->time = data_time; 588 this_monitoring_plugin->state->state_data->time = data_time;
559 expected=STATE_DATA_TEXT; 589 expected=STATE_DATA_TEXT;
560 } 590 }
561 break; 591 break;
562 case STATE_DATA_TEXT: 592 case STATE_DATA_TEXT:
563 this_nagios_plugin->state->state_data->data = strdup(line); 593 this_monitoring_plugin->state->state_data->data = strdup(line);
564 if(this_nagios_plugin->state->state_data->data==NULL) 594 if(this_monitoring_plugin->state->state_data->data==NULL)
565 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 595 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
566 expected=STATE_DATA_END; 596 expected=STATE_DATA_END;
567 status=TRUE; 597 status=TRUE;
@@ -596,8 +626,8 @@ void np_state_write_string(time_t data_time, char *data_string) {
596 current_time=data_time; 626 current_time=data_time;
597 627
598 /* If file doesn't currently exist, create directories */ 628 /* If file doesn't currently exist, create directories */
599 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) { 629 if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) {
600 asprintf(&directories, "%s", this_nagios_plugin->state->_filename); 630 asprintf(&directories, "%s", this_monitoring_plugin->state->_filename);
601 if(directories==NULL) 631 if(directories==NULL)
602 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 632 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
603 strerror(errno)); 633 strerror(errno));
@@ -607,7 +637,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
607 *p='\0'; 637 *p='\0';
608 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) { 638 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
609 /* Can't free this! Otherwise error message is wrong! */ 639 /* Can't free this! Otherwise error message is wrong! */
610 /* np_free(directories); */ 640 /* np_free(directories); */
611 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); 641 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
612 } 642 }
613 *p='/'; 643 *p='/';
@@ -616,7 +646,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
616 np_free(directories); 646 np_free(directories);
617 } 647 }
618 648
619 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename); 649 asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename);
620 if(temp_file==NULL) 650 if(temp_file==NULL)
621 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 651 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
622 strerror(errno)); 652 strerror(errno));
@@ -636,7 +666,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
636 666
637 fprintf(fp,"# NP State file\n"); 667 fprintf(fp,"# NP State file\n");
638 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); 668 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
639 fprintf(fp,"%d\n",this_nagios_plugin->state->data_version); 669 fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version);
640 fprintf(fp,"%lu\n",current_time); 670 fprintf(fp,"%lu\n",current_time);
641 fprintf(fp,"%s\n",data_string); 671 fprintf(fp,"%s\n",data_string);
642 672
@@ -654,7 +684,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
654 die(STATE_UNKNOWN, _("Error writing temp file")); 684 die(STATE_UNKNOWN, _("Error writing temp file"));
655 } 685 }
656 686
657 if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) { 687 if(rename(temp_file, this_monitoring_plugin->state->_filename)!=0) {
658 unlink(temp_file); 688 unlink(temp_file);
659 np_free(temp_file); 689 np_free(temp_file);
660 die(STATE_UNKNOWN, _("Cannot rename state temp file")); 690 die(STATE_UNKNOWN, _("Cannot rename state temp file"));
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 0cb371fd..d69b0da1 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -1,6 +1,6 @@
1#ifndef _UTILS_BASE_ 1#ifndef _UTILS_BASE_
2#define _UTILS_BASE_ 2#define _UTILS_BASE_
3/* Header file for nagios plugins utils_base.c */ 3/* Header file for Monitoring Plugins utils_base.c */
4 4
5#include "sha1.h" 5#include "sha1.h"
6 6
@@ -52,7 +52,7 @@ typedef struct np_struct {
52 state_key *state; 52 state_key *state;
53 int argc; 53 int argc;
54 char **argv; 54 char **argv;
55 } nagios_plugin; 55 } monitoring_plugin;
56 56
57range *parse_range_string (char *); 57range *parse_range_string (char *);
58int _set_thresholds(thresholds **, char *, char *); 58int _set_thresholds(thresholds **, char *, char *);
@@ -62,7 +62,7 @@ int check_range(double, range *);
62int get_status(double, thresholds *); 62int get_status(double, thresholds *);
63 63
64/* All possible characters in a threshold range */ 64/* All possible characters in a threshold range */
65#define NP_THRESHOLDS_CHARS "0123456789.:@~" 65#define NP_THRESHOLDS_CHARS "-0123456789.:@~"
66 66
67char *np_escaped_string (const char *); 67char *np_escaped_string (const char *);
68 68
@@ -79,6 +79,10 @@ int np_check_if_root(void);
79 * code from the above function, in case it's helpful for testing */ 79 * code from the above function, in case it's helpful for testing */
80int np_warn_if_not_root(void); 80int np_warn_if_not_root(void);
81 81
82/* mp_suid() returns true if the real and effective uids differs, such as when
83 * running a suid plugin */
84#define mp_suid() (getuid() != geteuid())
85
82/* 86/*
83 * Extract the value from key/value pairs, or return NULL. The value returned 87 * Extract the value from key/value pairs, or return NULL. The value returned
84 * can be free()ed. 88 * can be free()ed.
@@ -93,6 +97,11 @@ char *np_extract_value(const char*, const char*, char);
93 */ 97 */
94#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') 98#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
95 99
100/*
101 * Read a string representing a state (ok, warning... or numeric: 0, 1) and
102 * return the corresponding NP_STATE or ERROR)
103 */
104int mp_translate_state (char *);
96 105
97void np_enable_state(char *, int); 106void np_enable_state(char *, int);
98state_data *np_state_read(); 107state_data *np_state_read();
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c
index 0c853dcc..9e214bd4 100644
--- a/lib/utils_cmd.c
+++ b/lib/utils_cmd.c
@@ -1,9 +1,9 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios run command utilities 3* Monitoring run command utilities
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2005-2006 Nagios Plugins Development Team 6* Copyright (c) 2005-2006 Monitoring Plugins Development Team
7* 7*
8* Description : 8* Description :
9* 9*
@@ -390,6 +390,9 @@ cmd_file_read ( char *filename, output *out, int flags)
390 390
391 if(out) 391 if(out)
392 out->lines = _cmd_fetch_output (fd, out, flags); 392 out->lines = _cmd_fetch_output (fd, out, flags);
393
394 if (close(fd) == -1)
395 die( STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno) );
393 396
394 return 0; 397 return 0;
395} 398}
diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h
index 8ebb5894..ebaf15be 100644
--- a/lib/utils_cmd.h
+++ b/lib/utils_cmd.h
@@ -2,7 +2,7 @@
2#define _UTILS_CMD_ 2#define _UTILS_CMD_
3 3
4/* 4/*
5 * Header file for nagios plugins utils_cmd.c 5 * Header file for Monitoring Plugins utils_cmd.c
6 * 6 *
7 * 7 *
8 */ 8 */
diff --git a/lib/utils_disk.c b/lib/utils_disk.c
index 5be2b2c0..efe35fc5 100644
--- a/lib/utils_disk.c
+++ b/lib/utils_disk.c
@@ -3,7 +3,7 @@
3* Library for check_disk 3* Library for check_disk
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 1999-2007 Nagios Plugins Development Team 6* Copyright (c) 1999-2007 Monitoring Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c
index 46ad7f72..b37c446f 100644
--- a/lib/utils_tcp.c
+++ b/lib/utils_tcp.c
@@ -3,7 +3,7 @@
3* Library for check_tcp 3* Library for check_tcp
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 1999-2013 Nagios Plugins Development Team 6* Copyright (c) 1999-2013 Monitoring Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*