Fixed Tizen Profile Build Flag as per Tizen 3.0
[platform/upstream/connman.git] / plugins / neard.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <arpa/inet.h>
31
32 #include <glib.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/dbus.h>
37 #include <connman/technology.h>
38 #include <connman/provision.h>
39
40 #include <gdbus.h>
41
42 #define TIMEOUT 30000
43
44 #define NEARD_SERVICE "org.neard"
45 #define NEARD_PATH "/"
46 #define NEARD_MANAGER_INTERFACE "org.neard.Manager"
47 #define NEARD_AGENT_INTERFACE "org.neard.HandoverAgent"
48 #define NEARD_ERROR_INTERFACE "org.neard.HandoverAgent.Error"
49
50 #define AGENT_PATH "/net/connman/neard_handover_agent"
51 #define AGENT_TYPE "wifi"
52
53 #define NEARD_SERVICE_GROUP "service_neard_provision"
54
55 /* Configuration keys */
56 #define SERVICE_KEY_TYPE               "Type"
57 #define SERVICE_KEY_SSID               "SSID"
58 #define SERVICE_KEY_PASSPHRASE         "Passphrase"
59 #define SERVICE_KEY_HIDDEN             "Hidden"
60
61 struct data_elements {
62         unsigned int value;
63         unsigned int length;
64         bool fixed_length;
65 };
66
67 typedef enum {
68         DE_AUTHENTICATION_TYPE = 0,
69         DE_NETWORK_KEY         = 1,
70         DE_SSID                = 2,
71         DE_MAX                 = 3,
72 } DEid;
73
74 static const struct data_elements  DEs[DE_MAX] = {
75         { 0x1003, 2,  TRUE  },
76         { 0x1027, 64, FALSE },
77         { 0x1045, 32, FALSE },
78 };
79
80 #define DE_VAL_OPEN                 0x0001
81 #define DE_VAL_PSK                  0x0022
82
83 struct wifi_sc {
84         gchar *ssid;
85         gchar *passphrase;
86 };
87
88 static DBusConnection *connection = NULL;
89 DBusPendingCall *register_call = NULL;
90 bool agent_registered = false;
91 static guint watch_id = 0;
92
93 static int set_2b_into_tlv(uint8_t *tlv_msg, uint16_t val)
94 {
95         uint16_t ne_val;
96         uint8_t *ins;
97
98         ins = (uint8_t *) &ne_val;
99
100         ne_val = htons(val);
101         tlv_msg[0] = ins[0];
102         tlv_msg[1] = ins[1];
103
104         return 2;
105 }
106
107 static int set_byte_array_into_tlv(uint8_t *tlv_msg,
108                                         uint8_t *array, int length)
109 {
110         memcpy((void *)tlv_msg, (void *)array, length);
111         return length;
112 }
113
114 static uint8_t *encode_to_tlv(const char *ssid, const char *psk, int *length)
115 {
116         uint16_t ssid_len, psk_len;
117         uint8_t *tlv_msg;
118         int pos = 0;
119
120         if (!ssid || !length)
121                 return NULL;
122
123         ssid_len = strlen(ssid);
124
125         *length = 6 + 4 + ssid_len;
126         if (psk) {
127                 psk_len = strlen(psk);
128                 *length += 4 + psk_len;
129         } else
130                 psk_len = 0;
131
132         tlv_msg = g_try_malloc0(sizeof(uint8_t) * (*length));
133         if (!tlv_msg)
134                 return NULL;
135
136         pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_SSID].value);
137         pos += set_2b_into_tlv(tlv_msg+pos, ssid_len);
138         pos += set_byte_array_into_tlv(tlv_msg+pos, (uint8_t *)ssid, ssid_len);
139
140         pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_AUTHENTICATION_TYPE].value);
141         pos += set_2b_into_tlv(tlv_msg+pos,
142                                         DEs[DE_AUTHENTICATION_TYPE].length);
143         if (psk) {
144                 pos += set_2b_into_tlv(tlv_msg+pos, DE_VAL_PSK);
145                 pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_NETWORK_KEY].value);
146                 pos += set_2b_into_tlv(tlv_msg+pos, psk_len);
147                 pos += set_byte_array_into_tlv(tlv_msg+pos,
148                                                 (uint8_t *)psk, psk_len);
149         } else
150                 pos += set_2b_into_tlv(tlv_msg+pos, DE_VAL_OPEN);
151
152         return tlv_msg;
153 }
154
155 static DBusMessage *get_reply_on_error(DBusMessage *message, int error)
156 {
157         if (error == ENOTSUP)
158                 return g_dbus_create_error(message,
159                                         NEARD_ERROR_INTERFACE ".NotSupported",
160                                         "Operation is not supported");
161
162         return g_dbus_create_error(message,
163                 NEARD_ERROR_INTERFACE ".Failed", "Invalid parameters");
164 }
165
166 static int parse_request_oob_params(DBusMessage *message,
167                                 const uint8_t **tlv_msg, int *length)
168 {
169         DBusMessageIter iter;
170         DBusMessageIter array;
171         DBusMessageIter dict_entry;
172         const char *key;
173         int arg_type;
174
175         if (!tlv_msg || !length)
176                 return -EINVAL;
177
178         dbus_message_iter_init(message, &iter);
179
180         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
181                 return -EINVAL;
182
183         dbus_message_iter_recurse(&iter, &array);
184         arg_type = dbus_message_iter_get_arg_type(&array);
185         if (arg_type != DBUS_TYPE_DICT_ENTRY)
186                 return -EINVAL;
187
188         while (arg_type != DBUS_TYPE_INVALID) {
189                 dbus_message_iter_recurse(&array, &dict_entry);
190                 if (dbus_message_iter_get_arg_type(&dict_entry) !=
191                                                         DBUS_TYPE_STRING)
192                         return -EINVAL;
193
194                 dbus_message_iter_get_basic(&dict_entry, &key);
195                 if (g_strcmp0(key, "WSC") == 0) {
196                         DBusMessageIter value;
197                         DBusMessageIter fixed_array;
198
199                         dbus_message_iter_next(&dict_entry);
200                         dbus_message_iter_recurse(&dict_entry, &value);
201
202                         if (dbus_message_iter_get_arg_type(&value) !=
203                                                         DBUS_TYPE_ARRAY)
204                                 return -EINVAL;
205
206                         dbus_message_iter_recurse(&value, &fixed_array);
207                         dbus_message_iter_get_fixed_array(&fixed_array,
208                                                         tlv_msg, length);
209                         return 0;
210                 }
211
212                 dbus_message_iter_next(&array);
213                 arg_type = dbus_message_iter_get_arg_type(&array);
214         }
215
216         return -EINVAL;
217 }
218
219 static DBusMessage *create_request_oob_reply(DBusMessage *message)
220 {
221         DBusMessage *reply;
222         DBusMessageIter iter;
223         DBusMessageIter dict;
224         const char *ssid, *psk;
225         uint8_t *tlv_msg;
226         int length;
227
228         if (!connman_technology_get_wifi_tethering(&ssid, &psk))
229                 return get_reply_on_error(message, ENOTSUP);
230
231         tlv_msg = encode_to_tlv(ssid, psk, &length);
232         if (!tlv_msg)
233                 return get_reply_on_error(message, ENOTSUP);
234
235         reply = dbus_message_new_method_return(message);
236         if (!reply)
237                 goto out;
238
239         dbus_message_iter_init_append(reply, &iter);
240
241         connman_dbus_dict_open(&iter, &dict);
242
243         connman_dbus_dict_append_fixed_array(&dict, "WSC",
244                                         DBUS_TYPE_BYTE, &tlv_msg, length);
245
246         dbus_message_iter_close_container(&iter, &dict);
247
248 out:
249         g_free(tlv_msg);
250
251         return reply;
252 }
253
254 static DBusMessage *request_oob_method(DBusConnection *dbus_conn,
255                                         DBusMessage *message, void *user_data)
256 {
257         DBusMessageIter iter;
258
259         DBG("");
260
261         dbus_message_iter_init(message, &iter);
262
263         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
264                 return get_reply_on_error(message, EINVAL);
265
266         return create_request_oob_reply(message);
267 }
268
269 static void free_wifi_sc(struct wifi_sc *wsc)
270 {
271         if (!wsc)
272                 return;
273
274         g_free(wsc->ssid);
275         g_free(wsc->passphrase);
276
277         g_free(wsc);
278 }
279
280 static inline int get_2b_from_tlv(const uint8_t *tlv_msg, uint16_t *val)
281 {
282         *val = ntohs(tlv_msg[0] | (tlv_msg[1] << 8));
283
284         return 2;
285 }
286
287 static uint8_t *get_byte_array_from_tlv(const uint8_t *tlv_msg, int length)
288 {
289         uint8_t *array;
290
291         array = g_try_malloc0(sizeof(uint8_t) * length);
292         if (!array)
293                 return NULL;
294
295         memcpy((void *)array, (void *)tlv_msg, length*sizeof(uint8_t));
296
297         return array;
298 }
299
300 static char *get_string_from_tlv(const uint8_t *tlv_msg, int length)
301 {
302         char *str;
303
304         str = g_try_malloc0((sizeof(char) * length) + 1);
305         if (!str)
306                 return NULL;
307
308         memcpy((void *)str, (void *)tlv_msg, length*sizeof(char));
309
310         return str;
311 }
312
313 static inline DEid get_de_id(uint16_t attr)
314 {
315         int i;
316
317         for (i = 0; i < DE_MAX && DEs[i].value != 0; i++) {
318                 if (attr == DEs[i].value)
319                         return i;
320         }
321
322         return DE_MAX;
323 }
324
325 static inline bool is_de_length_fine(DEid id, uint16_t length)
326 {
327         if (DEs[id].fixed_length)
328                 return (length == DEs[id].length);
329
330         return (length <= DEs[id].length);
331 }
332
333 static char *get_hexstr_from_byte_array(const uint8_t *bt, int length)
334 {
335         char *hex_str;
336         int i, j;
337
338         hex_str = g_try_malloc0(((length*2)+1) * sizeof(char));
339         if (!hex_str)
340                 return NULL;
341
342         for (i = 0, j = 0; i < length; i++, j += 2)
343                 snprintf(hex_str+j, 3, "%02x", bt[i]);
344
345         return hex_str;
346 }
347
348 static struct wifi_sc *decode_from_tlv(const uint8_t *tlv_msg, int length)
349 {
350         struct wifi_sc *wsc;
351         uint16_t attr, len;
352         uint8_t *bt;
353         int pos;
354         DEid id;
355
356         if (!tlv_msg || length == 0)
357                 return NULL;
358
359         wsc = g_try_malloc0(sizeof(struct wifi_sc));
360         if (!wsc)
361                 return NULL;
362
363         pos = 0;
364         while (pos < length) {
365                 pos += get_2b_from_tlv(tlv_msg+pos, &attr);
366                 pos += get_2b_from_tlv(tlv_msg+pos, &len);
367
368                 if (len > length - pos)
369                         goto error;
370
371                 id = get_de_id(attr);
372                 if (id == DE_MAX) {
373                         pos += len;
374                         continue;
375                 }
376
377                 if (!is_de_length_fine(id, len))
378                         goto error;
379
380                 switch (id) {
381                 case DE_AUTHENTICATION_TYPE:
382                         break;
383                 case DE_NETWORK_KEY:
384                         wsc->passphrase = get_string_from_tlv(tlv_msg+pos,
385                                                                         len);
386                         break;
387                 case DE_SSID:
388                         bt = get_byte_array_from_tlv(tlv_msg+pos, len);
389                         wsc->ssid = get_hexstr_from_byte_array(bt, len);
390                         g_free(bt);
391
392                         break;
393                 case DE_MAX:
394                         /* Falling back. Should never happen though. */
395                 default:
396                         break;
397                 }
398
399                 pos += len;
400         }
401
402         return wsc;
403
404 error:
405         free_wifi_sc(wsc);
406         return NULL;
407 }
408
409 static int handle_wcs_data(const uint8_t *tlv_msg, int length)
410 {
411         struct wifi_sc *wsc = NULL;
412         GKeyFile *keyfile = NULL;
413         int ret = -EINVAL;
414
415         wsc = decode_from_tlv(tlv_msg, length);
416         if (!wsc)
417                 return -EINVAL;
418
419         if (!wsc->ssid)
420                 goto out;
421
422         keyfile = g_key_file_new();
423         if (!keyfile) {
424                 ret = -ENOMEM;
425                 goto out;
426         }
427
428         g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
429                                         SERVICE_KEY_TYPE, "wifi");
430         g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
431                                         SERVICE_KEY_SSID, wsc->ssid);
432         g_key_file_set_boolean(keyfile, NEARD_SERVICE_GROUP,
433                                         SERVICE_KEY_HIDDEN, TRUE);
434
435         if (wsc->passphrase)
436                 g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
437                                 SERVICE_KEY_PASSPHRASE, wsc->passphrase);
438
439         ret = connman_config_provision_mutable_service(keyfile);
440
441 out:
442         g_key_file_free(keyfile);
443         free_wifi_sc(wsc);
444         return ret;
445 }
446
447 static DBusMessage *push_oob_method(DBusConnection *dbus_conn,
448                                         DBusMessage *message, void *user_data)
449 {
450         const uint8_t *tlv_msg;
451         int err = EINVAL;
452         int length;
453
454         DBG("");
455
456         if (parse_request_oob_params(message, &tlv_msg, &length) != 0)
457                 return get_reply_on_error(message, err);
458
459         err = handle_wcs_data(tlv_msg, length);
460         if (err != 0)
461                 return get_reply_on_error(message, err);
462
463         return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
464 }
465
466 static DBusMessage *release_method(DBusConnection *dbus_conn,
467                                         DBusMessage *message, void *user_data)
468 {
469         DBG("");
470
471         agent_registered = false;
472         g_dbus_unregister_interface(connection,
473                                         AGENT_PATH, NEARD_AGENT_INTERFACE);
474
475         return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
476 }
477
478 static const GDBusMethodTable neard_methods[] = {
479         { GDBUS_ASYNC_METHOD("RequestOOB",
480                 GDBUS_ARGS({ "data", "a{sv}" }),
481                 GDBUS_ARGS({ "data", "a{sv}" }), request_oob_method) },
482         { GDBUS_ASYNC_METHOD("PushOOB",
483                 GDBUS_ARGS({ "data", "a{sv}"}), NULL, push_oob_method) },
484         { GDBUS_METHOD("Release", NULL, NULL, release_method) },
485         { },
486 };
487
488 static void cleanup_register_call(void)
489 {
490         if (register_call) {
491                 dbus_pending_call_cancel(register_call);
492                 dbus_pending_call_unref(register_call);
493                 register_call = NULL;
494         }
495 }
496
497 static void register_agent_cb(DBusPendingCall *pending, void *user_data)
498 {
499         DBusMessage *reply;
500
501         if (!dbus_pending_call_get_completed(pending))
502                 return;
503
504         register_call = NULL;
505
506         reply = dbus_pending_call_steal_reply(pending);
507         if (!reply)
508                 goto out;
509
510         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
511                 g_dbus_unregister_interface(connection,
512                                         AGENT_PATH, NEARD_AGENT_INTERFACE);
513         } else
514                 agent_registered = true;
515
516         dbus_message_unref(reply);
517 out:
518         dbus_pending_call_unref(pending);
519 }
520
521 static void register_agent(void)
522 {
523         const char *path = AGENT_PATH;
524         const char *type = AGENT_TYPE;
525         DBusMessage *message;
526
527         message = dbus_message_new_method_call(NEARD_SERVICE, NEARD_PATH,
528                                                 NEARD_MANAGER_INTERFACE,
529                                                 "RegisterHandoverAgent");
530         if (!message)
531                 return;
532
533         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
534                         &path, DBUS_TYPE_STRING, &type, DBUS_TYPE_INVALID);
535
536         if (!dbus_connection_send_with_reply(connection, message,
537                                                 &register_call, TIMEOUT)) {
538                 dbus_message_unref(message);
539                 goto out;
540         }
541
542         if (!dbus_pending_call_set_notify(register_call,
543                                                 register_agent_cb, NULL, NULL))
544                 cleanup_register_call();
545
546 out:
547         dbus_message_unref(message);
548 }
549
550 static void unregister_agent(void)
551 {
552         const char *path = AGENT_PATH;
553         const char *type = AGENT_TYPE;
554         DBusMessage *message;
555
556         if (!agent_registered)
557                 return cleanup_register_call();
558
559         agent_registered = false;
560
561         message = dbus_message_new_method_call(NEARD_SERVICE, NEARD_PATH,
562                                                 NEARD_MANAGER_INTERFACE,
563                                                 "UnregisterHandoverAgent");
564         if (message) {
565                 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
566                         &path, DBUS_TYPE_STRING, &type, DBUS_TYPE_INVALID);
567                 g_dbus_send_message(connection, message);
568         }
569
570         g_dbus_unregister_interface(connection,
571                                         AGENT_PATH, NEARD_AGENT_INTERFACE);
572 }
573
574 static void neard_is_present(DBusConnection *conn, void *user_data)
575 {
576         DBG("");
577
578         if (agent_registered)
579                 return;
580
581         if (g_dbus_register_interface(connection, AGENT_PATH,
582                                         NEARD_AGENT_INTERFACE, neard_methods,
583                                         NULL, NULL, NULL, NULL))
584                 register_agent();
585 }
586
587 static void neard_is_out(DBusConnection *conn, void *user_data)
588 {
589         DBG("");
590
591         if (agent_registered) {
592                 g_dbus_unregister_interface(connection,
593                                         AGENT_PATH, NEARD_AGENT_INTERFACE);
594                 agent_registered = false;
595         }
596
597         cleanup_register_call();
598 }
599
600 static int neard_init(void)
601 {
602         connection = connman_dbus_get_connection();
603         if (!connection)
604                 return -EIO;
605
606         watch_id = g_dbus_add_service_watch(connection, NEARD_SERVICE,
607                                                 neard_is_present, neard_is_out,
608                                                 NULL, NULL);
609         if (watch_id == 0) {
610                 dbus_connection_unref(connection);
611                 return -ENOMEM;
612         }
613
614         return 0;
615 }
616
617 static void neard_exit(void)
618 {
619         unregister_agent();
620
621         if (watch_id != 0)
622                 g_dbus_remove_watch(connection, watch_id);
623         if (connection)
624                 dbus_connection_unref(connection);
625 }
626
627 CONNMAN_PLUGIN_DEFINE(neard, "Neard handover plugin", VERSION,
628                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, neard_init, neard_exit)