dc8c96ec7f7e20e6e34d693dc88b3df2b582fc6b
[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(stroke_msg_t *msg, FILE *out)
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.reauth,
351 msg->add_conn.rekey.tries,
352 msg->add_conn.rekey.ike_lifetime,
353 msg->add_conn.rekey.ike_lifetime - msg->add_conn.rekey.margin,
354 msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100);
355
356 if (msg->add_conn.algorithms.ike)
357 {
358 char *proposal_string;
359 char *strict = msg->add_conn.algorithms.ike + strlen(msg->add_conn.algorithms.ike) - 1;
360
361 if (*strict == '!')
362 *strict = '\0';
363 else
364 strict = NULL;
365
366 while ((proposal_string = strsep(&msg->add_conn.algorithms.ike, ",")))
367 {
368 proposal = proposal_create_from_string(PROTO_IKE, proposal_string);
369 if (proposal == NULL)
370 {
371 DBG1(DBG_CFG, "invalid IKE proposal string: %s", proposal_string);
372 my_id->destroy(my_id);
373 other_id->destroy(other_id);
374 my_ts->destroy(my_ts);
375 other_ts->destroy(other_ts);
376 my_ca->destroy(my_ca);
377 other_ca->destroy(other_ca);
378 connection->destroy(connection);
379 return;
380 }
381 connection->add_proposal(connection, proposal);
382 }
383 if (!strict)
384 {
385 proposal = proposal_create_default(PROTO_IKE);
386 connection->add_proposal(connection, proposal);
387 }
388 }
389 else
390 {
391 proposal = proposal_create_default(PROTO_IKE);
392 connection->add_proposal(connection, proposal);
393 }
394
395 policy = policy_create(msg->add_conn.name, my_id, other_id,
396 msg->add_conn.auth_method, msg->add_conn.eap_type,
397 msg->add_conn.rekey.ipsec_lifetime,
398 msg->add_conn.rekey.ipsec_lifetime - msg->add_conn.rekey.margin,
399 msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100,
400 msg->add_conn.me.updown, msg->add_conn.me.hostaccess,
401 msg->add_conn.mode, msg->add_conn.dpd.action);
402 policy->add_my_traffic_selector(policy, my_ts);
403 policy->add_other_traffic_selector(policy, other_ts);
404 policy->add_authorities(policy, my_ca, other_ca);
405
406 if (msg->add_conn.algorithms.esp)
407 {
408 char *proposal_string;
409 char *strict = msg->add_conn.algorithms.esp + strlen(msg->add_conn.algorithms.esp) - 1;
410
411 if (*strict == '!')
412 *strict = '\0';
413 else
414 strict = NULL;
415
416 while ((proposal_string = strsep(&msg->add_conn.algorithms.esp, ",")))
417 {
418 proposal = proposal_create_from_string(PROTO_ESP, proposal_string);
419 if (proposal == NULL)
420 {
421 DBG1(DBG_CFG, "invalid ESP proposal string: %s", proposal_string);
422 policy->destroy(policy);
423 connection->destroy(connection);
424 return;
425 }
426 policy->add_proposal(policy, proposal);
427 }
428 if (!strict)
429 {
430 proposal = proposal_create_default(PROTO_ESP);
431 policy->add_proposal(policy, proposal);
432 }
433 }
434 else
435 {
436 proposal = proposal_create_default(PROTO_ESP);
437 policy->add_proposal(policy, proposal);
438 }
439
440 /* add to global connection list */
441 charon->connections->add_connection(charon->connections, connection);
442 DBG1(DBG_CFG, "added connection '%s': %H[%D]...%H[%D]",
443 msg->add_conn.name, my_host, my_id, other_host, other_id);
444 /* add to global policy list */
445 charon->policies->add_policy(charon->policies, policy);
446
447 return;
448
449 /* mopping up after parsing errors */
450
451 destroy_ids:
452 my_id->destroy(my_id);
453 other_id->destroy(other_id);
454
455 destroy_hosts:
456 my_host->destroy(my_host);
457 other_host->destroy(other_host);
458 }
459
460 /**
461 * Delete a connection from the list
462 */
463 static void stroke_del_conn(stroke_msg_t *msg, FILE *out)
464 {
465 status_t status;
466
467 pop_string(msg, &(msg->del_conn.name));
468 DBG1(DBG_CFG, "received stroke: delete '%s'", msg->del_conn.name);
469
470 status = charon->connections->delete_connection(charon->connections,
471 msg->del_conn.name);
472 charon->policies->delete_policy(charon->policies, msg->del_conn.name);
473 if (status == SUCCESS)
474 {
475 fprintf(out, "deleted connection '%s'\n", msg->del_conn.name);
476 }
477 else
478 {
479 fprintf(out, "no connection named '%s'\n", msg->del_conn.name);
480 }
481 }
482
483 /**
484 * initiate a connection by name
485 */
486 static void stroke_initiate(stroke_msg_t *msg, FILE *out)
487 {
488 initiate_job_t *job;
489 connection_t *connection;
490 policy_t *policy;
491 ike_sa_t *init_ike_sa = NULL;
492 signal_t signal;
493
494 pop_string(msg, &(msg->initiate.name));
495 DBG1(DBG_CFG, "received stroke: initiate '%s'", msg->initiate.name);
496
497 connection = charon->connections->get_connection_by_name(charon->connections,
498 msg->initiate.name);
499 if (connection == NULL)
500 {
501 if (msg->output_verbosity >= 0)
502 {
503 fprintf(out, "no connection named '%s'\n", msg->initiate.name);
504 }
505 return;
506 }
507 if (!connection->is_ikev2(connection))
508 {
509 connection->destroy(connection);
510 return;
511 }
512
513 policy = charon->policies->get_policy_by_name(charon->policies,
514 msg->initiate.name);
515 if (policy == NULL)
516 {
517 if (msg->output_verbosity >= 0)
518 {
519 fprintf(out, "no policy named '%s'\n", msg->initiate.name);
520 }
521 connection->destroy(connection);
522 return;
523 }
524
525 job = initiate_job_create(connection, NULL, policy);
526 /*
527 if (msg->output_verbosity < 0)
528 {
529 TODO: detach immediately if verbosity is SILENT. Local credential store
530 is not threadsave yet, so this would cause crashes!!
531 charon->job_queue->add(charon->job_queue, (job_t*)job);
532 return;
533 }*/
534
535 charon->bus->set_listen_state(charon->bus, TRUE);
536 charon->job_queue->add(charon->job_queue, (job_t*)job);
537 while (TRUE)
538 {
539 level_t level;
540 int thread;
541 ike_sa_t *ike_sa;
542 char* format;
543 va_list args;
544
545 signal = charon->bus->listen(charon->bus, &level, &thread, &ike_sa, &format, &args);
546
547 if ((init_ike_sa == NULL || ike_sa == init_ike_sa) &&
548 level <= msg->output_verbosity)
549 {
550 if (vfprintf(out, format, args) < 0 ||
551 fprintf(out, "\n") < 0 ||
552 fflush(out))
553 {
554 charon->bus->set_listen_state(charon->bus, FALSE);
555 break;
556 }
557 }
558
559 switch (signal)
560 {
561 case CHILD_UP_SUCCESS:
562 case CHILD_UP_FAILED:
563 case IKE_UP_FAILED:
564 if (ike_sa == init_ike_sa)
565 {
566 charon->bus->set_listen_state(charon->bus, FALSE);
567 return;
568 }
569 continue;
570 case CHILD_UP_START:
571 case IKE_UP_START:
572 if (init_ike_sa == NULL)
573 {
574 init_ike_sa = ike_sa;
575 }
576 continue;
577 default:
578 continue;
579 }
580 }
581 }
582
583 /**
584 * route/unroute a policy (install SPD entries)
585 */
586 static void stroke_route(stroke_msg_t *msg, FILE *out, bool route)
587 {
588 route_job_t *job;
589 connection_t *connection;
590 policy_t *policy;
591
592 pop_string(msg, &(msg->route.name));
593 DBG1(DBG_CFG, "received stroke: %s '%s'",
594 route ? "route" : "unroute", msg->route.name);
595
596 /* we wouldn't need a connection, but we only want to route policies
597 * whose connections are keyexchange=ikev2. */
598 connection = charon->connections->get_connection_by_name(charon->connections,
599 msg->route.name);
600 if (connection == NULL)
601 {
602 fprintf(out, "no connection named '%s'\n", msg->route.name);
603 return;
604 }
605 if (!connection->is_ikev2(connection))
606 {
607 connection->destroy(connection);
608 return;
609 }
610
611 policy = charon->policies->get_policy_by_name(charon->policies,
612 msg->route.name);
613 if (policy == NULL)
614 {
615 fprintf(out, "no policy named '%s'\n", msg->route.name);
616 connection->destroy(connection);
617 return;
618 }
619 fprintf(out, "%s policy '%s'\n",
620 route ? "routing" : "unrouting", msg->route.name);
621 job = route_job_create(connection, policy, route);
622 charon->job_queue->add(charon->job_queue, (job_t*)job);
623 }
624
625 /**
626 * terminate a connection by name
627 */
628 static void stroke_terminate(stroke_msg_t *msg, FILE *out)
629 {
630 pop_string(msg, &(msg->terminate.name));
631 DBG1(DBG_CFG, "received stroke: terminate '%s'", msg->terminate.name);
632
633 charon->ike_sa_manager->delete_by_name(charon->ike_sa_manager, msg->terminate.name);
634 }
635
636 /**
637 * show status of daemon
638 */
639 static void stroke_statusall(stroke_msg_t *msg, FILE *out)
640 {
641 iterator_t *iterator;
642 linked_list_t *list;
643 host_t *host;
644 connection_t *connection;
645 policy_t *policy;
646 ike_sa_t *ike_sa;
647 char *name = NULL;
648
649 leak_detective_status(out);
650
651 fprintf(out, "Performance:\n");
652 fprintf(out, " worker threads: %d idle of %d,",
653 charon->thread_pool->get_idle_threads(charon->thread_pool),
654 charon->thread_pool->get_pool_size(charon->thread_pool));
655 fprintf(out, " job queue load: %d,",
656 charon->job_queue->get_count(charon->job_queue));
657 fprintf(out, " scheduled events: %d\n",
658 charon->event_queue->get_count(charon->event_queue));
659 list = charon->socket->create_local_address_list(charon->socket);
660
661 fprintf(out, "Listening on %d IP addresses:\n", list->get_count(list));
662 while (list->remove_first(list, (void**)&host) == SUCCESS)
663 {
664 fprintf(out, " %H\n", host);
665 host->destroy(host);
666 }
667 list->destroy(list);
668
669 if (msg->status.name)
670 {
671 pop_string(msg, &(msg->status.name));
672 name = msg->status.name;
673 }
674
675 iterator = charon->connections->create_iterator(charon->connections);
676 if (iterator->get_count(iterator) > 0)
677 {
678 fprintf(out, "Connections:\n");
679 }
680 while (iterator->iterate(iterator, (void**)&connection))
681 {
682 if (connection->is_ikev2(connection)
683 && (name == NULL || streq(name, connection->get_name(connection))))
684 {
685 fprintf(out, "%12s: %H...%H\n",
686 connection->get_name(connection),
687 connection->get_my_host(connection),
688 connection->get_other_host(connection));
689 }
690 }
691 iterator->destroy(iterator);
692
693 iterator = charon->policies->create_iterator(charon->policies);
694 if (iterator->get_count(iterator) > 0)
695 {
696 fprintf(out, "Policies:\n");
697 }
698 while (iterator->iterate(iterator, (void**)&policy))
699 {
700 if (name == NULL || streq(name, policy->get_name(policy)))
701 {
702 fprintf(out, "%12s: '%D'...'%D'\n",
703 policy->get_name(policy),
704 policy->get_my_id(policy),
705 policy->get_other_id(policy));
706 }
707 }
708 iterator->destroy(iterator);
709
710 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
711 if (iterator->get_count(iterator) > 0)
712 {
713 fprintf(out, "Security Associations:\n");
714 }
715 while (iterator->iterate(iterator, (void**)&ike_sa))
716 {
717 bool ike_sa_printed = FALSE;
718 child_sa_t *child_sa;
719 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
720
721 /* print IKE_SA */
722 if (name == NULL || strncmp(name, ike_sa->get_name(ike_sa), strlen(name)) == 0)
723 {
724 fprintf(out, "%#K\n", ike_sa);
725 ike_sa_printed = TRUE;
726 }
727
728 while (children->iterate(children, (void**)&child_sa))
729 {
730 bool child_sa_match = name == NULL ||
731 strncmp(name, child_sa->get_name(child_sa), strlen(name)) == 0;
732
733 /* print IKE_SA if its name differs from the CHILD_SA's name */
734 if (!ike_sa_printed && child_sa_match)
735 {
736 fprintf(out, "%#K\n", ike_sa);
737 ike_sa_printed = TRUE;
738 }
739
740 /* print CHILD_SA */
741 if (child_sa_match)
742 {
743 fprintf(out, "%#P\n", child_sa);
744 }
745 }
746 children->destroy(children);
747 }
748 iterator->destroy(iterator);
749 }
750
751 /**
752 * show status of daemon
753 */
754 static void stroke_status(stroke_msg_t *msg, FILE *out)
755 {
756 iterator_t *iterator;
757 ike_sa_t *ike_sa;
758 char *name = NULL;
759
760 if (msg->status.name)
761 {
762 pop_string(msg, &(msg->status.name));
763 name = msg->status.name;
764 }
765
766 iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager);
767 while (iterator->iterate(iterator, (void**)&ike_sa))
768 {
769 bool ike_sa_printed = FALSE;
770 child_sa_t *child_sa;
771 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
772
773 /* print IKE_SA */
774 if (name == NULL || strncmp(name, ike_sa->get_name(ike_sa), strlen(name)) == 0)
775 {
776 fprintf(out, "%K\n", ike_sa);
777 ike_sa_printed = TRUE;
778 }
779
780 while (children->iterate(children, (void**)&child_sa))
781 {
782 bool child_sa_match = name == NULL ||
783 strncmp(name, child_sa->get_name(child_sa), strlen(name)) == 0;
784
785 /* print IKE_SA if its name differs from the CHILD_SA's name */
786 if (!ike_sa_printed && child_sa_match)
787 {
788 fprintf(out, "%K\n", ike_sa);
789 ike_sa_printed = TRUE;
790 }
791
792 /* print CHILD_SA */
793 if (child_sa_match)
794 {
795 fprintf(out, "%P\n", child_sa);
796 }
797 }
798 children->destroy(children);
799 }
800 iterator->destroy(iterator);
801 }
802
803 /**
804 * list various information
805 */
806 static void stroke_list(stroke_msg_t *msg, FILE *out)
807 {
808 iterator_t *iterator;
809
810 if (msg->list.flags & LIST_CERTS)
811 {
812 x509_t *cert;
813
814 iterator = charon->credentials->create_cert_iterator(charon->credentials);
815 if (iterator->get_count(iterator))
816 {
817 fprintf(out, "\n");
818 fprintf(out, "List of X.509 End Entity Certificates:\n");
819 fprintf(out, "\n");
820 }
821 while (iterator->iterate(iterator, (void**)&cert))
822 {
823 fprintf(out, "%#Q", cert, msg->list.utc);
824 if (charon->credentials->has_rsa_private_key(
825 charon->credentials, cert->get_public_key(cert)))
826 {
827 fprintf(out, ", has private key");
828 }
829 fprintf(out, "\n");
830
831 }
832 iterator->destroy(iterator);
833 }
834 if (msg->list.flags & LIST_CACERTS)
835 {
836 x509_t *cert;
837
838 iterator = charon->credentials->create_cacert_iterator(charon->credentials);
839 if (iterator->get_count(iterator))
840 {
841 fprintf(out, "\n");
842 fprintf(out, "List of X.509 CA Certificates:\n");
843 fprintf(out, "\n");
844 }
845 while (iterator->iterate(iterator, (void**)&cert))
846 {
847 fprintf(out, "%#Q\n", cert, msg->list.utc);
848 }
849 iterator->destroy(iterator);
850 }
851 if (msg->list.flags & LIST_CRLS)
852 {
853 crl_t *crl;
854
855 iterator = charon->credentials->create_crl_iterator(charon->credentials);
856 if (iterator->get_count(iterator))
857 {
858 fprintf(out, "\n");
859 fprintf(out, "List of X.509 CRLs:\n");
860 fprintf(out, "\n");
861 }
862 while (iterator->iterate(iterator, (void**)&crl))
863 {
864 fprintf(out, "%#U\n", crl, msg->list.utc);
865 }
866 iterator->destroy(iterator);
867 }
868 }
869
870 /**
871 * reread various information
872 */
873 static void stroke_reread(stroke_msg_t *msg, FILE *out)
874 {
875 if (msg->reread.flags & REREAD_CACERTS)
876 {
877 charon->credentials->load_ca_certificates(charon->credentials);
878 }
879 if (msg->reread.flags & REREAD_CRLS)
880 {
881 charon->credentials->load_crls(charon->credentials);
882 }
883 }
884
885 signal_t get_signal_from_logtype(char *type)
886 {
887 if (strcasecmp(type, "any") == 0) return SIG_ANY;
888 else if (strcasecmp(type, "mgr") == 0) return DBG_MGR;
889 else if (strcasecmp(type, "ike") == 0) return DBG_IKE;
890 else if (strcasecmp(type, "chd") == 0) return DBG_CHD;
891 else if (strcasecmp(type, "job") == 0) return DBG_JOB;
892 else if (strcasecmp(type, "cfg") == 0) return DBG_CFG;
893 else if (strcasecmp(type, "knl") == 0) return DBG_KNL;
894 else if (strcasecmp(type, "net") == 0) return DBG_NET;
895 else if (strcasecmp(type, "enc") == 0) return DBG_ENC;
896 else if (strcasecmp(type, "lib") == 0) return DBG_LIB;
897 else return -1;
898 }
899
900 /**
901 * set the verbosity debug output
902 */
903 static void stroke_loglevel(stroke_msg_t *msg, FILE *out)
904 {
905 signal_t signal;
906
907 pop_string(msg, &(msg->loglevel.type));
908 DBG1(DBG_CFG, "received stroke: loglevel %d for %s",
909 msg->loglevel.level, msg->loglevel.type);
910
911 signal = get_signal_from_logtype(msg->loglevel.type);
912 if (signal < 0)
913 {
914 fprintf(out, "invalid type (%s)!\n", msg->loglevel.type);
915 return;
916 }
917
918 charon->outlog->set_level(charon->outlog, signal, msg->loglevel.level);
919 charon->syslog->set_level(charon->syslog, signal, msg->loglevel.level);
920 }
921
922 /**
923 * process a stroke request from the socket pointed by "fd"
924 */
925 static void stroke_process(int *fd)
926 {
927 stroke_msg_t *msg;
928 u_int16_t msg_length;
929 ssize_t bytes_read;
930 FILE *out;
931 int strokefd = *fd;
932
933 /* peek the length */
934 bytes_read = recv(strokefd, &msg_length, sizeof(msg_length), MSG_PEEK);
935 if (bytes_read != sizeof(msg_length))
936 {
937 DBG1(DBG_CFG, "reading length of stroke message failed");
938 close(strokefd);
939 return;
940 }
941
942 /* read message */
943 msg = malloc(msg_length);
944 bytes_read = recv(strokefd, msg, msg_length, 0);
945 if (bytes_read != msg_length)
946 {
947 DBG1(DBG_CFG, "reading stroke message failed: %m");
948 close(strokefd);
949 return;
950 }
951
952 out = fdopen(dup(strokefd), "w");
953 if (out == NULL)
954 {
955 DBG1(DBG_CFG, "opening stroke output channel failed: %m");
956 close(strokefd);
957 free(msg);
958 return;
959 }
960
961 DBG3(DBG_CFG, "stroke message %b", (void*)msg, msg_length);
962
963 switch (msg->type)
964 {
965 case STR_INITIATE:
966 stroke_initiate(msg, out);
967 break;
968 case STR_ROUTE:
969 stroke_route(msg, out, TRUE);
970 break;
971 case STR_UNROUTE:
972 stroke_route(msg, out, FALSE);
973 break;
974 case STR_TERMINATE:
975 stroke_terminate(msg, out);
976 break;
977 case STR_STATUS:
978 stroke_status(msg, out);
979 break;
980 case STR_STATUS_ALL:
981 stroke_statusall(msg, out);
982 break;
983 case STR_ADD_CONN:
984 stroke_add_conn(msg, out);
985 break;
986 case STR_DEL_CONN:
987 stroke_del_conn(msg, out);
988 break;
989 case STR_LOGLEVEL:
990 stroke_loglevel(msg, out);
991 break;
992 case STR_LIST:
993 stroke_list(msg, out);
994 break;
995 case STR_REREAD:
996 stroke_reread(msg, out);
997 break;
998 default:
999 DBG1(DBG_CFG, "received unknown stroke");
1000 }
1001 fclose(out);
1002 close(strokefd);
1003 free(msg);
1004 }
1005
1006 /**
1007 * Implementation of private_stroke_t.stroke_receive.
1008 */
1009 static void stroke_receive(private_stroke_t *this)
1010 {
1011 struct sockaddr_un strokeaddr;
1012 int strokeaddrlen = sizeof(strokeaddr);
1013 int strokefd;
1014 int oldstate;
1015 pthread_t thread;
1016
1017 /* ignore sigpipe. writing over the pipe back to the console
1018 * only fails if SIGPIPE is ignored. */
1019 signal(SIGPIPE, SIG_IGN);
1020
1021 /* disable cancellation by default */
1022 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
1023
1024 while (TRUE)
1025 {
1026 /* wait for connections, but allow thread to terminate */
1027 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
1028 strokefd = accept(this->socket, (struct sockaddr *)&strokeaddr, &strokeaddrlen);
1029 pthread_setcancelstate(oldstate, NULL);
1030
1031 if (strokefd < 0)
1032 {
1033 DBG1(DBG_CFG, "accepting stroke connection failed: %m");
1034 continue;
1035 }
1036
1037 /* handle request asynchronously */
1038 if (pthread_create(&thread, NULL, (void*(*)(void*))stroke_process, (void*)&strokefd) != 0)
1039 {
1040 DBG1(DBG_CFG, "failed to spawn stroke thread: %m");
1041 }
1042 /* detach so the thread terminates cleanly */
1043 pthread_detach(thread);
1044 }
1045 }
1046
1047 /**
1048 * Implementation of stroke_t.destroy.
1049 */
1050 static void destroy(private_stroke_t *this)
1051 {
1052 pthread_cancel(this->assigned_thread);
1053 pthread_join(this->assigned_thread, NULL);
1054
1055 close(this->socket);
1056 unlink(socket_addr.sun_path);
1057 free(this);
1058 }
1059
1060 /*
1061 * Described in header-file
1062 */
1063 stroke_t *stroke_create()
1064 {
1065 private_stroke_t *this = malloc_thing(private_stroke_t);
1066 mode_t old;
1067
1068 /* public functions */
1069 this->public.destroy = (void (*)(stroke_t*))destroy;
1070
1071 /* set up unix socket */
1072 this->socket = socket(AF_UNIX, SOCK_STREAM, 0);
1073 if (this->socket == -1)
1074 {
1075 DBG1(DBG_CFG, "could not create whack socket");
1076 free(this);
1077 return NULL;
1078 }
1079
1080 old = umask(~S_IRWXU);
1081 if (bind(this->socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0)
1082 {
1083 DBG1(DBG_CFG, "could not bind stroke socket: %m");
1084 close(this->socket);
1085 free(this);
1086 return NULL;
1087 }
1088 umask(old);
1089
1090 if (listen(this->socket, 0) < 0)
1091 {
1092 DBG1(DBG_CFG, "could not listen on stroke socket: %m");
1093 close(this->socket);
1094 unlink(socket_addr.sun_path);
1095 free(this);
1096 return NULL;
1097 }
1098
1099 /* start a thread reading from the socket */
1100 if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))stroke_receive, this) != 0)
1101 {
1102 DBG1(DBG_CFG, "could not spawn stroke thread");
1103 close(this->socket);
1104 unlink(socket_addr.sun_path);
1105 free(this);
1106 return NULL;
1107 }
1108
1109 return (&this->public);
1110 }