Fixed some coverity
[platform/core/connectivity/net-config.git] / src / wifi-config.c
index bd1c112..aea051b 100755 (executable)
@@ -32,6 +32,8 @@
 #include "util.h"
 #include "neterror.h"
 #include "wifi-config.h"
+#include "netsupplicant.h"
+#include "wifi-key-encryption.h"
 
 #define CONNMAN_STORAGE         "/var/lib/connman"
 
@@ -163,6 +165,7 @@ static gboolean __get_group_name(const gchar *prefix, const gchar *config_id, gc
        ret = __get_mac_address(&mac_address);
        if ((ret != TRUE) || (strlen(mac_address) == 0)) {
                ERR("Cannot get WIFI MAC address");
+               g_free(mac_address);
                return FALSE;
        }
 
@@ -218,9 +221,8 @@ static GKeyFile *__get_configuration_keyfile(const gchar *group_name)
        path = g_strdup_printf(CONNMAN_STORAGE "/%s/settings", group_name);
 
        keyfile = netconfig_keyfile_load(path);
-       if (keyfile == NULL) {
+       if (keyfile == NULL)
                ERR("keyfile[%s] is NULL", path);
-       }
 
        g_free(path);
 
@@ -289,8 +291,8 @@ static gboolean _load_configuration(const gchar *config_id, struct wifi_config *
        config->name = g_key_file_get_string(keyfile, group_name, WIFI_CONFIG_NAME, NULL);
        DBG("name [%s]", config->name);
 
-       ret = __get_security_type(config_id, &config->security_type);
-       if (ret != TRUE) {
+       __get_security_type(config_id, &config->security_type);
+       if (config->security_type == NULL) {
                ERR("Fail to _get_security_type");
                g_key_file_free(keyfile);
                g_free(group_name);
@@ -594,6 +596,319 @@ gboolean wifi_config_remove_configuration(const gchar *config_id)
        return ret;
 }
 
+int __netconfig_hex_char_to_num(char c)
+{
+       if (c >= '0' && c <= '9')
+               return c - '0';
+
+       if (c >= 'a' && c <= 'f')
+               return c - 'a' + 10;
+
+       if (c >= 'A' && c <= 'F')
+               return c - 'A' + 10;
+
+       return -1;
+}
+
+int __netconfig_hex_to_byte(const char *hex)
+{
+       int a, b;
+
+       a = __netconfig_hex_char_to_num(*hex++);
+       if (a < 0)
+               return -1;
+
+       b = __netconfig_hex_char_to_num(*hex++);
+       if (b < 0)
+               return -1;
+
+       return (a << 4) | b;
+}
+
+int __netconfig_hex_str_to_bin(const char *hex, unsigned char *buf, size_t len)
+{
+       size_t i;
+       int a;
+       const char *ipos = hex;
+       unsigned char *opos = buf;
+
+       for (i = 0; i < len; i++) {
+               a = __netconfig_hex_to_byte(ipos);
+               if (a < 0)
+                       return -1;
+
+               *opos++ = a;
+               ipos += 2;
+       }
+
+       return 0;
+}
+
+static int __netconfig_byte_to_txt(const unsigned char *src, char **dst, int src_len)
+{
+       int dst_length = 0;
+       int i = 0;
+       char *buf = NULL;
+
+       if (src_len <= 0) {
+               ERR("Invalid parameter.");
+               return -1;
+       }
+
+       *dst = (char *) g_try_malloc0((2*src_len)+1);
+       if (!(*dst)) {
+               ERR("failed to allocate memory to buffer.");
+               return -1;
+       }
+
+       buf = (*dst);
+
+       for (i = 0; i < src_len; i++) {
+               snprintf(buf, 3, "%02x", src[i]);
+               buf += 2;
+               dst_length += 2;
+       }
+
+       return dst_length;
+}
+
+static int __netconfig_unpack_ay_malloc(unsigned char **dst, GVariantIter *iter)
+{
+       GVariantIter *iter_copy = NULL;
+       int length = 0;
+       char tmp = 0;
+       unsigned char *tmp_dst = NULL;
+
+       if (!dst || *dst || !iter) {
+               ERR("Invalid parameter");
+               return 0;
+       }
+
+       iter_copy = g_variant_iter_copy(iter);
+
+       while (g_variant_iter_loop(iter, "y", &tmp))
+               length++;
+       g_variant_iter_free(iter);
+
+       tmp_dst = (unsigned char *)g_try_malloc0(length + 1);
+       if (!tmp_dst) {
+               ERR("failed to allocate memory");
+               g_variant_iter_free(iter_copy);
+               return 0;
+       }
+
+       length = 0;
+       while (g_variant_iter_loop(iter_copy, "y", &tmp_dst[length]))
+               length++;
+       g_variant_iter_free(iter_copy);
+
+       if (length == 0) {
+               g_free(tmp_dst);
+               tmp_dst = NULL;
+       } else {
+               tmp_dst[length] = '\0';
+       }
+
+       *dst = tmp_dst;
+       DBG("Length [%d]", length);
+       return length;
+}
+
+gboolean _add_vsie(int frame_id, const char* vsie)
+{
+       GVariant *params = NULL;
+       GVariant *message = NULL;
+       GVariantBuilder *bytearray_builder = NULL;
+       char *if_path;
+       int i = 0;
+       size_t vsie_len = 0;
+
+       unsigned char *bytearray = NULL;
+       size_t bytearray_len = 0;
+
+       if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
+               DBG("Invalid parameter, frame-id: %d", frame_id);
+               return FALSE;
+       }
+
+       vsie_len = strlen(vsie);
+       if (vsie_len == 0) {
+               DBG("vsie length is zero");
+               return FALSE;
+       }
+
+       bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
+
+       bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
+       if (bytearray == NULL) {
+               DBG("Failed to allocate memory to bytearray");
+               return FALSE;
+       }
+
+       if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
+               DBG("invalid vsie string");
+               g_free(bytearray);
+               return FALSE;
+       }
+
+       bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       for (i = 0; i < bytearray_len; i++)
+               g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
+
+       params = g_variant_new("(iay)", frame_id, bytearray_builder);
+       g_variant_builder_unref(bytearray_builder);
+
+       if_path = netconfig_wifi_get_supplicant_interface();
+
+       if (if_path == NULL) {
+               ERR("Fail to get wpa_supplicant DBus path");
+               g_free(bytearray);
+               return FALSE;
+       }
+
+       message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
+                       if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemAdd", params);
+
+       g_free(if_path);
+       if (message == NULL) {
+               ERR("Failed to send command to wpa_supplicant");
+               g_free(bytearray);
+               return FALSE;
+       }
+
+       DBG("Succeeded to add vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
+
+       g_free(bytearray);
+       return TRUE;
+}
+
+gboolean _get_vsie(int frame_id, char **vsie)
+{
+       GVariant *params = NULL;
+       GVariant *message = NULL;
+       char *if_path;
+
+       if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
+               DBG("Invalid parameter, frame-id: %d", frame_id);
+               return FALSE;
+       }
+
+       if_path = netconfig_wifi_get_supplicant_interface();
+       if (if_path == NULL) {
+               ERR("Fail to get wpa_supplicant DBus path");
+               return FALSE;
+       }
+
+       params = g_variant_new("(i)", frame_id);
+
+       message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
+                       if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemGet", params);
+
+       g_free(if_path);
+       if (message == NULL) {
+               ERR("Failed to send command to wpa_supplicant");
+               return FALSE;
+       } else {
+               GVariantIter *iter = NULL;
+               unsigned char *vsie_bytes = NULL;
+               int vsie_len = 0;
+               int ret = 0;
+
+               g_variant_get(message, "(ay)", &iter);
+               if (iter == NULL) {
+                       ERR("vsie is not present");
+                       return FALSE;
+               }
+
+               vsie_len = __netconfig_unpack_ay_malloc(&vsie_bytes, iter);
+               if (vsie_bytes == NULL) {
+                       ERR("vsie_bytes not allocated");
+                       return FALSE;
+               }
+
+               ret = __netconfig_byte_to_txt(vsie_bytes, vsie, vsie_len);
+               if (ret < 0) {
+                       g_free(vsie_bytes);
+                       ERR("vsie not allocated.");
+                       return FALSE;
+               }
+
+               g_free(vsie_bytes);
+       }
+
+       ERR("Succeeded to get vsie: Frame ID[%d], VSIE[%s]", frame_id, *vsie);
+
+       return TRUE;
+
+}
+
+gboolean _remove_vsie(int frame_id, const char *vsie)
+{
+       GVariant *params = NULL;
+       GVariant *message = NULL;
+       GVariantBuilder *bytearray_builder = NULL;
+       char *if_path;
+       int i = 0;
+       size_t vsie_len = 0;
+
+       unsigned char *bytearray = NULL;
+       size_t bytearray_len = 0;
+
+       if (frame_id >= NETCONFIG_VSIE_FRAME_MAX) {
+               DBG("Invalid parameter, frame-id: %d", frame_id);
+               return FALSE;
+       }
+
+       vsie_len = strlen(vsie);
+       if (vsie_len == 0) {
+               DBG("vsie length is zero");
+               return FALSE;
+       }
+
+       bytearray_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
+
+       bytearray = (unsigned char *) g_try_malloc0(bytearray_len);
+       if (bytearray == NULL) {
+               DBG("Failed to allocate memory to bytearray");
+               return FALSE;
+       }
+
+       if (__netconfig_hex_str_to_bin(vsie, bytearray, bytearray_len) < 0) {
+               DBG("invalid vsie string");
+               g_free(bytearray);
+               return FALSE;
+       }
+
+       bytearray_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       for (i = 0; i < bytearray_len; i++)
+               g_variant_builder_add(bytearray_builder, "y", bytearray[i]);
+
+       params = g_variant_new("(iay)", frame_id, bytearray_builder);
+       g_variant_builder_unref(bytearray_builder);
+
+       if_path = netconfig_wifi_get_supplicant_interface();
+       if (if_path == NULL) {
+               ERR("Fail to get wpa_supplicant DBus path");
+               g_free(bytearray);
+               return FALSE;
+       }
+
+       message = netconfig_supplicant_invoke_dbus_method(SUPPLICANT_SERVICE,
+                       if_path, SUPPLICANT_INTERFACE ".Interface", "VendorElemRem", params);
+
+       g_free(if_path);
+       if (message == NULL) {
+               ERR("Failed to send command to wpa_supplicant");
+               g_free(bytearray);
+               return FALSE;
+       }
+
+       DBG("Succeeded to remove vsie: Frame ID[%d], VSIE[%s]", frame_id, vsie);
+
+       g_free(bytearray);
+       return TRUE;
+}
+
 /* dbus method */
 gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
 {
@@ -602,13 +917,13 @@ gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
        guint length;
        gchar **result = NULL;
 
-       g_return_val_if_fail(wifi != NULL, FALSE);
+       g_return_val_if_fail(wifi != NULL, TRUE);
 
        config_ids = _get_list();
        if (config_ids == NULL) {
-               netconfig_error_no_profile(context);
                ERR("Fail to get config list");
-               return FALSE;
+               netconfig_error_no_profile(context);
+               return TRUE;
        }
 
        length = g_slist_length(config_ids);
@@ -640,7 +955,7 @@ gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
        GVariantBuilder *b = NULL;
        struct wifi_config *conf = NULL;
 
-       g_return_val_if_fail(wifi != NULL, FALSE);
+       g_return_val_if_fail(wifi != NULL, TRUE);
 
        conf = g_new0(struct wifi_config, 1);
 
@@ -649,7 +964,7 @@ gboolean handle_load_configuration(Wifi *wifi, GDBusMethodInvocation *context,
                g_free(conf);
                ERR("Fail to _load_configuration");
                netconfig_error_no_profile(context);
-               return FALSE;
+               return TRUE;
        }
 
        b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
@@ -693,7 +1008,7 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
        if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
                ERR("Invalid parameter");
                netconfig_error_invalid_parameter(context);
-               return FALSE;
+               return TRUE;
        }
 
        conf = g_new0(struct wifi_config, 1);
@@ -749,15 +1064,27 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
                g_free(conf->proxy_address);
                g_free(conf);
                ERR("Fail to get_wifi_config_group_name");
-               return FALSE;
+               netconfig_error_fail_save_congifuration(context);
+               return TRUE;
        }
 
        keyfile = g_key_file_new();
        g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
        g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
 
-       if (conf->passphrase != NULL)
+       if (conf->passphrase != NULL) {
+               gchar *enc_data = NULL;
+               enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
+
+               if (!enc_data) {
+                       ERR("Failed to encrypt the passphrase");
+               } else {
+                       g_free(conf->passphrase);
+                       conf->passphrase = enc_data;
+               }
+
                g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
+       }
 
        g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
        g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
@@ -785,6 +1112,7 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
        }
 
        g_key_file_free(keyfile);
+       g_free(group_name);
        g_free(conf->name);
        g_free(conf->ssid);
        g_free(conf->passphrase);
@@ -794,7 +1122,7 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
 
        g_variant_iter_free(iter);
 
-       return ret;
+       return TRUE;
 }
 
 gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *context,
@@ -804,7 +1132,7 @@ gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *contex
        GVariantBuilder *b = NULL;
        struct wifi_config *conf = NULL;
 
-       g_return_val_if_fail(wifi != NULL, FALSE);
+       g_return_val_if_fail(wifi != NULL, TRUE);
 
        conf = g_new0(struct wifi_config, 1);
        conf->eap_config = g_new0(struct wifi_eap_config, 1);
@@ -815,7 +1143,7 @@ gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *contex
                g_free(conf);
                ERR("Fail to _load_configuration");
                netconfig_error_no_profile(context);
-               return FALSE;
+               return TRUE;
        }
 
        b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
@@ -895,7 +1223,7 @@ gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *contex
        if ((wifi == NULL) || (config_id == NULL) || (configuration == NULL)) {
                ERR("Invalid parameter");
                netconfig_error_invalid_parameter(context);
-               return FALSE;
+               return TRUE;
        }
 
        conf = g_new0(struct wifi_config, 1);
@@ -1003,15 +1331,25 @@ gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *contex
        if (ret != TRUE) {
                __free_wifi_configuration(conf);
                ERR("Fail to get_wifi_config_group_name");
-               return FALSE;
+               return TRUE;
        }
 
        keyfile = g_key_file_new();
        g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_NAME, conf->name);
        g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_SSID, conf->ssid);
 
-       if (conf->passphrase != NULL)
+       if (conf->passphrase != NULL) {
+               gchar *enc_data = NULL;
+               enc_data = _netconfig_encrypt_passphrase(conf->passphrase);
+
+               if (!enc_data) {
+                       ERR("Failed to encrypt the passphrase");
+               } else {
+                       g_free(conf->passphrase);
+                       conf->passphrase = enc_data;
+               }
                g_key_file_set_string(keyfile, group_name, WIFI_CONFIG_PASSPHRASE, conf->passphrase);
+       }
 
        g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_FAVORITE, conf->favorite);
        g_key_file_set_boolean(keyfile, group_name, WIFI_CONFIG_AUTOCONNECT, conf->autoconnect);
@@ -1071,11 +1409,12 @@ gboolean handle_save_eap_configuration(Wifi *wifi, GDBusMethodInvocation *contex
        }
 
        g_key_file_free(keyfile);
+       g_free(group_name);
        __free_wifi_configuration(conf);
 
        g_variant_iter_free(iter);
 
-       return ret;
+       return TRUE;
 }
 
 gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
@@ -1085,7 +1424,7 @@ gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
        if ((wifi == NULL) || (config_id == NULL)) {
                ERR("Invalid parameter");
                netconfig_error_invalid_parameter(context);
-               return FALSE;
+               return TRUE;
        }
 
        ret = _remove_configuration(config_id);
@@ -1093,11 +1432,11 @@ gboolean handle_remove_configuration(Wifi *wifi, GDBusMethodInvocation *context,
                /* no configuration or error */
                ERR("No [%s] configuration", config_id);
                netconfig_error_no_profile(context);
-               return FALSE;
+               return TRUE;
        }
 
        wifi_complete_remove_configuration(wifi, context);
-       return ret;
+       return TRUE;
 }
 
 /* config field key / value */
@@ -1126,9 +1465,9 @@ gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
        gboolean ret = FALSE;
        gchar *keyfile_key = NULL;
 
-       g_return_val_if_fail(wifi != NULL, FALSE);
-       g_return_val_if_fail(config_id != NULL, FALSE);
-       g_return_val_if_fail(key != NULL, FALSE);
+       g_return_val_if_fail(wifi != NULL, TRUE);
+       g_return_val_if_fail(config_id != NULL, TRUE);
+       g_return_val_if_fail(key != NULL, TRUE);
 
        DBG("Key[%s] Value[%d]", key, value);
 
@@ -1137,7 +1476,7 @@ gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
                if (!ret) {
                        ERR("Fail to [%s]set_wifi_config_field(%s/manual)", config_id, WIFI_CONFIG_PROXY_METHOD);
                        netconfig_error_invalid_parameter(context);
-                       return FALSE;
+                       return TRUE;
                }
                keyfile_key = g_strdup_printf("%s", WIFI_CONFIG_PROXY_SERVER);
        } else if (g_strcmp0(key, WIFI_CONFIG_HIDDEN) == 0) {
@@ -1161,20 +1500,19 @@ gboolean handle_set_config_field(Wifi *wifi, GDBusMethodInvocation *context,
        } else {
                ERR("Not supported key[%s]", key);
                netconfig_error_invalid_parameter(context);
-               return FALSE;
+               return TRUE;
        }
 
        ret = _set_field(config_id, keyfile_key, (const gchar *)value);
        if (!ret) {
                ERR("Fail to [%s]set_wifi_config_field(%s/%s)", config_id, key, value);
-               ret = FALSE;
        }
 
        if (keyfile_key != NULL)
                g_free(keyfile_key);
 
        wifi_complete_set_config_field(wifi, context);
-       return ret;
+       return TRUE;
 }
 
 gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context, const gchar *config_id)
@@ -1185,18 +1523,83 @@ gboolean handle_get_config_passphrase(Wifi *wifi, GDBusMethodInvocation *context
        if ((wifi == NULL) || (config_id == NULL)) {
                ERR("Invalid parameter");
                netconfig_error_invalid_parameter(context);
-               return FALSE;
+               return TRUE;
        }
 
        ret = _get_field(config_id, WIFI_CONFIG_PASSPHRASE, &passphrase);
        if (!ret) {
                ERR("Fail to [%s] _get_field(%s)", config_id, WIFI_CONFIG_PASSPHRASE);
                netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
-               return FALSE;
+               return TRUE;
        }
 
        wifi_complete_get_config_passphrase(wifi, context, passphrase);
        g_free(passphrase);
 
-       return ret;
+       return TRUE;
+}
+
+gboolean handle_add_vsie(Wifi *wifi, GDBusMethodInvocation *context,
+               int frame_id, const gchar *vsie)
+{
+       DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
+
+       g_return_val_if_fail(wifi != NULL, TRUE);
+       g_return_val_if_fail(vsie != NULL, TRUE);
+
+       gboolean ret = FALSE;
+
+       ret = _add_vsie(frame_id, vsie);
+       if (!ret) {
+               DBG("Failed to add vsie: %s", vsie);
+               netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
+               return TRUE;
+       }
+
+       wifi_complete_add_vsie(wifi, context);
+       return TRUE;
+}
+
+gboolean handle_get_vsie(Wifi *wifi, GDBusMethodInvocation *context,
+               int frame_id)
+{
+       DBG("Frame ID: [%d]", frame_id);
+
+       g_return_val_if_fail(wifi != NULL, TRUE);
+
+       gboolean ret = FALSE;
+       gchar *vsie = NULL;
+
+       ret = _get_vsie(frame_id, &vsie);
+       if (!ret) {
+               DBG("Failed to get vsie for frame:[%d]", frame_id);
+               netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
+               return TRUE;
+       }
+
+       DBG("Received vsie: %s", vsie);
+       wifi_complete_get_vsie(wifi, context, vsie);
+
+       return TRUE;
+}
+
+gboolean handle_remove_vsie(Wifi *wifi, GDBusMethodInvocation *context,
+               int frame_id, const gchar *vsie)
+{
+       DBG("Frame ID: [%d] VSIE: [%s]", frame_id, vsie);
+
+       g_return_val_if_fail(wifi != NULL, TRUE);
+       g_return_val_if_fail(vsie != NULL, TRUE);
+
+       gboolean ret = FALSE;
+
+       ret = _remove_vsie(frame_id, vsie);
+       if (!ret) {
+               DBG("Failed to remove vsie: %s", vsie);
+               netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "OperationFailed");
+               return TRUE;
+       }
+
+       wifi_complete_remove_vsie(wifi, context);
+       return TRUE;
 }