OpenDNSSEC-signer 1.2.1

/build/buildd-opendnssec_1.2.1.dfsg-1-ia64-j6OroR/opendnssec-1.2.1.dfsg/signer/src/daemon/cfg.c

Go to the documentation of this file.
00001 /*
00002  * $Id: cfg.c 4294 2011-01-13 19:58:29Z jakob $
00003  *
00004  * Copyright (c) 2009 NLNet Labs. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00021  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00023  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 
00034 #include "config.h"
00035 #include "daemon/cfg.h"
00036 #include "parser/confparser.h"
00037 #include "util/file.h"
00038 #include "util/log.h"
00039 #include "util/se_malloc.h"
00040 
00041 #include <errno.h>
00042 #include <stdio.h> /* fprintf() */
00043 #include <string.h> /* strerror() */
00044 
00045 
00050 engineconfig_type*
00051 engine_config(const char* cfgfile, int cmdline_verbosity)
00052 {
00053     engineconfig_type* ecfg = (engineconfig_type*) se_calloc(1,
00054         sizeof(engineconfig_type));
00055     const char* rngfile = ODS_SE_RNGDIR "/conf.rng";
00056     FILE* cfgfd = NULL;
00057 
00058     se_log_assert(cfgfile);
00059     se_log_verbose("read config file: %s", cfgfile?cfgfile:"(null)");
00060 
00061     /* check syntax (slows down parsing configuration file) */
00062     if (parse_file_check(cfgfile, rngfile) != 0) {
00063         se_log_error("unable to parse cfgfile %s", cfgfile?cfgfile:"(null)");
00064         se_free((void*) ecfg);
00065         return NULL;
00066     }
00067 
00068     /* open cfgfile */
00069     cfgfd = se_fopen(cfgfile, NULL, "r");
00070     if (cfgfd) {
00071         /* get values */
00072         ecfg->cfg_filename = se_strdup(cfgfile);
00073         ecfg->zonelist_filename = parse_conf_zonelist_filename(cfgfile);
00074         ecfg->zonefetch_filename = parse_conf_zonefetch_filename(cfgfile);
00075         ecfg->log_filename = parse_conf_log_filename(cfgfile);
00076         ecfg->pid_filename = parse_conf_pid_filename(cfgfile);
00077         ecfg->notify_command = parse_conf_notify_command(cfgfile);
00078         ecfg->clisock_filename = parse_conf_clisock_filename(cfgfile);
00079         ecfg->working_dir = parse_conf_working_dir(cfgfile);
00080         ecfg->username = parse_conf_username(cfgfile);
00081         ecfg->group = parse_conf_group(cfgfile);
00082         ecfg->chroot = parse_conf_chroot(cfgfile);
00083         ecfg->use_syslog = parse_conf_use_syslog(cfgfile);
00084         ecfg->num_worker_threads = parse_conf_worker_threads(cfgfile);
00085         ecfg->num_signer_threads = parse_conf_signer_threads(cfgfile);
00086         ecfg->verbosity = cmdline_verbosity;
00087 
00088         /* done */
00089         se_fclose(cfgfd);
00090         return ecfg;
00091     }
00092 
00093     se_log_error("unable to read cfgfile %s", cfgfile?cfgfile:"(null)");
00094     return NULL;
00095 }
00096 
00097 
00102 int
00103 engine_check_config(engineconfig_type* config)
00104 {
00105     int ret = 0;
00106 
00107     if (!config) {
00108         se_log_error("engine config does not exist");
00109         return 1;
00110     }
00111 
00112     /* room for more checks here */
00113 
00114     return ret;
00115 }
00116 
00117 
00122 void
00123 engine_config_print(FILE* out, engineconfig_type* config)
00124 {
00125     se_log_assert(out);
00126     se_log_debug("print config");
00127 
00128     fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
00129 
00130     if (config) {
00131         fprintf(out, "<Configuration>\n");
00132 
00133         /* Common */
00134         fprintf(out, "\t<Common>\n");
00135         if (config->use_syslog && config->log_filename) {
00136                 fprintf(out, "\t\t<Logging>\n");
00137                 fprintf(out, "\t\t\t<Syslog>\n");
00138                 fprintf(out, "\t\t\t\t<Facility>%s</Facility>\n",
00139                 config->log_filename);
00140                 fprintf(out, "\t\t\t</Syslog>\n");
00141                 fprintf(out, "\t\t</Logging>\n");
00142                 } else if (config->log_filename) {
00143                 fprintf(out, "\t\t<Logging>\n");
00144                 fprintf(out, "\t\t\t<File>\n");
00145                 fprintf(out, "\t\t\t\t<Filename>%s</Filename>\n",
00146                 config->log_filename);
00147                 fprintf(out, "\t\t\t</File>\n");
00148                 fprintf(out, "\t\t</Logging>\n");
00149         }
00150 
00151         fprintf(out, "\t\t<ZoneListFile>%s</ZoneListFile>\n",
00152             config->zonelist_filename);
00153         if (config->zonefetch_filename) {
00154             fprintf(out, "\t\t<ZoneFetchFile>%s</ZoneFetchFile>\n",
00155                 config->zonefetch_filename);
00156         }
00157 
00158         fprintf(out, "\t</Common>\n");
00159 
00160         /* Signer */
00161         fprintf(out, "\t<Signer>\n");
00162         if (config->username || config->group || config->chroot) {
00163             fprintf(out, "\t\t<Privileges>\n");
00164             if (config->username) {
00165                 fprintf(out, "\t\t<User>%s</User>\n", config->username);
00166             }
00167             if (config->group) {
00168                 fprintf(out, "\t\t<Group>%s</Group>\n", config->group);
00169             }
00170             if (config->chroot) {
00171                 fprintf(out, "\t\t<Directory>%s</Directory>\n",
00172                     config->chroot);
00173             }
00174             fprintf(out, "\t\t</Privileges>\n");
00175         }
00176         fprintf(out, "\t\t<WorkingDirectory>%s</WorkingDirectory>\n",
00177             config->working_dir);
00178         fprintf(out, "\t\t<WorkerThreads>%i</WorkerThreads>\n",
00179             config->num_worker_threads);
00180         fprintf(out, "\t\t<SignerThreads>%i</SignerThreads>\n",
00181             config->num_signer_threads);
00182         if (config->notify_command) {
00183             fprintf(out, "\t\t<NotifyCommand>%s</NotifyCommand>\n",
00184                 config->notify_command);
00185         }
00186         fprintf(out, "\t</Signer>\n");
00187 
00188         fprintf(out, "</Configuration>\n");
00189 
00190         /* make configurable:
00191            - pid_filename
00192            - clisock_filename
00193          */
00194     }
00195 
00196     return;
00197 }
00198 
00199 
00204 void
00205 engine_config_cleanup(engineconfig_type* config)
00206 {
00207     if (config) {
00208         se_log_debug("clean up config");
00209         if (config->cfg_filename) {
00210             se_free((void*) config->cfg_filename);
00211             config->cfg_filename = NULL;
00212         }
00213         if (config->zonelist_filename) {
00214             se_free((void*) config->zonelist_filename);
00215             config->zonelist_filename = NULL;
00216         }
00217         if (config->zonefetch_filename) {
00218             se_free((void*) config->zonefetch_filename);
00219             config->zonefetch_filename = NULL;
00220         }
00221         if (config->log_filename) {
00222             se_free((void*) config->log_filename);
00223             config->zonefetch_filename = NULL;
00224         }
00225         if (config->pid_filename) {
00226             se_free((void*) config->pid_filename);
00227             config->pid_filename = NULL;
00228         }
00229         if (config->notify_command) {
00230             se_free((void*) config->notify_command);
00231             config->notify_command = NULL;
00232         }
00233         if (config->clisock_filename) {
00234             se_free((void*) config->clisock_filename);
00235             config->clisock_filename = NULL;
00236         }
00237         if (config->working_dir) {
00238             se_free((void*) config->working_dir);
00239             config->working_dir = NULL;
00240         }
00241         if (config->username) {
00242             se_free((void*) config->username);
00243             config->username = NULL;
00244         }
00245         if (config->group) {
00246             se_free((void*) config->group);
00247             config->group = NULL;
00248         }
00249         if (config->chroot) {
00250             se_free((void*) config->chroot);
00251             config->chroot = NULL;
00252         }
00253         se_free((void*) config);
00254     } else {
00255         se_log_warning("cleanup empty config");
00256     }
00257 
00258     return;
00259 }