parse_ini.c 8.0 KB

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