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