Include manual policy priorities and restriction to interfaces in vici list-conn...
[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, *interface;
686 uint32_t manual_prio;
687 linked_list_t *list;
688 traffic_selector_t *ts;
689 vici_builder_t *b;
690
691 ike = request->get_str(request, NULL, "ike");
692
693 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends,
694 NULL, NULL, NULL, NULL, IKE_ANY);
695 while (enumerator->enumerate(enumerator, &peer_cfg))
696 {
697 if (ike && !streq(ike, peer_cfg->get_name(peer_cfg)))
698 {
699 continue;
700 }
701
702 b = vici_builder_create();
703 b->begin_section(b, peer_cfg->get_name(peer_cfg));
704
705 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
706
707 b->begin_list(b, "local_addrs");
708 str = ike_cfg->get_my_addr(ike_cfg);
709 tokens = enumerator_create_token(str, ",", " ");
710 while (tokens->enumerate(tokens, &str))
711 {
712 b->add_li(b, "%s", str);
713 }
714 tokens->destroy(tokens);
715 b->end_list(b);
716
717 b->begin_list(b, "remote_addrs");
718 str = ike_cfg->get_other_addr(ike_cfg);
719 tokens = enumerator_create_token(str, ",", " ");
720 while (tokens->enumerate(tokens, &str))
721 {
722 b->add_li(b, "%s", str);
723 }
724 tokens->destroy(tokens);
725 b->end_list(b);
726
727 b->add_kv(b, "version", "%N", ike_version_names,
728 peer_cfg->get_ike_version(peer_cfg));
729
730 build_auth_cfgs(peer_cfg, TRUE, b);
731 build_auth_cfgs(peer_cfg, FALSE, b);
732
733 b->begin_section(b, "children");
734
735 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
736 while (children->enumerate(children, &child_cfg))
737 {
738 b->begin_section(b, child_cfg->get_name(child_cfg));
739
740 b->add_kv(b, "mode", "%N", ipsec_mode_names,
741 child_cfg->get_mode(child_cfg));
742
743 b->begin_list(b, "local-ts");
744 list = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
745 selectors = list->create_enumerator(list);
746 while (selectors->enumerate(selectors, &ts))
747 {
748 b->add_li(b, "%R", ts);
749 }
750 selectors->destroy(selectors);
751 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
752 b->end_list(b /* local-ts */);
753
754 b->begin_list(b, "remote-ts");
755 list = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
756 selectors = list->create_enumerator(list);
757 while (selectors->enumerate(selectors, &ts))
758 {
759 b->add_li(b, "%R", ts);
760 }
761 selectors->destroy(selectors);
762 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
763 b->end_list(b /* remote-ts */);
764
765 interface = child_cfg->get_interface(child_cfg);
766 if (interface)
767 {
768 b->add_kv(b, "interface", "%s", interface);
769 }
770
771 manual_prio = child_cfg->get_manual_prio(child_cfg);
772 if (manual_prio)
773 {
774 b->add_kv(b, "priority", "%u", manual_prio);
775 }
776
777 b->end_section(b);
778 }
779 children->destroy(children);
780
781 b->end_section(b); /* children */
782
783 b->end_section(b); /* name */
784
785 this->dispatcher->raise_event(this->dispatcher, "list-conn", id,
786 b->finalize(b));
787 }
788 enumerator->destroy(enumerator);
789
790 b = vici_builder_create();
791 return b->finalize(b);
792 }
793
794 /**
795 * Do we have a private key for given certificate
796 */
797 static bool has_privkey(certificate_t *cert)
798 {
799 private_key_t *private;
800 public_key_t *public;
801 identification_t *keyid;
802 chunk_t chunk;
803 bool found = FALSE;
804
805 public = cert->get_public_key(cert);
806 if (public)
807 {
808 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
809 {
810 keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
811 private = lib->credmgr->get_private(lib->credmgr,
812 public->get_type(public), keyid, NULL);
813 if (private)
814 {
815 found = TRUE;
816 private->destroy(private);
817 }
818 keyid->destroy(keyid);
819 }
820 public->destroy(public);
821 }
822 return found;
823 }
824
825 /**
826 * Store cert filter data
827 */
828 typedef struct {
829 certificate_type_t type;
830 x509_flag_t flag;
831 identification_t *subject;
832 } cert_filter_t;
833
834 /**
835 * Enumerate all X.509 certificates with a given flag
836 */
837 static void enum_x509(private_vici_query_t *this, u_int id,
838 linked_list_t *certs, cert_filter_t *filter,
839 x509_flag_t flag)
840 {
841 enumerator_t *enumerator;
842 certificate_t *cert;
843 vici_builder_t *b;
844 chunk_t encoding;
845 x509_t *x509;
846
847 if (filter->type != CERT_ANY && filter->flag != X509_ANY &&
848 filter->flag != flag)
849 {
850 return;
851 }
852
853 enumerator = certs->create_enumerator(certs);
854 while (enumerator->enumerate(enumerator, &cert))
855 {
856 x509 = (x509_t*)cert;
857 if ((x509->get_flags(x509) & X509_ANY) != flag)
858 {
859 continue;
860 }
861
862 if (cert->get_encoding(cert, CERT_ASN1_DER, &encoding))
863 {
864 b = vici_builder_create();
865 b->add_kv(b, "type", "%N", certificate_type_names, CERT_X509);
866 b->add_kv(b, "flag", "%N", x509_flag_names, flag);
867 if (has_privkey(cert))
868 {
869 b->add_kv(b, "has_privkey", "yes");
870 }
871 b->add(b, VICI_KEY_VALUE, "data", encoding);
872 free(encoding.ptr);
873
874 this->dispatcher->raise_event(this->dispatcher, "list-cert", id,
875 b->finalize(b));
876 }
877 }
878 enumerator->destroy(enumerator);
879 }
880
881 /**
882 * Enumerate all non-X.509 certificate types
883 */
884 static void enum_others(private_vici_query_t *this, u_int id,
885 linked_list_t *certs, certificate_type_t type)
886 {
887 enumerator_t *enumerator;
888 certificate_t *cert;
889 vici_builder_t *b;
890 chunk_t encoding, t_ch;
891 cred_encoding_type_t encoding_type;
892 identification_t *subject;
893 time_t not_before, not_after;
894
895 encoding_type = (type == CERT_TRUSTED_PUBKEY) ? PUBKEY_SPKI_ASN1_DER :
896 CERT_ASN1_DER;
897
898 enumerator = certs->create_enumerator(certs);
899 while (enumerator->enumerate(enumerator, &cert))
900 {
901 if (cert->get_encoding(cert, encoding_type, &encoding))
902 {
903 b = vici_builder_create();
904 b->add_kv(b, "type", "%N", certificate_type_names, type);
905 if (has_privkey(cert))
906 {
907 b->add_kv(b, "has_privkey", "yes");
908 }
909 b->add(b, VICI_KEY_VALUE, "data", encoding);
910 free(encoding.ptr);
911
912 if (type == CERT_TRUSTED_PUBKEY)
913 {
914 subject = cert->get_subject(cert);
915 if (subject->get_type(subject) != ID_KEY_ID)
916 {
917 b->add_kv(b, "subject", "%Y", cert->get_subject(cert));
918 }
919 cert->get_validity(cert, NULL, &not_before, &not_after);
920 if (not_before != UNDEFINED_TIME)
921 {
922 t_ch = asn1_from_time(&not_before, ASN1_GENERALIZEDTIME);
923 b->add(b, VICI_KEY_VALUE, "not-before", chunk_skip(t_ch, 2));
924 chunk_free(&t_ch);
925 }
926 if (not_after != UNDEFINED_TIME)
927 {
928 t_ch = asn1_from_time(&not_after, ASN1_GENERALIZEDTIME);
929 b->add(b, VICI_KEY_VALUE, "not-after", chunk_skip(t_ch, 2));
930 chunk_free(&t_ch);
931 }
932 }
933 this->dispatcher->raise_event(this->dispatcher, "list-cert", id,
934 b->finalize(b));
935 }
936 }
937 enumerator->destroy(enumerator);
938 }
939
940 /**
941 * Enumerate all certificates of a given type
942 */
943 static void enum_certs(private_vici_query_t *this, u_int id,
944 cert_filter_t *filter, certificate_type_t type)
945 {
946 enumerator_t *e1, *e2;
947 certificate_t *cert, *current;
948 linked_list_t *certs;
949 bool found;
950
951 if (filter->type != CERT_ANY && filter->type != type)
952 {
953 return;
954 }
955 certs = linked_list_create();
956
957 e1 = lib->credmgr->create_cert_enumerator(lib->credmgr, type, KEY_ANY,
958 filter->subject, FALSE);
959 while (e1->enumerate(e1, &cert))
960 {
961 found = FALSE;
962
963 e2 = certs->create_enumerator(certs);
964 while (e2->enumerate(e2, &current))
965 {
966 if (current->equals(current, cert))
967 {
968 found = TRUE;
969 break;
970 }
971 }
972 e2->destroy(e2);
973
974 if (!found)
975 {
976 certs->insert_last(certs, cert->get_ref(cert));
977 }
978 }
979 e1->destroy(e1);
980
981 if (type == CERT_X509)
982 {
983 enum_x509(this, id, certs, filter, X509_NONE);
984 enum_x509(this, id, certs, filter, X509_CA);
985 enum_x509(this, id, certs, filter, X509_AA);
986 enum_x509(this, id, certs, filter, X509_OCSP_SIGNER);
987 }
988 else
989 {
990 enum_others(this, id, certs, type);
991 }
992 certs->destroy_offset(certs, offsetof(certificate_t, destroy));
993 }
994
995 CALLBACK(list_certs, vici_message_t*,
996 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
997 {
998 cert_filter_t filter = {
999 .type = CERT_ANY,
1000 .flag = X509_ANY,
1001 .subject = NULL
1002 };
1003 vici_builder_t *b;
1004 char *str;
1005
1006 str = request->get_str(request, "ANY", "type");
1007 if (enum_from_name(certificate_type_names, str, &filter.type))
1008 {
1009 if (filter.type == CERT_X509)
1010 {
1011 str = request->get_str(request, "ANY", "flag");
1012 if (!enum_from_name(x509_flag_names, str, &filter.flag))
1013 {
1014 DBG1(DBG_CFG, "invalid certificate flag '%s'", str);
1015 goto finalize;
1016 }
1017 }
1018 }
1019 else if (!vici_cert_info_from_str(str, &filter.type, &filter.flag))
1020 {
1021 DBG1(DBG_CFG, "invalid certificate type '%s'", str);
1022 goto finalize;
1023 }
1024
1025 str = request->get_str(request, NULL, "subject");
1026 if (str)
1027 {
1028 filter.subject = identification_create_from_string(str);
1029 }
1030
1031 enum_certs(this, id, &filter, CERT_TRUSTED_PUBKEY);
1032 enum_certs(this, id, &filter, CERT_X509);
1033 enum_certs(this, id, &filter, CERT_X509_AC);
1034 enum_certs(this, id, &filter, CERT_X509_CRL);
1035 enum_certs(this, id, &filter, CERT_X509_OCSP_RESPONSE);
1036 DESTROY_IF(filter.subject);
1037
1038 finalize:
1039 b = vici_builder_create();
1040 return b->finalize(b);
1041 }
1042
1043 /**
1044 * Add a key/value pair of ALG => plugin
1045 */
1046 static void add_algorithm(vici_builder_t *b, enum_name_t *alg_names,
1047 int alg_type, const char *plugin_name)
1048 {
1049 char alg_name[BUF_LEN];
1050
1051 sprintf(alg_name, "%N", alg_names, alg_type);
1052 b->add_kv(b, alg_name, (char*)plugin_name);
1053 }
1054
1055 CALLBACK(get_algorithms, vici_message_t*,
1056 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
1057 {
1058 vici_builder_t *b;
1059 enumerator_t *enumerator;
1060 encryption_algorithm_t encryption;
1061 integrity_algorithm_t integrity;
1062 hash_algorithm_t hash;
1063 pseudo_random_function_t prf;
1064 diffie_hellman_group_t group;
1065 rng_quality_t quality;
1066 const char *plugin_name;
1067
1068 b = vici_builder_create();
1069
1070 b->begin_section(b, "encryption");
1071 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1072 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1073 {
1074 add_algorithm(b, encryption_algorithm_names, encryption, plugin_name);
1075 }
1076 enumerator->destroy(enumerator);
1077 b->end_section(b);
1078
1079 b->begin_section(b, "integrity");
1080 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1081 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
1082 {
1083 add_algorithm(b, integrity_algorithm_names, integrity, plugin_name);
1084 }
1085 enumerator->destroy(enumerator);
1086 b->end_section(b);
1087
1088 b->begin_section(b, "aead");
1089 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
1090 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1091 {
1092 add_algorithm(b, encryption_algorithm_names, encryption, plugin_name);
1093 }
1094 enumerator->destroy(enumerator);
1095 b->end_section(b);
1096
1097 b->begin_section(b, "hasher");
1098 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1099 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
1100 {
1101 add_algorithm(b, hash_algorithm_names, hash, plugin_name);
1102 }
1103 enumerator->destroy(enumerator);
1104 b->end_section(b);
1105
1106 b->begin_section(b, "prf");
1107 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1108 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
1109 {
1110 add_algorithm(b, pseudo_random_function_names, prf, plugin_name);
1111 }
1112 enumerator->destroy(enumerator);
1113 b->end_section(b);
1114
1115 b->begin_section(b, "dh");
1116 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1117 while (enumerator->enumerate(enumerator, &group, &plugin_name))
1118 {
1119 add_algorithm(b, diffie_hellman_group_names, group, plugin_name);
1120 }
1121 enumerator->destroy(enumerator);
1122 b->end_section(b);
1123
1124 b->begin_section(b, "rng");
1125 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
1126 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
1127 {
1128 add_algorithm(b, rng_quality_names, quality, plugin_name);
1129 }
1130 enumerator->destroy(enumerator);
1131 b->end_section(b);
1132
1133 b->begin_section(b, "nonce-gen");
1134 enumerator = lib->crypto->create_nonce_gen_enumerator(lib->crypto);
1135 while (enumerator->enumerate(enumerator, &plugin_name))
1136 {
1137 b->add_kv(b, "NONCE_GEN", (char*)plugin_name);
1138 }
1139 enumerator->destroy(enumerator);
1140 b->end_section(b);
1141
1142 return b->finalize(b);
1143 }
1144
1145 CALLBACK(version, vici_message_t*,
1146 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
1147 {
1148 vici_builder_t *b;
1149
1150 b = vici_builder_create();
1151 b->add_kv(b, "daemon", "%s", lib->ns);
1152 b->add_kv(b, "version", "%s", VERSION);
1153
1154 #ifdef WIN32
1155 {
1156 OSVERSIONINFOEX osvie;
1157
1158 memset(&osvie, 0, sizeof(osvie));
1159 osvie.dwOSVersionInfoSize = sizeof(osvie);
1160
1161 if (GetVersionEx((LPOSVERSIONINFO)&osvie))
1162 {
1163 b->add_kv(b, "sysname", "Windows %s",
1164 osvie.wProductType == VER_NT_WORKSTATION ? "Client" : "Server");
1165 b->add_kv(b, "release", "%d.%d.%d (SP %d.%d)",
1166 osvie.dwMajorVersion, osvie.dwMinorVersion, osvie.dwBuildNumber,
1167 osvie.wServicePackMajor, osvie.wServicePackMinor);
1168 b->add_kv(b, "machine", "%s",
1169 #ifdef WIN64
1170 "x86_64");
1171 #else
1172 "x86");
1173 #endif /* !WIN64 */
1174 }
1175 }
1176 #else /* !WIN32 */
1177 {
1178 struct utsname utsname;
1179
1180 if (uname(&utsname) == 0)
1181 {
1182 b->add_kv(b, "sysname", "%s", utsname.sysname);
1183 b->add_kv(b, "release", "%s", utsname.release);
1184 b->add_kv(b, "machine", "%s", utsname.machine);
1185 }
1186 }
1187 #endif /* !WIN32 */
1188 return b->finalize(b);
1189 }
1190
1191 CALLBACK(stats, vici_message_t*,
1192 private_vici_query_t *this, char *name, u_int id, vici_message_t *request)
1193 {
1194 vici_builder_t *b;
1195 enumerator_t *enumerator;
1196 plugin_t *plugin;
1197 time_t since, now;
1198 int i;
1199
1200 b = vici_builder_create();
1201
1202 now = time_monotonic(NULL);
1203 since = time(NULL) - (now - this->uptime);
1204
1205 b->begin_section(b, "uptime");
1206 b->add_kv(b, "running", "%V", &now, &this->uptime);
1207 b->add_kv(b, "since", "%T", &since, FALSE);
1208 b->end_section(b);
1209
1210 b->begin_section(b, "workers");
1211 b->add_kv(b, "total", "%d",
1212 lib->processor->get_total_threads(lib->processor));
1213 b->add_kv(b, "idle", "%d",
1214 lib->processor->get_idle_threads(lib->processor));
1215 b->begin_section(b, "active");
1216 for (i = 0; i < JOB_PRIO_MAX; i++)
1217 {
1218 b->add_kv(b, enum_to_name(job_priority_names, i), "%d",
1219 lib->processor->get_working_threads(lib->processor, i));
1220 }
1221 b->end_section(b);
1222 b->end_section(b);
1223
1224 b->begin_section(b, "queues");
1225 for (i = 0; i < JOB_PRIO_MAX; i++)
1226 {
1227 b->add_kv(b, enum_to_name(job_priority_names, i), "%d",
1228 lib->processor->get_job_load(lib->processor, i));
1229 }
1230 b->end_section(b);
1231
1232 b->add_kv(b, "scheduled", "%d",
1233 lib->scheduler->get_job_load(lib->scheduler));
1234
1235 b->begin_section(b, "ikesas");
1236 b->add_kv(b, "total", "%u",
1237 charon->ike_sa_manager->get_count(charon->ike_sa_manager));
1238 b->add_kv(b, "half-open", "%u",
1239 charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
1240 NULL, FALSE));
1241 b->end_section(b);
1242
1243 b->begin_list(b, "plugins");
1244 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
1245 while (enumerator->enumerate(enumerator, &plugin, NULL))
1246 {
1247 b->add_li(b, "%s", plugin->get_name(plugin));
1248 }
1249 enumerator->destroy(enumerator);
1250 b->end_list(b);
1251
1252 #ifdef WIN32
1253 {
1254 DWORD lasterr = ERROR_INVALID_HANDLE;
1255 HANDLE heaps[32];
1256 int i, count;
1257 char buf[16];
1258 size_t total = 0;
1259 int allocs = 0;
1260
1261 b->begin_section(b, "mem");
1262 count = GetProcessHeaps(countof(heaps), heaps);
1263 for (i = 0; i < count; i++)
1264 {
1265 PROCESS_HEAP_ENTRY entry = {};
1266 size_t heap_total = 0;
1267 int heap_allocs = 0;
1268
1269 if (HeapLock(heaps[i]))
1270 {
1271 while (HeapWalk(heaps[i], &entry))
1272 {
1273 if (entry.wFlags & PROCESS_HEAP_ENTRY_BUSY)
1274 {
1275 heap_total += entry.cbData;
1276 heap_allocs++;
1277 }
1278 }
1279 lasterr = GetLastError();
1280 HeapUnlock(heaps[i]);
1281 }
1282 if (lasterr != ERROR_NO_MORE_ITEMS)
1283 {
1284 break;
1285 }
1286 snprintf(buf, sizeof(buf), "heap-%d", i);
1287 b->begin_section(b, buf);
1288 b->add_kv(b, "total", "%zu", heap_total);
1289 b->add_kv(b, "allocs", "%d", heap_allocs);
1290 b->end_section(b);
1291
1292 total += heap_total;
1293 allocs += heap_allocs;
1294 }
1295 if (lasterr == ERROR_NO_MORE_ITEMS)
1296 {
1297 b->add_kv(b, "total", "%zu", total);
1298 b->add_kv(b, "allocs", "%d", allocs);
1299 }
1300 b->end_section(b);
1301 }
1302 #endif
1303
1304 #ifdef HAVE_MALLINFO
1305 {
1306 struct mallinfo mi = mallinfo();
1307
1308 b->begin_section(b, "mallinfo");
1309 b->add_kv(b, "sbrk", "%u", mi.arena);
1310 b->add_kv(b, "mmap", "%u", mi.hblkhd);
1311 b->add_kv(b, "used", "%u", mi.uordblks);
1312 b->add_kv(b, "free", "%u", mi.fordblks);
1313 b->end_section(b);
1314 }
1315 #endif /* HAVE_MALLINFO */
1316
1317 return b->finalize(b);
1318 }
1319
1320 static void manage_command(private_vici_query_t *this,
1321 char *name, vici_command_cb_t cb, bool reg)
1322 {
1323 this->dispatcher->manage_command(this->dispatcher, name,
1324 reg ? cb : NULL, this);
1325 }
1326
1327 /**
1328 * (Un-)register dispatcher functions
1329 */
1330 static void manage_commands(private_vici_query_t *this, bool reg)
1331 {
1332 this->dispatcher->manage_event(this->dispatcher, "list-sa", reg);
1333 this->dispatcher->manage_event(this->dispatcher, "list-policy", reg);
1334 this->dispatcher->manage_event(this->dispatcher, "list-conn", reg);
1335 this->dispatcher->manage_event(this->dispatcher, "list-cert", reg);
1336 this->dispatcher->manage_event(this->dispatcher, "ike-updown", reg);
1337 this->dispatcher->manage_event(this->dispatcher, "ike-rekey", reg);
1338 this->dispatcher->manage_event(this->dispatcher, "child-updown", reg);
1339 this->dispatcher->manage_event(this->dispatcher, "child-rekey", reg);
1340 manage_command(this, "list-sas", list_sas, reg);
1341 manage_command(this, "list-policies", list_policies, reg);
1342 manage_command(this, "list-conns", list_conns, reg);
1343 manage_command(this, "list-certs", list_certs, reg);
1344 manage_command(this, "get-algorithms", get_algorithms, reg);
1345 manage_command(this, "version", version, reg);
1346 manage_command(this, "stats", stats, reg);
1347 }
1348
1349 METHOD(listener_t, ike_updown, bool,
1350 private_vici_query_t *this, ike_sa_t *ike_sa, bool up)
1351 {
1352 vici_builder_t *b;
1353 time_t now;
1354
1355 if (!this->dispatcher->has_event_listeners(this->dispatcher, "ike-updown"))
1356 {
1357 return TRUE;
1358 }
1359
1360 now = time_monotonic(NULL);
1361
1362 b = vici_builder_create();
1363
1364 if (up)
1365 {
1366 b->add_kv(b, "up", "yes");
1367 }
1368
1369 b->begin_section(b, ike_sa->get_name(ike_sa));
1370 list_ike(this, b, ike_sa, now);
1371 b->end_section(b);
1372
1373 this->dispatcher->raise_event(this->dispatcher,
1374 "ike-updown", 0, b->finalize(b));
1375
1376 return TRUE;
1377 }
1378
1379 METHOD(listener_t, ike_rekey, bool,
1380 private_vici_query_t *this, ike_sa_t *old, ike_sa_t *new)
1381 {
1382 vici_builder_t *b;
1383 time_t now;
1384
1385 if (!this->dispatcher->has_event_listeners(this->dispatcher, "ike-rekey"))
1386 {
1387 return TRUE;
1388 }
1389
1390 now = time_monotonic(NULL);
1391
1392 b = vici_builder_create();
1393 b->begin_section(b, old->get_name(old));
1394 b->begin_section(b, "old");
1395 list_ike(this, b, old, now);
1396 b->end_section(b);
1397 b->begin_section(b, "new");
1398 list_ike(this, b, new, now);
1399 b->end_section(b);
1400 b->end_section(b);
1401
1402 this->dispatcher->raise_event(this->dispatcher,
1403 "ike-rekey", 0, b->finalize(b));
1404
1405 return TRUE;
1406 }
1407
1408 METHOD(listener_t, child_updown, bool,
1409 private_vici_query_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa, bool up)
1410 {
1411 vici_builder_t *b;
1412 time_t now;
1413
1414 if (!this->dispatcher->has_event_listeners(this->dispatcher, "child-updown"))
1415 {
1416 return TRUE;
1417 }
1418
1419 now = time_monotonic(NULL);
1420 b = vici_builder_create();
1421
1422 if (up)
1423 {
1424 b->add_kv(b, "up", "yes");
1425 }
1426
1427 b->begin_section(b, ike_sa->get_name(ike_sa));
1428 list_ike(this, b, ike_sa, now);
1429 b->begin_section(b, "child-sas");
1430
1431 b->begin_section(b, child_sa->get_name(child_sa));
1432 list_child(this, b, child_sa, now);
1433 b->end_section(b);
1434
1435 b->end_section(b);
1436 b->end_section(b);
1437
1438 this->dispatcher->raise_event(this->dispatcher,
1439 "child-updown", 0, b->finalize(b));
1440
1441 return TRUE;
1442 }
1443
1444 METHOD(listener_t, child_rekey, bool,
1445 private_vici_query_t *this, ike_sa_t *ike_sa, child_sa_t *old,
1446 child_sa_t *new)
1447 {
1448 vici_builder_t *b;
1449 time_t now;
1450
1451 if (!this->dispatcher->has_event_listeners(this->dispatcher, "child-rekey"))
1452 {
1453 return TRUE;
1454 }
1455
1456 now = time_monotonic(NULL);
1457 b = vici_builder_create();
1458
1459 b->begin_section(b, ike_sa->get_name(ike_sa));
1460 list_ike(this, b, ike_sa, now);
1461 b->begin_section(b, "child-sas");
1462
1463 b->begin_section(b, old->get_name(old));
1464
1465 b->begin_section(b, "old");
1466 list_child(this, b, old, now);
1467 b->end_section(b);
1468 b->begin_section(b, "new");
1469 list_child(this, b, new, now);
1470 b->end_section(b);
1471
1472 b->end_section(b);
1473
1474 b->end_section(b);
1475 b->end_section(b);
1476
1477 this->dispatcher->raise_event(this->dispatcher,
1478 "child-rekey", 0, b->finalize(b));
1479
1480 return TRUE;
1481 }
1482
1483 METHOD(vici_query_t, destroy, void,
1484 private_vici_query_t *this)
1485 {
1486 manage_commands(this, FALSE);
1487 free(this);
1488 }
1489
1490 /**
1491 * See header
1492 */
1493 vici_query_t *vici_query_create(vici_dispatcher_t *dispatcher)
1494 {
1495 private_vici_query_t *this;
1496
1497 INIT(this,
1498 .public = {
1499 .listener = {
1500 .ike_updown = _ike_updown,
1501 .ike_rekey = _ike_rekey,
1502 .child_updown = _child_updown,
1503 .child_rekey = _child_rekey,
1504 },
1505 .destroy = _destroy,
1506 },
1507 .dispatcher = dispatcher,
1508 .uptime = time_monotonic(NULL),
1509 );
1510
1511 manage_commands(this, TRUE);
1512
1513 return &this->public;
1514 }