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