summaryrefslogtreecommitdiffstats
path: root/lib/parse_ini.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r--lib/parse_ini.c247
1 files changed, 247 insertions, 0 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
new file mode 100644
index 00000000..5155b6df
--- /dev/null
+++ b/lib/parse_ini.c
@@ -0,0 +1,247 @@
1#include "common.h"
2#include "parse_ini.h"
3#include "utils_base.h"
4#include <ctype.h>
5
6/* np_ini_info contains the result of parsing a "locator" in the format
7 * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
8 */
9typedef struct {
10 char *file;
11 char *stanza;
12} np_ini_info;
13
14/* eat all characters from a FILE pointer until n is encountered */
15#define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
16
17/* internal function that returns the constructed defaults options */
18static char* read_defaults(FILE *f, const char *stanza);
19/* internal function that converts a single line into options format */
20static int add_option(FILE *f, char **optbuf, size_t *bufsize);
21
22/* parse_locator decomposes a string of the form
23 * [stanza][@filename]
24 * into its seperate parts
25 */
26static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
27 size_t locator_len, stanza_len;
28
29 locator_len=strlen(locator);
30 stanza_len=strcspn(locator, "@");
31 /* if a non-default stanza is provided */
32 if(stanza_len>0){
33 i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
34 strncpy(i->stanza, locator, stanza_len);
35 i->stanza[stanza_len]='\0';
36 } else { /* otherwise we use the default stanza */
37 i->stanza=strdup(def_stanza);
38 }
39 /* if there is no @file part */
40 if(stanza_len==locator_len){
41 i->file=strdup(NP_DEFAULT_INI_PATH);
42 } else {
43 i->file=strdup(&(locator[stanza_len+1]));
44 }
45
46 if(i->file==NULL || i->stanza==NULL){
47 die(STATE_UNKNOWN, _("malloc() failed!\n"));
48 }
49}
50
51/* this is the externally visible function used by plugins */
52char* np_get_defaults(const char *locator, const char *default_section){
53 FILE *inifile=NULL;
54 char *defaults=NULL;
55 np_ini_info i;
56
57 parse_locator(locator, default_section, &i);
58 /* if a file was specified or if we're using the default file */
59 if(i.file != NULL && strlen(i.file) > 0){
60 if(strcmp(i.file, "-")==0){
61 inifile=stdout;
62 } else {
63 inifile=fopen(i.file, "r");
64 }
65 if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
66 defaults=read_defaults(inifile, i.stanza);
67 free(i.file);
68 if(inifile!=stdout) fclose(inifile);
69 }
70 free(i.stanza);
71 return defaults;
72}
73
74/* read_defaults is where the meat of the parsing takes place.
75 *
76 * note that this may be called by a setuid binary, so we need to
77 * be extra careful about user-supplied input (i.e. avoiding possible
78 * format string vulnerabilities, etc)
79 */
80static char* read_defaults(FILE *f, const char *stanza){
81 int c;
82 char *opts=NULL;
83 size_t i, stanza_len, opts_buf_size=0;
84 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
85
86 stanza_len=strlen(stanza);
87
88 /* our little stanza-parsing state machine. */
89 while((c=fgetc(f))!=EOF){
90 /* gobble up leading whitespace */
91 if(isspace(c)) continue;
92 switch(c){
93 /* globble up coment lines */
94 case '#':
95 GOBBLE_TO(f, c, '\n');
96 break;
97 /* start of a stanza. check to see if it matches */
98 case '[':
99 stanzastate=WRONGSTANZA;
100 for(i=0; i<stanza_len; i++){
101 c=fgetc(f);
102 /* nope, read to the end of the stanza header */
103 if(c!=stanza[i]) {
104 GOBBLE_TO(f, c, ']');
105 break;
106 }
107 }
108 /* if it matched up to here and the next char is ']'... */
109 if(i==stanza_len){
110 c=fgetc(f);
111 if(c==']') stanzastate=RIGHTSTANZA;
112 }
113 break;
114 /* otherwise, we're in the body of a stanza or a parse error */
115 default:
116 switch(stanzastate){
117 /* we never found the start of the first stanza, so
118 * we're dealing with a config error
119 */
120 case NOSTANZA:
121 die(STATE_UNKNOWN, _("Config file error"));
122 break;
123 /* we're in a stanza, but for a different plugin */
124 case WRONGSTANZA:
125 GOBBLE_TO(f, c, '\n');
126 break;
127 /* okay, this is where we start taking the config */
128 case RIGHTSTANZA:
129 ungetc(c, f);
130 if(add_option(f, &opts, &opts_buf_size)){
131 die(STATE_UNKNOWN, _("Config file error"));
132 }
133 break;
134 }
135 break;
136 }
137 }
138 return opts;
139}
140
141/*
142 * read one line of input in the format
143 * ^option[[:space:]]*(=[[:space:]]*value)?
144 * and creates it as a cmdline argument
145 * --option[=value]
146 * appending it to the string pointed to by optbuf (which will
147 * be dynamically grown if needed)
148 */
149static int add_option(FILE *f, char **optbuf, size_t *bufsize){
150 char *newbuf=*optbuf;
151 char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
152 char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
153 short done_reading=0, equals=0, value=0;
154 size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0, bs=*bufsize;
155 size_t opt_len=0, val_len=0;
156
157 /* read one line from the file */
158 while(!done_reading){
159 /* grow if necessary */
160 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
161 linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
162 linebuf=realloc(linebuf, linebuf_sz);
163 if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
164 }
165 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
166 else {
167 read_pos=strlen(linebuf);
168 if(linebuf[read_pos-1]=='\n') {
169 linebuf[--read_pos]='\0';
170 done_reading=1;
171 }
172 }
173 }
174 lineend=&linebuf[read_pos];
175 /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
176
177 /* skip leading whitespace */
178 for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
179 /* continue to '=' or EOL, watching for spaces that might precede it */
180 for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
181 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
182 else optend=NULL;
183 }
184 if(optend==NULL) optend=eqptr;
185 --optend;
186 /* ^[[:space:]]*=foo is a syntax error */
187 if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
188 /* continue from '=' to start of value or EOL */
189 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
190 /* continue to the end of value, watching for trailing space/comments */
191 for(valend=valptr; valend<lineend; valend++){
192 if(isspace(*valend) && spaceptr==NULL) spaceptr=valend;
193 else if(*valend=='#') break;
194 else spaceptr=NULL;
195 }
196 if(spaceptr!=NULL) valend=spaceptr;
197 --valend;
198 /* calculate the length of "--foo" */
199 opt_len=1+optend-optptr;
200 cfg_len=2+(opt_len);
201 /* if valptr<lineend then we have to also allocate space for "=bar" */
202 if(valptr<lineend) {
203 equals=value=1;
204 val_len=1+valend-valptr;
205 cfg_len+=1+val_len;
206 }
207 /* if valptr==valend then we have "=" but no "bar" */
208 else if (valptr==lineend) {
209 equals=1;
210 cfg_len+=1;
211 }
212
213 /* okay, now we have all the info we need, so we grow the default opts
214 * buffer if it's necessary, and put everything together.
215 * (+2 is for a potential space and a null byte)
216 */
217 read_pos=(newbuf==NULL)?0:strlen(newbuf);
218 if(newbuf==NULL || read_pos+cfg_len+2 >= bs){
219 bs=(bs>0)?(bs+cfg_len+2)<<1:cfg_len+1;
220 newbuf=realloc(newbuf, bs);
221 if(newbuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
222 }
223 if(read_pos>0) newbuf[read_pos++]=' ';
224 strncpy(&newbuf[read_pos], "--", 2); read_pos+=2;
225 strncpy(&newbuf[read_pos], optptr, opt_len); read_pos+=opt_len;
226 if(equals) newbuf[read_pos++]='=';
227 if(value) {
228 strncpy(&newbuf[read_pos], valptr, val_len); read_pos+=val_len;
229 }
230 newbuf[read_pos]='\0';
231
232 *optbuf=newbuf;
233 *bufsize=bs;
234
235 free(linebuf);
236 return 0;
237}
238
239int main(){
240 char *optstr=NULL;
241 optstr=np_get_defaults("check_foo@./foo.ini", "check_bar");
242 if(optstr!=NULL) {
243 printf("optstr:\n\t%s\n", optstr);
244 free(optstr);
245 }
246 return 0;
247}