The Battle for Wesnoth  1.19.0-dev
forum_user_handler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #ifdef HAVE_MYSQLPP
17 
20 #include "hash.hpp"
21 #include "log.hpp"
22 #include "config.hpp"
23 
24 #include <boost/asio/post.hpp>
25 
26 #include <cstdlib>
27 #include <sstream>
28 
29 static lg::log_domain log_mp_user_handler("mp_user_handler");
30 #define ERR_UH LOG_STREAM(err, log_mp_user_handler)
31 #define WRN_UH LOG_STREAM(warn, log_mp_user_handler)
32 #define LOG_UH LOG_STREAM(info, log_mp_user_handler)
33 #define DBG_UH LOG_STREAM(debug, log_mp_user_handler)
34 
35 namespace {
36  const int USER_INACTIVE = 1;
37  const int USER_IGNORE = 2;
38 }
39 
40 fuh::fuh(const config& c)
41  : conn_(c)
42  , db_users_table_(c["db_users_table"].str())
43  , db_extra_table_(c["db_extra_table"].str())
44  , mp_mod_group_(0)
45 {
46  try {
47  mp_mod_group_ = std::stoi(c["mp_mod_group"].str());
48  } catch(...) {
49  ERR_UH << "Failed to convert the mp_mod_group value of '" << c["mp_mod_group"].str() << "' into an int! Defaulting to " << mp_mod_group_ << ".";
50  }
51 }
52 
53 bool fuh::login(const std::string& name, const std::string& password) {
54  // Retrieve users' password as hash
55  try {
56  std::string hash = get_hashed_password_from_db(name);
57 
58  if(utils::md5::is_valid_hash(hash) || utils::bcrypt::is_valid_prefix(hash)) { // md5 hash
59  return password == hash;
60  } else {
61  ERR_UH << "Invalid hash for user '" << name << "'";
62  return false;
63  }
64  } catch (const error& e) {
65  ERR_UH << "Could not retrieve hash for user '" << name << "' :" << e.message;
66  return false;
67  }
68 }
69 
70 std::string fuh::extract_salt(const std::string& name) {
71 
72  // Some double security, this should never be needed
73  if(!(user_exists(name))) {
74  return "";
75  }
76 
77  std::string hash;
78 
79  try {
80  hash = get_hashed_password_from_db(name);
81  } catch (const error& e) {
82  ERR_UH << "Could not retrieve hash for user '" << name << "' :" << e.message;
83  return "";
84  }
85 
87  return hash.substr(0,12);
88 
90  try {
92  } catch(const utils::hash_error& err) {
93  ERR_UH << "Error getting salt from hash of user '" << name << "': " << err.what();
94  return "";
95  }
96  }
97 
98  return "";
99 }
100 
101 void fuh::user_logged_in(const std::string& name) {
102  conn_.write_user_int("user_lastvisit", name, static_cast<int>(std::time(nullptr)));
103 }
104 
105 bool fuh::user_exists(const std::string& name) {
106  return conn_.user_exists(name);
107 }
108 
109 long fuh::get_forum_id(const std::string& name) {
110  return conn_.get_forum_id(name);
111 }
112 
113 bool fuh::user_is_active(const std::string& name) {
114  int user_type = conn_.get_user_int(db_users_table_, "user_type", name);
115  return user_type != USER_INACTIVE && user_type != USER_IGNORE;
116 }
117 
118 bool fuh::user_is_moderator(const std::string& name) {
119  if(!user_exists(name)){
120  return false;
121  }
122  return conn_.get_user_int(db_extra_table_, "user_is_moderator", name) == 1 || (mp_mod_group_ != 0 && conn_.is_user_in_group(name, mp_mod_group_));
123 }
124 
125 void fuh::set_is_moderator(const std::string& name, const bool& is_moderator) {
126  if(!user_exists(name)){
127  return;
128  }
129  conn_.write_user_int("user_is_moderator", name, is_moderator);
130 }
131 
132 fuh::ban_info fuh::user_is_banned(const std::string& name, const std::string& addr)
133 {
134  ban_check b = conn_.get_ban_info(name, addr);
135  switch(b.get_ban_type())
136  {
137  case BAN_NONE:
138  return {};
139  case BAN_IP:
140  LOG_UH << "User '" << name << "' ip " << addr << " banned by IP address";
141  return { BAN_IP, b.get_ban_duration() };
142  case BAN_USER:
143  LOG_UH << "User '" << name << "' uid " << b.get_user_id() << " banned by uid";
144  return { BAN_USER, b.get_ban_duration() };
145  case BAN_EMAIL:
146  LOG_UH << "User '" << name << "' email " << b.get_email() << " banned by email address";
147  return { BAN_EMAIL, b.get_ban_duration() };
148  default:
149  ERR_UH << "Invalid ban type '" << b.get_ban_type() << "' returned for user '" << name << "'";
150  return {};
151  }
152 }
153 
154 std::string fuh::user_info(const std::string& name) {
155  if(!user_exists(name)) {
156  throw error("No user with the name '" + name + "' exists.");
157  }
158 
159  std::time_t reg_date = get_registrationdate(name);
160  std::time_t ll_date = get_lastlogin(name);
161 
162  std::string reg_string = ctime(&reg_date);
163  std::string ll_string;
164 
165  if(ll_date) {
166  ll_string = ctime(&ll_date);
167  } else {
168  ll_string = "Never\n";
169  }
170 
171  std::stringstream info;
172  info << "Name: " << name << "\n"
173  << "Registered: " << reg_string
174  << "Last login: " << ll_string;
175  if(!user_is_active(name)) {
176  info << "This account is currently inactive.\n";
177  }
178 
179  return info.str();
180 }
181 
182 std::string fuh::get_hashed_password_from_db(const std::string& user) {
183  return conn_.get_user_string(db_users_table_, "user_password", user);
184 }
185 
186 std::string fuh::get_user_email(const std::string& user) {
187  return conn_.get_user_string(db_users_table_, "user_email", user);
188 }
189 
190 void fuh::db_update_addon_download_count(const std::string& instance_version, const std::string& id, const std::string& version) {
191  return conn_.update_addon_download_count(instance_version, id, version);
192 }
193 
194 std::time_t fuh::get_lastlogin(const std::string& user) {
195  return std::time_t(conn_.get_user_int(db_extra_table_, "user_lastvisit", user));
196 }
197 
198 std::time_t fuh::get_registrationdate(const std::string& user) {
199  return std::time_t(conn_.get_user_int(db_users_table_, "user_regdate", user));
200 }
201 
202 std::string fuh::get_uuid(){
203  return conn_.get_uuid();
204 }
205 
206 std::string fuh::get_tournaments(){
207  return conn_.get_tournaments();
208 }
209 
210 void fuh::async_get_and_send_game_history(boost::asio::io_service& io_service, wesnothd::server& s, wesnothd::player_iterator player, int player_id, int offset, std::string& search_game_name, int search_content_type, std::string& search_content) {
211  boost::asio::post([this, &s, player, player_id, offset, &io_service, search_game_name, search_content_type, search_content] {
212  boost::asio::post(io_service, [player, &s, doc = conn_.get_game_history(player_id, offset, search_game_name, search_content_type, search_content)]{
213  s.send_to_player(player, *doc);
214  });
215  });
216 }
217 
218 void fuh::db_insert_game_info(const std::string& uuid, int game_id, const std::string& version, const std::string& name, int reload, int observers, int is_public, int has_password){
219  conn_.insert_game_info(uuid, game_id, version, name, reload, observers, is_public, has_password);
220 }
221 
222 void fuh::db_update_game_end(const std::string& uuid, int game_id, const std::string& replay_location){
223  conn_.update_game_end(uuid, game_id, replay_location);
224 }
225 
226 void fuh::db_insert_game_player_info(const std::string& uuid, int game_id, const std::string& username, int side_number, int is_host, const std::string& faction, const std::string& version, const std::string& source, const std::string& current_user, const std::string& leaders){
227  conn_.insert_game_player_info(uuid, game_id, username, side_number, is_host, faction, version, source, current_user, leaders);
228 }
229 
230 unsigned long long fuh::db_insert_game_content_info(const std::string& uuid, int game_id, const std::string& type, const std::string& name, const std::string& id, const std::string& addon_id, const std::string& addon_version){
231  return conn_.insert_game_content_info(uuid, game_id, type, name, id, addon_id, addon_version);
232 }
233 
234 void fuh::db_set_oos_flag(const std::string& uuid, int game_id){
235  conn_.set_oos_flag(uuid, game_id);
236 }
237 
238 void fuh::async_test_query(boost::asio::io_service& io_service, int limit) {
239  boost::asio::post([this, limit, &io_service] {
240  ERR_UH << "async test query starts!";
241  int i = conn_.async_test_query(limit);
242  boost::asio::post(io_service, [i]{ ERR_UH << "async test query output: " << i; });
243  });
244 }
245 
246 bool fuh::db_topic_id_exists(int topic_id) {
247  return conn_.topic_id_exists(topic_id);
248 }
249 
250 void fuh::db_insert_addon_info(const std::string& instance_version, const std::string& id, const std::string& name, const std::string& type, const std::string& version, bool forum_auth, int topic_id, const std::string uploader) {
251  conn_.insert_addon_info(instance_version, id, name, type, version, forum_auth, topic_id, uploader);
252 }
253 
254 unsigned long long fuh::db_insert_login(const std::string& username, const std::string& ip, const std::string& version) {
255  return conn_.insert_login(username, ip, version);
256 }
257 
258 void fuh::db_update_logout(unsigned long long login_id) {
259  conn_.update_logout(login_id);
260 }
261 
262 void fuh::get_users_for_ip(const std::string& ip, std::ostringstream* out) {
263  conn_.get_users_for_ip(ip, out);
264 }
265 
266 void fuh::get_ips_for_user(const std::string& username, std::ostringstream* out) {
267  conn_.get_ips_for_user(username, out);
268 }
269 
270 bool fuh::db_is_user_primary_author(const std::string& instance_version, const std::string& id, const std::string& username) {
271  return conn_.is_user_author(instance_version, id, username, 1);
272 }
273 
274 bool fuh::db_is_user_secondary_author(const std::string& instance_version, const std::string& id, const std::string& username) {
275  return conn_.is_user_author(instance_version, id, username, 0);
276 }
277 
278 void fuh::db_delete_addon_authors(const std::string& instance_version, const std::string& id) {
279  conn_.delete_addon_authors(instance_version, id);
280 }
281 
282 void fuh::db_insert_addon_authors(const std::string& instance_version, const std::string& id, const std::string& primary_author, const std::vector<std::string>& secondary_authors) {
283  conn_.insert_addon_author(instance_version, id, primary_author, 1);
284 
285  // ignore any duplicate authors
286  std::set<std::string> inserted_authors;
287  inserted_authors.emplace(primary_author);
288 
289  for(const std::string& secondary_author : secondary_authors) {
290  if(inserted_authors.count(secondary_author) == 0) {
291  inserted_authors.emplace(secondary_author);
292  conn_.insert_addon_author(instance_version, id, secondary_author, 0);
293  }
294  }
295 }
296 
297 bool fuh::db_do_any_authors_exist(const std::string& instance_version, const std::string& id) {
298  return conn_.do_any_authors_exist(instance_version, id);
299 }
300 
301 #endif //HAVE_MYSQLPP
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
void delete_addon_authors(const std::string &instance_version, const std::string &id)
bool do_any_authors_exist(const std::string &instance_version, const std::string &id)
void update_addon_download_count(const std::string &instance_version, const std::string &id, const std::string &version)
bool topic_id_exists(int topic_id)
int async_test_query(int limit)
bool is_user_author(const std::string &instance_version, const std::string &id, const std::string &username, int is_primary)
long get_forum_id(const std::string &name)
std::unique_ptr< simple_wml::document > get_game_history(int player_id, int offset, std::string search_game_name, int search_content_type, std::string search_content)
This is an asynchronous query that is executed on a separate connection to retrieve the game history ...
void get_ips_for_user(const std::string &username, std::ostringstream *out)
bool is_user_in_group(const std::string &name, int group_id)
bool user_exists(const std::string &name)
void get_users_for_ip(const std::string &ip, std::ostringstream *out)
void insert_addon_author(const std::string &instance_version, const std::string &id, const std::string author, int is_primary)
unsigned long long insert_login(const std::string &username, const std::string &ip, const std::string &version)
void update_logout(unsigned long long login_id)
void write_user_int(const std::string &column, const std::string &name, int value)
The provided value is updated if a row exists or a new row inserted otherwise.
void set_oos_flag(const std::string &uuid, int game_id)
void update_game_end(const std::string &uuid, int game_id, const std::string &replay_location)
int get_user_int(const std::string &table, const std::string &column, const std::string &name)
ban_check get_ban_info(const std::string &name, const std::string &ip)
void insert_addon_info(const std::string &instance_version, const std::string &id, const std::string &name, const std::string &type, const std::string &version, bool forum_auth, int topic_id, const std::string uploader)
std::string get_user_string(const std::string &table, const std::string &column, const std::string &name)
void insert_game_info(const std::string &uuid, int game_id, const std::string &version, const std::string &name, int reload, int observers, int is_public, int has_password)
void insert_game_player_info(const std::string &uuid, int game_id, const std::string &username, int side_number, int is_host, const std::string &faction, const std::string &version, const std::string &source, const std::string &current_user, const std::string &leaders)
std::string get_uuid()
std::string get_tournaments()
unsigned long long insert_game_content_info(const std::string &uuid, int game_id, const std::string &type, const std::string &name, const std::string &id, const std::string &addon_id, const std::string &addon_version)
void db_set_oos_flag(const std::string &uuid, int game_id)
Sets the OOS flag in the database if wesnothd is told by a client it has detected an OOS error.
std::string get_uuid()
std::string db_users_table_
The name of the phpbb users table.
bool user_is_moderator(const std::string &name)
void db_delete_addon_authors(const std::string &instance_version, const std::string &id)
Removes the authors information from addon_author for a particular addon and version.
void db_insert_addon_info(const std::string &instance_version, const std::string &id, const std::string &name, const std::string &type, const std::string &version, bool forum_auth, int topic_id, const std::string uploader)
Inserts information about an uploaded add-on into the database.
std::string get_hashed_password_from_db(const std::string &user)
void db_insert_addon_authors(const std::string &instance_version, const std::string &id, const std::string &primary_author, const std::vector< std::string > &secondary_authors)
Inserts rows for the primary and secondary authors for a particular addon and version.
void set_is_moderator(const std::string &name, const bool &is_moderator)
Sets or unsets whether the player should be considered a moderator in the extra table.
long get_forum_id(const std::string &name)
int mp_mod_group_
The group ID of the forums MP Moderators group.
void get_users_for_ip(const std::string &ip, std::ostringstream *out)
Searches for all players that logged in using the ip address.
bool db_topic_id_exists(int topic_id)
Checks whether a forum thread with topic_id exists.
ban_info user_is_banned(const std::string &name, const std::string &addr)
std::string db_extra_table_
The name of the extras custom table, not part of a phpbb database.
void db_update_game_end(const std::string &uuid, int game_id, const std::string &replay_location)
Update the game related information when the game ends.
std::string get_user_email(const std::string &user)
std::string extract_salt(const std::string &name)
Needed because the hashing algorithm used by phpbb requires some info from the original hash to recre...
void db_update_addon_download_count(const std::string &instance_version, const std::string &id, const std::string &version)
Increments the download count for this add-on for the specific version.
bool db_is_user_secondary_author(const std::string &instance_version, const std::string &id, const std::string &username)
Checks whether the provided username is a secondary author of the add-on.
std::string get_tournaments()
void user_logged_in(const std::string &name)
Sets the last login time to the current time.
void get_ips_for_user(const std::string &username, std::ostringstream *out)
Searches for all ip addresses used by the player.
bool login(const std::string &name, const std::string &password)
Retrieves the player's hashed password from the phpbb forum database and checks if it matches the has...
fuh(const config &c)
Reads wesnothd's config for the data needed to initialize this class and dbconn.
void async_test_query(boost::asio::io_service &io_service, int limit)
A simple test query for running a query asynchronously.
std::time_t get_lastlogin(const std::string &user)
std::time_t get_registrationdate(const std::string &user)
bool user_is_active(const std::string &name)
std::string user_info(const std::string &name)
bool db_do_any_authors_exist(const std::string &instance_version, const std::string &id)
Checks whether any author information exists for a particular addon and version, since if there's no ...
bool db_is_user_primary_author(const std::string &instance_version, const std::string &id, const std::string &username)
Checks whether the provided username is the primary author of the add-on.
unsigned long long db_insert_login(const std::string &username, const std::string &ip, const std::string &version)
Inserts into the database for when a player logs in.
void db_insert_game_player_info(const std::string &uuid, int game_id, const std::string &username, int side_number, int is_host, const std::string &faction, const std::string &version, const std::string &source, const std::string &current_user, const std::string &leaders)
Inserts player information per side.
dbconn conn_
An instance of the class responsible for executing the queries and handling the database connection.
bool user_exists(const std::string &name)
unsigned long long db_insert_game_content_info(const std::string &uuid, int game_id, const std::string &type, const std::string &name, const std::string &id, const std::string &addon_id, const std::string &addon_version)
Inserts information about the content being played.
void async_get_and_send_game_history(boost::asio::io_service &io_service, wesnothd::server &s, wesnothd::player_iterator player, int player_id, int offset, std::string &search_game_name, int search_content_type, std::string &search_content)
Runs an asynchronous query to fetch the user's game history data.
void db_update_logout(unsigned long long login_id)
Updates the database for when a player logs out.
void db_insert_game_info(const std::string &uuid, int game_id, const std::string &version, const std::string &name, int reload, int observers, int is_public, int has_password)
Inserts game related information.
@ BAN_EMAIL
Account email address ban.
@ BAN_IP
IP address ban.
@ BAN_USER
User account/name ban.
@ BAN_NONE
Not a ban.
static bool is_valid_prefix(const std::string &hash)
Definition: hash.cpp:169
std::string get_salt() const
Definition: hash.cpp:176
static bcrypt from_hash_string(const std::string &input)
Definition: hash.cpp:150
static bool is_valid_hash(const std::string &hash)
Definition: hash.cpp:100
std::size_t i
Definition: function.cpp:968
int side_number
Definition: game_info.hpp:40
logger & err()
Definition: log.cpp:302
logger & info()
Definition: log.cpp:314
std::string password(const std::string &server, const std::string &login)
player_connections::const_iterator player_iterator
Ban status description.
mock_char c
static map_location::DIRECTION s
#define e
#define b