Fix dlog format error
[platform/core/connectivity/net-config.git] / src / signal-handler.c
index d81db12..8dcef2c 100755 (executable)
@@ -40,6 +40,7 @@
 #include "wifi-background-scan.h"
 #include "wifi-tdls.h"
 #include "ip-conflict-detect.h"
+#include "wifi-key-encryption.h"
 #if defined TIZEN_DEBUG_ENABLE
 #include "network-dump.h"
 #define NETWORK_LOG_DUMP_SCRIPT  "/opt/var/lib/net-config/network_log_dump.sh"
@@ -142,6 +143,8 @@ static void __netconfig_extract_ipv4_signal_data(GVariant *dictionary,
                                params = g_variant_new("(@a{sv})",
                                                                           g_variant_builder_end(builder));
 
+                               g_variant_builder_unref(builder);
+
                                netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
                                                   NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
                                                   params);
@@ -184,6 +187,7 @@ static void __netconfig_extract_ipv6_signal_data(GVariant *dictionary,
 
                                params = g_variant_new("(@a{sv})",
                                                                           g_variant_builder_end(builder));
+                               g_variant_builder_unref(builder);
 
                                netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
                                                   NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
@@ -216,8 +220,8 @@ static int __netconfig_handle_execute_file(const char *file_path,
 
                errno = 0;
                if (execve(file_path, args, envs) == -1) {
-                       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
-                       DBG("Fail to execute command (%s)", error_buf);
+                       DBG("Fail to execute command (%s)",
+                                       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
                        exit(1);
                }
        } else if (pid > 0) {
@@ -238,8 +242,8 @@ static int __netconfig_handle_execute_file(const char *file_path,
                return rv;
        }
 
-       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
-       DBG("failed to fork(%s)", error_buf);
+       DBG("failed to fork(%s)",
+               strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
        return -EIO;
 }
 
@@ -290,10 +294,8 @@ static void _technology_signal_cb(GDBusConnection *conn,
                        /* Tethering state */
                        wifi_state_set_tech_state(NETCONFIG_WIFI_TECH_TETHERED);
                }
-               if (key)
-                       g_free(key);
-               if (var)
-                       g_variant_unref(var);
+               g_free(key);
+               g_variant_unref(var);
        }
 }
 
@@ -324,19 +326,6 @@ static void _service_signal_cb(GDBusConnection *conn,
                g_variant_get(variant, "s", &property);
 
                DBG("[%s] %s", property, path);
-               if (netconfig_is_wifi_profile(path) || netconfig_is_ethernet_profile(path)) {
-                       if (g_strcmp0(property, "ready") == 0) {
-                               for (idx = 0; idx < MAX_SOCKET_OPEN_RETRY; idx++) {
-                                       sd = start_ip_conflict_mon();
-                                       if (sd != NULL)
-                                               break;
-                               }
-                       } else if (g_strcmp0(property, "online") == 0) {
-                               // do nothing
-                       } else {
-                               stop_ip_conflict_mon();
-                       }
-               }
 
                if (netconfig_is_wifi_profile(path) == TRUE) {
                        int wifi_state = 0;
@@ -451,12 +440,27 @@ static void _service_signal_cb(GDBusConnection *conn,
                                netconfig_update_default_profile(NULL);
                        }
                }
+
+               if (netconfig_is_wifi_profile(path) || netconfig_is_ethernet_profile(path)) {
+                       if (g_strcmp0(property, "ready") == 0) {
+                               for (idx = 0; idx < MAX_SOCKET_OPEN_RETRY; idx++) {
+                                       sd = start_ip_conflict_mon();
+                                       if (sd != NULL)
+                                               break;
+                               }
+                       } else if (g_strcmp0(property, "online") == 0) {
+                               // do nothing
+                       } else {
+                               stop_ip_conflict_mon();
+                       }
+               }
+
                g_free(property);
        } else if (g_strcmp0(sigvalue, "Proxy") == 0) {
                if (netconfig_is_wifi_profile(path) != TRUE || g_strcmp0(path, netconfig_get_default_profile()) != 0)
                        goto done;
 
-               if (!g_variant_type_equal(variant, G_VARIANT_TYPE_ARRAY))
+               if (!g_variant_is_of_type(variant, G_VARIANT_TYPE_ARRAY))
                        goto done;
 
                g_variant_get(variant, "a{sv}", &iter);
@@ -480,6 +484,7 @@ static void _service_signal_cb(GDBusConnection *conn,
 
                                sig_params = g_variant_new("(@a{sv})",
                                                                g_variant_builder_end(builder));
+                               g_variant_builder_unref(builder);
 
                                netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
                                                   NETCONFIG_NETWORK_INTERFACE, "NetworkConfigChanged",
@@ -501,6 +506,7 @@ static void _service_signal_cb(GDBusConnection *conn,
 
                                        sig_params = g_variant_new("(@a{sv})",
                                                                g_variant_builder_end(builder));
+                                       g_variant_builder_unref(builder);
 
                                        netconfig_dbus_emit_signal(NULL, NETCONFIG_NETWORK_PATH,
                                                           NETCONFIG_NETWORK_INTERFACE,
@@ -522,14 +528,20 @@ static void _service_signal_cb(GDBusConnection *conn,
                g_variant_get(variant, "s", &property);
                INFO("[%s] Property : %s", sigvalue, property);
 #if defined TIZEN_DEBUG_ENABLE
-               if (g_strcmp0(property, "connect-failed") == 0 || g_strcmp0(property, "invalid-key") == 0 || g_strcmp0(property, "auth-failed") == 0)
+               if (g_strcmp0(property, "dhcp-failed") == 0
+                       || g_strcmp0(property, "connect-failed") == 0
+                       || g_strcmp0(property, "login-failed") == 0
+                       || g_strcmp0(property, "auth-failed") == 0
+                       || g_strcmp0(property, "invalid-key") == 0) {
+
+                       INFO("start dump");
                        _start_dump();
+               }
 #endif
                g_free(property);
        }
 done:
-       if (sigvalue)
-               g_free(sigvalue);
+       g_free(sigvalue);
 
        if (variant)
                g_variant_unref(variant);
@@ -555,12 +567,9 @@ static void _dbus_name_changed_cb(GDBusConnection *conn,
 
                connman_register_agent();
        }
-       if (name)
-               g_free(name);
-       if (old)
-               g_free(old);
-       if (new)
-               g_free(new);
+       g_free(name);
+       g_free(old);
+       g_free(new);
 
        return;
 }
@@ -930,6 +939,72 @@ error:
        netconfig_wifi_notify_wps_fail_event(config_error, error_indication);
 }
 
+static void _find_service_and_set_passphrase(char *ssid, char *key)
+{
+       GString *str;
+       GVariant *reply = NULL;
+       int i, j, ssid_len;
+       char *service_path;
+       char *dev_mac_addr;
+       char ident[13];
+       gchar *enc_passphrase;
+
+       ssid_len = strlen(ssid);
+
+       str = g_string_sized_new((ssid_len * 2) + 55);
+       if (!str)
+               return;
+
+       dev_mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
+       if (!dev_mac_addr) {
+               ERR("Failed to get mac address from vconf key");
+               g_string_free(str, TRUE);
+               return;
+       }
+
+       j = 0;
+       for (i = 0; dev_mac_addr[i]; i++) {
+               if (dev_mac_addr[i] != ':')
+                       ident[j++] = dev_mac_addr[i];
+       }
+       ident[j] = '\0';
+
+       g_string_append_printf(str, "%s%s_", CONNMAN_WIFI_SERVICE_PROFILE_PREFIX,
+                                                  ident);
+       free(dev_mac_addr);
+
+       for (i = 0; i < ssid_len; i++) {
+               if (ssid[i] != '"')
+                       g_string_append_printf(str, "%02x", ssid[i]);
+       }
+
+       g_string_append_printf(str, "_managed_psk");
+
+       service_path = g_string_free(str, FALSE);
+
+       enc_passphrase = _netconfig_encrypt_passphrase(key);
+       if (!enc_passphrase) {
+               ERR("Failed to encrypt passphrase");
+               g_free(service_path);
+               return;
+       }
+
+       INFO("wps service_path %s Passphrase %s", service_path, enc_passphrase);
+
+       reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE, service_path,
+                                        CONNMAN_SERVICE_INTERFACE, "SetProperty",
+                                        g_variant_new("(sv)", "Passphrase",
+                                                                  g_variant_new_string(enc_passphrase)));
+       if (reply != NULL)
+               g_variant_unref(reply);
+       else
+               ERR("Failed to set passphrase");
+
+       g_free(service_path);
+
+       return;
+}
+
 static void _supplicant_wifi_wps_credentials(GVariant *param)
 {
        gchar *key;
@@ -940,6 +1015,7 @@ static void _supplicant_wifi_wps_credentials(GVariant *param)
        int config_error = 0;
        int error_indication = 0;
        gsize ssid_len = 0;
+       char *key_mgmt = NULL;
 
        if (param == NULL) {
                ERR("Param is NULL");
@@ -958,7 +1034,8 @@ static void _supplicant_wifi_wps_credentials(GVariant *param)
 
                        INFO("wps password len %d ", key_len);
                        if (key_len > 0) {
-                               t_key = g_variant_get_fixed_array(variant, &key_len, sizeof(guchar));
+                               t_key = g_variant_get_fixed_array(variant, &key_len,
+                                                 sizeof(guchar));
                                if (!t_key) {
                                        g_free(key);
                                        g_variant_unref(variant);
@@ -971,7 +1048,8 @@ static void _supplicant_wifi_wps_credentials(GVariant *param)
                                SLOGI("WPS AP Security ->Open");
                } else if (g_strcmp0(key, "SSID") == 0) {
                        const char *t_key = NULL;
-                       t_key = g_variant_get_fixed_array(variant, &ssid_len, sizeof(guchar));
+                       t_key = g_variant_get_fixed_array(variant, &ssid_len,
+                                                 sizeof(guchar));
                        INFO("wps ssid_len is %d ", ssid_len);
                        if (!t_key) {
                                g_free(key);
@@ -985,11 +1063,42 @@ static void _supplicant_wifi_wps_credentials(GVariant *param)
                                ssid_len = 0;
                        }
                        INFO("SSID in process credentials %s", ssid);
+               } else if (g_strcmp0(key, "AuthType") == 0) {
+                       gchar *auth_type;
+                       GVariantIter *var_iter;
+                       g_variant_get(variant, "as", &var_iter);
+                       while (g_variant_iter_loop(var_iter, "s", &auth_type)) {
+                               INFO("wps auth_type %s", auth_type);
+                               if (g_strcmp0(auth_type, "wpa-psk") == 0 ||
+                                       g_strcmp0(auth_type, "wpa2-psk") == 0)
+                                       key_mgmt = "psk";
+                               else if (g_strcmp0(auth_type, "open") == 0)
+                                       key_mgmt = "none";
+                       }
+                       g_variant_iter_free(var_iter);
+               } else if (g_strcmp0(key, "EncrType") == 0) {
+                       gchar *encr_type;
+                       GVariantIter *var_iter;
+                       g_variant_get(variant, "as", &var_iter);
+                       while (g_variant_iter_loop(var_iter, "s", &encr_type))
+                               INFO("wps encr_type %s", encr_type);
+                       g_variant_iter_free(var_iter);
+               } else if (g_strcmp0(key, "BSSID") == 0) {
+                       const guchar *bssid;
+                       gsize bssid_len;
+                       bssid = g_variant_get_fixed_array(variant, &bssid_len,
+                                                               sizeof(guchar));
+                       if (bssid && bssid_len == 6)
+                               INFO("wps BSSID %2x:%2x:%2x:%2x:%2x:%2x", bssid[0], bssid[1],
+                                        bssid[2], bssid[3], bssid[4], bssid[5]);
                }
        }
 
        g_variant_iter_free(iter);
 
+       if (g_strcmp0(key_mgmt, "psk") == 0)
+               _find_service_and_set_passphrase(ssid, wps_key);
+
 #if 0
        /*
         * Notify WPS Credentials only when requested through WPS PBC