nm-vpn-manager.c 13.5 KB
Newer Older
Dan Williams's avatar
Dan Williams committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
18
 * Copyright (C) 2005 - 2012 Red Hat, Inc.
Dan Williams's avatar
Dan Williams committed
19 20
 * Copyright (C) 2006 - 2008 Novell, Inc.
 */
21

22
#include <string.h>
23
#include <gio/gio.h>
24

25 26
#include "nm-vpn-manager.h"
#include "nm-vpn-service.h"
27
#include "nm-vpn-connection.h"
28
#include "nm-setting-vpn.h"
29
#include "nm-dbus-manager.h"
30
#include "NetworkManagerVPN.h"
31
#include "nm-marshal.h"
32
#include "nm-enum-types.h"
33
#include "nm-logging.h"
34

35
G_DEFINE_TYPE (NMVPNManager, nm_vpn_manager, G_TYPE_OBJECT)
36

37 38
#define NM_VPN_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_VPN_MANAGER, NMVPNManagerPrivate))

39
typedef struct {
40 41 42 43 44
	gboolean disposed;

	GHashTable *services;
	GFileMonitor *monitor;
	guint monitor_id;
45
} NMVPNManagerPrivate;
46

47
enum {
48
	CONNECTION_ACTIVATED,
49 50 51 52 53 54 55
	CONNECTION_DEACTIVATED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

56
GQuark
57 58 59 60 61 62 63 64 65
nm_vpn_manager_error_quark (void)
{
	static GQuark quark = 0;
	if (!quark)
		quark = g_quark_from_static_string ("nm-vpn-manager-error");
	return quark;
}


66
static NMVPNService *
67
get_service_by_namefile (NMVPNManager *self, const char *namefile)
68
{
69 70 71
	NMVPNManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE (self);
	GHashTableIter iter;
	gpointer data;
72

73 74
	g_return_val_if_fail (namefile, NULL);
	g_return_val_if_fail (g_path_is_absolute (namefile), NULL);
75

76 77 78 79
	g_hash_table_iter_init (&iter, priv->services);
	while (g_hash_table_iter_next (&iter, NULL, &data)) {
		NMVPNService *candidate = NM_VPN_SERVICE (data);
		const char *service_namefile;
80

81 82 83 84
		service_namefile = nm_vpn_service_get_name_file (candidate);
		if (!strcmp (namefile, service_namefile))
			return candidate;
	}
85
	return NULL;
86 87
}

88
static NMVPNConnection *
89
find_active_vpn_connection_by_connection (NMVPNManager *self, NMConnection *connection)
90
{
91 92 93
	NMVPNManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE (self);
	GHashTableIter iter;
	gpointer data;
Dan Williams's avatar
Dan Williams committed
94 95
	GSList *active, *aiter;
	NMVPNConnection *found = NULL;
96

97
	g_return_val_if_fail (connection, NULL);
98
	g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
99

100
	g_hash_table_iter_init (&iter, priv->services);
Dan Williams's avatar
Dan Williams committed
101 102 103 104
	while (g_hash_table_iter_next (&iter, NULL, &data) && (found == NULL)) {
		active = nm_vpn_service_get_active_connections (NM_VPN_SERVICE (data));
		for (aiter = active; aiter; aiter = g_slist_next (aiter)) {
			NMVPNConnection *vpn = NM_VPN_CONNECTION (aiter->data);
105

Dan Williams's avatar
Dan Williams committed
106 107 108 109
			if (nm_vpn_connection_get_connection (vpn) == connection) {
				found = vpn;
				break;
			}
110
		}
Dan Williams's avatar
Dan Williams committed
111
		g_slist_free (active);
112
	}
Dan Williams's avatar
Dan Williams committed
113
	return found;
114 115
}

116
static void
117
connection_vpn_state_changed (NMVPNConnection *connection,
118 119
                              NMVPNConnectionState new_state,
                              NMVPNConnectionState old_state,
120 121
                              NMVPNConnectionStateReason reason,
                              gpointer user_data)
122
{
123 124
	NMVPNManager *manager = NM_VPN_MANAGER (user_data);

125
	switch (new_state) {
126 127 128
	case NM_VPN_CONNECTION_STATE_ACTIVATED:
		g_signal_emit (manager, signals[CONNECTION_ACTIVATED], 0, connection);
		break;
129 130
	case NM_VPN_CONNECTION_STATE_FAILED:
	case NM_VPN_CONNECTION_STATE_DISCONNECTED:
131
		g_signal_emit (manager, signals[CONNECTION_DEACTIVATED], 0, connection, new_state, old_state, reason);
132 133 134 135
		break;
	default:
		break;
	}
136 137
}

138
NMActiveConnection *
139 140 141
nm_vpn_manager_activate_connection (NMVPNManager *manager,
                                    NMConnection *connection,
                                    NMDevice *device,
142
                                    const char *specific_object,
143 144
                                    gboolean user_requested,
                                    gulong user_uid,
145
                                    GError **error)
146
{
147 148
	NMSettingVPN *vpn_setting;
	NMVPNService *service;
149
	NMVPNConnection *vpn = NULL;
150
	const char *service_name;
151

152 153 154 155 156
	g_return_val_if_fail (NM_IS_VPN_MANAGER (manager), NULL);
	g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
	g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
	g_return_val_if_fail (error != NULL, NULL);
	g_return_val_if_fail (*error == NULL, NULL);
157

158 159
	if (   nm_device_get_state (device) != NM_DEVICE_STATE_ACTIVATED
	    && nm_device_get_state (device) != NM_DEVICE_STATE_SECONDARIES) {
160 161 162 163
		g_set_error (error,
		             NM_VPN_MANAGER_ERROR, NM_VPN_MANAGER_ERROR_DEVICE_NOT_ACTIVE,
		             "%s", "The base device for the VPN connection was not active.");
		return NULL;
164 165
	}

166
	vpn_setting = nm_connection_get_setting_vpn (connection);
167 168 169 170 171
	if (!vpn_setting) {
		g_set_error (error,
		             NM_VPN_MANAGER_ERROR, NM_VPN_MANAGER_ERROR_CONNECTION_INVALID,
		             "%s", "The connection was not a VPN connection.");
		return NULL;
172 173
	}

174 175 176 177
	vpn = find_active_vpn_connection_by_connection (manager, connection);
	if (vpn) {
		nm_vpn_connection_disconnect (vpn, NM_VPN_CONNECTION_STATE_REASON_USER_DISCONNECTED);
		vpn = NULL;
178 179
	}

180 181 182
	service_name = nm_setting_vpn_get_service_type (vpn_setting);
	g_assert (service_name);
	service = g_hash_table_lookup (NM_VPN_MANAGER_GET_PRIVATE (manager)->services, service_name);
183 184 185
	if (!service) {
		g_set_error (error,
		             NM_VPN_MANAGER_ERROR, NM_VPN_MANAGER_ERROR_SERVICE_INVALID,
186 187 188 189 190
		             "The VPN service '%s' was not installed.",
		             service_name);
		return NULL;
	}

191
	vpn = nm_vpn_service_activate (service, connection, device, specific_object, user_requested, user_uid, error);
192
	if (vpn) {
193
		g_signal_connect (vpn, NM_VPN_CONNECTION_INTERNAL_STATE_CHANGED,
194 195
		                  G_CALLBACK (connection_vpn_state_changed),
		                  manager);
196
	}
197

198
	return (NMActiveConnection *) vpn;
199 200
}

201
gboolean
202
nm_vpn_manager_deactivate_connection (NMVPNManager *self,
203 204
                                      const char *path,
                                      NMVPNConnectionStateReason reason)
205
{
206
	NMVPNManagerPrivate *priv;
207 208
	GHashTableIter iter;
	gpointer data;
Dan Williams's avatar
Dan Williams committed
209 210
	GSList *active, *aiter;
	gboolean success = FALSE;
211

212 213
	g_return_val_if_fail (self, FALSE);
	g_return_val_if_fail (NM_IS_VPN_MANAGER (self), FALSE);
214
	g_return_val_if_fail (path != NULL, FALSE);
215

216 217
	priv = NM_VPN_MANAGER_GET_PRIVATE (self);
	g_hash_table_iter_init (&iter, priv->services);
Dan Williams's avatar
Dan Williams committed
218
	while (g_hash_table_iter_next (&iter, NULL, &data) && (success == FALSE)) {
219
		active = nm_vpn_service_get_active_connections (NM_VPN_SERVICE (data));
Dan Williams's avatar
Dan Williams committed
220 221
		for (aiter = active; aiter; aiter = g_slist_next (aiter)) {
			NMVPNConnection *vpn = NM_VPN_CONNECTION (aiter->data);
222 223
			const char *vpn_path;

224
			vpn_path = nm_active_connection_get_path (NM_ACTIVE_CONNECTION (vpn));
225
			if (!strcmp (path, vpn_path)) {
226
				nm_vpn_connection_disconnect (vpn, reason);
Dan Williams's avatar
Dan Williams committed
227 228
				success = TRUE;
				break;
229 230
			}
		}
Dan Williams's avatar
Dan Williams committed
231
		g_slist_free (active);
232
	}
233

Dan Williams's avatar
Dan Williams committed
234
	return success;
235 236
}

237 238 239
NMVPNConnection *
nm_vpn_manager_get_vpn_connection_for_active (NMVPNManager *manager,
                                              const char *active_path)
240 241
{
	NMVPNManagerPrivate *priv;
242 243 244
	GHashTableIter iter;
	gpointer data;
	GSList *active, *elt;
245 246 247 248

	g_return_val_if_fail (NM_IS_VPN_MANAGER (manager), NULL);

	priv = NM_VPN_MANAGER_GET_PRIVATE (manager);
249 250 251
	g_hash_table_iter_init (&iter, priv->services);
	while (g_hash_table_iter_next (&iter, NULL, &data)) {
		active = nm_vpn_service_get_active_connections (NM_VPN_SERVICE (data));
252
		for (elt = active; elt; elt = g_slist_next (elt)) {
253
			NMVPNConnection *vpn = NM_VPN_CONNECTION (elt->data);
254 255
			const char *ac_path;

256
			ac_path = nm_active_connection_get_path (NM_ACTIVE_CONNECTION (vpn));
257
			if (ac_path && !strcmp (ac_path, active_path))
258
				return vpn;
259 260 261 262 263 264
		}
	}

	return NULL;
}

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
static char *
service_name_from_file (const char *path)
{
	GKeyFile *kf = NULL;
	char *service_name = NULL;

	g_return_val_if_fail (g_path_is_absolute (path), NULL);

	if (!g_str_has_suffix (path, ".name"))
		return NULL;

	kf = g_key_file_new ();
	if (g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, NULL))
		service_name = g_key_file_get_string (kf, VPN_CONNECTION_GROUP, "service", NULL);

	g_key_file_free (kf);
	return service_name;
}

static void
try_add_service (NMVPNManager *self, const char *namefile)
{
	NMVPNManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE (self);
	NMVPNService *service = NULL;
	GError *error = NULL;
	const char *service_name;
	char *tmp;

	g_return_if_fail (g_path_is_absolute (namefile));

	/* Make sure we don't add dupes */
	tmp = service_name_from_file (namefile);
	if (tmp)
		service = g_hash_table_lookup (priv->services, tmp);
	g_free (tmp);
	if (service)
		return;

	/* New service, add it */
	service = nm_vpn_service_new (namefile, &error);
	if (!service) {
		nm_log_warn (LOGD_VPN, "failed to load VPN service file %s: (%d) %s",
307
		             namefile,
308 309 310 311 312 313 314 315
		             error ? error->code : -1,
		             error && error->message ? error->message : "(unknown)");
		g_clear_error (&error);
		return;
	}

	service_name = nm_vpn_service_get_dbus_service (service);
	g_hash_table_insert (priv->services, (char *) service_name, service);
316
	nm_log_info (LOGD_VPN, "VPN: loaded %s", service_name);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
}

static void
vpn_dir_changed (GFileMonitor *monitor,
                 GFile *file,
                 GFile *other_file,
                 GFileMonitorEvent event_type,
                 gpointer user_data)
{
	NMVPNManager *self = NM_VPN_MANAGER (user_data);
	NMVPNManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE (self);
	NMVPNService *service;
	char *path;

	path = g_file_get_path (file);
	if (!g_str_has_suffix (path, ".name")) {
		g_free (path);
		return;
	}

	switch (event_type) {
	case G_FILE_MONITOR_EVENT_DELETED:
		nm_log_dbg (LOGD_VPN, "service file %s deleted", path);

		service = get_service_by_namefile (self, path);
		if (service) {
			const char *service_name = nm_vpn_service_get_dbus_service (service);

			/* Stop active VPN connections and destroy the service */
			nm_vpn_service_connections_stop (service, TRUE,
			                                 NM_VPN_CONNECTION_STATE_REASON_SERVICE_STOPPED);
348
			nm_log_info (LOGD_VPN, "VPN: unloaded %s", service_name);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
			g_hash_table_remove (priv->services, service_name);
		}
		break;
	case G_FILE_MONITOR_EVENT_CREATED:
	case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
		nm_log_dbg (LOGD_VPN, "service file %s created or modified", path);
		try_add_service (self, path);
		break;
	default:
		nm_log_dbg (LOGD_VPN, "service file %s change event %d", path, event_type);
		break;
	}

	g_free (path);
}

/******************************************************************************/

367
NMVPNManager *
368
nm_vpn_manager_get (void)
369
{
370
	static NMVPNManager *singleton = NULL;
371

372 373 374 375
	if (!singleton)
		singleton = NM_VPN_MANAGER (g_object_new (NM_TYPE_VPN_MANAGER, NULL));
	else
		g_object_ref (singleton);
376

377 378
	g_assert (singleton);
	return singleton;
379 380
}

381
static void
382
nm_vpn_manager_init (NMVPNManager *self)
383
{
384 385 386 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
	NMVPNManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE (self);
	GFile *file;
	GDir *dir;
	const char *fn;
	char *path;

	priv->services = g_hash_table_new_full (g_str_hash, g_str_equal,
	                                        NULL, g_object_unref);

	/* Watch the VPN directory for changes */
	file = g_file_new_for_path (VPN_NAME_FILES_DIR "/");
	priv->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
	g_object_unref (file);
	if (priv->monitor) {
		priv->monitor_id = g_signal_connect (priv->monitor, "changed",
		                                     G_CALLBACK (vpn_dir_changed), self);
	}

	/* Load VPN service files */
	dir = g_dir_open (VPN_NAME_FILES_DIR, 0, NULL);
	if (dir) {
		while ((fn = g_dir_read_name (dir))) {
			/* only parse filenames that end with .name */
			if (g_str_has_suffix (fn, ".name")) {
				path = g_build_filename (VPN_NAME_FILES_DIR, fn, NULL);
				try_add_service (self, path);
				g_free (path);
			}
		}
		g_dir_close (dir);
	}
415 416
}

417
static void
418
dispose (GObject *object)
419
{
420
	NMVPNManagerPrivate *priv = NM_VPN_MANAGER_GET_PRIVATE (object);
421

422 423 424 425 426 427 428 429 430 431 432 433
	if (!priv->disposed) {
		priv->disposed = TRUE;

		if (priv->monitor) {
			if (priv->monitor_id)
				g_signal_handler_disconnect (priv->monitor, priv->monitor_id);
			g_file_monitor_cancel (priv->monitor);
			g_object_unref (priv->monitor);
		}

		g_hash_table_destroy (priv->services);
	}
434

435
	G_OBJECT_CLASS (nm_vpn_manager_parent_class)->dispose (object);
436 437 438
}

static void
439
nm_vpn_manager_class_init (NMVPNManagerClass *manager_class)
440
{
441
	GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
442

443
	g_type_class_add_private (manager_class, sizeof (NMVPNManagerPrivate));
444

445
	/* virtual methods */
446
	object_class->dispose = dispose;
447

448
	/* signals */
449 450 451 452 453 454 455 456
	signals[CONNECTION_ACTIVATED] =
		g_signal_new ("connection-activated",
				    G_OBJECT_CLASS_TYPE (object_class),
				    G_SIGNAL_RUN_FIRST,
				    0, NULL, NULL,
				    g_cclosure_marshal_VOID__OBJECT,
				    G_TYPE_NONE, 1, G_TYPE_OBJECT);

457 458 459 460
	signals[CONNECTION_DEACTIVATED] =
		g_signal_new ("connection-deactivated",
				    G_OBJECT_CLASS_TYPE (object_class),
				    G_SIGNAL_RUN_FIRST,
461
				    0, NULL, NULL,
462
				    _nm_marshal_VOID__OBJECT_UINT_UINT_UINT,
463
				    G_TYPE_NONE, 4, G_TYPE_OBJECT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
464 465

	dbus_g_error_domain_register (NM_VPN_MANAGER_ERROR, NULL, NM_TYPE_VPN_MANAGER_ERROR);
466
}
467