]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client.cpp
Enforce hiding nametag
[dragonfireclient.git] / src / client.cpp
index 8a9d62d2922df193eea653f69360a0ef77fedbe3..780b07872d4d90a9cc6dbadf0d9f3d5fdba2e169 100644 (file)
@@ -22,13 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <sstream>
 #include <IFileSystem.h>
 #include "jthread/jmutexautolock.h"
+#include "util/auth.h"
 #include "util/directiontables.h"
 #include "util/pointedthing.h"
 #include "util/serialize.h"
 #include "util/string.h"
+#include "util/srp.h"
 #include "client.h"
 #include "network/clientopcodes.h"
-#include "main.h"
 #include "filesys.h"
 #include "porting.h"
 #include "mapblock_mesh.h"
@@ -49,6 +50,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "drawscene.h"
 #include "database-sqlite3.h"
 #include "serialization.h"
+#include "guiscalingfilter.h"
 
 extern gui::IGUIEnvironment* guienv;
 
@@ -255,6 +257,8 @@ Client::Client(
        m_highlighted_pos(0,0,0),
        m_map_seed(0),
        m_password(password),
+       m_chosen_auth_mech(AUTH_MECHANISM_NONE),
+       m_auth_data(NULL),
        m_access_denied(false),
        m_itemdef_received(false),
        m_nodedef_received(false),
@@ -388,11 +392,11 @@ void Client::step(float dtime)
                if(counter <= 0.0) {
                        counter = 2.0;
 
-                       Player *myplayer = m_env.getLocalPlayer();              
+                       Player *myplayer = m_env.getLocalPlayer();
                        FATAL_ERROR_IF(myplayer == NULL, "Local player not found in environment.");
 
-                       // Send TOSERVER_INIT
-                       // [0] u16 TOSERVER_INIT
+                       // Send TOSERVER_INIT_LEGACY
+                       // [0] u16 TOSERVER_INIT_LEGACY
                        // [2] u8 SER_FMT_VER_HIGHEST_READ
                        // [3] u8[20] player_name
                        // [23] u8[28] password (new in some version)
@@ -404,18 +408,13 @@ void Client::step(float dtime)
                        memset(pName, 0, PLAYERNAME_SIZE * sizeof(char));
                        memset(pPassword, 0, PASSWORD_SIZE * sizeof(char));
 
+                       std::string hashed_password = translatePassword(myplayer->getName(), m_password);
                        snprintf(pName, PLAYERNAME_SIZE, "%s", myplayer->getName());
-                       snprintf(pPassword, PASSWORD_SIZE, "%s", m_password.c_str());
+                       snprintf(pPassword, PASSWORD_SIZE, "%s", hashed_password.c_str());
 
-                       NetworkPacket pkt(TOSERVER_INIT_LEGACY,
-                                       1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2);
-
-                       pkt << (u8) SER_FMT_VER_HIGHEST_READ;
-                       pkt.putRawString(pName,PLAYERNAME_SIZE);
-                       pkt.putRawString(pPassword, PASSWORD_SIZE);
-                       pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX;
-
-                       Send(&pkt);
+                       sendLegacyInit(pName, pPassword);
+                       if (LATEST_PROTOCOL_VERSION >= 25)
+                               sendInit(myplayer->getName());
                }
 
                // Not connected, return
@@ -455,19 +454,8 @@ void Client::step(float dtime)
                                        [3+6] v3s16 pos_1
                                        ...
                                */
-                               NetworkPacket pkt(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * sendlist.size());
-
-                               pkt << (u8) sendlist.size();
 
-                               u32 k = 0;
-                               for(std::vector<v3s16>::iterator
-                                               j = sendlist.begin();
-                                               j != sendlist.end(); ++j) {
-                                       pkt << *j;
-                                       k++;
-                               }
-
-                               Send(&pkt);
+                               sendDeletedBlocks(sendlist);
 
                                if(i == deleted_blocks.end())
                                        break;
@@ -575,9 +563,7 @@ void Client::step(float dtime)
                                        [0] u8 count
                                        [1] v3s16 pos_0
                                */
-                               NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6);
-                               pkt << (u8) 1 << r.p;
-                               Send(&pkt);
+                               sendGotBlocks(r.p);
                        }
                }
 
@@ -646,7 +632,7 @@ void Client::step(float dtime)
        if(m_removed_sounds_check_timer >= 2.32) {
                m_removed_sounds_check_timer = 0;
                // Find removed sounds and clear references to them
-               std::set<s32> removed_server_ids;
+               std::vector<s32> removed_server_ids;
                for(std::map<s32, int>::iterator
                                i = m_sounds_server_to_client.begin();
                                i != m_sounds_server_to_client.end();) {
@@ -657,24 +643,13 @@ void Client::step(float dtime)
                                m_sounds_server_to_client.erase(server_id);
                                m_sounds_client_to_server.erase(client_id);
                                m_sounds_to_objects.erase(client_id);
-                               removed_server_ids.insert(server_id);
+                               removed_server_ids.push_back(server_id);
                        }
                }
 
                // Sync to server
                if(!removed_server_ids.empty()) {
-                       size_t server_ids = removed_server_ids.size();
-                       assert(server_ids <= 0xFFFF);
-
-                       NetworkPacket pkt(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4);
-
-                       pkt << (u16) (server_ids & 0xFFFF);
-
-                       for(std::set<s32>::iterator i = removed_server_ids.begin();
-                                       i != removed_server_ids.end(); i++)
-                               pkt << *i;
-
-                       Send(&pkt);
+                       sendRemovedSounds(removed_server_ids);
                }
        }
 
@@ -866,10 +841,9 @@ void Client::ReceiveAll()
 void Client::Receive()
 {
        DSTACK(__FUNCTION_NAME);
-       SharedBuffer<u8> data;
-       u16 sender_peer_id;
-       u32 datasize = m_con.Receive(sender_peer_id, data);
-       ProcessData(*data, datasize, sender_peer_id);
+       NetworkPacket pkt;
+       m_con.Receive(&pkt);
+       ProcessData(&pkt);
 }
 
 inline void Client::handleCommand(NetworkPacket* pkt)
@@ -881,19 +855,12 @@ inline void Client::handleCommand(NetworkPacket* pkt)
 /*
        sender_peer_id given to this shall be quaranteed to be a valid peer
 */
-void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
+void Client::ProcessData(NetworkPacket *pkt)
 {
        DSTACK(__FUNCTION_NAME);
 
-       // Ignore packets that don't even fit a command
-       if(datasize < 2) {
-               m_packetcounter.add(60000);
-               return;
-       }
-
-       NetworkPacket pkt(data, datasize, sender_peer_id);
-
-       ToClientCommand command = (ToClientCommand) pkt.getCommand();
+       ToClientCommand command = (ToClientCommand) pkt->getCommand();
+       u32 sender_peer_id = pkt->getPeerId();
 
        //infostream<<"Client: received command="<<command<<std::endl;
        m_packetcounter.add((u16)command);
@@ -921,7 +888,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
         * as a byte mask
         */
        if(toClientCommandTable[command].state == TOCLIENT_STATE_NOT_CONNECTED) {
-               handleCommand(&pkt);
+               handleCommand(pkt);
                return;
        }
 
@@ -936,7 +903,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
          Handle runtime commands
        */
 
-       handleCommand(&pkt);
+       handleCommand(pkt);
 }
 
 void Client::Send(NetworkPacket* pkt)
@@ -983,6 +950,157 @@ void Client::interact(u8 action, const PointedThing& pointed)
        Send(&pkt);
 }
 
+void Client::deleteAuthData()
+{
+       if (!m_auth_data)
+               return;
+
+       switch (m_chosen_auth_mech) {
+               case AUTH_MECHANISM_FIRST_SRP:
+                       break;
+               case AUTH_MECHANISM_SRP:
+               case AUTH_MECHANISM_LEGACY_PASSWORD:
+                       srp_user_delete((SRPUser *) m_auth_data);
+                       m_auth_data = NULL;
+                       break;
+               case AUTH_MECHANISM_NONE:
+                       break;
+       }
+}
+
+
+AuthMechanism Client::choseAuthMech(const u32 mechs)
+{
+       if (mechs & AUTH_MECHANISM_SRP)
+               return AUTH_MECHANISM_SRP;
+
+       if (mechs & AUTH_MECHANISM_FIRST_SRP)
+               return AUTH_MECHANISM_FIRST_SRP;
+
+       if (mechs & AUTH_MECHANISM_LEGACY_PASSWORD)
+               return AUTH_MECHANISM_LEGACY_PASSWORD;
+
+       return AUTH_MECHANISM_NONE;
+}
+
+void Client::sendLegacyInit(const char* playerName, const char* playerPassword)
+{
+       NetworkPacket pkt(TOSERVER_INIT_LEGACY,
+                       1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2);
+
+       pkt << (u8) SER_FMT_VER_HIGHEST_READ;
+       pkt.putRawString(playerName,PLAYERNAME_SIZE);
+       pkt.putRawString(playerPassword, PASSWORD_SIZE);
+       pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX;
+
+       Send(&pkt);
+}
+
+void Client::sendInit(const std::string &playerName)
+{
+       NetworkPacket pkt(TOSERVER_INIT, 1 + 2 + 2 + (1 + playerName.size()));
+
+       // we don't support network compression yet
+       u16 supp_comp_modes = NETPROTO_COMPRESSION_NONE;
+       pkt << (u8) SER_FMT_VER_HIGHEST_READ << (u16) supp_comp_modes;
+       pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX;
+       pkt << playerName;
+
+       Send(&pkt);
+}
+
+void Client::startAuth(AuthMechanism chosen_auth_mechanism)
+{
+       m_chosen_auth_mech = chosen_auth_mechanism;
+
+       switch (chosen_auth_mechanism) {
+               case AUTH_MECHANISM_FIRST_SRP: {
+                       // send srp verifier to server
+                       NetworkPacket resp_pkt(TOSERVER_FIRST_SRP, 0);
+                       char *salt, *bytes_v;
+                       std::size_t len_salt, len_v;
+                       salt = NULL;
+                       getSRPVerifier(getPlayerName(), m_password,
+                               &salt, &len_salt, &bytes_v, &len_v);
+                       resp_pkt
+                               << std::string((char*)salt, len_salt)
+                               << std::string((char*)bytes_v, len_v)
+                               << (u8)((m_password == "") ? 1 : 0);
+                       free(salt);
+                       free(bytes_v);
+                       Send(&resp_pkt);
+                       break;
+               }
+               case AUTH_MECHANISM_SRP:
+               case AUTH_MECHANISM_LEGACY_PASSWORD: {
+                       u8 based_on = 1;
+
+                       if (chosen_auth_mechanism == AUTH_MECHANISM_LEGACY_PASSWORD) {
+                               m_password = translatePassword(getPlayerName(), m_password);
+                               based_on = 0;
+                       }
+
+                       std::string playername_u = lowercase(getPlayerName());
+                       m_auth_data = srp_user_new(SRP_SHA256, SRP_NG_2048,
+                               getPlayerName().c_str(), playername_u.c_str(),
+                               (const unsigned char *) m_password.c_str(),
+                               m_password.length(), NULL, NULL);
+                       char *bytes_A = 0;
+                       size_t len_A = 0;
+                       srp_user_start_authentication((struct SRPUser *) m_auth_data,
+                               NULL, NULL, 0, (unsigned char **) &bytes_A, &len_A);
+
+                       NetworkPacket resp_pkt(TOSERVER_SRP_BYTES_A, 0);
+                       resp_pkt << std::string(bytes_A, len_A) << based_on;
+                       free(bytes_A);
+                       Send(&resp_pkt);
+                       break;
+               }
+               case AUTH_MECHANISM_NONE:
+                       break; // not handled in this method
+       }
+}
+
+void Client::sendDeletedBlocks(std::vector<v3s16> &blocks)
+{
+       NetworkPacket pkt(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * blocks.size());
+
+       pkt << (u8) blocks.size();
+
+       u32 k = 0;
+       for(std::vector<v3s16>::iterator
+                       j = blocks.begin();
+                       j != blocks.end(); ++j) {
+               pkt << *j;
+               k++;
+       }
+
+       Send(&pkt);
+}
+
+void Client::sendGotBlocks(v3s16 block)
+{
+       NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6);
+       pkt << (u8) 1 << block;
+       Send(&pkt);
+}
+
+void Client::sendRemovedSounds(std::vector<s32> &soundList)
+{
+       size_t server_ids = soundList.size();
+       assert(server_ids <= 0xFFFF);
+
+       NetworkPacket pkt(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4);
+
+       pkt << (u16) (server_ids & 0xFFFF);
+
+       for(std::vector<s32>::iterator i = soundList.begin();
+                       i != soundList.end(); i++)
+               pkt << *i;
+
+       Send(&pkt);
+}
+
 void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
                const std::map<std::string, std::string> &fields)
 {
@@ -1049,28 +1167,34 @@ void Client::sendChatMessage(const std::wstring &message)
        Send(&pkt);
 }
 
-void Client::sendChangePassword(const std::wstring &oldpassword,
-        const std::wstring &newpassword)
+void Client::sendChangePassword(const std::string &oldpassword,
+        const std::string &newpassword)
 {
        Player *player = m_env.getLocalPlayer();
-       if(player == NULL)
+       if (player == NULL)
                return;
 
        std::string playername = player->getName();
-       std::string oldpwd = translatePassword(playername, oldpassword);
-       std::string newpwd = translatePassword(playername, newpassword);
-
-       NetworkPacket pkt(TOSERVER_PASSWORD_LEGACY, 2 * PASSWORD_SIZE);
-
-       for(u8 i = 0; i < PASSWORD_SIZE; i++) {
-               pkt << (u8) (i < oldpwd.length() ? oldpwd[i] : 0);
-       }
+       if (m_proto_ver >= 25) {
+               // get into sudo mode and then send new password to server
+               m_password = oldpassword;
+               m_new_password = newpassword;
+               startAuth(choseAuthMech(m_sudo_auth_methods));
+       } else {
+               std::string oldpwd = translatePassword(playername, oldpassword);
+               std::string newpwd = translatePassword(playername, newpassword);
+
+               NetworkPacket pkt(TOSERVER_PASSWORD_LEGACY, 2 * PASSWORD_SIZE);
+
+               for (u8 i = 0; i < PASSWORD_SIZE; i++) {
+                       pkt << (u8) (i < oldpwd.length() ? oldpwd[i] : 0);
+               }
 
-       for(u8 i = 0; i < PASSWORD_SIZE; i++) {
-               pkt << (u8) (i < newpwd.length() ? newpwd[i] : 0);
+               for (u8 i = 0; i < PASSWORD_SIZE; i++) {
+                       pkt << (u8) (i < newpwd.length() ? newpwd[i] : 0);
+               }
+               Send(&pkt);
        }
-
-       Send(&pkt);
 }
 
 
@@ -1105,12 +1229,12 @@ void Client::sendReady()
        DSTACK(__FUNCTION_NAME);
 
        NetworkPacket pkt(TOSERVER_CLIENT_READY,
-                       1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(minetest_version_hash));
+                       1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(g_version_hash));
 
-       pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH_ORIG
-               << (u8) 0 << (u16) strlen(minetest_version_hash);
+       pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH
+               << (u8) 0 << (u16) strlen(g_version_hash);
 
-       pkt.putRawString(minetest_version_hash, (u16) strlen(minetest_version_hash));
+       pkt.putRawString(g_version_hash, (u16) strlen(g_version_hash));
        Send(&pkt);
 }
 
@@ -1594,6 +1718,11 @@ void Client::afterContentReceived(IrrlichtDevice *device)
 
        const wchar_t* text = wgettext("Loading textures...");
 
+       // Clear cached pre-scaled 2D GUI images, as this cache
+       // might have images with the same name but different
+       // content from previous sessions.
+       guiScalingCacheClear(device->getVideoDriver());
+
        // Rebuild inherited images and recreate textures
        infostream<<"- Rebuilding images and textures"<<std::endl;
        draw_load_screen(text,device, guienv, 0, 70);
@@ -1613,7 +1742,7 @@ void Client::afterContentReceived(IrrlichtDevice *device)
        draw_load_screen(text, device, guienv, 0, 72);
        m_nodedef->updateAliases(m_itemdef);
        m_nodedef->setNodeRegistrationStatus(true);
-       m_nodedef->runNodeResolverCallbacks();
+       m_nodedef->runNodeResolveCallbacks();
        delete[] text;
 
        // Update node textures and assign shaders to each tile
@@ -1683,19 +1812,45 @@ void Client::makeScreenshot(IrrlichtDevice *device)
 {
        irr::video::IVideoDriver *driver = device->getVideoDriver();
        irr::video::IImage* const raw_image = driver->createScreenShot();
-       if (raw_image) {
-               irr::video::IImage* const image = driver->createImage(video::ECF_R8G8B8,
-                       raw_image->getDimension());
+
+       if (!raw_image)
+               return;
+
+       time_t t = time(NULL);
+       struct tm *tm = localtime(&t);
+
+       char timetstamp_c[64];
+       strftime(timetstamp_c, sizeof(timetstamp_c), "%Y%m%d_%H%M%S", tm);
+
+       std::string filename_base = g_settings->get("screenshot_path")
+                       + DIR_DELIM
+                       + std::string("screenshot_")
+                       + std::string(timetstamp_c);
+       std::string filename_ext = ".png";
+       std::string filename;
+
+       // Try to find a unique filename
+       unsigned serial = 0;
+
+       while (serial < SCREENSHOT_MAX_SERIAL_TRIES) {
+               filename = filename_base + (serial > 0 ? ("_" + itos(serial)) : "") + filename_ext;
+               std::ifstream tmp(filename.c_str());
+               if (!tmp.good())
+                       break;  // File did not apparently exist, we'll go with it
+               serial++;
+       }
+
+       if (serial == SCREENSHOT_MAX_SERIAL_TRIES) {
+               infostream << "Could not find suitable filename for screenshot" << std::endl;
+       } else {
+               irr::video::IImage* const image =
+                               driver->createImage(video::ECF_R8G8B8, raw_image->getDimension());
 
                if (image) {
                        raw_image->copyTo(image);
-                       irr::c8 filename[256];
-                       snprintf(filename, sizeof(filename),
-                               (std::string("%s") + DIR_DELIM + "screenshot_%u.png").c_str(),
-                                g_settings->get("screenshot_path").c_str(),
-                                device->getTimer()->getRealTime());
+
                        std::ostringstream sstr;
-                       if (driver->writeImageToFile(image, filename)) {
+                       if (driver->writeImageToFile(image, filename.c_str())) {
                                sstr << "Saved screenshot to '" << filename << "'";
                        } else {
                                sstr << "Failed to save screenshot '" << filename << "'";
@@ -1704,8 +1859,9 @@ void Client::makeScreenshot(IrrlichtDevice *device)
                        infostream << sstr.str() << std::endl;
                        image->drop();
                }
-               raw_image->drop();
        }
+
+       raw_image->drop();
 }
 
 // IGameDef interface