parse_ini.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /* FIXME: N::P dies if section is not found */
  32. /* FIXME: N::P dies if config file is not found */
  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;
  40. /* eat all characters from a FILE pointer until n is encountered */
  41. #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
  42. /* internal function that returns the constructed defaults options */
  43. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
  44. /* internal function that converts a single line into options format */
  45. static int add_option(FILE *f, np_arg_list **optlst);
  46. /* internal function to find default file */
  47. static char* default_file(void);
  48. /* parse_locator decomposes a string of the form
  49. * [stanza][@filename]
  50. * into its seperate parts
  51. */
  52. static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
  53. size_t locator_len=0, stanza_len=0;
  54. /* if locator is NULL we'll use default values */
  55. if(locator){
  56. locator_len=strlen(locator);
  57. stanza_len=strcspn(locator, "@");
  58. }
  59. /* if a non-default stanza is provided */
  60. if(stanza_len>0){
  61. i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
  62. strncpy(i->stanza, locator, stanza_len);
  63. i->stanza[stanza_len]='\0';
  64. } else { /* otherwise we use the default stanza */
  65. i->stanza=strdup(def_stanza);
  66. }
  67. /* if there is no @file part */
  68. if(stanza_len==locator_len){
  69. i->file=default_file();
  70. } else {
  71. i->file=strdup(&(locator[stanza_len+1]));
  72. }
  73. if(i->file==NULL || i->stanza==NULL){
  74. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  75. }
  76. }
  77. /* this is the externally visible function used by extra_opts */
  78. np_arg_list* np_get_defaults(const char *locator, const char *default_section){
  79. FILE *inifile=NULL;
  80. np_arg_list *defaults=NULL;
  81. np_ini_info i;
  82. parse_locator(locator, default_section, &i);
  83. /* if a file was specified or if we're using the default file */
  84. if(i.file != NULL && strlen(i.file) > 0){
  85. if(strcmp(i.file, "-")==0){
  86. inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
  87. } else {
  88. inifile=fopen(i.file, "r");
  89. }
  90. if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
  91. if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdout) { /* FIXME: Shouldn't it be 'stdin' ??? */
  92. /* We got nothing, try the default section */
  93. rewind(inifile);
  94. read_defaults(inifile, default_section, &defaults);
  95. }
  96. free(i.file);
  97. if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
  98. }
  99. free(i.stanza);
  100. return defaults;
  101. }
  102. /* read_defaults is where the meat of the parsing takes place.
  103. *
  104. * note that this may be called by a setuid binary, so we need to
  105. * be extra careful about user-supplied input (i.e. avoiding possible
  106. * format string vulnerabilities, etc)
  107. */
  108. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
  109. int c, status=FALSE;
  110. size_t i, stanza_len;
  111. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
  112. stanza_len=strlen(stanza);
  113. /* our little stanza-parsing state machine. */
  114. while((c=fgetc(f))!=EOF){
  115. /* gobble up leading whitespace */
  116. if(isspace(c)) continue;
  117. switch(c){
  118. /* globble up coment lines */
  119. case '#':
  120. GOBBLE_TO(f, c, '\n');
  121. break;
  122. /* start of a stanza. check to see if it matches */
  123. case '[':
  124. stanzastate=WRONGSTANZA;
  125. for(i=0; i<stanza_len; i++){
  126. c=fgetc(f);
  127. /* Strip leading whitespace */
  128. if(i==0) for(c; isspace(c); c=fgetc(f));
  129. /* nope, read to the end of the line */
  130. if(c!=stanza[i]) {
  131. GOBBLE_TO(f, c, '\n');
  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. /* Strip trailing whitespace */
  139. for(c; isspace(c); c=fgetc(f));
  140. if(c==']') stanzastate=RIGHTSTANZA;
  141. }
  142. break;
  143. /* otherwise, we're in the body of a stanza or a parse error */
  144. default:
  145. switch(stanzastate){
  146. /* we never found the start of the first stanza, so
  147. * we're dealing with a config error
  148. */
  149. case NOSTANZA:
  150. die(STATE_UNKNOWN, _("Config file error"));
  151. break;
  152. /* we're in a stanza, but for a different plugin */
  153. case WRONGSTANZA:
  154. GOBBLE_TO(f, c, '\n');
  155. break;
  156. /* okay, this is where we start taking the config */
  157. case RIGHTSTANZA:
  158. ungetc(c, f);
  159. if(add_option(f, opts)){
  160. die(STATE_UNKNOWN, _("Config file error"));
  161. }
  162. status=TRUE;
  163. break;
  164. }
  165. break;
  166. }
  167. }
  168. return status;
  169. }
  170. /*
  171. * read one line of input in the format
  172. * ^option[[:space:]]*(=[[:space:]]*value)?
  173. * and creates it as a cmdline argument
  174. * --option[=value]
  175. * appending it to the linked list optbuf.
  176. */
  177. static int add_option(FILE *f, np_arg_list **optlst){
  178. np_arg_list *opttmp=*optlst, *optnew;
  179. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  180. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  181. short done_reading=0, equals=0, value=0;
  182. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  183. size_t opt_len=0, val_len=0;
  184. /* read one line from the file */
  185. while(!done_reading){
  186. /* grow if necessary */
  187. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  188. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  189. linebuf=realloc(linebuf, linebuf_sz);
  190. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  191. }
  192. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  193. else {
  194. read_pos=strlen(linebuf);
  195. if(linebuf[read_pos-1]=='\n') {
  196. linebuf[--read_pos]='\0';
  197. done_reading=1;
  198. }
  199. }
  200. }
  201. lineend=&linebuf[read_pos];
  202. /* 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 here. 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. /* 1-character params needs only one dash */
  226. if(opt_len==1)
  227. cfg_len=1+(opt_len);
  228. else
  229. cfg_len=2+(opt_len);
  230. /* if valptr<lineend then we have to also allocate space for "=bar" */
  231. if(valptr<lineend) {
  232. equals=value=1;
  233. val_len=1+valend-valptr;
  234. cfg_len+=1+val_len;
  235. }
  236. /* if valptr==valend then we have "=" but no "bar" */
  237. else if(valptr==lineend) {
  238. equals=1;
  239. cfg_len+=1;
  240. }
  241. /* A line with no equal sign isn't valid */
  242. if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
  243. /* okay, now we have all the info we need, so we create a new np_arg_list
  244. * element and set the argument...
  245. */
  246. optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
  247. optnew->next=NULL;
  248. read_pos=0;
  249. optnew->arg=(char *)malloc(cfg_len+1);
  250. /* 1-character params needs only one dash */
  251. if(opt_len==1) {
  252. strncpy(&optnew->arg[read_pos], "-", 1);
  253. read_pos+=1;
  254. } else {
  255. strncpy(&optnew->arg[read_pos], "--", 2);
  256. read_pos+=2;
  257. }
  258. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  259. if(value) {
  260. optnew->arg[read_pos++]='=';
  261. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  262. }
  263. optnew->arg[read_pos]='\0';
  264. /* ...and put that to the end of the list */
  265. if(*optlst==NULL) {
  266. *optlst=optnew;
  267. } else {
  268. while(opttmp->next!=NULL) {
  269. opttmp=opttmp->next;
  270. }
  271. opttmp->next = optnew;
  272. }
  273. free(linebuf);
  274. return 0;
  275. }
  276. static char* default_file(void){
  277. char *np_env=NULL;
  278. /* FIXME: STUB */
  279. return "";
  280. #if 0
  281. if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
  282. /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
  283. * every PATHs defined (colon-separated).
  284. */
  285. }
  286. if !file_found
  287. search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1;
  288. if !file_found
  289. search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2;
  290. if !file_found
  291. return empty string (or null if we want to die);
  292. return file name;
  293. #endif
  294. }