67d8367f5503c21a7fd5cd3217bcdd60f055ac6b
1 /*****************************************************************************
2 *
3 * Nagios-plugins parse_ini library
4 *
5 * License: GPL
6 * Copyright (c) 2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * $Id$
25 *
26 *****************************************************************************/
28 #include "common.h"
29 #include "parse_ini.h"
30 #include "utils_base.h"
31 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
37 /* FIXME: N::P dies if section is not found */
38 /* FIXME: N::P dies if config file is not found */
40 /* np_ini_info contains the result of parsing a "locator" in the format
41 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
42 */
43 typedef struct {
44 char *file;
45 char *stanza;
46 } np_ini_info;
48 /* eat all characters from a FILE pointer until n is encountered */
49 #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
51 /* internal function that returns the constructed defaults options */
52 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
53 /* internal function that converts a single line into options format */
54 static int add_option(FILE *f, np_arg_list **optlst);
55 /* internal function to find default file */
56 static char* default_file(void);
57 /* internal function to stat() files */
58 static int test_file(const char* env, int len, const char* file, char* temp_file);
60 /* parse_locator decomposes a string of the form
61 * [stanza][@filename]
62 * into its seperate parts
63 */
64 static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
65 size_t locator_len=0, stanza_len=0;
67 /* if locator is NULL we'll use default values */
68 if(locator){
69 locator_len=strlen(locator);
70 stanza_len=strcspn(locator, "@");
71 }
72 /* if a non-default stanza is provided */
73 if(stanza_len>0){
74 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
75 strncpy(i->stanza, locator, stanza_len);
76 i->stanza[stanza_len]='\0';
77 } else { /* otherwise we use the default stanza */
78 i->stanza=strdup(def_stanza);
79 }
80 /* if there is no @file part */
81 if(stanza_len==locator_len){
82 i->file=default_file();
83 } else {
84 i->file=strdup(&(locator[stanza_len+1]));
85 }
87 if(i->file==NULL || i->stanza==NULL){
88 die(STATE_UNKNOWN, _("malloc() failed!\n"));
89 }
90 }
92 /* this is the externally visible function used by extra_opts */
93 np_arg_list* np_get_defaults(const char *locator, const char *default_section){
94 FILE *inifile=NULL;
95 np_arg_list *defaults=NULL;
96 np_ini_info i;
98 parse_locator(locator, default_section, &i);
99 /* if a file was specified or if we're using the default file */
100 if(i.file != NULL && strlen(i.file) > 0){
101 if(strcmp(i.file, "-")==0){
102 inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
103 } else {
104 inifile=fopen(i.file, "r");
105 }
106 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
107 if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
108 /* We got nothing, try the default section */
109 rewind(inifile);
110 read_defaults(inifile, default_section, &defaults);
111 }
113 free(i.file);
114 if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
115 }
116 free(i.stanza);
117 return defaults;
118 }
120 /* read_defaults is where the meat of the parsing takes place.
121 *
122 * note that this may be called by a setuid binary, so we need to
123 * be extra careful about user-supplied input (i.e. avoiding possible
124 * format string vulnerabilities, etc)
125 */
126 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
127 int c, status=FALSE;
128 size_t i, stanza_len;
129 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
131 stanza_len=strlen(stanza);
133 /* our little stanza-parsing state machine. */
134 while((c=fgetc(f))!=EOF){
135 /* gobble up leading whitespace */
136 if(isspace(c)) continue;
137 switch(c){
138 /* globble up coment lines */
139 case '#':
140 GOBBLE_TO(f, c, '\n');
141 break;
142 /* start of a stanza. check to see if it matches */
143 case '[':
144 stanzastate=WRONGSTANZA;
145 for(i=0; i<stanza_len; i++){
146 c=fgetc(f);
147 /* Strip leading whitespace */
148 if(i==0) for(c; isspace(c); c=fgetc(f));
149 /* nope, read to the end of the line */
150 if(c!=stanza[i]) {
151 GOBBLE_TO(f, c, '\n');
152 break;
153 }
154 }
155 /* if it matched up to here and the next char is ']'... */
156 if(i==stanza_len){
157 c=fgetc(f);
158 /* Strip trailing whitespace */
159 for(c; isspace(c); c=fgetc(f));
160 if(c==']') stanzastate=RIGHTSTANZA;
161 }
162 break;
163 /* otherwise, we're in the body of a stanza or a parse error */
164 default:
165 switch(stanzastate){
166 /* we never found the start of the first stanza, so
167 * we're dealing with a config error
168 */
169 case NOSTANZA:
170 die(STATE_UNKNOWN, _("Config file error"));
171 break;
172 /* we're in a stanza, but for a different plugin */
173 case WRONGSTANZA:
174 GOBBLE_TO(f, c, '\n');
175 break;
176 /* okay, this is where we start taking the config */
177 case RIGHTSTANZA:
178 ungetc(c, f);
179 if(add_option(f, opts)){
180 die(STATE_UNKNOWN, _("Config file error"));
181 }
182 status=TRUE;
183 break;
184 }
185 break;
186 }
187 }
188 return status;
189 }
191 /*
192 * read one line of input in the format
193 * ^option[[:space:]]*(=[[:space:]]*value)?
194 * and creates it as a cmdline argument
195 * --option[=value]
196 * appending it to the linked list optbuf.
197 */
198 static int add_option(FILE *f, np_arg_list **optlst){
199 np_arg_list *opttmp=*optlst, *optnew;
200 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
201 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
202 short done_reading=0, equals=0, value=0;
203 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
204 size_t opt_len=0, val_len=0;
206 /* read one line from the file */
207 while(!done_reading){
208 /* grow if necessary */
209 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
210 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
211 linebuf=realloc(linebuf, linebuf_sz);
212 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
213 }
214 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
215 else {
216 read_pos=strlen(linebuf);
217 if(linebuf[read_pos-1]=='\n') {
218 linebuf[--read_pos]='\0';
219 done_reading=1;
220 }
221 }
222 }
223 lineend=&linebuf[read_pos];
224 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
226 /* skip leading whitespace */
227 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
228 /* continue to '=' or EOL, watching for spaces that might precede it */
229 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
230 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
231 else optend=NULL;
232 }
233 if(optend==NULL) optend=eqptr;
234 --optend;
235 /* ^[[:space:]]*=foo is a syntax error */
236 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
237 /* continue from '=' to start of value or EOL */
238 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
239 /* continue to the end of value (FIXME: watching for trailing comments) */
240 for(valend=valptr; valend<lineend; valend++)
241 /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
242 if(*valend=='#') break;
243 --valend;
244 /* Finally trim off trailing spaces */
245 for(valend; isspace(*valend); valend--);
246 /* calculate the length of "--foo" */
247 opt_len=1+optend-optptr;
248 /* 1-character params needs only one dash */
249 if(opt_len==1)
250 cfg_len=1+(opt_len);
251 else
252 cfg_len=2+(opt_len);
253 /* if valptr<lineend then we have to also allocate space for "=bar" */
254 if(valptr<lineend) {
255 equals=value=1;
256 val_len=1+valend-valptr;
257 cfg_len+=1+val_len;
258 }
259 /* if valptr==valend then we have "=" but no "bar" */
260 else if(valptr==lineend) {
261 equals=1;
262 cfg_len+=1;
263 }
264 /* A line with no equal sign isn't valid */
265 if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
267 /* okay, now we have all the info we need, so we create a new np_arg_list
268 * element and set the argument...
269 */
270 optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
271 optnew->next=NULL;
273 read_pos=0;
274 optnew->arg=(char *)malloc(cfg_len+1);
275 /* 1-character params needs only one dash */
276 if(opt_len==1) {
277 strncpy(&optnew->arg[read_pos], "-", 1);
278 read_pos+=1;
279 } else {
280 strncpy(&optnew->arg[read_pos], "--", 2);
281 read_pos+=2;
282 }
283 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
284 if(value) {
285 optnew->arg[read_pos++]='=';
286 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
287 }
288 optnew->arg[read_pos]='\0';
290 /* ...and put that to the end of the list */
291 if(*optlst==NULL) {
292 *optlst=optnew;
293 } else {
294 while(opttmp->next!=NULL) {
295 opttmp=opttmp->next;
296 }
297 opttmp->next = optnew;
298 }
300 free(linebuf);
301 return 0;
302 }
304 static char* default_file(void){
305 struct stat sb;
306 char *np_env=NULL, *default_file=NULL;
307 char temp_file[MAX_INPUT_BUFFER];
308 size_t len;
310 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
311 /* skip ant starting colon... */
312 while(*np_env==':') np_env++;
313 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
314 * every PATHs defined (colon-separated).
315 */
316 while((len=strcspn(np_env,":"))>0){
317 /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
318 if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
319 test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
320 default_file=strdup(temp_file);
321 break;
322 }
324 /* Move on to the next token */
325 np_env+=len;
326 while(*np_env==':') np_env++;
327 } /* while(...) */
328 } /* if(getenv("NAGIOS_CONFIG_PATH")) */
330 /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
331 if(!default_file){
332 if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
333 test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
334 test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
335 test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
336 default_file=strdup(temp_file);
337 }
339 /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
340 if(!default_file){
341 if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
342 test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
343 test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
344 default_file=strdup(temp_file);
345 }
347 /* Return default_file or empty string (should return NULL if we want to
348 * die there...
349 */
350 if(default_file)
351 return default_file;
352 return "";
353 }
355 /* put together len bytes from env and the filename and test for its
356 * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
357 */
358 static int test_file(const char* env, int len, const char* file, char* temp_file){
359 struct stat sb;
361 /* test for len + filelen + '/' + '\0' */
362 if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
364 strncpy(temp_file,env,len);
365 temp_file[len]='\0';
366 strncat(temp_file,"/",len+1);
367 strncat(temp_file,file,len+strlen(file)+1);
369 if(stat(temp_file, &sb) != -1) return 1;
370 return 0;
371 }