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