| 1 | /* |
| 2 | * Copyright (C) 2000 Onlyer ([email protected]) |
| 3 | * Copyright (C) 2001 Ross Combs ([email protected]) |
| 4 | * Copyright (C) 2002 Gianluigi Tiesi ([email protected]) |
| 5 | * Copyright (C) 2004 CreepLord ([email protected]) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | #include "common/setup_before.h" |
| 22 | #include "versioncheck.h" |
| 23 | |
| 24 | #include <cctype> |
| 25 | #include <cerrno> |
| 26 | #include <chrono> |
| 27 | #include <climits> |
| 28 | #include <cstdint> |
| 29 | #include <cstdio> |
| 30 | #include <cstdlib> |
| 31 | #include <cstring> |
| 32 | #include <ctime> |
| 33 | #include <fstream> |
| 34 | #include <iomanip> |
| 35 | #include <regex> |
| 36 | #include <sstream> |
| 37 | #include <string> |
| 38 | #include <tuple> |
| 39 | #include <unordered_map> |
| 40 | |
| 41 | #include "common/eventlog.h" |
| 42 | |
| 43 | #include "common/field_sizes.h" |
| 44 | #include "common/format.h" |
| 45 | #include "common/hash_tuple.hpp" |
| 46 | #include "common/proginfo.h" |
| 47 | #include "compat/strcasecmp.h" |
| 48 | #include "common/token.h" |
| 49 | #include "common/util.h" |
| 50 | |
| 51 | |
| 52 | #include "json/json.hpp" |
| 53 | |
| 54 | #include "prefs.h" |
| 55 | #include "common/setup_after.h" |
| 56 | |
| 57 | |
| 58 | using json = nlohmann::json; |
| 59 | |
| 60 | |
| 61 | namespace pvpgn |
| 62 | { |
| 63 | |
| 64 | namespace bnetd |
| 65 | { |
| 66 | std::unordered_map<std::tuple<std::uint32_t, std::uint32_t, t_tag, t_tag>, VersionCheck, hash_tuple::hash<std::tuple<std::uint32_t, std::uint32_t, t_tag, t_tag>>> vc_entries; |
| 67 | std::unordered_map<std::tuple<t_tag, t_tag, std::uint32_t>, std::tuple<std::string, std::string>, hash_tuple::hash<std::tuple<t_tag, t_tag, std::uint32_t>>> cr_entries; |
| 68 | |
| 69 | bool versioncheck_conf_is_loaded = false; |
| 70 | |
| 71 | // bool validate_file_metadata_format(const std::string& metadata); |
| 72 | |
| 73 | |
| 74 | bool load_versioncheck_conf(const std::string& filename) |
| 75 | { |
| 76 | if (versioncheck_conf_is_loaded) |
| 77 | { |
| 78 | eventlog(eventlog_level_error, __FUNCTION__, "Could not load {}, a versioncheck configuration file is already loaded", filename); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | auto t0 = std::chrono::steady_clock::now(); |
| 83 | |
| 84 | std::ifstream file_stream(filename, std::ios::in); |
| 85 | if (!file_stream.is_open()) |
| 86 | { |
| 87 | eventlog(eventlog_level_error, __FUNCTION__, "Could not open file \"{}\" for reading", filename); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | json jconf; |
| 92 | try |
| 93 | { |
| 94 | file_stream >> jconf; |
| 95 | } |
| 96 | catch (const std::exception& e) |
| 97 | { |
| 98 | eventlog(eventlog_level_error, __FUNCTION__, "Could not parse {}: {}", filename, e.what()); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | vc_entries.reserve(185); |
| 103 | |
| 104 | for (auto jclient : jconf.items()) |
| 105 | { |
| 106 | t_tag client = tag_str_to_uint(jclient.key().c_str()); |
| 107 | if (!tag_check_client(client)) |
| 108 | { |
| 109 | eventlog(eventlog_level_error, __FUNCTION__, "Invalid client: {}", jclient.key()); |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | for (auto jarchitecture : jclient.value().items()) |
| 114 | { |
| 115 | t_tag architecture = tag_str_to_uint(jarchitecture.key().c_str()); |
| 116 | if (!tag_check_arch(architecture)) |
| 117 | { |
| 118 | eventlog(eventlog_level_error, __FUNCTION__, "Invalid architecture: {}", jarchitecture.key()); |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | for (auto jversion_id : jarchitecture.value().items()) |
| 123 | { |
| 124 | std::uint32_t version_id; |
| 125 | try |
| 126 | { |
| 127 | version_id = std::stoul(jversion_id.key(), nullptr, 0); |
| 128 | } |
| 129 | catch (const std::exception& e) |
| 130 | { |
| 131 | eventlog(eventlog_level_error, __FUNCTION__, "Invalid version id \"{}\": {}", jversion_id.key(), e.what()); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | // FIXME: do error checking for these |
| 136 | std::string checkrevision_filename = jversion_id.value()["checkRevisionFile"].get<std::string>(); |
| 137 | std::string checkrevision_equation = jversion_id.value()["equation"].get<std::string>(); |
| 138 | |
| 139 | cr_entries.emplace( |
| 140 | std::piecewise_construct, |
| 141 | std::forward_as_tuple(architecture, client, version_id), |
| 142 | std::forward_as_tuple(checkrevision_filename, checkrevision_equation) |
| 143 | ); |
| 144 | |
| 145 | for (auto jentry : jversion_id.value()["entries"]) |
| 146 | { |
| 147 | try |
| 148 | { |
| 149 | VersionCheck entry(jentry["title"].get<std::string>(), |
| 150 | version_id, |
| 151 | jentry["version"].get<std::string>(), |
| 152 | jentry["hash"].get<std::string>(), |
| 153 | architecture, |
| 154 | client, |
| 155 | jentry["versionTag"].get<std::string>() |
| 156 | ); |
| 157 | |
| 158 | vc_entries.insert({ std::make_tuple(entry.m_version_id, entry.m_game_version, entry.m_architecture, entry.m_client), entry }); |
| 159 | } |
| 160 | catch (const std::exception& e) |
| 161 | { |
| 162 | eventlog(eventlog_level_error, __FUNCTION__, "Failed to load versioncheck entry: {}", e.what()); |
| 163 | continue; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | auto t1 = std::chrono::steady_clock::now(); |
| 171 | |
| 172 | eventlog(eventlog_level_info, __FUNCTION__, "Successfully loaded {} versioncheck entries in {} microseconds", vc_entries.size(), std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count()); |
| 173 | |
| 174 | versioncheck_conf_is_loaded = true; |
| 175 | |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | void unload_versioncheck_conf() |
| 180 | { |
| 181 | if (versioncheck_conf_is_loaded) |
| 182 | { |
| 183 | vc_entries.clear(); |
| 184 | |
| 185 | cr_entries.clear(); |
| 186 | |
| 187 | versioncheck_conf_is_loaded = false; |
| 188 | |
| 189 | eventlog(eventlog_level_info, __FUNCTION__, "Successfully unloaded all version check entries"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | std::tuple<std::string, std::string> select_checkrevision(t_tag architecture, t_tag client, std::uint32_t version_id) |
| 194 | { |
| 195 | static const std::tuple<std::string, std::string> default_checkrevision { "ver-IX86-1.mpq", "A=42 B=42 C=42 4 A=A^S B=B^B C=C^C A=A^S" }; |
| 196 | |
| 197 | auto key = cr_entries.find(std::make_tuple(architecture, client, version_id)); |
| 198 | if (key != cr_entries.end()) |
| 199 | { |
| 200 | return key->second; |
| 201 | } |
| 202 | |
| 203 | eventlog(eventlog_level_debug, __FUNCTION__, "Could not find corresponding CheckRevision entry, returning default CheckRevision"); |
| 204 | |
| 205 | return default_checkrevision; |
| 206 | } |
| 207 | |
| 208 | // could use C++17 std::optional here |
| 209 | const VersionCheck* select_versioncheck(t_tag architecture, t_tag client, std::uint32_t version_id, |
| 210 | std::uint32_t checkrevision_version, std::uint32_t checkrevision_checksum) |
| 211 | { |
| 212 | auto it = vc_entries.find(std::make_tuple(version_id, checkrevision_version, architecture, client)); |
| 213 | if (it == vc_entries.end()) |
| 214 | { |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
| 218 | if (it->second.m_checksum != checkrevision_checksum |
| 219 | && !prefs_get_allow_bad_version()) |
| 220 | { |
| 221 | return nullptr; |
| 222 | } |
| 223 | |
| 224 | return &(it->second); |
| 225 | } |
| 226 | |
| 227 | VersionCheck::VersionCheck(const std::string& title, std::uint32_t version_id, const std::string& game_version, |
| 228 | const std::string& checksum, t_tag architecture, t_tag client, const std::string& version_tag) |
| 229 | : m_version_id(version_id), m_architecture(architecture), m_client(client) |
| 230 | { |
| 231 | if (verstr_to_vernum(game_version.c_str(), reinterpret_cast<unsigned long *>(&this->m_game_version)) < 0) |
| 232 | { |
| 233 | throw std::runtime_error("Invalid version \"" + game_version + "\" in entry \"" + title + "\""); |
| 234 | } |
| 235 | |
| 236 | this->m_checksum = std::stoul(checksum, nullptr, 0); |
| 237 | |
| 238 | // FIXME: check for uniqueness |
| 239 | this->m_version_tag = version_tag; |
| 240 | } |
| 241 | |
| 242 | std::string VersionCheck::get_version_tag() const |
| 243 | { |
| 244 | return this->m_version_tag; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | // This function only validates the format of the metadata string |
| 249 | // There is no need to validate the information in the string because validating the checksum and file version is sufficient |
| 250 | bool validate_file_metadata_format(const std::string& metadata) |
| 251 | { |
| 252 | if (metadata.empty()) |
| 253 | { |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | try |
| 258 | { |
| 259 | std::smatch tokens; |
| 260 | if (std::regex_match(metadata, tokens, std::regex{ R"(([[:print:]]+\.exe) (\d\d/\d\d/\d\d \d\d:\d\d:\d\d) (\d+))" }) == false) |
| 261 | { |
| 262 | eventlog(eventlog_level_error, __FUNCTION__, "got invalid file metadata \"{}\"", metadata); |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | catch (const std::regex_error& e) |
| 267 | { |
| 268 | eventlog(eventlog_level_error, __FUNCTION__, "{} (code {})", e.what(), e.code()); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | */ |
| 275 | |
| 276 | } |
| 277 | |
| 278 | } |