OpenDNSSEC-enforcer  2.1.9
enforce_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 <getopt.h>
31 #include "config.h"
32 
33 #include "cmdhandler.h"
35 #include "daemon/engine.h"
36 #include "enforcer/enforce_task.h"
37 #include "file.h"
38 #include "log.h"
39 #include "str.h"
40 #include "clientpipe.h"
41 
42 #include "enforcer/enforce_cmd.h"
43 
44 static const char *module_str = "enforce_cmd";
45 
46 #define MAX_ARGS 4
47 
52 static void
53 usage(int sockfd)
54 {
55  client_printf(sockfd,
56  "enforce\n"
57  " --zone <zone> aka -z\n");
58 }
59 
60 static void
61 help(int sockfd)
62 {
63  client_printf(sockfd,
64  "Force enforce task to run for a zone."
65  " Without arguments run enforce task for every zone.\n"
66  "\nOptions:\n"
67  "zone Schedule enforce task for this zone for *now*\n"
68  "\n"
69  );
70 }
71 
76 static int
77 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
78 {
79  time_t t_next;
80  task_type *task;
81  char *buf;
82  int argc = 0;
83  char const *argv[MAX_ARGS];
84  int long_index = 0, opt = 0;
85  char const *zone_name = NULL;
86  int pos;
87  db_connection_t* dbconn = getconnectioncontext(context);
88  engine_type* engine = getglobalcontext(context);
89 
90  static struct option long_options[] = {
91  {"zone", required_argument, 0, 'z'},
92  {0, 0, 0, 0}
93  };
94 
95  ods_log_debug("[%s] %s command", module_str, enforce_funcblock.cmdname);
96 
97  if (!cmd) return -1;
98 
99  if (!(buf = strdup(cmd))) {
100  client_printf_err(sockfd, "memory error\n");
101  return -1;
102  }
103  argc = ods_str_explode(buf, MAX_ARGS, 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, enforce_funcblock.cmdname);
108  free(buf);
109  return -1;
110  }
111 
112  optind = 0;
113  while ((opt = getopt_long(argc, (char* const*)argv, "z:", long_options, &long_index)) != -1) {
114  switch (opt) {
115  case 'z':
116  zone_name = optarg;
117  break;
118  default:
119  client_printf_err(sockfd, "unknown arguments\n");
120  ods_log_error("[%s] unknown arguments for %s command",
121  module_str, enforce_funcblock.cmdname);
122  return -1;
123 
124  }
125  }
126 
127  if (zone_name) {
128  enforce_task_flush_zone(engine, zone_name);
129  } else {
130  enforce_task_flush_all(engine, dbconn);
131  }
132  free(buf);
133  return 0;
134 }
135 
136 struct cmd_func_block enforce_funcblock = {
137  "enforce", &usage, &help, NULL, &run
138 };
#define MAX_ARGS
Definition: enforce_cmd.c:46
struct cmd_func_block enforce_funcblock
Definition: enforce_cmd.c:136
void enforce_task_flush_all(engine_type *engine, db_connection_t *dbconn)
Definition: enforce_task.c:179
void enforce_task_flush_zone(engine_type *engine, char const *zonename)
Definition: enforce_task.c:152
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
engine_type * getglobalcontext(cmdhandler_ctx_type *context)