From 392b328a9573b5b63a03da22bf86f3b5581d820a Mon Sep 17 00:00:00 2001 From: Akkadius Date: Sun, 7 Jul 2019 04:32:59 -0500 Subject: [PATCH] Centralize local account creation and create API endpoint for creation --- loginserver/CMakeLists.txt | 2 + loginserver/account_management.cpp | 72 +++++++++++++++++++++ loginserver/account_management.h | 31 +++++++++ loginserver/client.cpp | 2 +- loginserver/encryption.cpp | 35 ++++++++++ loginserver/encryption.h | 6 ++ loginserver/loginserver_command_handler.cpp | 13 +--- loginserver/loginserver_webserver.cpp | 23 ++++++- 8 files changed, 171 insertions(+), 13 deletions(-) create mode 100644 loginserver/account_management.cpp create mode 100644 loginserver/account_management.h diff --git a/loginserver/CMakeLists.txt b/loginserver/CMakeLists.txt index 64bbc224f..43217a48d 100644 --- a/loginserver/CMakeLists.txt +++ b/loginserver/CMakeLists.txt @@ -1,6 +1,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) SET(eqlogin_sources + account_management.cpp client.cpp client_manager.cpp database.cpp @@ -13,6 +14,7 @@ SET(eqlogin_sources ) SET(eqlogin_headers + account_management.h client.h client_manager.h database.h diff --git a/loginserver/account_management.cpp b/loginserver/account_management.cpp new file mode 100644 index 000000000..d5a2fabdb --- /dev/null +++ b/loginserver/account_management.cpp @@ -0,0 +1,72 @@ +/** + * EQEmulator: Everquest Server Emulator + * Copyright (C) 2001-2019 EQEmulator Development Team (https://github.com/EQEmu/Server) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY except by those people which sell it, which + * are required to give you total support for your newly bought product; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "account_management.h" +#include "login_server.h" + +extern LoginServer server; + +/** + * @param username + * @param password + * @return + */ +bool AccountManagement::CreateLocalLoginServerAccount( + std::string username, + std::string password +) +{ + auto mode = server.options.GetEncryptionMode(); + auto hash = eqcrypt_hash(username, password, mode); + + LogInfo( + "Attempting to create local login account for user [{0}] encryption algorithm [{1}] ({2})", + username, + GetEncryptionByModeId(mode), + mode + ); + + unsigned int db_id = 0; + std::string db_loginserver = server.options.GetDefaultLoginServerName(); + if (server.db->DoesLoginServerAccountExist(username, hash, db_loginserver, 1)) { + LogInfo( + "Attempting to create local login account for user [{0}] login [{1}] db_id [{2}] but already exists!", + username, + db_loginserver, + db_id + ); + + return false; + } + + if (server.db->CreateLoginData(username, hash, db_loginserver, db_id)) { + LogInfo( + "Account creation success for user [{0}] encryption algorithm [{1}] ({2})", + username, + GetEncryptionByModeId(mode), + mode + ); + return true; + } + + LogError("Failed to create local login account for user [{0}]!", username); + + return false; +} diff --git a/loginserver/account_management.h b/loginserver/account_management.h new file mode 100644 index 000000000..907aaf509 --- /dev/null +++ b/loginserver/account_management.h @@ -0,0 +1,31 @@ +/** + * EQEmulator: Everquest Server Emulator + * Copyright (C) 2001-2019 EQEmulator Development Team (https://github.com/EQEmu/Server) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY except by those people which sell it, which + * are required to give you total support for your newly bought product; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#ifndef EQEMU_ACCOUNT_MANAGEMENT_H +#define EQEMU_ACCOUNT_MANAGEMENT_H + +#include "iostream" + +class AccountManagement { +public: + static bool CreateLocalLoginServerAccount(std::string username, std::string password); +}; + + +#endif //EQEMU_ACCOUNT_MANAGEMENT_H diff --git a/loginserver/client.cpp b/loginserver/client.cpp index a2cefd39b..0a48c8d3a 100644 --- a/loginserver/client.cpp +++ b/loginserver/client.cpp @@ -87,7 +87,7 @@ bool Client::Process() } if (server.options.IsTraceOn()) { - LogInfo("Server list request received from client."); + LogDebug("Server list request received from client."); } SendServerListPacket(*(uint32_t *) app->pBuffer); diff --git a/loginserver/encryption.cpp b/loginserver/encryption.cpp index bf15bd796..27d22cca8 100644 --- a/loginserver/encryption.cpp +++ b/loginserver/encryption.cpp @@ -230,4 +230,39 @@ bool eqcrypt_verify_hash(const std::string &username, const std::string &passwor } return false; +} + +std::string GetEncryptionByModeId(uint32 mode) { + switch (mode) { + case EncryptionModeMD5: + return "MD5"; + case EncryptionModeMD5PassUser: + return "MD5PassUser"; + case EncryptionModeMD5UserPass: + return "MD5UserPass"; + case EncryptionModeMD5Triple: + return "MD5Triple"; + case EncryptionModeSHA: + return "SHA"; + case EncryptionModeSHAPassUser: + return "SHAPassUser"; + case EncryptionModeSHAUserPass: + return "SHAUserPass"; + case EncryptionModeSHATriple: + return "SHATriple"; + case EncryptionModeSHA512: + return "SHA512"; + case EncryptionModeSHA512PassUser: + return "SHA512PassUser"; + case EncryptionModeSHA512UserPass: + return "SHA512UserPass"; + case EncryptionModeSHA512Triple: + return "SHA512Triple"; + case EncryptionModeArgon2: + return "Argon2"; + case EncryptionModeSCrypt: + return "SCrypt"; + default: + return ""; + } } \ No newline at end of file diff --git a/loginserver/encryption.h b/loginserver/encryption.h index a6e79292f..000a3d0b4 100644 --- a/loginserver/encryption.h +++ b/loginserver/encryption.h @@ -21,6 +21,7 @@ #pragma once #include +#include "../common/types.h" enum EncryptionMode { @@ -40,6 +41,11 @@ enum EncryptionMode EncryptionModeSCrypt = 14 }; +/** + * @param mode + * @return + */ +std::string GetEncryptionByModeId(uint32 mode); const char* eqcrypt_block(const char *buffer_in, size_t buffer_in_sz, char* buffer_out, bool enc); std::string eqcrypt_hash(const std::string &username, const std::string &password, int mode); bool eqcrypt_verify_hash(const std::string &username, const std::string &password, const std::string &pwhash, int mode); diff --git a/loginserver/loginserver_command_handler.cpp b/loginserver/loginserver_command_handler.cpp index 260201ca0..cebf23746 100644 --- a/loginserver/loginserver_command_handler.cpp +++ b/loginserver/loginserver_command_handler.cpp @@ -24,6 +24,7 @@ #include "../common/util/uuid.h" #include "login_server.h" #include "loginserver_webserver.h" +#include "account_management.h" extern LoginServer server; @@ -163,17 +164,7 @@ namespace LoginserverCommandHandler { exit(1); } - std::string user = cmd("--username").str(); - std::string password = cmd("--password").str(); - - auto mode = server.options.GetEncryptionMode(); - auto hash = eqcrypt_hash(user, password, mode); - - unsigned int db_id = 0; - std::string db_login = server.options.GetDefaultLoginServerName(); - if (!server.db->CreateLoginData(user, hash, db_login, db_id)) { - - } + AccountManagement::CreateLocalLoginServerAccount(cmd("--username").str(), cmd("--password").str()); } } diff --git a/loginserver/loginserver_webserver.cpp b/loginserver/loginserver_webserver.cpp index c5b3eecf6..d11b081db 100644 --- a/loginserver/loginserver_webserver.cpp +++ b/loginserver/loginserver_webserver.cpp @@ -23,6 +23,7 @@ #include "login_server.h" #include "../common/json/json.h" #include "../common/string_util.h" +#include "account_management.h" extern LoginServer server; @@ -38,7 +39,6 @@ namespace LoginserverWebserver { api.Get( "/servers/list", [](const httplib::Request &request, httplib::Response &res) { - LoginserverWebserver::TokenManager::AuthCanRead(request, res); Json::Value response; @@ -60,6 +60,27 @@ namespace LoginserverWebserver { LoginserverWebserver::SendResponse(response, res); } ); + + api.Post( + "/account/create", [](const httplib::Request &request, httplib::Response &res) { + LoginserverWebserver::TokenManager::AuthCanWrite(request, res); + + Json::Value response; + bool account_created = AccountManagement::CreateLocalLoginServerAccount( + request.get_param_value("username"), + request.get_param_value("password") + ); + + if (account_created) { + response["message"] = "Account created successfully!"; + } + else { + response["message"] = "Account failed to create!"; + } + + LoginserverWebserver::SendResponse(response, res); + } + ); } /**