fixed a 64-bit issue with time_t printf hooks
[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 uptime = time(NULL) - this->uptime;
269 bool first = TRUE;
270 u_int size, online, offline;
271
272 fprintf(out, "Performance:\n");
273 fprintf(out, " uptime: %V, since %#T\n", &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 (first)
294 {
295 first = FALSE;
296 fprintf(out, "Virtual IP pools (size/online/offline):\n");
297 }
298 fprintf(out, " %s: %lu/%lu/%lu\n", pool, size, online, offline);
299 }
300 enumerator->destroy(enumerator);
301
302 enumerator = charon->kernel_interface->create_address_enumerator(
303 charon->kernel_interface, FALSE, FALSE);
304 fprintf(out, "Listening IP addresses:\n");
305 while (enumerator->enumerate(enumerator, (void**)&host))
306 {
307 fprintf(out, " %H\n", host);
308 }
309 enumerator->destroy(enumerator);
310
311 fprintf(out, "Connections:\n");
312 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
313 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
314 {
315 void *ptr;
316 certificate_t *cert;
317 auth_item_t item;
318 auth_info_t *auth;
319 enumerator_t *auth_enumerator;
320 identification_t *my_ca = NULL, *other_ca = NULL;
321 identification_t *eap_identity = NULL;
322 u_int32_t *eap_type = NULL;
323 bool ac_groups = FALSE;
324
325 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
326 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
327 {
328 continue;
329 }
330
331 /* determine any required CAs, EAP type, EAP identity,
332 * and the presence of AC groups
333 */
334 auth = peer_cfg->get_auth(peer_cfg);
335 auth_enumerator = auth->create_item_enumerator(auth);
336 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
337 {
338 switch (item)
339 {
340 case AUTHN_EAP_TYPE:
341 eap_type = (u_int32_t *)ptr;
342 break;
343 case AUTHN_EAP_IDENTITY:
344 eap_identity = (identification_t *)ptr;
345 break;
346 case AUTHN_CA_CERT:
347 cert = (certificate_t *)ptr;
348 my_ca = cert->get_subject(cert);
349 break;
350 case AUTHN_CA_CERT_NAME:
351 my_ca = (identification_t *)ptr;
352 break;
353 case AUTHZ_CA_CERT:
354 cert = (certificate_t *)ptr;
355 other_ca = cert->get_subject(cert);
356 break;
357 case AUTHZ_CA_CERT_NAME:
358 other_ca = (identification_t *)ptr;
359 break;
360 case AUTHZ_AC_GROUP:
361 ac_groups = TRUE;
362 break;
363 default:
364 break;
365 }
366 }
367 auth_enumerator->destroy(auth_enumerator);
368
369 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
370 fprintf(out, "%12s: %s[%D]...%s[%D]\n", peer_cfg->get_name(peer_cfg),
371 ike_cfg->get_my_addr(ike_cfg), peer_cfg->get_my_id(peer_cfg),
372 ike_cfg->get_other_addr(ike_cfg), peer_cfg->get_other_id(peer_cfg));
373 if (my_ca || other_ca)
374 {
375 fprintf(out, "%12s: CAs: ", peer_cfg->get_name(peer_cfg));
376 if (my_ca)
377 {
378 fprintf(out, "\"%D\"...", my_ca);
379 }
380 else
381 {
382 fprintf(out, "%%any...");
383 }
384 if (other_ca)
385 {
386 fprintf(out, "\"%D\"\n", other_ca);
387 }
388 else
389 {
390 fprintf(out, "%%any\n");
391 }
392 }
393
394 if (ac_groups)
395 {
396 bool first = TRUE;
397
398 fprintf(out, "%12s: groups: ", peer_cfg->get_name(peer_cfg));
399 auth_enumerator = auth->create_item_enumerator(auth);
400 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
401 {
402 if (item == AUTHZ_AC_GROUP)
403 {
404 identification_t *group = (identification_t *)ptr;
405
406 fprintf(out, "%s%D", first? "":", ", group);
407 first = FALSE;
408 }
409 }
410 auth_enumerator->destroy(auth_enumerator);
411 fprintf(out, "\n");
412 }
413
414 fprintf(out, "%12s: %N ", peer_cfg->get_name(peer_cfg),
415 auth_class_names, get_auth_class(peer_cfg));
416 if (eap_type)
417 {
418 fprintf(out, "and %N ", eap_type_names, *eap_type);
419 }
420 fprintf(out, "authentication");
421 if (eap_identity)
422 {
423 fprintf(out, ", EAP identity: '%D'", eap_identity);
424 }
425 dpd = peer_cfg->get_dpd(peer_cfg);
426 if (dpd)
427 {
428 fprintf(out, ", dpddelay=%us", dpd);
429 }
430 fprintf(out, "\n");
431
432 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
433 while (children->enumerate(children, &child_cfg))
434 {
435 linked_list_t *my_ts, *other_ts;
436
437 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
438 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
439 fprintf(out, "%12s: %#R=== %#R", child_cfg->get_name(child_cfg),
440 my_ts, other_ts);
441 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
442 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
443
444 if (dpd)
445 {
446 fprintf(out, ", dpdaction=%N", action_names,
447 child_cfg->get_dpd_action(child_cfg));
448 }
449 fprintf(out, "\n");
450 }
451 children->destroy(children);
452 }
453 enumerator->destroy(enumerator);
454 }
455
456 fprintf(out, "Security Associations:\n");
457 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
458 while (enumerator->enumerate(enumerator, &ike_sa))
459 {
460 bool ike_printed = FALSE;
461 child_sa_t *child_sa;
462 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
463
464 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
465 {
466 log_ike_sa(out, ike_sa, all);
467 found = TRUE;
468 ike_printed = TRUE;
469 }
470
471 while (children->iterate(children, (void**)&child_sa))
472 {
473 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
474 {
475 if (!ike_printed)
476 {
477 log_ike_sa(out, ike_sa, all);
478 found = TRUE;
479 ike_printed = TRUE;
480 }
481 log_child_sa(out, child_sa, all);
482 }
483 }
484 children->destroy(children);
485 }
486 enumerator->destroy(enumerator);
487
488 if (!found)
489 {
490 if (name)
491 {
492 fprintf(out, " no match\n");
493 }
494 else
495 {
496 fprintf(out, " none\n");
497 }
498 }
499 }
500
501 /**
502 * create a unique certificate list without duplicates
503 * certicates having the same issuer are grouped together.
504 */
505 static linked_list_t* create_unique_cert_list(certificate_type_t type)
506 {
507 linked_list_t *list = linked_list_create();
508 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
509 charon->credentials, type, KEY_ANY,
510 NULL, FALSE);
511 certificate_t *cert;
512
513 while (enumerator->enumerate(enumerator, (void**)&cert))
514 {
515 iterator_t *iterator = list->create_iterator(list, TRUE);
516 identification_t *issuer = cert->get_issuer(cert);
517 bool previous_same, same = FALSE, last = TRUE;
518 certificate_t *list_cert;
519
520 while (iterator->iterate(iterator, (void**)&list_cert))
521 {
522 /* exit if we have a duplicate? */
523 if (list_cert->equals(list_cert, cert))
524 {
525 last = FALSE;
526 break;
527 }
528 /* group certificates with same issuer */
529 previous_same = same;
530 same = list_cert->has_issuer(list_cert, issuer);
531 if (previous_same && !same)
532 {
533 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
534 last = FALSE;
535 break;
536 }
537 }
538 iterator->destroy(iterator);
539
540 if (last)
541 {
542 list->insert_last(list, (void *)cert->get_ref(cert));
543 }
544 }
545 enumerator->destroy(enumerator);
546 return list;
547 }
548
549 /**
550 * list all raw public keys
551 */
552 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
553 {
554 bool first = TRUE;
555
556 enumerator_t *enumerator = list->create_enumerator(list);
557 certificate_t *cert;
558
559 while (enumerator->enumerate(enumerator, (void**)&cert))
560 {
561 public_key_t *public = cert->get_public_key(cert);
562
563 if (public)
564 {
565 private_key_t *private = NULL;
566 identification_t *id, *keyid;
567
568 if (first)
569 {
570 fprintf(out, "\n");
571 fprintf(out, "List of Raw Public Keys:\n");
572 first = FALSE;
573 }
574 fprintf(out, "\n");
575
576 /* list public key information */
577 id = public->get_id(public, ID_PUBKEY_SHA1);
578 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
579
580 private = charon->credentials->get_private(
581 charon->credentials,
582 public->get_type(public), keyid, NULL);
583 fprintf(out, " pubkey: %N %d bits%s\n",
584 key_type_names, public->get_type(public),
585 public->get_keysize(public) * 8,
586 private ? ", has private key" : "");
587 fprintf(out, " keyid: %D\n", keyid);
588 fprintf(out, " subjkey: %D\n", id);
589 DESTROY_IF(private);
590 public->destroy(public);
591 }
592 }
593 enumerator->destroy(enumerator);
594 }
595
596 /**
597 * list all X.509 certificates matching the flags
598 */
599 static void stroke_list_certs(linked_list_t *list, char *label,
600 x509_flag_t flags, bool utc, FILE *out)
601 {
602 bool first = TRUE;
603 time_t now = time(NULL);
604 enumerator_t *enumerator = list->create_enumerator(list);
605 certificate_t *cert;
606
607 while (enumerator->enumerate(enumerator, (void**)&cert))
608 {
609 x509_t *x509 = (x509_t*)cert;
610 x509_flag_t x509_flags = x509->get_flags(x509);
611
612 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
613 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
614 {
615 enumerator_t *enumerator;
616 identification_t *altName;
617 bool first_altName = TRUE;
618 chunk_t serial = x509->get_serial(x509);
619 identification_t *authkey = x509->get_authKeyIdentifier(x509);
620 time_t notBefore, notAfter;
621 public_key_t *public = cert->get_public_key(cert);
622
623 if (first)
624 {
625 fprintf(out, "\n");
626 fprintf(out, "List of %s:\n", label);
627 first = FALSE;
628 }
629 fprintf(out, "\n");
630
631 /* list subjectAltNames */
632 enumerator = x509->create_subjectAltName_enumerator(x509);
633 while (enumerator->enumerate(enumerator, (void**)&altName))
634 {
635 if (first_altName)
636 {
637 fprintf(out, " altNames: ");
638 first_altName = FALSE;
639 }
640 else
641 {
642 fprintf(out, ", ");
643 }
644 fprintf(out, "%D", altName);
645 }
646 if (!first_altName)
647 {
648 fprintf(out, "\n");
649 }
650 enumerator->destroy(enumerator);
651
652 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
653 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
654 fprintf(out, " serial: %#B\n", &serial);
655
656 /* list validity */
657 cert->get_validity(cert, &now, &notBefore, &notAfter);
658 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
659 if (now < notBefore)
660 {
661 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
662 }
663 else
664 {
665 fprintf(out, "ok\n");
666 }
667 fprintf(out, " not after %#T, ", &notAfter, utc);
668 if (now > notAfter)
669 {
670 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
671 }
672 else
673 {
674 fprintf(out, "ok");
675 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
676 {
677 fprintf(out, " (expires in %#V)", &now, &notAfter);
678 }
679 fprintf(out, " \n");
680 }
681
682 /* list public key information */
683 if (public)
684 {
685 private_key_t *private = NULL;
686 identification_t *id, *keyid;
687
688 id = public->get_id(public, ID_PUBKEY_SHA1);
689 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
690
691 private = charon->credentials->get_private(
692 charon->credentials,
693 public->get_type(public), keyid, NULL);
694 fprintf(out, " pubkey: %N %d bits%s\n",
695 key_type_names, public->get_type(public),
696 public->get_keysize(public) * 8,
697 private ? ", has private key" : "");
698 fprintf(out, " keyid: %D\n", keyid);
699 fprintf(out, " subjkey: %D\n", id);
700 DESTROY_IF(private);
701 public->destroy(public);
702 }
703
704 /* list optional authorityKeyIdentifier */
705 if (authkey)
706 {
707 fprintf(out, " authkey: %D\n", authkey);
708 }
709 }
710 }
711 enumerator->destroy(enumerator);
712 }
713
714 /**
715 * list all X.509 attribute certificates
716 */
717 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
718 {
719 bool first = TRUE;
720 time_t thisUpdate, nextUpdate, now = time(NULL);
721 enumerator_t *enumerator = list->create_enumerator(list);
722 certificate_t *cert;
723
724 while (enumerator->enumerate(enumerator, (void**)&cert))
725 {
726 ac_t *ac = (ac_t*)cert;
727 chunk_t serial = ac->get_serial(ac);
728 chunk_t holderSerial = ac->get_holderSerial(ac);
729 identification_t *holderIssuer = ac->get_holderIssuer(ac);
730 identification_t *authkey = ac->get_authKeyIdentifier(ac);
731 identification_t *entityName = cert->get_subject(cert);
732
733 if (first)
734 {
735 fprintf(out, "\n");
736 fprintf(out, "List of X.509 Attribute Certificates:\n");
737 first = FALSE;
738 }
739 fprintf(out, "\n");
740
741 if (entityName)
742 {
743 fprintf(out, " holder: \"%D\"\n", entityName);
744 }
745 if (holderIssuer)
746 {
747 fprintf(out, " hissuer: \"%D\"\n", holderIssuer);
748 }
749 if (holderSerial.ptr)
750 {
751 fprintf(out, " hserial: %#B\n", &holderSerial);
752 }
753 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
754 fprintf(out, " serial: %#B\n", &serial);
755
756 /* list validity */
757 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
758 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
759 fprintf(out, " next %#T, ", &nextUpdate, utc);
760 if (now > nextUpdate)
761 {
762 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
763 }
764 else
765 {
766 fprintf(out, "ok");
767 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
768 {
769 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
770 }
771 fprintf(out, " \n");
772 }
773
774 /* list optional authorityKeyIdentifier */
775 if (authkey)
776 {
777 fprintf(out, " authkey: %D\n", authkey);
778 }
779 }
780 enumerator->destroy(enumerator);
781 }
782
783 /**
784 * list all X.509 CRLs
785 */
786 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
787 {
788 bool first = TRUE;
789 time_t thisUpdate, nextUpdate, now = time(NULL);
790 enumerator_t *enumerator = list->create_enumerator(list);
791 certificate_t *cert;
792
793 while (enumerator->enumerate(enumerator, (void**)&cert))
794 {
795 crl_t *crl = (crl_t*)cert;
796 chunk_t serial = crl->get_serial(crl);
797 identification_t *authkey = crl->get_authKeyIdentifier(crl);
798
799 if (first)
800 {
801 fprintf(out, "\n");
802 fprintf(out, "List of X.509 CRLs:\n");
803 first = FALSE;
804 }
805 fprintf(out, "\n");
806
807 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
808
809 /* list optional crlNumber */
810 if (serial.ptr)
811 {
812 fprintf(out, " serial: %#B\n", &serial);
813 }
814
815 /* count the number of revoked certificates */
816 {
817 int count = 0;
818 enumerator_t *enumerator = crl->create_enumerator(crl);
819
820 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
821 {
822 count++;
823 }
824 fprintf(out, " revoked: %d certificate%s\n", count,
825 (count == 1)? "" : "s");
826 enumerator->destroy(enumerator);
827 }
828
829 /* list validity */
830 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
831 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
832 fprintf(out, " next %#T, ", &nextUpdate, utc);
833 if (now > nextUpdate)
834 {
835 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
836 }
837 else
838 {
839 fprintf(out, "ok");
840 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
841 {
842 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
843 }
844 fprintf(out, " \n");
845 }
846
847 /* list optional authorityKeyIdentifier */
848 if (authkey)
849 {
850 fprintf(out, " authkey: %D\n", authkey);
851 }
852 }
853 enumerator->destroy(enumerator);
854 }
855
856 /**
857 * list all OCSP responses
858 */
859 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
860 {
861 bool first = TRUE;
862 enumerator_t *enumerator = list->create_enumerator(list);
863 certificate_t *cert;
864
865 while (enumerator->enumerate(enumerator, (void**)&cert))
866 {
867 if (first)
868 {
869 fprintf(out, "\n");
870 fprintf(out, "List of OCSP responses:\n");
871 fprintf(out, "\n");
872 first = FALSE;
873 }
874
875 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
876 }
877 enumerator->destroy(enumerator);
878 }
879
880 /**
881 * List of registered cryptographical algorithms
882 */
883 static void list_algs(FILE *out)
884 {
885 enumerator_t *enumerator;
886 encryption_algorithm_t encryption;
887 integrity_algorithm_t integrity;
888 hash_algorithm_t hash;
889 pseudo_random_function_t prf;
890 diffie_hellman_group_t group;
891
892 fprintf(out, "\n");
893 fprintf(out, "List of registered IKEv2 Algorithms:\n");
894 fprintf(out, "\n encryption: ");
895 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
896 while (enumerator->enumerate(enumerator, &encryption))
897 {
898 fprintf(out, "%N ", encryption_algorithm_names, encryption);
899 }
900 enumerator->destroy(enumerator);
901 fprintf(out, "\n integrity: ");
902 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
903 while (enumerator->enumerate(enumerator, &integrity))
904 {
905 fprintf(out, "%N ", integrity_algorithm_names, integrity);
906 }
907 enumerator->destroy(enumerator);
908 fprintf(out, "\n hasher: ");
909 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
910 while (enumerator->enumerate(enumerator, &hash))
911 {
912 fprintf(out, "%N ", hash_algorithm_names, hash);
913 }
914 enumerator->destroy(enumerator);
915 fprintf(out, "\n prf: ");
916 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
917 while (enumerator->enumerate(enumerator, &prf))
918 {
919 fprintf(out, "%N ", pseudo_random_function_names, prf);
920 }
921 enumerator->destroy(enumerator);
922 fprintf(out, "\n dh-group: ");
923 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
924 while (enumerator->enumerate(enumerator, &group))
925 {
926 fprintf(out, "%N ", diffie_hellman_group_names, group);
927 }
928 enumerator->destroy(enumerator);
929 fprintf(out, "\n");
930 }
931
932 /**
933 * Implementation of stroke_list_t.list.
934 */
935 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
936 {
937 linked_list_t *cert_list = NULL;
938
939 if (msg->list.flags & LIST_PUBKEYS)
940 {
941 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
942
943 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
944 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
945 }
946 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
947 {
948 cert_list = create_unique_cert_list(CERT_X509);
949 }
950 if (msg->list.flags & LIST_CERTS)
951 {
952 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
953 0, msg->list.utc, out);
954 }
955 if (msg->list.flags & LIST_CACERTS)
956 {
957 stroke_list_certs(cert_list, "X.509 CA Certificates",
958 X509_CA, msg->list.utc, out);
959 }
960 if (msg->list.flags & LIST_OCSPCERTS)
961 {
962 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
963 X509_OCSP_SIGNER, msg->list.utc, out);
964 }
965 if (msg->list.flags & LIST_AACERTS)
966 {
967 stroke_list_certs(cert_list, "X.509 AA Certificates",
968 X509_AA, msg->list.utc, out);
969 }
970 if (msg->list.flags & LIST_ACERTS)
971 {
972 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
973
974 stroke_list_acerts(ac_list, msg->list.utc, out);
975 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
976 }
977 if (msg->list.flags & LIST_CRLS)
978 {
979 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
980
981 stroke_list_crls(crl_list, msg->list.utc, out);
982 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
983 }
984 if (msg->list.flags & LIST_OCSP)
985 {
986 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
987
988 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
989
990 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
991 }
992 if (msg->list.flags & LIST_ALGS)
993 {
994 list_algs(out);
995 }
996 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
997 }
998
999 /**
1000 * Print leases of a single pool
1001 */
1002 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1003 host_t *address, u_int size, u_int online, u_int offline)
1004 {
1005 enumerator_t *enumerator;
1006 identification_t *id;
1007 host_t *lease;
1008 bool on;
1009 int found = 0;
1010
1011 fprintf(out, "Leases in pool '%s', usage: %lu/%lu, %lu online\n",
1012 pool, online + offline, size, online);
1013 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1014 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1015 {
1016 if (!address || address->ip_equals(address, lease))
1017 {
1018 fprintf(out, " %15H %s '%D'\n",
1019 lease, on ? "online" : "offline", id);
1020 found++;
1021 }
1022 }
1023 enumerator->destroy(enumerator);
1024 if (!found)
1025 {
1026 fprintf(out, " no matching leases found\n");
1027 }
1028 }
1029
1030 /**
1031 * Implementation of stroke_list_t.leases
1032 */
1033 static void leases(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1034 {
1035 enumerator_t *enumerator;
1036 u_int size, offline, online;
1037 host_t *address = NULL;
1038 char *pool;
1039 int found = 0;
1040
1041 if (msg->leases.address)
1042 {
1043 address = host_create_from_string(msg->leases.address, 0);
1044 }
1045
1046 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1047 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1048 {
1049 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1050 {
1051 pool_leases(this, out, pool, address, size, online, offline);
1052 found++;
1053 }
1054 }
1055 enumerator->destroy(enumerator);
1056 if (!found)
1057 {
1058 if (msg->leases.pool)
1059 {
1060 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1061 }
1062 else
1063 {
1064 fprintf(out, "no pools found\n");
1065 }
1066 }
1067 DESTROY_IF(address);
1068 }
1069
1070 /**
1071 * Implementation of stroke_list_t.destroy
1072 */
1073 static void destroy(private_stroke_list_t *this)
1074 {
1075 free(this);
1076 }
1077
1078 /*
1079 * see header file
1080 */
1081 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1082 {
1083 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
1084
1085 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
1086 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
1087 this->public.leases = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))leases;
1088 this->public.destroy = (void(*)(stroke_list_t*))destroy;
1089
1090 this->uptime = time(NULL);
1091 this->attribute = attribute;
1092
1093 return &this->public;
1094 }
1095