Use a separate interface for loggers.
[strongswan.git] / src / libcharon / control / controller.c
1 /*
2 * Copyright (C) 2011 Tobias Brunner
3 * Copyright (C) 2007-2011 Martin Willi
4 * Copyright (C) 2011 revosec AG
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "controller.h"
19
20 #include <sys/types.h>
21 #include <dirent.h>
22 #include <sys/stat.h>
23 #include <dlfcn.h>
24
25 #include <daemon.h>
26 #include <library.h>
27 #include <threading/thread.h>
28 #include <threading/semaphore.h>
29
30 typedef struct private_controller_t private_controller_t;
31 typedef struct interface_listener_t interface_listener_t;
32 typedef struct interface_logger_t interface_logger_t;
33
34 /**
35 * Private data of an stroke_t object.
36 */
37 struct private_controller_t {
38
39 /**
40 * Public part of stroke_t object.
41 */
42 controller_t public;
43 };
44
45 /**
46 * helper struct for the logger interface
47 */
48 struct interface_logger_t {
49 /**
50 * public logger interface
51 */
52 logger_t public;
53
54 /**
55 * reference to the listener
56 */
57 interface_listener_t *listener;
58
59 /**
60 * interface callback (listener gets redirected to here)
61 */
62 controller_cb_t callback;
63
64 /**
65 * user parameter to pass to callback
66 */
67 void *param;
68 };
69
70 /**
71 * helper struct to map listener callbacks to interface callbacks
72 */
73 struct interface_listener_t {
74
75 /**
76 * public bus listener interface
77 */
78 listener_t public;
79
80 /**
81 * logger interface
82 */
83 interface_logger_t logger;
84
85 /**
86 * status of the operation, return to method callers
87 */
88 status_t status;
89
90 /**
91 * child configuration, used for initiate
92 */
93 child_cfg_t *child_cfg;
94
95 /**
96 * peer configuration, used for initiate
97 */
98 peer_cfg_t *peer_cfg;
99
100 /**
101 * IKE_SA to handle
102 */
103 ike_sa_t *ike_sa;
104
105 /**
106 * CHILD_SA to handle
107 */
108 child_sa_t *child_sa;
109
110 /**
111 * unique ID, used for various methods
112 */
113 u_int32_t id;
114
115 /**
116 * semaphore to implement wait_for_listener()
117 */
118 semaphore_t *done;
119 };
120
121
122 typedef struct interface_job_t interface_job_t;
123
124 /**
125 * job for asynchronous listen operations
126 */
127 struct interface_job_t {
128
129 /**
130 * job interface
131 */
132 job_t public;
133
134 /**
135 * associated listener
136 */
137 interface_listener_t listener;
138 };
139
140 /**
141 * This function properly unregisters a listener that is used
142 * with wait_for_listener()
143 */
144 static inline bool listener_done(interface_listener_t *listener)
145 {
146 if (listener->done)
147 {
148 listener->done->post(listener->done);
149 }
150 return FALSE;
151 }
152
153 /**
154 * thread_cleanup_t handler to unregister and cleanup a listener
155 */
156 static void listener_cleanup(interface_listener_t *listener)
157 {
158 charon->bus->remove_listener(charon->bus, &listener->public);
159 charon->bus->remove_logger(charon->bus, &listener->logger.public);
160 listener->done->destroy(listener->done);
161 }
162
163 /**
164 * Registers the listener, executes the job and then waits synchronously until
165 * the listener is done or the timeout occured.
166 *
167 * @note Use 'return listener_done(listener)' to properly unregister a listener
168 *
169 * @param listener listener to register
170 * @param job job to execute asynchronously when registered, or NULL
171 * @param timeout max timeout in ms to listen for events, 0 to disable
172 * @return TRUE if timed out
173 */
174 static bool wait_for_listener(interface_listener_t *listener, job_t *job,
175 u_int timeout)
176 {
177 bool old, timed_out = FALSE;
178
179 listener->done = semaphore_create(0);
180
181 charon->bus->add_logger(charon->bus, &listener->logger.public);
182 charon->bus->add_listener(charon->bus, &listener->public);
183 lib->processor->queue_job(lib->processor, job);
184
185 thread_cleanup_push((thread_cleanup_t)listener_cleanup, listener);
186 old = thread_cancelability(TRUE);
187 if (timeout)
188 {
189 timed_out = listener->done->timed_wait(listener->done, timeout);
190 }
191 else
192 {
193 listener->done->wait(listener->done);
194 }
195 thread_cancelability(old);
196 thread_cleanup_pop(TRUE);
197 return timed_out;
198 }
199
200 METHOD(logger_t, listener_log, void,
201 interface_logger_t *this, debug_t group, level_t level, int thread,
202 ike_sa_t *ike_sa, char* format, va_list args)
203 {
204 if (this->listener->ike_sa == ike_sa)
205 {
206 if (!this->callback(this->param, group, level, ike_sa, format, args))
207 {
208 this->listener->status = NEED_MORE;
209 listener_done(this->listener);
210 }
211 }
212 }
213
214 METHOD(job_t, get_priority_medium, job_priority_t,
215 job_t *this)
216 {
217 return JOB_PRIO_MEDIUM;
218 }
219
220 METHOD(listener_t, ike_state_change, bool,
221 interface_listener_t *this, ike_sa_t *ike_sa, ike_sa_state_t state)
222 {
223 if (this->ike_sa == ike_sa)
224 {
225 switch (state)
226 {
227 #ifdef ME
228 case IKE_ESTABLISHED:
229 { /* mediation connections are complete without CHILD_SA */
230 peer_cfg_t *peer_cfg = ike_sa->get_peer_cfg(ike_sa);
231
232 if (peer_cfg->is_mediation(peer_cfg))
233 {
234 this->status = SUCCESS;
235 return listener_done(this);
236 }
237 break;
238 }
239 #endif /* ME */
240 case IKE_DESTROYING:
241 if (ike_sa->get_state(ike_sa) == IKE_DELETING)
242 { /* proper termination */
243 this->status = SUCCESS;
244 }
245 return listener_done(this);
246 default:
247 break;
248 }
249 }
250 return TRUE;
251 }
252
253 METHOD(listener_t, child_state_change, bool,
254 interface_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
255 child_sa_state_t state)
256 {
257 if (this->ike_sa == ike_sa)
258 {
259 switch (state)
260 {
261 case CHILD_INSTALLED:
262 this->status = SUCCESS;
263 return listener_done(this);
264 case CHILD_DESTROYING:
265 switch (child_sa->get_state(child_sa))
266 {
267 case CHILD_DELETING:
268 /* proper delete */
269 this->status = SUCCESS;
270 break;
271 default:
272 break;
273 }
274 return listener_done(this);
275 default:
276 break;
277 }
278 }
279 return TRUE;
280 }
281
282 METHOD(job_t, recheckin, void,
283 interface_job_t *job)
284 {
285 if (job->listener.ike_sa)
286 {
287 charon->ike_sa_manager->checkin(charon->ike_sa_manager,
288 job->listener.ike_sa);
289 }
290 }
291
292 METHOD(controller_t, create_ike_sa_enumerator, enumerator_t*,
293 private_controller_t *this, bool wait)
294 {
295 return charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager,
296 wait);
297 }
298
299 METHOD(job_t, initiate_execute, void,
300 interface_job_t *job)
301 {
302 ike_sa_t *ike_sa;
303 interface_listener_t *listener = &job->listener;
304 peer_cfg_t *peer_cfg = listener->peer_cfg;
305
306 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
307 peer_cfg);
308 if (!ike_sa)
309 {
310 listener->child_cfg->destroy(listener->child_cfg);
311 peer_cfg->destroy(peer_cfg);
312 /* trigger down event to release listener */
313 listener->ike_sa = charon->ike_sa_manager->checkout_new(
314 charon->ike_sa_manager, IKE_ANY, TRUE);
315 DESTROY_IF(listener->ike_sa);
316 listener->status = FAILED;
317 return;
318 }
319 listener->ike_sa = ike_sa;
320
321 if (ike_sa->get_peer_cfg(ike_sa) == NULL)
322 {
323 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
324 }
325 peer_cfg->destroy(peer_cfg);
326
327 if (ike_sa->initiate(ike_sa, listener->child_cfg, 0, NULL, NULL) == SUCCESS)
328 {
329 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
330 listener->status = SUCCESS;
331 }
332 else
333 {
334 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
335 ike_sa);
336 listener->status = FAILED;
337 }
338 }
339
340 METHOD(controller_t, initiate, status_t,
341 private_controller_t *this, peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
342 controller_cb_t callback, void *param, u_int timeout)
343 {
344 interface_job_t job = {
345 .listener = {
346 .public = {
347 .ike_state_change = _ike_state_change,
348 .child_state_change = _child_state_change,
349 },
350 .logger = {
351 .public = {
352 .log = _listener_log,
353 },
354 .callback = callback,
355 .param = param,
356 },
357 .status = FAILED,
358 .child_cfg = child_cfg,
359 .peer_cfg = peer_cfg,
360 },
361 .public = {
362 .execute = _initiate_execute,
363 .get_priority = _get_priority_medium,
364 .destroy = _recheckin,
365 },
366 };
367 job.listener.logger.listener = &job.listener;
368
369 if (callback == NULL)
370 {
371 initiate_execute(&job);
372 }
373 else
374 {
375 if (wait_for_listener(&job.listener, &job.public, timeout))
376 {
377 job.listener.status = OUT_OF_RES;
378 }
379 }
380 return job.listener.status;
381 }
382
383 METHOD(job_t, terminate_ike_execute, void,
384 interface_job_t *job)
385 {
386 interface_listener_t *listener = &job->listener;
387 ike_sa_t *ike_sa = listener->ike_sa;
388
389 charon->bus->set_sa(charon->bus, ike_sa);
390
391 if (ike_sa->delete(ike_sa) != DESTROY_ME)
392 {
393 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
394 /* delete failed */
395 listener->status = FAILED;
396 }
397 else
398 {
399 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
400 ike_sa);
401 listener->status = SUCCESS;
402 }
403 }
404
405 METHOD(controller_t, terminate_ike, status_t,
406 controller_t *this, u_int32_t unique_id,
407 controller_cb_t callback, void *param, u_int timeout)
408 {
409 ike_sa_t *ike_sa;
410 interface_job_t job = {
411 .listener = {
412 .public = {
413 .ike_state_change = _ike_state_change,
414 .child_state_change = _child_state_change,
415 },
416 .logger = {
417 .public = {
418 .log = _listener_log,
419 },
420 .callback = callback,
421 .param = param,
422 },
423 .status = FAILED,
424 .id = unique_id,
425 },
426 .public = {
427 .execute = _terminate_ike_execute,
428 .get_priority = _get_priority_medium,
429 .destroy = _recheckin,
430 },
431 };
432 job.listener.logger.listener = &job.listener;
433
434 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
435 unique_id, FALSE);
436 if (ike_sa == NULL)
437 {
438 DBG1(DBG_IKE, "unable to terminate IKE_SA: ID %d not found", unique_id);
439 return NOT_FOUND;
440 }
441 job.listener.ike_sa = ike_sa;
442
443 if (callback == NULL)
444 {
445 terminate_ike_execute(&job);
446 }
447 else
448 {
449 if (wait_for_listener(&job.listener, &job.public, timeout))
450 {
451 job.listener.status = OUT_OF_RES;
452 }
453 /* checkin of the ike_sa happened in the thread that executed the job */
454 charon->bus->set_sa(charon->bus, NULL);
455 }
456 return job.listener.status;
457 }
458
459 METHOD(job_t, terminate_child_execute, void,
460 interface_job_t *job)
461 {
462 interface_listener_t *listener = &job->listener;
463 ike_sa_t *ike_sa = listener->ike_sa;
464 child_sa_t *child_sa = listener->child_sa;
465
466 charon->bus->set_sa(charon->bus, ike_sa);
467 if (ike_sa->delete_child_sa(ike_sa, child_sa->get_protocol(child_sa),
468 child_sa->get_spi(child_sa, TRUE), FALSE) != DESTROY_ME)
469 {
470 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
471 listener->status = SUCCESS;
472 }
473 else
474 {
475 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
476 ike_sa);
477 listener->status = FAILED;
478 }
479 }
480
481 METHOD(controller_t, terminate_child, status_t,
482 controller_t *this, u_int32_t reqid,
483 controller_cb_t callback, void *param, u_int timeout)
484 {
485 ike_sa_t *ike_sa;
486 child_sa_t *child_sa;
487 enumerator_t *enumerator;
488 interface_job_t job = {
489 .listener = {
490 .public = {
491 .ike_state_change = _ike_state_change,
492 .child_state_change = _child_state_change,
493 },
494 .logger = {
495 .public = {
496 .log = _listener_log,
497 },
498 .callback = callback,
499 .param = param,
500 },
501 .status = FAILED,
502 .id = reqid,
503 },
504 .public = {
505 .execute = _terminate_child_execute,
506 .get_priority = _get_priority_medium,
507 .destroy = _recheckin,
508 },
509 };
510 job.listener.logger.listener = &job.listener;
511
512 ike_sa = charon->ike_sa_manager->checkout_by_id(charon->ike_sa_manager,
513 reqid, TRUE);
514 if (ike_sa == NULL)
515 {
516 DBG1(DBG_IKE, "unable to terminate, CHILD_SA with ID %d not found",
517 reqid);
518 return NOT_FOUND;
519 }
520 job.listener.ike_sa = ike_sa;
521
522 enumerator = ike_sa->create_child_sa_enumerator(ike_sa);
523 while (enumerator->enumerate(enumerator, (void**)&child_sa))
524 {
525 if (child_sa->get_state(child_sa) != CHILD_ROUTED &&
526 child_sa->get_reqid(child_sa) == reqid)
527 {
528 break;
529 }
530 child_sa = NULL;
531 }
532 enumerator->destroy(enumerator);
533
534 if (child_sa == NULL)
535 {
536 DBG1(DBG_IKE, "unable to terminate, established "
537 "CHILD_SA with ID %d not found", reqid);
538 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
539 return NOT_FOUND;
540 }
541 job.listener.child_sa = child_sa;
542
543 if (callback == NULL)
544 {
545 terminate_child_execute(&job);
546 }
547 else
548 {
549 if (wait_for_listener(&job.listener, &job.public, timeout))
550 {
551 job.listener.status = OUT_OF_RES;
552 }
553 /* checkin of the ike_sa happened in the thread that executed the job */
554 charon->bus->set_sa(charon->bus, NULL);
555 }
556 return job.listener.status;
557 }
558
559 /**
560 * See header
561 */
562 bool controller_cb_empty(void *param, debug_t group, level_t level,
563 ike_sa_t *ike_sa, char *format, va_list args)
564 {
565 return TRUE;
566 }
567
568 METHOD(controller_t, destroy, void,
569 private_controller_t *this)
570 {
571 free(this);
572 }
573
574 /*
575 * Described in header-file
576 */
577 controller_t *controller_create(void)
578 {
579 private_controller_t *this;
580
581 INIT(this,
582 .public = {
583 .create_ike_sa_enumerator = _create_ike_sa_enumerator,
584 .initiate = _initiate,
585 .terminate_ike = _terminate_ike,
586 .terminate_child = _terminate_child,
587 .destroy = _destroy,
588 },
589 );
590
591 return &this->public;
592 }
593