Code

c915d79504bba7c218689063909359f551903bad
[nagiosplug.git] / lib / parse_ini.c
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         }
72         
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;
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 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                                         /* 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)){
158                                                         die(STATE_UNKNOWN, _("Config file error"));
159                                                 }
160                                                 break;
161                                 }
162                                 break;
163                 }
164         }
165         return opts;
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 linked list optbuf.
174  */
175 static int add_option(FILE *f, np_arg_list **optlst){
176         np_arg_list *opttmp=*optlst, *optnew;
177         char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
178         char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
179         short done_reading=0, equals=0, value=0;
180         size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
181         size_t opt_len=0, val_len=0;
183         /* read one line from the file */
184         while(!done_reading){
185                 /* grow if necessary */
186                 if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
187                         linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
188                         linebuf=realloc(linebuf, linebuf_sz);
189                         if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
190                 }
191                 if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
192                 else {
193                         read_pos=strlen(linebuf);
194                         if(linebuf[read_pos-1]=='\n') {
195                                 linebuf[--read_pos]='\0';
196                                 done_reading=1;
197                         }
198                 }
199         }
200         lineend=&linebuf[read_pos];
201         /* all that to read one line.  isn't C fun? :) now comes the parsing :/ */
203         /* skip leading whitespace */
204         for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
205         /* continue to '=' or EOL, watching for spaces that might precede it */
206         for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
207                 if(isspace(*eqptr) && optend==NULL) optend=eqptr;
208                 else optend=NULL;
209         }
210         if(optend==NULL) optend=eqptr;
211         --optend;
212         /* ^[[:space:]]*=foo is a syntax error */
213         if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
214         /* continue from '=' to start of value or EOL */
215         for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
216         /* continue to the end of value (FIXME: watching for trailing comments) */
217         for(valend=valptr; valend<lineend; valend++)
218                 /* FIXME: N::P doesn't allow comments. Remove next line and parse_ini won't either */
219                 if(*valend=='#') break;
220         --valend;
221         /* Finally trim off trailing spaces */
222         for(valend; isspace(*valend); valend--);
223         /* calculate the length of "--foo" */
224         opt_len=1+optend-optptr;
225         cfg_len=2+(opt_len);
226         /* if valptr<lineend then we have to also allocate space for "=bar" */
227         if(valptr<lineend) {
228                 equals=value=1;
229                 val_len=1+valend-valptr;
230                 cfg_len+=1+val_len;
231         }
232         /* if valptr==valend then we have "=" but no "bar" */
233         else if (valptr==lineend) {
234                 equals=1;
235                 cfg_len+=1;
236         }
238         /* okay, now we have all the info we need, so we create a new np_arg_list
239          * element and set the argument...
240          */
241         optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
242         optnew->next=NULL;
244         read_pos=0;
245         optnew->arg=(char *)malloc(cfg_len+1);
246         strncpy(&optnew->arg[read_pos], "--", 2); read_pos+=2;
247         strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
248         if(equals) optnew->arg[read_pos++]='=';
249         if(value) {
250                 strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
251         }
252         optnew->arg[read_pos]='\0';
254         /* ...and put that to the end of the list */
255         if (*optlst==NULL) {
256                 *optlst=optnew;
257         }       else {
258                 while (opttmp->next!=NULL) {
259                         opttmp=opttmp->next;
260                 }
261                 opttmp->next = optnew;
262         }
264         free(linebuf);
265         return 0;