parse_ini.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
  42. /* internal function that converts a single line into options format */
  43. static int add_option(FILE *f, np_arg_list **optlst);
  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. np_arg_list* np_get_defaults(const char *locator, const char *default_section){
  72. FILE *inifile=NULL;
  73. np_arg_list *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; /* FIXME: Shouldn't it be 'stdin' ??? */
  80. } else {
  81. inifile=fopen(i.file, "r");
  82. }
  83. if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
  84. if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
  85. /* We got nothing, try the default section */
  86. rewind(inifile);
  87. read_defaults(inifile, default_section, &defaults);
  88. }
  89. free(i.file);
  90. if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
  91. }
  92. free(i.stanza);
  93. return defaults;
  94. }
  95. /* read_defaults is where the meat of the parsing takes place.
  96. *
  97. * note that this may be called by a setuid binary, so we need to
  98. * be extra careful about user-supplied input (i.e. avoiding possible
  99. * format string vulnerabilities, etc)
  100. */
  101. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
  102. int c, status=FALSE;
  103. size_t i, stanza_len;
  104. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
  105. stanza_len=strlen(stanza);
  106. /* our little stanza-parsing state machine. */
  107. while((c=fgetc(f))!=EOF){
  108. /* gobble up leading whitespace */
  109. if(isspace(c)) continue;
  110. switch(c){
  111. /* globble up coment lines */
  112. case '#':
  113. GOBBLE_TO(f, c, '\n');
  114. break;
  115. /* start of a stanza. check to see if it matches */
  116. case '[':
  117. stanzastate=WRONGSTANZA;
  118. for(i=0; i<stanza_len; i++){
  119. c=fgetc(f);
  120. /* Strip leading whitespace */
  121. if(i==0) for(c; isspace(c); c=fgetc(f));
  122. /* nope, read to the end of the line */
  123. if(c!=stanza[i]) {
  124. GOBBLE_TO(f, c, '\n');
  125. break;
  126. }
  127. }
  128. /* if it matched up to here and the next char is ']'... */
  129. if(i==stanza_len){
  130. c=fgetc(f);
  131. /* Strip trailing whitespace */
  132. for(c; isspace(c); c=fgetc(f));
  133. if(c==']') stanzastate=RIGHTSTANZA;
  134. }
  135. break;
  136. /* otherwise, we're in the body of a stanza or a parse error */
  137. default:
  138. switch(stanzastate){
  139. /* we never found the start of the first stanza, so
  140. * we're dealing with a config error
  141. */
  142. case NOSTANZA:
  143. die(STATE_UNKNOWN, _("Config file error"));
  144. break;
  145. /* we're in a stanza, but for a different plugin */
  146. case WRONGSTANZA:
  147. GOBBLE_TO(f, c, '\n');
  148. break;
  149. /* okay, this is where we start taking the config */
  150. case RIGHTSTANZA:
  151. ungetc(c, f);
  152. if(add_option(f, opts)){
  153. die(STATE_UNKNOWN, _("Config file error"));
  154. }
  155. status=TRUE;
  156. break;
  157. }
  158. break;
  159. }
  160. }
  161. return status;
  162. }
  163. /*
  164. * read one line of input in the format
  165. * ^option[[:space:]]*(=[[:space:]]*value)?
  166. * and creates it as a cmdline argument
  167. * --option[=value]
  168. * appending it to the linked list optbuf.
  169. */
  170. static int add_option(FILE *f, np_arg_list **optlst){
  171. np_arg_list *opttmp=*optlst, *optnew;
  172. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  173. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  174. short done_reading=0, equals=0, value=0;
  175. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  176. size_t opt_len=0, val_len=0;
  177. /* read one line from the file */
  178. while(!done_reading){
  179. /* grow if necessary */
  180. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  181. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  182. linebuf=realloc(linebuf, linebuf_sz);
  183. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  184. }
  185. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  186. else {
  187. read_pos=strlen(linebuf);
  188. if(linebuf[read_pos-1]=='\n') {
  189. linebuf[--read_pos]='\0';
  190. done_reading=1;
  191. }
  192. }
  193. }
  194. lineend=&linebuf[read_pos];
  195. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  196. /* skip leading whitespace */
  197. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  198. /* continue to '=' or EOL, watching for spaces that might precede it */
  199. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  200. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  201. else optend=NULL;
  202. }
  203. if(optend==NULL) optend=eqptr;
  204. --optend;
  205. /* ^[[:space:]]*=foo is a syntax error */
  206. if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
  207. /* continue from '=' to start of value or EOL */
  208. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  209. /* continue to the end of value (FIXME: watching for trailing comments) */
  210. for(valend=valptr; valend<lineend; valend++)
  211. /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
  212. if(*valend=='#') break;
  213. --valend;
  214. /* Finally trim off trailing spaces */
  215. for(valend; isspace(*valend); valend--);
  216. /* calculate the length of "--foo" */
  217. opt_len=1+optend-optptr;
  218. /* 1-character params needs only one dash */
  219. if(opt_len==1)
  220. cfg_len=1+(opt_len);
  221. else
  222. cfg_len=2+(opt_len);
  223. /* if valptr<lineend then we have to also allocate space for "=bar" */
  224. if(valptr<lineend) {
  225. equals=value=1;
  226. val_len=1+valend-valptr;
  227. cfg_len+=1+val_len;
  228. }
  229. /* if valptr==valend then we have "=" but no "bar" */
  230. else if(valptr==lineend) {
  231. equals=1;
  232. cfg_len+=1;
  233. }
  234. /* A line with no equal sign isn't valid */
  235. if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
  236. /* okay, now we have all the info we need, so we create a new np_arg_list
  237. * element and set the argument...
  238. */
  239. optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
  240. optnew->next=NULL;
  241. read_pos=0;
  242. optnew->arg=(char *)malloc(cfg_len+1);
  243. /* 1-character params needs only one dash */
  244. if(opt_len==1) {
  245. strncpy(&optnew->arg[read_pos], "-", 1);
  246. read_pos+=1;
  247. } else {
  248. strncpy(&optnew->arg[read_pos], "--", 2);
  249. read_pos+=2;
  250. }
  251. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  252. if(value) {
  253. optnew->arg[read_pos++]='=';
  254. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  255. }
  256. optnew->arg[read_pos]='\0';
  257. /* ...and put that to the end of the list */
  258. if(*optlst==NULL) {
  259. *optlst=optnew;
  260. } else {
  261. while(opttmp->next!=NULL) {
  262. opttmp=opttmp->next;
  263. }
  264. opttmp->next = optnew;
  265. }
  266. free(linebuf);
  267. return 0;
  268. }