From: Sangwan Kwon Date: Mon, 27 Jul 2020 05:58:55 +0000 (+0900) Subject: Reorganize directory structure about virtual-table X-Git-Tag: submit/tizen/20200810.073515~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=92db6036973da786f92634c83a85a5a768bee116;p=platform%2Fcore%2Fsecurity%2Fvist.git Reorganize directory structure about virtual-table Change-Id: I22a067aa48666157dfe9af37fe370aecc0b18f31 Signed-off-by: Sangwan Kwon --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 995e690..0fd59d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,8 +39,18 @@ SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++1z -O2 -DNDEBUG") SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed") SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") +SET(VIST_SRCS ${CMAKE_SOURCE_DIR}/src/vist) +SET(VIST_TABLES_SRCS ${CMAKE_SOURCE_DIR}/tables) + +SET(TARGET_OSQUERY_LIB osquery) +SET(TARGET_VIST_CLIENT_LIB vist-client) +SET(TARGET_VIST_COMMON_LIB vist-common) +SET(TARGET_VIST_LIB vist) +SET(TARGET_VIST_POLICY_LIB vist-policy) + ADD_DEFINITIONS("-fPIC") +INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}") INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/src") # Suppresse SYSTEM header's warnings: INCLUDE_DIRECTORIES(SYSTEM "${CMAKE_SOURCE_DIR}/src/osquery/include") @@ -50,6 +60,7 @@ ENABLE_TESTING() ADD_SUBDIRECTORY(data) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(tables) IF(DEFINED GBS_BUILD) ADD_SUBDIRECTORY(systemd) diff --git a/plugins/bluetooth/CMakeLists.txt b/plugins/bluetooth/CMakeLists.txt deleted file mode 100644 index 07a2346..0000000 --- a/plugins/bluetooth/CMakeLists.txt +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright (c) 2019 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. -# -SET(TARGET "vist-plugin-bluetooth") -SET(TARGET_TEST ${TARGET}-test) - -SET(PLUGIN_SOURCES "bluetooth.cpp") - -SET(DEPENDENCY bluetooth-api - capi-network-bluetooth) - -PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) - -INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS}) - -ADD_LIBRARY(${TARGET} SHARED ${PLUGIN_SOURCES}) -SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default") -TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES} - vist-common) - -INSTALL(FILES libvist-plugin-bluetooth.so - RENAME bluetooth - DESTINATION ${PLUGIN_INSTALL_DIR}) - -ADD_EXECUTABLE(${TARGET_TEST} ../tests.cpp - bluetooth.cpp - bluetooth-test.cpp) -TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES} - vist-common - vist-client - gtest - pthread) -INSTALL(TARGETS ${TARGET_TEST} - DESTINATION ${CMAKE_INSTALL_BINDIR} - PERMISSIONS OWNER_READ - OWNER_WRITE - OWNER_EXECUTE - GROUP_READ - GROUP_EXECUTE - WORLD_READ - WORLD_EXECUTE) diff --git a/plugins/bluetooth/bluetooth-test.cpp b/plugins/bluetooth/bluetooth-test.cpp deleted file mode 100644 index 1c282a8..0000000 --- a/plugins/bluetooth/bluetooth-test.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2019-present 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 "bluetooth.hpp" -#include "../test-util.hpp" - -#include -#include -#include - -#include - -#include - -using namespace vist; - -TEST(BluetoothTests, change_policy_state) -{ - using namespace vist::policy::plugin; - - try { - test::change_policy_state(); - test::change_policy_state(); - test::change_policy_state(); - test::change_policy_state(); - } catch(const vist::Exception& e) { - EXPECT_TRUE(false) << e.what(); - } -} - -TEST(BluetoothTests, get_policies) -{ - auto rows = Query::Execute("SELECT * FROM bluetooth"); - - EXPECT_TRUE(rows.size() == 1); -} - -TEST(BluetoothTests, set_policies) -{ - Query::Execute("INSERT INTO policy_admin (name) VALUES ('vist-plugin-bluetooth-test')"); - - Query::Execute("UPDATE bluetooth SET desktopConnectivity = 3, state = 7"); - Query::Execute("UPDATE bluetooth SET pairing = 2, tethering = 9"); - - auto rows = Query::Execute("SELECT * FROM bluetooth"); - if (rows.size() == 1) { - EXPECT_EQ(rows[0]["state"], "7"); - EXPECT_EQ(rows[0]["desktopConnectivity"], "3"); - EXPECT_EQ(rows[0]["pairing"], "2"); - EXPECT_EQ(rows[0]["tethering"], "9"); - } else { - EXPECT_TRUE(false); - } - - Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-plugin-bluetooth-test'"); -} - -TEST(BluetoothTest, query_builder) -{ - using namespace vist::schema; - - std::string query = BluetoothTable.selectAll(); - EXPECT_EQ(query, "SELECT * FROM bluetooth"); - - query = BluetoothTable.update(Bluetooth::DesktopConnectivity = 3, Bluetooth::State = 7); - EXPECT_EQ(query, "UPDATE bluetooth SET desktopConnectivity = 3, state = 7"); - - query = BluetoothTable.update(Bluetooth::Pairing = 2, Bluetooth::Tethering = 9); - EXPECT_EQ(query, "UPDATE bluetooth SET pairing = 2, tethering = 9"); -} diff --git a/plugins/bluetooth/bluetooth.cpp b/plugins/bluetooth/bluetooth.cpp deleted file mode 100644 index 26b3c41..0000000 --- a/plugins/bluetooth/bluetooth.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2019 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 "bluetooth.hpp" - -#include -#include -#include - -#include -#include - -#include - -namespace vist { -namespace policy { -namespace plugin { - -namespace { - -bool failed(auto value) -{ - return value == BLUETOOTH_DPM_RESULT_ACCESS_DENIED || value == BLUETOOTH_DPM_RESULT_FAIL; -} - -auto allowed(int value) -{ - return value ? BLUETOOTH_DPM_ALLOWED : BLUETOOTH_DPM_RESTRICTED; -} - -auto bt_allowed(int value) -{ - return value ? BLUETOOTH_DPM_BT_ALLOWED : BLUETOOTH_DPM_BT_RESTRICTED; -} - -} // anonymous namespace - -void BluetoothState::onChanged(const PolicyValue& value) -{ - auto enable = bt_allowed(value); - auto ret = ::bluetooth_dpm_set_allow_mode(enable); - if (failed(ret)) - THROW(ErrCode::RuntimeError) << "Failed to change bluetooth state: " << ret; - - INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable; -} - -void DesktopConnectivity::onChanged(const PolicyValue& value) -{ - auto enable = allowed(value); - auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable); - if (failed(ret)) - THROW(ErrCode::RuntimeError) << "Failed to change bt_desktop_connectivity: " << ret; - - INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable; -} - -void Pairing::onChanged(const PolicyValue& value) -{ - auto enable = allowed(value); - auto ret = ::bluetooth_dpm_set_pairing_state(enable); - if (failed(ret)) - THROW(ErrCode::RuntimeError) << "Failed to change bluetooth pairing: " << ret; - - INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable; -} - -void Tethering::onChanged(const PolicyValue& value) -{ - auto enable = value; - INFO(VIST_PLUGIN) << "Bluetooth tethering state is changed to " << enable; -} - -Bluetooth::Bluetooth(const std::string& name) : PolicyProvider(name) -{ - if (::bt_initialize() != BT_ERROR_NONE) - THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider."; -} - -Bluetooth::~Bluetooth() -{ - ::bt_deinitialize(); -} - -// TODO(Sangwan): Add privilege to provider -#define PRIVILEGE "http://tizen.org/privilege/dpm.bluetooth" - -extern "C" PolicyProvider* PolicyFactory() -{ - INFO(VIST_PLUGIN) << "Bluetooth plugin loaded."; - Bluetooth* provider = new Bluetooth("bluetooth"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - - return provider; -} - -} // namespace plugin -} // namespace policy -} // namespace vist diff --git a/plugins/bluetooth/bluetooth.hpp b/plugins/bluetooth/bluetooth.hpp deleted file mode 100644 index b32bf66..0000000 --- a/plugins/bluetooth/bluetooth.hpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2019 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 - */ - -#pragma once - -#include -#include -#include - -namespace vist { -namespace policy { -namespace plugin { - -class BluetoothState : public PolicyModel { -public: - BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -class DesktopConnectivity : public PolicyModel { -public: - DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -class Pairing: public PolicyModel { -public: - Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -class Tethering: public PolicyModel { -public: - Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {} - void onChanged(const PolicyValue&); -}; - -class Bluetooth final : public PolicyProvider { -public: - Bluetooth(const std::string& name); - ~Bluetooth(); -}; - -} // namespace plugin -} // namespace policy -} // namespace vist diff --git a/plugins/sample/CMakeLists.txt b/plugins/sample/CMakeLists.txt deleted file mode 100644 index 439fa43..0000000 --- a/plugins/sample/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright (c) 2019 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. -# -SET(TARGET "vist-plugin-sample") - -INCLUDE_DIRECTORIES(SYSTEM) - -ADD_LIBRARY(${TARGET} SHARED sample.cpp) -SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default") -TARGET_LINK_LIBRARIES(${TARGET} vist-common) - -IF(DEFINED GBS_BUILD) - INSTALL(FILES libvist-plugin-sample.so - RENAME sample - DESTINATION ${PLUGIN_INSTALL_DIR}) -ELSE(DEFINED GBS_BUILD) - INSTALL(TARGETS ${TARGET} - RENAME sample - DESTINATION ${PLUGIN_INSTALL_DIR}) -ENDIF(DEFINED GBS_BUILD) diff --git a/plugins/sample/sample.cpp b/plugins/sample/sample.cpp deleted file mode 100644 index 5327fb1..0000000 --- a/plugins/sample/sample.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2019 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 - -using namespace vist; -using namespace vist::policy; - -class SampleIntPolicy : public PolicyModel { -public: - SampleIntPolicy() : PolicyModel("sample-int-policy", PolicyValue(7)) - { - } - - void onChanged(const PolicyValue& value) override - { - INFO(VIST_PLUGIN) << "sample-int-policy is changed to " - << static_cast(value); - } -}; - -class SampleStrPolicy : public PolicyModel { -public: - SampleStrPolicy() : PolicyModel("sample-str-policy", PolicyValue("TEST")) - { - } - - void onChanged(const PolicyValue& value) override - { - INFO(VIST_PLUGIN) << "sample-str-policy is changed to " - << static_cast(value); - } - - int compare(const PolicyValue& lhs, const PolicyValue& rhs) const override - { - std::string lvalue = lhs; - std::string rvalue = rhs; - - INFO(VIST_PLUGIN) << "Custom compare method is called with [" << lvalue - << "], [" << rvalue << "]"; - - return lvalue.compare(rvalue); - } -}; - -class Sample : public PolicyProvider { -public: - Sample(const std::string& name) : PolicyProvider(name) - { - } - - ~Sample() - { - } -}; - -extern "C" PolicyProvider* PolicyFactory() -{ - INFO(VIST_PLUGIN) << "Sample plugin loaded."; - Sample* provider = new Sample("sample"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - - return provider; -} diff --git a/plugins/test-util.hpp b/plugins/test-util.hpp deleted file mode 100644 index 06a278e..0000000 --- a/plugins/test-util.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 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 - */ - -#pragma once - -#include -#include - -#include - -namespace vist { -namespace test { - -using namespace vist::policy; - -template -void change_policy_state() -{ - std::shared_ptr policy = std::make_shared(); - /// enable policy - policy->onChanged(PolicyValue(1)); - /// disable policy - policy->onChanged(PolicyValue(0)); -} - -} // namespace test -} // namespace vist diff --git a/plugins/tests.cpp b/plugins/tests.cpp deleted file mode 100644 index 0fcfc89..0000000 --- a/plugins/tests.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2019 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 - -int main(int argc, char* argv[]) { - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/plugins/usb/CMakeLists.txt b/plugins/usb/CMakeLists.txt deleted file mode 100644 index cfc1c3f..0000000 --- a/plugins/usb/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (c) 2019 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. -# -SET(TARGET "dpm-plugin-usb") - -SET(PLUGIN_SOURCES "usb.cpp") - -SET(DEPENDENCY klay) - -PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) - -SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack") - -ADD_LIBRARY(${TARGET} SHARED ${PLUGIN_SOURCES}) -SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default") -INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS}) -TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES}) - -INSTALL(FILES libdpm-plugin-usb.so - RENAME usb - DESTINATION ${PLUGIN_INSTALL_DIR}) diff --git a/plugins/usb/usb.cpp b/plugins/usb/usb.cpp deleted file mode 100644 index 82dac70..0000000 --- a/plugins/usb/usb.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Copyright (c) 2019 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 "../dlog.h" - -#define DEVICED_SYSNOTI_INTERFACE \ - "org.tizen.system.deviced", \ - "/Org/Tizen/System/DeviceD/SysNoti", \ - "org.tizen.system.deviced.SysNoti", \ - "control" - -class DebuggingMode : public GlobalPolicy { -public: - DebuggingMode() : GlobalPolicy("usb-debugging") - { - PolicyEventNotifier::create("usb_debugging"); - } - - bool apply(const DataType& value) - { - int enable = value; - PolicyEventNotifier::emit("usb_debugging", enable ? "allowed" : "disallowed"); - return true; - } -}; - -class Tethering : public GlobalPolicy { -public: - Tethering() : GlobalPolicy("usb-tethering") - { - PolicyEventNotifier::create("usb_tethering"); - } - - bool apply(const DataType& value) - { - int enable = value; - PolicyEventNotifier::emit("usb_tethering", enable ? "allowed" : "disallowed"); - return true; - } -}; - -class Client : public GlobalPolicy { -public: - Client() : GlobalPolicy("usb-client") - { - PolicyEventNotifier::create("usb_client"); - sendDbusSignal(); - } - - bool apply(const DataType& value) - { - int ret; - int enable = value; - - try { - std::string pid(std::to_string(::getpid())); - std::string state(std::to_string(enable)); - dbus::Connection &systemDBus = dbus::Connection::getSystem(); - systemDBus.methodcall(DEVICED_SYSNOTI_INTERFACE, - -1, "(i)", "(sisss)", - "control", 3, pid.c_str(), "1", state.c_str()).get("(i)", &ret); - } catch(runtime::Exception& e) { - ERROR(PLUGINS, "Failed to enforce usb client"); - return false; - } - - if (ret == 0) { - PolicyEventNotifier::emit("usb_client", enable ? "allowed" : "disallowed"); - return true; - } - - return false; - } - - void sendDbusSignal(void) - { - int ret; - int enable = get().value; - - try { - std::string pid(std::to_string(::getpid())); - std::string state(std::to_string(enable)); - dbus::Connection &systemDBus = dbus::Connection::getSystem(); - systemDBus.methodcall(DEVICED_SYSNOTI_INTERFACE, - -1, "(i)", "(sisss)", - "control", 3, pid.c_str(), "1", state.c_str()).get("(i)", &ret); - } catch(runtime::Exception& e) { - ERROR(PLUGINS, "Failed to enforce usb client"); - } - } -}; - -class Usb : public AbstractPolicyProvider { -public: - int setDebuggingState(bool enable); - bool getDebuggingState(); - - int setTetheringState(bool enable); - bool getTetheringState(); - - int setClientState(bool enable); - bool getClientState(); - -private: - DebuggingMode debugging; - Tethering tethering; - Client client; -}; - -int Usb::setDebuggingState(bool enable) -{ - try { - debugging.set(enable); - } catch (runtime::Exception& e) { - ERROR(PLUGINS, e.what()); - return -1; - } - - return 0; -} - -bool Usb::getDebuggingState() -{ - return debugging.get(); -} - -int Usb::setTetheringState(bool enable) -{ - try { - tethering.set(enable); - } catch (runtime::Exception& e) { - ERROR(PLUGINS, e.what()); - return -1; - } - - return 0; -} - -bool Usb::getTetheringState() -{ - return tethering.get(); -} - -int Usb::setClientState(bool enable) -{ - try { - client.set(enable); - } catch (runtime::Exception& e) { - ERROR(PLUGINS, e.what()); - return -1; - } - - return 0; -} - -bool Usb::getClientState() -{ - return client.get(); -} - -extern "C" { - -#define PRIVILEGE_USB "http://tizen.org/privilege/dpm.usb" -#define PRIVILEGE_DEBUGGING "http://tizen.org/privilege/dpm.debugging" - -AbstractPolicyProvider *PolicyFactory(PolicyControlContext& context) -{ - INFO(PLUGINS, "Usb plugin loaded"); - Usb *policy = new Usb(); - - context.expose(policy, PRIVILEGE_DEBUGGING, (int)(Usb::setDebuggingState)(bool)); - context.expose(policy, PRIVILEGE_USB, (int)(Usb::setTetheringState)(bool)); - context.expose(policy, PRIVILEGE_USB, (int)(Usb::setClientState)(bool)); - - context.expose(policy, "", (bool)(Usb::getDebuggingState)()); - context.expose(policy, "", (bool)(Usb::getTetheringState)()); - context.expose(policy, "", (bool)(Usb::getClientState)()); - - return policy; -} - -} // extern "C" diff --git a/plugins/wifi/CMakeLists.txt b/plugins/wifi/CMakeLists.txt deleted file mode 100644 index 4a70348..0000000 --- a/plugins/wifi/CMakeLists.txt +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) 2019 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. -# -SET(TARGET "vist-plugin-wifi") -SET(TARGET_TEST ${TARGET}-test) - -SET(PLUGIN_SOURCES "wifi.cpp") - -SET(DEPENDENCY klay - capi-network-wifi-manager - capi-network-connection) - -PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) - -INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS}) - -ADD_LIBRARY(${TARGET} SHARED ${PLUGIN_SOURCES}) -SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default") -TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES} - vist-common) -INSTALL(FILES libvist-plugin-wifi.so - RENAME wifi - DESTINATION ${PLUGIN_INSTALL_DIR}) - -ADD_EXECUTABLE(${TARGET_TEST} ../tests.cpp - wifi.cpp - wifi-test.cpp) -TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES} - vist-common - gtest - pthread) -INSTALL(TARGETS ${TARGET_TEST} - DESTINATION ${CMAKE_INSTALL_BINDIR} - PERMISSIONS OWNER_READ - OWNER_WRITE - OWNER_EXECUTE - GROUP_READ - GROUP_EXECUTE - WORLD_READ - WORLD_EXECUTE) diff --git a/plugins/wifi/wifi-test.cpp b/plugins/wifi/wifi-test.cpp deleted file mode 100644 index ca048fd..0000000 --- a/plugins/wifi/wifi-test.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2019 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 "wifi.hpp" -#include "../test-util.hpp" - -#include - -#include - -using namespace vist; -using namespace vist::policy::plugin; - -TEST(WifiTests, change_policy_state) -{ - try { - test::change_policy_state(); - test::change_policy_state(); - test::change_policy_state(); - test::change_policy_state(); - } catch(const vist::Exception& e) { - EXPECT_TRUE(false) << e.what(); - } -} diff --git a/plugins/wifi/wifi.cpp b/plugins/wifi/wifi.cpp deleted file mode 100644 index cb169d4..0000000 --- a/plugins/wifi/wifi.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (c) 2019 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 "wifi.hpp" - -#include -#include - -#include - -#include - -#define NETCONFIG_INTERFACE \ - "net.netconfig", \ - "/net/netconfig/network", \ - "net.netconfig.network" - -namespace vist { -namespace policy { -namespace plugin { - -void WifiState::onChanged(const PolicyValue& value) -{ - int enable = value; - INFO(VIST_PLUGIN) << "Wifi state is changed to " << enable; - klay::dbus::Connection &systemDBus = klay::dbus::Connection::getSystem(); - systemDBus.methodcall(NETCONFIG_INTERFACE, - "DevicePolicySetWifi", - -1, - "", - "(i)", - enable); -} - -void ProfileChange::onChanged(const PolicyValue& value) -{ - int enable = value; - INFO(VIST_PLUGIN) << "Wifi profile change state is changed to " << enable; - dbus::Connection &systemDBus = dbus::Connection::getSystem(); - systemDBus.methodcall(NETCONFIG_INTERFACE, - "DevicePolicySetWifiProfile", - -1, - "", - "(i)", - enable); -} - -void Hotspot::onChanged(const PolicyValue& value) -{ - int enable = value; - INFO(VIST_PLUGIN) << "Wifi hotspot change state is changed to " << enable; -} - -void SsidRestriction::onChanged(const PolicyValue& value) -{ - int enable = value; - INFO(VIST_PLUGIN) << "Wifi ssid restriction change state is changed to " << enable; -} - -Wifi::Wifi(const std::string& name) : PolicyProvider(name) -{ - int ret = ::wifi_manager_initialize(&handle); - if (ret != WIFI_MANAGER_ERROR_NONE) { - if (ret == WIFI_MANAGER_ERROR_NOT_SUPPORTED) - ERROR("Wifi manager is not supported."); - - THROW(ErrCode::RuntimeError) << "Failed to init WiFi Manager."; - } -} - -Wifi::~Wifi() -{ - ::wifi_manager_deinitialize(handle); -} - -// TODO(Sangwan): Add privilege to provider -#define PRIVILEGE "http://tizen.org/privilege/dpm.wifi" - -extern "C" PolicyProvider* PolicyFactory() -{ - INFO(VIST_PLUGIN) << "Wifi plugin loaded."; - Wifi* provider = new Wifi("wifi"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - - return provider; -} - -} // namespace plugin -} // namespace policy -} // namespace vist diff --git a/plugins/wifi/wifi.hpp b/plugins/wifi/wifi.hpp deleted file mode 100644 index b076cbb..0000000 --- a/plugins/wifi/wifi.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2019 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 - */ - -#pragma once - -#include -#include -#include - -#include - -namespace vist { -namespace policy { -namespace plugin { - -struct WifiState : public PolicyModel { - WifiState() : PolicyModel("wifi", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -struct ProfileChange : public PolicyModel { - ProfileChange() : PolicyModel("wifi-profile-change", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -struct Hotspot : public PolicyModel { - Hotspot() : PolicyModel("wifi-hotspot", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -struct SsidRestriction : public PolicyModel { - SsidRestriction() : PolicyModel("wifi-ssid-restriction", PolicyValue(0)) {} - void onChanged(const PolicyValue& value) override; -}; - -class Wifi final : public PolicyProvider { -public: - Wifi(const std::string& name); - ~Wifi(); - -private: - ::wifi_manager_h handle = nullptr; -}; - -} // namespace plugin -} // namespace policy -} // namespace vist diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b8168b5..43477b5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +# Copyright (c) 2019-present 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. @@ -12,11 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License -SET(TARGET_OSQUERY_LIB osquery) -SET(TARGET_VIST_CLIENT_LIB vist-client) -SET(TARGET_VIST_COMMON_LIB vist-common) -SET(TARGET_VIST_LIB vist) -SET(TARGET_VIST_POLICY_LIB vist-policy) - ADD_SUBDIRECTORY(osquery) ADD_SUBDIRECTORY(vist) diff --git a/src/vist/service/CMakeLists.txt b/src/vist/service/CMakeLists.txt index 983e1d2..b11ed24 100644 --- a/src/vist/service/CMakeLists.txt +++ b/src/vist/service/CMakeLists.txt @@ -12,7 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License -ADD_VIST_LIBRARY(vist_core vistd.cpp) +ADD_VIST_LIBRARY(vist_core vistd.cpp + ${VIST_TABLES_SRCS}/built-in/policy-admin.cpp + ${VIST_TABLES_SRCS}/built-in/policy.cpp) + FILE(GLOB CORE_TESTS "tests/*.cpp") ADD_VIST_TEST(${CORE_TESTS}) diff --git a/src/vist/service/vistd.cpp b/src/vist/service/vistd.cpp index c2fffbb..f3a7fdd 100644 --- a/src/vist/service/vistd.cpp +++ b/src/vist/service/vistd.cpp @@ -22,10 +22,10 @@ #include #include #include - #include -#include -#include + +#include +#include #include #include diff --git a/src/vist/table/CMakeLists.txt b/src/vist/table/CMakeLists.txt index e17acc0..354515a 100644 --- a/src/vist/table/CMakeLists.txt +++ b/src/vist/table/CMakeLists.txt @@ -13,13 +13,4 @@ # limitations under the License ## static virtual table -ADD_VIST_LIBRARY(vist_table util.cpp - policy/policy-admin.cpp - policy/policy.cpp) - -## dynamic virtual table -ADD_SUBDIRECTORY(policy/sample) - -IF(DEFINED GBS_BUILD) - ADD_SUBDIRECTORY(policy/bluetooth) -ENDIF(DEFINED GBS_BUILD) +ADD_VIST_LIBRARY(vist_table util.cpp) diff --git a/src/vist/table/policy/bluetooth/CMakeLists.txt b/src/vist/table/policy/bluetooth/CMakeLists.txt deleted file mode 100644 index 6f63912..0000000 --- a/src/vist/table/policy/bluetooth/CMakeLists.txt +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) 2019-present 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. -# -SET(TARGET "vist-bluetooth-policy") -SET(TARGET_TEST ${TARGET}-test) - -SET(DEPENDENCY bluetooth-api - capi-network-bluetooth) - -PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) - -INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS}) - -## LIB ########################################### -ADD_LIBRARY(${TARGET} SHARED policy.cpp table.cpp) -TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES} - ${TARGET_VIST_COMMON_LIB} - ${TARGET_VIST_POLICY_LIB}) - -INSTALL(TARGETS ${TARGET} DESTINATION ${TABLE_INSTALL_DIR}) - -## TEST ############################################# -ADD_EXECUTABLE(${TARGET_TEST} ../../../main/tests.cpp - policy.cpp - table.cpp - tests/bluetooth.cpp) -TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES} - ${TARGET_VIST_CLIENT_LIB} - ${TARGET_VIST_LIB} - gtest - pthread) -INSTALL(TARGETS ${TARGET_TEST} - DESTINATION ${CMAKE_INSTALL_BINDIR} - PERMISSIONS OWNER_READ - OWNER_WRITE - OWNER_EXECUTE - GROUP_READ - GROUP_EXECUTE - WORLD_READ - WORLD_EXECUTE) diff --git a/src/vist/table/policy/bluetooth/policy.cpp b/src/vist/table/policy/bluetooth/policy.cpp deleted file mode 100644 index 49ba0b4..0000000 --- a/src/vist/table/policy/bluetooth/policy.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2019-present 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 "policy.hpp" - -#include -#include - -#include -#include -#include - -namespace vist { -namespace policy { - -namespace { - -bool failed(int value) -{ - return value == BLUETOOTH_DPM_RESULT_ACCESS_DENIED || value == BLUETOOTH_DPM_RESULT_FAIL; -} - -auto allowed(int value) -{ - return value ? BLUETOOTH_DPM_ALLOWED : BLUETOOTH_DPM_RESTRICTED; -} - -auto bt_allowed(int value) -{ - return value ? BLUETOOTH_DPM_BT_ALLOWED : BLUETOOTH_DPM_BT_RESTRICTED; -} - -} // anonymous namespace - -void BluetoothState::onChanged(const PolicyValue& value) -{ - auto enable = bt_allowed(value); - auto ret = ::bluetooth_dpm_set_allow_mode(enable); - if (failed(ret)) - THROW(ErrCode::RuntimeError) << "Failed to change bluetooth state: " << ret; - - INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable; -} - -void DesktopConnectivity::onChanged(const PolicyValue& value) -{ - auto enable = allowed(value); - auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable); - if (failed(ret)) - THROW(ErrCode::RuntimeError) << "Failed to change bt_desktop_connectivity: " << ret; - - INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable; -} - -void Pairing::onChanged(const PolicyValue& value) -{ - auto enable = allowed(value); - auto ret = ::bluetooth_dpm_set_pairing_state(enable); - if (failed(ret)) - THROW(ErrCode::RuntimeError) << "Failed to change bluetooth pairing: " << ret; - - INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable; -} - -void Tethering::onChanged(const PolicyValue& value) -{ - auto enable = value; - INFO(VIST_PLUGIN) << "Bluetooth tethering state is changed to " << enable; -} - -Bluetooth::Bluetooth(const std::string& name) : PolicyProvider(name) -{ - if (::bt_initialize() != BT_ERROR_NONE) - THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider."; -} - -Bluetooth::~Bluetooth() -{ - ::bt_deinitialize(); -} - -} // namespace policy -} // namespace vist diff --git a/src/vist/table/policy/bluetooth/policy.hpp b/src/vist/table/policy/bluetooth/policy.hpp deleted file mode 100644 index 3c927df..0000000 --- a/src/vist/table/policy/bluetooth/policy.hpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019-present 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 - */ - -#pragma once - -#include -#include -#include - -namespace vist { -namespace policy { - -class BluetoothState : public PolicyModel { -public: - BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -class DesktopConnectivity : public PolicyModel { -public: - DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -class Pairing: public PolicyModel { -public: - Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; - -class Tethering: public PolicyModel { -public: - Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {} - void onChanged(const PolicyValue&); -}; - -class Bluetooth final : public PolicyProvider { -public: - Bluetooth(const std::string& name); - ~Bluetooth(); -}; - -} // namespace policy -} // namespace vist diff --git a/src/vist/table/policy/bluetooth/table.cpp b/src/vist/table/policy/bluetooth/table.cpp deleted file mode 100644 index 6bedb72..0000000 --- a/src/vist/table/policy/bluetooth/table.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2020-present 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 "policy.hpp" -#include "table.hpp" - -#include -#include -#include -#include -#include -#include - -extern "C" vist::table::DynamicTable* DynamicTableFactory() -{ - return new vist::table::BluetoothTable; -} - -namespace vist { -namespace table { - -namespace { - -std::map ALIAS = { - { "state", "bluetooth" }, - { "desktopConnectivity", "bluetooth-desktop-connectivity" }, - { "pairing", "bluetooth-pairing" }, - { "tethering", "bluetooth-tethering"} -}; - -void setPolicy(const std::string& name, int value) -{ - vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value)); -} - -} // anonymous namespace - -void BluetoothTable::init() -{ - Builder::table("bluetooth"); - - auto provider = std::make_shared("bluetooth"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - - policy::API::Admin::AddProvider(provider); - - INFO(VIST_PLUGIN) << "Bluetooth plugin loaded."; -} - -TableColumns BluetoothTable::columns() const -{ - return { - Builder::column("state"), - Builder::column("desktopConnectivity"), - Builder::column("pairing"), - Builder::column("tethering") - }; -} - -QueryData BluetoothTable::select(QueryContext&) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Select query about bluetooth table."; - - Row row; - for (const auto& [schemaName, policyName] : ALIAS) { - int value = vist::policy::API::Get(policyName); - row[schemaName] = std::to_string(value); - } - - QueryData results; - results.emplace_back(std::move(row)); - - return results; - - TABLE_EXCEPTION_GUARD_END -} - -QueryData BluetoothTable::update(QueryContext&, const PluginRequest& request) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Update query about bluetooth table."; - - /// TODO(Sangwan): Sync vtab schema with policy definition - setPolicy("bluetooth", Parser::column(request, 0)); - setPolicy("bluetooth-desktop-connectivity", Parser::column(request, 1)); - setPolicy("bluetooth-pairing", Parser::column(request, 2)); - setPolicy("bluetooth-tethering", Parser::column(request, 3)); - - return success(); - - TABLE_EXCEPTION_GUARD_END -} - -} // namespace table -} // namespace vist diff --git a/src/vist/table/policy/bluetooth/table.hpp b/src/vist/table/policy/bluetooth/table.hpp deleted file mode 100644 index 0acfde2..0000000 --- a/src/vist/table/policy/bluetooth/table.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2020-present 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 - */ -/* - * Query example - * - SELECT * FROM bluetooth - * - UPDATE bluetooth SET state = 1 // enable - * - UPDATE bluetooth SET state = 0 // disable - */ - -#include - -namespace vist { -namespace table { - -class BluetoothTable final : public DynamicTable { -public: - void init(); - -private: - TableColumns columns() const override; - QueryData select(QueryContext&) override; - QueryData update(QueryContext&, const PluginRequest& request) override; -}; - -} // namespace table -} // namespace vist diff --git a/src/vist/table/policy/bluetooth/tests/bluetooth.cpp b/src/vist/table/policy/bluetooth/tests/bluetooth.cpp deleted file mode 100644 index d682805..0000000 --- a/src/vist/table/policy/bluetooth/tests/bluetooth.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2019-present 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 "../policy.hpp" - -#include -#include -#include -#include - -#include - -#include - -using namespace vist; -using namespace vist::policy; - -namespace { - -template -void change_policy_state() -{ - std::shared_ptr policy = std::make_shared(); - /// enable policy - policy->onChanged(PolicyValue(1)); - /// disable policy - policy->onChanged(PolicyValue(0)); -} - -} // anonymous namespace - -TEST(BluetoothTests, change_policy_state) -{ - try { - change_policy_state(); - change_policy_state(); - change_policy_state(); - change_policy_state(); - } catch(const vist::Exception& e) { - EXPECT_TRUE(false) << e.what(); - } -} - -TEST(BluetoothTests, get_policies) -{ - auto rows = Query::Execute("SELECT * FROM bluetooth"); - - EXPECT_TRUE(rows.size() == 1); -} - -TEST(BluetoothTests, set_policies) -{ - // Binany should be enrolled as admin to control policy. - Query::Execute("INSERT INTO policy_admin (name) VALUES ('vist-bluetooth-policy-test')"); - - Query::Execute("UPDATE bluetooth SET desktopConnectivity = 3, state = 7"); - Query::Execute("UPDATE bluetooth SET pairing = 2, tethering = 9"); - - auto rows = Query::Execute("SELECT * FROM bluetooth"); - if (rows.size() == 1) { - EXPECT_EQ(rows[0]["state"], "7"); - EXPECT_EQ(rows[0]["desktopConnectivity"], "3"); - EXPECT_EQ(rows[0]["pairing"], "2"); - EXPECT_EQ(rows[0]["tethering"], "9"); - } else { - EXPECT_TRUE(false); - } - - Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-bluetooth-policy-test'"); -} diff --git a/src/vist/table/policy/policy-admin.cpp b/src/vist/table/policy/policy-admin.cpp deleted file mode 100644 index 55179b4..0000000 --- a/src/vist/table/policy/policy-admin.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (c) 2019-present 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 "policy-admin.hpp" - -#include -#include -#include -#include -#include -#include - -namespace vist { -namespace table { - -namespace { - -std::string removeAlias(std::string&& alias) -{ - auto pos = alias.find(";"); - auto token = alias.substr(0, pos); - - if (token == "name") - return alias.erase(0, pos + 1); - else - return std::string(); -} - -} // anonymous namespace - -void PolicyAdminTable::Init() -{ - Builder::table("policy_admin"); -} - -TableColumns PolicyAdminTable::columns() const -{ - return { - Builder::column("name"), - Builder::column("activated"), - }; -} - -QueryData PolicyAdminTable::select(QueryContext& context) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Select query about policy-admin table."; - - QueryData results; - auto admins = vist::policy::API::Admin::GetAll(); - - for (auto& admin : admins) { - if (context.constraints["name"].exists(EQUALS)) { /// where clause - auto names = context.constraints["name"].getAll(EQUALS); - for (const auto& name : names) { - if (name == admin.first) { - Row row; - row["name"] = admin.first; - row["activated"] = std::to_string(admin.second); - - DEBUG(VIST) << "Admin info [name]: " << row["name"] - << ", [activated]:" << row["activated"]; - - results.emplace_back(std::move(row)); - } - } - } else { /// select *; - Row row; - row["name"] = admin.first; - row["activated"] = std::to_string(admin.second); - - DEBUG(VIST) << "Admin info [name]: " << row["name"] - << ", [activated]:" << row["activated"]; - results.emplace_back(std::move(row)); - } - } - - return results; - - TABLE_EXCEPTION_GUARD_END -} - -QueryData PolicyAdminTable::insert(QueryContext&, const PluginRequest& request) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Insert query about policy-admin table."; - auto admin = Parser::column(request, 0); - - DEBUG(VIST) << "Admin info [name]: " << admin; - vist::policy::API::Admin::Enroll(admin); - - return success(); - - TABLE_EXCEPTION_GUARD_END -} - -QueryData PolicyAdminTable::delete_(QueryContext&, const PluginRequest& request) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Delete query about policy-admin table."; - auto admin = removeAlias(Parser::column(request, 0)); - - DEBUG(VIST) << "Admin info [name]: " << admin; - vist::policy::API::Admin::Disenroll(admin); - - return success(); - - TABLE_EXCEPTION_GUARD_END -} - -QueryData PolicyAdminTable::update(QueryContext&, const PluginRequest& request) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Update query about policy-admin table."; - - auto name = Parser::column(request, 0); - auto activated = Parser::column(request, 1); - - vist::policy::API::Admin::Activate(name, activated); - - return success(); - - TABLE_EXCEPTION_GUARD_END -} - -} // namespace tables -} // namespace osquery diff --git a/src/vist/table/policy/policy-admin.hpp b/src/vist/table/policy/policy-admin.hpp deleted file mode 100644 index 6112f8c..0000000 --- a/src/vist/table/policy/policy-admin.hpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2020-present 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 - -namespace vist { -namespace table { - -using namespace osquery; - -class PolicyAdminTable final : public TablePlugin { -public: - static void Init(); - -private: - TableColumns columns() const override; - QueryData select(QueryContext&) override; - QueryData delete_(QueryContext&, const PluginRequest& request) override; - QueryData insert(QueryContext&, const PluginRequest& request) override; - QueryData update(QueryContext&, const PluginRequest& request) override; -}; - -} // namespace table -} // namespace vist diff --git a/src/vist/table/policy/policy.cpp b/src/vist/table/policy/policy.cpp deleted file mode 100644 index e3526a5..0000000 --- a/src/vist/table/policy/policy.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2019-present 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 "policy.hpp" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -namespace vist { -namespace table { - -namespace { - -Row convert(const std::string& name, const vist::policy::PolicyValue& value) -{ - Row r; - r["name"] = name; - r["value"] = value.dump(); - - return r; -} - -} // anonymous namespace - -void PolicyTable::Init() -{ - Builder::table("policy"); -} - -TableColumns PolicyTable::columns() const -{ - return { - Builder::column("name"), - Builder::column("value") - }; -} - -QueryData PolicyTable::select(QueryContext& context) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Select query about policy table."; - - QueryData results; - if (context.constraints["name"].exists(EQUALS)) { /// where clause - auto names = context.constraints["name"].getAll(EQUALS); - for (const auto& name : names) { - auto value = vist::policy::API::Get(name); - auto row = convert(name, value); - - results.emplace_back(std::move(row)); - } - } else { /// select *; - auto policies = vist::policy::API::GetAll(); - for (auto& policy : policies) { - auto row = convert(policy.first, policy.second); - - results.emplace_back(std::move(row)); - } - } - - return results; - - TABLE_EXCEPTION_GUARD_END -} - -QueryData PolicyTable::update(QueryContext&, const PluginRequest& request) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Update query about policy table."; - - auto name = Parser::column(request, 0); - auto dumpedValue = Parser::column(request, 1); - - vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(dumpedValue, true)); - - return success(); - - TABLE_EXCEPTION_GUARD_END -} - -} // namespace table -} // namespace vist diff --git a/src/vist/table/policy/policy.hpp b/src/vist/table/policy/policy.hpp deleted file mode 100644 index 7bdcd4f..0000000 --- a/src/vist/table/policy/policy.hpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2020-present 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 - -namespace vist { -namespace table { - -using namespace osquery; - -class PolicyTable final : public TablePlugin { -public: - static void Init(); - -private: - TableColumns columns() const override; - QueryData select(QueryContext&) override; - QueryData update(QueryContext&, const PluginRequest& request) override; -}; - -} // namespace table -} // namespace vist diff --git a/src/vist/table/policy/sample/CMakeLists.txt b/src/vist/table/policy/sample/CMakeLists.txt deleted file mode 100644 index 9a1765a..0000000 --- a/src/vist/table/policy/sample/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright (c) 2019-present 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. -# -SET(TARGET "vist-table-sample") - -INCLUDE_DIRECTORIES(SYSTEM) - -ADD_LIBRARY(${TARGET} SHARED table.cpp) -TARGET_LINK_LIBRARIES(${TARGET} vist-common ${TARGET_VIST_POLICY_LIB}) - -INSTALL(TARGETS ${TARGET} DESTINATION ${TABLE_INSTALL_DIR}) diff --git a/src/vist/table/policy/sample/policy.hpp b/src/vist/table/policy/sample/policy.hpp deleted file mode 100644 index b9c309f..0000000 --- a/src/vist/table/policy/sample/policy.hpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2020-present 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 - */ - -#pragma once - -#include -#include -#include -#include - -#include -#include - -namespace vist { -namespace policy { - -class SampleIntPolicy : public PolicyModel { -public: - SampleIntPolicy() : PolicyModel("sample_int_policy", PolicyValue(7)) - { - } - - void onChanged(const PolicyValue& value) override - { - INFO(VIST_PLUGIN) << "sample_int_policy is changed to " - << static_cast(value); - } -}; - -class SampleStrPolicy : public PolicyModel { -public: - SampleStrPolicy() : PolicyModel("sample_str_policy", PolicyValue("TEST")) - { - } - - void onChanged(const PolicyValue& value) override - { - INFO(VIST_PLUGIN) << "sample_str_policy is changed to " - << static_cast(value); - } - - int compare(const PolicyValue& lhs, const PolicyValue& rhs) const override - { - std::string lvalue = lhs; - std::string rvalue = rhs; - - INFO(VIST_PLUGIN) << "Custom compare method is called with [" << lvalue - << "], [" << rvalue << "]"; - - return lvalue.compare(rvalue); - } -}; - -class SampleProvider : public PolicyProvider { -public: - SampleProvider(const std::string& name) : PolicyProvider(name) - { - } - - ~SampleProvider() - { - } -}; - -} // namespace policy -} // namespace vist diff --git a/src/vist/table/policy/sample/table.cpp b/src/vist/table/policy/sample/table.cpp deleted file mode 100644 index cdab50c..0000000 --- a/src/vist/table/policy/sample/table.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2020-present 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 "policy.hpp" -#include "table.hpp" - -#include -#include -#include -#include -#include -#include - -extern "C" vist::table::DynamicTable* DynamicTableFactory() -{ - return new vist::table::SamplePolicyTable; -} - -namespace vist { -namespace table { - -void SamplePolicyTable::init() -{ - Builder::table("sample_policy"); - - // Register policy to policy-manager - using namespace policy; - auto provider = std::make_shared("sample-provider"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - - policy::API::Admin::AddProvider(std::move(provider)); - - INFO(VIST_PLUGIN) << "Sample plugin loaded."; -} - -TableColumns SamplePolicyTable::columns() const -{ - return { - Builder::column("sample_int_policy"), - Builder::column("sample_str_policy") - }; -} - -QueryData SamplePolicyTable::select(QueryContext&) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Select query about sample-policy table."; - - Row row; - int intPolicy = vist::policy::API::Get("sample_int_policy"); - row["sample_int_policy"] = std::to_string(intPolicy); - - std::string strPolicy = vist::policy::API::Get("sample_str_policy"); - row["sample_str_policy"] = strPolicy; - - QueryData results; - results.emplace_back(std::move(row)); - - return results; - - TABLE_EXCEPTION_GUARD_END -} - -QueryData SamplePolicyTable::update(QueryContext&, const PluginRequest& request) -{ - TABLE_EXCEPTION_GUARD_START - - INFO(VIST) << "Update query about sample-policy table."; - - auto intPolicy = Parser::column(request, 0); - auto strPolicy = Parser::column(request, 1); - - policy::API::Admin::Set("sample_int_policy", policy::PolicyValue(intPolicy)); - policy::API::Admin::Set("sample_str_policy", policy::PolicyValue(strPolicy)); - - return success(); - - TABLE_EXCEPTION_GUARD_END -} - -} // namespace table -} // namespace vist diff --git a/src/vist/table/policy/sample/table.hpp b/src/vist/table/policy/sample/table.hpp deleted file mode 100644 index e66e862..0000000 --- a/src/vist/table/policy/sample/table.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2020-present 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 - */ -/* - * Query example - * - SELECT * FROM sample_policy - * - UPDATE sample_policy SET sample_int_policy = 99 - * - UPDATE sample_policy SET sample_str_policy = 'TEST_VALUE' - */ - -#include -#include - -namespace vist { -namespace table { - -class SamplePolicyTable final : public DynamicTable { -public: - void init(); - -private: - TableColumns columns() const override; - QueryData select(QueryContext&) override; - QueryData update(QueryContext&, const PluginRequest& request) override; -}; - -} // namespace table -} // namespace vist diff --git a/tables/CMakeLists.txt b/tables/CMakeLists.txt new file mode 100644 index 0000000..7430534 --- /dev/null +++ b/tables/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (c) 2020-present 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 + +ADD_SUBDIRECTORY(policy/sample) + +IF(DEFINED GBS_BUILD) + ADD_SUBDIRECTORY(policy/bluetooth) +ENDIF(DEFINED GBS_BUILD) + +# TODO: Add built-in table's TC diff --git a/tables/built-in/policy-admin.cpp b/tables/built-in/policy-admin.cpp new file mode 100644 index 0000000..55179b4 --- /dev/null +++ b/tables/built-in/policy-admin.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2019-present 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 "policy-admin.hpp" + +#include +#include +#include +#include +#include +#include + +namespace vist { +namespace table { + +namespace { + +std::string removeAlias(std::string&& alias) +{ + auto pos = alias.find(";"); + auto token = alias.substr(0, pos); + + if (token == "name") + return alias.erase(0, pos + 1); + else + return std::string(); +} + +} // anonymous namespace + +void PolicyAdminTable::Init() +{ + Builder::table("policy_admin"); +} + +TableColumns PolicyAdminTable::columns() const +{ + return { + Builder::column("name"), + Builder::column("activated"), + }; +} + +QueryData PolicyAdminTable::select(QueryContext& context) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Select query about policy-admin table."; + + QueryData results; + auto admins = vist::policy::API::Admin::GetAll(); + + for (auto& admin : admins) { + if (context.constraints["name"].exists(EQUALS)) { /// where clause + auto names = context.constraints["name"].getAll(EQUALS); + for (const auto& name : names) { + if (name == admin.first) { + Row row; + row["name"] = admin.first; + row["activated"] = std::to_string(admin.second); + + DEBUG(VIST) << "Admin info [name]: " << row["name"] + << ", [activated]:" << row["activated"]; + + results.emplace_back(std::move(row)); + } + } + } else { /// select *; + Row row; + row["name"] = admin.first; + row["activated"] = std::to_string(admin.second); + + DEBUG(VIST) << "Admin info [name]: " << row["name"] + << ", [activated]:" << row["activated"]; + results.emplace_back(std::move(row)); + } + } + + return results; + + TABLE_EXCEPTION_GUARD_END +} + +QueryData PolicyAdminTable::insert(QueryContext&, const PluginRequest& request) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Insert query about policy-admin table."; + auto admin = Parser::column(request, 0); + + DEBUG(VIST) << "Admin info [name]: " << admin; + vist::policy::API::Admin::Enroll(admin); + + return success(); + + TABLE_EXCEPTION_GUARD_END +} + +QueryData PolicyAdminTable::delete_(QueryContext&, const PluginRequest& request) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Delete query about policy-admin table."; + auto admin = removeAlias(Parser::column(request, 0)); + + DEBUG(VIST) << "Admin info [name]: " << admin; + vist::policy::API::Admin::Disenroll(admin); + + return success(); + + TABLE_EXCEPTION_GUARD_END +} + +QueryData PolicyAdminTable::update(QueryContext&, const PluginRequest& request) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Update query about policy-admin table."; + + auto name = Parser::column(request, 0); + auto activated = Parser::column(request, 1); + + vist::policy::API::Admin::Activate(name, activated); + + return success(); + + TABLE_EXCEPTION_GUARD_END +} + +} // namespace tables +} // namespace osquery diff --git a/tables/built-in/policy-admin.hpp b/tables/built-in/policy-admin.hpp new file mode 100644 index 0000000..6112f8c --- /dev/null +++ b/tables/built-in/policy-admin.hpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020-present 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 + +namespace vist { +namespace table { + +using namespace osquery; + +class PolicyAdminTable final : public TablePlugin { +public: + static void Init(); + +private: + TableColumns columns() const override; + QueryData select(QueryContext&) override; + QueryData delete_(QueryContext&, const PluginRequest& request) override; + QueryData insert(QueryContext&, const PluginRequest& request) override; + QueryData update(QueryContext&, const PluginRequest& request) override; +}; + +} // namespace table +} // namespace vist diff --git a/tables/built-in/policy.cpp b/tables/built-in/policy.cpp new file mode 100644 index 0000000..e3526a5 --- /dev/null +++ b/tables/built-in/policy.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019-present 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 "policy.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace vist { +namespace table { + +namespace { + +Row convert(const std::string& name, const vist::policy::PolicyValue& value) +{ + Row r; + r["name"] = name; + r["value"] = value.dump(); + + return r; +} + +} // anonymous namespace + +void PolicyTable::Init() +{ + Builder::table("policy"); +} + +TableColumns PolicyTable::columns() const +{ + return { + Builder::column("name"), + Builder::column("value") + }; +} + +QueryData PolicyTable::select(QueryContext& context) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Select query about policy table."; + + QueryData results; + if (context.constraints["name"].exists(EQUALS)) { /// where clause + auto names = context.constraints["name"].getAll(EQUALS); + for (const auto& name : names) { + auto value = vist::policy::API::Get(name); + auto row = convert(name, value); + + results.emplace_back(std::move(row)); + } + } else { /// select *; + auto policies = vist::policy::API::GetAll(); + for (auto& policy : policies) { + auto row = convert(policy.first, policy.second); + + results.emplace_back(std::move(row)); + } + } + + return results; + + TABLE_EXCEPTION_GUARD_END +} + +QueryData PolicyTable::update(QueryContext&, const PluginRequest& request) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Update query about policy table."; + + auto name = Parser::column(request, 0); + auto dumpedValue = Parser::column(request, 1); + + vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(dumpedValue, true)); + + return success(); + + TABLE_EXCEPTION_GUARD_END +} + +} // namespace table +} // namespace vist diff --git a/tables/built-in/policy.hpp b/tables/built-in/policy.hpp new file mode 100644 index 0000000..7bdcd4f --- /dev/null +++ b/tables/built-in/policy.hpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020-present 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 + +namespace vist { +namespace table { + +using namespace osquery; + +class PolicyTable final : public TablePlugin { +public: + static void Init(); + +private: + TableColumns columns() const override; + QueryData select(QueryContext&) override; + QueryData update(QueryContext&, const PluginRequest& request) override; +}; + +} // namespace table +} // namespace vist diff --git a/tables/policy/bluetooth/CMakeLists.txt b/tables/policy/bluetooth/CMakeLists.txt new file mode 100644 index 0000000..d5685bc --- /dev/null +++ b/tables/policy/bluetooth/CMakeLists.txt @@ -0,0 +1,51 @@ +# Copyright (c) 2019-present 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. +# +SET(TARGET "vist-bluetooth-policy") +SET(TARGET_TEST ${TARGET}-test) + +SET(DEPENDENCY bluetooth-api + capi-network-bluetooth) + +PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) + +INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS}) + +## LIB ########################################### +ADD_LIBRARY(${TARGET} SHARED policy.cpp table.cpp) +TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES} + ${TARGET_VIST_COMMON_LIB} + ${TARGET_VIST_POLICY_LIB}) + +INSTALL(TARGETS ${TARGET} DESTINATION ${TABLE_INSTALL_DIR}) + +## TEST ############################################# +ADD_EXECUTABLE(${TARGET_TEST} ${VIST_SRCS}/main/tests.cpp + policy.cpp + table.cpp + tests/bluetooth.cpp) +TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES} + ${TARGET_VIST_CLIENT_LIB} + ${TARGET_VIST_LIB} + gtest + pthread) +INSTALL(TARGETS ${TARGET_TEST} + DESTINATION ${CMAKE_INSTALL_BINDIR} + PERMISSIONS OWNER_READ + OWNER_WRITE + OWNER_EXECUTE + GROUP_READ + GROUP_EXECUTE + WORLD_READ + WORLD_EXECUTE) diff --git a/tables/policy/bluetooth/policy.cpp b/tables/policy/bluetooth/policy.cpp new file mode 100644 index 0000000..49ba0b4 --- /dev/null +++ b/tables/policy/bluetooth/policy.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2019-present 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 "policy.hpp" + +#include +#include + +#include +#include +#include + +namespace vist { +namespace policy { + +namespace { + +bool failed(int value) +{ + return value == BLUETOOTH_DPM_RESULT_ACCESS_DENIED || value == BLUETOOTH_DPM_RESULT_FAIL; +} + +auto allowed(int value) +{ + return value ? BLUETOOTH_DPM_ALLOWED : BLUETOOTH_DPM_RESTRICTED; +} + +auto bt_allowed(int value) +{ + return value ? BLUETOOTH_DPM_BT_ALLOWED : BLUETOOTH_DPM_BT_RESTRICTED; +} + +} // anonymous namespace + +void BluetoothState::onChanged(const PolicyValue& value) +{ + auto enable = bt_allowed(value); + auto ret = ::bluetooth_dpm_set_allow_mode(enable); + if (failed(ret)) + THROW(ErrCode::RuntimeError) << "Failed to change bluetooth state: " << ret; + + INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable; +} + +void DesktopConnectivity::onChanged(const PolicyValue& value) +{ + auto enable = allowed(value); + auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable); + if (failed(ret)) + THROW(ErrCode::RuntimeError) << "Failed to change bt_desktop_connectivity: " << ret; + + INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable; +} + +void Pairing::onChanged(const PolicyValue& value) +{ + auto enable = allowed(value); + auto ret = ::bluetooth_dpm_set_pairing_state(enable); + if (failed(ret)) + THROW(ErrCode::RuntimeError) << "Failed to change bluetooth pairing: " << ret; + + INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable; +} + +void Tethering::onChanged(const PolicyValue& value) +{ + auto enable = value; + INFO(VIST_PLUGIN) << "Bluetooth tethering state is changed to " << enable; +} + +Bluetooth::Bluetooth(const std::string& name) : PolicyProvider(name) +{ + if (::bt_initialize() != BT_ERROR_NONE) + THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider."; +} + +Bluetooth::~Bluetooth() +{ + ::bt_deinitialize(); +} + +} // namespace policy +} // namespace vist diff --git a/tables/policy/bluetooth/policy.hpp b/tables/policy/bluetooth/policy.hpp new file mode 100644 index 0000000..3c927df --- /dev/null +++ b/tables/policy/bluetooth/policy.hpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019-present 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 + */ + +#pragma once + +#include +#include +#include + +namespace vist { +namespace policy { + +class BluetoothState : public PolicyModel { +public: + BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {} + void onChanged(const PolicyValue& value) override; +}; + +class DesktopConnectivity : public PolicyModel { +public: + DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {} + void onChanged(const PolicyValue& value) override; +}; + +class Pairing: public PolicyModel { +public: + Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {} + void onChanged(const PolicyValue& value) override; +}; + +class Tethering: public PolicyModel { +public: + Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {} + void onChanged(const PolicyValue&); +}; + +class Bluetooth final : public PolicyProvider { +public: + Bluetooth(const std::string& name); + ~Bluetooth(); +}; + +} // namespace policy +} // namespace vist diff --git a/tables/policy/bluetooth/table.cpp b/tables/policy/bluetooth/table.cpp new file mode 100644 index 0000000..6bedb72 --- /dev/null +++ b/tables/policy/bluetooth/table.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2020-present 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 "policy.hpp" +#include "table.hpp" + +#include +#include +#include +#include +#include +#include + +extern "C" vist::table::DynamicTable* DynamicTableFactory() +{ + return new vist::table::BluetoothTable; +} + +namespace vist { +namespace table { + +namespace { + +std::map ALIAS = { + { "state", "bluetooth" }, + { "desktopConnectivity", "bluetooth-desktop-connectivity" }, + { "pairing", "bluetooth-pairing" }, + { "tethering", "bluetooth-tethering"} +}; + +void setPolicy(const std::string& name, int value) +{ + vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value)); +} + +} // anonymous namespace + +void BluetoothTable::init() +{ + Builder::table("bluetooth"); + + auto provider = std::make_shared("bluetooth"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + + policy::API::Admin::AddProvider(provider); + + INFO(VIST_PLUGIN) << "Bluetooth plugin loaded."; +} + +TableColumns BluetoothTable::columns() const +{ + return { + Builder::column("state"), + Builder::column("desktopConnectivity"), + Builder::column("pairing"), + Builder::column("tethering") + }; +} + +QueryData BluetoothTable::select(QueryContext&) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Select query about bluetooth table."; + + Row row; + for (const auto& [schemaName, policyName] : ALIAS) { + int value = vist::policy::API::Get(policyName); + row[schemaName] = std::to_string(value); + } + + QueryData results; + results.emplace_back(std::move(row)); + + return results; + + TABLE_EXCEPTION_GUARD_END +} + +QueryData BluetoothTable::update(QueryContext&, const PluginRequest& request) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Update query about bluetooth table."; + + /// TODO(Sangwan): Sync vtab schema with policy definition + setPolicy("bluetooth", Parser::column(request, 0)); + setPolicy("bluetooth-desktop-connectivity", Parser::column(request, 1)); + setPolicy("bluetooth-pairing", Parser::column(request, 2)); + setPolicy("bluetooth-tethering", Parser::column(request, 3)); + + return success(); + + TABLE_EXCEPTION_GUARD_END +} + +} // namespace table +} // namespace vist diff --git a/tables/policy/bluetooth/table.hpp b/tables/policy/bluetooth/table.hpp new file mode 100644 index 0000000..0acfde2 --- /dev/null +++ b/tables/policy/bluetooth/table.hpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020-present 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 + */ +/* + * Query example + * - SELECT * FROM bluetooth + * - UPDATE bluetooth SET state = 1 // enable + * - UPDATE bluetooth SET state = 0 // disable + */ + +#include + +namespace vist { +namespace table { + +class BluetoothTable final : public DynamicTable { +public: + void init(); + +private: + TableColumns columns() const override; + QueryData select(QueryContext&) override; + QueryData update(QueryContext&, const PluginRequest& request) override; +}; + +} // namespace table +} // namespace vist diff --git a/tables/policy/bluetooth/tests/bluetooth.cpp b/tables/policy/bluetooth/tests/bluetooth.cpp new file mode 100644 index 0000000..d682805 --- /dev/null +++ b/tables/policy/bluetooth/tests/bluetooth.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2019-present 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 "../policy.hpp" + +#include +#include +#include +#include + +#include + +#include + +using namespace vist; +using namespace vist::policy; + +namespace { + +template +void change_policy_state() +{ + std::shared_ptr policy = std::make_shared(); + /// enable policy + policy->onChanged(PolicyValue(1)); + /// disable policy + policy->onChanged(PolicyValue(0)); +} + +} // anonymous namespace + +TEST(BluetoothTests, change_policy_state) +{ + try { + change_policy_state(); + change_policy_state(); + change_policy_state(); + change_policy_state(); + } catch(const vist::Exception& e) { + EXPECT_TRUE(false) << e.what(); + } +} + +TEST(BluetoothTests, get_policies) +{ + auto rows = Query::Execute("SELECT * FROM bluetooth"); + + EXPECT_TRUE(rows.size() == 1); +} + +TEST(BluetoothTests, set_policies) +{ + // Binany should be enrolled as admin to control policy. + Query::Execute("INSERT INTO policy_admin (name) VALUES ('vist-bluetooth-policy-test')"); + + Query::Execute("UPDATE bluetooth SET desktopConnectivity = 3, state = 7"); + Query::Execute("UPDATE bluetooth SET pairing = 2, tethering = 9"); + + auto rows = Query::Execute("SELECT * FROM bluetooth"); + if (rows.size() == 1) { + EXPECT_EQ(rows[0]["state"], "7"); + EXPECT_EQ(rows[0]["desktopConnectivity"], "3"); + EXPECT_EQ(rows[0]["pairing"], "2"); + EXPECT_EQ(rows[0]["tethering"], "9"); + } else { + EXPECT_TRUE(false); + } + + Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-bluetooth-policy-test'"); +} diff --git a/tables/policy/sample/CMakeLists.txt b/tables/policy/sample/CMakeLists.txt new file mode 100644 index 0000000..9a1765a --- /dev/null +++ b/tables/policy/sample/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright (c) 2019-present 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. +# +SET(TARGET "vist-table-sample") + +INCLUDE_DIRECTORIES(SYSTEM) + +ADD_LIBRARY(${TARGET} SHARED table.cpp) +TARGET_LINK_LIBRARIES(${TARGET} vist-common ${TARGET_VIST_POLICY_LIB}) + +INSTALL(TARGETS ${TARGET} DESTINATION ${TABLE_INSTALL_DIR}) diff --git a/tables/policy/sample/policy.hpp b/tables/policy/sample/policy.hpp new file mode 100644 index 0000000..b9c309f --- /dev/null +++ b/tables/policy/sample/policy.hpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2020-present 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 + */ + +#pragma once + +#include +#include +#include +#include + +#include +#include + +namespace vist { +namespace policy { + +class SampleIntPolicy : public PolicyModel { +public: + SampleIntPolicy() : PolicyModel("sample_int_policy", PolicyValue(7)) + { + } + + void onChanged(const PolicyValue& value) override + { + INFO(VIST_PLUGIN) << "sample_int_policy is changed to " + << static_cast(value); + } +}; + +class SampleStrPolicy : public PolicyModel { +public: + SampleStrPolicy() : PolicyModel("sample_str_policy", PolicyValue("TEST")) + { + } + + void onChanged(const PolicyValue& value) override + { + INFO(VIST_PLUGIN) << "sample_str_policy is changed to " + << static_cast(value); + } + + int compare(const PolicyValue& lhs, const PolicyValue& rhs) const override + { + std::string lvalue = lhs; + std::string rvalue = rhs; + + INFO(VIST_PLUGIN) << "Custom compare method is called with [" << lvalue + << "], [" << rvalue << "]"; + + return lvalue.compare(rvalue); + } +}; + +class SampleProvider : public PolicyProvider { +public: + SampleProvider(const std::string& name) : PolicyProvider(name) + { + } + + ~SampleProvider() + { + } +}; + +} // namespace policy +} // namespace vist diff --git a/tables/policy/sample/table.cpp b/tables/policy/sample/table.cpp new file mode 100644 index 0000000..cdab50c --- /dev/null +++ b/tables/policy/sample/table.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2020-present 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 "policy.hpp" +#include "table.hpp" + +#include +#include +#include +#include +#include +#include + +extern "C" vist::table::DynamicTable* DynamicTableFactory() +{ + return new vist::table::SamplePolicyTable; +} + +namespace vist { +namespace table { + +void SamplePolicyTable::init() +{ + Builder::table("sample_policy"); + + // Register policy to policy-manager + using namespace policy; + auto provider = std::make_shared("sample-provider"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + + policy::API::Admin::AddProvider(std::move(provider)); + + INFO(VIST_PLUGIN) << "Sample plugin loaded."; +} + +TableColumns SamplePolicyTable::columns() const +{ + return { + Builder::column("sample_int_policy"), + Builder::column("sample_str_policy") + }; +} + +QueryData SamplePolicyTable::select(QueryContext&) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Select query about sample-policy table."; + + Row row; + int intPolicy = vist::policy::API::Get("sample_int_policy"); + row["sample_int_policy"] = std::to_string(intPolicy); + + std::string strPolicy = vist::policy::API::Get("sample_str_policy"); + row["sample_str_policy"] = strPolicy; + + QueryData results; + results.emplace_back(std::move(row)); + + return results; + + TABLE_EXCEPTION_GUARD_END +} + +QueryData SamplePolicyTable::update(QueryContext&, const PluginRequest& request) +{ + TABLE_EXCEPTION_GUARD_START + + INFO(VIST) << "Update query about sample-policy table."; + + auto intPolicy = Parser::column(request, 0); + auto strPolicy = Parser::column(request, 1); + + policy::API::Admin::Set("sample_int_policy", policy::PolicyValue(intPolicy)); + policy::API::Admin::Set("sample_str_policy", policy::PolicyValue(strPolicy)); + + return success(); + + TABLE_EXCEPTION_GUARD_END +} + +} // namespace table +} // namespace vist diff --git a/tables/policy/sample/table.hpp b/tables/policy/sample/table.hpp new file mode 100644 index 0000000..e66e862 --- /dev/null +++ b/tables/policy/sample/table.hpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020-present 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 + */ +/* + * Query example + * - SELECT * FROM sample_policy + * - UPDATE sample_policy SET sample_int_policy = 99 + * - UPDATE sample_policy SET sample_str_policy = 'TEST_VALUE' + */ + +#include +#include + +namespace vist { +namespace table { + +class SamplePolicyTable final : public DynamicTable { +public: + void init(); + +private: + TableColumns columns() const override; + QueryData select(QueryContext&) override; + QueryData update(QueryContext&, const PluginRequest& request) override; +}; + +} // namespace table +} // namespace vist