nm-dbus-vpn.c 21.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* NetworkManager -- Network link manager
 *
 * Dan Williams <dcbw@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * (C) Copyright 2005 Red Hat, Inc.
 */

#include <glib.h>
#include <dbus/dbus.h>
#include "NetworkManagerMain.h"
#include "NetworkManagerDevice.h"
#include "NetworkManagerDbus.h"
#include "NetworkManagerUtils.h"
#include "nm-dbus-vpn.h"
#include "nm-vpn-manager.h"
#include "nm-vpn-connection.h"
#include "nm-utils.h"


/*
 * nm_dbus_vpn_signal_vpn_connection_update
 *
37
 * Notifies the bus that a VPN connection's properties have changed.
38
39
 *
 */
40
void nm_dbus_vpn_signal_vpn_connection_update (DBusConnection *con, NMVPNConnection *vpn, const char *signal)
41
42
43
44
45
46
47
{
	DBusMessage	*message;
	const char	*vpn_name;

	g_return_if_fail (con != NULL);
	g_return_if_fail (vpn != NULL);

48
	if (!(message = dbus_message_new_signal (NM_DBUS_PATH_VPN, NM_DBUS_INTERFACE_VPN, signal)))
49
50
51
52
53
54
55
56
	{
		nm_warning ("Not enough memory for new dbus message!");
		return;
	}

	vpn_name = nm_vpn_connection_get_name (vpn);
	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn_name, DBUS_TYPE_INVALID);
	if (!dbus_connection_send (con, message, NULL))
57
		nm_warning ("Could not raise the %s signal!", signal);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

	dbus_message_unref (message);
}


/*
 * nm_dbus_vpn_signal_vpn_connection_change
 *
 * Notifies the bus that the current VPN connection, if any, has changed.
 *
 */
void nm_dbus_vpn_signal_vpn_connection_change (DBusConnection *con, NMVPNConnection *vpn)
{
	DBusMessage	*message;
	const char	*vpn_name;

	g_return_if_fail (con != NULL);

	if (!(message = dbus_message_new_signal (NM_DBUS_PATH_VPN, NM_DBUS_INTERFACE_VPN, "VPNConnectionChange")))
	{
		nm_warning ("Not enough memory for new dbus message!");
		return;
	}

	if (vpn)
		vpn_name = nm_vpn_connection_get_name (vpn);
	else
		vpn_name = "";
	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn_name, DBUS_TYPE_INVALID);
	if (!dbus_connection_send (con, message, NULL))
		nm_warning ("Could not raise the VPNConnectionChange signal!");

	dbus_message_unref (message);
}


/*
95
 * nnm_dbus_vpn_signal_vpn_failure
96
 *
97
 * Proxy a VPN Failure message from the vpn daemon to the bus.
98
99
 *
 */
100
void nm_dbus_vpn_signal_vpn_failed (DBusConnection *con, const char *signal, NMVPNConnection *vpn, const char *error_msg)
101
102
103
104
105
{
	DBusMessage	*message;
	const char	*vpn_name;

	g_return_if_fail (con != NULL);
106
	g_return_if_fail (signal != NULL);
107
108
109
	g_return_if_fail (vpn != NULL);
	g_return_if_fail (error_msg != NULL);

110
	if (!(message = dbus_message_new_signal (NM_DBUS_PATH_VPN, NM_DBUS_INTERFACE_VPN, signal)))
111
112
113
114
115
116
117
118
	{
		nm_warning ("Not enough memory for new dbus message!");
		return;
	}

	vpn_name = nm_vpn_connection_get_name (vpn);
	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn_name, DBUS_TYPE_STRING, &error_msg, DBUS_TYPE_INVALID);
	if (!dbus_connection_send (con, message, NULL))
119
		nm_warning ("Could not raise the %s signal!", signal);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

	dbus_message_unref (message);
}


/*
 * nnm_dbus_vpn_signal_vpn_login_banner
 *
 * Pass the VPN's login banner message to the bus if anyone wants to use it.
 *
 */
void nm_dbus_vpn_signal_vpn_login_banner (DBusConnection *con, NMVPNConnection *vpn, const char *banner)
{
	DBusMessage	*message;
	const char	*vpn_name;

	g_return_if_fail (con != NULL);
	g_return_if_fail (vpn != NULL);
	g_return_if_fail (banner != NULL);

140
	if (!(message = dbus_message_new_signal (NM_DBUS_PATH_VPN, NM_DBUS_INTERFACE_VPN, NM_DBUS_VPN_SIGNAL_LOGIN_BANNER)))
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	{
		nm_warning ("Not enough memory for new dbus message!");
		return;
	}

	vpn_name = nm_vpn_connection_get_name (vpn);
	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn_name, DBUS_TYPE_STRING, &banner, DBUS_TYPE_INVALID);
	if (!dbus_connection_send (con, message, NULL))
		nm_warning ("Could not raise the VPNLoginBanner signal!");

	dbus_message_unref (message);
}


/*
 * nm_dbus_vpn_get_vpn_data
 *
 * Get VPN specific data from NMI for a vpn connection
 *
 * NOTE: caller MUST free returned value using g_strfreev()
 *
 */
static char ** nm_dbus_vpn_get_vpn_data (DBusConnection *connection, NMVPNConnection *vpn, int *num_items)
{
	DBusMessage		*message;
	DBusError			 error;
	DBusMessage		*reply;
	char			    **data_items = NULL;
	const char		*vpn_name;

	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (vpn != NULL, NULL);
	g_return_val_if_fail (num_items != NULL, NULL);

	*num_items = -1;

	if (!(message = dbus_message_new_method_call (NMI_DBUS_SERVICE, NMI_DBUS_PATH, NMI_DBUS_INTERFACE, "getVPNConnectionVPNData")))
	{
		nm_warning ("nm_dbus_vpn_get_vpn_data(): Couldn't allocate the dbus message");
		return (NULL);
	}

	vpn_name = nm_vpn_connection_get_name (vpn);
	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn_name, DBUS_TYPE_INVALID);

	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
	dbus_message_unref (message);
	if (dbus_error_is_set (&error))
		nm_warning ("nm_dbus_vpn_get_vpn_data(): %s raised %s", error.name, error.message);
	else if (!reply)
		nm_info ("nm_dbus_vpn_get_vpn_data(): reply was NULL.");
	else
	{
		DBusMessageIter iter, array_iter;
		GArray *buffer;

		dbus_message_iter_init (reply, &iter);
		dbus_message_iter_recurse (&iter, &array_iter);

		buffer = g_array_new (TRUE, TRUE, sizeof (gchar *));

		if (buffer == NULL)
			return NULL;

		while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_STRING)
		{
			const char *value;
			char *str;
		
			dbus_message_iter_get_basic (&array_iter, &value);
			str = g_strdup (value);
			
			if (str == NULL)
			{
				g_array_free (buffer, TRUE);
				return NULL;
			}

			g_array_append_val (buffer, str);

			dbus_message_iter_next (&array_iter);
		}
		data_items = (gchar **)(buffer->data);
		*num_items = buffer->len;
		g_array_free (buffer, FALSE);
	}
	
	if (reply)
		dbus_message_unref (reply);

	return (data_items);
}


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * nm_dbus_vpn_get_routes
 *
 * Get VPN routes from NMI for a vpn connection
 *
 * NOTE: caller MUST free returned value using g_strfreev()
 *
 */
char ** nm_dbus_vpn_get_routes (DBusConnection *connection, NMVPNConnection *vpn, int *num_items)
{
	DBusMessage		*message;
	DBusError			 error;
	DBusMessage		*reply;
	char			    **routes = NULL;
	const char		*vpn_name;

	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (vpn != NULL, NULL);
	g_return_val_if_fail (num_items != NULL, NULL);

	*num_items = -1;

	if (!(message = dbus_message_new_method_call (NMI_DBUS_SERVICE, NMI_DBUS_PATH, NMI_DBUS_INTERFACE, "getVPNConnectionRoutes")))
	{
		nm_warning ("nm_dbus_vpn_get_routes(): Couldn't allocate the dbus message");
		return (NULL);
	}

	vpn_name = nm_vpn_connection_get_name (vpn);
	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn_name, DBUS_TYPE_INVALID);

	dbus_error_init (&error);
	reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
	dbus_message_unref (message);
	if (dbus_error_is_set (&error))
		nm_warning ("nm_dbus_vpn_get_routes(): %s raised %s", error.name, error.message);
	else if (!reply)
		nm_info ("nm_dbus_vpn_get_routes(): reply was NULL.");
	else
	{
		DBusMessageIter iter, array_iter;
		GArray *buffer;

		dbus_message_iter_init (reply, &iter);
		dbus_message_iter_recurse (&iter, &array_iter);

		buffer = g_array_new (TRUE, TRUE, sizeof (gchar *));

		if (buffer == NULL)
			return NULL;

		while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_STRING)
		{
			const char *value;
			char *str;
		
			dbus_message_iter_get_basic (&array_iter, &value);
			str = g_strdup (value);
			
			if (str == NULL)
			{
				g_array_free (buffer, TRUE);
				return NULL;
			}

			g_array_append_val (buffer, str);

			dbus_message_iter_next (&array_iter);
		}
		routes = (gchar **)(buffer->data);
		*num_items = buffer->len;
		g_array_free (buffer, FALSE);
	}
	
	if (reply)
		dbus_message_unref (reply);

	return (routes);
}


317
318
319
320
321
322
323
typedef struct UpdateOneVPNCBData
{
	NMData *	data;
	char *	vpn;
} UpdateOneVPNCBData;


324
static void free_update_one_vpn_cb_data (UpdateOneVPNCBData *data)
325
326
327
328
329
330
{
	if (data)
		g_free (data->vpn);
	g_free (data);
}

331
/*
332
 * nm_dbus_vpn_update_one_connection_cb
333
334
335
336
 *
 * Retrieve and add to our VPN Manager one VPN connection from NMI.
 *
 */
337
static void nm_dbus_vpn_update_one_connection_cb (DBusPendingCall *pcall, void *user_data)
338
{
339
340
341
342
343
	UpdateOneVPNCBData *	cb_data = (UpdateOneVPNCBData *) user_data;
	DBusMessage *			reply;
	const char *			con_name = NULL;
	const char *			service_name = NULL;
	const char *			user_name = NULL;
344
	
345
346
347
348
	g_return_if_fail (pcall != NULL);
	g_return_if_fail (cb_data != NULL);
	g_return_if_fail (cb_data->data != NULL);
	g_return_if_fail (cb_data->data->vpn_manager != NULL);
349

350
	dbus_pending_call_ref (pcall);
351

352
353
	if (!dbus_pending_call_get_completed (pcall))
		goto out;
354

355
356
357
358
	if (!(reply = dbus_pending_call_steal_reply (pcall)))
		goto out;

	if (dbus_message_is_error (reply, "BadVPNConnectionData"))
359
	{
360
361
362
363
364
365
		NMVPNConnection *vpn = nm_vpn_manager_find_connection_by_name (cb_data->data->vpn_manager, cb_data->vpn);

		nm_vpn_connection_ref (vpn);
		nm_vpn_manager_remove_connection (cb_data->data->vpn_manager, vpn);
		nm_dbus_vpn_signal_vpn_connection_update (cb_data->data->dbus_connection, vpn, "VPNConnectionRemoved");
		nm_vpn_connection_unref (vpn);
366
367
368
		goto out;
	}

369
	if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &con_name, DBUS_TYPE_STRING, &service_name,
370
371
									DBUS_TYPE_STRING, &user_name, DBUS_TYPE_INVALID))
	{
372
373
374
375
376
377
378
379
380
381
382
		NMVPNConnection *	vpn;
		gboolean			new = TRUE;

		if ((vpn = nm_vpn_manager_find_connection_by_name (cb_data->data->vpn_manager, con_name)))
		{
			nm_vpn_manager_remove_connection (cb_data->data->vpn_manager, vpn);
			new = FALSE;
		}

		vpn = nm_vpn_manager_add_connection (cb_data->data->vpn_manager, con_name, service_name, user_name);
		nm_dbus_vpn_signal_vpn_connection_update (cb_data->data->dbus_connection, vpn, new ? "VPNConnectionAdded" : "VPNConnectionUpdate");
383
384
385
386
	}
	dbus_message_unref (reply);

out:
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
	dbus_pending_call_unref (pcall);
}


/*
 * nm_dbus_vpn_connections_update_cb
 *
 * Async callback from nnm_dbus_vpn_connections_update
 *
 */
static void nm_dbus_vpn_connections_update_cb (DBusPendingCall *pcall, void *user_data)
{
	NMData *			data = (NMData *) user_data;
	DBusMessage *		reply;
	DBusMessageIter	iter, array_iter;

	g_return_if_fail (pcall);
	g_return_if_fail (data != NULL);

	dbus_pending_call_ref (pcall);

	if (!dbus_pending_call_get_completed (pcall))
		goto out;

	if (!(reply = dbus_pending_call_steal_reply (pcall)))
		goto out;

	if (message_is_error (reply))
		goto out;

	nm_info ("Updating VPN Connections...");

	dbus_message_iter_init (reply, &iter);
	dbus_message_iter_recurse (&iter, &array_iter);
	while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_STRING)
	{
		DBusMessage *	message;
		const char *	value;

		dbus_message_iter_get_basic (&array_iter, &value);

		if ((message = dbus_message_new_method_call (NMI_DBUS_SERVICE, NMI_DBUS_PATH, NMI_DBUS_INTERFACE, "getVPNConnectionProperties")))
		{
			DBusPendingCall *		vpn_pcall = NULL;

			dbus_message_append_args (message, DBUS_TYPE_STRING, &value, DBUS_TYPE_INVALID);
			dbus_connection_send_with_reply (data->dbus_connection, message, &vpn_pcall, -1);
			dbus_message_unref (message);
			if (vpn_pcall)
			{
				UpdateOneVPNCBData *	vpn_cb_data = g_malloc0 (sizeof (UpdateOneVPNCBData));
438

439
440
441
442
443
444
445
446
447
448
449
				vpn_cb_data->data = data;
				vpn_cb_data->vpn = g_strdup (value);
				dbus_pending_call_set_notify (vpn_pcall, nm_dbus_vpn_update_one_connection_cb, vpn_cb_data, (DBusFreeFunction) free_update_one_vpn_cb_data);
			}
		}
		dbus_message_iter_next (&array_iter);
	}
	dbus_message_unref (reply);

out:
	dbus_pending_call_unref (pcall);
450
451
452
453
}


/*
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
 * nm_dbus_vpn_update_one_vpn_connection
 *
 * Update one VPN connection
 *
 */
void nm_dbus_vpn_update_one_vpn_connection (DBusConnection *connection, const char *vpn, NMData *data)
{
	DBusMessage *			message;
	DBusPendingCall *		pcall = NULL;

	g_return_if_fail (connection != NULL);
	g_return_if_fail (vpn != NULL);
	g_return_if_fail (data != NULL);

	if (!(message = dbus_message_new_method_call (NMI_DBUS_SERVICE, NMI_DBUS_PATH, NMI_DBUS_INTERFACE, "getVPNConnectionProperties")))
	{
		nm_warning ("nm_dbus_update_one_vpn_connection(): Couldn't allocate the dbus message");
		return;
	}

	dbus_message_append_args (message, DBUS_TYPE_STRING, &vpn, DBUS_TYPE_INVALID);
	dbus_connection_send_with_reply (connection, message, &pcall, -1);
	dbus_message_unref (message);
	if (pcall)
	{
		UpdateOneVPNCBData *	cb_data = g_malloc0 (sizeof (UpdateOneVPNCBData));

		cb_data->data = data;
		cb_data->vpn = g_strdup (vpn);
		dbus_pending_call_set_notify (pcall, nm_dbus_vpn_update_one_connection_cb, cb_data, (DBusFreeFunction) free_update_one_vpn_cb_data);
	}
}


/*
 * nm_dbus_vpn_connections_update_from_nmi
490
491
492
493
 *
 * Update VPN connections from NetworkManagerInfo.
 *
 */
494
static gboolean nm_dbus_vpn_connections_update_from_nmi (NMData *data)
495
{
496
497
	DBusMessage *		message;
	DBusPendingCall *	pcall;
498
499
500
501
502
503
504
505
506
507
508
509
510
511

	g_return_val_if_fail (data != NULL, FALSE);
	g_return_val_if_fail (data->dbus_connection != NULL, FALSE);
	g_return_val_if_fail (data->vpn_manager != NULL, FALSE);

	/* Clear all existing connections in preparation for new ones */
	nm_vpn_manager_clear_connections (data->vpn_manager);

	if (!(message = dbus_message_new_method_call (NMI_DBUS_SERVICE, NMI_DBUS_PATH, NMI_DBUS_INTERFACE, "getVPNConnections")))
	{
		nm_warning ("nm_dbus_vpn_connections_update (): Couldn't allocate the dbus message");
		return FALSE;
	}

512
	dbus_connection_send_with_reply (data->dbus_connection, message, &pcall, -1);
513
	dbus_message_unref (message);
514
	if (pcall)
515
	{
516
517
		dbus_pending_call_set_notify (pcall, nm_dbus_vpn_connections_update_cb, data, NULL);
		dbus_pending_call_block (pcall);
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	}

	return FALSE;
}


/*
 * nm_dbus_vpn_schedule_vpn_connections_update
 *
 * Schedule an update of VPN connections in the main thread
 *
 */
void nm_dbus_vpn_schedule_vpn_connections_update (NMData *app_data)
{
	GSource	*source = NULL;

	g_return_if_fail (app_data != NULL);
	g_return_if_fail (app_data->main_context != NULL);

	source = g_idle_source_new ();
	/* We want this idle source to run before any other idle source */
	g_source_set_priority (source, G_PRIORITY_HIGH_IDLE);
540
	g_source_set_callback (source, (GSourceFunc) nm_dbus_vpn_connections_update_from_nmi, app_data, NULL);
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
	g_source_attach (source, app_data->main_context);
	g_source_unref (source);
}


/*
 * nm_dbus_vpn_get_vpn_connections
 *
 * Returns a string array of VPN connection names.
 *
 */
static DBusMessage *nm_dbus_vpn_get_vpn_connections (DBusConnection *connection, DBusMessage *message, NMDbusCBData *data)
{
	DBusMessage		*reply = NULL;
	char				**vpn_names = NULL;
	int				 num_names;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (data->data != NULL, NULL);
	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	if (!data->data->vpn_manager)
	{
		reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPN, "NoVPNConnections", "There are no available VPN connections.");
		goto out;
	}

	vpn_names = nm_vpn_manager_get_connection_names (data->data->vpn_manager);
	num_names = vpn_names ? g_strv_length (vpn_names) : 0;
	if (num_names == 0)
	{
		reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPN, "NoVPNConnections", "There are no available VPN connections.");
		goto out;
	}

	if (!(reply = dbus_message_new_method_return (message)))
		goto out;

580
	dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &vpn_names, num_names, DBUS_TYPE_INVALID);
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620

out:
	if (vpn_names)
		g_strfreev (vpn_names); 

	return (reply);
}


/*
 * nm_dbus_vpn_get_vpn_connection_properties
 *
 * Grab properties of a VPN connection
 *
 */
static DBusMessage *nm_dbus_vpn_get_vpn_connection_properties (DBusConnection *connection, DBusMessage *message, NMDbusCBData *data)
{
	DBusMessage		*reply = NULL;
	DBusError			 error;
	const char		*name;
	gboolean			 good = FALSE;
	NMVPNConnection	*vpn_con;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (data->data != NULL, NULL);
	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	/* Check for no VPN Manager */
	if (!data->data->vpn_manager)
		return nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPN, "NoVPNConnections", "There are no available VPN connections.");

	if (!(reply = dbus_message_new_method_return (message)))
		return NULL;

	dbus_error_init (&error);
	if (dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
	{
		if ((vpn_con = nm_vpn_manager_find_connection_by_name (data->data->vpn_manager, name)))
		{
621
622
			const char *user_name;
			const char *service;
623

624
625
626
627
			user_name = nm_vpn_connection_get_user_name (vpn_con);
			service = nm_vpn_service_get_service_name (nm_vpn_connection_get_service (vpn_con));

			dbus_message_append_args (reply, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &user_name, DBUS_TYPE_STRING, &service, DBUS_TYPE_INVALID);
628
629
630
631
632
			good = TRUE;
		}
	}

	if (!good)
633
		reply = nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPN, "InvalidVPNConnection", "No VPN connection with that name was found.");
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682

	return reply;
}


/*
 * nm_dbus_vpn_get_active_vpn_connection
 *
 * Return the name of the currently active VPN connection.
 *
 */
static DBusMessage *nm_dbus_vpn_get_active_vpn_connection (DBusConnection *connection, DBusMessage *message, NMDbusCBData *data)
{
	DBusMessage		*reply = NULL;
	const char		*name;
	NMVPNConnection	*vpn = NULL;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (data->data != NULL, NULL);
	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	/* Check for no VPN Manager */
	if (!data->data->vpn_manager)
		return nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPN, "NoVPNConnections", "There are no available VPN connections.");

	if (!(vpn = nm_vpn_manager_get_active_vpn_connection (data->data->vpn_manager)))
		return nm_dbus_create_error_message (message, NM_DBUS_INTERFACE_VPN, "NoActiveVPNConnection", "There is no active VPN connection.");

	if (!(reply = dbus_message_new_method_return (message)))
		return NULL;

	name = nm_vpn_connection_get_name (vpn);
	dbus_message_append_args (reply, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);

	return reply;
}


/*
 * nm_dbus_vpn_activate_connection
 *
 * Activate a specific VPN connection.
 *
 */
static DBusMessage *nm_dbus_vpn_activate_connection (DBusConnection *connection, DBusMessage *message, NMDbusCBData *data)
{
	DBusError			 error;
	const char		*name;
683
684
	char                    **passwords;
	int                     num_passwords;
685
686
687
688
689
690
691
	NMVPNConnection	*vpn;

	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (data->data != NULL, NULL);
	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

692
693
	nm_info ("Entering");

694
	dbus_error_init (&error);
695
696

	if (dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &passwords, &num_passwords, DBUS_TYPE_INVALID))	{
697
698
699
700
701
702
703
704
705
706
707
		if ((vpn = nm_vpn_manager_find_connection_by_name (data->data->vpn_manager, name)))
		{
			int	item_count = -1;
			char **items;

			if ((items = nm_dbus_vpn_get_vpn_data (connection, vpn, &item_count)))
			{
				char				*joined_string = g_strjoinv (" / ", items);
				NMVPNService		*service = nm_vpn_connection_get_service (vpn);;

				nm_info ("Will activate VPN connection '%s', service '%s', user_name '%s', vpn_data '%s'.",
708
					name, nm_vpn_service_get_service_name (service), nm_vpn_connection_get_user_name (vpn), joined_string);
709
				nm_vpn_manager_activate_vpn_connection (data->data->vpn_manager, vpn, passwords, num_passwords, items, item_count);
710
711
712
713

				g_free (joined_string);
				g_strfreev (items);
			}
714
715
		} else {
			nm_warning ("Cannot find name '%s'", name);
716
		}
717
718
	} else {
		nm_warning ("Syntax error receiving nm_dbus_vpn_activate_connection");
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
	}

	return NULL;
}


/*
 * nm_dbus_vpn_deactivate_connection
 *
 * Deactivate the active VPN connection, if any.
 *
 */
static DBusMessage *nm_dbus_vpn_deactivate_connection (DBusConnection *connection, DBusMessage *message, NMDbusCBData *data)
{
	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (data->data != NULL, NULL);
	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (message != NULL, NULL);

	nm_info ("Will deactivate the current VPN connection.");
	nm_vpn_manager_deactivate_vpn_connection (data->data->vpn_manager);

	return NULL;
}


/*
 * nm_dbus_vpn_methods_setup
 *
 * Register handlers for dbus methods on the
 * org.freedesktop.NetworkManager.VPNConnections object.
 *
 */
NMDbusMethodList *nm_dbus_vpn_methods_setup (void)
{
	NMDbusMethodList	*list = nm_dbus_method_list_new (NULL);

	nm_dbus_method_list_add_method (list, "getVPNConnections",			nm_dbus_vpn_get_vpn_connections);
	nm_dbus_method_list_add_method (list, "getVPNConnectionProperties",	nm_dbus_vpn_get_vpn_connection_properties);
	nm_dbus_method_list_add_method (list, "getActiveVPNConnection",		nm_dbus_vpn_get_active_vpn_connection);
	nm_dbus_method_list_add_method (list, "activateVPNConnection",		nm_dbus_vpn_activate_connection);
	nm_dbus_method_list_add_method (list, "deactivateVPNConnection",		nm_dbus_vpn_deactivate_connection);

	return (list);
}