libhydra: Move kernel interface to libcharon
[strongswan.git] / src / libcharon / plugins / stroke / stroke_list.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2015 Andreas Steffen
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "stroke_list.h"
20
21 #include <inttypes.h>
22 #include <time.h>
23 #include <sys/utsname.h>
24
25 #ifdef HAVE_MALLINFO
26 #include <malloc.h>
27 #endif /* HAVE_MALLINFO */
28
29 #include <daemon.h>
30 #include <collections/linked_list.h>
31 #include <plugins/plugin.h>
32 #include <credentials/certificates/x509.h>
33 #include <credentials/certificates/certificate_printer.h>
34 #include <config/peer_cfg.h>
35
36 typedef struct private_stroke_list_t private_stroke_list_t;
37
38 /**
39 * private data of stroke_list
40 */
41 struct private_stroke_list_t {
42
43 /**
44 * public functions
45 */
46 stroke_list_t public;
47
48 /**
49 * Kind of *swan we run
50 */
51 char *swan;
52
53 /**
54 * timestamp of daemon start
55 */
56 time_t uptime;
57
58 /**
59 * strokes attribute provider
60 */
61 stroke_attribute_t *attribute;
62 };
63
64 /**
65 * Static certificate printer object
66 */
67 static certificate_printer_t *cert_printer = NULL;
68
69 /**
70 * Log tasks of a specific queue to out
71 */
72 static void log_task_q(FILE *out, ike_sa_t *ike_sa, task_queue_t q, char *name)
73 {
74 enumerator_t *enumerator;
75 bool has = FALSE;
76 task_t *task;
77
78 enumerator = ike_sa->create_task_enumerator(ike_sa, q);
79 while (enumerator->enumerate(enumerator, &task))
80 {
81 if (!has)
82 {
83 fprintf(out, "%12s[%d]: Tasks %s: ", ike_sa->get_name(ike_sa),
84 ike_sa->get_unique_id(ike_sa), name);
85 has = TRUE;
86 }
87 fprintf(out, "%N ", task_type_names, task->get_type(task));
88 }
89 enumerator->destroy(enumerator);
90 if (has)
91 {
92 fprintf(out, "\n");
93 }
94 }
95
96 /**
97 * log an IKE_SA to out
98 */
99 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
100 {
101 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
102 time_t now = time_monotonic(NULL);
103
104 fprintf(out, "%12s[%d]: %N",
105 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
106 ike_sa_state_names, ike_sa->get_state(ike_sa));
107
108 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
109 {
110 time_t established;
111
112 established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
113 fprintf(out, " %V ago", &now, &established);
114 }
115
116 fprintf(out, ", %H[%Y]...%H[%Y]\n",
117 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
118 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
119
120 if (all)
121 {
122 proposal_t *ike_proposal;
123 identification_t *eap_id;
124
125 eap_id = ike_sa->get_other_eap_id(ike_sa);
126
127 if (!eap_id->equals(eap_id, ike_sa->get_other_id(ike_sa)))
128 {
129 fprintf(out, "%12s[%d]: Remote %s identity: %Y\n",
130 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
131 ike_sa->get_version(ike_sa) == IKEV1 ? "XAuth" : "EAP",
132 eap_id);
133 }
134
135 ike_proposal = ike_sa->get_proposal(ike_sa);
136
137 fprintf(out, "%12s[%d]: %N SPIs: %.16"PRIx64"_i%s %.16"PRIx64"_r%s",
138 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
139 ike_version_names, ike_sa->get_version(ike_sa),
140 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
141 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
142
143
144 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
145 {
146 time_t rekey, reauth;
147 peer_cfg_t *peer_cfg;
148
149 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY);
150 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
151 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
152
153 if (rekey)
154 {
155 fprintf(out, ", rekeying in %V", &rekey, &now);
156 }
157 if (reauth)
158 {
159 bool first = TRUE;
160 enumerator_t *enumerator;
161 auth_cfg_t *auth;
162
163 fprintf(out, ", ");
164 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, TRUE);
165 while (enumerator->enumerate(enumerator, &auth))
166 {
167 if (!first)
168 {
169 fprintf(out, "+");
170 }
171 first = FALSE;
172 fprintf(out, "%N", auth_class_names,
173 auth->get(auth, AUTH_RULE_AUTH_CLASS));
174 }
175 enumerator->destroy(enumerator);
176 fprintf(out, " reauthentication in %V", &reauth, &now);
177 }
178 if (!rekey && !reauth)
179 {
180 fprintf(out, ", rekeying disabled");
181 }
182 }
183 fprintf(out, "\n");
184
185 if (ike_proposal)
186 {
187 char buf[BUF_LEN];
188
189 snprintf(buf, BUF_LEN, "%P", ike_proposal);
190 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
191 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
192 buf+4);
193 }
194
195 log_task_q(out, ike_sa, TASK_QUEUE_QUEUED, "queued");
196 log_task_q(out, ike_sa, TASK_QUEUE_ACTIVE, "active");
197 log_task_q(out, ike_sa, TASK_QUEUE_PASSIVE, "passive");
198 }
199 }
200
201 /**
202 * log an CHILD_SA to out
203 */
204 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
205 {
206 time_t use_in, use_out, rekey, now;
207 u_int64_t bytes_in, bytes_out, packets_in, packets_out;
208 proposal_t *proposal;
209 linked_list_t *my_ts, *other_ts;
210 child_cfg_t *config;
211
212 config = child_sa->get_config(child_sa);
213 now = time_monotonic(NULL);
214
215 fprintf(out, "%12s{%d}: %N, %N%s, reqid %u",
216 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
217 child_sa_state_names, child_sa->get_state(child_sa),
218 ipsec_mode_names, child_sa->get_mode(child_sa),
219 config->use_proxy_mode(config) ? "_PROXY" : "",
220 child_sa->get_reqid(child_sa));
221
222 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
223 {
224 fprintf(out, ", %N%s SPIs: %.8x_i %.8x_o",
225 protocol_id_names, child_sa->get_protocol(child_sa),
226 child_sa->has_encap(child_sa) ? " in UDP" : "",
227 ntohl(child_sa->get_spi(child_sa, TRUE)),
228 ntohl(child_sa->get_spi(child_sa, FALSE)));
229
230 if (child_sa->get_ipcomp(child_sa) != IPCOMP_NONE)
231 {
232 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
233 ntohs(child_sa->get_cpi(child_sa, TRUE)),
234 ntohs(child_sa->get_cpi(child_sa, FALSE)));
235 }
236
237 if (all)
238 {
239 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
240 child_sa->get_unique_id(child_sa));
241
242 proposal = child_sa->get_proposal(child_sa);
243 if (proposal)
244 {
245 u_int16_t alg, ks;
246 bool first = TRUE;
247
248 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
249 &alg, &ks) && alg != ENCR_UNDEFINED)
250 {
251 fprintf(out, "%N", encryption_algorithm_names, alg);
252 first = FALSE;
253 if (ks)
254 {
255 fprintf(out, "_%u", ks);
256 }
257 }
258 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
259 &alg, &ks) && alg != AUTH_UNDEFINED)
260 {
261 fprintf(out, "%s%N", first ? "" : "/",
262 integrity_algorithm_names, alg);
263 if (ks)
264 {
265 fprintf(out, "_%u", ks);
266 }
267 }
268 if (proposal->get_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
269 &alg, NULL))
270 {
271 fprintf(out, "/%N", diffie_hellman_group_names, alg);
272 }
273 if (proposal->get_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
274 &alg, NULL) && alg == EXT_SEQ_NUMBERS)
275 {
276 fprintf(out, "/ESN");
277 }
278 }
279
280 child_sa->get_usestats(child_sa, TRUE,
281 &use_in, &bytes_in, &packets_in);
282 fprintf(out, ", %" PRIu64 " bytes_i", bytes_in);
283 if (use_in)
284 {
285 fprintf(out, " (%" PRIu64 " pkt%s, %" PRIu64 "s ago)",
286 packets_in, (packets_in == 1) ? "": "s",
287 (u_int64_t)(now - use_in));
288 }
289
290 child_sa->get_usestats(child_sa, FALSE,
291 &use_out, &bytes_out, &packets_out);
292 fprintf(out, ", %" PRIu64 " bytes_o", bytes_out);
293 if (use_out)
294 {
295 fprintf(out, " (%" PRIu64 " pkt%s, %" PRIu64 "s ago)",
296 packets_out, (packets_out == 1) ? "": "s",
297 (u_int64_t)(now - use_out));
298 }
299 fprintf(out, ", rekeying ");
300
301 rekey = child_sa->get_lifetime(child_sa, FALSE);
302 if (rekey)
303 {
304 if (now > rekey)
305 {
306 fprintf(out, "active");
307 }
308 else
309 {
310 fprintf(out, "in %V", &now, &rekey);
311 }
312 }
313 else
314 {
315 fprintf(out, "disabled");
316 }
317
318 }
319 }
320 else if (child_sa->get_state(child_sa) == CHILD_REKEYING ||
321 child_sa->get_state(child_sa) == CHILD_REKEYED)
322 {
323 rekey = child_sa->get_lifetime(child_sa, TRUE);
324 fprintf(out, ", expires in %V", &now, &rekey);
325 }
326
327 my_ts = linked_list_create_from_enumerator(
328 child_sa->create_ts_enumerator(child_sa, TRUE));
329 other_ts = linked_list_create_from_enumerator(
330 child_sa->create_ts_enumerator(child_sa, FALSE));
331 fprintf(out, "\n%12s{%d}: %#R === %#R\n",
332 child_sa->get_name(child_sa), child_sa->get_unique_id(child_sa),
333 my_ts, other_ts);
334 my_ts->destroy(my_ts);
335 other_ts->destroy(other_ts);
336 }
337
338 /**
339 * Log a configs local or remote authentication config to out
340 */
341 static void log_auth_cfgs(FILE *out, peer_cfg_t *peer_cfg, bool local)
342 {
343 enumerator_t *enumerator, *rules;
344 auth_rule_t rule;
345 auth_cfg_t *auth;
346 auth_class_t auth_class;
347 identification_t *id;
348 certificate_t *cert;
349 cert_validation_t valid;
350 char *name;
351
352 name = peer_cfg->get_name(peer_cfg);
353
354 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
355 while (enumerator->enumerate(enumerator, &auth))
356 {
357 fprintf(out, "%12s: %s", name, local ? "local: " : "remote:");
358 id = auth->get(auth, AUTH_RULE_IDENTITY);
359 if (id)
360 {
361 fprintf(out, " [%Y]", id);
362 }
363 fprintf(out, " uses ");
364
365 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
366 if (auth_class == AUTH_CLASS_EAP)
367 {
368 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
369 {
370 fprintf(out, "EAP authentication");
371 }
372 else
373 {
374 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
375 {
376 fprintf(out, "EAP_%" PRIuPTR "-%" PRIuPTR " authentication",
377 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
378 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
379 }
380 else
381 {
382 fprintf(out, "%N authentication", eap_type_names,
383 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
384 }
385 }
386 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
387 if (id)
388 {
389 fprintf(out, " with EAP identity '%Y'", id);
390 }
391 fprintf(out, "\n");
392 }
393 else if (auth_class == AUTH_CLASS_XAUTH)
394 {
395 fprintf(out, "%N authentication: %s", auth_class_names, auth_class,
396 auth->get(auth, AUTH_RULE_XAUTH_BACKEND) ?: "any");
397 id = auth->get(auth, AUTH_RULE_XAUTH_IDENTITY);
398 if (id)
399 {
400 fprintf(out, " with XAuth identity '%Y'", id);
401 }
402 fprintf(out, "\n");
403 }
404 else
405 {
406 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
407 }
408
409 cert = auth->get(auth, AUTH_RULE_CA_CERT);
410 if (cert)
411 {
412 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
413 }
414
415 cert = auth->get(auth, AUTH_RULE_IM_CERT);
416 if (cert)
417 {
418 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
419 }
420
421 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
422 if (cert)
423 {
424 fprintf(out, "%12s: cert: \"%Y\"\n", name,
425 cert->get_subject(cert));
426 }
427
428 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
429 if (valid != VALIDATION_FAILED)
430 {
431 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
432 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
433 }
434
435 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
436 if (valid != VALIDATION_FAILED)
437 {
438 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
439 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
440 }
441
442 rules = auth->create_enumerator(auth);
443 while (rules->enumerate(rules, &rule, &id))
444 {
445 if (rule == AUTH_RULE_GROUP)
446 {
447 fprintf(out, "%12s: group: %Y\n", name, id);
448 }
449 }
450 rules->destroy(rules);
451 }
452 enumerator->destroy(enumerator);
453 }
454
455 METHOD(stroke_list_t, status, void,
456 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out,
457 bool all, bool wait)
458 {
459 enumerator_t *enumerator, *children;
460 ike_cfg_t *ike_cfg;
461 child_cfg_t *child_cfg;
462 child_sa_t *child_sa;
463 ike_sa_t *ike_sa;
464 linked_list_t *my_ts, *other_ts;
465 bool first, found = FALSE;
466 char *name = msg->status.name;
467 u_int half_open;
468
469 if (all)
470 {
471 peer_cfg_t *peer_cfg;
472 ike_version_t ike_version;
473 char *pool;
474 host_t *host;
475 u_int32_t dpd;
476 time_t since, now;
477 u_int size, online, offline, i;
478 struct utsname utsname;
479
480 now = time_monotonic(NULL);
481 since = time(NULL) - (now - this->uptime);
482
483 fprintf(out, "Status of IKE charon daemon (%sSwan "VERSION, this->swan);
484 if (uname(&utsname) == 0)
485 {
486 fprintf(out, ", %s %s, %s",
487 utsname.sysname, utsname.release, utsname.machine);
488 }
489 fprintf(out, "):\n uptime: %V, since %T\n", &now, &this->uptime, &since,
490 FALSE);
491 #ifdef HAVE_MALLINFO
492 {
493 struct mallinfo mi = mallinfo();
494
495 fprintf(out, " malloc: sbrk %u, mmap %u, used %u, free %u\n",
496 mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
497 }
498 #endif /* HAVE_MALLINFO */
499 fprintf(out, " worker threads: %d of %d idle, ",
500 lib->processor->get_idle_threads(lib->processor),
501 lib->processor->get_total_threads(lib->processor));
502 for (i = 0; i < JOB_PRIO_MAX; i++)
503 {
504 fprintf(out, "%s%d", i == 0 ? "" : "/",
505 lib->processor->get_working_threads(lib->processor, i));
506 }
507 fprintf(out, " working, job queue: ");
508 for (i = 0; i < JOB_PRIO_MAX; i++)
509 {
510 fprintf(out, "%s%d", i == 0 ? "" : "/",
511 lib->processor->get_job_load(lib->processor, i));
512 }
513 fprintf(out, ", scheduled: %d\n",
514 lib->scheduler->get_job_load(lib->scheduler));
515 fprintf(out, " loaded plugins: %s\n",
516 lib->plugins->loaded_plugins(lib->plugins));
517
518 first = TRUE;
519 enumerator = this->attribute->create_pool_enumerator(this->attribute);
520 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
521 {
522 if (name && !streq(name, pool))
523 {
524 continue;
525 }
526 if (first)
527 {
528 first = FALSE;
529 fprintf(out, "Virtual IP pools (size/online/offline):\n");
530 }
531 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
532 }
533 enumerator->destroy(enumerator);
534
535 enumerator = charon->kernel->create_address_enumerator(charon->kernel,
536 ADDR_TYPE_REGULAR);
537 fprintf(out, "Listening IP addresses:\n");
538 while (enumerator->enumerate(enumerator, (void**)&host))
539 {
540 fprintf(out, " %H\n", host);
541 }
542 enumerator->destroy(enumerator);
543
544 fprintf(out, "Connections:\n");
545 enumerator = charon->backends->create_peer_cfg_enumerator(
546 charon->backends, NULL, NULL, NULL, NULL, IKE_ANY);
547 while (enumerator->enumerate(enumerator, &peer_cfg))
548 {
549 char *my_addr, *other_addr;
550
551 if (name && !streq(name, peer_cfg->get_name(peer_cfg)))
552 {
553 continue;
554 }
555
556 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
557 ike_version = peer_cfg->get_ike_version(peer_cfg);
558 my_addr = ike_cfg->get_my_addr(ike_cfg);
559 other_addr = ike_cfg->get_other_addr(ike_cfg);
560 fprintf(out, "%12s: %s...%s %N", peer_cfg->get_name(peer_cfg),
561 my_addr, other_addr, ike_version_names, ike_version);
562
563 if (ike_version == IKEV1 && peer_cfg->use_aggressive(peer_cfg))
564 {
565 fprintf(out, " Aggressive");
566 }
567
568 dpd = peer_cfg->get_dpd(peer_cfg);
569 if (dpd)
570 {
571 fprintf(out, ", dpddelay=%us", dpd);
572 }
573 fprintf(out, "\n");
574
575 log_auth_cfgs(out, peer_cfg, TRUE);
576 log_auth_cfgs(out, peer_cfg, FALSE);
577
578 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
579 while (children->enumerate(children, &child_cfg))
580 {
581 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
582 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
583 fprintf(out, "%12s: child: %#R === %#R %N",
584 child_cfg->get_name(child_cfg), my_ts, other_ts,
585 ipsec_mode_names, child_cfg->get_mode(child_cfg));
586 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
587 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
588
589 if (dpd)
590 {
591 fprintf(out, ", dpdaction=%N", action_names,
592 child_cfg->get_dpd_action(child_cfg));
593 }
594 fprintf(out, "\n");
595 }
596 children->destroy(children);
597 }
598 enumerator->destroy(enumerator);
599 }
600
601 /* Enumerate shunt policies */
602 first = TRUE;
603 enumerator = charon->shunts->create_enumerator(charon->shunts);
604 while (enumerator->enumerate(enumerator, &child_cfg))
605 {
606 if (name && !streq(name, child_cfg->get_name(child_cfg)))
607 {
608 continue;
609 }
610 if (first)
611 {
612 fprintf(out, "Shunted Connections:\n");
613 first = FALSE;
614 }
615 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
616 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
617 fprintf(out, "%12s: %#R === %#R %N\n",
618 child_cfg->get_name(child_cfg), my_ts, other_ts,
619 ipsec_mode_names, child_cfg->get_mode(child_cfg));
620 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
621 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
622 }
623 enumerator->destroy(enumerator);
624
625 /* Enumerate traps */
626 first = TRUE;
627 enumerator = charon->traps->create_enumerator(charon->traps);
628 while (enumerator->enumerate(enumerator, NULL, &child_sa))
629 {
630 if (name && !streq(name, child_sa->get_name(child_sa)))
631 {
632 continue;
633 }
634 if (first)
635 {
636 fprintf(out, "Routed Connections:\n");
637 first = FALSE;
638 }
639 log_child_sa(out, child_sa, all);
640 }
641 enumerator->destroy(enumerator);
642
643 half_open = charon->ike_sa_manager->get_half_open_count(
644 charon->ike_sa_manager, NULL, FALSE);
645 fprintf(out, "Security Associations (%u up, %u connecting):\n",
646 charon->ike_sa_manager->get_count(charon->ike_sa_manager) - half_open,
647 half_open);
648 enumerator = charon->controller->create_ike_sa_enumerator(
649 charon->controller, wait);
650 while (enumerator->enumerate(enumerator, &ike_sa) && ferror(out) == 0)
651 {
652 bool ike_printed = FALSE;
653 enumerator_t *children = ike_sa->create_child_sa_enumerator(ike_sa);
654
655 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
656 {
657 log_ike_sa(out, ike_sa, all);
658 found = TRUE;
659 ike_printed = TRUE;
660 }
661
662 while (children->enumerate(children, (void**)&child_sa))
663 {
664 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
665 {
666 if (!ike_printed)
667 {
668 log_ike_sa(out, ike_sa, all);
669 found = TRUE;
670 ike_printed = TRUE;
671 }
672 log_child_sa(out, child_sa, all);
673 }
674 }
675 children->destroy(children);
676 }
677 enumerator->destroy(enumerator);
678
679 if (!found)
680 {
681 if (name)
682 {
683 fprintf(out, " no match\n");
684 }
685 else
686 {
687 fprintf(out, " none\n");
688 }
689 }
690 }
691
692 /**
693 * create a unique certificate list without duplicates
694 * certicates having the same issuer are grouped together.
695 */
696 static linked_list_t* create_unique_cert_list(certificate_type_t type)
697 {
698 linked_list_t *list = linked_list_create();
699 enumerator_t *enumerator = lib->credmgr->create_cert_enumerator(
700 lib->credmgr, type, KEY_ANY, NULL, FALSE);
701 certificate_t *cert;
702
703 while (enumerator->enumerate(enumerator, (void**)&cert))
704 {
705 enumerator_t *added = list->create_enumerator(list);
706 identification_t *issuer = cert->get_issuer(cert);
707 bool previous_same, same = FALSE, found = FALSE;
708 certificate_t *list_cert;
709
710 while (added->enumerate(added, (void**)&list_cert))
711 {
712 if (list_cert->equals(list_cert, cert))
713 { /* stop if we found a duplicate*/
714 found = TRUE;
715 break;
716 }
717 previous_same = same;
718 same = list_cert->has_issuer(list_cert, issuer);
719 if (previous_same && !same)
720 { /* group certificates with same issuer */
721 break;
722 }
723 }
724 if (!found)
725 {
726 list->insert_before(list, added, cert->get_ref(cert));
727 }
728 added->destroy(added);
729 }
730 enumerator->destroy(enumerator);
731 return list;
732 }
733
734 /**
735 * Is there a matching private key?
736 */
737 static bool has_privkey(certificate_t *cert)
738 {
739 public_key_t *public;
740 private_key_t *private = NULL;
741 chunk_t keyid;
742 identification_t *id;
743
744 public = cert->get_public_key(cert);
745 if (!public)
746 {
747 return FALSE;
748 }
749 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
750 {
751 id = identification_create_from_encoding(ID_KEY_ID, keyid);
752 private = lib->credmgr->get_private(lib->credmgr,
753 public->get_type(public), id, NULL);
754 id->destroy(id);
755 }
756 public->destroy(public);
757 DESTROY_IF(private);
758
759 return (private != NULL);
760 }
761
762 /**
763 * list all X.509 certificates matching the flags
764 */
765 static void stroke_list_x509_certs(linked_list_t *list, x509_flag_t flag)
766 {
767 enumerator_t *enumerator;
768 certificate_t *cert;
769
770 enumerator = list->create_enumerator(list);
771 while (enumerator->enumerate(enumerator, (void**)&cert))
772 {
773 x509_t *x509 = (x509_t*)cert;
774 x509_flag_t flags = x509->get_flags(x509) & X509_ANY;
775
776 /* list only if flag is set or flag == 0 */
777 if ((flags & flag) || flags == flag)
778 {
779 cert_printer->print_caption(cert_printer, CERT_X509, flag);
780 cert_printer->print(cert_printer, cert, has_privkey(cert));
781 }
782 }
783 enumerator->destroy(enumerator);
784 }
785
786 /**
787 * list all other certificates types
788 */
789 static void stroke_list_other_certs(certificate_type_t type)
790 {
791 enumerator_t *enumerator;
792 certificate_t *cert;
793 linked_list_t *list;
794
795 list = create_unique_cert_list(type);
796
797 enumerator = list->create_enumerator(list);
798 while (enumerator->enumerate(enumerator, &cert))
799 {
800 cert_printer->print_caption(cert_printer, cert->get_type(cert), X509_NONE);
801 cert_printer->print(cert_printer, cert, has_privkey(cert));
802 }
803 enumerator->destroy(enumerator);
804
805 list->destroy_offset(list, offsetof(certificate_t, destroy));
806 }
807
808 /**
809 * Print the name of an algorithm plus the name of the plugin that registered it
810 */
811 static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
812 const char *plugin_name)
813 {
814 char alg_name[BUF_LEN];
815 int alg_name_len;
816
817 if (alg_names)
818 {
819 alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type,
820 plugin_name);
821 }
822 else
823 {
824 alg_name_len = sprintf(alg_name, " [%s]", plugin_name);
825 }
826 if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
827 {
828 fprintf(out, "\n ");
829 *len = 13;
830 }
831 fprintf(out, "%s", alg_name);
832 *len += alg_name_len;
833 }
834
835 /**
836 * List of registered cryptographical algorithms
837 */
838 static void list_algs(FILE *out)
839 {
840 enumerator_t *enumerator;
841 encryption_algorithm_t encryption;
842 integrity_algorithm_t integrity;
843 hash_algorithm_t hash;
844 pseudo_random_function_t prf;
845 diffie_hellman_group_t group;
846 rng_quality_t quality;
847 const char *plugin_name;
848 int len;
849
850 fprintf(out, "\n");
851 fprintf(out, "List of registered IKE algorithms:\n");
852 fprintf(out, "\n encryption:");
853 len = 13;
854 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
855 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
856 {
857 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
858 }
859 enumerator->destroy(enumerator);
860 fprintf(out, "\n integrity: ");
861 len = 13;
862 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
863 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
864 {
865 print_alg(out, &len, integrity_algorithm_names, integrity, plugin_name);
866 }
867 enumerator->destroy(enumerator);
868 fprintf(out, "\n aead: ");
869 len = 13;
870 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
871 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
872 {
873 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
874 }
875 enumerator->destroy(enumerator);
876 fprintf(out, "\n hasher: ");
877 len = 13;
878 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
879 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
880 {
881 print_alg(out, &len, hash_algorithm_names, hash, plugin_name);
882 }
883 enumerator->destroy(enumerator);
884 fprintf(out, "\n prf: ");
885 len = 13;
886 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
887 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
888 {
889 print_alg(out, &len, pseudo_random_function_names, prf, plugin_name);
890 }
891 enumerator->destroy(enumerator);
892 fprintf(out, "\n dh-group: ");
893 len = 13;
894 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
895 while (enumerator->enumerate(enumerator, &group, &plugin_name))
896 {
897 print_alg(out, &len, diffie_hellman_group_names, group, plugin_name);
898 }
899 enumerator->destroy(enumerator);
900 fprintf(out, "\n random-gen:");
901 len = 13;
902 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
903 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
904 {
905 print_alg(out, &len, rng_quality_names, quality, plugin_name);
906 }
907 enumerator->destroy(enumerator);
908 fprintf(out, "\n nonce-gen: ");
909 len = 13;
910 enumerator = lib->crypto->create_nonce_gen_enumerator(lib->crypto);
911 while (enumerator->enumerate(enumerator, &plugin_name))
912 {
913 print_alg(out, &len, NULL, 0, plugin_name);
914 }
915 enumerator->destroy(enumerator);
916 fprintf(out, "\n");
917 }
918
919 /**
920 * List loaded plugin information
921 */
922 static void list_plugins(FILE *out)
923 {
924 plugin_feature_t *features, *fp;
925 enumerator_t *enumerator;
926 linked_list_t *list;
927 plugin_t *plugin;
928 int count, i;
929 bool loaded;
930 char *str;
931
932 fprintf(out, "\n");
933 fprintf(out, "List of loaded Plugins:\n");
934 fprintf(out, "\n");
935
936 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
937 while (enumerator->enumerate(enumerator, &plugin, &list))
938 {
939 fprintf(out, "%s:\n", plugin->get_name(plugin));
940 if (plugin->get_features)
941 {
942 count = plugin->get_features(plugin, &features);
943 for (i = 0; i < count; i++)
944 {
945 str = plugin_feature_get_string(&features[i]);
946 switch (features[i].kind)
947 {
948 case FEATURE_PROVIDE:
949 fp = &features[i];
950 loaded = list->find_first(list, NULL,
951 (void**)&fp) == SUCCESS;
952 fprintf(out, " %s%s\n",
953 str, loaded ? "" : " (not loaded)");
954 break;
955 case FEATURE_DEPENDS:
956 fprintf(out, " %s\n", str);
957 break;
958 case FEATURE_SDEPEND:
959 fprintf(out, " %s (soft)\n", str);
960 break;
961 default:
962 break;
963 }
964 free(str);
965 }
966 }
967 list->destroy(list);
968 }
969 enumerator->destroy(enumerator);
970 }
971
972 METHOD(stroke_list_t, list, void,
973 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
974 {
975 linked_list_t *cert_list = NULL;
976
977 cert_printer = certificate_printer_create(out, TRUE, msg->list.utc);
978
979 if (msg->list.flags & LIST_PUBKEYS)
980 {
981 stroke_list_other_certs(CERT_TRUSTED_PUBKEY);
982 }
983 if (msg->list.flags & LIST_CERTS)
984 {
985 stroke_list_other_certs(CERT_GPG);
986 }
987 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
988 {
989 cert_list = create_unique_cert_list(CERT_X509);
990 }
991 if (msg->list.flags & LIST_CERTS)
992 {
993 stroke_list_x509_certs(cert_list, X509_NONE);
994 }
995 if (msg->list.flags & LIST_CACERTS)
996 {
997 stroke_list_x509_certs(cert_list, X509_CA);
998 }
999 if (msg->list.flags & LIST_OCSPCERTS)
1000 {
1001 stroke_list_x509_certs(cert_list, X509_OCSP_SIGNER);
1002 }
1003 if (msg->list.flags & LIST_AACERTS)
1004 {
1005 stroke_list_x509_certs(cert_list, X509_AA);
1006 }
1007 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1008
1009 if (msg->list.flags & LIST_ACERTS)
1010 {
1011 stroke_list_other_certs(CERT_X509_AC);
1012 }
1013 if (msg->list.flags & LIST_CRLS)
1014 {
1015 stroke_list_other_certs(CERT_X509_CRL);
1016 }
1017 if (msg->list.flags & LIST_OCSP)
1018 {
1019 stroke_list_other_certs(CERT_X509_OCSP_RESPONSE);
1020 }
1021 if (msg->list.flags & LIST_ALGS)
1022 {
1023 list_algs(out);
1024 }
1025 if (msg->list.flags & LIST_PLUGINS)
1026 {
1027 list_plugins(out);
1028 }
1029 cert_printer->destroy(cert_printer);
1030 cert_printer = NULL;
1031 }
1032
1033 /**
1034 * Print leases of a single pool
1035 */
1036 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1037 host_t *address, u_int size, u_int online, u_int offline)
1038 {
1039 enumerator_t *enumerator;
1040 identification_t *id;
1041 host_t *lease;
1042 bool on;
1043 int found = 0;
1044
1045 fprintf(out, "Leases in pool '%s', usage: %u/%u, %u online\n",
1046 pool, online + offline, size, online);
1047 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1048 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1049 {
1050 if (!address || address->ip_equals(address, lease))
1051 {
1052 fprintf(out, " %15H %s '%Y'\n",
1053 lease, on ? "online" : "offline", id);
1054 found++;
1055 }
1056 }
1057 enumerator->destroy(enumerator);
1058 if (!found)
1059 {
1060 fprintf(out, " no matching leases found\n");
1061 }
1062 }
1063
1064 METHOD(stroke_list_t, leases, void,
1065 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1066 {
1067 enumerator_t *enumerator;
1068 u_int size, offline, online;
1069 host_t *address = NULL;
1070 char *pool;
1071 int found = 0;
1072
1073 if (msg->leases.address)
1074 {
1075 address = host_create_from_string(msg->leases.address, 0);
1076 }
1077
1078 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1079 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1080 {
1081 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1082 {
1083 pool_leases(this, out, pool, address, size, online, offline);
1084 found++;
1085 }
1086 }
1087 enumerator->destroy(enumerator);
1088 if (!found)
1089 {
1090 if (msg->leases.pool)
1091 {
1092 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1093 }
1094 else
1095 {
1096 fprintf(out, "no pools found\n");
1097 }
1098 }
1099 DESTROY_IF(address);
1100 }
1101
1102 METHOD(stroke_list_t, destroy, void,
1103 private_stroke_list_t *this)
1104 {
1105 free(this);
1106 }
1107
1108 /*
1109 * see header file
1110 */
1111 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1112 {
1113 private_stroke_list_t *this;
1114
1115 INIT(this,
1116 .public = {
1117 .list = _list,
1118 .status = _status,
1119 .leases = _leases,
1120 .destroy = _destroy,
1121 },
1122 .uptime = time_monotonic(NULL),
1123 .swan = "strong",
1124 .attribute = attribute,
1125 );
1126
1127 if (lib->settings->get_bool(lib->settings,
1128 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk", FALSE))
1129 {
1130 this->swan = "weak";
1131 }
1132
1133 return &this->public;
1134 }