mirror of
https://github.com/valitydev/osquery-1.git
synced 2024-11-07 18:08:53 +00:00
a67525fae1
Summary: Pull Request resolved: https://github.com/facebook/osquery/pull/5375 LICENSE is now defined in a single file on the root of the project, update the header to contain that information. **Project LICENSE did not change.** Reviewed By: akindyakov Differential Revision: D13750575 fbshipit-source-id: 1e608a81b260b8395f9d008fc67f463160c1fc2b
144 lines
4.4 KiB
C++
144 lines
4.4 KiB
C++
/**
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed as defined on the LICENSE file found in the
|
|
* root directory of this source tree.
|
|
*/
|
|
|
|
#include <iostream>
|
|
|
|
#include "osquery/database/plugins/ephemeral.h"
|
|
|
|
namespace osquery {
|
|
|
|
template <typename T>
|
|
Status EphemeralDatabasePlugin::getAny(const std::string& domain,
|
|
const std::string& key,
|
|
T& value) const {
|
|
auto domainIterator = db_.find(domain);
|
|
if (domainIterator == db_.end()) {
|
|
return Status(1, "Domain " + domain + " does not exist");
|
|
}
|
|
|
|
auto keyIterator = domainIterator->second.find(key);
|
|
if (keyIterator == domainIterator->second.end()) {
|
|
return Status(1, "Key " + key + " in domain " + domain + " does not exist");
|
|
}
|
|
|
|
try {
|
|
value = boost::get<T>(keyIterator->second);
|
|
} catch (const boost::bad_get& e) {
|
|
LOG(WARNING) << "Type error getting string value for (domain,key) : ("
|
|
<< key << "," << domain << ") " << e.what();
|
|
return Status(
|
|
1, "EphemeralDatabasePlugin::get was requested incorrect type(string)");
|
|
}
|
|
return Status(0);
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::get(const std::string& domain,
|
|
const std::string& key,
|
|
std::string& value) const {
|
|
return this->getAny(domain, key, value);
|
|
}
|
|
Status EphemeralDatabasePlugin::get(const std::string& domain,
|
|
const std::string& key,
|
|
int& value) const {
|
|
return this->getAny(domain, key, value);
|
|
}
|
|
|
|
void EphemeralDatabasePlugin::setValue(const std::string& domain,
|
|
const std::string& key,
|
|
const std::string& value) {
|
|
db_[domain][key] = value;
|
|
}
|
|
|
|
void EphemeralDatabasePlugin::setValue(const std::string& domain,
|
|
const std::string& key,
|
|
int value) {
|
|
db_[domain][key] = value;
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::put(const std::string& domain,
|
|
const std::string& key,
|
|
const std::string& value) {
|
|
setValue(domain, key, value);
|
|
return Status(0);
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::put(const std::string& domain,
|
|
const std::string& key,
|
|
int value) {
|
|
setValue(domain, key, value);
|
|
return Status(0);
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::putBatch(const std::string& domain,
|
|
const DatabaseStringValueList& data) {
|
|
for (const auto& p : data) {
|
|
const auto& key = p.first;
|
|
const auto& value = p.second;
|
|
|
|
setValue(domain, key, value);
|
|
}
|
|
|
|
return Status(0, "OK");
|
|
}
|
|
|
|
void EphemeralDatabasePlugin::dumpDatabase() const {
|
|
for (const auto& domainValue : db_) {
|
|
const auto& domain = domainValue.first;
|
|
for (const auto& keyValue : domainValue.second) {
|
|
const auto& key = keyValue.first;
|
|
const auto& value = keyValue.second;
|
|
std::cout << domain << "[" << key << "]: " << value << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::remove(const std::string& domain,
|
|
const std::string& k) {
|
|
db_[domain].erase(k);
|
|
return Status(0);
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::removeRange(const std::string& domain,
|
|
const std::string& low,
|
|
const std::string& high) {
|
|
std::vector<std::string> keys;
|
|
for (const auto& it : db_[domain]) {
|
|
if (it.first >= low && it.first <= high) {
|
|
keys.push_back(it.first);
|
|
}
|
|
}
|
|
|
|
for (const auto& key : keys) {
|
|
db_[domain].erase(key);
|
|
}
|
|
return Status(0);
|
|
}
|
|
|
|
Status EphemeralDatabasePlugin::scan(const std::string& domain,
|
|
std::vector<std::string>& results,
|
|
const std::string& prefix,
|
|
size_t max) const {
|
|
if (db_.count(domain) == 0) {
|
|
return Status(0);
|
|
}
|
|
|
|
for (const auto& key : db_.at(domain)) {
|
|
if (!prefix.empty() &&
|
|
!(std::mismatch(prefix.begin(), prefix.end(), key.first.begin())
|
|
.first == prefix.end())) {
|
|
continue;
|
|
}
|
|
results.push_back(key.first);
|
|
if (max > 0 && results.size() >= max) {
|
|
break;
|
|
}
|
|
}
|
|
return Status(0);
|
|
}
|
|
} // namespace osquery
|