Implemented IPsec policies restricted to given network interface
[strongswan.git] / src / libcharon / plugins / vici / vici_query.c
1 /*
2 * Copyright (C) 2015 Tobias Brunner, Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2014 Martin Willi
6 * Copyright (C) 2014 revosec AG
7 *
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 /*
20 * Copyright (C) 2014 Timo Teräs <timo.teras@iki.fi>
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #include "vici_query.h"
42 #include "vici_builder.h"
43 #include "vici_cert_info.h"
44
45 #include <inttypes.h>
46 #include <time.h>
47 #ifndef WIN32
48 #include <sys/utsname.h>
49 #endif
50 #ifdef HAVE_MALLINFO
51 #include <malloc.h>
52 #endif
53
54 #include <daemon.h>
55 #include <asn1/asn1.h>
56 #include <credentials/certificates/certificate.h>
57 #include <credentials/certificates/x509.h>
58
59 typedef struct private_vici_query_t private_vici_query_t;
60
61 /**
62 * Private data of an vici_query_t object.
63 */
64 struct private_vici_query_t {
65
66 /**
67 * Public vici_query_t interface.
68 */
69 vici_query_t public;
70
71 /**
72 * Dispatcher
73 */
74 vici_dispatcher_t *dispatcher;
75
76 /**
77 * Daemon startup timestamp
78 */
79 time_t uptime;
80 };
81
82 /**
83 * List details of a CHILD_SA
84 */
85 static void list_child(private_vici_query_t *this, vici_builder_t *b,
86 child_sa_t *child, time_t now)
87 {
88 time_t t;
89 uint64_t bytes, packets;
90 uint16_t alg, ks;
91 proposal_t *proposal;
92 enumerator_t *enumerator;
93 traffic_selector_t *ts;
94
95 b->add_kv(b, "uniqueid", "%u", child->get_unique_id(child));
96 b->add_kv(b, "reqid", "%u", child->get_reqid(child));
97 b->add_kv(b, "state", "%N", child_sa_state_names, child->get_state(child));
98 b->add_kv(b, "mode", "%N", ipsec_mode_names, child->get_mode(child));
99 if (child->get_state(child) == CHILD_INSTALLED ||
100 child->get_state(child) == CHILD_REKEYING ||
101 child->get_state(child) == CHILD_REKEYED)
102 {
103 b->add_kv(b, "protocol", "%N", protocol_id_names,
104 child->get_protocol(child));
105 if (child->has_encap(child))
106 {
107 b->add_kv(b, "encap", "yes");
108 }
109 b->add_kv(b, "spi-in", "%.8x", ntohl(child->get_spi(child, TRUE)));
110 b->add_kv(b, "spi-out", "%.8x", ntohl(child->get_spi(child, FALSE)));
111
112 if (child->get_ipcomp(child) != IPCOMP_NONE)
113 {
114 b->add_kv(b, "cpi-in", "%.4x", ntohs(child->get_cpi(child, TRUE)));
115 b->add_kv(b, "cpi-out", "%.4x", ntohs(child->get_cpi(child, FALSE)));
116 }
117 proposal = child->get_proposal(child);
118 if (proposal)
119 {
120 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
121 &alg, &ks) && alg != ENCR_UNDEFINED)
122 {
123 b->add_kv(b, "encr-alg", "%N", encryption_algorithm_names, alg);
124 if (ks)
125 {
126 b->add_kv(b, "encr-keysize", "%u", ks);
127 }
128 }
129 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
130 &alg, &ks) && alg != AUTH_UNDEFINED)
131 {
132 b->add_kv(b, "integ-alg", "%N", integrity_algorithm_names, alg);
133 if (ks)
134 {
135 b->add_kv(b, "integ-keysize", "%u", ks);
136 }
137 }
138 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
139 &alg, NULL))
140 {
141 b->add_kv(b, "dh-group", "%N", diffie_hellman_group_names, alg);
142 }
143 if (proposal->get_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
144 &alg, NULL) && alg == EXT_SEQ_NUMBERS)
145 {
146 b->add_kv(b, "esn", "1");
147 }
148 }
149
150 child->get_usestats(child, TRUE, &t, &bytes, &packets);
151 b->add_kv(b, "bytes-in", "%" PRIu64, bytes);
152 b->add_kv(b, "packets-in", "%" PRIu64, packets);
153 if (t)
154 {
155 b->add_kv(b, "use-in", "%"PRIu64, (uint64_t)(now - t));
156 }
157
158 child->get_usestats(child, FALSE, &t, &bytes, &packets);
159 b->add_kv(b, "bytes-out", "%"PRIu64, bytes);
160 b->add_kv(b, "packets-out", "%"PRIu64, packets);
161 if (t)
162 {
163 b->add_kv(b, "use-out", "%"PRIu64, (uint64_t)(now - t));
164 }
165
166 t = child->get_lifetime(child, FALSE);
167 if (t)
168 {
169 b->add_kv(b, "rekey-time", "%"PRId64, (int64_t)(t - now));
170 }
171 t = child->get_lifetime(child, TRUE);
172 if (t)
173 {
174 b->add_kv(b, "life-time", "%"PRId64, (int64_t)(t - now));
175 }
176 t = child->get_installtime(child);
177 b->add_kv(b, "install-time", "%"PRId64, (int64_t)(now - t));
178 }
179
180 b->begin_list(b, "local-ts");
181 enumerator = child->create_ts_enumerator(child, TRUE);
182 while (enumerator->enumerate(enumerator, &ts))
183 {
184 b->add_li(b, "%R", ts);
185 }
186 enumerator->destroy(enumerator);
187 b->end_list(b /* local-ts */);
188
189 b->begin_list(b, "remote-ts");
190 enumerator = child->create_ts_enumerator(child, FALSE);
191 while (enumerator->enumerate(enumerator, &ts))
192 {
193 b->add_li(b, "%R", ts);
194 }
195 enumerator->destroy(enumerator);
196 b->end_list(b /* remote-ts */);
197 }
198
199 /**
200 * List tasks in a specific queue
201 */
202 static void list_task_queue(private_vici_query_t *this, vici_builder_t *b,
203 ike_sa_t *ike_sa, task_queue_t q, char *name)
204 {
205 enumerator_t *enumerator;
206 bool has = FALSE;
207 task_t *task;
208
209 enumerator = ike_sa->create_task_enumerator(ike_sa, q);
210 while (enumerator->enumerate(enumerator, &task))
211 {
212 if (!has)
213 {
214 b->begin_list(b, name);
215 has = TRUE;
216 }
217 b->add_li(b, "%N", task_type_names, task->get_type(task));
218 }
219 enumerator->destroy(enumerator);
220 if (has)
221 {
222 b->end_list(b);
223 }
224 }
225
226 /**
227 * Add an IKE_SA condition to the given builder
228 */
229 static void add_condition(vici_builder_t *b, ike_sa_t *ike_sa,
230 char *key, ike_condition_t cond)
231 {
232 if (ike_sa->has_condition(ike_sa, cond))
233 {
234 b->add_kv(b, key, "yes");
235 }
236 }
237
238 /**
239 * List virtual IPs
240 */
241 static void list_vips(private_vici_query_t *this, vici_builder_t *b,
242 ike_sa_t *ike_sa, bool local, char *name)
243 {
244 enumerator_t *enumerator;
245 bool has = FALSE;
246 host_t *vip;
247
248 enumerator = ike_sa->create_virtual_ip_enumerator(ike_sa, local);
249 while (enumerator->enumerate(enumerator, &vip))
250 {
251 if (!has)
252 {
253 b->begin_list(b, name);
254 has = TRUE;
255 }
256 b->add_li(b, "%H", vip);
257 }
258 enumerator->destroy(enumerator);
259 if (has)
260 {
261 b->end_list(b);
262 }
263 }
264
265 /**
266 * List details of an IKE_SA
267 */
268 static void list_ike(private_vici_query_t *this, vici_builder_t *b,
269 ike_sa_t *ike_sa, time_t now)
270 {
271 time_t t;
272 ike_sa_id_t *id;
273 identification_t *eap;
274 proposal_t *proposal;
275 uint16_t alg, ks;
276 host_t *host;
277
278 b->add_kv(b, "uniqueid", "%u", ike_sa->get_unique_id(ike_sa));
279 b->add_kv(b, "version", "%u", ike_sa->get_version(ike_sa));
280 b->add_kv(b, "state", "%N", ike_sa_state_names, ike_sa->get_state(ike_sa));
281
282 host = ike_sa->get_my_host(ike_sa);
283 b->add_kv(b, "local-host", "%H", host);
284 b->add_kv(b, "local-port", "%d", host->get_port(host));
285 b->add_kv(b, "local-id", "%Y", ike_sa->get_my_id(ike_sa));
286
287 host = ike_sa->get_other_host(ike_sa);
288 b->add_kv(b, "remote-host", "%H", host);
289 b->add_kv(b, "remote-port", "%d", host->get_port(host));
290 b->add_kv(b, "remote-id", "%Y", ike_sa->get_other_id(ike_sa));
291
292 eap = ike_sa->get_other_eap_id(ike_sa);
293
294 if (!eap->equals(eap, ike_sa->get_other_id(ike_sa)))
295 {
296 if (ike_sa->get_version(ike_sa) == IKEV1)
297 {
298 b->add_kv(b, "remote-xauth-id", "%Y", eap);
299 }
300 else
301 {
302 b->add_kv(b, "remote-eap-id", "%Y", eap);
303 }
304 }
305
306 id = ike_sa->get_id(ike_sa);
307 if (id->is_initiator(id))
308 {
309 b->add_kv(b, "initiator", "yes");
310 }
311 b->add_kv(b, "initiator-spi", "%.16"PRIx64,
312 be64toh(id->get_initiator_spi(id)));
313 b->add_kv(b, "responder-spi", "%.16"PRIx64,
314 be64toh(id->get_responder_spi(id)));
315
316 add_condition(b, ike_sa, "nat-local", COND_NAT_HERE);
317 add_condition(b, ike_sa, "nat-remote", COND_NAT_THERE);
318 add_condition(b, ike_sa, "nat-fake", COND_NAT_FAKE);
319 add_condition(b, ike_sa, "nat-any", COND_NAT_ANY);
320
321 proposal = ike_sa->get_proposal(ike_sa);
322 if (proposal)
323 {
324 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &ks))
325 {
326 b->add_kv(b, "encr-alg", "%N", encryption_algorithm_names, alg);
327 if (ks)
328 {
329 b->add_kv(b, "encr-keysize", "%u", ks);
330 }
331 }
332 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, &ks))
333 {
334 b->add_kv(b, "integ-alg", "%N", integrity_algorithm_names, alg);
335 if (ks)
336 {
337 b->add_kv(b, "integ-keysize", "%u", ks);
338 }
339 }
340 if (proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL))
341 {
342 b->add_kv(b, "prf-alg", "%N", pseudo_random_function_names, alg);
343 }
344 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP, &alg, NULL))
345 {
346 b->add_kv(b, "dh-group", "%N", diffie_hellman_group_names, alg);
347 }
348 }
349
350 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
351 {
352 t = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
353 b->add_kv(b, "established", "%"PRId64, (int64_t)(now - t));
354 t = ike_sa->get_statistic(ike_sa, STAT_REKEY);
355 if (t)
356 {
357 b->add_kv(b, "rekey-time", "%"PRId64, (int64_t)(t - now));
358 }
359 t = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
360 if (t)
361 {
362 b->add_kv(b, "reauth-time", "%"PRId64, (int64_t)(t - now));
363 }
364 }
365
366 list_vips(this, b, ike_sa, TRUE, "local-vips");
367 list_vips(this, b, ike_sa, FALSE, "remote-vips");
368
369 list_task_queue(this, b, ike_sa, TASK_QUEUE_QUEUED, "tasks-queued");
370 list_task_queue(this, b, ike_sa, TASK_QUEUE_ACTIVE, "tasks-active");
371 list_task_queue(this, b, ike_sa, TASK_QUEUE_PASSIVE, "tasks-passive");
372 }
373
374 CALLBACK(list_sas, vici_message_t*,
375 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
376 {
377 vici_builder_t *b;
378 enumerator_t *isas, *csas;
379 ike_sa_t *ike_sa;
380 child_sa_t *child_sa;
381 time_t now;
382 char *ike;
383 u_int ike_id;
384 bool bl;
385
386 bl = request->get_str(request, NULL, "noblock") == NULL;
387 ike = request->get_str(request, NULL, "ike");
388 ike_id = request->get_int(request, 0, "ike-id");
389
390 isas = charon->controller->create_ike_sa_enumerator(charon->controller, bl);
391 while (isas->enumerate(isas, &ike_sa))
392 {
393 if (ike && !streq(ike, ike_sa->get_name(ike_sa)))
394 {
395 continue;
396 }
397 if (ike_id && ike_id != ike_sa->get_unique_id(ike_sa))
398 {
399 continue;
400 }
401
402 now = time_monotonic(NULL);
403
404 b = vici_builder_create();
405 b->begin_section(b, ike_sa->get_name(ike_sa));
406
407 list_ike(this, b, ike_sa, now);
408
409 b->begin_section(b, "child-sas");
410 csas = ike_sa->create_child_sa_enumerator(ike_sa);
411 while (csas->enumerate(csas, &child_sa))
412 {
413 b->begin_section(b, child_sa->get_name(child_sa));
414 list_child(this, b, child_sa, now);
415 b->end_section(b);
416 }
417 csas->destroy(csas);
418 b->end_section(b /* child-sas */ );
419
420 b->end_section(b);
421
422 this->dispatcher->raise_event(this->dispatcher, "list-sa", id,
423 b->finalize(b));
424 }
425 isas->destroy(isas);
426
427 b = vici_builder_create();
428 return b->finalize(b);
429 }
430
431 /**
432 * Raise a list-policy event for given CHILD_SA
433 */
434 static void raise_policy(private_vici_query_t *this, u_int id, child_sa_t *child)
435 {
436 enumerator_t *enumerator;
437 traffic_selector_t *ts;
438 vici_builder_t *b;
439
440 b = vici_builder_create();
441 b->begin_section(b, child->get_name(child));
442
443 b->add_kv(b, "mode", "%N", ipsec_mode_names, child->get_mode(child));
444
445 b->begin_list(b, "local-ts");
446 enumerator = child->create_ts_enumerator(child, TRUE);
447 while (enumerator->enumerate(enumerator, &ts))
448 {
449 b->add_li(b, "%R", ts);
450 }
451 enumerator->destroy(enumerator);
452 b->end_list(b /* local-ts */);
453
454 b->begin_list(b, "remote-ts");
455 enumerator = child->create_ts_enumerator(child, FALSE);
456 while (enumerator->enumerate(enumerator, &ts))
457 {
458 b->add_li(b, "%R", ts);
459 }
460 enumerator->destroy(enumerator);
461 b->end_list(b /* remote-ts */);
462
463 b->end_section(b);
464
465 this->dispatcher->raise_event(this->dispatcher, "list-policy", id,
466 b->finalize(b));
467 }
468
469 /**
470 * Raise a list-policy event for given CHILD_SA config
471 */
472 static void raise_policy_cfg(private_vici_query_t *this, u_int id,
473 child_cfg_t *cfg)
474 {
475 enumerator_t *enumerator;
476 linked_list_t *list;
477 traffic_selector_t *ts;
478 vici_builder_t *b;
479
480 b = vici_builder_create();
481 b->begin_section(b, cfg->get_name(cfg));
482
483 b->add_kv(b, "mode", "%N", ipsec_mode_names, cfg->get_mode(cfg));
484
485 b->begin_list(b, "local-ts");
486 list = cfg->get_traffic_selectors(cfg, TRUE, NULL, NULL);
487 enumerator = list->create_enumerator(list);
488 while (enumerator->enumerate(enumerator, &ts))
489 {
490 b->add_li(b, "%R", ts);
491 }
492 enumerator->destroy(enumerator);
493 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
494 b->end_list(b /* local-ts */);
495
496 b->begin_list(b, "remote-ts");
497 list = cfg->get_traffic_selectors(cfg, FALSE, NULL, NULL);
498 enumerator = list->create_enumerator(list);
499 while (enumerator->enumerate(enumerator, &ts))
500 {
501 b->add_li(b, "%R", ts);
502 }
503 enumerator->destroy(enumerator);
504 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
505 b->end_list(b /* remote-ts */);
506
507 b->end_section(b);
508
509 this->dispatcher->raise_event(this->dispatcher, "list-policy", id,
510 b->finalize(b));
511 }
512
513 CALLBACK(list_policies, vici_message_t*,
514 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
515 {
516 enumerator_t *enumerator;
517 vici_builder_t *b;
518 child_sa_t *child_sa;
519 child_cfg_t *child_cfg;
520 bool drop, pass, trap;
521 char *child;
522
523 drop = request->get_str(request, NULL, "drop") != NULL;
524 pass = request->get_str(request, NULL, "pass") != NULL;
525 trap = request->get_str(request, NULL, "trap") != NULL;
526 child = request->get_str(request, NULL, "child");
527
528 if (trap)
529 {
530 enumerator = charon->traps->create_enumerator(charon->traps);
531 while (enumerator->enumerate(enumerator, NULL, &child_sa))
532 {
533 if (child && !streq(child, child_sa->get_name(child_sa)))
534 {
535 continue;
536 }
537 raise_policy(this, id, child_sa);
538 }
539 enumerator->destroy(enumerator);
540 }
541
542 if (drop || pass)
543 {
544 enumerator = charon->shunts->create_enumerator(charon->shunts);
545 while (enumerator->enumerate(enumerator, &child_cfg))
546 {
547 if (child && !streq(child, child_cfg->get_name(child_cfg)))
548 {
549 continue;
550 }
551 switch (child_cfg->get_mode(child_cfg))
552 {
553 case MODE_DROP:
554 if (drop)
555 {
556 raise_policy_cfg(this, id, child_cfg);
557 }
558 break;
559 case MODE_PASS:
560 if (pass)
561 {
562 raise_policy_cfg(this, id, child_cfg);
563 }
564 break;
565 default:
566 break;
567 }
568 }
569 enumerator->destroy(enumerator);
570 }
571
572 b = vici_builder_create();
573 return b->finalize(b);
574 }
575
576 /**
577 * Build sections for auth configs, local or remote
578 */
579 static void build_auth_cfgs(peer_cfg_t *peer_cfg, bool local, vici_builder_t *b)
580 {
581 enumerator_t *enumerator, *rules;
582 auth_rule_t rule;
583 auth_cfg_t *auth;
584 union {
585 uintptr_t u;
586 identification_t *id;
587 certificate_t *cert;
588 char *str;
589 } v;
590 char buf[32];
591 int i = 0;
592
593 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
594 while (enumerator->enumerate(enumerator, &auth))
595 {
596 snprintf(buf, sizeof(buf), "%s-%d", local ? "local" : "remote", ++i);
597 b->begin_section(b, buf);
598
599 rules = auth->create_enumerator(auth);
600 while (rules->enumerate(rules, &rule, &v))
601 {
602 switch (rule)
603 {
604 case AUTH_RULE_AUTH_CLASS:
605 b->add_kv(b, "class", "%N", auth_class_names, v.u);
606 break;
607 case AUTH_RULE_EAP_TYPE:
608 b->add_kv(b, "eap-type", "%N", eap_type_names, v.u);
609 break;
610 case AUTH_RULE_EAP_VENDOR:
611 b->add_kv(b, "eap-vendor", "%u", v.u);
612 break;
613 case AUTH_RULE_XAUTH_BACKEND:
614 b->add_kv(b, "xauth", "%s", v.str);
615 break;
616 case AUTH_RULE_CRL_VALIDATION:
617 b->add_kv(b, "revocation", "%N", cert_validation_names, v.u);
618 break;
619 case AUTH_RULE_IDENTITY:
620 b->add_kv(b, "id", "%Y", v.id);
621 break;
622 case AUTH_RULE_AAA_IDENTITY:
623 b->add_kv(b, "aaa_id", "%Y", v.id);
624 break;
625 case AUTH_RULE_EAP_IDENTITY:
626 b->add_kv(b, "eap_id", "%Y", v.id);
627 break;
628 case AUTH_RULE_XAUTH_IDENTITY:
629 b->add_kv(b, "xauth_id", "%Y", v.id);
630 break;
631 default:
632 break;
633 }
634 }
635 rules->destroy(rules);
636
637 b->begin_list(b, "groups");
638 rules = auth->create_enumerator(auth);
639 while (rules->enumerate(rules, &rule, &v))
640 {
641 if (rule == AUTH_RULE_GROUP)
642 {
643 b->add_li(b, "%Y", v.id);
644 }
645 }
646 rules->destroy(rules);
647 b->end_list(b);
648
649 b->begin_list(b, "certs");
650 rules = auth->create_enumerator(auth);
651 while (rules->enumerate(rules, &rule, &v))
652 {
653 if (rule == AUTH_RULE_SUBJECT_CERT)
654 {
655 b->add_li(b, "%Y", v.cert->get_subject(v.cert));
656 }
657 }
658 rules->destroy(rules);
659 b->end_list(b);
660
661 b->begin_list(b, "cacerts");
662 rules = auth->create_enumerator(auth);
663 while (rules->enumerate(rules, &rule, &v))
664 {
665 if (rule == AUTH_RULE_CA_CERT)
666 {
667 b->add_li(b, "%Y", v.cert->get_subject(v.cert));
668 }
669 }
670 rules->destroy(rules);
671 b->end_list(b);
672
673 b->end_section(b);
674 }
675 enumerator->destroy(enumerator);
676 }
677
678 CALLBACK(list_conns, vici_message_t*,
679 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
680 {
681 enumerator_t *enumerator, *tokens, *selectors, *children;
682 peer_cfg_t *peer_cfg;
683 ike_cfg_t *ike_cfg;
684 child_cfg_t *child_cfg;
685 char *ike, *str;
686 linked_list_t *list;
687 traffic_selector_t *ts;
688 vici_builder_t *b;
689
690 ike = request->get_str(request, NULL, "ike");
691
692 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends,
693 NULL, NULL, NULL, NULL, IKE_ANY);
694 while (enumerator->enumerate(enumerator, &peer_cfg))
695 {
696 if (ike && !streq(ike, peer_cfg->get_name(peer_cfg)))
697 {
698 continue;
699 }
700
701 b = vici_builder_create();
702 b->begin_section(b, peer_cfg->get_name(peer_cfg));
703
704 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
705
706 b->begin_list(b, "local_addrs");
707 str = ike_cfg->get_my_addr(ike_cfg);
708 tokens = enumerator_create_token(str, ",", " ");
709 while (tokens->enumerate(tokens, &str))
710 {
711 b->add_li(b, "%s", str);
712 }
713 tokens->destroy(tokens);
714 b->end_list(b);
715
716 b->begin_list(b, "remote_addrs");
717 str = ike_cfg->get_other_addr(ike_cfg);
718 tokens = enumerator_create_token(str, ",", " ");
719 while (tokens->enumerate(tokens, &str))
720 {
721 b->add_li(b, "%s", str);
722 }
723 tokens->destroy(tokens);
724 b->end_list(b);
725
726 b->add_kv(b, "version", "%N", ike_version_names,
727 peer_cfg->get_ike_version(peer_cfg));
728
729 build_auth_cfgs(peer_cfg, TRUE, b);
730 build_auth_cfgs(peer_cfg, FALSE, b);
731
732 b->begin_section(b, "children");
733
734 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
735 while (children->enumerate(children, &child_cfg))
736 {
737 b->begin_section(b, child_cfg->get_name(child_cfg));
738
739 b->add_kv(b, "mode", "%N", ipsec_mode_names,
740 child_cfg->get_mode(child_cfg));
741
742 b->begin_list(b, "local-ts");
743 list = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
744 selectors = list->create_enumerator(list);
745 while (selectors->enumerate(selectors, &ts))
746 {
747 b->add_li(b, "%R", ts);
748 }
749 selectors->destroy(selectors);
750 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
751 b->end_list(b /* local-ts */);
752
753 b->begin_list(b, "remote-ts");
754 list = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
755 selectors = list->create_enumerator(list);
756 while (selectors->enumerate(selectors, &ts))
757 {
758 b->add_li(b, "%R", ts);
759 }
760 selectors->destroy(selectors);
761 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
762 b->end_list(b /* remote-ts */);
763
764 b->end_section(b);
765 }
766 children->destroy(children);
767
768 b->end_section(b); /* children */
769
770 b->end_section(b); /* name */
771
772 this->dispatcher->raise_event(this->dispatcher, "list-conn", id,
773 b->finalize(b));
774 }
775 enumerator->destroy(enumerator);
776
777 b = vici_builder_create();
778 return b->finalize(b);
779 }
780
781 /**
782 * Do we have a private key for given certificate
783 */
784 static bool has_privkey(certificate_t *cert)
785 {
786 private_key_t *private;
787 public_key_t *public;
788 identification_t *keyid;
789 chunk_t chunk;
790 bool found = FALSE;
791
792 public = cert->get_public_key(cert);
793 if (public)
794 {
795 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
796 {
797 keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
798 private = lib->credmgr->get_private(lib->credmgr,
799 public->get_type(public), keyid, NULL);
800 if (private)
801 {
802 found = TRUE;
803 private->destroy(private);
804 }
805 keyid->destroy(keyid);
806 }
807 public->destroy(public);
808 }
809 return found;
810 }
811
812 /**
813 * Store cert filter data
814 */
815 typedef struct {
816 certificate_type_t type;
817 x509_flag_t flag;
818 identification_t *subject;
819 } cert_filter_t;
820
821 /**
822 * Enumerate all X.509 certificates with a given flag
823 */
824 static void enum_x509(private_vici_query_t *this, u_int id,
825 linked_list_t *certs, cert_filter_t *filter,
826 x509_flag_t flag)
827 {
828 enumerator_t *enumerator;
829 certificate_t *cert;
830 vici_builder_t *b;
831 chunk_t encoding;
832 x509_t *x509;
833
834 if (filter->type != CERT_ANY && filter->flag != X509_ANY &&
835 filter->flag != flag)
836 {
837 return;
838 }
839
840 enumerator = certs->create_enumerator(certs);
841 while (enumerator->enumerate(enumerator, &cert))
842 {
843 x509 = (x509_t*)cert;
844 if ((x509->get_flags(x509) & X509_ANY) != flag)
845 {
846 continue;
847 }
848
849 if (cert->get_encoding(cert, CERT_ASN1_DER, &encoding))
850 {
851 b = vici_builder_create();
852 b->add_kv(b, "type", "%N", certificate_type_names, CERT_X509);
853 b->add_kv(b, "flag", "%N", x509_flag_names, flag);
854 if (has_privkey(cert))
855 {
856 b->add_kv(b, "has_privkey", "yes");
857 }
858 b->add(b, VICI_KEY_VALUE, "data", encoding);
859 free(encoding.ptr);
860
861 this->dispatcher->raise_event(this->dispatcher, "list-cert", id,
862 b->finalize(b));
863 }
864 }
865 enumerator->destroy(enumerator);
866 }
867
868 /**
869 * Enumerate all non-X.509 certificate types
870 */
871 static void enum_others(private_vici_query_t *this, u_int id,
872 linked_list_t *certs, certificate_type_t type)
873 {
874 enumerator_t *enumerator;
875 certificate_t *cert;
876 vici_builder_t *b;
877 chunk_t encoding, t_ch;
878 cred_encoding_type_t encoding_type;
879 identification_t *subject;
880 time_t not_before, not_after;
881
882 encoding_type = (type == CERT_TRUSTED_PUBKEY) ? PUBKEY_SPKI_ASN1_DER :
883 CERT_ASN1_DER;
884
885 enumerator = certs->create_enumerator(certs);
886 while (enumerator->enumerate(enumerator, &cert))
887 {
888 if (cert->get_encoding(cert, encoding_type, &encoding))
889 {
890 b = vici_builder_create();
891 b->add_kv(b, "type", "%N", certificate_type_names, type);
892 if (has_privkey(cert))
893 {
894 b->add_kv(b, "has_privkey", "yes");
895 }
896 b->add(b, VICI_KEY_VALUE, "data", encoding);
897 free(encoding.ptr);
898
899 if (type == CERT_TRUSTED_PUBKEY)
900 {
901 subject = cert->get_subject(cert);
902 if (subject->get_type(subject) != ID_KEY_ID)
903 {
904 b->add_kv(b, "subject", "%Y", cert->get_subject(cert));
905 }
906 cert->get_validity(cert, NULL, &not_before, &not_after);
907 if (not_before != UNDEFINED_TIME)
908 {
909 t_ch = asn1_from_time(&not_before, ASN1_GENERALIZEDTIME);
910 b->add(b, VICI_KEY_VALUE, "not-before", chunk_skip(t_ch, 2));
911 chunk_free(&t_ch);
912 }
913 if (not_after != UNDEFINED_TIME)
914 {
915 t_ch = asn1_from_time(&not_after, ASN1_GENERALIZEDTIME);
916 b->add(b, VICI_KEY_VALUE, "not-after", chunk_skip(t_ch, 2));
917 chunk_free(&t_ch);
918 }
919 }
920 this->dispatcher->raise_event(this->dispatcher, "list-cert", id,
921 b->finalize(b));
922 }
923 }
924 enumerator->destroy(enumerator);
925 }
926
927 /**
928 * Enumerate all certificates of a given type
929 */
930 static void enum_certs(private_vici_query_t *this, u_int id,
931 cert_filter_t *filter, certificate_type_t type)
932 {
933 enumerator_t *e1, *e2;
934 certificate_t *cert, *current;
935 linked_list_t *certs;
936 bool found;
937
938 if (filter->type != CERT_ANY && filter->type != type)
939 {
940 return;
941 }
942 certs = linked_list_create();
943
944 e1 = lib->credmgr->create_cert_enumerator(lib->credmgr, type, KEY_ANY,
945 filter->subject, FALSE);
946 while (e1->enumerate(e1, &cert))
947 {
948 found = FALSE;
949
950 e2 = certs->create_enumerator(certs);
951 while (e2->enumerate(e2, &current))
952 {
953 if (current->equals(current, cert))
954 {
955 found = TRUE;
956 break;
957 }
958 }
959 e2->destroy(e2);
960
961 if (!found)
962 {
963 certs->insert_last(certs, cert->get_ref(cert));
964 }
965 }
966 e1->destroy(e1);
967
968 if (type == CERT_X509)
969 {
970 enum_x509(this, id, certs, filter, X509_NONE);
971 enum_x509(this, id, certs, filter, X509_CA);
972 enum_x509(this, id, certs, filter, X509_AA);
973 enum_x509(this, id, certs, filter, X509_OCSP_SIGNER);
974 }
975 else
976 {
977 enum_others(this, id, certs, type);
978 }
979 certs->destroy_offset(certs, offsetof(certificate_t, destroy));
980 }
981
982 CALLBACK(list_certs, vici_message_t*,
983 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
984 {
985 cert_filter_t filter = {
986 .type = CERT_ANY,
987 .flag = X509_ANY,
988 .subject = NULL
989 };
990 vici_builder_t *b;
991 char *str;
992
993 str = request->get_str(request, "ANY", "type");
994 if (enum_from_name(certificate_type_names, str, &filter.type))
995 {
996 if (filter.type == CERT_X509)
997 {
998 str = request->get_str(request, "ANY", "flag");
999 if (!enum_from_name(x509_flag_names, str, &filter.flag))
1000 {
1001 DBG1(DBG_CFG, "invalid certificate flag '%s'", str);
1002 goto finalize;
1003 }
1004 }
1005 }
1006 else if (!vici_cert_info_from_str(str, &filter.type, &filter.flag))
1007 {
1008 DBG1(DBG_CFG, "invalid certificate type '%s'", str);
1009 goto finalize;
1010 }
1011
1012 str = request->get_str(request, NULL, "subject");
1013 if (str)
1014 {
1015 filter.subject = identification_create_from_string(str);
1016 }
1017
1018 enum_certs(this, id, &filter, CERT_TRUSTED_PUBKEY);
1019 enum_certs(this, id, &filter, CERT_X509);
1020 enum_certs(this, id, &filter, CERT_X509_AC);
1021 enum_certs(this, id, &filter, CERT_X509_CRL);
1022 enum_certs(this, id, &filter, CERT_X509_OCSP_RESPONSE);
1023 DESTROY_IF(filter.subject);
1024
1025 finalize:
1026 b = vici_builder_create();
1027 return b->finalize(b);
1028 }
1029
1030 /**
1031 * Add a key/value pair of ALG => plugin
1032 */
1033 static void add_algorithm(vici_builder_t *b, enum_name_t *alg_names,
1034 int alg_type, const char *plugin_name)
1035 {
1036 char alg_name[BUF_LEN];
1037
1038 sprintf(alg_name, "%N", alg_names, alg_type);
1039 b->add_kv(b, alg_name, (char*)plugin_name);
1040 }
1041
1042 CALLBACK(get_algorithms, vici_message_t*,
1043 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
1044 {
1045 vici_builder_t *b;
1046 enumerator_t *enumerator;
1047 encryption_algorithm_t encryption;
1048 integrity_algorithm_t integrity;
1049 hash_algorithm_t hash;
1050 pseudo_random_function_t prf;
1051 diffie_hellman_group_t group;
1052 rng_quality_t quality;
1053 const char *plugin_name;
1054
1055 b = vici_builder_create();
1056
1057 b->begin_section(b, "encryption");
1058 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1059 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1060 {
1061 add_algorithm(b, encryption_algorithm_names, encryption, plugin_name);
1062 }
1063 enumerator->destroy(enumerator);
1064 b->end_section(b);
1065
1066 b->begin_section(b, "integrity");
1067 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1068 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
1069 {
1070 add_algorithm(b, integrity_algorithm_names, integrity, plugin_name);
1071 }
1072 enumerator->destroy(enumerator);
1073 b->end_section(b);
1074
1075 b->begin_section(b, "aead");
1076 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
1077 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1078 {
1079 add_algorithm(b, encryption_algorithm_names, encryption, plugin_name);
1080 }
1081 enumerator->destroy(enumerator);
1082 b->end_section(b);
1083
1084 b->begin_section(b, "hasher");
1085 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1086 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
1087 {
1088 add_algorithm(b, hash_algorithm_names, hash, plugin_name);
1089 }
1090 enumerator->destroy(enumerator);
1091 b->end_section(b);
1092
1093 b->begin_section(b, "prf");
1094 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1095 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
1096 {
1097 add_algorithm(b, pseudo_random_function_names, prf, plugin_name);
1098 }
1099 enumerator->destroy(enumerator);
1100 b->end_section(b);
1101
1102 b->begin_section(b, "dh");
1103 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1104 while (enumerator->enumerate(enumerator, &group, &plugin_name))
1105 {
1106 add_algorithm(b, diffie_hellman_group_names, group, plugin_name);
1107 }
1108 enumerator->destroy(enumerator);
1109 b->end_section(b);
1110
1111 b->begin_section(b, "rng");
1112 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
1113 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
1114 {
1115 add_algorithm(b, rng_quality_names, quality, plugin_name);
1116 }
1117 enumerator->destroy(enumerator);
1118 b->end_section(b);
1119
1120 b->begin_section(b, "nonce-gen");
1121 enumerator = lib->crypto->create_nonce_gen_enumerator(lib->crypto);
1122 while (enumerator->enumerate(enumerator, &plugin_name))
1123 {
1124 b->add_kv(b, "NONCE_GEN", (char*)plugin_name);
1125 }
1126 enumerator->destroy(enumerator);
1127 b->end_section(b);
1128
1129 return b->finalize(b);
1130 }
1131
1132 CALLBACK(version, vici_message_t*,
1133 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
1134 {
1135 vici_builder_t *b;
1136
1137 b = vici_builder_create();
1138 b->add_kv(b, "daemon", "%s", lib->ns);
1139 b->add_kv(b, "version", "%s", VERSION);
1140
1141 #ifdef WIN32
1142 {
1143 OSVERSIONINFOEX osvie;
1144
1145 memset(&osvie, 0, sizeof(osvie));
1146 osvie.dwOSVersionInfoSize = sizeof(osvie);
1147
1148 if (GetVersionEx((LPOSVERSIONINFO)&osvie))
1149 {
1150 b->add_kv(b, "sysname", "Windows %s",
1151 osvie.wProductType == VER_NT_WORKSTATION ? "Client" : "Server");
1152 b->add_kv(b, "release", "%d.%d.%d (SP %d.%d)",
1153 osvie.dwMajorVersion, osvie.dwMinorVersion, osvie.dwBuildNumber,
1154 osvie.wServicePackMajor, osvie.wServicePackMinor);
1155 b->add_kv(b, "machine", "%s",
1156 #ifdef WIN64
1157 "x86_64");
1158 #else
1159 "x86");
1160 #endif /* !WIN64 */
1161 }
1162 }
1163 #else /* !WIN32 */
1164 {
1165 struct utsname utsname;
1166
1167 if (uname(&utsname) == 0)
1168 {
1169 b->add_kv(b, "sysname", "%s", utsname.sysname);
1170 b->add_kv(b, "release", "%s", utsname.release);
1171 b->add_kv(b, "machine", "%s", utsname.machine);
1172 }
1173 }
1174 #endif /* !WIN32 */
1175 return b->finalize(b);
1176 }
1177
1178 CALLBACK(stats, vici_message_t*,
1179 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
1180 {
1181 vici_builder_t *b;
1182 enumerator_t *enumerator;
1183 plugin_t *plugin;
1184 time_t since, now;
1185 int i;
1186
1187 b = vici_builder_create();
1188
1189 now = time_monotonic(NULL);
1190 since = time(NULL) - (now - this->uptime);
1191
1192 b->begin_section(b, "uptime");
1193 b->add_kv(b, "running", "%V", &now, &this->uptime);
1194 b->add_kv(b, "since", "%T", &since, FALSE);
1195 b->end_section(b);
1196
1197 b->begin_section(b, "workers");
1198 b->add_kv(b, "total", "%d",
1199 lib->processor->get_total_threads(lib->processor));
1200 b->add_kv(b, "idle", "%d",
1201 lib->processor->get_idle_threads(lib->processor));
1202 b->begin_section(b, "active");
1203 for (i = 0; i < JOB_PRIO_MAX; i++)
1204 {
1205 b->add_kv(b, enum_to_name(job_priority_names, i), "%d",
1206 lib->processor->get_working_threads(lib->processor, i));
1207 }
1208 b->end_section(b);
1209 b->end_section(b);
1210
1211 b->begin_section(b, "queues");
1212 for (i = 0; i < JOB_PRIO_MAX; i++)
1213 {
1214 b->add_kv(b, enum_to_name(job_priority_names, i), "%d",
1215 lib->processor->get_job_load(lib->processor, i));
1216 }
1217 b->end_section(b);
1218
1219 b->add_kv(b, "scheduled", "%d",
1220 lib->scheduler->get_job_load(lib->scheduler));
1221
1222 b->begin_section(b, "ikesas");
1223 b->add_kv(b, "total", "%u",
1224 charon->ike_sa_manager->get_count(charon->ike_sa_manager));
1225 b->add_kv(b, "half-open", "%u",
1226 charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
1227 NULL, FALSE));
1228 b->end_section(b);
1229
1230 b->begin_list(b, "plugins");
1231 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
1232 while (enumerator->enumerate(enumerator, &plugin, NULL))
1233 {
1234 b->add_li(b, "%s", plugin->get_name(plugin));
1235 }
1236 enumerator->destroy(enumerator);
1237 b->end_list(b);
1238
1239 #ifdef WIN32
1240 {
1241 DWORD lasterr = ERROR_INVALID_HANDLE;
1242 HANDLE heaps[32];
1243 int i, count;
1244 char buf[16];
1245 size_t total = 0;
1246 int allocs = 0;
1247
1248 b->begin_section(b, "mem");
1249 count = GetProcessHeaps(countof(heaps), heaps);
1250 for (i = 0; i < count; i++)
1251 {
1252 PROCESS_HEAP_ENTRY entry = {};
1253 size_t heap_total = 0;
1254 int heap_allocs = 0;
1255
1256 if (HeapLock(heaps[i]))
1257 {
1258 while (HeapWalk(heaps[i], &entry))
1259 {
1260 if (entry.wFlags & PROCESS_HEAP_ENTRY_BUSY)
1261 {
1262 heap_total += entry.cbData;
1263 heap_allocs++;
1264 }
1265 }
1266 lasterr = GetLastError();
1267 HeapUnlock(heaps[i]);
1268 }
1269 if (lasterr != ERROR_NO_MORE_ITEMS)
1270 {
1271 break;
1272 }
1273 snprintf(buf, sizeof(buf), "heap-%d", i);
1274 b->begin_section(b, buf);
1275 b->add_kv(b, "total", "%zu", heap_total);
1276 b->add_kv(b, "allocs", "%d", heap_allocs);
1277 b->end_section(b);
1278
1279 total += heap_total;
1280 allocs += heap_allocs;
1281 }
1282 if (lasterr == ERROR_NO_MORE_ITEMS)
1283 {
1284 b->add_kv(b, "total", "%zu", total);
1285 b->add_kv(b, "allocs", "%d", allocs);
1286 }
1287 b->end_section(b);
1288 }
1289 #endif
1290
1291 #ifdef HAVE_MALLINFO
1292 {
1293 struct mallinfo mi = mallinfo();
1294
1295 b->begin_section(b, "mallinfo");
1296 b->add_kv(b, "sbrk", "%u", mi.arena);
1297 b->add_kv(b, "mmap", "%u", mi.hblkhd);
1298 b->add_kv(b, "used", "%u", mi.uordblks);
1299 b->add_kv(b, "free", "%u", mi.fordblks);
1300 b->end_section(b);
1301 }
1302 #endif /* HAVE_MALLINFO */
1303
1304 return b->finalize(b);
1305 }
1306
1307 static void manage_command(private_vici_query_t *this,
1308 char *name, vici_command_cb_t cb, bool reg)
1309 {
1310 this->dispatcher->manage_command(this->dispatcher, name,
1311 reg ? cb : NULL, this);
1312 }
1313
1314 /**
1315 * (Un-)register dispatcher functions
1316 */
1317 static void manage_commands(private_vici_query_t *this, bool reg)
1318 {
1319 this->dispatcher->manage_event(this->dispatcher, "list-sa", reg);
1320 this->dispatcher->manage_event(this->dispatcher, "list-policy", reg);
1321 this->dispatcher->manage_event(this->dispatcher, "list-conn", reg);
1322 this->dispatcher->manage_event(this->dispatcher, "list-cert", reg);
1323 this->dispatcher->manage_event(this->dispatcher, "ike-updown", reg);
1324 this->dispatcher->manage_event(this->dispatcher, "ike-rekey", reg);
1325 this->dispatcher->manage_event(this->dispatcher, "child-updown", reg);
1326 this->dispatcher->manage_event(this->dispatcher, "child-rekey", reg);
1327 manage_command(this, "list-sas", list_sas, reg);
1328 manage_command(this, "list-policies", list_policies, reg);
1329 manage_command(this, "list-conns", list_conns, reg);
1330 manage_command(this, "list-certs", list_certs, reg);
1331 manage_command(this, "get-algorithms", get_algorithms, reg);
1332 manage_command(this, "version", version, reg);
1333 manage_command(this, "stats", stats, reg);
1334 }
1335
1336 METHOD(listener_t, ike_updown, bool,
1337 private_vici_query_t *this, ike_sa_t *ike_sa, bool up)
1338 {
1339 vici_builder_t *b;
1340 time_t now;
1341
1342 if (!this->dispatcher->has_event_listeners(this->dispatcher, "ike-updown"))
1343 {
1344 return TRUE;
1345 }
1346
1347 now = time_monotonic(NULL);
1348
1349 b = vici_builder_create();
1350
1351 if (up)
1352 {
1353 b->add_kv(b, "up", "yes");
1354 }
1355
1356 b->begin_section(b, ike_sa->get_name(ike_sa));
1357 list_ike(this, b, ike_sa, now);
1358 b->end_section(b);
1359
1360 this->dispatcher->raise_event(this->dispatcher,
1361 "ike-updown", 0, b->finalize(b));
1362
1363 return TRUE;
1364 }
1365
1366 METHOD(listener_t, ike_rekey, bool,
1367 private_vici_query_t *this, ike_sa_t *old, ike_sa_t *new)
1368 {
1369 vici_builder_t *b;
1370 time_t now;
1371
1372 if (!this->dispatcher->has_event_listeners(this->dispatcher, "ike-rekey"))
1373 {
1374 return TRUE;
1375 }
1376
1377 now = time_monotonic(NULL);
1378
1379 b = vici_builder_create();
1380 b->begin_section(b, old->get_name(old));
1381 b->begin_section(b, "old");
1382 list_ike(this, b, old, now);
1383 b->end_section(b);
1384 b->begin_section(b, "new");
1385 list_ike(this, b, new, now);
1386 b->end_section(b);
1387 b->end_section(b);
1388
1389 this->dispatcher->raise_event(this->dispatcher,
1390 "ike-rekey", 0, b->finalize(b));
1391
1392 return TRUE;
1393 }
1394
1395 METHOD(listener_t, child_updown, bool,
1396 private_vici_query_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa, bool up)
1397 {
1398 vici_builder_t *b;
1399 time_t now;
1400
1401 if (!this->dispatcher->has_event_listeners(this->dispatcher, "child-updown"))
1402 {
1403 return TRUE;
1404 }
1405
1406 now = time_monotonic(NULL);
1407 b = vici_builder_create();
1408
1409 if (up)
1410 {
1411 b->add_kv(b, "up", "yes");
1412 }
1413
1414 b->begin_section(b, ike_sa->get_name(ike_sa));
1415 list_ike(this, b, ike_sa, now);
1416 b->begin_section(b, "child-sas");
1417
1418 b->begin_section(b, child_sa->get_name(child_sa));
1419 list_child(this, b, child_sa, now);
1420 b->end_section(b);
1421
1422 b->end_section(b);
1423 b->end_section(b);
1424
1425 this->dispatcher->raise_event(this->dispatcher,
1426 "child-updown", 0, b->finalize(b));
1427
1428 return TRUE;
1429 }
1430
1431 METHOD(listener_t, child_rekey, bool,
1432 private_vici_query_t *this, ike_sa_t *ike_sa, child_sa_t *old,
1433 child_sa_t *new)
1434 {
1435 vici_builder_t *b;
1436 time_t now;
1437
1438 if (!this->dispatcher->has_event_listeners(this->dispatcher, "child-rekey"))
1439 {
1440 return TRUE;
1441 }
1442
1443 now = time_monotonic(NULL);
1444 b = vici_builder_create();
1445
1446 b->begin_section(b, ike_sa->get_name(ike_sa));
1447 list_ike(this, b, ike_sa, now);
1448 b->begin_section(b, "child-sas");
1449
1450 b->begin_section(b, old->get_name(old));
1451
1452 b->begin_section(b, "old");
1453 list_child(this, b, old, now);
1454 b->end_section(b);
1455 b->begin_section(b, "new");
1456 list_child(this, b, new, now);
1457 b->end_section(b);
1458
1459 b->end_section(b);
1460
1461 b->end_section(b);
1462 b->end_section(b);
1463
1464 this->dispatcher->raise_event(this->dispatcher,
1465 "child-rekey", 0, b->finalize(b));
1466
1467 return TRUE;
1468 }
1469
1470 METHOD(vici_query_t, destroy, void,
1471 private_vici_query_t *this)
1472 {
1473 manage_commands(this, FALSE);
1474 free(this);
1475 }
1476
1477 /**
1478 * See header
1479 */
1480 vici_query_t *vici_query_create(vici_dispatcher_t *dispatcher)
1481 {
1482 private_vici_query_t *this;
1483
1484 INIT(this,
1485 .public = {
1486 .listener = {
1487 .ike_updown = _ike_updown,
1488 .ike_rekey = _ike_rekey,
1489 .child_updown = _child_updown,
1490 .child_rekey = _child_rekey,
1491 },
1492 .destroy = _destroy,
1493 },
1494 .dispatcher = dispatcher,
1495 .uptime = time_monotonic(NULL),
1496 );
1497
1498 manage_commands(this, TRUE);
1499
1500 return &this->public;
1501 }