parse_ini.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "utils_base.h"
  29. #include "parse_ini.h"
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. /* TODO: die like N::P if section is not found */
  35. /* TODO: die like N::P if config file is not found */
  36. /* np_ini_info contains the result of parsing a "locator" in the format
  37. * [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
  38. */
  39. typedef struct {
  40. char *file;
  41. char *stanza;
  42. } np_ini_info;
  43. /* eat all characters from a FILE pointer until n is encountered */
  44. #define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
  45. /* internal function that returns the constructed defaults options */
  46. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
  47. /* internal function that converts a single line into options format */
  48. static int add_option(FILE *f, np_arg_list **optlst);
  49. /* internal function to find default file */
  50. static char* default_file(void);
  51. /* internal function to stat() files */
  52. static int test_file(const char* env, int len, const char* file, char* temp_file);
  53. /* parse_locator decomposes a string of the form
  54. * [stanza][@filename]
  55. * into its seperate parts
  56. */
  57. static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i){
  58. size_t locator_len=0, stanza_len=0;
  59. /* if locator is NULL we'll use default values */
  60. if(locator){
  61. locator_len=strlen(locator);
  62. stanza_len=strcspn(locator, "@");
  63. }
  64. /* if a non-default stanza is provided */
  65. if(stanza_len>0){
  66. i->stanza=(char*)malloc(sizeof(char)*(stanza_len+1));
  67. strncpy(i->stanza, locator, stanza_len);
  68. i->stanza[stanza_len]='\0';
  69. } else { /* otherwise we use the default stanza */
  70. i->stanza=strdup(def_stanza);
  71. }
  72. /* if there is no @file part */
  73. if(stanza_len==locator_len){
  74. i->file=default_file();
  75. } else {
  76. i->file=strdup(&(locator[stanza_len+1]));
  77. }
  78. if(i->file==NULL || i->stanza==NULL){
  79. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  80. }
  81. }
  82. /* this is the externally visible function used by extra_opts */
  83. np_arg_list* np_get_defaults(const char *locator, const char *default_section){
  84. FILE *inifile=NULL;
  85. np_arg_list *defaults=NULL;
  86. np_ini_info i;
  87. parse_locator(locator, default_section, &i);
  88. /* if a file was specified or if we're using the default file */
  89. if(i.file != NULL && strlen(i.file) > 0){
  90. if(strcmp(i.file, "-")==0){
  91. inifile=stdin;
  92. } else {
  93. inifile=fopen(i.file, "r");
  94. }
  95. if(inifile==NULL) die(STATE_UNKNOWN, _("Can't read config file"));
  96. if(read_defaults(inifile, i.stanza, &defaults)==FALSE && strcmp(i.stanza, default_section) && inifile!=stdin) {
  97. /* We got nothing, try the default section */
  98. rewind(inifile);
  99. read_defaults(inifile, default_section, &defaults);
  100. }
  101. free(i.file);
  102. if(inifile!=stdin) fclose(inifile);
  103. }
  104. free(i.stanza);
  105. return defaults;
  106. }
  107. /* read_defaults is where the meat of the parsing takes place.
  108. *
  109. * note that this may be called by a setuid binary, so we need to
  110. * be extra careful about user-supplied input (i.e. avoiding possible
  111. * format string vulnerabilities, etc)
  112. */
  113. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
  114. int c, status=FALSE;
  115. size_t i, stanza_len;
  116. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
  117. stanza_len=strlen(stanza);
  118. /* our little stanza-parsing state machine. */
  119. while((c=fgetc(f))!=EOF){
  120. /* gobble up leading whitespace */
  121. if(isspace(c)) continue;
  122. switch(c){
  123. /* globble up coment lines */
  124. case '#':
  125. GOBBLE_TO(f, c, '\n');
  126. break;
  127. /* start of a stanza. check to see if it matches */
  128. case '[':
  129. stanzastate=WRONGSTANZA;
  130. for(i=0; i<stanza_len; i++){
  131. c=fgetc(f);
  132. /* Strip leading whitespace */
  133. if(i==0) for(c; isspace(c); c=fgetc(f));
  134. /* nope, read to the end of the line */
  135. if(c!=stanza[i]) {
  136. GOBBLE_TO(f, c, '\n');
  137. break;
  138. }
  139. }
  140. /* if it matched up to here and the next char is ']'... */
  141. if(i==stanza_len){
  142. c=fgetc(f);
  143. /* Strip trailing whitespace */
  144. for(c; isspace(c); c=fgetc(f));
  145. if(c==']') stanzastate=RIGHTSTANZA;
  146. }
  147. break;
  148. /* otherwise, we're in the body of a stanza or a parse error */
  149. default:
  150. switch(stanzastate){
  151. /* we never found the start of the first stanza, so
  152. * we're dealing with a config error
  153. */
  154. case NOSTANZA:
  155. die(STATE_UNKNOWN, _("Config file error"));
  156. break;
  157. /* we're in a stanza, but for a different plugin */
  158. case WRONGSTANZA:
  159. GOBBLE_TO(f, c, '\n');
  160. break;
  161. /* okay, this is where we start taking the config */
  162. case RIGHTSTANZA:
  163. ungetc(c, f);
  164. if(add_option(f, opts)){
  165. die(STATE_UNKNOWN, _("Config file error"));
  166. }
  167. status=TRUE;
  168. break;
  169. }
  170. break;
  171. }
  172. }
  173. return status;
  174. }
  175. /*
  176. * read one line of input in the format
  177. * ^option[[:space:]]*(=[[:space:]]*value)?
  178. * and creates it as a cmdline argument
  179. * --option[=value]
  180. * appending it to the linked list optbuf.
  181. */
  182. static int add_option(FILE *f, np_arg_list **optlst){
  183. np_arg_list *opttmp=*optlst, *optnew;
  184. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  185. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  186. short done_reading=0, equals=0, value=0;
  187. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  188. size_t opt_len=0, val_len=0;
  189. /* read one line from the file */
  190. while(!done_reading){
  191. /* grow if necessary */
  192. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  193. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  194. linebuf=realloc(linebuf, linebuf_sz);
  195. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  196. }
  197. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  198. else {
  199. read_pos=strlen(linebuf);
  200. if(linebuf[read_pos-1]=='\n') {
  201. linebuf[--read_pos]='\0';
  202. done_reading=1;
  203. }
  204. }
  205. }
  206. lineend=&linebuf[read_pos];
  207. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  208. /* skip leading whitespace */
  209. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  210. /* continue to '=' or EOL, watching for spaces that might precede it */
  211. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  212. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  213. else optend=NULL;
  214. }
  215. if(optend==NULL) optend=eqptr;
  216. --optend;
  217. /* ^[[:space:]]*=foo is a syntax error */
  218. if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
  219. /* continue from '=' to start of value or EOL */
  220. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  221. /* continue to the end of value (FIXME: watching for trailing comments) */
  222. for(valend=valptr; valend<lineend; valend++)
  223. /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
  224. if(*valend=='#') break;
  225. --valend;
  226. /* Finally trim off trailing spaces */
  227. for(valend; isspace(*valend); valend--);
  228. /* calculate the length of "--foo" */
  229. opt_len=1+optend-optptr;
  230. /* 1-character params needs only one dash */
  231. if(opt_len==1)
  232. cfg_len=1+(opt_len);
  233. else
  234. cfg_len=2+(opt_len);
  235. /* if valptr<lineend then we have to also allocate space for "=bar" */
  236. if(valptr<lineend) {
  237. equals=value=1;
  238. val_len=1+valend-valptr;
  239. cfg_len+=1+val_len;
  240. }
  241. /* if valptr==valend then we have "=" but no "bar" */
  242. else if(valptr==lineend) {
  243. equals=1;
  244. cfg_len+=1;
  245. }
  246. /* A line with no equal sign isn't valid */
  247. if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
  248. /* okay, now we have all the info we need, so we create a new np_arg_list
  249. * element and set the argument...
  250. */
  251. optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
  252. optnew->next=NULL;
  253. read_pos=0;
  254. optnew->arg=(char *)malloc(cfg_len+1);
  255. /* 1-character params needs only one dash */
  256. if(opt_len==1) {
  257. strncpy(&optnew->arg[read_pos], "-", 1);
  258. read_pos+=1;
  259. } else {
  260. strncpy(&optnew->arg[read_pos], "--", 2);
  261. read_pos+=2;
  262. }
  263. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  264. if(value) {
  265. optnew->arg[read_pos++]='=';
  266. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  267. }
  268. optnew->arg[read_pos]='\0';
  269. /* ...and put that to the end of the list */
  270. if(*optlst==NULL) {
  271. *optlst=optnew;
  272. } else {
  273. while(opttmp->next!=NULL) {
  274. opttmp=opttmp->next;
  275. }
  276. opttmp->next = optnew;
  277. }
  278. free(linebuf);
  279. return 0;
  280. }
  281. static char* default_file(void){
  282. struct stat sb;
  283. char *np_env=NULL, *default_file=NULL;
  284. char temp_file[MAX_INPUT_BUFFER];
  285. size_t len;
  286. if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
  287. /* skip ant starting colon... */
  288. while(*np_env==':') np_env++;
  289. /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
  290. * every PATHs defined (colon-separated).
  291. */
  292. while((len=strcspn(np_env,":"))>0){
  293. /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
  294. if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  295. test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
  296. default_file=strdup(temp_file);
  297. break;
  298. }
  299. /* Move on to the next token */
  300. np_env+=len;
  301. while(*np_env==':') np_env++;
  302. } /* while(...) */
  303. } /* if(getenv("NAGIOS_CONFIG_PATH")) */
  304. /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
  305. if(!default_file){
  306. if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  307. test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  308. test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  309. test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
  310. default_file=strdup(temp_file);
  311. }
  312. /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
  313. if(!default_file){
  314. if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  315. test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  316. test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
  317. default_file=strdup(temp_file);
  318. }
  319. /* Return default_file or empty string (should return NULL if we want to
  320. * die there...
  321. */
  322. if(default_file)
  323. return default_file;
  324. return "";
  325. }
  326. /* put together len bytes from env and the filename and test for its
  327. * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
  328. */
  329. static int test_file(const char* env, int len, const char* file, char* temp_file){
  330. struct stat sb;
  331. /* test for len + filelen + '/' + '\0' */
  332. if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
  333. strncpy(temp_file,env,len);
  334. temp_file[len]='\0';
  335. strncat(temp_file,"/",len+1);
  336. strncat(temp_file,file,len+strlen(file)+1);
  337. if(stat(temp_file, &sb) != -1) return 1;
  338. return 0;
  339. }