2015-02-19 01:19:45 +00:00
|
|
|
/*
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2015-02-19 01:19:45 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef OSQUERY_BUILD_SDK
|
|
|
|
#define OSQUERY_BUILD_SDK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <osquery/config.h>
|
|
|
|
#include <osquery/core.h>
|
|
|
|
#include <osquery/database.h>
|
|
|
|
#include <osquery/events.h>
|
|
|
|
#include <osquery/extensions.h>
|
|
|
|
#include <osquery/filesystem.h>
|
|
|
|
#include <osquery/flags.h>
|
|
|
|
#include <osquery/logger.h>
|
|
|
|
#include <osquery/registry.h>
|
|
|
|
#include <osquery/sql.h>
|
|
|
|
#include <osquery/status.h>
|
|
|
|
#include <osquery/tables.h>
|
2015-02-19 23:19:00 +00:00
|
|
|
|
|
|
|
namespace osquery {
|
2015-03-04 02:40:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Create the external SQLite implementation wrapper.
|
|
|
|
*
|
|
|
|
* Anything built with only libosquery and not the 'additional' library will
|
|
|
|
* not include a native SQL implementation. This applies to extensions and
|
|
|
|
* separate applications built with the osquery SDK.
|
|
|
|
*
|
|
|
|
* The ExternalSQLPlugin is a wrapper around the SQLite API, which forwards
|
|
|
|
* calls to an osquery extension manager (core).
|
|
|
|
*/
|
2015-02-19 23:19:00 +00:00
|
|
|
REGISTER_INTERNAL(ExternalSQLPlugin, "sql", "sql");
|
2015-03-04 02:40:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Mimic the REGISTER macro, extensions should use this helper.
|
|
|
|
*
|
|
|
|
* The SDK does not provide a REGISTER macro for modules or extensions.
|
2015-03-04 16:45:21 +00:00
|
|
|
* Tools built with the osquery SDK should use REGISTER_EXTERNAL to add to
|
2015-03-04 02:40:24 +00:00
|
|
|
* their own 'external' registry. This registry will broadcast to the osquery
|
|
|
|
* extension manager (core) in an extension.
|
|
|
|
*
|
|
|
|
* osquery 'modules' should not construct their plugin registrations in
|
|
|
|
* global scope (global construction time). Instead they should use the
|
|
|
|
* module call-in well defined symbol, declare their SDK constraints, then
|
2015-03-04 16:45:21 +00:00
|
|
|
* use the REGISTER_MODULE call within `initModule`.
|
2015-03-04 02:40:24 +00:00
|
|
|
*/
|
2017-01-07 20:21:35 +00:00
|
|
|
#define REGISTER_EXTERNAL(t, r, n) \
|
|
|
|
namespace registries { \
|
|
|
|
const ::osquery::registries::PI<t> k##ExtensionRegistryItem##t(r, n, false); \
|
|
|
|
}
|
2015-03-05 19:28:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create an osquery extension 'module'.
|
|
|
|
*
|
|
|
|
* This helper macro creates a constructor to declare an osquery module is
|
|
|
|
* loading. The osquery registry is set up when modules (shared objects) are
|
|
|
|
* discovered via search paths and opened. At that phase the registry is locked
|
|
|
|
* meaning no additional plugins can be registered. To unlock the registry
|
|
|
|
* for modifications a module must call Registry::declareModule. This declares
|
|
|
|
* and any plugins added will use the metadata in the declare to determine:
|
|
|
|
* - The name of the module adding the plugin
|
|
|
|
* - The SDK version the module was built with, to determine compatibility
|
|
|
|
* - The minimum SDK the module requires from osquery core
|
|
|
|
*
|
|
|
|
* The registry is again locked when the module load is complete and a well
|
|
|
|
* known module-exported symbol is called.
|
|
|
|
*/
|
2016-08-31 23:45:06 +00:00
|
|
|
#define CREATE_MODULE(name, version, min_sdk_version) \
|
|
|
|
extern "C" EXPORT_FUNCTION void initModule(void); \
|
|
|
|
struct osquery_InternalStructCreateModule { \
|
|
|
|
osquery_InternalStructCreateModule(void) { \
|
2017-01-07 20:21:35 +00:00
|
|
|
Registry::get().declareModule( \
|
2016-08-31 23:45:06 +00:00
|
|
|
name, version, min_sdk_version, OSQUERY_SDK_VERSION); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
static osquery_InternalStructCreateModule osquery_internal_module_instance_;
|
2015-03-05 19:28:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create an osquery extension 'module', if an expression is true.
|
|
|
|
*
|
|
|
|
* This is a helper testing wrapper around CREATE_MODULE and DECLARE_MODULE.
|
|
|
|
* It allows unit and integration tests to generate global construction code
|
|
|
|
* that depends on data/variables available during global construction.
|
|
|
|
*
|
|
|
|
* And example use includes checking if a process environment variable is
|
|
|
|
* defined. If defined the module is declared.
|
|
|
|
*/
|
2016-08-31 23:45:06 +00:00
|
|
|
#define CREATE_MODULE_IF(expr, name, version, min_sdk_version) \
|
|
|
|
extern "C" EXPORT_FUNCTION void initModule(void); \
|
|
|
|
struct osquery_InternalStructCreateModule { \
|
|
|
|
osquery_InternalStructCreateModule(void) { \
|
|
|
|
if ((expr)) { \
|
2017-01-07 20:21:35 +00:00
|
|
|
Registry::get().declareModule( \
|
2016-08-31 23:45:06 +00:00
|
|
|
name, version, min_sdk_version, OSQUERY_SDK_VERSION); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
static osquery_InternalStructCreateModule osquery_internal_module_instance_;
|
2015-03-04 16:45:21 +00:00
|
|
|
|
|
|
|
/// Helper replacement for REGISTER, used within extension modules.
|
2017-01-07 20:21:35 +00:00
|
|
|
#define REGISTER_MODULE(t, r, n) \
|
|
|
|
auto t##Module = Registry::get().registry(r)->add(n, std::make_shared<t>());
|
2015-03-04 16:45:21 +00:00
|
|
|
|
2015-03-04 02:40:24 +00:00
|
|
|
// Remove registry-helper macros from the SDK.
|
|
|
|
#undef REGISTER
|
2015-03-04 16:45:21 +00:00
|
|
|
#define REGISTER "Do not REGISTER in the osquery SDK"
|
2015-03-04 02:40:24 +00:00
|
|
|
#undef REGISTER_INTERNAL
|
2015-03-04 16:45:21 +00:00
|
|
|
#define REGISTER_INTERNAL "Do not REGISTER_INTERNAL in the osquery SDK"
|
2015-03-04 02:40:24 +00:00
|
|
|
#undef CREATE_REGISTRY
|
2015-03-04 16:45:21 +00:00
|
|
|
#define CREATE_REGISTRY "Do not CREATE_REGISTRY in the osquery SDK"
|
2015-03-04 02:40:24 +00:00
|
|
|
#undef CREATE_LAZY_REGISTRY
|
2015-03-04 16:45:21 +00:00
|
|
|
#define CREATE_LAZY_REGISTRY "Do not CREATE_LAZY_REGISTRY in the osquery SDK"
|
2015-02-19 23:19:00 +00:00
|
|
|
}
|