Skip to content
API Documentation
BastionGuard™
bsc::rule::Operator Struct Reference

#include <Rule.hpp>

Public Member Functions

bool matches (const bsc::model::Connection &conn) const

Public Attributes

OperatorType type {OperatorType::Unknown}
std::string operand
std::string data
bool sensitive {false}
std::vector< Operator > list

Detailed Description

Definition at line 13 of file Rule.hpp.

Member Function Documentation

◆ matches()

bool bsc::rule::Operator::matches ( const bsc::model::Connection & conn) const
nodiscard

Definition at line 230 of file Rule.cpp.

230 {
231 const auto op = normalize_operand(operand);
232 if (op == "true") return true;
233 if (type == OperatorType::List || op == "list") {
234 if (list.empty()) return true;
235 for (const auto& child : list) if (!child.matches(conn)) return false;
236 return true;
237 }
238 const auto values = field_values(conn, op);
239 auto str_matches = [&](const std::string& value) -> bool {
240 switch (type) {
242 return sensitive ? value == data : to_lower(value) == to_lower(data);
244 try {
245 return std::regex_search(value, std::regex(data, sensitive ? std::regex::ECMAScript : (std::regex::ECMAScript | std::regex::icase)));
246 } catch (...) { return false; }
248 if (const auto it = g_aliases.find(data); it != g_aliases.end()) { for (const auto& cidr : it->second) if (ip_in_cidr(value, cidr)) return true; return false; }
249 return ip_in_cidr(value, data);
250 case OperatorType::Range: {
251 const auto range = parse_range(data);
252 if (!range) return false;
253 unsigned long long n = 0;
254 auto [p, ec] = std::from_chars(value.data(), value.data() + value.size(), n);
255 return ec == std::errc{} && n >= range->first && n <= range->second;
256 }
257 case OperatorType::Lists: {
258 namespace fs = std::filesystem;
259 std::error_code ec;
260 if (!fs::exists(data, ec)) return false;
261 const auto is_domain_regex = op == "lists.domains_regexp";
262 const auto is_nets = op == "lists.nets";
263 auto match_file = [&](const fs::path& p) {
264 std::ifstream in(p);
265 std::string line;
266 while (std::getline(in, line)) {
267 line = trim(line);
268 if (line.empty() || line[0] == '#') continue;
269 if (is_nets) { if (ip_in_cidr(value, line)) return true; continue; }
270 if (is_domain_regex) { try { if (std::regex_search(value, std::regex(line, sensitive ? std::regex::ECMAScript : (std::regex::ECMAScript | std::regex::icase)))) return true; } catch (...) {} continue; }
271 auto cmp = sensitive ? line : to_lower(line);
272 auto vv = sensitive ? value : to_lower(value);
273 if (op == "lists.domains" && vv.size() >= cmp.size()) { if (vv == cmp || (vv.size() > cmp.size() && vv.ends_with('.' + cmp))) return true; }
274 else if (cmp == vv) return true;
275 }
276 return false;
277 };
278 if (fs::is_regular_file(data, ec)) return match_file(data);
279 for (const auto& entry : fs::directory_iterator(data, ec)) if (entry.is_regular_file() && match_file(entry.path())) return true;
280 return false;
281 }
282 default:
283 return false;
284 }
285 };
286 for (const auto& value : values) if (str_matches(value)) return true;
287 return false;
288}
bool ip_in_cidr(const std::string &ip, const std::string &cidr)
Definition Rule.cpp:142
std::optional< std::pair< unsigned long long, unsigned long long > > parse_range(const std::string &data)
Definition Rule.cpp:165
std::map< std::string, std::vector< std::string > > g_aliases
Definition Rule.cpp:23
std::string normalize_operand(std::string op)
Definition Rule.cpp:56
std::string to_lower(std::string s)
Definition Rule.cpp:52
std::vector< std::string > field_values(const bsc::model::Connection &c, const std::string &operand)
Definition Rule.cpp:80
std::string operand
Definition Rule.hpp:15
std::string data
Definition Rule.hpp:16
OperatorType type
Definition Rule.hpp:14
std::vector< Operator > list
Definition Rule.hpp:18

References data, bsc::rule::List, list, bsc::rule::Lists, bsc::rule::Network, operand, bsc::rule::Range, bsc::rule::Regexp, sensitive, bsc::rule::Simple, and type.

Referenced by bsc::rule::Rule::matches().

Member Data Documentation

◆ data

◆ list

◆ operand

◆ sensitive

◆ type


The documentation for this struct was generated from the following files:
  • BastionGuard/thirdparty/bastionguard-secure-connection/daemon/include/bsc/rule/Rule.hpp
  • BastionGuard/thirdparty/bastionguard-secure-connection/daemon/src/rule/Rule.cpp