parse_ini.c 8.0 KB

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