OpenDNSSEC-signer 1.3.0rc3
/build/buildd2-opendnssec_1.3.0~rc3-1-mips-lpJjcT/opendnssec-1.3.0~rc3/signer/src/daemon/cfg.c
Go to the documentation of this file.
00001 /*
00002  * $Id: cfg.c 5227 2011-06-12 08:51:24Z 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 "shared/allocator.h"
00038 #include "shared/file.h"
00039 #include "shared/log.h"
00040 #include "shared/status.h"
00041 
00042 #include <errno.h>
00043 #include <stdio.h>
00044 #include <string.h>
00045 
00046 static const char* conf_str = "config";
00047 
00048 
00053 engineconfig_type*
00054 engine_config(allocator_type* allocator, const char* cfgfile,
00055     int cmdline_verbosity)
00056 {
00057     engineconfig_type* ecfg;
00058     const char* rngfile = ODS_SE_RNGDIR "/conf.rng";
00059     FILE* cfgfd = NULL;
00060 
00061     if (!allocator) {
00062         ods_log_error("[%s] failed to read: no allocator available", conf_str);
00063         return NULL;
00064     }
00065     ods_log_assert(allocator);
00066     if (!cfgfile) {
00067         ods_log_error("[%s] failed to read: no filename given", conf_str);
00068         return NULL;
00069     }
00070     ods_log_assert(cfgfile);
00071     ods_log_verbose("[%s] read cfgfile: %s", conf_str, cfgfile);
00072 
00073     ecfg = (engineconfig_type*) allocator_alloc(allocator,
00074         sizeof(engineconfig_type));
00075     if (!ecfg) {
00076         ods_log_error("[%s] failed to read: allocator failed", conf_str);
00077         return NULL;
00078     }
00079 
00080     ecfg->allocator = allocator;
00081 
00082     /* check syntax (slows down parsing configuration file) */
00083     if (parse_file_check(cfgfile, rngfile) != ODS_STATUS_OK) {
00084         ods_log_error("[%s] failed to read: unable to parse file %s",
00085             conf_str, cfgfile);
00086         return NULL;
00087     }
00088 
00089     /* open cfgfile */
00090     cfgfd = ods_fopen(cfgfile, NULL, "r");
00091     if (cfgfd) {
00092         /* get values */
00093         ecfg->cfg_filename = allocator_strdup(allocator, cfgfile);
00094         ecfg->zonelist_filename = parse_conf_zonelist_filename(allocator,
00095             cfgfile);
00096         ecfg->zonefetch_filename = parse_conf_zonefetch_filename(allocator,
00097             cfgfile);
00098         ecfg->log_filename = parse_conf_log_filename(allocator, cfgfile);
00099         ecfg->pid_filename = parse_conf_pid_filename(allocator, cfgfile);
00100         ecfg->notify_command = parse_conf_notify_command(allocator, cfgfile);
00101         ecfg->clisock_filename = parse_conf_clisock_filename(allocator,
00102             cfgfile);
00103         ecfg->working_dir = parse_conf_working_dir(allocator, cfgfile);
00104         ecfg->username = parse_conf_username(allocator, cfgfile);
00105         ecfg->group = parse_conf_group(allocator, cfgfile);
00106         ecfg->chroot = parse_conf_chroot(allocator, cfgfile);
00107         ecfg->use_syslog = parse_conf_use_syslog(cfgfile);
00108         ecfg->num_worker_threads = parse_conf_worker_threads(cfgfile);
00109         ecfg->num_adapters = 0;
00110         ecfg->num_signer_threads = parse_conf_signer_threads(cfgfile);
00111         ecfg->verbosity = cmdline_verbosity;
00112 
00113         /* done */
00114         ods_fclose(cfgfd);
00115         return ecfg;
00116     }
00117 
00118     ods_log_error("[%s] failed to read: unable to open file %s", conf_str,
00119         cfgfile);
00120     return NULL;
00121 }
00122 
00123 
00128 ods_status
00129 engine_config_check(engineconfig_type* config)
00130 {
00131     if (!config) {
00132         ods_log_error("[%s] check failed: config does not exist", conf_str);
00133         return ODS_STATUS_CFG_ERR;
00134     }
00135     if (!config->zonelist_filename) {
00136         ods_log_error("[%s] check failed: no zonelist filename", conf_str);
00137         return ODS_STATUS_CFG_ERR;
00138     }
00139     if (!config->clisock_filename) {
00140         ods_log_error("[%s] check failed: no socket filename", conf_str);
00141         return ODS_STATUS_CFG_ERR;
00142     }
00143 
00144     /*  [TODO] room for more checks here */
00145 
00146     return ODS_STATUS_OK;
00147 }
00148 
00149 
00154 void
00155 engine_config_print(FILE* out, engineconfig_type* config)
00156 {
00157     if (!out) {
00158         return;
00159     }
00160 
00161     fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
00162     if (config) {
00163         fprintf(out, "<Configuration>\n");
00164 
00165         /* Common */
00166         fprintf(out, "\t<Common>\n");
00167         if (config->use_syslog && config->log_filename) {
00168                 fprintf(out, "\t\t<Logging>\n");
00169                 fprintf(out, "\t\t\t<Syslog>\n");
00170                 fprintf(out, "\t\t\t\t<Facility>%s</Facility>\n",
00171                 config->log_filename);
00172                 fprintf(out, "\t\t\t</Syslog>\n");
00173                 fprintf(out, "\t\t</Logging>\n");
00174                 } else if (config->log_filename) {
00175                 fprintf(out, "\t\t<Logging>\n");
00176                 fprintf(out, "\t\t\t<File>\n");
00177                 fprintf(out, "\t\t\t\t<Filename>%s</Filename>\n",
00178                 config->log_filename);
00179                 fprintf(out, "\t\t\t</File>\n");
00180                 fprintf(out, "\t\t</Logging>\n");
00181         }
00182 
00183         fprintf(out, "\t\t<ZoneListFile>%s</ZoneListFile>\n",
00184             config->zonelist_filename);
00185         if (config->zonefetch_filename) {
00186             fprintf(out, "\t\t<ZoneFetchFile>%s</ZoneFetchFile>\n",
00187                 config->zonefetch_filename);
00188         }
00189 
00190         fprintf(out, "\t</Common>\n");
00191 
00192         /* Signer */
00193         fprintf(out, "\t<Signer>\n");
00194         if (config->username || config->group || config->chroot) {
00195             fprintf(out, "\t\t<Privileges>\n");
00196             if (config->username) {
00197                 fprintf(out, "\t\t<User>%s</User>\n", config->username);
00198             }
00199             if (config->group) {
00200                 fprintf(out, "\t\t<Group>%s</Group>\n", config->group);
00201             }
00202             if (config->chroot) {
00203                 fprintf(out, "\t\t<Directory>%s</Directory>\n",
00204                     config->chroot);
00205             }
00206             fprintf(out, "\t\t</Privileges>\n");
00207         }
00208         fprintf(out, "\t\t<WorkingDirectory>%s</WorkingDirectory>\n",
00209             config->working_dir);
00210         fprintf(out, "\t\t<WorkerThreads>%i</WorkerThreads>\n",
00211             config->num_worker_threads);
00212         fprintf(out, "\t\t<SignerThreads>%i</SignerThreads>\n",
00213             config->num_signer_threads);
00214         if (config->notify_command) {
00215             fprintf(out, "\t\t<NotifyCommand>%s</NotifyCommand>\n",
00216                 config->notify_command);
00217         }
00218         fprintf(out, "\t</Signer>\n");
00219 
00220         fprintf(out, "</Configuration>\n");
00221 
00222         /* make configurable:
00223            - pid_filename
00224            - clisock_filename
00225          */
00226     }
00227     return;
00228 }
00229 
00230 
00235 void
00236 engine_config_cleanup(engineconfig_type* config)
00237 {
00238     allocator_type* allocator;
00239     if (!config) {
00240         return;
00241     }
00242     allocator = config->allocator;
00243     allocator_deallocate(allocator, (void*) config->cfg_filename);
00244     allocator_deallocate(allocator, (void*) config->zonelist_filename);
00245     allocator_deallocate(allocator, (void*) config->zonefetch_filename);
00246     allocator_deallocate(allocator, (void*) config->log_filename);
00247     allocator_deallocate(allocator, (void*) config->pid_filename);
00248     allocator_deallocate(allocator, (void*) config->notify_command);
00249     allocator_deallocate(allocator, (void*) config->clisock_filename);
00250     allocator_deallocate(allocator, (void*) config->working_dir);
00251     allocator_deallocate(allocator, (void*) config->username);
00252     allocator_deallocate(allocator, (void*) config->group);
00253     allocator_deallocate(allocator, (void*) config->chroot);
00254     allocator_deallocate(allocator, (void*) config);
00255     return;
00256 }
00257