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