parse_ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*****************************************************************************
  2. *
  3. * Nagios-plugins parse_ini library
  4. *
  5. * License: GPL
  6. * Copyright (c) 2007 Nagios Plugins Development Team
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. *****************************************************************************/
  23. #include "common.h"
  24. #include "utils_base.h"
  25. #include "parse_ini.h"
  26. #include <ctype.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. /* TODO: die like N::P if config file is not found */
  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. /* internal function to find default file */
  45. static char* default_file(void);
  46. /* internal function to stat() files */
  47. static int test_file(const char* env, int len, const char* file, char* temp_file);
  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=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)
  92. die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
  93. free(i.file);
  94. if(inifile!=stdin) fclose(inifile);
  95. }
  96. free(i.stanza);
  97. return defaults;
  98. }
  99. /* read_defaults is where the meat of the parsing takes place.
  100. *
  101. * note that this may be called by a setuid binary, so we need to
  102. * be extra careful about user-supplied input (i.e. avoiding possible
  103. * format string vulnerabilities, etc)
  104. */
  105. static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts){
  106. int c, status=FALSE;
  107. size_t i, stanza_len;
  108. enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate=NOSTANZA;
  109. stanza_len=strlen(stanza);
  110. /* our little stanza-parsing state machine. */
  111. while((c=fgetc(f))!=EOF){
  112. /* gobble up leading whitespace */
  113. if(isspace(c)) continue;
  114. switch(c){
  115. /* globble up coment lines */
  116. case ';':
  117. case '#':
  118. GOBBLE_TO(f, c, '\n');
  119. break;
  120. /* start of a stanza. check to see if it matches */
  121. case '[':
  122. stanzastate=WRONGSTANZA;
  123. for(i=0; i<stanza_len; i++){
  124. c=fgetc(f);
  125. /* Strip leading whitespace */
  126. if(i==0) for(c; isspace(c); c=fgetc(f));
  127. /* nope, read to the end of the line */
  128. if(c!=stanza[i]) {
  129. GOBBLE_TO(f, c, '\n');
  130. break;
  131. }
  132. }
  133. /* if it matched up to here and the next char is ']'... */
  134. if(i==stanza_len){
  135. c=fgetc(f);
  136. /* Strip trailing whitespace */
  137. for(c; isspace(c); c=fgetc(f));
  138. if(c==']') stanzastate=RIGHTSTANZA;
  139. }
  140. break;
  141. /* otherwise, we're in the body of a stanza or a parse error */
  142. default:
  143. switch(stanzastate){
  144. /* we never found the start of the first stanza, so
  145. * we're dealing with a config error
  146. */
  147. case NOSTANZA:
  148. die(STATE_UNKNOWN, _("Config file error"));
  149. break;
  150. /* we're in a stanza, but for a different plugin */
  151. case WRONGSTANZA:
  152. GOBBLE_TO(f, c, '\n');
  153. break;
  154. /* okay, this is where we start taking the config */
  155. case RIGHTSTANZA:
  156. ungetc(c, f);
  157. if(add_option(f, opts)){
  158. die(STATE_UNKNOWN, _("Config file error"));
  159. }
  160. status=TRUE;
  161. break;
  162. }
  163. break;
  164. }
  165. }
  166. return status;
  167. }
  168. /*
  169. * read one line of input in the format
  170. * ^option[[:space:]]*(=[[:space:]]*value)?
  171. * and creates it as a cmdline argument
  172. * --option[=value]
  173. * appending it to the linked list optbuf.
  174. */
  175. static int add_option(FILE *f, np_arg_list **optlst){
  176. np_arg_list *opttmp=*optlst, *optnew;
  177. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  178. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  179. short done_reading=0, equals=0, value=0;
  180. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  181. size_t opt_len=0, val_len=0;
  182. /* read one line from the file */
  183. while(!done_reading){
  184. /* grow if necessary */
  185. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  186. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  187. linebuf=realloc(linebuf, linebuf_sz);
  188. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  189. }
  190. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  191. else {
  192. read_pos=strlen(linebuf);
  193. if(linebuf[read_pos-1]=='\n') {
  194. linebuf[--read_pos]='\0';
  195. done_reading=1;
  196. }
  197. }
  198. }
  199. lineend=&linebuf[read_pos];
  200. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  201. /* skip leading whitespace */
  202. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  203. /* continue to '=' or EOL, watching for spaces that might precede it */
  204. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  205. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  206. else optend=NULL;
  207. }
  208. if(optend==NULL) optend=eqptr;
  209. --optend;
  210. /* ^[[:space:]]*=foo is a syntax error */
  211. if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
  212. /* continue from '=' to start of value or EOL */
  213. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  214. /* continue to the end of value */
  215. for(valend=valptr; valend<lineend; valend++);
  216. --valend;
  217. /* Finally trim off trailing spaces */
  218. for(valend; isspace(*valend); valend--);
  219. /* calculate the length of "--foo" */
  220. opt_len=1+optend-optptr;
  221. /* 1-character params needs only one dash */
  222. if(opt_len==1)
  223. cfg_len=1+(opt_len);
  224. else
  225. cfg_len=2+(opt_len);
  226. /* if valptr<lineend then we have to also allocate space for "=bar" */
  227. if(valptr<lineend) {
  228. equals=value=1;
  229. val_len=1+valend-valptr;
  230. cfg_len+=1+val_len;
  231. }
  232. /* if valptr==valend then we have "=" but no "bar" */
  233. else if(valptr==lineend) {
  234. equals=1;
  235. cfg_len+=1;
  236. }
  237. /* A line with no equal sign isn't valid */
  238. if(equals==0) die(STATE_UNKNOWN, _("Config file error\n"));
  239. /* okay, now we have all the info we need, so we create a new np_arg_list
  240. * element and set the argument...
  241. */
  242. optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
  243. optnew->next=NULL;
  244. read_pos=0;
  245. optnew->arg=(char *)malloc(cfg_len+1);
  246. /* 1-character params needs only one dash */
  247. if(opt_len==1) {
  248. strncpy(&optnew->arg[read_pos], "-", 1);
  249. read_pos+=1;
  250. } else {
  251. strncpy(&optnew->arg[read_pos], "--", 2);
  252. read_pos+=2;
  253. }
  254. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  255. if(value) {
  256. optnew->arg[read_pos++]='=';
  257. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  258. }
  259. optnew->arg[read_pos]='\0';
  260. /* ...and put that to the end of the list */
  261. if(*optlst==NULL) {
  262. *optlst=optnew;
  263. } else {
  264. while(opttmp->next!=NULL) {
  265. opttmp=opttmp->next;
  266. }
  267. opttmp->next = optnew;
  268. }
  269. free(linebuf);
  270. return 0;
  271. }
  272. static char* default_file(void){
  273. struct stat sb;
  274. char *np_env=NULL, *default_file=NULL;
  275. char temp_file[MAX_INPUT_BUFFER];
  276. size_t len;
  277. if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
  278. /* skip any starting colon... */
  279. while(*np_env==':') np_env++;
  280. /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
  281. * every PATHs defined (colon-separated).
  282. */
  283. while((len=strcspn(np_env,":"))>0){
  284. /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
  285. if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  286. test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
  287. default_file=strdup(temp_file);
  288. break;
  289. }
  290. /* Move on to the next token */
  291. np_env+=len;
  292. while(*np_env==':') np_env++;
  293. } /* while(...) */
  294. } /* if(getenv("NAGIOS_CONFIG_PATH")) */
  295. /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
  296. if(!default_file){
  297. if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  298. test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  299. test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  300. test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
  301. default_file=strdup(temp_file);
  302. }
  303. /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
  304. if(!default_file){
  305. if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  306. test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  307. test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
  308. default_file=strdup(temp_file);
  309. }
  310. /* Return default_file or empty string (should return NULL if we want plugins
  311. * to die there)...
  312. */
  313. if(default_file)
  314. return default_file;
  315. return "";
  316. }
  317. /* put together len bytes from env and the filename and test for its
  318. * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
  319. */
  320. static int test_file(const char* env, int len, const char* file, char* temp_file){
  321. struct stat sb;
  322. /* test if len + filelen + '/' + '\0' fits in temp_file */
  323. if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
  324. strncpy(temp_file,env,len);
  325. temp_file[len]='\0';
  326. strncat(temp_file,"/",len+1);
  327. strncat(temp_file,file,len+strlen(file)+1);
  328. if(stat(temp_file, &sb) != -1) return 1;
  329. return 0;
  330. }