Moved data structures to new collections subfolder
[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 <collections/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", name, local ? "local: " : "remote:");
342 id = auth->get(auth, AUTH_RULE_IDENTITY);
343 if (id)
344 {
345 fprintf(out, " [%Y]", id);
346 }
347 fprintf(out, " uses ");
348
349 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
350 if (auth_class == AUTH_CLASS_EAP)
351 {
352 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
353 {
354 fprintf(out, "EAP authentication");
355 }
356 else
357 {
358 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
359 {
360 fprintf(out, "EAP_%" PRIuPTR "-%" PRIuPTR " authentication",
361 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
362 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
363 }
364 else
365 {
366 fprintf(out, "%N authentication", eap_type_names,
367 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
368 }
369 }
370 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
371 if (id)
372 {
373 fprintf(out, " with EAP identity '%Y'", id);
374 }
375 fprintf(out, "\n");
376 }
377 else if (auth_class == AUTH_CLASS_XAUTH)
378 {
379 fprintf(out, "%N authentication: %s", auth_class_names, auth_class,
380 auth->get(auth, AUTH_RULE_XAUTH_BACKEND) ?: "any");
381 id = auth->get(auth, AUTH_RULE_XAUTH_IDENTITY);
382 if (id)
383 {
384 fprintf(out, " with XAuth identity '%Y'", id);
385 }
386 fprintf(out, "\n");
387 }
388 else
389 {
390 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
391 }
392
393 cert = auth->get(auth, AUTH_RULE_CA_CERT);
394 if (cert)
395 {
396 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
397 }
398
399 cert = auth->get(auth, AUTH_RULE_IM_CERT);
400 if (cert)
401 {
402 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
403 }
404
405 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
406 if (cert)
407 {
408 fprintf(out, "%12s: cert: \"%Y\"\n", name,
409 cert->get_subject(cert));
410 }
411
412 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
413 if (valid != VALIDATION_FAILED)
414 {
415 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
416 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
417 }
418
419 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
420 if (valid != VALIDATION_FAILED)
421 {
422 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
423 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
424 }
425
426 rules = auth->create_enumerator(auth);
427 while (rules->enumerate(rules, &rule, &id))
428 {
429 if (rule == AUTH_RULE_GROUP)
430 {
431 fprintf(out, "%12s: group: %Y\n", name, id);
432 }
433 }
434 rules->destroy(rules);
435 }
436 enumerator->destroy(enumerator);
437 }
438
439 METHOD(stroke_list_t, status, void,
440 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out,
441 bool all, bool wait)
442 {
443 enumerator_t *enumerator, *children;
444 ike_cfg_t *ike_cfg;
445 child_cfg_t *child_cfg;
446 child_sa_t *child_sa;
447 ike_sa_t *ike_sa;
448 linked_list_t *my_ts, *other_ts;
449 bool first, found = FALSE;
450 char *name = msg->status.name;
451 u_int half_open;
452
453 if (all)
454 {
455 peer_cfg_t *peer_cfg;
456 ike_version_t ike_version;
457 char *pool;
458 host_t *host;
459 u_int32_t dpd;
460 time_t since, now;
461 u_int size, online, offline, i;
462 struct utsname utsname;
463
464 now = time_monotonic(NULL);
465 since = time(NULL) - (now - this->uptime);
466
467 fprintf(out, "Status of IKE charon daemon (%sSwan "VERSION, this->swan);
468 if (uname(&utsname) == 0)
469 {
470 fprintf(out, ", %s %s, %s",
471 utsname.sysname, utsname.release, utsname.machine);
472 }
473 fprintf(out, "):\n uptime: %V, since %T\n", &now, &this->uptime, &since,
474 FALSE);
475 #ifdef HAVE_MALLINFO
476 {
477 struct mallinfo mi = mallinfo();
478
479 fprintf(out, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
480 mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
481 }
482 #endif /* HAVE_MALLINFO */
483 fprintf(out, " worker threads: %d of %d idle, ",
484 lib->processor->get_idle_threads(lib->processor),
485 lib->processor->get_total_threads(lib->processor));
486 for (i = 0; i < JOB_PRIO_MAX; i++)
487 {
488 fprintf(out, "%s%d", i == 0 ? "" : "/",
489 lib->processor->get_working_threads(lib->processor, i));
490 }
491 fprintf(out, " working, job queue: ");
492 for (i = 0; i < JOB_PRIO_MAX; i++)
493 {
494 fprintf(out, "%s%d", i == 0 ? "" : "/",
495 lib->processor->get_job_load(lib->processor, i));
496 }
497 fprintf(out, ", scheduled: %d\n",
498 lib->scheduler->get_job_load(lib->scheduler));
499 fprintf(out, " loaded plugins: %s\n",
500 lib->plugins->loaded_plugins(lib->plugins));
501
502 first = TRUE;
503 enumerator = this->attribute->create_pool_enumerator(this->attribute);
504 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
505 {
506 if (name && !streq(name, pool))
507 {
508 continue;
509 }
510 if (first)
511 {
512 first = FALSE;
513 fprintf(out, "Virtual IP pools (size/online/offline):\n");
514 }
515 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
516 }
517 enumerator->destroy(enumerator);
518
519 enumerator = hydra->kernel_interface->create_address_enumerator(
520 hydra->kernel_interface, ADDR_TYPE_REGULAR);
521 fprintf(out, "Listening IP addresses:\n");
522 while (enumerator->enumerate(enumerator, (void**)&host))
523 {
524 fprintf(out, " %H\n", host);
525 }
526 enumerator->destroy(enumerator);
527
528 fprintf(out, "Connections:\n");
529 enumerator = charon->backends->create_peer_cfg_enumerator(
530 charon->backends, NULL, NULL, NULL, NULL, IKE_ANY);
531 while (enumerator->enumerate(enumerator, &peer_cfg))
532 {
533 char *my_addr, *other_addr;
534 bool my_allow_any, other_allow_any;
535
536 if (name && !streq(name, peer_cfg->get_name(peer_cfg)))
537 {
538 continue;
539 }
540
541 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
542 ike_version = peer_cfg->get_ike_version(peer_cfg);
543 my_addr = ike_cfg->get_my_addr(ike_cfg, &my_allow_any);
544 other_addr = ike_cfg->get_other_addr(ike_cfg, &other_allow_any);
545 fprintf(out, "%12s: %s%s...%s%s %N", peer_cfg->get_name(peer_cfg),
546 my_allow_any ? "%":"", my_addr,
547 other_allow_any ? "%":"", other_addr,
548 ike_version_names, ike_version);
549
550 if (ike_version == IKEV1 && peer_cfg->use_aggressive(peer_cfg))
551 {
552 fprintf(out, " Aggressive");
553 }
554
555 dpd = peer_cfg->get_dpd(peer_cfg);
556 if (dpd)
557 {
558 fprintf(out, ", dpddelay=%us", dpd);
559 }
560 fprintf(out, "\n");
561
562 log_auth_cfgs(out, peer_cfg, TRUE);
563 log_auth_cfgs(out, peer_cfg, FALSE);
564
565 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
566 while (children->enumerate(children, &child_cfg))
567 {
568 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
569 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
570 fprintf(out, "%12s: child: %#R=== %#R%N",
571 child_cfg->get_name(child_cfg), my_ts, other_ts,
572 ipsec_mode_names, child_cfg->get_mode(child_cfg));
573 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
574 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
575
576 if (dpd)
577 {
578 fprintf(out, ", dpdaction=%N", action_names,
579 child_cfg->get_dpd_action(child_cfg));
580 }
581 fprintf(out, "\n");
582 }
583 children->destroy(children);
584 }
585 enumerator->destroy(enumerator);
586 }
587
588 /* Enumerate shunt policies */
589 first = TRUE;
590 enumerator = charon->shunts->create_enumerator(charon->shunts);
591 while (enumerator->enumerate(enumerator, &child_cfg))
592 {
593 if (name && !streq(name, child_cfg->get_name(child_cfg)))
594 {
595 continue;
596 }
597 if (first)
598 {
599 fprintf(out, "Shunted Connections:\n");
600 first = FALSE;
601 }
602 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
603 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
604 fprintf(out, "%12s: %#R=== %#R%N\n",
605 child_cfg->get_name(child_cfg), my_ts, other_ts,
606 ipsec_mode_names, child_cfg->get_mode(child_cfg));
607 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
608 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
609 }
610 enumerator->destroy(enumerator);
611
612 /* Enumerate traps */
613 first = TRUE;
614 enumerator = charon->traps->create_enumerator(charon->traps);
615 while (enumerator->enumerate(enumerator, NULL, &child_sa))
616 {
617 if (name && !streq(name, child_sa->get_name(child_sa)))
618 {
619 continue;
620 }
621 if (first)
622 {
623 fprintf(out, "Routed Connections:\n");
624 first = FALSE;
625 }
626 log_child_sa(out, child_sa, all);
627 }
628 enumerator->destroy(enumerator);
629
630 half_open = charon->ike_sa_manager->get_half_open_count(
631 charon->ike_sa_manager, NULL);
632 fprintf(out, "Security Associations (%u up, %u connecting):\n",
633 charon->ike_sa_manager->get_count(charon->ike_sa_manager) - half_open,
634 half_open);
635 enumerator = charon->controller->create_ike_sa_enumerator(
636 charon->controller, wait);
637 while (enumerator->enumerate(enumerator, &ike_sa))
638 {
639 bool ike_printed = FALSE;
640 enumerator_t *children = ike_sa->create_child_sa_enumerator(ike_sa);
641
642 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
643 {
644 log_ike_sa(out, ike_sa, all);
645 found = TRUE;
646 ike_printed = TRUE;
647 }
648
649 while (children->enumerate(children, (void**)&child_sa))
650 {
651 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
652 {
653 if (!ike_printed)
654 {
655 log_ike_sa(out, ike_sa, all);
656 found = TRUE;
657 ike_printed = TRUE;
658 }
659 log_child_sa(out, child_sa, all);
660 }
661 }
662 children->destroy(children);
663 }
664 enumerator->destroy(enumerator);
665
666 if (!found)
667 {
668 if (name)
669 {
670 fprintf(out, " no match\n");
671 }
672 else
673 {
674 fprintf(out, " none\n");
675 }
676 }
677 }
678
679 /**
680 * create a unique certificate list without duplicates
681 * certicates having the same issuer are grouped together.
682 */
683 static linked_list_t* create_unique_cert_list(certificate_type_t type)
684 {
685 linked_list_t *list = linked_list_create();
686 enumerator_t *enumerator = lib->credmgr->create_cert_enumerator(
687 lib->credmgr, type, KEY_ANY, NULL, FALSE);
688 certificate_t *cert;
689
690 while (enumerator->enumerate(enumerator, (void**)&cert))
691 {
692 enumerator_t *added = list->create_enumerator(list);
693 identification_t *issuer = cert->get_issuer(cert);
694 bool previous_same, same = FALSE, found = FALSE;
695 certificate_t *list_cert;
696
697 while (added->enumerate(added, (void**)&list_cert))
698 {
699 if (list_cert->equals(list_cert, cert))
700 { /* stop if we found a duplicate*/
701 found = TRUE;
702 break;
703 }
704 previous_same = same;
705 same = list_cert->has_issuer(list_cert, issuer);
706 if (previous_same && !same)
707 { /* group certificates with same issuer */
708 break;
709 }
710 }
711 if (!found)
712 {
713 list->insert_before(list, added, cert->get_ref(cert));
714 }
715 added->destroy(added);
716 }
717 enumerator->destroy(enumerator);
718 return list;
719 }
720
721 /**
722 * Print a single public key.
723 */
724 static void list_public_key(public_key_t *public, FILE *out)
725 {
726 private_key_t *private = NULL;
727 chunk_t keyid;
728 identification_t *id;
729
730 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
731 {
732 id = identification_create_from_encoding(ID_KEY_ID, keyid);
733 private = lib->credmgr->get_private(lib->credmgr,
734 public->get_type(public), id, NULL);
735 id->destroy(id);
736 }
737
738 fprintf(out, " pubkey: %N %d bits%s\n",
739 key_type_names, public->get_type(public),
740 public->get_keysize(public),
741 private ? ", has private key" : "");
742 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid))
743 {
744 fprintf(out, " keyid: %#B\n", &keyid);
745 }
746 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
747 {
748 fprintf(out, " subjkey: %#B\n", &keyid);
749 }
750 DESTROY_IF(private);
751 }
752
753 /**
754 * list all raw public keys
755 */
756 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
757 {
758 bool first = TRUE;
759 time_t now = time(NULL), notBefore, notAfter;
760 enumerator_t *enumerator;
761 certificate_t *cert;
762
763 enumerator = list->create_enumerator(list);
764 while (enumerator->enumerate(enumerator, (void**)&cert))
765 {
766 identification_t *subject = cert->get_subject(cert);
767 public_key_t *public = cert->get_public_key(cert);
768
769 if (public)
770 {
771 if (first)
772 {
773 fprintf(out, "\n");
774 fprintf(out, "List of Raw Public Keys:\n");
775 first = FALSE;
776 }
777 fprintf(out, "\n");
778
779 /* list subject if available */
780 if (subject->get_type(subject) != ID_KEY_ID)
781 {
782 fprintf(out, " subject: %#Y\n", subject);
783 }
784
785 /* list validity if available*/
786 cert->get_validity(cert, &now, &notBefore, &notAfter);
787 if (notBefore != UNDEFINED_TIME && notAfter != UNDEFINED_TIME)
788 {
789 fprintf(out, " validity: not before %T, ", &notBefore, utc);
790 if (now < notBefore)
791 {
792 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
793 }
794 else
795 {
796 fprintf(out, "ok\n");
797 }
798 fprintf(out, " not after %T, ", &notAfter, utc);
799 if (now > notAfter)
800 {
801 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
802 }
803 else
804 {
805 fprintf(out, "ok");
806 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
807 {
808 fprintf(out, " (expires in %V)", &now, &notAfter);
809 }
810 fprintf(out, " \n");
811 }
812 }
813
814 list_public_key(public, out);
815 public->destroy(public);
816 }
817 }
818 enumerator->destroy(enumerator);
819 }
820
821 /**
822 * list OpenPGP certificates
823 */
824 static void stroke_list_pgp(linked_list_t *list,bool utc, FILE *out)
825 {
826 bool first = TRUE;
827 time_t now = time(NULL);
828 enumerator_t *enumerator = list->create_enumerator(list);
829 certificate_t *cert;
830
831 while (enumerator->enumerate(enumerator, (void**)&cert))
832 {
833 time_t created, until;
834 public_key_t *public;
835 pgp_certificate_t *pgp_cert = (pgp_certificate_t*)cert;
836 chunk_t fingerprint = pgp_cert->get_fingerprint(pgp_cert);
837
838 if (first)
839 {
840
841 fprintf(out, "\n");
842 fprintf(out, "List of PGP End Entity Certificates:\n");
843 first = FALSE;
844 }
845 fprintf(out, "\n");
846 fprintf(out, " userid: '%Y'\n", cert->get_subject(cert));
847
848 fprintf(out, " digest: %#B\n", &fingerprint);
849
850 /* list validity */
851 cert->get_validity(cert, &now, &created, &until);
852 fprintf(out, " created: %T\n", &created, utc);
853 fprintf(out, " until: %T%s\n", &until, utc,
854 (until == TIME_32_BIT_SIGNED_MAX) ? " (expires never)":"");
855
856 public = cert->get_public_key(cert);
857 if (public)
858 {
859 list_public_key(public, out);
860 public->destroy(public);
861 }
862 }
863 enumerator->destroy(enumerator);
864 }
865
866 /**
867 * list all X.509 certificates matching the flags
868 */
869 static void stroke_list_certs(linked_list_t *list, char *label,
870 x509_flag_t flags, bool utc, FILE *out)
871 {
872 bool first = TRUE;
873 time_t now = time(NULL);
874 enumerator_t *enumerator;
875 certificate_t *cert;
876 x509_flag_t flag_mask;
877
878 /* mask all auxiliary flags */
879 flag_mask = ~(X509_SERVER_AUTH | X509_CLIENT_AUTH | X509_IKE_INTERMEDIATE |
880 X509_SELF_SIGNED | X509_IP_ADDR_BLOCKS);
881
882 enumerator = list->create_enumerator(list);
883 while (enumerator->enumerate(enumerator, (void**)&cert))
884 {
885 x509_t *x509 = (x509_t*)cert;
886 x509_flag_t x509_flags = x509->get_flags(x509) & flag_mask;
887
888 /* list only if flag is set or flag == 0 */
889 if ((x509_flags & flags) || (x509_flags == flags))
890 {
891 enumerator_t *enumerator;
892 identification_t *altName;
893 bool first_altName = TRUE;
894 u_int pathlen;
895 chunk_t serial, authkey;
896 time_t notBefore, notAfter;
897 public_key_t *public;
898
899 if (first)
900 {
901 fprintf(out, "\n");
902 fprintf(out, "List of %s:\n", label);
903 first = FALSE;
904 }
905 fprintf(out, "\n");
906
907 /* list subjectAltNames */
908 enumerator = x509->create_subjectAltName_enumerator(x509);
909 while (enumerator->enumerate(enumerator, (void**)&altName))
910 {
911 if (first_altName)
912 {
913 fprintf(out, " altNames: ");
914 first_altName = FALSE;
915 }
916 else
917 {
918 fprintf(out, ", ");
919 }
920 fprintf(out, "%Y", altName);
921 }
922 if (!first_altName)
923 {
924 fprintf(out, "\n");
925 }
926 enumerator->destroy(enumerator);
927
928 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
929 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
930 serial = chunk_skip_zero(x509->get_serial(x509));
931 fprintf(out, " serial: %#B\n", &serial);
932
933 /* list validity */
934 cert->get_validity(cert, &now, &notBefore, &notAfter);
935 fprintf(out, " validity: not before %T, ", &notBefore, utc);
936 if (now < notBefore)
937 {
938 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
939 }
940 else
941 {
942 fprintf(out, "ok\n");
943 }
944 fprintf(out, " not after %T, ", &notAfter, utc);
945 if (now > notAfter)
946 {
947 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
948 }
949 else
950 {
951 fprintf(out, "ok");
952 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
953 {
954 fprintf(out, " (expires in %V)", &now, &notAfter);
955 }
956 fprintf(out, " \n");
957 }
958
959 public = cert->get_public_key(cert);
960 if (public)
961 {
962 list_public_key(public, out);
963 public->destroy(public);
964 }
965
966 /* list optional authorityKeyIdentifier */
967 authkey = x509->get_authKeyIdentifier(x509);
968 if (authkey.ptr)
969 {
970 fprintf(out, " authkey: %#B\n", &authkey);
971 }
972
973 /* list optional pathLenConstraint */
974 pathlen = x509->get_constraint(x509, X509_PATH_LEN);
975 if (pathlen != X509_NO_CONSTRAINT)
976 {
977 fprintf(out, " pathlen: %u\n", pathlen);
978 }
979
980 /* list optional ipAddrBlocks */
981 if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
982 {
983 traffic_selector_t *ipAddrBlock;
984 bool first_ipAddrBlock = TRUE;
985
986 fprintf(out, " addresses: ");
987 enumerator = x509->create_ipAddrBlock_enumerator(x509);
988 while (enumerator->enumerate(enumerator, &ipAddrBlock))
989 {
990 if (first_ipAddrBlock)
991 {
992 first_ipAddrBlock = FALSE;
993 }
994 else
995 {
996 fprintf(out, ", ");
997 }
998 fprintf(out, "%R", ipAddrBlock);
999 }
1000 enumerator->destroy(enumerator);
1001 fprintf(out, "\n");
1002 }
1003 }
1004 }
1005 enumerator->destroy(enumerator);
1006 }
1007
1008 /**
1009 * list all X.509 attribute certificates
1010 */
1011 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
1012 {
1013 bool first = TRUE;
1014 time_t thisUpdate, nextUpdate, now = time(NULL);
1015 enumerator_t *enumerator = list->create_enumerator(list);
1016 certificate_t *cert;
1017
1018 while (enumerator->enumerate(enumerator, (void**)&cert))
1019 {
1020 ac_t *ac = (ac_t*)cert;
1021 identification_t *id;
1022 ietf_attributes_t *groups;
1023 chunk_t chunk;
1024
1025 if (first)
1026 {
1027 fprintf(out, "\n");
1028 fprintf(out, "List of X.509 Attribute Certificates:\n");
1029 first = FALSE;
1030 }
1031 fprintf(out, "\n");
1032
1033 id = cert->get_subject(cert);
1034 if (id)
1035 {
1036 fprintf(out, " holder: \"%Y\"\n", id);
1037 }
1038 id = ac->get_holderIssuer(ac);
1039 if (id)
1040 {
1041 fprintf(out, " hissuer: \"%Y\"\n", id);
1042 }
1043 chunk = chunk_skip_zero(ac->get_holderSerial(ac));
1044 if (chunk.ptr)
1045 {
1046 fprintf(out, " hserial: %#B\n", &chunk);
1047 }
1048 groups = ac->get_groups(ac);
1049 if (groups)
1050 {
1051 fprintf(out, " groups: %s\n", groups->get_string(groups));
1052 groups->destroy(groups);
1053 }
1054 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
1055 chunk = chunk_skip_zero(ac->get_serial(ac));
1056 fprintf(out, " serial: %#B\n", &chunk);
1057
1058 /* list validity */
1059 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
1060 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
1061 fprintf(out, " next %T, ", &nextUpdate, utc);
1062 if (now > nextUpdate)
1063 {
1064 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1065 }
1066 else
1067 {
1068 fprintf(out, "ok");
1069 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
1070 {
1071 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1072 }
1073 fprintf(out, " \n");
1074 }
1075
1076 /* list optional authorityKeyIdentifier */
1077 chunk = ac->get_authKeyIdentifier(ac);
1078 if (chunk.ptr)
1079 {
1080 fprintf(out, " authkey: %#B\n", &chunk);
1081 }
1082 }
1083 enumerator->destroy(enumerator);
1084 }
1085
1086 /**
1087 * list all X.509 CRLs
1088 */
1089 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
1090 {
1091 bool first = TRUE;
1092 time_t thisUpdate, nextUpdate, now = time(NULL);
1093 enumerator_t *enumerator = list->create_enumerator(list);
1094 certificate_t *cert;
1095
1096 while (enumerator->enumerate(enumerator, (void**)&cert))
1097 {
1098 crl_t *crl = (crl_t*)cert;
1099 chunk_t chunk;
1100
1101 if (first)
1102 {
1103 fprintf(out, "\n");
1104 fprintf(out, "List of X.509 CRLs:\n");
1105 first = FALSE;
1106 }
1107 fprintf(out, "\n");
1108
1109 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
1110
1111 /* list optional crlNumber */
1112 chunk = chunk_skip_zero(crl->get_serial(crl));
1113 if (chunk.ptr)
1114 {
1115 fprintf(out, " serial: %#B\n", &chunk);
1116 }
1117 if (crl->is_delta_crl(crl, &chunk))
1118 {
1119 chunk = chunk_skip_zero(chunk);
1120 fprintf(out, " delta for: %#B\n", &chunk);
1121 }
1122
1123 /* count the number of revoked certificates */
1124 {
1125 int count = 0;
1126 enumerator_t *enumerator = crl->create_enumerator(crl);
1127
1128 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
1129 {
1130 count++;
1131 }
1132 fprintf(out, " revoked: %d certificate%s\n", count,
1133 (count == 1)? "" : "s");
1134 enumerator->destroy(enumerator);
1135 }
1136
1137 /* list validity */
1138 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
1139 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
1140 fprintf(out, " next %T, ", &nextUpdate, utc);
1141 if (now > nextUpdate)
1142 {
1143 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1144 }
1145 else
1146 {
1147 fprintf(out, "ok");
1148 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
1149 {
1150 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1151 }
1152 fprintf(out, " \n");
1153 }
1154
1155 /* list optional authorityKeyIdentifier */
1156 chunk = crl->get_authKeyIdentifier(crl);
1157 if (chunk.ptr)
1158 {
1159 fprintf(out, " authkey: %#B\n", &chunk);
1160 }
1161 }
1162 enumerator->destroy(enumerator);
1163 }
1164
1165 /**
1166 * list all OCSP responses
1167 */
1168 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
1169 {
1170 bool first = TRUE, ok;
1171 enumerator_t *enumerator = list->create_enumerator(list);
1172 certificate_t *cert;
1173 time_t produced, usable, now = time(NULL);
1174
1175 while (enumerator->enumerate(enumerator, (void**)&cert))
1176 {
1177 if (first)
1178 {
1179 fprintf(out, "\n");
1180 fprintf(out, "List of OCSP responses:\n");
1181 fprintf(out, "\n");
1182 first = FALSE;
1183 }
1184 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
1185
1186 /* check validity */
1187 ok = cert->get_validity(cert, &now, &produced, &usable);
1188 fprintf(out, " validity: produced at %T\n", &produced, utc);
1189 fprintf(out, " usable till %T, ", &usable, utc);
1190 if (ok)
1191 {
1192 fprintf(out, "ok\n");
1193 }
1194 else
1195 {
1196 fprintf(out, "expired (%V ago)\n", &now, &usable);
1197 }
1198 }
1199 enumerator->destroy(enumerator);
1200 }
1201
1202 /**
1203 * Print the name of an algorithm plus the name of the plugin that registered it
1204 */
1205 static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
1206 const char *plugin_name)
1207 {
1208 char alg_name[BUF_LEN];
1209 int alg_name_len;
1210
1211 if (alg_names)
1212 {
1213 alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type,
1214 plugin_name);
1215 }
1216 else
1217 {
1218 alg_name_len = sprintf(alg_name, " [%s]", plugin_name);
1219 }
1220 if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
1221 {
1222 fprintf(out, "\n ");
1223 *len = 13;
1224 }
1225 fprintf(out, "%s", alg_name);
1226 *len += alg_name_len;
1227 }
1228
1229 /**
1230 * List of registered cryptographical algorithms
1231 */
1232 static void list_algs(FILE *out)
1233 {
1234 enumerator_t *enumerator;
1235 encryption_algorithm_t encryption;
1236 integrity_algorithm_t integrity;
1237 hash_algorithm_t hash;
1238 pseudo_random_function_t prf;
1239 diffie_hellman_group_t group;
1240 rng_quality_t quality;
1241 const char *plugin_name;
1242 int len;
1243
1244 fprintf(out, "\n");
1245 fprintf(out, "List of registered IKEv2 Algorithms:\n");
1246 fprintf(out, "\n encryption:");
1247 len = 13;
1248 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1249 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1250 {
1251 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1252 }
1253 enumerator->destroy(enumerator);
1254 fprintf(out, "\n integrity: ");
1255 len = 13;
1256 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1257 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
1258 {
1259 print_alg(out, &len, integrity_algorithm_names, integrity, plugin_name);
1260 }
1261 enumerator->destroy(enumerator);
1262 fprintf(out, "\n aead: ");
1263 len = 13;
1264 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
1265 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1266 {
1267 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1268 }
1269 enumerator->destroy(enumerator);
1270 fprintf(out, "\n hasher: ");
1271 len = 13;
1272 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1273 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
1274 {
1275 print_alg(out, &len, hash_algorithm_names, hash, plugin_name);
1276 }
1277 enumerator->destroy(enumerator);
1278 fprintf(out, "\n prf: ");
1279 len = 13;
1280 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1281 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
1282 {
1283 print_alg(out, &len, pseudo_random_function_names, prf, plugin_name);
1284 }
1285 enumerator->destroy(enumerator);
1286 fprintf(out, "\n dh-group: ");
1287 len = 13;
1288 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1289 while (enumerator->enumerate(enumerator, &group, &plugin_name))
1290 {
1291 print_alg(out, &len, diffie_hellman_group_names, group, plugin_name);
1292 }
1293 enumerator->destroy(enumerator);
1294 fprintf(out, "\n random-gen:");
1295 len = 13;
1296 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
1297 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
1298 {
1299 print_alg(out, &len, rng_quality_names, quality, plugin_name);
1300 }
1301 enumerator->destroy(enumerator);
1302 fprintf(out, "\n nonce-gen: ");
1303 len = 13;
1304 enumerator = lib->crypto->create_nonce_gen_enumerator(lib->crypto);
1305 while (enumerator->enumerate(enumerator, &plugin_name))
1306 {
1307 print_alg(out, &len, NULL, 0, plugin_name);
1308 }
1309 enumerator->destroy(enumerator);
1310 fprintf(out, "\n");
1311 }
1312
1313 /**
1314 * List loaded plugin information
1315 */
1316 static void list_plugins(FILE *out)
1317 {
1318 plugin_feature_t *features, *fp;
1319 enumerator_t *enumerator;
1320 linked_list_t *list;
1321 plugin_t *plugin;
1322 int count, i;
1323 bool loaded;
1324 char *str;
1325
1326 fprintf(out, "\n");
1327 fprintf(out, "List of loaded Plugins:\n");
1328 fprintf(out, "\n");
1329
1330 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
1331 while (enumerator->enumerate(enumerator, &plugin, &list))
1332 {
1333 fprintf(out, "%s:\n", plugin->get_name(plugin));
1334 if (plugin->get_features)
1335 {
1336 count = plugin->get_features(plugin, &features);
1337 for (i = 0; i < count; i++)
1338 {
1339 str = plugin_feature_get_string(&features[i]);
1340 switch (features[i].kind)
1341 {
1342 case FEATURE_PROVIDE:
1343 fp = &features[i];
1344 loaded = list->find_first(list, NULL,
1345 (void**)&fp) == SUCCESS;
1346 fprintf(out, " %s%s\n",
1347 str, loaded ? "" : " (not loaded)");
1348 break;
1349 case FEATURE_DEPENDS:
1350 fprintf(out, " %s\n", str);
1351 break;
1352 case FEATURE_SDEPEND:
1353 fprintf(out, " %s (soft)\n", str);
1354 break;
1355 default:
1356 break;
1357 }
1358 free(str);
1359 }
1360 }
1361 }
1362 enumerator->destroy(enumerator);
1363 }
1364
1365 METHOD(stroke_list_t, list, void,
1366 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1367 {
1368 linked_list_t *cert_list = NULL;
1369
1370 if (msg->list.flags & LIST_PUBKEYS)
1371 {
1372 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
1373
1374 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
1375 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
1376 }
1377 if (msg->list.flags & LIST_CERTS)
1378 {
1379 linked_list_t *pgp_list = create_unique_cert_list(CERT_GPG);
1380
1381 stroke_list_pgp(pgp_list, msg->list.utc, out);
1382 pgp_list->destroy_offset(pgp_list, offsetof(certificate_t, destroy));
1383 }
1384 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
1385 {
1386 cert_list = create_unique_cert_list(CERT_X509);
1387 }
1388 if (msg->list.flags & LIST_CERTS)
1389 {
1390 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
1391 X509_NONE, msg->list.utc, out);
1392 }
1393 if (msg->list.flags & LIST_CACERTS)
1394 {
1395 stroke_list_certs(cert_list, "X.509 CA Certificates",
1396 X509_CA, msg->list.utc, out);
1397 }
1398 if (msg->list.flags & LIST_OCSPCERTS)
1399 {
1400 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
1401 X509_OCSP_SIGNER, msg->list.utc, out);
1402 }
1403 if (msg->list.flags & LIST_AACERTS)
1404 {
1405 stroke_list_certs(cert_list, "X.509 AA Certificates",
1406 X509_AA, msg->list.utc, out);
1407 }
1408 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1409
1410 if (msg->list.flags & LIST_ACERTS)
1411 {
1412 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
1413
1414 stroke_list_acerts(ac_list, msg->list.utc, out);
1415 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
1416 }
1417 if (msg->list.flags & LIST_CRLS)
1418 {
1419 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1420
1421 stroke_list_crls(crl_list, msg->list.utc, out);
1422 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1423 }
1424 if (msg->list.flags & LIST_OCSP)
1425 {
1426 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1427
1428 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1429
1430 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1431 }
1432 if (msg->list.flags & LIST_ALGS)
1433 {
1434 list_algs(out);
1435 }
1436 if (msg->list.flags & LIST_PLUGINS)
1437 {
1438 list_plugins(out);
1439 }
1440 }
1441
1442 /**
1443 * Print leases of a single pool
1444 */
1445 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1446 host_t *address, u_int size, u_int online, u_int offline)
1447 {
1448 enumerator_t *enumerator;
1449 identification_t *id;
1450 host_t *lease;
1451 bool on;
1452 int found = 0;
1453
1454 fprintf(out, "Leases in pool '%s', usage: %u/%u, %u online\n",
1455 pool, online + offline, size, online);
1456 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1457 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1458 {
1459 if (!address || address->ip_equals(address, lease))
1460 {
1461 fprintf(out, " %15H %s '%Y'\n",
1462 lease, on ? "online" : "offline", id);
1463 found++;
1464 }
1465 }
1466 enumerator->destroy(enumerator);
1467 if (!found)
1468 {
1469 fprintf(out, " no matching leases found\n");
1470 }
1471 }
1472
1473 METHOD(stroke_list_t, leases, void,
1474 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1475 {
1476 enumerator_t *enumerator;
1477 u_int size, offline, online;
1478 host_t *address = NULL;
1479 char *pool;
1480 int found = 0;
1481
1482 if (msg->leases.address)
1483 {
1484 address = host_create_from_string(msg->leases.address, 0);
1485 }
1486
1487 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1488 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1489 {
1490 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1491 {
1492 pool_leases(this, out, pool, address, size, online, offline);
1493 found++;
1494 }
1495 }
1496 enumerator->destroy(enumerator);
1497 if (!found)
1498 {
1499 if (msg->leases.pool)
1500 {
1501 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1502 }
1503 else
1504 {
1505 fprintf(out, "no pools found\n");
1506 }
1507 }
1508 DESTROY_IF(address);
1509 }
1510
1511 METHOD(stroke_list_t, destroy, void,
1512 private_stroke_list_t *this)
1513 {
1514 free(this);
1515 }
1516
1517 /*
1518 * see header file
1519 */
1520 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1521 {
1522 private_stroke_list_t *this;
1523
1524 INIT(this,
1525 .public = {
1526 .list = _list,
1527 .status = _status,
1528 .leases = _leases,
1529 .destroy = _destroy,
1530 },
1531 .uptime = time_monotonic(NULL),
1532 .swan = "strong",
1533 .attribute = attribute,
1534 );
1535
1536 if (lib->settings->get_bool(lib->settings,
1537 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk", FALSE))
1538 {
1539 this->swan = "weak";
1540 }
1541
1542 return &this->public;
1543 }
1544