forked from osquery/osquery
/
registry_interface.cpp
431 lines (349 loc) · 11.3 KB
/
registry_interface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
* Copyright (c) 2018-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/
#include <iostream>
#include <osquery/extensions.h>
#include <osquery/logger.h>
#include <osquery/sql/sqlite_util.h>
#include <osquery/registry_factory.h>
#include <osquery/registry_interface.h>
#include <osquery/utils/conversions/split.h>
#include <osquery/flags.h>
namespace osquery {
FLAG(bool, disable_native_tables, false, " tables");
void RegistryInterface::removeUnsafe(const std::string& item_name) {
if (items_.count(item_name) > 0) {
items_[item_name]->tearDown();
items_.erase(item_name);
}
// Populate list of aliases to remove (those that mask item_name).
std::vector<std::string> removed_aliases;
for (const auto& alias : aliases_) {
if (alias.second == item_name) {
removed_aliases.push_back(alias.first);
}
}
for (const auto& alias : removed_aliases) {
aliases_.erase(alias);
}
}
void RegistryInterface::remove(const std::string& item_name) {
WriteLock lock(mutex_);
removeUnsafe(item_name);
}
bool RegistryInterface::isInternal(const std::string& item_name) const {
ReadLock lock(mutex_);
return isInternal_(item_name);
}
std::map<std::string, RouteUUID> RegistryInterface::getExternal() const {
ReadLock lock(mutex_);
return external_;
}
std::string RegistryInterface::getActive() const {
ReadLock lock(mutex_);
return active_;
}
std::string RegistryInterface::getName() const {
ReadLock lock(mutex_);
return name_;
}
size_t RegistryInterface::count() const {
ReadLock lock(mutex_);
return items_.size();
}
Status RegistryInterface::setActive(const std::string& item_name) {
UpgradeLock lock(mutex_);
// Default support multiple active plugins.
for (const auto& item : osquery::split(item_name, ",")) {
if (items_.count(item) == 0 && external_.count(item) == 0) {
return Status::failure("Unknown registry plugin: " + item);
}
}
Status status;
{
WriteUpgradeLock wlock(lock);
active_ = item_name;
}
// The active plugin is setup when initialized.
for (const auto& item : osquery::split(item_name, ",")) {
if (exists_(item, true)) {
status = RegistryFactory::get().plugin(name_, item)->setUp();
} else if (exists_(item, false) && !RegistryFactory::get().external()) {
// If the active plugin is within an extension we must wait.
// An extension will first broadcast the registry, then receive the list
// of active plugins, active them if they are extension-local, and finally
// start their extension socket.
status = pingExtension(getExtensionSocket(external_.at(item)));
}
if (!status.ok()) {
break;
}
}
return status;
}
RegistryRoutes RegistryInterface::getRoutes() const {
ReadLock lock(mutex_);
RegistryRoutes route_table;
for (const auto& item : items_) {
if (isInternal_(item.first)) {
// This is an internal plugin, do not include the route.
continue;
}
bool has_alias = false;
for (const auto& alias : aliases_) {
if (alias.second == item.first) {
// If the item name is masked by at least one alias, it will not
// broadcast under the internal item name.
route_table[alias.first] = item.second->routeInfo();
has_alias = true;
}
}
if (!has_alias) {
route_table[item.first] = item.second->routeInfo();
}
}
return route_table;
}
Status RegistryInterface::call(const std::string& item_name,
const PluginRequest& request,
PluginResponse& response) {
PluginRef plugin;
{
ReadLock lock(mutex_);
// Search local plugins (items) for the plugin.
if (items_.count(item_name) > 0) {
plugin = items_.at(item_name);
}
}
if (plugin) {
return plugin->call(request, response);
}
RouteUUID uuid;
{
ReadLock lock(mutex_);
// Check if the item was broadcasted as a plugin within an extension.
if (external_.count(item_name) > 0) {
// The item is a registered extension, call the extension by UUID.
uuid = external_.at(item_name);
} else if (routes_.count(item_name) > 0) {
// The item has a route, but no extension, pass in the route info.
response = routes_.at(item_name);
return Status::success();
} else if (RegistryFactory::get().external()) {
// If this is an extension's registry forward unknown calls to the core.
uuid = 0;
} else {
return Status::failure("Cannot call registry item: " + item_name);
}
}
return callExtension(uuid, name_, item_name, request, response);
}
Status RegistryInterface::addAlias(const std::string& item_name,
const std::string& alias) {
WriteLock lock(mutex_);
if (aliases_.count(alias) > 0) {
return Status::failure("Duplicate alias: " + alias);
}
aliases_[alias] = item_name;
return Status::success();
}
std::string RegistryInterface::getAlias(const std::string& alias) const {
ReadLock lock(mutex_);
if (aliases_.count(alias) == 0) {
return alias;
}
return aliases_.at(alias);
}
Status RegistryInterface::addPlugin(const std::string& plugin_name,
const PluginRef& plugin_item,
bool internal) {
WriteLock lock(mutex_);
if (items_.count(plugin_name) > 0) {
return Status::failure("Duplicate registry item exists: " + plugin_name);
}
plugin_item->setName(plugin_name);
items_.emplace(std::make_pair(plugin_name, plugin_item));
// The item can be listed as internal, meaning it does not broadcast.
if (internal) {
internal_.push_back(plugin_name);
}
return Status::success();
}
void RegistryInterface::setUp() {
UpgradeLock lock(mutex_);
// If this registry does not auto-setup do NOT setup the registry items.
if (!auto_setup_) {
return;
}
// If the registry is using a single 'active' plugin, setUp that plugin.
// For config and logger, only setUp the selected plugin.
if (active_.size() != 0 && exists_(active_, true)) {
items_.at(active_)->setUp();
return;
}
// Try to set up each of the registry items.
// If they fail, remove them from the registry.
std::vector<std::string> failed;
for (auto& item : items_) {
if (!item.second->setUp().ok()) {
failed.push_back(item.first);
}
}
{
WriteUpgradeLock wlock(lock);
for (const auto& failed_item : failed) {
removeUnsafe(failed_item);
}
}
}
void RegistryInterface::configure() {
ReadLock lock(mutex_);
if (!active_.empty() && exists_(active_, true)) {
items_.at(active_)->configure();
} else {
for (auto& item : items_) {
item.second->configure();
}
}
}
Status RegistryInterface::addExternal(const RouteUUID& uuid,
const RegistryRoutes& routes) {
// Add each route name (item name) to the tracking.
for (const auto& route : routes) {
// Keep the routes info assigned to the registry.
{
WriteLock wlock(mutex_);
routes_[route.first] = route.second;
}
auto status = addExternalPlugin(route.first, route.second);
if (status.ok()) {
WriteLock wlock(mutex_);
external_[route.first] = uuid;
} else {
return status;
}
}
return Status::success();
}
/// Remove all the routes for a given uuid.
void RegistryInterface::removeExternal(const RouteUUID& uuid) {
WriteLock lock(mutex_);
std::vector<std::string> removed_items;
// Create list of items to remove by filtering uuid
for (const auto& item : external_) {
if (item.second == uuid) {
removed_items.push_back(item.first);
}
}
for (const auto& item : removed_items) {
removeExternalPlugin(item);
}
// Remove items belonging to the external uuid.
for (const auto& item : removed_items) {
external_.erase(item);
routes_.erase(item);
}
}
/// Facility method to check if a registry item exists.
bool RegistryInterface::exists(const std::string& item_name, bool local) const {
ReadLock lock(mutex_);
return exists_(item_name, local);
}
/// Facility method to list the registry item identifiers.
std::vector<std::string> RegistryInterface::names() const {
ReadLock lock(mutex_);
std::vector<std::string> names;
for (const auto& item : items_) {
names.push_back(item.first);
}
// Also add names of external plugins.
for (const auto& item : external_) {
names.push_back(item.first);
}
return names;
}
std::map<std::string, PluginRef> RegistryInterface::plugins() {
ReadLock lock(mutex_);
return items_;
}
void RegistryInterface::setname(const std::string& name) {
WriteLock lock(mutex_);
name_ = name;
}
bool RegistryInterface::isInternal_(const std::string& item_name) const {
ReadLock lock(mutex_);
if (std::find(internal_.begin(), internal_.end(), item_name) ==
internal_.end()) {
return false;
}
return true;
}
bool RegistryInterface::exists_(const std::string& item_name,
bool local) const {
ReadLock lock(mutex_);
bool has_local = (items_.count(item_name) > 0);
bool has_external = (external_.count(item_name) > 0);
bool has_route = (routes_.count(item_name) > 0);
return (local) ? has_local : has_local || has_external || has_route;
}
AutoRegisterInterface::AutoRegisterInterface(const char* _type,
const char* _name,
bool optional) // Is optional misnamed?
: type_(_type), name_(_name), optional_(optional) {}
AutoRegisterSet& AutoRegisterInterface::registries() {
static AutoRegisterSet registries_;
return registries_;
}
AutoRegisterSet& AutoRegisterInterface::plugins() {
static AutoRegisterSet plugins_;
return plugins_;
}
void AutoRegisterInterface::autoloadRegistry(
std::unique_ptr<AutoRegisterInterface> ar_) {
registries().push_back(std::move(ar_));
}
void AutoRegisterInterface::autoloadPlugin(
std::unique_ptr<AutoRegisterInterface> ar_) {
// seph here
//
// Note, this happens _before_ disable_tables is parsed, so we can't
// just use `SQLiteDBManager::isDisabled(ar_->name_.c_str())` That
// may be something to change.
//
// flags don;t work either? Is this running before the flag parsing?
//
// Update -- this is supper weird. Flags should be in by now. But
// let's do this in codegen?
if (strncmp(ar_->type_.c_str(), "table", 5) == 0
&& ! ar_->optional_ // This var seems to be inverted
//&& SQLiteDBManager::isDisabled(ar_->name_.c_str())
//&& FLAGS_disable_native_tables
) {
std::cout << "seph skipping a plugin for "
<< ar_->type_.c_str()
<< " " << ar_->name_.c_str()
<< "\n";
return;
}
// std::cout << "seph loading a plugin for "
// << ar_->type_.c_str()
// << " " << ar_->name_.c_str()
// << "\n";
//if !(osquery::SQLiteDBManager::isDisabled(plugin_name)) {
plugins().push_back(std::move(ar_));
}
void registryAndPluginInit() {
for (const auto& it : AutoRegisterInterface::registries()) {
it->run();
}
for (const auto& it : AutoRegisterInterface::plugins()) {
it->run();
}
AutoRegisterSet().swap(AutoRegisterInterface::registries());
AutoRegisterSet().swap(AutoRegisterInterface::plugins());
}
} // namespace osquery