parse_ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. case '#':
  122. GOBBLE_TO(f, c, '\n');
  123. break;
  124. /* start of a stanza. check to see if it matches */
  125. case '[':
  126. stanzastate=WRONGSTANZA;
  127. for(i=0; i<stanza_len; i++){
  128. c=fgetc(f);
  129. /* Strip leading whitespace */
  130. if(i==0) for(c; isspace(c); c=fgetc(f));
  131. /* nope, read to the end of the line */
  132. if(c!=stanza[i]) {
  133. GOBBLE_TO(f, c, '\n');
  134. break;
  135. }
  136. }
  137. /* if it matched up to here and the next char is ']'... */
  138. if(i==stanza_len){
  139. c=fgetc(f);
  140. /* Strip trailing whitespace */
  141. for(c; isspace(c); c=fgetc(f));
  142. if(c==']') stanzastate=RIGHTSTANZA;
  143. }
  144. break;
  145. /* otherwise, we're in the body of a stanza or a parse error */
  146. default:
  147. switch(stanzastate){
  148. /* we never found the start of the first stanza, so
  149. * we're dealing with a config error
  150. */
  151. case NOSTANZA:
  152. die(STATE_UNKNOWN, _("Config file error"));
  153. break;
  154. /* we're in a stanza, but for a different plugin */
  155. case WRONGSTANZA:
  156. GOBBLE_TO(f, c, '\n');
  157. break;
  158. /* okay, this is where we start taking the config */
  159. case RIGHTSTANZA:
  160. ungetc(c, f);
  161. if(add_option(f, opts)){
  162. die(STATE_UNKNOWN, _("Config file error"));
  163. }
  164. status=TRUE;
  165. break;
  166. }
  167. break;
  168. }
  169. }
  170. return status;
  171. }
  172. /*
  173. * read one line of input in the format
  174. * ^option[[:space:]]*(=[[:space:]]*value)?
  175. * and creates it as a cmdline argument
  176. * --option[=value]
  177. * appending it to the linked list optbuf.
  178. */
  179. static int add_option(FILE *f, np_arg_list **optlst){
  180. np_arg_list *opttmp=*optlst, *optnew;
  181. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  182. char *eqptr=NULL, *valptr=NULL, *spaceptr=NULL, *valend=NULL;
  183. short done_reading=0, equals=0, value=0;
  184. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  185. size_t opt_len=0, val_len=0;
  186. /* read one line from the file */
  187. while(!done_reading){
  188. /* grow if necessary */
  189. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  190. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  191. linebuf=realloc(linebuf, linebuf_sz);
  192. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  193. }
  194. if(fgets(&linebuf[read_pos], read_sz, f)==NULL) done_reading=1;
  195. else {
  196. read_pos=strlen(linebuf);
  197. if(linebuf[read_pos-1]=='\n') {
  198. linebuf[--read_pos]='\0';
  199. done_reading=1;
  200. }
  201. }
  202. }
  203. lineend=&linebuf[read_pos];
  204. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  205. /* skip leading whitespace */
  206. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  207. /* continue to '=' or EOL, watching for spaces that might precede it */
  208. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  209. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  210. else optend=NULL;
  211. }
  212. if(optend==NULL) optend=eqptr;
  213. --optend;
  214. /* ^[[:space:]]*=foo is a syntax error */
  215. if(optptr==eqptr) die(STATE_UNKNOWN, _("Config file error\n"));
  216. /* continue from '=' to start of value or EOL */
  217. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  218. /* continue to the end of value */
  219. for(valend=valptr; valend<lineend; valend++);
  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. struct stat sb;
  278. char *np_env=NULL, *default_file=NULL;
  279. char temp_file[MAX_INPUT_BUFFER];
  280. size_t len;
  281. if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
  282. /* skip any starting colon... */
  283. while(*np_env==':') np_env++;
  284. /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
  285. * every PATHs defined (colon-separated).
  286. */
  287. while((len=strcspn(np_env,":"))>0){
  288. /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
  289. if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  290. test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
  291. default_file=strdup(temp_file);
  292. break;
  293. }
  294. /* Move on to the next token */
  295. np_env+=len;
  296. while(*np_env==':') np_env++;
  297. } /* while(...) */
  298. } /* if(getenv("NAGIOS_CONFIG_PATH")) */
  299. /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
  300. if(!default_file){
  301. if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  302. test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  303. test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
  304. test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
  305. default_file=strdup(temp_file);
  306. }
  307. /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
  308. if(!default_file){
  309. if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  310. test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
  311. test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
  312. default_file=strdup(temp_file);
  313. }
  314. /* Return default_file or empty string (should return NULL if we want plugins
  315. * to die there)...
  316. */
  317. if(default_file)
  318. return default_file;
  319. return "";
  320. }
  321. /* put together len bytes from env and the filename and test for its
  322. * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
  323. */
  324. static int test_file(const char* env, int len, const char* file, char* temp_file){
  325. struct stat sb;
  326. /* test if len + filelen + '/' + '\0' fits in temp_file */
  327. if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
  328. strncpy(temp_file,env,len);
  329. temp_file[len]='\0';
  330. strncat(temp_file,"/",len+1);
  331. strncat(temp_file,file,len+strlen(file)+1);
  332. if(stat(temp_file, &sb) != -1) return 1;
  333. return 0;
  334. }