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