/
sql.cpp
150 lines (128 loc) · 4.13 KB
/
sql.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
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <sstream>
#include <osquery/core.h>
#include <osquery/logger.h>
#include <osquery/sql.h>
#include <osquery/tables.h>
#include <osquery/registry.h>
namespace osquery {
FLAG(int32, value_max, 512, "Maximum returned row value size");
SQL::SQL(const std::string& q) {
status_ = query(q, results_);
}
const QueryData& SQL::rows() const {
return results_;
}
bool SQL::ok() {
return status_.ok();
}
const Status& SQL::getStatus() const {
return status_;
}
std::string SQL::getMessageString() {
return status_.toString();
}
static inline void escapeNonPrintableBytes(std::string& data) {
std::string escaped;
// clang-format off
char const hex_chars[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
};
// clang-format on
bool needs_replacement = false;
for (size_t i = 0; i < data.length(); i++) {
if (((unsigned char)data[i]) < 0x20 || ((unsigned char)data[i]) >= 0x80) {
needs_replacement = true;
escaped += "\\x";
escaped += hex_chars[(((unsigned char)data[i])) >> 4];
escaped += hex_chars[((unsigned char)data[i] & 0x0F) >> 0];
} else {
escaped += data[i];
}
}
// Only replace if any escapes were made.
if (needs_replacement) {
data = std::move(escaped);
}
}
void escapeNonPrintableBytesEx(std::string& data) {
return escapeNonPrintableBytes(data);
}
void SQL::escapeResults() {
for (auto& row : results_) {
for (auto& column : row) {
escapeNonPrintableBytes(column.second);
}
}
}
QueryData SQL::selectAllFrom(const std::string& table) {
PluginResponse response;
Registry::call("table", table, {{"action", "generate"}}, response);
return response;
}
QueryData SQL::selectAllFrom(const std::string& table,
const std::string& column,
ConstraintOperator op,
const std::string& expr) {
PluginRequest request = {{"action", "generate"}};
{
// Create a fake content, there will be no caching.
QueryContext ctx;
ctx.constraints[column].add(Constraint(op, expr));
TablePlugin::setRequestFromContext(ctx, request);
}
PluginResponse response;
Registry::call("table", table, request, response);
return response;
}
Status SQLPlugin::call(const PluginRequest& request, PluginResponse& response) {
response.clear();
if (request.count("action") == 0) {
return Status(1, "SQL plugin must include a request action");
}
if (request.at("action") == "query") {
return this->query(request.at("query"), response);
} else if (request.at("action") == "columns") {
TableColumns columns;
auto status = this->getQueryColumns(request.at("query"), columns);
// Convert columns to response
for (const auto& column : columns) {
response.push_back({{"n", std::get<0>(column)},
{"t", columnTypeName(std::get<1>(column))},
{"o", INTEGER(std::get<2>(column))}});
}
return status;
} else if (request.at("action") == "attach") {
// Attach a virtual table name using an optional included definition.
return this->attach(request.at("table"));
} else if (request.at("action") == "detach") {
this->detach(request.at("table"));
return Status(0, "OK");
}
return Status(1, "Unknown action");
}
Status query(const std::string& q, QueryData& results) {
return Registry::call(
"sql", "sql", {{"action", "query"}, {"query", q}}, results);
}
Status getQueryColumns(const std::string& q, TableColumns& columns) {
PluginResponse response;
auto status = Registry::call(
"sql", "sql", {{"action", "columns"}, {"query", q}}, response);
// Convert response to columns
for (const auto& item : response) {
columns.push_back(
make_tuple(item.at("n"), columnTypeName(item.at("t")), DEFAULT));
}
return status;
}
}