nm-manager.c 72.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2007 - 2008 Novell, Inc.
 * Copyright 2007 - 2014 Red Hat, Inc.
 */

22
#include "nm-default.h"
23

24
#include "nm-manager.h"
25 26

#include "nm-utils.h"
27
#include "nm-checkpoint.h"
28
#include "nm-common-macros.h"
29 30 31 32 33 34 35
#include "nm-device-ethernet.h"
#include "nm-device-wifi.h"
#include "nm-core-internal.h"
#include "nm-object-private.h"
#include "nm-active-connection.h"
#include "nm-vpn-connection.h"
#include "nm-dbus-helpers.h"
36
#include "c-list/src/c-list.h"
37

38
#include "introspection/org.freedesktop.NetworkManager.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

void _nm_device_wifi_set_wireless_enabled (NMDeviceWifi *device, gboolean enabled);

static void nm_manager_initable_iface_init (GInitableIface *iface);
static void nm_manager_async_initable_iface_init (GAsyncInitableIface *iface);
static GInitableIface *nm_manager_parent_initable_iface;
static GAsyncInitableIface *nm_manager_parent_async_initable_iface;

G_DEFINE_TYPE_WITH_CODE (NMManager, nm_manager, NM_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, nm_manager_initable_iface_init);
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, nm_manager_async_initable_iface_init);
                         )

#define NM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_MANAGER, NMManagerPrivate))

typedef struct {
55
	NMDBusManager *proxy;
56
	GCancellable *props_cancellable;
57 58 59 60
	char *version;
	NMState state;
	gboolean startup;
	GPtrArray *devices;
61
	GPtrArray *all_devices;
62
	GPtrArray *active_connections;
63 64
	GPtrArray *checkpoints;
	GSList *added_checkpoints;
65 66 67
	NMConnectivityState connectivity;
	NMActiveConnection *primary_connection;
	NMActiveConnection *activating_connection;
68
	NMMetered metered;
69 70 71 72 73 74 75

	GCancellable *perm_call_cancellable;
	GHashTable *permissions;

	/* Activations waiting for their NMActiveConnection
	 * to appear and then their callback to be called.
	 */
76
	CList pending_activations;
77 78 79 80 81 82 83 84 85 86

	gboolean networking_enabled;
	gboolean wireless_enabled;
	gboolean wireless_hw_enabled;

	gboolean wwan_enabled;
	gboolean wwan_hw_enabled;

	gboolean wimax_enabled;
	gboolean wimax_hw_enabled;
87 88 89

	gboolean connectivity_check_available;
	gboolean connectivity_check_enabled;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
} NMManagerPrivate;

enum {
	PROP_0,
	PROP_VERSION,
	PROP_STATE,
	PROP_STARTUP,
	PROP_NETWORKING_ENABLED,
	PROP_WIRELESS_ENABLED,
	PROP_WIRELESS_HARDWARE_ENABLED,
	PROP_WWAN_ENABLED,
	PROP_WWAN_HARDWARE_ENABLED,
	PROP_WIMAX_ENABLED,
	PROP_WIMAX_HARDWARE_ENABLED,
	PROP_ACTIVE_CONNECTIONS,
	PROP_CONNECTIVITY,
106 107
	PROP_CONNECTIVITY_CHECK_AVAILABLE,
	PROP_CONNECTIVITY_CHECK_ENABLED,
108 109 110
	PROP_PRIMARY_CONNECTION,
	PROP_ACTIVATING_CONNECTION,
	PROP_DEVICES,
111
	PROP_CHECKPOINTS,
112
	PROP_METERED,
113
	PROP_ALL_DEVICES,
114 115 116 117 118 119 120

	LAST_PROP
};

enum {
	DEVICE_ADDED,
	DEVICE_REMOVED,
121 122
	ANY_DEVICE_ADDED,
	ANY_DEVICE_REMOVED,
123 124
	ACTIVE_CONNECTION_ADDED,
	ACTIVE_CONNECTION_REMOVED,
125 126
	CHECKPOINT_ADDED,
	CHECKPOINT_REMOVED,
127 128 129 130 131 132 133
	PERMISSION_CHANGED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

134
/*****************************************************************************/
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
typedef struct {
	NMManager *manager;
	GSimpleAsyncResult *simple;
	char *path;
} CheckpointInfo;

static CheckpointInfo *
find_checkpoint_info (NMManager *manager, const char *path)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
	CheckpointInfo *info;
	GSList *iter;

	for (iter = priv->added_checkpoints; iter; iter = g_slist_next (iter)) {
		info = iter->data;
151
		if (nm_streq0 (path, info->path))
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
			return info;
	}

	return NULL;
}

static void
checkpoint_info_complete (NMManager *self,
                          CheckpointInfo *info,
                          NMCheckpoint *checkpoint,
                          GError *error)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);

	g_return_if_fail (info);

	if (checkpoint) {
		g_simple_async_result_set_op_res_gpointer (info->simple,
		                                           g_object_ref (checkpoint),
		                                           g_object_unref);
	} else
		g_simple_async_result_set_from_error (info->simple, error);
	g_simple_async_result_complete (info->simple);

	g_object_unref (info->simple);
	priv->added_checkpoints = g_slist_remove (priv->added_checkpoints, info);

	g_free (info->path);
	g_slice_free (CheckpointInfo, info);
}

183 184 185 186 187
static void
nm_manager_init (NMManager *manager)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);

188 189
	c_list_init (&priv->pending_activations);

190 191 192
	priv->state = NM_STATE_UNKNOWN;
	priv->connectivity = NM_CONNECTIVITY_UNKNOWN;

193
	priv->permissions = g_hash_table_new (nm_direct_hash, NULL);
194
	priv->devices = g_ptr_array_new ();
195
	priv->all_devices = g_ptr_array_new ();
196
	priv->active_connections = g_ptr_array_new ();
197 198 199 200 201 202 203 204
}

static void
poke_wireless_devices_with_rf_status (NMManager *manager)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
	int i;

205 206
	for (i = 0; i < priv->all_devices->len; i++) {
		NMDevice *device = g_ptr_array_index (priv->all_devices, i);
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

		if (NM_IS_DEVICE_WIFI (device))
			_nm_device_wifi_set_wireless_enabled (NM_DEVICE_WIFI (device), priv->wireless_enabled);
	}
}

static void
wireless_enabled_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
{
	poke_wireless_devices_with_rf_status (NM_MANAGER (object));
}

static void manager_recheck_permissions (NMDBusManager *proxy, gpointer user_data);

static void
init_dbus (NMObject *object)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (object);
	const NMPropertiesInfo property_info[] = {
		{ NM_MANAGER_VERSION,                   &priv->version },
		{ NM_MANAGER_STATE,                     &priv->state },
		{ NM_MANAGER_STARTUP,                   &priv->startup },
		{ NM_MANAGER_NETWORKING_ENABLED,        &priv->networking_enabled },
		{ NM_MANAGER_WIRELESS_ENABLED,          &priv->wireless_enabled },
		{ NM_MANAGER_WIRELESS_HARDWARE_ENABLED, &priv->wireless_hw_enabled },
		{ NM_MANAGER_WWAN_ENABLED,              &priv->wwan_enabled },
		{ NM_MANAGER_WWAN_HARDWARE_ENABLED,     &priv->wwan_hw_enabled },
		{ NM_MANAGER_WIMAX_ENABLED,             &priv->wimax_enabled },
		{ NM_MANAGER_WIMAX_HARDWARE_ENABLED,    &priv->wimax_hw_enabled },
236
		{ NM_MANAGER_ACTIVE_CONNECTIONS,        &priv->active_connections, NULL, NM_TYPE_ACTIVE_CONNECTION, "active-connection" },
237
		{ NM_MANAGER_CONNECTIVITY,              &priv->connectivity },
238 239
		{ NM_MANAGER_CONNECTIVITY_CHECK_AVAILABLE, &priv->connectivity_check_available },
		{ NM_MANAGER_CONNECTIVITY_CHECK_ENABLED, &priv->connectivity_check_enabled },
240 241 242
		{ NM_MANAGER_PRIMARY_CONNECTION,        &priv->primary_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
		{ NM_MANAGER_ACTIVATING_CONNECTION,     &priv->activating_connection, NULL, NM_TYPE_ACTIVE_CONNECTION },
		{ NM_MANAGER_DEVICES,                   &priv->devices, NULL, NM_TYPE_DEVICE, "device" },
243
		{ NM_MANAGER_CHECKPOINTS,               &priv->checkpoints, NULL, NM_TYPE_CHECKPOINT, "checkpoint" },
244
		{ NM_MANAGER_METERED,                   &priv->metered },
245
		{ NM_MANAGER_ALL_DEVICES,               &priv->all_devices, NULL, NM_TYPE_DEVICE, "any-device" },
246 247 248 249 250
		{ NULL },
	};

	NM_OBJECT_CLASS (nm_manager_parent_class)->init_dbus (object);

251
	priv->proxy = NMDBUS_MANAGER (_nm_object_get_proxy (object, NM_DBUS_INTERFACE));
252 253 254 255 256
	_nm_object_register_properties (object,
	                                NM_DBUS_INTERFACE,
	                                property_info);

	/* Permissions */
257 258
	g_signal_connect_object (priv->proxy, "check-permissions",
				 G_CALLBACK (manager_recheck_permissions), object, 0);
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static void
object_creation_failed (NMObject *object, const char *failed_path)
{
	NMManager *self = NM_MANAGER (object);
	CheckpointInfo *info;
	GError *add_error;

	info = find_checkpoint_info (self, failed_path);
	if (info) {
		add_error = g_error_new_literal (NM_CLIENT_ERROR,
		                                 NM_CLIENT_ERROR_OBJECT_CREATION_FAILED,
		                                 _("Checkpoint was removed before it was initialized"));
		checkpoint_info_complete (self, info, NULL, add_error);
		g_error_free (add_error);
	}
}

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
static NMClientPermission
nm_permission_to_client (const char *nm)
{
	if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SLEEP_WAKE))
		return NM_CLIENT_PERMISSION_SLEEP_WAKE;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_NETWORK_CONTROL))
		return NM_CLIENT_PERMISSION_NETWORK_CONTROL;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED))
		return NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_WIFI_SHARE_OPEN))
		return NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN;
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME;
303 304
	else if (!strcmp (nm, NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS))
		return NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS;
305 306
	else if (!strcmp (nm, NM_AUTH_PERMISSION_RELOAD))
		return NM_CLIENT_PERMISSION_RELOAD;
307 308
	else if (!strcmp (nm, NM_AUTH_PERMISSION_CHECKPOINT_ROLLBACK))
		return NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK;
309 310
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_STATISTICS))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS;
311 312
	else if (!strcmp (nm, NM_AUTH_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK))
		return NM_CLIENT_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK;
313 314 315 316 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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

	return NM_CLIENT_PERMISSION_NONE;
}

static NMClientPermissionResult
nm_permission_result_to_client (const char *nm)
{
	if (!strcmp (nm, "yes"))
		return NM_CLIENT_PERMISSION_RESULT_YES;
	else if (!strcmp (nm, "no"))
		return NM_CLIENT_PERMISSION_RESULT_NO;
	else if (!strcmp (nm, "auth"))
		return NM_CLIENT_PERMISSION_RESULT_AUTH;
	return NM_CLIENT_PERMISSION_RESULT_UNKNOWN;
}

static void
update_permissions (NMManager *self, GVariant *permissions)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
	GHashTableIter iter;
	gpointer key, value;
	NMClientPermission perm;
	NMClientPermissionResult perm_result;
	GList *keys, *keys_iter;

	/* get list of old permissions for change notification */
	keys = g_hash_table_get_keys (priv->permissions);
	g_hash_table_remove_all (priv->permissions);

	if (permissions) {
		GVariantIter viter;
		const char *pkey, *pvalue;

		/* Process new permissions */
		g_variant_iter_init (&viter, permissions);
		while (g_variant_iter_next (&viter, "{&s&s}", &pkey, &pvalue)) {
			perm = nm_permission_to_client (pkey);
			perm_result = nm_permission_result_to_client (pvalue);
			if (perm) {
				g_hash_table_insert (priv->permissions,
				                     GUINT_TO_POINTER (perm),
				                     GUINT_TO_POINTER (perm_result));

				/* Remove this permission from the list of previous permissions
				 * we'll be sending NM_CLIENT_PERMISSION_RESULT_UNKNOWN for
				 * in the change signal since it is still a known permission.
				 */
				keys = g_list_remove (keys, GUINT_TO_POINTER (perm));
			}
		}
	}

	/* Signal changes in all updated permissions */
	g_hash_table_iter_init (&iter, priv->permissions);
	while (g_hash_table_iter_next (&iter, &key, &value)) {
		g_signal_emit (self, signals[PERMISSION_CHANGED], 0,
		               GPOINTER_TO_UINT (key),
		               GPOINTER_TO_UINT (value));
	}

	/* And signal changes in all permissions that used to be valid but for
	 * some reason weren't received in the last request (if any).
	 */
	for (keys_iter = keys; keys_iter; keys_iter = g_list_next (keys_iter)) {
		g_signal_emit (self, signals[PERMISSION_CHANGED], 0,
		               GPOINTER_TO_UINT (keys_iter->data),
		               NM_CLIENT_PERMISSION_RESULT_UNKNOWN);
	}
	g_list_free (keys);
}

static gboolean
get_permissions_sync (NMManager *self, GError **error)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
	GVariant *permissions;

391
	if (nmdbus_manager_call_get_permissions_sync (priv->proxy,
392 393 394 395 396 397
	                                              &permissions,
	                                              NULL, error)) {
		update_permissions (self, permissions);
		g_variant_unref (permissions);
		return TRUE;
	} else {
398 399
		if (error && *error)
			g_dbus_error_strip_remote_error (*error);
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 438 439 440 441 442 443 444 445 446 447
		update_permissions (self, NULL);
		return FALSE;
	}
}

static void
get_permissions_reply (GObject *object,
                       GAsyncResult *result,
                       gpointer user_data)
{
	NMManager *self;
	NMManagerPrivate *priv;
	GVariant *permissions = NULL;
	GError *error = NULL;

	/* WARNING: this may be called after the manager is disposed, so we can't
	 * look at self/priv until after we've determined that that isn't the case.
	 */

	nmdbus_manager_call_get_permissions_finish (NMDBUS_MANAGER (object),
	                                            &permissions,
	                                            result, &error);
	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		/* @self has been disposed. */
		g_error_free (error);
		return;
	}

	self = user_data;
	priv = NM_MANAGER_GET_PRIVATE (self);

	update_permissions (self, permissions);

	g_clear_pointer (&permissions, g_variant_unref);
	g_clear_error (&error);
	g_clear_object (&priv->perm_call_cancellable);
}

static void
manager_recheck_permissions (NMDBusManager *proxy, gpointer user_data)
{
	NMManager *self = NM_MANAGER (user_data);
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);

	if (priv->perm_call_cancellable)
		return;

	priv->perm_call_cancellable = g_cancellable_new ();
448
	nmdbus_manager_call_get_permissions (priv->proxy,
449 450 451 452 453 454 455 456 457 458
	                                     priv->perm_call_cancellable,
	                                     get_permissions_reply,
	                                     self);
}

const char *
nm_manager_get_version (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

459
	return nm_str_not_empty (NM_MANAGER_GET_PRIVATE (manager)->version);
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
}

NMState
nm_manager_get_state (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NM_STATE_UNKNOWN);

	return NM_MANAGER_GET_PRIVATE (manager)->state;
}

gboolean
nm_manager_get_startup (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NM_STATE_UNKNOWN);

	return NM_MANAGER_GET_PRIVATE (manager)->startup;
}

gboolean
nm_manager_networking_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->networking_enabled;
}

gboolean
nm_manager_networking_set_enabled (NMManager *manager, gboolean enable, GError **error)
{
489 490
	gboolean ret;

491 492
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

493
	ret = nmdbus_manager_call_enable_sync (NM_MANAGER_GET_PRIVATE (manager)->proxy,
494 495 496 497 498
	                                       enable,
	                                       NULL, error);
	if (error && *error)
		g_dbus_error_strip_remote_error (*error);
	return ret;
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 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 580 581
}

gboolean
nm_manager_wireless_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->wireless_enabled;
}

void
nm_manager_wireless_set_enabled (NMManager *manager, gboolean enabled)
{
	g_return_if_fail (NM_IS_MANAGER (manager));

	_nm_object_set_property (NM_OBJECT (manager),
	                         NM_DBUS_INTERFACE,
	                         "WirelessEnabled",
	                         "b", enabled);
}

gboolean
nm_manager_wireless_hardware_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->wireless_hw_enabled;
}

gboolean
nm_manager_wwan_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->wwan_enabled;
}

void
nm_manager_wwan_set_enabled (NMManager *manager, gboolean enabled)
{
	g_return_if_fail (NM_IS_MANAGER (manager));

	_nm_object_set_property (NM_OBJECT (manager),
	                         NM_DBUS_INTERFACE,
	                         "WwanEnabled",
	                         "b", enabled);
}

gboolean
nm_manager_wwan_hardware_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->wwan_hw_enabled;
}

gboolean
nm_manager_wimax_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->wimax_enabled;
}

void
nm_manager_wimax_set_enabled (NMManager *manager, gboolean enabled)
{
	g_return_if_fail (NM_IS_MANAGER (manager));

	_nm_object_set_property (NM_OBJECT (manager),
	                         NM_DBUS_INTERFACE,
	                         "WimaxEnabled",
	                         "b", enabled);
}

gboolean
nm_manager_wimax_hardware_get_enabled (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->wimax_hw_enabled;
}

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
gboolean
nm_manager_connectivity_check_get_available (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);

	return NM_MANAGER_GET_PRIVATE (manager)->connectivity_check_available;
}

gboolean
nm_manager_connectivity_check_get_enabled (NMManager *manager)
{
	return NM_MANAGER_GET_PRIVATE (manager)->connectivity_check_enabled;
}

void
nm_manager_connectivity_check_set_enabled (NMManager *manager, gboolean enabled)
{
	g_return_if_fail (NM_IS_MANAGER (manager));

	_nm_object_set_property (NM_OBJECT (manager),
	                         NM_DBUS_INTERFACE,
	                         "ConnectivityCheckEnabled",
	                         "b", enabled);
}

607 608 609
gboolean
nm_manager_get_logging (NMManager *manager, char **level, char **domains, GError **error)
{
610 611
	gboolean ret;

612 613 614 615 616 617 618 619
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);
	g_return_val_if_fail (level == NULL || *level == NULL, FALSE);
	g_return_val_if_fail (domains == NULL || *domains == NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	if (!level && !domains)
		return TRUE;

620
	ret = nmdbus_manager_call_get_logging_sync (NM_MANAGER_GET_PRIVATE (manager)->proxy,
621 622 623 624 625
	                                            level, domains,
	                                            NULL, error);
	if (error && *error)
		g_dbus_error_strip_remote_error (*error);
	return ret;
626 627 628 629 630
}

gboolean
nm_manager_set_logging (NMManager *manager, const char *level, const char *domains, GError **error)
{
631 632
	gboolean ret;

633 634 635 636 637 638 639 640 641 642 643
	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	if (!level && !domains)
		return TRUE;

	if (!level)
		level = "";
	if (!domains)
		domains = "";

644
	ret = nmdbus_manager_call_set_logging_sync (NM_MANAGER_GET_PRIVATE (manager)->proxy,
645 646 647 648 649
	                                            level, domains,
	                                            NULL, error);
	if (error && *error)
		g_dbus_error_strip_remote_error (*error);
	return ret;
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
}

NMClientPermissionResult
nm_manager_get_permission_result (NMManager *manager, NMClientPermission permission)
{
	gpointer result;

	g_return_val_if_fail (NM_IS_MANAGER (manager), NM_CLIENT_PERMISSION_RESULT_UNKNOWN);

	result = g_hash_table_lookup (NM_MANAGER_GET_PRIVATE (manager)->permissions,
	                              GUINT_TO_POINTER (permission));
	return GPOINTER_TO_UINT (result);
}

NMConnectivityState
nm_manager_get_connectivity (NMManager *manager)
{
667
	g_return_val_if_fail (NM_IS_MANAGER (manager), NM_CONNECTIVITY_UNKNOWN);
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

	return NM_MANAGER_GET_PRIVATE (manager)->connectivity;
}

NMConnectivityState
nm_manager_check_connectivity (NMManager *manager,
                               GCancellable *cancellable,
                               GError **error)
{
	NMManagerPrivate *priv;
	guint32 connectivity;

	g_return_val_if_fail (NM_IS_MANAGER (manager), NM_CONNECTIVITY_UNKNOWN);
	priv = NM_MANAGER_GET_PRIVATE (manager);

683
	if (nmdbus_manager_call_check_connectivity_sync (priv->proxy,
684
	                                                 &connectivity,
685 686 687 688 689
	                                                 cancellable, error)) {
		if (connectivity != priv->connectivity) {
			priv->connectivity = connectivity;
			g_object_notify (G_OBJECT (manager), NM_MANAGER_CONNECTIVITY);
		}
690
		return connectivity;
691
	}
692 693 694
	else {
		if (error && *error)
			g_dbus_error_strip_remote_error (*error);
695
		return NM_CONNECTIVITY_UNKNOWN;
696
	}
697 698 699 700 701 702 703 704 705 706
}

static void
check_connectivity_cb (GObject *object,
                       GAsyncResult *result,
                       gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
	guint32 connectivity;
	GError *error = NULL;
707 708
	NMManager *manager;
	NMManagerPrivate *priv;
709 710 711

	if (nmdbus_manager_call_check_connectivity_finish (NMDBUS_MANAGER (object),
	                                                   &connectivity,
712
	                                                   result, &error)) {
713
		g_simple_async_result_set_op_res_gssize (simple, connectivity);
714 715 716 717 718 719 720 721

		manager = NM_MANAGER (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
		priv = NM_MANAGER_GET_PRIVATE (manager);
		if (connectivity != priv->connectivity) {
			priv->connectivity = connectivity;
			g_object_notify (G_OBJECT (manager), NM_MANAGER_CONNECTIVITY);
		}
	} else {
722
		g_dbus_error_strip_remote_error (error);
723
		g_simple_async_result_take_error (simple, error);
724
	}
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}

void
nm_manager_check_connectivity_async (NMManager *manager,
                                     GCancellable *cancellable,
                                     GAsyncReadyCallback callback,
                                     gpointer user_data)
{
	NMManagerPrivate *priv;
	GSimpleAsyncResult *simple;

	g_return_if_fail (NM_IS_MANAGER (manager));
	priv = NM_MANAGER_GET_PRIVATE (manager);

	simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
	                                    nm_manager_check_connectivity_async);
744 745
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
746
	nmdbus_manager_call_check_connectivity (priv->proxy,
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	                                        cancellable,
	                                        check_connectivity_cb, simple);
}

NMConnectivityState
nm_manager_check_connectivity_finish (NMManager *manager,
                                      GAsyncResult *result,
                                      GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager), nm_manager_check_connectivity_async), NM_CONNECTIVITY_UNKNOWN);

	simple = G_SIMPLE_ASYNC_RESULT (result);

	if (g_simple_async_result_propagate_error (simple, error))
		return NM_CONNECTIVITY_UNKNOWN;
	return (NMConnectivityState) g_simple_async_result_get_op_res_gssize (simple);
}

767
/*****************************************************************************/
768
/* Devices                                                      */
769
/*****************************************************************************/
770 771 772 773 774 775 776 777 778

const GPtrArray *
nm_manager_get_devices (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

	return NM_MANAGER_GET_PRIVATE (manager)->devices;
}

779 780 781 782 783 784 785 786
const GPtrArray *
nm_manager_get_all_devices (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

	return NM_MANAGER_GET_PRIVATE (manager)->all_devices;
}

787 788 789 790
NMDevice *
nm_manager_get_device_by_path (NMManager *manager, const char *object_path)
{
	const GPtrArray *devices;
791
	guint i;
792 793 794 795 796 797 798 799

	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
	g_return_val_if_fail (object_path, NULL);

	devices = nm_manager_get_devices (manager);
	for (i = 0; i < devices->len; i++) {
		NMDevice *candidate = g_ptr_array_index (devices, i);
		if (!strcmp (nm_object_get_path (NM_OBJECT (candidate)), object_path)) {
800
			return candidate;
801 802
		}
	}
803 804 805 806 807 808 809 810 811
	return NULL;
}

static NMCheckpoint *
get_checkpoint_by_path (NMManager *manager, const char *object_path)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
	NMCheckpoint *candidate;
	guint i;
812

813 814 815 816 817
	for (i = 0; i < priv->checkpoints->len; i++) {
		candidate = priv->checkpoints->pdata[i];
		if (nm_streq (nm_object_get_path (NM_OBJECT (candidate)), object_path))
			return candidate;
	}
818
	return NULL;
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
}

NMDevice *
nm_manager_get_device_by_iface (NMManager *manager, const char *iface)
{
	const GPtrArray *devices;
	int i;
	NMDevice *device = NULL;

	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
	g_return_val_if_fail (iface, NULL);

	devices = nm_manager_get_devices (manager);
	for (i = 0; i < devices->len; i++) {
		NMDevice *candidate = g_ptr_array_index (devices, i);
		if (!strcmp (nm_device_get_iface (candidate), iface)) {
			device = candidate;
			break;
		}
	}

	return device;
}

843
/*****************************************************************************/
844
/* Active Connections                                           */
845
/*****************************************************************************/
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

const GPtrArray *
nm_manager_get_active_connections (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

	return NM_MANAGER_GET_PRIVATE (manager)->active_connections;
}

NMActiveConnection *
nm_manager_get_primary_connection (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

	return NM_MANAGER_GET_PRIVATE (manager)->primary_connection;
}

NMActiveConnection *
nm_manager_get_activating_connection (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

	return NM_MANAGER_GET_PRIVATE (manager)->activating_connection;
}

871 872 873 874 875 876
typedef enum {
	ACTIVATE_TYPE_ACTIVATE_CONNECTION,
	ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION,
	ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION2,
} ActivateType;

877
typedef struct {
878
	CList lst;
879 880 881 882 883
	NMManager *manager;
	GSimpleAsyncResult *simple;
	GCancellable *cancellable;
	char *active_path;
	char *new_connection_path;
884 885 886
	GVariant *add_and_activate_output;
	gulong cancelled_id;
	ActivateType activate_type;
887 888
} ActivateInfo;

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
_NMActivateResult *
_nm_activate_result_new (NMActiveConnection *active,
                         GVariant *add_and_activate_output)
{
	_NMActivateResult *r;

	nm_assert (!add_and_activate_output || g_variant_is_of_type (add_and_activate_output, G_VARIANT_TYPE ("a{sv}")));
	nm_assert (!add_and_activate_output || !g_variant_is_floating (add_and_activate_output));

	r = g_slice_new (_NMActivateResult);
	*r = (_NMActivateResult) {
		.active                  = g_object_ref (active),
		.add_and_activate_output = nm_g_variant_ref (add_and_activate_output),
	};
	return r;
}

void
_nm_activate_result_free (_NMActivateResult *result)
{
	g_object_unref (result->active);
	nm_g_variant_unref (result->add_and_activate_output);
	g_slice_free (_NMActivateResult, result);
}

914 915 916 917 918
static void
activate_info_complete (ActivateInfo *info,
                        NMActiveConnection *active,
                        GError *error)
{
919 920 921 922
	nm_clear_g_signal_handler (info->cancellable, &info->cancelled_id);

	c_list_unlink_stale (&info->lst);

923 924 925 926 927 928
	if (active) {
		g_simple_async_result_set_op_res_gpointer (info->simple,
		                                           _nm_activate_result_new (active,
		                                                                    info->add_and_activate_output),
		                                           (GDestroyNotify) _nm_activate_result_free);
	} else
929
		g_simple_async_result_set_from_error (info->simple, error);
930

931 932
	g_simple_async_result_complete (info->simple);

933
	nm_g_variant_unref (info->add_and_activate_output);
934 935 936
	g_free (info->active_path);
	g_free (info->new_connection_path);
	g_object_unref (info->simple);
937
	nm_g_object_unref (info->cancellable);
938 939 940
	g_slice_free (ActivateInfo, info);
}

941 942
static NMActiveConnection *
find_active_connection_by_path (NMManager *self, const char *ac_path)
943 944 945 946
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
	int i;

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	for (i = 0; i < priv->active_connections->len; i++) {
		NMActiveConnection *candidate = g_ptr_array_index (priv->active_connections, i);
		const char *candidate_path = nm_object_get_path (NM_OBJECT (candidate));

		if (g_strcmp0 (ac_path, candidate_path) == 0)
			return candidate;
	}

	return NULL;
}

static void
recheck_pending_activations (NMManager *self)
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self);
962
	CList *iter, *safe;
963 964 965
	NMActiveConnection *candidate;
	const GPtrArray *devices;
	NMDevice *device;
966 967 968 969
	GDBusObjectManager *object_manager = NULL;
	GError *error;

	object_manager = _nm_object_get_dbus_object_manager (NM_OBJECT (self));
970

971 972 973 974
	/* For each pending activation, look for an active connection that has the
	 * pending activation's object path, where the active connection and its
	 * device have both updated their properties to point to each other, and
	 * call the pending connection's callback.
975
	 */
976 977
	c_list_for_each_safe (iter, safe, &priv->pending_activations) {
		ActivateInfo *info = c_list_entry (iter, ActivateInfo, lst);
978
		gs_unref_object GDBusObject *dbus_obj = NULL;
979

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
		if (!info->active_path)
			continue;

		/* Check that the object manager still knows about the object.
		 * It could be that it vanished before we even learned its name. */
		dbus_obj = g_dbus_object_manager_get_object (object_manager, info->active_path);
		if (!dbus_obj) {
			error = g_error_new_literal (NM_CLIENT_ERROR,
			                             NM_CLIENT_ERROR_OBJECT_CREATION_FAILED,
			                             _("Active connection removed before it was initialized"));
			activate_info_complete (info, NULL, error);
			g_clear_error (&error);
			break;
		}

995 996 997
		candidate = find_active_connection_by_path (self, info->active_path);
		if (!candidate)
			continue;
998

999 1000 1001 1002
		/* Check that the AC and device are both ready */
		devices = nm_active_connection_get_devices (candidate);
		if (devices->len == 0)
			continue;
1003 1004 1005 1006 1007 1008

		if (!NM_IS_VPN_CONNECTION (candidate)) {
			device = devices->pdata[0];
			if (nm_device_get_active_connection (device) != candidate)
				continue;
		}
1009

1010 1011
		activate_info_complete (info, candidate, NULL);
		break;
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	}
}

static void
activation_cancelled (GCancellable *cancellable,
                      gpointer user_data)
{
	ActivateInfo *info = user_data;
	GError *error = NULL;

	if (!g_cancellable_set_error_if_cancelled (cancellable, &error))
		return;

	activate_info_complete (info, NULL, error);
	g_clear_error (&error);
}

static void
activate_cb (GObject *object,
             GAsyncResult *result,
             gpointer user_data)
{
	ActivateInfo *info = user_data;
	GError *error = NULL;

	if (nmdbus_manager_call_activate_connection_finish (NMDBUS_MANAGER (object),
	                                                    &info->active_path,
	                                                    result, &error)) {
		if (info->cancellable) {
			info->cancelled_id = g_signal_connect (info->cancellable, "cancelled",
			                                       G_CALLBACK (activation_cancelled), info);
		}

1045
		recheck_pending_activations (info->manager);
1046
	} else {
1047
		g_dbus_error_strip_remote_error (error);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		activate_info_complete (info, NULL, error);
		g_clear_error (&error);
	}
}

void
nm_manager_activate_connection_async (NMManager *manager,
                                      NMConnection *connection,
                                      NMDevice *device,
                                      const char *specific_object,
                                      GCancellable *cancellable,
                                      GAsyncReadyCallback callback,
                                      gpointer user_data)
{
	NMManagerPrivate *priv;
	ActivateInfo *info;

	g_return_if_fail (NM_IS_MANAGER (manager));
	if (device)
		g_return_if_fail (NM_IS_DEVICE (device));
	if (connection)
		g_return_if_fail (NM_IS_CONNECTION (connection));

1071 1072
	priv = NM_MANAGER_GET_PRIVATE (manager);

1073
	info = g_slice_new0 (ActivateInfo);
1074
	info->activate_type = ACTIVATE_TYPE_ACTIVATE_CONNECTION;
1075 1076 1077
	info->manager = manager;
	info->simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
	                                          nm_manager_activate_connection_async);
1078 1079
	if (cancellable)
		g_simple_async_result_set_check_cancellable (info->simple, cancellable);
1080 1081
	info->cancellable = cancellable ? g_object_ref (cancellable) : NULL;

1082
	c_list_link_tail (&priv->pending_activations, &info->lst);
1083

1084
	nmdbus_manager_call_activate_connection (priv->proxy,
1085 1086
	                                         connection ? nm_connection_get_path (connection) : "/",
	                                         device ? nm_object_get_path (NM_OBJECT (device)) : "/",
1087
	                                         specific_object ?: "/",
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
	                                         cancellable,
	                                         activate_cb, info);
}

NMActiveConnection *
nm_manager_activate_connection_finish (NMManager *manager,
                                       GAsyncResult *result,
                                       GError **error)
{
	GSimpleAsyncResult *simple;
1098
	_NMActivateResult *r;
1099 1100 1101 1102 1103 1104

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager), nm_manager_activate_connection_async), NULL);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return NULL;
1105 1106 1107

	r = g_simple_async_result_get_op_res_gpointer (simple);
	return g_object_ref (r->active);
1108 1109 1110 1111 1112 1113 1114 1115
}

static void
add_activate_cb (GObject *object,
                 GAsyncResult *result,
                 gpointer user_data)
{
	ActivateInfo *info = user_data;
1116
	gs_free_error GError *error = NULL;
1117 1118 1119 1120 1121 1122 1123 1124
	gboolean success;

	if (info->activate_type == ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION) {
		success = nmdbus_manager_call_add_and_activate_connection_finish (NMDBUS_MANAGER (object),
		                                                                  NULL,
		                                                                  &info->active_path,
		                                                                  result,
		                                                                  &error);
1125
	} else {
1126 1127 1128 1129 1130 1131 1132 1133
		success = nmdbus_manager_call_add_and_activate_connection2_finish (NMDBUS_MANAGER (object),
		                                                                   NULL,
		                                                                   &info->active_path,
		                                                                   &info->add_and_activate_output,
		                                                                   result,
		                                                                   &error);
	}
	if (!success) {
1134
		g_dbus_error_strip_remote_error (error);
1135
		activate_info_complete (info, NULL, error);
1136 1137 1138 1139 1140 1141
		return;
	}

	if (info->cancellable) {
		info->cancelled_id = g_signal_connect (info->cancellable, "cancelled",
		                                       G_CALLBACK (activation_cancelled), info);
1142
	}
1143 1144

	recheck_pending_activations (info->manager);
1145 1146 1147 1148 1149 1150 1151
}

void
nm_manager_add_and_activate_connection_async (NMManager *manager,
                                              NMConnection *partial,
                                              NMDevice *device,
                                              const char *specific_object,
1152
                                              GVariant *options,
1153
                                              gboolean force_v2,
1154 1155 1156 1157 1158 1159 1160
                                              GCancellable *cancellable,
                                              GAsyncReadyCallback callback,
                                              gpointer user_data)
{
	NMManagerPrivate *priv;
	GVariant *dict = NULL;
	ActivateInfo *info;
1161
	ActivateType activate_type = ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION;
1162 1163 1164 1165 1166 1167

	g_return_if_fail (NM_IS_MANAGER (manager));
	g_return_if_fail (NM_IS_DEVICE (device));
	if (partial)
		g_return_if_fail (NM_IS_CONNECTION (partial));

1168 1169
	priv = NM_MANAGER_GET_PRIVATE (manager);

1170 1171 1172 1173
	info = g_slice_new0 (ActivateInfo);
	info->manager = manager;
	info->simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
	                                          nm_manager_add_and_activate_connection_async);
1174 1175
	if (cancellable)
		g_simple_async_result_set_check_cancellable (info->simple, cancellable);
1176 1177
	info->cancellable = cancellable ? g_object_ref (cancellable) : NULL;

1178
	c_list_link_tail (&priv->pending_activations, &info->lst);
1179 1180 1181 1182 1183

	if (partial)
		dict = nm_connection_to_dbus (partial, NM_CONNECTION_SERIALIZE_ALL);
	if (!dict)
		dict = g_variant_new_array (G_VARIANT_TYPE ("{sa{sv}}"), NULL, 0);
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	if (force_v2) {
		if (!options)
			options = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
		activate_type = ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION2;
	} else {
		if (options) {
			if (g_variant_n_children (options) > 0)
				activate_type = ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION2;
			else
				nm_g_variant_unref_floating (options);
		}
	}

	info->activate_type = activate_type;

	if (activate_type == ACTIVATE_TYPE_ADD_AND_ACTIVATE_CONNECTION2) {
		nmdbus_manager_call_add_and_activate_connection2 (priv->proxy,
		                                                  dict,
		                                                  nm_object_get_path (NM_OBJECT (device)),
		                                                  specific_object ?: "/",
		                                                  options,
		                                                  cancellable,
		                                                  add_activate_cb,
		                                                  info);
	} else {
		nmdbus_manager_call_add_and_activate_connection (priv->proxy,
		                                                  dict,
		                                                  nm_object_get_path (NM_OBJECT (device)),
		                                                  specific_object ?: "/",
		                                                  cancellable,
		                                                  add_activate_cb,
		                                                  info);
	}
1217 1218 1219 1220 1221
}

NMActiveConnection *
nm_manager_add_and_activate_connection_finish (NMManager *manager,
                                               GAsyncResult *result,
1222
                                               GVariant **out_result,
1223 1224 1225
                                               GError **error)
{
	GSimpleAsyncResult *simple;
1226
	_NMActivateResult *r;
1227 1228 1229 1230

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager), nm_manager_add_and_activate_connection_async), NULL);

	simple = G_SIMPLE_ASYNC_RESULT (result);
1231 1232
	if (g_simple_async_result_propagate_error (simple, error)) {
		NM_SET_OUT (out_result, NULL);
1233
		return NULL;
1234 1235 1236 1237 1238
	}

	r = g_simple_async_result_get_op_res_gpointer (simple);
	NM_SET_OUT (out_result, nm_g_variant_ref (r->add_and_activate_output));
	return g_object_ref (r->active);
1239 1240 1241
}

static void
1242
device_ac_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
1243
{
1244 1245 1246 1247 1248 1249 1250 1251
	NMManager *self = user_data;

	recheck_pending_activations (self);
}

static void
device_added (NMManager *self, NMDevice *device)
{
1252 1253
	g_signal_connect_object (device, "notify::" NM_DEVICE_ACTIVE_CONNECTION,
	                         G_CALLBACK (device_ac_changed), self, 0);
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
}

static void
device_removed (NMManager *self, NMDevice *device)
{
	g_signal_handlers_disconnect_by_func (device, G_CALLBACK (device_ac_changed), self);
}

static void
ac_devices_changed (GObject *object, GParamSpec *pspec, gpointer user_data)
{
	NMManager *self = user_data;

	recheck_pending_activations (self);
}

static void
active_connection_added (NMManager *self, NMActiveConnection *ac)
{
1273 1274
	g_signal_connect_object (ac, "notify::" NM_ACTIVE_CONNECTION_DEVICES,
	                         G_CALLBACK (ac_devices_changed), self, 0);
1275 1276 1277 1278 1279 1280 1281
	recheck_pending_activations (self);
}

static void
active_connection_removed (NMManager *self, NMActiveConnection *ac)
{
	g_signal_handlers_disconnect_by_func (ac, G_CALLBACK (ac_devices_changed), self);
1282
	recheck_pending_activations (self);
1283 1284
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
static void
checkpoint_added (NMManager *manager, NMCheckpoint *checkpoint)
{
	CheckpointInfo *info;

	info = find_checkpoint_info (manager, nm_object_get_path (NM_OBJECT (checkpoint)));
	if (info)
		checkpoint_info_complete (manager, info, checkpoint, NULL);
}

1295 1296 1297 1298 1299 1300 1301
gboolean
nm_manager_deactivate_connection (NMManager *manager,
                                  NMActiveConnection *active,
                                  GCancellable *cancellable,
                                  GError **error)
{
	const char *path;
1302
	gboolean ret;
1303 1304 1305 1306 1307

	g_return_val_if_fail (NM_IS_MANAGER (manager), FALSE);
	g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (active), FALSE);

	path = nm_object_get_path (NM_OBJECT (active));
1308
	ret = nmdbus_manager_call_deactivate_connection_sync (NM_MANAGER_GET_PRIVATE (manager)->proxy,
1309 1310 1311 1312 1313
	                                                      path,
	                                                      cancellable, error);
	if (error && *error)
		g_dbus_error_strip_remote_error (*error);
	return ret;
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
}

static void
deactivated_cb (GObject *object,
                GAsyncResult *result,
                gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
	GError *error = NULL;

	if (nmdbus_manager_call_deactivate_connection_finish (NMDBUS_MANAGER (object),
	                                                      result, &error))
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
1327 1328
	else {
		g_dbus_error_strip_remote_error (error);
1329
		g_simple_async_result_take_error (simple, error);
1330
	}
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}

void
nm_manager_deactivate_connection_async (NMManager *manager,
                                        NMActiveConnection *active,
                                        GCancellable *cancellable,
                                        GAsyncReadyCallback callback,
                                        gpointer user_data)
{
	const char *path;
	GSimpleAsyncResult *simple;

	g_return_if_fail (NM_IS_MANAGER (manager));
	g_return_if_fail (NM_IS_ACTIVE_CONNECTION (active));

	simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
	                                    nm_manager_deactivate_connection_async);
1350 1351
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
1352 1353

	path = nm_object_get_path (NM_OBJECT (active));
1354
	nmdbus_manager_call_deactivate_connection (NM_MANAGER_GET_PRIVATE (manager)->proxy,
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	                                           path,
	                                           cancellable,
	                                           deactivated_cb, simple);
}

gboolean
nm_manager_deactivate_connection_finish (NMManager *manager,
                                         GAsyncResult *result,
                                         GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager), nm_manager_deactivate_connection_async), FALSE);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return FALSE;
	else
		return g_simple_async_result_get_op_res_gboolean (simple);
}

1376
/*****************************************************************************/
1377 1378

static void
1379
free_active_connections (NMManager *manager)
1380 1381 1382 1383 1384 1385 1386
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
	int i;

	if (!priv->active_connections)
		return;

1387 1388 1389 1390 1391
	/* Break circular refs */
	for (i = 0; i < priv->active_connections->len; i++)
		g_object_run_dispose (G_OBJECT (priv->active_connections->pdata[i]));
	g_ptr_array_unref (priv->active_connections);
	priv->active_connections = NULL;
1392 1393
}

1394
/*****************************************************************************/
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
static const char **
get_device_paths (const GPtrArray *devices)
{
	const char **array;
	guint i;

	array = g_new (const char *, devices->len + 1);
	for (i = 0; i < devices->len; i++)
		array[i] = nm_object_get_path (NM_OBJECT (devices->pdata[i]));

	array[i] = NULL;

	return array;
}

const GPtrArray *
nm_manager_get_checkpoints (NMManager *manager)
{
	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);

	return NM_MANAGER_GET_PRIVATE (manager)->checkpoints;
}

static void
checkpoint_created_cb (GObject *object,
                       GAsyncResult *result,
                       gpointer user_data)
{
	CheckpointInfo *info = user_data;
1425 1426 1427
	NMManager *self = info->manager;
	gs_free_error GError *error = NULL;
	NMCheckpoint *checkpoint;
1428 1429 1430 1431 1432

	nmdbus_manager_call_checkpoint_create_finish (NMDBUS_MANAGER (object),
	                                              &info->path, result, &error);
	if (error) {
		g_dbus_error_strip_remote_error (error);
1433 1434
		checkpoint_info_complete (self, info, NULL, error);
		return;
1435
	}
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445

	checkpoint = get_checkpoint_by_path (self, info->path);
	if (!checkpoint) {
		/* this is really problematic. The async request returned, but
		 * we don't yet have a visible (fully initialized) NMCheckpoint instance
		 * to return. Wait longer for it to appear. However, it's ugly. */
		return;
	}

	checkpoint_info_complete (self, info, checkpoint, NULL);
1446 1447 1448
}

void
Thomas Haller's avatar
Thomas Haller committed
1449 1450 1451 1452 1453 1454 1455
nm_manager_checkpoint_create (NMManager *manager,
                              const GPtrArray *devices,
                              guint32 rollback_timeout,
                              NMCheckpointCreateFlags flags,
                              GCancellable *cancellable,
                              GAsyncReadyCallback callback,
                              gpointer user_data)
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
{
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
	gs_free const char **paths = NULL;
	CheckpointInfo *info;

	g_return_if_fail (NM_IS_MANAGER (manager));

	info = g_slice_new0 (CheckpointInfo);
	info->manager = manager;
	info->simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
Thomas Haller's avatar
Thomas Haller committed
1466
	                                          nm_manager_checkpoint_create);
1467 1468
	if (cancellable)
		g_simple_async_result_set_check_cancellable (info->simple, cancellable);
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
	paths = get_device_paths (devices);
	nmdbus_manager_call_checkpoint_create (NM_MANAGER_GET_PRIVATE (manager)->proxy,
	                                       paths,
	                                       rollback_timeout,
	                                       flags,
	                                       cancellable,
	                                       checkpoint_created_cb,
	                                       info);
	priv->added_checkpoints = g_slist_append (priv->added_checkpoints, info);
}

NMCheckpoint *
nm_manager_checkpoint_create_finish (NMManager *manager,
                                     GAsyncResult *result,
                                     GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (NM_IS_MANAGER (manager), NULL);
	g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return NULL;
	else
		return g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
}

static void
checkpoint_destroy_cb (GObject *object,
                       GAsyncResult *result,
                       gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
	GError *error = NULL;

	if (nmdbus_manager_call_checkpoint_destroy_finish (NMDBUS_MANAGER (object),
	                                                   result, &error))
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
	else {
		g_dbus_error_strip_remote_error (error);
		g_simple_async_result_take_error (simple, error);
	}
	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}

void
Thomas Haller's avatar
Thomas Haller committed
1517 1518 1519 1520 1521
nm_manager_checkpoint_destroy (NMManager *manager,
                               const char *checkpoint_path,
                               GCancellable *cancellable,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
1522 1523 1524 1525
{
	GSimpleAsyncResult *simple;

	g_return_if_fail (NM_IS_MANAGER (manager));
Thomas Haller's avatar
Thomas Haller committed
1526
	g_return_if_fail (checkpoint_path && checkpoint_path[0] == '/');
1527 1528

	simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
Thomas Haller's avatar
Thomas Haller committed
1529
	                                    nm_manager_checkpoint_destroy);
1530 1531
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
1532 1533

	nmdbus_manager_call_checkpoint_destroy (NM_MANAGER_GET_PRIVATE (manager)->proxy,
Thomas Haller's avatar
Thomas Haller committed
1534
	                                        checkpoint_path,
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	                                        cancellable,
	                                        checkpoint_destroy_cb, simple);
}

gboolean
nm_manager_checkpoint_destroy_finish (NMManager *manager,
                                      GAsyncResult *result,
                                      GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager),
Thomas Haller's avatar
Thomas Haller committed
1547
	                                                      nm_manager_checkpoint_destroy),
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	                      FALSE);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return FALSE;
	else
		return g_simple_async_result_get_op_res_gboolean (simple);
}

static void
checkpoint_rollback_cb (GObject *object,
                        GAsyncResult *result,
                        gpointer user_data)
{
	GSimpleAsyncResult *simple = user_data;
	GError *error = NULL;
	GVariant *variant = NULL;
	GVariantIter iter;
	GHashTable *hash;
	const char *path;
	guint r;

	if (nmdbus_manager_call_checkpoint_rollback_finish (NMDBUS_MANAGER (object),
	                                                    &variant,
	                                                    result,
	                                                    &error)) {
1574
		hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, NULL);
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
		g_variant_iter_init (&iter, variant);
		while (g_variant_iter_next (&iter, "{&su}", &path, &r))
			g_hash_table_insert (hash, g_strdup (path), GUINT_TO_POINTER (r));
		g_simple_async_result_set_op_res_gpointer (simple, hash, (GDestroyNotify) g_hash_table_unref);
	} else {
		g_dbus_error_strip_remote_error (error);
		g_simple_async_result_take_error (simple, error);
	}
	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}

void
Thomas Haller's avatar
Thomas Haller committed
1588 1589 1590 1591 1592
nm_manager_checkpoint_rollback (NMManager *manager,
                                const char *checkpoint_path,
                                GCancellable *cancellable,
                                GAsyncReadyCallback callback,
                                gpointer user_data)
1593 1594 1595 1596
{
	GSimpleAsyncResult *simple;

	g_return_if_fail (NM_IS_MANAGER (manager));
Thomas Haller's avatar
Thomas Haller committed
1597
	g_return_if_fail (checkpoint_path && checkpoint_path[0] == '/');
1598 1599

	simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
Thomas Haller's avatar
Thomas Haller committed
1600
	                                    nm_manager_checkpoint_rollback);
1601 1602
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);
1603 1604

	nmdbus_manager_call_checkpoint_rollback (NM_MANAGER_GET_PRIVATE (manager)->proxy,
Thomas Haller's avatar
Thomas Haller committed
1605
	                                         checkpoint_path,
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
	                                         cancellable,
	                                         checkpoint_rollback_cb, simple);
}

GHashTable *
nm_manager_checkpoint_rollback_finish (NMManager *manager,
                                       GAsyncResult *result,
                                       GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager),
Thomas Haller's avatar
Thomas Haller committed
1618
	                                                      nm_manager_checkpoint_rollback),
1619 1620 1621 1622 1623 1624 1625 1626 1627
	                      NULL);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	if (g_simple_async_result_propagate_error (simple, error))
		return NULL;
	else
		return g_simple_async_result_get_op_res_gpointer (simple);
}

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
static void
checkpoint_adjust_rollback_timeout_cb (GObject *object,
                                       GAsyncResult *result,
                                       gpointer user_data)
{
	gs_unref_object GSimpleAsyncResult *simple = user_data;
	GError *error = NULL;

	if (nmdbus_manager_call_checkpoint_adjust_rollback_timeout_finish (NMDBUS_MANAGER (object),
	                                                                   result,
	                                                                   &error))
		g_simple_async_result_set_op_res_gboolean (simple, TRUE);
	else {
		g_dbus_error_strip_remote_error (error);
		g_simple_async_result_take_error (simple, error);
	}
	g_simple_async_result_complete (simple);
}

void
nm_manager_checkpoint_adjust_rollback_timeout (NMManager *manager,
                                               const char *checkpoint_path,
                                               guint32 add_timeout,
                                               GCancellable *cancellable,
                                               GAsyncReadyCallback callback,
                                               gpointer user_data)
{
	GSimpleAsyncResult *simple;

	g_return_if_fail (NM_IS_MANAGER (manager));
	g_return_if_fail (checkpoint_path && checkpoint_path[0] == '/');

	simple = g_simple_async_result_new (G_OBJECT (manager), callback, user_data,
	                                    nm_manager_checkpoint_adjust_rollback_timeout);
	if (cancellable)
		g_simple_async_result_set_check_cancellable (simple, cancellable);

	nmdbus_manager_call_checkpoint_adjust_rollback_timeout (NM_MANAGER_GET_PRIVATE (manager)->proxy,
	                                                        checkpoint_path,
	                                                        add_timeout,
	                                                        cancellable,
	                                                        checkpoint_adjust_rollback_timeout_cb,
	                                                        simple);
}

gboolean
nm_manager_checkpoint_adjust_rollback_timeout_finish (NMManager *manager,
                                                      GAsyncResult *result,
                                                      GError **error)
{
	GSimpleAsyncResult *simple;

	g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (manager),
	                                                      nm_manager_checkpoint_adjust_rollback_timeout),
	                      FALSE);

	simple = G_SIMPLE_ASYNC_RESULT (result);
	return !g_simple_async_result_propagate_error (simple, error);
}

1688 1689
/*****************************************************************************/

1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
static void
constructed (GObject *object)
{
	G_OBJECT_CLASS (nm_manager_parent_class)->constructed (object);

	g_signal_connect (object, "notify::" NM_MANAGER_WIRELESS_ENABLED,
	                  G_CALLBACK (wireless_enabled_cb), NULL);
}

static gboolean
init_sync (GInitable *initable, GCancellable *cancellable, GError **error)
{
	NMManager *manager = NM_MANAGER (initable);
1703
	GError *local_error = NULL;
1704

1705 1706 1707 1708
	if (!nm_manager_parent_initable_iface->init (initable, cancellable, error)) {
		/* Never happens. */
		g_return_val_if_reached (FALSE);
	}
1709

1710 1711 1712 1713
	if (!get_permissions_sync (manager, &local_error)) {
		g_warning ("Unable to get permissions: %s\n", local_error->message);
		g_error_free (local_error);
	}
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735

	return TRUE;
}

typedef struct {
	NMManager *manager;
	GCancellable *cancellable;
	GSimpleAsyncResult *result;
} NMManagerInitData;

static void
init_async_complete (NMManagerInitData *init_data)
{
	g_simple_async_result_complete (init_data->result);
	g_object_unref (init_data->result);
	g_clear_object (&init_data->cancellable);
	g_slice_free (NMManagerInitData, init_data);
}

static void
init_async_got_permissions (GObject *object, GAsyncResult *result, gpointer user_data)
{
1736
	NMManager *manager = user_data;
1737 1738 1739 1740 1741
	GVariant *permissions;

	if (nmdbus_manager_call_get_permissions_finish (NMDBUS_MANAGER (object),
	                                                &permissions,
	                                                result, NULL)) {
1742
		update_permissions (manager, permissions);
1743 1744
		g_variant_unref (permissions);
	} else
1745
		update_permissions (manager, NULL);
1746

1747
	g_object_unref (manager);
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
}

static void
init_async_parent_inited (GObject *source, GAsyncResult *result, gpointer user_data)
{
	NMManagerInitData *init_data = user_data;
	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (init_data->manager);
	GError *error = NULL;

	if (!nm_manager_parent_async_initable_iface->init_finish (G_ASYNC_INITABLE (source), result, &error)) {
		g_simple_async_result_take_error (init_data->result, error);
		init_async_complete (init_data);
		return;
	}