summaryrefslogtreecommitdiff
path: root/plugins/neard.c
blob: 69586df653b01140551c682f924d641d54f130eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 *  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>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>

#include <glib.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/dbus.h>
#include <connman/technology.h>
#include <connman/provision.h>

#include <gdbus.h>

#define TIMEOUT 30000

#define NEARD_SERVICE "org.neard"
#define NEARD_PATH "/"
#define NEARD_MANAGER_INTERFACE "org.neard.Manager"
#define NEARD_AGENT_INTERFACE "org.neard.HandoverAgent"
#define NEARD_ERROR_INTERFACE "org.neard.HandoverAgent.Error"

#define AGENT_PATH "/net/connman/neard_handover_agent"
#define AGENT_TYPE "wifi"

#define NEARD_SERVICE_GROUP "service_neard_provision"

/* Configuration keys */
#define SERVICE_KEY_TYPE               "Type"
#define SERVICE_KEY_SSID               "SSID"
#define SERVICE_KEY_PASSPHRASE         "Passphrase"
#define SERVICE_KEY_HIDDEN             "Hidden"

struct data_elements {
	unsigned int value;
	unsigned int length;
	bool fixed_length;
};

typedef enum {
	DE_AUTHENTICATION_TYPE = 0,
	DE_NETWORK_KEY         = 1,
	DE_SSID                = 2,
	DE_MAX                 = 3,
} DEid;

static const struct data_elements  DEs[DE_MAX] = {
	{ 0x1003, 2,  TRUE  },
	{ 0x1027, 64, FALSE },
	{ 0x1045, 32, FALSE },
};

#define DE_VAL_OPEN                 0x0001
#define DE_VAL_PSK                  0x0022

struct wifi_sc {
	gchar *ssid;
	gchar *passphrase;
};

static DBusConnection *connection = NULL;
DBusPendingCall *register_call = NULL;
bool agent_registered = false;
static guint watch_id = 0;

static int set_2b_into_tlv(uint8_t *tlv_msg, uint16_t val)
{
	uint16_t ne_val;
	uint8_t *ins;

	ins = (uint8_t *) &ne_val;

	ne_val = htons(val);
	tlv_msg[0] = ins[0];
	tlv_msg[1] = ins[1];

	return 2;
}

static int set_byte_array_into_tlv(uint8_t *tlv_msg,
					uint8_t *array, int length)
{
	memcpy((void *)tlv_msg, (void *)array, length);
	return length;
}

static uint8_t *encode_to_tlv(const char *ssid, const char *psk, int *length)
{
	uint16_t ssid_len, psk_len;
	uint8_t *tlv_msg;
	int pos = 0;

	if (!ssid || !length)
		return NULL;

	ssid_len = strlen(ssid);

	*length = 6 + 4 + ssid_len;
	if (psk) {
		psk_len = strlen(psk);
		*length += 4 + psk_len;
	} else
		psk_len = 0;

	tlv_msg = g_try_malloc0(sizeof(uint8_t) * (*length));
	if (!tlv_msg)
		return NULL;

	pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_SSID].value);
	pos += set_2b_into_tlv(tlv_msg+pos, ssid_len);
	pos += set_byte_array_into_tlv(tlv_msg+pos, (uint8_t *)ssid, ssid_len);

	pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_AUTHENTICATION_TYPE].value);
	pos += set_2b_into_tlv(tlv_msg+pos,
					DEs[DE_AUTHENTICATION_TYPE].length);
	if (psk) {
		pos += set_2b_into_tlv(tlv_msg+pos, DE_VAL_PSK);
		pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_NETWORK_KEY].value);
		pos += set_2b_into_tlv(tlv_msg+pos, psk_len);
		pos += set_byte_array_into_tlv(tlv_msg+pos,
						(uint8_t *)psk, psk_len);
	} else
		pos += set_2b_into_tlv(tlv_msg+pos, DE_VAL_OPEN);

	return tlv_msg;
}

static DBusMessage *get_reply_on_error(DBusMessage *message, int error)
{
	if (error == ENOTSUP)
		return g_dbus_create_error(message,
					NEARD_ERROR_INTERFACE ".NotSupported",
					"Operation is not supported");

	return g_dbus_create_error(message,
		NEARD_ERROR_INTERFACE ".Failed", "Invalid parameters");
}

static int parse_request_oob_params(DBusMessage *message,
				const uint8_t **tlv_msg, int *length)
{
	DBusMessageIter iter;
	DBusMessageIter array;
	DBusMessageIter dict_entry;
	const char *key;
	int arg_type;

	if (!tlv_msg || !length)
		return -EINVAL;

	dbus_message_iter_init(message, &iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
		return -EINVAL;

	dbus_message_iter_recurse(&iter, &array);
	arg_type = dbus_message_iter_get_arg_type(&array);
	if (arg_type != DBUS_TYPE_DICT_ENTRY)
		return -EINVAL;

	while (arg_type != DBUS_TYPE_INVALID) {
		dbus_message_iter_recurse(&array, &dict_entry);
		if (dbus_message_iter_get_arg_type(&dict_entry) !=
							DBUS_TYPE_STRING)
			return -EINVAL;

		dbus_message_iter_get_basic(&dict_entry, &key);
		if (g_strcmp0(key, "WSC") == 0) {
			DBusMessageIter value;
			DBusMessageIter fixed_array;

			dbus_message_iter_next(&dict_entry);
			dbus_message_iter_recurse(&dict_entry, &value);

			if (dbus_message_iter_get_arg_type(&value) !=
							DBUS_TYPE_ARRAY)
				return -EINVAL;

			dbus_message_iter_recurse(&value, &fixed_array);
			dbus_message_iter_get_fixed_array(&fixed_array,
							tlv_msg, length);
			return 0;
		}

		dbus_message_iter_next(&array);
		arg_type = dbus_message_iter_get_arg_type(&array);
	}

	return -EINVAL;
}

static DBusMessage *create_request_oob_reply(DBusMessage *message)
{
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter dict;
	const char *ssid, *psk;
	uint8_t *tlv_msg;
	int length;

	if (!connman_technology_get_wifi_tethering(&ssid, &psk))
		return get_reply_on_error(message, ENOTSUP);

	tlv_msg = encode_to_tlv(ssid, psk, &length);
	if (!tlv_msg)
		return get_reply_on_error(message, ENOTSUP);

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

	dbus_message_iter_init_append(reply, &iter);

	connman_dbus_dict_open(&iter, &dict);

	connman_dbus_dict_append_fixed_array(&dict, "WSC",
					DBUS_TYPE_BYTE, &tlv_msg, length);

	dbus_message_iter_close_container(&iter, &dict);

out:
	g_free(tlv_msg);

	return reply;
}

static DBusMessage *request_oob_method(DBusConnection *dbus_conn,
					DBusMessage *message, void *user_data)
{
	DBusMessageIter iter;

	DBG("");

	dbus_message_iter_init(message, &iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
		return get_reply_on_error(message, EINVAL);

	return create_request_oob_reply(message);
}

static void free_wifi_sc(struct wifi_sc *wsc)
{
	if (!wsc)
		return;

	g_free(wsc->ssid);
	g_free(wsc->passphrase);

	g_free(wsc);
}

static inline int get_2b_from_tlv(const uint8_t *tlv_msg, uint16_t *val)
{
	*val = ntohs(tlv_msg[0] | (tlv_msg[1] << 8));

	return 2;
}

static uint8_t *get_byte_array_from_tlv(const uint8_t *tlv_msg, int length)
{
	uint8_t *array;

	array = g_try_malloc0(sizeof(uint8_t) * length);
	if (!array)
		return NULL;

	memcpy((void *)array, (void *)tlv_msg, length*sizeof(uint8_t));

	return array;
}

static char *get_string_from_tlv(const uint8_t *tlv_msg, int length)
{
	char *str;

	str = g_try_malloc0((sizeof(char) * length) + 1);
	if (!str)
		return NULL;

	memcpy((void *)str, (void *)tlv_msg, length*sizeof(char));

	return str;
}

static inline DEid get_de_id(uint16_t attr)
{
	int i;

	for (i = 0; i < DE_MAX && DEs[i].value != 0; i++) {
		if (attr == DEs[i].value)
			return i;
	}

	return DE_MAX;
}

static inline bool is_de_length_fine(DEid id, uint16_t length)
{
	if (DEs[id].fixed_length)
		return (length == DEs[id].length);

	return (length <= DEs[id].length);
}

static char *get_hexstr_from_byte_array(const uint8_t *bt, int length)
{
	char *hex_str;
	int i, j;

	hex_str = g_try_malloc0(((length*2)+1) * sizeof(char));
	if (!hex_str)
		return NULL;

	for (i = 0, j = 0; i < length; i++, j += 2)
		snprintf(hex_str+j, 3, "%02x", bt[i]);

	return hex_str;
}

static struct wifi_sc *decode_from_tlv(const uint8_t *tlv_msg, int length)
{
	struct wifi_sc *wsc;
	uint16_t attr, len;
	uint8_t *bt;
	int pos;
	DEid id;

	if (!tlv_msg || length == 0)
		return NULL;

	wsc = g_try_malloc0(sizeof(struct wifi_sc));
	if (!wsc)
		return NULL;

	pos = 0;
	while (pos < length) {
		pos += get_2b_from_tlv(tlv_msg+pos, &attr);
		pos += get_2b_from_tlv(tlv_msg+pos, &len);

		if (len > length - pos)
			goto error;

		id = get_de_id(attr);
		if (id == DE_MAX) {
			pos += len;
			continue;
		}

		if (!is_de_length_fine(id, len))
			goto error;

		switch (id) {
		case DE_AUTHENTICATION_TYPE:
			break;
		case DE_NETWORK_KEY:
			wsc->passphrase = get_string_from_tlv(tlv_msg+pos,
									len);
			break;
		case DE_SSID:
			bt = get_byte_array_from_tlv(tlv_msg+pos, len);
			wsc->ssid = get_hexstr_from_byte_array(bt, len);
			g_free(bt);

			break;
		case DE_MAX:
			/* Falling back. Should never happen though. */
		default:
			break;
		}

		pos += len;
	}

	return wsc;

error:
	free_wifi_sc(wsc);
	return NULL;
}

static int handle_wcs_data(const uint8_t *tlv_msg, int length)
{
	struct wifi_sc *wsc = NULL;
	GKeyFile *keyfile = NULL;
	int ret = -EINVAL;

	wsc = decode_from_tlv(tlv_msg, length);
	if (!wsc)
		return -EINVAL;

	if (!wsc->ssid)
		goto out;

	keyfile = g_key_file_new();
	if (!keyfile) {
		ret = -ENOMEM;
		goto out;
	}

	g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
					SERVICE_KEY_TYPE, "wifi");
	g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
					SERVICE_KEY_SSID, wsc->ssid);
	g_key_file_set_boolean(keyfile, NEARD_SERVICE_GROUP,
					SERVICE_KEY_HIDDEN, TRUE);

	if (wsc->passphrase)
		g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
				SERVICE_KEY_PASSPHRASE, wsc->passphrase);

	ret = connman_config_provision_mutable_service(keyfile);

out:
	g_key_file_free(keyfile);
	free_wifi_sc(wsc);
	return ret;
}

static DBusMessage *push_oob_method(DBusConnection *dbus_conn,
					DBusMessage *message, void *user_data)
{
	const uint8_t *tlv_msg;
	int err = EINVAL;
	int length;

	DBG("");

	if (parse_request_oob_params(message, &tlv_msg, &length) != 0)
		return get_reply_on_error(message, err);

	err = handle_wcs_data(tlv_msg, length);
	if (err != 0)
		return get_reply_on_error(message, err);

	return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
}

static DBusMessage *release_method(DBusConnection *dbus_conn,
					DBusMessage *message, void *user_data)
{
	DBG("");

	agent_registered = false;
	g_dbus_unregister_interface(connection,
					AGENT_PATH, NEARD_AGENT_INTERFACE);

	return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
}

static const GDBusMethodTable neard_methods[] = {
	{ GDBUS_ASYNC_METHOD("RequestOOB",
		GDBUS_ARGS({ "data", "a{sv}" }),
		GDBUS_ARGS({ "data", "a{sv}" }), request_oob_method) },
	{ GDBUS_ASYNC_METHOD("PushOOB",
		GDBUS_ARGS({ "data", "a{sv}"}), NULL, push_oob_method) },
	{ GDBUS_METHOD("Release", NULL, NULL, release_method) },
	{ },
};

static void cleanup_register_call(void)
{
	if (register_call) {
		dbus_pending_call_cancel(register_call);
		dbus_pending_call_unref(register_call);
		register_call = NULL;
	}
}

static void register_agent_cb(DBusPendingCall *pending, void *user_data)
{
	DBusMessage *reply;

	if (!dbus_pending_call_get_completed(pending))
		return;

	register_call = NULL;

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

	if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
		g_dbus_unregister_interface(connection,
					AGENT_PATH, NEARD_AGENT_INTERFACE);
	} else
		agent_registered = true;

	dbus_message_unref(reply);
out:
	dbus_pending_call_unref(pending);
}

static void register_agent(void)
{
	const char *path = AGENT_PATH;
	const char *type = AGENT_TYPE;
	DBusMessage *message;

	message = dbus_message_new_method_call(NEARD_SERVICE, NEARD_PATH,
						NEARD_MANAGER_INTERFACE,
						"RegisterHandoverAgent");
	if (!message)
		return;

	dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
			&path, DBUS_TYPE_STRING, &type, DBUS_TYPE_INVALID);

	if (!dbus_connection_send_with_reply(connection, message,
						&register_call, TIMEOUT))
		goto out;

	if (!dbus_pending_call_set_notify(register_call,
						register_agent_cb, NULL, NULL))
		cleanup_register_call();

out:
	dbus_message_unref(message);
}

static void unregister_agent(void)
{
	const char *path = AGENT_PATH;
	const char *type = AGENT_TYPE;
	DBusMessage *message;

	if (!agent_registered)
		return cleanup_register_call();

	agent_registered = false;

	message = dbus_message_new_method_call(NEARD_SERVICE, NEARD_PATH,
						NEARD_MANAGER_INTERFACE,
						"UnregisterHandoverAgent");
	if (message) {
		dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
			&path, DBUS_TYPE_STRING, &type, DBUS_TYPE_INVALID);
		g_dbus_send_message(connection, message);
	}

	g_dbus_unregister_interface(connection,
					AGENT_PATH, NEARD_AGENT_INTERFACE);
}

static void neard_is_present(DBusConnection *conn, void *user_data)
{
	DBG("");

	if (agent_registered)
		return;

	if (g_dbus_register_interface(connection, AGENT_PATH,
					NEARD_AGENT_INTERFACE, neard_methods,
					NULL, NULL, NULL, NULL))
		register_agent();
}

static void neard_is_out(DBusConnection *conn, void *user_data)
{
	DBG("");

	if (agent_registered) {
		g_dbus_unregister_interface(connection,
					AGENT_PATH, NEARD_AGENT_INTERFACE);
		agent_registered = false;
	}

	cleanup_register_call();
}

static int neard_init(void)
{
	connection = connman_dbus_get_connection();
	if (!connection)
		return -EIO;

	watch_id = g_dbus_add_service_watch(connection, NEARD_SERVICE,
						neard_is_present, neard_is_out,
						NULL, NULL);
	if (watch_id == 0) {
		dbus_connection_unref(connection);
		return -ENOMEM;
	}

	return 0;
}

static void neard_exit(void)
{
	unregister_agent();

	if (watch_id != 0)
		g_dbus_remove_watch(connection, watch_id);
	if (connection)
		dbus_connection_unref(connection);
}

CONNMAN_PLUGIN_DEFINE(neard, "Neard handover plugin", VERSION,
		CONNMAN_PLUGIN_PRIORITY_DEFAULT, neard_init, neard_exit)