parse_ini.c 11 KB

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