OpenDNSSEC-enforcer  2.0.2
zonelist_import_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
3  * Copyright (c) 2014 OpenDNSSEC AB (svb)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include "config.h"
30 #include <limits.h>
31 
32 #include "daemon/engine.h"
33 #include "daemon/cmdhandler.h"
34 #include "log.h"
35 #include "str.h"
36 #include "clientpipe.h"
37 #include "enforcer/enforce_task.h"
40 
42 
43 static const char *module_str = "zonelist_import_cmd";
44 
45 static void
46 usage(int sockfd)
47 {
48  client_printf(sockfd,
49  "zonelist import\n"
50  " [--remove-missing-zones] aka -r\n"
51  /* We require the user to give an absolute path. The daemon
52  * and the client might not have the same working directory. */
53  " [--file <absolute path>] aka -f\n"
54  );
55 }
56 
57 static void
58 help(int sockfd)
59 {
60  client_printf(sockfd,
61  "Import zones from zonelist.xml into enforcer database.\n"
62  "\nOptions:\n"
63  "remove-missing-zones Remove any zones from database not existed in zonelist file\n"
64  "file File to import, instead of zonelist file configured in conf.xml\n\n"
65  );
66 }
67 
68 static int
69 handles(const char *cmd, ssize_t n)
70 {
71  return ods_check_command(cmd, n, zonelist_import_funcblock()->cmdname) ? 1 : 0;
72 }
73 
74 static int
75 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
76  db_connection_t *dbconn)
77 {
78  char path[PATH_MAX], buf[ODS_SE_MAXLINE];
79  int ret, argc, remove_missing_zones;
80  #define NARGV 8
81  const char *argv[NARGV];
82  const char* zonelist_path = NULL;
83 
84  ods_log_debug("[%s] %s command", module_str, zonelist_import_funcblock()->cmdname);
85 
86  if (!engine || !engine->config ||
87  !engine->config->zonelist_filename || !dbconn)
88  {
89  return 1;
90  }
91 
92  cmd = ods_check_command(cmd, n, zonelist_import_funcblock()->cmdname);
93  if (!cmd) return -1;
94  /* Use buf as an intermediate buffer for the command.*/
95  strncpy(buf, cmd, sizeof(buf));
96  buf[sizeof(buf)-1] = '\0';
97  /* separate the arguments*/
98  argc = ods_str_explode(buf, NARGV, argv);
99  if (argc > NARGV) {
100  ods_log_warning("[%s] too many arguments for %s command",
101  module_str, zonelist_import_funcblock()->cmdname);
102  client_printf(sockfd,"too many arguments\n");
103  return -1;
104  }
105  remove_missing_zones = (ods_find_arg(&argc, argv, "remove-missing-zones", "r") >= 0);
106  (void)ods_find_arg_and_param(&argc, argv, "file", "f", &zonelist_path);
107  if (argc) {
108  ods_log_warning("[%s] unknown arguments for %s command",
109  module_str, zonelist_import_funcblock()->cmdname);
110  client_printf(sockfd,"unknown arguments\n");
111  return -1;
112  }
113 
114  ret = zonelist_import(sockfd, engine, dbconn, remove_missing_zones, zonelist_path);
115  if (ret == ZONELIST_IMPORT_NO_CHANGE) {
116  return 0;
117  } else if (ret != ZONELIST_IMPORT_OK) {
118  return 1;
119  }
120 
121  if (snprintf(path, sizeof(path), "%s/%s", engine->config->working_dir, OPENDNSSEC_ENFORCER_ZONELIST) >= (int)sizeof(path)
122  || zonelist_export(sockfd, dbconn, path, 0) != ZONELIST_EXPORT_OK)
123  {
124  ods_log_error("[%s] internal zonelist export failed", module_str);
125  client_printf_err(sockfd, "Unable to export the internal zonelist %s, updates will not reach the Signer!\n", path);
126  return 1;
127  }
128  else {
129  ods_log_info("[%s] internal zonelist exported successfully", module_str);
130  }
131 
132  flush_enforce_task(engine, 1);
133 
134  return 0;
135 }
136 
137 static struct cmd_func_block funcblock = {
138  "zonelist import", &usage, &help, &handles, &run
139 };
140 
141 struct cmd_func_block*
143 {
144  return &funcblock;
145 }
#define NARGV
#define ZONELIST_IMPORT_OK
void(* help)(int sockfd)
Definition: cmdhandler.h:64
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
const char * zonelist_filename
Definition: cfg.h:57
void ods_log_info(const char *format,...)
Definition: log.c:55
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
void ods_log_error(const char *format,...)
Definition: log.c:69
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
#define ZONELIST_EXPORT_OK
engineconfig_type * config
Definition: engine.h:53
int zonelist_export(int sockfd, db_connection_t *connection, const char *filename, int comment)
struct cmd_func_block * zonelist_import_funcblock(void)
const char * working_dir
Definition: cfg.h:64
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67
#define ZONELIST_IMPORT_NO_CHANGE
void ods_log_warning(const char *format,...)
Definition: log.c:62
int flush_enforce_task(engine_type *engine, bool enforce_all)
Definition: enforce_task.c:323