2 * Copyright (C) 2015 Tobias Brunner, Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2014 Martin Willi
6 * Copyright (C) 2014 revosec AG
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>.
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
20 * Copyright (C) 2014 Timo Teräs <timo.teras@iki.fi>
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:
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
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
41 #include "vici_query.h"
42 #include "vici_builder.h"
43 #include "vici_cert_info.h"
48 #include <sys/utsname.h>
55 #include <credentials/certificates/certificate.h>
56 #include <credentials/certificates/x509.h>
58 typedef struct private_vici_query_t private_vici_query_t
;
61 * Private data of an vici_query_t object.
63 struct private_vici_query_t
{
66 * Public vici_query_t interface.
73 vici_dispatcher_t
*dispatcher
;
76 * Daemon startup timestamp
82 * List details of a CHILD_SA
84 static void list_child(private_vici_query_t
*this, vici_builder_t
*b
,
85 child_sa_t
*child
, time_t now
)
88 u_int64_t bytes
, packets
;
91 enumerator_t
*enumerator
;
92 traffic_selector_t
*ts
;
94 b
->add_kv(b
, "uniqueid", "%u", child
->get_unique_id(child
));
95 b
->add_kv(b
, "reqid", "%u", child
->get_reqid(child
));
96 b
->add_kv(b
, "state", "%N", child_sa_state_names
, child
->get_state(child
));
97 b
->add_kv(b
, "mode", "%N", ipsec_mode_names
, child
->get_mode(child
));
98 if (child
->get_state(child
) == CHILD_INSTALLED
||
99 child
->get_state(child
) == CHILD_REKEYING
||
100 child
->get_state(child
) == CHILD_REKEYED
)
102 b
->add_kv(b
, "protocol", "%N", protocol_id_names
,
103 child
->get_protocol(child
));
104 if (child
->has_encap(child
))
106 b
->add_kv(b
, "encap", "yes");
108 b
->add_kv(b
, "spi-in", "%.8x", ntohl(child
->get_spi(child
, TRUE
)));
109 b
->add_kv(b
, "spi-out", "%.8x", ntohl(child
->get_spi(child
, FALSE
)));
111 if (child
->get_ipcomp(child
) != IPCOMP_NONE
)
113 b
->add_kv(b
, "cpi-in", "%.4x", ntohs(child
->get_cpi(child
, TRUE
)));
114 b
->add_kv(b
, "cpi-out", "%.4x", ntohs(child
->get_cpi(child
, FALSE
)));
116 proposal
= child
->get_proposal(child
);
119 if (proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
,
120 &alg
, &ks
) && alg
!= ENCR_UNDEFINED
)
122 b
->add_kv(b
, "encr-alg", "%N", encryption_algorithm_names
, alg
);
125 b
->add_kv(b
, "encr-keysize", "%u", ks
);
128 if (proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
,
129 &alg
, &ks
) && alg
!= ENCR_UNDEFINED
)
131 b
->add_kv(b
, "integ-alg", "%N", integrity_algorithm_names
, alg
);
134 b
->add_kv(b
, "integ-keysize", "%u", ks
);
137 if (proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
,
140 b
->add_kv(b
, "prf-alg", "%N", pseudo_random_function_names
, alg
);
142 if (proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
,
145 b
->add_kv(b
, "dh-group", "%N", diffie_hellman_group_names
, alg
);
147 if (proposal
->get_algorithm(proposal
, EXTENDED_SEQUENCE_NUMBERS
,
148 &alg
, NULL
) && alg
== EXT_SEQ_NUMBERS
)
150 b
->add_kv(b
, "esn", "1");
154 child
->get_usestats(child
, TRUE
, &t
, &bytes
, &packets
);
155 b
->add_kv(b
, "bytes-in", "%" PRIu64
, bytes
);
156 b
->add_kv(b
, "packets-in", "%" PRIu64
, packets
);
159 b
->add_kv(b
, "use-in", "%"PRIu64
, (u_int64_t
)(now
- t
));
162 child
->get_usestats(child
, FALSE
, &t
, &bytes
, &packets
);
163 b
->add_kv(b
, "bytes-out", "%"PRIu64
, bytes
);
164 b
->add_kv(b
, "packets-out", "%"PRIu64
, packets
);
167 b
->add_kv(b
, "use-out", "%"PRIu64
, (u_int64_t
)(now
- t
));
170 t
= child
->get_lifetime(child
, FALSE
);
173 b
->add_kv(b
, "rekey-time", "%"PRId64
, (int64_t)(t
- now
));
175 t
= child
->get_lifetime(child
, TRUE
);
178 b
->add_kv(b
, "life-time", "%"PRId64
, (int64_t)(t
- now
));
180 t
= child
->get_installtime(child
);
181 b
->add_kv(b
, "install-time", "%"PRId64
, (int64_t)(now
- t
));
184 b
->begin_list(b
, "local-ts");
185 enumerator
= child
->create_ts_enumerator(child
, TRUE
);
186 while (enumerator
->enumerate(enumerator
, &ts
))
188 b
->add_li(b
, "%R", ts
);
190 enumerator
->destroy(enumerator
);
191 b
->end_list(b
/* local-ts */);
193 b
->begin_list(b
, "remote-ts");
194 enumerator
= child
->create_ts_enumerator(child
, FALSE
);
195 while (enumerator
->enumerate(enumerator
, &ts
))
197 b
->add_li(b
, "%R", ts
);
199 enumerator
->destroy(enumerator
);
200 b
->end_list(b
/* remote-ts */);
204 * List tasks in a specific queue
206 static void list_task_queue(private_vici_query_t
*this, vici_builder_t
*b
,
207 ike_sa_t
*ike_sa
, task_queue_t q
, char *name
)
209 enumerator_t
*enumerator
;
213 enumerator
= ike_sa
->create_task_enumerator(ike_sa
, q
);
214 while (enumerator
->enumerate(enumerator
, &task
))
218 b
->begin_list(b
, name
);
221 b
->add_li(b
, "%N", task_type_names
, task
->get_type(task
));
223 enumerator
->destroy(enumerator
);
231 * Add an IKE_SA condition to the given builder
233 static void add_condition(vici_builder_t
*b
, ike_sa_t
*ike_sa
,
234 char *key
, ike_condition_t cond
)
236 if (ike_sa
->has_condition(ike_sa
, cond
))
238 b
->add_kv(b
, key
, "yes");
245 static void list_vips(private_vici_query_t
*this, vici_builder_t
*b
,
246 ike_sa_t
*ike_sa
, bool local
, char *name
)
248 enumerator_t
*enumerator
;
252 enumerator
= ike_sa
->create_virtual_ip_enumerator(ike_sa
, local
);
253 while (enumerator
->enumerate(enumerator
, &vip
))
257 b
->begin_list(b
, name
);
260 b
->add_li(b
, "%H", vip
);
262 enumerator
->destroy(enumerator
);
270 * List details of an IKE_SA
272 static void list_ike(private_vici_query_t
*this, vici_builder_t
*b
,
273 ike_sa_t
*ike_sa
, time_t now
)
277 identification_t
*eap
;
278 proposal_t
*proposal
;
281 b
->add_kv(b
, "uniqueid", "%u", ike_sa
->get_unique_id(ike_sa
));
282 b
->add_kv(b
, "version", "%u", ike_sa
->get_version(ike_sa
));
283 b
->add_kv(b
, "state", "%N", ike_sa_state_names
, ike_sa
->get_state(ike_sa
));
285 b
->add_kv(b
, "local-host", "%H", ike_sa
->get_my_host(ike_sa
));
286 b
->add_kv(b
, "local-id", "%Y", ike_sa
->get_my_id(ike_sa
));
288 b
->add_kv(b
, "remote-host", "%H", ike_sa
->get_other_host(ike_sa
));
289 b
->add_kv(b
, "remote-id", "%Y", ike_sa
->get_other_id(ike_sa
));
291 eap
= ike_sa
->get_other_eap_id(ike_sa
);
293 if (!eap
->equals(eap
, ike_sa
->get_other_id(ike_sa
)))
295 if (ike_sa
->get_version(ike_sa
) == IKEV1
)
297 b
->add_kv(b
, "remote-xauth-id", "%Y", eap
);
301 b
->add_kv(b
, "remote-eap-id", "%Y", eap
);
305 id
= ike_sa
->get_id(ike_sa
);
306 if (id
->is_initiator(id
))
308 b
->add_kv(b
, "initiator", "yes");
310 b
->add_kv(b
, "initiator-spi", "%.16"PRIx64
, id
->get_initiator_spi(id
));
311 b
->add_kv(b
, "responder-spi", "%.16"PRIx64
, id
->get_responder_spi(id
));
313 add_condition(b
, ike_sa
, "nat-local", COND_NAT_HERE
);
314 add_condition(b
, ike_sa
, "nat-remote", COND_NAT_THERE
);
315 add_condition(b
, ike_sa
, "nat-fake", COND_NAT_FAKE
);
316 add_condition(b
, ike_sa
, "nat-any", COND_NAT_ANY
);
318 proposal
= ike_sa
->get_proposal(ike_sa
);
321 if (proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &alg
, &ks
))
323 b
->add_kv(b
, "encr-alg", "%N", encryption_algorithm_names
, alg
);
326 b
->add_kv(b
, "encr-keysize", "%u", ks
);
329 if (proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &alg
, &ks
))
331 b
->add_kv(b
, "integ-alg", "%N", integrity_algorithm_names
, alg
);
334 b
->add_kv(b
, "integ-keysize", "%u", ks
);
337 if (proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &alg
, NULL
))
339 b
->add_kv(b
, "prf-alg", "%N", pseudo_random_function_names
, alg
);
341 if (proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
, &alg
, NULL
))
343 b
->add_kv(b
, "dh-group", "%N", diffie_hellman_group_names
, alg
);
347 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
)
349 t
= ike_sa
->get_statistic(ike_sa
, STAT_ESTABLISHED
);
350 b
->add_kv(b
, "established", "%"PRId64
, (int64_t)(now
- t
));
351 t
= ike_sa
->get_statistic(ike_sa
, STAT_REKEY
);
354 b
->add_kv(b
, "rekey-time", "%"PRId64
, (int64_t)(t
- now
));
356 t
= ike_sa
->get_statistic(ike_sa
, STAT_REAUTH
);
359 b
->add_kv(b
, "reauth-time", "%"PRId64
, (int64_t)(t
- now
));
363 list_vips(this, b
, ike_sa
, TRUE
, "local-vips");
364 list_vips(this, b
, ike_sa
, FALSE
, "remote-vips");
366 list_task_queue(this, b
, ike_sa
, TASK_QUEUE_QUEUED
, "tasks-queued");
367 list_task_queue(this, b
, ike_sa
, TASK_QUEUE_ACTIVE
, "tasks-active");
368 list_task_queue(this, b
, ike_sa
, TASK_QUEUE_PASSIVE
, "tasks-passive");
371 CALLBACK(list_sas
, vici_message_t
*,
372 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
375 enumerator_t
*isas
, *csas
;
377 child_sa_t
*child_sa
;
383 bl
= request
->get_str(request
, NULL
, "noblock") == NULL
;
384 ike
= request
->get_str(request
, NULL
, "ike");
385 ike_id
= request
->get_int(request
, 0, "ike-id");
387 isas
= charon
->controller
->create_ike_sa_enumerator(charon
->controller
, bl
);
388 while (isas
->enumerate(isas
, &ike_sa
))
390 if (ike
&& !streq(ike
, ike_sa
->get_name(ike_sa
)))
394 if (ike_id
&& ike_id
!= ike_sa
->get_unique_id(ike_sa
))
399 now
= time_monotonic(NULL
);
401 b
= vici_builder_create();
402 b
->begin_section(b
, ike_sa
->get_name(ike_sa
));
404 list_ike(this, b
, ike_sa
, now
);
406 b
->begin_section(b
, "child-sas");
407 csas
= ike_sa
->create_child_sa_enumerator(ike_sa
);
408 while (csas
->enumerate(csas
, &child_sa
))
410 b
->begin_section(b
, child_sa
->get_name(child_sa
));
411 list_child(this, b
, child_sa
, now
);
415 b
->end_section(b
/* child-sas */ );
419 this->dispatcher
->raise_event(this->dispatcher
, "list-sa", id
,
424 b
= vici_builder_create();
425 return b
->finalize(b
);
429 * Raise a list-policy event for given CHILD_SA
431 static void raise_policy(private_vici_query_t
*this, u_int id
, child_sa_t
*child
)
433 enumerator_t
*enumerator
;
434 traffic_selector_t
*ts
;
437 b
= vici_builder_create();
438 b
->begin_section(b
, child
->get_name(child
));
440 b
->add_kv(b
, "mode", "%N", ipsec_mode_names
, child
->get_mode(child
));
442 b
->begin_list(b
, "local-ts");
443 enumerator
= child
->create_ts_enumerator(child
, TRUE
);
444 while (enumerator
->enumerate(enumerator
, &ts
))
446 b
->add_li(b
, "%R", ts
);
448 enumerator
->destroy(enumerator
);
449 b
->end_list(b
/* local-ts */);
451 b
->begin_list(b
, "remote-ts");
452 enumerator
= child
->create_ts_enumerator(child
, FALSE
);
453 while (enumerator
->enumerate(enumerator
, &ts
))
455 b
->add_li(b
, "%R", ts
);
457 enumerator
->destroy(enumerator
);
458 b
->end_list(b
/* remote-ts */);
462 this->dispatcher
->raise_event(this->dispatcher
, "list-policy", id
,
467 * Raise a list-policy event for given CHILD_SA config
469 static void raise_policy_cfg(private_vici_query_t
*this, u_int id
,
472 enumerator_t
*enumerator
;
474 traffic_selector_t
*ts
;
477 b
= vici_builder_create();
478 b
->begin_section(b
, cfg
->get_name(cfg
));
480 b
->add_kv(b
, "mode", "%N", ipsec_mode_names
, cfg
->get_mode(cfg
));
482 b
->begin_list(b
, "local-ts");
483 list
= cfg
->get_traffic_selectors(cfg
, TRUE
, NULL
, NULL
);
484 enumerator
= list
->create_enumerator(list
);
485 while (enumerator
->enumerate(enumerator
, &ts
))
487 b
->add_li(b
, "%R", ts
);
489 enumerator
->destroy(enumerator
);
490 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
491 b
->end_list(b
/* local-ts */);
493 b
->begin_list(b
, "remote-ts");
494 list
= cfg
->get_traffic_selectors(cfg
, FALSE
, NULL
, NULL
);
495 enumerator
= list
->create_enumerator(list
);
496 while (enumerator
->enumerate(enumerator
, &ts
))
498 b
->add_li(b
, "%R", ts
);
500 enumerator
->destroy(enumerator
);
501 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
502 b
->end_list(b
/* remote-ts */);
506 this->dispatcher
->raise_event(this->dispatcher
, "list-policy", id
,
510 CALLBACK(list_policies
, vici_message_t
*,
511 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
513 enumerator_t
*enumerator
;
515 child_sa_t
*child_sa
;
516 child_cfg_t
*child_cfg
;
517 bool drop
, pass
, trap
;
520 drop
= request
->get_str(request
, NULL
, "drop") != NULL
;
521 pass
= request
->get_str(request
, NULL
, "pass") != NULL
;
522 trap
= request
->get_str(request
, NULL
, "trap") != NULL
;
523 child
= request
->get_str(request
, NULL
, "child");
527 enumerator
= charon
->traps
->create_enumerator(charon
->traps
);
528 while (enumerator
->enumerate(enumerator
, NULL
, &child_sa
))
530 if (child
&& !streq(child
, child_sa
->get_name(child_sa
)))
534 raise_policy(this, id
, child_sa
);
536 enumerator
->destroy(enumerator
);
541 enumerator
= charon
->shunts
->create_enumerator(charon
->shunts
);
542 while (enumerator
->enumerate(enumerator
, &child_cfg
))
544 if (child
&& !streq(child
, child_cfg
->get_name(child_cfg
)))
548 switch (child_cfg
->get_mode(child_cfg
))
553 raise_policy_cfg(this, id
, child_cfg
);
559 raise_policy_cfg(this, id
, child_cfg
);
566 enumerator
->destroy(enumerator
);
569 b
= vici_builder_create();
570 return b
->finalize(b
);
574 * Build sections for auth configs, local or remote
576 static void build_auth_cfgs(peer_cfg_t
*peer_cfg
, bool local
, vici_builder_t
*b
)
578 enumerator_t
*enumerator
, *rules
;
583 identification_t
*id
;
590 enumerator
= peer_cfg
->create_auth_cfg_enumerator(peer_cfg
, local
);
591 while (enumerator
->enumerate(enumerator
, &auth
))
593 snprintf(buf
, sizeof(buf
), "%s-%d", local ?
"local" : "remote", ++i
);
594 b
->begin_section(b
, buf
);
596 rules
= auth
->create_enumerator(auth
);
597 while (rules
->enumerate(rules
, &rule
, &v
))
601 case AUTH_RULE_AUTH_CLASS
:
602 b
->add_kv(b
, "class", "%N", auth_class_names
, v
.u
);
604 case AUTH_RULE_EAP_TYPE
:
605 b
->add_kv(b
, "eap-type", "%N", eap_type_names
, v
.u
);
607 case AUTH_RULE_EAP_VENDOR
:
608 b
->add_kv(b
, "eap-vendor", "%u", v
.u
);
610 case AUTH_RULE_XAUTH_BACKEND
:
611 b
->add_kv(b
, "xauth", "%s", v
.str
);
613 case AUTH_RULE_CRL_VALIDATION
:
614 b
->add_kv(b
, "revocation", "%N", cert_validation_names
, v
.u
);
616 case AUTH_RULE_IDENTITY
:
617 b
->add_kv(b
, "id", "%Y", v
.id
);
619 case AUTH_RULE_AAA_IDENTITY
:
620 b
->add_kv(b
, "aaa_id", "%Y", v
.id
);
622 case AUTH_RULE_EAP_IDENTITY
:
623 b
->add_kv(b
, "eap_id", "%Y", v
.id
);
625 case AUTH_RULE_XAUTH_IDENTITY
:
626 b
->add_kv(b
, "xauth_id", "%Y", v
.id
);
632 rules
->destroy(rules
);
634 b
->begin_list(b
, "groups");
635 rules
= auth
->create_enumerator(auth
);
636 while (rules
->enumerate(rules
, &rule
, &v
))
638 if (rule
== AUTH_RULE_GROUP
)
640 b
->add_li(b
, "%Y", v
.id
);
643 rules
->destroy(rules
);
646 b
->begin_list(b
, "certs");
647 rules
= auth
->create_enumerator(auth
);
648 while (rules
->enumerate(rules
, &rule
, &v
))
650 if (rule
== AUTH_RULE_SUBJECT_CERT
)
652 b
->add_li(b
, "%Y", v
.cert
->get_subject(v
.cert
));
655 rules
->destroy(rules
);
658 b
->begin_list(b
, "cacerts");
659 rules
= auth
->create_enumerator(auth
);
660 while (rules
->enumerate(rules
, &rule
, &v
))
662 if (rule
== AUTH_RULE_CA_CERT
)
664 b
->add_li(b
, "%Y", v
.cert
->get_subject(v
.cert
));
667 rules
->destroy(rules
);
672 enumerator
->destroy(enumerator
);
675 CALLBACK(list_conns
, vici_message_t
*,
676 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
678 enumerator_t
*enumerator
, *tokens
, *selectors
, *children
;
679 peer_cfg_t
*peer_cfg
;
681 child_cfg_t
*child_cfg
;
684 traffic_selector_t
*ts
;
687 ike
= request
->get_str(request
, NULL
, "ike");
689 enumerator
= charon
->backends
->create_peer_cfg_enumerator(charon
->backends
,
690 NULL
, NULL
, NULL
, NULL
, IKE_ANY
);
691 while (enumerator
->enumerate(enumerator
, &peer_cfg
))
693 if (ike
&& !streq(ike
, peer_cfg
->get_name(peer_cfg
)))
698 b
= vici_builder_create();
699 b
->begin_section(b
, peer_cfg
->get_name(peer_cfg
));
701 ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
703 b
->begin_list(b
, "local_addrs");
704 str
= ike_cfg
->get_my_addr(ike_cfg
);
705 tokens
= enumerator_create_token(str
, ",", " ");
706 while (tokens
->enumerate(tokens
, &str
))
708 b
->add_li(b
, "%s", str
);
710 tokens
->destroy(tokens
);
713 b
->begin_list(b
, "remote_addrs");
714 str
= ike_cfg
->get_other_addr(ike_cfg
);
715 tokens
= enumerator_create_token(str
, ",", " ");
716 while (tokens
->enumerate(tokens
, &str
))
718 b
->add_li(b
, "%s", str
);
720 tokens
->destroy(tokens
);
723 b
->add_kv(b
, "version", "%N", ike_version_names
,
724 peer_cfg
->get_ike_version(peer_cfg
));
726 build_auth_cfgs(peer_cfg
, TRUE
, b
);
727 build_auth_cfgs(peer_cfg
, FALSE
, b
);
729 b
->begin_section(b
, "children");
731 children
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
732 while (children
->enumerate(children
, &child_cfg
))
734 b
->begin_section(b
, child_cfg
->get_name(child_cfg
));
736 b
->add_kv(b
, "mode", "%N", ipsec_mode_names
,
737 child_cfg
->get_mode(child_cfg
));
739 b
->begin_list(b
, "local-ts");
740 list
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
741 selectors
= list
->create_enumerator(list
);
742 while (selectors
->enumerate(selectors
, &ts
))
744 b
->add_li(b
, "%R", ts
);
746 selectors
->destroy(selectors
);
747 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
748 b
->end_list(b
/* local-ts */);
750 b
->begin_list(b
, "remote-ts");
751 list
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
752 selectors
= list
->create_enumerator(list
);
753 while (selectors
->enumerate(selectors
, &ts
))
755 b
->add_li(b
, "%R", ts
);
757 selectors
->destroy(selectors
);
758 list
->destroy_offset(list
, offsetof(traffic_selector_t
, destroy
));
759 b
->end_list(b
/* remote-ts */);
763 children
->destroy(children
);
765 b
->end_section(b
); /* children */
767 b
->end_section(b
); /* name */
769 this->dispatcher
->raise_event(this->dispatcher
, "list-conn", id
,
772 enumerator
->destroy(enumerator
);
774 b
= vici_builder_create();
775 return b
->finalize(b
);
779 * Do we have a private key for given certificate
781 static bool has_privkey(certificate_t
*cert
)
783 private_key_t
*private;
784 public_key_t
*public;
785 identification_t
*keyid
;
789 public = cert
->get_public_key(cert
);
792 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &chunk
))
794 keyid
= identification_create_from_encoding(ID_KEY_ID
, chunk
);
795 private = lib
->credmgr
->get_private(lib
->credmgr
,
796 public->get_type(public), keyid
, NULL
);
800 private->destroy(private);
802 keyid
->destroy(keyid
);
804 public->destroy(public);
810 * Store cert filter data
813 certificate_type_t type
;
815 identification_t
*subject
;
819 * Enumerate all X.509 certificates with a given flag
821 static void enum_x509(private_vici_query_t
*this, u_int id
,
822 linked_list_t
*certs
, cert_filter_t
*filter
,
825 enumerator_t
*enumerator
;
831 if (filter
->type
!= CERT_ANY
&& filter
->flag
!= X509_ANY
&&
832 filter
->flag
!= flag
)
837 enumerator
= certs
->create_enumerator(certs
);
838 while (enumerator
->enumerate(enumerator
, &cert
))
840 x509
= (x509_t
*)cert
;
841 if ((x509
->get_flags(x509
) & X509_ANY
) != flag
)
846 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &encoding
))
848 b
= vici_builder_create();
849 b
->add_kv(b
, "type", "%N", certificate_type_names
, CERT_X509
);
850 b
->add_kv(b
, "flag", "%N", x509_flag_names
, flag
);
851 if (has_privkey(cert
))
853 b
->add_kv(b
, "has_privkey", "yes");
855 b
->add(b
, VICI_KEY_VALUE
, "data", encoding
);
858 this->dispatcher
->raise_event(this->dispatcher
, "list-cert", id
,
862 enumerator
->destroy(enumerator
);
866 * Enumerate all non-X.509 certificate types
868 static void enum_others(private_vici_query_t
*this, u_int id
,
869 linked_list_t
*certs
, cert_filter_t
*filter
)
871 enumerator_t
*enumerator
;
876 enumerator
= certs
->create_enumerator(certs
);
877 while (enumerator
->enumerate(enumerator
, &cert
))
879 if (cert
->get_encoding(cert
, CERT_ASN1_DER
, &encoding
))
881 b
= vici_builder_create();
882 b
->add_kv(b
, "type", "%N", certificate_type_names
,
883 cert
->get_type(cert
));
884 if (has_privkey(cert
))
886 b
->add_kv(b
, "has_privkey", "yes");
888 b
->add(b
, VICI_KEY_VALUE
, "data", encoding
);
891 this->dispatcher
->raise_event(this->dispatcher
, "list-cert", id
,
895 enumerator
->destroy(enumerator
);
899 * Enumerate all certificates of a given type
901 static void enum_certs(private_vici_query_t
*this, u_int id
,
902 cert_filter_t
*filter
, certificate_type_t type
)
904 enumerator_t
*e1
, *e2
;
905 certificate_t
*cert
, *current
;
906 linked_list_t
*certs
;
909 if (filter
->type
!= CERT_ANY
&& filter
->type
!= type
)
913 certs
= linked_list_create();
915 e1
= lib
->credmgr
->create_cert_enumerator(lib
->credmgr
, type
, KEY_ANY
,
916 filter
->subject
, FALSE
);
917 while (e1
->enumerate(e1
, &cert
))
921 e2
= certs
->create_enumerator(certs
);
922 while (e2
->enumerate(e2
, ¤t
))
924 if (current
->equals(current
, cert
))
934 certs
->insert_last(certs
, cert
->get_ref(cert
));
939 if (type
== CERT_X509
)
941 enum_x509(this, id
, certs
, filter
, X509_NONE
);
942 enum_x509(this, id
, certs
, filter
, X509_CA
);
943 enum_x509(this, id
, certs
, filter
, X509_AA
);
944 enum_x509(this, id
, certs
, filter
, X509_OCSP_SIGNER
);
948 enum_others(this, id
, certs
, filter
);
950 certs
->destroy_offset(certs
, offsetof(certificate_t
, destroy
));
953 CALLBACK(list_certs
, vici_message_t
*,
954 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
956 cert_filter_t filter
= {
964 str
= request
->get_str(request
, "ANY", "type");
965 if (enum_from_name(certificate_type_names
, str
, &filter
.type
))
967 if (filter
.type
== CERT_X509
)
969 str
= request
->get_str(request
, "ANY", "flag");
970 if (!enum_from_name(x509_flag_names
, str
, &filter
.flag
))
972 DBG1(DBG_CFG
, "invalid certificate flag '%s'", str
);
977 else if (!vici_cert_info_from_str(str
, &filter
.type
, &filter
.flag
))
979 DBG1(DBG_CFG
, "invalid certificate type '%s'", str
);
983 str
= request
->get_str(request
, NULL
, "subject");
986 filter
.subject
= identification_create_from_string(str
);
989 enum_certs(this, id
, &filter
, CERT_TRUSTED_PUBKEY
);
990 enum_certs(this, id
, &filter
, CERT_X509
);
991 enum_certs(this, id
, &filter
, CERT_X509_AC
);
992 enum_certs(this, id
, &filter
, CERT_X509_CRL
);
993 enum_certs(this, id
, &filter
, CERT_X509_OCSP_RESPONSE
);
994 DESTROY_IF(filter
.subject
);
997 b
= vici_builder_create();
998 return b
->finalize(b
);
1002 * Add a key/value pair of ALG => plugin
1004 static void add_algorithm(vici_builder_t
*b
, enum_name_t
*alg_names
,
1005 int alg_type
, const char *plugin_name
)
1007 char alg_name
[BUF_LEN
];
1009 sprintf(alg_name
, "%N", alg_names
, alg_type
);
1010 b
->add_kv(b
, alg_name
, (char*)plugin_name
);
1013 CALLBACK(get_algorithms
, vici_message_t
*,
1014 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
1017 enumerator_t
*enumerator
;
1018 encryption_algorithm_t encryption
;
1019 integrity_algorithm_t integrity
;
1020 hash_algorithm_t hash
;
1021 pseudo_random_function_t prf
;
1022 diffie_hellman_group_t group
;
1023 rng_quality_t quality
;
1024 const char *plugin_name
;
1026 b
= vici_builder_create();
1028 b
->begin_section(b
, "encryption");
1029 enumerator
= lib
->crypto
->create_crypter_enumerator(lib
->crypto
);
1030 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1032 add_algorithm(b
, encryption_algorithm_names
, encryption
, plugin_name
);
1034 enumerator
->destroy(enumerator
);
1037 b
->begin_section(b
, "integrity");
1038 enumerator
= lib
->crypto
->create_signer_enumerator(lib
->crypto
);
1039 while (enumerator
->enumerate(enumerator
, &integrity
, &plugin_name
))
1041 add_algorithm(b
, integrity_algorithm_names
, integrity
, plugin_name
);
1043 enumerator
->destroy(enumerator
);
1046 b
->begin_section(b
, "aead");
1047 enumerator
= lib
->crypto
->create_aead_enumerator(lib
->crypto
);
1048 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1050 add_algorithm(b
, encryption_algorithm_names
, encryption
, plugin_name
);
1052 enumerator
->destroy(enumerator
);
1055 b
->begin_section(b
, "hasher");
1056 enumerator
= lib
->crypto
->create_hasher_enumerator(lib
->crypto
);
1057 while (enumerator
->enumerate(enumerator
, &hash
, &plugin_name
))
1059 add_algorithm(b
, hash_algorithm_names
, hash
, plugin_name
);
1061 enumerator
->destroy(enumerator
);
1064 b
->begin_section(b
, "prf");
1065 enumerator
= lib
->crypto
->create_prf_enumerator(lib
->crypto
);
1066 while (enumerator
->enumerate(enumerator
, &prf
, &plugin_name
))
1068 add_algorithm(b
, pseudo_random_function_names
, prf
, plugin_name
);
1070 enumerator
->destroy(enumerator
);
1073 b
->begin_section(b
, "dh");
1074 enumerator
= lib
->crypto
->create_dh_enumerator(lib
->crypto
);
1075 while (enumerator
->enumerate(enumerator
, &group
, &plugin_name
))
1077 add_algorithm(b
, diffie_hellman_group_names
, group
, plugin_name
);
1079 enumerator
->destroy(enumerator
);
1082 b
->begin_section(b
, "rng");
1083 enumerator
= lib
->crypto
->create_rng_enumerator(lib
->crypto
);
1084 while (enumerator
->enumerate(enumerator
, &quality
, &plugin_name
))
1086 add_algorithm(b
, rng_quality_names
, quality
, plugin_name
);
1088 enumerator
->destroy(enumerator
);
1091 b
->begin_section(b
, "nonce-gen");
1092 enumerator
= lib
->crypto
->create_nonce_gen_enumerator(lib
->crypto
);
1093 while (enumerator
->enumerate(enumerator
, &plugin_name
))
1095 b
->add_kv(b
, "NONCE_GEN", (char*)plugin_name
);
1097 enumerator
->destroy(enumerator
);
1100 return b
->finalize(b
);
1103 CALLBACK(version
, vici_message_t
*,
1104 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
1108 b
= vici_builder_create();
1109 b
->add_kv(b
, "daemon", "%s", lib
->ns
);
1110 b
->add_kv(b
, "version", "%s", VERSION
);
1114 OSVERSIONINFOEX osvie
;
1116 memset(&osvie
, 0, sizeof(osvie
));
1117 osvie
.dwOSVersionInfoSize
= sizeof(osvie
);
1119 if (GetVersionEx((LPOSVERSIONINFO
)&osvie
))
1121 b
->add_kv(b
, "sysname", "Windows %s",
1122 osvie
.wProductType
== VER_NT_WORKSTATION ?
"Client" : "Server");
1123 b
->add_kv(b
, "release", "%d.%d.%d (SP %d.%d)",
1124 osvie
.dwMajorVersion
, osvie
.dwMinorVersion
, osvie
.dwBuildNumber
,
1125 osvie
.wServicePackMajor
, osvie
.wServicePackMinor
);
1126 b
->add_kv(b
, "machine", "%s",
1136 struct utsname utsname
;
1138 if (uname(&utsname
) == 0)
1140 b
->add_kv(b
, "sysname", "%s", utsname
.sysname
);
1141 b
->add_kv(b
, "release", "%s", utsname
.release
);
1142 b
->add_kv(b
, "machine", "%s", utsname
.machine
);
1146 return b
->finalize(b
);
1149 CALLBACK(stats
, vici_message_t
*,
1150 private_vici_query_t
*this, char *name
, u_int id
, vici_message_t
*request
)
1153 enumerator_t
*enumerator
;
1158 b
= vici_builder_create();
1160 now
= time_monotonic(NULL
);
1161 since
= time(NULL
) - (now
- this->uptime
);
1163 b
->begin_section(b
, "uptime");
1164 b
->add_kv(b
, "running", "%V", &now
, &this->uptime
);
1165 b
->add_kv(b
, "since", "%T", &since
, FALSE
);
1168 b
->begin_section(b
, "workers");
1169 b
->add_kv(b
, "total", "%d",
1170 lib
->processor
->get_total_threads(lib
->processor
));
1171 b
->add_kv(b
, "idle", "%d",
1172 lib
->processor
->get_idle_threads(lib
->processor
));
1173 b
->begin_section(b
, "active");
1174 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
1176 b
->add_kv(b
, enum_to_name(job_priority_names
, i
), "%d",
1177 lib
->processor
->get_working_threads(lib
->processor
, i
));
1182 b
->begin_section(b
, "queues");
1183 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
1185 b
->add_kv(b
, enum_to_name(job_priority_names
, i
), "%d",
1186 lib
->processor
->get_job_load(lib
->processor
, i
));
1190 b
->add_kv(b
, "scheduled", "%d",
1191 lib
->scheduler
->get_job_load(lib
->scheduler
));
1193 b
->begin_section(b
, "ikesas");
1194 b
->add_kv(b
, "total", "%u",
1195 charon
->ike_sa_manager
->get_count(charon
->ike_sa_manager
));
1196 b
->add_kv(b
, "half-open", "%u",
1197 charon
->ike_sa_manager
->get_half_open_count(charon
->ike_sa_manager
,
1201 b
->begin_list(b
, "plugins");
1202 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
1203 while (enumerator
->enumerate(enumerator
, &plugin
, NULL
))
1205 b
->add_li(b
, "%s", plugin
->get_name(plugin
));
1207 enumerator
->destroy(enumerator
);
1212 DWORD lasterr
= ERROR_INVALID_HANDLE
;
1219 b
->begin_section(b
, "mem");
1220 count
= GetProcessHeaps(countof(heaps
), heaps
);
1221 for (i
= 0; i
< count
; i
++)
1223 PROCESS_HEAP_ENTRY entry
= {};
1224 size_t heap_total
= 0;
1225 int heap_allocs
= 0;
1227 if (HeapLock(heaps
[i
]))
1229 while (HeapWalk(heaps
[i
], &entry
))
1231 if (entry
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
)
1233 heap_total
+= entry
.cbData
;
1237 lasterr
= GetLastError();
1238 HeapUnlock(heaps
[i
]);
1240 if (lasterr
!= ERROR_NO_MORE_ITEMS
)
1244 snprintf(buf
, sizeof(buf
), "heap-%d", i
);
1245 b
->begin_section(b
, buf
);
1246 b
->add_kv(b
, "total", "%zu", heap_total
);
1247 b
->add_kv(b
, "allocs", "%d", heap_allocs
);
1250 total
+= heap_total
;
1251 allocs
+= heap_allocs
;
1253 if (lasterr
== ERROR_NO_MORE_ITEMS
)
1255 b
->add_kv(b
, "total", "%zu", total
);
1256 b
->add_kv(b
, "allocs", "%d", allocs
);
1262 #ifdef HAVE_MALLINFO
1264 struct mallinfo mi
= mallinfo();
1266 b
->begin_section(b
, "mallinfo");
1267 b
->add_kv(b
, "sbrk", "%u", mi
.arena
);
1268 b
->add_kv(b
, "mmap", "%u", mi
.hblkhd
);
1269 b
->add_kv(b
, "used", "%u", mi
.uordblks
);
1270 b
->add_kv(b
, "free", "%u", mi
.fordblks
);
1273 #endif /* HAVE_MALLINFO */
1275 return b
->finalize(b
);
1278 static void manage_command(private_vici_query_t
*this,
1279 char *name
, vici_command_cb_t cb
, bool reg
)
1281 this->dispatcher
->manage_command(this->dispatcher
, name
,
1282 reg ? cb
: NULL
, this);
1286 * (Un-)register dispatcher functions
1288 static void manage_commands(private_vici_query_t
*this, bool reg
)
1290 this->dispatcher
->manage_event(this->dispatcher
, "list-sa", reg
);
1291 this->dispatcher
->manage_event(this->dispatcher
, "list-policy", reg
);
1292 this->dispatcher
->manage_event(this->dispatcher
, "list-conn", reg
);
1293 this->dispatcher
->manage_event(this->dispatcher
, "list-cert", reg
);
1294 this->dispatcher
->manage_event(this->dispatcher
, "ike-updown", reg
);
1295 this->dispatcher
->manage_event(this->dispatcher
, "ike-rekey", reg
);
1296 this->dispatcher
->manage_event(this->dispatcher
, "child-updown", reg
);
1297 this->dispatcher
->manage_event(this->dispatcher
, "child-rekey", reg
);
1298 manage_command(this, "list-sas", list_sas
, reg
);
1299 manage_command(this, "list-policies", list_policies
, reg
);
1300 manage_command(this, "list-conns", list_conns
, reg
);
1301 manage_command(this, "list-certs", list_certs
, reg
);
1302 manage_command(this, "get-algorithms", get_algorithms
, reg
);
1303 manage_command(this, "version", version
, reg
);
1304 manage_command(this, "stats", stats
, reg
);
1307 METHOD(listener_t
, ike_updown
, bool,
1308 private_vici_query_t
*this, ike_sa_t
*ike_sa
, bool up
)
1313 if (!this->dispatcher
->has_event_listeners(this->dispatcher
, "ike-updown"))
1318 now
= time_monotonic(NULL
);
1320 b
= vici_builder_create();
1324 b
->add_kv(b
, "up", "yes");
1327 b
->begin_section(b
, ike_sa
->get_name(ike_sa
));
1328 list_ike(this, b
, ike_sa
, now
);
1331 this->dispatcher
->raise_event(this->dispatcher
,
1332 "ike-updown", 0, b
->finalize(b
));
1337 METHOD(listener_t
, ike_rekey
, bool,
1338 private_vici_query_t
*this, ike_sa_t
*old
, ike_sa_t
*new)
1343 if (!this->dispatcher
->has_event_listeners(this->dispatcher
, "ike-rekey"))
1348 now
= time_monotonic(NULL
);
1350 b
= vici_builder_create();
1351 b
->begin_section(b
, old
->get_name(old
));
1352 b
->begin_section(b
, "old");
1353 list_ike(this, b
, old
, now
);
1355 b
->begin_section(b
, "new");
1356 list_ike(this, b
, new, now
);
1360 this->dispatcher
->raise_event(this->dispatcher
,
1361 "ike-rekey", 0, b
->finalize(b
));
1366 METHOD(listener_t
, child_updown
, bool,
1367 private_vici_query_t
*this, ike_sa_t
*ike_sa
, child_sa_t
*child_sa
, bool up
)
1372 if (!this->dispatcher
->has_event_listeners(this->dispatcher
, "child-updown"))
1377 now
= time_monotonic(NULL
);
1378 b
= vici_builder_create();
1382 b
->add_kv(b
, "up", "yes");
1385 b
->begin_section(b
, ike_sa
->get_name(ike_sa
));
1386 list_ike(this, b
, ike_sa
, now
);
1387 b
->begin_section(b
, "child-sas");
1389 b
->begin_section(b
, child_sa
->get_name(child_sa
));
1390 list_child(this, b
, child_sa
, now
);
1396 this->dispatcher
->raise_event(this->dispatcher
,
1397 "child-updown", 0, b
->finalize(b
));
1402 METHOD(listener_t
, child_rekey
, bool,
1403 private_vici_query_t
*this, ike_sa_t
*ike_sa
, child_sa_t
*old
,
1409 if (!this->dispatcher
->has_event_listeners(this->dispatcher
, "child-rekey"))
1414 now
= time_monotonic(NULL
);
1415 b
= vici_builder_create();
1417 b
->begin_section(b
, ike_sa
->get_name(ike_sa
));
1418 list_ike(this, b
, ike_sa
, now
);
1419 b
->begin_section(b
, "child-sas");
1421 b
->begin_section(b
, old
->get_name(old
));
1423 b
->begin_section(b
, "old");
1424 list_child(this, b
, old
, now
);
1426 b
->begin_section(b
, "new");
1427 list_child(this, b
, new, now
);
1435 this->dispatcher
->raise_event(this->dispatcher
,
1436 "child-rekey", 0, b
->finalize(b
));
1441 METHOD(vici_query_t
, destroy
, void,
1442 private_vici_query_t
*this)
1444 manage_commands(this, FALSE
);
1451 vici_query_t
*vici_query_create(vici_dispatcher_t
*dispatcher
)
1453 private_vici_query_t
*this;
1458 .ike_updown
= _ike_updown
,
1459 .ike_rekey
= _ike_rekey
,
1460 .child_updown
= _child_updown
,
1461 .child_rekey
= _child_rekey
,
1463 .destroy
= _destroy
,
1465 .dispatcher
= dispatcher
,
1466 .uptime
= time_monotonic(NULL
),
1469 manage_commands(this, TRUE
);
1471 return &this->public;