fixed bad cast which resulted in a crash on "ipsec update"
[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;
703 child_cfg_t *child;
704
705 pop_string(msg, &(msg->del_conn.name));
706 DBG1(DBG_CFG, "received stroke: delete connection '%s'", msg->del_conn.name);
707
708 peer_iter = charon->backends->create_iterator(charon->backends);
709 while (peer_iter->iterate(peer_iter, (void**)&peer))
710 {
711 /* remove peer config with such a name */
712 if (streq(peer->get_name(peer), msg->del_conn.name))
713 {
714 peer_iter->remove(peer_iter);
715 peer->destroy(peer);
716 continue;
717 }
718 /* remove any child with such a name */
719 child_iter = peer->create_child_cfg_iterator(peer);
720 while (child_iter->iterate(child_iter, (void**)&child))
721 {
722 if (streq(child->get_name(child), msg->del_conn.name))
723 {
724 child_iter->remove(child_iter);
725 child->destroy(child);
726 }
727 }
728 child_iter->destroy(child_iter);
729 }
730 peer_iter->destroy(peer_iter);
731
732 fprintf(out, "deleted connection '%s'\n", msg->del_conn.name);
733 }
734
735 /**
736 * get the child_cfg with the same name as the peer cfg
737 */
738 static child_cfg_t* get_child_from_peer(peer_cfg_t *peer_cfg, char *name)
739 {
740 child_cfg_t *current, *found = NULL;
741 iterator_t *iterator;
742
743 iterator = peer_cfg->create_child_cfg_iterator(peer_cfg);
744 while (iterator->iterate(iterator, (void**)&current))
745 {
746 if (streq(current->get_name(current), name))
747 {
748 found = current;
749 found->get_ref(found);
750 break;
751 }
752 }
753 iterator->destroy(iterator);
754 return found;
755 }
756
757 /**
758 * logging to the stroke interface
759 */
760 static bool stroke_log(stroke_log_info_t *info, signal_t signal, level_t level,
761 ike_sa_t *ike_sa, char *format, va_list args)
762 {
763 if (level <= info->level)
764 {
765 if (vfprintf(info->out, format, args) < 0 ||
766 fprintf(info->out, "\n") < 0 ||
767 fflush(info->out) != 0)
768 {
769 return FALSE;
770 }
771 }
772 return TRUE;
773 }
774
775 /**
776 * initiate a connection by name
777 */
778 static void stroke_initiate(stroke_msg_t *msg, FILE *out)
779 {
780 peer_cfg_t *peer_cfg;
781 child_cfg_t *child_cfg;
782 stroke_log_info_t info;
783
784 pop_string(msg, &(msg->initiate.name));
785 DBG1(DBG_CFG, "received stroke: initiate '%s'", msg->initiate.name);
786
787 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
788 msg->initiate.name);
789 if (peer_cfg == NULL)
790 {
791 fprintf(out, "no config named '%s'\n", msg->initiate.name);
792 return;
793 }
794 if (peer_cfg->get_ike_version(peer_cfg) != 2)
795 {
796 DBG1(DBG_CFG, "ignoring initiation request for IKEv%d config",
797 peer_cfg->get_ike_version(peer_cfg));
798 peer_cfg->destroy(peer_cfg);
799 return;
800 }
801
802 child_cfg = get_child_from_peer(peer_cfg, msg->initiate.name);
803 if (child_cfg == NULL)
804 {
805 fprintf(out, "no child config named '%s'\n", msg->initiate.name);
806 peer_cfg->destroy(peer_cfg);
807 return;
808 }
809
810 if (msg->output_verbosity < 0)
811 {
812 charon->interfaces->initiate(charon->interfaces, peer_cfg, child_cfg,
813 NULL, NULL);
814 }
815 else
816 {
817 info.out = out;
818 info.level = msg->output_verbosity;
819 charon->interfaces->initiate(charon->interfaces, peer_cfg, child_cfg,
820 (interface_manager_cb_t)stroke_log, &info);
821 }
822 }
823
824 /**
825 * route a policy (install SPD entries)
826 */
827 static void stroke_route(stroke_msg_t *msg, FILE *out)
828 {
829 peer_cfg_t *peer_cfg;
830 child_cfg_t *child_cfg;
831 stroke_log_info_t info;
832
833 pop_string(msg, &(msg->route.name));
834 DBG1(DBG_CFG, "received stroke: route '%s'", msg->route.name);
835
836 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends,
837 msg->route.name);
838 if (peer_cfg == NULL)
839 {
840 fprintf(out, "no config named '%s'\n", msg->route.name);
841 return;
842 }
843 if (peer_cfg->get_ike_version(peer_cfg) != 2)
844 {
845 peer_cfg->destroy(peer_cfg);
846 return;
847 }
848
849 child_cfg = get_child_from_peer(peer_cfg, msg->route.name);
850 if (child_cfg == NULL)
851 {
852 fprintf(out, "no child config named '%s'\n", msg->route.name);
853 peer_cfg->destroy(peer_cfg);
854 return;
855 }
856
857 info.out = out;
858 info.level = msg->output_verbosity;
859 charon->interfaces->route(charon->interfaces, peer_cfg, child_cfg,
860 (interface_manager_cb_t)stroke_log, &info);
861 peer_cfg->destroy(peer_cfg);
862 child_cfg->destroy(child_cfg);
863 }
864
865 /**
866 * unroute a policy
867 */
868 static void stroke_unroute(stroke_msg_t *msg, FILE *out)
869 {
870 char *name;
871 ike_sa_t *ike_sa;
872 iterator_t *iterator;
873 stroke_log_info_t info;
874
875 pop_string(msg, &(msg->terminate.name));
876 name = msg->terminate.name;
877
878 info.out = out;
879 info.level = msg->output_verbosity;
880
881 iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
882 while (iterator->iterate(iterator, (void**)&ike_sa))
883 {
884 child_sa_t *child_sa;
885 iterator_t *children;
886 u_int32_t id;
887
888 children = ike_sa->create_child_sa_iterator(ike_sa);
889 while (children->iterate(children, (void**)&child_sa))
890 {
891 if (child_sa->get_state(child_sa) == CHILD_ROUTED &&
892 streq(name, child_sa->get_name(child_sa)))
893 {
894 id = child_sa->get_reqid(child_sa);
895 children->destroy(children);
896 iterator->destroy(iterator);
897 charon->interfaces->unroute(charon->interfaces, id,
898 (interface_manager_cb_t)stroke_log, &info);
899 return;
900 }
901 }
902 children->destroy(children);
903 }
904 iterator->destroy(iterator);
905 DBG1(DBG_CFG, "no such SA found");
906 }
907
908 /**
909 * terminate a connection by name
910 */
911 static void stroke_terminate(stroke_msg_t *msg, FILE *out)
912 {
913 char *string, *pos = NULL, *name = NULL;
914 u_int32_t id = 0;
915 bool child;
916 int len;
917 ike_sa_t *ike_sa;
918 iterator_t *iterator;
919 stroke_log_info_t info;
920
921 pop_string(msg, &(msg->terminate.name));
922 string = msg->terminate.name;
923 DBG1(DBG_CFG, "received stroke: terminate '%s'", string);
924
925 len = strlen(string);
926 if (len < 1)
927 {
928 DBG1(DBG_CFG, "error parsing string");
929 return;
930 }
931 switch (string[len-1])
932 {
933 case '}':
934 child = TRUE;
935 pos = strchr(string, '{');
936 break;
937 case ']':
938 child = FALSE;
939 pos = strchr(string, '[');
940 break;
941 default:
942 name = string;
943 child = FALSE;
944 break;
945 }
946
947 if (name)
948 {
949 /* is a single name */
950 }
951 else if (pos == string + len - 2)
952 { /* is name[] or name{} */
953 string[len-2] = '\0';
954 name = string;
955 }
956 else
957 { /* is name[123] or name{23} */
958 string[len-1] = '\0';
959 id = atoi(pos + 1);
960 if (id == 0)
961 {
962 DBG1(DBG_CFG, "error parsing string");
963 return;
964 }
965 }
966
967 info.out = out;
968 info.level = msg->output_verbosity;
969
970 iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
971 while (iterator->iterate(iterator, (void**)&ike_sa))
972 {
973 child_sa_t *child_sa;
974 iterator_t *children;
975
976 if (child)
977 {
978 children = ike_sa->create_child_sa_iterator(ike_sa);
979 while (children->iterate(children, (void**)&child_sa))
980 {
981 if ((name && streq(name, child_sa->get_name(child_sa))) ||
982 (id && id == child_sa->get_reqid(child_sa)))
983 {
984 id = child_sa->get_reqid(child_sa);
985 children->destroy(children);
986 iterator->destroy(iterator);
987
988 charon->interfaces->terminate_child(charon->interfaces, id,
989 (interface_manager_cb_t)stroke_log, &info);
990 return;
991 }
992 }
993 children->destroy(children);
994 }
995 else if ((name && streq(name, ike_sa->get_name(ike_sa))) ||
996 (id && id == ike_sa->get_unique_id(ike_sa)))
997 {
998 id = ike_sa->get_unique_id(ike_sa);
999 /* unlock manager first */
1000 iterator->destroy(iterator);
1001
1002 charon->interfaces->terminate_ike(charon->interfaces, id,
1003 (interface_manager_cb_t)stroke_log, &info);
1004 return;
1005 }
1006
1007 }
1008 iterator->destroy(iterator);
1009 DBG1(DBG_CFG, "no such SA found");
1010 }
1011
1012 /**
1013 * Add a ca information record to the cainfo list
1014 */
1015 static void stroke_add_ca(stroke_msg_t *msg, FILE *out)
1016 {
1017 x509_t *cacert;
1018 ca_info_t *ca_info;
1019
1020 pop_string(msg, &msg->add_ca.name);
1021 pop_string(msg, &msg->add_ca.cacert);
1022 pop_string(msg, &msg->add_ca.crluri);
1023 pop_string(msg, &msg->add_ca.crluri2);
1024 pop_string(msg, &msg->add_ca.ocspuri);
1025 pop_string(msg, &msg->add_ca.ocspuri2);
1026
1027 DBG1(DBG_CFG, "received stroke: add ca '%s'", msg->add_ca.name);
1028
1029 DBG2(DBG_CFG, "ca %s", msg->add_ca.name);
1030 DBG2(DBG_CFG, " cacert=%s", msg->add_ca.cacert);
1031 DBG2(DBG_CFG, " crluri=%s", msg->add_ca.crluri);
1032 DBG2(DBG_CFG, " crluri2=%s", msg->add_ca.crluri2);
1033 DBG2(DBG_CFG, " ocspuri=%s", msg->add_ca.ocspuri);
1034 DBG2(DBG_CFG, " ocspuri2=%s", msg->add_ca.ocspuri2);
1035
1036 if (msg->add_ca.cacert == NULL)
1037 {
1038 DBG1(DBG_CFG, "missing cacert parameter\n");
1039 return;
1040 }
1041
1042 cacert = load_ca_certificate(msg->add_ca.cacert);
1043
1044 if (cacert == NULL)
1045 {
1046 return;
1047 }
1048 ca_info = ca_info_create(msg->add_ca.name, cacert);
1049
1050 if (msg->add_ca.crluri)
1051 {
1052 chunk_t uri = { msg->add_ca.crluri, strlen(msg->add_ca.crluri) };
1053
1054 ca_info->add_crluri(ca_info, uri);
1055 }
1056 if (msg->add_ca.crluri2)
1057 {
1058 chunk_t uri = { msg->add_ca.crluri2, strlen(msg->add_ca.crluri2) };
1059
1060 ca_info->add_crluri(ca_info, uri);
1061 }
1062 if (msg->add_ca.ocspuri)
1063 {
1064 chunk_t uri = { msg->add_ca.ocspuri, strlen(msg->add_ca.ocspuri) };
1065
1066 ca_info->add_ocspuri(ca_info, uri);
1067 }
1068 if (msg->add_ca.ocspuri2)
1069 {
1070 chunk_t uri = { msg->add_ca.ocspuri2, strlen(msg->add_ca.ocspuri2) };
1071
1072 ca_info->add_ocspuri(ca_info, uri);
1073 }
1074 charon->credentials->add_ca_info(charon->credentials, ca_info);
1075 DBG1(DBG_CFG, "added ca '%s'", msg->add_ca.name);
1076
1077 }
1078
1079 /**
1080 * Delete a ca information record from the cainfo list
1081 */
1082 static void stroke_del_ca(stroke_msg_t *msg, FILE *out)
1083 {
1084 status_t status;
1085
1086 pop_string(msg, &(msg->del_ca.name));
1087 DBG1(DBG_CFG, "received stroke: delete ca '%s'", msg->del_ca.name);
1088
1089 status = charon->credentials->release_ca_info(charon->credentials,
1090 msg->del_ca.name);
1091
1092 if (status == SUCCESS)
1093 {
1094 fprintf(out, "deleted ca '%s'\n", msg->del_ca.name);
1095 }
1096 else
1097 {
1098 fprintf(out, "no ca named '%s'\n", msg->del_ca.name);
1099 }
1100 }
1101
1102 /**
1103 * log an IKE_SA to out
1104 */
1105 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
1106 {
1107 peer_cfg_t *cfg = ike_sa->get_peer_cfg(ike_sa);
1108 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
1109 u_int32_t next, now = time(NULL);
1110
1111 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
1112 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
1113 ike_sa_state_names, ike_sa->get_state(ike_sa),
1114 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
1115 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
1116
1117 if (all)
1118 {
1119 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s, ",
1120 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
1121 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
1122 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
1123
1124 ike_sa->get_stats(ike_sa, &next);
1125 if (next)
1126 {
1127 fprintf(out, "%s in %V\n", cfg->use_reauth(cfg) ?
1128 "reauthentication" : "rekeying", &now, &next);
1129 }
1130 else
1131 {
1132 fprintf(out, "rekeying disabled\n");
1133 }
1134 }
1135 }
1136
1137 /**
1138 * log an CHILD_SA to out
1139 */
1140 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
1141 {
1142 u_int32_t rekey, now = time(NULL);
1143 u_int32_t use_in, use_out, use_fwd;
1144 encryption_algorithm_t encr_alg;
1145 integrity_algorithm_t int_alg;
1146 size_t encr_len, int_len;
1147 mode_t mode;
1148
1149 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
1150 &int_alg, &int_len, &rekey, &use_in, &use_out,
1151 &use_fwd);
1152
1153 fprintf(out, "%12s{%d}: %N, %N",
1154 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
1155 child_sa_state_names, child_sa->get_state(child_sa),
1156 mode_names, mode);
1157
1158 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
1159 {
1160 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
1161 protocol_id_names, child_sa->get_protocol(child_sa),
1162 htonl(child_sa->get_spi(child_sa, TRUE)),
1163 htonl(child_sa->get_spi(child_sa, FALSE)));
1164
1165 if (all)
1166 {
1167 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
1168 child_sa->get_reqid(child_sa));
1169
1170
1171 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
1172 {
1173 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
1174
1175 if (encr_len)
1176 {
1177 fprintf(out, "-%d", encr_len);
1178 }
1179 fprintf(out, "/");
1180 }
1181
1182 fprintf(out, "%N", integrity_algorithm_names, int_alg);
1183 if (int_len)
1184 {
1185 fprintf(out, "-%d", int_len);
1186 }
1187 fprintf(out, ", rekeying ");
1188
1189 if (rekey)
1190 {
1191 fprintf(out, "in %V", &now, &rekey);
1192 }
1193 else
1194 {
1195 fprintf(out, "disabled");
1196 }
1197
1198 fprintf(out, ", last use: ");
1199 use_in = max(use_in, use_fwd);
1200 if (use_in)
1201 {
1202 fprintf(out, "%ds_i ", now - use_in);
1203 }
1204 else
1205 {
1206 fprintf(out, "no_i ");
1207 }
1208 if (use_out)
1209 {
1210 fprintf(out, "%ds_o ", now - use_out);
1211 }
1212 else
1213 {
1214 fprintf(out, "no_o ");
1215 }
1216 }
1217 }
1218
1219 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
1220 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
1221 child_sa->get_traffic_selectors(child_sa, TRUE),
1222 child_sa->get_traffic_selectors(child_sa, FALSE));
1223 }
1224
1225 /**
1226 * show status of daemon
1227 */
1228 static void stroke_status(stroke_msg_t *msg, FILE *out, bool all)
1229 {
1230 iterator_t *iterator, *children;
1231 host_t *host;
1232 peer_cfg_t *peer_cfg;
1233 ike_cfg_t *ike_cfg;
1234 child_cfg_t *child_cfg;
1235 ike_sa_t *ike_sa;
1236 char *name = NULL;
1237
1238 if (msg->status.name)
1239 {
1240 pop_string(msg, &(msg->status.name));
1241 name = msg->status.name;
1242 }
1243
1244 if (all)
1245 {
1246 leak_detective_status(out);
1247
1248 fprintf(out, "Performance:\n");
1249 fprintf(out, " worker threads: %d idle of %d,",
1250 charon->processor->get_idle_threads(charon->processor),
1251 charon->processor->get_total_threads(charon->processor));
1252 fprintf(out, " job queue load: %d,",
1253 charon->processor->get_job_load(charon->processor));
1254 fprintf(out, " scheduled events: %d\n",
1255 charon->scheduler->get_job_load(charon->scheduler));
1256 iterator = charon->kernel_interface->create_address_iterator(
1257 charon->kernel_interface);
1258 fprintf(out, "Listening IP addresses:\n");
1259 while (iterator->iterate(iterator, (void**)&host))
1260 {
1261 fprintf(out, " %H\n", host);
1262 }
1263 iterator->destroy(iterator);
1264
1265 fprintf(out, "Connections:\n");
1266 iterator = charon->backends->create_iterator(charon->backends);
1267 while (iterator->iterate(iterator, (void**)&peer_cfg))
1268 {
1269 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
1270 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
1271 {
1272 continue;
1273 }
1274
1275 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
1276 fprintf(out, "%12s: %H[%D]...%H[%D]\n", peer_cfg->get_name(peer_cfg),
1277 ike_cfg->get_my_host(ike_cfg), peer_cfg->get_my_id(peer_cfg),
1278 ike_cfg->get_other_host(ike_cfg), peer_cfg->get_other_id(peer_cfg));
1279 {
1280 identification_t *my_ca = peer_cfg->get_my_ca(peer_cfg);
1281 identification_t *other_ca = peer_cfg->get_other_ca(peer_cfg);
1282 linked_list_t *groups = peer_cfg->get_groups(peer_cfg);
1283
1284 if (my_ca->get_type(my_ca) != ID_ANY
1285 || other_ca->get_type(other_ca) != ID_ANY)
1286 {
1287 fprintf(out, "%12s: CAs: '%D'...'%D'\n", peer_cfg->get_name(peer_cfg),
1288 my_ca, other_ca);
1289 }
1290 if (groups->get_count(groups) > 0)
1291 {
1292 fprintf(out, "%12s: groups: ", peer_cfg->get_name(peer_cfg));
1293 ietfAttr_list_list(groups, out);
1294 fprintf(out, "\n");
1295 }
1296
1297 }
1298 children = peer_cfg->create_child_cfg_iterator(peer_cfg);
1299 while (children->iterate(children, (void**)&child_cfg))
1300 {
1301 linked_list_t *my_ts, *other_ts;
1302 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
1303 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
1304 fprintf(out, "%12s: %#R=== %#R\n", child_cfg->get_name(child_cfg),
1305 my_ts, other_ts);
1306 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
1307 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
1308 }
1309 children->destroy(children);
1310 }
1311 iterator->destroy(iterator);
1312 }
1313
1314 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
1315 if (all && iterator->get_count(iterator) > 0)
1316 {
1317 fprintf(out, "Security Associations:\n");
1318 }
1319 while (iterator->iterate(iterator, (void**)&ike_sa))
1320 {
1321 bool ike_printed = FALSE;
1322 child_sa_t *child_sa;
1323 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
1324
1325 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
1326 {
1327 log_ike_sa(out, ike_sa, all);
1328 ike_printed = TRUE;
1329 }
1330
1331 while (children->iterate(children, (void**)&child_sa))
1332 {
1333 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
1334 {
1335 if (!ike_printed)
1336 {
1337 log_ike_sa(out, ike_sa, all);
1338 ike_printed = TRUE;
1339 }
1340 log_child_sa(out, child_sa, all);
1341 }
1342 }
1343 children->destroy(children);
1344 }
1345 iterator->destroy(iterator);
1346 }
1347
1348 /**
1349 * list all authority certificates matching a specified flag
1350 */
1351 static void list_auth_certificates(u_int flag, const char *label,
1352 bool utc, FILE *out)
1353 {
1354 bool first = TRUE;
1355 x509_t *cert;
1356
1357 iterator_t *iterator = charon->credentials->create_auth_cert_iterator(charon->credentials);
1358
1359 while (iterator->iterate(iterator, (void**)&cert))
1360 {
1361 if (cert->has_authority_flag(cert, flag))
1362 {
1363 if (first)
1364 {
1365 fprintf(out, "\n");
1366 fprintf(out, "List of X.509 %s Certificates:\n", label);
1367 fprintf(out, "\n");
1368 first = FALSE;
1369 }
1370 cert->list(cert, out, utc);
1371 fprintf(out, "\n");
1372 }
1373 }
1374 iterator->destroy(iterator);
1375 }
1376
1377 /**
1378 * list various information
1379 */
1380 static void stroke_list(stroke_msg_t *msg, FILE *out)
1381 {
1382 iterator_t *iterator;
1383
1384 if (msg->list.flags & LIST_CERTS)
1385 {
1386 x509_t *cert;
1387
1388 iterator = charon->credentials->create_cert_iterator(charon->credentials);
1389 if (iterator->get_count(iterator))
1390 {
1391 fprintf(out, "\n");
1392 fprintf(out, "List of X.509 End Entity Certificates:\n");
1393 fprintf(out, "\n");
1394 }
1395 while (iterator->iterate(iterator, (void**)&cert))
1396 {
1397 cert->list(cert, out, msg->list.utc);
1398 if (charon->credentials->has_rsa_private_key(
1399 charon->credentials, cert->get_public_key(cert)))
1400 {
1401 fprintf(out, ", has private key");
1402 }
1403 fprintf(out, "\n");
1404
1405 }
1406 iterator->destroy(iterator);
1407 }
1408 if (msg->list.flags & LIST_CACERTS)
1409 {
1410 list_auth_certificates(AUTH_CA, "CA", msg->list.utc, out);
1411 }
1412 if (msg->list.flags & LIST_OCSPCERTS)
1413 {
1414 list_auth_certificates(AUTH_OCSP, "OCSP", msg->list.utc, out);
1415 }
1416 if (msg->list.flags & LIST_AACERTS)
1417 {
1418 list_auth_certificates(AUTH_AA, "AA", msg->list.utc, out);
1419 }
1420 if (msg->list.flags & LIST_ACERTS)
1421 {
1422 x509ac_t *cert;
1423
1424 iterator = charon->credentials->create_acert_iterator(charon->credentials);
1425 if (iterator->get_count(iterator))
1426 {
1427 fprintf(out, "\n");
1428 fprintf(out, "List of X.509 Attribute Certificates:\n");
1429 fprintf(out, "\n");
1430 }
1431 while (iterator->iterate(iterator, (void**)&cert))
1432 {
1433 cert->list(cert, out, msg->list.utc);
1434 }
1435 iterator->destroy(iterator);
1436 }
1437 if (msg->list.flags & LIST_CAINFOS)
1438 {
1439 ca_info_t *ca_info;
1440 bool first = TRUE;
1441
1442 iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1443 while (iterator->iterate(iterator, (void**)&ca_info))
1444 {
1445 if (ca_info->is_ca(ca_info))
1446 {
1447 if (first)
1448 {
1449 fprintf(out, "\n");
1450 fprintf(out, "List of X.509 CA Information Records:\n");
1451 fprintf(out, "\n");
1452 first = FALSE;
1453 }
1454 ca_info->list(ca_info, out, msg->list.utc);
1455 }
1456 }
1457 iterator->destroy(iterator);
1458 }
1459 if (msg->list.flags & LIST_CRLS)
1460 {
1461 ca_info_t *ca_info;
1462 bool first = TRUE;
1463
1464 iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1465 while (iterator->iterate(iterator, (void **)&ca_info))
1466 {
1467 if (ca_info->is_ca(ca_info) && ca_info->has_crl(ca_info))
1468 {
1469 if (first)
1470 {
1471 fprintf(out, "\n");
1472 fprintf(out, "List of X.509 CRLs:\n");
1473 fprintf(out, "\n");
1474 first = FALSE;
1475 }
1476 ca_info->list_crl(ca_info, out, msg->list.utc);
1477 }
1478 }
1479 iterator->destroy(iterator);
1480 }
1481 if (msg->list.flags & LIST_OCSP)
1482 {
1483 ca_info_t *ca_info;
1484 bool first = TRUE;
1485
1486 iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1487 while (iterator->iterate(iterator, (void **)&ca_info))
1488 {
1489 if (ca_info->is_ca(ca_info) && ca_info->has_certinfos(ca_info))
1490 {
1491 if (first)
1492 {
1493 fprintf(out, "\n");
1494 fprintf(out, "List of OCSP responses:\n");
1495 first = FALSE;
1496 }
1497 fprintf(out, "\n");
1498 ca_info->list_certinfos(ca_info, out, msg->list.utc);
1499 }
1500 }
1501 iterator->destroy(iterator);
1502 }
1503 }
1504
1505 /**
1506 * reread various information
1507 */
1508 static void stroke_reread(stroke_msg_t *msg, FILE *out)
1509 {
1510 if (msg->reread.flags & REREAD_SECRETS)
1511 {
1512 charon->credentials->load_secrets(charon->credentials, TRUE);
1513 }
1514 if (msg->reread.flags & REREAD_CACERTS)
1515 {
1516 charon->credentials->load_ca_certificates(charon->credentials);
1517 }
1518 if (msg->reread.flags & REREAD_OCSPCERTS)
1519 {
1520 charon->credentials->load_ocsp_certificates(charon->credentials);
1521 }
1522 if (msg->reread.flags & REREAD_AACERTS)
1523 {
1524 charon->credentials->load_aa_certificates(charon->credentials);
1525 }
1526 if (msg->reread.flags & REREAD_ACERTS)
1527 {
1528 charon->credentials->load_attr_certificates(charon->credentials);
1529 }
1530 if (msg->reread.flags & REREAD_CRLS)
1531 {
1532 charon->credentials->load_crls(charon->credentials);
1533 }
1534 }
1535
1536 /**
1537 * purge various information
1538 */
1539 static void stroke_purge(stroke_msg_t *msg, FILE *out)
1540 {
1541 if (msg->purge.flags & PURGE_OCSP)
1542 {
1543 iterator_t *iterator = charon->credentials->create_cainfo_iterator(charon->credentials);
1544 ca_info_t *ca_info;
1545
1546 while (iterator->iterate(iterator, (void**)&ca_info))
1547 {
1548 if (ca_info->is_ca(ca_info))
1549 {
1550 ca_info->purge_ocsp(ca_info);
1551 }
1552 }
1553 iterator->destroy(iterator);
1554 }
1555 }
1556
1557 signal_t get_signal_from_logtype(char *type)
1558 {
1559 if (strcasecmp(type, "any") == 0) return SIG_ANY;
1560 else if (strcasecmp(type, "mgr") == 0) return DBG_MGR;
1561 else if (strcasecmp(type, "ike") == 0) return DBG_IKE;
1562 else if (strcasecmp(type, "chd") == 0) return DBG_CHD;
1563 else if (strcasecmp(type, "job") == 0) return DBG_JOB;
1564 else if (strcasecmp(type, "cfg") == 0) return DBG_CFG;
1565 else if (strcasecmp(type, "knl") == 0) return DBG_KNL;
1566 else if (strcasecmp(type, "net") == 0) return DBG_NET;
1567 else if (strcasecmp(type, "enc") == 0) return DBG_ENC;
1568 else if (strcasecmp(type, "lib") == 0) return DBG_LIB;
1569 else return -1;
1570 }
1571
1572 /**
1573 * set the verbosity debug output
1574 */
1575 static void stroke_loglevel(stroke_msg_t *msg, FILE *out)
1576 {
1577 signal_t signal;
1578
1579 pop_string(msg, &(msg->loglevel.type));
1580 DBG1(DBG_CFG, "received stroke: loglevel %d for %s",
1581 msg->loglevel.level, msg->loglevel.type);
1582
1583 signal = get_signal_from_logtype(msg->loglevel.type);
1584 if (signal < 0)
1585 {
1586 fprintf(out, "invalid type (%s)!\n", msg->loglevel.type);
1587 return;
1588 }
1589
1590 charon->outlog->set_level(charon->outlog, signal, msg->loglevel.level);
1591 charon->syslog->set_level(charon->syslog, signal, msg->loglevel.level);
1592 }
1593
1594 /**
1595 * process a stroke request from the socket pointed by "fd"
1596 */
1597 static job_requeue_t stroke_process(int *fdp)
1598 {
1599 stroke_msg_t *msg;
1600 u_int16_t msg_length;
1601 ssize_t bytes_read;
1602 FILE *out;
1603 int strokefd = *fdp;
1604
1605 /* peek the length */
1606 bytes_read = recv(strokefd, &msg_length, sizeof(msg_length), MSG_PEEK);
1607 if (bytes_read != sizeof(msg_length))
1608 {
1609 DBG1(DBG_CFG, "reading length of stroke message failed: %s",
1610 strerror(errno));
1611 close(strokefd);
1612 return JOB_REQUEUE_NONE;
1613 }
1614
1615 /* read message */
1616 msg = malloc(msg_length);
1617 bytes_read = recv(strokefd, msg, msg_length, 0);
1618 if (bytes_read != msg_length)
1619 {
1620 DBG1(DBG_CFG, "reading stroke message failed: %s", strerror(errno));
1621 close(strokefd);
1622 return JOB_REQUEUE_NONE;
1623 }
1624
1625 out = fdopen(strokefd, "w");
1626 if (out == NULL)
1627 {
1628 DBG1(DBG_CFG, "opening stroke output channel failed: %s", strerror(errno));
1629 close(strokefd);
1630 free(msg);
1631 return JOB_REQUEUE_NONE;
1632 }
1633
1634 DBG3(DBG_CFG, "stroke message %b", (void*)msg, msg_length);
1635
1636 /* the stroke_* functions are blocking, as they listen on the bus. Add
1637 * cancellation handlers. */
1638 pthread_cleanup_push((void*)fclose, out);
1639 pthread_cleanup_push(free, msg);
1640
1641 switch (msg->type)
1642 {
1643 case STR_INITIATE:
1644 stroke_initiate(msg, out);
1645 break;
1646 case STR_ROUTE:
1647 stroke_route(msg, out);
1648 break;
1649 case STR_UNROUTE:
1650 stroke_unroute(msg, out);
1651 break;
1652 case STR_TERMINATE:
1653 stroke_terminate(msg, out);
1654 break;
1655 case STR_STATUS:
1656 stroke_status(msg, out, FALSE);
1657 break;
1658 case STR_STATUS_ALL:
1659 stroke_status(msg, out, TRUE);
1660 break;
1661 case STR_ADD_CONN:
1662 stroke_add_conn(msg, out);
1663 break;
1664 case STR_DEL_CONN:
1665 stroke_del_conn(msg, out);
1666 break;
1667 case STR_ADD_CA:
1668 stroke_add_ca(msg, out);
1669 break;
1670 case STR_DEL_CA:
1671 stroke_del_ca(msg, out);
1672 break;
1673 case STR_LOGLEVEL:
1674 stroke_loglevel(msg, out);
1675 break;
1676 case STR_LIST:
1677 stroke_list(msg, out);
1678 break;
1679 case STR_REREAD:
1680 stroke_reread(msg, out);
1681 break;
1682 case STR_PURGE:
1683 stroke_purge(msg, out);
1684 break;
1685 default:
1686 DBG1(DBG_CFG, "received unknown stroke");
1687 }
1688 /* remove and execute cancellation handlers */
1689 pthread_cleanup_pop(1);
1690 pthread_cleanup_pop(1);
1691
1692 return JOB_REQUEUE_NONE;
1693 }
1694
1695
1696 /**
1697 * Implementation of private_stroke_interface_t.stroke_receive.
1698 */
1699 static job_requeue_t stroke_receive(private_stroke_interface_t *this)
1700 {
1701 struct sockaddr_un strokeaddr;
1702 int strokeaddrlen = sizeof(strokeaddr);
1703 int strokefd, *fdp;
1704 int oldstate;
1705 callback_job_t *job;
1706
1707 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
1708 strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
1709 pthread_setcancelstate(oldstate, NULL);
1710
1711 if (strokefd < 0)
1712 {
1713 DBG1(DBG_CFG, "accepting stroke connection failed: %s", strerror(errno));
1714 return JOB_REQUEUE_FAIR;
1715 }
1716
1717 fdp = malloc_thing(int);
1718 *fdp = strokefd;
1719 job = callback_job_create((callback_job_cb_t)stroke_process, fdp, free, this->job);
1720 charon->processor->queue_job(charon->processor, (job_t*)job);
1721
1722 return JOB_REQUEUE_FAIR;
1723 }
1724
1725 /**
1726 * Implementation of interface_t.destroy.
1727 */
1728 static void destroy(private_stroke_interface_t *this)
1729 {
1730 this->job->cancel(this->job);
1731 free(this);
1732 }
1733
1734 /*
1735 * Described in header-file
1736 */
1737 interface_t *interface_create()
1738 {
1739 struct sockaddr_un socket_addr = { AF_UNIX, STROKE_SOCKET};
1740 private_stroke_interface_t *this = malloc_thing(private_stroke_interface_t);
1741 mode_t old;
1742
1743 /* public functions */
1744 this->public.interface.destroy = (void (*)(interface_t*))destroy;
1745
1746 /* set up unix socket */
1747 this->socket = socket(AF_UNIX, SOCK_STREAM, 0);
1748 if (this->socket == -1)
1749 {
1750 DBG1(DBG_CFG, "could not create stroke socket");
1751 free(this);
1752 return NULL;
1753 }
1754
1755 unlink(socket_addr.sun_path);
1756 old = umask(~(S_IRWXU | S_IRWXG));
1757 if (bind(this->socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
1758 {
1759 DBG1(DBG_CFG, "could not bind stroke socket: %s", strerror(errno));
1760 close(this->socket);
1761 free(this);
1762 return NULL;
1763 }
1764 umask(old);
1765 if (chown(socket_addr.sun_path, IPSEC_UID, IPSEC_GID) != 0)
1766 {
1767 DBG1(DBG_CFG, "changing stroke socket permissions failed: %s",
1768 strerror(errno));
1769 }
1770
1771 if (listen(this->socket, 0) < 0)
1772 {
1773 DBG1(DBG_CFG, "could not listen on stroke socket: %s", strerror(errno));
1774 close(this->socket);
1775 unlink(socket_addr.sun_path);
1776 free(this);
1777 return NULL;
1778 }
1779
1780 this->job = callback_job_create((callback_job_cb_t)stroke_receive,
1781 this, NULL, NULL);
1782 charon->processor->queue_job(charon->processor, (job_t*)this->job);
1783
1784 return &this->public.interface;
1785 }
1786