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