cfg.c
Go to the documentation of this file.
/*
* Copyright (c) 2009 NLNet Labs. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "daemon/cfg.h"
#include "parser/confparser.h"
#include "file.h"
#include "log.h"
#include "status.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
static const char* conf_str = "config";
static const char *
strdup_or_null(const char *s)
{
return s?strdup(s):s;
}
engineconfig_type*
engine_config(const char* cfgfile,
int cmdline_verbosity, engineconfig_type* oldcfg)
{
engineconfig_type* ecfg;
const char* rngfile = ODS_SE_RNGDIR "/conf.rng";
FILE* cfgfd = NULL;
if (!cfgfile || cfgfile[0] == 0) {
ods_log_error("[%s] failed to read: no filename given", conf_str);
return NULL;
}
ods_log_verbose("[%s] read cfgfile: %s", conf_str, cfgfile);
/* check syntax (slows down parsing configuration file) */
if (parse_file_check(cfgfile, rngfile) != ODS_STATUS_OK) {
ods_log_error("[%s] failed to read: unable to parse file %s",
conf_str, cfgfile);
return NULL;
}
/* open cfgfile */
cfgfd = ods_fopen(cfgfile, NULL, "r");
if (cfgfd) {
ecfg = malloc(sizeof(engineconfig_type));
if (!ecfg) {
ods_log_error("[%s] failed to read: malloc failed", conf_str);
ods_fclose(cfgfd);
return NULL;
}
if (oldcfg) {
/* This is a reload */
ecfg->cfg_filename = strdup(oldcfg->cfg_filename);
ecfg->clisock_filename = strdup(oldcfg->clisock_filename);
ecfg->working_dir = strdup(oldcfg->working_dir);
ecfg->username = strdup_or_null(oldcfg->username);
ecfg->group = strdup_or_null(oldcfg->group);
ecfg->chroot = strdup_or_null(oldcfg->chroot);
ecfg->pid_filename = strdup(oldcfg->pid_filename);
ecfg->datastore = strdup(oldcfg->datastore);
ecfg->db_host = strdup_or_null(oldcfg->db_host);
ecfg->db_username = strdup_or_null(oldcfg->db_username);
ecfg->db_password = strdup_or_null(oldcfg->db_password);
ecfg->db_port = oldcfg->db_port;
ecfg->db_type = oldcfg->db_type;
} else {
ecfg->cfg_filename = strdup(cfgfile);
ecfg->clisock_filename = parse_conf_clisock_filename(cfgfile);
ecfg->working_dir = parse_conf_working_dir(cfgfile);
ecfg->username = parse_conf_username(cfgfile);
ecfg->group = parse_conf_group(cfgfile);
ecfg->chroot = parse_conf_chroot(cfgfile);
ecfg->pid_filename = parse_conf_pid_filename(cfgfile);
ecfg->datastore = parse_conf_datastore(cfgfile);
ecfg->db_host = parse_conf_db_host(cfgfile);
ecfg->db_username = parse_conf_db_username(cfgfile);
ecfg->db_password = parse_conf_db_password(cfgfile);
ecfg->db_port = parse_conf_db_port(cfgfile);
ecfg->db_type = parse_conf_db_type(cfgfile);
}
/* get values */
ecfg->policy_filename = parse_conf_policy_filename(cfgfile);
ecfg->zonelist_filename = parse_conf_zonelist_filename(cfgfile);
ecfg->zonefetch_filename = parse_conf_zonefetch_filename(cfgfile);
ecfg->log_filename = parse_conf_log_filename(cfgfile);
ecfg->delegation_signer_submit_command =
parse_conf_delegation_signer_submit_command(cfgfile);
ecfg->delegation_signer_retract_command =
parse_conf_delegation_signer_retract_command(cfgfile);
ecfg->use_syslog = parse_conf_use_syslog(cfgfile);
ecfg->num_worker_threads = parse_conf_worker_threads(cfgfile);
ecfg->manual_keygen = parse_conf_manual_keygen(cfgfile);
ecfg->repositories = parse_conf_repositories(cfgfile);
/* If any verbosity has been specified at cmd line we will use that */
ecfg->verbosity = cmdline_verbosity > 0 ?
cmdline_verbosity : parse_conf_verbosity(cfgfile);
ecfg->automatic_keygen_duration =
parse_conf_automatic_keygen_period(cfgfile);
ecfg->rollover_notification =
parse_conf_rollover_notification(cfgfile);
/* done */
ods_fclose(cfgfd);
return ecfg;
}
ods_log_error("[%s] failed to read: unable to open file %s", conf_str,
cfgfile);
return NULL;
}
ods_status
engine_config_check(engineconfig_type* config)
{
if (!config) {
ods_log_error("[%s] check failed: config does not exist", conf_str);
return ODS_STATUS_CFG_ERR;
}
if (!config->policy_filename) {
ods_log_error("[%s] check failed: no policy filename", conf_str);
return ODS_STATUS_CFG_ERR;
}
if (!config->zonelist_filename) {
ods_log_error("[%s] check failed: no zonelist filename", conf_str);
return ODS_STATUS_CFG_ERR;
}
if (!config->clisock_filename) {
ods_log_error("[%s] check failed: no socket filename", conf_str);
return ODS_STATUS_CFG_ERR;
}
if (!config->datastore) {
ods_log_error("[%s] check failed: no datastore", conf_str);
return ODS_STATUS_CFG_ERR;
}
/* [TODO] room for more checks here */
return ODS_STATUS_OK;
}
void
engine_config_print(FILE* out, engineconfig_type* config)
{
if (!out) {
return;
}
ods_log_assert(out);
fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if (config) {
ods_log_assert(config);
fprintf(out, "<Configuration>\n");
/* Common */
fprintf(out, "\t<Common>\n");
if (config->use_syslog && config->log_filename) {
fprintf(out, "\t\t<Logging>\n");
fprintf(out, "\t\t\t<Syslog>\n");
fprintf(out, "\t\t\t\t<Facility>%s</Facility>\n",
config->log_filename);
fprintf(out, "\t\t\t</Syslog>\n");
fprintf(out, "\t\t</Logging>\n");
} else if (config->log_filename) {
fprintf(out, "\t\t<Logging>\n");
fprintf(out, "\t\t\t<File>\n");
fprintf(out, "\t\t\t\t<Filename>%s</Filename>\n",
config->log_filename);
fprintf(out, "\t\t\t</File>\n");
fprintf(out, "\t\t</Logging>\n");
}
fprintf(out, "\t\t<PolicyFile>%s</PolicyFile>\n",
config->policy_filename);
fprintf(out, "\t\t<ZoneListFile>%s</ZoneListFile>\n",
config->zonelist_filename);
if (config->zonefetch_filename) {
fprintf(out, "\t\t<ZoneFetchFile>%s</ZoneFetchFile>\n",
config->zonefetch_filename);
}
fprintf(out, "\t</Common>\n");
/* Enforcer */
fprintf(out, "\t<Enforcer>\n");
if (config->username || config->group || config->chroot) {
fprintf(out, "\t\t<Privileges>\n");
if (config->username) {
fprintf(out, "\t\t<User>%s</User>\n", config->username);
}
if (config->group) {
fprintf(out, "\t\t<Group>%s</Group>\n", config->group);
}
if (config->chroot) {
fprintf(out, "\t\t<Directory>%s</Directory>\n",
config->chroot);
}
fprintf(out, "\t\t</Privileges>\n");
}
fprintf(out, "\t\t<WorkingDirectory>%s</WorkingDirectory>\n",
config->working_dir);
fprintf(out, "\t\t<WorkerThreads>%i</WorkerThreads>\n",
config->num_worker_threads);
if (config->manual_keygen) {
fprintf(out, "\t\t<ManualKeyGeneration/>\n");
}
if (config->delegation_signer_submit_command) {
fprintf(out, "\t\t<DelegationSignerSubmitCommand>%s</DelegationSignerSubmitCommand>\n",
config->delegation_signer_submit_command);
}
if (config->delegation_signer_retract_command) {
fprintf(out, "\t\t<DelegationSignerRetractCommand>%s</DelegationSignerRetractCommand>\n",
config->delegation_signer_retract_command);
}
fprintf(out, "\t</Enforcer>\n");
fprintf(out, "</Configuration>\n");
/* make configurable:
- pid_filename
- clisock_filename
*/
}
}
void
engine_config_cleanup(engineconfig_type* config)
{
if (!config) {
return;
}
free((void*) config->cfg_filename);
free((void*) config->policy_filename);
free((void*) config->zonelist_filename);
free((void*) config->zonefetch_filename);
free((void*) config->log_filename);
free((void*) config->pid_filename);
free((void*) config->delegation_signer_submit_command);
free((void*) config->delegation_signer_retract_command);
free((void*) config->clisock_filename);
free((void*) config->working_dir);
free((void*) config->username);
free((void*) config->group);
free((void*) config->chroot);
free((void*) config->datastore);
free((void*) config->db_host);
free((void*) config->db_username);
free((void*) config->db_password);
hsm_repository_free(config->repositories);
config->repositories = NULL;
free(config);
}
engine_config
engineconfig_type * engine_config(const char *cfgfile, int cmdline_verbosity, engineconfig_type *oldcfg)
Definition cfg.c:59
engine_config_cleanup
void engine_config_cleanup(engineconfig_type *config)
Definition cfg.c:278
engine_config_print
void engine_config_print(FILE *out, engineconfig_type *config)
Definition cfg.c:189
engine_config_check
ods_status engine_config_check(engineconfig_type *config)
Definition cfg.c:155
cfg.h
parse_conf_db_username
const char * parse_conf_db_username(const char *cfgfile)
Definition confparser.c:568
parse_conf_db_type
engineconfig_database_type_t parse_conf_db_type(const char *cfgfile)
Definition confparser.c:678
parse_conf_clisock_filename
const char * parse_conf_clisock_filename(const char *cfgfile)
Definition confparser.c:435
parse_conf_delegation_signer_retract_command
const char * parse_conf_delegation_signer_retract_command(const char *cfgfile)
Definition confparser.c:419
parse_conf_db_host
const char * parse_conf_db_host(const char *cfgfile)
Definition confparser.c:552
parse_conf_policy_filename
const char * parse_conf_policy_filename(const char *cfgfile)
Definition confparser.c:314
parse_conf_delegation_signer_submit_command
const char * parse_conf_delegation_signer_submit_command(const char *cfgfile)
Definition confparser.c:403
parse_conf_db_port
int parse_conf_db_port(const char *cfgfile)
Definition confparser.c:663
parse_conf_zonelist_filename
const char * parse_conf_zonelist_filename(const char *cfgfile)
Definition confparser.c:330
parse_conf_working_dir
const char * parse_conf_working_dir(const char *cfgfile)
Definition confparser.c:458
parse_conf_log_filename
const char * parse_conf_log_filename(const char *cfgfile)
Definition confparser.c:364
parse_conf_worker_threads
int parse_conf_worker_threads(const char *cfgfile)
Definition confparser.c:634
parse_conf_username
const char * parse_conf_username(const char *cfgfile)
Definition confparser.c:477
parse_conf_rollover_notification
time_t parse_conf_rollover_notification(const char *cfgfile)
Definition confparser.c:724
parse_conf_zonefetch_filename
const char * parse_conf_zonefetch_filename(const char *cfgfile)
Definition confparser.c:347
parse_conf_pid_filename
const char * parse_conf_pid_filename(const char *cfgfile)
Definition confparser.c:384
parse_conf_use_syslog
int parse_conf_use_syslog(const char *cfgfile)
Definition confparser.c:604
parse_conf_db_password
const char * parse_conf_db_password(const char *cfgfile)
Definition confparser.c:584
parse_conf_chroot
const char * parse_conf_chroot(const char *cfgfile)
Definition confparser.c:511
parse_conf_datastore
const char * parse_conf_datastore(const char *cfgfile)
Definition confparser.c:527
parse_conf_group
const char * parse_conf_group(const char *cfgfile)
Definition confparser.c:494
parse_conf_automatic_keygen_period
time_t parse_conf_automatic_keygen_period(const char *cfgfile)
Definition confparser.c:703
parse_conf_manual_keygen
int parse_conf_manual_keygen(const char *cfgfile)
Definition confparser.c:650
parse_conf_verbosity
int parse_conf_verbosity(const char *cfgfile)
Definition confparser.c:617
parse_file_check
ods_status parse_file_check(const char *cfgfile, const char *rngfile)
Definition confparser.c:53
confparser.h
parse_conf_repositories
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition confparser.c:205
engineconfig_struct
Definition cfg.h:54
engineconfig_struct::chroot
const char * chroot
Definition cfg.h:67
engineconfig_struct::rollover_notification
time_t rollover_notification
Definition cfg.h:78
engineconfig_struct::policy_filename
const char * policy_filename
Definition cfg.h:56
engineconfig_struct::datastore
const char * datastore
Definition cfg.h:68
engineconfig_struct::cfg_filename
const char * cfg_filename
Definition cfg.h:55
engineconfig_struct::db_port
int db_port
Definition cfg.h:76
engineconfig_struct::zonefetch_filename
const char * zonefetch_filename
Definition cfg.h:58
engineconfig_struct::db_password
const char * db_password
Definition cfg.h:71
engineconfig_struct::verbosity
int verbosity
Definition cfg.h:75
engineconfig_struct::delegation_signer_submit_command
const char * delegation_signer_submit_command
Definition cfg.h:61
engineconfig_struct::group
const char * group
Definition cfg.h:66
engineconfig_struct::db_type
engineconfig_database_type_t db_type
Definition cfg.h:80
engineconfig_struct::delegation_signer_retract_command
const char * delegation_signer_retract_command
Definition cfg.h:62
engineconfig_struct::clisock_filename
const char * clisock_filename
Definition cfg.h:63
engineconfig_struct::working_dir
const char * working_dir
Definition cfg.h:64
engineconfig_struct::repositories
hsm_repository_t * repositories
Definition cfg.h:79
engineconfig_struct::num_worker_threads
int num_worker_threads
Definition cfg.h:73
engineconfig_struct::use_syslog
int use_syslog
Definition cfg.h:72
engineconfig_struct::pid_filename
const char * pid_filename
Definition cfg.h:60
engineconfig_struct::automatic_keygen_duration
time_t automatic_keygen_duration
Definition cfg.h:77
engineconfig_struct::zonelist_filename
const char * zonelist_filename
Definition cfg.h:57
engineconfig_struct::log_filename
const char * log_filename
Definition cfg.h:59
engineconfig_struct::manual_keygen
int manual_keygen
Definition cfg.h:74
engineconfig_struct::db_username
const char * db_username
Definition cfg.h:70
engineconfig_struct::db_host
const char * db_host
Definition cfg.h:69
engineconfig_struct::username
const char * username
Definition cfg.h:65