OpenDNSSEC-signer  1.4.9
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 "shared/allocator.h"
36 #include "shared/file.h"
37 #include "shared/log.h"
38 #include "shared/status.h"
39 
40 #include <errno.h>
41 #include <stdio.h>
42 #include <string.h>
43 
44 static const char* conf_str = "config";
45 
46 
52 engine_config(allocator_type* allocator, const char* cfgfile,
53  int cmdline_verbosity)
54 {
55  engineconfig_type* ecfg;
56  const char* rngfile = ODS_SE_RNGDIR "/conf.rng";
57  FILE* cfgfd = NULL;
58 
59  if (!allocator || !cfgfile) {
60  return NULL;
61  }
62  /* check syntax (slows down parsing configuration file) */
63  if (parse_file_check(cfgfile, rngfile) != ODS_STATUS_OK) {
64  ods_log_error("[%s] unable to create config: parse error in %s",
65  conf_str, cfgfile);
66  return NULL;
67  }
68  /* open cfgfile */
69  cfgfd = ods_fopen(cfgfile, NULL, "r");
70  if (cfgfd) {
71  ods_log_verbose("[%s] read cfgfile: %s", conf_str, cfgfile);
72  /* create config */
73  ecfg = (engineconfig_type*) allocator_alloc(allocator,
74  sizeof(engineconfig_type));
75  if (!ecfg) {
76  ods_log_error("[%s] unable to create config: allocator_alloc() "
77  "failed", conf_str);
78  ods_fclose(cfgfd);
79  return NULL;
80  }
81  ecfg->allocator = allocator;
82  /* get values */
83  ecfg->cfg_filename = allocator_strdup(allocator, cfgfile);
85  cfgfile);
86  ecfg->log_filename = parse_conf_log_filename(allocator, cfgfile);
87  ecfg->pid_filename = parse_conf_pid_filename(allocator, cfgfile);
88  ecfg->notify_command = parse_conf_notify_command(allocator, cfgfile);
90  cfgfile);
91  ecfg->working_dir = parse_conf_working_dir(allocator, cfgfile);
92  ecfg->username = parse_conf_username(allocator, cfgfile);
93  ecfg->group = parse_conf_group(allocator, cfgfile);
94  ecfg->chroot = parse_conf_chroot(allocator, cfgfile);
95  ecfg->use_syslog = parse_conf_use_syslog(cfgfile);
98  /* If any verbosity has been specified at cmd line we will use that */
99  if (cmdline_verbosity > 0) {
100  ecfg->verbosity = cmdline_verbosity;
101  }
102  else {
103  ecfg->verbosity = parse_conf_verbosity(cfgfile);
104  }
105  ecfg->interfaces = parse_conf_listener(allocator, cfgfile);
106  /* done */
107  ods_fclose(cfgfd);
108  return ecfg;
109  }
110  ods_log_error("[%s] unable to create config: failed to open file %s",
111  conf_str, cfgfile);
112  return NULL;
113 }
114 
115 
122 {
123  if (!config) {
124  ods_log_error("[%s] config-check failed: no config", conf_str);
125  return ODS_STATUS_CFG_ERR;
126  }
127  if (!config->cfg_filename) {
128  ods_log_error("[%s] config-check failed: no config filename",
129  conf_str);
130  return ODS_STATUS_CFG_ERR;
131  }
132  if (!config->zonelist_filename) {
133  ods_log_error("[%s] config-check failed: no zonelist filename",
134  conf_str);
135  return ODS_STATUS_CFG_ERR;
136  }
137  if (!config->clisock_filename) {
138  ods_log_error("[%s] config-check failed: no socket filename",
139  conf_str);
140  return ODS_STATUS_CFG_ERR;
141  }
142  if (!config->interfaces) {
143  ods_log_error("[%s] config-check failed: no listener",
144  conf_str);
145  return ODS_STATUS_CFG_ERR;
146  }
147  /* [TODO] room for more checks here */
148  return ODS_STATUS_OK;
149 }
150 
151 
156 void
158 {
159  if (!out) {
160  return;
161  }
162  fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
163  if (config) {
164  fprintf(out, "<Configuration>\n");
165 
166  /* Common */
167  fprintf(out, "\t<Common>\n");
168  if (config->use_syslog && config->log_filename) {
169  fprintf(out, "\t\t<Logging>\n");
170  fprintf(out, "\t\t\t<Syslog>\n");
171  fprintf(out, "\t\t\t\t<Facility>%s</Facility>\n",
172  config->log_filename);
173  fprintf(out, "\t\t\t</Syslog>\n");
174  fprintf(out, "\t\t</Logging>\n");
175  } else if (config->log_filename) {
176  fprintf(out, "\t\t<Logging>\n");
177  fprintf(out, "\t\t\t<File>\n");
178  fprintf(out, "\t\t\t\t<Filename>%s</Filename>\n",
179  config->log_filename);
180  fprintf(out, "\t\t\t</File>\n");
181  fprintf(out, "\t\t</Logging>\n");
182  }
183  fprintf(out, "\t\t<ZoneListFile>%s</ZoneListFile>\n",
184  config->zonelist_filename);
185  fprintf(out, "\t</Common>\n");
186 
187  /* Signer */
188  fprintf(out, "\t<Signer>\n");
189  if (config->username || config->group || config->chroot) {
190  fprintf(out, "\t\t<Privileges>\n");
191  if (config->username) {
192  fprintf(out, "\t\t<User>%s</User>\n", config->username);
193  }
194  if (config->group) {
195  fprintf(out, "\t\t<Group>%s</Group>\n", config->group);
196  }
197  if (config->chroot) {
198  fprintf(out, "\t\t<Directory>%s</Directory>\n",
199  config->chroot);
200  }
201  fprintf(out, "\t\t</Privileges>\n");
202  }
203  if (config->interfaces) {
204  size_t i = 0;
205  fprintf(out, "\t\t<Listener>\n");
206 
207  for (i=0; i < config->interfaces->count; i++) {
208  fprintf(out, "\t\t\t<Interface>");
209  if (config->interfaces->interfaces[i].address) {
210  fprintf(out, "<Address>%s</Address>",
211  config->interfaces->interfaces[i].address);
212  }
213  if (config->interfaces->interfaces[i].port) {
214  fprintf(out, "<Port>%s</Port>",
215  config->interfaces->interfaces[i].port);
216  }
217  fprintf(out, "<Interface>\n");
218  }
219  fprintf(out, "\t\t</Listener>\n");
220 
221  }
222 
223  fprintf(out, "\t\t<WorkingDirectory>%s</WorkingDirectory>\n",
224  config->working_dir);
225  fprintf(out, "\t\t<WorkerThreads>%i</WorkerThreads>\n",
226  config->num_worker_threads);
227  fprintf(out, "\t\t<SignerThreads>%i</SignerThreads>\n",
228  config->num_signer_threads);
229  if (config->notify_command) {
230  fprintf(out, "\t\t<NotifyCommand>%s</NotifyCommand>\n",
231  config->notify_command);
232  }
233  fprintf(out, "\t</Signer>\n");
234 
235  fprintf(out, "</Configuration>\n");
236 
237  /* make configurable:
238  - pid_filename
239  - clisock_filename
240  */
241  }
242  return;
243 }
244 
245 
250 void
252 {
253  allocator_type* allocator = NULL;
254  if (!config) {
255  return;
256  }
257  allocator = config->allocator;
258  listener_cleanup(config->interfaces);
259  allocator_deallocate(allocator, (void*) config->notify_command);
260  allocator_deallocate(allocator, (void*) config->cfg_filename);
261  allocator_deallocate(allocator, (void*) config->zonelist_filename);
262  allocator_deallocate(allocator, (void*) config->log_filename);
263  allocator_deallocate(allocator, (void*) config->pid_filename);
264  allocator_deallocate(allocator, (void*) config->clisock_filename);
265  allocator_deallocate(allocator, (void*) config->working_dir);
266  allocator_deallocate(allocator, (void*) config->username);
267  allocator_deallocate(allocator, (void*) config->group);
268  allocator_deallocate(allocator, (void*) config->chroot);
269  allocator_deallocate(allocator, (void*) config);
270  return;
271 }
272 
void engine_config_cleanup(engineconfig_type *config)
Definition: cfg.c:251
void engine_config_print(FILE *out, engineconfig_type *config)
Definition: cfg.c:157
int num_signer_threads
Definition: cfg.h:63
int parse_conf_worker_threads(const char *cfgfile)
Definition: confparser.c:490
void listener_cleanup(listener_type *listener)
Definition: listener.c:223
const char * cfg_filename
Definition: cfg.h:51
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:66
const char * zonelist_filename
Definition: cfg.h:52
const char * group
Definition: cfg.h:59
int parse_conf_use_syslog(const char *cfgfile)
Definition: confparser.c:460
const char * parse_conf_zonelist_filename(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:293
enum ods_enum_status ods_status
Definition: status.h:90
ods_status parse_file_check(const char *cfgfile, const char *rngfile)
Definition: confparser.c:53
const char * parse_conf_log_filename(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:310
void ods_log_error(const char *format,...)
Definition: log.c:334
FILE * ods_fopen(const char *file, const char *dir, const char *mode)
Definition: file.c:190
listener_type * parse_conf_listener(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:142
allocator_type * allocator
Definition: cfg.h:49
const char * log_filename
Definition: cfg.h:53
const char * clisock_filename
Definition: cfg.h:56
engineconfig_type * engine_config(allocator_type *allocator, const char *cfgfile, int cmdline_verbosity)
Definition: cfg.c:52
const char * parse_conf_group(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:422
int parse_conf_signer_threads(const char *cfgfile)
Definition: confparser.c:507
int num_worker_threads
Definition: cfg.h:62
const char * parse_conf_chroot(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:439
size_t count
Definition: listener.h:84
const char * notify_command
Definition: cfg.h:55
const char * parse_conf_clisock_filename(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:366
char * allocator_strdup(allocator_type *allocator, const char *string)
Definition: allocator.c:121
char * address
Definition: listener.h:71
const char * working_dir
Definition: cfg.h:57
int use_syslog
Definition: cfg.h:61
void ods_log_verbose(const char *format,...)
Definition: log.c:286
ods_status engine_config_check(engineconfig_type *config)
Definition: cfg.c:121
void ods_fclose(FILE *fd)
Definition: file.c:250
const char * parse_conf_notify_command(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:349
const char * username
Definition: cfg.h:58
listener_type * interfaces
Definition: cfg.h:50
interface_type * interfaces
Definition: listener.h:83
const char * parse_conf_working_dir(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:385
int parse_conf_verbosity(const char *cfgfile)
Definition: confparser.c:473
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:135
const char * parse_conf_username(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:405
const char * parse_conf_pid_filename(allocator_type *allocator, const char *cfgfile)
Definition: confparser.c:330
const char * pid_filename
Definition: cfg.h:54
const char * chroot
Definition: cfg.h:60