Set passphrase for connman service when connecting using WPS without SSID
[platform/core/connectivity/net-config.git] / src / signal-handler.c
index 4fe67ad..fc0b1cf 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"
@@ -191,6 +195,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)
@@ -255,20 +324,20 @@ 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;
+               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();
                        }
-               } 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;
@@ -453,6 +522,17 @@ 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:
@@ -858,6 +938,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;
@@ -868,6 +1014,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");
@@ -886,7 +1033,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);
@@ -899,7 +1047,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);
@@ -913,11 +1062,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