OpenDNSSEC-enforcer  2.0.3
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 "daemon/cmdhandler.h"
35 #include "str.h"
36 #include "log.h"
37 #include "file.h"
38 #include "daemon/engine.h"
39 #include "clientpipe.h"
40 #include "daemon/cfg.h"
41 #include "parser/confparser.h"
42 #include "status.h"
43 #include "utils/kc_helper.h"
44 #include "daemon/engine.h"
45 #include "libhsm.h"
46 
48 
49 static const char *module_str = "update_repositorylist_cmd";
50 
51 /* 0 succes, 1 error */
52 static int
53 validate_configfile(const char* cfgfile)
54 {
55  char *kasp = NULL, *zonelist = NULL, **replist = NULL;
56  int repcount, i;
57  int cc_status = check_conf(cfgfile, &kasp, &zonelist, &replist,
58  &repcount, 0);
59  free(kasp);
60  free(zonelist);
61  if (replist) for (i = 0; i < repcount; i++) free(replist[i]);
62  free(replist);
63  return cc_status;
64 }
65 
72 static int
73 perform_update_repositorylist(int sockfd, engine_type* engine)
74 {
75  const char* cfgfile = ODS_SE_CFGFILE;
76  int status = 1;
77  hsm_repository_t* new_reps;
78 
79  if (validate_configfile(cfgfile)) {
80  ods_log_error_and_printf(sockfd, module_str,
81  "Unable to validate '%s' consistency.", cfgfile);
82  return 0;
83  }
84 
85  /* key gen tasks must be stopped, hsm connections must be closed
86  * easiest way is to stop all workers, */
87  pthread_mutex_lock(&engine->signal_lock);
90  engine_stop_workers(engine);
91  new_reps = parse_conf_repositories(cfgfile);
92  if (!new_reps) {
93  /* revert */
94  status = 0;
95  client_printf(sockfd, "Could not load new repositories. Will continue with old.\n");
96  } else {
97  /* succes */
98  hsm_repository_free(engine->config->repositories);
99  engine->config->repositories = new_reps;
100  engine->need_to_reload = 1;
101  client_printf(sockfd, "new repositories parsed successful.\n");
102  client_printf(sockfd, "Notifying enforcer of new respositories.\n");
103  /* kick daemon thread so it will reload the hsms */
104  pthread_cond_signal(&engine->signal_cond);
105  }
106  engine_start_workers(engine);
107  pthread_mutex_unlock(&engine->signal_lock);
108  return status;
109 }
110 
111 static void
112 usage(int sockfd)
113 {
114  client_printf(sockfd,
115  "update repositorylist\n");
116 }
117 
118 static void
119 help(int sockfd)
120 {
121  client_printf(sockfd,
122  "Import respositories from conf.xml into the enforcer.\n\n");
123 }
124 
125 static int
126 handles(const char *cmd, ssize_t n)
127 {
128  return ods_check_command(cmd, n, update_repositorylist_funcblock()->cmdname)?1:0;
129 }
130 
131 static int
132 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
133  db_connection_t *dbconn)
134 {
135  (void)cmd; (void)n, (void)dbconn;
136  ods_log_debug("[%s] %s command", module_str,
138 
139  if (!perform_update_repositorylist(sockfd, engine)) {
140  ods_log_error_and_printf(sockfd, module_str,
141  "unable to update repositorylist.");
142  return 1;
143  }
144  return 0;
145 }
146 
147 static struct cmd_func_block funcblock = {
148  "update repositorylist", &usage, &help, &handles, &run
149 };
150 
151 struct cmd_func_block*
153 {
154  return &funcblock;
155 }
void(* help)(int sockfd)
Definition: cmdhandler.h:64
int check_conf(const char *conf, char **kasp, char **zonelist, char ***repo_listout, int *repo_countout, int verbose)
Definition: kc_helper.c:1395
void ods_log_debug(const char *format,...)
Definition: log.c:41
void engine_start_workers(engine_type *engine)
Definition: engine.c:214
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition: confparser.c:205
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
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
hsm_repository_t * repositories
Definition: cfg.h:78
struct cmd_func_block * update_repositorylist_funcblock(void)
pthread_mutex_t signal_lock
Definition: engine.h:70
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67
int need_to_reload
Definition: engine.h:66