Start IP conflict monitoring only after default profile has been updated
[platform/core/connectivity/net-config.git] / src / signal-handler.c
index b9720a8..8d7b66b 100755 (executable)
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <vconf.h>
 #include <vconf-keys.h>
+#include <sys/wait.h>
 
 #include "log.h"
 #include "util.h"
 #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"
+#define MAX_SIZE_ERROR_BUFFER 256
 #endif
 
 #define DBUS_SERVICE_DBUS                      "org.freedesktop.DBus"
@@ -139,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);
@@ -181,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",
@@ -191,6 +198,71 @@ static void __netconfig_extract_ipv6_signal_data(GVariant *dictionary,
        }
 }
 
+#if defined TIZEN_DEBUG_ENABLE
+static int __netconfig_handle_execute_file(const char *file_path,
+               char *const args[], char *const envs[])
+{
+       pid_t pid = 0;
+       int status = 0;
+       int rv = 0;
+       errno = 0;
+       register unsigned int index = 0;
+       char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
+
+       while (args[index] != NULL) {
+               DBG("%s", args[index]);
+               index++;
+       }
+
+       if (!(pid = fork())) {
+               DBG("pid(%d), ppid (%d)", getpid(), getppid());
+               DBG("Inside child, exec (%s) command", 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);
+                       exit(1);
+               }
+       } else if (pid > 0) {
+               if (waitpid(pid, &status, 0) == -1)
+                       DBG("wait pid (%u) status (%d)", pid, status);
+
+               if (WIFEXITED(status)) {
+                       rv = WEXITSTATUS(status);
+                       DBG("exited, status=%d", rv);
+               } else if (WIFSIGNALED(status)) {
+                       DBG("killed by signal %d", WTERMSIG(status));
+               } else if (WIFSTOPPED(status)) {
+                       DBG("stopped by signal %d", WSTOPSIG(status));
+               } else if (WIFCONTINUED(status)) {
+                       DBG("continued");
+               }
+
+               return rv;
+       }
+
+       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+       DBG("failed to fork(%s)", error_buf);
+       return -EIO;
+}
+
+static int _start_dump()
+{
+       int rv = 0;
+       gchar *path = NETWORK_LOG_DUMP_SCRIPT;
+       char *const args[] = { NETWORK_LOG_DUMP_SCRIPT, NULL };
+       char *const envs[] = { NULL };
+
+       rv = __netconfig_handle_execute_file(path, args, envs);
+       if (rv < 0) {
+               ERR("Fail to execute network log dump shell");
+               return -EIO;
+       }
+       return 0;
+}
+#endif
+
 static void _technology_signal_cb(GDBusConnection *conn,
                const gchar *name, const gchar *path, const gchar *interface,
                const gchar *sig, GVariant *param, gpointer user_data)
@@ -222,16 +294,15 @@ 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);
        }
 }
 
 static void _service_signal_cb(GDBusConnection *conn,
                const gchar *name, const gchar *path,
-               const gchar *interface, const gchar *sig, GVariant *param, gpointer user_data)
+               const gchar *interface, const gchar *sig,
+               GVariant *param, gpointer user_data)
 {
        gchar *sigvalue = NULL;
        gchar *property;
@@ -254,20 +325,7 @@ static void _service_signal_cb(GDBusConnection *conn,
        if (g_strcmp0(sigvalue, "State") == 0) {
                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();
-               }
-       }
+               DBG("[%s] %s", property, path);
 
                if (netconfig_is_wifi_profile(path) == TRUE) {
                        int wifi_state = 0;
@@ -382,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);
@@ -411,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",
@@ -432,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,
@@ -452,11 +527,21 @@ static void _service_signal_cb(GDBusConnection *conn,
        } else if (g_strcmp0(sigvalue, "Error") == 0) {
                g_variant_get(variant, "s", &property);
                INFO("[%s] Property : %s", sigvalue, property);
+#if defined TIZEN_DEBUG_ENABLE
+               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);
@@ -482,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;
 }
@@ -583,9 +665,11 @@ static void _supplicant_properties_changed(GDBusConnection *conn,
                const gchar *name, const gchar *path, const gchar *interface,
                const gchar *sig, GVariant *param, gpointer user_data)
 {
-       gchar *key;
-       GVariantIter *iter;
-       GVariant *variant;
+       DBG("Properties changed handling!");
+       gchar *key = NULL;
+       const gchar *state = NULL;
+       GVariantIter *iter = NULL;
+       GVariant *variant = NULL;
        gboolean scanning = FALSE;
 
        if (param == NULL)
@@ -603,7 +687,34 @@ static void _supplicant_properties_changed(GDBusConnection *conn,
 
                        g_variant_unref(variant);
                        g_free(key);
+                       variant = NULL;
+                       key = NULL;
+                       break;
+               } else if (g_strcmp0(key, "State") == 0) {
+                       state = g_variant_get_string(variant, NULL);
+                       if (state != NULL)
+                               ERR("Supplicant state : %s", state);
+
+                       g_variant_unref(variant);
+                       g_free(key);
+                       variant = NULL;
+                       key = NULL;
+                       break;
+               } else if (g_strcmp0(key, "DisconnectReason") == 0) {
+                       int reason = g_variant_get_int32(variant);
+                       ERR("Supplicant DisconnReason : %d", reason);
+
+                       g_variant_unref(variant);
+                       g_free(key);
+                       variant = NULL;
+                       key = NULL;
                        break;
+               } else {
+                       gchar *value;
+                       value = g_variant_print(variant, TRUE);
+                       DBG("Supplicant %s : %s", key, value);
+
+                       g_free(value);
                }
        }
 
@@ -828,16 +939,83 @@ 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;
-       char ssid[32];
-       char wps_key[100];
+       char ssid[32] = {0, };
+       char wps_key[100] = {0, };
        GVariantIter *iter;
        GVariant *variant;
        int config_error = 0;
        int error_indication = 0;
        gsize ssid_len = 0;
+       char *key_mgmt = NULL;
 
        if (param == NULL) {
                ERR("Param is NULL");
@@ -856,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);
@@ -869,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);
@@ -883,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