[WGID-200328, 200330, 200606] Fixed some codes for secure coding
[platform/core/connectivity/net-config.git] / src / utils / util.c
index 3124924..495567d 100755 (executable)
@@ -17,7 +17,7 @@
  *
  */
 
-#include <app.h>
+#include <dlfcn.h>
 #include <errno.h>
 #include <vconf.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <vconf-keys.h>
-#include <syspopup_caller.h>
-#include <bundle.h>
-#include <bundle_internal.h>
-#include <eventsystem.h>
 #include <tzplatform_config.h>
+#include <system_info.h>
 
 #include "log.h"
 #include "util.h"
@@ -48,6 +45,7 @@
 #define MAC_INFO_FILEPATH              tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
 #define MAC_ADDRESS_FILEPATH   "/sys/class/net/wlan0/address"
 #define MAC_ADDRESS_MAX_LEN            18
+#define HEADED_PLUGIN_FILEPATH         "/usr/lib/net-config-plugin-headed.so"
 
 static gboolean netconfig_device_picker_test = FALSE;
 static int mdnsd_ref_count = 0;
@@ -56,6 +54,10 @@ typedef struct {
        int conn_id;
 } dnssd_conn_destroy_data;
 
+static gboolean netconfig_plugin_headed_enabled = FALSE;
+static void *handle_headed;
+static struct netconfig_headed_plugin_t *headed_plugin;
+
 GKeyFile *netconfig_keyfile_load(const char *pathname)
 {
        GKeyFile *keyfile = NULL;
@@ -198,39 +200,13 @@ static gboolean __netconfig_test_device_picker()
 
 static void __netconfig_pop_device_picker(void)
 {
-#if defined TIZEN_WEARABLE
-       int ret = 0;
-       app_control_h   control = NULL;
-
-       ret = app_control_create(&control);
-       if (APP_CONTROL_ERROR_NONE != ret) {
-               DBG("failed to create app control");
-               return ;
-       }
-
-       app_control_add_extra_data(control, "viewtype", "scanlist");
-
-       app_control_set_app_id(control, "org.tizen.wifi");
-       ret = app_control_send_launch_request(control, NULL, NULL);
-       if (APP_CONTROL_ERROR_NONE == ret)
-               DBG("Launch request sent successfully");
-
-       app_control_destroy(control);
-#else
-       bundle *b = NULL;
-       int wifi_ug_state = 0;
-
-       netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
-       if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
+       if (!netconfig_plugin_headed_enabled)
                return;
 
-       b = bundle_create();
-
-       DBG("Launch Wi-Fi device picker");
-       syspopup_launch("wifi-qs", b);
+       if (!headed_plugin)
+               return;
 
-       bundle_free(b);
-#endif
+       headed_plugin->pop_device_picker();
 }
 
 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
@@ -443,6 +419,58 @@ int netconfig_execute_file(const char *file_path,
        return -EIO;
 }
 
+int netconfig_execute_cmd(const char *cmd)
+{
+       if (cmd == NULL)
+               return -EIO;
+
+       pid_t pid = 0;
+       int status = 0;
+       int rv = 0;
+       errno = 0;
+       char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
+       gchar **args = NULL;
+
+       DBG("command: %s", cmd);
+
+       args = g_strsplit_set(cmd, " ", -1);
+
+       if (!(pid = fork())) {
+               DBG("pid(%d), ppid (%d)", getpid(), getppid());
+
+               errno = 0;
+               if (execv(args[0], args) == -1) {
+                       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+                       DBG("Fail to execute command (%s)", error_buf);
+                       g_strfreev(args);
+                       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");
+               }
+
+               g_strfreev(args);
+               return rv;
+       }
+
+       strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+       DBG("failed to fork(%s)", error_buf);
+       g_strfreev(args);
+
+       return -EIO;
+}
+
 static void on_clat_handler()
 {
        pid_t clat_pid = 0;
@@ -864,105 +892,59 @@ gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
 
 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
 {
-       int ret = 0;
-       bundle *b;
-       static gboolean is_found_noti_exists = FALSE;
-       static gboolean is_portal_noti_exists = FALSE;
-
-       if (noti == NULL) {
-               ERR("Invalid notification");
+       if (!netconfig_plugin_headed_enabled)
                return FALSE;
-       }
-
-       if (g_strcmp0(noti, NETCONFIG_DEL_FOUND_AP_NOTI) == 0) {
-               if (is_found_noti_exists == FALSE)
-                       return TRUE;
-
-               is_found_noti_exists = FALSE;
-       } else if (g_strcmp0(noti, NETCONFIG_ADD_FOUND_AP_NOTI) == 0) {
-               if (is_found_noti_exists == TRUE)
-                       return TRUE;
-
-               is_found_noti_exists = TRUE;
-       } else if (g_strcmp0(noti, NETCONFIG_ADD_PORTAL_NOTI) == 0) {
-               if (is_portal_noti_exists == TRUE)
-                       return TRUE;
-
-               is_portal_noti_exists = TRUE;
-       } else if (g_strcmp0(noti, NETCONFIG_DEL_PORTAL_NOTI) == 0) {
-               if (is_portal_noti_exists == FALSE)
-                       return TRUE;
-
-               is_portal_noti_exists = FALSE;
-       }
-
-       b = bundle_create();
-       bundle_add(b, "_SYSPOPUP_TYPE_", noti);
-
-       if (ssid != NULL) {
-               DBG("ssid (%s)", ssid);
-               bundle_add(b, "_AP_NAME_", ssid);
-       }
 
-       ret = syspopup_launch("net-popup", b);
-
-       bundle_free(b);
-
-       if (ret < 0) {
-               ERR("Unable to launch noti-popup. Err = %d", ret);
+       if (!headed_plugin)
                return FALSE;
-       }
 
-       DBG("Successfully sent notification (%s)", noti);
-       return TRUE;
+       return headed_plugin->send_notification_to_net_popup(noti, ssid);
 }
 
 int netconfig_send_message_to_net_popup(const char *title,
                const char *content, const char *type, const char *ssid)
 {
-       int ret = 0;
-       bundle *b = bundle_create();
-
-       bundle_add(b, "_SYSPOPUP_TITLE_", title);
-       bundle_add(b, "_SYSPOPUP_CONTENT_", content);
-       bundle_add(b, "_SYSPOPUP_TYPE_", type);
-       bundle_add(b, "_AP_NAME_", ssid);
+       if (!netconfig_plugin_headed_enabled)
+               return 0;
 
-       ret = syspopup_launch("net-popup", b);
+       if (!headed_plugin)
+               return 0;
 
-       bundle_free(b);
-
-       return ret;
+       return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
 }
 
 int netconfig_send_restriction_to_net_popup(const char *title,
                const char *type, const char *restriction)
 {
-       int ret = 0;
-       bundle *b = bundle_create();
+       if (!netconfig_plugin_headed_enabled)
+               return 0;
 
-       bundle_add(b, "_SYSPOPUP_TITLE_", title);
-       bundle_add(b, "_SYSPOPUP_CONTENT_", "security restriction");
-       bundle_add(b, "_SYSPOPUP_TYPE_", type);
-       bundle_add(b, "_RESTRICTED_TYPE_", restriction);
+       if (!headed_plugin)
+               return 0;
 
-       ret = syspopup_launch("net-popup", b);
+       return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
+}
 
-       bundle_free(b);
+void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
+{
+       if (!netconfig_plugin_headed_enabled)
+               return;
 
-       return ret;
+       if (!headed_plugin)
+               return;
+
+       headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
 }
 
-void netconfig_set_system_event(const char * sys_evt, const char * evt_key, const char * evt_val)
+void __netconfig_pop_wifi_connected_poppup(const char *ssid)
 {
-       bundle *b = NULL;
+       if (!netconfig_plugin_headed_enabled)
+               return;
 
-       DBG("System event set [%s : %s : %s]", sys_evt, evt_key, evt_val);
+       if (!headed_plugin)
+               return;
 
-       b = bundle_create();
-       bundle_add_str(b, evt_key, evt_val);
-       eventsystem_send_system_event(sys_evt, b);
-       bundle_free(b);
+       headed_plugin->pop_wifi_connected_poppup(ssid);
 }
 
 void netconfig_set_vconf_int(const char * key, int value)
@@ -1084,3 +1066,69 @@ void netconfig_set_mac_address_from_file(void)
        g_free(mac_lower_str);
        fclose(file);
 }
+
+tizen_profile_t _get_tizen_profile()
+{
+       static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
+       if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
+               return profile;
+
+       char *profileName;
+       system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
+       switch (*profileName) {
+       case 'm':
+       case 'M':
+               profile = TIZEN_PROFILE_MOBILE;
+               break;
+       case 'w':
+       case 'W':
+               profile = TIZEN_PROFILE_WEARABLE;
+               break;
+       case 't':
+       case 'T':
+               profile = TIZEN_PROFILE_TV;
+               break;
+       case 'i':
+       case 'I':
+               profile = TIZEN_PROFILE_IVI;
+               break;
+       default: // common or unknown ==> ALL ARE COMMON.
+               profile = TIZEN_PROFILE_COMMON;
+       }
+       free(profileName);
+
+       return profile;
+}
+
+void netconfig_plugin_init()
+{
+       handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
+       if (!handle_headed) {
+               ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
+               return;
+       }
+
+       headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
+       if (!headed_plugin) {
+               ERR("Can't load symbol: %s", dlerror());
+               dlclose(handle_headed);
+               return;
+       }
+
+       netconfig_plugin_headed_enabled = TRUE;
+}
+
+void netconfig_plugin_deinit()
+{
+       if (!netconfig_plugin_headed_enabled)
+               return;
+
+       netconfig_plugin_headed_enabled = FALSE;
+       dlclose(handle_headed);
+}
+
+gboolean netconfig_get_headed_plugin_flag()
+{
+       return netconfig_plugin_headed_enabled;
+}
+