improved selection of ipsec status|statusall <name>
[strongswan.git] / src / charon / threads / stroke_interface.c
1 /**
2 * @file stroke.c
3 *
4 * @brief Implementation of stroke_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 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/crl.h>
42 #include <queues/jobs/initiate_job.h>
43 #include <queues/jobs/route_job.h>
44 #include <utils/leak_detective.h>
45
46 #define IKE_PORT 500
47 #define PATH_BUF 256
48
49
50 struct sockaddr_un socket_addr = { AF_UNIX, STROKE_SOCKET};
51
52
53 typedef struct private_stroke_t private_stroke_t;
54
55 /**
56 * Private data of an stroke_t object.
57 */
58 struct private_stroke_t {
59
60 /**
61 * Public part of stroke_t object.
62 */
63 stroke_t public;
64
65 /**
66 * Output stream (stroke console)
67 */
68 FILE *out;
69
70 /**
71 * Unix socket to listen for strokes
72 */
73 int socket;
74
75 /**
76 * Thread which reads from the Socket
77 */
78 pthread_t assigned_thread;
79 };
80
81 /**
82 * Helper function which corrects the string pointers
83 * in a stroke_msg_t. Strings in a stroke_msg sent over "wire"
84 * contains RELATIVE addresses (relative to the beginning of the
85 * stroke_msg). They must be corrected if they reach our address
86 * space...
87 */
88 static void pop_string(stroke_msg_t *msg, char **string)
89 {
90 if (*string == NULL)
91 return;
92
93 /* check for sanity of string pointer and string */
94 if (string < (char**)msg
95 || string > (char**)msg + sizeof(stroke_msg_t)
96 || (unsigned long)*string < (unsigned long)((char*)msg->buffer - (char*)msg)
97 || (unsigned long)*string > msg->length)
98 {
99 *string = "(invalid pointer in stroke msg)";
100 }
101 else
102 {
103 *string = (char*)msg + (unsigned long)*string;
104 }
105 }
106
107 /**
108 * Load end entitity certificate
109 */
110 static x509_t* load_end_certificate(const char *filename, identification_t **idp)
111 {
112 char path[PATH_BUF];
113 x509_t *cert;
114
115 if (*filename == '/')
116 {
117 /* absolute path name */
118 snprintf(path, sizeof(path), "%s", filename);
119 }
120 else
121 {
122 /* relative path name */
123 snprintf(path, sizeof(path), "%s/%s", CERTIFICATE_DIR, filename);
124 }
125
126 cert = x509_create_from_file(path, "end entity certificate");
127
128 if (cert)
129 {
130 identification_t *id = *idp;
131 identification_t *subject = cert->get_subject(cert);
132
133 err_t ugh = cert->is_valid(cert, NULL);
134
135 if (ugh != NULL)
136 {
137 DBG1(DBG_CFG, "warning: certificate %s", ugh);
138 }
139 if (!id->equals(id, subject) && !cert->equals_subjectAltName(cert, id))
140 {
141 id->destroy(id);
142 id = subject;
143 *idp = id->clone(id);
144 }
145 return charon->credentials->add_end_certificate(charon->credentials, cert);
146 }
147 return NULL;
148 }
149
150 /**
151 * Add a connection to the configuration list
152 */
153 static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg)
154 {
155 connection_t *connection;
156 policy_t *policy;
157 identification_t *my_id, *other_id;
158 identification_t *my_ca = NULL;
159 identification_t *other_ca = NULL;
160 bool my_ca_same = FALSE;
161 bool other_ca_same =FALSE;
162 host_t *my_host, *other_host, *my_subnet, *other_subnet;
163 proposal_t *proposal;
164 traffic_selector_t *my_ts, *other_ts;
165
166 pop_string(msg, &msg->add_conn.name);
167 pop_string(msg, &msg->add_conn.me.address);
168 pop_string(msg, &msg->add_conn.other.address);
169 pop_string(msg, &msg->add_conn.me.subnet);
170 pop_string(msg, &msg->add_conn.other.subnet);
171 pop_string(msg, &msg->add_conn.me.id);
172 pop_string(msg, &msg->add_conn.other.id);
173 pop_string(msg, &msg->add_conn.me.cert);
174 pop_string(msg, &msg->add_conn.other.cert);
175 pop_string(msg, &msg->add_conn.me.ca);
176 pop_string(msg, &msg->add_conn.other.ca);
177 pop_string(msg, &msg->add_conn.me.updown);
178 pop_string(msg, &msg->add_conn.other.updown);
179 pop_string(msg, &msg->add_conn.algorithms.ike);
180 pop_string(msg, &msg->add_conn.algorithms.esp);
181
182 DBG1(DBG_CFG, "received stroke: add connection '%s'", msg->add_conn.name);
183
184 DBG2(DBG_CFG, "conn %s", msg->add_conn.name);
185 DBG2(DBG_CFG, " right=%s", msg->add_conn.me.address);
186 DBG2(DBG_CFG, " left=%s", msg->add_conn.other.address);
187 DBG2(DBG_CFG, " rightsubnet=%s", msg->add_conn.me.subnet);
188 DBG2(DBG_CFG, " leftsubnet=%s", msg->add_conn.other.subnet);
189 DBG2(DBG_CFG, " rightid=%s", msg->add_conn.me.id);
190 DBG2(DBG_CFG, " leftid=%s", msg->add_conn.other.id);
191 DBG2(DBG_CFG, " rightcert=%s", msg->add_conn.me.cert);
192 DBG2(DBG_CFG, " leftcert=%s", msg->add_conn.other.cert);
193 DBG2(DBG_CFG, " rightca=%s", msg->add_conn.me.ca);
194 DBG2(DBG_CFG, " leftca=%s", msg->add_conn.other.ca);
195 DBG2(DBG_CFG, " ike=%s", msg->add_conn.algorithms.ike);
196 DBG2(DBG_CFG, " esp=%s", msg->add_conn.algorithms.esp);
197
198 my_host = msg->add_conn.me.address?
199 host_create_from_string(msg->add_conn.me.address, IKE_PORT) : NULL;
200 if (my_host == NULL)
201 {
202 DBG1(DBG_CFG, "invalid host: %s\n", msg->add_conn.me.address);
203 return;
204 }
205
206 other_host = msg->add_conn.other.address ?
207 host_create_from_string(msg->add_conn.other.address, IKE_PORT) : NULL;
208 if (other_host == NULL)
209 {
210 DBG1(DBG_CFG, "invalid host: %s\n", msg->add_conn.other.address);
211 my_host->destroy(my_host);
212 return;
213 }
214
215 if (charon->socket->is_local_address(charon->socket, other_host, NULL))
216 {
217 stroke_end_t tmp_end;
218 host_t *tmp_host;
219
220 DBG2(DBG_CFG, "left is other host, swapping ends\n");
221
222 tmp_host = my_host;
223 my_host = other_host;
224 other_host = tmp_host;
225
226 tmp_end = msg->add_conn.me;
227 msg->add_conn.me = msg->add_conn.other;
228 msg->add_conn.other = tmp_end;
229 }
230 else if (!charon->socket->is_local_address(charon->socket, my_host, NULL))
231 {
232 DBG1(DBG_CFG, "left nor right host is our side, aborting\n");
233 goto destroy_hosts;
234 }
235
236 my_id = identification_create_from_string(msg->add_conn.me.id ?
237 msg->add_conn.me.id : msg->add_conn.me.address);
238 if (my_id == NULL)
239 {
240 DBG1(DBG_CFG, "invalid ID: %s\n", msg->add_conn.me.id);
241 goto destroy_hosts;
242 }
243
244 other_id = identification_create_from_string(msg->add_conn.other.id ?
245 msg->add_conn.other.id : msg->add_conn.other.address);
246 if (other_id == NULL)
247 {
248 DBG1(DBG_CFG, "invalid ID: %s\n", msg->add_conn.other.id);
249 my_id->destroy(my_id);
250 goto destroy_hosts;
251 }
252
253 my_subnet = host_create_from_string(msg->add_conn.me.subnet ?
254 msg->add_conn.me.subnet : msg->add_conn.me.address, IKE_PORT);
255 if (my_subnet == NULL)
256 {
257 DBG1(DBG_CFG, "invalid subnet: %s\n", msg->add_conn.me.subnet);
258 goto destroy_ids;
259 }
260
261 other_subnet = host_create_from_string(msg->add_conn.other.subnet ?
262 msg->add_conn.other.subnet : msg->add_conn.other.address, IKE_PORT);
263 if (other_subnet == NULL)
264 {
265 DBG1(DBG_CFG, "invalid subnet: %s\n", msg->add_conn.me.subnet);
266 my_subnet->destroy(my_subnet);
267 goto destroy_ids;
268 }
269
270 my_ts = traffic_selector_create_from_subnet(my_subnet,
271 msg->add_conn.me.subnet ? msg->add_conn.me.subnet_mask : 0,
272 msg->add_conn.me.protocol, msg->add_conn.me.port);
273 my_subnet->destroy(my_subnet);
274
275 other_ts = traffic_selector_create_from_subnet(other_subnet,
276 msg->add_conn.other.subnet ? msg->add_conn.other.subnet_mask : 0,
277 msg->add_conn.other.protocol, msg->add_conn.other.port);
278 other_subnet->destroy(other_subnet);
279
280 if (msg->add_conn.me.ca)
281 {
282 if (streq(msg->add_conn.me.ca, "%same"))
283 {
284 my_ca_same = TRUE;
285 }
286 else
287 {
288 my_ca = identification_create_from_string(msg->add_conn.me.ca);
289 }
290 }
291 if (msg->add_conn.other.ca)
292 {
293 if (streq(msg->add_conn.other.ca, "%same"))
294 {
295 other_ca_same = TRUE;
296 }
297 else
298 {
299 other_ca = identification_create_from_string(msg->add_conn.other.ca);
300 }
301 }
302 if (msg->add_conn.me.cert)
303 {
304 x509_t *cert = load_end_certificate(msg->add_conn.me.cert, &my_id);
305
306 if (my_ca == NULL && !my_ca_same && cert)
307 {
308 identification_t *issuer = cert->get_issuer(cert);
309
310 my_ca = issuer->clone(issuer);
311 }
312 }
313 if (msg->add_conn.other.cert)
314 {
315 x509_t *cert = load_end_certificate(msg->add_conn.other.cert, &other_id);
316
317 if (other_ca == NULL && !other_ca_same && cert)
318 {
319 identification_t *issuer = cert->get_issuer(cert);
320
321 other_ca = issuer->clone(issuer);
322 }
323 }
324 if (other_ca_same && my_ca)
325 {
326 other_ca = my_ca->clone(my_ca);
327 }
328 else if (my_ca_same && other_ca)
329 {
330 my_ca = other_ca->clone(other_ca);
331 }
332 if (my_ca == NULL)
333 {
334 my_ca = identification_create_from_string("%any");
335 }
336 if (other_ca == NULL)
337 {
338 other_ca = identification_create_from_string("%any");
339 }
340 DBG2(DBG_CFG, " my ca: '%D'", my_ca);
341 DBG2(DBG_CFG, " other ca:'%D'", other_ca);
342 DBG2(DBG_CFG, " updown: '%s'", msg->add_conn.me.updown);
343
344 connection = connection_create(msg->add_conn.name,
345 msg->add_conn.ikev2,
346 msg->add_conn.me.sendcert,
347 msg->add_conn.other.sendcert,
348 my_host, other_host,
349 msg->add_conn.dpd.delay,
350 msg->add_conn.rekey.tries,
351 msg->add_conn.rekey.ike_lifetime,
352 msg->add_conn.rekey.ike_lifetime - msg->add_conn.rekey.margin,
353 msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100);
354
355 if (msg->add_conn.algorithms.ike)
356 {
357 char *proposal_string;
358 char *strict = msg->add_conn.algorithms.ike + strlen(msg->add_conn.algorithms.ike) - 1;
359
360 if (*strict == '!')
361 *strict = '\0';
362 else
363 strict = NULL;
364
365 while ((proposal_string = strsep(&msg->add_conn.algorithms.ike, ",")))
366 {
367 proposal = proposal_create_from_string(PROTO_IKE, proposal_string);
368 if (proposal == NULL)
369 {
370 DBG1(DBG_CFG, "invalid IKE proposal string: %s", proposal_string);
371 my_id->destroy(my_id);
372 other_id->destroy(other_id);
373 my_ts->destroy(my_ts);
374 other_ts->destroy(other_ts);
375 my_ca->destroy(my_ca);
376 other_ca->destroy(other_ca);
377 connection->destroy(connection);
378 return;
379 }
380 connection->add_proposal(connection, proposal);
381 }
382 if (!strict)
383 {
384 proposal = proposal_create_default(PROTO_IKE);
385 connection->add_proposal(connection, proposal);
386 }
387 }
388 else
389 {
390 proposal = proposal_create_default(PROTO_IKE);
391 connection->add_proposal(connection, proposal);
392 }
393
394 policy = policy_create(msg->add_conn.name, my_id, other_id,
395 msg->add_conn.auth_method,
396 msg->add_conn.rekey.ipsec_lifetime,
397 msg->add_conn.rekey.ipsec_lifetime - msg->add_conn.rekey.margin,
398 msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100,
399 msg->add_conn.me.updown, msg->add_conn.me.hostaccess,
400 msg->add_conn.dpd.action);
401 policy->add_my_traffic_selector(policy, my_ts);
402 policy->add_other_traffic_selector(policy, other_ts);
403 policy->add_authorities(policy, my_ca, other_ca);
404
405 if (msg->add_conn.algorithms.esp)
406 {
407 char *proposal_string;
408 char *strict = msg->add_conn.algorithms.esp + strlen(msg->add_conn.algorithms.esp) - 1;
409
410 if (*strict == '!')
411 *strict = '\0';
412 else
413 strict = NULL;
414
415 while ((proposal_string = strsep(&msg->add_conn.algorithms.esp, ",")))
416 {
417 proposal = proposal_create_from_string(PROTO_ESP, proposal_string);
418 if (proposal == NULL)
419 {
420 DBG1(DBG_CFG, "invalid ESP proposal string: %s", proposal_string);
421 policy->destroy(policy);
422 connection->destroy(connection);
423 return;
424 }
425 policy->add_proposal(policy, proposal);
426 }
427 if (!strict)
428 {
429 proposal = proposal_create_default(PROTO_ESP);
430 policy->add_proposal(policy, proposal);
431 }
432 }
433 else
434 {
435 proposal = proposal_create_default(PROTO_ESP);
436 policy->add_proposal(policy, proposal);
437 }
438
439 /* add to global connection list */
440 charon->connections->add_connection(charon->connections, connection);
441 DBG1(DBG_CFG, "added connection '%s': %H[%D]...%H[%D]",
442 msg->add_conn.name, my_host, my_id, other_host, other_id);
443 /* add to global policy list */
444 charon->policies->add_policy(charon->policies, policy);
445
446 return;
447
448 /* mopping up after parsing errors */
449
450 destroy_ids:
451 my_id->destroy(my_id);
452 other_id->destroy(other_id);
453
454 destroy_hosts:
455 my_host->destroy(my_host);
456 other_host->destroy(other_host);
457 }
458
459 /**
460 * Delete a connection from the list
461 */
462 static void stroke_del_conn(private_stroke_t *this, stroke_msg_t *msg)
463 {
464 status_t status;
465
466 pop_string(msg, &(msg->del_conn.name));
467 DBG1(DBG_CFG, "received stroke: delete '%s'", msg->del_conn.name);
468
469 status = charon->connections->delete_connection(charon->connections,
470 msg->del_conn.name);
471 charon->policies->delete_policy(charon->policies, msg->del_conn.name);
472 if (status == SUCCESS)
473 {
474 fprintf(this->out, "deleted connection '%s'\n", msg->del_conn.name);
475 }
476 else
477 {
478 fprintf(this->out, "no connection named '%s'\n", msg->del_conn.name);
479 }
480 }
481
482 /**
483 * initiate a connection by name
484 */
485 static void stroke_initiate(private_stroke_t *this, stroke_msg_t *msg)
486 {
487 initiate_job_t *job;
488 connection_t *connection;
489 policy_t *policy;
490 ike_sa_t *init_ike_sa = NULL;
491 signal_t signal;
492
493 pop_string(msg, &(msg->initiate.name));
494 DBG1(DBG_CFG, "received stroke: initiate '%s'", msg->initiate.name);
495
496 connection = charon->connections->get_connection_by_name(charon->connections,
497 msg->initiate.name);
498 if (connection == NULL)
499 {
500 if (msg->output_verbosity >= 0)
501 {
502 fprintf(this->out, "no connection named '%s'\n", msg->initiate.name);
503 }
504 return;
505 }
506 if (!connection->is_ikev2(connection))
507 {
508 connection->destroy(connection);
509 return;
510 }
511
512 policy = charon->policies->get_policy_by_name(charon->policies,
513 msg->initiate.name);
514 if (policy == NULL)
515 {
516 if (msg->output_verbosity >= 0)
517 {
518 fprintf(this->out, "no policy named '%s'\n", msg->initiate.name);
519 }
520 connection->destroy(connection);
521 return;
522 }
523
524 job = initiate_job_create(connection, policy);
525 /*
526 if (msg->output_verbosity < 0)
527 {
528 TODO: detach immediately if verbosity is SILENT. Local credential store
529 is not threadsave yet, so this would cause crashes!!
530 charon->job_queue->add(charon->job_queue, (job_t*)job);
531 return;
532 }*/
533
534 charon->bus->set_listen_state(charon->bus, TRUE);
535 charon->job_queue->add(charon->job_queue, (job_t*)job);
536 while (TRUE)
537 {
538 level_t level;
539 int thread;
540 ike_sa_t *ike_sa;
541 char* format;
542 va_list args;
543
544 signal = charon->bus->listen(charon->bus, &level, &thread, &ike_sa, &format, &args);
545
546 if ((init_ike_sa == NULL || ike_sa == init_ike_sa) &&
547 level <= msg->output_verbosity)
548 {
549 if (vfprintf(this->out, format, args) < 0 ||
550 fprintf(this->out, "\n") < 0 ||
551 fflush(this->out))
552 {
553 charon->bus->set_listen_state(charon->bus, FALSE);
554 break;
555 }
556 }
557
558 switch (signal)
559 {
560 case CHILD_UP_SUCCESS:
561 case CHILD_UP_FAILED:
562 case IKE_UP_FAILED:
563 if (ike_sa == init_ike_sa)
564 {
565 charon->bus->set_listen_state(charon->bus, FALSE);
566 return;
567 }
568 continue;
569 case CHILD_UP_START:
570 case IKE_UP_START:
571 if (init_ike_sa == NULL)
572 {
573 init_ike_sa = ike_sa;
574 }
575 continue;
576 default:
577 continue;
578 }
579 }
580 }
581
582 /**
583 * route/unroute a policy (install SPD entries)
584 */
585 static void stroke_route(private_stroke_t *this, stroke_msg_t *msg, bool route)
586 {
587 route_job_t *job;
588 connection_t *connection;
589 policy_t *policy;
590
591 pop_string(msg, &(msg->route.name));
592 DBG1(DBG_CFG, "received stroke: %s '%s'",
593 route ? "route" : "unroute", msg->route.name);
594
595 /* we wouldn't need a connection, but we only want to route policies
596 * whose connections are keyexchange=ikev2. */
597 connection = charon->connections->get_connection_by_name(charon->connections,
598 msg->route.name);
599 if (connection == NULL)
600 {
601 fprintf(this->out, "no connection named '%s'\n", msg->route.name);
602 return;
603 }
604 if (!connection->is_ikev2(connection))
605 {
606 connection->destroy(connection);
607 return;
608 }
609
610 policy = charon->policies->get_policy_by_name(charon->policies,
611 msg->route.name);
612 if (policy == NULL)
613 {
614 fprintf(this->out, "no policy named '%s'\n", msg->route.name);
615 connection->destroy(connection);
616 return;
617 }
618 fprintf(this->out, "%s policy '%s'\n",
619 route ? "routing" : "unrouting", msg->route.name);
620 job = route_job_create(connection, policy, route);
621 charon->job_queue->add(charon->job_queue, (job_t*)job);
622 }
623
624 /**
625 * terminate a connection by name
626 */
627 static void stroke_terminate(private_stroke_t *this, stroke_msg_t *msg)
628 {
629 pop_string(msg, &(msg->terminate.name));
630 DBG1(DBG_CFG, "received stroke: terminate '%s'", msg->terminate.name);
631
632 charon->ike_sa_manager->delete_by_name(charon->ike_sa_manager, msg->terminate.name);
633 }
634
635 /**
636 * show status of daemon
637 */
638 static void stroke_statusall(private_stroke_t *this, stroke_msg_t *msg)
639 {
640 iterator_t *iterator;
641 linked_list_t *list;
642 host_t *host;
643 connection_t *connection;
644 policy_t *policy;
645 ike_sa_t *ike_sa;
646 char *name = NULL;
647
648 leak_detective_status(this->out);
649
650 fprintf(this->out, "Performance:\n");
651 fprintf(this->out, " worker threads: %d idle of %d,",
652 charon->thread_pool->get_idle_threads(charon->thread_pool),
653 charon->thread_pool->get_pool_size(charon->thread_pool));
654 fprintf(this->out, " job queue load: %d,",
655 charon->job_queue->get_count(charon->job_queue));
656 fprintf(this->out, " scheduled events: %d\n",
657 charon->event_queue->get_count(charon->event_queue));
658 list = charon->socket->create_local_address_list(charon->socket);
659
660 fprintf(this->out, "Listening on %d IP addresses:\n", list->get_count(list));
661 while (list->remove_first(list, (void**)&host) == SUCCESS)
662 {
663 fprintf(this->out, " %H\n", host);
664 host->destroy(host);
665 }
666 list->destroy(list);
667
668 if (msg->status.name)
669 {
670 pop_string(msg, &(msg->status.name));
671 name = msg->status.name;
672 }
673
674 iterator = charon->connections->create_iterator(charon->connections);
675 if (iterator->get_count(iterator) > 0)
676 {
677 fprintf(this->out, "Connections:\n");
678 }
679 while (iterator->iterate(iterator, (void**)&connection))
680 {
681 if (connection->is_ikev2(connection)
682 && (name == NULL || streq(name, connection->get_name(connection))))
683 {
684 fprintf(this->out, "%12s: %H...%H\n",
685 connection->get_name(connection),
686 connection->get_my_host(connection),
687 connection->get_other_host(connection));
688 }
689 }
690 iterator->destroy(iterator);
691
692 iterator = charon->policies->create_iterator(charon->policies);
693 if (iterator->get_count(iterator) > 0)
694 {
695 fprintf(this->out, "Policies:\n");
696 }
697 while (iterator->iterate(iterator, (void**)&policy))
698 {
699 if (name == NULL || streq(name, policy->get_name(policy)))
700 {
701 fprintf(this->out, "%12s: '%D'...'%D'\n",
702 policy->get_name(policy),
703 policy->get_my_id(policy),
704 policy->get_other_id(policy));
705 }
706 }
707 iterator->destroy(iterator);
708
709 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
710 if (iterator->get_count(iterator) > 0)
711 {
712 fprintf(this->out, "Security Associations:\n");
713 }
714 while (iterator->iterate(iterator, (void**)&ike_sa))
715 {
716 bool ike_sa_printed = FALSE;
717 child_sa_t *child_sa;
718 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
719
720 /* print IKE_SA */
721 if (name == NULL || strncmp(name, ike_sa->get_name(ike_sa), strlen(name)) == 0)
722 {
723 fprintf(this->out, "%#K\n", ike_sa);
724 ike_sa_printed = TRUE;
725 }
726
727 while (children->iterate(children, (void**)&child_sa))
728 {
729 bool child_sa_match = name == NULL ||
730 strncmp(name, child_sa->get_name(child_sa), strlen(name)) == 0;
731
732 /* print IKE_SA if its name differs from the CHILD_SA's name */
733 if (!ike_sa_printed && child_sa_match)
734 {
735 fprintf(this->out, "%#K\n", ike_sa);
736 ike_sa_printed = TRUE;
737 }
738
739 /* print CHILD_SA */
740 if (child_sa_match)
741 {
742 fprintf(this->out, "%#P\n", child_sa);
743 }
744 }
745 children->destroy(children);
746 }
747 iterator->destroy(iterator);
748 }
749
750 /**
751 * show status of daemon
752 */
753 static void stroke_status(private_stroke_t *this, stroke_msg_t *msg)
754 {
755 iterator_t *iterator;
756 ike_sa_t *ike_sa;
757 char *name = NULL;
758
759 if (msg->status.name)
760 {
761 pop_string(msg, &(msg->status.name));
762 name = msg->status.name;
763 }
764
765 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
766 while (iterator->iterate(iterator, (void**)&ike_sa))
767 {
768 bool ike_sa_printed = FALSE;
769 child_sa_t *child_sa;
770 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
771
772 /* print IKE_SA */
773 if (name == NULL || strncmp(name, ike_sa->get_name(ike_sa), strlen(name)) == 0)
774 {
775 fprintf(this->out, "%K\n", ike_sa);
776 ike_sa_printed = TRUE;
777 }
778
779 while (children->iterate(children, (void**)&child_sa))
780 {
781 bool child_sa_match = name == NULL ||
782 strncmp(name, child_sa->get_name(child_sa), strlen(name)) == 0;
783
784 /* print IKE_SA if its name differs from the CHILD_SA's name */
785 if (!ike_sa_printed && child_sa_match)
786 {
787 fprintf(this->out, "%K\n", ike_sa);
788 ike_sa_printed = TRUE;
789 }
790
791 /* print CHILD_SA */
792 if (child_sa_match)
793 {
794 fprintf(this->out, "%P\n", child_sa);
795 }
796 }
797 children->destroy(children);
798 }
799 iterator->destroy(iterator);
800 }
801
802 /**
803 * list various information
804 */
805 static void stroke_list(private_stroke_t *this, stroke_msg_t *msg)
806 {
807 iterator_t *iterator;
808
809 if (msg->list.flags & LIST_CERTS)
810 {
811 x509_t *cert;
812
813 iterator = charon->credentials->create_cert_iterator(charon->credentials);
814 if (iterator->get_count(iterator))
815 {
816 fprintf(this->out, "\n");
817 fprintf(this->out, "List of X.509 End Entity Certificates:\n");
818 fprintf(this->out, "\n");
819 }
820 while (iterator->iterate(iterator, (void**)&cert))
821 {
822 fprintf(this->out, "%#Q", cert, msg->list.utc);
823 if (charon->credentials->has_rsa_private_key(
824 charon->credentials, cert->get_public_key(cert)))
825 {
826 fprintf(this->out, ", has private key");
827 }
828 fprintf(this->out, "\n");
829
830 }
831 iterator->destroy(iterator);
832 }
833 if (msg->list.flags & LIST_CACERTS)
834 {
835 x509_t *cert;
836
837 iterator = charon->credentials->create_cacert_iterator(charon->credentials);
838 if (iterator->get_count(iterator))
839 {
840 fprintf(this->out, "\n");
841 fprintf(this->out, "List of X.509 CA Certificates:\n");
842 fprintf(this->out, "\n");
843 }
844 while (iterator->iterate(iterator, (void**)&cert))
845 {
846 fprintf(this->out, "%#Q\n", cert, msg->list.utc);
847 }
848 iterator->destroy(iterator);
849 }
850 if (msg->list.flags & LIST_CRLS)
851 {
852 crl_t *crl;
853
854 iterator = charon->credentials->create_crl_iterator(charon->credentials);
855 if (iterator->get_count(iterator))
856 {
857 fprintf(this->out, "\n");
858 fprintf(this->out, "List of X.509 CRLs:\n");
859 fprintf(this->out, "\n");
860 }
861 while (iterator->iterate(iterator, (void**)&crl))
862 {
863 fprintf(this->out, "%#U\n", crl, msg->list.utc);
864 }
865 iterator->destroy(iterator);
866 }
867 }
868
869 /**
870 * reread various information
871 */
872 static void stroke_reread(private_stroke_t *this, stroke_msg_t *msg)
873 {
874 if (msg->reread.flags & REREAD_CACERTS)
875 {
876 charon->credentials->load_ca_certificates(charon->credentials);
877 }
878 if (msg->reread.flags & REREAD_CRLS)
879 {
880 charon->credentials->load_crls(charon->credentials);
881 }
882 }
883
884 signal_t get_signal_from_logtype(char *type)
885 {
886 if (strcasecmp(type, "any") == 0) return SIG_ANY;
887 else if (strcasecmp(type, "mgr") == 0) return DBG_MGR;
888 else if (strcasecmp(type, "ike") == 0) return DBG_IKE;
889 else if (strcasecmp(type, "chd") == 0) return DBG_CHD;
890 else if (strcasecmp(type, "job") == 0) return DBG_JOB;
891 else if (strcasecmp(type, "cfg") == 0) return DBG_CFG;
892 else if (strcasecmp(type, "knl") == 0) return DBG_KNL;
893 else if (strcasecmp(type, "net") == 0) return DBG_NET;
894 else if (strcasecmp(type, "enc") == 0) return DBG_ENC;
895 else if (strcasecmp(type, "lib") == 0) return DBG_LIB;
896 else return -1;
897 }
898
899 /**
900 * set the verbosity debug output
901 */
902 static void stroke_loglevel(private_stroke_t *this, stroke_msg_t *msg)
903 {
904 signal_t signal;
905
906 pop_string(msg, &(msg->loglevel.type));
907 DBG1(DBG_CFG, "received stroke: loglevel %d for %s",
908 msg->loglevel.level, msg->loglevel.type);
909
910 signal = get_signal_from_logtype(msg->loglevel.type);
911 if (signal < 0)
912 {
913 fprintf(this->out, "invalid type (%s)!\n", msg->loglevel.type);
914 return;
915 }
916
917 charon->outlog->set_level(charon->outlog, signal, msg->loglevel.level);
918 charon->syslog->set_level(charon->syslog, signal, msg->loglevel.level);
919 }
920
921 /**
922 * Implementation of private_stroke_t.stroke_receive.
923 */
924 static void stroke_receive(private_stroke_t *this)
925 {
926 stroke_msg_t *msg;
927 u_int16_t msg_length;
928 struct sockaddr_un strokeaddr;
929 int strokeaddrlen = sizeof(strokeaddr);
930 ssize_t bytes_read;
931 int strokefd;
932 int oldstate;
933
934 /* ignore sigpipe. writing over the pipe back to the console
935 * only fails if SIGPIPE is ignored. */
936 signal(SIGPIPE, SIG_IGN);
937
938 /* disable cancellation by default */
939 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
940
941 while (1)
942 {
943 /* wait for connections, but allow thread to terminate */
944 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
945 strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
946 pthread_setcancelstate(oldstate, NULL);
947
948 if (strokefd < 0)
949 {
950 DBG1(DBG_CFG, "accepting stroke connection failed: %m");
951 continue;
952 }
953
954 /* peek the length */
955 bytes_read = recv(strokefd, &msg_length, sizeof(msg_length), MSG_PEEK);
956 if (bytes_read != sizeof(msg_length))
957 {
958 DBG1(DBG_CFG, "reading lenght of stroke message failed");
959 close(strokefd);
960 continue;
961 }
962
963 /* read message */
964 msg = malloc(msg_length);
965 bytes_read = recv(strokefd, msg, msg_length, 0);
966 if (bytes_read != msg_length)
967 {
968 DBG1(DBG_CFG, "reading stroke message failed: %m");
969 close(strokefd);
970 continue;
971 }
972
973 this->out = fdopen(dup(strokefd), "w");
974 if (this->out == NULL)
975 {
976 DBG1(DBG_CFG, "opening stroke output channel failed: %m");
977 close(strokefd);
978 free(msg);
979 continue;
980 }
981
982 DBG3(DBG_CFG, "stroke message %b", (void*)msg, msg_length);
983
984 switch (msg->type)
985 {
986 case STR_INITIATE:
987 stroke_initiate(this, msg);
988 break;
989 case STR_ROUTE:
990 stroke_route(this, msg, TRUE);
991 break;
992 case STR_UNROUTE:
993 stroke_route(this, msg, FALSE);
994 break;
995 case STR_TERMINATE:
996 stroke_terminate(this, msg);
997 break;
998 case STR_STATUS:
999 stroke_status(this, msg);
1000 break;
1001 case STR_STATUS_ALL:
1002 stroke_statusall(this, msg);
1003 break;
1004 case STR_ADD_CONN:
1005 stroke_add_conn(this, msg);
1006 break;
1007 case STR_DEL_CONN:
1008 stroke_del_conn(this, msg);
1009 break;
1010 case STR_LOGLEVEL:
1011 stroke_loglevel(this, msg);
1012 break;
1013 case STR_LIST:
1014 stroke_list(this, msg);
1015 break;
1016 case STR_REREAD:
1017 stroke_reread(this, msg);
1018 break;
1019 default:
1020 DBG1(DBG_CFG, "received unknown stroke");
1021 }
1022 fclose(this->out);
1023 close(strokefd);
1024 free(msg);
1025 }
1026 }
1027
1028 /**
1029 * Implementation of stroke_t.destroy.
1030 */
1031 static void destroy(private_stroke_t *this)
1032 {
1033 pthread_cancel(this->assigned_thread);
1034 pthread_join(this->assigned_thread, NULL);
1035
1036 close(this->socket);
1037 unlink(socket_addr.sun_path);
1038 free(this);
1039 }
1040
1041 /*
1042 * Described in header-file
1043 */
1044 stroke_t *stroke_create()
1045 {
1046 private_stroke_t *this = malloc_thing(private_stroke_t);
1047 mode_t old;
1048
1049 /* public functions */
1050 this->public.destroy = (void (*)(stroke_t*))destroy;
1051
1052 /* set up unix socket */
1053 this->socket = socket(AF_UNIX, SOCK_STREAM, 0);
1054 if (this->socket == -1)
1055 {
1056 DBG1(DBG_CFG, "could not create whack socket");
1057 free(this);
1058 return NULL;
1059 }
1060
1061 old = umask(~S_IRWXU);
1062 if (bind(this->socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
1063 {
1064 DBG1(DBG_CFG, "could not bind stroke socket: %m");
1065 close(this->socket);
1066 free(this);
1067 return NULL;
1068 }
1069 umask(old);
1070
1071 if (listen(this->socket, 0) < 0)
1072 {
1073 DBG1(DBG_CFG, "could not listen on stroke socket: %m");
1074 close(this->socket);
1075 unlink(socket_addr.sun_path);
1076 free(this);
1077 return NULL;
1078 }
1079
1080 /* start a thread reading from the socket */
1081 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))stroke_receive, this) != 0)
1082 {
1083 DBG1(DBG_CFG, "could not spawn stroke thread");
1084 close(this->socket);
1085 unlink(socket_addr.sun_path);
1086 free(this);
1087 return NULL;
1088 }
1089
1090 return (&this->public);
1091 }