OpenDNSSEC-enforcer  2.1.10
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 #include <getopt.h>
32 
33 #include "daemon/engine.h"
34 #include "cmdhandler.h"
36 #include "log.h"
37 #include "str.h"
38 #include "clientpipe.h"
39 #include "enforcer/enforce_task.h"
42 
44 
45 static const char *module_str = "zonelist_import_cmd";
46 
47 static void
48 usage(int sockfd)
49 {
50  client_printf(sockfd,
51  "zonelist import\n"
52  " [--remove-missing-zones] aka -r\n"
53  /* We require the user to give an absolute path. The daemon
54  * and the client might not have the same working directory. */
55  " [--file <absolute path>] aka -f\n"
56  );
57 }
58 
59 static void
60 help(int sockfd)
61 {
62  client_printf(sockfd,
63  "Import zones from zonelist.xml into enforcer database.\n"
64  "\nOptions:\n"
65  "remove-missing-zones Remove any zones from database not existed in zonelist file\n"
66  "file File to import, instead of zonelist file configured in conf.xml\n\n"
67  );
68 }
69 
70 static int
71 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
72 {
73  char path[PATH_MAX], buf[ODS_SE_MAXLINE];
74  int ret, argc = 0, remove_missing_zones = 0;
75  #define NARGV 5
76  int long_index = 0, opt = 0;
77  const char *argv[NARGV];
78  const char* zonelist_path = NULL;
79  db_connection_t* dbconn = getconnectioncontext(context);
80  engine_type* engine = getglobalcontext(context);
81 
82  static struct option long_options[] = {
83  {"remove-missing-zones", no_argument, 0, 'r'},
84  {"file", required_argument, 0, 'f'},
85  {0, 0, 0, 0}
86  };
87 
88  ods_log_debug("[%s] %s command", module_str, zonelist_import_funcblock.cmdname);
89 
90  if (!engine || !engine->config ||
91  !engine->config->zonelist_filename || !dbconn)
92  {
93  return 1;
94  }
95 
96  if (!cmd) return -1;
97 
98  /* Use buf as an intermediate buffer for the command.*/
99  strncpy(buf, cmd, sizeof(buf));
100  buf[sizeof(buf)-1] = '\0';
101 
102  /* separate the arguments*/
103  argc = ods_str_explode(buf, NARGV, argv);
104  if (argc == -1) {
105  client_printf_err(sockfd, "too many arguments\n");
106  ods_log_error("[%s] too many arguments for %s command",
107  module_str, zonelist_import_funcblock.cmdname);
108  return -1;
109  }
110 
111  optind = 0;
112  while ((opt = getopt_long(argc, (char* const*)argv, "rf:", long_options, &long_index)) != -1) {
113  switch (opt) {
114  case 'r':
115  remove_missing_zones = 1;
116  break;
117  case 'f':
118  zonelist_path = optarg;
119  break;
120  default:
121  client_printf_err(sockfd, "unknown arguments\n");
122  ods_log_error("[%s] unknown arguments for %s command",
123  module_str, zonelist_import_funcblock.cmdname);
124  return -1;
125  }
126  }
127 
128  ret = zonelist_import(sockfd, engine, dbconn, remove_missing_zones, zonelist_path);
129  if (ret == ZONELIST_IMPORT_NO_CHANGE) {
130  return 0;
131  } else if (ret != ZONELIST_IMPORT_OK) {
132  return 1;
133  }
134 
135  if (snprintf(path, sizeof(path), "%s/%s", engine->config->working_dir, OPENDNSSEC_ENFORCER_ZONELIST) >= (int)sizeof(path)
136  || zonelist_export(sockfd, dbconn, path, 0) != ZONELIST_EXPORT_OK)
137  {
138  ods_log_error("[%s] internal zonelist export failed", module_str);
139  client_printf_err(sockfd, "Unable to export the internal zonelist %s, updates will not reach the Signer!\n", path);
140  return 1;
141  }
142  else {
143  ods_log_info("[%s] internal zonelist exported successfully", module_str);
144  }
145 
146  /* YBS Only flush for zones with changed policy */
147  enforce_task_flush_all(engine, dbconn);
148 
149  return 0;
150 }
151 
152 struct cmd_func_block zonelist_import_funcblock = {
153  "zonelist import", &usage, &help, NULL, &run
154 };
void enforce_task_flush_all(engine_type *engine, db_connection_t *dbconn)
Definition: enforce_task.c:179
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
engineconfig_type * config
Definition: engine.h:48
const char * working_dir
Definition: cfg.h:64
const char * zonelist_filename
Definition: cfg.h:57
int zonelist_export(int sockfd, db_connection_t *connection, const char *filename, int comment)
#define ZONELIST_EXPORT_OK
int zonelist_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete, const char *zonelist_path)
#define ZONELIST_IMPORT_NO_CHANGE
#define ZONELIST_IMPORT_OK
struct cmd_func_block zonelist_import_funcblock
#define NARGV