Cleanup driver and wpa_supplicant when tech power on is failed
[platform/core/connectivity/net-config.git] / src / vpnsvc-internal.c
index f0b4d88..0c71d45 100755 (executable)
@@ -157,10 +157,8 @@ static char *connman_default_profile(GDBusConnection *connection)
 
        if (message) {
                g_variant_get(message, "(a(oa{sv}))", &iter);
-               while (g_variant_iter_loop(iter, "(oa{sv})", &key, &value)) {
+               if (g_variant_iter_loop(iter, "(oa{sv})", &key, &value))
                        profile = strdup(key);
-                       break;
-               }
 
                if (value)
                        g_variant_iter_free(value);
@@ -478,28 +476,6 @@ static int del_dns_suffix()
        return VPNSVC_ERROR_NONE;
 }
 
-static int _check_config_str(void)
-{
-       char *buf = NULL;
-       size_t len;
-
-       len = confstr(_CS_PATH, NULL, 0);
-       if (len == 0)
-               return -1;
-
-       if ((buf = malloc(len)) == NULL)
-               return -1;
-
-       if (confstr(_CS_PATH, buf, len) == 0) {
-               free(buf);
-               return -1;
-       }
-
-       free(buf);
-
-       return 0;
-}
-
 #if 0
 static void dns_nat_register(char **vpn_dns_address, size_t nr_dns, char *vpn_device_address)
 {
@@ -531,13 +507,13 @@ static void dns_nat_unregister(void)
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_FLUSH_CHAIN_FROM_NAT,
                        iptables_cmd, iptables_nat_chain_name);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_DEL_CHAIN_FROM_NAT,
                        iptables_cmd, iptables_nat_chain_name);
        if (netconfig_execute_cmd(buf))
@@ -556,55 +532,55 @@ static void iptables_register(void)
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_FLUSH_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_APPEND_DROP_RULE,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_APPEND_RETURN_RULE,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_INSERT_RULE,
                        iptables_cmd, filter, iptables_filter_prefix, filter);
 
        filter = iptables_filter_in;
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_CREATE_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_FLUSH_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_APPEND_DROP_RULE,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_APPEND_RETURN_RULE,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_INSERT_RULE,
                        iptables_cmd, filter, iptables_filter_prefix, filter);
 }
@@ -621,13 +597,13 @@ static void iptables_unregister(void)
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_FLUSH_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_DEL_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
@@ -635,19 +611,19 @@ static void iptables_unregister(void)
 
        filter = iptables_filter_in;
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_DEL_RULE,
                        iptables_cmd, filter, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_FLUSH_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_DEL_CHAIN,
                        iptables_cmd, iptables_filter_prefix, filter);
        if (netconfig_execute_cmd(buf))
@@ -664,7 +640,7 @@ static void iptables_rule(const char c, const char *addr, const int mask)
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_APPEND_ACCEPT_RULE,
                        iptables_cmd, c,  iptables_filter_prefix,
                        iptables_filter_in, 's', addr, mask);
@@ -682,7 +658,7 @@ static void iptables_rule_interface(const char c, const char *addr, const int ma
        if (netconfig_execute_cmd(buf))
                ERR("Failed to execute command: %s", buf);
 
-       memset(buf, '0', sizeof(buf));
+       memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf), IPTABLES_FMT_APPEND_ACCEPT_RULE_WITH_INTF,
                        iptables_cmd, c, iptables_filter_prefix, iptables_filter_in,
                        'i', interface, 's', addr, mask);
@@ -727,7 +703,7 @@ static int get_interface_index(const char *iface_name)
        memset(&ifr, 0, sizeof(ifr));
 
        if (*iface_name)
-               strncpy(ifr.ifr_name, iface_name, IFNAMSIZ);
+               strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1);
 
        /* get an interface name by ifindex */
        if (ioctl(sk, SIOCGIFINDEX, &ifr) < 0) {
@@ -780,10 +756,9 @@ static int check_interface_precondition(const char *iface_name)
 int vpn_service_init(const char* iface_name, size_t iface_name_len, int fd, vpnsvc_tun_s *handle_s)
 {
        struct ifreq ifr;
-       size_t len = 0;
        char buf[BUF_SIZE_FOR_ERR] = { 0 };
 
-       DBG("enter vpn_daemon_init, iface_name : %s, iface_name_len : %d, fd : %d\n", iface_name, iface_name_len, fd);
+       DBG("enter vpn_daemon_init, iface_name : %s, iface_name_len : %zd, fd : %d\n", iface_name, iface_name_len, fd);
 
        memset(&ifr, 0, sizeof(ifr));
 
@@ -797,6 +772,7 @@ int vpn_service_init(const char* iface_name, size_t iface_name_len, int fd, vpns
 
        if (*iface_name)
                strncpy(ifr.ifr_name, iface_name, iface_name_len);
+       ifr.ifr_name[iface_name_len] = '\0';
 
        DBG("before init, ifindex : %d", ifr.ifr_ifindex);
 
@@ -820,39 +796,23 @@ int vpn_service_init(const char* iface_name, size_t iface_name_len, int fd, vpns
 
        handle_s->fd = 0;   /* server fd does not meaning */
        handle_s->index = get_interface_index(iface_name);
-       len = strlen(ifr.ifr_name);
-       strncpy(handle_s->name, ifr.ifr_name, len);
-       handle_s->name[len] = '\0';
+       g_strlcpy(handle_s->name, ifr.ifr_name, VPNSVC_VPN_IFACE_NAME_LEN);
 
        return VPNSVC_ERROR_NONE;
 }
 
 int vpn_service_deinit(const char* dev_name)
 {
-       char buf[100], *cmd;
-       FILE *fp = NULL;
-
-       if (_check_config_str() != 0) {
-               ERR("Failed to get configuration string");
-               return VPNSVC_ERROR_IO_ERROR;
-       }
+       char buf[100];
 
        snprintf(buf, sizeof(buf), "/usr/sbin/ip link del %s", dev_name);
 
-       cmd = g_try_malloc0(strlen(buf) + 1);
-       strncpy(cmd, buf, strlen(buf));
-
-       DBG("link delete cmd : %s", cmd);
-
-       fp = popen(cmd, "r");
-       g_free(cmd);
-
-       if (fp != NULL) {
-               pclose(fp);
-               return VPNSVC_ERROR_NONE;
-       } else {
+       if (netconfig_execute_cmd(buf)) {
+               ERR("Failed to execute command: %s", buf);
                return VPNSVC_ERROR_IO_ERROR;
        }
+
+       return VPNSVC_ERROR_NONE;
 }
 
 int vpn_service_protect(int socket_fd, const char* dev_name)