38bcb39bfefe83fac7a64484c69f2866371b20a7
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 np_arg_list* read_defaults(FILE *f, const char *stanza);
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 defaults=read_defaults(inifile, i.stanza);
94 free(i.file);
95 if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
96 }
97 free(i.stanza);
98 return defaults;
99 }
101 /* read_defaults is where the meat of the parsing takes place.
102 *
103 * note that this may be called by a setuid binary, so we need to
104 * be extra careful about user-supplied input (i.e. avoiding possible
105 * format string vulnerabilities, etc)
106 */
107 static np_arg_list* read_defaults(FILE *f, const char *stanza){
108 int c;
109 np_arg_list *opts=NULL;
110 size_t i, stanza_len;
111 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
113 stanza_len=strlen(stanza);
115 /* our little stanza-parsing state machine. */
116 while((c=fgetc(f))!=EOF){
117 /* gobble up leading whitespace */
118 if(isspace(c)) continue;
119 switch(c){
120 /* globble up coment lines */
121 case '#':
122 GOBBLE_TO(f, c, '\n');
123 break;
124 /* start of a stanza. check to see if it matches */
125 case '[':
126 stanzastate=WRONGSTANZA;
127 for(i=0; i<stanza_len; i++){
128 c=fgetc(f);
129 /* Strip leading whitespace */
130 if(i==0) for(c; isspace(c); c=fgetc(f));
131 /* nope, read to the end of the line */
132 if(c!=stanza[i]) {
133 GOBBLE_TO(f, c, '\n');
134 break;
135 }
136 }
137 /* if it matched up to here and the next char is ']'... */
138 if(i==stanza_len){
139 c=fgetc(f);
140 /* Strip trailing whitespace */
141 for(c; isspace(c); c=fgetc(f));
142 if(c==']') stanzastate=RIGHTSTANZA;
143 }
144 break;
145 /* otherwise, we're in the body of a stanza or a parse error */
146 default:
147 switch(stanzastate){
148 /* we never found the start of the first stanza, so
149 * we're dealing with a config error
150 */
151 case NOSTANZA:
152 die(STATE_UNKNOWN, _("Config file error"));
153 break;
154 /* we're in a stanza, but for a different plugin */
155 case WRONGSTANZA:
156 GOBBLE_TO(f, c, '\n');
157 break;
158 /* okay, this is where we start taking the config */
159 case RIGHTSTANZA:
160 ungetc(c, f);
161 if(add_option(f, &opts)){
162 die(STATE_UNKNOWN, _("Config file error"));
163 }
164 break;
165 }
166 break;
167 }
168 }
169 return opts;
170 }
172 /*
173 * read one line of input in the format
174 * ^option[[:space:]]*(=[[:space:]]*value)?
175 * and creates it as a cmdline argument
176 * --option[=value]
177 * appending it to the linked list optbuf.
178 */
179 static int add_option(FILE *f, np_arg_list **optlst){
180 np_arg_list *opttmp=*optlst, *optnew;
181 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
182 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
183 short done_reading=0, equals=0, value=0;
184 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
185 size_t opt_len=0, val_len=0;
187 /* read one line from the file */
188 while(!done_reading){
189 /* grow if necessary */
190 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
191 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
192 linebuf=realloc(linebuf, linebuf_sz);
193 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
194 }
195 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
196 else {
197 read_pos=strlen(linebuf);
198 if(linebuf[read_pos-1]=='\n') {
199 linebuf[--read_pos]='\0';
200 done_reading=1;
201 }
202 }
203 }
204 lineend=&linebuf[read_pos];
205 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
207 /* skip leading whitespace */
208 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
209 /* continue to '=' or EOL, watching for spaces that might precede it */
210 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
211 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
212 else optend=NULL;
213 }
214 if(optend==NULL) optend=eqptr;
215 --optend;
216 /* ^[[:space:]]*=foo is a syntax error */
217 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
218 /* continue from '=' to start of value or EOL */
219 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
220 /* continue to the end of value (FIXME: watching for trailing comments) */
221 for(valend=valptr; valend<lineend; valend++)
222 /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
223 if(*valend=='#') break;
224 --valend;
225 /* Finally trim off trailing spaces */
226 for(valend; isspace(*valend); valend--);
227 /* calculate the length of "--foo" */
228 opt_len=1+optend-optptr;
229 /* 1-character params needs only one dash */
230 if(opt_len==1)
231 cfg_len=1+(opt_len);
232 else
233 cfg_len=2+(opt_len);
234 /* if valptr<lineend then we have to also allocate space for "=bar" */
235 if(valptr<lineend) {
236 equals=value=1;
237 val_len=1+valend-valptr;
238 cfg_len+=1+val_len;
239 }
240 /* if valptr==valend then we have "=" but no "bar" */
241 else if(valptr==lineend) {
242 equals=1;
243 cfg_len+=1;
244 }
246 /* okay, now we have all the info we need, so we create a new np_arg_list
247 * element and set the argument...
248 */
249 optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
250 optnew->next=NULL;
252 read_pos=0;
253 optnew->arg=(char *)malloc(cfg_len+1);
254 /* 1-character params needs only one dash */
255 if(opt_len==1) {
256 strncpy(&optnew->arg[read_pos], "-", 1);
257 read_pos+=1;
258 } else {
259 strncpy(&optnew->arg[read_pos], "--", 2);
260 read_pos+=2;
261 }
262 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
263 if(equals) optnew->arg[read_pos++]='=';
264 if(value) {
265 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
266 }
267 optnew->arg[read_pos]='\0';
269 /* ...and put that to the end of the list */
270 if(*optlst==NULL) {
271 *optlst=optnew;
272 } else {
273 while(opttmp->next!=NULL) {
274 opttmp=opttmp->next;
275 }
276 opttmp->next = optnew;
277 }
279 free(linebuf);
280 return 0;
281 }