encoding: Accept all exchange types for non IKEv1/IKEv2 major versions
[strongswan.git] / src / swanctl / commands / load_pools.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <errno.h>
19 #include <limits.h>
20
21 #include "command.h"
22 #include "swanctl.h"
23
24 /**
25 * Add a vici list from a comma separated string value
26 */
27 static void add_list_key(vici_req_t *req, char *key, char *value)
28 {
29 enumerator_t *enumerator;
30 char *token;
31
32 vici_begin_list(req, key);
33 enumerator = enumerator_create_token(value, ",", " ");
34 while (enumerator->enumerate(enumerator, &token))
35 {
36 vici_add_list_itemf(req, "%s", token);
37 }
38 enumerator->destroy(enumerator);
39 vici_end_list(req);
40 }
41
42 /**
43 * Translate setting key/values from a section into vici key-values/lists
44 */
45 static void add_key_values(vici_req_t *req, settings_t *cfg, char *section)
46 {
47 enumerator_t *enumerator;
48 char *key, *value;
49
50 enumerator = cfg->create_key_value_enumerator(cfg, section);
51 while (enumerator->enumerate(enumerator, &key, &value))
52 {
53 /* pool subnet is encoded as key/value, all other attributes as list */
54 if (streq(key, "addrs"))
55 {
56 vici_add_key_valuef(req, key, "%s", value);
57 }
58 else
59 {
60 add_list_key(req, key, value);
61 }
62 }
63 enumerator->destroy(enumerator);
64 }
65
66 /**
67 * Load a pool configuration
68 */
69 static bool load_pool(vici_conn_t *conn, settings_t *cfg,
70 char *section, command_format_options_t format)
71 {
72 vici_req_t *req;
73 vici_res_t *res;
74 bool ret = TRUE;
75 char buf[128];
76
77 snprintf(buf, sizeof(buf), "%s.%s", "pools", section);
78
79 req = vici_begin("load-pool");
80
81 vici_begin_section(req, section);
82 add_key_values(req, cfg, buf);
83 vici_end_section(req);
84
85 res = vici_submit(req, conn);
86 if (!res)
87 {
88 fprintf(stderr, "load-pool request failed: %s\n", strerror(errno));
89 return FALSE;
90 }
91 if (format & COMMAND_FORMAT_RAW)
92 {
93 vici_dump(res, "load-pool reply", format & COMMAND_FORMAT_PRETTY,
94 stdout);
95 }
96 else if (!streq(vici_find_str(res, "no", "success"), "yes"))
97 {
98 fprintf(stderr, "loading pool '%s' failed: %s\n",
99 section, vici_find_str(res, "", "errmsg"));
100 ret = FALSE;
101 }
102 else
103 {
104 printf("loaded pool '%s'\n", section);
105 }
106 vici_free_res(res);
107 return ret;
108 }
109
110 CALLBACK(list_pool, int,
111 linked_list_t *list, vici_res_t *res, char *name)
112 {
113 list->insert_last(list, strdup(name));
114 return 0;
115 }
116
117 /**
118 * Create a list of currently loaded pools
119 */
120 static linked_list_t* list_pools(vici_conn_t *conn,
121 command_format_options_t format)
122 {
123 linked_list_t *list;
124 vici_res_t *res;
125
126 list = linked_list_create();
127
128 res = vici_submit(vici_begin("get-pools"), conn);
129 if (res)
130 {
131 if (format & COMMAND_FORMAT_RAW)
132 {
133 vici_dump(res, "get-pools reply", format & COMMAND_FORMAT_PRETTY,
134 stdout);
135 }
136 vici_parse_cb(res, list_pool, NULL, NULL, list);
137 vici_free_res(res);
138 }
139 return list;
140 }
141
142 /**
143 * Remove and free a string from a list
144 */
145 static void remove_from_list(linked_list_t *list, char *str)
146 {
147 enumerator_t *enumerator;
148 char *current;
149
150 enumerator = list->create_enumerator(list);
151 while (enumerator->enumerate(enumerator, &current))
152 {
153 if (streq(current, str))
154 {
155 list->remove_at(list, enumerator);
156 free(current);
157 }
158 }
159 enumerator->destroy(enumerator);
160 }
161
162 /**
163 * Unload a pool by name
164 */
165 static bool unload_pool(vici_conn_t *conn, char *name,
166 command_format_options_t format)
167 {
168 vici_req_t *req;
169 vici_res_t *res;
170 bool ret = TRUE;
171
172 req = vici_begin("unload-pool");
173 vici_add_key_valuef(req, "name", "%s", name);
174 res = vici_submit(req, conn);
175 if (!res)
176 {
177 fprintf(stderr, "unload-pool request failed: %s\n", strerror(errno));
178 return FALSE;
179 }
180 if (format & COMMAND_FORMAT_RAW)
181 {
182 vici_dump(res, "unload-pool reply", format & COMMAND_FORMAT_PRETTY,
183 stdout);
184 }
185 else if (!streq(vici_find_str(res, "no", "success"), "yes"))
186 {
187 fprintf(stderr, "unloading pool '%s' failed: %s\n",
188 name, vici_find_str(res, "", "errmsg"));
189 ret = FALSE;
190 }
191 vici_free_res(res);
192 return ret;
193 }
194
195 static int load_pools(vici_conn_t *conn)
196 {
197 command_format_options_t format = COMMAND_FORMAT_NONE;
198 u_int found = 0, loaded = 0, unloaded = 0;
199 char *arg, *section;
200 enumerator_t *enumerator;
201 linked_list_t *pools;
202 settings_t *cfg;
203
204 while (TRUE)
205 {
206 switch (command_getopt(&arg))
207 {
208 case 'h':
209 return command_usage(NULL);
210 case 'P':
211 format |= COMMAND_FORMAT_PRETTY;
212 /* fall through to raw */
213 case 'r':
214 format |= COMMAND_FORMAT_RAW;
215 continue;
216 case EOF:
217 break;
218 default:
219 return command_usage("invalid --load-pools option");
220 }
221 break;
222 }
223
224 cfg = settings_create(SWANCTL_CONF);
225 if (!cfg)
226 {
227 fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
228 return EINVAL;
229 }
230
231 pools = list_pools(conn, format);
232
233 enumerator = cfg->create_section_enumerator(cfg, "pools");
234 while (enumerator->enumerate(enumerator, &section))
235 {
236 remove_from_list(pools, section);
237 found++;
238 if (load_pool(conn, cfg, section, format))
239 {
240 loaded++;
241 }
242 }
243 enumerator->destroy(enumerator);
244
245 cfg->destroy(cfg);
246
247 /* unload all pools in daemon, but not in file */
248 while (pools->remove_first(pools, (void**)&section) == SUCCESS)
249 {
250 if (unload_pool(conn, section, format))
251 {
252 unloaded++;
253 }
254 free(section);
255 }
256 pools->destroy(pools);
257
258 if (format & COMMAND_FORMAT_RAW)
259 {
260 return 0;
261 }
262 if (found == 0)
263 {
264 printf("no pools found, %u unloaded\n", unloaded);
265 return 0;
266 }
267 if (loaded == found)
268 {
269 printf("successfully loaded %u pools, %u unloaded\n",
270 loaded, unloaded);
271 return 0;
272 }
273 fprintf(stderr, "loaded %u of %u pools, %u failed to load, "
274 "%u unloaded\n", loaded, found, found - loaded, unloaded);
275 return EINVAL;
276 }
277
278 /**
279 * Register the command.
280 */
281 static void __attribute__ ((constructor))reg()
282 {
283 command_register((command_t) {
284 load_pools, 'a', "load-pools", "(re-)load pool configuration",
285 {"[--raw|--pretty"},
286 {
287 {"help", 'h', 0, "show usage information"},
288 {"raw", 'r', 0, "dump raw response message"},
289 {"pretty", 'P', 0, "dump raw response message in pretty print"},
290 }
291 });
292 }