Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifdef HAVE_CONFIG_H
19 : #include "config.h"
20 : #endif
21 : #include "namedirectory.h"
22 :
23 : #include "logger.h"
24 : #include "string_utils.h"
25 : #include "fileutils.h"
26 : #include "base64.h"
27 : #include "scheduled_executor.h"
28 :
29 : #include <asio.hpp>
30 :
31 : #include "manager.h"
32 : #include <opendht/crypto.h>
33 : #include <opendht/utils.h>
34 : #include <opendht/http.h>
35 : #include <opendht/logger.h>
36 : #include <opendht/thread_pool.h>
37 :
38 : #include <cstddef>
39 : #include <msgpack.hpp>
40 : #include <json/json.h>
41 :
42 : /* for visual studio */
43 : #include <ciso646>
44 : #include <sstream>
45 : #include <regex>
46 : #include <fstream>
47 :
48 : namespace jami {
49 :
50 : constexpr const char* const QUERY_NAME {"/name/"};
51 : constexpr const char* const QUERY_ADDR {"/addr/"};
52 : constexpr auto CACHE_DIRECTORY {"namecache"sv};
53 : constexpr const char DEFAULT_SERVER_HOST[] = "https://ns.jami.net";
54 :
55 : const std::string HEX_PREFIX = "0x";
56 : constexpr std::chrono::seconds SAVE_INTERVAL {5};
57 :
58 : /** Parser for URIs. ( protocol ) ( username ) ( hostname ) */
59 : const std::regex URI_VALIDATOR {
60 : "^([a-zA-Z]+:(?://)?)?(?:([a-z0-9-_]{1,64})@)?([a-zA-Z0-9\\-._~%!$&'()*+,;=:\\[\\]]+)"};
61 : const std::regex NAME_VALIDATOR {"^[a-zA-Z0-9-_]{3,32}$"};
62 :
63 : constexpr size_t MAX_RESPONSE_SIZE {1024ul * 1024};
64 :
65 : using Request = dht::http::Request;
66 :
67 : void
68 3 : toLower(std::string& string)
69 : {
70 3 : std::transform(string.begin(), string.end(), string.begin(), ::tolower);
71 3 : }
72 :
73 : NameDirectory&
74 0 : NameDirectory::instance()
75 : {
76 0 : return instance(DEFAULT_SERVER_HOST);
77 : }
78 :
79 : void
80 4 : NameDirectory::lookupUri(std::string_view uri, const std::string& default_server, LookupCallback cb)
81 : {
82 4 : const std::string& default_ns = default_server.empty() ? DEFAULT_SERVER_HOST : default_server;
83 4 : std::svmatch pieces_match;
84 4 : if (std::regex_match(uri, pieces_match, URI_VALIDATOR)) {
85 4 : if (pieces_match.size() == 4) {
86 4 : if (pieces_match[2].length() == 0)
87 4 : instance(default_ns).lookupName(pieces_match[3], std::move(cb));
88 : else
89 0 : instance(pieces_match[3].str()).lookupName(pieces_match[2], std::move(cb));
90 4 : return;
91 : }
92 : }
93 0 : JAMI_ERROR("Unable to parse URI: {}", uri);
94 0 : cb("", Response::invalidResponse);
95 8 : }
96 :
97 25 : NameDirectory::NameDirectory(const std::string& serverUrl, std::shared_ptr<dht::Logger> l)
98 25 : : serverUrl_(serverUrl)
99 25 : , logger_(std::move(l))
100 50 : , httpContext_(Manager::instance().ioContext())
101 : {
102 25 : if (!serverUrl_.empty() && serverUrl_.back() == '/')
103 0 : serverUrl_.pop_back();
104 25 : resolver_ = std::make_shared<dht::http::Resolver>(*httpContext_, serverUrl, logger_);
105 25 : cachePath_ = fileutils::get_cache_dir() / CACHE_DIRECTORY / resolver_->get_url().host;
106 25 : }
107 :
108 25 : NameDirectory::~NameDirectory()
109 : {
110 25 : decltype(requests_) requests;
111 : {
112 25 : std::lock_guard lk(requestsMtx_);
113 25 : requests = std::move(requests_);
114 25 : }
115 25 : for (auto& req : requests)
116 0 : req->cancel();
117 25 : }
118 :
119 : void
120 25 : NameDirectory::load()
121 : {
122 25 : loadCache();
123 25 : }
124 :
125 : NameDirectory&
126 803 : NameDirectory::instance(const std::string& serverUrl, std::shared_ptr<dht::Logger> l)
127 : {
128 803 : const std::string& s = serverUrl.empty() ? DEFAULT_SERVER_HOST : serverUrl;
129 : static std::mutex instanceMtx {};
130 :
131 803 : std::lock_guard lock(instanceMtx);
132 803 : static std::map<std::string, NameDirectory> instances {};
133 803 : auto it = instances.find(s);
134 803 : if (it != instances.end())
135 778 : return it->second;
136 25 : auto r = instances.emplace(std::piecewise_construct,
137 25 : std::forward_as_tuple(s),
138 25 : std::forward_as_tuple(s, l));
139 25 : if (r.second)
140 25 : r.first->second.load();
141 25 : return r.first->second;
142 803 : }
143 :
144 : void
145 792 : NameDirectory::setHeaderFields(Request& request)
146 : {
147 792 : request.set_header_field(restinio::http_field_t::user_agent, fmt::format("Jami ({}/{})",
148 792 : jami::platform(), jami::arch()));
149 792 : request.set_header_field(restinio::http_field_t::accept, "*/*");
150 792 : request.set_header_field(restinio::http_field_t::content_type, "application/json");
151 792 : }
152 :
153 : void
154 790 : NameDirectory::lookupAddress(const std::string& addr, LookupCallback cb)
155 : {
156 790 : auto cacheResult = nameCache(addr);
157 790 : if (not cacheResult.empty()) {
158 1 : cb(cacheResult, Response::found);
159 1 : return;
160 : }
161 789 : auto request = std::make_shared<Request>(*httpContext_,
162 789 : resolver_,
163 2367 : serverUrl_ + QUERY_ADDR + addr);
164 : try {
165 789 : request->set_method(restinio::http_method_get());
166 789 : setHeaderFields(*request);
167 1578 : request->add_on_done_callback(
168 789 : [this, cb = std::move(cb), addr](const dht::http::Response& response) {
169 789 : if (response.status_code >= 400 && response.status_code < 500) {
170 788 : auto cacheResult = nameCache(addr);
171 788 : if (not cacheResult.empty())
172 0 : cb(cacheResult, Response::found);
173 : else
174 788 : cb("", Response::notFound);
175 789 : } else if (response.status_code != 200) {
176 0 : JAMI_ERROR("Address lookup for {} failed with code={}",
177 : addr, response.status_code);
178 0 : cb("", Response::error);
179 : } else {
180 : try {
181 1 : Json::Value json;
182 1 : std::string err;
183 1 : Json::CharReaderBuilder rbuilder;
184 1 : auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
185 2 : if (!reader->parse(response.body.data(),
186 1 : response.body.data() + response.body.size(),
187 : &json,
188 : &err)) {
189 0 : JAMI_DBG("Address lookup for %s: Unable to parse server response: %s",
190 : addr.c_str(),
191 : response.body.c_str());
192 0 : cb("", Response::error);
193 0 : return;
194 : }
195 1 : auto name = json["name"].asString();
196 1 : if (name.empty()) {
197 0 : cb(name, Response::notFound);
198 0 : return;
199 : }
200 3 : JAMI_DEBUG("Found name for {}: {}", addr, name);
201 : {
202 1 : std::lock_guard l(cacheLock_);
203 1 : addrCache_.emplace(name, addr);
204 1 : nameCache_.emplace(addr, name);
205 1 : }
206 1 : cb(name, Response::found);
207 1 : scheduleCacheSave();
208 1 : } catch (const std::exception& e) {
209 0 : JAMI_ERROR("Error when performing address lookup: {}", e.what());
210 0 : cb("", Response::error);
211 0 : }
212 : }
213 789 : std::lock_guard lk(requestsMtx_);
214 789 : if (auto req = response.request.lock())
215 789 : requests_.erase(req);
216 789 : });
217 : {
218 789 : std::lock_guard lk(requestsMtx_);
219 789 : requests_.emplace(request);
220 789 : }
221 789 : request->send();
222 0 : } catch (const std::exception& e) {
223 0 : JAMI_ERROR("Error when performing address lookup: {}", e.what());
224 0 : std::lock_guard lk(requestsMtx_);
225 0 : if (request)
226 0 : requests_.erase(request);
227 0 : }
228 790 : }
229 :
230 : bool
231 0 : NameDirectory::verify(const std::string& name,
232 : const dht::crypto::PublicKey& pk,
233 : const std::string& signature)
234 : {
235 0 : return pk.checkSignature(std::vector<uint8_t>(name.begin(), name.end()),
236 0 : base64::decode(signature));
237 : }
238 :
239 : void
240 4 : NameDirectory::lookupName(const std::string& n, LookupCallback cb)
241 : {
242 4 : std::string name {n};
243 4 : if (not validateName(name)) {
244 2 : cb("", Response::invalidResponse);
245 2 : return;
246 : }
247 2 : toLower(name);
248 2 : std::string cacheResult = addrCache(name);
249 2 : if (not cacheResult.empty()) {
250 0 : cb(cacheResult, Response::found);
251 0 : return;
252 : }
253 2 : auto request = std::make_shared<Request>(*httpContext_,
254 2 : resolver_,
255 6 : serverUrl_ + QUERY_NAME + name);
256 : try {
257 2 : request->set_method(restinio::http_method_get());
258 2 : setHeaderFields(*request);
259 2 : request->add_on_done_callback([this, name, cb = std::move(cb)](
260 6 : const dht::http::Response& response) {
261 2 : if (response.status_code >= 400 && response.status_code < 500)
262 1 : cb("", Response::notFound);
263 1 : else if (response.status_code < 200 || response.status_code > 299)
264 0 : cb("", Response::error);
265 : else {
266 : try {
267 1 : Json::Value json;
268 1 : std::string err;
269 1 : Json::CharReaderBuilder rbuilder;
270 1 : auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
271 2 : if (!reader->parse(response.body.data(),
272 1 : response.body.data() + response.body.size(),
273 : &json,
274 : &err)) {
275 0 : JAMI_ERROR("Name lookup for {}: Unable to parse server response: {}",
276 : name, response.body);
277 0 : cb("", Response::error);
278 0 : return;
279 : }
280 1 : auto addr = json["addr"].asString();
281 1 : auto publickey = json["publickey"].asString();
282 1 : auto signature = json["signature"].asString();
283 :
284 1 : if (!addr.compare(0, HEX_PREFIX.size(), HEX_PREFIX))
285 0 : addr = addr.substr(HEX_PREFIX.size());
286 1 : if (addr.empty()) {
287 0 : cb("", Response::notFound);
288 0 : return;
289 : }
290 1 : if (not publickey.empty() and not signature.empty()) {
291 : try {
292 0 : auto pk = dht::crypto::PublicKey(base64::decode(publickey));
293 0 : if (pk.getId().toString() != addr or not verify(name, pk, signature)) {
294 0 : cb("", Response::invalidResponse);
295 0 : return;
296 : }
297 0 : } catch (const std::exception& e) {
298 0 : cb("", Response::invalidResponse);
299 0 : return;
300 0 : }
301 : }
302 3 : JAMI_DEBUG("Found address for {}: {}", name, addr);
303 : {
304 1 : std::lock_guard l(cacheLock_);
305 1 : addrCache_.emplace(name, addr);
306 1 : nameCache_.emplace(addr, name);
307 1 : }
308 1 : cb(addr, Response::found);
309 1 : scheduleCacheSave();
310 1 : } catch (const std::exception& e) {
311 0 : JAMI_ERROR("Error when performing name lookup: {}", e.what());
312 0 : cb("", Response::error);
313 0 : }
314 : }
315 2 : if (auto req = response.request.lock())
316 2 : requests_.erase(req);
317 : });
318 : {
319 2 : std::lock_guard lk(requestsMtx_);
320 2 : requests_.emplace(request);
321 2 : }
322 2 : request->send();
323 0 : } catch (const std::exception& e) {
324 0 : JAMI_ERROR("Name lookup for {} failed: {}", name, e.what());
325 0 : std::lock_guard lk(requestsMtx_);
326 0 : if (request)
327 0 : requests_.erase(request);
328 0 : }
329 4 : }
330 :
331 : bool
332 5 : NameDirectory::validateName(const std::string& name) const
333 : {
334 5 : return std::regex_match(name, NAME_VALIDATOR);
335 : }
336 :
337 : using Blob = std::vector<uint8_t>;
338 : void
339 1 : NameDirectory::registerName(const std::string& addr,
340 : const std::string& n,
341 : const std::string& owner,
342 : RegistrationCallback cb,
343 : const std::string& signedname,
344 : const std::string& publickey)
345 : {
346 1 : std::string name {n};
347 1 : if (not validateName(name)) {
348 0 : cb(RegistrationResponse::invalidName, name);
349 0 : return;
350 : }
351 1 : toLower(name);
352 1 : auto cacheResult = addrCache(name);
353 1 : if (not cacheResult.empty()) {
354 0 : if (cacheResult == addr)
355 0 : cb(RegistrationResponse::success, name);
356 : else
357 0 : cb(RegistrationResponse::alreadyTaken, name);
358 0 : return;
359 : }
360 : {
361 1 : std::lock_guard l(cacheLock_);
362 1 : if (not pendingRegistrations_.emplace(addr, name).second) {
363 0 : JAMI_WARNING("RegisterName: already registering name {} {}", addr, name);
364 0 : cb(RegistrationResponse::error, name);
365 0 : return;
366 : }
367 1 : }
368 : std::string body = fmt::format("{{\"addr\":\"{}\",\"owner\":\"{}\",\"signature\":\"{}\",\"publickey\":\"{}\"}}",
369 : addr,
370 : owner,
371 : signedname,
372 2 : base64::encode(publickey));
373 1 : auto request = std::make_shared<Request>(*httpContext_,
374 1 : resolver_,
375 3 : serverUrl_ + QUERY_NAME + name);
376 : try {
377 1 : request->set_method(restinio::http_method_post());
378 1 : setHeaderFields(*request);
379 1 : request->set_body(body);
380 :
381 3 : JAMI_WARNING("RegisterName: sending request {} {}", addr, name);
382 :
383 2 : request->add_on_done_callback(
384 1 : [this, name, addr, cb = std::move(cb)](const dht::http::Response& response) {
385 : {
386 1 : std::lock_guard l(cacheLock_);
387 1 : pendingRegistrations_.erase(name);
388 1 : }
389 1 : if (response.status_code == 400) {
390 0 : cb(RegistrationResponse::incompleteRequest, name);
391 0 : JAMI_ERROR("RegistrationResponse::incompleteRequest");
392 1 : } else if (response.status_code == 401) {
393 0 : cb(RegistrationResponse::signatureVerificationFailed, name);
394 0 : JAMI_ERROR("RegistrationResponse::signatureVerificationFailed");
395 1 : } else if (response.status_code == 403) {
396 0 : cb(RegistrationResponse::alreadyTaken, name);
397 0 : JAMI_ERROR("RegistrationResponse::alreadyTaken");
398 1 : } else if (response.status_code == 409) {
399 0 : cb(RegistrationResponse::alreadyTaken, name);
400 0 : JAMI_ERROR("RegistrationResponse::alreadyTaken");
401 1 : } else if (response.status_code > 400 && response.status_code < 500) {
402 0 : cb(RegistrationResponse::alreadyTaken, name);
403 0 : JAMI_ERROR("RegistrationResponse::alreadyTaken");
404 1 : } else if (response.status_code < 200 || response.status_code > 299) {
405 0 : cb(RegistrationResponse::error, name);
406 0 : JAMI_ERROR("RegistrationResponse::error");
407 0 : } else {
408 1 : Json::Value json;
409 1 : std::string err;
410 1 : Json::CharReaderBuilder rbuilder;
411 :
412 1 : auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
413 2 : if (!reader->parse(response.body.data(),
414 1 : response.body.data() + response.body.size(),
415 : &json,
416 : &err)) {
417 0 : cb(RegistrationResponse::error, name);
418 0 : return;
419 : }
420 1 : auto success = json["success"].asBool();
421 3 : JAMI_DEBUG("Got reply for registration of {} {}: {}",
422 : name, addr, success ? "success" : "failure");
423 1 : if (success) {
424 1 : std::lock_guard l(cacheLock_);
425 1 : addrCache_.emplace(name, addr);
426 1 : nameCache_.emplace(addr, name);
427 1 : }
428 1 : cb(success ? RegistrationResponse::success : RegistrationResponse::error, name);
429 1 : }
430 1 : std::lock_guard lk(requestsMtx_);
431 1 : if (auto req = response.request.lock())
432 1 : requests_.erase(req);
433 1 : });
434 : {
435 1 : std::lock_guard lk(requestsMtx_);
436 1 : requests_.emplace(request);
437 1 : }
438 1 : request->send();
439 0 : } catch (const std::exception& e) {
440 0 : JAMI_ERROR("Error when performing name registration: {}", e.what());
441 0 : cb(RegistrationResponse::error, name);
442 : {
443 0 : std::lock_guard l(cacheLock_);
444 0 : pendingRegistrations_.erase(name);
445 0 : }
446 0 : std::lock_guard lk(requestsMtx_);
447 0 : if (request)
448 0 : requests_.erase(request);
449 0 : }
450 1 : }
451 :
452 : void
453 2 : NameDirectory::scheduleCacheSave()
454 : {
455 : // JAMI_DBG("Scheduling cache save to %s", cachePath_.c_str());
456 4 : std::weak_ptr<Task> task = Manager::instance().scheduler().scheduleIn(
457 6 : [this] { dht::ThreadPool::io().run([this] { saveCache(); }); }, SAVE_INTERVAL);
458 2 : std::swap(saveTask_, task);
459 2 : if (auto old = task.lock())
460 2 : old->cancel();
461 2 : }
462 :
463 : void
464 2 : NameDirectory::saveCache()
465 : {
466 2 : dhtnet::fileutils::recursive_mkdir(fileutils::get_cache_dir() / CACHE_DIRECTORY);
467 2 : std::lock_guard lock(dhtnet::fileutils::getFileLock(cachePath_));
468 2 : std::ofstream file(cachePath_, std::ios::trunc | std::ios::binary);
469 : {
470 2 : std::lock_guard l(cacheLock_);
471 2 : msgpack::pack(file, nameCache_);
472 2 : }
473 6 : JAMI_DEBUG("Saved {:d} name-address mappings to {}",
474 : nameCache_.size(), cachePath_);
475 2 : }
476 :
477 : void
478 25 : NameDirectory::loadCache()
479 : {
480 25 : msgpack::unpacker pac;
481 :
482 : // read file
483 : {
484 25 : std::lock_guard lock(dhtnet::fileutils::getFileLock(cachePath_));
485 25 : std::ifstream file(cachePath_);
486 25 : if (!file.is_open()) {
487 75 : JAMI_DEBUG("Unable to load {}", cachePath_);
488 25 : return;
489 : }
490 0 : std::string line;
491 0 : while (std::getline(file, line)) {
492 0 : pac.reserve_buffer(line.size());
493 0 : memcpy(pac.buffer(), line.data(), line.size());
494 0 : pac.buffer_consumed(line.size());
495 : }
496 50 : }
497 :
498 : // load values
499 0 : std::lock_guard l(cacheLock_);
500 0 : msgpack::object_handle oh;
501 0 : if (pac.next(oh))
502 0 : oh.get().convert(nameCache_);
503 0 : for (const auto& m : nameCache_)
504 0 : addrCache_.emplace(m.second, m.first);
505 0 : JAMI_DEBUG("Loaded {:d} name-address mappings", nameCache_.size());
506 25 : }
507 :
508 : } // namespace jami
|