child-cfg: Use flags for boolean options
[strongswan.git] / src / libcharon / plugins / stroke / stroke_list.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2015 Andreas Steffen
6 * HSR Hochschule fuer Technik Rapperswil
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 #include "stroke_list.h"
20
21 #include <inttypes.h>
22 #include <time.h>
23 #include <sys/utsname.h>
24
25 #ifdef HAVE_MALLINFO
26 #include <malloc.h>
27 #endif /* HAVE_MALLINFO */
28
29 #include <daemon.h>
30 #include <collections/linked_list.h>
31 #include <plugins/plugin.h>
32 #include <credentials/certificates/x509.h>
33 #include <credentials/certificates/certificate_printer.h>
34 #include <config/peer_cfg.h>
35
36 typedef struct private_stroke_list_t private_stroke_list_t;
37
38 /**
39 * private data of stroke_list
40 */
41 struct private_stroke_list_t {
42
43 /**
44 * public functions
45 */
46 stroke_list_t public;
47
48 /**
49 * Kind of *swan we run
50 */
51 char *swan;
52
53 /**
54 * timestamp of daemon start
55 */
56 time_t uptime;
57
58 /**
59 * strokes attribute provider
60 */
61 stroke_attribute_t *attribute;
62 };
63
64 /**
65 * Static certificate printer object
66 */
67 static certificate_printer_t *cert_printer = NULL;
68
69 /**
70 * Log tasks of a specific queue to out
71 */
72 static void log_task_q(FILE *out, ike_sa_t *ike_sa, task_queue_t q, char *name)
73 {
74 enumerator_t *enumerator;
75 bool has = FALSE;
76 task_t *task;
77
78 enumerator = ike_sa->create_task_enumerator(ike_sa, q);
79 while (enumerator->enumerate(enumerator, &task))
80 {
81 if (!has)
82 {
83 fprintf(out, "%12s[%d]: Tasks %s: ", ike_sa->get_name(ike_sa),
84 ike_sa->get_unique_id(ike_sa), name);
85 has = TRUE;
86 }
87 fprintf(out, "%N ", task_type_names, task->get_type(task));
88 }
89 enumerator->destroy(enumerator);
90 if (has)
91 {
92 fprintf(out, "\n");
93 }
94 }
95
96 /**
97 * log an IKE_SA to out
98 */
99 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
100 {
101 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
102 time_t now = time_monotonic(NULL);
103
104 fprintf(out, "%12s[%d]: %N",
105 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
106 ike_sa_state_names, ike_sa->get_state(ike_sa));
107
108 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
109 {
110 time_t established;
111
112 established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
113 fprintf(out, " %V ago", &now, &established);
114 }
115
116 fprintf(out, ", %H[%Y]...%H[%Y]\n",
117 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
118 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
119
120 if (all)
121 {
122 proposal_t *ike_proposal;
123 identification_t *eap_id;
124
125 eap_id = ike_sa->get_other_eap_id(ike_sa);
126
127 if (!eap_id->equals(eap_id, ike_sa->get_other_id(ike_sa)))
128 {
129 fprintf(out, "%12s[%d]: Remote %s identity: %Y\n",
130 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
131 ike_sa->get_version(ike_sa) == IKEV1 ? "XAuth" : "EAP",
132 eap_id);
133 }
134
135 ike_proposal = ike_sa->get_proposal(ike_sa);
136
137 fprintf(out, "%12s[%d]: %N SPIs: %.16"PRIx64"_i%s %.16"PRIx64"_r%s",
138 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
139 ike_version_names, ike_sa->get_version(ike_sa),
140 be64toh(id->get_initiator_spi(id)),
141 id->is_initiator(id) ? "*" : "",
142 be64toh(id->get_responder_spi(id)),
143 id->is_initiator(id) ? "" : "*");
144
145
146 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
147 {
148 time_t rekey, reauth;
149 peer_cfg_t *peer_cfg;
150
151 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY);
152 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
153 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
154
155 if (rekey)
156 {
157 fprintf(out, ", rekeying in %V", &rekey, &now);
158 }
159 if (reauth)
160 {
161 bool first = TRUE;
162 enumerator_t *enumerator;
163 auth_cfg_t *auth;
164
165 fprintf(out, ", ");
166 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, TRUE);
167 while (enumerator->enumerate(enumerator, &auth))
168 {
169 if (!first)
170 {
171 fprintf(out, "+");
172 }
173 first = FALSE;
174 fprintf(out, "%N", auth_class_names,
175 auth->get(auth, AUTH_RULE_AUTH_CLASS));
176 }
177 enumerator->destroy(enumerator);
178 fprintf(out, " reauthentication in %V", &reauth, &now);
179 }
180 if (!rekey && !reauth)
181 {
182 fprintf(out, ", rekeying disabled");
183 }
184 }
185 fprintf(out, "\n");
186
187 if (ike_proposal)
188 {
189 char buf[BUF_LEN];
190
191 snprintf(buf, BUF_LEN, "%P", ike_proposal);
192 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
193 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
194 buf+4);
195 }
196
197 log_task_q(out, ike_sa, TASK_QUEUE_QUEUED, "queued");
198 log_task_q(out, ike_sa, TASK_QUEUE_ACTIVE, "active");
199 log_task_q(out, ike_sa, TASK_QUEUE_PASSIVE, "passive");
200 }
201 }
202
203 /**
204 * log an CHILD_SA to out
205 */
206 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
207 {
208 time_t use_in, use_out, rekey, now;
209 uint64_t bytes_in, bytes_out, packets_in, packets_out;
210 proposal_t *proposal;
211 linked_list_t *my_ts, *other_ts;
212 child_cfg_t *config;
213
214 config = child_sa->get_config(child_sa);
215 now = time_monotonic(NULL);
216
217 fprintf(out, "%12s{%d}: %N, %N%s, reqid %u",
218 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
219 child_sa_state_names, child_sa->get_state(child_sa),
220 ipsec_mode_names, child_sa->get_mode(child_sa),
221 config->has_option(config, OPT_PROXY_MODE) ? "_PROXY" : "",
222 child_sa->get_reqid(child_sa));
223
224 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
225 {
226 fprintf(out, ", %N%s SPIs: %.8x_i %.8x_o",
227 protocol_id_names, child_sa->get_protocol(child_sa),
228 child_sa->has_encap(child_sa) ? " in UDP" : "",
229 ntohl(child_sa->get_spi(child_sa, TRUE)),
230 ntohl(child_sa->get_spi(child_sa, FALSE)));
231
232 if (child_sa->get_ipcomp(child_sa) != IPCOMP_NONE)
233 {
234 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
235 ntohs(child_sa->get_cpi(child_sa, TRUE)),
236 ntohs(child_sa->get_cpi(child_sa, FALSE)));
237 }
238
239 if (all)
240 {
241 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
242 child_sa->get_unique_id(child_sa));
243
244 proposal = child_sa->get_proposal(child_sa);
245 if (proposal)
246 {
247 uint16_t alg, ks;
248 bool first = TRUE;
249
250 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
251 &alg, &ks) && alg != ENCR_UNDEFINED)
252 {
253 fprintf(out, "%N", encryption_algorithm_names, alg);
254 first = FALSE;
255 if (ks)
256 {
257 fprintf(out, "_%u", ks);
258 }
259 }
260 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
261 &alg, &ks) && alg != AUTH_UNDEFINED)
262 {
263 fprintf(out, "%s%N", first ? "" : "/",
264 integrity_algorithm_names, alg);
265 if (ks)
266 {
267 fprintf(out, "_%u", ks);
268 }
269 }
270 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
271 &alg, NULL))
272 {
273 fprintf(out, "/%N", diffie_hellman_group_names, alg);
274 }
275 if (proposal->get_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
276 &alg, NULL) && alg == EXT_SEQ_NUMBERS)
277 {
278 fprintf(out, "/ESN");
279 }
280 }
281
282 child_sa->get_usestats(child_sa, TRUE,
283 &use_in, &bytes_in, &packets_in);
284 fprintf(out, ", %" PRIu64 " bytes_i", bytes_in);
285 if (use_in)
286 {
287 fprintf(out, " (%" PRIu64 " pkt%s, %" PRIu64 "s ago)",
288 packets_in, (packets_in == 1) ? "": "s",
289 (uint64_t)(now - use_in));
290 }
291
292 child_sa->get_usestats(child_sa, FALSE,
293 &use_out, &bytes_out, &packets_out);
294 fprintf(out, ", %" PRIu64 " bytes_o", bytes_out);
295 if (use_out)
296 {
297 fprintf(out, " (%" PRIu64 " pkt%s, %" PRIu64 "s ago)",
298 packets_out, (packets_out == 1) ? "": "s",
299 (uint64_t)(now - use_out));
300 }
301 fprintf(out, ", rekeying ");
302
303 rekey = child_sa->get_lifetime(child_sa, FALSE);
304 if (rekey)
305 {
306 if (now > rekey)
307 {
308 fprintf(out, "active");
309 }
310 else
311 {
312 fprintf(out, "in %V", &now, &rekey);
313 }
314 }
315 else
316 {
317 fprintf(out, "disabled");
318 }
319
320 }
321 }
322 else if (child_sa->get_state(child_sa) == CHILD_REKEYING ||
323 child_sa->get_state(child_sa) == CHILD_REKEYED)
324 {
325 rekey = child_sa->get_lifetime(child_sa, TRUE);
326 fprintf(out, ", expires in %V", &now, &rekey);
327 }
328
329 my_ts = linked_list_create_from_enumerator(
330 child_sa->create_ts_enumerator(child_sa, TRUE));
331 other_ts = linked_list_create_from_enumerator(
332 child_sa->create_ts_enumerator(child_sa, FALSE));
333 fprintf(out, "\n%12s{%d}: %#R === %#R\n",
334 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
335 my_ts, other_ts);
336 my_ts->destroy(my_ts);
337 other_ts->destroy(other_ts);
338 }
339
340 /**
341 * Log a configs local or remote authentication config to out
342 */
343 static void log_auth_cfgs(FILE *out, peer_cfg_t *peer_cfg, bool local)
344 {
345 enumerator_t *enumerator, *rules;
346 auth_rule_t rule;
347 auth_cfg_t *auth;
348 auth_class_t auth_class;
349 identification_t *id;
350 certificate_t *cert;
351 cert_validation_t valid;
352 char *name;
353
354 name = peer_cfg->get_name(peer_cfg);
355
356 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
357 while (enumerator->enumerate(enumerator, &auth))
358 {
359 fprintf(out, "%12s: %s", name, local ? "local: " : "remote:");
360 id = auth->get(auth, AUTH_RULE_IDENTITY);
361 if (id)
362 {
363 fprintf(out, " [%Y]", id);
364 }
365 fprintf(out, " uses ");
366
367 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
368 if (auth_class == AUTH_CLASS_EAP)
369 {
370 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
371 {
372 fprintf(out, "EAP authentication");
373 }
374 else
375 {
376 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
377 {
378 fprintf(out, "EAP_%" PRIuPTR "-%" PRIuPTR " authentication",
379 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
380 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
381 }
382 else
383 {
384 fprintf(out, "%N authentication", eap_type_names,
385 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
386 }
387 }
388 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
389 if (id)
390 {
391 fprintf(out, " with EAP identity '%Y'", id);
392 }
393 fprintf(out, "\n");
394 }
395 else if (auth_class == AUTH_CLASS_XAUTH)
396 {
397 fprintf(out, "%N authentication: %s", auth_class_names, auth_class,
398 auth->get(auth, AUTH_RULE_XAUTH_BACKEND) ?: "any");
399 id = auth->get(auth, AUTH_RULE_XAUTH_IDENTITY);
400 if (id)
401 {
402 fprintf(out, " with XAuth identity '%Y'", id);
403 }
404 fprintf(out, "\n");
405 }
406 else
407 {
408 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
409 }
410
411 cert = auth->get(auth, AUTH_RULE_CA_CERT);
412 if (cert)
413 {
414 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
415 }
416
417 cert = auth->get(auth, AUTH_RULE_IM_CERT);
418 if (cert)
419 {
420 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
421 }
422
423 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
424 if (cert)
425 {
426 fprintf(out, "%12s: cert: \"%Y\"\n", name,
427 cert->get_subject(cert));
428 }
429
430 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
431 if (valid != VALIDATION_FAILED)
432 {
433 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
434 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
435 }
436
437 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
438 if (valid != VALIDATION_FAILED)
439 {
440 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
441 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
442 }
443
444 rules = auth->create_enumerator(auth);
445 while (rules->enumerate(rules, &rule, &id))
446 {
447 if (rule == AUTH_RULE_GROUP)
448 {
449 fprintf(out, "%12s: group: %Y\n", name, id);
450 }
451 }
452 rules->destroy(rules);
453 }
454 enumerator->destroy(enumerator);
455 }
456
457 METHOD(stroke_list_t, status, void,
458 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out,
459 bool all, bool wait)
460 {
461 enumerator_t *enumerator, *children;
462 ike_cfg_t *ike_cfg;
463 child_cfg_t *child_cfg;
464 child_sa_t *child_sa;
465 ike_sa_t *ike_sa;
466 linked_list_t *my_ts, *other_ts;
467 bool first, found = FALSE;
468 char *name = msg->status.name;
469 u_int half_open;
470
471 if (all)
472 {
473 peer_cfg_t *peer_cfg;
474 ike_version_t ike_version;
475 char *pool;
476 host_t *host;
477 uint32_t dpd;
478 time_t since, now;
479 u_int size, online, offline, i;
480 struct utsname utsname;
481
482 now = time_monotonic(NULL);
483 since = time(NULL) - (now - this->uptime);
484
485 fprintf(out, "Status of IKE charon daemon (%sSwan "VERSION, this->swan);
486 if (uname(&utsname) == 0)
487 {
488 fprintf(out, ", %s %s, %s",
489 utsname.sysname, utsname.release, utsname.machine);
490 }
491 fprintf(out, "):\n uptime: %V, since %T\n", &now, &this->uptime, &since,
492 FALSE);
493 #ifdef HAVE_MALLINFO
494 {
495 struct mallinfo mi = mallinfo();
496
497 fprintf(out, " malloc: sbrk %u, mmap %u, used %u, free %u\n",
498 mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
499 }
500 #endif /* HAVE_MALLINFO */
501 fprintf(out, " worker threads: %d of %d idle, ",
502 lib->processor->get_idle_threads(lib->processor),
503 lib->processor->get_total_threads(lib->processor));
504 for (i = 0; i < JOB_PRIO_MAX; i++)
505 {
506 fprintf(out, "%s%d", i == 0 ? "" : "/",
507 lib->processor->get_working_threads(lib->processor, i));
508 }
509 fprintf(out, " working, job queue: ");
510 for (i = 0; i < JOB_PRIO_MAX; i++)
511 {
512 fprintf(out, "%s%d", i == 0 ? "" : "/",
513 lib->processor->get_job_load(lib->processor, i));
514 }
515 fprintf(out, ", scheduled: %d\n",
516 lib->scheduler->get_job_load(lib->scheduler));
517 fprintf(out, " loaded plugins: %s\n",
518 lib->plugins->loaded_plugins(lib->plugins));
519
520 first = TRUE;
521 enumerator = this->attribute->create_pool_enumerator(this->attribute);
522 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
523 {
524 if (name && !streq(name, pool))
525 {
526 continue;
527 }
528 if (first)
529 {
530 first = FALSE;
531 fprintf(out, "Virtual IP pools (size/online/offline):\n");
532 }
533 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
534 }
535 enumerator->destroy(enumerator);
536
537 enumerator = charon->kernel->create_address_enumerator(charon->kernel,
538 ADDR_TYPE_REGULAR);
539 fprintf(out, "Listening IP addresses:\n");
540 while (enumerator->enumerate(enumerator, (void**)&host))
541 {
542 fprintf(out, " %H\n", host);
543 }
544 enumerator->destroy(enumerator);
545
546 fprintf(out, "Connections:\n");
547 enumerator = charon->backends->create_peer_cfg_enumerator(
548 charon->backends, NULL, NULL, NULL, NULL, IKE_ANY);
549 while (enumerator->enumerate(enumerator, &peer_cfg))
550 {
551 char *my_addr, *other_addr;
552
553 if (name && !streq(name, peer_cfg->get_name(peer_cfg)))
554 {
555 continue;
556 }
557
558 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
559 ike_version = peer_cfg->get_ike_version(peer_cfg);
560 my_addr = ike_cfg->get_my_addr(ike_cfg);
561 other_addr = ike_cfg->get_other_addr(ike_cfg);
562 fprintf(out, "%12s: %s...%s %N", peer_cfg->get_name(peer_cfg),
563 my_addr, other_addr, ike_version_names, ike_version);
564
565 if (ike_version == IKEV1 && peer_cfg->use_aggressive(peer_cfg))
566 {
567 fprintf(out, " Aggressive");
568 }
569
570 dpd = peer_cfg->get_dpd(peer_cfg);
571 if (dpd)
572 {
573 fprintf(out, ", dpddelay=%us", dpd);
574 }
575 fprintf(out, "\n");
576
577 log_auth_cfgs(out, peer_cfg, TRUE);
578 log_auth_cfgs(out, peer_cfg, FALSE);
579
580 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
581 while (children->enumerate(children, &child_cfg))
582 {
583 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
584 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
585 fprintf(out, "%12s: child: %#R === %#R %N",
586 child_cfg->get_name(child_cfg), my_ts, other_ts,
587 ipsec_mode_names, child_cfg->get_mode(child_cfg));
588 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
589 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
590
591 if (dpd)
592 {
593 fprintf(out, ", dpdaction=%N", action_names,
594 child_cfg->get_dpd_action(child_cfg));
595 }
596 fprintf(out, "\n");
597 }
598 children->destroy(children);
599 }
600 enumerator->destroy(enumerator);
601 }
602
603 /* Enumerate shunt policies */
604 first = TRUE;
605 enumerator = charon->shunts->create_enumerator(charon->shunts);
606 while (enumerator->enumerate(enumerator, NULL, &child_cfg))
607 {
608 if (name && !streq(name, child_cfg->get_name(child_cfg)))
609 {
610 continue;
611 }
612 if (first)
613 {
614 fprintf(out, "Shunted Connections:\n");
615 first = FALSE;
616 }
617 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
618 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
619 fprintf(out, "%12s: %#R === %#R %N\n",
620 child_cfg->get_name(child_cfg), my_ts, other_ts,
621 ipsec_mode_names, child_cfg->get_mode(child_cfg));
622 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
623 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
624 }
625 enumerator->destroy(enumerator);
626
627 /* Enumerate traps */
628 first = TRUE;
629 enumerator = charon->traps->create_enumerator(charon->traps);
630 while (enumerator->enumerate(enumerator, NULL, &child_sa))
631 {
632 if (name && !streq(name, child_sa->get_name(child_sa)))
633 {
634 continue;
635 }
636 if (first)
637 {
638 fprintf(out, "Routed Connections:\n");
639 first = FALSE;
640 }
641 log_child_sa(out, child_sa, all);
642 }
643 enumerator->destroy(enumerator);
644
645 half_open = charon->ike_sa_manager->get_half_open_count(
646 charon->ike_sa_manager, NULL, FALSE);
647 fprintf(out, "Security Associations (%u up, %u connecting):\n",
648 charon->ike_sa_manager->get_count(charon->ike_sa_manager) - half_open,
649 half_open);
650 enumerator = charon->controller->create_ike_sa_enumerator(
651 charon->controller, wait);
652 while (enumerator->enumerate(enumerator, &ike_sa) && ferror(out) == 0)
653 {
654 bool ike_printed = FALSE;
655 enumerator_t *children = ike_sa->create_child_sa_enumerator(ike_sa);
656
657 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
658 {
659 log_ike_sa(out, ike_sa, all);
660 found = TRUE;
661 ike_printed = TRUE;
662 }
663
664 while (children->enumerate(children, (void**)&child_sa))
665 {
666 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
667 {
668 if (!ike_printed)
669 {
670 log_ike_sa(out, ike_sa, all);
671 found = TRUE;
672 ike_printed = TRUE;
673 }
674 log_child_sa(out, child_sa, all);
675 }
676 }
677 children->destroy(children);
678 }
679 enumerator->destroy(enumerator);
680
681 if (!found)
682 {
683 if (name)
684 {
685 fprintf(out, " no match\n");
686 }
687 else
688 {
689 fprintf(out, " none\n");
690 }
691 }
692 }
693
694 /**
695 * create a unique certificate list without duplicates
696 * certicates having the same issuer are grouped together.
697 */
698 static linked_list_t* create_unique_cert_list(certificate_type_t type)
699 {
700 linked_list_t *list = linked_list_create();
701 enumerator_t *enumerator = lib->credmgr->create_cert_enumerator(
702 lib->credmgr, type, KEY_ANY, NULL, FALSE);
703 certificate_t *cert;
704
705 while (enumerator->enumerate(enumerator, (void**)&cert))
706 {
707 enumerator_t *added = list->create_enumerator(list);
708 identification_t *issuer = cert->get_issuer(cert);
709 bool previous_same, same = FALSE, found = FALSE;
710 certificate_t *list_cert;
711
712 while (added->enumerate(added, (void**)&list_cert))
713 {
714 if (list_cert->equals(list_cert, cert))
715 { /* stop if we found a duplicate*/
716 found = TRUE;
717 break;
718 }
719 previous_same = same;
720 same = list_cert->has_issuer(list_cert, issuer);
721 if (previous_same && !same)
722 { /* group certificates with same issuer */
723 break;
724 }
725 }
726 if (!found)
727 {
728 list->insert_before(list, added, cert->get_ref(cert));
729 }
730 added->destroy(added);
731 }
732 enumerator->destroy(enumerator);
733 return list;
734 }
735
736 /**
737 * Is there a matching private key?
738 */
739 static bool has_privkey(certificate_t *cert)
740 {
741 public_key_t *public;
742 private_key_t *private = NULL;
743 chunk_t keyid;
744 identification_t *id;
745
746 public = cert->get_public_key(cert);
747 if (!public)
748 {
749 return FALSE;
750 }
751 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
752 {
753 id = identification_create_from_encoding(ID_KEY_ID, keyid);
754 private = lib->credmgr->get_private(lib->credmgr,
755 public->get_type(public), id, NULL);
756 id->destroy(id);
757 }
758 public->destroy(public);
759 DESTROY_IF(private);
760
761 return (private != NULL);
762 }
763
764 /**
765 * list all X.509 certificates matching the flags
766 */
767 static void stroke_list_x509_certs(linked_list_t *list, x509_flag_t flag)
768 {
769 enumerator_t *enumerator;
770 certificate_t *cert;
771
772 enumerator = list->create_enumerator(list);
773 while (enumerator->enumerate(enumerator, (void**)&cert))
774 {
775 x509_t *x509 = (x509_t*)cert;
776 x509_flag_t flags = x509->get_flags(x509) & X509_ANY;
777
778 /* list only if flag is set or flag == 0 */
779 if ((flags & flag) || flags == flag)
780 {
781 cert_printer->print_caption(cert_printer, CERT_X509, flag);
782 cert_printer->print(cert_printer, cert, has_privkey(cert));
783 }
784 }
785 enumerator->destroy(enumerator);
786 }
787
788 /**
789 * list all other certificates types
790 */
791 static void stroke_list_other_certs(certificate_type_t type)
792 {
793 enumerator_t *enumerator;
794 certificate_t *cert;
795 linked_list_t *list;
796
797 list = create_unique_cert_list(type);
798
799 enumerator = list->create_enumerator(list);
800 while (enumerator->enumerate(enumerator, &cert))
801 {
802 cert_printer->print_caption(cert_printer, cert->get_type(cert), X509_NONE);
803 cert_printer->print(cert_printer, cert, has_privkey(cert));
804 }
805 enumerator->destroy(enumerator);
806
807 list->destroy_offset(list, offsetof(certificate_t, destroy));
808 }
809
810 /**
811 * Print the name of an algorithm plus the name of the plugin that registered it
812 */
813 static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
814 const char *plugin_name)
815 {
816 char alg_name[BUF_LEN];
817 int alg_name_len;
818
819 if (alg_names)
820 {
821 alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type,
822 plugin_name);
823 }
824 else
825 {
826 alg_name_len = sprintf(alg_name, " [%s]", plugin_name);
827 }
828 if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
829 {
830 fprintf(out, "\n ");
831 *len = 13;
832 }
833 fprintf(out, "%s", alg_name);
834 *len += alg_name_len;
835 }
836
837 /**
838 * List of registered cryptographical algorithms
839 */
840 static void list_algs(FILE *out)
841 {
842 enumerator_t *enumerator;
843 encryption_algorithm_t encryption;
844 integrity_algorithm_t integrity;
845 hash_algorithm_t hash;
846 pseudo_random_function_t prf;
847 ext_out_function_t xof;
848 diffie_hellman_group_t group;
849 rng_quality_t quality;
850 const char *plugin_name;
851 int len;
852
853 fprintf(out, "\n");
854 fprintf(out, "List of registered IKE algorithms:\n");
855 fprintf(out, "\n encryption:");
856 len = 13;
857 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
858 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
859 {
860 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
861 }
862 enumerator->destroy(enumerator);
863 fprintf(out, "\n integrity: ");
864 len = 13;
865 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
866 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
867 {
868 print_alg(out, &len, integrity_algorithm_names, integrity, plugin_name);
869 }
870 enumerator->destroy(enumerator);
871 fprintf(out, "\n aead: ");
872 len = 13;
873 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
874 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
875 {
876 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
877 }
878 enumerator->destroy(enumerator);
879 fprintf(out, "\n hasher: ");
880 len = 13;
881 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
882 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
883 {
884 print_alg(out, &len, hash_algorithm_names, hash, plugin_name);
885 }
886 enumerator->destroy(enumerator);
887 fprintf(out, "\n prf: ");
888 len = 13;
889 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
890 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
891 {
892 print_alg(out, &len, pseudo_random_function_names, prf, plugin_name);
893 }
894 enumerator->destroy(enumerator);
895 fprintf(out, "\n xof: ");
896 len = 13;
897 enumerator = lib->crypto->create_xof_enumerator(lib->crypto);
898 while (enumerator->enumerate(enumerator, &xof, &plugin_name))
899 {
900 print_alg(out, &len, ext_out_function_names, xof, plugin_name);
901 }
902 enumerator->destroy(enumerator);
903 fprintf(out, "\n dh-group: ");
904 len = 13;
905 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
906 while (enumerator->enumerate(enumerator, &group, &plugin_name))
907 {
908 print_alg(out, &len, diffie_hellman_group_names, group, plugin_name);
909 }
910 enumerator->destroy(enumerator);
911 fprintf(out, "\n random-gen:");
912 len = 13;
913 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
914 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
915 {
916 print_alg(out, &len, rng_quality_names, quality, plugin_name);
917 }
918 enumerator->destroy(enumerator);
919 fprintf(out, "\n nonce-gen: ");
920 len = 13;
921 enumerator = lib->crypto->create_nonce_gen_enumerator(lib->crypto);
922 while (enumerator->enumerate(enumerator, &plugin_name))
923 {
924 print_alg(out, &len, NULL, 0, plugin_name);
925 }
926 enumerator->destroy(enumerator);
927 fprintf(out, "\n");
928 }
929
930 /**
931 * List loaded plugin information
932 */
933 static void list_plugins(FILE *out)
934 {
935 plugin_feature_t *features, *fp;
936 enumerator_t *enumerator;
937 linked_list_t *list;
938 plugin_t *plugin;
939 int count, i;
940 bool loaded;
941 char *str;
942
943 fprintf(out, "\n");
944 fprintf(out, "List of loaded Plugins:\n");
945 fprintf(out, "\n");
946
947 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
948 while (enumerator->enumerate(enumerator, &plugin, &list))
949 {
950 fprintf(out, "%s:\n", plugin->get_name(plugin));
951 if (plugin->get_features)
952 {
953 count = plugin->get_features(plugin, &features);
954 for (i = 0; i < count; i++)
955 {
956 str = plugin_feature_get_string(&features[i]);
957 switch (features[i].kind)
958 {
959 case FEATURE_PROVIDE:
960 fp = &features[i];
961 loaded = list->find_first(list, NULL,
962 (void**)&fp) == SUCCESS;
963 fprintf(out, " %s%s\n",
964 str, loaded ? "" : " (not loaded)");
965 break;
966 case FEATURE_DEPENDS:
967 fprintf(out, " %s\n", str);
968 break;
969 case FEATURE_SDEPEND:
970 fprintf(out, " %s (soft)\n", str);
971 break;
972 default:
973 break;
974 }
975 free(str);
976 }
977 }
978 list->destroy(list);
979 }
980 enumerator->destroy(enumerator);
981 }
982
983 METHOD(stroke_list_t, list, void,
984 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
985 {
986 linked_list_t *cert_list = NULL;
987
988 cert_printer = certificate_printer_create(out, TRUE, msg->list.utc);
989
990 if (msg->list.flags & LIST_PUBKEYS)
991 {
992 stroke_list_other_certs(CERT_TRUSTED_PUBKEY);
993 }
994 if (msg->list.flags & LIST_CERTS)
995 {
996 stroke_list_other_certs(CERT_GPG);
997 }
998 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
999 {
1000 cert_list = create_unique_cert_list(CERT_X509);
1001 }
1002 if (msg->list.flags & LIST_CERTS)
1003 {
1004 stroke_list_x509_certs(cert_list, X509_NONE);
1005 }
1006 if (msg->list.flags & LIST_CACERTS)
1007 {
1008 stroke_list_x509_certs(cert_list, X509_CA);
1009 }
1010 if (msg->list.flags & LIST_OCSPCERTS)
1011 {
1012 stroke_list_x509_certs(cert_list, X509_OCSP_SIGNER);
1013 }
1014 if (msg->list.flags & LIST_AACERTS)
1015 {
1016 stroke_list_x509_certs(cert_list, X509_AA);
1017 }
1018 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1019
1020 if (msg->list.flags & LIST_ACERTS)
1021 {
1022 stroke_list_other_certs(CERT_X509_AC);
1023 }
1024 if (msg->list.flags & LIST_CRLS)
1025 {
1026 stroke_list_other_certs(CERT_X509_CRL);
1027 }
1028 if (msg->list.flags & LIST_OCSP)
1029 {
1030 stroke_list_other_certs(CERT_X509_OCSP_RESPONSE);
1031 }
1032 if (msg->list.flags & LIST_ALGS)
1033 {
1034 list_algs(out);
1035 }
1036 if (msg->list.flags & LIST_PLUGINS)
1037 {
1038 list_plugins(out);
1039 }
1040 cert_printer->destroy(cert_printer);
1041 cert_printer = NULL;
1042 }
1043
1044 /**
1045 * Print leases of a single pool
1046 */
1047 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1048 host_t *address, u_int size, u_int online, u_int offline)
1049 {
1050 enumerator_t *enumerator;
1051 identification_t *id;
1052 host_t *lease;
1053 bool on;
1054 int found = 0;
1055
1056 fprintf(out, "Leases in pool '%s', usage: %u/%u, %u online\n",
1057 pool, online + offline, size, online);
1058 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1059 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1060 {
1061 if (!address || address->ip_equals(address, lease))
1062 {
1063 fprintf(out, " %15H %s '%Y'\n",
1064 lease, on ? "online" : "offline", id);
1065 found++;
1066 }
1067 }
1068 enumerator->destroy(enumerator);
1069 if (!found)
1070 {
1071 fprintf(out, " no matching leases found\n");
1072 }
1073 }
1074
1075 METHOD(stroke_list_t, leases, void,
1076 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1077 {
1078 enumerator_t *enumerator;
1079 u_int size, offline, online;
1080 host_t *address = NULL;
1081 char *pool;
1082 int found = 0;
1083
1084 if (msg->leases.address)
1085 {
1086 address = host_create_from_string(msg->leases.address, 0);
1087 }
1088
1089 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1090 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1091 {
1092 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1093 {
1094 pool_leases(this, out, pool, address, size, online, offline);
1095 found++;
1096 }
1097 }
1098 enumerator->destroy(enumerator);
1099 if (!found)
1100 {
1101 if (msg->leases.pool)
1102 {
1103 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1104 }
1105 else
1106 {
1107 fprintf(out, "no pools found\n");
1108 }
1109 }
1110 DESTROY_IF(address);
1111 }
1112
1113 METHOD(stroke_list_t, destroy, void,
1114 private_stroke_list_t *this)
1115 {
1116 free(this);
1117 }
1118
1119 /*
1120 * see header file
1121 */
1122 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1123 {
1124 private_stroke_list_t *this;
1125
1126 INIT(this,
1127 .public = {
1128 .list = _list,
1129 .status = _status,
1130 .leases = _leases,
1131 .destroy = _destroy,
1132 },
1133 .uptime = time_monotonic(NULL),
1134 .swan = "strong",
1135 .attribute = attribute,
1136 );
1137
1138 if (lib->settings->get_bool(lib->settings,
1139 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk", FALSE))
1140 {
1141 this->swan = "weak";
1142 }
1143
1144 return &this->public;
1145 }