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 char* 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, char **optbuf, size_t *bufsize);
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 char* np_get_defaults(const char *locator, const char *default_section){
80 FILE *inifile=NULL;
81 char *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;
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);
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 char* read_defaults(FILE *f, const char *stanza){
108 int c;
109 char *opts=NULL;
110 size_t i, stanza_len, opts_buf_size=0;
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 /* nope, read to the end of the stanza header */
130 if(c!=stanza[i]) {
131 GOBBLE_TO(f, c, ']');
132 break;
133 }
134 }
135 /* if it matched up to here and the next char is ']'... */
136 if(i==stanza_len){
137 c=fgetc(f);
138 if(c==']') stanzastate=RIGHTSTANZA;
139 }
140 break;
141 /* otherwise, we're in the body of a stanza or a parse error */
142 default:
143 switch(stanzastate){
144 /* we never found the start of the first stanza, so
145 * we're dealing with a config error
146 */
147 case NOSTANZA:
148 die(STATE_UNKNOWN, _("Config file error"));
149 break;
150 /* we're in a stanza, but for a different plugin */
151 case WRONGSTANZA:
152 GOBBLE_TO(f, c, '\n');
153 break;
154 /* okay, this is where we start taking the config */
155 case RIGHTSTANZA:
156 ungetc(c, f);
157 if(add_option(f, &opts, &opts_buf_size)){
158 die(STATE_UNKNOWN, _("Config file error"));
159 }
160 break;
161 }
162 break;
163 }
164 }
165 return opts;
166 }
168 /*
169 * read one line of input in the format
170 * ^option[[:space:]]*(=[[:space:]]*value)?
171 * and creates it as a cmdline argument
172 * --option[=value]
173 * appending it to the string pointed to by optbuf (which will
174 * be dynamically grown if needed)
175 */
176 static int add_option(FILE *f, char **optbuf, size_t *bufsize){
177 char *newbuf=*optbuf;
178 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
179 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
180 short done_reading=0, equals=0, value=0;
181 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0, bs=*bufsize;
182 size_t opt_len=0, val_len=0;
184 /* read one line from the file */
185 while(!done_reading){
186 /* grow if necessary */
187 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
188 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
189 linebuf=realloc(linebuf, linebuf_sz);
190 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
191 }
192 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
193 else {
194 read_pos=strlen(linebuf);
195 if(linebuf[read_pos-1]=='\n') {
196 linebuf[--read_pos]='\0';
197 done_reading=1;
198 }
199 }
200 }
201 lineend=&linebuf[read_pos];
202 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
204 /* skip leading whitespace */
205 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
206 /* continue to '=' or EOL, watching for spaces that might precede it */
207 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
208 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
209 else optend=NULL;
210 }
211 if(optend==NULL) optend=eqptr;
212 --optend;
213 /* ^[[:space:]]*=foo is a syntax error */
214 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
215 /* continue from '=' to start of value or EOL */
216 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
217 /* continue to the end of value, watching for trailing space/comments */
218 for(valend=valptr; valend<lineend; valend++){
219 if(isspace(*valend) && spaceptr==NULL) spaceptr=valend;
220 else if(*valend=='#') break;
221 else spaceptr=NULL;
222 }
223 if(spaceptr!=NULL) valend=spaceptr;
224 --valend;
225 /* calculate the length of "--foo" */
226 opt_len=1+optend-optptr;
227 cfg_len=2+(opt_len);
228 /* if valptr<lineend then we have to also allocate space for "=bar" */
229 if(valptr<lineend) {
230 equals=value=1;
231 val_len=1+valend-valptr;
232 cfg_len+=1+val_len;
233 }
234 /* if valptr==valend then we have "=" but no "bar" */
235 else if (valptr==lineend) {
236 equals=1;
237 cfg_len+=1;
238 }
240 /* okay, now we have all the info we need, so we grow the default opts
241 * buffer if it's necessary, and put everything together.
242 * (+2 is for a potential space and a null byte)
243 */
244 read_pos=(newbuf==NULL)?0:strlen(newbuf);
245 if(newbuf==NULL || read_pos+cfg_len+2 >= bs){
246 bs=(bs>0)?(bs+cfg_len+2)<<1:cfg_len+1;
247 newbuf=realloc(newbuf, bs);
248 if(newbuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
249 }
250 if(read_pos>0) newbuf[read_pos++]=' ';
251 strncpy(&newbuf[read_pos], "--", 2); read_pos+=2;
252 strncpy(&newbuf[read_pos], optptr, opt_len); read_pos+=opt_len;
253 if(equals) newbuf[read_pos++]='=';
254 if(value) {
255 strncpy(&newbuf[read_pos], valptr, val_len); read_pos+=val_len;
256 }
257 newbuf[read_pos]='\0';
259 *optbuf=newbuf;
260 *bufsize=bs;
262 free(linebuf);
263 return 0;
264 }