From 83018ea0e6d95a348e6f67fbe32f015f61349218 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Wed, 5 Aug 2020 15:39:27 +0900 Subject: [PATCH] Cleanup unnecessary osquery code Change-Id: I58e8249f934e3347c6696f08aa3d8a7f5d33fe54 Signed-off-by: Sangwan Kwon --- CMakeLists.txt | 5 -- src/osquery/CMakeLists.txt | 1 - src/osquery/core/CMakeLists.txt | 3 +- src/osquery/core/sql/scheduled_query.cpp | 9 --- src/osquery/core/sql/scheduled_query.h | 76 ------------------ src/osquery/include/osquery/core.h | 2 - src/osquery/include/osquery/query.h | 13 ---- src/osquery/utils/CMakeLists.txt | 9 +-- src/osquery/utils/attribute.h | 21 ----- src/osquery/utils/base64.cpp | 71 ----------------- src/osquery/utils/base64.h | 35 --------- src/osquery/utils/chars.cpp | 76 ------------------ src/osquery/utils/chars.h | 56 -------------- src/osquery/utils/conversions/join.h | 31 -------- src/osquery/utils/conversions/tests/join.cpp | 27 ------- src/osquery/utils/error/error.h | 5 +- src/osquery/utils/macros/macros.h | 31 -------- src/osquery/utils/mutex.h | 7 -- src/osquery/utils/status/status.h | 10 --- src/osquery/utils/status/tests/status.cpp | 30 -------- src/osquery/utils/system/env.h | 30 -------- src/osquery/utils/system/errno.h | 18 ----- src/osquery/utils/system/filepath.h | 18 ----- src/osquery/utils/system/posix/env.cpp | 40 ---------- src/osquery/utils/system/posix/errno.cpp | 55 ------------- src/osquery/utils/system/posix/errno.h | 69 ----------------- src/osquery/utils/system/posix/filepath.cpp | 47 ----------- .../utils/system/posix/tests/errno.cpp | 34 -------- src/osquery/utils/system/posix/time.cpp | 26 ------- src/osquery/utils/system/tests/cpu.cpp | 7 -- src/osquery/utils/system/tests/errno.cpp | 7 -- src/osquery/utils/system/tests/time.cpp | 36 --------- src/osquery/utils/system/time.cpp | 77 ------------------- src/osquery/utils/system/time.h | 62 --------------- 34 files changed, 4 insertions(+), 1040 deletions(-) delete mode 100644 src/osquery/core/sql/scheduled_query.cpp delete mode 100644 src/osquery/core/sql/scheduled_query.h delete mode 100644 src/osquery/utils/attribute.h delete mode 100644 src/osquery/utils/base64.cpp delete mode 100644 src/osquery/utils/base64.h delete mode 100644 src/osquery/utils/chars.cpp delete mode 100644 src/osquery/utils/chars.h delete mode 100644 src/osquery/utils/conversions/join.h delete mode 100644 src/osquery/utils/conversions/tests/join.cpp delete mode 100644 src/osquery/utils/macros/macros.h delete mode 100644 src/osquery/utils/system/env.h delete mode 100644 src/osquery/utils/system/errno.h delete mode 100644 src/osquery/utils/system/filepath.h delete mode 100644 src/osquery/utils/system/posix/env.cpp delete mode 100644 src/osquery/utils/system/posix/errno.cpp delete mode 100644 src/osquery/utils/system/posix/errno.h delete mode 100644 src/osquery/utils/system/posix/filepath.cpp delete mode 100644 src/osquery/utils/system/posix/tests/errno.cpp delete mode 100644 src/osquery/utils/system/posix/time.cpp delete mode 100644 src/osquery/utils/system/tests/cpu.cpp delete mode 100644 src/osquery/utils/system/tests/errno.cpp delete mode 100644 src/osquery/utils/system/tests/time.cpp delete mode 100644 src/osquery/utils/system/time.cpp delete mode 100644 src/osquery/utils/system/time.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0fd59d2..a188cc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,11 +28,6 @@ IF(NOT CMAKE_BUILD_TYPE) SET(CMAKE_BUILD_TYPE "DEBUG") ENDIF(NOT CMAKE_BUILD_TYPE) -message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") -message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") -message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") -message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") - SET(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++1z -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++1z -O2 -DNDEBUG") diff --git a/src/osquery/CMakeLists.txt b/src/osquery/CMakeLists.txt index 216fd92..64aa02e 100644 --- a/src/osquery/CMakeLists.txt +++ b/src/osquery/CMakeLists.txt @@ -23,7 +23,6 @@ ADD_OSQUERY_LINK(glog dl boost_regex boost_system - boost_thread boost_filesystem sqlite3) diff --git a/src/osquery/core/CMakeLists.txt b/src/osquery/core/CMakeLists.txt index 9416338..7707d15 100644 --- a/src/osquery/core/CMakeLists.txt +++ b/src/osquery/core/CMakeLists.txt @@ -17,8 +17,7 @@ ADD_OSQUERY_LIBRARY(osquery_core tables.cpp plugins/plugin.cpp plugins/sql.cpp sql/column.cpp - sql/diff_results.cpp - sql/scheduled_query.cpp) + sql/diff_results.cpp) FILE(GLOB OSQUERY_CORE_TESTS "tests/*.cpp") ADD_OSQUERY_TEST(${OSQUERY_SQL_TESTS}) diff --git a/src/osquery/core/sql/scheduled_query.cpp b/src/osquery/core/sql/scheduled_query.cpp deleted file mode 100644 index c2847a3..0000000 --- a/src/osquery/core/sql/scheduled_query.cpp +++ /dev/null @@ -1,9 +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 "scheduled_query.h" diff --git a/src/osquery/core/sql/scheduled_query.h b/src/osquery/core/sql/scheduled_query.h deleted file mode 100644 index 8dd928a..0000000 --- a/src/osquery/core/sql/scheduled_query.h +++ /dev/null @@ -1,76 +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. - */ - -#pragma once - -#include -#include - -#include - -namespace osquery { - -/** - * @brief Represents the relevant parameters of a scheduled query. - * - * Within the context of osqueryd, a scheduled query may have many relevant - * attributes. Those attributes are represented in this data structure. - */ -struct ScheduledQuery : private only_movable { - /// Name of the pack containing query - std::string pack_name; - - /// Name of the query - std::string name; - - /// The SQL query. - std::string query; - - /// Owner of the query - std::string oncall; - - /// How often the query should be executed, in second. - size_t interval{0}; - - /// A temporary splayed internal. - size_t splayed_interval{0}; - - /** - * @brief Queries are blacklisted based on logic in the configuration. - * - * Most calls to inspect scheduled queries will abstract away the blacklisting - * concept and only return non-blacklisted queries. The config may be asked - * to return all queries, thus it is important to capture this optional data. - */ - bool blacklisted{false}; - - /// Set of query options. - std::map options; - - ScheduledQuery(const std::string& pack_name, - const std::string& name, - const std::string& query) - : pack_name(pack_name), name(name), query(query) {} - ScheduledQuery() = default; - ScheduledQuery(ScheduledQuery&&) = default; - ScheduledQuery& operator=(ScheduledQuery&&) = default; - - /// equals operator - bool operator==(const ScheduledQuery& comp) const - { - return (comp.query == query) && (comp.interval == interval); - } - - /// not equals operator - bool operator!=(const ScheduledQuery& comp) const - { - return !(*this == comp); - } -}; - -} // namespace osquery diff --git a/src/osquery/include/osquery/core.h b/src/osquery/include/osquery/core.h index 95f074e..8cc3c4f 100644 --- a/src/osquery/include/osquery/core.h +++ b/src/osquery/include/osquery/core.h @@ -10,8 +10,6 @@ #include -#include - /// A configuration error is catastrophic and should exit the watcher. #define EXIT_CATASTROPHIC 78 diff --git a/src/osquery/include/osquery/query.h b/src/osquery/include/osquery/query.h index 4dfa95c..5b41270 100644 --- a/src/osquery/include/osquery/query.h +++ b/src/osquery/include/osquery/query.h @@ -18,7 +18,6 @@ #include #include -#include namespace osquery { @@ -78,18 +77,6 @@ public: */ class Query { public: - /** - * @brief Constructor which sets up necessary parameters of a Query object. - * - * Given a query, this constructor calculates the value of columnFamily_, - * which can be accessed via the getColumnFamilyName getter method. - * - * @param name The query name. - * @param q a SheduledQuery struct. - */ - explicit Query(std::string name, const ScheduledQuery& q) - : query_(q.query), name_(std::move(name)) {} - /** * @brief Serialize the data in RocksDB into a useful data structure * diff --git a/src/osquery/utils/CMakeLists.txt b/src/osquery/utils/CMakeLists.txt index 29c2a69..189b23f 100644 --- a/src/osquery/utils/CMakeLists.txt +++ b/src/osquery/utils/CMakeLists.txt @@ -12,15 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License -ADD_OSQUERY_LIBRARY(osquery_utils base64.cpp - chars.cpp - only_movable.cpp +ADD_OSQUERY_LIBRARY(osquery_utils only_movable.cpp status/status.cpp - system/time.cpp - system/posix/env.cpp - system/posix/errno.cpp - system/posix/filepath.cpp - system/posix/time.cpp conversions/tryto.cpp conversions/split.cpp) diff --git a/src/osquery/utils/attribute.h b/src/osquery/utils/attribute.h deleted file mode 100644 index dca1b59..0000000 --- a/src/osquery/utils/attribute.h +++ /dev/null @@ -1,21 +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. - */ - -#pragma once - -#if __cplusplus >= 201703L -#define OSQUERY_NODISCARD [[nodiscard]] -#else -#if defined(POSIX) -#define OSQUERY_NODISCARD __attribute__((warn_unused_result)) -#elif defined(WIDOWS) && defined(_MSC_VER) && _MSC_VER >= 1700 -#define OSQUERY_NODISCARD _Check_return_ -#else -#define OSQUERY_NODISCARD -#endif -#endif diff --git a/src/osquery/utils/base64.cpp b/src/osquery/utils/base64.cpp deleted file mode 100644 index b5e3154..0000000 --- a/src/osquery/utils/base64.cpp +++ /dev/null @@ -1,71 +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 "base64.h" - -#include -#include -#include -#include - -#include - -namespace bai = boost::archive::iterators; - -namespace osquery { - -namespace base64 { - -namespace { - -typedef bai::binary_from_base64 base64_str; -typedef bai::transform_width base64_dec; -typedef bai::transform_width base64_enc; -typedef bai::base64_from_binary it_base64; - -} // namespace - -std::string decode(std::string encoded) -{ - boost::erase_all(encoded, "\r\n"); - boost::erase_all(encoded, "\n"); - boost::trim_right_if(encoded, boost::is_any_of("=")); - - if (encoded.empty()) { - return encoded; - } - - try { - return std::string(base64_dec(encoded.data()), - base64_dec(encoded.data() + encoded.size())); - } catch (const boost::archive::iterators::dataflow_exception& e) { - INFO(OSQUERY) << "Could not base64 decode string: " << e.what(); - return ""; - } -} - -std::string encode(const std::string& unencoded) -{ - if (unencoded.empty()) { - return unencoded; - } - - size_t writePaddChars = (3U - unencoded.length() % 3U) % 3U; - try { - auto encoded = - std::string(it_base64(unencoded.begin()), it_base64(unencoded.end())); - encoded.append(std::string(writePaddChars, '=')); - return encoded; - } catch (const boost::archive::iterators::dataflow_exception& e) { - INFO(OSQUERY) << "Could not base64 decode string: " << e.what(); - return ""; - } -} - -} // namespace base64 -} // namespace osquery diff --git a/src/osquery/utils/base64.h b/src/osquery/utils/base64.h deleted file mode 100644 index aba99d3..0000000 --- a/src/osquery/utils/base64.h +++ /dev/null @@ -1,35 +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. - */ - -#pragma once - -#include - -namespace osquery { - -namespace base64 { - -/** - * @brief Decode a base64 encoded string. - * - * @param encoded The encode base64 string. - * @return Decoded string. - */ -std::string decode(std::string encoded); - -/** - * @brief Encode a string. - * - * @param A string to encode. - * @return Encoded string. - */ -std::string encode(const std::string& unencoded); - -} // namespace base64 - -} // namespace osquery diff --git a/src/osquery/utils/chars.cpp b/src/osquery/utils/chars.cpp deleted file mode 100644 index 85d2fda..0000000 --- a/src/osquery/utils/chars.cpp +++ /dev/null @@ -1,76 +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 -#include - -namespace osquery { - -bool isPrintable(const std::string& check) -{ - for (const unsigned char ch : check) { - if (ch >= 0x7F || ch <= 0x1F) { - return false; - } - } - return true; -} - -size_t utf8StringSize(const std::string& str) -{ - size_t res = 0; - std::string::const_iterator it = str.begin(); - for (; it != str.end(); incUtf8StringIterator(it, str.end())) { - res++; - } - - return res; -} - -std::string unescapeUnicode(const std::string& escaped) -{ - if (escaped.size() < 6) { - return escaped; - } - - std::string unescaped; - unescaped.reserve(escaped.size()); - for (size_t i = 0; i < escaped.size(); ++i) { - if (i < escaped.size() - 5 && '\\' == escaped[i] && 'u' == escaped[i + 1]) { - // Assume 2-byte wide unicode. - auto const exp = tryTo(escaped.substr(i + 2, 4), 16); - if (exp.isError()) { - WARN(OSQUERY) << "Unescaping a string with length: " << escaped.size() - << " failed at: " << i; - return ""; - } - long const value = exp.get(); - if (value < 255) { - unescaped += static_cast(value); - i += 5; - continue; - } - } else if (i < escaped.size() - 1 && '\\' == escaped[i] && - '\\' == escaped[i + 1]) { - // In the case of \\users 'sers' is not a unicode character - // If we see \\ we should skip them and we do this by adding - // an extra jump forward. - unescaped += escaped[i]; - ++i; - } - unescaped += escaped[i]; - } - return unescaped; -} - -} // namespace osquery diff --git a/src/osquery/utils/chars.h b/src/osquery/utils/chars.h deleted file mode 100644 index 0df4433..0000000 --- a/src/osquery/utils/chars.h +++ /dev/null @@ -1,56 +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. - */ - -#pragma once - -#include - -namespace osquery { - -/** - * @brief Check if a string is ASCII printable - * - * @param A string to check. - * @return If the string is printable. - */ -bool isPrintable(const std::string& check); - -/** - * @brief In-line helper function for use with utf8StringSize - */ -template -size_t incUtf8StringIterator(_Iterator1& it, const _Iterator2& last) -{ - if (it == last) { - return 0; - } - - size_t res = 1; - for (++it; last != it; ++it, ++res) { - unsigned char c = *it; - if (!(c & 0x80) || ((c & 0xC0) == 0xC0)) { - break; - } - } - - return res; -} - -/** - * @brief Get the length of a UTF-8 string - * - * @param str The UTF-8 string - * - * @return the length of the string - */ -size_t utf8StringSize(const std::string& str); - -/// Safely convert unicode escaped ASCII. -std::string unescapeUnicode(const std::string& escaped); - -} // namespace osquery diff --git a/src/osquery/utils/conversions/join.h b/src/osquery/utils/conversions/join.h deleted file mode 100644 index 7e73613..0000000 --- a/src/osquery/utils/conversions/join.h +++ /dev/null @@ -1,31 +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. - */ - -#pragma once - -#include - -#include - -namespace osquery { - -/** - * @brief Join a vector of strings inserting a token string between elements - * - * @param s the vector of strings to be joined. - * @param tok a token glue string to be inserted between elements. - * - * @return the joined string. - */ -template -inline std::string join(const SequenceType& s, const std::string& tok) -{ - return boost::algorithm::join(s, tok); -} - -} // namespace osquery diff --git a/src/osquery/utils/conversions/tests/join.cpp b/src/osquery/utils/conversions/tests/join.cpp deleted file mode 100644 index 0cb5f3d..0000000 --- a/src/osquery/utils/conversions/tests/join.cpp +++ /dev/null @@ -1,27 +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 - -namespace osquery { - -class ConversionsTests : public testing::Test {}; - -TEST_F(ConversionsTests, test_join) -{ - std::vector content = { - "one", "two", "three", - }; - EXPECT_EQ(join(content, ", "), "one, two, three"); -} - -} diff --git a/src/osquery/utils/error/error.h b/src/osquery/utils/error/error.h index 5ef5533..27649c7 100644 --- a/src/osquery/utils/error/error.h +++ b/src/osquery/utils/error/error.h @@ -8,7 +8,6 @@ #pragma once -#include #include #include @@ -137,7 +136,7 @@ inline std::ostream& operator<<(std::ostream& out, const ErrorBase& error) } template -OSQUERY_NODISCARD Error createError( +[[nodiscard]] Error createError( ErrorCodeEnumType error_code, Error underlying_error) { @@ -148,7 +147,7 @@ OSQUERY_NODISCARD Error createError( } template -OSQUERY_NODISCARD Error createError( +[[nodiscard]] Error createError( ErrorCodeEnumType error_code) { return Error(error_code); diff --git a/src/osquery/utils/macros/macros.h b/src/osquery/utils/macros/macros.h deleted file mode 100644 index 375fa11..0000000 --- a/src/osquery/utils/macros/macros.h +++ /dev/null @@ -1,31 +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. - */ - -#pragma once - -// clang-format off -#ifndef STR -#define STR_OF(x) #x -#define STR(x) STR_OF(x) -#endif - -#ifdef WIN32 -#define STR_EX(...) __VA_ARGS__ -#else -#define STR_EX(x) x -#endif -#define CONCAT(x, y) STR(STR_EX(x)STR_EX(y)) - -#ifdef WIN32 -#define USED_SYMBOL -#define EXPORT_FUNCTION __declspec(dllexport) -#else -#define USED_SYMBOL __attribute__((used)) -#define EXPORT_FUNCTION -#endif -// clang-format on diff --git a/src/osquery/utils/mutex.h b/src/osquery/utils/mutex.h index 6684f46..ea7e572 100644 --- a/src/osquery/utils/mutex.h +++ b/src/osquery/utils/mutex.h @@ -10,7 +10,6 @@ #include #include -#include namespace osquery { @@ -29,10 +28,4 @@ using RecursiveMutex = std::recursive_mutex; /// Helper alias for write locking a recursive mutex. using RecursiveLock = std::unique_lock; -/// Helper alias for upgrade locking a mutex. -using UpgradeLock = boost::upgrade_lock; - -/// Helper alias for write locking an upgrade lock. -using WriteUpgradeLock = boost::upgrade_to_unique_lock; - } // namespace osquery diff --git a/src/osquery/utils/status/status.h b/src/osquery/utils/status/status.h index d0bcb26..9dead61 100644 --- a/src/osquery/utils/status/status.h +++ b/src/osquery/utils/status/status.h @@ -9,7 +9,6 @@ #pragma once #include -#include #include #include @@ -170,13 +169,4 @@ private: ::std::ostream& operator<<(::std::ostream& os, const Status& s); -template -inline -typename std::enable_if::value, Status>::type -to(const Expected& expected) -{ - return expected ? Status::success() - : Status::failure(expected.getError().getMessage()); -} - } // namespace osquery diff --git a/src/osquery/utils/status/tests/status.cpp b/src/osquery/utils/status/tests/status.cpp index 5b4211e..5782b74 100644 --- a/src/osquery/utils/status/tests/status.cpp +++ b/src/osquery/utils/status/tests/status.cpp @@ -84,34 +84,4 @@ TEST_F(StatusTests, test_failure_with_success_code) #endif } -namespace { - -enum class TestError { - Semantic = 1, -}; - -bool stringContains(const std::string& where, const std::string& what) -{ - return boost::contains(where, what); -}; - -} // namespace - -TEST_F(StatusTests, test_expected_to_status_failure) -{ - const auto expected = Expected( - TestError::Semantic, "The ultimate failure reason"); - auto s = to(expected); - EXPECT_FALSE(s.ok()); - EXPECT_PRED2(stringContains, s.toString(), "The ultimate failure reason"); -} - -TEST_F(StatusTests, test_expected_to_status_success) -{ - const auto expected = - Expected("This is not a failure"); - auto s = to(expected); - EXPECT_TRUE(s.ok()); - EXPECT_EQ(s, Status::success()); -} } diff --git a/src/osquery/utils/system/env.h b/src/osquery/utils/system/env.h deleted file mode 100644 index 5a5e62d..0000000 --- a/src/osquery/utils/system/env.h +++ /dev/null @@ -1,30 +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. - */ - -#pragma once - -#include -#include - - -namespace osquery { - -/// Set the environment variable name with value value. -bool setEnvVar(const std::string& name, const std::string& value); - -/// Unsets the environment variable specified by name. -bool unsetEnvVar(const std::string& name); - -/** - * @brief Returns the value of the specified environment variable name. - * - * If the environment variable does not exist, boost::none is returned. - */ -boost::optional getEnvVar(const std::string& name); - -} // namespace osquery diff --git a/src/osquery/utils/system/errno.h b/src/osquery/utils/system/errno.h deleted file mode 100644 index 66298c8..0000000 --- a/src/osquery/utils/system/errno.h +++ /dev/null @@ -1,18 +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. - */ - -#pragma once - -#include - -namespace osquery { - -/// Returns a C++ string explaining the errnum -std::string platformStrerr(int errnum); - -} diff --git a/src/osquery/utils/system/filepath.h b/src/osquery/utils/system/filepath.h deleted file mode 100644 index d7a118a..0000000 --- a/src/osquery/utils/system/filepath.h +++ /dev/null @@ -1,18 +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. - */ - -#pragma once - -#include - -namespace osquery { - -/// Safer way to do realpath -const std::string canonicalize_file_name(const char* name); - -} diff --git a/src/osquery/utils/system/posix/env.cpp b/src/osquery/utils/system/posix/env.cpp deleted file mode 100644 index 6e878bc..0000000 --- a/src/osquery/utils/system/posix/env.cpp +++ /dev/null @@ -1,40 +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 { - -bool setEnvVar(const std::string& name, const std::string& value) -{ - auto ret = ::setenv(name.c_str(), value.c_str(), 1); - return (ret == 0); -} - -bool unsetEnvVar(const std::string& name) -{ - auto ret = ::unsetenv(name.c_str()); - return (ret == 0); -} - -boost::optional getEnvVar(const std::string& name) -{ - char* value = ::getenv(name.c_str()); - if (value) { - return std::string(value); - } - return boost::none; -} - -} // namespace osquery diff --git a/src/osquery/utils/system/posix/errno.cpp b/src/osquery/utils/system/posix/errno.cpp deleted file mode 100644 index d86ce2e..0000000 --- a/src/osquery/utils/system/posix/errno.cpp +++ /dev/null @@ -1,55 +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 { - -std::string platformStrerr(int errnum) -{ - return ::strerror(errnum); -} - -namespace impl { - -PosixError toPosixSystemError(int from_errno) -{ - static auto const table = std::unordered_map { - {EPERM, PosixError::PERM}, {ENOENT, PosixError::NOENT}, - {ESRCH, PosixError::SRCH}, {EINTR, PosixError::INTR}, - {EIO, PosixError::IO}, {ENXIO, PosixError::NXIO}, - {E2BIG, PosixError::T_BIG}, {ENOEXEC, PosixError::NOEXEC}, - {EBADF, PosixError::BADF}, {ECHILD, PosixError::CHILD}, - {EAGAIN, PosixError::AGAIN}, {ENOMEM, PosixError::NOMEM}, - {EACCES, PosixError::ACCES}, {EFAULT, PosixError::FAULT}, - {ENOTBLK, PosixError::NOTBLK}, {EBUSY, PosixError::BUSY}, - {EEXIST, PosixError::EXIST}, {EXDEV, PosixError::XDEV}, - {ENODEV, PosixError::NODEV}, {ENOTDIR, PosixError::NOTDIR}, - {EISDIR, PosixError::ISDIR}, {EINVAL, PosixError::INVAL}, - {ENFILE, PosixError::NFILE}, {EMFILE, PosixError::MFILE}, - {ENOTTY, PosixError::NOTTY}, {ETXTBSY, PosixError::TXTBSY}, - {EFBIG, PosixError::FBIG}, {ENOSPC, PosixError::NOSPC}, - {ESPIPE, PosixError::SPIPE}, {EROFS, PosixError::ROFS}, - {EMLINK, PosixError::MLINK}, {EPIPE, PosixError::PIPE}, - {EDOM, PosixError::DOM}, {ERANGE, PosixError::RANGE}, - }; - auto const it = table.find(from_errno); - if (it == table.end()) { - return PosixError::Unknown; - } - return it->second; -} - -} // namespace impl - -} // namespace osquery diff --git a/src/osquery/utils/system/posix/errno.h b/src/osquery/utils/system/posix/errno.h deleted file mode 100644 index 43eb0af..0000000 --- a/src/osquery/utils/system/posix/errno.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright (c) 2018-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. - */ - -#pragma once - -#include - -#include - -namespace osquery { - -enum class PosixError { - Unknown = 0, - PERM = EPERM, - NOENT = ENOENT, - SRCH = ESRCH, - INTR = EINTR, - IO = EIO, - NXIO = ENXIO, - T_BIG = E2BIG, - NOEXEC = ENOEXEC, - BADF = EBADF, - CHILD = ECHILD, - AGAIN = EAGAIN, - NOMEM = ENOMEM, - ACCES = EACCES, - FAULT = EFAULT, - NOTBLK = ENOTBLK, - BUSY = EBUSY, - EXIST = EEXIST, - XDEV = EXDEV, - NODEV = ENODEV, - NOTDIR = ENOTDIR, - ISDIR = EISDIR, - INVAL = EINVAL, - NFILE = ENFILE, - MFILE = EMFILE, - NOTTY = ENOTTY, - TXTBSY = ETXTBSY, - FBIG = EFBIG, - NOSPC = ENOSPC, - SPIPE = ESPIPE, - ROFS = EROFS, - MLINK = EMLINK, - PIPE = EPIPE, - DOM = EDOM, - RANGE = ERANGE, -}; - -namespace impl { - -PosixError toPosixSystemError(int from_errno); - -} - -template -inline typename std::enable_if::value, - PosixError>::type - to(int from_errno) -{ - return impl::toPosixSystemError(from_errno); -} - -} // namespace osquery diff --git a/src/osquery/utils/system/posix/filepath.cpp b/src/osquery/utils/system/posix/filepath.cpp deleted file mode 100644 index 0a8c51a..0000000 --- a/src/osquery/utils/system/posix/filepath.cpp +++ /dev/null @@ -1,47 +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 -#include - -namespace osquery { - -const std::string canonicalize_file_name(const char* name) -{ -#ifdef PATH_MAX - // On supported platforms where PATH_MAX is defined we can pass null - // as buffer, and allow libc to alloced space - // On centos/ubuntu libc will use realloc so we will be able to resolve - // any path - // On darwin libc will allocate PATH_MAX buffer for us - char* resolved = realpath(name, nullptr); - std::string result = (resolved == nullptr) ? name : resolved; - free(resolved); -#else -#warning PATH_MAX is undefined, please read comment below - // PATH_MAX is not defined, very likely it's not officially supported - // os, our best guess is _PC_PATH_MAX if available - // In case of failure fallback to "safe" buffer of 8K - - long int path_max = pathconf(name, _PC_PATH_MAX); - if (path_max <= 0) { - path_max = 8 * 1024; - } - char* buffer = static_cast(malloc(path_max)); - char* resolved = realpath(name, buffer); - std::string result = (resolved == nullptr) ? name : resolved; - free(buffer); -#endif - return result; -} - -} // namespace osquery diff --git a/src/osquery/utils/system/posix/tests/errno.cpp b/src/osquery/utils/system/posix/tests/errno.cpp deleted file mode 100644 index c08d045..0000000 --- a/src/osquery/utils/system/posix/tests/errno.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright (c) 2018-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 - -namespace osquery { -namespace { - -class PosixErrnoTests : public testing::Test {}; - -TEST_F(PosixErrnoTests, to) -{ - EXPECT_EQ(PosixError::Unknown, to(0)); - EXPECT_EQ(PosixError::Unknown, to(-1)); - EXPECT_EQ(PosixError::Unknown, to(98765)); - EXPECT_EQ(PosixError::Unknown, to(987654)); - - EXPECT_EQ(PosixError::PIPE, to(EPIPE)); - EXPECT_EQ(PosixError::DOM, to(EDOM)); - EXPECT_EQ(PosixError::RANGE, to(ERANGE)); - EXPECT_EQ(PosixError::T_BIG, to(E2BIG)); -} - -} // namespace -} // namespace osquery diff --git a/src/osquery/utils/system/posix/time.cpp b/src/osquery/utils/system/posix/time.cpp deleted file mode 100644 index 24fbfa1..0000000 --- a/src/osquery/utils/system/posix/time.cpp +++ /dev/null @@ -1,26 +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 - -namespace osquery { - -std::string platformAsctime(const struct tm* timeptr) -{ - if (timeptr == nullptr) { - return ""; - } - - // Manual says at least 26 characters. - char buffer[32] = {0}; - return ::asctime_r(timeptr, buffer); -} - -} // namespace osquery diff --git a/src/osquery/utils/system/tests/cpu.cpp b/src/osquery/utils/system/tests/cpu.cpp deleted file mode 100644 index 84e0532..0000000 --- a/src/osquery/utils/system/tests/cpu.cpp +++ /dev/null @@ -1,7 +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. - */ diff --git a/src/osquery/utils/system/tests/errno.cpp b/src/osquery/utils/system/tests/errno.cpp deleted file mode 100644 index d50e445..0000000 --- a/src/osquery/utils/system/tests/errno.cpp +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Copyright (c) 2018-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. - */ diff --git a/src/osquery/utils/system/tests/time.cpp b/src/osquery/utils/system/tests/time.cpp deleted file mode 100644 index c668362..0000000 --- a/src/osquery/utils/system/tests/time.cpp +++ /dev/null @@ -1,36 +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 - -namespace osquery { - -class TimeTests : public testing::Test {}; - -TEST_F(TimeTests, test_asciitime) -{ - const std::time_t epoch = 1491518994; - const std::string expected = "Thu Apr 6 22:49:54 2017 UTC"; - - auto const result = std::gmtime(&epoch); - - EXPECT_EQ(expected, toAsciiTime(result)); -} - -TEST_F(TimeTests, test_asciitimeutc) -{ - const std::time_t epoch = 1491518994; - const std::string expected = "Thu Apr 6 22:49:54 2017 UTC"; - - auto const result = std::localtime(&epoch); - - EXPECT_EQ(expected, toAsciiTimeUTC(result)); -} -} diff --git a/src/osquery/utils/system/time.cpp b/src/osquery/utils/system/time.cpp deleted file mode 100644 index f45ae5c..0000000 --- a/src/osquery/utils/system/time.cpp +++ /dev/null @@ -1,77 +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 { - -std::string toAsciiTime(const struct tm* tm_time) -{ - if (tm_time == nullptr) { - return ""; - } - - auto time_str = platformAsctime(tm_time); - boost::algorithm::trim(time_str); - return time_str + " UTC"; -} - -std::string toAsciiTimeUTC(const struct tm* tm_time) -{ - size_t epoch = toUnixTime(tm_time); - struct tm tptr; - - std::memset(&tptr, 0, sizeof(tptr)); - - if (epoch == (size_t) -1) { - return ""; - } - -#ifdef OSQUERY_WINDOWS - _gmtime64_s(&tptr, (time_t*)&epoch); -#else - gmtime_r((time_t*)&epoch, &tptr); -#endif - return toAsciiTime(&tptr); -} - -std::string getAsciiTime() -{ - auto result = std::time(nullptr); - - struct tm now; -#ifdef OSQUERY_WINDOWS - _gmtime64_s(&now, &result); -#else - gmtime_r(&result, &now); -#endif - - return toAsciiTime(&now); -} - -size_t toUnixTime(const struct tm* tm_time) -{ - struct tm result; - std::memset(&result, 0, sizeof(result)); - - std::memcpy(&result, tm_time, sizeof(result)); - return mktime(&result); -} - -size_t getUnixTime() -{ - std::time_t ut = std::time(nullptr); - return ut < 0 ? 0 : ut; -} - -} // namespace osquery diff --git a/src/osquery/utils/system/time.h b/src/osquery/utils/system/time.h deleted file mode 100644 index 774c4e5..0000000 --- a/src/osquery/utils/system/time.h +++ /dev/null @@ -1,62 +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. - */ - -#pragma once - -#include -#include - -namespace osquery { - -/// Returns the ASCII version of the timeptr as a C++ string -std::string platformAsctime(const struct tm* timeptr); - -/** - * @brief Converts struct tm to a size_t - * - * @param tm_time the time/date to convert to UNIX epoch time - * - * @return an int representing the UNIX epoch time of the struct tm - */ -size_t toUnixTime(const struct tm* tm_time); - -/** - * @brief Getter for the current UNIX time. - * - * @return an int representing the amount of seconds since the UNIX epoch - */ -size_t getUnixTime(); - -/** - * @brief Converts a struct tm into a human-readable format. This expected the - * struct tm to be already in UTC time/ - * - * @param tm_time the time/date to convert to ASCII - * - * @return the data/time of tm_time in the format: "Wed Sep 21 10:27:52 2011" - */ -std::string toAsciiTime(const struct tm* tm_time); - -/** - * @brief Converts a struct tm to ASCII time UTC by converting the tm_time to - * epoch and then running gmtime() on the new epoch - * - * @param tm_time the local time/date to covert to UTC ASCII time - * - * @return the data/time of tm_time in the format: "Wed Sep 21 10:27:52 2011" - */ -std::string toAsciiTimeUTC(const struct tm* tm_time); - -/** - * @brief Getter for the current time, in a human-readable format. - * - * @return the current date/time in the format: "Wed Sep 21 10:27:52 2011" - */ -std::string getAsciiTime(); - -} -- 2.34.1