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