splitted stroke plugin to several 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 * $Id$
16 */
17
18 #include "stroke_list.h"
19
20 #include <daemon.h>
21 #include <credentials/certificates/x509.h>
22 #include <credentials/certificates/crl.h>
23
24 /* warning intervals for list functions */
25 #define CERT_WARNING_INTERVAL 30 /* days */
26 #define CRL_WARNING_INTERVAL 7 /* days */
27
28 typedef struct private_stroke_list_t private_stroke_list_t;
29
30 /**
31 * private data of stroke_list
32 */
33 struct private_stroke_list_t {
34
35 /**
36 * public functions
37 */
38 stroke_list_t public;
39 };
40
41 /**
42 * log an IKE_SA to out
43 */
44 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
45 {
46 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
47 u_int32_t rekey, reauth;
48
49 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
50 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
51 ike_sa_state_names, ike_sa->get_state(ike_sa),
52 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
53 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
54
55 if (all)
56 {
57 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
58 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
59 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
60 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
61
62 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY_TIME);
63 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH_TIME);
64 if (rekey)
65 {
66 fprintf(out, ", rekeying in %V", &rekey);
67 }
68 if (reauth)
69 {
70 fprintf(out, ", reauthentication in %V", &reauth);
71 }
72 if (!rekey && !reauth)
73 {
74 fprintf(out, ", rekeying disabled");
75 }
76 fprintf(out, "\n");
77 }
78 }
79
80 /**
81 * log an CHILD_SA to out
82 */
83 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
84 {
85 u_int32_t rekey, now = time(NULL);
86 u_int32_t use_in, use_out, use_fwd;
87 encryption_algorithm_t encr_alg;
88 integrity_algorithm_t int_alg;
89 size_t encr_len, int_len;
90 mode_t mode;
91
92 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
93 &int_alg, &int_len, &rekey, &use_in, &use_out,
94 &use_fwd);
95
96 fprintf(out, "%12s{%d}: %N, %N",
97 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
98 child_sa_state_names, child_sa->get_state(child_sa),
99 mode_names, mode);
100
101 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
102 {
103 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
104 protocol_id_names, child_sa->get_protocol(child_sa),
105 htonl(child_sa->get_spi(child_sa, TRUE)),
106 htonl(child_sa->get_spi(child_sa, FALSE)));
107
108 if (all)
109 {
110 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
111 child_sa->get_reqid(child_sa));
112
113
114 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
115 {
116 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
117
118 if (encr_len)
119 {
120 fprintf(out, "-%d", encr_len);
121 }
122 fprintf(out, "/");
123 }
124
125 fprintf(out, "%N", integrity_algorithm_names, int_alg);
126 if (int_len)
127 {
128 fprintf(out, "-%d", int_len);
129 }
130 fprintf(out, ", rekeying ");
131
132 if (rekey)
133 {
134 fprintf(out, "in %#V", &now, &rekey);
135 }
136 else
137 {
138 fprintf(out, "disabled");
139 }
140
141 fprintf(out, ", last use: ");
142 use_in = max(use_in, use_fwd);
143 if (use_in)
144 {
145 fprintf(out, "%ds_i ", now - use_in);
146 }
147 else
148 {
149 fprintf(out, "no_i ");
150 }
151 if (use_out)
152 {
153 fprintf(out, "%ds_o ", now - use_out);
154 }
155 else
156 {
157 fprintf(out, "no_o ");
158 }
159 }
160 }
161
162 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
163 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
164 child_sa->get_traffic_selectors(child_sa, TRUE),
165 child_sa->get_traffic_selectors(child_sa, FALSE));
166 }
167
168 /**
169 * Implementation of stroke_list_t.status.
170 */
171 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
172 {
173 enumerator_t *enumerator, *children;
174 iterator_t *iterator;
175 host_t *host;
176 peer_cfg_t *peer_cfg;
177 ike_cfg_t *ike_cfg;
178 child_cfg_t *child_cfg;
179 ike_sa_t *ike_sa;
180 char *name = NULL;
181
182 name = msg->status.name;
183
184 if (all)
185 {
186 fprintf(out, "Performance:\n");
187 fprintf(out, " worker threads: %d idle of %d,",
188 charon->processor->get_idle_threads(charon->processor),
189 charon->processor->get_total_threads(charon->processor));
190 fprintf(out, " job queue load: %d,",
191 charon->processor->get_job_load(charon->processor));
192 fprintf(out, " scheduled events: %d\n",
193 charon->scheduler->get_job_load(charon->scheduler));
194 iterator = charon->kernel_interface->create_address_iterator(
195 charon->kernel_interface);
196 fprintf(out, "Listening IP addresses:\n");
197 while (iterator->iterate(iterator, (void**)&host))
198 {
199 fprintf(out, " %H\n", host);
200 }
201 iterator->destroy(iterator);
202
203 fprintf(out, "Connections:\n");
204 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
205 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
206 {
207 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
208 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
209 {
210 continue;
211 }
212
213 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
214 fprintf(out, "%12s: %H[%D]...%H[%D]\n", peer_cfg->get_name(peer_cfg),
215 ike_cfg->get_my_host(ike_cfg), peer_cfg->get_my_id(peer_cfg),
216 ike_cfg->get_other_host(ike_cfg), peer_cfg->get_other_id(peer_cfg));
217 /* TODO: list CAs and groups */
218 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
219 while (children->enumerate(children, &child_cfg))
220 {
221 linked_list_t *my_ts, *other_ts;
222 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
223 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
224 fprintf(out, "%12s: %#R=== %#R\n", child_cfg->get_name(child_cfg),
225 my_ts, other_ts);
226 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
227 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
228 }
229 children->destroy(children);
230 }
231 enumerator->destroy(enumerator);
232 }
233
234 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
235 if (all && iterator->get_count(iterator) > 0)
236 {
237 fprintf(out, "Security Associations:\n");
238 }
239 while (iterator->iterate(iterator, (void**)&ike_sa))
240 {
241 bool ike_printed = FALSE;
242 child_sa_t *child_sa;
243 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
244
245 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
246 {
247 log_ike_sa(out, ike_sa, all);
248 ike_printed = TRUE;
249 }
250
251 while (children->iterate(children, (void**)&child_sa))
252 {
253 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
254 {
255 if (!ike_printed)
256 {
257 log_ike_sa(out, ike_sa, all);
258 ike_printed = TRUE;
259 }
260 log_child_sa(out, child_sa, all);
261 }
262 }
263 children->destroy(children);
264 }
265 iterator->destroy(iterator);
266 }
267
268 /**
269 * list all X.509 certificates matching the flags
270 */
271 static void stroke_list_certs(char *label, x509_flag_t flags, bool utc, FILE *out)
272 {
273 bool first = TRUE;
274 time_t now = time(NULL);
275 certificate_t *cert;
276 enumerator_t *enumerator;
277
278 enumerator = charon->credentials->create_cert_enumerator(
279 charon->credentials, CERT_X509, KEY_ANY, NULL, FALSE);
280 while (enumerator->enumerate(enumerator, (void**)&cert))
281 {
282 x509_t *x509 = (x509_t*)cert;
283 x509_flag_t x509_flags = x509->get_flags(x509);
284
285 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
286 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
287 {
288 enumerator_t *enumerator;
289 identification_t *altName;
290 bool first_altName = TRUE;
291 chunk_t serial = x509->get_serial(x509);
292 identification_t *authkey = x509->get_authKeyIdentifier(x509);
293 time_t notBefore, notAfter;
294 public_key_t *public = cert->get_public_key(cert);
295
296 if (first)
297 {
298 fprintf(out, "\n");
299 fprintf(out, "List of %s:\n", label);
300 first = FALSE;
301 }
302 fprintf(out, "\n");
303
304 /* list subjectAltNames */
305 enumerator = x509->create_subjectAltName_enumerator(x509);
306 while (enumerator->enumerate(enumerator, (void**)&altName))
307 {
308 if (first_altName)
309 {
310 fprintf(out, " altNames: ");
311 first_altName = FALSE;
312 }
313 else
314 {
315 fprintf(out, ", ");
316 }
317 fprintf(out, "%D", altName);
318 }
319 if (!first_altName)
320 {
321 fprintf(out, "\n");
322 }
323 enumerator->destroy(enumerator);
324
325 fprintf(out, " subject: %D\n", cert->get_subject(cert));
326 fprintf(out, " issuer: %D\n", cert->get_issuer(cert));
327 fprintf(out, " serial: %#B\n", &serial);
328
329 /* list validity */
330 cert->get_validity(cert, &now, &notBefore, &notAfter);
331 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
332 if (now < notBefore)
333 {
334 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
335 }
336 else
337 {
338 fprintf(out, "ok\n");
339 }
340 fprintf(out, " not after %#T, ", &notAfter, utc);
341 if (now > notAfter)
342 {
343 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
344 }
345 else
346 {
347 fprintf(out, "ok");
348 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
349 {
350 fprintf(out, " (expires in %#V)", &now, &notAfter);
351 }
352 fprintf(out, " \n");
353 }
354
355 /* list public key information */
356 if (public)
357 {
358 private_key_t *private = NULL;
359 identification_t *id, *keyid;
360
361 id = public->get_id(public, ID_PUBKEY_SHA1);
362 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
363
364 private = charon->credentials->get_private(
365 charon->credentials,
366 public->get_type(public), keyid, NULL);
367 fprintf(out, " pubkey: %N %d bits%s\n",
368 key_type_names, public->get_type(public),
369 public->get_keysize(public) * 8,
370 private ? ", has private key" : "");
371 fprintf(out, " keyid: %D\n", keyid);
372 fprintf(out, " subjkey: %D\n", id);
373 DESTROY_IF(private);
374 public->destroy(public);
375 }
376
377 /* list optional authorityKeyIdentifier */
378 if (authkey)
379 {
380 fprintf(out, " authkey: %D\n", authkey);
381 }
382 }
383 }
384 enumerator->destroy(enumerator);
385 }
386
387 /**
388 * list all X.509 CRLs
389 */
390 static void stroke_list_crls(bool utc, FILE *out)
391 {
392 bool first = TRUE;
393 time_t thisUpdate, nextUpdate, now = time(NULL);
394 certificate_t *cert;
395 enumerator_t *enumerator;
396
397 enumerator = charon->credentials->create_cert_enumerator(
398 charon->credentials, CERT_X509_CRL, KEY_ANY, NULL, FALSE);
399 while (enumerator->enumerate(enumerator, (void**)&cert))
400 {
401 crl_t *crl = (crl_t*)cert;
402 chunk_t serial = crl->get_serial(crl);
403 identification_t *authkey = crl->get_authKeyIdentifier(crl);
404
405 if (first)
406 {
407 fprintf(out, "\n");
408 fprintf(out, "List of X.509 CRLs:\n");
409 first = FALSE;
410 }
411 fprintf(out, "\n");
412
413 fprintf(out, " issuer: %D\n", cert->get_issuer(cert));
414
415 /* list optional crlNumber */
416 if (serial.ptr)
417 {
418 fprintf(out, " serial: %#B\n", &serial);
419 }
420
421 /* count the number of revoked certificates */
422 {
423 int count = 0;
424 enumerator_t *enumerator = crl->create_enumerator(crl);
425
426 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
427 {
428 count++;
429 }
430 fprintf(out, " revoked: %d certificate%s\n", count,
431 (count == 1)? "" : "s");
432 enumerator->destroy(enumerator);
433 }
434
435 /* list validity */
436 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
437 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
438 fprintf(out, " next %#T, ", &nextUpdate, utc);
439 if (now > nextUpdate)
440 {
441 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
442 }
443 else
444 {
445 fprintf(out, "ok");
446 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
447 {
448 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
449 }
450 fprintf(out, " \n");
451 }
452
453 /* list optional authorityKeyIdentifier */
454 if (authkey)
455 {
456 fprintf(out, " authkey: %D\n", authkey);
457 }
458 }
459 enumerator->destroy(enumerator);
460 }
461
462 /**
463 * Implementation of stroke_list_t.list.
464 */
465 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
466 {
467 if (msg->list.flags & LIST_CERTS)
468 {
469 stroke_list_certs("X.509 End Entity Certificates",
470 0, msg->list.utc, out);
471 }
472 if (msg->list.flags & LIST_CACERTS)
473 {
474 stroke_list_certs("X.509 CA Certificates",
475 X509_CA, msg->list.utc, out);
476 }
477 if (msg->list.flags & LIST_OCSPCERTS)
478 {
479 stroke_list_certs("X.509 OCSP Signer Certificates",
480 X509_OCSP_SIGNER, msg->list.utc, out);
481 }
482 if (msg->list.flags & LIST_AACERTS)
483 {
484 stroke_list_certs("X.509 AA Certificates",
485 X509_AA, msg->list.utc, out);
486 }
487 if (msg->list.flags & LIST_ACERTS)
488 {
489
490 }
491 if (msg->list.flags & LIST_CRLS)
492 {
493 stroke_list_crls(msg->list.utc, out);
494 }
495 if (msg->list.flags & LIST_OCSP)
496 {
497
498 }
499 }
500
501 /**
502 * Implementation of stroke_list_t.destroy
503 */
504 static void destroy(private_stroke_list_t *this)
505 {
506 free(this);
507 }
508
509 /*
510 * see header file
511 */
512 stroke_list_t *stroke_list_create()
513 {
514 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
515
516 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
517 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
518 this->public.destroy = (void(*)(stroke_list_t*))destroy;
519
520 return &this->public;
521 }
522