Create expired cert for testing purposes. Updated tests to check
[monitoring-plugins.git] / lib / parse_ini.c
blob004396fe803378c18c501bb17743364b8cb6508b
1 /*****************************************************************************
2 *
3 * Nagios-plugins parse_ini library
4 *
5 * License: GPL
6 * Copyright (c) 2007 Nagios Plugins Development Team
7 *
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
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *****************************************************************************/
24 #include "common.h"
25 #include "utils_base.h"
26 #include "parse_ini.h"
27 #include <ctype.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
33 /* TODO: die like N::P if config file is not found */
35 /* 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)
38 typedef struct {
39 char *file;
40 char *stanza;
41 } np_ini_info;
43 /* 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))
46 /* internal function that returns the constructed defaults options */
47 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
48 /* internal function that converts a single line into options format */
49 static int add_option(FILE *f, np_arg_list **optlst);
50 /* internal function to find default file */
51 static char* default_file(void);
52 /* internal function to stat() files */
53 static int test_file(const char* env, int len, const char* file, char* temp_file);
55 /* parse_locator decomposes a string of the form
56 * [stanza][@filename]
57 * into its seperate parts
59 static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
60 size_t locator_len=0, stanza_len=0;
62 /* if locator is NULL we'll use default values */
63 if(locator){
64 locator_len=strlen(locator);
65 stanza_len=strcspn(locator, "@");
67 /* if a non-default stanza is provided */
68 if(stanza_len>0){
69 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
70 strncpy(i->stanza, locator, stanza_len);
71 i->stanza[stanza_len]='\0';
72 } else { /* otherwise we use the default stanza */
73 i->stanza=strdup(def_stanza);
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);
81 } else {
82 i->file=strdup(&(locator[stanza_len+1]));
85 if(i->file==NULL || i->stanza==NULL){
86 die(STATE_UNKNOWN, _("malloc() failed!\n"));
90 /* this is the externally visible function used by extra_opts */
91 np_arg_list* np_get_defaults(const char *locator, const char *default_section){
92 FILE *inifile=NULL;
93 np_arg_list *defaults=NULL;
94 np_ini_info i;
96 parse_locator(locator, default_section, &i);
97 /* if a file was specified or if we're using the default file */
98 if(i.file != NULL && strlen(i.file) > 0){
99 if(strcmp(i.file, "-")==0){
100 inifile=stdin;
101 } else {
102 inifile=fopen(i.file, "r");
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);
108 free(i.file);
109 if(inifile!=stdin) fclose(inifile);
111 free(i.stanza);
112 return defaults;
115 /* read_defaults is where the meat of the parsing takes place.
117 * 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
119 * format string vulnerabilities, etc)
121 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
122 int c, status=FALSE;
123 size_t i, stanza_len;
124 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
126 stanza_len=strlen(stanza);
128 /* our little stanza-parsing state machine. */
129 while((c=fgetc(f))!=EOF){
130 /* gobble up leading whitespace */
131 if(isspace(c)) continue;
132 switch(c){
133 /* globble up coment lines */
134 case ';':
135 case '#':
136 GOBBLE_TO(f, c, '\n');
137 break;
138 /* start of a stanza. check to see if it matches */
139 case '[':
140 stanzastate=WRONGSTANZA;
141 for(i=0; i<stanza_len; i++){
142 c=fgetc(f);
143 /* Strip leading whitespace */
144 if(i==0) for(c; isspace(c); c=fgetc(f));
145 /* nope, read to the end of the line */
146 if(c!=stanza[i]) {
147 GOBBLE_TO(f, c, '\n');
148 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;
158 break;
159 /* otherwise, we're in the body of a stanza or a parse error */
160 default:
161 switch(stanzastate){
162 /* we never found the start of the first stanza, so
163 * we're dealing with a config error
165 case NOSTANZA:
166 die(STATE_UNKNOWN, _("Config file error"));
167 break;
168 /* we're in a stanza, but for a different plugin */
169 case WRONGSTANZA:
170 GOBBLE_TO(f, c, '\n');
171 break;
172 /* okay, this is where we start taking the config */
173 case RIGHTSTANZA:
174 ungetc(c, f);
175 if(add_option(f, opts)){
176 die(STATE_UNKNOWN, _("Config file error"));
178 status=TRUE;
179 break;
181 break;
184 return status;
188 * read one line of input in the format
189 * ^option[[:space:]]*(=[[:space:]]*value)?
190 * and creates it as a cmdline argument
191 * --option[=value]
192 * appending it to the linked list optbuf.
194 static int add_option(FILE *f, np_arg_list **optlst){
195 np_arg_list *opttmp=*optlst, *optnew;
196 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
197 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
198 short done_reading=0, equals=0, value=0;
199 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
200 size_t opt_len=0, val_len=0;
202 /* read one line from the file */
203 while(!done_reading){
204 /* grow if necessary */
205 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
206 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
207 linebuf=realloc(linebuf, linebuf_sz);
208 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
210 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
211 else {
212 read_pos=strlen(linebuf);
213 if(linebuf[read_pos-1]=='\n') {
214 linebuf[--read_pos]='\0';
215 done_reading=1;
219 lineend=&linebuf[read_pos];
220 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
222 /* skip leading whitespace */
223 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
224 /* continue to '=' or EOL, watching for spaces that might precede it */
225 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
226 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
227 else optend=NULL;
229 if(optend==NULL) optend=eqptr;
230 --optend;
231 /* ^[[:space:]]*=foo is a syntax error */
232 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
233 /* continue from '=' to start of value or EOL */
234 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
235 /* continue to the end of value */
236 for(valend=valptr; valend<lineend; valend++);
237 --valend;
238 /* Finally trim off trailing spaces */
239 for(valend; isspace(*valend); valend--);
240 /* calculate the length of "--foo" */
241 opt_len=1+optend-optptr;
242 /* 1-character params needs only one dash */
243 if(opt_len==1)
244 cfg_len=1+(opt_len);
245 else
246 cfg_len=2+(opt_len);
247 /* if valptr<lineend then we have to also allocate space for "=bar" */
248 if(valptr<lineend) {
249 equals=value=1;
250 val_len=1+valend-valptr;
251 cfg_len+=1+val_len;
253 /* if valptr==valend then we have "=" but no "bar" */
254 else if(valptr==lineend) {
255 equals=1;
256 cfg_len+=1;
258 /* A line with no equal sign isn't valid */
259 if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
261 /* okay, now we have all the info we need, so we create a new np_arg_list
262 * element and set the argument...
264 optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
265 optnew->next=NULL;
267 read_pos=0;
268 optnew->arg=(char *)malloc(cfg_len+1);
269 /* 1-character params needs only one dash */
270 if(opt_len==1) {
271 strncpy(&optnew->arg[read_pos], "-", 1);
272 read_pos+=1;
273 } else {
274 strncpy(&optnew->arg[read_pos], "--", 2);
275 read_pos+=2;
277 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
278 if(value) {
279 optnew->arg[read_pos++]='=';
280 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
282 optnew->arg[read_pos]='\0';
284 /* ...and put that to the end of the list */
285 if(*optlst==NULL) {
286 *optlst=optnew;
287 } else {
288 while(opttmp->next!=NULL) {
289 opttmp=opttmp->next;
291 opttmp->next = optnew;
294 free(linebuf);
295 return 0;
298 static char* default_file(void){
299 struct stat sb;
300 char *np_env=NULL, *default_file=NULL;
301 char temp_file[MAX_INPUT_BUFFER];
302 size_t len;
304 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
305 /* skip any starting colon... */
306 while(*np_env==':') np_env++;
307 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
308 * every PATHs defined (colon-separated).
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;
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")) */
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);
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);
341 /* Return default_file or empty string (should return NULL if we want plugins
342 * to die there)...
344 if(default_file)
345 return default_file;
346 return "";
349 /* put together len bytes from env and the filename and test for its
350 * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
352 static int test_file(const char* env, int len, const char* file, char* temp_file){
353 struct stat sb;
355 /* test if len + filelen + '/' + '\0' fits in temp_file */
356 if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
358 strncpy(temp_file,env,len);
359 temp_file[len]='\0';
360 strncat(temp_file,"/",len+1);
361 strncat(temp_file,file,len+strlen(file)+1);
363 if(stat(temp_file, &sb) != -1) return 1;
364 return 0;