From d3cf71781d0f583bede1ebf32c32d0651b057edb Mon Sep 17 00:00:00 2001 From: taesubkim Date: Tue, 3 May 2016 13:09:35 +0900 Subject: [PATCH 01/16] [SVACE] 77596 Fixed svace issue Change-Id: Ibd09262afd95a5e2530c8c609f64c1e614fea92a Signed-off-by: Taesub Kim --- daemon/src/vpn_service_daemon.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/daemon/src/vpn_service_daemon.c b/daemon/src/vpn_service_daemon.c index f4f82b9..358f4af 100755 --- a/daemon/src/vpn_service_daemon.c +++ b/daemon/src/vpn_service_daemon.c @@ -146,12 +146,13 @@ static int add_dns_routes(char* if_name, char** dns_servers, size_t nr_dns) struct sockaddr_in addr; int sk; unsigned int i = 0; + char buf[BUF_SIZE_FOR_ERR] = { 0 }; LOGD("Enter add_routes"); sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (sk < 0) { - LOGE("socket failed : %s", strerror(errno)); + LOGE("socket failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); return VPNSVC_ERROR_IO_ERROR; } @@ -179,7 +180,7 @@ static int add_dns_routes(char* if_name, char** dns_servers, size_t nr_dns) rt.rt_dev = if_name; if (ioctl(sk, SIOCADDRT, &rt) < 0) { - LOGE("ioctl SIOCADDRT failed : %s", strerror(errno)); + LOGE("ioctl SIOCADDRT failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); close(sk); return VPNSVC_ERROR_IO_ERROR; } -- 2.7.4 From 0cd40e7fda377fcbe27680a9e9738fd32aac7d78 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 4 May 2016 10:55:23 +0900 Subject: [PATCH 02/16] Fix passed to proc after free Change-Id: I63f1612ace96f94f4b25344fc8df1032d51d18dc Signed-off-by: hyunuktak --- framework/src/capi_vpn_service.c | 2 +- packaging/capi-vpn-service.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/capi_vpn_service.c b/framework/src/capi_vpn_service.c index d83ce10..ff52f53 100755 --- a/framework/src/capi_vpn_service.c +++ b/framework/src/capi_vpn_service.c @@ -395,8 +395,8 @@ EXPORT_API int vpnsvc_deinit(vpnsvc_h handle) LOGD("tun fd close success"); /* free allocared handle memory */ + vpn_handle_list = g_slist_remove(vpn_handle_list, tun_s); _vpnsvc_deinit_vpnsvc_tun_s(tun_s); - vpn_handle_list = g_slist_remove(vpn_handle_list, handle); } return result; diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 50c11a2..9e3b66d 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.14 +Version: 1.0.15 Release: 1 Group: System/Network License: Apache-2.0 -- 2.7.4 From a492b54ce4b566f5cbae93b7198cb54653dde6d6 Mon Sep 17 00:00:00 2001 From: taesubkim Date: Fri, 13 May 2016 16:33:36 +0900 Subject: [PATCH 03/16] Modified build option naming rule Change-Id: Ia04cf1a5d374d0aac39d4945a232b32542485ec9 Signed-off-by: Taesub Kim --- CMakeLists.txt | 9 --------- packaging/capi-vpn-service.spec | 19 +++---------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b60293d..ee997cb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,15 +24,6 @@ ADD_DEFINITIONS("-fPIC") # ADD_DEFINITIONS("-Werror") # Make all warnings into errors. ADD_DEFINITIONS("-Wall") # Generate all warnings -STRING(REGEX MATCH "([^.]*)" API_VERSION "${VERSION}") -ADD_DEFINITIONS("-DAPI_VERSION=\"$(API_VERSION)\"") -ADD_DEFINITIONS("-DSMACK_ENABLED") - -IF (CMAKE_BUILD_TYPE MATCHES "DEBUG") - ADD_DEFINITIONS("-DTIZEN_DEBUG_ENABLE") - ADD_DEFINITIONS("-DBUILD_TYPE_DEBUG") -ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG") - ADD_SUBDIRECTORY(framework) ADD_SUBDIRECTORY(daemon) ADD_SUBDIRECTORY(test) diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 9e3b66d..f30b897 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.15 +Version: 1.0.16 Release: 1 Group: System/Network License: Apache-2.0 @@ -65,26 +65,13 @@ vpnsvc daemon package %setup -q %build -%if 0%{?sec_build_binary_debug_enable} -export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE" -export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE" -export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" -%endif - -%if 0%{?tizen_build_binary_release_type_eng} -export CFLAGS="$CFLAGS -DTIZEN_ENGINEER_MODE" -export CXXFLAGS="$CXXFLAGS -DTIZEN_ENGINEER_MODE" -export FFLAGS="$FFLAGS -DTIZEN_ENGINEER_MODE" -%endif - export LDFLAGS+="-Wl,--rpath=%{_libdir}" MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` %cmake . -DVERSION=%{version} \ - -DFULLVER=%{version} \ - -DMAJORVER=${MAJORVER} \ + -DFULLVER=%{version} \ + -DMAJORVER=${MAJORVER} \ -DCMAKE_BUILD_TYPE=%{?build_type:%build_type}%{!?build_type:RELEASE} \ - -DTIZEN_ENGINEER_MODE=%{?tizen_build_binary_release_type_eng:1}%{!?tizen_build_binary_release_type_eng:0} \ -DCMAKE_VERBOSE_MAKEFILE=ON \ -DLIB_INSTALL_DIR=%{_libdir} -- 2.7.4 From 77b35d2863f3ea1b39fab094eed2c33f640b5998 Mon Sep 17 00:00:00 2001 From: taesubkim Date: Thu, 19 May 2016 17:29:36 +0900 Subject: [PATCH 04/16] Fixed build warning Change-Id: I41ebf9068cc4b1f10e23db8a5009c86dd72f952a Signed-off-by: Taesub Kim --- daemon/src/vpn_service_daemon.c | 14 ++++++++++++++ framework/src/capi_vpn_service.c | 1 - packaging/capi-vpn-service.spec | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/daemon/src/vpn_service_daemon.c b/daemon/src/vpn_service_daemon.c index 358f4af..33dbbf7 100755 --- a/daemon/src/vpn_service_daemon.c +++ b/daemon/src/vpn_service_daemon.c @@ -57,8 +57,10 @@ static char iptables_rule_with_interface_fmt[] = "%s -%c %s%s -%c %s -%c %s/%d - /*static char iptables_usage_fmt[] = "%s -L %s%s -n -v -w;";*/ /* iptables -t nat -A CAPI_VPN_SERVICE_OUTPUT -p udp -d --dport 53 -j DNAT --to */ static char iptables_nat_chain_name[] = "CAPI_VPN_SERVICE_NAT_OUTPUT"; +#if 0 static char iptables_nat_register_init_fmt[] = "%s -t nat -N %s -w;" "%s -t nat -F %s -w;" "%s -t nat -I %s -j %s -w;"; static char iptables_nat_register_rule_fmt[] = "%s -t nat -A %s -p udp -d %s --dport 53 -j DNAT --to %s:53 -w;"; +#endif static char iptables_nat_unregister_fmt[] = "%s -t nat -D %s -j %s -w;" "%s -t nat -F %s -w;" "%s -t nat -X %s -w;"; typedef unsigned long int ipv4; /* Declare variable type for ipv4 net address. */ @@ -268,6 +270,7 @@ static char *connman_default_profile(GDBusConnection *connection) return profile; } +#if 0 static char *connman_get_items(GDBusConnection *connection, char *profile, const char *keystr) { GVariant *message = NULL; @@ -319,6 +322,7 @@ static char *connman_get_items(GDBusConnection *connection, char *profile, const return items; } +#endif static void connman_set_items(GDBusConnection *connection, char *profile, const char *keystr, char *items) @@ -348,6 +352,7 @@ static void connman_set_items(GDBusConnection *connection, char *profile, } +#if 0 static char *connman_get_nameservers(GDBusConnection *connection, char *profile) { return connman_get_items(connection, profile, "Nameservers"); @@ -357,6 +362,7 @@ static char *connman_get_nameservers_conf(GDBusConnection *connection, char *pro { return connman_get_items(connection, profile, "Nameservers.Configuration"); } +#endif static void connman_set_nameservers(GDBusConnection *connection, char *profile, char *nameservers) @@ -365,6 +371,7 @@ static void connman_set_nameservers(GDBusConnection *connection, char *profile, "Nameservers.Configuration", nameservers); } +#if 0 static char *connman_get_domains(GDBusConnection *connection, char *profile) { return connman_get_items(connection, profile, "Domains"); @@ -374,6 +381,7 @@ static char *connman_get_domains_conf(GDBusConnection *connection, char *profile { return connman_get_items(connection, profile, "Domains.Configuration"); } +#endif static void connman_set_domains(GDBusConnection *connection, char *profile, char *domains) @@ -382,6 +390,7 @@ static void connman_set_domains(GDBusConnection *connection, char *profile, "Domains.Configuration", domains); } +#if 0 static int add_dns_servers(char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt) { char *profile = NULL; @@ -449,6 +458,7 @@ static int add_dns_servers(char** dns_servers, size_t nr_dns, size_t total_dns_s free(profile); return VPNSVC_ERROR_NONE; } +#endif static int del_dns_servers() { @@ -474,6 +484,7 @@ static int del_dns_servers() return VPNSVC_ERROR_NONE; } +#if 0 static int add_dns_suffix(const char* dns_suffix, size_t dns_suffix_len) { char *profile = NULL; @@ -537,6 +548,7 @@ static int add_dns_suffix(const char* dns_suffix, size_t dns_suffix_len) return VPNSVC_ERROR_NONE; } +#endif static int del_dns_suffix() { @@ -573,6 +585,7 @@ static void iptables_exec(char *cmdline) pclose(fp); } +#if 0 static void dns_nat_register(char **vpn_dns_address, size_t nr_dns, char *vpn_device_address) { int size = 0, i; @@ -592,6 +605,7 @@ static void dns_nat_register(char **vpn_dns_address, size_t nr_dns, char *vpn_de LOGD("iptable dns nat reg cmd : %s", buf); iptables_exec(buf); } +#endif static void dns_nat_unregister(void) { diff --git a/framework/src/capi_vpn_service.c b/framework/src/capi_vpn_service.c index ff52f53..88daf01 100755 --- a/framework/src/capi_vpn_service.c +++ b/framework/src/capi_vpn_service.c @@ -17,7 +17,6 @@ * */ - #include "capi_vpn_service_private.h" #include #include diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index f30b897..17bbfc7 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.16 +Version: 1.0.17 Release: 1 Group: System/Network License: Apache-2.0 -- 2.7.4 From 00a023a2d64e204acabfde20f7244152c73b641a Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 24 May 2016 20:25:38 +0900 Subject: [PATCH 05/16] Modify the error value for access denied Change-Id: I27b7cc6271c2207b0fd5ccaea1f410d68a9628da Signed-off-by: hyunuktak --- framework/src/capi_vpn_service.c | 2 +- packaging/capi-vpn-service.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/capi_vpn_service.c b/framework/src/capi_vpn_service.c index 88daf01..0e96d7e 100755 --- a/framework/src/capi_vpn_service.c +++ b/framework/src/capi_vpn_service.c @@ -327,7 +327,7 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) if (result != VPNSVC_ERROR_NONE) { LOGE("vpnsvc_init() failed"); //LCOV_EXCL_LINE _vpnsvc_deinit_vpnsvc_tun_s(tmp_s); //LCOV_EXCL_LINE - result = VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + result = VPNSVC_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE } else { LOGD("vpnsvc_init() succeed"); tmp_s->fd = iface_fd; /* client fd must be set */ diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 17bbfc7..3faea52 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.17 +Version: 1.0.18 Release: 1 Group: System/Network License: Apache-2.0 -- 2.7.4 From 4d324f9fb8ae0321036e2d0027635b122ce19241 Mon Sep 17 00:00:00 2001 From: taesubkim Date: Fri, 3 Jun 2016 17:01:15 +0900 Subject: [PATCH 06/16] Remove the gcov flog Change-Id: I7cae0cf9fc405b421e8add1fdcec14001e9ea7ea Signed-off-by: Taesub Kim --- framework/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 6d619d5..a01be18 100755 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -25,7 +25,7 @@ FOREACH(flag ${${PACKAGE_NAME}_CFLAGS}) ENDFOREACH(flag) # Compiler flags -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden -fprofile-arcs -ftest-coverage") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") -- 2.7.4 From dcd6feb10a9de7f1de2dbe11ede850d368cac5dc Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 25 May 2016 18:44:18 +0900 Subject: [PATCH 07/16] Fixed memory leaks but it has below latent defect as ever. by 0x49420E3: g_simple_async_result_complete (in /usr/lib/libgio-2.0.so.0.4400.1) Change-Id: I5a99828ca919e9f050db2d77eafe17002c4b2921 Signed-off-by: hyunuktak --- daemon/include/vpnsvc.h | 1 + daemon/src/vpn_service_daemon_main.c | 1 + daemon/src/vpndbus.c | 1 + daemon/src/vpnsvc.c | 40 +++++++++++++++++++++++++++--------- packaging/capi-vpn-service.spec | 2 +- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/daemon/include/vpnsvc.h b/daemon/include/vpnsvc.h index 7e92207..3c2887a 100755 --- a/daemon/include/vpnsvc.h +++ b/daemon/include/vpnsvc.h @@ -37,6 +37,7 @@ typedef enum _net_vpn_service_privilege_e { } net_vpn_service_privilege_e; void vpnsvc_create_and_init(void); +void vpnsvc_destroy_deinit(void); Vpnsvc *get_vpnsvc_object(void); gboolean vpn_service_gdbus_check_privilege(GDBusMethodInvocation *invocation, net_vpn_service_privilege_e _privilege); diff --git a/daemon/src/vpn_service_daemon_main.c b/daemon/src/vpn_service_daemon_main.c index 1f14aaf..ff3037b 100755 --- a/daemon/src/vpn_service_daemon_main.c +++ b/daemon/src/vpn_service_daemon_main.c @@ -62,6 +62,7 @@ int main(void) g_main_loop_run(main_loop); vpnsvc_cleanup_gdbus(); + vpnsvc_destroy_deinit(); return 0; } diff --git a/daemon/src/vpndbus.c b/daemon/src/vpndbus.c index 9c12f82..d079d84 100755 --- a/daemon/src/vpndbus.c +++ b/daemon/src/vpndbus.c @@ -235,6 +235,7 @@ void vpnsvc_cleanup_gdbus(void) LOGD("VPN Service Cleanup!"); g_bus_unown_name(owner_id); + g_object_unref(manager_server_vpn); return; } diff --git a/daemon/src/vpnsvc.c b/daemon/src/vpnsvc.c index e6a4967..123db6f 100755 --- a/daemon/src/vpnsvc.c +++ b/daemon/src/vpnsvc.c @@ -205,8 +205,7 @@ gboolean handle_vpn_up(Vpnsvc *object, i = 0; while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { int temp_dest_str_len = strlen(route_dest); - routes[i] = malloc((sizeof(char) * temp_dest_str_len)+1); - memset(routes[i], 0, sizeof(char) * temp_dest_str_len); + routes[i] = g_try_malloc0((sizeof(char) * temp_dest_str_len)+1); strncpy(routes[i], route_dest, temp_dest_str_len); routes[i][temp_dest_str_len] = '\0'; prefix[i] = route_prefix; @@ -222,7 +221,7 @@ gboolean handle_vpn_up(Vpnsvc *object, if (arg_nr_dns > 0) { if (arg_dns_servers != NULL) { GVariant *array = g_variant_get_variant(arg_dns_servers); - dns_servers = (char **)malloc(arg_nr_dns*sizeof(char *)); + dns_servers = (char **)g_try_malloc0(arg_nr_dns*sizeof(char *)); if (dns_servers == NULL) { LOGE("malloc failed."); result = VPNSVC_ERROR_OUT_OF_MEMORY; @@ -232,7 +231,7 @@ gboolean handle_vpn_up(Vpnsvc *object, i = 0; while (g_variant_iter_loop(&iter, "s", &temp_dns_server)) { int temp_dns_str_len = strlen(temp_dns_server); - dns_servers[i] = (char *)malloc((temp_dns_str_len+1)*sizeof(char)); + dns_servers[i] = (char *)g_try_malloc0((temp_dns_str_len + 1) * sizeof(char)); strncpy(dns_servers[i], temp_dns_server, strlen(temp_dns_server)); dns_servers[i][temp_dns_str_len] = '\0'; total_dns_string_cnt += temp_dns_str_len; @@ -247,12 +246,17 @@ gboolean handle_vpn_up(Vpnsvc *object, total_dns_string_cnt, arg_dns_suffix, arg_mtu); done: /* free pointers */ + for (i = 0; i < arg_nr_routes; i++) { + if(routes[i]) + g_free(routes[i]); + } + if (dns_servers) { for (i = 0; i < arg_nr_dns; i++) { if (dns_servers[i]) - free(dns_servers[i]); + g_free(dns_servers[i]); } - free(dns_servers); + g_free(dns_servers); } vpnsvc_complete_vpn_up(object, invocation, result); @@ -326,8 +330,7 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, i = 0; while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { int tmp_route_len = strlen(route_dest); - nets_vpn[i] = malloc(sizeof(char) * tmp_route_len + 1); - memset(nets_vpn[i], 0, sizeof(char) * tmp_route_len); + nets_vpn[i] = g_try_malloc0(sizeof(char) * tmp_route_len + 1); strncpy(nets_vpn[i], route_dest, tmp_route_len); nets_vpn[i][tmp_route_len] = '\0'; prefix_vpn[i] = route_prefix; @@ -346,8 +349,7 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, i = 0; while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { int tmp_route_len = strlen(route_dest); - nets_orig[i] = malloc(sizeof(char) * tmp_route_len + 1); - memset(nets_orig[i], 0, sizeof(char) * tmp_route_len); + nets_orig[i] = g_try_malloc0(sizeof(char) * tmp_route_len + 1); strncpy(nets_orig[i], route_dest, tmp_route_len); nets_orig[i][tmp_route_len] = '\0'; prefix_orig[i] = route_prefix; @@ -363,6 +365,11 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, done: + for (i = 0; i < arg_nr_nets_vpn; ++i) { + g_free(nets_orig[i]); + g_free(nets_vpn[i]); + } + vpnsvc_complete_vpn_block_networks(object, invocation, result); return TRUE; @@ -441,6 +448,13 @@ void vpnsvc_create_and_init(void) return; } +void vpnsvc_destroy_deinit(void) +{ + LOGD("Deinit vpn object."); + + if (vpnsvc) + g_object_unref(vpnsvc); +} gboolean vpn_service_gdbus_check_privilege(GDBusMethodInvocation *invocation, net_vpn_service_privilege_e _privilege) { @@ -506,5 +520,11 @@ gboolean vpn_service_gdbus_check_privilege(GDBusMethodInvocation *invocation, ne if (ret == CYNARA_API_ACCESS_ALLOWED) LOGD("cynara PASS"); + cynara_finish(p_cynara); + + g_free(client); + g_free(user); + g_free(client_session); + return (ret == CYNARA_API_ACCESS_ALLOWED) ? TRUE : FALSE; } diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 3faea52..8638d30 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.18 +Version: 1.0.19 Release: 1 Group: System/Network License: Apache-2.0 -- 2.7.4 From baac123f6cf408202c087cd209c38f830aa2f096 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 26 May 2016 16:26:56 +0900 Subject: [PATCH 08/16] Added some method for gdbus return error Change-Id: Ib4457ce756fb4df86c7dc87493280f6b410b611b Signed-off-by: hyunuktak --- daemon/CMakeLists.txt | 1 + daemon/include/vpnerror.h | 41 +++++++++++++++++++++++++++++++ daemon/src/vpnerror.c | 53 ++++++++++++++++++++++++++++++++++++++++ daemon/src/vpnsvc.c | 45 ++++++++++++---------------------- framework/src/capi_vpn_service.c | 36 ++++++++++++++++----------- packaging/capi-vpn-service.spec | 2 +- 6 files changed, 133 insertions(+), 45 deletions(-) create mode 100755 daemon/include/vpnerror.h create mode 100755 daemon/src/vpnerror.c diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 14e080d..6ffc7ea 100755 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -15,6 +15,7 @@ SET(pc_requires "capi-base-common") SET(SRCS src/vpnsvc.c src/vpndbus.c + src/vpnerror.c src/vpn_service_daemon.c src/vpn_service_daemon_main.c ) diff --git a/daemon/include/vpnerror.h b/daemon/include/vpnerror.h new file mode 100755 index 0000000..e18a788 --- /dev/null +++ b/daemon/include/vpnerror.h @@ -0,0 +1,41 @@ +/* + * VPN Service Module + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __VPNSERVICE_VPNERROR_H__ +#define __VPNSERVICE_VPNERROR_H__ + +#include +#include +#include + +#include "vpn_service_daemon.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void vpnsvc_error_inprogress(GDBusMethodInvocation *context); +void vpnsvc_error_invalid_parameter(GDBusMethodInvocation *context); +void vpnsvc_error_permission_denied(GDBusMethodInvocation *context); + +#ifdef __cplusplus +} +#endif + +#endif /* __VPNSERVICE_VPNERROR_H__ */ diff --git a/daemon/src/vpnerror.c b/daemon/src/vpnerror.c new file mode 100755 index 0000000..2369c26 --- /dev/null +++ b/daemon/src/vpnerror.c @@ -0,0 +1,53 @@ +/* + * VPN Service Module + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +#include "vpndbus.h" +#include "vpnerror.h" + +#define VPNSVC_ERROR_INTERFACE VPNSERVICE_INTERFACE ".Error" + +void vpnsvc_error_inprogress(GDBusMethodInvocation *context) +{ + LOGE("dbus method return error"); + g_dbus_method_invocation_return_error(context, + G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + VPNSVC_ERROR_INTERFACE ".InProgress"); +} + +void vpnsvc_error_invalid_parameter(GDBusMethodInvocation *context) +{ + LOGE("dbus method return error"); + g_dbus_method_invocation_return_error(context, + G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + VPNSVC_ERROR_INTERFACE ".InvalidParameter"); +} + +void vpnsvc_error_permission_denied(GDBusMethodInvocation *context) +{ + LOGE("dbus method return error"); + g_dbus_method_invocation_return_error(context, + G_DBUS_ERROR, + G_DBUS_ERROR_ACCESS_DENIED, + VPNSVC_ERROR_INTERFACE ".PermissionDenied"); +} diff --git a/daemon/src/vpnsvc.c b/daemon/src/vpnsvc.c index 123db6f..b5d2e9e 100755 --- a/daemon/src/vpnsvc.c +++ b/daemon/src/vpnsvc.c @@ -25,6 +25,7 @@ #include "vpnsvc.h" #include "vpndbus.h" +#include "vpnerror.h" #include "vpn_service_daemon.h" #include "cynara-client.h" @@ -53,9 +54,8 @@ gboolean handle_vpn_init(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } vpnsvc_tun_s handle_s; @@ -80,8 +80,6 @@ gboolean handle_vpn_init(Vpnsvc *object, LOGD("handle_s.fd : %d, handle_s.index : %d, handle_s.name : %s", handle_s.fd, handle_s.index, handle_s.name); -done: - vpnsvc_complete_vpn_init(object, invocation, result, handle_s.index, handle_s.name); return TRUE; @@ -98,16 +96,14 @@ gboolean handle_vpn_deinit(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } LOGD("vpn_deinit, %s\n", arg_dev_name); result = vpn_daemon_deinit(arg_dev_name); -done: vpnsvc_complete_vpn_deinit(object, invocation, result); return TRUE; @@ -124,9 +120,8 @@ gboolean handle_vpn_protect(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } int socket; @@ -146,7 +141,6 @@ gboolean handle_vpn_protect(Vpnsvc *object, result = vpn_daemon_protect(socket, arg_dev_name); -done: vpnsvc_complete_vpn_protect(object, invocation, result); return TRUE; @@ -182,9 +176,8 @@ gboolean handle_vpn_up(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE_ADMIN) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } LOGD("iface_index : %d", arg_iface_index); @@ -274,17 +267,14 @@ gboolean handle_vpn_down(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE_ADMIN) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } LOGD("vpn_down, %d\n", arg_iface_index); result = vpn_daemon_down(arg_iface_index); -done: - vpnsvc_complete_vpn_down(object, invocation, result); return TRUE; @@ -315,9 +305,8 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } LOGD("vpn_block_networks"); @@ -363,8 +352,6 @@ gboolean handle_vpn_block_networks(Vpnsvc *object, /* call function */ result = vpn_daemon_block_networks(nets_vpn, prefix_vpn, arg_nr_nets_vpn, nets_orig, prefix_orig, arg_nr_nets_orig); -done: - for (i = 0; i < arg_nr_nets_vpn; ++i) { g_free(nets_orig[i]); g_free(nets_vpn[i]); @@ -385,16 +372,14 @@ gboolean handle_vpn_unblock_networks(Vpnsvc *object, /* check privilege */ if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - LOGE("permission denied, and finished request."); - result = VPNSVC_ERROR_PERMISSION_DENIED; - goto done; + vpnsvc_error_permission_denied(invocation); + return FALSE; } LOGD("vpn_unblock_networks"); result = vpn_daemon_unblock_networks(); -done: vpnsvc_complete_vpn_unblock_networks(object, invocation, result); return TRUE; diff --git a/framework/src/capi_vpn_service.c b/framework/src/capi_vpn_service.c index 0e96d7e..dbd5763 100755 --- a/framework/src/capi_vpn_service.c +++ b/framework/src/capi_vpn_service.c @@ -156,17 +156,13 @@ GVariant *_vpnsvc_invoke_dbus_method(GDBusConnection *connection, if (reply == NULL) { if (error != NULL) {//LCOV_EXCL_LINE - if (error->code == G_DBUS_ERROR_ACCESS_DENIED) {//LCOV_EXCL_LINE - LOGE("g_dbus_connection_call_sync() failed"//LCOV_EXCL_LINE - "error [%d: %s]", error->code, error->message); - *dbus_error = VPNSVC_ERROR_PERMISSION_DENIED;//LCOV_EXCL_LINE - g_error_free(error);//LCOV_EXCL_LINE - } else { - LOGE("g_dbus_connection_call_sync() failed"//LCOV_EXCL_LINE - "error [%d: %s]", error->code, error->message); - *dbus_error = VPNSVC_ERROR_IO_ERROR;//LCOV_EXCL_LINE - g_error_free(error);//LCOV_EXCL_LINE - } + LOGE("g_dbus_connection_call_sync() failed "//LCOV_EXCL_LINE + "error [%d: %s]", error->code, error->message); + if (error->code == G_DBUS_ERROR_ACCESS_DENIED) //LCOV_EXCL_LINE + *dbus_error = VPNSVC_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE + else + *dbus_error = VPNSVC_ERROR_IO_ERROR; //LCOV_EXCL_LINE + g_error_free(error);//LCOV_EXCL_LINE } else { LOGE("g_dbus_connection_call_sync() failed");//LCOV_EXCL_LINE *dbus_error = VPNSVC_ERROR_IPC_FAILED;//LCOV_EXCL_LINE @@ -226,12 +222,15 @@ GVariant *_vpnsvc_invoke_dbus_method_with_fd(GDBusConnection *connection, if (reply == NULL) { if (error != NULL) {//LCOV_EXCL_LINE - LOGE("g_dbus_connection_call_sync() failed" //LCOV_EXCL_LINE + LOGE("g_dbus_connection_call_with_unix_fd_list_sync() failed " //LCOV_EXCL_LINE "error [%d: %s]", error->code, error->message); - *dbus_error = VPNSVC_ERROR_IO_ERROR; //LCOV_EXCL_LINE + if (error->code == G_DBUS_ERROR_ACCESS_DENIED) //LCOV_EXCL_LINE + *dbus_error = VPNSVC_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE + else + *dbus_error = VPNSVC_ERROR_IO_ERROR; //LCOV_EXCL_LINE g_error_free(error); //LCOV_EXCL_LINE } else { - LOGE("g_dbus_connection_call_sync() failed"); //LCOV_EXCL_LINE + LOGE("g_dbus_connection_call_with_unix_fd_list_sync() failed"); //LCOV_EXCL_LINE *dbus_error = VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE } @@ -315,6 +314,9 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) iface_fd, &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + if (op == NULL) { close(iface_fd); //LCOV_EXCL_LINE _vpnsvc_deinit_vpnsvc_tun_s(tmp_s); //LCOV_EXCL_LINE @@ -527,6 +529,9 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle, const char* local_ip, const char* remo dns_suffix, tun_s->mtu), &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + if (op == NULL) { return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE } else { @@ -576,6 +581,9 @@ EXPORT_API int vpnsvc_down(vpnsvc_h handle) g_variant_new("(i)", tun_s->index), &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + if (op == NULL) { return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE } else { diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 8638d30..7cdcce2 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.19 +Version: 1.0.20 Release: 1 Group: System/Network License: Apache-2.0 -- 2.7.4 From 7c6223896789ac2f3b69ab8006620d2369365123 Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Thu, 9 Jun 2016 19:03:08 +0900 Subject: [PATCH 09/16] Remove capability for vpn service daemon Change-Id: I980fda4df00a6490fc154627b2c07ed00e1a09b3 Signed-off-by: hyunuktak --- packaging/capi-vpn-service.spec | 2 +- packaging/vpnsvc-daemon.service | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 7cdcce2..836f947 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.20 +Version: 1.0.21 Release: 1 Group: System/Network License: Apache-2.0 diff --git a/packaging/vpnsvc-daemon.service b/packaging/vpnsvc-daemon.service index a31465a..171d855 100755 --- a/packaging/vpnsvc-daemon.service +++ b/packaging/vpnsvc-daemon.service @@ -10,3 +10,5 @@ RemainAfterExit=yes ExecStart=/usr/bin/vpnsvc-daemon Restart=always RestartSec=0 +CapabilityBoundingSet=~CAP_MAC_ADMIN +CapabilityBoundingSet=~CAP_MAC_OVERRIDE -- 2.7.4 From bc5f8ce0bf7fa4abfbd45d99fcf009f6c678cf68 Mon Sep 17 00:00:00 2001 From: taesubkim Date: Fri, 10 Jun 2016 09:14:36 +0900 Subject: [PATCH 10/16] Apply ASLR for executable vpnsvc-daemon and vpnsvc_test Change-Id: I0ebf0d3e7cd06d9d9ef9369576a95267af9ca17f Signed-off-by: Taesub Kim --- daemon/CMakeLists.txt | 5 +++-- test/CMakeLists.txt | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index 6ffc7ea..6f1e4c1 100755 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -35,12 +35,13 @@ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -I${DAEMON_DIR}") SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") +ADD_DEFINITIONS("-fPIE") ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") ADD_DEFINITIONS("-DFACTORYFS=\"$ENV{FACTORYFS}\"") ADD_DEFINITIONS("-DDATAFS=\"$ENV{DATADIR}\"") ADD_DEFINITIONS("-DSLP_DEBUG") -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR}" -pie") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR}"") ADD_CUSTOM_COMMAND( WORKING_DIRECTORY @@ -53,7 +54,7 @@ ADD_CUSTOM_COMMAND( COMMENT "Generating GDBus .c/.h") ADD_EXECUTABLE(${PACKAGE_NAME} ${SRCS} ${DAEMON_DIR}/generated-code.c) -TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl) +TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl -pie) INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION bin) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f2146f8..4a2fcca 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,11 +31,12 @@ SET(EXTRA_C_FLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_C_FLAGS}") SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") +ADD_DEFINITIONS("-fPIE") ADD_DEFINITIONS("-DSLP_DEBUG") SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR}) ADD_EXECUTABLE(${PACKAGE_NAME} ${TEST_SRCS}) -TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} "-ldl" capi-vpnsvc) +TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} "-ldl" capi-vpnsvc -pie) INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION bin/) -- 2.7.4 From 50a95a0fea0427ed750b0ae58ab851b826040aad Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Tue, 14 Jun 2016 16:09:30 +0900 Subject: [PATCH 11/16] Apply coding rule Change-Id: I27052e643043340996f7d7f09a0d37e95f2eedea Signed-off-by: hyunuktak --- daemon/src/vpn_service_daemon.c | 3 +-- daemon/src/vpnsvc.c | 2 +- test/vpn_service_test.c | 7 ++++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/daemon/src/vpn_service_daemon.c b/daemon/src/vpn_service_daemon.c index 33dbbf7..db079df 100755 --- a/daemon/src/vpn_service_daemon.c +++ b/daemon/src/vpn_service_daemon.c @@ -964,9 +964,8 @@ int vpn_daemon_up(int iface_index, const char* local_ip, const char* remote_ip, } } - if (nr_dns > 0) { + if (nr_dns > 0) dns_nat_register(dns_servers, nr_dns, local_ip); - } #endif return ret; diff --git a/daemon/src/vpnsvc.c b/daemon/src/vpnsvc.c index b5d2e9e..afde6f6 100755 --- a/daemon/src/vpnsvc.c +++ b/daemon/src/vpnsvc.c @@ -240,7 +240,7 @@ gboolean handle_vpn_up(Vpnsvc *object, done: /* free pointers */ for (i = 0; i < arg_nr_routes; i++) { - if(routes[i]) + if (routes[i]) g_free(routes[i]); } diff --git a/test/vpn_service_test.c b/test/vpn_service_test.c index e1b4dbb..b293817 100755 --- a/test/vpn_service_test.c +++ b/test/vpn_service_test.c @@ -28,8 +28,6 @@ #endif #define LOG_TAG "VPNSVC_TEST" -#define TEST_VPN_IF_NAME "vpnsvc_test" - #define TEST_CONSOLE_PRINT(FMT, ARG...) fprintf(stderr, FMT, ##ARG); \ fprintf(stderr, "\n") #define TEST_CONSOLE_INPUT(BUFFER, LENGTH) \ @@ -68,9 +66,12 @@ static const char *test_print_error(vpnsvc_error_e error) int test_vpnsvc_init() { - char *name = TEST_VPN_IF_NAME; int rv = VPNSVC_ERROR_NONE; int int_value; + char name[33]; + + printf("Input VPN interface name to initialize : "); + rv = scanf("%32s", name); rv = vpnsvc_init(name, &handle); -- 2.7.4 From e8278cd0a2c5c6a56ee5bebe1c39b428d617d30c Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 15 Jun 2016 19:27:37 +0900 Subject: [PATCH 12/16] Remove daemon and change dbus to net-config Change-Id: I07089748de4ca3abdb81d2a0691d9012a033b89f Signed-off-by: hyunuktak --- CMakeLists.txt | 73 +- .../capi-vpnsvc.manifest => capi-vpnsvc.manifest | 0 framework/capi-vpnsvc.pc.in => capi-vpnsvc.pc.in | 0 daemon/CMakeLists.txt | 60 -- daemon/include/vpn_service_daemon.h | 38 - daemon/include/vpndbus.h | 58 -- daemon/include/vpnerror.h | 41 - daemon/include/vpnsvc.h | 49 - daemon/interfaces/org.tizen.vpnsvc.xml | 46 - daemon/src/vpn_service_daemon.c | 1064 -------------------- daemon/src/vpn_service_daemon_main.c | 68 -- daemon/src/vpndbus.c | 241 ----- daemon/src/vpnerror.c | 53 - daemon/src/vpnsvc.c | 515 ---------- daemon/vpnsvc-daemon.manifest | 5 - framework/CMakeLists.txt | 64 -- .../include => include}/capi_vpn_service_private.h | 13 +- packaging/capi-vpn-service.spec | 41 +- packaging/dbus-vpnsvc-daemon.conf | 17 - packaging/org.tizen.vpnsvc.service | 7 - packaging/vpnsvc-daemon.service | 14 - {framework/src => src}/capi_vpn_service.c | 152 ++- test/CMakeLists.txt | 3 +- 23 files changed, 161 insertions(+), 2461 deletions(-) rename framework/capi-vpnsvc.manifest => capi-vpnsvc.manifest (100%) rename framework/capi-vpnsvc.pc.in => capi-vpnsvc.pc.in (100%) delete mode 100755 daemon/CMakeLists.txt delete mode 100755 daemon/include/vpn_service_daemon.h delete mode 100755 daemon/include/vpndbus.h delete mode 100755 daemon/include/vpnerror.h delete mode 100755 daemon/include/vpnsvc.h delete mode 100755 daemon/interfaces/org.tizen.vpnsvc.xml delete mode 100755 daemon/src/vpn_service_daemon.c delete mode 100755 daemon/src/vpn_service_daemon_main.c delete mode 100755 daemon/src/vpndbus.c delete mode 100755 daemon/src/vpnerror.c delete mode 100755 daemon/src/vpnsvc.c delete mode 100755 daemon/vpnsvc-daemon.manifest delete mode 100755 framework/CMakeLists.txt rename {framework/include => include}/capi_vpn_service_private.h (84%) delete mode 100755 packaging/dbus-vpnsvc-daemon.conf delete mode 100755 packaging/org.tizen.vpnsvc.service delete mode 100755 packaging/vpnsvc-daemon.service rename {framework/src => src}/capi_vpn_service.c (90%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee997cb..6dc3c6e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,30 +1,65 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT("capi-vpn-service-pkg") +SET(PACKAGE_NAME capi-vpnsvc) +SET(LIB_NAME ${PACKAGE_NAME}) +PROJECT(${LIB_NAME}) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(LIBDIR ${LIB_INSTALL_DIR}) +SET(INCLUDEDIR "\${prefix}/include") +SET(VERSION 0.1) + +SET(requires "dlog dbus-1 glib-2.0 gio-2.0 gio-unix-2.0 capi-base-common capi-appfw-application capi-appfw-app-manager capi-system-info") +SET(pc_requires "capi-base-common") -############################# cmake packages ################################## +SET(SRCS + src/capi_vpn_service.c +) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) INCLUDE(FindPkgConfig) +pkg_check_modules(${PACKAGE_NAME} REQUIRED ${requires}) +FOREACH(flag ${${PACKAGE_NAME}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) -############################# compiler flags ################################## +# Compiler flags +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") -SET(CMAKE_INSTALL_PREFIX /usr) -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) -SET(EXEC_PREFIX ${PREFIX}/bin) -SET(LIBDIR ${LIB_INSTALL_DIR}) -SET(INCLUDEDIR ${PREFIX}/include) +ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") +ADD_DEFINITIONS("-DFACTORYFS=\"$ENV{FACTORYFS}\"") +ADD_DEFINITIONS("-DDATAFS=\"$ENV{DATADIR}\"") +ADD_DEFINITIONS("-DSLP_DEBUG") -# If supported for the target machine, emit position-independent code,suitable -# for dynamic linking and avoiding any limit on the size of the global offset -# table. This option makes a difference on the m68k, PowerPC and SPARC. -# (BJ: our ARM too?) -ADD_DEFINITIONS("-fPIC") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR}) -# Set compiler warning flags +ADD_LIBRARY(${PACKAGE_NAME} SHARED ${SRCS}) +TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl) -# ADD_DEFINITIONS("-Werror") # Make all warnings into errors. -ADD_DEFINITIONS("-Wall") # Generate all warnings +INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION ${LIBDIR}) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/vpn_service.h DESTINATION include) -ADD_SUBDIRECTORY(framework) -ADD_SUBDIRECTORY(daemon) -ADD_SUBDIRECTORY(test) +SET_TARGET_PROPERTIES(${PACKAGE_NAME} + PROPERTIES + VERSION ${FULLVER} + SOVERSION ${MAJORVER} + CLEAN_DIRECT_OUTPUT 1 +) + + +SET(PC_NAME ${PACKAGE_NAME}) +SET(PC_REQUIRED ${pc_requires}) +SET(PC_CFLAGS -I\${includedir}) +SET(PC_LDFLAGS -l${PACKAGE_NAME}) +CONFIGURE_FILE( + ${PACKAGE_NAME}.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${PACKAGE_NAME}.pc + @ONLY +) + +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PACKAGE_NAME}.pc DESTINATION ${LIBDIR}/pkgconfig) + +ADD_SUBDIRECTORY(test) diff --git a/framework/capi-vpnsvc.manifest b/capi-vpnsvc.manifest similarity index 100% rename from framework/capi-vpnsvc.manifest rename to capi-vpnsvc.manifest diff --git a/framework/capi-vpnsvc.pc.in b/capi-vpnsvc.pc.in similarity index 100% rename from framework/capi-vpnsvc.pc.in rename to capi-vpnsvc.pc.in diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt deleted file mode 100755 index 6f1e4c1..0000000 --- a/daemon/CMakeLists.txt +++ /dev/null @@ -1,60 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -SET(PACKAGE_NAME vpnsvc-daemon) -SET(LIB_NAME ${PACKAGE_NAME}) -PROJECT(${LIB_NAME}) -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) -SET(EXEC_PREFIX "\${prefix}") -SET(LIBDIR ${LIB_INSTALL_DIR}) -SET(INCLUDEDIR "\${prefix}/include") -SET(DAEMON_DIR "${CMAKE_SOURCE_DIR}/daemon") -SET(VERSION 0.1) - -SET(requires "dlog dbus-1 glib-2.0 gio-2.0 gio-unix-2.0 capi-base-common capi-appfw-package-manager cynara-client cynara-creds-gdbus cynara-session") -SET(pc_requires "capi-base-common") - -SET(SRCS - src/vpnsvc.c - src/vpndbus.c - src/vpnerror.c - src/vpn_service_daemon.c - src/vpn_service_daemon_main.c -) - -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include - ${CMAKE_SOURCE_DIR}/framework/include - ${CMAKE_SOURCE_DIR}/daemon/include) - -INCLUDE(FindPkgConfig) -pkg_check_modules(${PACKAGE_NAME} REQUIRED ${requires}) -FOREACH(flag ${${PACKAGE_NAME}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -# Compiler flags -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden") -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -I${DAEMON_DIR}") -SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") - -ADD_DEFINITIONS("-fPIE") -ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") -ADD_DEFINITIONS("-DFACTORYFS=\"$ENV{FACTORYFS}\"") -ADD_DEFINITIONS("-DDATAFS=\"$ENV{DATADIR}\"") -ADD_DEFINITIONS("-DSLP_DEBUG") - -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR}"") - -ADD_CUSTOM_COMMAND( - WORKING_DIRECTORY - OUTPUT ${DAEMON_DIR}/generated-code.c - COMMAND gdbus-codegen --interface-prefix org.tizen. - --generate-c-code generated-code - --c-generate-object-manager - --generate-docbook generated-code-docs - ${DAEMON_DIR}/interfaces/org.tizen.vpnsvc.xml - COMMENT "Generating GDBus .c/.h") - -ADD_EXECUTABLE(${PACKAGE_NAME} ${SRCS} ${DAEMON_DIR}/generated-code.c) -TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl -pie) - -INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION bin) - diff --git a/daemon/include/vpn_service_daemon.h b/daemon/include/vpn_service_daemon.h deleted file mode 100755 index f0b10b0..0000000 --- a/daemon/include/vpn_service_daemon.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - - -#ifndef __TIZEN_CAPI_VPN_SERVICE_DAEMON_H__ -#define __TIZEN_CAPI_VPN_SERVICE_DAEMON_H__ - -#include "capi_vpn_service_private.h" - -int vpn_daemon_init(const char* iface_name, size_t iface_name_len, int fd, vpnsvc_tun_s *handle_s); -int vpn_daemon_deinit(const char* dev_name); -int vpn_daemon_protect(int socket, const char* dev_name); -int vpn_daemon_up(int iface_index, const char* local_ip, const char* remote_ip, - char* routes[], int prefix[], size_t nr_routes, - char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt, - const char* dns_suffix, const unsigned int mtu); -int vpn_daemon_down(int iface_index); -int vpn_daemon_block_networks(char* nets_vpn[], int prefix_vpn[], size_t nr_nets_vpn, - char* nets_orig[], int prefix_orig[], size_t nr_nets_orig); -int vpn_daemon_unblock_networks(void); - -#endif /* __TIZEN_CAPI_VPN_SERVICE_DAEMON_H__ */ diff --git a/daemon/include/vpndbus.h b/daemon/include/vpndbus.h deleted file mode 100755 index 85b39f6..0000000 --- a/daemon/include/vpndbus.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __VPNSERVICE_VPNDBUS_H__ -#define __VPNSERVICE_VPNDBUS_H__ - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define VPNSERVICE_SERVICE "org.tizen.vpnsvc" -#define VPNSERVICE_INTERFACE "org.tizen.vpnsvc" -#define VPNSERVICE_PATH "/org/tizen/vpnsvc" - -typedef void (*vpnsvc_got_name_cb)(void); - -GDBusObjectManagerServer *vpnsvc_get_vpn_manager(void); -GDBusConnection *vpnsvc_gdbus_get_connection(void); -GCancellable *vpnsvc_gdbus_get_gdbus_cancellable(void); -void vpnsvc_gdbus_pending_call_ref(void); -void vpnsvc_gdbus_pending_call_unref(void); -int vpnsvc_create_gdbus_call(GDBusConnection *conn); - -gboolean vpnsvc_invoke_dbus_method_nonblock(const char *dest, const char *path, - const char *interface_name, const char *method, GVariant *params, - GAsyncReadyCallback notify_func); -GVariant *vpnsvc_invoke_dbus_method(const char *dest, const char *path, - const char *interface_name, const char *method, - GVariant *params); - -int vpnsvc_setup_gdbus(vpnsvc_got_name_cb cb); -void vpnsvc_cleanup_gdbus(void); - -#ifdef __cplusplus -} -#endif - -#endif /* __VPNSERVICE_VPNDBUS_H__ */ diff --git a/daemon/include/vpnerror.h b/daemon/include/vpnerror.h deleted file mode 100755 index e18a788..0000000 --- a/daemon/include/vpnerror.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __VPNSERVICE_VPNERROR_H__ -#define __VPNSERVICE_VPNERROR_H__ - -#include -#include -#include - -#include "vpn_service_daemon.h" - -#ifdef __cplusplus -extern "C" { -#endif - -void vpnsvc_error_inprogress(GDBusMethodInvocation *context); -void vpnsvc_error_invalid_parameter(GDBusMethodInvocation *context); -void vpnsvc_error_permission_denied(GDBusMethodInvocation *context); - -#ifdef __cplusplus -} -#endif - -#endif /* __VPNSERVICE_VPNERROR_H__ */ diff --git a/daemon/include/vpnsvc.h b/daemon/include/vpnsvc.h deleted file mode 100755 index 3c2887a..0000000 --- a/daemon/include/vpnsvc.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __VPNSERVICE_VPNSVC_H__ -#define __VPNSERVICE_VPNSVC_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -#include "generated-code.h" - -typedef enum _net_vpn_service_privilege_e { - PRIVILEGE_VPN_SERVICE = 0x00, - PRIVILEGE_VPN_SERVICE_ADMIN, - PRIVILEGE_INTERNET, -} net_vpn_service_privilege_e; - -void vpnsvc_create_and_init(void); -void vpnsvc_destroy_deinit(void); -Vpnsvc *get_vpnsvc_object(void); -gboolean vpn_service_gdbus_check_privilege(GDBusMethodInvocation *invocation, - net_vpn_service_privilege_e _privilege); - -#ifdef __cplusplus -} -#endif - -#endif /* __VPNSERVICE_VPNSVC_H__ */ diff --git a/daemon/interfaces/org.tizen.vpnsvc.xml b/daemon/interfaces/org.tizen.vpnsvc.xml deleted file mode 100755 index 4bcc50f..0000000 --- a/daemon/interfaces/org.tizen.vpnsvc.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/daemon/src/vpn_service_daemon.c b/daemon/src/vpn_service_daemon.c deleted file mode 100755 index db079df..0000000 --- a/daemon/src/vpn_service_daemon.c +++ /dev/null @@ -1,1064 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "vpn_service_daemon.h" - -#ifdef LOG_TAG -#undef LOG_TAG -#endif -#define LOG_TAG "VPNSVC_DAEMON" -#define BUF_SIZE_FOR_ERR 100 - -#define CONNMAN_SERVICE "net.connman" -#define CONNMAN_INTERFACE_MANAGER "net.connman.Manager" -#define CONNMAN_INTERFACE_SERVICE "net.connman.Service" - - -/* for iptables */ -static char iptables_cmd[] = "/usr/sbin/iptables"; -static char iptables_filter_prefix[] = "CAPI_VPN_SERVICE_"; -static char iptables_filter_out[] = "OUTPUT"; -static char iptables_filter_in[] = "INPUT"; -static char iptables_filter_interface_wlan[] = "wlan0"; -/* static char iptables_register_fmt[] = "%s -N %s%s -w;" "%s -F %s%s -w;" "%s -A %s%s -j RETURN -w;" "%s -I %s -j %s%s -w;"; */ -static char iptables_register_fmt[] = "%s -N %s%s -w;" "%s -F %s%s -w;" "%s -A %s%s -j DROP -w;" "%s -A %s%s -j RETURN -w;" "%s -I %s -j %s%s -w;"; -static char iptables_unregister_fmt[] = "%s -D %s -j %s%s -w;" "%s -F %s%s -w;" "%s -X %s%s -w;"; -static char iptables_rule_fmt[] = "%s -%c %s%s -%c %s/%d -j ACCEPT -w;"; -static char iptables_rule_with_interface_fmt[] = "%s -%c %s%s -%c %s -%c %s/%d -j ACCEPT -w;"; -/*static char iptables_usage_fmt[] = "%s -L %s%s -n -v -w;";*/ -/* iptables -t nat -A CAPI_VPN_SERVICE_OUTPUT -p udp -d --dport 53 -j DNAT --to */ -static char iptables_nat_chain_name[] = "CAPI_VPN_SERVICE_NAT_OUTPUT"; -#if 0 -static char iptables_nat_register_init_fmt[] = "%s -t nat -N %s -w;" "%s -t nat -F %s -w;" "%s -t nat -I %s -j %s -w;"; -static char iptables_nat_register_rule_fmt[] = "%s -t nat -A %s -p udp -d %s --dport 53 -j DNAT --to %s:53 -w;"; -#endif -static char iptables_nat_unregister_fmt[] = "%s -t nat -D %s -j %s -w;" "%s -t nat -F %s -w;" "%s -t nat -X %s -w;"; - -typedef unsigned long int ipv4; /* Declare variable type for ipv4 net address. */ - -static GDBusConnection *global_connection = NULL; - -static ipv4 make_mask(int prefix) -{ - ipv4 mask = 0; - int i = 0; - - for (i = prefix; i > 0; i--) - mask += (ipv4) (1 << (32 - i)); - return mask; -} - -static in_addr_t host2net(ipv4 host) -{ - in_addr_t net; - - net = 0; - - net |= (host & 0x000000FF) << 24; - net |= (host & 0x0000FF00) << 8; - net |= (host & 0x00FF0000) >> 8; - net |= (host & 0xFF000000) >> 24; - - return net; -} - -static int add_routes(char* iface_name, char* routes[], int prefix[], size_t nr_routes) -{ - struct rtentry rt; - struct sockaddr_in addr; - int sk; - unsigned int i = 0; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - - LOGD("Enter add_routes"); - - sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (sk < 0) { - LOGE("socket failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - return VPNSVC_ERROR_IO_ERROR; - } - - for (i = 0; i < nr_routes; i++) { - memset(&rt, 0, sizeof(rt)); - rt.rt_flags = RTF_UP; - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(routes[i]); - memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst)); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway)); - - /* set mask using by prefix length */ - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_addr.s_addr = host2net(make_mask(prefix[i])); - memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask)); - - rt.rt_dev = iface_name; - - if (ioctl(sk, SIOCADDRT, &rt) < 0) { - LOGE("ioctl SIOCADDRT failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - } - - close(sk); - - return VPNSVC_ERROR_NONE; -} - -static int add_dns_routes(char* if_name, char** dns_servers, size_t nr_dns) -{ - struct rtentry rt; - struct sockaddr_in addr; - int sk; - unsigned int i = 0; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - - LOGD("Enter add_routes"); - - sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (sk < 0) { - LOGE("socket failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - return VPNSVC_ERROR_IO_ERROR; - } - - for (i = 0; i < nr_dns; i++) { - memset(&rt, 0, sizeof(rt)); - rt.rt_flags = RTF_UP; - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(dns_servers[i]); - memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst)); - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway)); - - /* set mask using by prefix length */ - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_addr.s_addr = host2net(make_mask(32)); - memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask)); - - rt.rt_dev = if_name; - - if (ioctl(sk, SIOCADDRT, &rt) < 0) { - LOGE("ioctl SIOCADDRT failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - } - - close(sk); - - return VPNSVC_ERROR_NONE; -} - -static void connman_connection_open(void) -{ - if (global_connection == NULL) { - GError *error = NULL; -#if !GLIB_CHECK_VERSION(2, 36, 0) - g_type_init(); -#endif - - global_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (global_connection == NULL) { - if (error != NULL) { - LOGE("Error connman connection open: %s", error->message); - g_error_free(error); - } - } - } -} - -static void connman_connection_close(GDBusConnection *connection) -{ - if (connection) - g_object_unref(connection); -} - -static GVariant *connman_method_call( - GDBusConnection *connection, char *service, char *path, - char *interface, char *method, GVariant *params) -{ - GError *error = NULL; - GVariant *message = NULL; - - message = g_dbus_connection_call_sync( - connection, service, path, interface, method, params, - NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); - - if (message == NULL) { - if (error != NULL) { - LOGE("error: g_dbus_connection_call_sync [%d: %s]", error->code, error->message); - g_error_free(error); - } else { - LOGE("error: g_dbus_connection_call_sync\n"); - } - } - - return message; -} - -static char *connman_default_profile(GDBusConnection *connection) -{ - gchar *key = NULL; - GVariantIter *value = NULL; - GVariant *message = NULL; - GVariantIter *iter = NULL; - char *profile = NULL; - - message = connman_method_call(connection, CONNMAN_SERVICE, "/", - CONNMAN_INTERFACE_MANAGER, "GetServices", NULL); - - if (message) { - g_variant_get(message, "(a(oa{sv}))", &iter); - while (g_variant_iter_loop(iter, "(oa{sv})", &key, &value)) { - profile = strdup(key); - break; - } - - if (value) - g_variant_iter_free(value); - if (key) - g_free(key); - - g_variant_iter_free(iter); - g_variant_unref(message); - } - - return profile; -} - -#if 0 -static char *connman_get_items(GDBusConnection *connection, char *profile, const char *keystr) -{ - GVariant *message = NULL; - GVariantIter *iter = NULL; - GVariantIter *next = NULL; - gchar *obj = NULL; - char *items = NULL; - - message = connman_method_call(connection, CONNMAN_SERVICE, "/", - CONNMAN_INTERFACE_MANAGER, "GetServices", NULL); - - if (message) { - g_variant_get(message, "(a(oa{sv}))", &iter); - while (g_variant_iter_loop(iter, "(oa{sv})", &obj, &next)) { - if (strcmp(obj, profile) == 0) { - GVariant *var; - gchar *key; - - while (g_variant_iter_loop(next, "{sv}", &key, &var)) { - if (g_strcmp0(key, keystr) == 0) { - GVariantIter *iter_item; - const gchar *value = NULL; - - g_variant_get(var, "as", &iter_item); - while (g_variant_iter_loop(iter_item, "s", &value)) { - if (items) { - char *tmp_items; - - tmp_items = (char *) malloc(strlen(items) + 1 + strlen(value) + 1); - if (items) { - snprintf(tmp_items, strlen(tmp_items), "%s,%s", items, value); - free(items); - items = tmp_items; - } - } else { - items = strdup(value); - } - } - g_variant_iter_free(iter_item); - break; - } - } - break; - } - } - g_variant_iter_free(iter); - g_variant_unref(message); - } - - return items; -} -#endif - -static void connman_set_items(GDBusConnection *connection, char *profile, - const char *keystr, char *items) -{ - GVariant *message = NULL; - GVariantBuilder *builder = NULL; - GVariant *params = NULL; - char *strings = strdup(items); - char *addr = NULL; - char *temp = NULL; - - builder = g_variant_builder_new(G_VARIANT_TYPE("as")); - if ((addr = strtok_r(strings, ", ", &temp)) != NULL) { - do { - g_variant_builder_add(builder, "s", addr); - } while ((addr = strtok_r(NULL, ", ", &temp)) != NULL); - } - free(strings); - params = g_variant_new("(sv)", keystr, - g_variant_builder_end(builder)); - g_variant_builder_unref(builder); - - message = connman_method_call(connection, CONNMAN_SERVICE, profile, - CONNMAN_INTERFACE_SERVICE, "SetProperty", params); - if (message) - g_variant_unref(message); - -} - -#if 0 -static char *connman_get_nameservers(GDBusConnection *connection, char *profile) -{ - return connman_get_items(connection, profile, "Nameservers"); -} - -static char *connman_get_nameservers_conf(GDBusConnection *connection, char *profile) -{ - return connman_get_items(connection, profile, "Nameservers.Configuration"); -} -#endif - -static void connman_set_nameservers(GDBusConnection *connection, char *profile, - char *nameservers) -{ - return connman_set_items(connection, profile, - "Nameservers.Configuration", nameservers); -} - -#if 0 -static char *connman_get_domains(GDBusConnection *connection, char *profile) -{ - return connman_get_items(connection, profile, "Domains"); -} - -static char *connman_get_domains_conf(GDBusConnection *connection, char *profile) -{ - return connman_get_items(connection, profile, "Domains.Configuration"); -} -#endif - -static void connman_set_domains(GDBusConnection *connection, char *profile, - char *domains) -{ - return connman_set_items(connection, profile, - "Domains.Configuration", domains); -} - -#if 0 -static int add_dns_servers(char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt) -{ - char *profile = NULL; - char *items = NULL; - char *org_items = NULL; - char *new_items = NULL; - unsigned int i = 0; - - connman_connection_open(); - - profile = connman_default_profile(global_connection); - if (profile == NULL) { - LOGE("connman_default_profile failed"); - connman_connection_close(global_connection); - return VPNSVC_ERROR_IPC_FAILED; - } - - LOGD("profile : %s\n", profile); - - /* add name servers */ - org_items = connman_get_nameservers(global_connection, profile); - - if (org_items) { - LOGD("original DNS : %s\n", org_items); - /* nr_dns = comma(,) count */ - items = (char *) calloc((total_dns_string_cnt + nr_dns + strlen(org_items) + 1), sizeof(char)); - if (items == NULL) { - LOGE("OOM while malloc\n"); - return VPNSVC_ERROR_OUT_OF_MEMORY; - } - strncpy(items, org_items, strlen(org_items)); - for (i = 0 ; i < nr_dns ; i++) { - strncat(items, ",", 1); - strncat(items, dns_servers[i], strlen(dns_servers[i])); - } - free(org_items); - org_items = NULL; - } else { - /* nr_dns = comma(,) count + end null char */ - items = (char *) calloc(total_dns_string_cnt + nr_dns, sizeof(char)); - if (items == NULL) { - LOGE("OOM while malloc\n"); - return VPNSVC_ERROR_OUT_OF_MEMORY; - } - for (i = 0 ; i < nr_dns ; i++) { - strncat(items, dns_servers[i], strlen(dns_servers[i])); - if (i != nr_dns - 1) - strncat(items, ",", 1); - } - } - - if (items) { - LOGD("adding DNS : %s\n", items); - connman_set_nameservers(global_connection, profile, items); - free(items); - items = NULL; - } - - /* print new DNSs */ - new_items = connman_get_nameservers_conf(global_connection, profile); - LOGD("new_dns : %s\n", new_items); - - if (new_items) - free(new_items); - free(profile); - return VPNSVC_ERROR_NONE; -} -#endif - -static int del_dns_servers() -{ - char *profile = NULL; - - connman_connection_open(); - - profile = connman_default_profile(global_connection); - if (profile == NULL) { - LOGE("connman_default_profile failed"); - connman_connection_close(global_connection); - return VPNSVC_ERROR_IPC_FAILED; - } - - LOGD("profile : %s", profile); - - /* del name servers */ - connman_set_nameservers(global_connection, profile, ""); - - if (profile) - free(profile); - - return VPNSVC_ERROR_NONE; -} - -#if 0 -static int add_dns_suffix(const char* dns_suffix, size_t dns_suffix_len) -{ - char *profile = NULL; - char *items = NULL; - char *org_items = NULL; - char *new_items = NULL; - - connman_connection_open(); - - profile = connman_default_profile(global_connection); - if (profile == NULL) { - LOGE("connman_default_profile failed"); - connman_connection_close(global_connection); - return VPNSVC_ERROR_IPC_FAILED; - } - - LOGD("profile : %s", profile); - - /* add name servers */ - org_items = connman_get_domains(global_connection, profile); - - if (org_items) { - LOGD("original DNS suffix : %s", org_items); - /* comma(,) and end null character included */ - items = (char *) calloc((dns_suffix_len + strlen(org_items) + 2), sizeof(char)); - if (items == NULL) { - LOGE("OOM while malloc"); - return VPNSVC_ERROR_OUT_OF_MEMORY; - } - strncpy(items, org_items, strlen(org_items)); - strncat(items, ",", 1); - strncat(items, dns_suffix, dns_suffix_len); - free(org_items); - org_items = NULL; - } else { - /* nr_dns = comma(,) count + end null char */ - items = (char *) calloc((dns_suffix_len + 1), sizeof(char)); - if (items == NULL) { - LOGE("OOM while malloc"); - return VPNSVC_ERROR_OUT_OF_MEMORY; - } - strncat(items, dns_suffix, dns_suffix_len); - } - - if (items) { - LOGD("adding DNS suffix : %s\n", items); - connman_set_domains(global_connection, profile, items); - free(items); - items = NULL; - } - - /* print new domains */ - new_items = connman_get_domains_conf(global_connection, profile); - LOGD("new DNS suffix : %s\n", new_items); - - if (new_items) - free(new_items); - - if (profile) - free(profile); - - return VPNSVC_ERROR_NONE; -} -#endif - -static int del_dns_suffix() -{ - char *profile = NULL; - - connman_connection_open(); - - profile = connman_default_profile(global_connection); - if (profile == NULL) { - LOGE("connman_default_profile failed"); - connman_connection_close(global_connection); - return VPNSVC_ERROR_IPC_FAILED; - } - - LOGD("profile : %s", profile); - - /* del DNS suffix */ - connman_set_domains(global_connection, profile, ""); - - if (profile) - free(profile); - - return VPNSVC_ERROR_NONE; -} - - -static void iptables_exec(char *cmdline) -{ - FILE *fp = NULL; - - fp = popen(cmdline, "r"); - - if (fp != NULL) - pclose(fp); -} - -#if 0 -static void dns_nat_register(char **vpn_dns_address, size_t nr_dns, char *vpn_device_address) -{ - int size = 0, i; - char buf[8192]; - - snprintf(buf + size, sizeof(buf) - size, iptables_nat_register_init_fmt, - iptables_cmd, iptables_nat_chain_name, - iptables_cmd, iptables_nat_chain_name, - iptables_cmd, iptables_filter_out, iptables_nat_chain_name); - size = strlen(buf); - - for (i = 0 ; i < nr_dns ; i++) { - snprintf(buf + size, sizeof(buf) - size, iptables_nat_register_rule_fmt, - iptables_cmd, iptables_nat_chain_name, vpn_dns_address[i], vpn_device_address); - size = strlen(buf); - } - LOGD("iptable dns nat reg cmd : %s", buf); - iptables_exec(buf); -} -#endif - -static void dns_nat_unregister(void) -{ - int size = 0; - char buf[8192]; - - snprintf(buf + size, sizeof(buf) - size, iptables_nat_unregister_fmt, - iptables_cmd, iptables_filter_out, iptables_nat_chain_name, - iptables_cmd, iptables_nat_chain_name, - iptables_cmd, iptables_nat_chain_name); - size = strlen(buf); - LOGD("iptable dns nat unreg cmd : %s", buf); - iptables_exec(buf); -} - -static void iptables_register(void) -{ - int size = 0; - char buf[8192], *filter; - - filter = iptables_filter_out; - snprintf(buf + size, sizeof(buf) - size, iptables_register_fmt, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, filter, iptables_filter_prefix, filter); - size = strlen(buf); - filter = iptables_filter_in; - snprintf(buf + size, sizeof(buf) - size, iptables_register_fmt, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, filter, iptables_filter_prefix, filter); - LOGD("iptable reg cmd : %s", buf); - iptables_exec(buf); -} - -static void iptables_unregister(void) -{ - int size = 0; - char buf[8192], *filter; - - filter = iptables_filter_out; - snprintf(buf + size, sizeof(buf) - size, iptables_unregister_fmt, - iptables_cmd, filter, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter); - size = strlen(buf); - filter = iptables_filter_in; - snprintf(buf + size, sizeof(buf) - size, iptables_unregister_fmt, - iptables_cmd, filter, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter, - iptables_cmd, iptables_filter_prefix, filter); - LOGD("iptable unreg cmd : %s", buf); - iptables_exec(buf); -} - -static void iptables_rule(const char c, const char *addr, const int mask) -{ - int size = 0; - char buf[4096]; - - snprintf(buf + size, sizeof(buf) - size, iptables_rule_fmt, iptables_cmd, c, - iptables_filter_prefix, iptables_filter_out, 'd', addr, mask); - size = strlen(buf); - snprintf(buf + size, sizeof(buf) - size, iptables_rule_fmt, iptables_cmd, c, - iptables_filter_prefix, iptables_filter_in, 's', addr, mask); - LOGD("iptable cmd : %s", buf); - iptables_exec(buf); -} - -static void iptables_rule_interface(const char c, const char *addr, const int mask, const char *interface) -{ - int size = 0; - char buf[4096]; - - snprintf(buf + size, sizeof(buf) - size, - iptables_rule_with_interface_fmt, iptables_cmd, - c, iptables_filter_prefix, iptables_filter_out, - 'o', interface, 'd', addr, mask); - size = strlen(buf); - snprintf(buf + size, sizeof(buf) - size, - iptables_rule_with_interface_fmt, iptables_cmd, - c, iptables_filter_prefix, iptables_filter_in, - 'i', interface, 's', addr, mask); - LOGD("iptable cmd : %s", buf); - iptables_exec(buf); -} - -void iptables_add_orig(const char *addr, const int mask) -{ - iptables_rule_interface('I', addr, mask, iptables_filter_interface_wlan); -} - -void iptables_delete_orig(const char *addr, const int mask) -{ - iptables_rule_interface('D', addr, mask, iptables_filter_interface_wlan); -} - -void iptables_add(const char *addr, const int mask) -{ - iptables_rule('I', addr, mask); -} - -void iptables_delete(const char *addr, const int mask) -{ - iptables_rule('D', addr, mask); -} - -static int get_interface_index(const char *iface_name) -{ - struct ifreq ifr; - int sk = 0; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - - LOGD("enter get_interface_index, iface_name : %s", iface_name); - - sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (sk < 0) { - LOGE("socket failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - return VPNSVC_ERROR_IO_ERROR; - } - - memset(&ifr, 0, sizeof(ifr)); - - if (*iface_name) - strncpy(ifr.ifr_name, iface_name, strlen(iface_name)); - - /* get an interface name by ifindex */ - if (ioctl(sk, SIOCGIFINDEX, &ifr) < 0) { - LOGE("ioctl SIOCGIFINDEX failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - close(sk); - - return ifr.ifr_ifindex; -} - - -int vpn_daemon_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 }; - - LOGD("enter vpn_daemon_init, iface_name : %s, iface_name_len : %d, fd : %d\n", iface_name, iface_name_len, fd); - - memset(&ifr, 0, sizeof(ifr)); - - /* Flags: IFF_TUN - TUN device (no Ethernet headers) - * IFF_TAP - TAP device - * - * IFF_NO_PI - Do not provide packet information - */ - - ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - - if (*iface_name) - strncpy(ifr.ifr_name, iface_name, iface_name_len); - - LOGD("before init, ifindex : %d", ifr.ifr_ifindex); - - if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) { - LOGE("TUNSETIFF Failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(fd); - return VPNSVC_ERROR_IO_ERROR; - } - - if (ioctl(fd, TUNSETOWNER, 5000) < 0) { - LOGE("TUNSETOWNER Failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(fd); - return VPNSVC_ERROR_IO_ERROR; - } - - if (ioctl(fd, TUNSETPERSIST, 1) < 0) { - LOGE("TUNSETPERSIST Failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(fd); - return VPNSVC_ERROR_IO_ERROR; - } - - 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'; - - return VPNSVC_ERROR_NONE; -} - -int vpn_daemon_deinit(const char* dev_name) -{ - char buf[100]; - FILE *fp = NULL; - - snprintf(buf, sizeof(buf), "/usr/sbin/ip link del %s", dev_name); - LOGD("link delete cmd : %s", buf); - - fp = popen(buf, "r"); - if (fp != NULL) { - pclose(fp); - return VPNSVC_ERROR_NONE; - } else { - return VPNSVC_ERROR_IO_ERROR; - } -} - -int vpn_daemon_protect(int socket_fd, const char* dev_name) -{ - int ret = VPNSVC_ERROR_NONE; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - LOGD("enter vpn_daemon_protect, socket : %d, dev_name : %s\n", socket_fd, dev_name); - - ret = setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, - dev_name, strlen(dev_name)); - - if (ret < 0) { - LOGD("setsockopt failed : %d, %s", ret, strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - ret = VPNSVC_ERROR_IO_ERROR; - } else { - ret = VPNSVC_ERROR_NONE; - } - - return ret; -} - -int vpn_daemon_up(int iface_index, const char* local_ip, const char* remote_ip, - char* routes[], int prefix[], size_t nr_routes, - char** dns_servers, size_t nr_dns, size_t total_dns_string_cnt, - const char* dns_suffix, const unsigned int mtu) { - - struct sockaddr_in local_addr; - struct sockaddr_in remote_addr; - struct ifreq ifr_tun; - int sk; - int ret = VPNSVC_ERROR_NONE; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - - LOGD("enter vpn_daemon_up"); - - LOGD("iface_index : %d", iface_index); - LOGD("local ip : %s", local_ip); - LOGD("remote ip : %s", remote_ip); - LOGD("route pointer : %p, nr_routes : %d, dns_server pointer : %p, nr_dns : %d, dns_suffix : %s, mtu : %d", routes, nr_routes, dns_servers, nr_dns, dns_suffix, mtu); - - - sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (sk < 0) { - LOGE("socket failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - return VPNSVC_ERROR_IO_ERROR; - } - - memset(&ifr_tun, 0, sizeof(ifr_tun)); - ifr_tun.ifr_ifindex = iface_index; - - /* get an interface name by ifindex */ - if (ioctl(sk, SIOCGIFNAME, &ifr_tun) < 0) { - LOGE("ioctl SIOCGIFNAME failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - /* local ip setting */ - memset(&local_addr, 0, sizeof(local_addr)); - local_addr.sin_addr.s_addr = inet_addr(local_ip); /* network byte order */ - local_addr.sin_family = AF_INET; - memcpy(&ifr_tun.ifr_addr, &local_addr, sizeof(ifr_tun.ifr_addr)); - if (ioctl(sk, SIOCSIFADDR, &ifr_tun) < 0) { - LOGE("ioctl SIOCSIFADDR failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - /* remote ip setting */ - memset(&remote_addr, 0, sizeof(remote_addr)); - remote_addr.sin_addr.s_addr = inet_addr(remote_ip); /*network byte order*/ - remote_addr.sin_family = AF_INET; - memcpy(&ifr_tun.ifr_dstaddr, &remote_addr, sizeof(ifr_tun.ifr_dstaddr)); - if (ioctl(sk, SIOCSIFDSTADDR, &ifr_tun) < 0) { - LOGE("ioctl SIOCSIFDSTADDR failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - /* set the flags for vpn up */ - if (ioctl(sk, SIOCGIFFLAGS, &ifr_tun) < 0) { - LOGE("ioctl SIOCGIFFLAGS failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - ifr_tun.ifr_flags |= IFF_UP; - ifr_tun.ifr_flags |= IFF_RUNNING; - - if (ioctl(sk, SIOCSIFFLAGS, &ifr_tun) < 0) { - LOGE("ioctl SIOCSIFFLAGS failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - /* mtu setting */ - if (ioctl(sk, SIOCGIFMTU, &ifr_tun) < 0) { - LOGE("ioctl SIOCGIFMTU failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - if (mtu > 0 && ifr_tun.ifr_mtu != (int)mtu) { - ifr_tun.ifr_mtu = mtu; - if (ioctl(sk, SIOCSIFMTU, &ifr_tun) < 0) { - LOGE("ioctl SIOCSIFMTU failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - } - - close(sk); - - /* add routes */ - if (nr_routes > 0) { - ret = add_routes(ifr_tun.ifr_name, routes, prefix, nr_routes); - if (ret != VPNSVC_ERROR_NONE) { - LOGE("add_routes failed"); - return ret; - } - } - - /* add DNS routes */ - if (nr_dns > 0) { - ret = add_dns_routes(ifr_tun.ifr_name, dns_servers, nr_dns); - if (ret != VPNSVC_ERROR_NONE) { - LOGE("add_dns failed"); - return ret; - } - } - -#if 0 - /* add DNS servers */ - if (nr_dns > 0) { - ret = add_dns_servers(dns_servers, nr_dns, total_dns_string_cnt); - if (ret != VPNSVC_ERROR_NONE) { - LOGE("add_dns failed"); - return ret; - } - } - - /* add_dns_suffix */ - if (dns_suffix) { - ret = add_dns_suffix(dns_suffix, strlen(dns_suffix)); - if (ret != VPNSVC_ERROR_NONE) { - LOGE("add_dns_suffix failed"); - return ret; - } - } - - if (nr_dns > 0) - dns_nat_register(dns_servers, nr_dns, local_ip); -#endif - - return ret; -} - - - -int vpn_daemon_down(int iface_index) -{ - struct ifreq ifr, addr_ifr; - struct sockaddr_in *addr = NULL; - int sk; - char buf[BUF_SIZE_FOR_ERR] = { 0 }; - - sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); - if (sk < 0) { - LOGE("socket failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - return VPNSVC_ERROR_IO_ERROR; - } - - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_ifindex = iface_index; - - if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) { - LOGE("ioctl SIOCGIFNAME failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) { - LOGE("ioctl SIOCGIFFLAGS failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - memset(&addr_ifr, 0, sizeof(addr_ifr)); - memcpy(&addr_ifr.ifr_name, &ifr.ifr_name, sizeof(ifr.ifr_name) - 1); - addr = (struct sockaddr_in *)&addr_ifr.ifr_addr; - addr->sin_family = AF_INET; - if (ioctl(sk, SIOCSIFADDR, &addr_ifr) < 0) - LOGD("ioctl SIOCSIFADDR (could not clear IP address) failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - - if (!(ifr.ifr_flags & IFF_UP)) { - LOGD("Interface already down"); - close(sk); - return VPNSVC_ERROR_NONE; - } - - ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC; - if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) { - LOGE("ioctl SIOCSIFFLAGS (interface down) failed : %s", strerror_r(errno, buf, BUF_SIZE_FOR_ERR)); - close(sk); - return VPNSVC_ERROR_IO_ERROR; - } - - close(sk); - - /* routes are will be removed automatically while down interfaces */ - /* remove dns servers */ - del_dns_servers(); - - /* remove dns suffix */ - del_dns_suffix(); - - /* remove dns filter */ - dns_nat_unregister(); - - return VPNSVC_ERROR_NONE; -} - -int vpn_daemon_block_networks(char* nets_vpn[], int prefix_vpn[], size_t nr_nets_vpn, - char* nets_orig[], int prefix_orig[], size_t nr_nets_orig) { - unsigned int i; - - /* iptable chain regist */ - iptables_register(); - - for (i = 0; i < nr_nets_vpn; i++) { - LOGD("block[%d] ip/mask : %s/%d", i, nets_vpn[i], prefix_vpn[i]); - iptables_add(nets_vpn[i], prefix_vpn[i]); - } - - for (i = 0; i < nr_nets_orig; i++) { - LOGD("allow[%d] ip/mask : %s/%d", i, nets_orig[i], prefix_orig[i]); - iptables_add_orig(nets_orig[i], prefix_orig[i]); - } - - return VPNSVC_ERROR_NONE; -} - -int vpn_daemon_unblock_networks(void) -{ - iptables_unregister(); - - return VPNSVC_ERROR_NONE; -} diff --git a/daemon/src/vpn_service_daemon_main.c b/daemon/src/vpn_service_daemon_main.c deleted file mode 100755 index ff3037b..0000000 --- a/daemon/src/vpn_service_daemon_main.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include /* exit, EXIT_FAILURE */ -#include - -#include "vpnsvc.h" -#include "vpndbus.h" - - -#ifdef LOG_TAG -#undef LOG_TAG -#endif - -#define LOG_TAG "VPNSVC_DAEMON" - -static GMainLoop *main_loop = NULL; - -static void __vpnsvc_got_name_cb(void) -{ - vpnsvc_create_and_init(); -} - -int main(void) -{ - int ret; - - LOGD("VPN Service"); - if (daemon(0, 0) != 0) - LOGD("Cannot start daemon"); - -#if !GLIB_CHECK_VERSION(2, 36, 0) - g_type_init(); -#endif - - main_loop = g_main_loop_new(NULL, FALSE); - if (main_loop == NULL) { - LOGE("Couldn't create GMainLoop\n"); - return 0; - } - - ret = vpnsvc_setup_gdbus(__vpnsvc_got_name_cb); - if (ret > 0) { - LOGE("_vpnsvc_setup_gdbus is failed\n"); - return 0; - } - - g_main_loop_run(main_loop); - - vpnsvc_cleanup_gdbus(); - vpnsvc_destroy_deinit(); - - return 0; -} diff --git a/daemon/src/vpndbus.c b/daemon/src/vpndbus.c deleted file mode 100755 index d079d84..0000000 --- a/daemon/src/vpndbus.c +++ /dev/null @@ -1,241 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include -#include -#include -#include - -#include "vpndbus.h" - -#ifdef LOG_TAG -#undef LOG_TAG -#endif -#define LOG_TAG "VPNSVC_DAEMON" - -#define DBUS_REPLY_TIMEOUT (120 * 1000) - -static GDBusObjectManagerServer *manager_server_vpn = NULL; -static guint owner_id = 0; -static vpnsvc_got_name_cb g_callback = NULL; - -struct gdbus_conn_data { - GDBusConnection *connection; - int conn_ref_count; - GCancellable *cancellable; -}; - -static struct gdbus_conn_data gconn_data = {NULL, 0, NULL}; - -GDBusObjectManagerServer *vpnsvc_get_vpn_manager(void) -{ - return manager_server_vpn; -} - -GDBusConnection *vpnsvc_gdbus_get_connection(void) -{ - return gconn_data.connection; -} - -GCancellable *vpnsvc_gdbus_get_gdbus_cancellable(void) -{ - return gconn_data.cancellable; -} - -void vpnsvc_gdbus_pending_call_ref(void) -{ - g_object_ref(gconn_data.connection); - - __sync_fetch_and_add(&gconn_data.conn_ref_count, 1); -} - -void vpnsvc_gdbus_pending_call_unref(void) -{ - if (gconn_data.conn_ref_count < 1) - return; - - g_object_unref(gconn_data.connection); - - if (__sync_sub_and_fetch(&gconn_data.conn_ref_count, 1) < 1) { - /* TODO: Check this - * gconn_data.connection = NULL; - */ - } -} - -int vpnsvc_create_gdbus_call(GDBusConnection *conn) -{ - if (gconn_data.connection != NULL) { - LOGE("Connection already set"); - return -1; - } - - gconn_data.connection = conn; - if (gconn_data.connection == NULL) { - LOGE("Failed to connect to the D-BUS daemon"); - return -1; - } - - gconn_data.cancellable = g_cancellable_new(); - - return 0; -} - - -gboolean vpnsvc_invoke_dbus_method_nonblock(const char *dest, const char *path, - const char *interface_name, const char *method, GVariant *params, - GAsyncReadyCallback notify_func) -{ - GDBusConnection *connection = NULL; - - LOGD("[GDBUS Async] %s %s %s", interface_name, method, path); - - connection = vpnsvc_gdbus_get_connection(); - if (connection == NULL) { - LOGE("Failed to get gdbus connection"); - return FALSE; - } - - g_dbus_connection_call(connection, - dest, - path, - interface_name, - method, - params, - NULL, - G_DBUS_CALL_FLAGS_NONE, - DBUS_REPLY_TIMEOUT, - vpnsvc_gdbus_get_gdbus_cancellable(), - (GAsyncReadyCallback) notify_func, - NULL); - - if (notify_func != NULL) - vpnsvc_gdbus_pending_call_ref(); - - return TRUE; -} - -GVariant *vpnsvc_invoke_dbus_method(const char *dest, const char *path, - const char *interface_name, const char *method, GVariant *params) -{ - - GError *error = NULL; - GVariant *reply = NULL; - GDBusConnection *connection; - - connection = vpnsvc_gdbus_get_connection(); - if (connection == NULL) { - LOGE("Failed to get GDBusconnection"); - return reply; - } - - reply = g_dbus_connection_call_sync( - connection, - dest, - path, - interface_name, - method, - params, - NULL, - G_DBUS_CALL_FLAGS_NONE, - DBUS_REPLY_TIMEOUT, - vpnsvc_gdbus_get_gdbus_cancellable(), - &error); - - if (reply == NULL) { - if (error != NULL) { - LOGE("g_dbus_connection_call_sync() failed" - "error [%d: %s]", error->code, error->message); - g_error_free(error); - } else { - LOGE("g_dbus_connection_call_sync() failed"); - } - - return NULL; - } - - return reply; -} - -static void __vpnsvc_got_bus_cb(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - LOGD("connection: [%p] name: [%s] user_data: [%p]", conn, name, user_data); - - vpnsvc_create_gdbus_call(conn); -} - -static void __vpnsvc_got_name_cb(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - LOGD("connection: [%p] name: [%s] user_data: [%p]", conn, name, user_data); - - if (g_callback != NULL) - g_callback(); -} - -static void __vpnsvc_lost_name_cb(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - LOGD("connection: [%p] name: [%s] user_data: [%p]", conn, name, user_data); - /* May service name is already in use */ - LOGE("Service name is already in use"); - - /* The result of DBus name request is only permitted, - * such as DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER. - */ - exit(2); -} - -int vpnsvc_setup_gdbus(vpnsvc_got_name_cb cb) -{ - LOGD("VPN Service Setup!"); - - g_callback = cb; - - manager_server_vpn = g_dbus_object_manager_server_new( - VPNSERVICE_PATH); - if (manager_server_vpn == NULL) { - LOGE("Manager server for VPNSERVICE_PATH not created."); - exit(1); - } - - owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, VPNSERVICE_SERVICE, - G_BUS_NAME_OWNER_FLAGS_NONE, __vpnsvc_got_bus_cb, - __vpnsvc_got_name_cb, __vpnsvc_lost_name_cb, - NULL, NULL); - if (!owner_id) { - LOGE("Could not get system bus!"); - return -EIO; - } - - LOGI("Got system bus!"); - return 0; -} - -void vpnsvc_cleanup_gdbus(void) -{ - LOGD("VPN Service Cleanup!"); - - g_bus_unown_name(owner_id); - g_object_unref(manager_server_vpn); - - return; -} diff --git a/daemon/src/vpnerror.c b/daemon/src/vpnerror.c deleted file mode 100755 index 2369c26..0000000 --- a/daemon/src/vpnerror.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include - -#include "vpndbus.h" -#include "vpnerror.h" - -#define VPNSVC_ERROR_INTERFACE VPNSERVICE_INTERFACE ".Error" - -void vpnsvc_error_inprogress(GDBusMethodInvocation *context) -{ - LOGE("dbus method return error"); - g_dbus_method_invocation_return_error(context, - G_DBUS_ERROR, - G_DBUS_ERROR_ACCESS_DENIED, - VPNSVC_ERROR_INTERFACE ".InProgress"); -} - -void vpnsvc_error_invalid_parameter(GDBusMethodInvocation *context) -{ - LOGE("dbus method return error"); - g_dbus_method_invocation_return_error(context, - G_DBUS_ERROR, - G_DBUS_ERROR_ACCESS_DENIED, - VPNSVC_ERROR_INTERFACE ".InvalidParameter"); -} - -void vpnsvc_error_permission_denied(GDBusMethodInvocation *context) -{ - LOGE("dbus method return error"); - g_dbus_method_invocation_return_error(context, - G_DBUS_ERROR, - G_DBUS_ERROR_ACCESS_DENIED, - VPNSVC_ERROR_INTERFACE ".PermissionDenied"); -} diff --git a/daemon/src/vpnsvc.c b/daemon/src/vpnsvc.c deleted file mode 100755 index afde6f6..0000000 --- a/daemon/src/vpnsvc.c +++ /dev/null @@ -1,515 +0,0 @@ -/* - * VPN Service Module - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include -#include -#include -#include - -#include "vpnsvc.h" -#include "vpndbus.h" -#include "vpnerror.h" -#include "vpn_service_daemon.h" - -#include "cynara-client.h" -#include "cynara-creds-gdbus.h" -#include "cynara-session.h" - -#ifdef LOG_TAG -#undef LOG_TAG -#endif -#define LOG_TAG "VPNSVC_DAEMON" - -static Vpnsvc *vpnsvc = NULL; - -/********************* - * Handler Functions * - ********************/ -gboolean handle_vpn_init(Vpnsvc *object, - GDBusMethodInvocation *invocation, - const gchar *arg_iface_name, - guint arg_iface_name_len) -{ - LOGD("handle_vpn_init"); - - int result = VPNSVC_ERROR_NONE; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false - || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - vpnsvc_tun_s handle_s; - GDBusMessage *msg; - GUnixFDList *fd_list; - int fd_list_length; - const int *fds; - - LOGD("vpn_init, %s, %u\n", arg_iface_name, arg_iface_name_len); - - msg = g_dbus_method_invocation_get_message(invocation); - fd_list = g_dbus_message_get_unix_fd_list(msg); - fds = g_unix_fd_list_peek_fds(fd_list, &fd_list_length); - - if (fd_list_length <= 0) - LOGD("D-Bus Message doesn't contain any fd!"); - - LOGD("fd:%d\n", *fds); - - result = vpn_daemon_init(arg_iface_name, arg_iface_name_len, *fds, &handle_s); - - LOGD("handle_s.fd : %d, handle_s.index : %d, handle_s.name : %s", - handle_s.fd, handle_s.index, handle_s.name); - - vpnsvc_complete_vpn_init(object, invocation, result, handle_s.index, handle_s.name); - - return TRUE; -} - -gboolean handle_vpn_deinit(Vpnsvc *object, - GDBusMethodInvocation *invocation, - const gchar *arg_dev_name) -{ - LOGD("handle_vpn_deinit"); - - int result = VPNSVC_ERROR_NONE; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false - || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - LOGD("vpn_deinit, %s\n", arg_dev_name); - - result = vpn_daemon_deinit(arg_dev_name); - - vpnsvc_complete_vpn_deinit(object, invocation, result); - - return TRUE; -} - -gboolean handle_vpn_protect(Vpnsvc *object, - GDBusMethodInvocation *invocation, - const gchar *arg_dev_name) -{ - LOGD("handle_vpn_protect"); - - int result = VPNSVC_ERROR_NONE; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false - || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - int socket; - GDBusMessage *msg; - GUnixFDList *fd_list; - int fd_list_length; - const int *fds; - - msg = g_dbus_method_invocation_get_message(invocation); - fd_list = g_dbus_message_get_unix_fd_list(msg); - fds = g_unix_fd_list_peek_fds(fd_list, &fd_list_length); - if (fd_list_length <= 0) - LOGD("D-Bus Message doesn't contain any fd!"); - - socket = *fds; - LOGD("vpn_protect, %d, %s\n", socket, arg_dev_name); - - result = vpn_daemon_protect(socket, arg_dev_name); - - vpnsvc_complete_vpn_protect(object, invocation, result); - - return TRUE; -} - -gboolean handle_vpn_up(Vpnsvc *object, - GDBusMethodInvocation *invocation, - gint arg_iface_index, - const gchar *arg_local_ip, - const gchar *arg_remote_ip, - GVariant *arg_routes, - guint arg_nr_routes, - GVariant *arg_dns_servers, - guint arg_nr_dns, - const gchar *arg_dns_suffix, - guint arg_mtu) -{ - LOGD("handle_vpn_up"); - - int result = VPNSVC_ERROR_NONE; - - char *routes[arg_nr_routes]; - int prefix[arg_nr_routes]; - char **dns_servers = NULL; - - unsigned int i = 0; - size_t total_dns_string_cnt = 0; - gchar* temp_dns_server; - GVariantIter iter; - - gchar* route_dest; - gint route_prefix; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE_ADMIN) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - LOGD("iface_index : %d", arg_iface_index); - LOGD("local ip : %s", arg_local_ip); - LOGD("remote ip : %s", arg_remote_ip); - LOGD("dns_suffix : %s", arg_dns_suffix); - LOGD("mtu : %u", arg_mtu); - LOGD("arg_routes: %p", arg_routes); - LOGD("nr_routes : %u", arg_nr_routes); - LOGD("arg_dns_servers: %p", arg_dns_servers); - LOGD("nr_dns : %u", arg_nr_dns); - - /* arg_routes check */ - if (arg_nr_routes > 0) { - if (arg_routes != NULL) { - GVariant *dict = g_variant_get_variant(arg_routes); - g_variant_iter_init(&iter, dict); - i = 0; - while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { - int temp_dest_str_len = strlen(route_dest); - routes[i] = g_try_malloc0((sizeof(char) * temp_dest_str_len)+1); - strncpy(routes[i], route_dest, temp_dest_str_len); - routes[i][temp_dest_str_len] = '\0'; - prefix[i] = route_prefix; - LOGD("routes[%d] = %s \t", i, (routes[i] == NULL) ? "" : routes[i]); - LOGD("prefix[%d] = %d ", i, prefix[i]); - i++; - } - } - } - - - /* arg_nr_dns check */ - if (arg_nr_dns > 0) { - if (arg_dns_servers != NULL) { - GVariant *array = g_variant_get_variant(arg_dns_servers); - dns_servers = (char **)g_try_malloc0(arg_nr_dns*sizeof(char *)); - if (dns_servers == NULL) { - LOGE("malloc failed."); - result = VPNSVC_ERROR_OUT_OF_MEMORY; - goto done; - } - g_variant_iter_init(&iter, array); - i = 0; - while (g_variant_iter_loop(&iter, "s", &temp_dns_server)) { - int temp_dns_str_len = strlen(temp_dns_server); - dns_servers[i] = (char *)g_try_malloc0((temp_dns_str_len + 1) * sizeof(char)); - strncpy(dns_servers[i], temp_dns_server, strlen(temp_dns_server)); - dns_servers[i][temp_dns_str_len] = '\0'; - total_dns_string_cnt += temp_dns_str_len; - LOGD("dns_servers[%d] : %s", i, (dns_servers[i] == NULL) ? "" : dns_servers[i]); - i++; - } - } - } - - result = vpn_daemon_up(arg_iface_index, arg_local_ip, arg_remote_ip, - routes, prefix, arg_nr_routes, dns_servers, arg_nr_dns, - total_dns_string_cnt, arg_dns_suffix, arg_mtu); -done: - /* free pointers */ - for (i = 0; i < arg_nr_routes; i++) { - if (routes[i]) - g_free(routes[i]); - } - - if (dns_servers) { - for (i = 0; i < arg_nr_dns; i++) { - if (dns_servers[i]) - g_free(dns_servers[i]); - } - g_free(dns_servers); - } - - vpnsvc_complete_vpn_up(object, invocation, result); - - return TRUE; -} - -gboolean handle_vpn_down(Vpnsvc *object, - GDBusMethodInvocation *invocation, - gint arg_iface_index) -{ - LOGD("handle_vpn_down"); - - int result = VPNSVC_ERROR_NONE; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE_ADMIN) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - LOGD("vpn_down, %d\n", arg_iface_index); - - result = vpn_daemon_down(arg_iface_index); - - vpnsvc_complete_vpn_down(object, invocation, result); - - return TRUE; -} - -gboolean handle_vpn_block_networks(Vpnsvc *object, - GDBusMethodInvocation *invocation, - GVariant *arg_nets_vpn, - guint arg_nr_nets_vpn, - GVariant *arg_nets_orig, - guint arg_nr_nets_orig) -{ - LOGD("handle_vpn_block_networks"); - - int result = VPNSVC_ERROR_NONE; - - char *nets_vpn[arg_nr_nets_vpn]; - int prefix_vpn[arg_nr_nets_vpn]; - - char *nets_orig[arg_nr_nets_vpn]; - int prefix_orig[arg_nr_nets_vpn]; - - int i = 0; - GVariantIter iter; - gchar* route_dest; - gint route_prefix; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false - || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - LOGD("vpn_block_networks"); - - /* arg_nets_vpn check */ - if (arg_nr_nets_vpn > 0) { - if (arg_nets_vpn != NULL) { - GVariant *dict_nets_vpn = g_variant_get_variant(arg_nets_vpn); - g_variant_iter_init(&iter, dict_nets_vpn); - i = 0; - while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { - int tmp_route_len = strlen(route_dest); - nets_vpn[i] = g_try_malloc0(sizeof(char) * tmp_route_len + 1); - strncpy(nets_vpn[i], route_dest, tmp_route_len); - nets_vpn[i][tmp_route_len] = '\0'; - prefix_vpn[i] = route_prefix; - LOGD("nets_vpn[%d] = %s \t", i, (nets_vpn[i] == NULL) ? "" : nets_vpn[i]); - LOGD("prefix_vpn[%d] = %d ", i, prefix_vpn[i]); - i++; - } - } - } - - /* arg_nets_orig check */ - if (arg_nr_nets_orig > 0) { - if (arg_nets_orig != NULL) { - GVariant *dict_nets_orig = g_variant_get_variant(arg_nets_orig); - g_variant_iter_init(&iter, dict_nets_orig); - i = 0; - while (g_variant_iter_loop(&iter, "{si}", &route_dest, &route_prefix)) { - int tmp_route_len = strlen(route_dest); - nets_orig[i] = g_try_malloc0(sizeof(char) * tmp_route_len + 1); - strncpy(nets_orig[i], route_dest, tmp_route_len); - nets_orig[i][tmp_route_len] = '\0'; - prefix_orig[i] = route_prefix; - LOGD("nets_orig[%d] = %s \t", i, (nets_orig[i] == NULL) ? "" : nets_orig[i]); - LOGD("prefix_orig[%d] = %d ", i, prefix_orig[i]); - i++; - } - } - } - - /* call function */ - result = vpn_daemon_block_networks(nets_vpn, prefix_vpn, arg_nr_nets_vpn, nets_orig, prefix_orig, arg_nr_nets_orig); - - for (i = 0; i < arg_nr_nets_vpn; ++i) { - g_free(nets_orig[i]); - g_free(nets_vpn[i]); - } - - vpnsvc_complete_vpn_block_networks(object, invocation, result); - - return TRUE; -} - -gboolean handle_vpn_unblock_networks(Vpnsvc *object, - GDBusMethodInvocation *invocation) -{ - LOGD("handle_vpn_unblock_networks"); - - int result = VPNSVC_ERROR_NONE; - - /* check privilege */ - if (vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_VPN_SERVICE) == false - || vpn_service_gdbus_check_privilege(invocation, PRIVILEGE_INTERNET) == false) { - vpnsvc_error_permission_denied(invocation); - return FALSE; - } - - LOGD("vpn_unblock_networks"); - - result = vpn_daemon_unblock_networks(); - - vpnsvc_complete_vpn_unblock_networks(object, invocation, result); - - return TRUE; -} - -/***************************** - * Initializations Functions * - ****************************/ -Vpnsvc *get_vpnsvc_object(void) -{ - return vpnsvc; -} - -void vpnsvc_create_and_init(void) -{ - LOGD("Create vpn object."); - GDBusInterfaceSkeleton *interface = NULL; - GDBusConnection *connection; - GDBusObjectManagerServer *server = vpnsvc_get_vpn_manager(); - if (server == NULL) - return; - - connection = vpnsvc_gdbus_get_connection(); - g_dbus_object_manager_server_set_connection(server, connection); - - /* Interface */ - vpnsvc = vpnsvc_skeleton_new(); - interface = G_DBUS_INTERFACE_SKELETON(vpnsvc); - - /* VPN Service */ - g_signal_connect(vpnsvc, "handle-vpn-init", - G_CALLBACK(handle_vpn_init), NULL); - g_signal_connect(vpnsvc, "handle-vpn-deinit", - G_CALLBACK(handle_vpn_deinit), NULL); - g_signal_connect(vpnsvc, "handle-vpn-protect", - G_CALLBACK(handle_vpn_protect), NULL); - g_signal_connect(vpnsvc, "handle-vpn-up", - G_CALLBACK(handle_vpn_up), NULL); - g_signal_connect(vpnsvc, "handle-vpn-down", - G_CALLBACK(handle_vpn_down), NULL); - g_signal_connect(vpnsvc, "handle-vpn-block-networks", - G_CALLBACK(handle_vpn_block_networks), NULL); - g_signal_connect(vpnsvc, "handle-vpn-unblock-networks", - G_CALLBACK(handle_vpn_unblock_networks), NULL); - - if (!g_dbus_interface_skeleton_export(interface, connection, - VPNSERVICE_PATH, NULL)) { - LOGE("Export VPNSERVICE_PATH for vpn failed"); - } - - return; -} - -void vpnsvc_destroy_deinit(void) -{ - LOGD("Deinit vpn object."); - - if (vpnsvc) - g_object_unref(vpnsvc); -} - -gboolean vpn_service_gdbus_check_privilege(GDBusMethodInvocation *invocation, net_vpn_service_privilege_e _privilege) -{ - - int ret = 0; - int pid = 0; - char *user; - char *client; - char *client_session; - char *privilege = NULL; - cynara *p_cynara = NULL; - const char *sender_unique_name; - GDBusConnection *connection; - - connection = g_dbus_method_invocation_get_connection(invocation); - sender_unique_name = g_dbus_method_invocation_get_sender(invocation); - - ret = cynara_initialize(&p_cynara, NULL); - if (ret != CYNARA_API_SUCCESS) { - LOGD("cynara_initialize() failed"); - return FALSE; - } - - ret = cynara_creds_gdbus_get_pid(connection, sender_unique_name, &pid); - if (ret != CYNARA_API_SUCCESS) { - LOGD("cynara_creds_gdbus_get_pid() failed"); - return FALSE; - } - - ret = cynara_creds_gdbus_get_user(connection, sender_unique_name, USER_METHOD_DEFAULT, &user); - if (ret != CYNARA_API_SUCCESS) { - LOGD("cynara_creds_gdbus_get_user() failed"); - return FALSE; - } - - ret = cynara_creds_gdbus_get_client(connection, sender_unique_name, CLIENT_METHOD_DEFAULT, &client); - if (ret != CYNARA_API_SUCCESS) { - LOGD("cynara_creds_gdbus_get_client() failed"); - return FALSE; - } - - switch (_privilege) { - case PRIVILEGE_VPN_SERVICE: - privilege = "http://tizen.org/privilege/vpnservice"; - break; - - case PRIVILEGE_VPN_SERVICE_ADMIN: - privilege = "http://tizen.org/privilege/vpnservice.admin"; - break; - - case PRIVILEGE_INTERNET: - privilege = "http://tizen.org/privilege/internet"; - break; - default: - LOGD("Undifined privilege"); - return FALSE; - break; - } - - client_session = cynara_session_from_pid(pid); - - ret = cynara_check(p_cynara, client, client_session, user, privilege); - if (ret == CYNARA_API_ACCESS_ALLOWED) - LOGD("cynara PASS"); - - cynara_finish(p_cynara); - - g_free(client); - g_free(user); - g_free(client_session); - - return (ret == CYNARA_API_ACCESS_ALLOWED) ? TRUE : FALSE; -} diff --git a/daemon/vpnsvc-daemon.manifest b/daemon/vpnsvc-daemon.manifest deleted file mode 100755 index 97e8c31..0000000 --- a/daemon/vpnsvc-daemon.manifest +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt deleted file mode 100755 index a01be18..0000000 --- a/framework/CMakeLists.txt +++ /dev/null @@ -1,64 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -SET(PACKAGE_NAME capi-vpnsvc) -SET(LIB_NAME ${PACKAGE_NAME}) -PROJECT(${LIB_NAME}) -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) -SET(EXEC_PREFIX "\${prefix}") -SET(LIBDIR ${LIB_INSTALL_DIR}) -SET(INCLUDEDIR "\${prefix}/include") -SET(VERSION 0.1) - -SET(requires "dlog dbus-1 glib-2.0 gio-2.0 gio-unix-2.0 capi-base-common capi-appfw-application capi-appfw-app-manager capi-system-info") -SET(pc_requires "capi-base-common") - -SET(SRCS - src/capi_vpn_service.c -) - -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include - ${CMAKE_SOURCE_DIR}/framework/include) - -INCLUDE(FindPkgConfig) -pkg_check_modules(${PACKAGE_NAME} REQUIRED ${requires}) -FOREACH(flag ${${PACKAGE_NAME}_CFLAGS}) - SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") -ENDFOREACH(flag) - -# Compiler flags -SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fPIC -Wall -fvisibility=hidden") -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") - -ADD_DEFINITIONS("-DPREFIX=\"${PREFIX}\"") -ADD_DEFINITIONS("-DFACTORYFS=\"$ENV{FACTORYFS}\"") -ADD_DEFINITIONS("-DDATAFS=\"$ENV{DATADIR}\"") -ADD_DEFINITIONS("-DSLP_DEBUG") - -SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath="${LIBDIR}) - -ADD_LIBRARY(${PACKAGE_NAME} SHARED ${SRCS}) -TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl) - -INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION ${LIBDIR}) -INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/vpn_service.h DESTINATION include) - -SET_TARGET_PROPERTIES(${PACKAGE_NAME} - PROPERTIES - VERSION ${FULLVER} - SOVERSION ${MAJORVER} - CLEAN_DIRECT_OUTPUT 1 -) - - -SET(PC_NAME ${PACKAGE_NAME}) -SET(PC_REQUIRED ${pc_requires}) -SET(PC_CFLAGS -I\${includedir}) -SET(PC_LDFLAGS -l${PACKAGE_NAME}) - -CONFIGURE_FILE( - ${PACKAGE_NAME}.pc.in - ${CMAKE_CURRENT_SOURCE_DIR}/${PACKAGE_NAME}.pc - @ONLY -) - -INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PACKAGE_NAME}.pc DESTINATION ${LIBDIR}/pkgconfig) diff --git a/framework/include/capi_vpn_service_private.h b/include/capi_vpn_service_private.h similarity index 84% rename from framework/include/capi_vpn_service_private.h rename to include/capi_vpn_service_private.h index 613fb7f..1157fef 100755 --- a/framework/include/capi_vpn_service_private.h +++ b/include/capi_vpn_service_private.h @@ -41,14 +41,11 @@ extern "C" { #endif /* __cplusplus */ -#define DBUS_DAEMON_SERVICE_NAME "org.freedesktop.DBus" -#define DBUS_DAEMON_OBJECT_NAME "/org/freedesktop/DBus" -#define DBUS_DAEMON_INTERFACE_NAME "org.freedesktop.DBus" -#define DBUS_DAEMON_START_SERVICE_METHOD_NAME "StartServiceByName" - -#define VPNSVC_DBUS_SERVICE_NAME "org.tizen.vpnsvc" -#define VPNSVC_DBUS_INTERFACE_NAME "org.tizen.vpnsvc" -#define VPNSVC_DBUS_INTERFACE_OBJ_NAME "/org/tizen/vpnsvc" +#define NETCONFIG_SERVICE_NAME "net.netconfig" +#define NETCONFIG_VPNSVC_PATH "/net/netconfig/vpnsvc" +#define NETCONFIG_VPNSVC_INTERFACE_NAME "net.netconfig.vpnsvc" +#define NETCONFIG_NETWORK_PATH "/net/netconfig/network" +#define NETCONFIG_NETWORK_INTERFACE "net.netconfig.network" #define _MAX_FILE_PATH_LEN 512 #define _USER_SETTING_DEFAULT_MTU 1500 diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 836f947..6d55a53 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,14 +1,11 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.21 +Version: 1.0.22 Release: 1 Group: System/Network License: Apache-2.0 URL: N/A Source0: %{name}-%{version}.tar.gz -Source1: vpnsvc-daemon.service -Source2: org.tizen.vpnsvc.service -Source3: dbus-vpnsvc-daemon.conf BuildRequires: cmake BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(dbus-glib-1) @@ -19,14 +16,11 @@ BuildRequires: pkgconfig(capi-appfw-application) BuildRequires: pkgconfig(capi-appfw-package-manager) BuildRequires: pkgconfig(capi-appfw-app-manager) BuildRequires: pkgconfig(capi-system-info) -BuildRequires: pkgconfig(cynara-client) -BuildRequires: pkgconfig(cynara-creds-gdbus) -BuildRequires: pkgconfig(cynara-session) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig %description -capi-vpn-service framework, service +capi-vpn-service %package -n capi-vpnsvc Summary: VPN service library in TIZEN C API @@ -50,17 +44,6 @@ capi-vpnsvc CAPI devel package #%description -n vpnsvc_test #vpnsvc test package -%package -n vpnsvc-daemon -Summary: Vpnsvc daemon -Group: Development/Libraries -Requires: systemd -Requires(preun): systemd -Requires(post): systemd -Requires(postun): systemd - -%description -n vpnsvc-daemon -vpnsvc daemon package - %prep %setup -q @@ -85,12 +68,6 @@ cp LICENSE %{buildroot}/%{_datadir}/license/capi-vpnsvc #cp LICENSE.APLv2 %{buildroot}/usr/share/license/fpasmtztransport %make_install -mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d -install -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/dbus-1/system.d/vpnsvc-daemon.conf -mkdir -p %{buildroot}%{_libdir}/systemd/system -install -m 0644 %{SOURCE1} %{buildroot}%{_libdir}/systemd/system/vpnsvc-daemon.service -mkdir -p %{buildroot}%{_datadir}/dbus-1/system-services -install -m 0644 %{SOURCE2} %{buildroot}%{_datadir}/dbus-1/system-services/org.tizen.vpnsvc.service %clean rm -rf %{buildroot} @@ -99,21 +76,9 @@ rm -rf %{buildroot} ln -s %{_libdir}/libcapi-vpnsvc.so.0 %{_libdir}/libcapi-vpnsvc.so %postun -if [ $1 == 0 ]; then - # unistall - systemctl daemon-reload -fi - -%files -n vpnsvc-daemon -%manifest daemon/vpnsvc-daemon.manifest -%attr(0755,root,root) %{_bindir}/vpnsvc-daemon -%defattr(-,root,root,-) -%{_sysconfdir}/dbus-1/system.d/*.conf -%{_libdir}/systemd/system/vpnsvc-daemon.service -%{_datadir}/dbus-1/system-services/org.tizen.vpnsvc.service %files -n capi-vpnsvc -%manifest framework/capi-vpnsvc.manifest +%manifest capi-vpnsvc.manifest %{_libdir}/libcapi-vpnsvc.so.* %{_datadir}/license/capi-vpnsvc %{_bindir}/vpnsvc_test diff --git a/packaging/dbus-vpnsvc-daemon.conf b/packaging/dbus-vpnsvc-daemon.conf deleted file mode 100755 index 9a1daea..0000000 --- a/packaging/dbus-vpnsvc-daemon.conf +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/packaging/org.tizen.vpnsvc.service b/packaging/org.tizen.vpnsvc.service deleted file mode 100755 index 079da3c..0000000 --- a/packaging/org.tizen.vpnsvc.service +++ /dev/null @@ -1,7 +0,0 @@ -[D-BUS Service] -Name=org.tizen.vpnsvc -Exec=/bin/false -User=root -Group=root -SystemdService=vpnsvc-daemon.service - diff --git a/packaging/vpnsvc-daemon.service b/packaging/vpnsvc-daemon.service deleted file mode 100755 index 171d855..0000000 --- a/packaging/vpnsvc-daemon.service +++ /dev/null @@ -1,14 +0,0 @@ -[Unit] -Description=Start vpn-service-daemon for vpn-service - -[Service] -User=root -Group=root -Type=dbus -BusName=org.tizen.vpnsvc -RemainAfterExit=yes -ExecStart=/usr/bin/vpnsvc-daemon -Restart=always -RestartSec=0 -CapabilityBoundingSet=~CAP_MAC_ADMIN -CapabilityBoundingSet=~CAP_MAC_OVERRIDE diff --git a/framework/src/capi_vpn_service.c b/src/capi_vpn_service.c similarity index 90% rename from framework/src/capi_vpn_service.c rename to src/capi_vpn_service.c index dbd5763..0cc758f 100755 --- a/framework/src/capi_vpn_service.c +++ b/src/capi_vpn_service.c @@ -263,39 +263,6 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) vpnsvc_tun_s *tmp_s = NULL; _vpnsvc_init_vpnsvc_tun_s(&tmp_s); - op = _vpnsvc_invoke_dbus_method(tmp_s->connection, - DBUS_DAEMON_SERVICE_NAME, - DBUS_DAEMON_OBJECT_NAME, - DBUS_DAEMON_INTERFACE_NAME, - DBUS_DAEMON_START_SERVICE_METHOD_NAME, - g_variant_new("(su)", VPNSVC_DBUS_SERVICE_NAME, 0), - &dbus_result); - - if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) - return VPNSVC_ERROR_PERMISSION_DENIED; - - if (op == NULL) { - _vpnsvc_deinit_vpnsvc_tun_s(tmp_s); //LCOV_EXCL_LINE - LOGD("Service [%s] Start Failed!", VPNSVC_DBUS_SERVICE_NAME); //LCOV_EXCL_LINE - return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE - } else { - unsigned int status = 0; - g_variant_get(op, "(u)", &status); - if (1 == status) { /* DBUS_START_REPLY_SUCCESS */ - LOGD("Service [%s] Started Successfully!", VPNSVC_DBUS_SERVICE_NAME); - } else if (2 == status) { /* DBUS_START_REPLY_ALREADY_RUNNING */ - LOGD("Service [%s] Already Running!", VPNSVC_DBUS_SERVICE_NAME); - } else { - LOGD("Service [%s] Not Started! Status[%d]", VPNSVC_DBUS_SERVICE_NAME, status); //LCOV_EXCL_LINE - g_variant_unref(op); //LCOV_EXCL_LINE - op = NULL; //LCOV_EXCL_LINE - _vpnsvc_deinit_vpnsvc_tun_s(tmp_s); //LCOV_EXCL_LINE - return VPNSVC_ERROR_IO_ERROR; //LCOV_EXCL_LINE - } - g_variant_unref(op); - op = NULL; - } - if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) { LOGE("tun device open fail\n"); //LCOV_EXCL_LINE close(iface_fd);//LCOV_EXCL_LINE @@ -305,10 +272,21 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) LOGD("client iface_fd : %d", iface_fd); + op = _vpnsvc_invoke_dbus_method(tmp_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + op = _vpnsvc_invoke_dbus_method_with_fd(tmp_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_init", g_variant_new("(su)", iface_name, strlen(iface_name)), iface_fd, @@ -369,9 +347,20 @@ EXPORT_API int vpnsvc_deinit(vpnsvc_h handle) if (tun_s->fd > 0) { op = _vpnsvc_invoke_dbus_method(tun_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_deinit", g_variant_new("(s)", tun_s->name), &dbus_result); @@ -425,11 +414,22 @@ EXPORT_API int vpnsvc_protect(vpnsvc_h handle, int socket_fd, const char* iface_ return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + /* call vpnsvc_protect */ op = _vpnsvc_invoke_dbus_method_with_fd(tun_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_protect", g_variant_new("(s)", iface_name), socket_fd, @@ -520,9 +520,20 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle, const char* local_ip, const char* remo LOGD("dns_suffix : %s", dns_suffix); op = _vpnsvc_invoke_dbus_method(tun_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_up", g_variant_new("(issvuvusu)", tun_s->index, local_ip, \ remote_ip, route_param, num_routes, dns_param, num_dns_servers, \ @@ -574,9 +585,20 @@ EXPORT_API int vpnsvc_down(vpnsvc_h handle) } op = _vpnsvc_invoke_dbus_method(tun_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_down", g_variant_new("(i)", tun_s->index), &dbus_result); @@ -715,9 +737,20 @@ EXPORT_API int vpnsvc_block_networks(vpnsvc_h handle, nets_param_orig = g_variant_builder_end(&nets_builder); op = _vpnsvc_invoke_dbus_method(tun_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_block_networks", g_variant_new("(vuvu)", nets_param_vpn, num_allow_routes_vpn, nets_param_orig, num_allow_routes_orig), @@ -765,9 +798,20 @@ EXPORT_API int vpnsvc_unblock_networks(vpnsvc_h handle) } op = _vpnsvc_invoke_dbus_method(tun_s->connection, - VPNSVC_DBUS_SERVICE_NAME, - VPNSVC_DBUS_INTERFACE_OBJ_NAME, - VPNSVC_DBUS_INTERFACE_NAME, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_unblock_networks", g_variant_new("()"), &dbus_result); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4a2fcca..daa6988 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,8 +17,7 @@ SET(TEST_SRCS vpn_service_test.c) -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include - ${CMAKE_SOURCE_DIR}/framework/include) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) INCLUDE(FindPkgConfig) pkg_check_modules(${PACKAGE_NAME} REQUIRED ${requires}) -- 2.7.4 From 9bd6013f577b109b5a879784e6a337ce8649ca3d Mon Sep 17 00:00:00 2001 From: hyunuktak Date: Wed, 22 Jun 2016 13:27:37 +0900 Subject: [PATCH 13/16] Fix svace for 102129 Variable 'iface_fd' is used by calling function 'close' after comparison with negative value. Change-Id: Ia5a853cccdba011177dc538c762ad15ff9060705 Signed-off-by: hyunuktak --- packaging/capi-vpn-service.spec | 2 +- src/capi_vpn_service.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packaging/capi-vpn-service.spec b/packaging/capi-vpn-service.spec index 6d55a53..303e576 100755 --- a/packaging/capi-vpn-service.spec +++ b/packaging/capi-vpn-service.spec @@ -1,6 +1,6 @@ Name: vpnsvc-pkg Summary: VPN service library in TIZEN C API -Version: 1.0.22 +Version: 1.0.23 Release: 1 Group: System/Network License: Apache-2.0 diff --git a/src/capi_vpn_service.c b/src/capi_vpn_service.c index 0cc758f..b76677f 100755 --- a/src/capi_vpn_service.c +++ b/src/capi_vpn_service.c @@ -265,7 +265,6 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) if ((iface_fd = open("/dev/net/tun", O_RDWR)) < 0) { LOGE("tun device open fail\n"); //LCOV_EXCL_LINE - close(iface_fd);//LCOV_EXCL_LINE _vpnsvc_deinit_vpnsvc_tun_s(tmp_s); //LCOV_EXCL_LINE return VPNSVC_ERROR_IO_ERROR; //LCOV_EXCL_LINE } -- 2.7.4 From ba9d70c1b59403768eeef50f6aa557a0c8e4fe58 Mon Sep 17 00:00:00 2001 From: "chleun.moon" Date: Fri, 23 Sep 2016 11:37:01 +0900 Subject: [PATCH 14/16] Remove reference warning Change-Id: I114b1f890b35f79b405b98a855a5f34d5e2bc786 Signed-off-by: cheoleun --- include/vpn_service.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/vpn_service.h b/include/vpn_service.h index feaf473..3c9e279 100755 --- a/include/vpn_service.h +++ b/include/vpn_service.h @@ -78,7 +78,7 @@ typedef void* vpnsvc_h; /** * @brief Initializes VPN interface. - * @detail You should call vpnsvc_get_iface_name() for checking the actual initialized VPN interface name. (In case of duplicated interface name) + * @details You should call vpnsvc_get_iface_name() for checking the actual initialized VPN interface name. (In case of duplicated interface name) * @since_tizen 3.0 * @privlevel public * @privilege %http://tizen.org/privilege/vpnservice \n -- 2.7.4 From 9db179cf2ab89d602fde116832dd45b0fe7defca Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Thu, 25 Aug 2016 09:58:16 +0900 Subject: [PATCH 15/16] Add APIs - Set local/remote IP address - Add/Remove dns server Change-Id: I654a1c5a3507932c4ffc1acb638ff6f9d4c8b713 Signed-off-by: Seonah Moon --- doc/vpn_doc.h | 4 + include/capi_vpn_service_private.h | 14 +- include/vpn_service.h | 149 +++++++++++++-- include/vpn_service_internal.h | 15 +- src/capi_vpn_service.c | 364 ++++++++++++++++++++++++++++++++----- test/vpn_service_test.c | 175 ++++++++++++++---- 6 files changed, 605 insertions(+), 116 deletions(-) diff --git a/doc/vpn_doc.h b/doc/vpn_doc.h index 4c8073b..1693579 100755 --- a/doc/vpn_doc.h +++ b/doc/vpn_doc.h @@ -32,11 +32,15 @@ * @section CAPI_NETWORK_VPN_SERVICE_MODULE_OVERVEW Overview * The VPN Service functions for managing VPN. * There can be only one VPN connection running at the same time. The existing interface is deactivated when a new one is created. + * * Using the VPN Service, you can implement features that allow the users of your application to: * - Initialize / Deinitialize the VPN device * - Routing management * - DNS management * - Firewall management + * + * To enable or disable VPN interface, you should launch VPN popup by using app-control. + * * @section CAPI_NETWORK_VPN_SERVICE_MODULE_FEATURE Related Features * This API is related with the following features:\n * - http://tizen.org/feature/network.vpn\n diff --git a/include/capi_vpn_service_private.h b/include/capi_vpn_service_private.h index 1157fef..4cb8542 100755 --- a/include/capi_vpn_service_private.h +++ b/include/capi_vpn_service_private.h @@ -41,16 +41,21 @@ extern "C" { #endif /* __cplusplus */ + #define NETCONFIG_SERVICE_NAME "net.netconfig" #define NETCONFIG_VPNSVC_PATH "/net/netconfig/vpnsvc" #define NETCONFIG_VPNSVC_INTERFACE_NAME "net.netconfig.vpnsvc" #define NETCONFIG_NETWORK_PATH "/net/netconfig/network" #define NETCONFIG_NETWORK_INTERFACE "net.netconfig.network" +#define VPNSVC_IP4_STRING_LEN 16 +#define VPNSVC_VPN_IFACE_NAME_LEN 16 +#define VPNSVC_SESSION_STRING_LEN 32 + #define _MAX_FILE_PATH_LEN 512 #define _USER_SETTING_DEFAULT_MTU 1500 #define _USER_SETTING_DEFAULT_SESSION "VPN_Session" - +#define _USER_SETTING_DEFAULT_ROUTE_PREFIX 32 #define VPN_SERVICE_FEATURE "http://tizen.org/feature/network.vpn" #define CHECK_FEATURE_SUPPORTED(feature_name) \ @@ -67,9 +72,12 @@ typedef struct _vpnsvc_tun_s { GDBusConnection *connection; /**< D-Bus Connection */ int fd; /**< tun socket fd */ int index; /**< tun index (if.iface_index) */ - char name[VPNSVC_VPN_IFACE_NAME_LEN]; /**< tun name (if.iface_name) */ - char session[VPNSVC_SESSION_STRING_LEN];/**< session name (user setting) */ + char name[VPNSVC_VPN_IFACE_NAME_LEN]; /**< tun name (if.iface_name) */ + char session[VPNSVC_SESSION_STRING_LEN]; /**< session name (user setting) */ + char local_ip[VPNSVC_IP4_STRING_LEN]; /**< local IP address */ + char remote_ip[VPNSVC_IP4_STRING_LEN]; /**< remote IP address */ unsigned int mtu; /**< mtu (user setting) */ + char *dns_suffix; /**< dns suffix (optional) */ } vpnsvc_tun_s; int _vpnsvc_check_feature_supported(const char *feature_name); diff --git a/include/vpn_service.h b/include/vpn_service.h index 3c9e279..f111d2c 100755 --- a/include/vpn_service.h +++ b/include/vpn_service.h @@ -33,24 +33,6 @@ extern "C" { */ /** - * @brief IPv4 address string length (includes end null character). - * @since_tizen 3.0 - */ -#define VPNSVC_IP4_STRING_LEN 16 - -/** - * @brief VPN interface name length. - * @since_tizen 3.0 - */ -#define VPNSVC_VPN_IFACE_NAME_LEN 16 - -/** - * @brief Session name string length (includes end null character). - * @since_tizen 3.0 - */ -#define VPNSVC_SESSION_STRING_LEN 32 - -/** * @brief Enumeration for VPN service error types. * @details Indicate formats of error type field */ @@ -221,6 +203,24 @@ int vpnsvc_block_networks(vpnsvc_h handle, int vpnsvc_unblock_networks(vpnsvc_h handle); /** + * @brief Updates settings (Local IP / Remote IP / MTU). + * @since_tizen 3.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/vpnservice \n + * %http://tizen.org/privilege/internet + * @param[in] handle The VPN interface handle + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IPC_FAILED Cannot connect to service daemon + * @retval #VPNSVC_ERROR_PERMISSION_DENIED Permission Denied + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_update_settings(vpnsvc_h handle); + +/** * @brief Gets the fd of the VPN interface. * @since_tizen 3.0 * @param[in] handle The VPN interface handle @@ -264,11 +264,13 @@ int vpnsvc_get_iface_name(vpnsvc_h handle, char** iface_name); /** * @brief Sets the MTU of the VPN interface. * @since_tizen 3.0 + * @remarks vpnsvc_update_settings() should be invoked after setting MTU. * @param[in] handle The VPN interface handle * @param[in] mtu The MTU (Maximum Transmission Unit) value to be set for VPN interface. Default MTU size is 1500. * @return 0 on success. Otherwise, a negative error value. * @retval #VPNSVC_ERROR_NONE Success * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported * @pre Before calling this function, VPN interface should be initialized already. * @see vpnsvc_init() @@ -320,6 +322,117 @@ int vpnsvc_set_session(vpnsvc_h handle, const char* session); int vpnsvc_get_session(vpnsvc_h handle, char** session); /** + * @brief Sets the VPN client IP address. + * @since_tizen 3.0 + * @remarks The local address should be set before enabling VPN interface. \n + * vpnsvc_update_settings() should be invoked after setting local IP address. + * @param[in] handle The VPN interface handle + * @param[in] local_ip The local (VPN client) IP address (IPv4 only) + + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_set_local_ip_address(vpnsvc_h handle, const char *local_ip); + +/** + * @brief Sets the VPN server IP address. + * @since_tizen 3.0 + * @remarks The remote address should be set before enabling VPN interface. \n + * vpnsvc_update_settings() should be invoked after setting remote IP address. + * @param[in] handle The VPN interface handle + * @param[in] remote_ip The remote (VPN server) IP address (IPv4 only) + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_set_remote_ip_address(vpnsvc_h handle, const char *remote_ip); + +/** + * @brief Adds the route address. + * @since_tizen 3.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/vpnservice \n + * %http://tizen.org/privilege/internet + * @remarks The route address should be set after enabling VPN interface. + * @param[in] handle The VPN interface handle + * @param[in] route_address Destination address of the routes (IPv4 only) + * @param[in] prefix The prefix of routes, netmask length (also called a prefix, e.g. 8, 16, 24, 32) + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_add_route(vpnsvc_h handle, const char *route_address, int prefix); + +/** + * @brief Removes the route address. + * @since_tizen 3.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/vpnservice \n + * %http://tizen.org/privilege/internet + * @param[in] handle The VPN interface handle + * @param[in] route_address Destination address of the routes (IPv4 only) + * @param[in] prefix The prefix of routes, netmask length (also called a prefix, e.g. 8, 16, 24, 32) + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_remove_route(vpnsvc_h handle, const char *route_address, int prefix); + +/** + * @brief Adds the DNS server name. + * @since_tizen 3.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/vpnservice \n + * %http://tizen.org/privilege/internet + * @remarks The dns address should be set after enabling VPN interface. + * @param[in] handle The VPN interface handle + * @param[in] dns_server The DNS server address (IPv4 only) + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_add_dns_server(vpnsvc_h handle, const char *dns_server); + +/** + * @brief Removes the DNS server name. + * @since_tizen 3.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/vpnservice \n + * %http://tizen.org/privilege/internet + * @param[in] handle The VPN interface handle + * @param[in] dns_server The DNS server address (IPv4 only) + * @return 0 on success. Otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IO_ERROR I/O Error (e.g. socket I/O error) + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @see vpnsvc_init() + */ +int vpnsvc_remove_dns_server(vpnsvc_h handle, const char *dns_server); + +/** * @} */ diff --git a/include/vpn_service_internal.h b/include/vpn_service_internal.h index bf69850..24fc6bc 100755 --- a/include/vpn_service_internal.h +++ b/include/vpn_service_internal.h @@ -37,16 +37,7 @@ extern "C" { * @brief Sets-up VPN interface and brings it up. Installs specified routes/DNS servers/DNS suffix. * @since_tizen 3.0 * @privlevel platform - * @privilege %http://tizen.org/privilege/vpnservice.admin * @param[in] handle The VPN interface handle - * @param[in] local_ip The local(vpn client) IP address - * @param[in] remote_ip The remote(vpn server) IP address - * @param[in] routes_dest_addr Destination address of the routes - * @param[in] routes_prefix The prefix of routes, netmask length (also called a prefix, e.g. 8, 16, 24, 32) - * @param[in] num_routes The number of routes, Unlimitation - * @param[in] dns_servers The list of DNS server names - Optional - * @param[in] num_dns_servers The number of DNS server names - Optionl, Unlimitation - * @param[in] dns_suffix The DNS suffix - Optional (e.g. tizen.org) * @return 0 on success. otherwise, a negative error value. * @retval #VPNSVC_ERROR_NONE Success * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter @@ -58,17 +49,13 @@ extern "C" { * @see vpnsvc_init() * @see vpnsvc_down() */ -int vpnsvc_up(vpnsvc_h handle, const char* local_ip, const char* remote_ip, - char *routes_dest_addr[], int routes_prefix[], size_t num_routes, - const char** dns_servers, size_t num_dns_servers, - const char* dns_suffix); +int vpnsvc_up(vpnsvc_h handle); /** * @internal * @brief Brings the VPN interface down and restores original DNS servers/domains. * @since_tizen 3.0 * @privlevel platform - * @privilege %http://tizen.org/privilege/vpnservice.admin * @param[in] handle The VPN interface handle * @return 0 on success. otherwise, a negative error value. * @retval #VPNSVC_ERROR_NONE Success diff --git a/src/capi_vpn_service.c b/src/capi_vpn_service.c index b76677f..96e76ae 100755 --- a/src/capi_vpn_service.c +++ b/src/capi_vpn_service.c @@ -104,6 +104,7 @@ static void _vpnsvc_init_vpnsvc_tun_s(vpnsvc_tun_s **s) (*s)->mtu = _USER_SETTING_DEFAULT_MTU; strncpy((*s)->session, _USER_SETTING_DEFAULT_SESSION, VPNSVC_SESSION_STRING_LEN); (*s)->session[VPNSVC_SESSION_STRING_LEN-1] = '\0'; + (*s)->dns_suffix = NULL; } static void _vpnsvc_deinit_vpnsvc_tun_s(vpnsvc_tun_s *s) @@ -118,6 +119,11 @@ static void _vpnsvc_deinit_vpnsvc_tun_s(vpnsvc_tun_s *s) memset(s->name, 0, VPNSVC_VPN_IFACE_NAME_LEN); memset(s->session, 0, VPNSVC_SESSION_STRING_LEN); + if (s->dns_suffix) { + g_free(s->dns_suffix); + s->dns_suffix = NULL; + } + if (s) g_free(s); } @@ -451,19 +457,12 @@ EXPORT_API int vpnsvc_protect(vpnsvc_h handle, int socket_fd, const char* iface_ return result; } -EXPORT_API int vpnsvc_up(vpnsvc_h handle, const char* local_ip, const char* remote_ip, - char* routes_dest_add[], int routes_prefix[], size_t num_routes, - const char** dns_servers, size_t num_dns_servers, - const char* dns_suffix) +EXPORT_API int vpnsvc_up(vpnsvc_h handle) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); int result = VPNSVC_ERROR_NONE; int dbus_result; - GVariantBuilder route_builder, dns_builder; - size_t i = 0; - GVariant *route_param = NULL; - GVariant *dns_param = NULL; vpnsvc_tun_s *tun_s = NULL; /* parameter check */ @@ -483,40 +482,14 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle, const char* local_ip, const char* remo return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } - if (local_ip == NULL || remote_ip == NULL) { + if (tun_s->local_ip == NULL || tun_s->remote_ip == NULL) { LOGE("local and remote ip are invalid"); //LCOV_EXCL_LINE return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } LOGD("iface_index %d", tun_s->index); - LOGD("local_ip : %s, remote_ip : %s", local_ip, remote_ip); - - /* make a route parameter */ - g_variant_builder_init(&route_builder, G_VARIANT_TYPE("a{si}")); - for (i = 0 ; i < num_routes ; i++) { - if (strlen(routes_dest_add[i]) <= 0) { - LOGE("invalid dest[%d]", i); //LCOV_EXCL_LINE - return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - } - g_variant_builder_add(&route_builder, "{si}", routes_dest_add[i], routes_prefix[i]); - LOGD("dest[%d] : %s", i, routes_dest_add[i]); - LOGD("prefix[i] : %d", i, routes_prefix[i]); - } - route_param = g_variant_builder_end(&route_builder); - - /* make a dns parameter */ - g_variant_builder_init(&dns_builder, G_VARIANT_TYPE("as")); - for (i = 0 ; i < num_dns_servers ; i++) { - if (strlen(dns_servers[i]) <= 0) { - LOGE("invalid dns_servers[%d]", i); //LCOV_EXCL_LINE - return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - } - LOGD("dns_servers[%d] : %s", i, dns_servers[i]); - g_variant_builder_add(&dns_builder, "s", dns_servers[i]); - } - dns_param = g_variant_builder_end(&dns_builder); - - LOGD("dns_suffix : %s", dns_suffix); + LOGD("local_ip : %s, remote_ip : %s", tun_s->local_ip, tun_s->remote_ip); + LOGD("dns_suffix : %s", tun_s->dns_suffix); op = _vpnsvc_invoke_dbus_method(tun_s->connection, NETCONFIG_SERVICE_NAME, @@ -534,9 +507,7 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle, const char* local_ip, const char* remo NETCONFIG_VPNSVC_PATH, NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_up", - g_variant_new("(issvuvusu)", tun_s->index, local_ip, \ - remote_ip, route_param, num_routes, dns_param, num_dns_servers, \ - dns_suffix, tun_s->mtu), + g_variant_new("(i)", tun_s->index), &dbus_result); if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) @@ -831,6 +802,95 @@ EXPORT_API int vpnsvc_unblock_networks(vpnsvc_h handle) return result; } +EXPORT_API int vpnsvc_update_settings(vpnsvc_h handle) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + int dbus_result; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + tun_s = (vpnsvc_tun_s*)handle; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_NETWORK_PATH, + NETCONFIG_NETWORK_INTERFACE, + "CheckInternetPrivilege", + NULL, + &dbus_result); + + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_set_local_ip_address", + g_variant_new("(is)", tun_s->index, tun_s->local_ip), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_set_local_ip_address() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_set_local_ip_address() succeed"); + } + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_set_remote_ip_address", + g_variant_new("(is)", tun_s->index, tun_s->remote_ip), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_set_remote_ip_address() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_set_remote_ip-address() succeed"); + } + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_set_mtu", + g_variant_new("(iu)", tun_s->index, tun_s->mtu), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_set_mtu() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_set_mtu() succeed"); + } + + return result; +} + EXPORT_API int vpnsvc_get_iface_fd(vpnsvc_h handle, int* iface_fd) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); @@ -912,6 +972,8 @@ EXPORT_API int vpnsvc_set_mtu(vpnsvc_h handle, int mtu) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + int result = VPNSVC_ERROR_NONE; + int dbus_result; vpnsvc_tun_s *tun_s = NULL; /* parameter check */ @@ -927,7 +989,8 @@ EXPORT_API int vpnsvc_set_mtu(vpnsvc_h handle, int mtu) } tun_s->mtu = mtu; - return VPNSVC_ERROR_NONE; + + return result; } EXPORT_API int vpnsvc_set_blocking(vpnsvc_h handle, bool blocking) @@ -1016,3 +1079,222 @@ EXPORT_API int vpnsvc_get_session(vpnsvc_h handle, char** session) return VPNSVC_ERROR_NONE; } + +EXPORT_API int vpnsvc_set_local_ip_address(vpnsvc_h handle, const char *local_ip) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + tun_s = (vpnsvc_tun_s*)handle; + + if (local_ip == NULL) { + LOGE("Local IP address is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + strncpy(tun_s->local_ip, local_ip, VPNSVC_IP4_STRING_LEN); + + return VPNSVC_ERROR_NONE; +} + +EXPORT_API int vpnsvc_set_remote_ip_address(vpnsvc_h handle, const char *remote_ip) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + tun_s = (vpnsvc_tun_s*)handle; + + if (remote_ip == NULL) { + LOGE("Remote IP address is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + strncpy(tun_s->remote_ip, remote_ip, VPNSVC_IP4_STRING_LEN); + + return VPNSVC_ERROR_NONE; +} + +EXPORT_API int vpnsvc_add_route(vpnsvc_h handle, const char *route_address, int prefix) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + int dbus_result; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + tun_s = (vpnsvc_tun_s*)handle; + + if (route_address == NULL) { + LOGE("remove_address is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_add_route", + g_variant_new("(ssi)", tun_s->name, route_address, prefix), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_add_route() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_add_route() succeed"); + } + + return result; +} + +EXPORT_API int vpnsvc_remove_route(vpnsvc_h handle, const char *route_address, int prefix) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + int dbus_result; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + tun_s = (vpnsvc_tun_s*)handle; + + if (route_address == NULL) { + LOGE("route_address is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_remove_route", + g_variant_new("(ssi)", tun_s->name, route_address, prefix), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_remove_route() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_remove_route() succeed"); + } + + return result; +} + +EXPORT_API int vpnsvc_add_dns_server(vpnsvc_h handle, const char *dns_server) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + int dbus_result; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + tun_s = (vpnsvc_tun_s*)handle; + + if (dns_server == NULL) { + LOGE("dns_server is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_add_dns_server", + g_variant_new("(ss)", tun_s->name, dns_server), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_add_dns_server() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_add_dns_server() succeed"); + } + + return result; +} + +EXPORT_API int vpnsvc_remove_dns_server(vpnsvc_h handle, const char *dns_server) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + int dbus_result; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); + return VPNSVC_ERROR_INVALID_PARAMETER; + } + tun_s = (vpnsvc_tun_s*)handle; + + if (dns_server == NULL) { + LOGE("dns_server is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + op = _vpnsvc_invoke_dbus_method(tun_s->connection, + NETCONFIG_SERVICE_NAME, + NETCONFIG_VPNSVC_PATH, + NETCONFIG_VPNSVC_INTERFACE_NAME, + "vpn_remove_dns_server", + g_variant_new("(ss)", tun_s->name, dns_server), + &dbus_result); + if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) + return VPNSVC_ERROR_PERMISSION_DENIED; + + if (op == NULL) { + return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE + } else { + g_variant_get(op, "(i)", &result); + if (result != VPNSVC_ERROR_NONE) + LOGE("vpn_remove_dns_server() failed"); //LCOV_EXCL_LINE + else + LOGD("vpn_remove_dns_server() succeed"); + } + + return result; +} diff --git a/test/vpn_service_test.c b/test/vpn_service_test.c index b293817..010791d 100755 --- a/test/vpn_service_test.c +++ b/test/vpn_service_test.c @@ -19,9 +19,13 @@ #include #include +#include #include +#include +#include -#include "capi_vpn_service_private.h" +#include "vpn_service.h" +#include "vpn_service_internal.h" #ifdef LOG_TAG #undef LOG_TAG @@ -36,6 +40,10 @@ perror("fgets() failed!!!");\ } while (0); +#define VPNSVC_IP4_STRING_LEN 16 +#define VPNSVC_VPN_IFACE_NAME_LEN 16 +#define VPNSVC_SESSION_STRING_LEN 32 + vpnsvc_h handle = NULL; static const char *test_print_error(vpnsvc_error_e error) @@ -150,49 +158,13 @@ int test_vpnsvc_protect() int test_vpnsvc_up() { int rv; - char local[VPNSVC_IP4_STRING_LEN] = {'\0',}; - char remote[VPNSVC_IP4_STRING_LEN] = {'\0',}; - char *routes[2]; - int prefix[2]; - int nr_routes = 2; - const char *dns_server[2]; - int nr_dns = 2; - char dns_suffix[100] = "tizen.org"; if (!handle) { printf("invalid handle\n"); return -1; } - strncpy(local, "192.168.0.82", VPNSVC_IP4_STRING_LEN); - strncpy(remote, "192.168.0.1", VPNSVC_IP4_STRING_LEN); - - routes[0] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); - routes[1] = malloc(sizeof(char) * VPNSVC_IP4_STRING_LEN); - - memset(routes[0], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); - memset(routes[1], 0, sizeof(char) * VPNSVC_IP4_STRING_LEN); - - strncpy(routes[0], "192.168.0.10", VPNSVC_IP4_STRING_LEN); - prefix[0] = 32; - - strncpy(routes[1], "192.168.0.11", VPNSVC_IP4_STRING_LEN); - prefix[1] = 32; - - char *dns1 = "1.1.1.1"; - char *dns2 = "2.2.2.2"; - - dns_server[0] = dns1; - dns_server[1] = dns2; - - rv = vpnsvc_up(handle, local, remote, routes, prefix, nr_routes, dns_server, nr_dns, dns_suffix); - - free(routes[0]); - free(routes[1]); - - routes[0] = NULL; - routes[1] = NULL; - + rv = vpnsvc_up(handle); if (rv != VPNSVC_ERROR_NONE) printf("vpnsvc up fail [%s]\n", test_print_error(rv)); else @@ -306,8 +278,11 @@ int test_vpnsvc_unblock_networks() int test_vpnsvc_set_mtu() { int rv; + int mtu = 0; - rv = vpnsvc_set_mtu(handle, 9000); + printf("Input MTU: "); + rv = scanf("%d", &mtu); + rv = vpnsvc_set_mtu(handle, mtu); if (rv != VPNSVC_ERROR_NONE) { printf("vpnsvc set mtu fail [%s]\n", test_print_error(rv)); return -1; @@ -355,6 +330,114 @@ int test_vpnsvc_set_session() } } +int test_vpnsvc_set_local_ip_address() +{ + int rv; + char local_ip[VPNSVC_IP4_STRING_LEN]; + + printf("Input local IP address: "); + rv = scanf("%s", local_ip); + + rv = vpnsvc_set_local_ip_address(handle, local_ip); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc set local IP address fail [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + +int test_vpnsvc_set_remote_ip_address() +{ + int rv; + char remote_ip[VPNSVC_IP4_STRING_LEN]; + + printf("Input remote IP address: "); + rv = scanf("%s", remote_ip); + + rv = vpnsvc_set_remote_ip_address(handle, remote_ip); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc set remote IP address fail [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + +int test_vpnsvc_add_route() +{ + int rv; + int prefix; + char addr[VPNSVC_IP4_STRING_LEN]; + + printf("Input route address: "); + rv = scanf("%s", addr); + printf("Input prefix: "); + rv = scanf("%d", &prefix); + + rv = vpnsvc_add_route(handle, addr, prefix); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc add route address fail [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + +int test_vpnsvc_remove_route() +{ + int rv; + int prefix; + char addr[VPNSVC_IP4_STRING_LEN]; + + printf("Input route address: "); + rv = scanf("%s", addr); + printf("Input prefix: "); + rv = scanf("%d", &prefix); + + rv = vpnsvc_remove_route(handle, addr, prefix); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc remove route address fail [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + +int test_vpnsvc_add_dns_server() +{ + int rv; + char addr[VPNSVC_IP4_STRING_LEN]; + + printf("Input dns server: "); + rv = scanf("%s", addr); + + rv = vpnsvc_add_dns_server(handle, addr); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc add dns server fail [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + +int test_vpnsvc_remove_dns_server() +{ + int rv; + char addr[VPNSVC_IP4_STRING_LEN]; + + printf("Input dns server address: "); + rv = scanf("%s", addr); + + rv = vpnsvc_remove_dns_server(handle, addr); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc remove dns server fail [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + int test_exit() { exit(0); @@ -374,6 +457,12 @@ int (*test_function_table[])(void) = { test_vpnsvc_set_mtu, test_vpnsvc_set_blocking, test_vpnsvc_set_session, + test_vpnsvc_set_local_ip_address, + test_vpnsvc_set_remote_ip_address, + test_vpnsvc_add_route, + test_vpnsvc_remove_route, + test_vpnsvc_add_dns_server, + test_vpnsvc_remove_dns_server, test_exit, }; @@ -387,7 +476,7 @@ int main() printf("1 : vpnsvc_init\n"); printf("2 : vpnsvc_deinit\n"); printf("3 : vpnsvc_protect\n"); - printf("4 : vpnsvc_up\n"); + printf("4 : vpnsvc_up(1->13->14)\n"); printf("5 : vpnsvc_down\n"); printf("6 : vpnsvc_read\n"); printf("7 : vpnsvc_write\n"); @@ -396,6 +485,12 @@ int main() printf("10 : vpnsvc_set_mtu\n"); printf("11 : vpnsvc_set_blocking\n"); printf("12 : vpnsvc_set_session\n"); + printf("13 : vpnsvc_set_local_ip_address\n"); + printf("14 : vpnsvc_set_remote_ip_address\n"); + printf("15 : vpnsvc_add_route\n"); + printf("16 : vpnsvc_remove_route\n"); + printf("17 : vpnsvc_add_dns_server\n"); + printf("18 : vpnsvc_remove_dns_server\n"); printf("q : quit\n"); TEST_CONSOLE_INPUT(input, 3); -- 2.7.4 From ab2291c943985add8a3abb68dc587896b18c4dc9 Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Mon, 19 Sep 2016 19:32:34 +0900 Subject: [PATCH 16/16] Add internal APIs for appcontrol Change-Id: Id49f8b977e2cd3d3c449ce193077ce3c639f66e4 Signed-off-by: Seonah Moon --- CMakeLists.txt | 1 + include/vpn_service_internal.h | 52 +++++++++++++- src/capi_vpn_service.c | 149 +++++++++++++++++++++-------------------- test/vpn_service_test.c | 100 +++++++++++++++++++++++---- 4 files changed, 216 insertions(+), 86 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6dc3c6e..3de910a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ TARGET_LINK_LIBRARIES(${PACKAGE_NAME} ${${PACKAGE_NAME}_LDFLAGS} -lrt -ldl) INSTALL(TARGETS ${PACKAGE_NAME} DESTINATION ${LIBDIR}) INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/vpn_service.h DESTINATION include) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/vpn_service_internal.h DESTINATION include) SET_TARGET_PROPERTIES(${PACKAGE_NAME} PROPERTIES diff --git a/include/vpn_service_internal.h b/include/vpn_service_internal.h index 24fc6bc..466d348 100755 --- a/include/vpn_service_internal.h +++ b/include/vpn_service_internal.h @@ -34,7 +34,57 @@ extern "C" { /** * @internal - * @brief Sets-up VPN interface and brings it up. Installs specified routes/DNS servers/DNS suffix. + * @brief Creates VPN service handle. + * @since_tizen 3.0 + * @privlevel platform + * @param[in] handle The VPN interface handle + * @return 0 on success. otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IPC_FAILED Cannot connect to service daemon + * @retval #VPNSVC_ERROR_PERMISSION_DENIED Permission Denied + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @post If you want to set interface down, please call vpnsvc_down(). + */ +int vpnsvc_create(vpnsvc_h *handle); + +/** + * @internal + * @brief Destroys VPN service handle. + * @since_tizen 3.0 + * @privlevel platform + * @param[in] handle The VPN interface handle + * @return 0 on success. otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IPC_FAILED Cannot connect to service daemon + * @retval #VPNSVC_ERROR_PERMISSION_DENIED Permission Denied + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be created already. + */ +int vpnsvc_destroy(vpnsvc_h handle); + +/** + * @internal + * @brief Sets VPN interface name. + * @since_tizen 3.0 + * @privlevel platform + * @param[in] handle The VPN interface handle + * @param[in] iface_name The VPN interface name + * @return 0 on success. otherwise, a negative error value. + * @retval #VPNSVC_ERROR_NONE Success + * @retval #VPNSVC_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VPNSVC_ERROR_IPC_FAILED Cannot connect to service daemon + * @retval #VPNSVC_ERROR_PERMISSION_DENIED Permission Denied + * @retval #VPNSVC_ERROR_NOT_SUPPORTED Not Supported + * @pre The VPN interface should be initialized already. + * @post If you want to set interface down, please call vpnsvc_down(). + */ +int vpnsvc_set_iface_name(vpnsvc_h handle, const char *iface_name); + +/** + * @internal + * @brief Sets-up VPN interface and brings it up. * @since_tizen 3.0 * @privlevel platform * @param[in] handle The VPN interface handle diff --git a/src/capi_vpn_service.c b/src/capi_vpn_service.c index 96e76ae..bd57ef1 100755 --- a/src/capi_vpn_service.c +++ b/src/capi_vpn_service.c @@ -101,6 +101,7 @@ static void _vpnsvc_init_vpnsvc_tun_s(vpnsvc_tun_s **s) } /* Setting Default User Settings */ + (*s)->index = 0; (*s)->mtu = _USER_SETTING_DEFAULT_MTU; strncpy((*s)->session, _USER_SETTING_DEFAULT_SESSION, VPNSVC_SESSION_STRING_LEN); (*s)->session[VPNSVC_SESSION_STRING_LEN-1] = '\0'; @@ -246,6 +247,47 @@ GVariant *_vpnsvc_invoke_dbus_method_with_fd(GDBusConnection *connection, return reply; } +EXPORT_API int vpnsvc_create(vpnsvc_h *handle) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + LOGD("enter vpnsvc_create"); + + /* parameter check */ + if (_vpn_check_handle_validity(*handle)) { + LOGE("Invalid parameter"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + vpnsvc_tun_s *tmp_s = NULL; + _vpnsvc_init_vpnsvc_tun_s(&tmp_s); + *handle = tmp_s; + LOGD("handle : %p", (*handle)); + + return VPNSVC_ERROR_NONE; +} + +EXPORT_API int vpnsvc_destroy(vpnsvc_h handle) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + int result = VPNSVC_ERROR_NONE; + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + tun_s = (vpnsvc_tun_s*)handle; + + LOGD("enter vpnsvc_destroy"); + + _vpnsvc_deinit_vpnsvc_tun_s(tun_s); + + return result; +} + EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); @@ -255,7 +297,6 @@ EXPORT_API int vpnsvc_init(const char* iface_name, vpnsvc_h *handle) int iface_fd = 0; LOGD("enter vpnsvc_init, iface_name : %s", iface_name); - LOGD("handle : %p\n", handle); /* parameter check */ if (iface_name == NULL || strlen(iface_name) <= 0) { @@ -474,22 +515,15 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle) LOGD("enter vpnsvc_up"); - if (tun_s->index <= 0) { - LOGE("invalid handle"); //LCOV_EXCL_LINE - return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - } else if (tun_s->connection == NULL) { + if (tun_s->connection == NULL) { LOGE("Connection Object is NULL"); //LCOV_EXCL_LINE return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - } - - if (tun_s->local_ip == NULL || tun_s->remote_ip == NULL) { - LOGE("local and remote ip are invalid"); //LCOV_EXCL_LINE + } else if (tun_s->name[0] == 0) { + LOGE("invalid handle"); //LCOV_EXCL_LINE return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } - LOGD("iface_index %d", tun_s->index); - LOGD("local_ip : %s, remote_ip : %s", tun_s->local_ip, tun_s->remote_ip); - LOGD("dns_suffix : %s", tun_s->dns_suffix); + LOGD("iface_index %d, iface_name %s", tun_s->index, tun_s->name); op = _vpnsvc_invoke_dbus_method(tun_s->connection, NETCONFIG_SERVICE_NAME, @@ -507,7 +541,7 @@ EXPORT_API int vpnsvc_up(vpnsvc_h handle) NETCONFIG_VPNSVC_PATH, NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_up", - g_variant_new("(i)", tun_s->index), + g_variant_new("(s)", tun_s->name), &dbus_result); if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) @@ -546,9 +580,6 @@ EXPORT_API int vpnsvc_down(vpnsvc_h handle) if (tun_s == NULL) { LOGE("handle is a NULL"); //LCOV_EXCL_LINE return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE - } else if (tun_s->index <= 0) { - LOGE("invalid handle"); //LCOV_EXCL_LINE - return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE } else if (tun_s->connection == NULL) { LOGE("Connection Object is NULL"); //LCOV_EXCL_LINE return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE @@ -570,7 +601,7 @@ EXPORT_API int vpnsvc_down(vpnsvc_h handle) NETCONFIG_VPNSVC_PATH, NETCONFIG_VPNSVC_INTERFACE_NAME, "vpn_down", - g_variant_new("(i)", tun_s->index), + g_variant_new("(s)", tun_s->name), &dbus_result); if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) @@ -818,62 +849,11 @@ EXPORT_API int vpnsvc_update_settings(vpnsvc_h handle) tun_s = (vpnsvc_tun_s*)handle; op = _vpnsvc_invoke_dbus_method(tun_s->connection, - NETCONFIG_SERVICE_NAME, - NETCONFIG_NETWORK_PATH, - NETCONFIG_NETWORK_INTERFACE, - "CheckInternetPrivilege", - NULL, - &dbus_result); - - if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) - return VPNSVC_ERROR_PERMISSION_DENIED; - - op = _vpnsvc_invoke_dbus_method(tun_s->connection, - NETCONFIG_SERVICE_NAME, - NETCONFIG_VPNSVC_PATH, - NETCONFIG_VPNSVC_INTERFACE_NAME, - "vpn_set_local_ip_address", - g_variant_new("(is)", tun_s->index, tun_s->local_ip), - &dbus_result); - if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) - return VPNSVC_ERROR_PERMISSION_DENIED; - - if (op == NULL) { - return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE - } else { - g_variant_get(op, "(i)", &result); - if (result != VPNSVC_ERROR_NONE) - LOGE("vpn_set_local_ip_address() failed"); //LCOV_EXCL_LINE - else - LOGD("vpn_set_local_ip_address() succeed"); - } - - op = _vpnsvc_invoke_dbus_method(tun_s->connection, - NETCONFIG_SERVICE_NAME, - NETCONFIG_VPNSVC_PATH, - NETCONFIG_VPNSVC_INTERFACE_NAME, - "vpn_set_remote_ip_address", - g_variant_new("(is)", tun_s->index, tun_s->remote_ip), - &dbus_result); - if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) - return VPNSVC_ERROR_PERMISSION_DENIED; - - if (op == NULL) { - return VPNSVC_ERROR_IPC_FAILED; //LCOV_EXCL_LINE - } else { - g_variant_get(op, "(i)", &result); - if (result != VPNSVC_ERROR_NONE) - LOGE("vpn_set_remote_ip_address() failed"); //LCOV_EXCL_LINE - else - LOGD("vpn_set_remote_ip-address() succeed"); - } - - op = _vpnsvc_invoke_dbus_method(tun_s->connection, NETCONFIG_SERVICE_NAME, NETCONFIG_VPNSVC_PATH, NETCONFIG_VPNSVC_INTERFACE_NAME, - "vpn_set_mtu", - g_variant_new("(iu)", tun_s->index, tun_s->mtu), + "vpn_update_settings", + g_variant_new("(issu)", tun_s->index, tun_s->local_ip, tun_s->remote_ip, tun_s->mtu), &dbus_result); if (dbus_result == VPNSVC_ERROR_PERMISSION_DENIED) return VPNSVC_ERROR_PERMISSION_DENIED; @@ -883,9 +863,9 @@ EXPORT_API int vpnsvc_update_settings(vpnsvc_h handle) } else { g_variant_get(op, "(i)", &result); if (result != VPNSVC_ERROR_NONE) - LOGE("vpn_set_mtu() failed"); //LCOV_EXCL_LINE + LOGE("vpn_update_settings() failed"); //LCOV_EXCL_LINE else - LOGD("vpn_set_mtu() succeed"); + LOGD("vpn_update_settings() succeed"); } return result; @@ -968,12 +948,35 @@ EXPORT_API int vpnsvc_get_iface_name(vpnsvc_h handle, char** iface_name) return VPNSVC_ERROR_NONE; } +EXPORT_API int vpnsvc_set_iface_name(vpnsvc_h handle, const char *iface_name) +{ + CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); + + vpnsvc_tun_s *tun_s = NULL; + + /* parameter check */ + if (handle == NULL) { + LOGE("handle is a NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + tun_s = (vpnsvc_tun_s*)handle; + + if (iface_name == NULL) { + LOGE("Remote IP address is NULL"); //LCOV_EXCL_LINE + return VPNSVC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE + } + + strncpy(tun_s->name, iface_name, VPNSVC_VPN_IFACE_NAME_LEN); + + return VPNSVC_ERROR_NONE; +} + + EXPORT_API int vpnsvc_set_mtu(vpnsvc_h handle, int mtu) { CHECK_FEATURE_SUPPORTED(VPN_SERVICE_FEATURE); int result = VPNSVC_ERROR_NONE; - int dbus_result; vpnsvc_tun_s *tun_s = NULL; /* parameter check */ diff --git a/test/vpn_service_test.c b/test/vpn_service_test.c index 010791d..baa3a88 100755 --- a/test/vpn_service_test.c +++ b/test/vpn_service_test.c @@ -336,7 +336,7 @@ int test_vpnsvc_set_local_ip_address() char local_ip[VPNSVC_IP4_STRING_LEN]; printf("Input local IP address: "); - rv = scanf("%s", local_ip); + rv = scanf("%15s", local_ip); rv = vpnsvc_set_local_ip_address(handle, local_ip); if (rv != VPNSVC_ERROR_NONE) { @@ -353,7 +353,7 @@ int test_vpnsvc_set_remote_ip_address() char remote_ip[VPNSVC_IP4_STRING_LEN]; printf("Input remote IP address: "); - rv = scanf("%s", remote_ip); + rv = scanf("%15s", remote_ip); rv = vpnsvc_set_remote_ip_address(handle, remote_ip); if (rv != VPNSVC_ERROR_NONE) { @@ -364,6 +364,19 @@ int test_vpnsvc_set_remote_ip_address() return 1; } +int test_vpnsvc_update_settings() +{ + int rv; + + rv = vpnsvc_update_settings(handle); + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc update settings [%s]\n", test_print_error(rv)); + return -1; + } + + return 1; +} + int test_vpnsvc_add_route() { int rv; @@ -371,7 +384,7 @@ int test_vpnsvc_add_route() char addr[VPNSVC_IP4_STRING_LEN]; printf("Input route address: "); - rv = scanf("%s", addr); + rv = scanf("%15s", addr); printf("Input prefix: "); rv = scanf("%d", &prefix); @@ -391,7 +404,7 @@ int test_vpnsvc_remove_route() char addr[VPNSVC_IP4_STRING_LEN]; printf("Input route address: "); - rv = scanf("%s", addr); + rv = scanf("%15s", addr); printf("Input prefix: "); rv = scanf("%d", &prefix); @@ -410,7 +423,7 @@ int test_vpnsvc_add_dns_server() char addr[VPNSVC_IP4_STRING_LEN]; printf("Input dns server: "); - rv = scanf("%s", addr); + rv = scanf("%15s", addr); rv = vpnsvc_add_dns_server(handle, addr); if (rv != VPNSVC_ERROR_NONE) { @@ -427,7 +440,7 @@ int test_vpnsvc_remove_dns_server() char addr[VPNSVC_IP4_STRING_LEN]; printf("Input dns server address: "); - rv = scanf("%s", addr); + rv = scanf("%15s", addr); rv = vpnsvc_remove_dns_server(handle, addr); if (rv != VPNSVC_ERROR_NONE) { @@ -438,6 +451,61 @@ int test_vpnsvc_remove_dns_server() return 1; } +int test_vpnsvc_create() +{ + int rv = VPNSVC_ERROR_NONE; + int int_value; + + rv = vpnsvc_create(&handle); + + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc create fail [%s]\n", test_print_error(rv)); + return -1; + } else { + printf("vpnsvc create success(%p)\n", handle); + return 1; + } +} + +int test_vpnsvc_destroy() +{ + int rv = VPNSVC_ERROR_NONE; + int int_value; + + rv = vpnsvc_destroy(handle); + + if (rv != VPNSVC_ERROR_NONE) { + printf("vpnsvc destroy fail [%s]\n", test_print_error(rv)); + return -1; + } else { + printf("vpnsvc destroy success(%p)\n", handle); + return 1; + } +} + +int test_vpnsvc_set_iface_name() +{ + int rv; + char iface_name[VPNSVC_VPN_IFACE_NAME_LEN]; + + if (!handle) { + printf("invalid handle - handle is NULL\n"); + return -1; + } + + printf("Interface name: "); + rv = scanf("%15s", iface_name); + + rv = vpnsvc_set_iface_name(handle, iface_name); + + if (rv != VPNSVC_ERROR_NONE) + printf("vpnsvc set iface name fail [%s]\n", test_print_error(rv)); + else + printf("vpnsvc set iface name success\n"); + + return 1; +} + int test_exit() { exit(0); @@ -459,10 +527,14 @@ int (*test_function_table[])(void) = { test_vpnsvc_set_session, test_vpnsvc_set_local_ip_address, test_vpnsvc_set_remote_ip_address, + test_vpnsvc_update_settings, test_vpnsvc_add_route, test_vpnsvc_remove_route, test_vpnsvc_add_dns_server, test_vpnsvc_remove_dns_server, + test_vpnsvc_create, + test_vpnsvc_destroy, + test_vpnsvc_set_iface_name, test_exit, }; @@ -476,8 +548,8 @@ int main() printf("1 : vpnsvc_init\n"); printf("2 : vpnsvc_deinit\n"); printf("3 : vpnsvc_protect\n"); - printf("4 : vpnsvc_up(1->13->14)\n"); - printf("5 : vpnsvc_down\n"); + printf("4 : vpnsvc_up(internal)\n"); + printf("5 : vpnsvc_down(internal)\n"); printf("6 : vpnsvc_read\n"); printf("7 : vpnsvc_write\n"); printf("8 : vpnsvc_block_networks\n"); @@ -487,10 +559,14 @@ int main() printf("12 : vpnsvc_set_session\n"); printf("13 : vpnsvc_set_local_ip_address\n"); printf("14 : vpnsvc_set_remote_ip_address\n"); - printf("15 : vpnsvc_add_route\n"); - printf("16 : vpnsvc_remove_route\n"); - printf("17 : vpnsvc_add_dns_server\n"); - printf("18 : vpnsvc_remove_dns_server\n"); + printf("15 : vpnsvc_update_settings\n"); + printf("16 : vpnsvc_add_route\n"); + printf("17 : vpnsvc_remove_route\n"); + printf("18 : vpnsvc_add_dns_server\n"); + printf("19 : vpnsvc_remove_dns_server\n"); + printf("20 : vpnsvc_create(internal)\n"); + printf("21 : vpnsvc_destroy(internal)\n"); + printf("22 : vpnsvc_set_iface_name(internal)\n"); printf("q : quit\n"); TEST_CONSOLE_INPUT(input, 3); -- 2.7.4