OpenDNSSEC-enforcer 2.1.13
cfg.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
32#include "config.h"
33#include "daemon/cfg.h"
34#include "parser/confparser.h"
35#include "file.h"
36#include "log.h"
37#include "status.h"
38
39#include <errno.h>
40#include <stdio.h>
41#include <string.h>
42
43static const char* conf_str = "config";
44
48static const char *
49strdup_or_null(const char *s)
50{
51 return s?strdup(s):s;
52}
53
59engine_config(const char* cfgfile,
60 int cmdline_verbosity, engineconfig_type* oldcfg)
61{
63 const char* rngfile = ODS_SE_RNGDIR "/conf.rng";
64 FILE* cfgfd = NULL;
65
66 if (!cfgfile || cfgfile[0] == 0) {
67 ods_log_error("[%s] failed to read: no filename given", conf_str);
68 return NULL;
69 }
70 ods_log_verbose("[%s] read cfgfile: %s", conf_str, cfgfile);
71
72 /* check syntax (slows down parsing configuration file) */
73 if (parse_file_check(cfgfile, rngfile) != ODS_STATUS_OK) {
74 ods_log_error("[%s] failed to read: unable to parse file %s",
75 conf_str, cfgfile);
76 return NULL;
77 }
78
79 /* open cfgfile */
80 cfgfd = ods_fopen(cfgfile, NULL, "r");
81 if (cfgfd) {
82 ecfg = malloc(sizeof(engineconfig_type));
83 if (!ecfg) {
84 ods_log_error("[%s] failed to read: malloc failed", conf_str);
85 ods_fclose(cfgfd);
86 return NULL;
87 }
88 if (oldcfg) {
89 /* This is a reload */
90 ecfg->cfg_filename = strdup(oldcfg->cfg_filename);
91 ecfg->clisock_filename = strdup(oldcfg->clisock_filename);
92 ecfg->working_dir = strdup(oldcfg->working_dir);
93 ecfg->username = strdup_or_null(oldcfg->username);
94 ecfg->group = strdup_or_null(oldcfg->group);
95 ecfg->chroot = strdup_or_null(oldcfg->chroot);
96 ecfg->pid_filename = strdup(oldcfg->pid_filename);
97 ecfg->datastore = strdup(oldcfg->datastore);
98 ecfg->db_host = strdup_or_null(oldcfg->db_host);
99 ecfg->db_username = strdup_or_null(oldcfg->db_username);
100 ecfg->db_password = strdup_or_null(oldcfg->db_password);
101 ecfg->db_port = oldcfg->db_port;
102 ecfg->db_type = oldcfg->db_type;
103 } else {
104 ecfg->cfg_filename = strdup(cfgfile);
106 ecfg->working_dir = parse_conf_working_dir(cfgfile);
107 ecfg->username = parse_conf_username(cfgfile);
108 ecfg->group = parse_conf_group(cfgfile);
109 ecfg->chroot = parse_conf_chroot(cfgfile);
110 ecfg->pid_filename = parse_conf_pid_filename(cfgfile);
111 ecfg->datastore = parse_conf_datastore(cfgfile);
112 ecfg->db_host = parse_conf_db_host(cfgfile);
113 ecfg->db_username = parse_conf_db_username(cfgfile);
114 ecfg->db_password = parse_conf_db_password(cfgfile);
115 ecfg->db_port = parse_conf_db_port(cfgfile);
116 ecfg->db_type = parse_conf_db_type(cfgfile);
117 }
118 /* get values */
122 ecfg->log_filename = parse_conf_log_filename(cfgfile);
127 ecfg->use_syslog = parse_conf_use_syslog(cfgfile);
130 ecfg->repositories = parse_conf_repositories(cfgfile);
131 /* If any verbosity has been specified at cmd line we will use that */
132 ecfg->verbosity = cmdline_verbosity > 0 ?
133 cmdline_verbosity : parse_conf_verbosity(cfgfile);
138
139 /* done */
140 ods_fclose(cfgfd);
141 return ecfg;
142 }
143
144 ods_log_error("[%s] failed to read: unable to open file %s", conf_str,
145 cfgfile);
146 return NULL;
147}
148
149
154ods_status
156{
157 if (!config) {
158 ods_log_error("[%s] check failed: config does not exist", conf_str);
159 return ODS_STATUS_CFG_ERR;
160 }
161 if (!config->policy_filename) {
162 ods_log_error("[%s] check failed: no policy filename", conf_str);
163 return ODS_STATUS_CFG_ERR;
164 }
165 if (!config->zonelist_filename) {
166 ods_log_error("[%s] check failed: no zonelist filename", conf_str);
167 return ODS_STATUS_CFG_ERR;
168 }
169 if (!config->clisock_filename) {
170 ods_log_error("[%s] check failed: no socket filename", conf_str);
171 return ODS_STATUS_CFG_ERR;
172 }
173 if (!config->datastore) {
174 ods_log_error("[%s] check failed: no datastore", conf_str);
175 return ODS_STATUS_CFG_ERR;
176 }
177
178 /* [TODO] room for more checks here */
179
180 return ODS_STATUS_OK;
181}
182
183
188void
190{
191 if (!out) {
192 return;
193 }
194 ods_log_assert(out);
195
196 fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
197 if (config) {
198 ods_log_assert(config);
199
200 fprintf(out, "<Configuration>\n");
201
202 /* Common */
203 fprintf(out, "\t<Common>\n");
204 if (config->use_syslog && config->log_filename) {
205 fprintf(out, "\t\t<Logging>\n");
206 fprintf(out, "\t\t\t<Syslog>\n");
207 fprintf(out, "\t\t\t\t<Facility>%s</Facility>\n",
208 config->log_filename);
209 fprintf(out, "\t\t\t</Syslog>\n");
210 fprintf(out, "\t\t</Logging>\n");
211 } else if (config->log_filename) {
212 fprintf(out, "\t\t<Logging>\n");
213 fprintf(out, "\t\t\t<File>\n");
214 fprintf(out, "\t\t\t\t<Filename>%s</Filename>\n",
215 config->log_filename);
216 fprintf(out, "\t\t\t</File>\n");
217 fprintf(out, "\t\t</Logging>\n");
218 }
219
220 fprintf(out, "\t\t<PolicyFile>%s</PolicyFile>\n",
221 config->policy_filename);
222 fprintf(out, "\t\t<ZoneListFile>%s</ZoneListFile>\n",
223 config->zonelist_filename);
224 if (config->zonefetch_filename) {
225 fprintf(out, "\t\t<ZoneFetchFile>%s</ZoneFetchFile>\n",
226 config->zonefetch_filename);
227 }
228
229 fprintf(out, "\t</Common>\n");
230
231 /* Enforcer */
232 fprintf(out, "\t<Enforcer>\n");
233 if (config->username || config->group || config->chroot) {
234 fprintf(out, "\t\t<Privileges>\n");
235 if (config->username) {
236 fprintf(out, "\t\t<User>%s</User>\n", config->username);
237 }
238 if (config->group) {
239 fprintf(out, "\t\t<Group>%s</Group>\n", config->group);
240 }
241 if (config->chroot) {
242 fprintf(out, "\t\t<Directory>%s</Directory>\n",
243 config->chroot);
244 }
245 fprintf(out, "\t\t</Privileges>\n");
246 }
247 fprintf(out, "\t\t<WorkingDirectory>%s</WorkingDirectory>\n",
248 config->working_dir);
249 fprintf(out, "\t\t<WorkerThreads>%i</WorkerThreads>\n",
250 config->num_worker_threads);
251 if (config->manual_keygen) {
252 fprintf(out, "\t\t<ManualKeyGeneration/>\n");
253 }
255 fprintf(out, "\t\t<DelegationSignerSubmitCommand>%s</DelegationSignerSubmitCommand>\n",
257 }
259 fprintf(out, "\t\t<DelegationSignerRetractCommand>%s</DelegationSignerRetractCommand>\n",
261 }
262 fprintf(out, "\t</Enforcer>\n");
263
264 fprintf(out, "</Configuration>\n");
265
266 /* make configurable:
267 - pid_filename
268 - clisock_filename
269 */
270 }
271}
272
277void
279{
280 if (!config) {
281 return;
282 }
283 free((void*) config->cfg_filename);
284 free((void*) config->policy_filename);
285 free((void*) config->zonelist_filename);
286 free((void*) config->zonefetch_filename);
287 free((void*) config->log_filename);
288 free((void*) config->pid_filename);
289 free((void*) config->delegation_signer_submit_command);
290 free((void*) config->delegation_signer_retract_command);
291 free((void*) config->clisock_filename);
292 free((void*) config->working_dir);
293 free((void*) config->username);
294 free((void*) config->group);
295 free((void*) config->chroot);
296 free((void*) config->datastore);
297 free((void*) config->db_host);
298 free((void*) config->db_username);
299 free((void*) config->db_password);
300 hsm_repository_free(config->repositories);
301 config->repositories = NULL;
302 free(config);
303}
304
engineconfig_type * engine_config(const char *cfgfile, int cmdline_verbosity, engineconfig_type *oldcfg)
Definition cfg.c:59
void engine_config_cleanup(engineconfig_type *config)
Definition cfg.c:278
void engine_config_print(FILE *out, engineconfig_type *config)
Definition cfg.c:189
ods_status engine_config_check(engineconfig_type *config)
Definition cfg.c:155
const char * parse_conf_db_username(const char *cfgfile)
Definition confparser.c:568
engineconfig_database_type_t parse_conf_db_type(const char *cfgfile)
Definition confparser.c:678
const char * parse_conf_clisock_filename(const char *cfgfile)
Definition confparser.c:435
const char * parse_conf_delegation_signer_retract_command(const char *cfgfile)
Definition confparser.c:419
const char * parse_conf_db_host(const char *cfgfile)
Definition confparser.c:552
const char * parse_conf_policy_filename(const char *cfgfile)
Definition confparser.c:314
const char * parse_conf_delegation_signer_submit_command(const char *cfgfile)
Definition confparser.c:403
int parse_conf_db_port(const char *cfgfile)
Definition confparser.c:663
const char * parse_conf_zonelist_filename(const char *cfgfile)
Definition confparser.c:330
const char * parse_conf_working_dir(const char *cfgfile)
Definition confparser.c:458
const char * parse_conf_log_filename(const char *cfgfile)
Definition confparser.c:364
int parse_conf_worker_threads(const char *cfgfile)
Definition confparser.c:634
const char * parse_conf_username(const char *cfgfile)
Definition confparser.c:477
time_t parse_conf_rollover_notification(const char *cfgfile)
Definition confparser.c:724
const char * parse_conf_zonefetch_filename(const char *cfgfile)
Definition confparser.c:347
const char * parse_conf_pid_filename(const char *cfgfile)
Definition confparser.c:384
int parse_conf_use_syslog(const char *cfgfile)
Definition confparser.c:604
const char * parse_conf_db_password(const char *cfgfile)
Definition confparser.c:584
const char * parse_conf_chroot(const char *cfgfile)
Definition confparser.c:511
const char * parse_conf_datastore(const char *cfgfile)
Definition confparser.c:527
const char * parse_conf_group(const char *cfgfile)
Definition confparser.c:494
time_t parse_conf_automatic_keygen_period(const char *cfgfile)
Definition confparser.c:703
int parse_conf_manual_keygen(const char *cfgfile)
Definition confparser.c:650
int parse_conf_verbosity(const char *cfgfile)
Definition confparser.c:617
ods_status parse_file_check(const char *cfgfile, const char *rngfile)
Definition confparser.c:53
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition confparser.c:205
const char * chroot
Definition cfg.h:67
time_t rollover_notification
Definition cfg.h:78
const char * policy_filename
Definition cfg.h:56
const char * datastore
Definition cfg.h:68
const char * cfg_filename
Definition cfg.h:55
const char * zonefetch_filename
Definition cfg.h:58
const char * db_password
Definition cfg.h:71
const char * delegation_signer_submit_command
Definition cfg.h:61
const char * group
Definition cfg.h:66
engineconfig_database_type_t db_type
Definition cfg.h:80
const char * delegation_signer_retract_command
Definition cfg.h:62
const char * clisock_filename
Definition cfg.h:63
const char * working_dir
Definition cfg.h:64
hsm_repository_t * repositories
Definition cfg.h:79
int num_worker_threads
Definition cfg.h:73
const char * pid_filename
Definition cfg.h:60
time_t automatic_keygen_duration
Definition cfg.h:77
const char * zonelist_filename
Definition cfg.h:57
const char * log_filename
Definition cfg.h:59
int manual_keygen
Definition cfg.h:74
const char * db_username
Definition cfg.h:70
const char * db_host
Definition cfg.h:69
const char * username
Definition cfg.h:65