OpenDNSSEC-enforcer 2.1.13
policy_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 <getopt.h>
30#include "daemon/engine.h"
31#include "cmdhandler.h"
33#include "log.h"
34#include "str.h"
35#include "clientpipe.h"
36#include "longgetopt.h"
40
41
43
44static const char *module_str = "policy_import_cmd";
45
46static void database_error_help(int sockfd) {
47 client_printf_err(sockfd,
48 "\nThe information in the database may have been changed during KASP update"
49 " and caused an update error, try rerunning policy import. If the problem persists"
50 " please check logs and database setup and after correcting the problem rerun policy import.\n"
51 );
52}
53
54static void
55usage(int sockfd)
56{
57 client_printf(sockfd,
58 "policy import\n"
59 " [--remove-missing-policies] aka -r\n"
60 );
61}
62
63static void
64help(int sockfd)
65{
66 client_printf(sockfd,
67 "Import policies from kasp.xml into the enforcer database.\n"
68 "\nOptions:\n"
69 "remove-missing-policies Remove any policies from database "
70 "that do not exist in the KASP file\n\n"
71 );
72}
73
74
75static int
76run(cmdhandler_ctx_type* context, int argc, char* argv[])
77{
78 int sockfd = context->sockfd;
79 struct longgetopt optctx;
80 db_connection_t* dbconn = getconnectioncontext(context);;
81 engine_type* engine = getglobalcontext(context);
82
83 int remove_missing_policies = 0;
84 int long_index = 0, opt = 0;
85
86 static struct option long_options[] = {
87 {"remove-missing-policies", no_argument, 0, 'r'},
88 {0, 0, 0, 0}
89 };
90
91 if (!engine || !engine->config || !engine->config->policy_filename
92 || !dbconn)
93 {
94 return 1;
95 }
96
97 for(opt = longgetopt(argc, argv, "r", long_options, &long_index, &optctx); opt != -1;
98 opt = longgetopt(argc, argv, NULL,long_options, &long_index, &optctx)) {
99 switch (opt) {
100 case 'r':
101 remove_missing_policies = 1;
102 break;
103 default:
104 client_printf_err(sockfd, "unknown arguments\n");
105 ods_log_error("[%s] unknown arguments for policy import command", module_str);
106 return -1;
107 }
108 }
109
110 switch (policy_import(sockfd, engine, dbconn, remove_missing_policies)) {
111 case POLICY_IMPORT_OK:
112 /* only zones in policy, and force! */
113 enforce_task_flush_all(engine, dbconn);
114 (void)flush_resalt_task_all(engine, dbconn);
115 return 0;
116 break;
117
121 break;
122
124 database_error_help(sockfd);
125 break;
126
127 default:
128 break;
129 }
130
131 return 1;
132}
133
134struct cmd_func_block policy_import_funcblock = {
135 "policy import", &usage, &help, NULL, NULL, &run, NULL
136};
void enforce_task_flush_all(engine_type *engine, db_connection_t *dbconn)
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
int policy_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete)
#define POLICY_IMPORT_ERR_ARGS
#define POLICY_IMPORT_ERR_DATABASE
#define POLICY_IMPORT_ERR_MEMORY
#define POLICY_IMPORT_ERR_XML
#define POLICY_IMPORT_OK
struct cmd_func_block policy_import_funcblock
int flush_resalt_task_all(engine_type *engine, db_connection_t *dbconn)
engineconfig_type * config
Definition engine.h:48
const char * policy_filename
Definition cfg.h:56