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