fce9bfeb7e14b69190c17a3439781bd039dd3ffb
[strongswan.git] / src / charon / control / interfaces / stroke_interface.c
1 /**
2 * @file stroke_interface.c
3 *
4 * @brief Implementation of stroke_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Tobias Brunner
10 * Copyright (C) 2006-2007 Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/fcntl.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <signal.h>
35
36 #include "stroke_interface.h"
37
38 #include <library.h>
39 #include <stroke.h>
40 #include <daemon.h>
41 #include <crypto/x509.h>
42 #include <crypto/ietf_attr_list.h>
43 #include <crypto/ac.h>
44 #include <crypto/ca.h>
45 #include <crypto/crl.h>
46 #include <control/interface_manager.h>
47 #include <control/interfaces/interface.h>
48 #include <utils/leak_detective.h>
49 #include <processing/jobs/callback_job.h>
50
51 #define IKE_PORT 500
52 #define PATH_BUF 256
53 #define STROKE_THREADS 3
54
55 typedef struct private_stroke_interface_t private_stroke_interface_t;
56
57 /**
58 * Private data of an stroke_interfacet object.
59 */
60 struct private_stroke_interface_t {
61
62 /**
63 * Public part of stroke_interfacet object.
64 */
65 stroke_interface_t public;
66
67 /**
68 * Unix socket to listen for strokes
69 */
70 int socket;
71
72 /**
73 * job accepting stroke messages
74 */
75 callback_job_t *job;
76 };
77
78 typedef struct stroke_log_info_t stroke_log_info_t;
79
80 /**
81 * helper struct to say what and where to log when using controller callback
82 */
83 struct stroke_log_info_t {
84
85 /**
86 * level to log up to
87 */
88 level_t level;
89
90 /**
91 * where to write log
92 */
93 FILE* out;
94 };
95
96 /**
97 * Helper function which corrects the string pointers
98 * in a stroke_msg_t. Strings in a stroke_msg sent over "wire"
99 * contains RELATIVE addresses (relative to the beginning of the
100 * stroke_msg). They must be corrected if they reach our address
101 * space...
102 */
103 static void pop_string(stroke_msg_t *msg, char **string)
104 {
105 if (*string == NULL)
106 return;
107
108 /* check for sanity of string pointer and string */
109 if (string < (char**)msg
110 || string > (char**)msg + sizeof(stroke_msg_t)
111 || (unsigned long)*string < (unsigned long)((char*)msg->buffer - (char*)msg)
112 || (unsigned long)*string > msg->length)
113 {
114 *string = "(invalid pointer in stroke msg)";
115 }
116 else
117 {
118 *string = (char*)msg + (unsigned long)*string;
119 }
120 }
121
122 /**
123 * Load end entitity certificate
124 */
125 static x509_t* load_end_certificate(const char *filename, identification_t **idp)
126 {
127 char path[PATH_BUF];
128 x509_t *cert;
129
130 if (*filename == '/')
131 {
132 /* absolute path name */
133 snprintf(path, sizeof(path), "%s", filename);
134 }
135 else
136 {
137 /* relative path name */
138 snprintf(path, sizeof(path), "%s/%s", CERTIFICATE_DIR, filename);
139 }
140
141 cert = x509_create_from_file(path, "end entity");
142
143 if (cert)
144 {
145 identification_t *id = *idp;
146 identification_t *subject = cert->get_subject(cert);
147
148 err_t ugh = cert->is_valid(cert, NULL);
149
150 if (ugh != NULL)
151 {
152 DBG1(DBG_CFG, "warning: certificate %s", ugh);
153 }
154 if (!id->equals(id, subject) && !cert->equals_subjectAltName(cert, id))
155 {
156 id->destroy(id);
157 id = subject;
158 *idp = id->clone(id);
159 }
160 return charon->credentials->add_end_certificate(charon->credentials, cert);
161 }
162 return NULL;
163 }
164
165 /**
166 * Load ca certificate
167 */
168 static x509_t* load_ca_certificate(const char *filename)
169 {
170 char path[PATH_BUF];
171 x509_t *cert;
172
173 if (*filename == '/')
174 {
175 /* absolute path name */
176 snprintf(path, sizeof(path), "%s", filename);
177 }
178 else
179 {
180 /* relative path name */
181 snprintf(path, sizeof(path), "%s/%s", CA_CERTIFICATE_DIR, filename);
182 }
183
184 cert = x509_create_from_file(path, "ca");
185
186 if (cert)
187 {
188 if (cert->is_ca(cert))
189 {
190 return charon->credentials->add_auth_certificate(charon->credentials, cert, AUTH_CA);
191 }
192 else
193 {
194 DBG1(DBG_CFG, " CA basic constraints flag not set, cert discarded");
195 cert->destroy(cert);
196 }
197 }
198 return NULL;
199 }
200
201 /**
202 * Pop the strings of a stroke_end_t struct and log them for debugging purposes
203 */
204 static void pop_end(stroke_msg_t *msg, const char* label, stroke_end_t *end)
205 {
206 pop_string(msg, &end->address);
207 pop_string(msg, &end->subnet);
208 pop_string(msg, &end->sourceip);
209 pop_string(msg, &end->id);
210 pop_string(msg, &end->cert);
211 pop_string(msg, &end->ca);
212 pop_string(msg, &end->groups);
213 pop_string(msg, &end->updown);
214
215 DBG2(DBG_CFG, " %s=%s", label, end->address);
216 DBG2(DBG_CFG, " %ssubnet=%s", label, end->subnet);
217 DBG2(DBG_CFG, " %ssourceip=%s", label, end->sourceip);
218 DBG2(DBG_CFG, " %sid=%s", label, end->id);
219 DBG2(DBG_CFG, " %scert=%s", label, end->cert);
220 DBG2(DBG_CFG, " %sca=%s", label, end->ca);
221 DBG2(DBG_CFG, " %sgroups=%s", label, end->groups);
222 DBG2(DBG_CFG, " %supdown=%s", label, end->updown);
223 }
224
225 /**
226 * Add a connection to the configuration list
227 */
228 static void stroke_add_conn(stroke_msg_t *msg, FILE *out)
229 {
230 ike_cfg_t *ike_cfg;
231 peer_cfg_t *peer_cfg;
232 peer_cfg_t *mediated_by_cfg = NULL;
233 child_cfg_t *child_cfg;
234 identification_t *my_id, *other_id;
235 identification_t *my_ca = NULL;
236 identification_t *other_ca = NULL;
237 identification_t *peer_id = NULL;
238 bool my_ca_same = FALSE;
239 bool other_ca_same =FALSE;
240 host_t *my_host, *other_host, *my_subnet, *other_subnet;
241 host_t *my_vip = NULL, *other_vip = NULL;
242 linked_list_t *my_groups = linked_list_create();
243 linked_list_t *other_groups = linked_list_create();
244 proposal_t *proposal;
245 traffic_selector_t *my_ts, *other_ts;
246 char *interface;
247 bool use_existing = FALSE;
248 iterator_t *iterator;
249
250 pop_string(msg, &msg->add_conn.name);
251 DBG1(DBG_CFG, "received stroke: add connection '%s'", msg->add_conn.name);
252 DBG2(DBG_CFG, "conn %s", msg->add_conn.name);
253 pop_end(msg, "left", &msg->add_conn.me);
254 pop_end(msg, "right", &msg->add_conn.other);
255 pop_string(msg, &msg->add_conn.algorithms.ike);
256 pop_string(msg, &msg->add_conn.algorithms.esp);
257 DBG2(DBG_CFG, " ike=%s", msg->add_conn.algorithms.ike);
258 DBG2(DBG_CFG, " esp=%s", msg->add_conn.algorithms.esp);
259 pop_string(msg, &msg->add_conn.p2p.mediated_by);
260 pop_string(msg, &msg->add_conn.p2p.peerid);
261 DBG2(DBG_CFG, " p2p_mediation=%s", msg->add_conn.p2p.mediation ? "yes" : "no");
262 DBG2(DBG_CFG, " p2p_mediated_by=%s", msg->add_conn.p2p.mediated_by);
263 DBG2(DBG_CFG, " p2p_peerid=%s", msg->add_conn.p2p.peerid);
264
265 my_host = msg->add_conn.me.address?
266 host_create_from_string(msg->add_conn.me.address, IKE_PORT) : NULL;
267 if (my_host == NULL)
268 {
269 DBG1(DBG_CFG, "invalid host: %s\n", msg->add_conn.me.address);
270 return;
271 }
272
273 other_host = msg->add_conn.other.address ?
274 host_create_from_string(msg->add_conn.other.address, IKE_PORT) : NULL;
275 if (other_host == NULL)
276 {
277 DBG1(DBG_CFG, "invalid host: %s\n", msg->add_conn.other.address);
278 my_host->destroy(my_host);
279 return;
280 }
281
282 interface = charon->kernel_interface->get_interface(charon->kernel_interface,
283 other_host);
284 if (interface)
285 {
286 stroke_end_t tmp_end;
287 host_t *tmp_host;
288
289 DBG2(DBG_CFG, "left is other host, swapping ends\n");
290
291 tmp_host = my_host;
292 my_host = other_host;
293 other_host = tmp_host;
294
295 tmp_end = msg->add_conn.me;
296 msg->add_conn.me = msg->add_conn.other;
297 msg->add_conn.other = tmp_end;
298 free(interface);
299 }
300 else
301 {
302 interface = charon->kernel_interface->get_interface(
303 charon->kernel_interface, my_host);
304 if (!interface)
305 {
306 DBG1(DBG_CFG, "left nor right host is our side, assuming left=local");
307 }
308 else
309 {
310 free(interface);
311 }
312 }
313
314 my_id = identification_create_from_string(msg->add_conn.me.id ?
315 msg->add_conn.me.id : msg->add_conn.me.address);
316 if (my_id == NULL)
317 {
318 DBG1(DBG_CFG, "invalid ID: %s\n", msg->add_conn.me.id);
319 goto destroy_hosts;
320 }
321
322 other_id = identification_create_from_string(msg->add_conn.other.id ?
323 msg->add_conn.other.id : msg->add_conn.other.address);
324 if (other_id == NULL)
325 {
326 DBG1(DBG_CFG, "invalid ID: %s\n", msg->add_conn.other.id);
327 my_id->destroy(my_id);
328 goto destroy_hosts;
329 }
330
331 #ifdef P2P
332 if (msg->add_conn.p2p.mediation && msg->add_conn.p2p.mediated_by)
333 {
334 DBG1(DBG_CFG, "a mediation connection cannot be a"
335 " mediated connection at the same time, aborting");
336 goto destroy_ids;
337 }
338
339 if (msg->add_conn.p2p.mediated_by)
340 {
341 mediated_by_cfg = charon->backends->get_peer_cfg_by_name(charon->backends, msg->add_conn.p2p.mediated_by);
342 if (!mediated_by_cfg)
343 {
344 DBG1(DBG_CFG, "mediation connection '%s' not found, aborting",
345 msg->add_conn.p2p.mediated_by);
346 goto destroy_ids;
347 }
348
349 if (!mediated_by_cfg->is_mediation(mediated_by_cfg))
350 {
351 DBG1(DBG_CFG, "connection '%s' as referred to by '%s' is"
352 "no mediation connection, aborting",
353 msg->add_conn.p2p.mediated_by, msg->add_conn.name);
354 goto destroy_ids;
355 }
356 }
357
358 if (msg->add_conn.p2p.peerid)
359 {
360 peer_id = identification_create_from_string(msg->add_conn.p2p.peerid);
361 if (!peer_id)
362 {
363 DBG1(DBG_CFG, "invalid peer ID: %s\n", msg->add_conn.p2p.peerid);
364 goto destroy_ids;
365 }
366 }
367 else
368 #endif /* P2P */
369 {
370 // no peer ID supplied, assume right ID
371 peer_id = other_id->clone(other_id);
372 }
373
374 my_subnet = host_create_from_string(msg->add_conn.me.subnet ?
375 msg->add_conn.me.subnet : msg->add_conn.me.address, IKE_PORT);
376 if (my_subnet == NULL)
377 {
378 DBG1(DBG_CFG, "invalid subnet: %s\n", msg->add_conn.me.subnet);
379 goto destroy_ids;
380 }
381
382 other_subnet = host_create_from_string(msg->add_conn.other.subnet ?
383 msg->add_conn.other.subnet : msg->add_conn.other.address, IKE_PORT);
384 if (other_subnet == NULL)
385 {
386 DBG1(DBG_CFG, "invalid subnet: %s\n", msg->add_conn.me.subnet);
387 my_subnet->destroy(my_subnet);
388 goto destroy_ids;
389 }
390
391 if (msg->add_conn.me.virtual_ip && msg->add_conn.me.sourceip)
392 {
393 my_vip = host_create_from_string(msg->add_conn.me.sourceip, 0);
394 }
395 if (msg->add_conn.other.virtual_ip && msg->add_conn.other.sourceip)
396 {
397 other_vip = host_create_from_string(msg->add_conn.other.sourceip, 0);
398 }
399
400 if (msg->add_conn.me.tohost)
401 {
402 my_ts = traffic_selector_create_dynamic(msg->add_conn.me.protocol,
403 my_host->get_family(my_host) == AF_INET ?
404 TS_IPV4_ADDR_RANGE : TS_IPV6_ADDR_RANGE,
405 msg->add_conn.me.port ? msg->add_conn.me.port : 0,
406 msg->add_conn.me.port ? msg->add_conn.me.port : 65535);
407 }
408 else
409 {
410 my_ts = traffic_selector_create_from_subnet(my_subnet,
411 msg->add_conn.me.subnet ? msg->add_conn.me.subnet_mask : 0,
412 msg->add_conn.me.protocol, msg->add_conn.me.port);
413 }
414 my_subnet->destroy(my_subnet);
415
416 if (msg->add_conn.other.tohost)
417 {
418 other_ts = traffic_selector_create_dynamic(msg->add_conn.other.protocol,
419 other_host->get_family(other_host) == AF_INET ?
420 TS_IPV4_ADDR_RANGE : TS_IPV6_ADDR_RANGE,
421 msg->add_conn.other.port ? msg->add_conn.other.port : 0,
422 msg->add_conn.other.port ? msg->add_conn.other.port : 65535);
423 }
424 else
425 {
426 other_ts = traffic_selector_create_from_subnet(other_subnet,
427 msg->add_conn.other.subnet ? msg->add_conn.other.subnet_mask : 0,
428 msg->add_conn.other.protocol, msg->add_conn.other.port);
429 }
430 other_subnet->destroy(other_subnet);
431
432 if (msg->add_conn.me.ca)
433 {
434 if (streq(msg->add_conn.me.ca, "%same"))
435 {
436 my_ca_same = TRUE;
437 }
438 else
439 {
440 my_ca = identification_create_from_string(msg->add_conn.me.ca);
441 }
442 }
443 if (msg->add_conn.other.ca)
444 {
445 if (streq(msg->add_conn.other.ca, "%same"))
446 {
447 other_ca_same = TRUE;
448 }
449 else
450 {
451 other_ca = identification_create_from_string(msg->add_conn.other.ca);
452 }
453 }
454 if (msg->add_conn.me.cert)
455 {
456 x509_t *cert = load_end_certificate(msg->add_conn.me.cert, &my_id);
457
458 if (cert)
459 {
460 ca_info_t *ca_info;
461
462 if (cert->is_self_signed(cert))
463 {
464 /* a self-signed certificate is its own ca */
465 ca_info = ca_info_create(NULL, cert);
466 ca_info = charon->credentials->add_ca_info(charon->credentials, ca_info);
467 cert->set_ca_info(cert, ca_info);
468 }
469 else
470 {
471 /* get_issuer() automatically sets cert->ca_info */
472 ca_info = charon->credentials->get_issuer(charon->credentials, cert);
473 }
474 if (my_ca == NULL && !my_ca_same)
475 {
476 identification_t *issuer = cert->get_issuer(cert);
477
478 my_ca = issuer->clone(issuer);
479 }
480 }
481 }
482 if (msg->add_conn.other.cert)
483 {
484 x509_t *cert = load_end_certificate(msg->add_conn.other.cert, &other_id);
485
486 if (cert)
487 {
488 ca_info_t *ca_info;
489
490 if (cert->is_self_signed(cert))
491 {
492 /* a self-signed certificate is its own ca */
493 ca_info = ca_info_create(NULL, cert);
494 ca_info = charon->credentials->add_ca_info(charon->credentials, ca_info);
495 cert->set_ca_info(cert, ca_info);
496 }
497 else
498 {
499 /* get_issuer() automatically sets cert->ca_info */
500 ca_info = charon->credentials->get_issuer(charon->credentials, cert);
501 }
502 if (other_ca == NULL && !other_ca_same)
503 {
504 identification_t *issuer = cert->get_issuer(cert);
505
506 other_ca = issuer->clone(issuer);
507 }
508 }
509 }
510 if (other_ca_same && my_ca)
511 {
512 other_ca = my_ca->clone(my_ca);
513 }
514 else if (my_ca_same && other_ca)
515 {
516 my_ca = other_ca->clone(other_ca);
517 }
518 if (my_ca == NULL)
519 {
520 my_ca = identification_create_from_string("%any");
521 }
522 if (other_ca == NULL)
523 {
524 other_ca = identification_create_from_string("%any");
525 }
526 DBG2(DBG_CFG, " my ca: '%D'", my_ca);
527 DBG2(DBG_CFG, " other ca:'%D'", other_ca);
528
529 if (msg->add_conn.other.groups)
530 {
531 ietfAttr_list_create_from_string(msg->add_conn.other.groups, other_groups);
532 }
533
534 /* have a look for an (almost) identical peer config to reuse */
535 iterator = charon->backends->create_iterator(charon->backends);
536 while (iterator->iterate(iterator, (void**)&peer_cfg))
537 {
538 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
539 if (my_id->equals(my_id, peer_cfg->get_my_id(peer_cfg))
540 && other_id->equals(other_id, peer_cfg->get_other_id(peer_cfg))
541 && my_host->equals(my_host, ike_cfg->get_my_host(ike_cfg))
542 && other_host->equals(other_host, ike_cfg->get_other_host(ike_cfg))
543 && other_ca->equals(other_ca, peer_cfg->get_other_ca(peer_cfg))
544 && ietfAttr_list_equals(other_groups, peer_cfg->get_groups(peer_cfg))
545 && peer_cfg->get_ike_version(peer_cfg) == (msg->add_conn.ikev2 ? 2 : 1)
546 && peer_cfg->get_auth_method(peer_cfg) == msg->add_conn.auth_method
547 && peer_cfg->get_eap_type(peer_cfg) == msg->add_conn.eap_type)
548 {
549 DBG1(DBG_CFG, "reusing existing configuration '%s'",
550 peer_cfg->get_name(peer_cfg));
551 use_existing = TRUE;
552 break;
553 }
554 }
555 iterator->destroy(iterator);
556
557 if (use_existing)
558 {
559 DESTROY_IF(my_vip);
560 DESTROY_IF(other_vip);
561 my_host->destroy(my_host);
562 my_id->destroy(my_id);
563 my_ca->destroy(my_ca);
564 other_host->destroy(other_host);
565 other_id->destroy(other_id);
566 other_ca->destroy(other_ca);
567 peer_id->destroy(peer_id);
568 DESTROY_IF(mediated_by_cfg);
569 ietfAttr_list_destroy(my_groups);
570 ietfAttr_list_destroy(other_groups);
571 }
572 else
573 {
574 ike_cfg = ike_cfg_create(msg->add_conn.other.sendcert != CERT_NEVER_SEND,
575 msg->add_conn.force_encap, my_host, other_host);
576
577 if (msg->add_conn.algorithms.ike)
578 {
579 char *proposal_string;
580 char *strict = msg->add_conn.algorithms.ike + strlen(msg->add_conn.algorithms.ike) - 1;
581
582 if (*strict == '!')
583 *strict = '\0';
584 else
585 strict = NULL;
586
587 while ((proposal_string = strsep(&msg->add_conn.algorithms.ike, ",")))
588 {
589 proposal = proposal_create_from_string(PROTO_IKE, proposal_string);
590 if (proposal == NULL)
591 {
592 DBG1(DBG_CFG, "invalid IKE proposal string: %s", proposal_string);
593 my_id->destroy(my_id);
594 other_id->destroy(other_id);
595 my_ts->destroy(my_ts);
596 other_ts->destroy(other_ts);
597 my_ca->destroy(my_ca);
598 other_ca->destroy(other_ca);
599 ike_cfg->destroy(ike_cfg);
600 return;
601 }
602 ike_cfg->add_proposal(ike_cfg, proposal);
603 }
604 if (!strict)
605 {
606 proposal = proposal_create_default(PROTO_IKE);
607 ike_cfg->add_proposal(ike_cfg, proposal);
608 }
609 }
610 else
611 {
612 proposal = proposal_create_default(PROTO_IKE);
613 ike_cfg->add_proposal(ike_cfg, proposal);
614 }
615
616
617 peer_cfg = peer_cfg_create(msg->add_conn.name, msg->add_conn.ikev2 ? 2 : 1,
618 ike_cfg, my_id, other_id, my_ca, other_ca, other_groups,
619 msg->add_conn.me.sendcert,
620 msg->add_conn.auth_method, msg->add_conn.eap_type,
621 msg->add_conn.rekey.tries, msg->add_conn.rekey.ike_lifetime,
622 msg->add_conn.rekey.ike_lifetime - msg->add_conn.rekey.margin,
623 msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100,
624 msg->add_conn.rekey.reauth, msg->add_conn.mobike,
625 msg->add_conn.dpd.delay, msg->add_conn.dpd.action, my_vip, other_vip,
626 msg->add_conn.p2p.mediation, mediated_by_cfg, peer_id);
627 }
628
629 child_cfg = child_cfg_create(
630 msg->add_conn.name, msg->add_conn.rekey.ipsec_lifetime,
631 msg->add_conn.rekey.ipsec_lifetime - msg->add_conn.rekey.margin,
632 msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100,
633 msg->add_conn.me.updown, msg->add_conn.me.hostaccess,
634 msg->add_conn.mode);
635
636 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
637
638 child_cfg->add_traffic_selector(child_cfg, TRUE, my_ts);
639 child_cfg->add_traffic_selector(child_cfg, FALSE, other_ts);
640
641 if (msg->add_conn.algorithms.esp)
642 {
643 char *proposal_string;
644 char *strict = msg->add_conn.algorithms.esp + strlen(msg->add_conn.algorithms.esp) - 1;
645
646 if (*strict == '!')
647 *strict = '\0';
648 else
649 strict = NULL;
650
651 while ((proposal_string = strsep(&msg->add_conn.algorithms.esp, ",")))
652 {
653 proposal = proposal_create_from_string(PROTO_ESP, proposal_string);
654 if (proposal == NULL)
655 {
656 DBG1(DBG_CFG, "invalid ESP proposal string: %s", proposal_string);
657 peer_cfg->destroy(peer_cfg);
658 return;
659 }
660 child_cfg->add_proposal(child_cfg, proposal);
661 }
662 if (!strict)
663 {
664 proposal = proposal_create_default(PROTO_ESP);
665 child_cfg->add_proposal(child_cfg, proposal);
666 }
667 }
668 else
669 {
670 proposal = proposal_create_default(PROTO_ESP);
671 child_cfg->add_proposal(child_cfg, proposal);
672 }
673
674 if (!use_existing)
675 {
676 /* add config to backend */
677 charon->backends->add_peer_cfg(charon->backends, peer_cfg);
678 DBG1(DBG_CFG, "added configuration '%s': %H[%D]...%H[%D]",
679 msg->add_conn.name, my_host, my_id, other_host, other_id);
680 }
681 return;
682
683 /* mopping up after parsing errors */
684
685 destroy_ids:
686 my_id->destroy(my_id);
687 other_id->destroy(other_id);
688 DESTROY_IF(mediated_by_cfg);
689 DESTROY_IF(peer_id);
690
691 destroy_hosts:
692 my_host->destroy(my_host);
693 other_host->destroy(other_host);
694 }
695
696 /**
697 * Delete a connection from the list
698 */
699 static void stroke_del_conn(stroke_msg_t *msg, FILE *out)
700 {
701 iterator_t *peer_iter, *child_iter;
702 peer_cfg_t *peer, *child;
703
704 pop_string(msg, &(msg->del_conn.name));
705 DBG1(DBG_CFG, "received stroke: delete connection '%s'", msg->del_conn.name);
706
707 peer_iter = charon->backends->create_iterator(charon->backends);
708 while (peer_iter->iterate(peer_iter, (void**)&peer))
709 {
710 /* remove peer config with such a name */
711 if (streq(peer->get_name(peer), msg->del_conn.name))
712 {
713 peer_iter->remove(peer_iter);
714 peer->destroy(peer);
715 continue;
716 }
717 /* remove any child with such a name */
718 child_iter = peer->create_child_cfg_iterator(peer);
719 while (child_iter->iterate(child_iter, (void**)&child))
720 {
721 if (streq(child->get_name(child), msg->del_conn.name))
722 {
723 child_iter->remove(child_iter);
724 child->destroy(child);
725 }
726 }
727 child_iter->destroy(child_iter);
728 }
729 peer_iter->destroy(peer_iter);
730
731 fprintf(out, "deleted connection '%s'\n", msg->del_conn.name);
732 }
733
734 /**
735 * get the child_cfg with the same name as the peer cfg
736 */
737 static child_cfg_t* get_child_from_peer(peer_cfg_t *peer_cfg, char *name)
738 {
739 child_cfg_t *current, *found = NULL;
740 iterator_t *iterator;
741
742 iterator = peer_cfg->create_child_cfg_iterator(peer_cfg);
743 while (iterator->iterate(iterator, (void**)&current))
744 {
745 if (streq(current->get_name(current), name))
746 {
747 found = current;
748 found->get_ref(found);
749 break;
750 }
751 }
752 iterator->destroy(iterator);
753 return found;
754 }
755
756 /**
757 * logging to the stroke interface
758 */
759 static bool stroke_log(stroke_log_info_t *info, signal_t signal, level_t level,
760 ike_sa_t *ike_sa, char *format, va_list args)
761 {
762 if (level <= info->level)
763 {
764 if (vfprintf(info->out, format, args) < 0 ||
765 fprintf(info->out, "\n") < 0 ||
766 fflush(info->out) != 0)
767 {
768 return FALSE;
769 }
770 }
771 return TRUE;
772 }
773
774 /**
775 * initiate a connection by name
776 */
777 static void stroke_initiate(stroke_msg_t *msg, FILE *out)
778 {
779 peer_cfg_t *peer_cfg;
780 child_cfg_t *child_cfg;
781 stroke_log_info_t info;
782
783 pop_string(msg, &(msg->initiate.name));
784 DBG1(DBG_CFG, "received stroke: initiate '%s'", msg->initiate.name);
785
786 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
787 msg->initiate.name);
788 if (peer_cfg == NULL)
789 {
790 fprintf(out, "no config named '%s'\n", msg->initiate.name);
791 return;
792 }
793 if (peer_cfg->get_ike_version(peer_cfg) != 2)
794 {
795 DBG1(DBG_CFG, "ignoring initiation request for IKEv%d config",
796 peer_cfg->get_ike_version(peer_cfg));
797 peer_cfg->destroy(peer_cfg);
798 return;
799 }
800
801 child_cfg = get_child_from_peer(peer_cfg, msg->initiate.name);
802 if (child_cfg == NULL)
803 {
804 fprintf(out, "no child config named '%s'\n", msg->initiate.name);
805 peer_cfg->destroy(peer_cfg);
806 return;
807 }
808
809 if (msg->output_verbosity < 0)
810 {
811 charon->interfaces->initiate(charon->interfaces, peer_cfg, child_cfg,
812 NULL, NULL);
813 }
814 else
815 {
816 info.out = out;
817 info.level = msg->output_verbosity;
818 charon->interfaces->initiate(charon->interfaces, peer_cfg, child_cfg,
819 (interface_manager_cb_t)stroke_log, &info);
820 }
821 }
822
823 /**
824 * route a policy (install SPD entries)
825 */
826 static void stroke_route(stroke_msg_t *msg, FILE *out)
827 {
828 peer_cfg_t *peer_cfg;
829 child_cfg_t *child_cfg;
830 stroke_log_info_t info;
831
832 pop_string(msg, &(msg->route.name));
833 DBG1(DBG_CFG, "received stroke: route '%s'", msg->route.name);
834
835 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
836 msg->route.name);
837 if (peer_cfg == NULL)
838 {
839 fprintf(out, "no config named '%s'\n", msg->route.name);
840 return;
841 }
842 if (peer_cfg->get_ike_version(peer_cfg) != 2)
843 {
844 peer_cfg->destroy(peer_cfg);
845 return;
846 }
847
848 child_cfg = get_child_from_peer(peer_cfg, msg->route.name);
849 if (child_cfg == NULL)
850 {
851 fprintf(out, "no child config named '%s'\n", msg->route.name);
852 peer_cfg->destroy(peer_cfg);
853 return;
854 }
855
856 info.out = out;
857 info.level = msg->output_verbosity;
858 charon->interfaces->route(charon->interfaces, peer_cfg, child_cfg,
859 (interface_manager_cb_t)stroke_log, &info);
860 peer_cfg->destroy(peer_cfg);
861 child_cfg->destroy(child_cfg);
862 }
863
864 /**
865 * unroute a policy
866 */
867 static void stroke_unroute(stroke_msg_t *msg, FILE *out)
868 {
869 char *name;
870 ike_sa_t *ike_sa;
871 iterator_t *iterator;
872 stroke_log_info_t info;
873
874 pop_string(msg, &(msg->terminate.name));
875 name = msg->terminate.name;
876
877 info.out = out;
878 info.level = msg->output_verbosity;
879
880 iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
881 while (iterator->iterate(iterator, (void**)&ike_sa))
882 {
883 child_sa_t *child_sa;
884 iterator_t *children;
885 u_int32_t id;
886
887 children = ike_sa->create_child_sa_iterator(ike_sa);
888 while (children->iterate(children, (void**)&child_sa))
889 {
890 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
891 streq(name, child_sa->get_name(child_sa)))
892 {
893 id = child_sa->get_reqid(child_sa);
894 children->destroy(children);
895 iterator->destroy(iterator);
896 charon->interfaces->unroute(charon->interfaces, id,
897 (interface_manager_cb_t)stroke_log, &info);
898 return;
899 }
900 }
901 children->destroy(children);
902 }
903 iterator->destroy(iterator);
904 DBG1(DBG_CFG, "no such SA found");
905 }
906
907 /**
908 * terminate a connection by name
909 */
910 static void stroke_terminate(stroke_msg_t *msg, FILE *out)
911 {
912 char *string, *pos = NULL, *name = NULL;
913 u_int32_t id = 0;
914 bool child;
915 int len;
916 ike_sa_t *ike_sa;
917 iterator_t *iterator;
918 stroke_log_info_t info;
919
920 pop_string(msg, &(msg->terminate.name));
921 string = msg->terminate.name;
922 DBG1(DBG_CFG, "received stroke: terminate '%s'", string);
923
924 len = strlen(string);
925 if (len < 1)
926 {
927 DBG1(DBG_CFG, "error parsing string");
928 return;
929 }
930 switch (string[len-1])
931 {
932 case '}':
933 child = TRUE;
934 pos = strchr(string, '{');
935 break;
936 case ']':
937 child = FALSE;
938 pos = strchr(string, '[');
939 break;
940 default:
941 name = string;
942 child = FALSE;
943 break;
944 }
945
946 if (name)
947 {
948 /* is a single name */
949 }
950 else if (pos == string + len - 2)
951 { /* is name[] or name{} */
952 string[len-2] = '\0';
953 name = string;
954 }
955 else
956 { /* is name[123] or name{23} */
957 string[len-1] = '\0';
958 id = atoi(pos + 1);
959 if (id == 0)
960 {
961 DBG1(DBG_CFG, "error parsing string");
962 return;
963 }
964 }
965
966 info.out = out;
967 info.level = msg->output_verbosity;
968
969 iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
970 while (iterator->iterate(iterator, (void**)&ike_sa))
971 {
972 child_sa_t *child_sa;
973 iterator_t *children;
974
975 if (child)
976 {
977 children = ike_sa->create_child_sa_iterator(ike_sa);
978 while (children->iterate(children, (void**)&child_sa))
979 {
980 if ((name && streq(name, child_sa->get_name(child_sa))) ||
981 (id && id == child_sa->get_reqid(child_sa)))
982 {
983 id = child_sa->get_reqid(child_sa);
984 children->destroy(children);
985 iterator->destroy(iterator);
986
987 charon->interfaces->terminate_child(charon->interfaces, id,
988 (interface_manager_cb_t)stroke_log, &info);
989 return;
990 }
991 }
992 children->destroy(children);
993 }
994 else if ((name && streq(name, ike_sa->get_name(ike_sa))) ||
995 (id && id == ike_sa->get_unique_id(ike_sa)))
996 {
997 id = ike_sa->get_unique_id(ike_sa);
998 /* unlock manager first */
999 iterator->destroy(iterator);
1000
1001 charon->interfaces->terminate_ike(charon->interfaces, id,
1002 (interface_manager_cb_t)stroke_log, &info);
1003 return;
1004 }
1005
1006 }
1007 iterator->destroy(iterator);
1008 DBG1(DBG_CFG, "no such SA found");
1009 }
1010
1011 /**
1012 * Add a ca information record to the cainfo list
1013 */
1014 static void stroke_add_ca(stroke_msg_t *msg, FILE *out)
1015 {
1016 x509_t *cacert;
1017 ca_info_t *ca_info;
1018
1019 pop_string(msg, &msg->add_ca.name);
1020 pop_string(msg, &msg->add_ca.cacert);
1021 pop_string(msg, &msg->add_ca.crluri);
1022 pop_string(msg, &msg->add_ca.crluri2);
1023 pop_string(msg, &msg->add_ca.ocspuri);
1024 pop_string(msg, &msg->add_ca.ocspuri2);
1025
1026 DBG1(DBG_CFG, "received stroke: add ca '%s'", msg->add_ca.name);
1027
1028 DBG2(DBG_CFG, "ca %s", msg->add_ca.name);
1029 DBG2(DBG_CFG, " cacert=%s", msg->add_ca.cacert);
1030 DBG2(DBG_CFG, " crluri=%s", msg->add_ca.crluri);
1031 DBG2(DBG_CFG, " crluri2=%s", msg->add_ca.crluri2);
1032 DBG2(DBG_CFG, " ocspuri=%s", msg->add_ca.ocspuri);
1033 DBG2(DBG_CFG, " ocspuri2=%s", msg->add_ca.ocspuri2);
1034
1035 if (msg->add_ca.cacert == NULL)
1036 {
1037 DBG1(DBG_CFG, "missing cacert parameter\n");
1038 return;
1039 }
1040
1041 cacert = load_ca_certificate(msg->add_ca.cacert);
1042
1043 if (cacert == NULL)
1044 {
1045 return;
1046 }
1047 ca_info = ca_info_create(msg->add_ca.name, cacert);
1048
1049 if (msg->add_ca.crluri)
1050 {
1051 chunk_t uri = { msg->add_ca.crluri, strlen(msg->add_ca.crluri) };
1052
1053 ca_info->add_crluri(ca_info, uri);
1054 }
1055 if (msg->add_ca.crluri2)
1056 {
1057 chunk_t uri = { msg->add_ca.crluri2, strlen(msg->add_ca.crluri2) };
1058
1059 ca_info->add_crluri(ca_info, uri);
1060 }
1061 if (msg->add_ca.ocspuri)
1062 {
1063 chunk_t uri = { msg->add_ca.ocspuri, strlen(msg->add_ca.ocspuri) };
1064
1065 ca_info->add_ocspuri(ca_info, uri);
1066 }
1067 if (msg->add_ca.ocspuri2)
1068 {
1069 chunk_t uri = { msg->add_ca.ocspuri2, strlen(msg->add_ca.ocspuri2) };
1070
1071 ca_info->add_ocspuri(ca_info, uri);
1072 }
1073 charon->credentials->add_ca_info(charon->credentials, ca_info);
1074 DBG1(DBG_CFG, "added ca '%s'", msg->add_ca.name);
1075
1076 }
1077
1078 /**
1079 * Delete a ca information record from the cainfo list
1080 */
1081 static void stroke_del_ca(stroke_msg_t *msg, FILE *out)
1082 {
1083 status_t status;
1084
1085 pop_string(msg, &(msg->del_ca.name));
1086 DBG1(DBG_CFG, "received stroke: delete ca '%s'", msg->del_ca.name);
1087
1088 status = charon->credentials->release_ca_info(charon->credentials,
1089 msg->del_ca.name);
1090
1091 if (status == SUCCESS)
1092 {
1093 fprintf(out, "deleted ca '%s'\n", msg->del_ca.name);
1094 }
1095 else
1096 {
1097 fprintf(out, "no ca named '%s'\n", msg->del_ca.name);
1098 }
1099 }
1100
1101 /**
1102 * log an IKE_SA to out
1103 */
1104 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
1105 {
1106 peer_cfg_t *cfg = ike_sa->get_peer_cfg(ike_sa);
1107 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
1108 u_int32_t next, now = time(NULL);
1109
1110 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
1111 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
1112 ike_sa_state_names, ike_sa->get_state(ike_sa),
1113 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
1114 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
1115
1116 if (all)
1117 {
1118 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s, ",
1119 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
1120 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
1121 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
1122
1123 ike_sa->get_stats(ike_sa, &next);
1124 if (next)
1125 {
1126 fprintf(out, "%s in %V\n", cfg->use_reauth(cfg) ?
1127 "reauthentication" : "rekeying", &now, &next);
1128 }
1129 else
1130 {
1131 fprintf(out, "rekeying disabled\n");
1132 }
1133 }
1134 }
1135
1136 /**
1137 * log an CHILD_SA to out
1138 */
1139 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
1140 {
1141 u_int32_t rekey, now = time(NULL);
1142 u_int32_t use_in, use_out, use_fwd;
1143 encryption_algorithm_t encr_alg;
1144 integrity_algorithm_t int_alg;
1145 size_t encr_len, int_len;
1146 mode_t mode;
1147
1148 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
1149 &int_alg, &int_len, &rekey, &use_in, &use_out,
1150 &use_fwd);
1151
1152 fprintf(out, "%12s{%d}: %N, %N",
1153 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
1154 child_sa_state_names, child_sa->get_state(child_sa),
1155 mode_names, mode);
1156
1157 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
1158 {
1159 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
1160 protocol_id_names, child_sa->get_protocol(child_sa),
1161 htonl(child_sa->get_spi(child_sa, TRUE)),
1162 htonl(child_sa->get_spi(child_sa, FALSE)));
1163
1164 if (all)
1165 {
1166 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
1167 child_sa->get_reqid(child_sa));
1168
1169
1170 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
1171 {
1172 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
1173
1174 if (encr_len)
1175 {
1176 fprintf(out, "-%d", encr_len);
1177 }
1178 fprintf(out, "/");
1179 }
1180
1181 fprintf(out, "%N", integrity_algorithm_names, int_alg);
1182 if (int_len)
1183 {
1184 fprintf(out, "-%d", int_len);
1185 }
1186 fprintf(out, ", rekeying ");
1187
1188 if (rekey)
1189 {
1190 fprintf(out, "in %V", &now, &rekey);
1191 }
1192 else
1193 {
1194 fprintf(out, "disabled");
1195 }
1196
1197 fprintf(out, ", last use: ");
1198 use_in = max(use_in, use_fwd);
1199 if (use_in)
1200 {
1201 fprintf(out, "%ds_i ", now - use_in);
1202 }
1203 else
1204 {
1205 fprintf(out, "no_i ");
1206 }
1207 if (use_out)
1208 {
1209 fprintf(out, "%ds_o ", now - use_out);
1210 }
1211 else
1212 {
1213 fprintf(out, "no_o ");
1214 }
1215 }
1216 }
1217
1218 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
1219 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
1220 child_sa->get_traffic_selectors(child_sa, TRUE),
1221 child_sa->get_traffic_selectors(child_sa, FALSE));
1222 }
1223
1224 /**
1225 * show status of daemon
1226 */
1227 static void stroke_status(stroke_msg_t *msg, FILE *out, bool all)
1228 {
1229 iterator_t *iterator, *children;
1230 host_t *host;
1231 peer_cfg_t *peer_cfg;
1232 ike_cfg_t *ike_cfg;
1233 child_cfg_t *child_cfg;
1234 ike_sa_t *ike_sa;
1235 char *name = NULL;
1236
1237 if (msg->status.name)
1238 {
1239 pop_string(msg, &(msg->status.name));
1240 name = msg->status.name;
1241 }
1242
1243 if (all)
1244 {
1245 leak_detective_status(out);
1246
1247 fprintf(out, "Performance:\n");
1248 fprintf(out, " worker threads: %d idle of %d,",
1249 charon->processor->get_idle_threads(charon->processor),
1250 charon->processor->get_total_threads(charon->processor));
1251 fprintf(out, " job queue load: %d,",
1252 charon->processor->get_job_load(charon->processor));
1253 fprintf(out, " scheduled events: %d\n",
1254 charon->scheduler->get_job_load(charon->scheduler));
1255 iterator = charon->kernel_interface->create_address_iterator(
1256 charon->kernel_interface);
1257 fprintf(out, "Listening IP addresses:\n");
1258 while (iterator->iterate(iterator, (void**)&host))
1259 {
1260 fprintf(out, " %H\n", host);
1261 }
1262 iterator->destroy(iterator);
1263
1264 fprintf(out, "Connections:\n");
1265 iterator = charon->backends->create_iterator(charon->backends);
1266 while (iterator->iterate(iterator, (void**)&peer_cfg))
1267 {
1268 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
1269 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
1270 {
1271 continue;
1272 }
1273
1274 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
1275 fprintf(out, "%12s: %H[%D]...%H[%D]\n", peer_cfg->get_name(peer_cfg),
1276 ike_cfg->get_my_host(ike_cfg), peer_cfg->get_my_id(peer_cfg),
1277 ike_cfg->get_other_host(ike_cfg), peer_cfg->get_other_id(peer_cfg));
1278 {
1279 identification_t *my_ca = peer_cfg->get_my_ca(peer_cfg);
1280 identification_t *other_ca = peer_cfg->get_other_ca(peer_cfg);
1281 linked_list_t *groups = peer_cfg->get_groups(peer_cfg);
1282
1283 if (my_ca->get_type(my_ca) != ID_ANY
1284 || other_ca->get_type(other_ca) != ID_ANY)
1285 {
1286 fprintf(out, "%12s: CAs: '%D'...'%D'\n", peer_cfg->get_name(peer_cfg),
1287 my_ca, other_ca);
1288 }
1289 if (groups->get_count(groups) > 0)
1290 {
1291 fprintf(out, "%12s: groups: ", peer_cfg->get_name(peer_cfg));
1292 ietfAttr_list_list(groups, out);
1293 fprintf(out, "\n");
1294 }
1295
1296 }
1297 children = peer_cfg->create_child_cfg_iterator(peer_cfg);
1298 while (children->iterate(children, (void**)&child_cfg))
1299 {
1300 linked_list_t *my_ts, *other_ts;
1301 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
1302 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
1303 fprintf(out, "%12s: %#R=== %#R\n", child_cfg->get_name(child_cfg),
1304 my_ts, other_ts);
1305 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1306 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1307 }
1308 children->destroy(children);
1309 }
1310 iterator->destroy(iterator);
1311 }
1312
1313 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
1314 if (all && iterator->get_count(iterator) > 0)
1315 {
1316 fprintf(out, "Security Associations:\n");
1317 }
1318 while (iterator->iterate(iterator, (void**)&ike_sa))
1319 {
1320 bool ike_printed = FALSE;
1321 child_sa_t *child_sa;
1322 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
1323
1324 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
1325 {
1326 log_ike_sa(out, ike_sa, all);
1327 ike_printed = TRUE;
1328 }
1329
1330 while (children->iterate(children, (void**)&child_sa))
1331 {
1332 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
1333 {
1334 if (!ike_printed)
1335 {
1336 log_ike_sa(out, ike_sa, all);
1337 ike_printed = TRUE;
1338 }
1339 log_child_sa(out, child_sa, all);
1340 }
1341 }
1342 children->destroy(children);
1343 }
1344 iterator->destroy(iterator);
1345 }
1346
1347 /**
1348 * list all authority certificates matching a specified flag
1349 */
1350 static void list_auth_certificates(u_int flag, const char *label,
1351 bool utc, FILE *out)
1352 {
1353 bool first = TRUE;
1354 x509_t *cert;
1355
1356 iterator_t *iterator = charon->credentials->create_auth_cert_iterator(charon->credentials);
1357
1358 while (iterator->iterate(iterator, (void**)&cert))
1359 {
1360 if (cert->has_authority_flag(cert, flag))
1361 {
1362 if (first)
1363 {
1364 fprintf(out, "\n");
1365 fprintf(out, "List of X.509 %s Certificates:\n", label);
1366 fprintf(out, "\n");
1367 first = FALSE;
1368 }
1369 cert->list(cert, out, utc);
1370 fprintf(out, "\n");
1371 }
1372 }
1373 iterator->destroy(iterator);
1374 }
1375
1376 /**
1377 * list various information
1378 */
1379 static void stroke_list(stroke_msg_t *msg, FILE *out)
1380 {
1381 iterator_t *iterator;
1382
1383 if (msg->list.flags & LIST_CERTS)
1384 {
1385 x509_t *cert;
1386
1387 iterator = charon->credentials->create_cert_iterator(charon->credentials);
1388 if (iterator->get_count(iterator))
1389 {
1390 fprintf(out, "\n");
1391 fprintf(out, "List of X.509 End Entity Certificates:\n");
1392 fprintf(out, "\n");
1393 }
1394 while (iterator->iterate(iterator, (void**)&cert))
1395 {
1396 cert->list(cert, out, msg->list.utc);
1397 if (charon->credentials->has_rsa_private_key(
1398 charon->credentials, cert->get_public_key(cert)))
1399 {
1400 fprintf(out, ", has private key");
1401 }
1402 fprintf(out, "\n");
1403
1404 }
1405 iterator->destroy(iterator);
1406 }
1407 if (msg->list.flags & LIST_CACERTS)
1408 {
1409 list_auth_certificates(AUTH_CA, "CA", msg->list.utc, out);
1410 }
1411 if (msg->list.flags & LIST_OCSPCERTS)
1412 {
1413 list_auth_certificates(AUTH_OCSP, "OCSP", msg->list.utc, out);
1414 }
1415 if (msg->list.flags & LIST_AACERTS)
1416 {
1417 list_auth_certificates(AUTH_AA, "AA", msg->list.utc, out);
1418 }
1419 if (msg->list.flags & LIST_ACERTS)
1420 {
1421 x509ac_t *cert;
1422
1423 iterator = charon->credentials->create_acert_iterator(charon->credentials);
1424 if (iterator->get_count(iterator))
1425 {
1426 fprintf(out, "\n");
1427 fprintf(out, "List of X.509 Attribute Certificates:\n");
1428 fprintf(out, "\n");
1429 }
1430 while (iterator->iterate(iterator, (void**)&cert))
1431 {
1432 cert->list(cert, out, msg->list.utc);
1433 }
1434 iterator->destroy(iterator);
1435 }
1436 if (msg->list.flags & LIST_CAINFOS)
1437 {
1438 ca_info_t *ca_info;
1439 bool first = TRUE;
1440
1441 iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1442 while (iterator->iterate(iterator, (void**)&ca_info))
1443 {
1444 if (ca_info->is_ca(ca_info))
1445 {
1446 if (first)
1447 {
1448 fprintf(out, "\n");
1449 fprintf(out, "List of X.509 CA Information Records:\n");
1450 fprintf(out, "\n");
1451 first = FALSE;
1452 }
1453 ca_info->list(ca_info, out, msg->list.utc);
1454 }
1455 }
1456 iterator->destroy(iterator);
1457 }
1458 if (msg->list.flags & LIST_CRLS)
1459 {
1460 ca_info_t *ca_info;
1461 bool first = TRUE;
1462
1463 iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1464 while (iterator->iterate(iterator, (void **)&ca_info))
1465 {
1466 if (ca_info->is_ca(ca_info) && ca_info->has_crl(ca_info))
1467 {
1468 if (first)
1469 {
1470 fprintf(out, "\n");
1471 fprintf(out, "List of X.509 CRLs:\n");
1472 fprintf(out, "\n");
1473 first = FALSE;
1474 }
1475 ca_info->list_crl(ca_info, out, msg->list.utc);
1476 }
1477 }
1478 iterator->destroy(iterator);
1479 }
1480 if (msg->list.flags & LIST_OCSP)
1481 {
1482 ca_info_t *ca_info;
1483 bool first = TRUE;
1484
1485 iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1486 while (iterator->iterate(iterator, (void **)&ca_info))
1487 {
1488 if (ca_info->is_ca(ca_info) && ca_info->has_certinfos(ca_info))
1489 {
1490 if (first)
1491 {
1492 fprintf(out, "\n");
1493 fprintf(out, "List of OCSP responses:\n");
1494 first = FALSE;
1495 }
1496 fprintf(out, "\n");
1497 ca_info->list_certinfos(ca_info, out, msg->list.utc);
1498 }
1499 }
1500 iterator->destroy(iterator);
1501 }
1502 }
1503
1504 /**
1505 * reread various information
1506 */
1507 static void stroke_reread(stroke_msg_t *msg, FILE *out)
1508 {
1509 if (msg->reread.flags & REREAD_SECRETS)
1510 {
1511 charon->credentials->load_secrets(charon->credentials, TRUE);
1512 }
1513 if (msg->reread.flags & REREAD_CACERTS)
1514 {
1515 charon->credentials->load_ca_certificates(charon->credentials);
1516 }
1517 if (msg->reread.flags & REREAD_OCSPCERTS)
1518 {
1519 charon->credentials->load_ocsp_certificates(charon->credentials);
1520 }
1521 if (msg->reread.flags & REREAD_AACERTS)
1522 {
1523 charon->credentials->load_aa_certificates(charon->credentials);
1524 }
1525 if (msg->reread.flags & REREAD_ACERTS)
1526 {
1527 charon->credentials->load_attr_certificates(charon->credentials);
1528 }
1529 if (msg->reread.flags & REREAD_CRLS)
1530 {
1531 charon->credentials->load_crls(charon->credentials);
1532 }
1533 }
1534
1535 /**
1536 * purge various information
1537 */
1538 static void stroke_purge(stroke_msg_t *msg, FILE *out)
1539 {
1540 if (msg->purge.flags & PURGE_OCSP)
1541 {
1542 iterator_t *iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1543 ca_info_t *ca_info;
1544
1545 while (iterator->iterate(iterator, (void**)&ca_info))
1546 {
1547 if (ca_info->is_ca(ca_info))
1548 {
1549 ca_info->purge_ocsp(ca_info);
1550 }
1551 }
1552 iterator->destroy(iterator);
1553 }
1554 }
1555
1556 signal_t get_signal_from_logtype(char *type)
1557 {
1558 if (strcasecmp(type, "any") == 0) return SIG_ANY;
1559 else if (strcasecmp(type, "mgr") == 0) return DBG_MGR;
1560 else if (strcasecmp(type, "ike") == 0) return DBG_IKE;
1561 else if (strcasecmp(type, "chd") == 0) return DBG_CHD;
1562 else if (strcasecmp(type, "job") == 0) return DBG_JOB;
1563 else if (strcasecmp(type, "cfg") == 0) return DBG_CFG;
1564 else if (strcasecmp(type, "knl") == 0) return DBG_KNL;
1565 else if (strcasecmp(type, "net") == 0) return DBG_NET;
1566 else if (strcasecmp(type, "enc") == 0) return DBG_ENC;
1567 else if (strcasecmp(type, "lib") == 0) return DBG_LIB;
1568 else return -1;
1569 }
1570
1571 /**
1572 * set the verbosity debug output
1573 */
1574 static void stroke_loglevel(stroke_msg_t *msg, FILE *out)
1575 {
1576 signal_t signal;
1577
1578 pop_string(msg, &(msg->loglevel.type));
1579 DBG1(DBG_CFG, "received stroke: loglevel %d for %s",
1580 msg->loglevel.level, msg->loglevel.type);
1581
1582 signal = get_signal_from_logtype(msg->loglevel.type);
1583 if (signal < 0)
1584 {
1585 fprintf(out, "invalid type (%s)!\n", msg->loglevel.type);
1586 return;
1587 }
1588
1589 charon->outlog->set_level(charon->outlog, signal, msg->loglevel.level);
1590 charon->syslog->set_level(charon->syslog, signal, msg->loglevel.level);
1591 }
1592
1593 /**
1594 * process a stroke request from the socket pointed by "fd"
1595 */
1596 static job_requeue_t stroke_process(int *fdp)
1597 {
1598 stroke_msg_t *msg;
1599 u_int16_t msg_length;
1600 ssize_t bytes_read;
1601 FILE *out;
1602 int strokefd = *fdp;
1603
1604 /* peek the length */
1605 bytes_read = recv(strokefd, &msg_length, sizeof(msg_length), MSG_PEEK);
1606 if (bytes_read != sizeof(msg_length))
1607 {
1608 DBG1(DBG_CFG, "reading length of stroke message failed: %s",
1609 strerror(errno));
1610 close(strokefd);
1611 return JOB_REQUEUE_NONE;
1612 }
1613
1614 /* read message */
1615 msg = malloc(msg_length);
1616 bytes_read = recv(strokefd, msg, msg_length, 0);
1617 if (bytes_read != msg_length)
1618 {
1619 DBG1(DBG_CFG, "reading stroke message failed: %s", strerror(errno));
1620 close(strokefd);
1621 return JOB_REQUEUE_NONE;
1622 }
1623
1624 out = fdopen(strokefd, "w");
1625 if (out == NULL)
1626 {
1627 DBG1(DBG_CFG, "opening stroke output channel failed: %s", strerror(errno));
1628 close(strokefd);
1629 free(msg);
1630 return JOB_REQUEUE_NONE;
1631 }
1632
1633 DBG3(DBG_CFG, "stroke message %b", (void*)msg, msg_length);
1634
1635 /* the stroke_* functions are blocking, as they listen on the bus. Add
1636 * cancellation handlers. */
1637 pthread_cleanup_push((void*)fclose, out);
1638 pthread_cleanup_push(free, msg);
1639
1640 switch (msg->type)
1641 {
1642 case STR_INITIATE:
1643 stroke_initiate(msg, out);
1644 break;
1645 case STR_ROUTE:
1646 stroke_route(msg, out);
1647 break;
1648 case STR_UNROUTE:
1649 stroke_unroute(msg, out);
1650 break;
1651 case STR_TERMINATE:
1652 stroke_terminate(msg, out);
1653 break;
1654 case STR_STATUS:
1655 stroke_status(msg, out, FALSE);
1656 break;
1657 case STR_STATUS_ALL:
1658 stroke_status(msg, out, TRUE);
1659 break;
1660 case STR_ADD_CONN:
1661 stroke_add_conn(msg, out);
1662 break;
1663 case STR_DEL_CONN:
1664 stroke_del_conn(msg, out);
1665 break;
1666 case STR_ADD_CA:
1667 stroke_add_ca(msg, out);
1668 break;
1669 case STR_DEL_CA:
1670 stroke_del_ca(msg, out);
1671 break;
1672 case STR_LOGLEVEL:
1673 stroke_loglevel(msg, out);
1674 break;
1675 case STR_LIST:
1676 stroke_list(msg, out);
1677 break;
1678 case STR_REREAD:
1679 stroke_reread(msg, out);
1680 break;
1681 case STR_PURGE:
1682 stroke_purge(msg, out);
1683 break;
1684 default:
1685 DBG1(DBG_CFG, "received unknown stroke");
1686 }
1687 /* remove and execute cancellation handlers */
1688 pthread_cleanup_pop(1);
1689 pthread_cleanup_pop(1);
1690
1691 return JOB_REQUEUE_NONE;
1692 }
1693
1694
1695 /**
1696 * Implementation of private_stroke_interface_t.stroke_receive.
1697 */
1698 static job_requeue_t stroke_receive(private_stroke_interface_t *this)
1699 {
1700 struct sockaddr_un strokeaddr;
1701 int strokeaddrlen = sizeof(strokeaddr);
1702 int strokefd, *fdp;
1703 int oldstate;
1704 callback_job_t *job;
1705
1706 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
1707 strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
1708 pthread_setcancelstate(oldstate, NULL);
1709
1710 if (strokefd < 0)
1711 {
1712 DBG1(DBG_CFG, "accepting stroke connection failed: %s", strerror(errno));
1713 return JOB_REQUEUE_FAIR;
1714 }
1715
1716 fdp = malloc_thing(int);
1717 *fdp = strokefd;
1718 job = callback_job_create((callback_job_cb_t)stroke_process, fdp, free, this->job);
1719 charon->processor->queue_job(charon->processor, (job_t*)job);
1720
1721 return JOB_REQUEUE_FAIR;
1722 }
1723
1724 /**
1725 * Implementation of interface_t.destroy.
1726 */
1727 static void destroy(private_stroke_interface_t *this)
1728 {
1729 this->job->cancel(this->job);
1730 free(this);
1731 }
1732
1733 /*
1734 * Described in header-file
1735 */
1736 interface_t *interface_create()
1737 {
1738 struct sockaddr_un socket_addr = { AF_UNIX, STROKE_SOCKET};
1739 private_stroke_interface_t *this = malloc_thing(private_stroke_interface_t);
1740 mode_t old;
1741
1742 /* public functions */
1743 this->public.interface.destroy = (void (*)(interface_t*))destroy;
1744
1745 /* set up unix socket */
1746 this->socket = socket(AF_UNIX, SOCK_STREAM, 0);
1747 if (this->socket == -1)
1748 {
1749 DBG1(DBG_CFG, "could not create stroke socket");
1750 free(this);
1751 return NULL;
1752 }
1753
1754 unlink(socket_addr.sun_path);
1755 old = umask(~(S_IRWXU | S_IRWXG));
1756 if (bind(this->socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
1757 {
1758 DBG1(DBG_CFG, "could not bind stroke socket: %s", strerror(errno));
1759 close(this->socket);
1760 free(this);
1761 return NULL;
1762 }
1763 umask(old);
1764 if (chown(socket_addr.sun_path, IPSEC_UID, IPSEC_GID) != 0)
1765 {
1766 DBG1(DBG_CFG, "changing stroke socket permissions failed: %s",
1767 strerror(errno));
1768 }
1769
1770 if (listen(this->socket, 0) < 0)
1771 {
1772 DBG1(DBG_CFG, "could not listen on stroke socket: %s", strerror(errno));
1773 close(this->socket);
1774 unlink(socket_addr.sun_path);
1775 free(this);
1776 return NULL;
1777 }
1778
1779 this->job = callback_job_create((callback_job_cb_t)stroke_receive,
1780 this, NULL, NULL);
1781 charon->processor->queue_job(charon->processor, (job_t*)this->job);
1782
1783 return &this->public.interface;
1784 }
1785