parse_ini.c 12 KB

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