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 /* np_ini_info contains the result of parsing a "locator" in the format
34 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
35 */
36 typedef struct {
37 char *file;
38 char *stanza;
39 } np_ini_info;
41 /* eat all characters from a FILE pointer until n is encountered */
42 #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
44 /* internal function that returns the constructed defaults options */
45 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
46 /* internal function that converts a single line into options format */
47 static int add_option(FILE *f, np_arg_list **optlst);
49 /* parse_locator decomposes a string of the form
50 * [stanza][@filename]
51 * into its seperate parts
52 */
53 static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
54 size_t locator_len, stanza_len;
56 locator_len=strlen(locator);
57 stanza_len=strcspn(locator, "@");
58 /* if a non-default stanza is provided */
59 if(stanza_len>0){
60 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
61 strncpy(i->stanza, locator, stanza_len);
62 i->stanza[stanza_len]='\0';
63 } else { /* otherwise we use the default stanza */
64 i->stanza=strdup(def_stanza);
65 }
66 /* if there is no @file part */
67 if(stanza_len==locator_len){
68 i->file=strdup(NP_DEFAULT_INI_PATH);
69 } else {
70 i->file=strdup(&(locator[stanza_len+1]));
71 }
73 if(i->file==NULL || i->stanza==NULL){
74 die(STATE_UNKNOWN, _("malloc() failed!\n"));
75 }
76 }
78 /* this is the externally visible function used by plugins */
79 np_arg_list* np_get_defaults(const char *locator, const char *default_section){
80 FILE *inifile=NULL;
81 np_arg_list *defaults=NULL;
82 np_ini_info i;
84 parse_locator(locator, default_section, &i);
85 /* if a file was specified or if we're using the default file */
86 if(i.file != NULL && strlen(i.file) > 0){
87 if(strcmp(i.file, "-")==0){
88 inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
89 } else {
90 inifile=fopen(i.file, "r");
91 }
92 if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
93 if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
94 /* We got nothing, try the default section */
95 rewind(inifile);
96 read_defaults(inifile, default_section, &defaults);
97 }
99 free(i.file);
100 if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
101 }
102 free(i.stanza);
103 return defaults;
104 }
106 /* read_defaults is where the meat of the parsing takes place.
107 *
108 * note that this may be called by a setuid binary, so we need to
109 * be extra careful about user-supplied input (i.e. avoiding possible
110 * format string vulnerabilities, etc)
111 */
112 static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
113 int c, status=FALSE;
114 size_t i, stanza_len;
115 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
117 stanza_len=strlen(stanza);
119 /* our little stanza-parsing state machine. */
120 while((c=fgetc(f))!=EOF){
121 /* gobble up leading whitespace */
122 if(isspace(c)) continue;
123 switch(c){
124 /* globble up coment lines */
125 case '#':
126 GOBBLE_TO(f, c, '\n');
127 break;
128 /* start of a stanza. check to see if it matches */
129 case '[':
130 stanzastate=WRONGSTANZA;
131 for(i=0; i<stanza_len; i++){
132 c=fgetc(f);
133 /* Strip leading whitespace */
134 if(i==0) for(c; isspace(c); c=fgetc(f));
135 /* nope, read to the end of the line */
136 if(c!=stanza[i]) {
137 GOBBLE_TO(f, c, '\n');
138 break;
139 }
140 }
141 /* if it matched up to here and the next char is ']'... */
142 if(i==stanza_len){
143 c=fgetc(f);
144 /* Strip trailing whitespace */
145 for(c; isspace(c); c=fgetc(f));
146 if(c==']') stanzastate=RIGHTSTANZA;
147 }
148 break;
149 /* otherwise, we're in the body of a stanza or a parse error */
150 default:
151 switch(stanzastate){
152 /* we never found the start of the first stanza, so
153 * we're dealing with a config error
154 */
155 case NOSTANZA:
156 die(STATE_UNKNOWN, _("Config file error"));
157 break;
158 /* we're in a stanza, but for a different plugin */
159 case WRONGSTANZA:
160 GOBBLE_TO(f, c, '\n');
161 break;
162 /* okay, this is where we start taking the config */
163 case RIGHTSTANZA:
164 ungetc(c, f);
165 if(add_option(f, opts)){
166 die(STATE_UNKNOWN, _("Config file error"));
167 }
168 status=TRUE;
169 break;
170 }
171 break;
172 }
173 }
174 return status;
175 }
177 /*
178 * read one line of input in the format
179 * ^option[[:space:]]*(=[[:space:]]*value)?
180 * and creates it as a cmdline argument
181 * --option[=value]
182 * appending it to the linked list optbuf.
183 */
184 static int add_option(FILE *f, np_arg_list **optlst){
185 np_arg_list *opttmp=*optlst, *optnew;
186 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
187 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
188 short done_reading=0, equals=0, value=0;
189 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
190 size_t opt_len=0, val_len=0;
192 /* read one line from the file */
193 while(!done_reading){
194 /* grow if necessary */
195 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
196 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
197 linebuf=realloc(linebuf, linebuf_sz);
198 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
199 }
200 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
201 else {
202 read_pos=strlen(linebuf);
203 if(linebuf[read_pos-1]=='\n') {
204 linebuf[--read_pos]='\0';
205 done_reading=1;
206 }
207 }
208 }
209 lineend=&linebuf[read_pos];
210 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
212 /* skip leading whitespace */
213 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
214 /* continue to '=' or EOL, watching for spaces that might precede it */
215 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
216 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
217 else optend=NULL;
218 }
219 if(optend==NULL) optend=eqptr;
220 --optend;
221 /* ^[[:space:]]*=foo is a syntax error */
222 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
223 /* continue from '=' to start of value or EOL */
224 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
225 /* continue to the end of value (FIXME: watching for trailing comments) */
226 for(valend=valptr; valend<lineend; valend++)
227 /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
228 if(*valend=='#') break;
229 --valend;
230 /* Finally trim off trailing spaces */
231 for(valend; isspace(*valend); valend--);
232 /* calculate the length of "--foo" */
233 opt_len=1+optend-optptr;
234 /* 1-character params needs only one dash */
235 if(opt_len==1)
236 cfg_len=1+(opt_len);
237 else
238 cfg_len=2+(opt_len);
239 /* if valptr<lineend then we have to also allocate space for "=bar" */
240 if(valptr<lineend) {
241 equals=value=1;
242 val_len=1+valend-valptr;
243 cfg_len+=1+val_len;
244 }
245 /* if valptr==valend then we have "=" but no "bar" */
246 else if(valptr==lineend) {
247 equals=1;
248 cfg_len+=1;
249 }
250 /* A line with no equal sign isn't valid */
251 if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
253 /* okay, now we have all the info we need, so we create a new np_arg_list
254 * element and set the argument...
255 */
256 optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
257 optnew->next=NULL;
259 read_pos=0;
260 optnew->arg=(char *)malloc(cfg_len+1);
261 /* 1-character params needs only one dash */
262 if(opt_len==1) {
263 strncpy(&optnew->arg[read_pos], "-", 1);
264 read_pos+=1;
265 } else {
266 strncpy(&optnew->arg[read_pos], "--", 2);
267 read_pos+=2;
268 }
269 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
270 if(value) {
271 optnew->arg[read_pos++]='=';
272 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
273 }
274 optnew->arg[read_pos]='\0';
276 /* ...and put that to the end of the list */
277 if(*optlst==NULL) {
278 *optlst=optnew;
279 } else {
280 while(opttmp->next!=NULL) {
281 opttmp=opttmp->next;
282 }
283 opttmp->next = optnew;
284 }
286 free(linebuf);
287 return 0;
288 }