4
0

parse_ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 test files access */
  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. if(strcmp(i->file, "") == 0){
  71. die(STATE_UNKNOWN, _("Cannot find '%s' or '%s' in any standard location.\n"), NP_DEFAULT_INI_FILENAME1, NP_DEFAULT_INI_FILENAME2);
  72. }
  73. } else {
  74. i->file=strdup(&(locator[stanza_len+1]));
  75. }
  76. if(i->file==NULL || i->stanza==NULL){
  77. die(STATE_UNKNOWN, _("malloc() failed!\n"));
  78. }
  79. }
  80. /* this is the externally visible function used by extra_opts */
  81. np_arg_list* np_get_defaults(const char *locator, const char *default_section){
  82. FILE *inifile=NULL;
  83. np_arg_list *defaults=NULL;
  84. np_ini_info i;
  85. parse_locator(locator, default_section, &i);
  86. /* if a file was specified or if we're using the default file */
  87. if(i.file != NULL && strlen(i.file) > 0){
  88. if(strcmp(i.file, "-")==0){
  89. inifile=stdin;
  90. } else {
  91. inifile=fopen(i.file, "r");
  92. }
  93. if(inifile==NULL) die(STATE_UNKNOWN, "%s\n", _("Can't read config file"));
  94. if(read_defaults(inifile, i.stanza, &defaults)==FALSE)
  95. die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file);
  96. free(i.file);
  97. if(inifile!=stdin) fclose(inifile);
  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. 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, "%s\n", _("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, "%s\n", _("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, "%s\n", _("Config file error"));
  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 */
  218. for(valend=valptr; valend<lineend; valend++);
  219. --valend;
  220. /* Finally trim off trailing spaces */
  221. for(valend; isspace(*valend); valend--);
  222. /* calculate the length of "--foo" */
  223. opt_len=1+optend-optptr;
  224. /* 1-character params needs only one dash */
  225. if(opt_len==1)
  226. cfg_len=1+(opt_len);
  227. else
  228. cfg_len=2+(opt_len);
  229. /* if valptr<lineend then we have to also allocate space for "=bar" */
  230. if(valptr<lineend) {
  231. equals=value=1;
  232. val_len=1+valend-valptr;
  233. cfg_len+=1+val_len;
  234. }
  235. /* if valptr==valend then we have "=" but no "bar" */
  236. else if(valptr==lineend) {
  237. equals=1;
  238. cfg_len+=1;
  239. }
  240. /* A line with no equal sign isn't valid */
  241. if(equals==0) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  242. /* okay, now we have all the info we need, so we create a new np_arg_list
  243. * element and set the argument...
  244. */
  245. optnew=(np_arg_list *)malloc(sizeof(np_arg_list));
  246. optnew->next=NULL;
  247. read_pos=0;
  248. optnew->arg=(char *)malloc(cfg_len+1);
  249. /* 1-character params needs only one dash */
  250. if(opt_len==1) {
  251. strncpy(&optnew->arg[read_pos], "-", 1);
  252. read_pos+=1;
  253. } else {
  254. strncpy(&optnew->arg[read_pos], "--", 2);
  255. read_pos+=2;
  256. }
  257. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  258. if(value) {
  259. optnew->arg[read_pos++]='=';
  260. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  261. }
  262. optnew->arg[read_pos]='\0';
  263. /* ...and put that to the end of the list */
  264. if(*optlst==NULL) {
  265. *optlst=optnew;
  266. } else {
  267. while(opttmp->next!=NULL) {
  268. opttmp=opttmp->next;
  269. }
  270. opttmp->next = optnew;
  271. }
  272. free(linebuf);
  273. return 0;
  274. }
  275. static char* default_file(void){
  276. struct stat sb;
  277. char *np_env=NULL, *default_file=NULL;
  278. char temp_file[MAX_INPUT_BUFFER];
  279. size_t len;
  280. if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
  281. /* skip any starting colon... */
  282. while(*np_env==':') np_env++;
  283. /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
  284. * every PATHs defined (colon-separated).
  285. */
  286. while((len=strcspn(np_env,":"))>0){
  287. /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
  288. if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  289. test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
  290. default_file=strdup(temp_file);
  291. break;
  292. }
  293. /* Move on to the next token */
  294. np_env+=len;
  295. while(*np_env==':') np_env++;
  296. } /* while(...) */
  297. } /* if(getenv("NAGIOS_CONFIG_PATH")) */
  298. /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
  299. if(!default_file){
  300. if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  301. test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  302. test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  303. test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
  304. default_file=strdup(temp_file);
  305. }
  306. /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
  307. if(!default_file){
  308. if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  309. test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  310. test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
  311. default_file=strdup(temp_file);
  312. }
  313. /* Return default_file or empty string (should return NULL if we want plugins
  314. * to die there)...
  315. */
  316. if(default_file)
  317. return default_file;
  318. return "";
  319. }
  320. /* put together len bytes from env and the filename and test for its
  321. * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
  322. */
  323. static int test_file(const char* env, int len, const char* file, char* temp_file){
  324. /* test if len + filelen + '/' + '\0' fits in temp_file */
  325. if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
  326. strncpy(temp_file,env,len);
  327. temp_file[len]='\0';
  328. strncat(temp_file,"/",len+1);
  329. strncat(temp_file,file,len+strlen(file)+1);
  330. if(access(temp_file, F_OK) == 0) return 1;
  331. return 0;
  332. }