]>
Commit | Line | Data |
---|---|---|
51bdc1f3 MW |
1 | /* |
2 | * Copyright (C) 2014 Martin Willi | |
3 | * Copyright (C) 2014 revosec AG | |
4 | * | |
afcd4661 AS |
5 | * Copyright (C) 2016 Andreas Steffen |
6 | * HSR Hochschule fuer Technik Rapperswil | |
7 | * | |
51bdc1f3 MW |
8 | * This program is free software; you can redistribute it and/or modify it |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | * for more details. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <stdio.h> | |
21 | #include <errno.h> | |
22 | ||
23 | #include "command.h" | |
24 | ||
25 | #include <collections/hashtable.h> | |
26 | ||
27 | /** | |
28 | * Free hashtable with contained strings | |
29 | */ | |
30 | static void free_hashtable(hashtable_t *hashtable) | |
31 | { | |
32 | enumerator_t *enumerator; | |
33 | char *str; | |
34 | ||
35 | enumerator = hashtable->create_enumerator(hashtable); | |
36 | while (enumerator->enumerate(enumerator, NULL, &str)) | |
37 | { | |
38 | free(str); | |
39 | } | |
40 | enumerator->destroy(enumerator); | |
41 | ||
42 | hashtable->destroy(hashtable); | |
43 | } | |
44 | ||
45 | CALLBACK(values, int, | |
46 | hashtable_t *sa, vici_res_t *res, char *name, void *value, int len) | |
47 | { | |
48 | chunk_t chunk; | |
49 | char *str; | |
50 | ||
51 | chunk = chunk_create(value, len); | |
52 | if (chunk_printable(chunk, NULL, ' ')) | |
53 | { | |
54 | if (asprintf(&str, "%.*s", len, value) >= 0) | |
55 | { | |
56 | free(sa->put(sa, name, str)); | |
57 | } | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
62 | ||
63 | CALLBACK(list, int, | |
64 | hashtable_t *sa, vici_res_t *res, char *name, void *value, int len) | |
65 | { | |
66 | chunk_t chunk; | |
67 | char *str; | |
68 | ||
69 | chunk = chunk_create(value, len); | |
70 | if (chunk_printable(chunk, NULL, ' ')) | |
71 | { | |
72 | str = sa->get(sa, name); | |
73 | if (asprintf(&str, "%s%s%.*s", | |
74 | str ?: "", str ? " " : "", len, value) >= 0) | |
75 | { | |
76 | free(sa->put(sa, name, str)); | |
77 | } | |
78 | } | |
79 | return 0; | |
80 | } | |
81 | ||
82 | CALLBACK(children_sn, int, | |
83 | hashtable_t *ike, vici_res_t *res, char *name) | |
84 | { | |
85 | hashtable_t *child; | |
b9522f9d | 86 | char *mode, *interface, *priority; |
4eaf08c3 AS |
87 | char *rekey_time, *rekey_bytes, *rekey_packets, *dpd_action, *dpd_delay; |
88 | bool no_time, no_bytes, no_packets, no_dpd, or = FALSE; | |
51bdc1f3 MW |
89 | int ret; |
90 | ||
91 | child = hashtable_create(hashtable_hash_str, hashtable_equals_str, 1); | |
92 | ret = vici_parse_cb(res, NULL, values, list, child); | |
93 | if (ret == 0) | |
94 | { | |
b9522f9d AS |
95 | mode = child->get(child, "mode"); |
96 | printf(" %s: %s, ", name, mode); | |
b1df6312 AS |
97 | |
98 | rekey_time = child->get(child, "rekey_time"); | |
99 | rekey_bytes = child->get(child, "rekey_bytes"); | |
100 | rekey_packets = child->get(child, "rekey_packets"); | |
4eaf08c3 AS |
101 | dpd_action = child->get(child, "dpd_action"); |
102 | dpd_delay = ike->get(ike, "dpd_delay"); | |
103 | ||
b1df6312 AS |
104 | no_time = streq(rekey_time, "0"); |
105 | no_bytes = streq(rekey_bytes, "0"); | |
106 | no_packets = streq(rekey_packets, "0"); | |
4eaf08c3 | 107 | no_dpd = streq(dpd_delay, "0"); |
b1df6312 | 108 | |
b9522f9d AS |
109 | if (strcaseeq(mode, "PASS") || strcaseeq(mode, "DROP") || |
110 | (no_time && no_bytes && no_packets)) | |
b1df6312 | 111 | { |
4eaf08c3 | 112 | printf("no rekeying"); |
b1df6312 AS |
113 | } |
114 | else | |
115 | { | |
116 | printf("rekeying every"); | |
117 | if (!no_time) | |
118 | { | |
119 | printf(" %ss", rekey_time); | |
120 | or = TRUE; | |
121 | } | |
122 | if (!no_bytes) | |
123 | { | |
124 | printf("%s %s bytes", or ? " or" : "", rekey_bytes); | |
125 | or = TRUE; | |
126 | } | |
127 | if (!no_packets) | |
128 | { | |
129 | printf("%s %s packets", or ? " or" : "", rekey_packets); | |
130 | } | |
b1df6312 | 131 | } |
4eaf08c3 AS |
132 | if (!no_dpd) |
133 | { | |
134 | printf(", dpd action is %s", dpd_action); | |
135 | } | |
136 | printf("\n"); | |
b1df6312 | 137 | |
51bdc1f3 MW |
138 | printf(" local: %s\n", child->get(child, "local-ts")); |
139 | printf(" remote: %s\n", child->get(child, "remote-ts")); | |
e9704e90 AS |
140 | |
141 | interface = child->get(child, "interface"); | |
142 | if (interface) | |
143 | { | |
144 | printf(" interface: %s\n", interface); | |
145 | } | |
146 | ||
147 | priority = child->get(child, "priority"); | |
148 | if (priority) | |
149 | { | |
150 | printf(" priority: %s\n", priority); | |
151 | } | |
51bdc1f3 MW |
152 | } |
153 | free_hashtable(child); | |
154 | return ret; | |
155 | } | |
156 | ||
157 | CALLBACK(conn_sn, int, | |
158 | hashtable_t *ike, vici_res_t *res, char *name) | |
159 | { | |
160 | int ret = 0; | |
161 | ||
162 | if (streq(name, "children")) | |
163 | { | |
4eaf08c3 | 164 | return vici_parse_cb(res, children_sn, NULL, NULL, ike); |
51bdc1f3 | 165 | } |
94bb26fa | 166 | if (strpfx(name, "local") || strpfx(name, "remote")) |
51bdc1f3 MW |
167 | { |
168 | hashtable_t *auth; | |
afcd4661 | 169 | char *class; |
51bdc1f3 MW |
170 | |
171 | auth = hashtable_create(hashtable_hash_str, hashtable_equals_str, 1); | |
172 | ret = vici_parse_cb(res, NULL, values, list, auth); | |
173 | if (ret == 0) | |
174 | { | |
afcd4661 AS |
175 | class = auth->get(auth, "class") ?: "unspecified"; |
176 | if (strcaseeq(class, "EAP")) | |
177 | { | |
178 | class = auth->get(auth, "eap-type") ?: class; | |
179 | } | |
51bdc1f3 | 180 | printf(" %s %s authentication:\n", |
afcd4661 | 181 | strpfx(name, "local") ? "local" : "remote", class); |
e88f21cf | 182 | if (auth->get(auth, "id")) |
51bdc1f3 MW |
183 | { |
184 | printf(" id: %s\n", auth->get(auth, "id")); | |
185 | } | |
e88f21cf AS |
186 | if (auth->get(auth, "eap_id")) |
187 | { | |
188 | printf(" eap_id: %s\n", auth->get(auth, "eap_id")); | |
189 | } | |
190 | if (auth->get(auth, "xauth_id")) | |
191 | { | |
192 | printf(" xauth_id: %s\n", auth->get(auth, "xauth_id")); | |
193 | } | |
194 | if (auth->get(auth, "aaa_id")) | |
195 | { | |
196 | printf(" aaa_id: %s\n", auth->get(auth, "aaa_id")); | |
197 | } | |
51bdc1f3 MW |
198 | if (auth->get(auth, "groups")) |
199 | { | |
200 | printf(" groups: %s\n", auth->get(auth, "groups")); | |
201 | } | |
202 | if (auth->get(auth, "certs")) | |
203 | { | |
204 | printf(" certs: %s\n", auth->get(auth, "certs")); | |
205 | } | |
206 | if (auth->get(auth, "cacerts")) | |
207 | { | |
208 | printf(" cacerts: %s\n", auth->get(auth, "cacerts")); | |
209 | } | |
210 | } | |
211 | free_hashtable(auth); | |
212 | } | |
213 | return ret; | |
214 | } | |
215 | ||
a2875525 MW |
216 | CALLBACK(conn_list, int, |
217 | hashtable_t *sa, vici_res_t *res, char *name, void *value, int len) | |
218 | { | |
219 | if (chunk_printable(chunk_create(value, len), NULL, ' ')) | |
220 | { | |
221 | if (streq(name, "local_addrs")) | |
222 | { | |
223 | printf(" local: %.*s\n", len, value); | |
224 | } | |
225 | if (streq(name, "remote_addrs")) | |
226 | { | |
227 | printf(" remote: %.*s\n", len, value); | |
228 | } | |
229 | } | |
230 | return 0; | |
231 | } | |
232 | ||
51bdc1f3 MW |
233 | CALLBACK(conns, int, |
234 | void *null, vici_res_t *res, char *name) | |
235 | { | |
4eaf08c3 AS |
236 | int ret; |
237 | char *version, *reauth_time, *rekey_time, *dpd_delay; | |
238 | hashtable_t *ike; | |
b1df6312 AS |
239 | |
240 | version = vici_find_str(res, "", "%s.version", name); | |
4eaf08c3 AS |
241 | reauth_time = vici_find_str(res, "0", "%s.reauth_time", name); |
242 | rekey_time = vici_find_str(res, "0", "%s.rekey_time", name); | |
243 | dpd_delay = vici_find_str(res, "0", "%s.dpd_delay", name); | |
244 | ||
245 | ike = hashtable_create(hashtable_hash_str, hashtable_equals_str, 1); | |
246 | free(ike->put(ike,"dpd_delay", strdup(dpd_delay))); | |
51bdc1f3 | 247 | |
b1df6312 AS |
248 | printf("%s: %s, ", name, version); |
249 | if (streq(version, "IKEv1")) | |
250 | { | |
251 | if (streq(reauth_time, "0")) | |
252 | { | |
253 | reauth_time = rekey_time; | |
254 | } | |
255 | } | |
256 | if (streq(reauth_time, "0")) | |
257 | { | |
258 | printf("no reauthentication"); | |
259 | } | |
260 | else | |
261 | { | |
262 | printf("reauthentication every %ss", reauth_time); | |
263 | } | |
4eaf08c3 | 264 | if (!streq(version, "IKEv1")) |
b1df6312 AS |
265 | { |
266 | if (streq(rekey_time, "0")) | |
267 | { | |
4eaf08c3 | 268 | printf(", no rekeying"); |
b1df6312 AS |
269 | } |
270 | else | |
271 | { | |
4eaf08c3 | 272 | printf(", rekeying every %ss", rekey_time); |
b1df6312 AS |
273 | } |
274 | } | |
4eaf08c3 AS |
275 | if (!streq(dpd_delay, "0")) |
276 | { | |
277 | printf(", dpd delay %ss", dpd_delay); | |
278 | } | |
279 | printf("\n"); | |
280 | ||
281 | ret = vici_parse_cb(res, conn_sn, NULL, conn_list, ike); | |
282 | free_hashtable(ike); | |
283 | return ret; | |
51bdc1f3 MW |
284 | } |
285 | ||
286 | CALLBACK(list_cb, void, | |
dacb75f5 | 287 | command_format_options_t *format, char *name, vici_res_t *res) |
51bdc1f3 | 288 | { |
dacb75f5 | 289 | if (*format & COMMAND_FORMAT_RAW) |
51bdc1f3 | 290 | { |
dacb75f5 AS |
291 | vici_dump(res, "list-conn event", *format & COMMAND_FORMAT_PRETTY, |
292 | stdout); | |
51bdc1f3 MW |
293 | } |
294 | else | |
295 | { | |
296 | if (vici_parse_cb(res, conns, NULL, NULL, NULL) != 0) | |
297 | { | |
298 | fprintf(stderr, "parsing conn event failed: %s\n", strerror(errno)); | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | static int list_conns(vici_conn_t *conn) | |
304 | { | |
305 | vici_req_t *req; | |
306 | vici_res_t *res; | |
dacb75f5 | 307 | command_format_options_t format = COMMAND_FORMAT_NONE; |
51bdc1f3 | 308 | char *arg; |
67f9f09d | 309 | int ret; |
51bdc1f3 MW |
310 | |
311 | while (TRUE) | |
312 | { | |
313 | switch (command_getopt(&arg)) | |
314 | { | |
315 | case 'h': | |
316 | return command_usage(NULL); | |
dacb75f5 AS |
317 | case 'P': |
318 | format |= COMMAND_FORMAT_PRETTY; | |
319 | /* fall through to raw */ | |
51bdc1f3 | 320 | case 'r': |
dacb75f5 | 321 | format |= COMMAND_FORMAT_RAW; |
51bdc1f3 MW |
322 | continue; |
323 | case EOF: | |
324 | break; | |
325 | default: | |
326 | return command_usage("invalid --list-conns option"); | |
327 | } | |
328 | break; | |
329 | } | |
dacb75f5 | 330 | if (vici_register(conn, "list-conn", list_cb, &format) != 0) |
51bdc1f3 | 331 | { |
67f9f09d | 332 | ret = errno; |
51bdc1f3 MW |
333 | fprintf(stderr, "registering for connections failed: %s\n", |
334 | strerror(errno)); | |
67f9f09d | 335 | return ret; |
51bdc1f3 MW |
336 | } |
337 | req = vici_begin("list-conns"); | |
338 | res = vici_submit(req, conn); | |
339 | if (!res) | |
340 | { | |
67f9f09d | 341 | ret = errno; |
51bdc1f3 | 342 | fprintf(stderr, "list-conns request failed: %s\n", strerror(errno)); |
67f9f09d | 343 | return ret; |
51bdc1f3 | 344 | } |
dacb75f5 | 345 | if (format & COMMAND_FORMAT_RAW) |
51bdc1f3 | 346 | { |
dacb75f5 AS |
347 | vici_dump(res, "list-conns reply", format & COMMAND_FORMAT_PRETTY, |
348 | stdout); | |
51bdc1f3 MW |
349 | } |
350 | vici_free_res(res); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | /** | |
355 | * Register the command. | |
356 | */ | |
357 | static void __attribute__ ((constructor))reg() | |
358 | { | |
359 | command_register((command_t) { | |
360 | list_conns, 'L', "list-conns", "list loaded configurations", | |
dacb75f5 | 361 | {"[--raw|--pretty]"}, |
51bdc1f3 MW |
362 | { |
363 | {"help", 'h', 0, "show usage information"}, | |
364 | {"raw", 'r', 0, "dump raw response message"}, | |
dacb75f5 | 365 | {"pretty", 'P', 0, "dump raw response message in pretty print"}, |
51bdc1f3 MW |
366 | } |
367 | }); | |
368 | } |