5d525f2f91185b5bae864bb1c8cc896046cf9fde
[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 /**
73 * set daemon state and send StateChange signal to the bus
74 */
75 static void set_state(private_dbus_interface_t *this, NMVPNState state)
76 {
77 DBusMessage* msg;
78
79 msg = dbus_message_new_signal(NM_DBUS_PATH_STRONG, NM_DBUS_INTERFACE_STRONG, NM_DBUS_VPN_SIGNAL_STATE_CHANGE);
80
81 if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &this->state,
82 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID) ||
83 !dbus_connection_send(this->conn, msg, NULL))
84 {
85 DBG1(DBG_CFG, "unable to send DBUS StateChange signal");
86 }
87 dbus_connection_flush(this->conn);
88 dbus_message_unref(msg);
89 this->state = state;
90 }
91
92
93 /**
94 * get the child_cfg with the same name as the peer cfg
95 */
96 static child_cfg_t* get_child_from_peer(peer_cfg_t *peer_cfg, char *name)
97 {
98 child_cfg_t *current, *found = NULL;
99 iterator_t *iterator;
100
101 iterator = peer_cfg->create_child_cfg_iterator(peer_cfg);
102 while (iterator->iterate(iterator, (void**)&current))
103 {
104 if (streq(current->get_name(current), name))
105 {
106 found = current;
107 found->get_ref(found);
108 break;
109 }
110 }
111 iterator->destroy(iterator);
112 return found;
113 }
114
115 /**
116 * get a peer configuration by its name, or a name of its children
117 */
118 static peer_cfg_t *get_peer_cfg_by_name(char *name)
119 {
120 iterator_t *i1, *i2;
121 peer_cfg_t *current, *found = NULL;
122 child_cfg_t *child;
123
124 i1 = charon->backends->create_iterator(charon->backends);
125 while (i1->iterate(i1, (void**)&current))
126 {
127 /* compare peer_cfgs name first */
128 if (streq(current->get_name(current), name))
129 {
130 found = current;
131 found->get_ref(found);
132 break;
133 }
134 /* compare all child_cfg names otherwise */
135 i2 = current->create_child_cfg_iterator(current);
136 while (i2->iterate(i2, (void**)&child))
137 {
138 if (streq(child->get_name(child), name))
139 {
140 found = current;
141 found->get_ref(found);
142 break;
143 }
144 }
145 i2->destroy(i2);
146 if (found)
147 {
148 break;
149 }
150 }
151 i1->destroy(i1);
152 return found;
153 }
154
155 /**
156 * logging dummy
157 */
158 static bool dbus_log(void *param, signal_t signal, level_t level,
159 ike_sa_t *ike_sa, char *format, va_list args)
160 {
161 return TRUE;
162 }
163
164
165 /**
166 * process NetworkManagers startConnection method call
167 */
168 static bool start_connection(private_dbus_interface_t *this, DBusMessage* msg)
169 {
170 DBusMessage *reply, *signal;
171 char *name, *user, **data, **passwords, **routes;
172 int data_count, passwords_count, routes_count;
173 u_int32_t me, other, p2p, netmask, mss;
174 char *dev, *domain, *banner;
175 const dbus_int32_t array[] = {};
176 const dbus_int32_t *varray = array;
177 peer_cfg_t *peer_cfg;
178 child_cfg_t *child_cfg;
179 status_t status = FAILED;
180
181 if (!dbus_message_get_args(msg, &this->err,
182 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &user,
183 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &passwords, &passwords_count,
184 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &data, &data_count,
185 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &routes, &routes_count,
186 DBUS_TYPE_INVALID))
187 {
188 return FALSE;
189 }
190 set_state(this, NM_VPN_STATE_STARTING);
191
192 peer_cfg = get_peer_cfg_by_name(name);
193 if (peer_cfg)
194 {
195 child_cfg = get_child_from_peer(peer_cfg, name);
196 if (child_cfg)
197 {
198 status = charon->interfaces->initiate(charon->interfaces, peer_cfg,
199 child_cfg, dbus_log, NULL);
200 }
201 }
202
203 if (status == SUCCESS)
204 {
205 signal = dbus_message_new_signal(NM_DBUS_PATH_STRONG,
206 NM_DBUS_INTERFACE_STRONG,
207 NM_DBUS_VPN_SIGNAL_IP4_CONFIG);
208 me = other = p2p = mss = netmask = 0;
209 dev = domain = banner = "";
210 if (dbus_message_append_args(signal,
211 DBUS_TYPE_UINT32, &other,
212 DBUS_TYPE_STRING, &dev,
213 DBUS_TYPE_UINT32, &me,
214 DBUS_TYPE_UINT32, &p2p,
215 DBUS_TYPE_UINT32, &netmask,
216 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
217 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
218 DBUS_TYPE_UINT32, &mss,
219 DBUS_TYPE_STRING, &domain,
220 DBUS_TYPE_STRING, &banner, DBUS_TYPE_INVALID))
221 {
222 dbus_connection_send(this->conn, signal, NULL);
223 }
224 dbus_message_unref(signal);
225 set_state(this, NM_VPN_STATE_STARTED);
226 }
227 else
228 {
229 set_state(this, NM_VPN_STATE_STOPPED);
230 }
231
232 reply = dbus_message_new_method_return(msg);
233 dbus_connection_send(this->conn, reply, NULL);
234 dbus_connection_flush(this->conn);
235 dbus_message_unref(reply);
236 return TRUE;
237 }
238
239 /**
240 * process NetworkManagers stopConnection method call
241 */
242 static bool stop_connection(private_dbus_interface_t *this, DBusMessage* msg)
243 {
244 set_state(this, NM_VPN_STATE_STOPPING);
245 set_state(this, NM_VPN_STATE_STOPPED);
246 return FALSE;
247 }
248
249 /**
250 * process NetworkManagers getState method call
251 */
252 static bool get_state(private_dbus_interface_t *this, DBusMessage* msg)
253 {
254 DBusMessage* reply;
255 reply = dbus_message_new_method_return(msg);
256 if (!reply || !dbus_message_append_args(reply,
257 DBUS_TYPE_UINT32, &this->state,
258 DBUS_TYPE_INVALID))
259 {
260 return FALSE;
261 }
262 dbus_connection_send(this->conn, reply, NULL);
263 return TRUE;
264 }
265
266 /**
267 * Handle incoming messages
268 */
269 static DBusHandlerResult message_handler(DBusConnection *con, DBusMessage *msg,
270 private_dbus_interface_t *this)
271 {
272 bool handled;
273
274 if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
275 "startConnection"))
276 {
277 handled = start_connection(this, msg);
278 }
279 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
280 "stopConnection"))
281 {
282 handled = stop_connection(this, msg);
283 }
284 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
285 "getState"))
286 {
287 handled = get_state(this, msg);
288 }
289 else
290 {
291 DBG1(DBG_CFG, "ignoring DBUS message %s.%s",
292 dbus_message_get_interface(msg), dbus_message_get_member(msg));
293 handled = FALSE;
294 }
295
296 if (handled)
297 {
298 return DBUS_HANDLER_RESULT_HANDLED;
299 }
300 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
301 }
302
303 /**
304 * Handle received signals
305
306 static DBusHandlerResult signal_handler(DBusConnection *con, DBusMessage *msg,
307 private_dbus_interface_t *this)
308 {
309 bool handled;
310
311 if (dbus_message_is_signal(msg, NM_DBUS_INTERFACE, "VPNConnectionStateChange"))
312 {
313 NMVPNState state;
314 char *name;
315
316 if (dbus_message_get_args(msg, &this->err, DBUS_TYPE_STRING, &name,
317 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID))
318 {
319 DBG1(DBG_CFG, "got state %d for %s", state, name);
320 }
321 handled = TRUE;
322 }
323 else
324 {
325 DBG1(DBG_CFG, "ignoring DBUS signal %s.%s",
326 dbus_message_get_interface(msg), dbus_message_get_member(msg));
327 handled = FALSE;
328 }
329 if (handled)
330 {
331 return DBUS_HANDLER_RESULT_HANDLED;
332 }
333 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
334 } */
335
336 /**
337 * dispatcher function processed by a seperate thread
338 */
339 static void dispatch(private_dbus_interface_t *this)
340 {
341 charon->drop_capabilities(charon, TRUE);
342
343 while (dbus_connection_read_write_dispatch(this->conn, -1))
344 {
345 /* nothing */
346 }
347 }
348
349 /**
350 * Implementation of interface_t.destroy.
351 */
352 static void destroy(private_dbus_interface_t *this)
353 {
354 pthread_cancel(this->thread);
355 pthread_join(this->thread, NULL);
356 dbus_error_free(&this->err);
357 free(this);
358 }
359
360 /*
361 * Described in header file
362 */
363 interface_t *interface_create()
364 {
365 int ret;
366 DBusObjectPathVTable v = {NULL, (void*)&message_handler, NULL, NULL, NULL, NULL};
367 private_dbus_interface_t *this = malloc_thing(private_dbus_interface_t);
368
369 this->public.interface.destroy = (void (*)(interface_t*))destroy;
370
371 dbus_error_init(&this->err);
372 this->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &this->err);
373 if (dbus_error_is_set(&this->err))
374 {
375 DBG1(DBG_CFG, "unable to open DBUS connection: %s", this->err.message);
376 charon->kill(charon, "DBUS initialization failed");
377 }
378
379 ret = dbus_bus_request_name(this->conn, NM_DBUS_SERVICE_STRONG,
380 DBUS_NAME_FLAG_REPLACE_EXISTING , &this->err);
381 if (dbus_error_is_set(&this->err))
382 {
383 DBG1(DBG_CFG, "unable to set DBUS name: %s", this->err.message);
384 charon->kill(charon, "unable to set DBUS name");
385 }
386 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
387 {
388 charon->kill(charon, "DBUS name already owned");
389 }
390 if (!dbus_connection_register_object_path(this->conn, NM_DBUS_PATH_STRONG, &v, this))
391 {
392 charon->kill(charon, "unable to register DBUS message handler");
393 }
394 /*
395 if (!dbus_connection_add_filter(this->conn, (void*)signal_handler, this, NULL))
396 {
397 charon->kill(charon, "unable to register DBUS signal handler");
398 }
399
400 dbus_bus_add_match(this->conn, "type='signal', "
401 "interface='" NM_DBUS_INTERFACE_VPN "',"
402 "path='" NM_DBUS_PATH_VPN "'", &this->err);
403 if (dbus_error_is_set (&this->err))
404 {
405 charon->kill(charon, "unable to add DBUS signal match");
406 }*/
407
408 this->state = NM_VPN_STATE_INIT;
409 set_state(this, NM_VPN_STATE_STOPPED);
410
411 if (pthread_create(&this->thread, NULL, (void*(*)(void*))dispatch, this) != 0)
412 {
413 charon->kill(charon, "unable to create stroke thread");
414 }
415
416 return &this->public.interface;
417 }