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