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 /* FIXME: N::P dies if section is not found */
34 /* FIXME: N::P dies if config file is not found */
36 /* np_ini_info contains the result of parsing a "locator" in the format
37 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
38 */
39 typedef struct {
40 char *file;
41 char *stanza;
42 } np_ini_info;
44 /* eat all characters from a FILE pointer until n is encountered */
45 #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
47 /* internal function that returns the constructed defaults options */
48 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
49 /* internal function that converts a single line into options format */
50 static int add_option(FILE *f, np_arg_list **optlst);
51 /* internal function to find default file */
52 static char* default_file(void);
54 /* parse_locator decomposes a string of the form
55 * [stanza][@filename]
56 * into its seperate parts
57 */
58 static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
59 size_t locator_len=0, stanza_len=0;
61 /* if locator is NULL we'll use default values */
62 if(locator){
63 locator_len=strlen(locator);
64 stanza_len=strcspn(locator, "@");
65 }
66 /* if a non-default stanza is provided */
67 if(stanza_len>0){
68 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
69 strncpy(i->stanza, locator, stanza_len);
70 i->stanza[stanza_len]='\0';
71 } else { /* otherwise we use the default stanza */
72 i->stanza=strdup(def_stanza);
73 }
74 /* if there is no @file part */
75 if(stanza_len==locator_len){
76 i->file=default_file();
77 } else {
78 i->file=strdup(&(locator[stanza_len+1]));
79 }
81 if(i->file==NULL || i->stanza==NULL){
82 die(STATE_UNKNOWN, _("malloc() failed!\n"));
83 }
84 }
86 /* this is the externally visible function used by extra_opts */
87 np_arg_list* np_get_defaults(const char *locator, const char *default_section){
88 FILE *inifile=NULL;
89 np_arg_list *defaults=NULL;
90 np_ini_info i;
92 parse_locator(locator, default_section, &i);
93 /* if a file was specified or if we're using the default file */
94 if(i.file != NULL && strlen(i.file) > 0){
95 if(strcmp(i.file, "-")==0){
96 inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
97 } else {
98 inifile=fopen(i.file, "r");
99 }
100 if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
101 if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
102 /* We got nothing, try the default section */
103 rewind(inifile);
104 read_defaults(inifile, default_section, &defaults);
105 }
107 free(i.file);
108 if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
109 }
110 free(i.stanza);
111 return defaults;
112 }
114 /* read_defaults is where the meat of the parsing takes place.
115 *
116 * note that this may be called by a setuid binary, so we need to
117 * be extra careful about user-supplied input (i.e. avoiding possible
118 * format string vulnerabilities, etc)
119 */
120 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
121 int c, status=FALSE;
122 size_t i, stanza_len;
123 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
125 stanza_len=strlen(stanza);
127 /* our little stanza-parsing state machine. */
128 while((c=fgetc(f))!=EOF){
129 /* gobble up leading whitespace */
130 if(isspace(c)) continue;
131 switch(c){
132 /* globble up coment lines */
133 case '#':
134 GOBBLE_TO(f, c, '\n');
135 break;
136 /* start of a stanza. check to see if it matches */
137 case '[':
138 stanzastate=WRONGSTANZA;
139 for(i=0; i<stanza_len; i++){
140 c=fgetc(f);
141 /* Strip leading whitespace */
142 if(i==0) for(c; isspace(c); c=fgetc(f));
143 /* nope, read to the end of the line */
144 if(c!=stanza[i]) {
145 GOBBLE_TO(f, c, '\n');
146 break;
147 }
148 }
149 /* if it matched up to here and the next char is ']'... */
150 if(i==stanza_len){
151 c=fgetc(f);
152 /* Strip trailing whitespace */
153 for(c; isspace(c); c=fgetc(f));
154 if(c==']') stanzastate=RIGHTSTANZA;
155 }
156 break;
157 /* otherwise, we're in the body of a stanza or a parse error */
158 default:
159 switch(stanzastate){
160 /* we never found the start of the first stanza, so
161 * we're dealing with a config error
162 */
163 case NOSTANZA:
164 die(STATE_UNKNOWN, _("Config file error"));
165 break;
166 /* we're in a stanza, but for a different plugin */
167 case WRONGSTANZA:
168 GOBBLE_TO(f, c, '\n');
169 break;
170 /* okay, this is where we start taking the config */
171 case RIGHTSTANZA:
172 ungetc(c, f);
173 if(add_option(f, opts)){
174 die(STATE_UNKNOWN, _("Config file error"));
175 }
176 status=TRUE;
177 break;
178 }
179 break;
180 }
181 }
182 return status;
183 }
185 /*
186 * read one line of input in the format
187 * ^option[[:space:]]*(=[[:space:]]*value)?
188 * and creates it as a cmdline argument
189 * --option[=value]
190 * appending it to the linked list optbuf.
191 */
192 static int add_option(FILE *f, np_arg_list **optlst){
193 np_arg_list *opttmp=*optlst, *optnew;
194 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
195 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
196 short done_reading=0, equals=0, value=0;
197 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
198 size_t opt_len=0, val_len=0;
200 /* read one line from the file */
201 while(!done_reading){
202 /* grow if necessary */
203 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
204 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
205 linebuf=realloc(linebuf, linebuf_sz);
206 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
207 }
208 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
209 else {
210 read_pos=strlen(linebuf);
211 if(linebuf[read_pos-1]=='\n') {
212 linebuf[--read_pos]='\0';
213 done_reading=1;
214 }
215 }
216 }
217 lineend=&linebuf[read_pos];
218 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
220 /* skip leading whitespace */
221 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
222 /* continue to '=' or EOL, watching for spaces that might precede it */
223 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
224 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
225 else optend=NULL;
226 }
227 if(optend==NULL) optend=eqptr;
228 --optend;
229 /* ^[[:space:]]*=foo is a syntax error */
230 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
231 /* continue from '=' to start of value or EOL */
232 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
233 /* continue to the end of value (FIXME: watching for trailing comments) */
234 for(valend=valptr; valend<lineend; valend++)
235 /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
236 if(*valend=='#') break;
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;
252 }
253 /* if valptr==valend then we have "=" but no "bar" */
254 else if(valptr==lineend) {
255 equals=1;
256 cfg_len+=1;
257 }
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...
263 */
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;
276 }
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;
281 }
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;
290 }
291 opttmp->next = optnew;
292 }
294 free(linebuf);
295 return 0;
296 }
298 static char* default_file(void){
299 char *np_env=NULL;
301 /* FIXME: STUB */
302 return "";
303 #if 0
304 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
305 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
306 * every PATHs defined (colon-separated).
307 */
308 }
309 if !file_found
310 search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1;
311 if !file_found
312 search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2;
313 if !file_found
314 return empty string (or null if we want to die);
315 return file name;
316 #endif
317 }