working dummy for NetworkManager DBUS interface
[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 peer_cfg->destroy(peer_cfg);
210 }
211 child_cfg->destroy(child_cfg);
212 }
213 reply = dbus_message_new_method_return(msg);
214 dbus_connection_send(this->conn, reply, NULL);
215 dbus_message_unref(reply);
216
217 if (status == SUCCESS)
218 {
219
220 set_state(this, NM_VPN_STATE_STARTED);
221 signal = dbus_message_new_signal(NM_DBUS_PATH_STRONG,
222 NM_DBUS_INTERFACE_STRONG,
223 NM_DBUS_VPN_SIGNAL_IP4_CONFIG);
224 me = other = p2p = mss = netmask = 0;
225 dev = domain = banner = "";
226 if (dbus_message_append_args(signal,
227 DBUS_TYPE_UINT32, &other,
228 DBUS_TYPE_STRING, &dev,
229 DBUS_TYPE_UINT32, &me,
230 DBUS_TYPE_UINT32, &p2p,
231 DBUS_TYPE_UINT32, &netmask,
232 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
233 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
234 DBUS_TYPE_UINT32, &mss,
235 DBUS_TYPE_STRING, &domain,
236 DBUS_TYPE_STRING, &banner, DBUS_TYPE_INVALID))
237 {
238 dbus_connection_send(this->conn, signal, NULL);
239 }
240 dbus_message_unref(signal);
241 }
242 else
243 {
244 set_state(this, NM_VPN_STATE_STOPPED);
245 }
246
247 dbus_connection_flush(this->conn);
248 return TRUE;
249 }
250
251 /**
252 * process NetworkManagers stopConnection method call
253 */
254 static bool stop_connection(private_dbus_interface_t *this, DBusMessage* msg)
255 {
256 u_int32_t id;
257 iterator_t *iterator;
258 ike_sa_t *ike_sa;
259
260 if (this->name == NULL)
261 {
262 return FALSE;
263 }
264
265 dbus_error_free(&this->err);
266
267 set_state(this, NM_VPN_STATE_STOPPING);
268
269 iterator = charon->interfaces->create_ike_sa_iterator(charon->interfaces);
270 while (iterator->iterate(iterator, (void**)&ike_sa))
271 {
272 child_sa_t *child_sa;
273 iterator_t *children;
274
275 if (this->name && streq(this->name, ike_sa->get_name(ike_sa)))
276 {
277 id = ike_sa->get_unique_id(ike_sa);
278 iterator->destroy(iterator);
279 charon->interfaces->terminate_ike(charon->interfaces, id, NULL, NULL);
280 set_state(this, NM_VPN_STATE_STOPPED);
281 return TRUE;;
282 }
283 children = ike_sa->create_child_sa_iterator(ike_sa);
284 while (children->iterate(children, (void**)&child_sa))
285 {
286 if (this->name && streq(this->name, child_sa->get_name(child_sa)))
287 {
288 id = child_sa->get_reqid(child_sa);
289 children->destroy(children);
290 iterator->destroy(iterator);
291 charon->interfaces->terminate_child(charon->interfaces, id, NULL, NULL);
292 set_state(this, NM_VPN_STATE_STOPPED);
293 return TRUE;
294 }
295 }
296 children->destroy(children);
297 }
298 iterator->destroy(iterator);
299 set_state(this, NM_VPN_STATE_STOPPED);
300 return TRUE;
301 }
302
303 /**
304 * process NetworkManagers getState method call
305 */
306 static bool get_state(private_dbus_interface_t *this, DBusMessage* msg)
307 {
308 DBusMessage* reply;
309 reply = dbus_message_new_method_return(msg);
310 if (!reply || !dbus_message_append_args(reply,
311 DBUS_TYPE_UINT32, &this->state,
312 DBUS_TYPE_INVALID))
313 {
314 return FALSE;
315 }
316 dbus_connection_send(this->conn, reply, NULL);
317 return TRUE;
318 }
319
320 /**
321 * Handle incoming messages
322 */
323 static DBusHandlerResult message_handler(DBusConnection *con, DBusMessage *msg,
324 private_dbus_interface_t *this)
325 {
326 bool handled;
327
328 if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
329 "startConnection"))
330 {
331 handled = start_connection(this, msg);
332 }
333 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
334 "stopConnection"))
335 {
336 handled = stop_connection(this, msg);
337 }
338 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
339 "getState"))
340 {
341 handled = get_state(this, msg);
342 }
343 else
344 {
345 DBG1(DBG_CFG, "ignoring DBUS message %s.%s",
346 dbus_message_get_interface(msg), dbus_message_get_member(msg));
347 handled = FALSE;
348 }
349
350 if (handled)
351 {
352 return DBUS_HANDLER_RESULT_HANDLED;
353 }
354 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
355 }
356
357 /**
358 * Handle received signals
359
360 static DBusHandlerResult signal_handler(DBusConnection *con, DBusMessage *msg,
361 private_dbus_interface_t *this)
362 {
363 bool handled;
364
365 if (dbus_message_is_signal(msg, NM_DBUS_INTERFACE, "VPNConnectionStateChange"))
366 {
367 NMVPNState state;
368 char *name;
369
370 if (dbus_message_get_args(msg, &this->err, DBUS_TYPE_STRING, &name,
371 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID))
372 {
373 DBG1(DBG_CFG, "got state %d for %s", state, name);
374 }
375 handled = TRUE;
376 }
377 else
378 {
379 DBG1(DBG_CFG, "ignoring DBUS signal %s.%s",
380 dbus_message_get_interface(msg), dbus_message_get_member(msg));
381 handled = FALSE;
382 }
383 if (handled)
384 {
385 return DBUS_HANDLER_RESULT_HANDLED;
386 }
387 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
388 } */
389
390 /**
391 * dispatcher function processed by a seperate thread
392 */
393 static void dispatch(private_dbus_interface_t *this)
394 {
395 charon->drop_capabilities(charon, TRUE);
396
397 while (dbus_connection_read_write_dispatch(this->conn, -1))
398 {
399 /* nothing */
400 }
401 }
402
403 /**
404 * Implementation of interface_t.destroy.
405 */
406 static void destroy(private_dbus_interface_t *this)
407 {
408 pthread_cancel(this->thread);
409 pthread_join(this->thread, NULL);
410 dbus_connection_close(this->conn);
411 dbus_error_free(&this->err);
412 dbus_shutdown();
413 free(this->name);
414 free(this);
415 }
416
417 /*
418 * Described in header file
419 */
420 interface_t *interface_create()
421 {
422 int ret;
423 DBusObjectPathVTable v = {NULL, (void*)&message_handler, NULL, NULL, NULL, NULL};
424 private_dbus_interface_t *this = malloc_thing(private_dbus_interface_t);
425
426 this->public.interface.destroy = (void (*)(interface_t*))destroy;
427
428 dbus_error_init(&this->err);
429 this->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &this->err);
430 if (dbus_error_is_set(&this->err))
431 {
432 DBG1(DBG_CFG, "unable to open DBUS connection: %s", this->err.message);
433 charon->kill(charon, "DBUS initialization failed");
434 }
435 dbus_connection_set_exit_on_disconnect(this->conn, FALSE);
436
437 ret = dbus_bus_request_name(this->conn, NM_DBUS_SERVICE_STRONG,
438 DBUS_NAME_FLAG_REPLACE_EXISTING , &this->err);
439 if (dbus_error_is_set(&this->err))
440 {
441 DBG1(DBG_CFG, "unable to set DBUS name: %s", this->err.message);
442 charon->kill(charon, "unable to set DBUS name");
443 }
444 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
445 {
446 charon->kill(charon, "DBUS name already owned");
447 }
448 if (!dbus_connection_register_object_path(this->conn, NM_DBUS_PATH_STRONG, &v, this))
449 {
450 charon->kill(charon, "unable to register DBUS message handler");
451 }
452 /*
453 if (!dbus_connection_add_filter(this->conn, (void*)signal_handler, this, NULL))
454 {
455 charon->kill(charon, "unable to register DBUS signal handler");
456 }
457
458 dbus_bus_add_match(this->conn, "type='signal', "
459 "interface='" NM_DBUS_INTERFACE_VPN "',"
460 "path='" NM_DBUS_PATH_VPN "'", &this->err);
461 if (dbus_error_is_set (&this->err))
462 {
463 charon->kill(charon, "unable to add DBUS signal match");
464 }*/
465
466 this->name = NULL;
467 this->state = NM_VPN_STATE_INIT;
468 set_state(this, NM_VPN_STATE_STOPPED);
469
470 if (pthread_create(&this->thread, NULL, (void*(*)(void*))dispatch, this) != 0)
471 {
472 charon->kill(charon, "unable to create stroke thread");
473 }
474
475 return &this->public.interface;
476 }
477