Store MAC address in lower case to vconf
[platform/core/connectivity/net-config.git] / src / wifi-eap-config.c
index 48597c9..1791ecf 100755 (executable)
@@ -29,6 +29,7 @@
 #include "wifi-state.h"
 #include "wifi-config.h"
 #include "wifi-eap-config.h"
+#include "network-state.h"
 #include "neterror.h"
 
 #define CONNMAN_CONFIG_FIELD_TYPE                      "Type"
@@ -68,7 +69,7 @@ static char *__get_encoded_ssid(const char *name)
        return str;
 }
 
-static int __config_save(const char *ssid, GKeyFile *keyfile)
+static int __config_save(const char *mac_addr, GKeyFile *keyfile)
 {
        gchar *data = NULL;
        gchar *config_file = NULL;
@@ -76,7 +77,7 @@ static int __config_save(const char *ssid, GKeyFile *keyfile)
        FILE *file = NULL;
        int err = 0;
 
-       config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR, ssid);
+       config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR, mac_addr);
        if (config_file == NULL) {
                err = -ENOMEM;
                goto out;
@@ -111,7 +112,7 @@ out:
        return err;
 }
 
-static int __config_delete(const char *ssid)
+static int __config_delete(const char *mac_addr, const char *ssid)
 {
        int err = 0;
        gchar *group_name = NULL;
@@ -121,7 +122,7 @@ static int __config_delete(const char *ssid)
        GKeyFile *keyfile = NULL;
        GError *error = NULL;
 
-       config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR, ssid);
+       config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR, mac_addr);
        if (config_file == NULL)
                return -ENOMEM;
 
@@ -165,7 +166,7 @@ static int __config_delete(const char *ssid)
                ERR("Failed to remove %s", cert_path);
        g_free(cert_path);
 
-       dirname = g_strdup_printf("%s/%s", WIFI_CERT_STORAGEDIR, ssid);
+       dirname = g_strdup_printf("%s/%s_%s", WIFI_CERT_STORAGEDIR, mac_addr, ssid);
        if (dirname != NULL) {
                if (g_file_test(dirname, G_FILE_TEST_EXISTS) == TRUE)
                        if (g_file_test(dirname, G_FILE_TEST_IS_DIR) == TRUE)
@@ -208,7 +209,7 @@ static gboolean __netconfig_copy_config(const char *src, const char *dst)
                g_error_free(error);
        }
 
-       INFO("Successfully installed[%d]", length);
+       INFO("Successfully installed[%zd]", length);
        g_free(buf);
 
        if (remove(src) != 0)
@@ -217,7 +218,7 @@ static gboolean __netconfig_copy_config(const char *src, const char *dst)
        return result;
 }
 
-static gboolean __netconfig_create_config(GVariant *fields)
+static gboolean __netconfig_create_config(const char *profile, GVariant *fields)
 {
        GKeyFile *keyfile = NULL;
        GVariantIter *iter;
@@ -228,6 +229,7 @@ static gboolean __netconfig_create_config(GVariant *fields)
        gboolean updated = FALSE;
        gchar *cert_file = NULL;
        gchar *cert_path = NULL;
+       char mac_str[13] = { 0, };
        int err = 0;
 
        g_variant_get(fields, "a{ss}", &iter);
@@ -254,6 +256,9 @@ static gboolean __netconfig_create_config(GVariant *fields)
                goto out;
        }
 
+       memcpy(mac_str, &profile[strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX)], 12);
+       mac_str[12] = '\0';
+
        /* Create unique service group name */
        group_name = g_strdup_printf("service_%s", encoded_ssid);
        if (group_name == NULL) {
@@ -275,8 +280,6 @@ static gboolean __netconfig_create_config(GVariant *fields)
                                g_strcmp0(field, CONNMAN_CONFIG_FIELD_EAP_METHOD) == 0 ||
                                g_strcmp0(field, CONNMAN_CONFIG_FIELD_PHASE2) ||
                                g_strcmp0(field, CONNMAN_CONFIG_FIELD_KEYMGMT_TYPE) == 0) {
-                       DBG("field: %s, value: %s", field, value);
-
                        if (value != NULL)
                                g_key_file_set_string(keyfile, group_name, field, value);
                } else if (g_strcmp0(field, CONNMAN_CONFIG_FIELD_CA_CERT_FILE) == 0 ||
@@ -293,10 +296,9 @@ static gboolean __netconfig_create_config(GVariant *fields)
                                }
 
                                cert_file++;
-                               DBG("field: %s, value: %s", field, cert_file);
 
-                               dirname = g_strdup_printf("%s/%s",
-                                               WIFI_CERT_STORAGEDIR, encoded_ssid);
+                               dirname = g_strdup_printf("%s/%s_%s", WIFI_CERT_STORAGEDIR,
+                                       mac_str, encoded_ssid);
                                if (dirname == NULL) {
                                        ERR("Failed to create dirname");
                                        g_free(field);
@@ -316,8 +318,8 @@ static gboolean __netconfig_create_config(GVariant *fields)
                                }
                                g_free(dirname);
 
-                               cert_path = g_strdup_printf("%s/%s/%s",
-                                               WIFI_CERT_STORAGEDIR, encoded_ssid, cert_file);
+                               cert_path = g_strdup_printf("%s/%s_%s/%s", WIFI_CERT_STORAGEDIR,
+                                       mac_str, encoded_ssid, cert_file);
                                if (cert_path == NULL) {
                                        ERR("Failed to create cert path");
                                        g_free(field);
@@ -336,18 +338,16 @@ static gboolean __netconfig_create_config(GVariant *fields)
                                g_free(cert_path);
                        }
                } else {
-                       DBG("field: %s, value: %s", field, value);
-
                        if (value != NULL)
                                g_key_file_set_string(keyfile, group_name, field, value);
                }
        }
 
-       err = __config_save((const char *)encoded_ssid, keyfile);
+       err = __config_save(mac_str, keyfile);
        if (err < 0)
-               ERR("Failed to create configuration %s[%d]", encoded_ssid, err);
+               ERR("Failed to create configuration %s[%d]", mac_str, err);
        else {
-               DBG("Successfully created %s", encoded_ssid);
+               DBG("Successfully created %s", mac_str);
                updated = TRUE;
        }
 
@@ -363,7 +363,7 @@ out:
        return updated;
 }
 
-static gboolean _delete_configuration(const gchar *profile)
+static gboolean _delete_configuration(const char *interface_name, const char *profile)
 {
        gboolean ret = FALSE;
        gchar *config_id = NULL;
@@ -375,7 +375,7 @@ static gboolean _delete_configuration(const gchar *profile)
        }
        ERR("get config_id [%s] from [%s]", config_id, profile);
 
-       ret = wifi_config_remove_configuration(config_id);
+       ret = wifi_config_remove_configuration(interface_name, config_id);
        if (ret != TRUE)
                ERR("Fail to wifi_config_remove_configuration [%s]", config_id);
 
@@ -393,13 +393,21 @@ static gboolean __netconfig_delete_config(const char *profile)
        char *ssid = NULL;
        int ssid_len = 0;
        int err = 0;
+       const char *interface_name = NULL;
+       char mac_str[13] = { 0, };
 
        if (NULL == profile) {
                ERR("Invalid profile name");
                return FALSE;
        }
 
-       if (_delete_configuration(profile) != TRUE)
+       interface_name = netconfig_get_ifname(profile);
+       if (interface_name == NULL) {
+               ERR("Invalid profile name");
+               return FALSE;
+       }
+
+       if (_delete_configuration(interface_name, profile) != TRUE)
                ERR("Fail to delete configuration [%s]", profile);
 
        wifi_ident = strstr(profile, "wifi_");
@@ -426,7 +434,11 @@ static gboolean __netconfig_delete_config(const char *profile)
        }
 
        g_strlcpy(ssid, essid, ssid_len + 1); /* include NULL-terminated */
-       err = __config_delete((const char *)ssid);
+
+       memcpy(mac_str, &profile[strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX)], 12);
+       mac_str[12] = '\0';
+
+       err = __config_delete(mac_str, ssid);
        if (err < 0) {
                ERR("Failed to delete configuration %s[%d]", ssid, err);
                g_free(ssid);
@@ -439,21 +451,17 @@ static gboolean __netconfig_delete_config(const char *profile)
        return TRUE;
 }
 
-static void __netconfig_eap_state(
-               wifi_service_state_e state, void *user_data);
-
-static wifi_state_notifier netconfig_eap_notifier = {
-               .wifi_state_changed = __netconfig_eap_state,
-               .user_data = NULL,
-};
-
-static void __netconfig_eap_state(
-               wifi_service_state_e state, void *user_data)
+static void __netconfig_eap_state(wifi_state_notifier_s *notifier,
+               char *service, wifi_service_state_e state, void *user_data)
 {
-       const char *wifi_profile = (const char *)user_data;
+       if (notifier == NULL)
+               return;
 
-       if (wifi_profile == NULL) {
-               wifi_state_notifier_unregister(&netconfig_eap_notifier);
+       if (service == NULL) {
+               wifi_state_notifier_unregister(notifier);
+               g_free(notifier->service);
+               g_free(notifier->user_data);
+               g_free(notifier);
                return;
        }
 
@@ -461,12 +469,12 @@ static void __netconfig_eap_state(
                return;
 
        if (state == NETCONFIG_WIFI_FAILURE)
-               __netconfig_delete_config(wifi_profile);
-
-       g_free(netconfig_eap_notifier.user_data);
-       netconfig_eap_notifier.user_data = NULL;
+               __netconfig_delete_config(service);
 
-       wifi_state_notifier_unregister(&netconfig_eap_notifier);
+       wifi_state_notifier_unregister(notifier);
+       g_free(notifier->service);
+       g_free(notifier->user_data);
+       g_free(notifier);
 }
 
 gboolean handle_create_eap_config(Wifi *wifi, GDBusMethodInvocation *context,
@@ -474,17 +482,27 @@ gboolean handle_create_eap_config(Wifi *wifi, GDBusMethodInvocation *context,
 {
        gboolean updated = FALSE;
        gboolean result = FALSE;
+       wifi_state_notifier_s *state_notifier = NULL;
+       const char *interface_name = NULL;
 
        g_return_val_if_fail(wifi != NULL, TRUE);
 
        DBG("Set agent fields for %s", service);
 
        if (netconfig_is_wifi_profile(service) != TRUE) {
-               netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_WRONG_PROFILE, "InvalidService");
+               netconfig_error_dbus_method_return(context,
+                       NETCONFIG_ERROR_WRONG_PROFILE, "InvalidService");
+               return TRUE;
+       }
+
+       interface_name = netconfig_get_ifname(service);
+       if (interface_name == NULL) {
+               netconfig_error_dbus_method_return(context,
+                       NETCONFIG_ERROR_WRONG_PROFILE, "InvalidService");
                return TRUE;
        }
 
-       updated = __netconfig_create_config(fields);
+       updated = __netconfig_create_config(service, fields);
        if (updated == TRUE) {
                wifi_complete_create_eap_config(wifi, context);
 
@@ -508,25 +526,24 @@ gboolean handle_create_eap_config(Wifi *wifi, GDBusMethodInvocation *context,
                                        passphrase = (const char *)value;
                        }
 
-                       netconfig_wifi_set_agent_field_for_eap_network(
-                                                                       name, identity, passphrase);
+                       netconfig_wifi_set_agent_field_for_eap_network(interface_name,
+                               name, identity, passphrase);
 
                        g_variant_iter_free(iter);
                }
 
                result = netconfig_invoke_dbus_method_nonblock(CONNMAN_SERVICE,
                                service, CONNMAN_SERVICE_INTERFACE, "Connect",
-                               NULL, __netconfig_wifi_connect_reply);
-
-               if (netconfig_eap_notifier.user_data != NULL) {
-                       g_free(netconfig_eap_notifier.user_data);
-                       netconfig_eap_notifier.user_data = NULL;
-
-                       wifi_state_notifier_unregister(&netconfig_eap_notifier);
+                               NULL, __netconfig_wifi_connect_reply,
+                               g_strdup(interface_name));
+
+               state_notifier = g_try_malloc0(sizeof(wifi_state_notifier_s));
+               if (state_notifier) {
+                       state_notifier->notifier = state_notifier;
+                       state_notifier->service = g_strdup(service);
+                       state_notifier->wifi_state_changed = __netconfig_eap_state;
+                       wifi_state_notifier_register(state_notifier);
                }
-
-               netconfig_eap_notifier.user_data = g_strdup(service);
-               wifi_state_notifier_register(&netconfig_eap_notifier);
        } else {
                netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INVALID_PARAMETER, "InvalidArguments");
        }
@@ -542,9 +559,9 @@ gboolean handle_delete_eap_config(Wifi *wifi, GDBusMethodInvocation *context,
 {
        g_return_val_if_fail(wifi != NULL, TRUE);
 
-       wifi_complete_delete_eap_config(wifi, context);
+       __netconfig_delete_config(profile);
 
-       __netconfig_delete_config((const char *)profile);
+       wifi_complete_delete_eap_config(wifi, context);
 
        return TRUE;
 }