FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
wazuh/src/shared_modules/utils/socketDBWrapper.hpp at fuzzing · postech-compsec/wazuh · GitHub
postech-compsec
wazuh
Repository navigation
Code
Pull requests
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
wazuh
/
src
/
shared_modules
/
utils
/
socketDBWrapper.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
214 lines (196 loc) · 8.01 KB
Breadcrumbs
wazuh
/
src
/
shared_modules
/
utils
/
socketDBWrapper.hpp
Copy path
File metadata and controls
214 lines (196 loc) · 8.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Socket DB Wrapper
* Copyright (C) 2015, Wazuh Inc.
* October 30, 2023.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/
#
ifndef
_SOCKET_DB_WRAPPER_HPP
#
define
_SOCKET_DB_WRAPPER_HPP
#
include
"
json.hpp
"
#
include
"
singleton.hpp
"
#
include
"
socketClient.hpp
"
#
include
"
socketDBWrapperException.hpp
"
#
include
<
condition_variable
>
#
include
<
mutex
>
#
include
<
string
>
#
include
<
utility
>
auto
constexpr
WDB_SOCKET
{
"
queue/db/wdb
"
};
char
constexpr
DB_WRAPPER_OK
[] = {
"
ok
"
};
char
constexpr
DB_WRAPPER_ERROR
[] = {
"
err
"
};
char
constexpr
DB_WRAPPER_UNKNOWN
[] {
"
unk
"
};
char
constexpr
DB_WRAPPER_IGNORE
[] {
"
ign
"
};
char
constexpr
DB_WRAPPER_DUE
[] {
"
due
"
};
enum
class
DbQueryStatus
:
uint8_t
{
UNKNOWN
,
JSON_PARSING
,
EMPTY_RESPONSE
,
QUERY_ERROR
,
QUERY_IGNORE
,
QUERY_UNKNOWN
,
QUERY_NOT_SYNCED
,
INVALID_RESPONSE
};
class
SocketDBWrapper
final
: public Singleton<SocketDBWrapper>
{
private:
std::unique_ptr<SocketClient<Socket<OSPrimitives, SizeHeaderProtocol>, EpollWrapper>> m_dbSocket;
nlohmann::json m_response;
nlohmann::json m_responsePartial;
std::string m_exceptionStr;
DbQueryStatus m_queryStatus {DbQueryStatus::
UNKNOWN
};
std::mutex m_mutexMessage;
std::mutex m_mutexResponse;
std::condition_variable m_conditionVariable;
bool
m_teardown {
false
};
bool
m_dataReady {
false
};
public:
void
init
(
const
std::string& socketPath =
WDB_SOCKET
)
{
m_teardown =
false
;
m_dbSocket = std::make_unique<SocketClient<Socket<OSPrimitives, SizeHeaderProtocol>, EpollWrapper>>(socketPath);
m_dbSocket->
connect
(
[&](
const
char
* body,
uint32_t
bodySize,
const
char
*,
uint32_t
)
{
std::scoped_lock lock {m_mutexResponse};
std::string
responsePacket
(body, bodySize);
if
(
0
== responsePacket.
compare
(
0
,
sizeof
(
DB_WRAPPER_DUE
) -
1
,
DB_WRAPPER_DUE
))
{
try
{
m_responsePartial.
push_back
(
nlohmann::json::parse
(responsePacket.
substr
(
4
)));
}
catch
(
const
nlohmann::detail::exception& ex)
{
m_queryStatus = DbQueryStatus::
JSON_PARSING
;
m_exceptionStr =
"
Error parsing JSON response:
"
+ responsePacket.
substr
(
4
) +
"
. Exception id:
"
+
std::to_string
(ex.
id
) +
"
.
"
+ ex.
what
();
}
}
else
{
if
(responsePacket.
empty
())
{
m_queryStatus = DbQueryStatus::
EMPTY_RESPONSE
;
m_exceptionStr =
"
Empty DB response
"
;
}
else
if
(
0
== responsePacket.
compare
(
0
,
sizeof
(
DB_WRAPPER_ERROR
) -
1
,
DB_WRAPPER_ERROR
))
{
m_queryStatus = DbQueryStatus::
QUERY_ERROR
;
m_exceptionStr =
"
DB query error:
"
+ responsePacket.
substr
(
sizeof
(
DB_WRAPPER_ERROR
));
}
else
if
(
0
== responsePacket.
compare
(
0
,
sizeof
(
DB_WRAPPER_IGNORE
) -
1
,
DB_WRAPPER_IGNORE
))
{
m_queryStatus = DbQueryStatus::
QUERY_IGNORE
;
m_exceptionStr =
"
DB query ignored:
"
+ responsePacket.
substr
(
sizeof
(
DB_WRAPPER_IGNORE
));
}
else
if
(
0
== responsePacket.
compare
(
0
,
sizeof
(
DB_WRAPPER_UNKNOWN
) -
1
,
DB_WRAPPER_UNKNOWN
))
{
m_queryStatus = DbQueryStatus::
QUERY_UNKNOWN
;
m_exceptionStr =
"
DB query unknown response:
"
+ responsePacket.
substr
(
sizeof
(
DB_WRAPPER_UNKNOWN
));
}
else
if
(
0
== responsePacket.
compare
(
0
,
sizeof
(
DB_WRAPPER_OK
) -
1
,
DB_WRAPPER_OK
))
{
if
(!m_responsePartial.
empty
())
{
m_response = m_responsePartial;
}
else
{
try
{
nlohmann::json responseParsed =
nlohmann::json::parse
(responsePacket.
substr
(
sizeof
(
DB_WRAPPER_OK
) -
1
));
if
(responseParsed.
type
() == nlohmann::json::
value_t
::array)
{
m_response =
std::move
(responseParsed);
}
else
{
if
(responseParsed.
contains
(
"
status
"
) &&
responseParsed.
at
(
"
status
"
) ==
"
NOT_SYNCED
"
)
{
m_queryStatus = DbQueryStatus::
QUERY_NOT_SYNCED
;
m_exceptionStr =
"
DB query not synced
"
;
}
else
{
m_response.
push_back
(responseParsed);
}
}
}
catch
(
const
nlohmann::detail::exception& ex)
{
m_queryStatus = DbQueryStatus::
JSON_PARSING
;
m_exceptionStr =
"
Error parsing JSON response:
"
+ responsePacket.
substr
(
sizeof
(
DB_WRAPPER_OK
) -
1
) +
"
. Exception id:
"
+
std::to_string
(ex.
id
) +
"
.
"
+ ex.
what
();
}
}
}
else
{
m_queryStatus = DbQueryStatus::
INVALID_RESPONSE
;
m_exceptionStr =
"
DB query invalid response:
"
+ responsePacket;
}
m_dataReady =
true
;
m_conditionVariable.
notify_one
();
}
});
}
void
query
(
const
std::string& query, nlohmann::json& response)
{
//
Acquire lock to avoid multiple threads sending queries at the same time
std::scoped_lock lockMessage {m_mutexMessage};
if
(m_teardown)
{
return
;
}
//
Acquire lock before clearing the response
std::unique_lock lockResponse {m_mutexResponse};
m_dataReady =
false
;
m_response.
clear
();
m_responsePartial.
clear
();
//
coverity[missing_lock]
m_exceptionStr.
clear
();
if
(!m_dbSocket)
{
throw
std::runtime_error
(
"
Socket DB Wrapper not initialized
"
);
}
m_dbSocket->
send
(query.
c_str
(), query.
size
());
m_conditionVariable.
wait
(lockResponse, [
this
] {
return
m_dataReady || m_teardown; });
if
(!m_exceptionStr.
empty
())
{
//
coverity[missing_lock]
switch
(m_queryStatus)
{
case
DbQueryStatus::
QUERY_NOT_SYNCED
:
throw
SocketDbWrapperException
(m_exceptionStr);
break
;
case
DbQueryStatus::
EMPTY_RESPONSE
:
case
DbQueryStatus::
UNKNOWN
:
case
DbQueryStatus::
QUERY_ERROR
:
case
DbQueryStatus::
QUERY_UNKNOWN
:
case
DbQueryStatus::
QUERY_IGNORE
:
case
DbQueryStatus::
JSON_PARSING
:
case
DbQueryStatus::
INVALID_RESPONSE
:
default
:
throw
std::runtime_error
(m_exceptionStr);
break
;
}
}
response = m_response;
}
/*
*
* @brief Teardown the Socket DB Wrapper object
*
*/
void
teardown
()
{
m_teardown =
true
;
m_conditionVariable.
notify_all
();
m_dbSocket->
stop
();
}
};
#
endif
//
_SOCKET_DB_WRAPPER_HPP
Back
|
FazBrowse Home
|
New Git URL