From 1f875096e0b4c4f547a690b21c3d41b6a00cbcbf Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Mon, 27 Apr 2020 18:59:55 +0900 Subject: [PATCH] Make virtual table at runtime Signed-off-by: Sangwan Kwon --- CMakeLists.txt | 6 +- plugins/bluetooth/bluetooth-test.cpp | 2 +- specs/CMakeLists.txt | 68 -------------- specs/blacklist | 5 -- specs/tizen/bluetooth.table | 10 --- specs/tizen/policy.table | 12 --- specs/tizen/policy_admin.table | 10 --- specs/utility/time.table | 26 ------ src/osquery/CMakeLists.txt | 7 +- src/osquery/tables/CMakeLists.txt | 31 ------- src/osquery/tables/tizen/tests/policy.cpp | 50 ----------- src/osquery/tables/utility/time.cpp | 89 ------------------- src/vist/CMakeLists.txt | 5 +- src/vist/client/tests/virtual-table.cpp | 51 +---------- src/vist/client/virtual-table.cpp | 18 +--- src/vist/{client => }/schema/bluetooth.hpp | 0 src/vist/{client => }/schema/policy.hpp | 0 src/vist/service/vistd.cpp | 8 ++ src/vist/table/CMakeLists.txt | 17 ++++ .../tables/tizen => vist/table}/bluetooth.cpp | 49 ++++++---- src/vist/table/bluetooth.hpp | 35 ++++++++ .../table/policy-admin.cpp} | 54 +++++++---- src/vist/table/policy-admin.hpp | 37 ++++++++ .../tables/tizen => vist/table}/policy.cpp | 51 +++++++---- .../schema/time.hpp => table/policy.hpp} | 24 +++-- 25 files changed, 225 insertions(+), 440 deletions(-) delete mode 100644 specs/CMakeLists.txt delete mode 100644 specs/blacklist delete mode 100644 specs/tizen/bluetooth.table delete mode 100644 specs/tizen/policy.table delete mode 100644 specs/tizen/policy_admin.table delete mode 100644 specs/utility/time.table delete mode 100644 src/osquery/tables/CMakeLists.txt delete mode 100644 src/osquery/tables/tizen/tests/policy.cpp delete mode 100644 src/osquery/tables/utility/time.cpp rename src/vist/{client => }/schema/bluetooth.hpp (100%) rename src/vist/{client => }/schema/policy.hpp (100%) create mode 100644 src/vist/table/CMakeLists.txt rename src/{osquery/tables/tizen => vist/table}/bluetooth.cpp (73%) create mode 100644 src/vist/table/bluetooth.hpp rename src/{osquery/tables/tizen/policy_admin.cpp => vist/table/policy-admin.cpp} (81%) create mode 100644 src/vist/table/policy-admin.hpp rename src/{osquery/tables/tizen => vist/table}/policy.cpp (74%) rename src/vist/{client/schema/time.hpp => table/policy.hpp} (57%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ce8c0be..d28a2bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,8 +49,9 @@ ADD_DEFINITIONS(-DOSQUERY_VERSION=${OSQUERY_VERSION} -DOSQUERY_BUILD_VERSION=${OSQUERY_VERSION} -DOSQUERY_BUILD_SDK_VERSION=${OSQUERY_VERSION}) -INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/src") -INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/src/osquery/include") +# Suppresse SYSTEM header's warnings: +INCLUDE_DIRECTORIES(SYSTEM "${CMAKE_SOURCE_DIR}/src") +INCLUDE_DIRECTORIES(SYSTEM "${CMAKE_SOURCE_DIR}/src/osquery/include") INCLUDE_DIRECTORIES("/usr/local/include") ENABLE_TESTING() @@ -59,7 +60,6 @@ ENABLE_TESTING() EXECUTE_PROCESS(COMMAND mkdir -p "${CMAKE_BINARY_DIR}/generated") ADD_SUBDIRECTORY(data) -ADD_SUBDIRECTORY(specs) ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(plugins) diff --git a/plugins/bluetooth/bluetooth-test.cpp b/plugins/bluetooth/bluetooth-test.cpp index 6a68d7b..1c282a8 100644 --- a/plugins/bluetooth/bluetooth-test.cpp +++ b/plugins/bluetooth/bluetooth-test.cpp @@ -18,7 +18,7 @@ #include "../test-util.hpp" #include -#include +#include #include #include diff --git a/specs/CMakeLists.txt b/specs/CMakeLists.txt deleted file mode 100644 index 56724b4..0000000 --- a/specs/CMakeLists.txt +++ /dev/null @@ -1,68 +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(OSQUERY_CODEGEN_PATH "${CMAKE_SOURCE_DIR}/tools/codegen") -SET(OSQUERY_TABLES_PATH "${CMAKE_SOURCE_DIR}") -SET(OSQUERY_GENERATED_PATH "${CMAKE_BINARY_DIR}/generated") - -SET(GENERATED_TABLES "") - -FILE(GLOB TABLE_FILES "${CMAKE_SOURCE_DIR}/specs/*.table") -FILE(GLOB TABLE_FILES_LINUX "${CMAKE_SOURCE_DIR}/specs/linux/*.table") -FILE(GLOB TABLE_FILES_UTILITY "${CMAKE_SOURCE_DIR}/specs/utility/*.table") -FILE(GLOB TABLE_FILES_TIZEN "${CMAKE_SOURCE_DIR}/specs/tizen/*.table") -LIST(APPEND TABLE_FILES ${TABLE_FILES_LINUX}) -LIST(APPEND TABLE_FILES ${TABLE_FILES_UTILITY}) -LIST(APPEND TABLE_FILES ${TABLE_FILES_TIZEN}) - -FILE(GLOB TABLE_FILES_TEMPLATES "${CMAKE_SOURCE_DIR}/tools/codegen/templates/*.in") -SET(GENERATION_DEPENDENCIES "${OSQUERY_CODEGEN_PATH}/gentable.py" - "${OSQUERY_CODEGEN_PATH}/amalgamate.py" - "${OSQUERY_TABLES_PATH}/specs/blacklist") - -LIST(APPEND GENERATION_DEPENDENCIES ${TABLE_FILES_TEMPLATES}) - -FOREACH(TABLE_FILE ${TABLE_FILES}) - SET(TABLE_FILE_GEN ${TABLE_FILE}) - STRING(REPLACE "${OSQUERY_TABLES_PATH}/specs" - "${OSQUERY_GENERATED_PATH}/tables" - TABLE_FILE_GEN - ${TABLE_FILE_GEN}) - STRING(REPLACE "linux/" "" TABLE_FILE_GEN ${TABLE_FILE_GEN}) - STRING(REPLACE "" "" TABLE_FILE_GEN ${TABLE_FILE_GEN}) - STRING(REPLACE ".table" ".cpp" TABLE_FILE_GEN ${TABLE_FILE_GEN}) - ADD_CUSTOM_COMMAND( - OUTPUT ${TABLE_FILE_GEN} - COMMAND - python "${OSQUERY_CODEGEN_PATH}/gentable.py" "${TABLE_FILE}" "${TABLE_FILE_GEN}" "$ENV{DISABLE_BLACKLIST}" - DEPENDS - ${TABLE_FILE} ${GENERATION_DEPENDENCIES} - - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") - LIST(APPEND GENERATED_TABLES ${TABLE_FILE_GEN}) -ENDFOREACH() - -SET(AMALGAMATION_FILE_GEN "${OSQUERY_GENERATED_PATH}/amalgamation.cpp") -ADD_CUSTOM_COMMAND( - OUTPUT ${AMALGAMATION_FILE_GEN} - COMMAND - python "${OSQUERY_CODEGEN_PATH}/amalgamate.py" - --templates "${OSQUERY_CODEGEN_PATH}/templates" - --sources "${OSQUERY_GENERATED_PATH}" - --output "${AMALGAMATION_FILE_GEN}" - DEPENDS - ${GENERATED_TABLES} - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") - -ADD_LIBRARY(osquery_generated_tables OBJECT "${AMALGAMATION_FILE_GEN}") diff --git a/specs/blacklist b/specs/blacklist deleted file mode 100644 index 376bf40..0000000 --- a/specs/blacklist +++ /dev/null @@ -1,5 +0,0 @@ -# osquery/tables/specs/blacklist -# Usage: add table spec names to this list to prevent table generation -# Example: add tables that are not yet ready for release - -quarantine diff --git a/specs/tizen/bluetooth.table b/specs/tizen/bluetooth.table deleted file mode 100644 index a803826..0000000 --- a/specs/tizen/bluetooth.table +++ /dev/null @@ -1,10 +0,0 @@ -table_name("bluetooth") -description("The policies related bluetooth.") -schema([ - Column("state", INTEGER, "The policy value about bluetooth state"), - Column("desktopConnectivity", INTEGER, "The policy value about bt desktop connectivity"), - Column("pairing", INTEGER, "The policy value about bluetooth pairing"), - Column("tethering", INTEGER, "The policy value about bluetooth tethering"), -]) -implementation("tizen/bluetooth@genBluetooth") -implementation_update("tizen/bluetooth@updateBluetooth") diff --git a/specs/tizen/policy.table b/specs/tizen/policy.table deleted file mode 100644 index 32eaa4f..0000000 --- a/specs/tizen/policy.table +++ /dev/null @@ -1,12 +0,0 @@ -table_name("policy") -description("Device polices.") -schema([ - Column("name", TEXT, "Policy name"), - Column("value", TEXT, "Policy value"), -]) -implementation("tizen/policy@genPolicy") -implementation_update("tizen/policy@updatePolicy") -examples([ - "select * from policy where name = 'bluetooth'", - "update policy set value = 'I/1' where name = 'bluetooth'", -]) diff --git a/specs/tizen/policy_admin.table b/specs/tizen/policy_admin.table deleted file mode 100644 index 6c6ee9b..0000000 --- a/specs/tizen/policy_admin.table +++ /dev/null @@ -1,10 +0,0 @@ -table_name("policy_admin") -description("Policy administrator.") -schema([ - Column("name", TEXT, "Policy admin name"), - Column("activated", INTEGER, "State of activation"), -]) -implementation("tizen/policy_admin@genPolicyAdmin") -implementation_delete("tizen/policy_admin@deletePolicyAdmin") -implementation_insert("tizen/policy_admin@insertPolicyAdmin") -implementation_update("tizen/policy_admin@updatePolicyAdmin") diff --git a/specs/utility/time.table b/specs/utility/time.table deleted file mode 100644 index 6177722..0000000 --- a/specs/utility/time.table +++ /dev/null @@ -1,26 +0,0 @@ -table_name("time") -description("Track current date and time in the system.") -schema([ - Column("weekday", TEXT, "Current weekday in the system"), - Column("year", INTEGER, "Current year in the system"), - Column("month", INTEGER, "Current month in the system"), - Column("day", INTEGER, "Current day in the system"), - Column("hour", INTEGER, "Current hour in the system"), - Column("minutes", INTEGER, "Current minutes in the system"), - Column("seconds", INTEGER, "Current seconds in the system"), - Column("timezone", TEXT, "Current timezone in the system"), - Column("local_time", INTEGER, "Current local UNIX time in the system", - aliases=["localtime"]), - Column("local_timezone", TEXT, "Current local timezone in the system"), - Column("unix_time", INTEGER, - "Current UNIX time in the system, converted to UTC if --utc enabled"), - Column("timestamp", TEXT, "Current timestamp (log format) in the system"), - Column("datetime", TEXT, "Current date and time (ISO format) in the system", - aliases=["date_time"]), - Column("iso_8601", TEXT, "Current time (ISO format) in the system"), -]) -extended_schema(WINDOWS, [ - Column("win_timestamp", BIGINT, "Timestamp value in 100 nanosecond units."), -]) -attributes(utility=True) -implementation("time@genTime") diff --git a/src/osquery/CMakeLists.txt b/src/osquery/CMakeLists.txt index a61e584..216fd92 100644 --- a/src/osquery/CMakeLists.txt +++ b/src/osquery/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. @@ -38,12 +38,9 @@ ENDIF(DEFINED GBS_BUILD) ADD_SUBDIRECTORY(core) ADD_SUBDIRECTORY(registry) ADD_SUBDIRECTORY(sql) -ADD_SUBDIRECTORY(tables) ADD_SUBDIRECTORY(utils) -ADD_LIBRARY(${TARGET_OSQUERY_LIB} - STATIC $ - ${${TARGET_OSQUERY_LIB}_SRCS}) +ADD_LIBRARY(${TARGET_OSQUERY_LIB} STATIC ${${TARGET_OSQUERY_LIB}_SRCS}) TARGET_LINK_LIBRARIES(${TARGET_OSQUERY_LIB} ${${TARGET_OSQUERY_LIB}_DEPS}) diff --git a/src/osquery/tables/CMakeLists.txt b/src/osquery/tables/CMakeLists.txt deleted file mode 100644 index 0a9370d..0000000 --- a/src/osquery/tables/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 - -FILE(GLOB OSQUERY_LINUX_TABLES "*/linux/*.cpp") -ADD_OSQUERY_LIBRARY(osquery_linux_tables ${OSQUERY_LINUX_TABLES}) - -FILE(GLOB OSQUERY_CROSS_TABLES "[!t]*/*.cpp") -ADD_OSQUERY_LIBRARY(osquery_tables ${OSQUERY_CROSS_TABLES}) - -FILE(GLOB OSQUERY_CROSS_TABLES_TESTS "[!uot]*/tests/*.cpp") -ADD_OSQUERY_TEST(${OSQUERY_CROSS_TABLES_TESTS}) - -### Tizen -ADD_OSQUERY_LINK(vist-policy) - -FILE(GLOB OSQUERY_TIZEN_TABLES "tizen/*.cpp") -ADD_OSQUERY_LIBRARY(osquery_tizen_tables ${OSQUERY_TIZEN_TABLES}) - -FILE(GLOB OSQUERY_TIZEN_TESTS "tizen/tests/*.cpp") -ADD_OSQUERY_TEST(${OSQUERY_TIZEN_TESTS}) diff --git a/src/osquery/tables/tizen/tests/policy.cpp b/src/osquery/tables/tizen/tests/policy.cpp deleted file mode 100644 index 482b4a7..0000000 --- a/src/osquery/tables/tizen/tests/policy.cpp +++ /dev/null @@ -1,50 +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 - -using namespace vist; - -class PolicyTests : public testing::Test {}; - -TEST_F(PolicyTests, get_all) { - auto policies = policy::API::GetAll(); - - EXPECT_TRUE(policies.size() > 0); -} - -TEST_F(PolicyTests, get_admin_all) { - auto admins = policy::API::Admin::GetAll(); - EXPECT_EQ(admins.size(), 1); - - policy::API::Admin::Enroll("testAdmin"); - admins = policy::API::Admin::GetAll(); - EXPECT_EQ(admins.size(), 2); - - policy::API::Admin::Enroll("testAdmin1"); - admins = policy::API::Admin::GetAll(); - EXPECT_EQ(admins.size(), 3); - - policy::API::Admin::Disenroll("testAdmin"); - admins = policy::API::Admin::GetAll(); - EXPECT_EQ(admins.size(), 2); - - policy::API::Admin::Disenroll("testAdmin1"); - admins = policy::API::Admin::GetAll(); - EXPECT_EQ(admins.size(), 1); -} diff --git a/src/osquery/tables/utility/time.cpp b/src/osquery/tables/utility/time.cpp deleted file mode 100644 index 42565e9..0000000 --- a/src/osquery/tables/utility/time.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed in accordance with the terms specified in - * the LICENSE file found in the root directory of this source tree. - */ - -#include - -#include - -#include - -#include - -namespace osquery { - -namespace tables { - -QueryData genTime(QueryContext& context) { - Row r; - time_t local_time = getUnixTime(); - auto osquery_time = getUnixTime(); - auto osquery_timestamp = getAsciiTime(); - - // The concept of 'now' is configurable. - struct tm gmt; - gmtime_r(&local_time, &gmt); - - struct tm now; - localtime_r(&local_time, &now); - - struct tm local; - localtime_r(&local_time, &local); - local_time = std::mktime(&local); - - char weekday[10] = {0}; - strftime(weekday, sizeof(weekday), "%A", &now); - - char timezone[5] = {0}; - strftime(timezone, sizeof(timezone), "%Z", &now); - - char local_timezone[5] = {0}; - strftime(local_timezone, sizeof(local_timezone), "%Z", &local); - - char iso_8601[21] = {0}; - strftime(iso_8601, sizeof(iso_8601), "%FT%TZ", &gmt); -#ifdef WIN32 - if (context.isColumnUsed("win_timestamp")) { - FILETIME ft = {0}; - GetSystemTimeAsFileTime(&ft); - LARGE_INTEGER li = {0}; - li.LowPart = ft.dwLowDateTime; - li.HighPart = ft.dwHighDateTime; - long long int hns = li.QuadPart; - r["win_timestamp"] = BIGINT(hns); - } -#endif - r["weekday"] = SQL_TEXT(weekday); - r["year"] = INTEGER(now.tm_year + 1900); - r["month"] = INTEGER(now.tm_mon + 1); - r["day"] = INTEGER(now.tm_mday); - r["hour"] = INTEGER(now.tm_hour); - r["minutes"] = INTEGER(now.tm_min); - r["seconds"] = INTEGER(now.tm_sec); - r["timezone"] = SQL_TEXT(timezone); - if (r["timezone"].empty()) { - r["timezone"] = "UTC"; - } - - r["local_time"] = INTEGER(local_time); - r["local_timezone"] = SQL_TEXT(local_timezone); - if (r["local_timezone"].empty()) { - r["local_timezone"] = "UTC"; - } - - r["unix_time"] = INTEGER(osquery_time); - r["timestamp"] = SQL_TEXT(osquery_timestamp); - // Date time is provided in ISO 8601 format, then duplicated in iso_8601. - r["datetime"] = SQL_TEXT(iso_8601); - r["iso_8601"] = SQL_TEXT(iso_8601); - - QueryData results; - results.push_back(r); - return results; -} -} // namespace tables -} // namespace osquery diff --git a/src/vist/CMakeLists.txt b/src/vist/CMakeLists.txt index 3f86197..ec7a5a1 100644 --- a/src/vist/CMakeLists.txt +++ b/src/vist/CMakeLists.txt @@ -26,7 +26,7 @@ IF(DEFINED GBS_BUILD) gflags libsystemd-daemon) PKG_CHECK_MODULES(VIST_COMMON_DEPS REQUIRED ${DEPENDENCY}) - INCLUDE_DIRECTORIES(${VIST_COMMON_DEPS_INCLUDE_DIRS}) + INCLUDE_DIRECTORIES(SYSTEM ${VIST_COMMON_DEPS_INCLUDE_DIRS}) ADD_DEFINITIONS(-DTIZEN="TIZEN") ENDIF(DEFINED GBS_BUILD) @@ -62,8 +62,9 @@ ADD_SUBDIRECTORY(policy) ADD_SUBDIRECTORY(client) # service -ADD_SUBDIRECTORY(service) ADD_SUBDIRECTORY(notification) +ADD_SUBDIRECTORY(service) +ADD_SUBDIRECTORY(table) ADD_LIBRARY(${TARGET_VIST_COMMON_LIB} STATIC ${${TARGET_VIST_COMMON_LIB}_SRCS}) diff --git a/src/vist/client/tests/virtual-table.cpp b/src/vist/client/tests/virtual-table.cpp index 27814ff..03fc974 100644 --- a/src/vist/client/tests/virtual-table.cpp +++ b/src/vist/client/tests/virtual-table.cpp @@ -17,63 +17,14 @@ #include #include - -#include -#include - #include +#include using namespace vist; using namespace vist::schema; class VirtualTableTests : public testing::Test {}; -TEST(VirtualTableTests, time_row_at) -{ - Time result = { -1, -1, -1 }; - - VirtualRow