From 0e859c45ebe5e56979a1721eb6fb4b4d824a4139 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Tue, 14 Jan 2020 10:56:31 +0900 Subject: [PATCH] policy: Add bluetooth virtual table Signed-off-by: Sangwan Kwon --- policy/bluetooth.md | 8 +-- specs/tizen/bluetooth.table | 9 ++++ src/osquery/tables/tizen/bluetooth.cpp | 69 ++++++++++++++++++++++++++ src/vist/client/query.cpp | 9 +++- src/vist/client/schema/bluetooth.hpp | 39 +++++++++++++++ src/vist/service/vistd.cpp | 2 +- 6 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 specs/tizen/bluetooth.table create mode 100644 src/osquery/tables/tizen/bluetooth.cpp create mode 100644 src/vist/client/schema/bluetooth.hpp diff --git a/policy/bluetooth.md b/policy/bluetooth.md index 15fa684..a820aea 100644 --- a/policy/bluetooth.md +++ b/policy/bluetooth.md @@ -3,7 +3,7 @@ | Table | Column | Type | Value | |---|---|---|---| | bluetooth | state | int | 0 = off , 1 = on | -| | desktop_connectivity | int | 0 = off , 1 = on | +| | desktopConnectivity | int | 0 = off , 1 = on | | | tethering | int | 0 = off , 1 = on | | | pairing | int | 0 = off , 1 = on | @@ -11,7 +11,7 @@ ```sql SELECT * FROM bluetooth SELECT state FROM bluetooth - SELECT desktop_connectivity FROM bluetooth + SELECT desktopConnectivity FROM bluetooth SELECT pairing FROM bluetooth SELECT tethering FROM bluetooth ``` @@ -19,7 +19,7 @@ ## Set bluetooth policies ```sql UPDATE bluetooth SET state = 1 # on - UPDATE bluetooth SET desktop_connectivity = 0 # off + UPDATE bluetooth SET desktopConnectivity = 0 # off UPDATE bluetooth SET pairing = 1 # on UPDATE bluetooth SET tethering = 0 # off UPDATE bluetooth SET state = 1, pairing = 0 @@ -35,7 +35,7 @@ int tethering; DECLARE_COLUMN(State, "state", &Bluetooth::state); - DECLARE_COLUMN(DesktopConnectivity, "desktop_connectivity", &Bluetooth::DesktopConnectivity); + DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::DesktopConnectivity); DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing); DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering); }; diff --git a/specs/tizen/bluetooth.table b/specs/tizen/bluetooth.table new file mode 100644 index 0000000..e4238c7 --- /dev/null +++ b/specs/tizen/bluetooth.table @@ -0,0 +1,9 @@ +table_name("bluetooth") +description("The policies related bluetooth.") +schema([ + Column("bluetooth", INTEGER, "The policy value about bluetooth state"), + Column("bluetooth-desktop-connectivity", INTEGER, "The policy value about bt desktop connectivity"), + Column("bluetooth-pairing", INTEGER, "The policy value about bluetooth pairing"), + Column("bluetooth-tethering", INTEGER, "The policy value about bluetooth tethering"), +]) +implementation("tizen/bluetooth@genBluetooth") diff --git a/src/osquery/tables/tizen/bluetooth.cpp b/src/osquery/tables/tizen/bluetooth.cpp new file mode 100644 index 0000000..fa8ad09 --- /dev/null +++ b/src/osquery/tables/tizen/bluetooth.cpp @@ -0,0 +1,69 @@ +/* + * 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 +#include +#include + +#include +#include + +#include +#include +#include + +namespace osquery { + +namespace { + +void getPolicy(Row& row, const std::string& name) +{ + int value = vist::policy::API::Get(name); + row[name] = std::to_string(value); +} + +} // anonymous namespace + +namespace tables { + +using namespace vist; + +QueryData genBluetooth(QueryContext& context) try { + INFO(VIST) << "Select query about policy table."; + + QueryData results; + + Row row; + getPolicy(row, "bluetooth"); + getPolicy(row, "bluetooth-desktop-connectivity"); + getPolicy(row, "bluetooth-pairing"); + getPolicy(row, "bluetooth-tethering"); + + results.emplace_back(std::move(row)); + + return results; +} catch (const vist::Exception& e) { + ERROR(VIST) << "Failed to query: " << e.what(); + Row r; + return { r }; +} catch (...) { + ERROR(VIST) << "Failed to query with unknown exception."; + Row r; + return { r }; +} + +} // namespace tables +} // namespace osquery diff --git a/src/vist/client/query.cpp b/src/vist/client/query.cpp index dcb84bd..2eedef2 100644 --- a/src/vist/client/query.cpp +++ b/src/vist/client/query.cpp @@ -31,7 +31,14 @@ Rows Query::Execute(const std::string& statement) rmi::Remote remote(SOCK_ADDR); auto query = REMOTE_METHOD(remote, &Vistd::query); - return query.invoke(statement); + auto rows = query.invoke(statement); + + DEBUG(VIST_CLIENT) << "Row's size: " << rows.size(); + for (const auto& row : rows) + for (const auto& col : row) + DEBUG(VIST_CLIENT) << col.first << ", " << col.second; + + return rows; } } // namespace vist diff --git a/src/vist/client/schema/bluetooth.hpp b/src/vist/client/schema/bluetooth.hpp new file mode 100644 index 0000000..3b0a5b6 --- /dev/null +++ b/src/vist/client/schema/bluetooth.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 + */ + +#pragma once + +namespace vist { +namespace schema { + + struct Bluetooth { + int state; + int desktopConnectivity; + int pairing; + int tethering; + + DECLARE_COLUMN(State, "state", &Bluetooth::state); + DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::DesktopConnectivity); + DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing); + DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering); + }; + + DECLARE_TABLE(bluetoothTable, "bluetooth", Bluetooth::State, + Bluetooth::DesktopConnectivity, + Bluetooth::Pairing, + Bluetooth::Tethering); +} // namesapce schema +} // namesapce vist diff --git a/src/vist/service/vistd.cpp b/src/vist/service/vistd.cpp index 4fb1566..882d9f8 100644 --- a/src/vist/service/vistd.cpp +++ b/src/vist/service/vistd.cpp @@ -67,7 +67,7 @@ Rows Vistd::query(const std::string& statement) if (!sql.ok()) THROW(ErrCode::RuntimeError) << "Faild to execute query: " << sql.getMessageString(); - return std::move(sql.rows()); + return sql.rows(); } } // namespace vist -- 2.34.1