2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-01-21 20:56:25 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-09-10 01:54:53 +00:00
|
|
|
#pragma once
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-12-01 09:05:46 +00:00
|
|
|
#include <map>
|
2014-07-31 00:35:19 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-01-02 05:55:10 +00:00
|
|
|
#include <osquery/flags.h>
|
2014-12-03 23:31:09 +00:00
|
|
|
#include <osquery/status.h>
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
namespace osquery {
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2015-01-02 05:55:10 +00:00
|
|
|
/// The builder or invoker may change the default config plugin.
|
|
|
|
DECLARE_string(config_retriever);
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief represents the relevant parameters of a scheduled query.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* Within the context of osqueryd, a scheduled query may have many relevant
|
|
|
|
* attributes. Those attributes are represented in this data structure.
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
struct OsqueryScheduledQuery {
|
2014-09-15 18:09:33 +00:00
|
|
|
/// name represents the "name" of a query.
|
2014-07-31 00:35:19 +00:00
|
|
|
std::string name;
|
|
|
|
|
2014-09-15 18:09:33 +00:00
|
|
|
/// query represents the actual SQL query.
|
2014-07-31 00:35:19 +00:00
|
|
|
std::string query;
|
|
|
|
|
2014-09-15 18:09:33 +00:00
|
|
|
/// interval represents how often the query should be executed, in minutes.
|
2014-07-31 00:35:19 +00:00
|
|
|
int interval;
|
|
|
|
|
2014-09-15 18:09:33 +00:00
|
|
|
/// equals operator
|
2014-07-31 00:35:19 +00:00
|
|
|
bool operator==(const OsqueryScheduledQuery& comp) const {
|
2014-08-15 07:25:30 +00:00
|
|
|
return (comp.name == name) && (comp.query == query) &&
|
2014-07-31 00:35:19 +00:00
|
|
|
(comp.interval == interval);
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:09:33 +00:00
|
|
|
/// not equals operator
|
2014-07-31 00:35:19 +00:00
|
|
|
bool operator!=(const OsqueryScheduledQuery& comp) const {
|
|
|
|
return !(*this == comp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A native representation of osquery configuration data.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* When you use osquery::Config::getInstance(), you are getting a singleton
|
|
|
|
* handle to interact with the data stored in an instance of this struct.
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
struct OsqueryConfig {
|
2014-09-15 18:09:33 +00:00
|
|
|
/// A vector of all of the queries that are scheduled to execute.
|
|
|
|
std::vector<OsqueryScheduledQuery> scheduledQueries;
|
2014-12-01 09:05:46 +00:00
|
|
|
std::map<std::string, std::string> options;
|
2014-07-31 00:35:19 +00:00
|
|
|
};
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A string which represents the default consfig retriever.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* The config plugin that you use to define your config retriever can be
|
|
|
|
* defined via a command-line flag, however, if you don't define a config
|
|
|
|
* plugin to use via the command-line, then the config retriever which is
|
|
|
|
* represented by the string stored in kDefaultConfigRetriever will be used.
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
extern const std::string kDefaultConfigRetriever;
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief A singleton that exposes accessors to osquery's configuration data.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* osquery has two types on configurations. Things that don't change during
|
|
|
|
* the execution of the process should be configured as command-line
|
|
|
|
* arguments. Things that can change during the lifetime of program execution
|
|
|
|
* should be defined using the osquery::config::Config class and the pluggable
|
|
|
|
* plugin interface that is included with it.
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
class Config {
|
2014-08-15 07:25:30 +00:00
|
|
|
public:
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief The primary way to access the Config singleton.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* osquery::config::Config::getInstance() provides access to the Config
|
|
|
|
* singleton
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @code{.cpp}
|
|
|
|
* auto config = osquery::config::Config::getInstance();
|
|
|
|
* @endcode
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @return a singleton instance of Config.
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
static std::shared_ptr<Config> getInstance();
|
|
|
|
|
2015-01-02 05:55:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Call the genConfig method of the config retriever plugin.
|
|
|
|
*
|
|
|
|
* This may perform a resource load such as TCP request or filesystem read.
|
|
|
|
*/
|
|
|
|
Status load();
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Get a vector of all scheduled queries.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @code{.cpp}
|
|
|
|
* auto config = osquery::config::Config::getInstance();
|
|
|
|
* for (const auto& q : config->getScheduledQueries()) {
|
|
|
|
* LOG(INFO) << "name: " << q.name;
|
|
|
|
* LOG(INFO) << "interval: " << q.interval;
|
|
|
|
* }
|
|
|
|
* @endcode
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @return a vector of OsqueryScheduledQuery's which represent the queries
|
|
|
|
* that are to be executed
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-09-15 18:17:48 +00:00
|
|
|
std::vector<OsqueryScheduledQuery> getScheduledQueries();
|
2014-08-15 07:25:30 +00:00
|
|
|
|
2014-11-17 22:19:09 +00:00
|
|
|
/**
|
2014-12-11 01:35:21 +00:00
|
|
|
* @brief Calculate the has of the osquery config
|
|
|
|
*
|
|
|
|
* @return The MD5 of the osquery config
|
|
|
|
*/
|
|
|
|
Status getMD5(std::string& hashString);
|
|
|
|
|
2015-01-21 20:56:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Check to ensure that the config is accessible and properly
|
|
|
|
* formatted
|
|
|
|
*
|
|
|
|
* @return an instance of osquery::Status, indicating the success or failure
|
|
|
|
* of the operation.
|
|
|
|
*/
|
|
|
|
static osquery::Status checkConfig();
|
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
private:
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Default constructor.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
|
|
|
* Since instances of Config should only be created via getInstance(),
|
|
|
|
* Config's constructor is private
|
|
|
|
*/
|
2015-01-02 05:55:10 +00:00
|
|
|
Config() {}
|
2014-07-31 00:35:19 +00:00
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Uses the specified config retriever to populate a config struct.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* Internally, genConfig checks to see if there was a config retriever
|
|
|
|
* specified on the command-line. If there was, it checks to see if that
|
|
|
|
* config retriever actually exists. If it does, it gets used to generate
|
|
|
|
* configuration data. If it does not, an error is logged.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* If no config retriever was specified, the config retriever represented by
|
|
|
|
* kDefaultConfigRetriever is used.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @param conf a reference to a struct which will be populated by the config
|
|
|
|
* retriever in use.
|
2014-09-15 18:09:33 +00:00
|
|
|
*
|
2014-09-16 07:28:23 +00:00
|
|
|
* @return an instance of osquery::Status, indicating the success or failure
|
|
|
|
* of the operation.
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-08-05 23:13:55 +00:00
|
|
|
static osquery::Status genConfig(OsqueryConfig& conf);
|
2014-08-15 07:25:30 +00:00
|
|
|
|
2014-12-11 01:35:21 +00:00
|
|
|
/**
|
|
|
|
* @brief Uses the specified config retriever to populate a string with the
|
|
|
|
* config JSON.
|
|
|
|
*
|
|
|
|
* Internally, genConfig checks to see if there was a config retriever
|
|
|
|
* specified on the command-line. If there was, it checks to see if that
|
|
|
|
* config retriever actually exists. If it does, it gets used to generate
|
|
|
|
* configuration data. If it does not, an error is logged.
|
|
|
|
*
|
|
|
|
* If no config retriever was specified, the config retriever represented by
|
|
|
|
* kDefaultConfigRetriever is used.
|
|
|
|
*
|
|
|
|
* @param conf a reference to a string which will be populated by the config
|
|
|
|
* retriever in use.
|
|
|
|
*
|
|
|
|
* @return an instance of osquery::Status, indicating the success or failure
|
|
|
|
* of the operation.
|
|
|
|
*/
|
|
|
|
static osquery::Status genConfig(std::string& conf);
|
|
|
|
|
2014-08-15 07:25:30 +00:00
|
|
|
private:
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief the private member that stores the raw osquery config data in a
|
|
|
|
* native format
|
2014-09-15 18:09:33 +00:00
|
|
|
*/
|
2014-07-31 00:35:19 +00:00
|
|
|
OsqueryConfig cfg_;
|
|
|
|
};
|
2014-08-15 07:25:30 +00:00
|
|
|
}
|