4
0

parse_ini.c 12 KB

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