Show some uname() info in "ipsec statusall"
[strongswan.git] / src / libcharon / plugins / stroke / stroke_list.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "stroke_list.h"
17
18 #include <inttypes.h>
19 #include <time.h>
20 #include <sys/utsname.h>
21
22 #ifdef HAVE_MALLINFO
23 #include <malloc.h>
24 #endif /* HAVE_MALLINFO */
25
26 #include <hydra.h>
27 #include <daemon.h>
28 #include <utils/linked_list.h>
29 #include <plugins/plugin.h>
30 #include <credentials/certificates/x509.h>
31 #include <credentials/certificates/ac.h>
32 #include <credentials/certificates/crl.h>
33 #include <credentials/certificates/pgp_certificate.h>
34 #include <credentials/ietf_attributes/ietf_attributes.h>
35 #include <config/peer_cfg.h>
36
37 /* warning intervals for list functions */
38 #define CERT_WARNING_INTERVAL 30 /* days */
39 #define CRL_WARNING_INTERVAL 7 /* days */
40 #define AC_WARNING_INTERVAL 1 /* day */
41
42 typedef struct private_stroke_list_t private_stroke_list_t;
43
44 /**
45 * private data of stroke_list
46 */
47 struct private_stroke_list_t {
48
49 /**
50 * public functions
51 */
52 stroke_list_t public;
53
54 /**
55 * Kind of *swan we run
56 */
57 char *swan;
58
59 /**
60 * timestamp of daemon start
61 */
62 time_t uptime;
63
64 /**
65 * strokes attribute provider
66 */
67 stroke_attribute_t *attribute;
68 };
69
70 /**
71 * Log tasks of a specific queue to out
72 */
73 static void log_task_q(FILE *out, ike_sa_t *ike_sa, task_queue_t q, char *name)
74 {
75 enumerator_t *enumerator;
76 bool has = FALSE;
77 task_t *task;
78
79 enumerator = ike_sa->create_task_enumerator(ike_sa, q);
80 while (enumerator->enumerate(enumerator, &task))
81 {
82 if (!has)
83 {
84 fprintf(out, "%12s[%d]: Tasks %s: ", ike_sa->get_name(ike_sa),
85 ike_sa->get_unique_id(ike_sa), name);
86 has = TRUE;
87 }
88 fprintf(out, "%N ", task_type_names, task->get_type(task));
89 }
90 enumerator->destroy(enumerator);
91 if (has)
92 {
93 fprintf(out, "\n");
94 }
95 }
96
97 /**
98 * log an IKE_SA to out
99 */
100 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
101 {
102 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
103 time_t now = time_monotonic(NULL);
104
105 fprintf(out, "%12s[%d]: %N",
106 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
107 ike_sa_state_names, ike_sa->get_state(ike_sa));
108
109 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
110 {
111 time_t established;
112
113 established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
114 fprintf(out, " %V ago", &now, &established);
115 }
116
117 fprintf(out, ", %H[%Y]...%H[%Y]\n",
118 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
119 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
120
121 if (all)
122 {
123 proposal_t *ike_proposal;
124 identification_t *eap_id;
125
126 eap_id = ike_sa->get_other_eap_id(ike_sa);
127
128 if (!eap_id->equals(eap_id, ike_sa->get_other_id(ike_sa)))
129 {
130 fprintf(out, "%12s[%d]: Remote %s identity: %Y\n",
131 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
132 ike_sa->get_version(ike_sa) == IKEV1 ? "XAuth" : "EAP",
133 eap_id);
134 }
135
136 ike_proposal = ike_sa->get_proposal(ike_sa);
137
138 fprintf(out, "%12s[%d]: %N SPIs: %.16"PRIx64"_i%s %.16"PRIx64"_r%s",
139 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
140 ike_version_names, ike_sa->get_version(ike_sa),
141 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
142 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
143
144
145 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
146 {
147 time_t rekey, reauth;
148 peer_cfg_t *peer_cfg;
149
150 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY);
151 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
152 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
153
154 if (rekey)
155 {
156 fprintf(out, ", rekeying in %V", &rekey, &now);
157 }
158 if (reauth)
159 {
160 bool first = TRUE;
161 enumerator_t *enumerator;
162 auth_cfg_t *auth;
163
164 fprintf(out, ", ");
165 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, TRUE);
166 while (enumerator->enumerate(enumerator, &auth))
167 {
168 if (!first)
169 {
170 fprintf(out, "+");
171 }
172 first = FALSE;
173 fprintf(out, "%N", auth_class_names,
174 auth->get(auth, AUTH_RULE_AUTH_CLASS));
175 }
176 enumerator->destroy(enumerator);
177 fprintf(out, " reauthentication in %V", &reauth, &now);
178 }
179 if (!rekey && !reauth)
180 {
181 fprintf(out, ", rekeying disabled");
182 }
183 }
184 fprintf(out, "\n");
185
186 if (ike_proposal)
187 {
188 char buf[BUF_LEN];
189
190 snprintf(buf, BUF_LEN, "%P", ike_proposal);
191 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
192 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
193 buf+4);
194 }
195
196 log_task_q(out, ike_sa, TASK_QUEUE_QUEUED, "queued");
197 log_task_q(out, ike_sa, TASK_QUEUE_ACTIVE, "active");
198 log_task_q(out, ike_sa, TASK_QUEUE_PASSIVE, "passive");
199 }
200 }
201
202 /**
203 * log an CHILD_SA to out
204 */
205 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
206 {
207 time_t use_in, use_out, rekey, now;
208 u_int64_t bytes_in, bytes_out;
209 proposal_t *proposal;
210 child_cfg_t *config = child_sa->get_config(child_sa);
211
212 now = time_monotonic(NULL);
213
214 fprintf(out, "%12s{%d}: %N, %N%s",
215 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
216 child_sa_state_names, child_sa->get_state(child_sa),
217 ipsec_mode_names, child_sa->get_mode(child_sa),
218 config->use_proxy_mode(config) ? "_PROXY" : "");
219
220 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
221 {
222 fprintf(out, ", %N%s SPIs: %.8x_i %.8x_o",
223 protocol_id_names, child_sa->get_protocol(child_sa),
224 child_sa->has_encap(child_sa) ? " in UDP" : "",
225 ntohl(child_sa->get_spi(child_sa, TRUE)),
226 ntohl(child_sa->get_spi(child_sa, FALSE)));
227
228 if (child_sa->get_ipcomp(child_sa) != IPCOMP_NONE)
229 {
230 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
231 ntohs(child_sa->get_cpi(child_sa, TRUE)),
232 ntohs(child_sa->get_cpi(child_sa, FALSE)));
233 }
234
235 if (all)
236 {
237 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
238 child_sa->get_reqid(child_sa));
239
240 proposal = child_sa->get_proposal(child_sa);
241 if (proposal)
242 {
243 u_int16_t encr_alg = ENCR_UNDEFINED, int_alg = AUTH_UNDEFINED;
244 u_int16_t encr_size = 0, int_size = 0;
245 u_int16_t esn = NO_EXT_SEQ_NUMBERS;
246
247 proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
248 &encr_alg, &encr_size);
249 proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
250 &int_alg, &int_size);
251 proposal->get_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
252 &esn, NULL);
253
254 if (encr_alg != ENCR_UNDEFINED)
255 {
256 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
257 if (encr_size)
258 {
259 fprintf(out, "_%u", encr_size);
260 }
261 }
262 if (int_alg != AUTH_UNDEFINED)
263 {
264 fprintf(out, "/%N", integrity_algorithm_names, int_alg);
265 if (int_size)
266 {
267 fprintf(out, "_%u", int_size);
268 }
269 }
270 if (esn == EXT_SEQ_NUMBERS)
271 {
272 fprintf(out, "/ESN");
273 }
274 }
275
276 child_sa->get_usestats(child_sa, TRUE, &use_in, &bytes_in);
277 fprintf(out, ", %" PRIu64 " bytes_i", bytes_in);
278 if (use_in)
279 {
280 fprintf(out, " (%" PRIu64 "s ago)", (u_int64_t)(now - use_in));
281 }
282
283 child_sa->get_usestats(child_sa, FALSE, &use_out, &bytes_out);
284 fprintf(out, ", %" PRIu64 " bytes_o", bytes_out);
285 if (use_out)
286 {
287 fprintf(out, " (%" PRIu64 "s ago)", (u_int64_t)(now - use_out));
288 }
289 fprintf(out, ", rekeying ");
290
291 rekey = child_sa->get_lifetime(child_sa, FALSE);
292 if (rekey)
293 {
294 if (now > rekey)
295 {
296 fprintf(out, "active");
297 }
298 else
299 {
300 fprintf(out, "in %V", &now, &rekey);
301 }
302 }
303 else
304 {
305 fprintf(out, "disabled");
306 }
307
308 }
309 }
310 else if (child_sa->get_state(child_sa) == CHILD_REKEYING)
311 {
312 rekey = child_sa->get_lifetime(child_sa, TRUE);
313 fprintf(out, ", expires in %V", &now, &rekey);
314 }
315
316 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
317 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
318 child_sa->get_traffic_selectors(child_sa, TRUE),
319 child_sa->get_traffic_selectors(child_sa, FALSE));
320 }
321
322 /**
323 * Log a configs local or remote authentication config to out
324 */
325 static void log_auth_cfgs(FILE *out, peer_cfg_t *peer_cfg, bool local)
326 {
327 enumerator_t *enumerator, *rules;
328 auth_rule_t rule;
329 auth_cfg_t *auth;
330 auth_class_t auth_class;
331 identification_t *id;
332 certificate_t *cert;
333 cert_validation_t valid;
334 char *name;
335
336 name = peer_cfg->get_name(peer_cfg);
337
338 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
339 while (enumerator->enumerate(enumerator, &auth))
340 {
341 fprintf(out, "%12s: %s [%Y] uses ", name, local ? "local: " : "remote:",
342 auth->get(auth, AUTH_RULE_IDENTITY));
343
344 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
345 if (auth_class == AUTH_CLASS_EAP)
346 {
347 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
348 {
349 fprintf(out, "EAP authentication");
350 }
351 else
352 {
353 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
354 {
355 fprintf(out, "EAP_%" PRIuPTR "-%" PRIuPTR " authentication",
356 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
357 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
358 }
359 else
360 {
361 fprintf(out, "%N authentication", eap_type_names,
362 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
363 }
364 }
365 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
366 if (id)
367 {
368 fprintf(out, " with EAP identity '%Y'", id);
369 }
370 fprintf(out, "\n");
371 }
372 else if (auth_class == AUTH_CLASS_XAUTH)
373 {
374 fprintf(out, "%N authentication: %s", auth_class_names, auth_class,
375 auth->get(auth, AUTH_RULE_XAUTH_BACKEND) ?: "any");
376 id = auth->get(auth, AUTH_RULE_XAUTH_IDENTITY);
377 if (id)
378 {
379 fprintf(out, " with XAuth identity '%Y'", id);
380 }
381 fprintf(out, "\n");
382 }
383 else
384 {
385 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
386 }
387
388 cert = auth->get(auth, AUTH_RULE_CA_CERT);
389 if (cert)
390 {
391 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
392 }
393
394 cert = auth->get(auth, AUTH_RULE_IM_CERT);
395 if (cert)
396 {
397 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
398 }
399
400 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
401 if (cert)
402 {
403 fprintf(out, "%12s: cert: \"%Y\"\n", name,
404 cert->get_subject(cert));
405 }
406
407 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
408 if (valid != VALIDATION_FAILED)
409 {
410 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
411 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
412 }
413
414 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
415 if (valid != VALIDATION_FAILED)
416 {
417 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
418 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
419 }
420
421 rules = auth->create_enumerator(auth);
422 while (rules->enumerate(rules, &rule, &id))
423 {
424 if (rule == AUTH_RULE_GROUP)
425 {
426 fprintf(out, "%12s: group: %Y\n", name, id);
427 }
428 }
429 rules->destroy(rules);
430 }
431 enumerator->destroy(enumerator);
432 }
433
434 METHOD(stroke_list_t, status, void,
435 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out,
436 bool all, bool wait)
437 {
438 enumerator_t *enumerator, *children;
439 ike_cfg_t *ike_cfg;
440 child_cfg_t *child_cfg;
441 child_sa_t *child_sa;
442 ike_sa_t *ike_sa;
443 linked_list_t *my_ts, *other_ts;
444 bool first, found = FALSE;
445 char *name = msg->status.name;
446 u_int half_open;
447
448 if (all)
449 {
450 peer_cfg_t *peer_cfg;
451 ike_version_t ike_version;
452 char *pool;
453 host_t *host;
454 u_int32_t dpd;
455 time_t since, now;
456 u_int size, online, offline, i;
457 struct utsname utsname;
458
459 now = time_monotonic(NULL);
460 since = time(NULL) - (now - this->uptime);
461
462 fprintf(out, "Status of IKE charon daemon (%sSwan "VERSION, this->swan);
463 if (uname(&utsname) == 0)
464 {
465 fprintf(out, ", %s %s, %s",
466 utsname.sysname, utsname.release, utsname.machine);
467 }
468 fprintf(out, "):\n uptime: %V, since %T\n", &now, &this->uptime, &since,
469 FALSE);
470 #ifdef HAVE_MALLINFO
471 {
472 struct mallinfo mi = mallinfo();
473
474 fprintf(out, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
475 mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
476 }
477 #endif /* HAVE_MALLINFO */
478 fprintf(out, " worker threads: %d of %d idle, ",
479 lib->processor->get_idle_threads(lib->processor),
480 lib->processor->get_total_threads(lib->processor));
481 for (i = 0; i < JOB_PRIO_MAX; i++)
482 {
483 fprintf(out, "%s%d", i == 0 ? "" : "/",
484 lib->processor->get_working_threads(lib->processor, i));
485 }
486 fprintf(out, " working, job queue: ");
487 for (i = 0; i < JOB_PRIO_MAX; i++)
488 {
489 fprintf(out, "%s%d", i == 0 ? "" : "/",
490 lib->processor->get_job_load(lib->processor, i));
491 }
492 fprintf(out, ", scheduled: %d\n",
493 lib->scheduler->get_job_load(lib->scheduler));
494 fprintf(out, " loaded plugins: %s\n",
495 lib->plugins->loaded_plugins(lib->plugins));
496
497 first = TRUE;
498 enumerator = this->attribute->create_pool_enumerator(this->attribute);
499 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
500 {
501 if (name && !streq(name, pool))
502 {
503 continue;
504 }
505 if (first)
506 {
507 first = FALSE;
508 fprintf(out, "Virtual IP pools (size/online/offline):\n");
509 }
510 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
511 }
512 enumerator->destroy(enumerator);
513
514 enumerator = hydra->kernel_interface->create_address_enumerator(
515 hydra->kernel_interface, FALSE, FALSE);
516 fprintf(out, "Listening IP addresses:\n");
517 while (enumerator->enumerate(enumerator, (void**)&host))
518 {
519 fprintf(out, " %H\n", host);
520 }
521 enumerator->destroy(enumerator);
522
523 fprintf(out, "Connections:\n");
524 enumerator = charon->backends->create_peer_cfg_enumerator(
525 charon->backends, NULL, NULL, NULL, NULL, IKE_ANY);
526 while (enumerator->enumerate(enumerator, &peer_cfg))
527 {
528 char *my_addr, *other_addr;
529 bool my_allow_any, other_allow_any;
530
531 if (name && !streq(name, peer_cfg->get_name(peer_cfg)))
532 {
533 continue;
534 }
535
536 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
537 ike_version = peer_cfg->get_ike_version(peer_cfg);
538 my_addr = ike_cfg->get_my_addr(ike_cfg, &my_allow_any);
539 other_addr = ike_cfg->get_other_addr(ike_cfg, &other_allow_any);
540 fprintf(out, "%12s: %s%s...%s%s %N", peer_cfg->get_name(peer_cfg),
541 my_allow_any ? "%":"", my_addr,
542 other_allow_any ? "%":"", other_addr,
543 ike_version_names, ike_version);
544
545 if (ike_version == IKEV1 && peer_cfg->use_aggressive(peer_cfg))
546 {
547 fprintf(out, " Aggressive");
548 }
549
550 dpd = peer_cfg->get_dpd(peer_cfg);
551 if (dpd)
552 {
553 fprintf(out, ", dpddelay=%us", dpd);
554 }
555 fprintf(out, "\n");
556
557 log_auth_cfgs(out, peer_cfg, TRUE);
558 log_auth_cfgs(out, peer_cfg, FALSE);
559
560 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
561 while (children->enumerate(children, &child_cfg))
562 {
563 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
564 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
565 fprintf(out, "%12s: child: %#R=== %#R%N",
566 child_cfg->get_name(child_cfg), my_ts, other_ts,
567 ipsec_mode_names, child_cfg->get_mode(child_cfg));
568 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
569 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
570
571 if (dpd)
572 {
573 fprintf(out, ", dpdaction=%N", action_names,
574 child_cfg->get_dpd_action(child_cfg));
575 }
576 fprintf(out, "\n");
577 }
578 children->destroy(children);
579 }
580 enumerator->destroy(enumerator);
581 }
582
583 /* Enumerate shunt policies */
584 first = TRUE;
585 enumerator = charon->shunts->create_enumerator(charon->shunts);
586 while (enumerator->enumerate(enumerator, &child_cfg))
587 {
588 if (name && !streq(name, child_cfg->get_name(child_cfg)))
589 {
590 continue;
591 }
592 if (first)
593 {
594 fprintf(out, "Shunted Connections:\n");
595 first = FALSE;
596 }
597 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
598 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
599 fprintf(out, "%12s: %#R=== %#R%N\n",
600 child_cfg->get_name(child_cfg), my_ts, other_ts,
601 ipsec_mode_names, child_cfg->get_mode(child_cfg));
602 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
603 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
604 }
605 enumerator->destroy(enumerator);
606
607 /* Enumerate traps */
608 first = TRUE;
609 enumerator = charon->traps->create_enumerator(charon->traps);
610 while (enumerator->enumerate(enumerator, NULL, &child_sa))
611 {
612 if (name && !streq(name, child_sa->get_name(child_sa)))
613 {
614 continue;
615 }
616 if (first)
617 {
618 fprintf(out, "Routed Connections:\n");
619 first = FALSE;
620 }
621 log_child_sa(out, child_sa, all);
622 }
623 enumerator->destroy(enumerator);
624
625 half_open = charon->ike_sa_manager->get_half_open_count(
626 charon->ike_sa_manager, NULL);
627 fprintf(out, "Security Associations (%u up, %u connecting):\n",
628 charon->ike_sa_manager->get_count(charon->ike_sa_manager) - half_open,
629 half_open);
630 enumerator = charon->controller->create_ike_sa_enumerator(
631 charon->controller, wait);
632 while (enumerator->enumerate(enumerator, &ike_sa))
633 {
634 bool ike_printed = FALSE;
635 enumerator_t *children = ike_sa->create_child_sa_enumerator(ike_sa);
636
637 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
638 {
639 log_ike_sa(out, ike_sa, all);
640 found = TRUE;
641 ike_printed = TRUE;
642 }
643
644 while (children->enumerate(children, (void**)&child_sa))
645 {
646 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
647 {
648 if (!ike_printed)
649 {
650 log_ike_sa(out, ike_sa, all);
651 found = TRUE;
652 ike_printed = TRUE;
653 }
654 log_child_sa(out, child_sa, all);
655 }
656 }
657 children->destroy(children);
658 }
659 enumerator->destroy(enumerator);
660
661 if (!found)
662 {
663 if (name)
664 {
665 fprintf(out, " no match\n");
666 }
667 else
668 {
669 fprintf(out, " none\n");
670 }
671 }
672 }
673
674 /**
675 * create a unique certificate list without duplicates
676 * certicates having the same issuer are grouped together.
677 */
678 static linked_list_t* create_unique_cert_list(certificate_type_t type)
679 {
680 linked_list_t *list = linked_list_create();
681 enumerator_t *enumerator = lib->credmgr->create_cert_enumerator(
682 lib->credmgr, type, KEY_ANY, NULL, FALSE);
683 certificate_t *cert;
684
685 while (enumerator->enumerate(enumerator, (void**)&cert))
686 {
687 enumerator_t *added = list->create_enumerator(list);
688 identification_t *issuer = cert->get_issuer(cert);
689 bool previous_same, same = FALSE, found = FALSE;
690 certificate_t *list_cert;
691
692 while (added->enumerate(added, (void**)&list_cert))
693 {
694 if (list_cert->equals(list_cert, cert))
695 { /* stop if we found a duplicate*/
696 found = TRUE;
697 break;
698 }
699 previous_same = same;
700 same = list_cert->has_issuer(list_cert, issuer);
701 if (previous_same && !same)
702 { /* group certificates with same issuer */
703 break;
704 }
705 }
706 if (!found)
707 {
708 list->insert_before(list, added, cert->get_ref(cert));
709 }
710 added->destroy(added);
711 }
712 enumerator->destroy(enumerator);
713 return list;
714 }
715
716 /**
717 * Print a single public key.
718 */
719 static void list_public_key(public_key_t *public, FILE *out)
720 {
721 private_key_t *private = NULL;
722 chunk_t keyid;
723 identification_t *id;
724
725 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
726 {
727 id = identification_create_from_encoding(ID_KEY_ID, keyid);
728 private = lib->credmgr->get_private(lib->credmgr,
729 public->get_type(public), id, NULL);
730 id->destroy(id);
731 }
732
733 fprintf(out, " pubkey: %N %d bits%s\n",
734 key_type_names, public->get_type(public),
735 public->get_keysize(public),
736 private ? ", has private key" : "");
737 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid))
738 {
739 fprintf(out, " keyid: %#B\n", &keyid);
740 }
741 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
742 {
743 fprintf(out, " subjkey: %#B\n", &keyid);
744 }
745 DESTROY_IF(private);
746 }
747
748 /**
749 * list all raw public keys
750 */
751 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
752 {
753 bool first = TRUE;
754 time_t now = time(NULL), notBefore, notAfter;
755 enumerator_t *enumerator;
756 certificate_t *cert;
757
758 enumerator = list->create_enumerator(list);
759 while (enumerator->enumerate(enumerator, (void**)&cert))
760 {
761 identification_t *subject = cert->get_subject(cert);
762 public_key_t *public = cert->get_public_key(cert);
763
764 if (public)
765 {
766 if (first)
767 {
768 fprintf(out, "\n");
769 fprintf(out, "List of Raw Public Keys:\n");
770 first = FALSE;
771 }
772 fprintf(out, "\n");
773
774 /* list subject if available */
775 if (subject->get_type(subject) != ID_KEY_ID)
776 {
777 fprintf(out, " subject: %#Y\n", subject);
778 }
779
780 /* list validity if available*/
781 cert->get_validity(cert, &now, &notBefore, &notAfter);
782 if (notBefore != UNDEFINED_TIME && notAfter != UNDEFINED_TIME)
783 {
784 fprintf(out, " validity: not before %T, ", &notBefore, utc);
785 if (now < notBefore)
786 {
787 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
788 }
789 else
790 {
791 fprintf(out, "ok\n");
792 }
793 fprintf(out, " not after %T, ", &notAfter, utc);
794 if (now > notAfter)
795 {
796 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
797 }
798 else
799 {
800 fprintf(out, "ok");
801 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
802 {
803 fprintf(out, " (expires in %V)", &now, &notAfter);
804 }
805 fprintf(out, " \n");
806 }
807 }
808
809 list_public_key(public, out);
810 public->destroy(public);
811 }
812 }
813 enumerator->destroy(enumerator);
814 }
815
816 /**
817 * list OpenPGP certificates
818 */
819 static void stroke_list_pgp(linked_list_t *list,bool utc, FILE *out)
820 {
821 bool first = TRUE;
822 time_t now = time(NULL);
823 enumerator_t *enumerator = list->create_enumerator(list);
824 certificate_t *cert;
825
826 while (enumerator->enumerate(enumerator, (void**)&cert))
827 {
828 time_t created, until;
829 public_key_t *public;
830 pgp_certificate_t *pgp_cert = (pgp_certificate_t*)cert;
831 chunk_t fingerprint = pgp_cert->get_fingerprint(pgp_cert);
832
833 if (first)
834 {
835
836 fprintf(out, "\n");
837 fprintf(out, "List of PGP End Entity Certificates:\n");
838 first = FALSE;
839 }
840 fprintf(out, "\n");
841 fprintf(out, " userid: '%Y'\n", cert->get_subject(cert));
842
843 fprintf(out, " digest: %#B\n", &fingerprint);
844
845 /* list validity */
846 cert->get_validity(cert, &now, &created, &until);
847 fprintf(out, " created: %T\n", &created, utc);
848 fprintf(out, " until: %T%s\n", &until, utc,
849 (until == TIME_32_BIT_SIGNED_MAX) ? " (expires never)":"");
850
851 public = cert->get_public_key(cert);
852 if (public)
853 {
854 list_public_key(public, out);
855 public->destroy(public);
856 }
857 }
858 enumerator->destroy(enumerator);
859 }
860
861 /**
862 * list all X.509 certificates matching the flags
863 */
864 static void stroke_list_certs(linked_list_t *list, char *label,
865 x509_flag_t flags, bool utc, FILE *out)
866 {
867 bool first = TRUE;
868 time_t now = time(NULL);
869 enumerator_t *enumerator;
870 certificate_t *cert;
871 x509_flag_t flag_mask;
872
873 /* mask all auxiliary flags */
874 flag_mask = ~(X509_SERVER_AUTH | X509_CLIENT_AUTH | X509_IKE_INTERMEDIATE |
875 X509_SELF_SIGNED | X509_IP_ADDR_BLOCKS);
876
877 enumerator = list->create_enumerator(list);
878 while (enumerator->enumerate(enumerator, (void**)&cert))
879 {
880 x509_t *x509 = (x509_t*)cert;
881 x509_flag_t x509_flags = x509->get_flags(x509) & flag_mask;
882
883 /* list only if flag is set or flag == 0 */
884 if ((x509_flags & flags) || (x509_flags == flags))
885 {
886 enumerator_t *enumerator;
887 identification_t *altName;
888 bool first_altName = TRUE;
889 u_int pathlen;
890 chunk_t serial, authkey;
891 time_t notBefore, notAfter;
892 public_key_t *public;
893
894 if (first)
895 {
896 fprintf(out, "\n");
897 fprintf(out, "List of %s:\n", label);
898 first = FALSE;
899 }
900 fprintf(out, "\n");
901
902 /* list subjectAltNames */
903 enumerator = x509->create_subjectAltName_enumerator(x509);
904 while (enumerator->enumerate(enumerator, (void**)&altName))
905 {
906 if (first_altName)
907 {
908 fprintf(out, " altNames: ");
909 first_altName = FALSE;
910 }
911 else
912 {
913 fprintf(out, ", ");
914 }
915 fprintf(out, "%Y", altName);
916 }
917 if (!first_altName)
918 {
919 fprintf(out, "\n");
920 }
921 enumerator->destroy(enumerator);
922
923 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
924 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
925 serial = chunk_skip_zero(x509->get_serial(x509));
926 fprintf(out, " serial: %#B\n", &serial);
927
928 /* list validity */
929 cert->get_validity(cert, &now, &notBefore, &notAfter);
930 fprintf(out, " validity: not before %T, ", &notBefore, utc);
931 if (now < notBefore)
932 {
933 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
934 }
935 else
936 {
937 fprintf(out, "ok\n");
938 }
939 fprintf(out, " not after %T, ", &notAfter, utc);
940 if (now > notAfter)
941 {
942 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
943 }
944 else
945 {
946 fprintf(out, "ok");
947 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
948 {
949 fprintf(out, " (expires in %V)", &now, &notAfter);
950 }
951 fprintf(out, " \n");
952 }
953
954 public = cert->get_public_key(cert);
955 if (public)
956 {
957 list_public_key(public, out);
958 public->destroy(public);
959 }
960
961 /* list optional authorityKeyIdentifier */
962 authkey = x509->get_authKeyIdentifier(x509);
963 if (authkey.ptr)
964 {
965 fprintf(out, " authkey: %#B\n", &authkey);
966 }
967
968 /* list optional pathLenConstraint */
969 pathlen = x509->get_constraint(x509, X509_PATH_LEN);
970 if (pathlen != X509_NO_CONSTRAINT)
971 {
972 fprintf(out, " pathlen: %u\n", pathlen);
973 }
974
975 /* list optional ipAddrBlocks */
976 if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
977 {
978 traffic_selector_t *ipAddrBlock;
979 bool first_ipAddrBlock = TRUE;
980
981 fprintf(out, " addresses: ");
982 enumerator = x509->create_ipAddrBlock_enumerator(x509);
983 while (enumerator->enumerate(enumerator, &ipAddrBlock))
984 {
985 if (first_ipAddrBlock)
986 {
987 first_ipAddrBlock = FALSE;
988 }
989 else
990 {
991 fprintf(out, ", ");
992 }
993 fprintf(out, "%R", ipAddrBlock);
994 }
995 enumerator->destroy(enumerator);
996 fprintf(out, "\n");
997 }
998 }
999 }
1000 enumerator->destroy(enumerator);
1001 }
1002
1003 /**
1004 * list all X.509 attribute certificates
1005 */
1006 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
1007 {
1008 bool first = TRUE;
1009 time_t thisUpdate, nextUpdate, now = time(NULL);
1010 enumerator_t *enumerator = list->create_enumerator(list);
1011 certificate_t *cert;
1012
1013 while (enumerator->enumerate(enumerator, (void**)&cert))
1014 {
1015 ac_t *ac = (ac_t*)cert;
1016 identification_t *id;
1017 ietf_attributes_t *groups;
1018 chunk_t chunk;
1019
1020 if (first)
1021 {
1022 fprintf(out, "\n");
1023 fprintf(out, "List of X.509 Attribute Certificates:\n");
1024 first = FALSE;
1025 }
1026 fprintf(out, "\n");
1027
1028 id = cert->get_subject(cert);
1029 if (id)
1030 {
1031 fprintf(out, " holder: \"%Y\"\n", id);
1032 }
1033 id = ac->get_holderIssuer(ac);
1034 if (id)
1035 {
1036 fprintf(out, " hissuer: \"%Y\"\n", id);
1037 }
1038 chunk = chunk_skip_zero(ac->get_holderSerial(ac));
1039 if (chunk.ptr)
1040 {
1041 fprintf(out, " hserial: %#B\n", &chunk);
1042 }
1043 groups = ac->get_groups(ac);
1044 if (groups)
1045 {
1046 fprintf(out, " groups: %s\n", groups->get_string(groups));
1047 groups->destroy(groups);
1048 }
1049 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
1050 chunk = chunk_skip_zero(ac->get_serial(ac));
1051 fprintf(out, " serial: %#B\n", &chunk);
1052
1053 /* list validity */
1054 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
1055 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
1056 fprintf(out, " next %T, ", &nextUpdate, utc);
1057 if (now > nextUpdate)
1058 {
1059 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1060 }
1061 else
1062 {
1063 fprintf(out, "ok");
1064 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
1065 {
1066 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1067 }
1068 fprintf(out, " \n");
1069 }
1070
1071 /* list optional authorityKeyIdentifier */
1072 chunk = ac->get_authKeyIdentifier(ac);
1073 if (chunk.ptr)
1074 {
1075 fprintf(out, " authkey: %#B\n", &chunk);
1076 }
1077 }
1078 enumerator->destroy(enumerator);
1079 }
1080
1081 /**
1082 * list all X.509 CRLs
1083 */
1084 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
1085 {
1086 bool first = TRUE;
1087 time_t thisUpdate, nextUpdate, now = time(NULL);
1088 enumerator_t *enumerator = list->create_enumerator(list);
1089 certificate_t *cert;
1090
1091 while (enumerator->enumerate(enumerator, (void**)&cert))
1092 {
1093 crl_t *crl = (crl_t*)cert;
1094 chunk_t chunk;
1095
1096 if (first)
1097 {
1098 fprintf(out, "\n");
1099 fprintf(out, "List of X.509 CRLs:\n");
1100 first = FALSE;
1101 }
1102 fprintf(out, "\n");
1103
1104 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
1105
1106 /* list optional crlNumber */
1107 chunk = chunk_skip_zero(crl->get_serial(crl));
1108 if (chunk.ptr)
1109 {
1110 fprintf(out, " serial: %#B\n", &chunk);
1111 }
1112 if (crl->is_delta_crl(crl, &chunk))
1113 {
1114 chunk = chunk_skip_zero(chunk);
1115 fprintf(out, " delta for: %#B\n", &chunk);
1116 }
1117
1118 /* count the number of revoked certificates */
1119 {
1120 int count = 0;
1121 enumerator_t *enumerator = crl->create_enumerator(crl);
1122
1123 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
1124 {
1125 count++;
1126 }
1127 fprintf(out, " revoked: %d certificate%s\n", count,
1128 (count == 1)? "" : "s");
1129 enumerator->destroy(enumerator);
1130 }
1131
1132 /* list validity */
1133 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
1134 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
1135 fprintf(out, " next %T, ", &nextUpdate, utc);
1136 if (now > nextUpdate)
1137 {
1138 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1139 }
1140 else
1141 {
1142 fprintf(out, "ok");
1143 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
1144 {
1145 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1146 }
1147 fprintf(out, " \n");
1148 }
1149
1150 /* list optional authorityKeyIdentifier */
1151 chunk = crl->get_authKeyIdentifier(crl);
1152 if (chunk.ptr)
1153 {
1154 fprintf(out, " authkey: %#B\n", &chunk);
1155 }
1156 }
1157 enumerator->destroy(enumerator);
1158 }
1159
1160 /**
1161 * list all OCSP responses
1162 */
1163 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
1164 {
1165 bool first = TRUE, ok;
1166 enumerator_t *enumerator = list->create_enumerator(list);
1167 certificate_t *cert;
1168 time_t produced, usable, now = time(NULL);
1169
1170 while (enumerator->enumerate(enumerator, (void**)&cert))
1171 {
1172 if (first)
1173 {
1174 fprintf(out, "\n");
1175 fprintf(out, "List of OCSP responses:\n");
1176 fprintf(out, "\n");
1177 first = FALSE;
1178 }
1179 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
1180
1181 /* check validity */
1182 ok = cert->get_validity(cert, &now, &produced, &usable);
1183 fprintf(out, " validity: produced at %T\n", &produced, utc);
1184 fprintf(out, " usable till %T, ", &usable, utc);
1185 if (ok)
1186 {
1187 fprintf(out, "ok\n");
1188 }
1189 else
1190 {
1191 fprintf(out, "expired (%V ago)\n", &now, &usable);
1192 }
1193 }
1194 enumerator->destroy(enumerator);
1195 }
1196
1197 /**
1198 * Print the name of an algorithm plus the name of the plugin that registered it
1199 */
1200 static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
1201 const char *plugin_name)
1202 {
1203 char alg_name[BUF_LEN];
1204 int alg_name_len;
1205
1206 if (alg_names)
1207 {
1208 alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type,
1209 plugin_name);
1210 }
1211 else
1212 {
1213 alg_name_len = sprintf(alg_name, " [%s]", plugin_name);
1214 }
1215 if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
1216 {
1217 fprintf(out, "\n ");
1218 *len = 13;
1219 }
1220 fprintf(out, "%s", alg_name);
1221 *len += alg_name_len;
1222 }
1223
1224 /**
1225 * List of registered cryptographical algorithms
1226 */
1227 static void list_algs(FILE *out)
1228 {
1229 enumerator_t *enumerator;
1230 encryption_algorithm_t encryption;
1231 integrity_algorithm_t integrity;
1232 hash_algorithm_t hash;
1233 pseudo_random_function_t prf;
1234 diffie_hellman_group_t group;
1235 rng_quality_t quality;
1236 const char *plugin_name;
1237 int len;
1238
1239 fprintf(out, "\n");
1240 fprintf(out, "List of registered IKEv2 Algorithms:\n");
1241 fprintf(out, "\n encryption:");
1242 len = 13;
1243 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1244 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1245 {
1246 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1247 }
1248 enumerator->destroy(enumerator);
1249 fprintf(out, "\n integrity: ");
1250 len = 13;
1251 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1252 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
1253 {
1254 print_alg(out, &len, integrity_algorithm_names, integrity, plugin_name);
1255 }
1256 enumerator->destroy(enumerator);
1257 fprintf(out, "\n aead: ");
1258 len = 13;
1259 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
1260 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1261 {
1262 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1263 }
1264 enumerator->destroy(enumerator);
1265 fprintf(out, "\n hasher: ");
1266 len = 13;
1267 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1268 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
1269 {
1270 print_alg(out, &len, hash_algorithm_names, hash, plugin_name);
1271 }
1272 enumerator->destroy(enumerator);
1273 fprintf(out, "\n prf: ");
1274 len = 13;
1275 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1276 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
1277 {
1278 print_alg(out, &len, pseudo_random_function_names, prf, plugin_name);
1279 }
1280 enumerator->destroy(enumerator);
1281 fprintf(out, "\n dh-group: ");
1282 len = 13;
1283 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1284 while (enumerator->enumerate(enumerator, &group, &plugin_name))
1285 {
1286 print_alg(out, &len, diffie_hellman_group_names, group, plugin_name);
1287 }
1288 enumerator->destroy(enumerator);
1289 fprintf(out, "\n random-gen:");
1290 len = 13;
1291 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
1292 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
1293 {
1294 print_alg(out, &len, rng_quality_names, quality, plugin_name);
1295 }
1296 enumerator->destroy(enumerator);
1297 fprintf(out, "\n nonce-gen: ");
1298 len = 13;
1299 enumerator = lib->crypto->create_nonce_gen_enumerator(lib->crypto);
1300 while (enumerator->enumerate(enumerator, &plugin_name))
1301 {
1302 print_alg(out, &len, NULL, 0, plugin_name);
1303 }
1304 enumerator->destroy(enumerator);
1305 fprintf(out, "\n");
1306 }
1307
1308 /**
1309 * List loaded plugin information
1310 */
1311 static void list_plugins(FILE *out)
1312 {
1313 plugin_feature_t *features, *fp;
1314 enumerator_t *enumerator;
1315 linked_list_t *list;
1316 plugin_t *plugin;
1317 int count, i;
1318 bool loaded;
1319 char *str;
1320
1321 fprintf(out, "\n");
1322 fprintf(out, "List of loaded Plugins:\n");
1323 fprintf(out, "\n");
1324
1325 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
1326 while (enumerator->enumerate(enumerator, &plugin, &list))
1327 {
1328 fprintf(out, "%s:\n", plugin->get_name(plugin));
1329 if (plugin->get_features)
1330 {
1331 count = plugin->get_features(plugin, &features);
1332 for (i = 0; i < count; i++)
1333 {
1334 str = plugin_feature_get_string(&features[i]);
1335 switch (features[i].kind)
1336 {
1337 case FEATURE_PROVIDE:
1338 fp = &features[i];
1339 loaded = list->find_first(list, NULL,
1340 (void**)&fp) == SUCCESS;
1341 fprintf(out, " %s%s\n",
1342 str, loaded ? "" : " (not loaded)");
1343 break;
1344 case FEATURE_DEPENDS:
1345 fprintf(out, " %s\n", str);
1346 break;
1347 case FEATURE_SDEPEND:
1348 fprintf(out, " %s (soft)\n", str);
1349 break;
1350 default:
1351 break;
1352 }
1353 free(str);
1354 }
1355 }
1356 }
1357 enumerator->destroy(enumerator);
1358 }
1359
1360 METHOD(stroke_list_t, list, void,
1361 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1362 {
1363 linked_list_t *cert_list = NULL;
1364
1365 if (msg->list.flags & LIST_PUBKEYS)
1366 {
1367 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
1368
1369 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
1370 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
1371 }
1372 if (msg->list.flags & LIST_CERTS)
1373 {
1374 linked_list_t *pgp_list = create_unique_cert_list(CERT_GPG);
1375
1376 stroke_list_pgp(pgp_list, msg->list.utc, out);
1377 pgp_list->destroy_offset(pgp_list, offsetof(certificate_t, destroy));
1378 }
1379 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
1380 {
1381 cert_list = create_unique_cert_list(CERT_X509);
1382 }
1383 if (msg->list.flags & LIST_CERTS)
1384 {
1385 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
1386 X509_NONE, msg->list.utc, out);
1387 }
1388 if (msg->list.flags & LIST_CACERTS)
1389 {
1390 stroke_list_certs(cert_list, "X.509 CA Certificates",
1391 X509_CA, msg->list.utc, out);
1392 }
1393 if (msg->list.flags & LIST_OCSPCERTS)
1394 {
1395 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
1396 X509_OCSP_SIGNER, msg->list.utc, out);
1397 }
1398 if (msg->list.flags & LIST_AACERTS)
1399 {
1400 stroke_list_certs(cert_list, "X.509 AA Certificates",
1401 X509_AA, msg->list.utc, out);
1402 }
1403 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1404
1405 if (msg->list.flags & LIST_ACERTS)
1406 {
1407 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
1408
1409 stroke_list_acerts(ac_list, msg->list.utc, out);
1410 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
1411 }
1412 if (msg->list.flags & LIST_CRLS)
1413 {
1414 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1415
1416 stroke_list_crls(crl_list, msg->list.utc, out);
1417 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1418 }
1419 if (msg->list.flags & LIST_OCSP)
1420 {
1421 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1422
1423 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1424
1425 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1426 }
1427 if (msg->list.flags & LIST_ALGS)
1428 {
1429 list_algs(out);
1430 }
1431 if (msg->list.flags & LIST_PLUGINS)
1432 {
1433 list_plugins(out);
1434 }
1435 }
1436
1437 /**
1438 * Print leases of a single pool
1439 */
1440 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1441 host_t *address, u_int size, u_int online, u_int offline)
1442 {
1443 enumerator_t *enumerator;
1444 identification_t *id;
1445 host_t *lease;
1446 bool on;
1447 int found = 0;
1448
1449 fprintf(out, "Leases in pool '%s', usage: %u/%u, %u online\n",
1450 pool, online + offline, size, online);
1451 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1452 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1453 {
1454 if (!address || address->ip_equals(address, lease))
1455 {
1456 fprintf(out, " %15H %s '%Y'\n",
1457 lease, on ? "online" : "offline", id);
1458 found++;
1459 }
1460 }
1461 enumerator->destroy(enumerator);
1462 if (!found)
1463 {
1464 fprintf(out, " no matching leases found\n");
1465 }
1466 }
1467
1468 METHOD(stroke_list_t, leases, void,
1469 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1470 {
1471 enumerator_t *enumerator;
1472 u_int size, offline, online;
1473 host_t *address = NULL;
1474 char *pool;
1475 int found = 0;
1476
1477 if (msg->leases.address)
1478 {
1479 address = host_create_from_string(msg->leases.address, 0);
1480 }
1481
1482 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1483 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1484 {
1485 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1486 {
1487 pool_leases(this, out, pool, address, size, online, offline);
1488 found++;
1489 }
1490 }
1491 enumerator->destroy(enumerator);
1492 if (!found)
1493 {
1494 if (msg->leases.pool)
1495 {
1496 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1497 }
1498 else
1499 {
1500 fprintf(out, "no pools found\n");
1501 }
1502 }
1503 DESTROY_IF(address);
1504 }
1505
1506 METHOD(stroke_list_t, destroy, void,
1507 private_stroke_list_t *this)
1508 {
1509 free(this);
1510 }
1511
1512 /*
1513 * see header file
1514 */
1515 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1516 {
1517 private_stroke_list_t *this;
1518
1519 INIT(this,
1520 .public = {
1521 .list = _list,
1522 .status = _status,
1523 .leases = _leases,
1524 .destroy = _destroy,
1525 },
1526 .uptime = time_monotonic(NULL),
1527 .swan = "strong",
1528 .attribute = attribute,
1529 );
1530
1531 if (lib->settings->get_bool(lib->settings,
1532 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk", FALSE))
1533 {
1534 this->swan = "weak";
1535 }
1536
1537 return &this->public;
1538 }
1539