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