parse_ini.c 11 KB

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