manager.c 23.5 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
27
#include <ctype.h>
28 29 30 31 32 33 34

#include <gdbus.h>

#include <connman/agent.h>

#include "connman.h"

35 36 37
#include <gsupplicant_util.h>
#include <gutil_misc.h>

38 39 40
#define SET_OFFLINE_MODE_ACCESS     CONNMAN_ACCESS_ALLOW
#define CREATE_SERVICE_ACCESS       CONNMAN_ACCESS_ALLOW

41 42
static bool connman_state_idle;
static dbus_bool_t sessionmode;
43 44 45 46 47 48 49 50 51 52
struct connman_access_manager_policy *manager_access_policy;

static struct connman_access_manager_policy *get_manager_access_policy()
{
	/* We can't initialize this variable in __connman_manager_init
	 * because __connman_manager_init runs before sailfish access
	 * plugin (or any other plugin) is loaded */
	if (!manager_access_policy) {
		/* Use the default policy */
		manager_access_policy =
53
			__connman_access_manager_policy_create(NULL);
54 55 56
	}
	return manager_access_policy;
}
57 58 59 60 61 62

static DBusMessage *get_properties(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	DBusMessage *reply;
	DBusMessageIter array, dict;
63
	dbus_bool_t offlinemode;
64
	dbus_uint32_t uint32_value;
65 66 67 68 69
	const char *str;

	DBG("conn %p", conn);

	reply = dbus_message_new_method_return(msg);
70
	if (!reply)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		return NULL;

	dbus_message_iter_init_append(reply, &array);

	connman_dbus_dict_open(&array, &dict);

	str = __connman_notifier_get_state();
	connman_dbus_dict_append_basic(&dict, "State",
						DBUS_TYPE_STRING, &str);

	offlinemode = __connman_technology_get_offlinemode();
	connman_dbus_dict_append_basic(&dict, "OfflineMode",
					DBUS_TYPE_BOOLEAN, &offlinemode);

	connman_dbus_dict_append_basic(&dict, "SessionMode",
					DBUS_TYPE_BOOLEAN,
					&sessionmode);

89 90 91 92 93
	uint32_value = connman_timeout_input_request();
	connman_dbus_dict_append_basic(&dict, "InputRequestTimeout",
					DBUS_TYPE_UINT32,
					&uint32_value);

94 95 96 97 98 99 100 101 102 103 104 105 106 107
	connman_dbus_dict_close(&array, &dict);

	return reply;
}

static DBusMessage *set_property(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	DBusMessageIter iter, value;
	const char *name;
	int type;

	DBG("conn %p", conn);

108
	if (!dbus_message_iter_init(msg, &iter))
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		return __connman_error_invalid_arguments(msg);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		return __connman_error_invalid_arguments(msg);

	dbus_message_iter_get_basic(&iter, &name);
	dbus_message_iter_next(&iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
		return __connman_error_invalid_arguments(msg);

	dbus_message_iter_recurse(&iter, &value);

	type = dbus_message_iter_get_arg_type(&value);

124
	if (g_str_equal(name, "OfflineMode")) {
125
		const char *sender = dbus_message_get_sender(msg);
126
		dbus_bool_t offlinemode;
127 128 129 130

		if (type != DBUS_TYPE_BOOLEAN)
			return __connman_error_invalid_arguments(msg);

131
		if (__connman_access_manager_policy_check(
132 133 134 135 136 137 138 139
				get_manager_access_policy(),
				CONNMAN_ACCESS_MANAGER_SET_PROPERTY,
				name, sender, SET_OFFLINE_MODE_ACCESS) !=
						CONNMAN_ACCESS_ALLOW) {
			DBG("access denied for %s", sender);
			return __connman_error_permission_denied(msg);
		}

140 141 142
		dbus_message_iter_get_basic(&value, &offlinemode);

		__connman_technology_set_offlinemode(offlinemode);
143
	} else if (g_str_equal(name, "SessionMode")) {
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

		if (type != DBUS_TYPE_BOOLEAN)
			return __connman_error_invalid_arguments(msg);

		dbus_message_iter_get_basic(&value, &sessionmode);

	} else
		return __connman_error_invalid_property(msg);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static void append_technology_structs(DBusMessageIter *iter, void *user_data)
{
	__connman_technology_list_struct(iter);
}

static DBusMessage *get_technologies(DBusConnection *conn,
		DBusMessage *msg, void *data)
{
	DBusMessage *reply;

	DBG("");

	reply = dbus_message_new_method_return(msg);
169
	if (!reply)
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
		return NULL;

	__connman_dbus_append_objpath_dict_array(reply,
			append_technology_structs, NULL);

	return reply;
}

static DBusMessage *remove_provider(DBusConnection *conn,
				    DBusMessage *msg, void *data)
{
	const char *path;
	int err;

	DBG("conn %p", conn);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
							DBUS_TYPE_INVALID);

	err = __connman_provider_remove_by_path(path);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static DBusConnection *connection = NULL;

198
static void idle_state(bool idle)
199 200 201 202 203 204
{

	DBG("idle %d", idle);

	connman_state_idle = idle;

205
	if (!connman_state_idle)
206 207 208 209 210 211 212 213 214 215
		return;
}

static struct connman_notifier technology_notifier = {
	.name		= "manager",
	.priority	= CONNMAN_NOTIFIER_PRIORITY_HIGH,
	.idle_state	= idle_state,
};

static void append_service_structs(DBusMessageIter *iter, void *user_data)
Slava Monich's avatar
Slava Monich committed
216
{
217 218 219 220 221 222 223 224 225
	__connman_service_list_struct(iter);
}

static DBusMessage *get_services(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	DBusMessage *reply;

	reply = dbus_message_new_method_return(msg);
226
	if (!reply)
227 228 229 230 231 232 233 234
		return NULL;

	__connman_dbus_append_objpath_dict_array(reply,
			append_service_structs, NULL);

	return reply;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static void append_peer_structs(DBusMessageIter *iter, void *user_data)
{
	__connman_peer_list_struct(iter);
}

static DBusMessage *get_peers(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	DBusMessage *reply;

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

	__connman_dbus_append_objpath_dict_array(reply,
250
					append_peer_structs, NULL);
251 252 253
	return reply;
}

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 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
static DBusMessage *connect_provider(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	int err;

	DBG("conn %p", conn);

	err = __connman_provider_create_and_connect(msg);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return NULL;
}

static DBusMessage *register_agent(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	const char *sender, *path;
	int err;

	DBG("conn %p", conn);

	sender = dbus_message_get_sender(msg);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
							DBUS_TYPE_INVALID);

	err = connman_agent_register(sender, path);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static DBusMessage *unregister_agent(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	const char *sender, *path;
	int err;

	DBG("conn %p", conn);

	sender = dbus_message_get_sender(msg);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
							DBUS_TYPE_INVALID);

	err = connman_agent_unregister(sender, path);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static DBusMessage *register_counter(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	const char *sender, *path;
	unsigned int accuracy, period;
	int err;

	DBG("conn %p", conn);

	sender = dbus_message_get_sender(msg);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
						DBUS_TYPE_UINT32, &accuracy,
						DBUS_TYPE_UINT32, &period,
							DBUS_TYPE_INVALID);

	/* FIXME: add handling of accuracy parameter */

	err = __connman_counter_register(sender, path, period);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static DBusMessage *unregister_counter(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	const char *sender, *path;
	int err;

	DBG("conn %p", conn);

	sender = dbus_message_get_sender(msg);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
							DBUS_TYPE_INVALID);

	err = __connman_counter_unregister(sender, path);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
static DBusMessage *reset_counters(DBusConnection *conn, DBusMessage *msg,
								void *data)
{
	const char *type = NULL;

	DBG("conn %p", conn);
	dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &type,
							DBUS_TYPE_INVALID);

	__connman_service_counter_reset_all(type);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

/* This key is checked only if the first CreateService argument is empty */
#define SERVICE_KEY_TYPE       "Type"

/* These should match the ones defined in service.c */
#define SERVICE_KEY_NAME       "Name"
#define SERVICE_KEY_SSID       "SSID"
#define SERVICE_KEY_SECURITY   "Security"

static DBusMessage *create_service(DBusConnection *conn, DBusMessage *msg,
								void *data)
Lorn Potter's avatar
Lorn Potter committed
377
{
378 379 380 381
	DBusMessage *reply;
	DBusMessageIter iter, array, entry;
	enum connman_service_type service_type;
	GKeyFile *settings;
382
	const char *sender = dbus_message_get_sender(msg);
383
	const char *device_ident, *network_ident, *type = NULL, *name = NULL;
384 385
	const char *ptr, *ssid = NULL, *security = NULL, *settings_type = NULL;
	char *ident, *p, *tmp_name = NULL, *tmp_ssid = NULL;
386 387 388 389 390 391 392 393 394 395

	/* N.B. The caller has checked the signature */
	dbus_message_iter_init(msg, &iter);
	dbus_message_iter_get_basic(&iter, &type);
	dbus_message_iter_next(&iter);
	dbus_message_iter_get_basic(&iter, &device_ident);
	dbus_message_iter_next(&iter);
	dbus_message_iter_get_basic(&iter, &network_ident);
	dbus_message_iter_next(&iter);

396 397 398 399
	/* Pull some important things from the settings */
	dbus_message_iter_recurse(&iter, &array);
	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
		const char *key = NULL;
400

401 402 403
		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);
404

405 406 407 408 409 410 411 412
		if (!g_strcmp0(key, SERVICE_KEY_TYPE)) {
			dbus_message_iter_get_basic(&entry, &settings_type);
		} else if (!g_strcmp0(key, SERVICE_KEY_NAME)) {
			dbus_message_iter_get_basic(&entry, &name);
		} else if (!g_strcmp0(key, SERVICE_KEY_SSID)) {
			dbus_message_iter_get_basic(&entry, &ssid);
		} else if (!g_strcmp0(key, SERVICE_KEY_SECURITY)) {
			dbus_message_iter_get_basic(&entry, &security);
413
		}
414 415

		dbus_message_iter_next(&array);
416 417
	}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	/* Nullify empty strings to simplify checks */
	if (type && !type[0]) type = NULL;
	if (device_ident && !device_ident[0]) device_ident = NULL;
	if (network_ident && !network_ident[0])network_ident  = NULL;
	if (settings_type && !settings_type[0]) settings_type = NULL;
	if (security && !security[0]) security = NULL;
	if (name && !name[0]) name = NULL;
	if (ssid && !ssid[0]) ssid = NULL;

	/* If the service type is missing, take one from the settings */
	if (!type) {
		type = settings_type;
	} else if (settings_type && strcmp(settings_type, type)) {
		DBG("service type mismatch %s/%s", type, settings_type);
		return __connman_error_invalid_arguments(msg);
	}

	if (type) {
436 437 438 439 440 441 442 443 444 445 446 447
		/* Check the service type (only wifi is supported for now) */
		service_type = __connman_service_string2type(type);
		if (service_type == CONNMAN_SERVICE_TYPE_UNKNOWN) {
			DBG("unknown device type %s", type);
			return __connman_error_invalid_arguments(msg);
		} else if (service_type != CONNMAN_SERVICE_TYPE_WIFI) {
			DBG("unsupported device type %s", type);
			return __connman_error_not_supported(msg);
		}
	} else {
		/* No device type given, assume wifi */
		service_type = CONNMAN_SERVICE_TYPE_WIFI;
448 449 450 451
		type = __connman_service_type2string(service_type);
	}

	/* Check access */
452
	if (__connman_access_manager_policy_check(get_manager_access_policy(),
453 454 455 456
			CONNMAN_ACCESS_MANAGER_CREATE_SERVICE, type, sender,
			CREATE_SERVICE_ACCESS) != CONNMAN_ACCESS_ALLOW) {
		DBG("access denied for %s", sender);
		return __connman_error_permission_denied(msg);
457
	}
Lorn Potter's avatar
Lorn Potter committed
458

459 460 461 462
	/*
	 * If no device identifier is given, assume the first device
	 * of this type.
	 */
463
	if (!device_ident) {
464 465
		struct connman_device *device =
			__connman_device_find_device(service_type);
Lorn Potter's avatar
Lorn Potter committed
466

467 468 469 470 471 472 473 474
		if (!device) {
			DBG("no devices of type %s", type);
			return __connman_error_invalid_arguments(msg);
		}
		device_ident = connman_device_get_ident(device);
	}

	/*
475 476 477 478
	 * Either name or SSID must be provided. If one of them is missing
	 * we generate it from the other (note that SSID doesn't have to
	 * be a valid string but it often is). This is wifi specific stuff
	 * but we don't support creation of services of any other type atm.
479
	 */
480 481 482
	if (ssid && !name) {
		/* Generate the name from SSID */
		GBytes* bytes = gutil_hex2bytes(ssid, -1);
483

484 485 486
		if (bytes) {
			name = tmp_name = gsupplicant_utf8_from_bytes(bytes);
			g_bytes_unref(bytes);
487
		}
488 489 490 491
		if (name) {
			DBG("%s => \"%s\"", ssid, name);
		} else {
			DBG("can't generate service name");
492 493
			return __connman_error_invalid_arguments(msg);
		}
494 495 496
	} else if (!ssid && name) {
		/* Generate SSID from the name */
		GString *buf = g_string_sized_new(strlen(name)*2);
497

498
		for (ptr = name; *ptr; ptr++) {
499
			g_string_append_printf(buf, "%02X", (guchar)*ptr);
500
		}
501 502 503 504 505 506 507
		ssid = tmp_ssid = g_string_free(buf, FALSE);
		DBG("\"%s\" => %s", name, ssid);
	} else if (!ssid && !name) {
		DBG("no name and no ssid, bailing out");
		return __connman_error_invalid_arguments(msg);
	} else {
		/* Validate SSID */
508 509 510 511 512 513 514 515 516 517 518
		for (ptr = ssid; *ptr; ptr++) {
			if (!isxdigit(*ptr)) {
				DBG("invalid ssid %s", ssid);
				return __connman_error_invalid_arguments(msg);
			}
		}

		if ((ptr - ssid) & 1) {
			DBG("invalid ssid length");
			return __connman_error_invalid_arguments(msg);
		}
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	}

	/*
	 * If no network identifier is provided, deduce one from ssid
	 * and security (we have to assume wifi here)
	 */
	if (network_ident) {
		ident = g_strconcat(type, "_", device_ident, "_",
							network_ident, NULL);
	} else {
		if (!security) {
			DBG("missing security");
			g_free(tmp_name);
			g_free(tmp_ssid);
			return __connman_error_invalid_arguments(msg);
		}

		if (__connman_service_string2security(security) ==
					CONNMAN_SERVICE_SECURITY_UNKNOWN) {
			DBG("invalid security %s", security);
			g_free(tmp_name);
			g_free(tmp_ssid);
			return __connman_error_invalid_arguments(msg);
		}
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

		ident = g_strconcat(type, "_", device_ident, "_",
					ssid, "_managed_", security, NULL);
	}

	/* Lowercase the identifier */
	p = g_utf8_strdown(ident, -1);
	if (p) {
		g_free(ident);
		ident = p;
	}

	/* Decode settings */
	settings = g_key_file_new();
	dbus_message_iter_recurse(&iter, &array);
	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
		const char *key, *value;

		dbus_message_iter_recurse(&array, &entry);
		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);
		dbus_message_iter_get_basic(&entry, &value);
		g_key_file_set_string(settings, ident, key, value);
		dbus_message_iter_next(&array);
	}

569 570 571 572 573 574 575
	/*
	 * Make sure both Name and SSID are in there (one of them may have
	 * been auto-generated from the other)
	 */
	g_key_file_set_string(settings, ident, SERVICE_KEY_NAME, name);
	g_key_file_set_string(settings, ident, SERVICE_KEY_SSID, ssid);

576 577 578 579 580 581 582 583
	/* Actually create the service (or update the existing one) */
	DBG("%s \"%s\"", ident, name);
	ptr = __connman_service_create(service_type, ident, settings);
	if (ptr) {
		DBG("%s", ptr);
		reply = g_dbus_create_reply(msg,
				DBUS_TYPE_OBJECT_PATH, &ptr,
				DBUS_TYPE_INVALID);
584
	} else {
585 586
		/* Passing zero to get the generic Failed error */
		reply = __connman_error_failed(msg, 0);
587 588 589
	}

	g_key_file_unref(settings);
590 591
	g_free(tmp_name);
	g_free(tmp_ssid);
592 593
	g_free(ident);
	return reply;
Lorn Potter's avatar
Lorn Potter committed
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 621 622 623 624 625 626 627 628 629 630 631 632 633 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
static DBusMessage *create_session(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	int err;

	DBG("conn %p", conn);

	err = __connman_session_create(msg);
	if (err < 0) {
		if (err == -EINPROGRESS)
			return NULL;

		return __connman_error_failed(msg, -err);
	}

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static DBusMessage *destroy_session(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	int err;

	DBG("conn %p", conn);

	err = __connman_session_destroy(msg);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

static DBusMessage *request_private_network(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	const char *sender;
	int  err;

	DBG("conn %p", conn);

	sender = dbus_message_get_sender(msg);

	err = __connman_private_network_request(msg, sender);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return NULL;
}

static DBusMessage *release_private_network(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	const char *path;
	int err;

	DBG("conn %p", conn);

	dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
							DBUS_TYPE_INVALID);

	err = __connman_private_network_release(path);
	if (err < 0)
		return __connman_error_failed(msg, -err);

	return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}

663 664 665 666 667 668 669 670 671 672
static int parse_peers_service_specs(DBusMessageIter *array,
			const unsigned char **spec, int *spec_len,
			const unsigned char **query, int *query_len,
			int *version)
{
	*spec = *query = NULL;
	*spec_len = *query_len = *version = 0;

	while (dbus_message_iter_get_arg_type(array) ==
							DBUS_TYPE_DICT_ENTRY) {
673
		DBusMessageIter entry, inter, value;
674 675 676 677 678 679
		const char *key;

		dbus_message_iter_recurse(array, &entry);
		dbus_message_iter_get_basic(&entry, &key);

		dbus_message_iter_next(&entry);
680 681

		dbus_message_iter_recurse(&entry, &inter);
682 683

		if  (!g_strcmp0(key, "BonjourResponse")) {
684
			dbus_message_iter_recurse(&inter, &value);
685 686 687
			dbus_message_iter_get_fixed_array(&value,
							spec, spec_len);
		} else if (!g_strcmp0(key, "BonjourQuery")) {
688
			dbus_message_iter_recurse(&inter, &value);
689 690 691
			dbus_message_iter_get_fixed_array(&value,
							query, query_len);
		} else if (!g_strcmp0(key, "UpnpService")) {
692
			dbus_message_iter_get_basic(&inter, spec);
693 694
			*spec_len = strlen((const char *)*spec)+1;
		} else if (!g_strcmp0(key, "UpnpVersion")) {
695
			dbus_message_iter_get_basic(&inter, version);
696 697 698 699 700 701 702
		} else if (!g_strcmp0(key, "WiFiDisplayIEs")) {
			if (*spec || *query)
				return -EINVAL;

			dbus_message_iter_recurse(&inter, &value);
			dbus_message_iter_get_fixed_array(&value,
							spec, spec_len);
703 704 705 706 707 708
		} else
			return -EINVAL;

		dbus_message_iter_next(array);
	}

709 710
	if ((*query && !*spec && !*version) ||
				(!*spec && !*query) || (!*spec && *version))
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		return -EINVAL;

	return 0;
}

static DBusMessage *register_peer_service(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	const unsigned char *spec, *query;
	DBusMessageIter iter, array;
	int spec_len, query_len;
	dbus_bool_t master;
	const char *owner;
	int version;
	int ret;

	DBG("");

	owner = dbus_message_get_sender(msg);

	dbus_message_iter_init(msg, &iter);
	dbus_message_iter_recurse(&iter, &array);

	ret = parse_peers_service_specs(&array, &spec, &spec_len,
						&query, &query_len, &version);
	if (ret)
		goto error;

	dbus_message_iter_next(&iter);
	dbus_message_iter_get_basic(&iter, &master);

	ret = __connman_peer_service_register(owner, msg, spec, spec_len,
743
					query, query_len, version,master);
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	if (!ret)
		return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
	if (ret == -EINPROGRESS)
		return NULL;
error:
	return __connman_error_failed(msg, -ret);
}

static DBusMessage *unregister_peer_service(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	const unsigned char *spec, *query;
	DBusMessageIter iter, array;
	int spec_len, query_len;
	const char *owner;
	int version;
	int ret;

	DBG("");

	owner = dbus_message_get_sender(msg);

	dbus_message_iter_init(msg, &iter);
	dbus_message_iter_recurse(&iter, &array);

	ret = parse_peers_service_specs(&array, &spec, &spec_len,
						&query, &query_len, &version);
	if (ret)
		goto error;

	ret = __connman_peer_service_unregister(owner, spec, spec_len,
						query, query_len, version);
	if (!ret)
		return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
error:
	return __connman_error_failed(msg, -ret);

}

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
static const GDBusMethodTable manager_methods[] = {
	{ GDBUS_METHOD("GetProperties",
			NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
			get_properties) },
	{ GDBUS_ASYNC_METHOD("SetProperty",
			GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
			NULL, set_property) },
	{ GDBUS_METHOD("GetTechnologies",
			NULL, GDBUS_ARGS({ "technologies", "a(oa{sv})" }),
			get_technologies) },
	{ GDBUS_DEPRECATED_METHOD("RemoveProvider",
			GDBUS_ARGS({ "provider", "o" }), NULL,
			remove_provider) },
	{ GDBUS_METHOD("GetServices",
			NULL, GDBUS_ARGS({ "services", "a(oa{sv})" }),
			get_services) },
799 800 801
	{ GDBUS_METHOD("GetPeers",
			NULL, GDBUS_ARGS({ "peers", "a(oa{sv})" }),
			get_peers) },
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	{ GDBUS_DEPRECATED_ASYNC_METHOD("ConnectProvider",
			      GDBUS_ARGS({ "provider", "a{sv}" }),
			      GDBUS_ARGS({ "path", "o" }),
			      connect_provider) },
	{ GDBUS_METHOD("RegisterAgent",
			GDBUS_ARGS({ "path", "o" }), NULL,
			register_agent) },
	{ GDBUS_METHOD("UnregisterAgent",
			GDBUS_ARGS({ "path", "o" }), NULL,
			unregister_agent) },
	{ GDBUS_METHOD("RegisterCounter",
			GDBUS_ARGS({ "path", "o" }, { "accuracy", "u" },
					{ "period", "u" }),
			NULL, register_counter) },
	{ GDBUS_METHOD("UnregisterCounter",
			GDBUS_ARGS({ "path", "o" }), NULL,
			unregister_counter) },
819 820 821 822 823 824 825 826 827 828
	{ GDBUS_METHOD("ResetCounters",
			GDBUS_ARGS({ "type", "s" }), NULL,
			reset_counters) },
	{ GDBUS_METHOD("CreateService",
			GDBUS_ARGS({ "service_type", "s" },
					{ "device_ident", "s" },
					{ "network_ident", "s" },
					{ "settings", "a(ss)" }),
			GDBUS_ARGS({ "service", "o" }),
			create_service) },
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	{ GDBUS_ASYNC_METHOD("CreateSession",
			GDBUS_ARGS({ "settings", "a{sv}" },
						{ "notifier", "o" }),
			GDBUS_ARGS({ "session", "o" }),
			create_session) },
	{ GDBUS_METHOD("DestroySession",
			GDBUS_ARGS({ "session", "o" }), NULL,
			destroy_session) },
	{ GDBUS_ASYNC_METHOD("RequestPrivateNetwork",
			      NULL, GDBUS_ARGS({ "path", "o" },
					       { "settings", "a{sv}" },
					       { "socket", "h" }),
			      request_private_network) },
	{ GDBUS_METHOD("ReleasePrivateNetwork",
			GDBUS_ARGS({ "path", "o" }), NULL,
			release_private_network) },
845 846 847 848 849 850 851
	{ GDBUS_ASYNC_METHOD("RegisterPeerService",
			GDBUS_ARGS({ "specification", "a{sv}" },
				   { "master", "b" }), NULL,
			register_peer_service) },
	{ GDBUS_METHOD("UnregisterPeerService",
			GDBUS_ARGS({ "specification", "a{sv}" }), NULL,
			unregister_peer_service) },
852 853 854 855 856 857 858 859 860 861 862 863 864 865
	{ },
};

static const GDBusSignalTable manager_signals[] = {
	{ GDBUS_SIGNAL("PropertyChanged",
			GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
	{ GDBUS_SIGNAL("TechnologyAdded",
			GDBUS_ARGS({ "path", "o" },
				   { "properties", "a{sv}" })) },
	{ GDBUS_SIGNAL("TechnologyRemoved",
			GDBUS_ARGS({ "path", "o" })) },
	{ GDBUS_SIGNAL("ServicesChanged",
			GDBUS_ARGS({ "changed", "a(oa{sv})" },
					{ "removed", "ao" })) },
866 867 868
	{ GDBUS_SIGNAL("PeersChanged",
			GDBUS_ARGS({ "changed", "a(oa{sv})" },
					{ "removed", "ao" })) },
869 870 871 872 873 874 875 876
	{ },
};

int __connman_manager_init(void)
{
	DBG("");

	connection = connman_dbus_get_connection();
877
	if (!connection)
878 879 880 881 882 883 884 885 886 887
		return -1;

	if (connman_notifier_register(&technology_notifier) < 0)
		connman_error("Failed to register technology notifier");

	g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
					CONNMAN_MANAGER_INTERFACE,
					manager_methods,
					manager_signals, NULL, NULL, NULL);

888
	connman_state_idle = true;
889 890 891 892 893 894 895 896

	return 0;
}

void __connman_manager_cleanup(void)
{
	DBG("");

897
	if (!connection)
898 899 900 901 902 903 904 905
		return;

	connman_notifier_unregister(&technology_notifier);

	g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
						CONNMAN_MANAGER_INTERFACE);

	dbus_connection_unref(connection);
906

907
	__connman_access_manager_policy_free(manager_access_policy);
908
	manager_access_policy = NULL;
909
}