]> git.lizzy.rs Git - minetest.git/blobdiff - src/network/clientpackethandler.cpp
Fix interact range check (thanks to @lhofhansl)
[minetest.git] / src / network / clientpackethandler.cpp
index a9096accc1adac85e0febe777c4913b2b84dcc33..411982f69109d72b81aa4ac3c45f743ee53782fd 100644 (file)
@@ -24,12 +24,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "map.h"
 #include "mapsector.h"
+#include "minimap.h"
 #include "nodedef.h"
 #include "serialization.h"
 #include "server.h"
-#include "strfnd.h"
+#include "util/strfnd.h"
 #include "network/clientopcodes.h"
 #include "util/serialize.h"
+#include "util/srp.h"
 
 void Client::handleCommand_Deprecated(NetworkPacket* pkt)
 {
@@ -43,32 +45,72 @@ void Client::handleCommand_Hello(NetworkPacket* pkt)
        if (pkt->getSize() < 1)
                return;
 
-       u8 deployed;
-       *pkt >> deployed;
+       u8 serialization_ver;
+       u16 proto_ver;
+       u16 compression_mode;
+       u32 auth_mechs;
+       std::string username_legacy; // for case insensitivity
+       *pkt >> serialization_ver >> compression_mode >> proto_ver
+               >> auth_mechs >> username_legacy;
+
+       // Chose an auth method we support
+       AuthMechanism chosen_auth_mechanism = choseAuthMech(auth_mechs);
 
        infostream << "Client: TOCLIENT_HELLO received with "
-                       "deployed=" << ((int)deployed & 0xff) << std::endl;
+                       << "serialization_ver=" << (u32)serialization_ver
+                       << ", auth_mechs=" << auth_mechs
+                       << ", proto_ver=" << proto_ver
+                       << ", compression_mode=" << compression_mode
+                       << ". Doing auth with mech " << chosen_auth_mechanism << std::endl;
 
-       if (!ser_ver_supported(deployed)) {
+       if (!ser_ver_supported(serialization_ver)) {
                infostream << "Client: TOCLIENT_HELLO: Server sent "
                                << "unsupported ser_fmt_ver"<< std::endl;
                return;
        }
 
-       m_server_ser_ver = deployed;
+       m_server_ser_ver = serialization_ver;
+       m_proto_ver = proto_ver;
+
+       //TODO verify that username_legacy matches sent username, only
+       // differs in casing (make both uppercase and compare)
+       // This is only neccessary though when we actually want to add casing support
+
+       if (m_chosen_auth_mech != AUTH_MECHANISM_NONE) {
+               // we recieved a TOCLIENT_HELLO while auth was already going on
+               errorstream << "Client: TOCLIENT_HELLO while auth was already going on"
+                       << "(chosen_mech=" << m_chosen_auth_mech << ")." << std::endl;
+               if ((m_chosen_auth_mech == AUTH_MECHANISM_SRP)
+                               || (m_chosen_auth_mech == AUTH_MECHANISM_LEGACY_PASSWORD)) {
+                       srp_user_delete((SRPUser *) m_auth_data);
+                       m_auth_data = 0;
+               }
+       }
+
+       // Authenticate using that method, or abort if there wasn't any method found
+       if (chosen_auth_mechanism != AUTH_MECHANISM_NONE) {
+               startAuth(chosen_auth_mechanism);
+       } else {
+               m_chosen_auth_mech = AUTH_MECHANISM_NONE;
+               m_access_denied = true;
+               m_access_denied_reason = "Unknown";
+               m_con.Disconnect();
+       }
 
-       // @ TODO auth to server
 }
 
 void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
 {
+       deleteAuthData();
+
        v3f playerpos;
-       *pkt >> playerpos >> m_map_seed >> m_recommended_send_interval;
+       *pkt >> playerpos >> m_map_seed >> m_recommended_send_interval
+               >> m_sudo_auth_methods;
 
        playerpos -= v3f(0, BS / 2, 0);
 
        // Set player position
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
        player->setPosition(playerpos);
 
@@ -82,25 +124,48 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
 
        m_state = LC_Init;
 }
+void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt)
+{
+       deleteAuthData();
+
+       m_password = m_new_password;
+
+       verbosestream << "Client: Recieved TOCLIENT_ACCEPT_SUDO_MODE." << std::endl;
 
+       // send packet to actually set the password
+       startAuth(AUTH_MECHANISM_FIRST_SRP);
+
+       // reset again
+       m_chosen_auth_mech = AUTH_MECHANISM_NONE;
+}
+void Client::handleCommand_DenySudoMode(NetworkPacket* pkt)
+{
+       m_chat_queue.push(L"Password change denied. Password NOT changed.");
+       // reset everything and be sad
+       deleteAuthData();
+}
 void Client::handleCommand_InitLegacy(NetworkPacket* pkt)
 {
        if (pkt->getSize() < 1)
                return;
 
-       u8 deployed;
-       *pkt >> deployed;
+       u8 server_ser_ver;
+       *pkt >> server_ser_ver;
 
        infostream << "Client: TOCLIENT_INIT_LEGACY received with "
-                       "deployed=" << ((int)deployed & 0xff) << std::endl;
+               "server_ser_ver=" << ((int)server_ser_ver & 0xff) << std::endl;
 
-       if (!ser_ver_supported(deployed)) {
+       if (!ser_ver_supported(server_ser_ver)) {
                infostream << "Client: TOCLIENT_INIT_LEGACY: Server sent "
                                << "unsupported ser_fmt_ver"<< std::endl;
                return;
        }
 
-       m_server_ser_ver = deployed;
+       m_server_ser_ver = server_ser_ver;
+
+       // We can be totally wrong with this guess
+       // but we only need some value < 25.
+       m_proto_ver = 24;
 
        // Get player position
        v3s16 playerpos_s16(0, BS * 2 + BS * 20, 0);
@@ -111,7 +176,7 @@ void Client::handleCommand_InitLegacy(NetworkPacket* pkt)
 
 
        // Set player position
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
        player->setPosition(playerpos_f);
 
@@ -148,13 +213,28 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
 
                u8 denyCode = SERVER_ACCESSDENIED_UNEXPECTED_DATA;
                *pkt >> denyCode;
-               if (denyCode == SERVER_ACCESSDENIED_CUSTOM_STRING) {
-                       std::wstring wide_reason;
-                       *pkt >> wide_reason;
-                       m_access_denied_reason = wide_to_narrow(wide_reason);
-               }
-               else if (denyCode < SERVER_ACCESSDENIED_MAX) {
+               if (denyCode == SERVER_ACCESSDENIED_SHUTDOWN ||
+                               denyCode == SERVER_ACCESSDENIED_CRASH) {
+                       *pkt >> m_access_denied_reason;
+                       if (m_access_denied_reason == "") {
+                               m_access_denied_reason = accessDeniedStrings[denyCode];
+                       }
+                       u8 reconnect;
+                       *pkt >> reconnect;
+                       m_access_denied_reconnect = reconnect & 1;
+               } else if (denyCode == SERVER_ACCESSDENIED_CUSTOM_STRING) {
+                       *pkt >> m_access_denied_reason;
+               } else if (denyCode < SERVER_ACCESSDENIED_MAX) {
                        m_access_denied_reason = accessDeniedStrings[denyCode];
+               } else {
+                       // Allow us to add new error messages to the
+                       // protocol without raising the protocol version, if we want to.
+                       // Until then (which may be never), this is outside
+                       // of the defined protocol.
+                       *pkt >> m_access_denied_reason;
+                       if (m_access_denied_reason == "") {
+                               m_access_denied_reason = "Unknown";
+                       }
                }
        }
        // 13/03/15 Legacy code from 0.4.12 and lesser. must stay 1 year
@@ -163,7 +243,7 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
                if (pkt->getSize() >= 2) {
                        std::wstring wide_reason;
                        *pkt >> wide_reason;
-                       m_access_denied_reason = wide_to_narrow(wide_reason);
+                       m_access_denied_reason = wide_to_utf8(wide_reason);
                }
        }
 }
@@ -253,7 +333,7 @@ void Client::handleCommand_Inventory(NetworkPacket* pkt)
        std::string datastring(pkt->getString(0), pkt->getSize());
        std::istringstream is(datastring, std::ios_base::binary);
 
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        player->inventory.deSerialize(is);
@@ -377,7 +457,6 @@ void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
 void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
 {
        /*
-               u16 command
                for all objects
                {
                        u16 id
@@ -385,33 +464,29 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
                        string message
                }
        */
-       char buf[6];
-       // Get all data except the command number
        std::string datastring(pkt->getString(0), pkt->getSize());
-       // Throw them in an istringstream
        std::istringstream is(datastring, std::ios_base::binary);
 
-       while(is.eof() == false) {
-               is.read(buf, 2);
-               u16 id = readU16((u8*)buf);
-               if (is.eof())
-                       break;
-               is.read(buf, 2);
-               size_t message_size = readU16((u8*)buf);
-               std::string message;
-               message.reserve(message_size);
-               for (u32 i = 0; i < message_size; i++) {
-                       is.read(buf, 1);
-                       message.append(buf, 1);
+       try {
+               while (is.good()) {
+                       u16 id = readU16(is);
+                       if (!is.good())
+                               break;
+
+                       std::string message = deSerializeString(is);
+
+                       // Pass on to the environment
+                       m_env.processActiveObjectMessage(id, message);
                }
-               // Pass on to the environment
-               m_env.processActiveObjectMessage(id, message);
+       } catch (SerializationError &e) {
+               errorstream << "Client::handleCommand_ActiveObjectMessages: "
+                       << "caught SerializationError: " << e.what() << std::endl;
        }
 }
 
 void Client::handleCommand_Movement(NetworkPacket* pkt)
 {
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g;
@@ -436,7 +511,7 @@ void Client::handleCommand_Movement(NetworkPacket* pkt)
 void Client::handleCommand_HP(NetworkPacket* pkt)
 {
 
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        u8 oldhp   = player->hp;
@@ -457,7 +532,7 @@ void Client::handleCommand_HP(NetworkPacket* pkt)
 
 void Client::handleCommand_Breath(NetworkPacket* pkt)
 {
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        u16 breath;
@@ -469,7 +544,7 @@ void Client::handleCommand_Breath(NetworkPacket* pkt)
 
 void Client::handleCommand_MovePlayer(NetworkPacket* pkt)
 {
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        v3f pos;
@@ -477,6 +552,7 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt)
 
        *pkt >> pos >> pitch >> yaw;
 
+       player->got_teleported = true;
        player->setPosition(pos);
 
        infostream << "Client got TOCLIENT_MOVE_PLAYER"
@@ -504,7 +580,7 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt)
 
 void Client::handleCommand_PlayerItem(NetworkPacket* pkt)
 {
-       infostream << "Client: WARNING: Ignoring TOCLIENT_PLAYERITEM" << std::endl;
+       warningstream << "Client: Ignoring TOCLIENT_PLAYERITEM" << std::endl;
 }
 
 void Client::handleCommand_DeathScreen(NetworkPacket* pkt)
@@ -547,7 +623,7 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt)
 
        // Mesh update thread must be stopped while
        // updating content definitions
-       sanity_check(!m_mesh_update_thread.IsRunning());
+       sanity_check(!m_mesh_update_thread.isRunning());
 
        for (u16 i = 0; i < num_files; i++) {
                std::string name, sha1_base64;
@@ -558,14 +634,13 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt)
                m_media_downloader->addFile(name, sha1_raw);
        }
 
-       std::vector<std::string> remote_media;
        try {
                std::string str;
 
                *pkt >> str;
 
                Strfnd sf(str);
-               while(!sf.atend()) {
+               while(!sf.at_end()) {
                        std::string baseurl = trim(sf.next(","));
                        if (baseurl != "")
                                m_media_downloader->addRemoteServer(baseurl);
@@ -620,7 +695,7 @@ void Client::handleCommand_Media(NetworkPacket* pkt)
 
        // Mesh update thread must be stopped while
        // updating content definitions
-       sanity_check(!m_mesh_update_thread.IsRunning());
+       sanity_check(!m_mesh_update_thread.isRunning());
 
        for (u32 i=0; i < num_files; i++) {
                std::string name;
@@ -636,7 +711,7 @@ void Client::handleCommand_Media(NetworkPacket* pkt)
 
 void Client::handleCommand_ToolDef(NetworkPacket* pkt)
 {
-       infostream << "Client: WARNING: Ignoring TOCLIENT_TOOLDEF" << std::endl;
+       warningstream << "Client: Ignoring TOCLIENT_TOOLDEF" << std::endl;
 }
 
 void Client::handleCommand_NodeDef(NetworkPacket* pkt)
@@ -646,7 +721,7 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt)
 
        // Mesh update thread must be stopped while
        // updating content definitions
-       sanity_check(!m_mesh_update_thread.IsRunning());
+       sanity_check(!m_mesh_update_thread.isRunning());
 
        // Decompress node definitions
        std::string datastring(pkt->getString(0), pkt->getSize());
@@ -663,7 +738,7 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt)
 
 void Client::handleCommand_CraftItemDef(NetworkPacket* pkt)
 {
-       infostream << "Client: WARNING: Ignoring TOCLIENT_CRAFTITEMDEF" << std::endl;
+       warningstream << "Client: Ignoring TOCLIENT_CRAFTITEMDEF" << std::endl;
 }
 
 void Client::handleCommand_ItemDef(NetworkPacket* pkt)
@@ -673,7 +748,7 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt)
 
        // Mesh update thread must be stopped while
        // updating content definitions
-       sanity_check(!m_mesh_update_thread.IsRunning());
+       sanity_check(!m_mesh_update_thread.isRunning());
 
        // Decompress item definitions
        std::string datastring(pkt->getString(0), pkt->getSize());
@@ -736,9 +811,7 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt)
 
        *pkt >> server_id;
 
-       std::map<s32, int>::iterator i =
-               m_sounds_server_to_client.find(server_id);
-
+       UNORDERED_MAP<s32, int>::iterator i = m_sounds_server_to_client.find(server_id);
        if (i != m_sounds_server_to_client.end()) {
                int client_id = i->second;
                m_sound->stopSound(client_id);
@@ -766,7 +839,7 @@ void Client::handleCommand_Privileges(NetworkPacket* pkt)
 
 void Client::handleCommand_InventoryFormSpec(NetworkPacket* pkt)
 {
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        // Store formspec in LocalPlayer
@@ -822,8 +895,10 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt)
        bool collisiondetection = readU8(is);
        std::string texture     = deSerializeLongString(is);
        bool vertical           = false;
+       bool collision_removal  = false;
        try {
                vertical = readU8(is);
+               collision_removal = readU8(is);
        } catch (...) {}
 
        ClientEvent event;
@@ -834,6 +909,7 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt)
        event.spawn_particle.expirationtime     = expirationtime;
        event.spawn_particle.size               = size;
        event.spawn_particle.collisiondetection = collisiondetection;
+       event.spawn_particle.collision_removal  = collision_removal;
        event.spawn_particle.vertical           = vertical;
        event.spawn_particle.texture            = new std::string(texture);
 
@@ -866,8 +942,13 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt)
        *pkt >> id;
 
        bool vertical = false;
+       bool collision_removal = false;
+       u16 attached_id = 0;
        try {
                *pkt >> vertical;
+               *pkt >> collision_removal;
+               *pkt >> attached_id;
+
        } catch (...) {}
 
        ClientEvent event;
@@ -885,6 +966,8 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt)
        event.add_particlespawner.minsize            = minsize;
        event.add_particlespawner.maxsize            = maxsize;
        event.add_particlespawner.collisiondetection = collisiondetection;
+       event.add_particlespawner.collision_removal  = collision_removal;
+       event.add_particlespawner.attached_id        = attached_id;
        event.add_particlespawner.vertical           = vertical;
        event.add_particlespawner.texture            = new std::string(texture);
        event.add_particlespawner.id                 = id;
@@ -1017,11 +1100,22 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt)
 
        *pkt >> flags >> mask;
 
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
+       bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE;
+
        player->hud_flags &= ~mask;
        player->hud_flags |= flags;
+
+       m_minimap_disabled_by_server = !(player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
+
+       // Hide minimap if it has been disabled by the server
+       if (m_minimap_disabled_by_server && was_minimap_visible) {
+               // defers a minimap update, therefore only call it if really
+               // needed, by checking that minimap was visible before
+               m_mapper->setMinimapMode(MINIMAP_MODE_OFF);
+       }
 }
 
 void Client::handleCommand_HudSetParam(NetworkPacket* pkt)
@@ -1030,7 +1124,7 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt)
 
        *pkt >> param >> value;
 
-       Player *player = m_env.getLocalPlayer();
+       LocalPlayer *player = m_env.getLocalPlayer();
        assert(player != NULL);
 
        if (param == HUD_PARAM_HOTBAR_ITEMCOUNT && value.size() == 4) {
@@ -1039,10 +1133,10 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt)
                        player->hud_hotbar_itemcount = hotbar_itemcount;
        }
        else if (param == HUD_PARAM_HOTBAR_IMAGE) {
-               ((LocalPlayer *) player)->hotbar_image = value;
+               player->hotbar_image = value;
        }
        else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) {
-               ((LocalPlayer *) player)->hotbar_selected_image = value;
+               player->hotbar_selected_image = value;
        }
 }
 
@@ -1102,3 +1196,36 @@ void Client::handleCommand_EyeOffset(NetworkPacket* pkt)
 
        *pkt >> player->eye_offset_first >> player->eye_offset_third;
 }
+
+void Client::handleCommand_SrpBytesSandB(NetworkPacket* pkt)
+{
+       if ((m_chosen_auth_mech != AUTH_MECHANISM_LEGACY_PASSWORD)
+                       && (m_chosen_auth_mech != AUTH_MECHANISM_SRP)) {
+               errorstream << "Client: Recieved SRP S_B login message,"
+                       << " but wasn't supposed to (chosen_mech="
+                       << m_chosen_auth_mech << ")." << std::endl;
+               return;
+       }
+
+       char *bytes_M = 0;
+       size_t len_M = 0;
+       SRPUser *usr = (SRPUser *) m_auth_data;
+       std::string s;
+       std::string B;
+       *pkt >> s >> B;
+
+       infostream << "Client: Recieved TOCLIENT_SRP_BYTES_S_B." << std::endl;
+
+       srp_user_process_challenge(usr, (const unsigned char *) s.c_str(), s.size(),
+               (const unsigned char *) B.c_str(), B.size(),
+               (unsigned char **) &bytes_M, &len_M);
+
+       if ( !bytes_M ) {
+               errorstream << "Client: SRP-6a S_B safety check violation!" << std::endl;
+               return;
+       }
+
+       NetworkPacket resp_pkt(TOSERVER_SRP_BYTES_M, 0);
+       resp_pkt << std::string(bytes_M, len_M);
+       Send(&resp_pkt);
+}