Merge "Fixed memory leak in SetField method" into tizen
[platform/core/connectivity/net-config.git] / src / wifi-agent.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <vconf.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <vconf-keys.h>
25
26 #include "log.h"
27 #include "util.h"
28 #include "wifi.h"
29 #include "netdbus.h"
30 #include "wifi-agent.h"
31 #include "wifi-state.h"
32 #include "wifi-eap-config.h"
33 #include "network-state.h"
34 #include "network-accessibility.h"
35
36 #define NETCONFIG_AGENT_FIELD_NAME                              "Name"
37 #define NETCONFIG_AGENT_FIELD_SSID                              "SSID"
38 #define NETCONFIG_AGENT_FIELD_IDENTITY                  "Identity"
39 #define NETCONFIG_AGENT_FIELD_PASSPHRASE                "Passphrase"
40 #define NETCONFIG_AGENT_FIELD_WPS                               "WPS"
41 #define NETCONFIG_AGENT_FIELD_WPS_PBC                   "WPS_PBC"
42 #define NETCONFIG_AGENT_FIELD_WPS_PIN                   "WPS_PIN"
43
44 #define NETCONFIG_AGENT_ERR_CONNECT_FAILED              "connect-failed"
45
46 struct netconfig_wifi_agent {
47         GByteArray *ssid;
48         char *name;
49         char *identity;
50         char *passphrase;
51         char *wps_pin;
52         gboolean wps_pbc;
53 };
54
55 static struct netconfig_wifi_agent agent;
56
57 static void __netconfig_agent_clear_fields(void)
58 {
59         if (agent.ssid)
60                 g_byte_array_free(agent.ssid, TRUE);
61         g_free(agent.name);
62         g_free(agent.identity);
63         g_free(agent.passphrase);
64         g_free(agent.wps_pin);
65
66         agent.ssid = NULL;
67         agent.name = NULL;
68         agent.identity = NULL;
69         agent.passphrase = NULL;
70         agent.wps_pin = NULL;
71         agent.wps_pbc = FALSE;
72 }
73
74 int connman_register_agent(void)
75 {
76         GVariant *reply = NULL;
77         GVariant *params = NULL;
78
79         params = g_variant_new("(o)", NETCONFIG_WIFI_PATH);
80         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
81                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
82                         "RegisterAgent", params);
83
84         if (reply == NULL) {
85                         ERR("Fail to register agent");
86                         return FALSE;
87         } else
88                 g_variant_unref(reply);
89
90         INFO("Registered to connman agent successfully");
91
92         return TRUE;
93 }
94
95 int connman_unregister_agent(void)
96 {
97         gboolean reply = FALSE;
98         GVariant *param = NULL;
99         const char *path = NETCONFIG_WIFI_PATH;
100
101         param = g_variant_new("(o)", path);
102
103         DBG("ConnMan agent unregister");
104
105         reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
106                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
107                         "UnregisterAgent", param, NULL);
108
109         if (reply != TRUE)
110                 ERR("Fail to unregister agent");
111
112         /* Clearing the agent fields */
113         __netconfig_agent_clear_fields();
114
115         return reply;
116 }
117
118 gboolean netconfig_wifi_set_agent_field_for_eap_network(
119                 const char *name, const char *identity, const char *passphrase)
120 {
121         int name_len;
122
123         if (name == NULL)
124                 return FALSE;
125
126         __netconfig_agent_clear_fields();
127
128         name_len = strlen(name);
129         agent.ssid = g_byte_array_sized_new(name_len);
130         agent.ssid->len = name_len;
131         memcpy(agent.ssid->data, name, name_len + 1);
132
133         if (identity)
134                 agent.identity = g_strdup(identity);
135
136         if (passphrase)
137                 agent.passphrase = g_strdup(passphrase);
138
139         DBG("Successfully configured for EAP network");
140
141         return TRUE;
142 }
143
144 gboolean handle_set_field(NetConnmanAgent *connman_agent,
145                 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
146 {
147         GError *error = NULL;
148         GVariantIter *iter;
149         gpointer field;
150         GVariant *value;
151         gboolean updated = FALSE;
152         gboolean reply = FALSE;
153
154         g_return_val_if_fail(connman_agent != NULL, FALSE);
155
156         DBG("Set agent fields for %s", service);
157
158         if (netconfig_is_wifi_profile(service) != TRUE) {
159                 error = g_error_new(G_DBUS_ERROR,
160                                 G_DBUS_ERROR_AUTH_FAILED,
161                                 CONNMAN_ERROR_INTERFACE ".InvalidService");
162
163                 g_dbus_method_invocation_return_gerror(context, error);
164                 g_clear_error(&error);
165
166                 return reply;
167         }
168
169         __netconfig_agent_clear_fields();
170         g_variant_get(fields, "a{sv}", &iter);
171         while (g_variant_iter_loop(iter, "{sv}", &field, &value)) {
172                 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0) {
173                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
174                                 agent.passphrase = g_strdup(g_variant_get_string(value, NULL));
175                                 updated = TRUE;
176
177                                 DBG("Field [%s] - []", field);
178                         }
179                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PBC) == 0) {
180                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING) &&
181                                         g_strcmp0(g_variant_get_string(value, NULL), "enable") == 0) {
182                                 agent.wps_pbc = TRUE;
183                                 updated = TRUE;
184
185                                 DBG("Field [%s] - [%d]", field, agent.wps_pbc);
186                         }
187                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS_PIN) == 0) {
188                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
189                                 agent.wps_pin = g_strdup(g_variant_get_string(value, NULL));
190                                 updated = TRUE;
191
192                                 DBG("Field [%s] - []", field);
193                         }
194                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0) {
195                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
196                                 agent.name = g_strdup(g_variant_get_string(value, NULL));
197                                 updated = TRUE;
198
199                                 DBG("Field [%s] - []", field);
200                         }
201                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0) {
202                         if (agent.ssid != NULL) {
203                                 g_byte_array_free(agent.ssid, TRUE);
204                                 agent.ssid = NULL;
205                         }
206
207                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_BYTESTRING)) {
208                                 guint8 char_value;
209                                 GVariantIter *iter1;
210                                 GByteArray *array = g_byte_array_new();
211
212                                 g_variant_get(value, "ay", &iter1);
213                                 while (g_variant_iter_loop(iter1, "y", &char_value))
214                                         g_byte_array_append(array, &char_value, 1);
215                                 g_variant_iter_free(iter1);
216                                 if (array != NULL && (array->len > 0)) {
217                                         agent.ssid = g_byte_array_sized_new(array->len);
218                                         agent.ssid->len = array->len;
219                                         memcpy(agent.ssid->data, array->data, array->len);
220                                         updated = TRUE;
221
222                                         DBG("Field [%s] - []", field);
223                                 }
224                         }
225                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0) {
226                         if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
227                                 agent.identity = g_strdup(g_variant_get_string(value, NULL));
228                                 updated = TRUE;
229
230                                 DBG("Field [%s] - []", field);
231                         }
232                 }
233         }
234
235         if (updated == TRUE) {
236                 reply = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
237                                 service, CONNMAN_SERVICE_INTERFACE, "Connect",
238                                 NULL, __netconfig_wifi_connect_reply);
239                 if (reply != TRUE) {
240                         ERR("Fail to connect Wi-Fi");
241                         __netconfig_agent_clear_fields();
242                         error = g_error_new(G_DBUS_ERROR,
243                                         G_DBUS_ERROR_AUTH_FAILED,
244                                         CONNMAN_ERROR_INTERFACE ".InvalidArguments");
245
246                         g_dbus_method_invocation_return_gerror(context, error);
247                         g_clear_error(&error);
248                         g_variant_iter_free(iter);
249                         return reply;
250                 }
251         } else {
252                 error = g_error_new(G_DBUS_ERROR,
253                                 G_DBUS_ERROR_AUTH_FAILED,
254                                 CONNMAN_ERROR_INTERFACE ".InvalidArguments");
255
256                 g_dbus_method_invocation_return_gerror(context, error);
257                 g_clear_error(&error);
258                 g_variant_iter_free(iter);
259                 return reply;
260         }
261
262         g_variant_iter_free(iter);
263
264         net_connman_agent_complete_set_field(connman_agent, context);
265         return reply;
266 }
267
268 gboolean handle_request_input(NetConnmanAgent *connman_agent,
269                 GDBusMethodInvocation *context, const gchar *service, GVariant *fields)
270 {
271         GVariantIter *iter;
272         gchar *field = NULL;
273         GVariant *r_value = NULL;
274         GVariant *out_table = NULL;
275         gboolean updated = FALSE;
276         GVariantBuilder *builder = NULL;
277
278         g_return_val_if_fail(connman_agent != NULL, FALSE);
279
280         if (NULL == service)
281                 return FALSE;
282
283         DBG("Agent fields requested for service: %s", service);
284
285         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
286
287         g_variant_get(fields, "a{sv}", &iter);
288         while (g_variant_iter_loop(iter, "{sv}", &field, &r_value)) {
289
290                 if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_PASSPHRASE) == 0 &&
291                                 agent.passphrase != NULL) {
292                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_PASSPHRASE,
293                                                         g_variant_new_string(agent.passphrase));
294
295                         updated = TRUE;
296                         DBG("Setting [%s] - []", field);
297                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_WPS) == 0 &&
298                                 (agent.wps_pbc == TRUE || agent.wps_pin != NULL)) {
299                         if (agent.wps_pbc == TRUE) {
300                                 /* Sending empty string for WPS push button method */
301                                 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(""));
302
303                                 updated = TRUE;
304                                 DBG("Setting empty string for [%s]", field);
305                         } else if (agent.wps_pin != NULL) {
306                                 g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_WPS, g_variant_new_string(agent.wps_pin));
307
308                                 updated = TRUE;
309                                 DBG("Setting string [%s] - []", field);
310                         }
311                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_NAME) == 0 &&
312                                 agent.name != NULL) {
313                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_NAME, g_variant_new_string(agent.name));
314
315                         updated = TRUE;
316                         DBG("Settings [%s] - []", field);
317                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_SSID) == 0 &&
318                                 agent.ssid != NULL) {
319                         int i = 0;
320                         GVariantBuilder *builder1 = NULL;
321                         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
322
323                         for (i = 0; i < (agent.ssid->len); i++)
324                                 g_variant_builder_add(builder1, "y", agent.ssid->data[i]);
325
326                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_SSID, g_variant_builder_end(builder1));
327                         if (builder1 != NULL)
328                                 g_variant_builder_unref(builder1);
329
330                         updated = TRUE;
331                         DBG("Settings [%s] - []", field);
332                 } else if (g_strcmp0(field, NETCONFIG_AGENT_FIELD_IDENTITY) == 0 &&
333                                 agent.identity != NULL) {
334                         g_variant_builder_add(builder, "{sv}", NETCONFIG_AGENT_FIELD_IDENTITY, g_variant_new_string(agent.identity));
335
336                         updated = TRUE;
337                         DBG("Settings [%s] - []", field);
338                 }
339         }
340
341         out_table = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
342
343         if (builder)
344                 g_variant_builder_unref(builder);
345
346         g_variant_iter_free(iter);
347
348
349         if (NULL == out_table) {
350                 net_connman_agent_complete_request_input(connman_agent, context, out_table);
351
352                 return FALSE;
353         }
354
355         if (updated == TRUE)
356                 g_dbus_method_invocation_return_value(context, out_table);
357         else {
358                 GError *error = NULL;
359                 error = g_error_new(G_DBUS_ERROR,
360                                 G_DBUS_ERROR_AUTH_FAILED,
361                                 "net.connman.Agent.Error.Canceled");
362
363                 g_dbus_method_invocation_return_gerror(context, error);
364                 g_clear_error(&error);
365         }
366
367         __netconfig_agent_clear_fields();
368
369         return updated;
370 }
371
372
373 gboolean handle_report_error(NetConnmanAgent *connman_agent,
374                 GDBusMethodInvocation *context, const gchar *service, const gchar *error)
375 {
376         gboolean ret = TRUE;
377
378         g_return_val_if_fail(connman_agent != NULL, FALSE);
379
380         net_connman_agent_complete_report_error(connman_agent, context);
381         DBG("Agent error for service[%s] - [%s]", service, error);
382
383         /* Do something when it failed to make a connection */
384
385         return ret;
386 }
387
388 #if defined TIZEN_CAPTIVE_PORTAL
389 #if defined TIZEN_WEARABLE
390 #define QUERY_FOR_INTERNET_INTERVAL                     2
391 #define TIMER_THRESHOLD                                         4
392 #else
393 #define QUERY_FOR_INTERNET_INTERVAL                     20
394 #define TIMER_THRESHOLD                                         120
395 #endif
396
397 static gboolean is_monitor_notifier_registered = FALSE;
398
399 #if defined TIZEN_WEARABLE
400 static gboolean is_portal_msg_shown = FALSE;
401 #endif
402
403 struct poll_timer_data {
404         guint time_elapsed;
405         guint timer_id;
406         void* data;
407 };
408
409 static struct poll_timer_data timer_data = {
410                 QUERY_FOR_INTERNET_INTERVAL, 0, NULL};
411
412 static gboolean __check_ignore_portal_list(const char * ssid)
413 {
414         char def_str[1024];
415         int i = 0;
416         int ignore_ap_count = 0;
417
418         if (ssid == NULL)
419                 return FALSE;
420
421         DBG("checking ssid [%s]", ssid);
422
423         DBG("csc string [%s]", def_str);
424         gchar ** ignore_ap_list = g_strsplit(def_str, ",", 0);
425         ignore_ap_count = g_strv_length(ignore_ap_list);
426         for (i = 0; i < ignore_ap_count; i++) {
427                 DBG("[%d] - [%s]", i, ignore_ap_list[i]);
428                 if (strncmp(ignore_ap_list[i], ssid, strlen(ssid)) == 0) {
429                         g_strfreev(ignore_ap_list);
430                         return TRUE;
431                 }
432         }
433
434         g_strfreev(ignore_ap_list);
435         return FALSE;
436 }
437
438 static void __wifi_state_monitor(wifi_service_state_e state,
439                 void *user_data);
440
441 static wifi_state_notifier wifi_state_monitor_notifier = {
442                 .wifi_state_changed = __wifi_state_monitor,
443                 .user_data = NULL,
444 };
445
446 static void __wifi_state_monitor(wifi_service_state_e state,
447                 void *user_data)
448 {
449         DBG("Wi-Fi state: %x", state);
450
451         if (state == NETCONFIG_WIFI_CONNECTED)
452                 return;
453
454         if (is_monitor_notifier_registered == TRUE) {
455                 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
456                 is_monitor_notifier_registered = FALSE;
457         }
458
459 #if defined TIZEN_WEARABLE
460         is_portal_msg_shown = FALSE;
461 #endif
462
463         /* suspend if Internet check activity in progress */
464         if (timer_data.timer_id == 0)
465                 return;
466
467         netconfig_stop_timer(&timer_data.timer_id);
468         netconfig_stop_internet_check();
469
470         DBG("Stopped Internet accessibility check");
471 }
472
473 static gboolean __netconfig_wifi_portal_login_timeout(gpointer data)
474 {
475         char *service_profile = NULL;
476         GVariant *reply = NULL;
477
478         DBG("");
479
480         struct poll_timer_data *timer = (struct poll_timer_data *)data;
481         if (timer == NULL)
482                 return FALSE;
483
484         if (TRUE == netconfig_get_internet_status()) {
485                 if (is_monitor_notifier_registered == TRUE) {
486                         wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
487                         is_monitor_notifier_registered = FALSE;
488                 }
489
490                 DBG("Portal logged in successfully and update ConnMan state");
491                 return FALSE; /* to stop the timer */
492         } else {
493                 if (timer->time_elapsed >= TIMER_THRESHOLD) {
494                         DBG("Login failed, update ConnMan");
495
496                         if (is_monitor_notifier_registered == TRUE) {
497                                 wifi_state_notifier_unregister(&wifi_state_monitor_notifier);
498                                 is_monitor_notifier_registered = FALSE;
499                         }
500
501                         /* Disconnect and forget the AP */
502                         service_profile = (char*) netconfig_get_default_profile();
503                         if (service_profile && netconfig_is_wifi_profile(service_profile)) {
504                                 /* Now forget the AP*/
505                                 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
506                                                 service_profile, CONNMAN_SERVICE_INTERFACE, "Remove",
507                                                 NULL);
508
509                                 if (reply != NULL)
510                                         g_variant_unref(reply);
511                                 else
512                                         ERR("Failed to forget the AP ");
513                         }
514                 } else {
515                         if (NETCONFIG_WIFI_CONNECTED ==
516                                         wifi_state_get_service_state()) {
517                                 /* check Internet availability by sending and receiving data*/
518                                 netconfig_check_internet_accessibility();
519                                 /* Returning TRUE itself is enough to restart the timer */
520                                 timer->time_elapsed = timer->time_elapsed +
521                                                                         QUERY_FOR_INTERNET_INTERVAL;
522                                 return TRUE;
523                         }
524                 }
525         }
526
527         return FALSE;
528 }
529
530 static void __netconfig_wifi_portal_login_timer_start(struct poll_timer_data
531                 *data)
532 {
533         DBG("__netconfig_wifi_browser_start_timer...starting timer");
534
535         if (data == NULL)
536                 return;
537
538         netconfig_stop_timer(&(data->timer_id));
539
540         /* Timer logic: After successful launch of browser, we would check for
541          * Internet status for every 20s until a threshold of 120s
542          */
543
544         data->time_elapsed = QUERY_FOR_INTERNET_INTERVAL;
545         netconfig_start_timer_seconds(QUERY_FOR_INTERNET_INTERVAL,
546                 __netconfig_wifi_portal_login_timeout, data, &(data->timer_id));
547 }
548 #endif
549
550 gboolean handle_request_browser(NetConnmanAgent *connman_agent,
551                 GDBusMethodInvocation *context, const gchar *service, const gchar *url)
552 {
553 #if defined TIZEN_CAPTIVE_PORTAL
554         gboolean ret = FALSE;
555         gboolean ignore_portal = FALSE;
556         const char * ssid = NULL;
557
558         g_return_val_if_fail(connman_agent != NULL, FALSE);
559
560         DBG("service[%s] - url[%s]", service, url);
561
562         ssid = netconfig_wifi_get_connected_essid(netconfig_get_default_profile());
563         if (ssid == NULL) {
564                 ERR("Connected AP name is NULL!!");
565                 net_connman_agent_complete_request_browser(connman_agent, context);
566                 return FALSE;
567         }
568
569         ignore_portal = __check_ignore_portal_list(ssid);
570
571         if (ignore_portal == TRUE) {
572                 net_connman_agent_complete_request_browser(connman_agent, context);
573                 return TRUE;
574         }
575         /* Register for Wifi state change notifier*/
576         if (is_monitor_notifier_registered == FALSE) {
577                 wifi_state_notifier_register(&wifi_state_monitor_notifier);
578                 is_monitor_notifier_registered = TRUE;
579         }
580
581         ret = netconfig_send_notification_to_net_popup(NETCONFIG_ADD_PORTAL_NOTI, ssid);
582
583         timer_data.time_elapsed = 0;
584         __netconfig_wifi_portal_login_timer_start(&timer_data);
585
586         net_connman_agent_complete_request_browser(connman_agent, context);
587         return ret;
588 #else
589         GError *error = NULL;
590         error = g_error_new(G_DBUS_ERROR,
591                         G_DBUS_ERROR_AUTH_FAILED,
592                         CONNMAN_ERROR_INTERFACE ".NotSupported");
593
594         g_dbus_method_invocation_return_gerror(context, error);
595         g_clear_error(&error);
596
597         return FALSE;
598 #endif
599 }