ike_sa_manager enumerable, not iterable
[strongswan.git] / src / charon / plugins / stroke / stroke_list.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "stroke_list.h"
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <credentials/certificates/x509.h>
23 #include <credentials/certificates/crl.h>
24
25 /* warning intervals for list functions */
26 #define CERT_WARNING_INTERVAL 30 /* days */
27 #define CRL_WARNING_INTERVAL 7 /* days */
28
29 typedef struct private_stroke_list_t private_stroke_list_t;
30
31 /**
32 * private data of stroke_list
33 */
34 struct private_stroke_list_t {
35
36 /**
37 * public functions
38 */
39 stroke_list_t public;
40
41 /**
42 * timestamp of daemon start
43 */
44 time_t uptime;
45 };
46
47 /**
48 * log an IKE_SA to out
49 */
50 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
51 {
52 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
53 u_int32_t rekey, reauth;
54
55 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
56 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
57 ike_sa_state_names, ike_sa->get_state(ike_sa),
58 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
59 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
60
61 if (all)
62 {
63 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
64 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
65 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
66 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
67
68 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY_TIME);
69 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH_TIME);
70 if (rekey)
71 {
72 fprintf(out, ", rekeying in %V", &rekey);
73 }
74 if (reauth)
75 {
76 fprintf(out, ", reauthentication in %V", &reauth);
77 }
78 if (!rekey && !reauth)
79 {
80 fprintf(out, ", rekeying disabled");
81 }
82 fprintf(out, "\n");
83 }
84 }
85
86 /**
87 * log an CHILD_SA to out
88 */
89 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
90 {
91 u_int32_t rekey, now = time(NULL);
92 u_int32_t use_in, use_out, use_fwd;
93 encryption_algorithm_t encr_alg;
94 integrity_algorithm_t int_alg;
95 size_t encr_len, int_len;
96 mode_t mode;
97
98 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
99 &int_alg, &int_len, &rekey, &use_in, &use_out,
100 &use_fwd);
101
102 fprintf(out, "%12s{%d}: %N, %N",
103 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
104 child_sa_state_names, child_sa->get_state(child_sa),
105 mode_names, mode);
106
107 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
108 {
109 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
110 protocol_id_names, child_sa->get_protocol(child_sa),
111 htonl(child_sa->get_spi(child_sa, TRUE)),
112 htonl(child_sa->get_spi(child_sa, FALSE)));
113
114 if (all)
115 {
116 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
117 child_sa->get_reqid(child_sa));
118
119
120 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
121 {
122 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
123
124 if (encr_len)
125 {
126 fprintf(out, "-%d", encr_len);
127 }
128 fprintf(out, "/");
129 }
130
131 fprintf(out, "%N", integrity_algorithm_names, int_alg);
132 if (int_len)
133 {
134 fprintf(out, "-%d", int_len);
135 }
136 fprintf(out, ", rekeying ");
137
138 if (rekey)
139 {
140 fprintf(out, "in %#V", &now, &rekey);
141 }
142 else
143 {
144 fprintf(out, "disabled");
145 }
146
147 fprintf(out, ", last use: ");
148 use_in = max(use_in, use_fwd);
149 if (use_in)
150 {
151 fprintf(out, "%ds_i ", now - use_in);
152 }
153 else
154 {
155 fprintf(out, "no_i ");
156 }
157 if (use_out)
158 {
159 fprintf(out, "%ds_o ", now - use_out);
160 }
161 else
162 {
163 fprintf(out, "no_o ");
164 }
165 }
166 }
167
168 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
169 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
170 child_sa->get_traffic_selectors(child_sa, TRUE),
171 child_sa->get_traffic_selectors(child_sa, FALSE));
172 }
173
174 /**
175 * Implementation of stroke_list_t.status.
176 */
177 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
178 {
179 enumerator_t *enumerator, *children;
180 iterator_t *iterator;
181 host_t *host;
182 peer_cfg_t *peer_cfg;
183 ike_cfg_t *ike_cfg;
184 child_cfg_t *child_cfg;
185 ike_sa_t *ike_sa;
186 char *name = NULL;
187 bool found = FALSE;
188 time_t uptime;
189
190 name = msg->status.name;
191
192 if (all)
193 {
194 uptime = time(NULL) - this->uptime;
195 fprintf(out, "Performance:\n");
196 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
197 fprintf(out, " worker threads: %d idle of %d,",
198 charon->processor->get_idle_threads(charon->processor),
199 charon->processor->get_total_threads(charon->processor));
200 fprintf(out, " job queue load: %d,",
201 charon->processor->get_job_load(charon->processor));
202 fprintf(out, " scheduled events: %d\n",
203 charon->scheduler->get_job_load(charon->scheduler));
204 iterator = charon->kernel_interface->create_address_iterator(
205 charon->kernel_interface);
206 fprintf(out, "Listening IP addresses:\n");
207 while (iterator->iterate(iterator, (void**)&host))
208 {
209 fprintf(out, " %H\n", host);
210 }
211 iterator->destroy(iterator);
212
213 fprintf(out, "Connections:\n");
214 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
215 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
216 {
217 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
218 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
219 {
220 continue;
221 }
222
223 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
224 fprintf(out, "%12s: %H[%D]...%H[%D]\n", peer_cfg->get_name(peer_cfg),
225 ike_cfg->get_my_host(ike_cfg), peer_cfg->get_my_id(peer_cfg),
226 ike_cfg->get_other_host(ike_cfg), peer_cfg->get_other_id(peer_cfg));
227 /* TODO: list CAs and groups */
228 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
229 while (children->enumerate(children, &child_cfg))
230 {
231 linked_list_t *my_ts, *other_ts;
232 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
233 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
234 fprintf(out, "%12s: %#R=== %#R\n", child_cfg->get_name(child_cfg),
235 my_ts, other_ts);
236 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
237 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
238 }
239 children->destroy(children);
240 }
241 enumerator->destroy(enumerator);
242 }
243
244 fprintf(out, "Security Associations:\n");
245 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
246 while (enumerator->enumerate(enumerator, &ike_sa))
247 {
248 bool ike_printed = FALSE;
249 child_sa_t *child_sa;
250 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
251
252 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
253 {
254 log_ike_sa(out, ike_sa, all);
255 found = TRUE;
256 ike_printed = TRUE;
257 }
258
259 while (children->iterate(children, (void**)&child_sa))
260 {
261 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
262 {
263 if (!ike_printed)
264 {
265 log_ike_sa(out, ike_sa, all);
266 found = TRUE;
267 ike_printed = TRUE;
268 }
269 log_child_sa(out, child_sa, all);
270 }
271 }
272 children->destroy(children);
273 }
274 enumerator->destroy(enumerator);
275
276 if (!found)
277 {
278 if (name)
279 {
280 fprintf(out, " no match\n");
281 }
282 else
283 {
284 fprintf(out, " none\n");
285 }
286 }
287 }
288
289 /**
290 * create a unique certificate list without duplicates
291 * certicates having the same issuer are grouped together.
292 */
293 static linked_list_t* create_unique_cert_list(certificate_type_t type)
294 {
295 linked_list_t *list = linked_list_create();
296 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
297 charon->credentials, type, KEY_ANY,
298 NULL, FALSE);
299 certificate_t *cert;
300
301 while (enumerator->enumerate(enumerator, (void**)&cert))
302 {
303 iterator_t *iterator = list->create_iterator(list, TRUE);
304 identification_t *issuer = cert->get_issuer(cert);
305 bool previous_same, same = FALSE, last = TRUE;
306 certificate_t *list_cert;
307
308 while (iterator->iterate(iterator, (void**)&list_cert))
309 {
310 /* exit if we have a duplicate? */
311 if (list_cert->equals(list_cert, cert))
312 {
313 last = FALSE;
314 break;
315 }
316 /* group certificates with same issuer */
317 previous_same = same;
318 same = list_cert->has_issuer(list_cert, issuer);
319 if (previous_same && !same)
320 {
321 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
322 last = FALSE;
323 break;
324 }
325 }
326 iterator->destroy(iterator);
327
328 if (last)
329 {
330 list->insert_last(list, (void *)cert->get_ref(cert));
331 }
332 }
333 enumerator->destroy(enumerator);
334 return list;
335 }
336
337 /**
338 * list all X.509 certificates matching the flags
339 */
340 static void stroke_list_certs(linked_list_t *list, char *label,
341 x509_flag_t flags, bool utc, FILE *out)
342 {
343 bool first = TRUE;
344 time_t now = time(NULL);
345 enumerator_t *enumerator = list->create_enumerator(list);
346 certificate_t *cert;
347
348 while (enumerator->enumerate(enumerator, (void**)&cert))
349 {
350 x509_t *x509 = (x509_t*)cert;
351 x509_flag_t x509_flags = x509->get_flags(x509);
352
353 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
354 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
355 {
356 enumerator_t *enumerator;
357 identification_t *altName;
358 bool first_altName = TRUE;
359 chunk_t serial = x509->get_serial(x509);
360 identification_t *authkey = x509->get_authKeyIdentifier(x509);
361 time_t notBefore, notAfter;
362 public_key_t *public = cert->get_public_key(cert);
363
364 if (first)
365 {
366 fprintf(out, "\n");
367 fprintf(out, "List of %s:\n", label);
368 first = FALSE;
369 }
370 fprintf(out, "\n");
371
372 /* list subjectAltNames */
373 enumerator = x509->create_subjectAltName_enumerator(x509);
374 while (enumerator->enumerate(enumerator, (void**)&altName))
375 {
376 if (first_altName)
377 {
378 fprintf(out, " altNames: ");
379 first_altName = FALSE;
380 }
381 else
382 {
383 fprintf(out, ", ");
384 }
385 fprintf(out, "%D", altName);
386 }
387 if (!first_altName)
388 {
389 fprintf(out, "\n");
390 }
391 enumerator->destroy(enumerator);
392
393 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
394 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
395 fprintf(out, " serial: %#B\n", &serial);
396
397 /* list validity */
398 cert->get_validity(cert, &now, &notBefore, &notAfter);
399 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
400 if (now < notBefore)
401 {
402 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
403 }
404 else
405 {
406 fprintf(out, "ok\n");
407 }
408 fprintf(out, " not after %#T, ", &notAfter, utc);
409 if (now > notAfter)
410 {
411 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
412 }
413 else
414 {
415 fprintf(out, "ok");
416 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
417 {
418 fprintf(out, " (expires in %#V)", &now, &notAfter);
419 }
420 fprintf(out, " \n");
421 }
422
423 /* list public key information */
424 if (public)
425 {
426 private_key_t *private = NULL;
427 identification_t *id, *keyid;
428
429 id = public->get_id(public, ID_PUBKEY_SHA1);
430 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
431
432 private = charon->credentials->get_private(
433 charon->credentials,
434 public->get_type(public), keyid, NULL);
435 fprintf(out, " pubkey: %N %d bits%s\n",
436 key_type_names, public->get_type(public),
437 public->get_keysize(public) * 8,
438 private ? ", has private key" : "");
439 fprintf(out, " keyid: %D\n", keyid);
440 fprintf(out, " subjkey: %D\n", id);
441 DESTROY_IF(private);
442 public->destroy(public);
443 }
444
445 /* list optional authorityKeyIdentifier */
446 if (authkey)
447 {
448 fprintf(out, " authkey: %D\n", authkey);
449 }
450 }
451 }
452 enumerator->destroy(enumerator);
453 }
454
455 /**
456 * list all X.509 attribute certificates
457 */
458 static void stroke_list_acerts(bool utc, FILE *out)
459 {
460
461 }
462
463 /**
464 * list all X.509 CRLs
465 */
466 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
467 {
468 bool first = TRUE;
469 time_t thisUpdate, nextUpdate, now = time(NULL);
470 enumerator_t *enumerator = list->create_enumerator(list);
471 certificate_t *cert;
472
473 while (enumerator->enumerate(enumerator, (void**)&cert))
474 {
475 crl_t *crl = (crl_t*)cert;
476 chunk_t serial = crl->get_serial(crl);
477 identification_t *authkey = crl->get_authKeyIdentifier(crl);
478
479 if (first)
480 {
481 fprintf(out, "\n");
482 fprintf(out, "List of X.509 CRLs:\n");
483 first = FALSE;
484 }
485 fprintf(out, "\n");
486
487 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
488
489 /* list optional crlNumber */
490 if (serial.ptr)
491 {
492 fprintf(out, " serial: %#B\n", &serial);
493 }
494
495 /* count the number of revoked certificates */
496 {
497 int count = 0;
498 enumerator_t *enumerator = crl->create_enumerator(crl);
499
500 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
501 {
502 count++;
503 }
504 fprintf(out, " revoked: %d certificate%s\n", count,
505 (count == 1)? "" : "s");
506 enumerator->destroy(enumerator);
507 }
508
509 /* list validity */
510 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
511 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
512 fprintf(out, " next %#T, ", &nextUpdate, utc);
513 if (now > nextUpdate)
514 {
515 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
516 }
517 else
518 {
519 fprintf(out, "ok");
520 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
521 {
522 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
523 }
524 fprintf(out, " \n");
525 }
526
527 /* list optional authorityKeyIdentifier */
528 if (authkey)
529 {
530 fprintf(out, " authkey: %D\n", authkey);
531 }
532 }
533 enumerator->destroy(enumerator);
534 }
535
536 /**
537 * list all OCSP responses
538 */
539 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
540 {
541 bool first = TRUE;
542 enumerator_t *enumerator = list->create_enumerator(list);
543 certificate_t *cert;
544
545 while (enumerator->enumerate(enumerator, (void**)&cert))
546 {
547 if (first)
548 {
549 fprintf(out, "\n");
550 fprintf(out, "List of OCSP responses:\n");
551 fprintf(out, "\n");
552 first = FALSE;
553 }
554
555 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
556 }
557 enumerator->destroy(enumerator);
558 }
559
560 /**
561 * Implementation of stroke_list_t.list.
562 */
563 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
564 {
565 linked_list_t *cert_list = NULL;
566
567 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
568 {
569 cert_list = create_unique_cert_list(CERT_X509);
570 }
571 if (msg->list.flags & LIST_CERTS)
572 {
573 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
574 0, msg->list.utc, out);
575 }
576 if (msg->list.flags & LIST_CACERTS)
577 {
578 stroke_list_certs(cert_list, "X.509 CA Certificates",
579 X509_CA, msg->list.utc, out);
580 }
581 if (msg->list.flags & LIST_OCSPCERTS)
582 {
583 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
584 X509_OCSP_SIGNER, msg->list.utc, out);
585 }
586 if (msg->list.flags & LIST_AACERTS)
587 {
588 stroke_list_certs(cert_list, "X.509 AA Certificates",
589 X509_AA, msg->list.utc, out);
590 }
591 if (msg->list.flags & LIST_ACERTS)
592 {
593 stroke_list_acerts(msg->list.utc, out);
594 }
595 if (msg->list.flags & LIST_CRLS)
596 {
597 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
598
599 stroke_list_crls(crl_list, msg->list.utc, out);
600 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
601 }
602 if (msg->list.flags & LIST_OCSP)
603 {
604 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
605
606 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
607 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
608 }
609 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
610 }
611
612 /**
613 * Implementation of stroke_list_t.destroy
614 */
615 static void destroy(private_stroke_list_t *this)
616 {
617 free(this);
618 }
619
620 /*
621 * see header file
622 */
623 stroke_list_t *stroke_list_create()
624 {
625 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
626
627 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
628 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
629 this->public.destroy = (void(*)(stroke_list_t*))destroy;
630
631 this->uptime = time(NULL);
632
633 return &this->public;
634 }
635