osquery-1/include/osquery/core.h

184 lines
4.7 KiB
C
Raw Normal View History

2014-07-31 00:35:19 +00:00
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
2014-07-31 00:35:19 +00:00
#include <string>
#include <vector>
2014-08-12 00:37:49 +00:00
#include <sqlite3.h>
#include <boost/filesystem.hpp>
2014-10-10 05:06:45 +00:00
#include "osquery/database/results.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
/**
* @brief The version of osquery
2014-09-15 19:23:07 +00:00
*/
extern const std::string kVersion;
/// Use a macro for the version literal, set the kVersion symbol in the library.
2014-11-09 00:55:19 +00:00
#ifndef STR
2014-11-09 04:27:28 +00:00
#define STR_OF(x) #x
#define STR(x) STR_OF(x)
2014-11-09 00:55:19 +00:00
#endif
#define OSQUERY_VERSION STR(OSQUERY_BUILD_VERSION)
2014-09-15 19:23:07 +00:00
2014-11-09 04:27:28 +00:00
/**
* @brief A helpful tool type to report when logging, print help, or debugging.
*/
enum osqueryTool {
OSQUERY_TOOL_SHELL,
OSQUERY_TOOL_DAEMON,
OSQUERY_TOOL_TEST,
2014-11-09 04:27:28 +00:00
};
/**
* @brief Execute a query
*
* This is a lower-level version of osquery::SQL. Prefer to use osquery::SQL.
*
* @code{.cpp}
* std::string q = "SELECT * FROM time;";
* int i = 0;
* auto qd = query(q, i);
* if (i == 0) {
* for (const auto& each : qd) {
* for (const auto& it : each) {
* LOG(INFO) << it.first << ": " << it.second;
* }
* }
* } else {
* LOG(ERROR) << "Error: " << i;
* }
* @endcode
*
* @param q the query to execute
* @param error_return an int indicating the success or failure of the query
*
* @return the results of the query
2014-09-15 19:23:07 +00:00
*/
2014-09-21 21:27:09 +00:00
osquery::QueryData query(const std::string& q, int& error_return);
2014-09-15 19:23:07 +00:00
/**
* @brief Execute a query on a specific database
2014-09-15 19:23:07 +00:00
*
* If you need to use a different database, other than the osquery default,
* use this method and pass along a pointer to a SQLite3 database. This is
* useful for testing.
2014-09-15 19:23:07 +00:00
*
* @param q the query to execute
* @param error_return an int indicating the success or failure of the query
* @param db the SQLite3 database the execute query q against
2014-09-15 19:23:07 +00:00
*
* @return the results of the query
2014-09-15 19:23:07 +00:00
*/
2014-09-21 21:29:28 +00:00
osquery::QueryData query(const std::string& q, int& error_return, sqlite3* db);
2014-07-31 00:35:19 +00:00
/**
* @brief Return a fully configured sqlite3 database object
2014-09-15 19:23:07 +00:00
*
* An osquery database is basically just a SQLite3 database with several
* virtual tables attached. This method is the main abstraction for creating
* SQLite3 databases within osquery.
2014-09-15 19:23:07 +00:00
*
* @return a SQLite3 database with all virtual tables attached
2014-09-15 19:23:07 +00:00
*/
2014-09-16 07:36:49 +00:00
sqlite3* createDB();
2014-09-13 21:28:45 +00:00
/**
* @brief Sets up various aspects of osquery execution state.
2014-09-15 19:23:07 +00:00
*
* osquery needs a few things to happen as soon as the executable begins
* executing. initOsquery takes care of setting up the relevant parameters.
* initOsquery should be called in an executable's `main()` function.
2014-09-15 19:23:07 +00:00
*
* @param argc the number of elements in argv
* @param argv the command-line arguments passed to `main()`
2014-09-15 19:23:07 +00:00
*/
2014-11-09 04:27:28 +00:00
void initOsquery(int argc, char* argv[], int tool = OSQUERY_TOOL_TEST);
2014-07-31 00:35:19 +00:00
/**
* @brief Split a given string based on an optional deliminator.
2014-09-15 19:23:07 +00:00
*
* If no deliminator is supplied, the string will be split based on whitespace.
2014-09-15 19:23:07 +00:00
*
* @param s the string that you'd like to split
* @param delim the delimiter which you'd like to split the string by
2014-09-15 19:23:07 +00:00
*
* @return a vector of strings which represent the split string that you
* passed as the s parameter.
2014-09-15 19:23:07 +00:00
*/
2014-09-16 07:36:49 +00:00
std::vector<std::string> split(const std::string& s,
const std::string& delim = "\t ");
/**
* @brief Getter for a host's current hostname
2014-09-15 19:23:07 +00:00
*
* @return a string representing the host's current hostname
2014-09-15 19:23:07 +00:00
*/
std::string getHostname();
/**
* @brief generate a uuid to uniquely identify this machine
*
* @return uuid string to identify this machine
*/
std::string generateHostUuid();
/**
* @brief Getter for the current time, in a human-readable format.
2014-09-15 19:23:07 +00:00
*
* @return the current date/time in the format: "Wed Sep 21 10:27:52 2011"
2014-09-15 19:23:07 +00:00
*/
std::string getAsciiTime();
/**
* @brief Getter for the current unix time.
2014-09-15 19:23:07 +00:00
*
* @return an int representing the amount of seconds since the unix epoch
2014-09-15 19:23:07 +00:00
*/
int getUnixTime();
/**
* @brief Return a vector of all home directories on the system
*
* @return a vector of strings representing the path of all home directories
*/
std::vector<boost::filesystem::path> getHomeDirectories();
2014-08-15 07:25:30 +00:00
}
2014-11-12 21:51:14 +00:00
/**
* @brief Inline helper function for use with utf8StringSize
*/
2014-11-13 01:13:15 +00:00
template <typename _Iterator1, typename _Iterator2>
2014-11-12 21:51:14 +00:00
inline size_t incUtf8StringIterator(_Iterator1& it, const _Iterator2& last) {
2014-11-13 01:13:15 +00:00
if (it == last)
return 0;
2014-11-12 21:51:14 +00:00
unsigned char c;
size_t res = 1;
2014-11-13 01:13:15 +00:00
for (++it; last != it; ++it, ++res) {
2014-11-12 21:51:14 +00:00
c = *it;
2014-11-13 01:13:15 +00:00
if (!(c & 0x80) || ((c & 0xC0) == 0xC0))
break;
2014-11-12 21:51:14 +00:00
}
return res;
}
/**
* @brief Get the length of a UTF-8 string
*
* @param str The UTF-8 string
*
* @return the length of the string
*/
2014-11-13 01:13:15 +00:00
inline size_t utf8StringSize(const std::string& str) {
2014-11-12 21:51:14 +00:00
size_t res = 0;
std::string::const_iterator it = str.begin();
2014-11-13 01:13:15 +00:00
for (; it != str.end(); incUtf8StringIterator(it, str.end()))
2014-11-12 21:51:14 +00:00
res++;
return res;
}