| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include "common/dns_utils.h" |
| 30 | // check local first (in the event of static or in-source compilation of libunbound) |
| 31 | #include "unbound.h" |
| 32 | |
| 33 | #include <deque> |
| 34 | #include <set> |
| 35 | #include <stdlib.h> |
| 36 | #include "include_base_utils.h" |
| 37 | #include "common/threadpool.h" |
| 38 | #include "crypto/crypto.h" |
| 39 | #include <boost/thread/mutex.hpp> |
| 40 | #include <boost/algorithm/string/join.hpp> |
| 41 | #include <boost/optional.hpp> |
| 42 | #include <boost/utility/string_ref.hpp> |
| 43 | using namespace epee; |
| 44 | |
| 45 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 46 | #define MONERO_DEFAULT_LOG_CATEGORY "net.dns" |
| 47 | |
| 48 | static const char *DEFAULT_DNS_PUBLIC_ADDR[] = |
| 49 | { |
| 50 | "194.150.168.168", // CCC (Germany) |
| 51 | "80.67.169.40", // FDN (France) |
| 52 | "89.233.43.71", // http://censurfridns.dk (Denmark) |
| 53 | "109.69.8.51", // punCAT (Spain) |
| 54 | "193.58.251.251", // SkyDNS (Russia) |
| 55 | }; |
| 56 | |
| 57 | static boost::mutex instance_lock; |
| 58 | |
| 59 | namespace |
| 60 | { |
| 61 | |
| 62 | /* |
| 63 | * The following two functions were taken from unbound-anchor.c, from |
| 64 | * the unbound library packaged with this source. The license and source |
| 65 | * can be found in $PROJECT_ROOT/external/unbound |
| 66 | */ |
| 67 | |
| 68 | /* Cert builtin commented out until it's used, as the compiler complains |
| 69 | |
| 70 | // return the built in root update certificate |
| 71 | static const char* |
| 72 | get_builtin_cert(void) |
| 73 | { |
| 74 | return |
| 75 | // The ICANN CA fetched at 24 Sep 2010. Valid to 2028 |
| 76 | "-----BEGIN CERTIFICATE-----\n" |
| 77 | "MIIDdzCCAl+gAwIBAgIBATANBgkqhkiG9w0BAQsFADBdMQ4wDAYDVQQKEwVJQ0FO\n" |
| 78 | "TjEmMCQGA1UECxMdSUNBTk4gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkxFjAUBgNV\n" |
| 79 | "BAMTDUlDQU5OIFJvb3QgQ0ExCzAJBgNVBAYTAlVTMB4XDTA5MTIyMzA0MTkxMloX\n" |
| 80 | "DTI5MTIxODA0MTkxMlowXTEOMAwGA1UEChMFSUNBTk4xJjAkBgNVBAsTHUlDQU5O\n" |
| 81 | "IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRYwFAYDVQQDEw1JQ0FOTiBSb290IENB\n" |
| 82 | "MQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKDb\n" |
| 83 | "cLhPNNqc1NB+u+oVvOnJESofYS9qub0/PXagmgr37pNublVThIzyLPGCJ8gPms9S\n" |
| 84 | "G1TaKNIsMI7d+5IgMy3WyPEOECGIcfqEIktdR1YWfJufXcMReZwU4v/AdKzdOdfg\n" |
| 85 | "ONiwc6r70duEr1IiqPbVm5T05l1e6D+HkAvHGnf1LtOPGs4CHQdpIUcy2kauAEy2\n" |
| 86 | "paKcOcHASvbTHK7TbbvHGPB+7faAztABLoneErruEcumetcNfPMIjXKdv1V1E3C7\n" |
| 87 | "MSJKy+jAqqQJqjZoQGB0necZgUMiUv7JK1IPQRM2CXJllcyJrm9WFxY0c1KjBO29\n" |
| 88 | "iIKK69fcglKcBuFShUECAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8B\n" |
| 89 | "Af8EBAMCAf4wHQYDVR0OBBYEFLpS6UmDJIZSL8eZzfyNa2kITcBQMA0GCSqGSIb3\n" |
| 90 | "DQEBCwUAA4IBAQAP8emCogqHny2UYFqywEuhLys7R9UKmYY4suzGO4nkbgfPFMfH\n" |
| 91 | "6M+Zj6owwxlwueZt1j/IaCayoKU3QsrYYoDRolpILh+FPwx7wseUEV8ZKpWsoDoD\n" |
| 92 | "2JFbLg2cfB8u/OlE4RYmcxxFSmXBg0yQ8/IoQt/bxOcEEhhiQ168H2yE5rxJMt9h\n" |
| 93 | "15nu5JBSewrCkYqYYmaxyOC3WrVGfHZxVI7MpIFcGdvSb2a1uyuua8l0BKgk3ujF\n" |
| 94 | "0/wsHNeP22qNyVO+XVBzrM8fk8BSUFuiT/6tZTYXRtEt5aKQZgXbKU5dUF3jT9qg\n" |
| 95 | "j/Br5BZw3X/zd325TvnswzMC1+ljLzHnQGGk\n" |
| 96 | "-----END CERTIFICATE-----\n" |
| 97 | ; |
| 98 | } |
| 99 | */ |
| 100 | |
| 101 | /** return the built in root DS trust anchor */ |
| 102 | static const char* const* |
| 103 | get_builtin_ds(void) |
| 104 | { |
| 105 | static const char * const ds[] = |
| 106 | { |
| 107 | ". IN DS 19036 8 2 49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5\n", |
| 108 | ". IN DS 20326 8 2 E06D44B80B8F1D39A95C0B0D7C65D08458E880409BBC683457104237C7F8EC8D\n", |
| 109 | NULL |
| 110 | }; |
| 111 | return ds; |
| 112 | } |
| 113 | |
| 114 | /************************************************************ |
| 115 | ************************************************************ |
| 116 | ***********************************************************/ |
| 117 | |
| 118 | } // anonymous namespace |
| 119 | |
| 120 | namespace tools |
| 121 | { |
| 122 | |
| 123 | static const char *get_record_name(int record_type) |
| 124 | { |
| 125 | switch (record_type) |
| 126 | { |
| 127 | case DNS_TYPE_A: return "A"; |
| 128 | case DNS_TYPE_TXT: return "TXT"; |
| 129 | case DNS_TYPE_AAAA: return "AAAA"; |
| 130 | case DNS_TYPE_TLSA: return "TLSA"; |
| 131 | default: return "unknown"; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // fuck it, I'm tired of dealing with getnameinfo()/inet_ntop/etc |
| 136 | boost::optional<std::string> ipv4_to_string(const char* src, size_t len) |
| 137 | { |
| 138 | if (len < 4) |
| 139 | { |
| 140 | MERROR("Invalid IPv4 address: " << std::string(src, len)); |
| 141 | return boost::none; |
| 142 | } |
| 143 | |
| 144 | std::stringstream ss; |
| 145 | unsigned int bytes[4]; |
| 146 | for (int i = 0; i < 4; i++) |
| 147 | { |
| 148 | unsigned char a = src[i]; |
| 149 | bytes[i] = a; |
| 150 | } |
| 151 | ss << bytes[0] << "." |
| 152 | << bytes[1] << "." |
| 153 | << bytes[2] << "." |
| 154 | << bytes[3]; |
| 155 | return ss.str(); |
| 156 | } |
| 157 | |
| 158 | // this obviously will need to change, but is here to reflect the above |
| 159 | // stop-gap measure and to make the tests pass at least... |
| 160 | boost::optional<std::string> ipv6_to_string(const char* src, size_t len) |
| 161 | { |
| 162 | if (len < 8) |
| 163 | { |
| 164 | MERROR("Invalid IPv4 address: " << std::string(src, len)); |
| 165 | return boost::none; |
| 166 | } |
| 167 | |
| 168 | std::stringstream ss; |
| 169 | unsigned int bytes[8]; |
| 170 | for (int i = 0; i < 8; i++) |
| 171 | { |
| 172 | unsigned char a = src[i]; |
| 173 | bytes[i] = a; |
| 174 | } |
| 175 | ss << bytes[0] << ":" |
| 176 | << bytes[1] << ":" |
| 177 | << bytes[2] << ":" |
| 178 | << bytes[3] << ":" |
| 179 | << bytes[4] << ":" |
| 180 | << bytes[5] << ":" |
| 181 | << bytes[6] << ":" |
| 182 | << bytes[7]; |
| 183 | return ss.str(); |
| 184 | } |
| 185 | |
| 186 | boost::optional<std::string> txt_to_string(const char* src, size_t len) |
| 187 | { |
| 188 | if (len == 0) |
| 189 | return boost::none; |
| 190 | return std::string(src+1, len-1); |
| 191 | } |
| 192 | |
| 193 | boost::optional<std::string> tlsa_to_string(const char* src, size_t len) |
| 194 | { |
| 195 | if (len < 4) |
| 196 | return boost::none; |
| 197 | return std::string(src, len); |
| 198 | } |
| 199 | |
| 200 | // custom smart pointer. |
| 201 | // TODO: see if std::auto_ptr and the like support custom destructors |
| 202 | template<typename type, void (*freefunc)(type*)> |
| 203 | class scoped_ptr |
| 204 | { |
| 205 | public: |
| 206 | scoped_ptr(): |
| 207 | ptr(nullptr) |
| 208 | { |
| 209 | } |
| 210 | scoped_ptr(type *p): |
| 211 | ptr(p) |
| 212 | { |
| 213 | } |
| 214 | ~scoped_ptr() |
| 215 | { |
| 216 | freefunc(ptr); |
| 217 | } |
| 218 | operator type *() { return ptr; } |
| 219 | type **operator &() { return &ptr; } |
| 220 | type *operator->() { return ptr; } |
| 221 | operator const type*() const { return &ptr; } |
| 222 | |
| 223 | private: |
| 224 | type* ptr; |
| 225 | }; |
| 226 | |
| 227 | typedef class scoped_ptr<ub_result,ub_resolve_free> ub_result_ptr; |
| 228 | |
| 229 | struct DNSResolverData |
| 230 | { |
| 231 | ub_ctx* m_ub_context; |
| 232 | }; |
| 233 | |
| 234 | // work around for bug https://www.nlnetlabs.nl/bugs-script/show_bug.cgi?id=515 needed for it to compile on e.g. Debian 7 |
| 235 | class string_copy { |
| 236 | public: |
| 237 | string_copy(const char *s): str(strdup(s)) {} |
| 238 | ~string_copy() { free(str); } |
| 239 | operator char*() { return str; } |
| 240 | |
| 241 | public: |
| 242 | char *str; |
| 243 | }; |
| 244 | |
| 245 | static void add_anchors(ub_ctx *ctx) |
| 246 | { |
| 247 | const char * const *ds = ::get_builtin_ds(); |
| 248 | while (*ds) |
| 249 | { |
| 250 | MINFO("adding trust anchor: " << *ds); |
| 251 | ub_ctx_add_ta(ctx, string_copy(*ds++)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | DNSResolver::DNSResolver() : m_data(new DNSResolverData()) |
| 256 | { |
| 257 | int use_dns_public = 0; |
| 258 | std::vector<std::string> dns_public_addr; |
| 259 | const char *DNS_PUBLIC = getenv("DNS_PUBLIC"); |
| 260 | if (DNS_PUBLIC) |
| 261 | { |
| 262 | dns_public_addr = tools::dns_utils::parse_dns_public(DNS_PUBLIC); |
| 263 | if (!dns_public_addr.empty()) |
| 264 | { |
| 265 | MGINFO("Using public DNS server(s): " << boost::join(dns_public_addr, ", ") << " (TCP)"); |
| 266 | use_dns_public = 1; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | MERROR("Failed to parse DNS_PUBLIC"); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // init libunbound context |
| 275 | m_data->m_ub_context = ub_ctx_create(); |
| 276 | |
| 277 | if (use_dns_public) |
| 278 | { |
| 279 | for (const auto &ip: dns_public_addr) |
| 280 | ub_ctx_set_fwd(m_data->m_ub_context, string_copy(ip.c_str())); |
| 281 | ub_ctx_set_option(m_data->m_ub_context, string_copy("do-udp:"), string_copy("no")); |
| 282 | ub_ctx_set_option(m_data->m_ub_context, string_copy("do-tcp:"), string_copy("yes")); |
| 283 | } |
| 284 | else { |
| 285 | // look for "/etc/resolv.conf" and "/etc/hosts" or platform equivalent |
| 286 | ub_ctx_resolvconf(m_data->m_ub_context, NULL); |
| 287 | ub_ctx_hosts(m_data->m_ub_context, NULL); |
| 288 | } |
| 289 | |
| 290 | add_anchors(m_data->m_ub_context); |
| 291 | |
| 292 | if (!DNS_PUBLIC) |
| 293 | { |
| 294 | // if no DNS_PUBLIC specified, we try a lookup to what we know |
| 295 | // should be a valid DNSSEC record, and switch to known good |
| 296 | // DNSSEC resolvers if verification fails |
| 297 | bool available, valid; |
| 298 | static const char *probe_hostname = "updates.moneropulse.org"; |
| 299 | auto records = get_txt_record(probe_hostname, available, valid); |
| 300 | if (!valid) |
| 301 | { |
| 302 | MINFO("Failed to verify DNSSEC record from " << probe_hostname << ", falling back to TCP with well known DNSSEC resolvers"); |
| 303 | ub_ctx_delete(m_data->m_ub_context); |
| 304 | m_data->m_ub_context = ub_ctx_create(); |
| 305 | add_anchors(m_data->m_ub_context); |
| 306 | for (const auto &ip: DEFAULT_DNS_PUBLIC_ADDR) |
| 307 | ub_ctx_set_fwd(m_data->m_ub_context, string_copy(ip)); |
| 308 | ub_ctx_set_option(m_data->m_ub_context, string_copy("do-udp:"), string_copy("no")); |
| 309 | ub_ctx_set_option(m_data->m_ub_context, string_copy("do-tcp:"), string_copy("yes")); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | DNSResolver::~DNSResolver() |
| 315 | { |
| 316 | if (m_data) |
| 317 | { |
| 318 | if (m_data->m_ub_context != NULL) |
| 319 | { |
| 320 | ub_ctx_delete(m_data->m_ub_context); |
| 321 | } |
| 322 | delete m_data; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | std::vector<std::string> DNSResolver::get_record(const std::string& url, int record_type, boost::optional<std::string> (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid) |
| 327 | { |
| 328 | std::vector<std::string> addresses; |
| 329 | dnssec_available = false; |
| 330 | dnssec_valid = false; |
| 331 | |
| 332 | // destructor takes care of cleanup |
| 333 | ub_result_ptr result; |
| 334 | |
| 335 | MDEBUG("Performing DNSSEC " << get_record_name(record_type) << " record query for " << url); |
| 336 | |
| 337 | // call DNS resolver, blocking. if return value not zero, something went wrong |
| 338 | if (!ub_resolve(m_data->m_ub_context, string_copy(url.c_str()), record_type, DNS_CLASS_IN, &result)) |
| 339 | { |
| 340 | dnssec_available = (result->secure || result->bogus); |
| 341 | dnssec_valid = result->secure && !result->bogus; |
| 342 | if (dnssec_available && !dnssec_valid) |
| 343 | MWARNING("Invalid DNSSEC " << get_record_name(record_type) << " record signature for " << url << ": " << result->why_bogus); |
| 344 | if (result->havedata) |
| 345 | { |
| 346 | for (size_t i=0; result->data[i] != NULL; i++) |
| 347 | { |
| 348 | boost::optional<std::string> res = (*reader)(result->data[i], result->len[i]); |
| 349 | if (res) |
| 350 | { |
| 351 | // do not dump dns record directly from dns into log |
| 352 | MINFO("Found " << get_record_name(record_type) << " record for " << url); |
| 353 | addresses.push_back(std::move(*res)); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return addresses; |
| 360 | } |
| 361 | |
| 362 | std::vector<std::string> DNSResolver::get_ipv4(const std::string& url, bool& dnssec_available, bool& dnssec_valid) |
| 363 | { |
| 364 | return get_record(url, DNS_TYPE_A, ipv4_to_string, dnssec_available, dnssec_valid); |
| 365 | } |
| 366 | |
| 367 | std::vector<std::string> DNSResolver::get_ipv6(const std::string& url, bool& dnssec_available, bool& dnssec_valid) |
| 368 | { |
| 369 | return get_record(url, DNS_TYPE_AAAA, ipv6_to_string, dnssec_available, dnssec_valid); |
| 370 | } |
| 371 | |
| 372 | std::vector<std::string> DNSResolver::get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid) |
| 373 | { |
| 374 | return get_record(url, DNS_TYPE_TXT, txt_to_string, dnssec_available, dnssec_valid); |
| 375 | } |
| 376 | |
| 377 | std::vector<std::string> DNSResolver::get_tlsa_tcp_record(const boost::string_ref url, const boost::string_ref port, bool& dnssec_available, bool& dnssec_valid) |
| 378 | { |
| 379 | std::string service_addr; |
| 380 | service_addr.reserve(url.size() + port.size() + 7); |
| 381 | service_addr.push_back('_'); |
| 382 | service_addr.append(port.data(), port.size()); |
| 383 | service_addr.append("._tcp."); |
| 384 | service_addr.append(url.data(), url.size()); |
| 385 | return get_record(service_addr, DNS_TYPE_TLSA, tlsa_to_string, dnssec_available, dnssec_valid); |
| 386 | } |
| 387 | |
| 388 | std::string DNSResolver::get_dns_format_from_oa_address(const std::string& oa_addr) |
| 389 | { |
| 390 | std::string addr(oa_addr); |
| 391 | auto first_at = addr.find("@"); |
| 392 | if (first_at == std::string::npos) |
| 393 | return addr; |
| 394 | |
| 395 | // convert [email protected] to name.domain.tld |
| 396 | addr.replace(first_at, 1, "."); |
| 397 | |
| 398 | return addr; |
| 399 | } |
| 400 | |
| 401 | DNSResolver& DNSResolver::instance() |
| 402 | { |
| 403 | boost::lock_guard<boost::mutex> lock(instance_lock); |
| 404 | |
| 405 | static DNSResolver staticInstance; |
| 406 | return staticInstance; |
| 407 | } |
| 408 | |
| 409 | DNSResolver DNSResolver::create() |
| 410 | { |
| 411 | return DNSResolver(); |
| 412 | } |
| 413 | |
| 414 | namespace dns_utils |
| 415 | { |
| 416 | |
| 417 | //----------------------------------------------------------------------- |
| 418 | // TODO: parse the string in a less stupid way, probably with regex |
| 419 | std::string address_from_txt_record(const std::string& s) |
| 420 | { |
| 421 | // make sure the txt record has "oa1:xmr" and find it |
| 422 | auto pos = s.find("oa1:xmr"); |
| 423 | if (pos == std::string::npos) |
| 424 | return {}; |
| 425 | // search from there to find "recipient_address=" |
| 426 | pos = s.find("recipient_address=", pos); |
| 427 | if (pos == std::string::npos) |
| 428 | return {}; |
| 429 | pos += 18; // move past "recipient_address=" |
| 430 | // find the next semicolon |
| 431 | auto pos2 = s.find(";", pos); |
| 432 | if (pos2 != std::string::npos) |
| 433 | { |
| 434 | // length of address == 95, we can at least validate that much here |
| 435 | if (pos2 - pos == 95) |
| 436 | { |
| 437 | return s.substr(pos, 95); |
| 438 | } |
| 439 | else if (pos2 - pos == 106) // length of address == 106 --> integrated address |
| 440 | { |
| 441 | return s.substr(pos, 106); |
| 442 | } |
| 443 | } |
| 444 | return {}; |
| 445 | } |
| 446 | /** |
| 447 | * @brief gets a monero address from the TXT record of a DNS entry |
| 448 | * |
| 449 | * gets the monero address from the TXT record of the DNS entry associated |
| 450 | * with <url>. If this lookup fails, or the TXT record does not contain an |
| 451 | * XMR address in the correct format, returns an empty string. <dnssec_valid> |
| 452 | * will be set true or false according to whether or not the DNS query passes |
| 453 | * DNSSEC validation. |
| 454 | * |
| 455 | * @param url the url to look up |
| 456 | * @param dnssec_valid return-by-reference for DNSSEC status of query |
| 457 | * |
| 458 | * @return a monero address (as a string) or an empty string |
| 459 | */ |
| 460 | std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid) |
| 461 | { |
| 462 | std::vector<std::string> addresses; |
| 463 | // get txt records |
| 464 | bool dnssec_available, dnssec_isvalid; |
| 465 | std::string oa_addr = DNSResolver::instance().get_dns_format_from_oa_address(url); |
| 466 | auto records = DNSResolver::instance().get_txt_record(oa_addr, dnssec_available, dnssec_isvalid); |
| 467 | |
| 468 | // TODO: update this to allow for conveying that dnssec was not available |
| 469 | if (dnssec_available && dnssec_isvalid) |
| 470 | { |
| 471 | dnssec_valid = true; |
| 472 | } |
| 473 | else dnssec_valid = false; |
| 474 | |
| 475 | // for each txt record, try to find a monero address in it. |
| 476 | for (auto& rec : records) |
| 477 | { |
| 478 | std::string addr = address_from_txt_record(rec); |
| 479 | if (addr.size()) |
| 480 | { |
| 481 | addresses.push_back(addr); |
| 482 | } |
| 483 | } |
| 484 | return addresses; |
| 485 | } |
| 486 | |
| 487 | std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm) |
| 488 | { |
| 489 | // attempt to get address from dns query |
| 490 | auto addresses = addresses_from_url(url, dnssec_valid); |
| 491 | if (addresses.empty()) |
| 492 | { |
| 493 | LOG_ERROR("wrong address: " << url); |
| 494 | return {}; |
| 495 | } |
| 496 | return dns_confirm(url, addresses, dnssec_valid); |
| 497 | } |
| 498 | |
| 499 | bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std::vector<std::string> &dns_urls) |
| 500 | { |
| 501 | // Prevent infinite recursion when distributing |
| 502 | if (dns_urls.empty()) return false; |
| 503 | |
| 504 | std::vector<std::set<std::string> > records; |
| 505 | records.resize(dns_urls.size()); |
| 506 | |
| 507 | size_t first_index = crypto::rand_idx(dns_urls.size()); |
| 508 | |
| 509 | // send all requests in parallel |
| 510 | std::deque<bool> avail(dns_urls.size(), false), valid(dns_urls.size(), false); |
| 511 | tools::threadpool& tpool = tools::threadpool::getInstanceForIO(); |
| 512 | tools::threadpool::waiter waiter(tpool); |
| 513 | for (size_t n = 0; n < dns_urls.size(); ++n) |
| 514 | { |
| 515 | tpool.submit(&waiter,[n, dns_urls, &records, &avail, &valid](){ |
| 516 | const auto res = tools::DNSResolver::instance().get_txt_record(dns_urls[n], avail[n], valid[n]); |
| 517 | for (const auto &s: res) |
| 518 | records[n].insert(s); |
| 519 | }); |
| 520 | } |
| 521 | waiter.wait(); |
| 522 | |
| 523 | size_t cur_index = first_index; |
| 524 | do |
| 525 | { |
| 526 | const std::string &url = dns_urls[cur_index]; |
| 527 | if (!avail[cur_index]) |
| 528 | { |
| 529 | records[cur_index].clear(); |
| 530 | LOG_PRINT_L2("DNSSEC not available for hostname: " << url << ", skipping."); |
| 531 | } |
| 532 | if (!valid[cur_index]) |
| 533 | { |
| 534 | records[cur_index].clear(); |
| 535 | LOG_PRINT_L2("DNSSEC validation failed for hostname: " << url << ", skipping."); |
| 536 | } |
| 537 | |
| 538 | cur_index++; |
| 539 | if (cur_index == dns_urls.size()) |
| 540 | { |
| 541 | cur_index = 0; |
| 542 | } |
| 543 | } while (cur_index != first_index); |
| 544 | |
| 545 | size_t num_valid_records = 0; |
| 546 | |
| 547 | for( const auto& record_set : records) |
| 548 | { |
| 549 | if (record_set.size() != 0) |
| 550 | { |
| 551 | num_valid_records++; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (num_valid_records < 2) |
| 556 | { |
| 557 | LOG_PRINT_L0("WARNING: no two valid DNS TXT records were received"); |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | typedef std::map<std::set<std::string>, uint32_t> map_t; |
| 562 | map_t record_count; |
| 563 | for (const auto &e: records) |
| 564 | { |
| 565 | if (!e.empty()) |
| 566 | ++record_count[e]; |
| 567 | } |
| 568 | |
| 569 | map_t::const_iterator good_record = record_count.end(); |
| 570 | for (map_t::const_iterator i = record_count.begin(); i != record_count.end(); ++i) |
| 571 | { |
| 572 | if (good_record == record_count.end() || i->second > good_record->second) |
| 573 | good_record = i; |
| 574 | } |
| 575 | |
| 576 | MDEBUG("Found " << (good_record == record_count.end() ? 0 : good_record->second) << "/" << dns_urls.size() << " matching records from " << num_valid_records << " valid records"); |
| 577 | if (good_record == record_count.end() || good_record->second < dns_urls.size() / 2 + 1) |
| 578 | { |
| 579 | LOG_PRINT_L0("WARNING: no majority of DNS TXT records matched (only " << good_record->second << "/" << dns_urls.size() << ")"); |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | good_records = {}; |
| 584 | for (const auto &s: good_record->first) |
| 585 | good_records.push_back(s); |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | std::vector<std::string> parse_dns_public(const char *s) |
| 590 | { |
| 591 | unsigned ip0, ip1, ip2, ip3; |
| 592 | char c; |
| 593 | std::vector<std::string> dns_public_addr; |
| 594 | if (!strcmp(s, "tcp")) |
| 595 | { |
| 596 | for (size_t i = 0; i < sizeof(DEFAULT_DNS_PUBLIC_ADDR) / sizeof(DEFAULT_DNS_PUBLIC_ADDR[0]); ++i) |
| 597 | dns_public_addr.push_back(DEFAULT_DNS_PUBLIC_ADDR[i]); |
| 598 | LOG_PRINT_L0("Using default public DNS server(s): " << boost::join(dns_public_addr, ", ") << " (TCP)"); |
| 599 | } |
| 600 | else if (sscanf(s, "tcp://%u.%u.%u.%u%c", &ip0, &ip1, &ip2, &ip3, &c) == 4) |
| 601 | { |
| 602 | if (ip0 > 255 || ip1 > 255 || ip2 > 255 || ip3 > 255) |
| 603 | { |
| 604 | MERROR("Invalid IP: " << s << ", using default"); |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | dns_public_addr.push_back(std::string(s + strlen("tcp://"))); |
| 609 | } |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | MERROR("Invalid DNS_PUBLIC contents, ignored"); |
| 614 | } |
| 615 | return dns_public_addr; |
| 616 | } |
| 617 | |
| 618 | } // namespace tools::dns_utils |
| 619 | |
| 620 | } // namespace tools |