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-02 05:55:10 +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-08-05 23:13:55 +00:00
|
|
|
|
2014-12-16 20:04:43 +00:00
|
|
|
#include <syslog.h>
|
2015-02-23 05:56:52 +00:00
|
|
|
#include <time.h>
|
2014-12-16 20:04:43 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2015-03-15 05:43:21 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-03-13 15:11:08 +00:00
|
|
|
|
2015-01-02 05:55:10 +00:00
|
|
|
#include <osquery/config.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/core.h>
|
2015-01-30 18:44:25 +00:00
|
|
|
#include <osquery/events.h>
|
2015-03-03 23:03:14 +00:00
|
|
|
#include <osquery/extensions.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/flags.h>
|
|
|
|
#include <osquery/filesystem.h>
|
2015-01-12 20:53:05 +00:00
|
|
|
#include <osquery/logger.h>
|
2014-12-03 23:14:02 +00:00
|
|
|
#include <osquery/registry.h>
|
2014-08-05 23:13:55 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
#include "osquery/core/watcher.h"
|
2014-08-05 23:13:55 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
namespace osquery {
|
2014-09-02 00:13:04 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
#define DESCRIPTION \
|
|
|
|
"osquery %s, your OS as a high-performance relational database\n"
|
|
|
|
#define EPILOG "\nosquery project page <http://osquery.io>.\n"
|
|
|
|
#define OPTIONS \
|
|
|
|
"\nosquery configuration options (set by config or CLI flags):\n\n"
|
|
|
|
#define OPTIONS_SHELL "\nosquery shell-only CLI flags:\n\n"
|
|
|
|
#define OPTIONS_CLI "osquery%s command line flags:\n\n"
|
|
|
|
#define USAGE "Usage: %s [OPTION]... %s\n\n"
|
|
|
|
#define CONFIG_ERROR \
|
|
|
|
"You are using default configurations for osqueryd for one or more of the " \
|
|
|
|
"following\n" \
|
|
|
|
"flags: pidfile, db_path.\n\n" \
|
|
|
|
"These options create files in /var/osquery but it looks like that path " \
|
|
|
|
"has not\n" \
|
|
|
|
"been created. Please consider explicitly defining those " \
|
|
|
|
"options as a different \n" \
|
|
|
|
"path. Additionally, review the \"using osqueryd\" wiki page:\n" \
|
|
|
|
" - https://github.com/facebook/osquery/wiki/using-osqueryd\n\n";
|
|
|
|
|
|
|
|
CLI_FLAG(bool,
|
|
|
|
config_check,
|
|
|
|
false,
|
|
|
|
"Check the format of an osquery config and exit");
|
2015-02-06 17:42:03 +00:00
|
|
|
|
|
|
|
#ifndef __APPLE__
|
2015-03-03 23:03:14 +00:00
|
|
|
CLI_FLAG(bool, daemonize, false, "Run as daemon (osqueryd only)");
|
2015-02-06 17:42:03 +00:00
|
|
|
#endif
|
|
|
|
|
2014-12-16 20:04:43 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
void printUsage(const std::string& binary, int tool) {
|
|
|
|
// Parse help options before gflags. Only display osquery-related options.
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, DESCRIPTION, OSQUERY_VERSION);
|
2014-12-16 20:04:43 +00:00
|
|
|
if (tool == OSQUERY_TOOL_SHELL) {
|
|
|
|
// The shell allows a caller to run a single SQL statement and exit.
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, USAGE, binary.c_str(), "[SQL STATEMENT]");
|
2014-12-16 20:04:43 +00:00
|
|
|
} else {
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, USAGE, binary.c_str(), "");
|
2015-02-25 04:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tool == OSQUERY_EXTENSION) {
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, OPTIONS_CLI, " extension");
|
2015-02-25 04:29:57 +00:00
|
|
|
Flag::printFlags(false, true);
|
|
|
|
} else {
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, OPTIONS_CLI, "");
|
|
|
|
Flag::printFlags(false, false, true);
|
|
|
|
fprintf(stdout, OPTIONS);
|
2015-02-25 04:29:57 +00:00
|
|
|
Flag::printFlags();
|
|
|
|
}
|
2014-12-16 20:04:43 +00:00
|
|
|
|
|
|
|
if (tool == OSQUERY_TOOL_SHELL) {
|
|
|
|
// Print shell flags.
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, OPTIONS_SHELL);
|
2015-02-17 00:26:06 +00:00
|
|
|
Flag::printFlags(true);
|
2014-12-16 20:04:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
fprintf(stdout, EPILOG);
|
2014-09-09 22:35:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 03:47:35 +00:00
|
|
|
Initializer::Initializer(int& argc, char**& argv, ToolType tool)
|
|
|
|
: argc_(&argc),
|
|
|
|
argv_(&argv),
|
2015-03-03 23:03:14 +00:00
|
|
|
tool_(tool),
|
|
|
|
binary_(fs::path(std::string(argv[0])).filename().string()) {
|
2015-02-24 11:47:12 +00:00
|
|
|
std::srand(time(nullptr));
|
2014-10-27 18:55:28 +00:00
|
|
|
|
2015-01-02 05:55:10 +00:00
|
|
|
// osquery implements a custom help/usage output.
|
2015-03-19 03:47:35 +00:00
|
|
|
std::string first_arg = (*argc_ > 1) ? std::string((*argv_)[1]) : "";
|
2014-11-09 04:27:28 +00:00
|
|
|
if ((first_arg == "--help" || first_arg == "-h" || first_arg == "-help") &&
|
|
|
|
tool != OSQUERY_TOOL_TEST) {
|
2015-03-03 23:03:14 +00:00
|
|
|
printUsage(binary_, tool_);
|
2014-10-27 16:34:13 +00:00
|
|
|
::exit(0);
|
2014-09-09 22:35:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:19:37 +00:00
|
|
|
// To change the default config plugin, compile osquery with
|
|
|
|
// -DOSQUERY_DEFAULT_CONFIG_PLUGIN=<new_default_plugin>
|
2015-01-26 18:44:27 +00:00
|
|
|
#ifdef OSQUERY_DEFAULT_CONFIG_PLUGIN
|
2015-02-05 00:54:44 +00:00
|
|
|
FLAGS_config_plugin = STR(OSQUERY_DEFAULT_CONFIG_PLUGIN);
|
2015-01-26 18:44:27 +00:00
|
|
|
#endif
|
2014-10-27 01:39:03 +00:00
|
|
|
|
2015-02-24 21:19:37 +00:00
|
|
|
// To change the default logger plugin, compile osquery with
|
|
|
|
// -DOSQUERY_DEFAULT_LOGGER_PLUGIN=<new_default_plugin>
|
2015-02-16 02:15:06 +00:00
|
|
|
#ifdef OSQUERY_DEFAULT_LOGGER_PLUGIN
|
|
|
|
FLAGS_logger_plugin = STR(OSQUERY_DEFAULT_LOGGER_PLUGIN);
|
|
|
|
#endif
|
|
|
|
|
2015-03-15 05:43:21 +00:00
|
|
|
if (tool == OSQUERY_TOOL_SHELL) {
|
|
|
|
// The shell is transient, rewrite config-loaded paths.
|
|
|
|
osquery::FLAGS_disable_logging = true;
|
|
|
|
|
|
|
|
// Get the caller's home dir for temporary storage/state management.
|
2015-03-19 03:47:35 +00:00
|
|
|
auto homedir = osqueryHomeDirectory();
|
2015-03-15 05:43:21 +00:00
|
|
|
if (osquery::pathExists(homedir).ok() ||
|
|
|
|
boost::filesystem::create_directory(homedir)) {
|
|
|
|
osquery::FLAGS_database_path = homedir + "/shell.db";
|
|
|
|
osquery::FLAGS_extensions_socket = homedir + "/shell.em";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 00:55:19 +00:00
|
|
|
// Set version string from CMake build
|
2015-02-17 00:26:06 +00:00
|
|
|
GFLAGS_NAMESPACE::SetVersionString(OSQUERY_VERSION);
|
2014-11-09 00:55:19 +00:00
|
|
|
|
2014-10-27 18:55:28 +00:00
|
|
|
// Let gflags parse the non-help options/flags.
|
2015-03-19 03:47:35 +00:00
|
|
|
GFLAGS_NAMESPACE::ParseCommandLineFlags(
|
|
|
|
argc_, argv_, (tool == OSQUERY_TOOL_SHELL));
|
2014-10-27 01:39:03 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
// If the caller is checking configuration, disable the watchdog/worker.
|
|
|
|
if (FLAGS_config_check) {
|
|
|
|
FLAGS_disable_watchdog = true;
|
2015-02-06 17:42:03 +00:00
|
|
|
}
|
2015-02-16 02:15:06 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
// Initialize the status and results logger.
|
|
|
|
initStatusLogger(binary_);
|
2015-03-13 15:11:08 +00:00
|
|
|
if (tool != OSQUERY_EXTENSION) {
|
|
|
|
VLOG(1) << "osquery initialized [version=" << OSQUERY_VERSION << "]";
|
|
|
|
} else {
|
|
|
|
VLOG(1) << "osquery extension initialized [sdk=" << OSQUERY_SDK_VERSION
|
|
|
|
<< "]";
|
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
void Initializer::initDaemon() {
|
2015-02-06 17:42:03 +00:00
|
|
|
#ifndef __APPLE__
|
|
|
|
// OSX uses launchd to daemonize.
|
|
|
|
if (osquery::FLAGS_daemonize) {
|
|
|
|
if (daemon(0, 0) == -1) {
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Print the version to SYSLOG.
|
2015-03-03 23:03:14 +00:00
|
|
|
syslog(
|
|
|
|
LOG_NOTICE, "%s started [version=%s]", binary_.c_str(), OSQUERY_VERSION);
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-02-24 21:19:37 +00:00
|
|
|
// check if /var/osquery exists
|
|
|
|
if ((Flag::isDefault("pidfile") || Flag::isDefault("db_path")) &&
|
|
|
|
!isDirectory("/var/osquery")) {
|
2015-03-03 23:03:14 +00:00
|
|
|
std::cerr << CONFIG_ERROR
|
2015-02-24 21:19:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 17:42:03 +00:00
|
|
|
// Create a process mutex around the daemon.
|
|
|
|
auto pid_status = createPidFile();
|
|
|
|
if (!pid_status.ok()) {
|
2015-03-03 23:03:14 +00:00
|
|
|
LOG(ERROR) << binary_ << " initialize failed: " << pid_status.toString();
|
2015-02-06 17:42:03 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-03-03 23:03:14 +00:00
|
|
|
}
|
2015-02-06 17:42:03 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void Initializer::initWatcher() {
|
|
|
|
// The watcher takes a list of paths to autoload extensions from.
|
2015-03-17 16:49:30 +00:00
|
|
|
loadExtensions();
|
2015-03-03 23:03:14 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Add a watcher service thread to start/watch an optional worker and set
|
|
|
|
// of optional extensions in the autoload paths.
|
2015-03-17 07:29:23 +00:00
|
|
|
if (Watcher::hasManagedExtensions() || !FLAGS_disable_watchdog) {
|
2015-03-19 03:47:35 +00:00
|
|
|
Dispatcher::getInstance().addService(std::make_shared<WatcherRunner>(
|
|
|
|
*argc_, *argv_, !FLAGS_disable_watchdog));
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
2015-03-03 23:03:14 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// If there are no autoloaded extensions, the watcher service will end,
|
|
|
|
// otherwise it will continue as a background thread and respawn them.
|
|
|
|
// If the watcher is also a worker watchdog it will do nothing but monitor
|
|
|
|
// the extensions and worker process.
|
|
|
|
if (!FLAGS_disable_watchdog) {
|
|
|
|
Dispatcher::joinServices();
|
2015-03-03 23:03:14 +00:00
|
|
|
// Executation should never reach this point.
|
2015-02-06 17:42:03 +00:00
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-08-05 23:13:55 +00:00
|
|
|
}
|
2015-02-01 08:35:44 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void Initializer::initWorker(const std::string& name) {
|
|
|
|
// Set the worker's process name.
|
2015-03-19 03:47:35 +00:00
|
|
|
size_t name_size = strlen((*argv_)[0]);
|
|
|
|
for (int i = 0; i < *argc_; i++) {
|
|
|
|
if ((*argv_)[i] != nullptr) {
|
|
|
|
memset((*argv_)[i], 0, strlen((*argv_)[i]));
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 03:47:35 +00:00
|
|
|
strncpy((*argv_)[0], name.c_str(), name_size);
|
2015-03-03 23:03:14 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Start a watcher watcher thread to exit the process if the watcher exits.
|
|
|
|
Dispatcher::getInstance().addService(
|
|
|
|
std::make_shared<WatcherWatcherRunner>(getppid()));
|
|
|
|
}
|
2015-03-04 16:45:21 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void Initializer::initWorkerWatcher(const std::string& name) {
|
|
|
|
if (isWorker()) {
|
|
|
|
initWorker(name);
|
|
|
|
} else {
|
|
|
|
// The watcher will forever monitor and spawn additional workers.
|
|
|
|
initWatcher();
|
2015-03-08 21:52:13 +00:00
|
|
|
}
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 07:29:23 +00:00
|
|
|
bool Initializer::isWorker() { return (getenv("OSQUERY_WORKER") != nullptr); }
|
2015-03-08 21:52:13 +00:00
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
void Initializer::initConfigLogger() {
|
|
|
|
// Use a delay, meaning the amount of milliseconds waited for extensions.
|
|
|
|
size_t delay = 0;
|
|
|
|
// The timeout is the maximum time in seconds to wait for extensions.
|
|
|
|
size_t timeout = atoi(FLAGS_extensions_timeout.c_str());
|
|
|
|
while (!Registry::setActive("config", FLAGS_config_plugin)) {
|
|
|
|
// If there is at least 1 autoloaded extension, it may broadcast a route
|
|
|
|
// to the active config plugin.
|
2015-03-17 07:29:23 +00:00
|
|
|
if (!Watcher::hasManagedExtensions() || delay > timeout * 1000) {
|
2015-03-13 15:11:08 +00:00
|
|
|
LOG(ERROR) << "Config plugin not found: " << FLAGS_config_plugin;
|
|
|
|
::exit(EXIT_CATASTROPHIC);
|
|
|
|
}
|
|
|
|
::usleep(kExtensionInitializeMLatency * 1000);
|
|
|
|
delay += kExtensionInitializeMLatency;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the same wait for a logger pluing too.
|
|
|
|
while (!Registry::setActive("logger", FLAGS_logger_plugin)) {
|
2015-03-17 07:29:23 +00:00
|
|
|
if (!Watcher::hasManagedExtensions() || delay > timeout * 1000) {
|
2015-03-13 15:11:08 +00:00
|
|
|
LOG(ERROR) << "Logger plugin not found: " << FLAGS_logger_plugin;
|
|
|
|
::exit(EXIT_CATASTROPHIC);
|
|
|
|
}
|
|
|
|
::usleep(kExtensionInitializeMLatency * 1000);
|
|
|
|
delay += kExtensionInitializeMLatency;
|
2015-03-08 21:52:13 +00:00
|
|
|
}
|
2015-03-13 15:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Initializer::start() {
|
|
|
|
// Load registry/extension modules before extensions.
|
|
|
|
osquery::loadModules();
|
2015-03-08 21:52:13 +00:00
|
|
|
|
2015-03-03 23:03:14 +00:00
|
|
|
// Bind to an extensions socket and wait for registry additions.
|
|
|
|
osquery::startExtensionManager();
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Then set the config/logger plugins, which use a single/active plugin.
|
|
|
|
initConfigLogger();
|
|
|
|
|
|
|
|
// Run the setup for all lazy registries (tables, SQL).
|
|
|
|
Registry::setUp();
|
2015-03-03 23:03:14 +00:00
|
|
|
|
|
|
|
if (FLAGS_config_check) {
|
|
|
|
// The initiator requested an initialization and config check.
|
|
|
|
auto s = Config::checkConfig();
|
|
|
|
if (!s.ok()) {
|
|
|
|
std::cerr << "Error reading config: " << s.toString() << "\n";
|
|
|
|
}
|
|
|
|
// A configuration check exits the application.
|
|
|
|
::exit(s.getCode());
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:11:08 +00:00
|
|
|
// Load the osquery config using the default/active config plugin.
|
|
|
|
Config::load();
|
2015-03-03 23:03:14 +00:00
|
|
|
|
|
|
|
// Check the backing store by allocating and exiting on error.
|
|
|
|
if (!DBHandle::checkDB()) {
|
|
|
|
LOG(ERROR) << binary_ << " initialize failed: Could not create DB handle";
|
|
|
|
if (isWorker()) {
|
|
|
|
::exit(EXIT_CATASTROPHIC);
|
|
|
|
} else {
|
|
|
|
::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the status and result plugin logger.
|
|
|
|
initLogger(binary_);
|
|
|
|
|
|
|
|
// Start event threads.
|
|
|
|
osquery::attachEvents();
|
|
|
|
osquery::EventFactory::delay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initializer::shutdown() {
|
2015-02-01 08:35:44 +00:00
|
|
|
// End any event type run loops.
|
2015-02-06 17:42:03 +00:00
|
|
|
EventFactory::end();
|
2015-02-01 08:35:44 +00:00
|
|
|
|
|
|
|
// Hopefully release memory used by global string constructors in gflags.
|
2015-02-17 00:26:06 +00:00
|
|
|
GFLAGS_NAMESPACE::ShutDownCommandLineFlags();
|
2015-02-01 08:35:44 +00:00
|
|
|
}
|
2014-08-15 07:25:30 +00:00
|
|
|
}
|