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