From db1fae68f2911e85bb3a0e4b7e3d34af1ba2dc3d Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Thu, 26 Dec 2019 13:47:56 +0900 Subject: [PATCH] Make policy plugins testable Signed-off-by: Sangwan Kwon --- packaging/vist.spec | 10 +- plugins/bluetooth/CMakeLists.txt | 24 +++- plugins/bluetooth/bluetooth-test.cpp | 37 +++++ plugins/bluetooth/bluetooth.cpp | 156 +++++++++++----------- plugins/bluetooth/bluetooth.hpp | 59 ++++++++ plugins/dlog.h | 56 -------- plugins/test-util.hpp | 40 ++++++ plugins/tests.cpp | 22 +++ plugins/wifi/CMakeLists.txt | 26 +++- plugins/wifi/wifi-test.cpp | 37 +++++ plugins/wifi/wifi.cpp | 138 ++++++++----------- plugins/wifi/wifi.hpp | 60 +++++++++ src/osquery/tables/tizen/policy.cpp | 15 ++- src/osquery/tables/tizen/policy_admin.cpp | 31 ++++- src/vist/client/virtual-table.hpp | 8 +- 15 files changed, 483 insertions(+), 236 deletions(-) create mode 100644 plugins/bluetooth/bluetooth-test.cpp create mode 100644 plugins/bluetooth/bluetooth.hpp delete mode 100644 plugins/dlog.h create mode 100644 plugins/test-util.hpp create mode 100644 plugins/tests.cpp create mode 100644 plugins/wifi/wifi-test.cpp create mode 100644 plugins/wifi/wifi.hpp diff --git a/packaging/vist.spec b/packaging/vist.spec index ee96538..251f0bc 100644 --- a/packaging/vist.spec +++ b/packaging/vist.spec @@ -120,18 +120,12 @@ Provides internal testcases for ViST implementation. %package plugins Summary: Virtaul Security Table (policy modules) Group: Security/Other -## Common -BuildRequires: pkgconfig(buxton2) -BuildRequires: pkgconfig(dlog) -BuildRequires: pkgconfig(capi-system-info) -BuildRequires: pkgconfig(capi-base-common) -BuildRequires: pkgconfig(klay) - ## Bluetooth BuildRequires: pkgconfig(bluetooth-api) BuildRequires: pkgconfig(capi-network-bluetooth) ## Wifi +BuildRequires: pkgconfig(klay) BuildRequires: pkgconfig(capi-network-wifi-manager) BuildRequires: pkgconfig(capi-network-connection) Requires: klay @@ -143,3 +137,5 @@ Provides plugins for controlling policies. %manifest packaging/%{name}-plugins.manifest %{vist_plugin_dir}/bluetooth %{vist_plugin_dir}/wifi +%{_bindir}/vist-plugin-bluetooth-test +%{_bindir}/vist-plugin-wifi-test diff --git a/plugins/bluetooth/CMakeLists.txt b/plugins/bluetooth/CMakeLists.txt index b18a5bf..9b4e231 100644 --- a/plugins/bluetooth/CMakeLists.txt +++ b/plugins/bluetooth/CMakeLists.txt @@ -13,12 +13,12 @@ # 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 - klay) + capi-network-bluetooth) PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY}) @@ -26,8 +26,26 @@ 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}) +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 + 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 new file mode 100644 index 0000000..fcf8f3d --- /dev/null +++ b/plugins/bluetooth/bluetooth-test.cpp @@ -0,0 +1,37 @@ +/* + * 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 "../test-util.hpp" + +#include + +#include + +using namespace vist; +using namespace vist::policy::plugin; + +TEST(BluetoothTests, 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/bluetooth/bluetooth.cpp b/plugins/bluetooth/bluetooth.cpp index 32da9bb..26b3c41 100644 --- a/plugins/bluetooth/bluetooth.cpp +++ b/plugins/bluetooth/bluetooth.cpp @@ -14,100 +14,102 @@ * limitations under the License */ +#include "bluetooth.hpp" + #include #include #include -#include -#include +#include +#include #include -#include "../dlog.h" - -#define BT_FAILED(ret) \ - (((int)(ret) == BLUETOOTH_DPM_RESULT_ACCESS_DENIED) || \ - ((int)(ret) == BLUETOOTH_DPM_RESULT_FAIL)) - -#define POLICY_IS_ALLOWED(enable) \ - ((int)(enable) ? BLUETOOTH_DPM_ALLOWED : \ - BLUETOOTH_DPM_RESTRICTED) - -#define STATE_CHANGE_IS_ALLOWED(enable) \ - ((int)(enable) ? BLUETOOTH_DPM_BT_ALLOWED : \ - BLUETOOTH_DPM_BT_RESTRICTED) - -using namespace vist::policy; - -class ModeChange : public PolicyModel { -public: - ModeChange() : PolicyModel("bluetooth", PolicyValue(1)) {} - - void onChanged(const PolicyValue& value) override - { - int ret = ::bluetooth_dpm_set_allow_mode(STATE_CHANGE_IS_ALLOWED(value)); - if (BT_FAILED(ret)) - throw std::runtime_error("Failed to set bluetooth."); - } -}; - -class DesktopConnectivity : public PolicyModel { -public: - DesktopConnectivity() : - PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {} - - void onChanged(const PolicyValue& value) override - { - int ret = ::bluetooth_dpm_set_desktop_connectivity_state(POLICY_IS_ALLOWED(value)); - if (BT_FAILED(ret)) - throw std::runtime_error("Failed to set bt_desktop_connectivity."); - } -}; - -class Pairing: public PolicyModel { -public: - Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {} - - void onChanged(const PolicyValue& value) override - { - int ret = ::bluetooth_dpm_set_pairing_state(POLICY_IS_ALLOWED(value)); - if (BT_FAILED(ret)) - throw std::runtime_error("Failed to set bt_pairing."); - } -}; - -class Tethering: public PolicyModel { -public: - Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {} - - void onChanged(const PolicyValue&) {} -}; - -class Bluetooth : public PolicyProvider { -public: - Bluetooth(const std::string& name) : PolicyProvider(name) - { - if (::bt_initialize() != BT_ERROR_NONE) - ERROR(PLUGINS,"Bluetooth framework was not initilaized"); - } - - ~Bluetooth() - { - ::bt_deinitialize(); - } -}; +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(PLUGINS, "Bluetooth plugin loaded."); + 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()); 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 new file mode 100644 index 0000000..b32bf66 --- /dev/null +++ b/plugins/bluetooth/bluetooth.hpp @@ -0,0 +1,59 @@ +/* + * 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/dlog.h b/plugins/dlog.h deleted file mode 100644 index 7d85170..0000000 --- a/plugins/dlog.h +++ /dev/null @@ -1,56 +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 DLogied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -#pragma once - -#include -#include - -#define PLUGINS osquery::DLog::getSink() - -namespace osquery { - -class DLog final { -public: - DLog(const DLog&) = delete; - DLog& operator=(const DLog&) = delete; - - DLog(DLog&&) noexcept = default; - DLog& operator=(DLog&&) noexcept = default; - - static inline DLog& instance() - { - static DLog dlog; - return dlog; - } - - static inline audit::LogSink* getSink() - { - return DLog::instance().logSink.get(); - } - -private: - DLog() - { - auto dlog = new audit::DlogLogSink("VIST_PLUGIN"); - this->logSink.reset(dynamic_cast(dlog)); - } - ~DLog() noexcept = default; - - std::unique_ptr logSink; -}; - -} // namespace osquery diff --git a/plugins/test-util.hpp b/plugins/test-util.hpp new file mode 100644 index 0000000..06a278e --- /dev/null +++ b/plugins/test-util.hpp @@ -0,0 +1,40 @@ +/* + * 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 new file mode 100644 index 0000000..0fcfc89 --- /dev/null +++ b/plugins/tests.cpp @@ -0,0 +1,22 @@ +/* + * 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/wifi/CMakeLists.txt b/plugins/wifi/CMakeLists.txt index d236772..4a70348 100644 --- a/plugins/wifi/CMakeLists.txt +++ b/plugins/wifi/CMakeLists.txt @@ -13,6 +13,7 @@ # limitations under the License. # SET(TARGET "vist-plugin-wifi") +SET(TARGET_TEST ${TARGET}-test) SET(PLUGIN_SOURCES "wifi.cpp") @@ -26,8 +27,25 @@ 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}) - +TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES} + vist-common) INSTALL(FILES libvist-plugin-wifi.so - RENAME wifi - DESTINATION ${PLUGIN_INSTALL_DIR}) + 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 new file mode 100644 index 0000000..ca048fd --- /dev/null +++ b/plugins/wifi/wifi-test.cpp @@ -0,0 +1,37 @@ +/* + * 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 index 5dff58f..cb169d4 100644 --- a/plugins/wifi/wifi.cpp +++ b/plugins/wifi/wifi.cpp @@ -14,115 +14,93 @@ * limitations under the License */ -#include -#include +#include "wifi.hpp" -#include -#include +#include +#include #include #include -#include "../dlog.h" - #define NETCONFIG_INTERFACE \ "net.netconfig", \ "/net/netconfig/network", \ "net.netconfig.network" -using namespace vist::policy; - -class ModeChange : public PolicyModel { -public: - ModeChange() : PolicyModel("wifi", PolicyValue(1)) {} - - void onChanged(const PolicyValue& value) override - { - int enable = value; - klay::dbus::Connection &systemDBus = klay::dbus::Connection::getSystem(); - systemDBus.methodcall(NETCONFIG_INTERFACE, - "DevicePolicySetWifi", - -1, - "", - "(i)", - enable); - } -}; - -class ProfileChange : public PolicyModel { -public: - ProfileChange() : PolicyModel("wifi-profile-change", PolicyValue(1)) {} - - void onChanged(const PolicyValue& value) override - { - int enable = value; - dbus::Connection &systemDBus = dbus::Connection::getSystem(); - systemDBus.methodcall(NETCONFIG_INTERFACE, - "DevicePolicySetWifiProfile", - -1, - "", - "(i)", - enable); - } -}; +namespace vist { +namespace policy { +namespace plugin { -class Hotspot : public PolicyModel { -public: - Hotspot() : PolicyModel("wifi-hotspot", PolicyValue(1)) {} +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 onChanged(const PolicyValue&) override - { - /// N/A - } -}; +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); +} -class SsidRestriction : public PolicyModel { -public: - SsidRestriction() : PolicyModel("wifi-ssid-restriction", PolicyValue(0)) {} +void Hotspot::onChanged(const PolicyValue& value) +{ + int enable = value; + INFO(VIST_PLUGIN) << "Wifi hotspot change state is changed to " << enable; +} - void onChanged(const PolicyValue&) override - { - /// N/A - } -}; - -class Wifi : public PolicyProvider { -public: - 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) - return; - - throw std::runtime_error("WiFi Manager initialization failed."); - } - } +void SsidRestriction::onChanged(const PolicyValue& value) +{ + int enable = value; + INFO(VIST_PLUGIN) << "Wifi ssid restriction change state is changed to " << enable; +} - ~Wifi() - { - if (handle == nullptr) - return; +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."); - ::wifi_manager_deinitialize(handle); + THROW(ErrCode::RuntimeError) << "Failed to init WiFi Manager."; } +} -private: - ::wifi_manager_h handle = nullptr; -}; +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(PLUGINS, "Wifi plugin loaded."); + 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()); 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 new file mode 100644 index 0000000..b076cbb --- /dev/null +++ b/plugins/wifi/wifi.hpp @@ -0,0 +1,60 @@ +/* + * 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/osquery/tables/tizen/policy.cpp b/src/osquery/tables/tizen/policy.cpp index 7ba5544..3d8041a 100644 --- a/src/osquery/tables/tizen/policy.cpp +++ b/src/osquery/tables/tizen/policy.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace osquery { @@ -41,6 +42,8 @@ Row convert(const std::string& name, const vist::policy::PolicyValue& value) namespace tables { +using namespace vist; + QueryData genPolicy(QueryContext& context) try { INFO(VIST) << "Select query about policy table."; @@ -63,8 +66,12 @@ QueryData genPolicy(QueryContext& context) try { } return results; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; } catch (...) { - ERROR(VIST) << "Failed to select query on policy."; + ERROR(VIST) << "Failed to query with unknown exception."; Row r; return { r }; } @@ -91,8 +98,12 @@ QueryData updatePolicy(QueryContext& context, const PluginRequest& request) try Row r; r["status"] = "success"; return { r }; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; } catch (...) { - ERROR(VIST) << "Failed to update query on policy."; + ERROR(VIST) << "Failed to query with unknown exception."; Row r; return { r }; } diff --git a/src/osquery/tables/tizen/policy_admin.cpp b/src/osquery/tables/tizen/policy_admin.cpp index 66aafe0..58a67f3 100644 --- a/src/osquery/tables/tizen/policy_admin.cpp +++ b/src/osquery/tables/tizen/policy_admin.cpp @@ -22,8 +22,11 @@ #include #include +#include #include +namespace osquery { + namespace { std::string getValue(std::string&& alias, const std::string& key) @@ -52,9 +55,10 @@ std::string parseAdmin(const std::string& request, bool insert = true) } // anonymous namespace -namespace osquery { namespace tables { +using namespace vist; + QueryData genPolicyAdmin(QueryContext& context) try { INFO(VIST) << "Select query about policy-admin table."; @@ -88,8 +92,12 @@ QueryData genPolicyAdmin(QueryContext& context) try { } return results; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; } catch (...) { - ERROR(VIST) << "Failed to select query on policy-admin."; + ERROR(VIST) << "Failed to query with unknown exception."; Row r; return { r }; } @@ -106,8 +114,12 @@ QueryData insertPolicyAdmin(QueryContext& context, const PluginRequest& request) Row r; r["status"] = "success"; return { r }; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; } catch (...) { - ERROR(VIST) << "Failed to insert query on policy-admin."; + ERROR(VIST) << "Failed to query with unknown exception."; Row r; return { r }; } @@ -124,8 +136,12 @@ QueryData deletePolicyAdmin(QueryContext& context, const PluginRequest& request) Row r; r["status"] = "success"; return { r }; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; } catch (...) { - ERROR(VIST) << "Failed to delete query on policy-admin."; + ERROR(VIST) << "Failed to query with unknown exception."; Row r; return { r }; } @@ -152,11 +168,16 @@ QueryData updatePolicyAdmin(QueryContext& context, const PluginRequest& request) Row r; r["status"] = "success"; return { r }; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; } catch (...) { - ERROR(VIST) << "Failed to insert query on policy-admin."; + ERROR(VIST) << "Failed to query with unknown exception."; Row r; return { r }; } + } // namespace tables } // namespace osquery diff --git a/src/vist/client/virtual-table.hpp b/src/vist/client/virtual-table.hpp index a64077a..6490941 100644 --- a/src/vist/client/virtual-table.hpp +++ b/src/vist/client/virtual-table.hpp @@ -16,6 +16,8 @@ #pragma once +#include + #include #include #include @@ -59,11 +61,13 @@ public: inline std::size_t size() const { return rows.size(); } template - VirtualRow& filter(Member Struct::*field, const std::string& value) + VirtualRow& filter(Member Struct::*field, const std::string& name) { for (auto& row : this->rows) - if (row[field] == value) + if (row[field] == name) return row; + + THROW(ErrCode::RuntimeError) << "Not exist field: " << name; } private: -- 2.34.1