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