OpenDNSSEC-enforcer 2.1.13
update_all_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 "daemon/engine.h"
37#include "file.h"
38#include "log.h"
39#include "str.h"
40#include "utils/kc_helper.h"
41#include "clientpipe.h"
42#include "longgetopt.h"
45
47
48static const char *module_str = "update_all_cmd";
49
50static void
51usage(int sockfd)
52{
53 client_printf(sockfd,
54 "update all\n"
55 );
56}
57
58static void
59help(int sockfd)
60{
61 client_printf(sockfd, "Perform policy import, update zonelist, and update repositorylist.\n\n");
62}
63
64static int
65check_all(int sockfd, engine_type* engine)
66{
67 char *kasp = NULL;
68 char *zonelist = NULL;
69 char **replist = NULL;
70 char **policy_names = NULL;
71 int repcount, i;
72 int policy_count = 0;
73 int error = 1;
74
75 if (check_conf(engine->config->cfg_filename, &kasp,
76 &zonelist, &replist, &repcount,
77 (ods_log_verbosity() >= 3)))
78 ods_log_error_and_printf(sockfd, module_str,
79 "Unable to validate '%s' consistency.",
80 engine->config->cfg_filename);
81 else if (check_kasp(kasp, replist, repcount, 0, &policy_names, &policy_count))
82 ods_log_error_and_printf(sockfd, module_str,
83 "Unable to validate '%s' consistency.", kasp);
84 else if (check_zonelist(zonelist, 0, policy_names, policy_count))
85 ods_log_error_and_printf(sockfd, module_str,
86 "Unable to validate '%s' consistency.", zonelist);
87 else error = 0;
88
89 free(kasp);
90 free(zonelist);
91 if (replist) {
92 for (i = 0; i < repcount; i++) free(replist[i]);
93 free(replist);
94 }
95 if (policy_names) {
96 for (i = 0; i < policy_count; i++) free(policy_names[i]);
97 }
98 return error;
99}
100
101static int
102run(cmdhandler_ctx_type* context, int argc, char* argv[])
103{
104 int sockfd = context->sockfd;
105 int error;
106 db_connection_t* dbconn = getconnectioncontext(context);
107 engine_type* engine = getglobalcontext(context);
108
109 /*
110 * Check conf.xml, KASP and zonelist. If there are no errors we stop all
111 * activity, update KASP and zonelist and then reload in order to load the
112 * new conf.xml
113 */
114 if (!(error = check_all(sockfd, engine))) {
115 /*
116 * Lock the engine and stop all workers
117 */
118 pthread_mutex_lock(&engine->signal_lock);
119 engine_stop_workers(engine);
120
121 policy_import(sockfd, engine, dbconn, 0);
122 zonelist_import(sockfd, engine, dbconn, 0, NULL);
123
124 /*
125 * Mark the engine for reload, signal it and start it again
126 */
127 engine->need_to_reload = 1;
128 pthread_cond_signal(&engine->signal_cond);
129 engine_start_workers(engine);
130 pthread_mutex_unlock(&engine->signal_lock);
131 }
132 return error;
133}
134
135struct cmd_func_block update_all_funcblock = {
136 "update all", &usage, &help, NULL, NULL, &run, NULL
137};
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
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_kasp(const char *kasp, char **repo_list, int repo_count, int verbose, char ***policy_names_out, int *policy_count_out)
Definition kc_helper.c:1776
int check_zonelist(const char *zonelist, int verbose, char **policy_names, int policy_count)
Definition kc_helper.c:1700
int check_conf(const char *conf, char **kasp, char **zonelist, char ***repo_listout, int *repo_countout, int verbose)
Definition kc_helper.c:1422
int policy_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete)
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
const char * cfg_filename
Definition cfg.h:55
struct cmd_func_block update_all_funcblock
int zonelist_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete, const char *zonelist_path)