OpenDNSSEC-enforcer 2.1.13
update_repositorylist_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Surfnet
3 * Copyright (c) 2011 .SE (The Internet Infrastructure Foundation).
4 * Copyright (c) 2011 OpenDNSSEC AB (svb)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include "config.h"
31
32#include <pthread.h>
33
34#include "cmdhandler.h"
36#include "str.h"
37#include "log.h"
38#include "file.h"
39#include "daemon/engine.h"
40#include "clientpipe.h"
41#include "daemon/cfg.h"
42#include "parser/confparser.h"
43#include "longgetopt.h"
44#include "status.h"
45#include "utils/kc_helper.h"
46#include "daemon/engine.h"
47#include "libhsm.h"
48
50
51static const char *module_str = "update_repositorylist_cmd";
52
53/* 0 succes, 1 error */
54static int
55validate_configfile(const char* cfgfile)
56{
57 char *kasp = NULL, *zonelist = NULL, **replist = NULL;
58 int repcount, i;
59 int cc_status = check_conf(cfgfile, &kasp, &zonelist, &replist,
60 &repcount, 0);
61 free(kasp);
62 free(zonelist);
63 if (replist) for (i = 0; i < repcount; i++) free(replist[i]);
64 free(replist);
65 return cc_status;
66}
67
74static int
75perform_update_repositorylist(int sockfd, engine_type* engine)
76{
77 const char* cfgfile = ODS_SE_CFGFILE;
78 int status = 1;
79 hsm_repository_t* new_reps;
80
81 if (validate_configfile(cfgfile)) {
82 ods_log_error_and_printf(sockfd, module_str,
83 "Unable to validate '%s' consistency.", cfgfile);
84 return 0;
85 }
86
87 /* key gen tasks must be stopped, hsm connections must be closed
88 * easiest way is to stop all workers, */
89 pthread_mutex_lock(&engine->signal_lock);
92 engine_stop_workers(engine);
93 new_reps = parse_conf_repositories(cfgfile);
94 if (!new_reps) {
95 /* revert */
96 status = 0;
97 client_printf(sockfd, "Could not load new repositories. Will continue with old.\n");
98 } else {
99 /* succes */
100 hsm_repository_free(engine->config->repositories);
101 engine->config->repositories = new_reps;
102 engine->need_to_reload = 1;
103 client_printf(sockfd, "new repositories parsed successful.\n");
104 client_printf(sockfd, "Notifying enforcer of new respositories.\n");
105 /* kick daemon thread so it will reload the hsms */
106 pthread_cond_signal(&engine->signal_cond);
107 }
108 engine_start_workers(engine);
109 pthread_mutex_unlock(&engine->signal_lock);
110 return status;
111}
112
113static void
114usage(int sockfd)
115{
116 client_printf(sockfd,
117 "update repositorylist\n");
118}
119
120static void
121help(int sockfd)
122{
123 client_printf(sockfd,
124 "Import respositories from conf.xml into the enforcer.\n\n");
125}
126
127static int
128run(cmdhandler_ctx_type* context, int argc, char* argv[])
129{
130 int sockfd = context->sockfd;
131 engine_type* engine = getglobalcontext(context);
132
133 if (!perform_update_repositorylist(sockfd, engine)) {
134 ods_log_error_and_printf(sockfd, module_str,
135 "unable to update repositorylist.");
136 return 1;
137 }
138 return 0;
139}
140
141struct cmd_func_block update_repositorylist_funcblock = {
142 "update repositorylist", &usage, &help, NULL, NULL, &run, NULL
143};
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
void engine_start_workers(engine_type *engine)
Definition: engine.c:174
void engine_stop_workers(engine_type *engine)
Definition: engine.c:193
int check_conf(const char *conf, char **kasp, char **zonelist, char ***repo_listout, int *repo_countout, int verbose)
Definition: kc_helper.c:1418
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition: confparser.c:205
pthread_mutex_t signal_lock
Definition: engine.h:65
pthread_cond_t signal_cond
Definition: engine.h:64
int need_to_reload
Definition: engine.h:56
engineconfig_type * config
Definition: engine.h:48
hsm_repository_t * repositories
Definition: cfg.h:79
struct cmd_func_block update_repositorylist_funcblock