OpenDNSSEC-enforcer  2.0.3
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 
43 static const char* conf_str = "config";
44 
48 static const char *
49 strdup_or_null(const char *s)
50 {
51  return s?strdup(s):s;
52 }
53 
59 engine_config(const char* cfgfile,
60  int cmdline_verbosity, engineconfig_type* oldcfg)
61 {
62  engineconfig_type* ecfg;
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);
129  ecfg->manual_keygen = parse_conf_manual_keygen(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);
136 
137  /* done */
138  ods_fclose(cfgfd);
139  return ecfg;
140  }
141 
142  ods_log_error("[%s] failed to read: unable to open file %s", conf_str,
143  cfgfile);
144  return NULL;
145 }
146 
147 
152 ods_status
154 {
155  if (!config) {
156  ods_log_error("[%s] check failed: config does not exist", conf_str);
157  return ODS_STATUS_CFG_ERR;
158  }
159  if (!config->policy_filename) {
160  ods_log_error("[%s] check failed: no policy filename", conf_str);
161  return ODS_STATUS_CFG_ERR;
162  }
163  if (!config->zonelist_filename) {
164  ods_log_error("[%s] check failed: no zonelist filename", conf_str);
165  return ODS_STATUS_CFG_ERR;
166  }
167  if (!config->clisock_filename) {
168  ods_log_error("[%s] check failed: no socket filename", conf_str);
169  return ODS_STATUS_CFG_ERR;
170  }
171  if (!config->datastore) {
172  ods_log_error("[%s] check failed: no datastore", conf_str);
173  return ODS_STATUS_CFG_ERR;
174  }
175 
176  /* [TODO] room for more checks here */
177 
178  return ODS_STATUS_OK;
179 }
180 
181 
186 void
188 {
189  if (!out) {
190  return;
191  }
192  ods_log_assert(out);
193 
194  fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
195  if (config) {
196  ods_log_assert(config);
197 
198  fprintf(out, "<Configuration>\n");
199 
200  /* Common */
201  fprintf(out, "\t<Common>\n");
202  if (config->use_syslog && config->log_filename) {
203  fprintf(out, "\t\t<Logging>\n");
204  fprintf(out, "\t\t\t<Syslog>\n");
205  fprintf(out, "\t\t\t\t<Facility>%s</Facility>\n",
206  config->log_filename);
207  fprintf(out, "\t\t\t</Syslog>\n");
208  fprintf(out, "\t\t</Logging>\n");
209  } else if (config->log_filename) {
210  fprintf(out, "\t\t<Logging>\n");
211  fprintf(out, "\t\t\t<File>\n");
212  fprintf(out, "\t\t\t\t<Filename>%s</Filename>\n",
213  config->log_filename);
214  fprintf(out, "\t\t\t</File>\n");
215  fprintf(out, "\t\t</Logging>\n");
216  }
217 
218  fprintf(out, "\t\t<PolicyFile>%s</PolicyFile>\n",
219  config->policy_filename);
220  fprintf(out, "\t\t<ZoneListFile>%s</ZoneListFile>\n",
221  config->zonelist_filename);
222  if (config->zonefetch_filename) {
223  fprintf(out, "\t\t<ZoneFetchFile>%s</ZoneFetchFile>\n",
224  config->zonefetch_filename);
225  }
226 
227  fprintf(out, "\t</Common>\n");
228 
229  /* Enforcer */
230  fprintf(out, "\t<Enforcer>\n");
231  if (config->username || config->group || config->chroot) {
232  fprintf(out, "\t\t<Privileges>\n");
233  if (config->username) {
234  fprintf(out, "\t\t<User>%s</User>\n", config->username);
235  }
236  if (config->group) {
237  fprintf(out, "\t\t<Group>%s</Group>\n", config->group);
238  }
239  if (config->chroot) {
240  fprintf(out, "\t\t<Directory>%s</Directory>\n",
241  config->chroot);
242  }
243  fprintf(out, "\t\t</Privileges>\n");
244  }
245  fprintf(out, "\t\t<WorkingDirectory>%s</WorkingDirectory>\n",
246  config->working_dir);
247  fprintf(out, "\t\t<WorkerThreads>%i</WorkerThreads>\n",
248  config->num_worker_threads);
249  if (config->manual_keygen) {
250  fprintf(out, "\t\t<ManualKeyGeneration/>\n");
251  }
252  if (config->delegation_signer_submit_command) {
253  fprintf(out, "\t\t<DelegationSignerSubmitCommand>%s</DelegationSignerSubmitCommand>\n",
255  }
256  if (config->delegation_signer_retract_command) {
257  fprintf(out, "\t\t<DelegationSignerRetractCommand>%s</DelegationSignerRetractCommand>\n",
259  }
260  fprintf(out, "\t</Enforcer>\n");
261 
262  fprintf(out, "</Configuration>\n");
263 
264  /* make configurable:
265  - pid_filename
266  - clisock_filename
267  */
268  }
269 }
270 
275 void
277 {
278  if (!config) {
279  return;
280  }
281  free((void*) config->cfg_filename);
282  free((void*) config->policy_filename);
283  free((void*) config->zonelist_filename);
284  free((void*) config->zonefetch_filename);
285  free((void*) config->log_filename);
286  free((void*) config->pid_filename);
287  free((void*) config->delegation_signer_submit_command);
288  free((void*) config->delegation_signer_retract_command);
289  free((void*) config->clisock_filename);
290  free((void*) config->working_dir);
291  free((void*) config->username);
292  free((void*) config->group);
293  free((void*) config->chroot);
294  free((void*) config->datastore);
295  free((void*) config->db_host);
296  free((void*) config->db_username);
297  free((void*) config->db_password);
298  hsm_repository_free(config->repositories);
299  config->repositories = NULL;
300  free(config);
301 }
302 
const char * delegation_signer_submit_command
Definition: cfg.h:61
void engine_config_cleanup(engineconfig_type *config)
Definition: cfg.c:276
void engine_config_print(FILE *out, engineconfig_type *config)
Definition: cfg.c:187
int parse_conf_worker_threads(const char *cfgfile)
Definition: confparser.c:630
const char * cfg_filename
Definition: cfg.h:55
const char * datastore
Definition: cfg.h:68
int parse_conf_db_port(const char *cfgfile)
Definition: confparser.c:659
time_t parse_conf_automatic_keygen_period(const char *cfgfile)
Definition: confparser.c:699
const char * parse_conf_db_host(const char *cfgfile)
Definition: confparser.c:548
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition: confparser.c:205
const char * zonelist_filename
Definition: cfg.h:57
engineconfig_type * engine_config(const char *cfgfile, int cmdline_verbosity, engineconfig_type *oldcfg)
Definition: cfg.c:59
const char * policy_filename
Definition: cfg.h:56
const char * db_host
Definition: cfg.h:69
time_t automatic_keygen_duration
Definition: cfg.h:77
const char * group
Definition: cfg.h:66
int parse_conf_use_syslog(const char *cfgfile)
Definition: confparser.c:600
const char * parse_conf_clisock_filename(const char *cfgfile)
Definition: confparser.c:431
ods_status parse_file_check(const char *cfgfile, const char *rngfile)
Definition: confparser.c:53
void ods_log_error(const char *format,...)
Definition: log.c:69
const char * parse_conf_delegation_signer_submit_command(const char *cfgfile)
Definition: confparser.c:399
const char * delegation_signer_retract_command
Definition: cfg.h:62
const char * parse_conf_working_dir(const char *cfgfile)
Definition: confparser.c:454
int parse_conf_manual_keygen(const char *cfgfile)
Definition: confparser.c:646
const char * db_password
Definition: cfg.h:71
const char * log_filename
Definition: cfg.h:59
const char * clisock_filename
Definition: cfg.h:63
const char * parse_conf_policy_filename(const char *cfgfile)
Definition: confparser.c:310
const char * parse_conf_group(const char *cfgfile)
Definition: confparser.c:490
int num_worker_threads
Definition: cfg.h:73
const char * parse_conf_log_filename(const char *cfgfile)
Definition: confparser.c:360
hsm_repository_t * repositories
Definition: cfg.h:78
const char * zonefetch_filename
Definition: cfg.h:58
const char * db_username
Definition: cfg.h:70
const char * parse_conf_db_password(const char *cfgfile)
Definition: confparser.c:580
const char * parse_conf_chroot(const char *cfgfile)
Definition: confparser.c:507
const char * parse_conf_datastore(const char *cfgfile)
Definition: confparser.c:523
engineconfig_database_type_t parse_conf_db_type(const char *cfgfile)
Definition: confparser.c:674
int manual_keygen
Definition: cfg.h:74
const char * working_dir
Definition: cfg.h:64
int use_syslog
Definition: cfg.h:72
void ods_log_verbose(const char *format,...)
Definition: log.c:48
ods_status engine_config_check(engineconfig_type *config)
Definition: cfg.c:153
const char * parse_conf_username(const char *cfgfile)
Definition: confparser.c:473
const char * username
Definition: cfg.h:65
const char * parse_conf_zonefetch_filename(const char *cfgfile)
Definition: confparser.c:343
int parse_conf_verbosity(const char *cfgfile)
Definition: confparser.c:613
const char * parse_conf_zonelist_filename(const char *cfgfile)
Definition: confparser.c:326
const char * parse_conf_pid_filename(const char *cfgfile)
Definition: confparser.c:380
engineconfig_database_type_t db_type
Definition: cfg.h:79
const char * parse_conf_delegation_signer_retract_command(const char *cfgfile)
Definition: confparser.c:415
const char * pid_filename
Definition: cfg.h:60
const char * chroot
Definition: cfg.h:67
const char * parse_conf_db_username(const char *cfgfile)
Definition: confparser.c:564