pkg-monitoring-plugins/lib/parse_ini.c

396 lines
11 KiB
C
Raw Normal View History

2013-11-26 22:55:28 +00:00
/*****************************************************************************
*
2014-07-11 19:01:00 +00:00
* Monitoring Plugins parse_ini library
2013-11-26 22:55:28 +00:00
*
* License: GPL
2014-07-11 19:01:00 +00:00
* Copyright (c) 2007 Monitoring Plugins Development Team
2013-11-26 22:55:28 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*****************************************************************************/
#include "common.h"
2014-07-11 19:01:00 +00:00
#include "idpriv.h"
2013-11-26 22:55:28 +00:00
#include "utils_base.h"
#include "parse_ini.h"
2014-07-11 19:01:00 +00:00
#include <ctype.h>
2013-11-26 22:55:28 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* np_ini_info contains the result of parsing a "locator" in the format
* [stanza_name][@config_filename] (check_foo@/etc/foo.ini, for example)
*/
typedef struct {
char *file;
char *stanza;
} np_ini_info;
2014-07-11 19:01:00 +00:00
static char *default_ini_file_names[] = {
"monitoring-plugins.ini",
"plugins.ini",
"nagios-plugins.ini",
NULL
};
static char *default_ini_path_names[] = {
"/usr/local/etc/monitoring-plugins/monitoring-plugins.ini",
"/usr/local/etc/monitoring-plugins.ini",
"/etc/monitoring-plugins/monitoring-plugins.ini",
"/etc/monitoring-plugins.ini",
/* deprecated path names (for backward compatibility): */
"/etc/nagios/plugins.ini",
"/usr/local/nagios/etc/plugins.ini",
"/usr/local/etc/nagios/plugins.ini",
"/etc/opt/nagios/plugins.ini",
"/etc/nagios-plugins.ini",
"/usr/local/etc/nagios-plugins.ini",
"/etc/opt/nagios-plugins.ini",
NULL
};
2013-11-26 22:55:28 +00:00
/* eat all characters from a FILE pointer until n is encountered */
#define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
/* internal function that returns the constructed defaults options */
static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
2014-07-11 19:01:00 +00:00
2013-11-26 22:55:28 +00:00
/* internal function that converts a single line into options format */
static int add_option(FILE *f, np_arg_list **optlst);
2014-07-11 19:01:00 +00:00
/* internal functions to find default file */
static char *default_file(void);
static char *default_file_in_path(void);
/*
* Parse_locator decomposes a string of the form
2013-11-26 22:55:28 +00:00
* [stanza][@filename]
2014-07-11 19:01:00 +00:00
* into its seperate parts.
2013-11-26 22:55:28 +00:00
*/
2014-07-11 19:01:00 +00:00
static void
parse_locator(const char *locator, const char *def_stanza, np_ini_info *i)
{
size_t locator_len = 0, stanza_len = 0;
2013-11-26 22:55:28 +00:00
/* if locator is NULL we'll use default values */
2014-07-11 19:01:00 +00:00
if (locator != NULL) {
locator_len = strlen(locator);
stanza_len = strcspn(locator, "@");
2013-11-26 22:55:28 +00:00
}
/* if a non-default stanza is provided */
2014-07-11 19:01:00 +00:00
if (stanza_len > 0) {
i->stanza = malloc(sizeof(char) * (stanza_len + 1));
2013-11-26 22:55:28 +00:00
strncpy(i->stanza, locator, stanza_len);
2014-07-11 19:01:00 +00:00
i->stanza[stanza_len] = '\0';
} else /* otherwise we use the default stanza */
i->stanza = strdup(def_stanza);
2014-07-11 19:01:00 +00:00
if (i->stanza == NULL)
2013-11-26 22:55:28 +00:00
die(STATE_UNKNOWN, _("malloc() failed!\n"));
2014-07-11 19:01:00 +00:00
/* check whether there's an @file part */
i->file = stanza_len == locator_len
? default_file()
: strdup(&(locator[stanza_len + 1]));
if (i->file == NULL || i->file[0] == '\0')
die(STATE_UNKNOWN,
_("Cannot find config file in any standard location.\n"));
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
/*
* This is the externally visible function used by extra_opts.
*/
np_arg_list *
np_get_defaults(const char *locator, const char *default_section)
{
FILE *inifile = NULL;
np_arg_list *defaults = NULL;
2013-11-26 22:55:28 +00:00
np_ini_info i;
2014-07-11 19:01:00 +00:00
int is_suid_plugin = mp_suid();
2013-11-26 22:55:28 +00:00
2014-07-11 19:01:00 +00:00
if (is_suid_plugin && idpriv_temp_drop() == -1)
die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"),
strerror(errno));
2013-11-26 22:55:28 +00:00
2014-07-11 19:01:00 +00:00
parse_locator(locator, default_section, &i);
inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r");
if (inifile == NULL)
die(STATE_UNKNOWN, _("Can't read config file: %s\n"),
strerror(errno));
if (read_defaults(inifile, i.stanza, &defaults) == FALSE)
die(STATE_UNKNOWN,
_("Invalid section '%s' in config file '%s'\n"), i.stanza,
i.file);
free(i.file);
if (inifile != stdin)
fclose(inifile);
2013-11-26 22:55:28 +00:00
free(i.stanza);
2014-07-11 19:01:00 +00:00
if (is_suid_plugin && idpriv_temp_restore() == -1)
die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"),
strerror(errno));
return defaults;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
/*
* The read_defaults() function is where the meat of the parsing takes place.
2013-11-26 22:55:28 +00:00
*
2014-07-11 19:01:00 +00:00
* Note that this may be called by a setuid binary, so we need to
2013-11-26 22:55:28 +00:00
* be extra careful about user-supplied input (i.e. avoiding possible
2014-07-11 19:01:00 +00:00
* format string vulnerabilities, etc).
2013-11-26 22:55:28 +00:00
*/
2014-07-11 19:01:00 +00:00
static int
read_defaults(FILE *f, const char *stanza, np_arg_list **opts)
{
int c, status = FALSE;
2013-11-26 22:55:28 +00:00
size_t i, stanza_len;
2014-07-11 19:01:00 +00:00
enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA;
2013-11-26 22:55:28 +00:00
2014-07-11 19:01:00 +00:00
stanza_len = strlen(stanza);
2013-11-26 22:55:28 +00:00
2014-07-11 19:01:00 +00:00
/* our little stanza-parsing state machine */
while ((c = fgetc(f)) != EOF) {
2013-11-26 22:55:28 +00:00
/* gobble up leading whitespace */
2014-07-11 19:01:00 +00:00
if (isspace(c))
continue;
switch (c) {
2013-11-26 22:55:28 +00:00
/* globble up coment lines */
2014-07-11 19:01:00 +00:00
case ';':
case '#':
GOBBLE_TO(f, c, '\n');
break;
/* start of a stanza, check to see if it matches */
case '[':
stanzastate = WRONGSTANZA;
for (i = 0; i < stanza_len; i++) {
c = fgetc(f);
/* strip leading whitespace */
if (i == 0)
for (; isspace(c); c = fgetc(f))
continue;
/* nope, read to the end of the line */
if (c != stanza[i]) {
GOBBLE_TO(f, c, '\n');
break;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
}
/* if it matched up to here and the next char is ']'... */
if (i == stanza_len) {
c = fgetc(f);
/* strip trailing whitespace */
for (; isspace(c); c = fgetc(f))
continue;
if (c == ']')
stanzastate = RIGHTSTANZA;
}
break;
2013-11-26 22:55:28 +00:00
/* otherwise, we're in the body of a stanza or a parse error */
2014-07-11 19:01:00 +00:00
default:
switch (stanzastate) {
/* we never found the start of the first stanza, so
* we're dealing with a config error
*/
case NOSTANZA:
die(STATE_UNKNOWN, "%s\n",
_("Config file error"));
/* we're in a stanza, but for a different plugin */
case WRONGSTANZA:
GOBBLE_TO(f, c, '\n');
break;
/* okay, this is where we start taking the config */
case RIGHTSTANZA:
ungetc(c, f);
if (add_option(f, opts)) {
die(STATE_UNKNOWN, "%s\n",
_("Config file error"));
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
status = TRUE;
2013-11-26 22:55:28 +00:00
break;
2014-07-11 19:01:00 +00:00
}
break;
2013-11-26 22:55:28 +00:00
}
}
return status;
}
/*
2014-07-11 19:01:00 +00:00
* Read one line of input in the format
2013-11-26 22:55:28 +00:00
* ^option[[:space:]]*(=[[:space:]]*value)?
2014-07-11 19:01:00 +00:00
* and create it as a cmdline argument
2013-11-26 22:55:28 +00:00
* --option[=value]
* appending it to the linked list optbuf.
*/
2014-07-11 19:01:00 +00:00
static int
add_option(FILE *f, np_arg_list **optlst)
{
np_arg_list *opttmp = *optlst, *optnew;
char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL;
char *eqptr = NULL, *valptr = NULL, *valend = NULL;
short done_reading = 0, equals = 0, value = 0;
size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0;
size_t opt_len = 0, val_len = 0;
2013-11-26 22:55:28 +00:00
/* read one line from the file */
2014-07-11 19:01:00 +00:00
while (!done_reading) {
2013-11-26 22:55:28 +00:00
/* grow if necessary */
2014-07-11 19:01:00 +00:00
if (linebuf == NULL || read_pos + read_sz >= linebuf_sz) {
linebuf_sz = linebuf_sz > 0 ? linebuf_sz << 1 : read_sz;
linebuf = realloc(linebuf, linebuf_sz);
if (linebuf == NULL)
die(STATE_UNKNOWN, _("malloc() failed!\n"));
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL)
done_reading = 1;
2013-11-26 22:55:28 +00:00
else {
2014-07-11 19:01:00 +00:00
read_pos = strlen(linebuf);
if (linebuf[read_pos - 1] == '\n') {
linebuf[--read_pos] = '\0';
done_reading = 1;
2013-11-26 22:55:28 +00:00
}
}
}
2014-07-11 19:01:00 +00:00
lineend = &linebuf[read_pos];
/* all that to read one line, isn't C fun? :) now comes the parsing :/ */
2013-11-26 22:55:28 +00:00
/* skip leading whitespace */
2014-07-11 19:01:00 +00:00
for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++)
continue;
2013-11-26 22:55:28 +00:00
/* continue to '=' or EOL, watching for spaces that might precede it */
2014-07-11 19:01:00 +00:00
for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) {
if (isspace(*eqptr) && optend == NULL)
optend = eqptr;
else
optend = NULL;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
if (optend == NULL)
optend = eqptr;
2013-11-26 22:55:28 +00:00
--optend;
/* ^[[:space:]]*=foo is a syntax error */
2014-07-11 19:01:00 +00:00
if (optptr == eqptr)
die(STATE_UNKNOWN, "%s\n", _("Config file error"));
2013-11-26 22:55:28 +00:00
/* continue from '=' to start of value or EOL */
2014-07-11 19:01:00 +00:00
for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr);
valptr++)
continue;
/* continue to the end of value */
2014-07-11 19:01:00 +00:00
for (valend = valptr; valend < lineend; valend++)
continue;
2013-11-26 22:55:28 +00:00
--valend;
2014-07-11 19:01:00 +00:00
/* finally trim off trailing spaces */
for (; isspace(*valend); valend--)
continue;
2013-11-26 22:55:28 +00:00
/* calculate the length of "--foo" */
2014-07-11 19:01:00 +00:00
opt_len = (size_t)(1 + optend - optptr);
2013-11-26 22:55:28 +00:00
/* 1-character params needs only one dash */
2014-07-11 19:01:00 +00:00
if (opt_len == 1)
cfg_len = 1 + (opt_len);
2013-11-26 22:55:28 +00:00
else
2014-07-11 19:01:00 +00:00
cfg_len = 2 + (opt_len);
2013-11-26 22:55:28 +00:00
/* if valptr<lineend then we have to also allocate space for "=bar" */
2014-07-11 19:01:00 +00:00
if (valptr < lineend) {
equals = value = 1;
val_len = (size_t)(1 + valend - valptr);
cfg_len += 1 + val_len;
2013-11-26 22:55:28 +00:00
}
/* if valptr==valend then we have "=" but no "bar" */
2014-07-11 19:01:00 +00:00
else if (valptr == lineend) {
equals = 1;
cfg_len += 1;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
/* a line with no equal sign isn't valid */
if (equals == 0)
die(STATE_UNKNOWN, "%s\n", _("Config file error"));
2013-11-26 22:55:28 +00:00
/* okay, now we have all the info we need, so we create a new np_arg_list
* element and set the argument...
*/
2014-07-11 19:01:00 +00:00
optnew = malloc(sizeof(np_arg_list));
optnew->next = NULL;
2013-11-26 22:55:28 +00:00
2014-07-11 19:01:00 +00:00
read_pos = 0;
optnew->arg = malloc(cfg_len + 1);
2013-11-26 22:55:28 +00:00
/* 1-character params needs only one dash */
2014-07-11 19:01:00 +00:00
if (opt_len == 1) {
2013-11-26 22:55:28 +00:00
strncpy(&optnew->arg[read_pos], "-", 1);
2014-07-11 19:01:00 +00:00
read_pos += 1;
2013-11-26 22:55:28 +00:00
} else {
strncpy(&optnew->arg[read_pos], "--", 2);
2014-07-11 19:01:00 +00:00
read_pos += 2;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
strncpy(&optnew->arg[read_pos], optptr, opt_len);
read_pos += opt_len;
if (value) {
optnew->arg[read_pos++] = '=';
strncpy(&optnew->arg[read_pos], valptr, val_len);
read_pos += val_len;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
optnew->arg[read_pos] = '\0';
2013-11-26 22:55:28 +00:00
/* ...and put that to the end of the list */
2014-07-11 19:01:00 +00:00
if (*optlst == NULL)
*optlst = optnew;
else {
while (opttmp->next != NULL)
opttmp = opttmp->next;
2013-11-26 22:55:28 +00:00
opttmp->next = optnew;
}
free(linebuf);
return 0;
}
2014-07-11 19:01:00 +00:00
static char *
default_file(void)
{
char **p, *ini_file;
if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL ||
(ini_file = default_file_in_path()) != NULL)
return ini_file;
for (p = default_ini_path_names; *p != NULL; p++)
if (access(*p, F_OK) == 0)
return *p;
return NULL;
2013-11-26 22:55:28 +00:00
}
2014-07-11 19:01:00 +00:00
static char *
default_file_in_path(void)
{
char *config_path, **file;
char *dir, *ini_file, *tokens;
if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL)
return NULL;
/* shall we spit out a warning that NAGIOS_CONFIG_PATH is deprecated? */
if ((tokens = strdup(config_path)) == NULL)
die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory"));
for (dir = strtok(tokens, ":"); dir != NULL; dir = strtok(NULL, ":")) {
for (file = default_ini_file_names; *file != NULL; file++) {
if ((asprintf(&ini_file, "%s/%s", dir, *file)) < 0)
die(STATE_UNKNOWN, "%s\n", _("Insufficient Memory"));
if (access(ini_file, F_OK) == 0) {
free(tokens);
return ini_file;
}
}
}
free(tokens);
return NULL;
2013-11-26 22:55:28 +00:00
}