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