[WGID-200328, 200330, 200606] Fixed some codes for secure coding
[platform/core/connectivity/net-config.git] / src / wifi-config.c
index 080fb6f..2023fd9 100755 (executable)
@@ -18,6 +18,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <dirent.h>
@@ -103,43 +104,45 @@ static gboolean __get_mac_address(gchar **mac_address)
        gchar *tmp = NULL;
        gchar mac[13] = { 0, };
        gint i = 0, j = 0;
-#if defined TIZEN_TV
-       FILE *fp = NULL;
-       char buf[WIFI_MAC_ADD_LENGTH + 1];
-       if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
-               fp = fopen(WIFI_MAC_ADD_PATH, "r");
-
-       if (fp == NULL) {
-               ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
-               *mac_address = NULL;
-               return FALSE;
-       }
 
-       if (fgets(buf, sizeof(buf), fp) == NULL) {
-               ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
-               *mac_address = NULL;
-               fclose(fp);
-               return FALSE;
-       }
-       tmp_mac = (char *)g_try_malloc0(WIFI_MAC_ADD_LENGTH + 1);
-       if (tmp_mac == NULL) {
-               ERR("malloc() failed");
-               *mac_address = NULL;
+       if (TIZEN_TV) {
+               FILE *fp = NULL;
+               char buf[WIFI_MAC_ADD_LENGTH + 1];
+               if (0 == access(WIFI_MAC_ADD_PATH, F_OK))
+                       fp = fopen(WIFI_MAC_ADD_PATH, "r");
+
+               if (fp == NULL) {
+                       ERR("Failed to open file %s\n", WIFI_MAC_ADD_PATH);
+                       *mac_address = NULL;
+                       return FALSE;
+               }
+
+               if (fgets(buf, sizeof(buf), fp) == NULL) {
+                       ERR("Failed to get MAC info from %s\n", WIFI_MAC_ADD_PATH);
+                       *mac_address = NULL;
+                       fclose(fp);
+                       return FALSE;
+               }
+               tmp_mac = (gchar *)malloc(WIFI_MAC_ADD_LENGTH + 1);
+               if (tmp_mac == NULL) {
+                       ERR("malloc() failed");
+                       *mac_address = NULL;
+                       fclose(fp);
+                       return FALSE;
+               }
+               memset(tmp_mac, 0, WIFI_MAC_ADD_LENGTH + 1);
+               g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
                fclose(fp);
-               return FALSE;
-       }
-       g_strlcpy(tmp_mac, buf, WIFI_MAC_ADD_LENGTH + 1);
-       fclose(fp);
-#else
-       tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
-       if (tmp_mac == NULL) {
-               ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
-               *mac_address = NULL;
-               return FALSE;
+       } else {
+               tmp_mac = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
+               if (tmp_mac == NULL) {
+                       ERR("vconf_get_str(WIFI_BSSID_ADDRESS) Failed");
+                       *mac_address = NULL;
+                       return FALSE;
+               }
        }
-#endif
        tmp = g_ascii_strdown(tmp_mac, (gssize)strlen(tmp_mac));
-       g_free(tmp_mac);
+       free(tmp_mac);
        while (tmp && tmp[i]) {
                if (tmp[i] != ':')
                        mac[j++] = tmp[i];
@@ -215,10 +218,10 @@ 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);
-       }
+
+       g_free(path);
 
        return keyfile;
 }
@@ -430,6 +433,7 @@ static gboolean _set_field(const gchar *config_id, const gchar *key, const gchar
        keyfile = __get_configuration_keyfile(group_name);
        if (keyfile == NULL) {
                ERR("Fail to __get_configuration_keyfile");
+               g_free(group_name);
                return FALSE;
        }
 
@@ -489,6 +493,7 @@ static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **val
        keyfile = __get_configuration_keyfile(group_name);
        if (keyfile == NULL) {
                ERR("Fail to __get_configuration_keyfile");
+               g_free(group_name);
                return FALSE;
        }
 
@@ -539,7 +544,6 @@ static gboolean _get_field(const gchar *config_id, const gchar *key, gchar **val
 static GSList *_get_list(void)
 {
        GSList *list = NULL;
-       struct dirent ent_struct;
        struct dirent *dp = NULL;
        DIR *dir;
 
@@ -549,7 +553,7 @@ static GSList *_get_list(void)
                return NULL;
        }
 
-       while ((readdir_r(dir, &ent_struct, &dp) == 0) && dp) {
+       while ((dp = readdir(dir)) != NULL) {
                if (g_strcmp0(dp->d_name, ".") == 0 || g_strcmp0(dp->d_name, "..") == 0 ||
                                strncmp(dp->d_name, WIFI_CONFIG_PREFIX, strlen(WIFI_CONFIG_PREFIX)) != 0) {
                        continue;
@@ -619,7 +623,7 @@ gboolean handle_get_config_ids(Wifi *wifi, GDBusMethodInvocation *context)
        wifi_complete_get_config_ids(wifi, context, (const gchar * const *)result);
 
        for (i = 0; i < length; i++)
-               if(result[i])
+               if (result[i])
                        g_free(result[i]);
 
        if (result)
@@ -737,6 +741,12 @@ gboolean handle_save_configuration(Wifi *wifi, GDBusMethodInvocation *context,
 
        ret = __get_group_name(WIFI_CONFIG_PREFIX, config_id, &group_name);
        if (ret != TRUE) {
+               g_free(conf->name);
+               g_free(conf->ssid);
+               g_free(conf->passphrase);
+               g_free(conf->is_hidden);
+               g_free(conf->proxy_address);
+               g_free(conf);
                ERR("Fail to get_wifi_config_group_name");
                return FALSE;
        }
@@ -811,14 +821,14 @@ gboolean handle_load_eap_configuration(Wifi *wifi, GDBusMethodInvocation *contex
        g_variant_builder_add(b, "{sv}", WIFI_CONFIG_NAME, g_variant_new_string(conf->name));
        g_variant_builder_add(b, "{sv}", WIFI_CONFIG_SECURITY_TYPE, g_variant_new_string(conf->security_type));
        g_variant_builder_add(b, "{sv}", WIFI_CONFIG_HIDDEN, g_variant_new_string(conf->is_hidden));
-       if (conf->proxy_address != NULL) {
+       if (conf->proxy_address != NULL)
                g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string(conf->proxy_address));
-       else
+       else
                g_variant_builder_add(b, "{sv}", WIFI_CONFIG_PROXYADDRESS, g_variant_new_string("NONE"));
 
-       if (conf->last_error != NULL) {
+       if (conf->last_error != NULL)
                g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string(conf->last_error));
-       else
+       else
                g_variant_builder_add(b, "{sv}", WIFI_CONFIG_FAILURE, g_variant_new_string("ERROR_NONE"));
 
        if (conf->eap_config != NULL) {