Logo Search packages:      
Sourcecode: audit version File versions  Download package

ids-config.c

/* ids-config.c -- 
 * Copyright 2008 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *   Steve Grubb <sgrubb@redhat.com>
 * 
 */

#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "ids-config.h"
#include <grp.h>

/* Local prototypes */
struct nv_pair
{
      const char *name;
      const char *value;
      const char *option;
};

struct kw_pair 
{
      const char *name;
      int (*parser)(struct nv_pair *, int, struct ids_conf *);
      int max_options;
};

struct nv_list
{ 
      const char *name;
      int option;
};

static char *get_line(FILE *f, char *buf);
static int nv_split(char *buf, struct nv_pair *nv);
static const struct kw_pair *kw_lookup(const char *val);

static int num_parser(struct nv_pair *nv, int line, 
            struct ids_conf *config);
static int group_parser(struct nv_pair *nv, int line, 
            struct ids_conf *config);
static int sanity_check(struct ids_conf *config);

static const struct kw_pair keywords[] = 
{
  {"log_group",                group_parser,          0 },
  {"num_logs",                 num_parser,                  0 },
  { NULL,                      NULL }
};

static const struct nv_list log_formats[] =
{
  {"raw",  0 },
  {"nolog", 1 },
  { NULL,  0 }
};


/*
 * Set everything to its default value
*/
static void clear_config(struct ids_conf *config)
{
      config->group = 0;
      config->num = 0L;
}

int load_config(struct ids_conf *config)
{
      int fd, rc, mode, lineno = 1;
      struct stat st;
      FILE *f;
      char buf[128];

      clear_config(config);

      /* open the file */
      mode = O_RDONLY;
      rc = open(CONFIG_FILE, mode);
      if (rc < 0) {
            if (errno != ENOENT) {
                  syslog(LOG_ERR, "Error opening config file (%s)", 
                        strerror(errno));
                  return 1;
            }
            syslog(LOG_WARNING,
                  "Config file %s doesn't exist, skipping", CONFIG_FILE);
            return 0;
      }
      fd = rc;

      /* check the file's permissions: owned by root, not world writable,
       * not symlink.
       */
      syslog(LOG_DEBUG, "Config file %s opened for parsing", 
                  CONFIG_FILE);
      if (fstat(fd, &st) < 0) {
            syslog(LOG_ERR, "Error fstat'ing config file (%s)", 
                  strerror(errno));
            close(fd);
            return 1;
      }
      if (st.st_uid != 0) {
            syslog(LOG_ERR, "Error - %s isn't owned by root", 
                  CONFIG_FILE);
            close(fd);
            return 1;
      }
      if ((st.st_mode & S_IWOTH) == S_IWOTH) {
            syslog(LOG_ERR, "Error - %s is world writable", 
                  CONFIG_FILE);
            close(fd);
            return 1;
      }
      if (!S_ISREG(st.st_mode)) {
            syslog(LOG_ERR, "Error - %s is not a regular file", 
                  CONFIG_FILE);
            close(fd);
            return 1;
      }

      /* it's ok, read line by line */
      f = fdopen(fd, "r");
      if (f == NULL) {
            syslog(LOG_ERR, "Error - fdopen failed (%s)", 
                  strerror(errno));
            close(fd);
            return 1;
      }

      while (get_line(f, buf)) {
            // convert line into name-value pair
            const struct kw_pair *kw;
            struct nv_pair nv;
            rc = nv_split(buf, &nv);
            switch (rc) {
                  case 0: // fine
                        break;
                  case 1: // not the right number of tokens.
                        syslog(LOG_ERR, 
                        "Wrong number of arguments for line %d in %s", 
                              lineno, CONFIG_FILE);
                        break;
                  case 2: // no '=' sign
                        syslog(LOG_ERR, 
                              "Missing equal sign for line %d in %s", 
                              lineno, CONFIG_FILE);
                        break;
                  default: // something else went wrong... 
                        syslog(LOG_ERR, 
                              "Unknown error for line %d in %s", 
                              lineno, CONFIG_FILE);
                        break;
            }
            if (nv.name == NULL) {
                  lineno++;
                  continue;
            }
            if (nv.value == NULL) {
                  fclose(f);
                  return 1;
            }

            /* identify keyword or error */
            kw = kw_lookup(nv.name);
            if (kw->name == NULL) {
                  syslog(LOG_ERR, 
                        "Unknown keyword \"%s\" in line %d of %s", 
                        nv.name, lineno, CONFIG_FILE);
                  fclose(f);
                  return 1;
            }

            /* Check number of options */
            if (kw->max_options == 0 && nv.option != NULL) {
                  syslog(LOG_ERR, 
                        "Keyword \"%s\" has invalid option "
                        "\"%s\" in line %d of %s", 
                        nv.name, nv.option, lineno, CONFIG_FILE);
                  fclose(f);
                  return 1;
            }

            /* dispatch to keyword's local parser */
            rc = kw->parser(&nv, lineno, config);
            if (rc != 0) {
                  fclose(f);
                  return 1; // local parser puts message out
            }

            lineno++;
      }

      fclose(f);
      if (lineno > 1)
            return sanity_check(config);
      return 0;
}

static char *get_line(FILE *f, char *buf)
{
      if (fgets_unlocked(buf, 128, f)) {
            /* remove newline */
            char *ptr = strchr(buf, 0x0a);
            if (ptr)
                  *ptr = 0;
            return buf;
      }
      return NULL;
}

static int nv_split(char *buf, struct nv_pair *nv)
{
      /* Get the name part */
      char *ptr;

      nv->name = NULL;
      nv->value = NULL;
      nv->option = NULL;
      ptr = strtok(buf, " ");
      if (ptr == NULL)
            return 0; /* If there's nothing, go to next line */
      if (ptr[0] == '#')
            return 0; /* If there's a comment, go to next line */
      nv->name = ptr;

      /* Check for a '=' */
      ptr = strtok(NULL, " ");
      if (ptr == NULL)
            return 1;
      if (strcmp(ptr, "=") != 0)
            return 2;

      /* get the value */
      ptr = strtok(NULL, " ");
      if (ptr == NULL)
            return 1;
      nv->value = ptr;

      /* See if there's an option */
      ptr = strtok(NULL, " ");
      if (ptr) {
            nv->option = ptr;

            /* Make sure there's nothing else */
            ptr = strtok(NULL, " ");
            if (ptr)
                  return 1;
      }

      /* Everything is OK */
      return 0;
}

static const struct kw_pair *kw_lookup(const char *val)
{
      int i = 0;
      while (keywords[i].name != NULL) {
            if (strcasecmp(keywords[i].name, val) == 0)
                  break;
            i++;
      }
      return &keywords[i];
}
 
static int num_parser(struct nv_pair *nv, int line, 
            struct ids_conf *config)
{
      const char *ptr = nv->value;
      unsigned long i;

      syslog(LOG_DEBUG, "num_parser called with: %s", nv->value);

      /* check that all chars are numbers */
      for (i=0; ptr[i]; i++) {
            if (!isdigit(ptr[i])) {
                  syslog(LOG_ERR, 
                        "Value %s should only be numbers - line %d",
                        nv->value, line);
                  return 1;
            }
      }

      /* convert to unsigned long */
      errno = 0;
      i = strtoul(nv->value, NULL, 10);
      if (errno) {
            syslog(LOG_ERR, 
                  "Error converting string to a number (%s) - line %d",
                  strerror(errno), line);
            return 1;
      }
      if (i > 99) {
            syslog(LOG_ERR, "num must be 99 or less");
            return 1;
      }
      config->num = i;
      return 0;
}

static int group_parser(struct nv_pair *nv, int line, 
            struct ids_conf *config)
{
      gid_t gid = 0;
      
      syslog(LOG_DEBUG, "group_parser called with: %s",
                                          nv->value);
      if (isdigit(nv->value[0])) {
            errno = 0;
            gid = strtoul(nv->value,NULL,10);
            if (errno) {
                  syslog(LOG_ERR,
                "Numeric group ID conversion error (%s) for %s - line %d\n",
                        strerror(errno), nv->value, line);
                  return 1;
            }
      } else {
            struct group *gr ;

            gr = getgrnam(nv->value);
            if (gr == NULL) {
                  syslog(LOG_ERR,
                   "Group ID is non-numeric and unknown (%s) - line %d\n",
                        nv->value, line);
                  return 1;
            }
            gid = gr->gr_gid;
      }
      config->group = gid;
      return 0;
}

/*
 * This function is where we do the integrated check of the audit config
 * options. At this point, all fields have been read. Returns 0 if no
 * problems and 1 if problems detected.
 */
static int sanity_check(struct ids_conf *config)
{
      /* Error checking */
      /* Warnings */
      return 0;
}

const char *audit_lookup_format(int fmt)
{
      int i;

      for (i=0; log_formats[i].name != NULL; i++) {
                if (log_formats[i].option == fmt)
                  return log_formats[i].name;
      }
      return NULL;
}

void free_config(struct ids_conf *config)
{
}


Generated by  Doxygen 1.6.0   Back to index