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