97bd3b32f7b53029de732ac1261de393bb7d60b2
[strongswan.git] / src / charon / plugins / dbus / dbus.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #define DBUS_API_SUBJECT_TO_CHANGE
19 #include <dbus/dbus.h>
20 #include <NetworkManager/NetworkManager.h>
21 #include <NetworkManager/NetworkManagerVPN.h>
22 #include <stdlib.h>
23
24 #include "dbus.h"
25
26 #include <library.h>
27 #include <daemon.h>
28 #include <processing/jobs/callback_job.h>
29
30
31 #define NM_DBUS_SERVICE_STRONG "org.freedesktop.NetworkManager.strongswan"
32 #define NM_dbus_STRONG "org.freedesktop.NetworkManager.strongswan"
33 #define NM_DBUS_PATH_STRONG "/org/freedesktop/NetworkManager/strongswan"
34
35 typedef struct private_dbus_t private_dbus_t;
36
37 /**
38 * Private data of an dbus_t object.
39 */
40 struct private_dbus_t {
41
42 /**
43 * Public part of dbus_t object.
44 */
45 dbus_t public;
46
47 /**
48 * DBUS connection
49 */
50 DBusConnection* conn;
51
52 /**
53 * error value used here and there
54 */
55 DBusError err;
56
57 /**
58 * state of the daemon
59 */
60 NMVPNState state;
61
62 /**
63 * job accepting stroke messages
64 */
65 callback_job_t *job;
66
67 /**
68 * name of the currently active connection
69 */
70 char *name;
71 };
72
73 /**
74 * set daemon state and send StateChange signal to the bus
75 */
76 static void set_state(private_dbus_t *this, NMVPNState state)
77 {
78 DBusMessage* msg;
79
80 msg = dbus_message_new_signal(NM_DBUS_PATH_STRONG, NM_dbus_STRONG, NM_DBUS_VPN_SIGNAL_STATE_CHANGE);
81
82 if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &this->state,
83 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID) ||
84 !dbus_connection_send(this->conn, msg, NULL))
85 {
86 DBG1(DBG_CFG, "unable to send DBUS StateChange signal");
87 }
88 dbus_connection_flush(this->conn);
89 dbus_message_unref(msg);
90 this->state = state;
91 }
92
93
94 /**
95 * get the child_cfg with the same name as the peer cfg
96 */
97 static child_cfg_t* get_child_from_peer(peer_cfg_t *peer_cfg, char *name)
98 {
99 child_cfg_t *current, *found = NULL;
100 iterator_t *iterator;
101
102 iterator = peer_cfg->create_child_cfg_iterator(peer_cfg);
103 while (iterator->iterate(iterator, (void**)&current))
104 {
105 if (streq(current->get_name(current), name))
106 {
107 found = current;
108 found->get_ref(found);
109 break;
110 }
111 }
112 iterator->destroy(iterator);
113 return found;
114 }
115
116
117 /**
118 * process NetworkManagers startConnection method call
119 */
120 static bool start_connection(private_dbus_t *this, DBusMessage* msg)
121 {
122 DBusMessage *reply, *signal;
123 char *name, *user, **data, **passwords, **routes;
124 int data_count, passwords_count, routes_count;
125 u_int32_t me, other, p2p, netmask, mss;
126 char *dev, *domain, *banner;
127 const dbus_int32_t array[] = {};
128 const dbus_int32_t *varray = array;
129 peer_cfg_t *peer_cfg;
130 child_cfg_t *child_cfg;
131 status_t status = FAILED;
132
133 dbus_error_free(&this->err);
134
135 if (!dbus_message_get_args(msg, &this->err,
136 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &user,
137 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &passwords, &passwords_count,
138 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &data, &data_count,
139 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &routes, &routes_count,
140 DBUS_TYPE_INVALID))
141 {
142 return FALSE;
143 }
144 set_state(this, NM_VPN_STATE_STARTING);
145
146 peer_cfg = charon->backends->get_peer_cfg_by_name(charon->backends, name);
147 if (peer_cfg)
148 {
149 free(this->name);
150 this->name = strdup(peer_cfg->get_name(peer_cfg));
151 child_cfg = get_child_from_peer(peer_cfg, name);
152 if (child_cfg)
153 {
154 status = charon->controller->initiate(charon->controller,
155 peer_cfg, child_cfg, controller_cb_empty, NULL);
156 }
157 else
158 {
159 peer_cfg->destroy(peer_cfg);
160 }
161 }
162 reply = dbus_message_new_method_return(msg);
163 dbus_connection_send(this->conn, reply, NULL);
164 dbus_message_unref(reply);
165
166 if (status == SUCCESS)
167 {
168
169 set_state(this, NM_VPN_STATE_STARTED);
170 signal = dbus_message_new_signal(NM_DBUS_PATH_STRONG,
171 NM_dbus_STRONG,
172 NM_DBUS_VPN_SIGNAL_IP4_CONFIG);
173 me = other = p2p = mss = netmask = 0;
174 dev = domain = banner = "";
175 if (dbus_message_append_args(signal,
176 DBUS_TYPE_UINT32, &other,
177 DBUS_TYPE_STRING, &dev,
178 DBUS_TYPE_UINT32, &me,
179 DBUS_TYPE_UINT32, &p2p,
180 DBUS_TYPE_UINT32, &netmask,
181 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
182 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
183 DBUS_TYPE_UINT32, &mss,
184 DBUS_TYPE_STRING, &domain,
185 DBUS_TYPE_STRING, &banner, DBUS_TYPE_INVALID))
186 {
187 dbus_connection_send(this->conn, signal, NULL);
188 }
189 dbus_message_unref(signal);
190 }
191 else
192 {
193 set_state(this, NM_VPN_STATE_STOPPED);
194 }
195
196 dbus_connection_flush(this->conn);
197 return TRUE;
198 }
199
200 /**
201 * process NetworkManagers stopConnection method call
202 */
203 static bool stop_connection(private_dbus_t *this, DBusMessage* msg)
204 {
205 u_int32_t id;
206 enumerator_t *enumerator;
207 ike_sa_t *ike_sa;
208
209 if (this->name == NULL)
210 {
211 return FALSE;
212 }
213
214 dbus_error_free(&this->err);
215
216 set_state(this, NM_VPN_STATE_STOPPING);
217
218 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
219 while (enumerator->enumerate(enumerator, (void**)&ike_sa))
220 {
221 child_sa_t *child_sa;
222 iterator_t *children;
223
224 if (this->name && streq(this->name, ike_sa->get_name(ike_sa)))
225 {
226 id = ike_sa->get_unique_id(ike_sa);
227 enumerator->destroy(enumerator);
228 charon->controller->terminate_ike(charon->controller, id, NULL, NULL);
229 set_state(this, NM_VPN_STATE_STOPPED);
230 return TRUE;;
231 }
232 children = ike_sa->create_child_sa_iterator(ike_sa);
233 while (children->iterate(children, (void**)&child_sa))
234 {
235 if (this->name && streq(this->name, child_sa->get_name(child_sa)))
236 {
237 id = child_sa->get_reqid(child_sa);
238 children->destroy(children);
239 enumerator->destroy(enumerator);
240 charon->controller->terminate_child(charon->controller, id, NULL, NULL);
241 set_state(this, NM_VPN_STATE_STOPPED);
242 return TRUE;
243 }
244 }
245 children->destroy(children);
246 }
247 enumerator->destroy(enumerator);
248 set_state(this, NM_VPN_STATE_STOPPED);
249 return TRUE;
250 }
251
252 /**
253 * process NetworkManagers getState method call
254 */
255 static bool get_state(private_dbus_t *this, DBusMessage* msg)
256 {
257 DBusMessage* reply;
258 reply = dbus_message_new_method_return(msg);
259 if (!reply || !dbus_message_append_args(reply,
260 DBUS_TYPE_UINT32, &this->state,
261 DBUS_TYPE_INVALID))
262 {
263 return FALSE;
264 }
265 dbus_connection_send(this->conn, reply, NULL);
266 return TRUE;
267 }
268
269 /**
270 * Handle incoming messages
271 */
272 static DBusHandlerResult message_handler(DBusConnection *con, DBusMessage *msg,
273 private_dbus_t *this)
274 {
275 bool handled;
276
277 if (dbus_message_is_method_call(msg, NM_dbus_STRONG,
278 "startConnection"))
279 {
280 handled = start_connection(this, msg);
281 }
282 else if (dbus_message_is_method_call(msg, NM_dbus_STRONG,
283 "stopConnection"))
284 {
285 handled = stop_connection(this, msg);
286 }
287 else if (dbus_message_is_method_call(msg, NM_dbus_STRONG,
288 "getState"))
289 {
290 handled = get_state(this, msg);
291 }
292 else
293 {
294 DBG1(DBG_CFG, "ignoring DBUS message %s.%s",
295 dbus_message_get_interface(msg), dbus_message_get_member(msg));
296 handled = FALSE;
297 }
298
299 if (handled)
300 {
301 return DBUS_HANDLER_RESULT_HANDLED;
302 }
303 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
304 }
305
306 /**
307 * Handle received signals
308
309 static DBusHandlerResult signal_handler(DBusConnection *con, DBusMessage *msg,
310 private_dbus_t *this)
311 {
312 bool handled;
313
314 if (dbus_message_is_signal(msg, NM_dbus, "VPNConnectionStateChange"))
315 {
316 NMVPNState state;
317 char *name;
318
319 if (dbus_message_get_args(msg, &this->err, DBUS_TYPE_STRING, &name,
320 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID))
321 {
322 DBG1(DBG_CFG, "got state %d for %s", state, name);
323 }
324 handled = TRUE;
325 }
326 else
327 {
328 DBG1(DBG_CFG, "ignoring DBUS signal %s.%s",
329 dbus_message_get_interface(msg), dbus_message_get_member(msg));
330 handled = FALSE;
331 }
332 if (handled)
333 {
334 return DBUS_HANDLER_RESULT_HANDLED;
335 }
336 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
337 } */
338
339 /**
340 * dispatcher function processed by a seperate thread
341 */
342 static job_requeue_t dispatch(private_dbus_t *this)
343 {
344 if (dbus_connection_read_write_dispatch(this->conn, -1))
345 {
346 return JOB_REQUEUE_DIRECT;
347 }
348 return JOB_REQUEUE_NONE;
349 }
350
351 /**
352 * Implementation of interface_t.destroy.
353 */
354 static void destroy(private_dbus_t *this)
355 {
356 this->job->cancel(this->job);
357 dbus_connection_close(this->conn);
358 dbus_error_free(&this->err);
359 dbus_shutdown();
360 free(this->name);
361 free(this);
362 }
363
364 /*
365 * Described in header file
366 */
367 plugin_t *plugin_create()
368 {
369 int ret;
370 DBusObjectPathVTable v = {NULL, (void*)&message_handler, NULL, NULL, NULL, NULL};
371 private_dbus_t *this = malloc_thing(private_dbus_t);
372
373 this->public.plugin.destroy = (void (*)(plugin_t*))destroy;
374
375 dbus_error_init(&this->err);
376 this->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &this->err);
377 if (dbus_error_is_set(&this->err))
378 {
379 DBG1(DBG_CFG, "unable to open DBUS connection: %s", this->err.message);
380 charon->kill(charon, "DBUS initialization failed");
381 }
382 dbus_connection_set_exit_on_disconnect(this->conn, FALSE);
383
384 ret = dbus_bus_request_name(this->conn, NM_DBUS_SERVICE_STRONG,
385 DBUS_NAME_FLAG_REPLACE_EXISTING , &this->err);
386 if (dbus_error_is_set(&this->err))
387 {
388 DBG1(DBG_CFG, "unable to set DBUS name: %s", this->err.message);
389 charon->kill(charon, "unable to set DBUS name");
390 }
391 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
392 {
393 charon->kill(charon, "DBUS name already owned");
394 }
395 if (!dbus_connection_register_object_path(this->conn, NM_DBUS_PATH_STRONG, &v, this))
396 {
397 charon->kill(charon, "unable to register DBUS message handler");
398 }
399 /*
400 if (!dbus_connection_add_filter(this->conn, (void*)signal_handler, this, NULL))
401 {
402 charon->kill(charon, "unable to register DBUS signal handler");
403 }
404
405 dbus_bus_add_match(this->conn, "type='signal', "
406 "interface='" NM_dbus_VPN "',"
407 "path='" NM_DBUS_PATH_VPN "'", &this->err);
408 if (dbus_error_is_set (&this->err))
409 {
410 charon->kill(charon, "unable to add DBUS signal match");
411 }*/
412
413 this->name = NULL;
414 this->state = NM_VPN_STATE_INIT;
415 set_state(this, NM_VPN_STATE_STOPPED);
416
417 this->job = callback_job_create((callback_job_cb_t)dispatch, this, NULL, NULL);
418 charon->processor->queue_job(charon->processor, (job_t*)this->job);
419
420 return &this->public.plugin;
421 }
422