listing routed connection in statusall
[strongswan.git] / src / charon / 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 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <credentials/certificates/x509.h>
23 #include <credentials/certificates/ac.h>
24 #include <credentials/certificates/crl.h>
25 #include <config/peer_cfg.h>
26
27 /* warning intervals for list functions */
28 #define CERT_WARNING_INTERVAL 30 /* days */
29 #define CRL_WARNING_INTERVAL 7 /* days */
30 #define AC_WARNING_INTERVAL 1 /* day */
31
32 typedef struct private_stroke_list_t private_stroke_list_t;
33
34 /**
35 * private data of stroke_list
36 */
37 struct private_stroke_list_t {
38
39 /**
40 * public functions
41 */
42 stroke_list_t public;
43
44 /**
45 * timestamp of daemon start
46 */
47 time_t uptime;
48
49 /**
50 * strokes attribute provider
51 */
52 stroke_attribute_t *attribute;
53 };
54
55 /**
56 * log an IKE_SA to out
57 */
58 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
59 {
60 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
61 time_t now = time(NULL);
62
63 fprintf(out, "%12s[%d]: %N",
64 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
65 ike_sa_state_names, ike_sa->get_state(ike_sa));
66
67 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
68 {
69 time_t established;
70
71 established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
72 fprintf(out, " %V ago", &now, &established);
73 }
74
75 fprintf(out, ", %H[%Y]...%H[%Y]\n",
76 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
77 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
78
79 if (all)
80 {
81 proposal_t *ike_proposal;
82
83 ike_proposal = ike_sa->get_proposal(ike_sa);
84
85 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
86 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
87 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
88 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
89
90
91 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
92 {
93 time_t rekey, reauth;
94 peer_cfg_t *peer_cfg;
95
96 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY);
97 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
98 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
99
100 if (rekey)
101 {
102 fprintf(out, ", rekeying in %V", &rekey, &now);
103 }
104 if (reauth)
105 {
106 bool first = TRUE;
107 enumerator_t *enumerator;
108 auth_cfg_t *auth;
109
110 fprintf(out, ", ");
111 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, TRUE);
112 while (enumerator->enumerate(enumerator, &auth))
113 {
114 if (!first)
115 {
116 fprintf(out, "+");
117 }
118 first = FALSE;
119 fprintf(out, "%N", auth_class_names,
120 auth->get(auth, AUTH_RULE_AUTH_CLASS));
121 }
122 enumerator->destroy(enumerator);
123 fprintf(out, " reauthentication in %V", &reauth, &now);
124 }
125 if (!rekey && !reauth)
126 {
127 fprintf(out, ", rekeying disabled");
128 }
129 }
130 fprintf(out, "\n");
131
132 if (ike_proposal)
133 {
134 char buf[BUF_LEN];
135
136 snprintf(buf, BUF_LEN, "%P", ike_proposal);
137 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
138 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
139 buf+4);
140 }
141 }
142 }
143
144 /**
145 * log an CHILD_SA to out
146 */
147 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
148 {
149 u_int32_t rekey, now = time(NULL);
150 u_int32_t use_in, use_out;
151 proposal_t *proposal;
152 child_cfg_t *config = child_sa->get_config(child_sa);
153
154 fprintf(out, "%12s{%d}: %N, %N%s",
155 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
156 child_sa_state_names, child_sa->get_state(child_sa),
157 ipsec_mode_names, child_sa->get_mode(child_sa),
158 config->use_proxy_mode(config) ? "_PROXY" : "");
159
160 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
161 {
162 fprintf(out, ", %N%s SPIs: %.8x_i %.8x_o",
163 protocol_id_names, child_sa->get_protocol(child_sa),
164 child_sa->has_encap(child_sa) ? " in UDP" : "",
165 ntohl(child_sa->get_spi(child_sa, TRUE)),
166 ntohl(child_sa->get_spi(child_sa, FALSE)));
167
168 if (child_sa->get_ipcomp(child_sa) != IPCOMP_NONE)
169 {
170 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
171 ntohs(child_sa->get_cpi(child_sa, TRUE)),
172 ntohs(child_sa->get_cpi(child_sa, FALSE)));
173 }
174
175 if (all)
176 {
177 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
178 child_sa->get_reqid(child_sa));
179
180 proposal = child_sa->get_proposal(child_sa);
181 if (proposal)
182 {
183 u_int16_t encr_alg = ENCR_UNDEFINED, int_alg = AUTH_UNDEFINED;
184 u_int16_t encr_size = 0, int_size = 0;
185
186 proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
187 &encr_alg, &encr_size);
188 proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
189 &int_alg, &int_size);
190
191 if (encr_alg != ENCR_UNDEFINED)
192 {
193 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
194 if (encr_size)
195 {
196 fprintf(out, "-%d", encr_size);
197 }
198 }
199 if (int_alg != AUTH_UNDEFINED)
200 {
201 fprintf(out, "/%N", integrity_algorithm_names, int_alg);
202 if (int_size)
203 {
204 fprintf(out, "-%d", int_size);
205 }
206 }
207 }
208 fprintf(out, ", rekeying ");
209
210 rekey = child_sa->get_lifetime(child_sa, FALSE);
211 if (rekey)
212 {
213 fprintf(out, "in %V", &now, &rekey);
214 }
215 else
216 {
217 fprintf(out, "disabled");
218 }
219
220 fprintf(out, ", last use: ");
221 use_in = child_sa->get_usetime(child_sa, TRUE);
222 if (use_in)
223 {
224 fprintf(out, "%ds_i ", now - use_in);
225 }
226 else
227 {
228 fprintf(out, "no_i ");
229 }
230 use_out = child_sa->get_usetime(child_sa, FALSE);
231 if (use_out)
232 {
233 fprintf(out, "%ds_o ", now - use_out);
234 }
235 else
236 {
237 fprintf(out, "no_o ");
238 }
239 }
240 }
241
242 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
243 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
244 child_sa->get_traffic_selectors(child_sa, TRUE),
245 child_sa->get_traffic_selectors(child_sa, FALSE));
246 }
247
248 /**
249 * Log a configs local or remote authentication config to out
250 */
251 static void log_auth_cfgs(FILE *out, peer_cfg_t *peer_cfg, bool local)
252 {
253 enumerator_t *enumerator, *rules;
254 auth_rule_t rule;
255 auth_cfg_t *auth;
256 auth_class_t auth_class;
257 identification_t *id;
258 certificate_t *cert;
259 cert_validation_t valid;
260 char *name;
261
262 name = peer_cfg->get_name(peer_cfg);
263
264 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
265 while (enumerator->enumerate(enumerator, &auth))
266 {
267 fprintf(out, "%12s: %s [%Y] uses ", name, local ? "local: " : "remote:",
268 auth->get(auth, AUTH_RULE_IDENTITY));
269
270 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
271 if (auth_class != AUTH_CLASS_EAP)
272 {
273 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
274 }
275 else
276 {
277 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
278 {
279 fprintf(out, "EAP authentication");
280 }
281 else
282 {
283 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
284 {
285 fprintf(out, "EAP_%d-%d authentication",
286 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
287 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
288 }
289 else
290 {
291 fprintf(out, "%N authentication", eap_type_names,
292 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
293 }
294 }
295 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
296 if (id)
297 {
298 fprintf(out, " with EAP identity '%Y'", id);
299 }
300 fprintf(out, "\n");
301 }
302
303 cert = auth->get(auth, AUTH_RULE_CA_CERT);
304 if (cert)
305 {
306 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
307 }
308
309 cert = auth->get(auth, AUTH_RULE_IM_CERT);
310 if (cert)
311 {
312 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
313 }
314
315 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
316 if (cert)
317 {
318 fprintf(out, "%12s: cert: \"%Y\"\n", name,
319 cert->get_subject(cert));
320 }
321
322 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
323 if (valid != VALIDATION_FAILED)
324 {
325 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
326 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
327 }
328
329 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
330 if (valid != VALIDATION_FAILED)
331 {
332 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
333 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
334 }
335
336 rules = auth->create_enumerator(auth);
337 while (rules->enumerate(rules, &rule, &id))
338 {
339 if (rule == AUTH_RULE_AC_GROUP)
340 {
341 fprintf(out, "%12s: group: %Y\n", name, id);
342 }
343 }
344 rules->destroy(rules);
345 }
346 enumerator->destroy(enumerator);
347 }
348
349 /**
350 * Implementation of stroke_list_t.status.
351 */
352 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
353 {
354 enumerator_t *enumerator, *children;
355 ike_cfg_t *ike_cfg;
356 child_cfg_t *child_cfg;
357 child_sa_t *child_sa;
358 ike_sa_t *ike_sa;
359 bool found = FALSE;
360 char *name = msg->status.name;
361
362 if (all)
363 {
364 peer_cfg_t *peer_cfg;
365 char *plugin, *pool;
366 host_t *host;
367 u_int32_t dpd;
368 time_t now = time(NULL);
369 bool first = TRUE;
370 u_int size, online, offline;
371
372 fprintf(out, "Performance:\n");
373 fprintf(out, " uptime: %V, since %T\n", &now, &this->uptime, &this->uptime, FALSE);
374 fprintf(out, " worker threads: %d idle of %d,",
375 charon->processor->get_idle_threads(charon->processor),
376 charon->processor->get_total_threads(charon->processor));
377 fprintf(out, " job queue load: %d,",
378 charon->processor->get_job_load(charon->processor));
379 fprintf(out, " scheduled events: %d\n",
380 charon->scheduler->get_job_load(charon->scheduler));
381 fprintf(out, " loaded plugins: ");
382 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
383 while (enumerator->enumerate(enumerator, &plugin))
384 {
385 fprintf(out, "%s ", plugin);
386 }
387 enumerator->destroy(enumerator);
388 fprintf(out, "\n");
389
390 enumerator = this->attribute->create_pool_enumerator(this->attribute);
391 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
392 {
393 if (name && !streq(name, pool))
394 {
395 continue;
396 }
397 if (first)
398 {
399 first = FALSE;
400 fprintf(out, "Virtual IP pools (size/online/offline):\n");
401 }
402 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
403 }
404 enumerator->destroy(enumerator);
405
406 enumerator = charon->kernel_interface->create_address_enumerator(
407 charon->kernel_interface, FALSE, FALSE);
408 fprintf(out, "Listening IP addresses:\n");
409 while (enumerator->enumerate(enumerator, (void**)&host))
410 {
411 fprintf(out, " %H\n", host);
412 }
413 enumerator->destroy(enumerator);
414
415 fprintf(out, "Connections:\n");
416 enumerator = charon->backends->create_peer_cfg_enumerator(
417 charon->backends, NULL, NULL, NULL, NULL);
418 while (enumerator->enumerate(enumerator, &peer_cfg))
419 {
420 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
421 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
422 {
423 continue;
424 }
425
426 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
427 fprintf(out, "%12s: %s...%s", peer_cfg->get_name(peer_cfg),
428 ike_cfg->get_my_addr(ike_cfg), ike_cfg->get_other_addr(ike_cfg));
429
430 dpd = peer_cfg->get_dpd(peer_cfg);
431 if (dpd)
432 {
433 fprintf(out, ", dpddelay=%us", dpd);
434 }
435 fprintf(out, "\n");
436
437 log_auth_cfgs(out, peer_cfg, TRUE);
438 log_auth_cfgs(out, peer_cfg, FALSE);
439
440 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
441 while (children->enumerate(children, &child_cfg))
442 {
443 linked_list_t *my_ts, *other_ts;
444
445 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
446 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
447 fprintf(out, "%12s: child: %#R=== %#R", child_cfg->get_name(child_cfg),
448 my_ts, other_ts);
449 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
450 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
451
452 if (dpd)
453 {
454 fprintf(out, ", dpdaction=%N", action_names,
455 child_cfg->get_dpd_action(child_cfg));
456 }
457 fprintf(out, "\n");
458 }
459 children->destroy(children);
460 }
461 enumerator->destroy(enumerator);
462 }
463
464 fprintf(out, "Routed Connections:\n");
465 enumerator = charon->traps->create_enumerator(charon->traps);
466 while (enumerator->enumerate(enumerator, NULL, &child_sa))
467 {
468 log_child_sa(out, child_sa, all);
469 }
470 enumerator->destroy(enumerator);
471
472 fprintf(out, "Security Associations:\n");
473 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
474 while (enumerator->enumerate(enumerator, &ike_sa))
475 {
476 bool ike_printed = FALSE;
477 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
478
479 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
480 {
481 log_ike_sa(out, ike_sa, all);
482 found = TRUE;
483 ike_printed = TRUE;
484 }
485
486 while (children->iterate(children, (void**)&child_sa))
487 {
488 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
489 {
490 if (!ike_printed)
491 {
492 log_ike_sa(out, ike_sa, all);
493 found = TRUE;
494 ike_printed = TRUE;
495 }
496 log_child_sa(out, child_sa, all);
497 }
498 }
499 children->destroy(children);
500 }
501 enumerator->destroy(enumerator);
502
503 if (!found)
504 {
505 if (name)
506 {
507 fprintf(out, " no match\n");
508 }
509 else
510 {
511 fprintf(out, " none\n");
512 }
513 }
514 }
515
516 /**
517 * create a unique certificate list without duplicates
518 * certicates having the same issuer are grouped together.
519 */
520 static linked_list_t* create_unique_cert_list(certificate_type_t type)
521 {
522 linked_list_t *list = linked_list_create();
523 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
524 charon->credentials, type, KEY_ANY,
525 NULL, FALSE);
526 certificate_t *cert;
527
528 while (enumerator->enumerate(enumerator, (void**)&cert))
529 {
530 iterator_t *iterator = list->create_iterator(list, TRUE);
531 identification_t *issuer = cert->get_issuer(cert);
532 bool previous_same, same = FALSE, last = TRUE;
533 certificate_t *list_cert;
534
535 while (iterator->iterate(iterator, (void**)&list_cert))
536 {
537 /* exit if we have a duplicate? */
538 if (list_cert->equals(list_cert, cert))
539 {
540 last = FALSE;
541 break;
542 }
543 /* group certificates with same issuer */
544 previous_same = same;
545 same = list_cert->has_issuer(list_cert, issuer);
546 if (previous_same && !same)
547 {
548 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
549 last = FALSE;
550 break;
551 }
552 }
553 iterator->destroy(iterator);
554
555 if (last)
556 {
557 list->insert_last(list, (void *)cert->get_ref(cert));
558 }
559 }
560 enumerator->destroy(enumerator);
561 return list;
562 }
563
564 /**
565 * list all raw public keys
566 */
567 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
568 {
569 bool first = TRUE;
570
571 enumerator_t *enumerator = list->create_enumerator(list);
572 certificate_t *cert;
573
574 while (enumerator->enumerate(enumerator, (void**)&cert))
575 {
576 public_key_t *public = cert->get_public_key(cert);
577
578 if (public)
579 {
580 private_key_t *private = NULL;
581 identification_t *id, *keyid;
582
583 if (first)
584 {
585 fprintf(out, "\n");
586 fprintf(out, "List of Raw Public Keys:\n");
587 first = FALSE;
588 }
589 fprintf(out, "\n");
590
591 /* list public key information */
592 id = public->get_id(public, ID_PUBKEY_SHA1);
593 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
594
595 private = charon->credentials->get_private(
596 charon->credentials,
597 public->get_type(public), keyid, NULL);
598 fprintf(out, " pubkey: %N %d bits%s\n",
599 key_type_names, public->get_type(public),
600 public->get_keysize(public) * 8,
601 private ? ", has private key" : "");
602 fprintf(out, " keyid: %Y\n", keyid);
603 fprintf(out, " subjkey: %Y\n", id);
604 DESTROY_IF(private);
605 public->destroy(public);
606 }
607 }
608 enumerator->destroy(enumerator);
609 }
610
611 /**
612 * list all X.509 certificates matching the flags
613 */
614 static void stroke_list_certs(linked_list_t *list, char *label,
615 x509_flag_t flags, bool utc, FILE *out)
616 {
617 bool first = TRUE;
618 time_t now = time(NULL);
619 enumerator_t *enumerator = list->create_enumerator(list);
620 certificate_t *cert;
621
622 while (enumerator->enumerate(enumerator, (void**)&cert))
623 {
624 x509_t *x509 = (x509_t*)cert;
625 x509_flag_t x509_flags = x509->get_flags(x509);
626
627 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
628 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
629 {
630 enumerator_t *enumerator;
631 identification_t *altName;
632 bool first_altName = TRUE;
633 chunk_t serial = x509->get_serial(x509);
634 identification_t *authkey = x509->get_authKeyIdentifier(x509);
635 time_t notBefore, notAfter;
636 public_key_t *public = cert->get_public_key(cert);
637
638 if (first)
639 {
640 fprintf(out, "\n");
641 fprintf(out, "List of %s:\n", label);
642 first = FALSE;
643 }
644 fprintf(out, "\n");
645
646 /* list subjectAltNames */
647 enumerator = x509->create_subjectAltName_enumerator(x509);
648 while (enumerator->enumerate(enumerator, (void**)&altName))
649 {
650 if (first_altName)
651 {
652 fprintf(out, " altNames: ");
653 first_altName = FALSE;
654 }
655 else
656 {
657 fprintf(out, ", ");
658 }
659 fprintf(out, "%Y", altName);
660 }
661 if (!first_altName)
662 {
663 fprintf(out, "\n");
664 }
665 enumerator->destroy(enumerator);
666
667 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
668 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
669 fprintf(out, " serial: %#B\n", &serial);
670
671 /* list validity */
672 cert->get_validity(cert, &now, &notBefore, &notAfter);
673 fprintf(out, " validity: not before %T, ", &notBefore, utc);
674 if (now < notBefore)
675 {
676 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
677 }
678 else
679 {
680 fprintf(out, "ok\n");
681 }
682 fprintf(out, " not after %T, ", &notAfter, utc);
683 if (now > notAfter)
684 {
685 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
686 }
687 else
688 {
689 fprintf(out, "ok");
690 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
691 {
692 fprintf(out, " (expires in %V)", &now, &notAfter);
693 }
694 fprintf(out, " \n");
695 }
696
697 /* list public key information */
698 if (public)
699 {
700 private_key_t *private = NULL;
701 identification_t *id, *keyid;
702
703 id = public->get_id(public, ID_PUBKEY_SHA1);
704 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
705
706 private = charon->credentials->get_private(
707 charon->credentials,
708 public->get_type(public), keyid, NULL);
709 fprintf(out, " pubkey: %N %d bits%s\n",
710 key_type_names, public->get_type(public),
711 public->get_keysize(public) * 8,
712 private ? ", has private key" : "");
713 fprintf(out, " keyid: %Y\n", keyid);
714 fprintf(out, " subjkey: %Y\n", id);
715 DESTROY_IF(private);
716 public->destroy(public);
717 }
718
719 /* list optional authorityKeyIdentifier */
720 if (authkey)
721 {
722 fprintf(out, " authkey: %Y\n", authkey);
723 }
724 }
725 }
726 enumerator->destroy(enumerator);
727 }
728
729 /**
730 * list all X.509 attribute certificates
731 */
732 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
733 {
734 bool first = TRUE;
735 time_t thisUpdate, nextUpdate, now = time(NULL);
736 enumerator_t *enumerator = list->create_enumerator(list);
737 certificate_t *cert;
738
739 while (enumerator->enumerate(enumerator, (void**)&cert))
740 {
741 ac_t *ac = (ac_t*)cert;
742 chunk_t serial = ac->get_serial(ac);
743 chunk_t holderSerial = ac->get_holderSerial(ac);
744 identification_t *holderIssuer = ac->get_holderIssuer(ac);
745 identification_t *authkey = ac->get_authKeyIdentifier(ac);
746 identification_t *entityName = cert->get_subject(cert);
747
748 if (first)
749 {
750 fprintf(out, "\n");
751 fprintf(out, "List of X.509 Attribute Certificates:\n");
752 first = FALSE;
753 }
754 fprintf(out, "\n");
755
756 if (entityName)
757 {
758 fprintf(out, " holder: \"%Y\"\n", entityName);
759 }
760 if (holderIssuer)
761 {
762 fprintf(out, " hissuer: \"%Y\"\n", holderIssuer);
763 }
764 if (holderSerial.ptr)
765 {
766 fprintf(out, " hserial: %#B\n", &holderSerial);
767 }
768 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
769 fprintf(out, " serial: %#B\n", &serial);
770
771 /* list validity */
772 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
773 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
774 fprintf(out, " next %T, ", &nextUpdate, utc);
775 if (now > nextUpdate)
776 {
777 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
778 }
779 else
780 {
781 fprintf(out, "ok");
782 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
783 {
784 fprintf(out, " (expires in %V)", &now, &nextUpdate);
785 }
786 fprintf(out, " \n");
787 }
788
789 /* list optional authorityKeyIdentifier */
790 if (authkey)
791 {
792 fprintf(out, " authkey: %Y\n", authkey);
793 }
794 }
795 enumerator->destroy(enumerator);
796 }
797
798 /**
799 * list all X.509 CRLs
800 */
801 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
802 {
803 bool first = TRUE;
804 time_t thisUpdate, nextUpdate, now = time(NULL);
805 enumerator_t *enumerator = list->create_enumerator(list);
806 certificate_t *cert;
807
808 while (enumerator->enumerate(enumerator, (void**)&cert))
809 {
810 crl_t *crl = (crl_t*)cert;
811 chunk_t serial = crl->get_serial(crl);
812 identification_t *authkey = crl->get_authKeyIdentifier(crl);
813
814 if (first)
815 {
816 fprintf(out, "\n");
817 fprintf(out, "List of X.509 CRLs:\n");
818 first = FALSE;
819 }
820 fprintf(out, "\n");
821
822 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
823
824 /* list optional crlNumber */
825 if (serial.ptr)
826 {
827 fprintf(out, " serial: %#B\n", &serial);
828 }
829
830 /* count the number of revoked certificates */
831 {
832 int count = 0;
833 enumerator_t *enumerator = crl->create_enumerator(crl);
834
835 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
836 {
837 count++;
838 }
839 fprintf(out, " revoked: %d certificate%s\n", count,
840 (count == 1)? "" : "s");
841 enumerator->destroy(enumerator);
842 }
843
844 /* list validity */
845 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
846 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
847 fprintf(out, " next %T, ", &nextUpdate, utc);
848 if (now > nextUpdate)
849 {
850 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
851 }
852 else
853 {
854 fprintf(out, "ok");
855 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
856 {
857 fprintf(out, " (expires in %V)", &now, &nextUpdate);
858 }
859 fprintf(out, " \n");
860 }
861
862 /* list optional authorityKeyIdentifier */
863 if (authkey)
864 {
865 fprintf(out, " authkey: %Y\n", authkey);
866 }
867 }
868 enumerator->destroy(enumerator);
869 }
870
871 /**
872 * list all OCSP responses
873 */
874 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
875 {
876 bool first = TRUE;
877 enumerator_t *enumerator = list->create_enumerator(list);
878 certificate_t *cert;
879
880 while (enumerator->enumerate(enumerator, (void**)&cert))
881 {
882 if (first)
883 {
884 fprintf(out, "\n");
885 fprintf(out, "List of OCSP responses:\n");
886 fprintf(out, "\n");
887 first = FALSE;
888 }
889
890 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
891 }
892 enumerator->destroy(enumerator);
893 }
894
895 /**
896 * List of registered cryptographical algorithms
897 */
898 static void list_algs(FILE *out)
899 {
900 enumerator_t *enumerator;
901 encryption_algorithm_t encryption;
902 integrity_algorithm_t integrity;
903 hash_algorithm_t hash;
904 pseudo_random_function_t prf;
905 diffie_hellman_group_t group;
906
907 fprintf(out, "\n");
908 fprintf(out, "List of registered IKEv2 Algorithms:\n");
909 fprintf(out, "\n encryption: ");
910 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
911 while (enumerator->enumerate(enumerator, &encryption))
912 {
913 fprintf(out, "%N ", encryption_algorithm_names, encryption);
914 }
915 enumerator->destroy(enumerator);
916 fprintf(out, "\n integrity: ");
917 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
918 while (enumerator->enumerate(enumerator, &integrity))
919 {
920 fprintf(out, "%N ", integrity_algorithm_names, integrity);
921 }
922 enumerator->destroy(enumerator);
923 fprintf(out, "\n hasher: ");
924 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
925 while (enumerator->enumerate(enumerator, &hash))
926 {
927 fprintf(out, "%N ", hash_algorithm_names, hash);
928 }
929 enumerator->destroy(enumerator);
930 fprintf(out, "\n prf: ");
931 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
932 while (enumerator->enumerate(enumerator, &prf))
933 {
934 fprintf(out, "%N ", pseudo_random_function_names, prf);
935 }
936 enumerator->destroy(enumerator);
937 fprintf(out, "\n dh-group: ");
938 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
939 while (enumerator->enumerate(enumerator, &group))
940 {
941 fprintf(out, "%N ", diffie_hellman_group_names, group);
942 }
943 enumerator->destroy(enumerator);
944 fprintf(out, "\n");
945 }
946
947 /**
948 * Implementation of stroke_list_t.list.
949 */
950 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
951 {
952 linked_list_t *cert_list = NULL;
953
954 if (msg->list.flags & LIST_PUBKEYS)
955 {
956 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
957
958 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
959 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
960 }
961 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
962 {
963 cert_list = create_unique_cert_list(CERT_X509);
964 }
965 if (msg->list.flags & LIST_CERTS)
966 {
967 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
968 0, msg->list.utc, out);
969 }
970 if (msg->list.flags & LIST_CACERTS)
971 {
972 stroke_list_certs(cert_list, "X.509 CA Certificates",
973 X509_CA, msg->list.utc, out);
974 }
975 if (msg->list.flags & LIST_OCSPCERTS)
976 {
977 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
978 X509_OCSP_SIGNER, msg->list.utc, out);
979 }
980 if (msg->list.flags & LIST_AACERTS)
981 {
982 stroke_list_certs(cert_list, "X.509 AA Certificates",
983 X509_AA, msg->list.utc, out);
984 }
985 if (msg->list.flags & LIST_ACERTS)
986 {
987 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
988
989 stroke_list_acerts(ac_list, msg->list.utc, out);
990 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
991 }
992 if (msg->list.flags & LIST_CRLS)
993 {
994 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
995
996 stroke_list_crls(crl_list, msg->list.utc, out);
997 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
998 }
999 if (msg->list.flags & LIST_OCSP)
1000 {
1001 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1002
1003 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1004
1005 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1006 }
1007 if (msg->list.flags & LIST_ALGS)
1008 {
1009 list_algs(out);
1010 }
1011 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1012 }
1013
1014 /**
1015 * Print leases of a single pool
1016 */
1017 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1018 host_t *address, u_int size, u_int online, u_int offline)
1019 {
1020 enumerator_t *enumerator;
1021 identification_t *id;
1022 host_t *lease;
1023 bool on;
1024 int found = 0;
1025
1026 fprintf(out, "Leases in pool '%s', usage: %lu/%lu, %lu online\n",
1027 pool, online + offline, size, online);
1028 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1029 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1030 {
1031 if (!address || address->ip_equals(address, lease))
1032 {
1033 fprintf(out, " %15H %s '%Y'\n",
1034 lease, on ? "online" : "offline", id);
1035 found++;
1036 }
1037 }
1038 enumerator->destroy(enumerator);
1039 if (!found)
1040 {
1041 fprintf(out, " no matching leases found\n");
1042 }
1043 }
1044
1045 /**
1046 * Implementation of stroke_list_t.leases
1047 */
1048 static void leases(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1049 {
1050 enumerator_t *enumerator;
1051 u_int size, offline, online;
1052 host_t *address = NULL;
1053 char *pool;
1054 int found = 0;
1055
1056 if (msg->leases.address)
1057 {
1058 address = host_create_from_string(msg->leases.address, 0);
1059 }
1060
1061 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1062 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1063 {
1064 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1065 {
1066 pool_leases(this, out, pool, address, size, online, offline);
1067 found++;
1068 }
1069 }
1070 enumerator->destroy(enumerator);
1071 if (!found)
1072 {
1073 if (msg->leases.pool)
1074 {
1075 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1076 }
1077 else
1078 {
1079 fprintf(out, "no pools found\n");
1080 }
1081 }
1082 DESTROY_IF(address);
1083 }
1084
1085 /**
1086 * Implementation of stroke_list_t.destroy
1087 */
1088 static void destroy(private_stroke_list_t *this)
1089 {
1090 free(this);
1091 }
1092
1093 /*
1094 * see header file
1095 */
1096 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1097 {
1098 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
1099
1100 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
1101 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
1102 this->public.leases = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))leases;
1103 this->public.destroy = (void(*)(stroke_list_t*))destroy;
1104
1105 this->uptime = time(NULL);
1106 this->attribute = attribute;
1107
1108 return &this->public;
1109 }
1110