| 1 | // Copyright (c) 2017-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 <boost/algorithm/string.hpp> |
| 30 | #include "misc_log_ex.h" |
| 31 | #include "util.h" |
| 32 | #include "dns_utils.h" |
| 33 | #include "updates.h" |
| 34 | |
| 35 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 36 | #define MONERO_DEFAULT_LOG_CATEGORY "updates" |
| 37 | |
| 38 | namespace tools |
| 39 | { |
| 40 | bool check_updates(const std::string &software, const std::string &buildtag, std::string &version, std::string &hash) |
| 41 | { |
| 42 | std::vector<std::string> records; |
| 43 | bool found = false; |
| 44 | |
| 45 | MDEBUG("Checking updates for " << buildtag << " " << software); |
| 46 | |
| 47 | // All four MoneroPulse domains have DNSSEC on and valid |
| 48 | static const std::vector<std::string> dns_urls = { |
| 49 | "updates.moneropulse.org", |
| 50 | "updates.moneropulse.net", |
| 51 | "updates.moneropulse.fr", |
| 52 | "updates.moneropulse.de", |
| 53 | "updates.moneropulse.co", |
| 54 | "updates.moneropulse.ch", |
| 55 | "updates.moneropulse.se" |
| 56 | }; |
| 57 | |
| 58 | if (!tools::dns_utils::load_txt_records_from_dns(records, dns_urls)) |
| 59 | return false; |
| 60 | |
| 61 | for (const auto& record : records) |
| 62 | { |
| 63 | std::vector<std::string> fields; |
| 64 | boost::split(fields, record, boost::is_any_of(":")); |
| 65 | if (fields.size() != 4) |
| 66 | { |
| 67 | MWARNING("Updates record does not have 4 fields: " << record); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if (software != fields[0] || buildtag != fields[1]) |
| 72 | continue; |
| 73 | |
| 74 | bool alnum = true; |
| 75 | for (auto c: fields[3]) |
| 76 | if (!isalnum(c)) |
| 77 | alnum = false; |
| 78 | if (fields[3].size() != 64 && !alnum) |
| 79 | { |
| 80 | MWARNING("Invalid hash: " << fields[3]); |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | // use highest version |
| 85 | if (found) |
| 86 | { |
| 87 | int cmp = vercmp(version.c_str(), fields[2].c_str()); |
| 88 | if (cmp > 0) |
| 89 | continue; |
| 90 | if (cmp == 0 && hash != fields[3]) |
| 91 | MWARNING("Two matches found for " << software << " version " << version << " on " << buildtag); |
| 92 | } |
| 93 | |
| 94 | version = fields[2]; |
| 95 | hash = fields[3]; |
| 96 | |
| 97 | MINFO("Found new version " << version << " with hash " << hash); |
| 98 | found = true; |
| 99 | } |
| 100 | return found; |
| 101 | } |
| 102 | |
| 103 | std::string get_update_url(const std::string &software, const std::string &subdir, const std::string &buildtag, const std::string &version, bool user) |
| 104 | { |
| 105 | const char *base = user ? "https://downloads.getmonero.org/" : "https://updates.getmonero.org/"; |
| 106 | #ifdef _WIN32 |
| 107 | static const char *extension = strncmp(buildtag.c_str(), "source", 6) ? (strncmp(buildtag.c_str(), "install-", 8) ? ".zip" : ".exe") : ".tar.bz2"; |
| 108 | #elif defined(__APPLE__) |
| 109 | static const char *extension = strncmp(software.c_str(), "monero-gui", 10) ? ".tar.bz2" : ".dmg"; |
| 110 | #else |
| 111 | static const char extension[] = ".tar.bz2"; |
| 112 | #endif |
| 113 | |
| 114 | std::string url; |
| 115 | |
| 116 | url = base; |
| 117 | if (!subdir.empty()) |
| 118 | url += subdir + "/"; |
| 119 | url = url + software + "-" + buildtag + "-v" + version + extension; |
| 120 | return url; |
| 121 | } |
| 122 | } |