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