443df635c344a299b67a61b23fec664efaaca249
[strongswan.git] / src / charon / control / interfaces / dbus_interface.c
1 /**
2 * @file dbus_interface.c
3 *
4 * @brief Implementation of dbus_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 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 #define DBUS_API_SUBJECT_TO_CHANGE
24 #include <dbus/dbus.h>
25 #include <NetworkManager/NetworkManager.h>
26 #include <NetworkManager/NetworkManagerVPN.h>
27 #include <stdlib.h>
28
29 #include "dbus_interface.h"
30
31 #include <library.h>
32 #include <daemon.h>
33
34
35 #define NM_DBUS_SERVICE_STRONG "org.freedesktop.NetworkManager.strongswan"
36 #define NM_DBUS_INTERFACE_STRONG "org.freedesktop.NetworkManager.strongswan"
37 #define NM_DBUS_PATH_STRONG "/org/freedesktop/NetworkManager/strongswan"
38
39 typedef struct private_dbus_interface_t private_dbus_interface_t;
40
41 /**
42 * Private data of an dbus_interface_t object.
43 */
44 struct private_dbus_interface_t {
45
46 /**
47 * Public part of dbus_t object.
48 */
49 dbus_interface_t public;
50
51 /**
52 * DBUS connection
53 */
54 DBusConnection* conn;
55
56 /**
57 * error value used here and there
58 */
59 DBusError err;
60
61 /**
62 * state of the daemon
63 */
64 NMVPNState state;
65
66 /**
67 * dispatcher thread for DBUS messages
68 */
69 pthread_t thread;
70
71 /**
72 * name of the currently active connection
73 */
74 char *name;
75 };
76
77 /**
78 * set daemon state and send StateChange signal to the bus
79 */
80 static void set_state(private_dbus_interface_t *this, NMVPNState state)
81 {
82 DBusMessage* msg;
83
84 msg = dbus_message_new_signal(NM_DBUS_PATH_STRONG, NM_DBUS_INTERFACE_STRONG, NM_DBUS_VPN_SIGNAL_STATE_CHANGE);
85
86 if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &this->state,
87 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID) ||
88 !dbus_connection_send(this->conn, msg, NULL))
89 {
90 DBG1(DBG_CFG, "unable to send DBUS StateChange signal");
91 }
92 dbus_connection_flush(this->conn);
93 dbus_message_unref(msg);
94 this->state = state;
95 }
96
97
98 /**
99 * get the child_cfg with the same name as the peer cfg
100 */
101 static child_cfg_t* get_child_from_peer(peer_cfg_t *peer_cfg, char *name)
102 {
103 child_cfg_t *current, *found = NULL;
104 iterator_t *iterator;
105
106 iterator = peer_cfg->create_child_cfg_iterator(peer_cfg);
107 while (iterator->iterate(iterator, (void**)&current))
108 {
109 if (streq(current->get_name(current), name))
110 {
111 found = current;
112 found->get_ref(found);
113 break;
114 }
115 }
116 iterator->destroy(iterator);
117 return found;
118 }
119
120 /**
121 * get a peer configuration by its name, or a name of its children
122 */
123 static peer_cfg_t *get_peer_cfg_by_name(char *name)
124 {
125 iterator_t *i1, *i2;
126 peer_cfg_t *current, *found = NULL;
127 child_cfg_t *child;
128
129 i1 = charon->backends->create_iterator(charon->backends);
130 while (i1->iterate(i1, (void**)&current))
131 {
132 /* compare peer_cfgs name first */
133 if (streq(current->get_name(current), name))
134 {
135 found = current;
136 found->get_ref(found);
137 break;
138 }
139 /* compare all child_cfg names otherwise */
140 i2 = current->create_child_cfg_iterator(current);
141 while (i2->iterate(i2, (void**)&child))
142 {
143 if (streq(child->get_name(child), name))
144 {
145 found = current;
146 found->get_ref(found);
147 break;
148 }
149 }
150 i2->destroy(i2);
151 if (found)
152 {
153 break;
154 }
155 }
156 i1->destroy(i1);
157 return found;
158 }
159
160 /**
161 * logging dummy
162 */
163 static bool dbus_log(void *param, signal_t signal, level_t level,
164 ike_sa_t *ike_sa, char *format, va_list args)
165 {
166 return TRUE;
167 }
168
169
170 /**
171 * process NetworkManagers startConnection method call
172 */
173 static bool start_connection(private_dbus_interface_t *this, DBusMessage* msg)
174 {
175 DBusMessage *reply, *signal;
176 char *name, *user, **data, **passwords, **routes;
177 int data_count, passwords_count, routes_count;
178 u_int32_t me, other, p2p, netmask, mss;
179 char *dev, *domain, *banner;
180 const dbus_int32_t array[] = {};
181 const dbus_int32_t *varray = array;
182 peer_cfg_t *peer_cfg;
183 child_cfg_t *child_cfg;
184 status_t status = FAILED;
185
186 dbus_error_free(&this->err);
187
188 if (!dbus_message_get_args(msg, &this->err,
189 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &user,
190 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &passwords, &passwords_count,
191 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &data, &data_count,
192 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &routes, &routes_count,
193 DBUS_TYPE_INVALID))
194 {
195 return FALSE;
196 }
197 set_state(this, NM_VPN_STATE_STARTING);
198
199 peer_cfg = get_peer_cfg_by_name(name);
200 if (peer_cfg)
201 {
202 free(this->name);
203 this->name = strdup(peer_cfg->get_name(peer_cfg));
204 child_cfg = get_child_from_peer(peer_cfg, name);
205 if (child_cfg)
206 {
207 status = charon->interfaces->initiate(charon->interfaces, peer_cfg,
208 child_cfg, dbus_log, NULL);
209 }
210 else
211 {
212 peer_cfg->destroy(peer_cfg);
213 }
214 }
215 reply = dbus_message_new_method_return(msg);
216 dbus_connection_send(this->conn, reply, NULL);
217 dbus_message_unref(reply);
218
219 if (status == SUCCESS)
220 {
221
222 set_state(this, NM_VPN_STATE_STARTED);
223 signal = dbus_message_new_signal(NM_DBUS_PATH_STRONG,
224 NM_DBUS_INTERFACE_STRONG,
225 NM_DBUS_VPN_SIGNAL_IP4_CONFIG);
226 me = other = p2p = mss = netmask = 0;
227 dev = domain = banner = "";
228 if (dbus_message_append_args(signal,
229 DBUS_TYPE_UINT32, &other,
230 DBUS_TYPE_STRING, &dev,
231 DBUS_TYPE_UINT32, &me,
232 DBUS_TYPE_UINT32, &p2p,
233 DBUS_TYPE_UINT32, &netmask,
234 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
235 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
236 DBUS_TYPE_UINT32, &mss,
237 DBUS_TYPE_STRING, &domain,
238 DBUS_TYPE_STRING, &banner, DBUS_TYPE_INVALID))
239 {
240 dbus_connection_send(this->conn, signal, NULL);
241 }
242 dbus_message_unref(signal);
243 }
244 else
245 {
246 set_state(this, NM_VPN_STATE_STOPPED);
247 }
248
249 dbus_connection_flush(this->conn);
250 return TRUE;
251 }
252
253 /**
254 * process NetworkManagers stopConnection method call
255 */
256 static bool stop_connection(private_dbus_interface_t *this, DBusMessage* msg)
257 {
258 u_int32_t id;
259 iterator_t *iterator;
260 ike_sa_t *ike_sa;
261
262 if (this->name == NULL)
263 {
264 return FALSE;
265 }
266
267 dbus_error_free(&this->err);
268
269 set_state(this, NM_VPN_STATE_STOPPING);
270
271 iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
272 while (iterator->iterate(iterator, (void**)&ike_sa))
273 {
274 child_sa_t *child_sa;
275 iterator_t *children;
276
277 if (this->name && streq(this->name, ike_sa->get_name(ike_sa)))
278 {
279 id = ike_sa->get_unique_id(ike_sa);
280 iterator->destroy(iterator);
281 charon->interfaces->terminate_ike(charon->interfaces, id, NULL, NULL);
282 set_state(this, NM_VPN_STATE_STOPPED);
283 return TRUE;;
284 }
285 children = ike_sa->create_child_sa_iterator(ike_sa);
286 while (children->iterate(children, (void**)&child_sa))
287 {
288 if (this->name && streq(this->name, child_sa->get_name(child_sa)))
289 {
290 id = child_sa->get_reqid(child_sa);
291 children->destroy(children);
292 iterator->destroy(iterator);
293 charon->interfaces->terminate_child(charon->interfaces, id, NULL, NULL);
294 set_state(this, NM_VPN_STATE_STOPPED);
295 return TRUE;
296 }
297 }
298 children->destroy(children);
299 }
300 iterator->destroy(iterator);
301 set_state(this, NM_VPN_STATE_STOPPED);
302 return TRUE;
303 }
304
305 /**
306 * process NetworkManagers getState method call
307 */
308 static bool get_state(private_dbus_interface_t *this, DBusMessage* msg)
309 {
310 DBusMessage* reply;
311 reply = dbus_message_new_method_return(msg);
312 if (!reply || !dbus_message_append_args(reply,
313 DBUS_TYPE_UINT32, &this->state,
314 DBUS_TYPE_INVALID))
315 {
316 return FALSE;
317 }
318 dbus_connection_send(this->conn, reply, NULL);
319 return TRUE;
320 }
321
322 /**
323 * Handle incoming messages
324 */
325 static DBusHandlerResult message_handler(DBusConnection *con, DBusMessage *msg,
326 private_dbus_interface_t *this)
327 {
328 bool handled;
329
330 if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
331 "startConnection"))
332 {
333 handled = start_connection(this, msg);
334 }
335 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
336 "stopConnection"))
337 {
338 handled = stop_connection(this, msg);
339 }
340 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
341 "getState"))
342 {
343 handled = get_state(this, msg);
344 }
345 else
346 {
347 DBG1(DBG_CFG, "ignoring DBUS message %s.%s",
348 dbus_message_get_interface(msg), dbus_message_get_member(msg));
349 handled = FALSE;
350 }
351
352 if (handled)
353 {
354 return DBUS_HANDLER_RESULT_HANDLED;
355 }
356 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
357 }
358
359 /**
360 * Handle received signals
361
362 static DBusHandlerResult signal_handler(DBusConnection *con, DBusMessage *msg,
363 private_dbus_interface_t *this)
364 {
365 bool handled;
366
367 if (dbus_message_is_signal(msg, NM_DBUS_INTERFACE, "VPNConnectionStateChange"))
368 {
369 NMVPNState state;
370 char *name;
371
372 if (dbus_message_get_args(msg, &this->err, DBUS_TYPE_STRING, &name,
373 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID))
374 {
375 DBG1(DBG_CFG, "got state %d for %s", state, name);
376 }
377 handled = TRUE;
378 }
379 else
380 {
381 DBG1(DBG_CFG, "ignoring DBUS signal %s.%s",
382 dbus_message_get_interface(msg), dbus_message_get_member(msg));
383 handled = FALSE;
384 }
385 if (handled)
386 {
387 return DBUS_HANDLER_RESULT_HANDLED;
388 }
389 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
390 } */
391
392 /**
393 * dispatcher function processed by a seperate thread
394 */
395 static void dispatch(private_dbus_interface_t *this)
396 {
397 charon->drop_capabilities(charon, TRUE);
398
399 while (dbus_connection_read_write_dispatch(this->conn, -1))
400 {
401 /* nothing */
402 }
403 }
404
405 /**
406 * Implementation of interface_t.destroy.
407 */
408 static void destroy(private_dbus_interface_t *this)
409 {
410 pthread_cancel(this->thread);
411 pthread_join(this->thread, NULL);
412 dbus_connection_close(this->conn);
413 dbus_error_free(&this->err);
414 dbus_shutdown();
415 free(this->name);
416 free(this);
417 }
418
419 /*
420 * Described in header file
421 */
422 interface_t *interface_create()
423 {
424 int ret;
425 DBusObjectPathVTable v = {NULL, (void*)&message_handler, NULL, NULL, NULL, NULL};
426 private_dbus_interface_t *this = malloc_thing(private_dbus_interface_t);
427
428 this->public.interface.destroy = (void (*)(interface_t*))destroy;
429
430 dbus_error_init(&this->err);
431 this->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &this->err);
432 if (dbus_error_is_set(&this->err))
433 {
434 DBG1(DBG_CFG, "unable to open DBUS connection: %s", this->err.message);
435 charon->kill(charon, "DBUS initialization failed");
436 }
437 dbus_connection_set_exit_on_disconnect(this->conn, FALSE);
438
439 ret = dbus_bus_request_name(this->conn, NM_DBUS_SERVICE_STRONG,
440 DBUS_NAME_FLAG_REPLACE_EXISTING , &this->err);
441 if (dbus_error_is_set(&this->err))
442 {
443 DBG1(DBG_CFG, "unable to set DBUS name: %s", this->err.message);
444 charon->kill(charon, "unable to set DBUS name");
445 }
446 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
447 {
448 charon->kill(charon, "DBUS name already owned");
449 }
450 if (!dbus_connection_register_object_path(this->conn, NM_DBUS_PATH_STRONG, &v, this))
451 {
452 charon->kill(charon, "unable to register DBUS message handler");
453 }
454 /*
455 if (!dbus_connection_add_filter(this->conn, (void*)signal_handler, this, NULL))
456 {
457 charon->kill(charon, "unable to register DBUS signal handler");
458 }
459
460 dbus_bus_add_match(this->conn, "type='signal', "
461 "interface='" NM_DBUS_INTERFACE_VPN "',"
462 "path='" NM_DBUS_PATH_VPN "'", &this->err);
463 if (dbus_error_is_set (&this->err))
464 {
465 charon->kill(charon, "unable to add DBUS signal match");
466 }*/
467
468 this->name = NULL;
469 this->state = NM_VPN_STATE_INIT;
470 set_state(this, NM_VPN_STATE_STOPPED);
471
472 if (pthread_create(&this->thread, NULL, (void*(*)(void*))dispatch, this) != 0)
473 {
474 charon->kill(charon, "unable to create stroke thread");
475 }
476
477 return &this->public.interface;
478 }
479