policy: Add bluetooth virtual table
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 14 Jan 2020 01:56:31 +0000 (10:56 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 25 Feb 2020 05:08:55 +0000 (14:08 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
policy/bluetooth.md
specs/tizen/bluetooth.table [new file with mode: 0644]
src/osquery/tables/tizen/bluetooth.cpp [new file with mode: 0644]
src/vist/client/query.cpp
src/vist/client/schema/bluetooth.hpp [new file with mode: 0644]
src/vist/service/vistd.cpp

index 15fa684cd7980fcc3b7c03cae35f9de1b0cecf19..a820aeab92fd059749fddb57eb0b97d457003565 100644 (file)
@@ -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 (file)
index 0000000..e4238c7
--- /dev/null
@@ -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 (file)
index 0000000..fa8ad09
--- /dev/null
@@ -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 <string>
+#include <memory>
+#include <stdexcept>
+
+#include <osquery/sql.h>
+#include <osquery/tables.h>
+
+#include <vist/policy/api.hpp>
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
+
+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<ErrCode>& 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
index dcb84bdf40527742fd2169d662f52cd55c21f500..2eedef2f817582f49cc7ae21d5b7c30f85246447 100644 (file)
@@ -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<Rows>(statement);
+       auto rows = query.invoke<Rows>(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 (file)
index 0000000..3b0a5b6
--- /dev/null
@@ -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
index 4fb156608ce7d6a7b52b46a18e9e56cdea98368d..882d9f8164d39a1d959e5fbca8297e2f22223773 100644 (file)
@@ -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