Skip to content
API Documentation
BastionGuard™
native_host.cpp File Reference

Go to the source code of this file.

Functions

static std::string now_iso ()
static bool read_exact (std::istream &in, void *buf, std::size_t n)
json read_message ()
void send_message (const json &j)
static std::string str_tolower (std::string s)
static std::string str_trim (std::string s)
static std::time_t to_time_t (fs::file_time_type ftime)
static void reload_blacklist_if_needed ()
static std::string extract_host_from_url (const std::string &url)
json action_set_lang (const json &req)
json action_restart_scanner (const json &)
json action_open_page (const json &req)
json action_ping (const json &)
json action_check_url (const json &req)
json action_get_mail_config (const json &)
json dispatch (const json &req)
int main ()

Variables

static std::unordered_set< std::string > cached_bl
static std::time_t cached_bl_mtime = 0
static fs::path cached_bl_path

Function Documentation

◆ action_check_url()

json action_check_url ( const json & req)

Definition at line 174 of file native_host.cpp.

174 {
175 try {
176 if (!req.contains("url") || !req["url"].is_string())
177 return { {"ok", false}, {"error", "missing 'url' string"} };
178
179 std::string url = req["url"].get<std::string>();
181
182 json extra;
183 extra["block"] = false;
184 extra["reason"] = "not_in_blacklist";
185
186 if (!cached_bl.empty()) {
187 std::string host = extract_host_from_url(url);
188 if (!host.empty()) {
189 if (cached_bl.find(host) != cached_bl.end()) {
190 extra["block"] = true;
191 extra["reason"] = "host_exact: " + host;
192 } else {
193 for (const auto &entry : cached_bl) {
194 if (entry.empty()) continue;
195 if (host.size() >= entry.size() &&
196 host.compare(host.size()-entry.size(), entry.size(), entry) == 0) {
197 extra["block"] = true;
198 extra["reason"] = "host_suffix: " + entry;
199 break;
200 }
201 }
202 }
203 } else {
204 for (const auto &entry : cached_bl) {
205 if (!entry.empty() && url.find(entry)!=std::string::npos) {
206 extra["block"] = true;
207 extra["reason"] = "url_substr: " + entry;
208 break;
209 }
210 }
211 }
212 }
213
214 return { {"ok", true}, {"url", url}, {"extra", extra} };
215 } catch (const std::exception &ex) {
216 return { {"ok", false}, {"error", std::string("exception: ")+ex.what()} };
217 }
218}
nlohmann::json json
Definition Backend.cpp:2908
static bool extract_host_from_url(const std::string &line, std::string &host)
static std::unordered_set< std::string > cached_bl
static void reload_blacklist_if_needed()

References action_check_url(), cached_bl, extract_host_from_url(), and reload_blacklist_if_needed().

Referenced by action_check_url(), and dispatch().

◆ action_get_mail_config()

json action_get_mail_config ( const json & )

Definition at line 221 of file native_host.cpp.

221 {
222 try {
223 const char* home = std::getenv("HOME");
224 if (!home) return { {"ok", false}, {"error", "HOME non impostata"} };
225
226 fs::path config_path = fs::path(home) / ".config/BastionGuard/mail.json";
227
228 std::error_code ec;
229 if (!fs::exists(config_path, ec))
230 return { {"ok", false}, {"error", "mail.json non trovato: " + config_path.string()} };
231
232 std::ifstream f(config_path);
233 if (!f.is_open())
234 return { {"ok", false}, {"error", "impossibile aprire mail.json"} };
235
236 json config = json::parse(f, nullptr, false);
237 if (config.is_discarded())
238 return { {"ok", false}, {"error", "mail.json non è JSON valido"} };
239
240 return { {"ok", true}, {"config", config} };
241
242 } catch (const std::exception &ex) {
243 return { {"ok", false}, {"error", std::string("eccezione: ") + ex.what()} };
244 }
245}

References action_get_mail_config().

Referenced by action_get_mail_config(), and dispatch().

◆ action_open_page()

json action_open_page ( const json & req)

Definition at line 171 of file native_host.cpp.

171{ return {{"ok", true}}; } // stub

References action_open_page().

Referenced by action_open_page(), and dispatch().

◆ action_ping()

json action_ping ( const json & )

Definition at line 172 of file native_host.cpp.

172{ return { {"ok", true}, {"pong_at", now_iso()} }; }
static std::string now_iso()

References action_ping(), and now_iso().

Referenced by action_ping(), and dispatch().

◆ action_restart_scanner()

json action_restart_scanner ( const json & )

Definition at line 170 of file native_host.cpp.

170{ return {{"ok", true}}; } // stub

References action_restart_scanner().

Referenced by action_restart_scanner(), and dispatch().

◆ action_set_lang()

json action_set_lang ( const json & req)

Definition at line 169 of file native_host.cpp.

169{ return {{"ok", true}}; } // stub

References action_set_lang().

Referenced by action_set_lang(), and dispatch().

◆ dispatch()

json dispatch ( const json & req)

Definition at line 247 of file native_host.cpp.

247 {
248 if (!req.is_object()) return { {"ok", false}, {"error", "request not an object"} };
249 if (!req.contains("action") || !req["action"].is_string()) return { {"ok", false}, {"error", "missing 'action'"} };
250 std::string act = req["action"].get<std::string>();
251
252 if (act == "set_lang") return action_set_lang(req);
253 if (act == "restart_scanner") return action_restart_scanner(req);
254 if (act == "open_page") return action_open_page(req);
255 if (act == "ping") return action_ping(req);
256 if (act == "check_url") return action_check_url(req);
257 if (act == "get_mail_config") return action_get_mail_config(req);
258
259 return { {"ok", false}, {"error", "unknown action: " + act} };
260}
json action_set_lang(const json &req)
json action_open_page(const json &req)
json action_check_url(const json &req)
json action_ping(const json &)
json action_get_mail_config(const json &)
json action_restart_scanner(const json &)

References action_check_url(), action_get_mail_config(), action_open_page(), action_ping(), action_restart_scanner(), action_set_lang(), and dispatch().

Referenced by dispatch(), and main().

◆ extract_host_from_url()

std::string extract_host_from_url ( const std::string & url)
static

Definition at line 156 of file native_host.cpp.

156 {
157 size_t p = url.find("://");
158 size_t start = (p==std::string::npos) ? 0 : p+3;
159 size_t at = url.find('@', start);
160 if (at != std::string::npos) start = at+1;
161 size_t end = url.find_first_of("/:?#", start);
162 std::string host = (end==std::string::npos) ? url.substr(start) : url.substr(start, end-start);
163 size_t colon = host.find(':');
164 if (colon != std::string::npos) host = host.substr(0, colon);
165 return str_tolower(str_trim(host));
166}
static std::string str_trim(std::string s)
static std::string str_tolower(std::string s)

References extract_host_from_url(), str_tolower(), and str_trim().

◆ main()

int main ( void )

Definition at line 263 of file native_host.cpp.

263 {
264 try {
265 while (true) {
266 json req = read_message();
267 if (req.is_discarded()) break;
268 std::cerr << "[native-host] req: " << req.dump() << std::endl;
269 json resp = dispatch(req);
270 resp["ts"] = now_iso();
271 send_message(resp);
272 std::cerr << "[native-host] resp: " << resp.dump() << std::endl;
273 }
274 } catch (const std::exception &ex) {
275 std::cerr << "[native-host] fatal: " << ex.what() << std::endl;
276 try {
277 json e = { {"ok", false}, {"error", std::string("fatal: ") + ex.what()} };
278 send_message(e);
279 } catch(...) {}
280 return 1;
281 }
282 return 0;
283}
json read_message()
json dispatch(const json &req)
void send_message(const json &j)

References dispatch(), main(), now_iso(), read_message(), and send_message().

◆ now_iso()

std::string now_iso ( )
static

Definition at line 43 of file native_host.cpp.

43 {
44 using namespace std::chrono;
45 auto t = system_clock::now();
46 std::time_t tt = system_clock::to_time_t(t);
47 std::tm tm{};
48 gmtime_r(&tt, &tm);
49 std::ostringstream ss;
50 ss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
51 return ss.str();
52}

References now_iso().

Referenced by action_ping(), main(), and now_iso().

◆ read_exact()

bool read_exact ( std::istream & in,
void * buf,
std::size_t n )
static

Definition at line 55 of file native_host.cpp.

55 {
56 std::size_t off = 0;
57 char* p = static_cast<char*>(buf);
58 while (off < n) {
59 in.read(p + off, n - off);
60 std::streamsize got = in.gcount();
61 if (got <= 0) return false;
62 off += static_cast<std::size_t>(got);
63 }
64 return true;
65}

References read_exact().

Referenced by read_exact(), and read_message().

◆ read_message()

json read_message ( )

Definition at line 67 of file native_host.cpp.

67 {
68 uint32_t len = 0;
69 if (!read_exact(std::cin, &len, sizeof(len))) throw std::runtime_error("EOF reading length");
70 uint32_t L = (uint8_t)reinterpret_cast<char*>(&len)[0]
71 | ((uint8_t)reinterpret_cast<char*>(&len)[1] << 8)
72 | ((uint8_t)reinterpret_cast<char*>(&len)[2] << 16)
73 | ((uint8_t)reinterpret_cast<char*>(&len)[3] << 24);
74 std::vector<char> buf(L);
75 if (!read_exact(std::cin, buf.data(), L)) throw std::runtime_error("EOF reading payload");
76 std::string s(buf.begin(), buf.end());
77 return json::parse(s, nullptr, false);
78}
static bool read_exact(std::istream &in, void *buf, std::size_t n)

References read_exact(), and read_message().

Referenced by main(), and read_message().

◆ reload_blacklist_if_needed()

void reload_blacklist_if_needed ( )
static

Definition at line 120 of file native_host.cpp.

120 {
121 const char* home = std::getenv("HOME");
122 if (!home) return;
123 fs::path bl = fs::path(home) / ".local/share/BastionGuard/data/phishing/blacklist.txt";
124
125 std::error_code ec;
126 if (!fs::exists(bl, ec)) {
127 std::cerr << "[native-host] blacklist not found: " << bl << std::endl;
128 cached_bl.clear();
129 cached_bl_mtime = 0;
130 return;
131 }
132 auto ftime = fs::last_write_time(bl, ec);
133 if (ec) return;
134 std::time_t mtime = to_time_t(ftime);
135 if (mtime == cached_bl_mtime && !cached_bl.empty()) return;
136
137 std::ifstream f(bl);
138 if (!f.is_open()) {
139 std::cerr << "[native-host] cannot open blacklist file" << std::endl;
140 return;
141 }
142 std::unordered_set<std::string> newset;
143 std::string line;
144 while (std::getline(f, line)) {
145 line = str_trim(line);
146 if (line.empty() || line[0]=='#') continue;
147 if (!line.empty() && line.back()=='\r') line.pop_back();
148 newset.insert(str_tolower(line));
149 }
150 cached_bl = std::move(newset);
151 cached_bl_mtime = mtime;
152 cached_bl_path = bl;
153 std::cerr << "[native-host] loaded blacklist entries=" << cached_bl.size() << std::endl;
154}
static std::time_t to_time_t(fs::file_time_type ftime)
static std::time_t cached_bl_mtime
static fs::path cached_bl_path

References cached_bl, cached_bl_mtime, cached_bl_path, reload_blacklist_if_needed(), str_tolower(), str_trim(), and to_time_t().

Referenced by action_check_url(), and reload_blacklist_if_needed().

◆ send_message()

void send_message ( const json & j)

Definition at line 80 of file native_host.cpp.

80 {
81 std::string s = j.dump();
82 uint32_t L = static_cast<uint32_t>(s.size());
83 unsigned char lenbuf[4];
84 lenbuf[0] = static_cast<unsigned char>(L & 0xff);
85 lenbuf[1] = static_cast<unsigned char>((L >> 8) & 0xff);
86 lenbuf[2] = static_cast<unsigned char>((L >> 16) & 0xff);
87 lenbuf[3] = static_cast<unsigned char>((L >> 24) & 0xff);
88 std::cout.write(reinterpret_cast<char*>(lenbuf), 4);
89 std::cout.write(s.data(), s.size());
90 std::cout.flush();
91}

References send_message().

Referenced by main(), and send_message().

◆ str_tolower()

std::string str_tolower ( std::string s)
inlinestatic

Definition at line 97 of file native_host.cpp.

97 {
98 std::transform(s.begin(), s.end(), s.begin(),
99 [](unsigned char c){ return std::tolower(c); });
100 return s;
101}

References str_tolower().

Referenced by extract_host_from_url(), reload_blacklist_if_needed(), and str_tolower().

◆ str_trim()

std::string str_trim ( std::string s)
inlinestatic

Definition at line 102 of file native_host.cpp.

102 {
103 const char* ws = " \t\r\n";
104 size_t start = s.find_first_not_of(ws);
105 if (start==std::string::npos) return "";
106 size_t end = s.find_last_not_of(ws);
107 return s.substr(start, end-start+1);
108}

References str_trim().

Referenced by extract_host_from_url(), reload_blacklist_if_needed(), and str_trim().

◆ to_time_t()

std::time_t to_time_t ( fs::file_time_type ftime)
static

Definition at line 111 of file native_host.cpp.

111 {
112 using namespace std::chrono;
113 auto sctp = time_point_cast<system_clock::duration>(
114 ftime - fs::file_time_type::clock::now()
115 + system_clock::now()
116 );
117 return system_clock::to_time_t(sctp);
118}

References to_time_t().

Referenced by reload_blacklist_if_needed(), and to_time_t().

Variable Documentation

◆ cached_bl

std::unordered_set<std::string> cached_bl
static

Definition at line 93 of file native_host.cpp.

Referenced by action_check_url(), and reload_blacklist_if_needed().

◆ cached_bl_mtime

std::time_t cached_bl_mtime = 0
static

Definition at line 94 of file native_host.cpp.

Referenced by reload_blacklist_if_needed().

◆ cached_bl_path

fs::path cached_bl_path
static

Definition at line 95 of file native_host.cpp.

Referenced by reload_blacklist_if_needed().