parse_ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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(; 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(; 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. /* we're in a stanza, but for a different plugin */
  188. case WRONGSTANZA:
  189. GOBBLE_TO(f, c, '\n');
  190. break;
  191. /* okay, this is where we start taking the config */
  192. case RIGHTSTANZA:
  193. ungetc(c, f);
  194. if(add_option(f, opts)){
  195. die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  196. }
  197. status=TRUE;
  198. break;
  199. }
  200. break;
  201. }
  202. }
  203. return status;
  204. }
  205. /*
  206. * read one line of input in the format
  207. * ^option[[:space:]]*(=[[:space:]]*value)?
  208. * and creates it as a cmdline argument
  209. * --option[=value]
  210. * appending it to the linked list optbuf.
  211. */
  212. static int add_option(FILE *f, np_arg_list **optlst){
  213. np_arg_list *opttmp=*optlst, *optnew;
  214. char *linebuf=NULL, *lineend=NULL, *optptr=NULL, *optend=NULL;
  215. char *eqptr=NULL, *valptr=NULL, *valend=NULL;
  216. short done_reading=0, equals=0, value=0;
  217. size_t cfg_len=0, read_sz=8, linebuf_sz=0, read_pos=0;
  218. size_t opt_len=0, val_len=0;
  219. /* read one line from the file */
  220. while(!done_reading){
  221. /* grow if necessary */
  222. if(linebuf==NULL || read_pos+read_sz >= linebuf_sz){
  223. linebuf_sz=(linebuf_sz>0)?linebuf_sz<<1:read_sz;
  224. linebuf=realloc(linebuf, linebuf_sz);
  225. if(linebuf==NULL) die(STATE_UNKNOWN, _("malloc() failed!\n"));
  226. }
  227. if(fgets(&linebuf[read_pos], (int)read_sz, f)==NULL) done_reading=1;
  228. else {
  229. read_pos=strlen(linebuf);
  230. if(linebuf[read_pos-1]=='\n') {
  231. linebuf[--read_pos]='\0';
  232. done_reading=1;
  233. }
  234. }
  235. }
  236. lineend=&linebuf[read_pos];
  237. /* all that to read one line. isn't C fun? :) now comes the parsing :/ */
  238. /* skip leading whitespace */
  239. for(optptr=linebuf; optptr<lineend && isspace(*optptr); optptr++);
  240. /* continue to '=' or EOL, watching for spaces that might precede it */
  241. for(eqptr=optptr; eqptr<lineend && *eqptr!='='; eqptr++){
  242. if(isspace(*eqptr) && optend==NULL) optend=eqptr;
  243. else optend=NULL;
  244. }
  245. if(optend==NULL) optend=eqptr;
  246. --optend;
  247. /* ^[[:space:]]*=foo is a syntax error */
  248. if(optptr==eqptr) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  249. /* continue from '=' to start of value or EOL */
  250. for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
  251. /* continue to the end of value */
  252. for(valend=valptr; valend<lineend; valend++);
  253. --valend;
  254. /* Finally trim off trailing spaces */
  255. for(; isspace(*valend); valend--);
  256. /* calculate the length of "--foo" */
  257. opt_len=(size_t)(1+optend-optptr);
  258. /* 1-character params needs only one dash */
  259. if(opt_len==1)
  260. cfg_len=1+(opt_len);
  261. else
  262. cfg_len=2+(opt_len);
  263. /* if valptr<lineend then we have to also allocate space for "=bar" */
  264. if(valptr<lineend) {
  265. equals=value=1;
  266. val_len=(size_t)(1+valend-valptr);
  267. cfg_len+=1+val_len;
  268. }
  269. /* if valptr==valend then we have "=" but no "bar" */
  270. else if(valptr==lineend) {
  271. equals=1;
  272. cfg_len+=1;
  273. }
  274. /* A line with no equal sign isn't valid */
  275. if(equals==0) die(STATE_UNKNOWN, "%s\n", _("Config file error"));
  276. /* okay, now we have all the info we need, so we create a new np_arg_list
  277. * element and set the argument...
  278. */
  279. optnew=malloc(sizeof(np_arg_list));
  280. optnew->next=NULL;
  281. read_pos=0;
  282. optnew->arg=malloc(cfg_len+1);
  283. /* 1-character params needs only one dash */
  284. if(opt_len==1) {
  285. strncpy(&optnew->arg[read_pos], "-", 1);
  286. read_pos+=1;
  287. } else {
  288. strncpy(&optnew->arg[read_pos], "--", 2);
  289. read_pos+=2;
  290. }
  291. strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
  292. if(value) {
  293. optnew->arg[read_pos++]='=';
  294. strncpy(&optnew->arg[read_pos], valptr, val_len); read_pos+=val_len;
  295. }
  296. optnew->arg[read_pos]='\0';
  297. /* ...and put that to the end of the list */
  298. if(*optlst==NULL) {
  299. *optlst=optnew;
  300. } else {
  301. while(opttmp->next!=NULL) {
  302. opttmp=opttmp->next;
  303. }
  304. opttmp->next = optnew;
  305. }
  306. free(linebuf);
  307. return 0;
  308. }
  309. static char *default_file_in_path(void){
  310. char *config_path, **file;
  311. char *dir, *ini_file, *tokens;
  312. if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL)
  313. return NULL;
  314. if((tokens=strdup(config_path))==NULL)
  315. die(STATE_UNKNOWN, _("Insufficient Memory"));
  316. for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){
  317. for(file=default_ini_file_names; *file!=NULL; file++){
  318. if((asprintf(&ini_file, "%s/%s", dir, *file))<0)
  319. die(STATE_UNKNOWN, _("Insufficient Memory"));
  320. if(access(ini_file, F_OK)==0){
  321. free(tokens);
  322. return ini_file;
  323. }
  324. }
  325. }
  326. free(tokens);
  327. return NULL;
  328. }
  329. static char *default_file(void){
  330. char **p, *ini_file;
  331. if((ini_file=default_file_in_path())!=NULL)
  332. return ini_file;
  333. for(p=default_ini_path_names; *p!=NULL; p++)
  334. if (access(*p, F_OK)==0)
  335. return *p;
  336. return NULL;
  337. }