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