parse_ini.c 11 KB

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