From e09fec3373abcf836635dc33a95e9b9975058eb9 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Wed, 18 May 2011 16:48:27 +0100 Subject: [PATCH 1/4] New map directory structure that avoids map size being limited by filesystem --- src/filesys.cpp | 19 ++++++++ src/filesys.h | 3 ++ src/map.cpp | 124 +++++++++++++++++++++++++++++++++--------------- src/map.h | 12 ++--- 4 files changed, 111 insertions(+), 47 deletions(-) diff --git a/src/filesys.cpp b/src/filesys.cpp index 287090e8a..8248a13d4 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -290,5 +290,24 @@ bool RecursiveDeleteContent(std::string path) return true; } +bool CreateAllDirs(std::string path) +{ + + size_t pos; + std::vector tocreate; + std::string basepath = path; + while(!PathExists(basepath)) + { + tocreate.push_back(basepath); + pos = basepath.rfind('/'); + if(pos == std::string::npos) + return false; + basepath = basepath.substr(0,pos); + } + for(int i=tocreate.size()-1;i>=0;i--) + CreateDir(tocreate[i]); + return true; +} + } // namespace fs diff --git a/src/filesys.h b/src/filesys.h index 4dd90b84e..b74b34f3d 100644 --- a/src/filesys.h +++ b/src/filesys.h @@ -38,6 +38,9 @@ std::vector GetDirListing(std::string path); // Returns true if already exists bool CreateDir(std::string path); +// Create all directories on the given path that don't already exist. +bool CreateAllDirs(std::string path); + bool PathExists(std::string path); // Only pass full paths to this one. True on success. diff --git a/src/map.cpp b/src/map.cpp index 63f01ddee..cd2ba9154 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -4875,9 +4875,9 @@ plan_b: return (s16)level; } -void ServerMap::createDir(std::string path) +void ServerMap::createDirs(std::string path) { - if(fs::CreateDir(path) == false) + if(fs::CreateAllDirs(path) == false) { m_dout<getPos(); - createDir(m_savedir); - createDir(m_savedir+"/sectors"); std::string dir = getSectorDir(pos); - createDir(dir); + createDirs(dir); std::string fullpath = dir + "/meta"; std::ofstream o(fullpath.c_str(), std::ios_base::binary); @@ -5282,22 +5303,21 @@ void ServerMap::saveSectorMeta(ServerMapSector *sector) sector->differs_from_disk = false; } -MapSector* ServerMap::loadSectorMeta(std::string dirname) +MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load) { DSTACK(__FUNCTION_NAME); // Get destination - v2s16 p2d = getSectorPos(dirname); - std::string dir = m_savedir + "/sectors/" + dirname; + v2s16 p2d = getSectorPos(sectordir); ServerMapSector *sector = NULL; - - std::string fullpath = dir + "/meta"; + + std::string fullpath = sectordir + "/meta"; std::ifstream is(fullpath.c_str(), std::ios_base::binary); if(is.good() == false) { // If the directory exists anyway, it probably is in some old // format. Just go ahead and create the sector. - if(fs::PathExists(dir)) + if(fs::PathExists(sectordir)) { dstream<<"ServerMap::loadSectorMeta(): Sector metafile " <differs_from_disk = false; @@ -5323,14 +5347,31 @@ MapSector* ServerMap::loadSectorMeta(std::string dirname) bool ServerMap::loadSectorFull(v2s16 p2d) { DSTACK(__FUNCTION_NAME); - std::string sectorsubdir = getSectorSubDir(p2d); MapSector *sector = NULL; + // The directory layout we're going to load from. + // 1 - original sectors/xxxxzzzz/ + // 2 - new sectors2/xxx/zzz/ + // If we load from anything but the latest structure, we will + // immediately save to the new one, and remove the old. + int loadlayout = 1; + std::string sectordir1 = getSectorDir(p2d, 1); + std::string sectordir; + if(fs::PathExists(sectordir1)) + { + sectordir = sectordir1; + } + else + { + loadlayout = 2; + sectordir = getSectorDir(p2d, 2); + } + //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out try{ - sector = loadSectorMeta(sectorsubdir); + sector = loadSectorMeta(sectordir, loadlayout != 2); } catch(InvalidFilenameException &e) { @@ -5349,7 +5390,7 @@ bool ServerMap::loadSectorFull(v2s16 p2d) Load blocks */ std::vector list2 = fs::GetDirListing - (m_savedir+"/sectors/"+sectorsubdir); + (sectordir); std::vector::iterator i2; for(i2=list2.begin(); i2!=list2.end(); i2++) { @@ -5357,16 +5398,25 @@ bool ServerMap::loadSectorFull(v2s16 p2d) if(i2->dir) continue; try{ - loadBlock(sectorsubdir, i2->name, sector); + loadBlock(sectordir, i2->name, sector, loadlayout != 2); } catch(InvalidFilenameException &e) { // This catches unknown crap in directory } } + + if(loadlayout != 2) + { + dstream<<"Sector converted to new layout - deleting "<< + sectordir1<getPos(); v2s16 p2d(p3d.X, p3d.Z); - createDir(m_savedir); - createDir(m_savedir+"/sectors"); std::string dir = getSectorDir(p2d); - createDir(dir); + createDirs(dir); - // Block file is map/sectors/xxxxxxxx/xxxx char cc[5]; snprintf(cc, 5, "%.4x", (unsigned int)p3d.Y&0xffff); std::string fullpath = dir + "/" + cc; @@ -5427,12 +5474,11 @@ void ServerMap::saveBlock(MapBlock *block) block->resetChangedFlag(); } -void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector) +void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load) { DSTACK(__FUNCTION_NAME); - // Block file is map/sectors/xxxxxxxx/xxxx - std::string fullpath = m_savedir+"/sectors/"+sectordir+"/"+blockfile; + std::string fullpath = sectordir+"/"+blockfile; try{ std::ifstream is(fullpath.c_str(), std::ios_base::binary); @@ -5496,7 +5542,7 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto */ // Save old format blocks in new format - if(version < SER_FMT_VER_HIGHEST) + if(version < SER_FMT_VER_HIGHEST || save_after_load) { saveBlock(block); } diff --git a/src/map.h b/src/map.h index fa52dc26b..09154547c 100644 --- a/src/map.h +++ b/src/map.h @@ -545,13 +545,9 @@ public: Misc. helper functions for fiddling with directory and file names when saving */ - void createDir(std::string path); - void createSaveDir(); - // returns something like "xxxxxxxx" - std::string getSectorSubDir(v2s16 pos); + void createDirs(std::string path); // returns something like "map/sectors/xxxxxxxx" - std::string getSectorDir(v2s16 pos); - std::string createSectorDir(v2s16 pos); + std::string getSectorDir(v2s16 pos, int layout = 2); // dirname: final directory name v2s16 getSectorPos(std::string dirname); v3s16 getBlockPos(std::string sectordir, std::string blockfile); @@ -572,7 +568,7 @@ public: // (no MapBlocks) // DEPRECATED? Sectors have no metadata anymore. void saveSectorMeta(ServerMapSector *sector); - MapSector* loadSectorMeta(std::string dirname); + MapSector* loadSectorMeta(std::string dirname, bool save_after_load); // Full load of a sector including all blocks. // returns true on success, false on failure. @@ -583,7 +579,7 @@ public: void saveBlock(MapBlock *block); // This will generate a sector with getSector if not found. - void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector); + void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false); // For debug printing virtual void PrintInfo(std::ostream &out); From b5ceaf445a68b9cda47fbd4246f074aa0a1d3966 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Thu, 19 May 2011 12:18:42 +0100 Subject: [PATCH 2/4] Don't say 'Creating server and client' if the server is remote, so not being created --- src/game.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/game.cpp b/src/game.cpp index d90262060..d6b1117c1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -672,7 +672,6 @@ void the_game( guienv->drawAll(); driver->endScene(); - std::cout< server; if(address == ""){ + std::cout<start(port); } @@ -688,6 +688,7 @@ void the_game( Create client */ + std::cout< Date: Fri, 20 May 2011 20:28:03 +0100 Subject: [PATCH 3/4] Passwords - password entry at main menu, stored and checked by server --- src/CMakeLists.txt | 2 + src/base64.cpp | 123 ++++++++++++++++++++++++++++ src/base64.h | 4 + src/client.cpp | 21 ++++- src/client.h | 9 +++ src/clientserver.h | 6 ++ src/game.cpp | 19 ++++- src/game.h | 1 + src/guiMainMenu.cpp | 45 +++++++---- src/guiMainMenu.h | 1 + src/main.cpp | 27 +++++++ src/player.cpp | 6 ++ src/player.h | 13 +++ src/server.cpp | 59 +++++++++++--- src/server.h | 6 +- src/sha1.cpp | 191 ++++++++++++++++++++++++++++++++++++++++++++ src/sha1.h | 35 ++++++++ 17 files changed, 534 insertions(+), 34 deletions(-) create mode 100644 src/base64.cpp create mode 100644 src/base64.h create mode 100644 src/sha1.cpp create mode 100644 src/sha1.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42260b3ae..26f4872da 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -95,6 +95,8 @@ set(minetest_SRCS tile.cpp game.cpp main.cpp + sha1.cpp + base64.cpp ) # Server sources diff --git a/src/base64.cpp b/src/base64.cpp new file mode 100644 index 000000000..2a863d161 --- /dev/null +++ b/src/base64.cpp @@ -0,0 +1,123 @@ +/* + base64.cpp and base64.h + + Copyright (C) 2004-2008 René Nyffenegger + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + + 3. This notice may not be removed or altered from any source distribution. + + René Nyffenegger rene.nyffenegger@adp-gmbh.ch + +*/ + +#include "base64.h" +#include + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + +static inline bool is_base64(unsigned char c) { + return (isalnum(c) || (c == '+') || (c == '/')); +} + +std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { + std::string ret; + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for(i = 0; (i <4) ; i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for(j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while((i++ < 3)) + ret += '='; + + } + + return ret; + +} + +std::string base64_decode(std::string const& encoded_string) { + int in_len = encoded_string.size(); + int i = 0; + int j = 0; + int in_ = 0; + unsigned char char_array_4[4], char_array_3[3]; + std::string ret; + + while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { + char_array_4[i++] = encoded_string[in_]; in_++; + if (i ==4) { + for (i = 0; i <4; i++) + char_array_4[i] = base64_chars.find(char_array_4[i]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (i = 0; (i < 3); i++) + ret += char_array_3[i]; + i = 0; + } + } + + if (i) { + for (j = i; j <4; j++) + char_array_4[j] = 0; + + for (j = 0; j <4; j++) + char_array_4[j] = base64_chars.find(char_array_4[j]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; + } + + return ret; +} diff --git a/src/base64.h b/src/base64.h new file mode 100644 index 000000000..65d5db8b2 --- /dev/null +++ b/src/base64.h @@ -0,0 +1,4 @@ +#include + +std::string base64_encode(unsigned char const* , unsigned int len); +std::string base64_decode(std::string const& s); diff --git a/src/client.cpp b/src/client.cpp index ce862a002..e2cda97c1 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -68,6 +68,7 @@ void * MeshUpdateThread::Thread() Client::Client( IrrlichtDevice *device, const char *playername, + std::string password, MapDrawControl &control): m_mesh_update_thread(), m_env( @@ -83,7 +84,9 @@ Client::Client( m_server_ser_ver(SER_FMT_VER_INVALID), m_inventory_updated(false), m_time_of_day(0), - m_map_seed(0) + m_map_seed(0), + m_password(password), + m_access_denied(false) { m_packetcounter_timer = 0.0; m_delete_unused_sectors_timer = 0.0; @@ -299,11 +302,14 @@ void Client::step(float dtime) // [0] u16 TOSERVER_INIT // [2] u8 SER_FMT_VER_HIGHEST // [3] u8[20] player_name - SharedBuffer data(2+1+PLAYERNAME_SIZE); + // [23] u8[28] password + SharedBuffer data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE); writeU16(&data[0], TOSERVER_INIT); writeU8(&data[2], SER_FMT_VER_HIGHEST); memset((char*)&data[3], 0, PLAYERNAME_SIZE); snprintf((char*)&data[3], PLAYERNAME_SIZE, "%s", myplayer->getName()); + snprintf((char*)&data[23], PASSWORD_SIZE, "%s", m_password.c_str()); + // Send as unreliable Send(0, data, false); } @@ -597,7 +603,16 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) return; } - + + if(command == TOCLIENT_ACCESS_DENIED) + { + // The server didn't like our password. Note, this needs + // to be processed even if the serialisation format has + // not been agreed yet, the same as TOCLIENT_INIT. + m_access_denied = true; + return; + } + if(ser_version == SER_FMT_VER_INVALID) { dout_client< m_client_event_queue; diff --git a/src/clientserver.h b/src/clientserver.h index 46ffa5eab..a64a11f08 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -150,6 +150,11 @@ enum ToClientCommand f1000 player pitch f1000 player yaw */ + + TOCLIENT_ACCESS_DENIED = 0x35, + /* + u16 command + */ }; enum ToServerCommand @@ -161,6 +166,7 @@ enum ToServerCommand [0] u16 TOSERVER_INIT [2] u8 SER_FMT_VER_HIGHEST [3] u8[20] player_name + [23] u8[28] password */ TOSERVER_INIT2 = 0x11, diff --git a/src/game.cpp b/src/game.cpp index d6b1117c1..69e673fa4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -634,6 +634,7 @@ void the_game( gui::IGUIFont* font, std::string map_dir, std::string playername, + std::string password, std::string address, u16 port, std::wstring &error_message @@ -689,7 +690,7 @@ void the_game( */ std::cout<= 10.0) { @@ -756,8 +761,16 @@ void the_game( if(could_connect == false) { - std::cout<remove(); return; } diff --git a/src/game.h b/src/game.h index 7cba1299e..eb289b8f2 100644 --- a/src/game.h +++ b/src/game.h @@ -67,6 +67,7 @@ void the_game( gui::IGUIFont* font, std::string map_dir, std::string playername, + std::string password, std::string address, u16 port, std::wstring &error_message diff --git a/src/guiMainMenu.cpp b/src/guiMainMenu.cpp index ac02f79e3..a30e006a6 100644 --- a/src/guiMainMenu.cpp +++ b/src/guiMainMenu.cpp @@ -164,30 +164,38 @@ void GUIMainMenu::regenerateGui(v2u32 screensize) //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT); } - // Nickname + // Nickname + password { - core::rect rect(0, 0, 100, 20); - rect += topleft_client + v2s32(40, 50+6); - const wchar_t *text = L"Nickname"; + core::rect rect(0, 0, 110, 20); + rect += topleft_client + v2s32(35, 50+6); + const wchar_t *text = L"Name/Password"; Environment->addStaticText(text, rect, false, true, this, -1); } { - core::rect rect(0, 0, 250, 30); + core::rect rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, 50); gui::IGUIElement *e = Environment->addEditBox(text_name.c_str(), rect, true, this, 258); if(text_name == L"") Environment->setFocus(e); } + { + core::rect rect(0, 0, 120, 30); + rect += topleft_client + v2s32(size_client.X-60-100, 50); + gui::IGUIEditBox *e = + Environment->addEditBox(L"", rect, true, this, 264); + e->setPasswordBox(true); + + } // Address + port { - core::rect rect(0, 0, 100, 20); - rect += topleft_client + v2s32(40, 100+6); - const wchar_t *text = L"Address + Port"; + core::rect rect(0, 0, 110, 20); + rect += topleft_client + v2s32(35, 100+6); + const wchar_t *text = L"Address/Port"; Environment->addStaticText(text, rect, false, true, this, -1); } { - core::rect rect(0, 0, 250, 30); + core::rect rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, 100); gui::IGUIElement *e = Environment->addEditBox(text_address.c_str(), rect, true, this, 256); @@ -195,9 +203,9 @@ void GUIMainMenu::regenerateGui(v2u32 screensize) Environment->setFocus(e); } { - core::rect rect(0, 0, 100, 30); + core::rect rect(0, 0, 120, 30); //rect += topleft_client + v2s32(160+250+20, 125); - rect += topleft_client + v2s32(size_client.X-40-100, 100); + rect += topleft_client + v2s32(size_client.X-60-100, 100); Environment->addEditBox(text_port.c_str(), rect, true, this, 257); } { @@ -208,13 +216,13 @@ void GUIMainMenu::regenerateGui(v2u32 screensize) } { core::rect rect(0, 0, 250, 30); - rect += topleft_client + v2s32(40, 150); + rect += topleft_client + v2s32(35, 150); Environment->addCheckBox(fancy_trees, rect, this, 263, L"Fancy trees"); } { core::rect rect(0, 0, 250, 30); - rect += topleft_client + v2s32(40, 150+30); + rect += topleft_client + v2s32(35, 150+30); Environment->addCheckBox(smooth_lighting, rect, this, 262, L"Smooth Lighting"); } @@ -245,12 +253,12 @@ void GUIMainMenu::regenerateGui(v2u32 screensize) // Server parameters { core::rect rect(0, 0, 250, 30); - rect += topleft_server + v2s32(40, 30); + rect += topleft_server + v2s32(35, 30); Environment->addCheckBox(creative_mode, rect, this, 259, L"Creative Mode"); } { core::rect rect(0, 0, 250, 30); - rect += topleft_server + v2s32(40, 60); + rect += topleft_server + v2s32(35, 60); Environment->addCheckBox(enable_damage, rect, this, 261, L"Enable Damage"); } // Map delete button @@ -296,6 +304,11 @@ void GUIMainMenu::acceptInput() if(e != NULL) m_data->name = e->getText(); } + { + gui::IGUIElement *e = getElementFromId(264); + if(e != NULL) + m_data->password = e->getText(); + } { gui::IGUIElement *e = getElementFromId(256); if(e != NULL) @@ -380,7 +393,7 @@ bool GUIMainMenu::OnEvent(const SEvent& event) { switch(event.GUIEvent.Caller->getID()) { - case 256: case 257: case 258: + case 256: case 257: case 258: case 264: acceptInput(); quitMenu(); return true; diff --git a/src/guiMainMenu.h b/src/guiMainMenu.h index 4999d68ba..edd519024 100644 --- a/src/guiMainMenu.h +++ b/src/guiMainMenu.h @@ -46,6 +46,7 @@ struct MainMenuData std::wstring address; std::wstring port; std::wstring name; + std::wstring password; bool fancy_trees; bool smooth_lighting; // Server options diff --git a/src/main.cpp b/src/main.cpp index 5d607b2d8..0b181a36d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -325,6 +325,8 @@ Making it more portable: #include "materials.h" #include "game.h" #include "keycode.h" +#include "sha1.h" +#include "base64.h" // This makes textures ITextureSource *g_texturesource = NULL; @@ -1170,6 +1172,7 @@ int main(int argc, char *argv[]) return 0; } + /* More parameters */ @@ -1324,11 +1327,15 @@ int main(int argc, char *argv[]) */ std::wstring error_message = L""; + // The password entered during the menu screen, + std::string password; + /* Menu-game loop */ while(device->run() && kill == false) { + // This is used for catching disconnects try { @@ -1428,6 +1435,25 @@ int main(int argc, char *argv[]) } playername = wide_to_narrow(menudata.name); + + // Get an sha-1 hash of the player's name combined with + // the password entered. That's what the server uses as + // their password. (Exception : if the password field is + // blank, we send a blank password - this is for backwards + // compatibility with password-less players). + if(menudata.password.length() > 0) + { + std::string slt=playername + wide_to_narrow(menudata.password); + SHA1 *sha1 = new SHA1(); + sha1->addBytes(slt.c_str(), slt.length()); + unsigned char *digest = sha1->getDigest(); + password = base64_encode(digest, 20); + } + else + { + password = ""; + } + address = wide_to_narrow(menudata.address); int newport = stoi(wide_to_narrow(menudata.port)); if(newport != 0) @@ -1474,6 +1500,7 @@ int main(int argc, char *argv[]) font, map_dir, playername, + password, address, port, error_message diff --git a/src/player.cpp b/src/player.cpp index 3f92e899c..539244709 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -87,6 +87,7 @@ Player::Player(): m_position(0,0,0) { updateName(""); + updatePassword(""); resetInventory(); } @@ -145,6 +146,7 @@ void Player::serialize(std::ostream &os) Settings args; args.setS32("version", 1); args.set("name", m_name); + args.set("password", m_password); args.setFloat("pitch", m_pitch); args.setFloat("yaw", m_yaw); args.setV3F("position", m_position); @@ -179,6 +181,10 @@ void Player::deSerialize(std::istream &is) //args.getS32("version"); std::string name = args.get("name"); updateName(name.c_str()); + std::string password = ""; + if(args.exists("password")) + password = args.get("password"); + updatePassword(password.c_str()); m_pitch = args.getFloat("pitch"); m_yaw = args.getFloat("yaw"); m_position = args.getV3F("position"); diff --git a/src/player.h b/src/player.h index be93766fd..925252e49 100644 --- a/src/player.h +++ b/src/player.h @@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "collision.h" #define PLAYERNAME_SIZE 20 +#define PASSWORD_SIZE 28 // Maximum password length. Allows for + // base64-encoded SHA-1. #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.," @@ -121,6 +123,16 @@ public: return m_name; } + virtual void updatePassword(const char *password) + { + snprintf(m_password, PASSWORD_SIZE, "%s", password); + } + + const char * getPassword() + { + return m_password; + } + virtual bool isLocal() const = 0; virtual void updateLight(u8 light_at_pos) {}; @@ -157,6 +169,7 @@ public: protected: char m_name[PLAYERNAME_SIZE]; + char m_password[PASSWORD_SIZE]; f32 m_pitch; f32 m_yaw; v3f m_speed; diff --git a/src/server.cpp b/src/server.cpp index d211186eb..051ca85fb 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1734,8 +1734,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // [0] u16 TOSERVER_INIT // [2] u8 SER_FMT_VER_HIGHEST // [3] u8[20] player_name + // [23] u8[28] password <--- can be sent without this, from old versions - if(datasize < 3) + if(datasize < 2+1+PLAYERNAME_SIZE) return; derr_server<getPassword(),password)) + { + derr_server< data((u8*)s.c_str(), s.size()); + // Send as reliable + con.Send(peer_id, 0, data, true); +} + /* Non-static send methods */ @@ -4052,8 +4091,7 @@ v3f findSpawnPos(ServerMap &map) ), BS); } -Player *Server::emergePlayer(const char *name, const char *password, - u16 peer_id) +Player *Server::emergePlayer(const char *name, const char *password, u16 peer_id) { /* Try to get an existing player @@ -4099,6 +4137,7 @@ Player *Server::emergePlayer(const char *name, const char *password, //player->peer_id = PEER_ID_INEXISTENT; player->peer_id = peer_id; player->updateName(name); + player->updatePassword(password); /* Set player position diff --git a/src/server.h b/src/server.h index d8b47aef9..289f09618 100644 --- a/src/server.h +++ b/src/server.h @@ -436,6 +436,7 @@ private: */ static void SendHP(con::Connection &con, u16 peer_id, u8 hp); + static void SendAccessDenied(con::Connection &con, u16 peer_id); /* Non-static send methods @@ -476,11 +477,12 @@ private: /* Get a player from memory or creates one. If player is already connected, return NULL + The password is not checked here - it is only used to + set the password if a new player is created. Call with env and con locked. */ - Player *emergePlayer(const char *name, const char *password, - u16 peer_id); + Player *emergePlayer(const char *name, const char *password, u16 peer_id); /* Update water pressure. diff --git a/src/sha1.cpp b/src/sha1.cpp new file mode 100644 index 000000000..93df10969 --- /dev/null +++ b/src/sha1.cpp @@ -0,0 +1,191 @@ +/* sha1.cpp + +Copyright (c) 2005 Michael D. Leonhard + +http://tamale.net/ + +This file is licensed under the terms described in the +accompanying LICENSE file. +*/ + +#include +#include +#include +#include + +#include "sha1.h" + +// print out memory in hexadecimal +void SHA1::hexPrinter( unsigned char* c, int l ) +{ + assert( c ); + assert( l > 0 ); + while( l > 0 ) + { + printf( " %02x", *c ); + l--; + c++; + } +} + +// circular left bit rotation. MSB wraps around to LSB +Uint32 SHA1::lrot( Uint32 x, int bits ) +{ + return (x<>(32 - bits)); +}; + +// Save a 32-bit unsigned integer to memory, in big-endian order +void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num ) +{ + assert( byte ); + byte[0] = (unsigned char)(num>>24); + byte[1] = (unsigned char)(num>>16); + byte[2] = (unsigned char)(num>>8); + byte[3] = (unsigned char)num; +} + + +// Constructor ******************************************************* +SHA1::SHA1() +{ + // make sure that the data type is the right size + assert( sizeof( Uint32 ) * 5 == 20 ); + + // initialize + H0 = 0x67452301; + H1 = 0xefcdab89; + H2 = 0x98badcfe; + H3 = 0x10325476; + H4 = 0xc3d2e1f0; + unprocessedBytes = 0; + size = 0; +} + +// Destructor ******************************************************** +SHA1::~SHA1() +{ + // erase data + H0 = H1 = H2 = H3 = H4 = 0; + for( int c = 0; c < 64; c++ ) bytes[c] = 0; + unprocessedBytes = size = 0; +} + +// process *********************************************************** +void SHA1::process() +{ + assert( unprocessedBytes == 64 ); + //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" ); + int t; + Uint32 a, b, c, d, e, K, f, W[80]; + // starting values + a = H0; + b = H1; + c = H2; + d = H3; + e = H4; + // copy and expand the message block + for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24) + +(bytes[t*4 + 1] << 16) + +(bytes[t*4 + 2] << 8) + + bytes[t*4 + 3]; + for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 ); + + /* main loop */ + Uint32 temp; + for( t = 0; t < 80; t++ ) + { + if( t < 20 ) { + K = 0x5a827999; + f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~ + } else if( t < 40 ) { + K = 0x6ed9eba1; + f = b ^ c ^ d; + } else if( t < 60 ) { + K = 0x8f1bbcdc; + f = (b & c) | (b & d) | (c & d); + } else { + K = 0xca62c1d6; + f = b ^ c ^ d; + } + temp = lrot(a,5) + f + e + W[t] + K; + e = d; + d = c; + c = lrot(b,30); + b = a; + a = temp; + //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e ); + } + /* add variables */ + H0 += a; + H1 += b; + H2 += c; + H3 += d; + H4 += e; + //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 ); + /* all bytes have been processed */ + unprocessedBytes = 0; +} + +// addBytes ********************************************************** +void SHA1::addBytes( const char* data, int num ) +{ + assert( data ); + assert( num > 0 ); + // add these bytes to the running total + size += num; + // repeat until all data is processed + while( num > 0 ) + { + // number of bytes required to complete block + int needed = 64 - unprocessedBytes; + assert( needed > 0 ); + // number of bytes to copy (use smaller of two) + int toCopy = (num < needed) ? num : needed; + // Copy the bytes + memcpy( bytes + unprocessedBytes, data, toCopy ); + // Bytes have been copied + num -= toCopy; + data += toCopy; + unprocessedBytes += toCopy; + + // there is a full block + if( unprocessedBytes == 64 ) process(); + } +} + +// digest ************************************************************ +unsigned char* SHA1::getDigest() +{ + // save the message size + Uint32 totalBitsL = size << 3; + Uint32 totalBitsH = size >> 29; + // add 0x80 to the message + addBytes( "\x80", 1 ); + + unsigned char footer[64] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + // block has no room for 8-byte filesize, so finish it + if( unprocessedBytes > 56 ) + addBytes( (char*)footer, 64 - unprocessedBytes); + assert( unprocessedBytes <= 56 ); + // how many zeros do we need + int neededZeros = 56 - unprocessedBytes; + // store file size (in bits) in big-endian format + storeBigEndianUint32( footer + neededZeros , totalBitsH ); + storeBigEndianUint32( footer + neededZeros + 4, totalBitsL ); + // finish the final block + addBytes( (char*)footer, neededZeros + 8 ); + // allocate memory for the digest bytes + unsigned char* digest = (unsigned char*)malloc( 20 ); + // copy the digest bytes + storeBigEndianUint32( digest, H0 ); + storeBigEndianUint32( digest + 4, H1 ); + storeBigEndianUint32( digest + 8, H2 ); + storeBigEndianUint32( digest + 12, H3 ); + storeBigEndianUint32( digest + 16, H4 ); + // return the digest + return digest; +} diff --git a/src/sha1.h b/src/sha1.h new file mode 100644 index 000000000..2f92fe8d1 --- /dev/null +++ b/src/sha1.h @@ -0,0 +1,35 @@ +/* sha1.h + +Copyright (c) 2005 Michael D. Leonhard + +http://tamale.net/ + +This file is licensed under the terms described in the +accompanying LICENSE file. +*/ + +#ifndef SHA1_HEADER +typedef unsigned int Uint32; + +class SHA1 +{ + private: + // fields + Uint32 H0, H1, H2, H3, H4; + unsigned char bytes[64]; + int unprocessedBytes; + Uint32 size; + void process(); + public: + SHA1(); + ~SHA1(); + void addBytes( const char* data, int num ); + unsigned char* getDigest(); + // utility methods + static Uint32 lrot( Uint32 x, int bits ); + static void storeBigEndianUint32( unsigned char* byte, Uint32 num ); + static void hexPrinter( unsigned char* c, int l ); +}; + +#define SHA1_HEADER +#endif From 52d857cf1527948e1db5eead457e51b6741de8a2 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Fri, 20 May 2011 21:37:13 +0100 Subject: [PATCH 4/4] Passwords - a few corrections to the previous commit --- src/main.cpp | 1 + src/server.cpp | 2 +- src/sha1.cpp | 20 ++++++++++++++++++-- src/sha1.h | 20 ++++++++++++++++++-- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0b181a36d..184643b9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1448,6 +1448,7 @@ int main(int argc, char *argv[]) sha1->addBytes(slt.c_str(), slt.length()); unsigned char *digest = sha1->getDigest(); password = base64_encode(digest, 20); + free(digest); } else { diff --git a/src/server.cpp b/src/server.cpp index 051ca85fb..b5cb48a36 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1780,7 +1780,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(datasize == 2+1+PLAYERNAME_SIZE) { // old version - assume blank password - *password = 0; + password[0] = 0; } else { diff --git a/src/sha1.cpp b/src/sha1.cpp index 93df10969..98180adc7 100644 --- a/src/sha1.cpp +++ b/src/sha1.cpp @@ -4,8 +4,24 @@ Copyright (c) 2005 Michael D. Leonhard http://tamale.net/ -This file is licensed under the terms described in the -accompanying LICENSE file. +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ #include diff --git a/src/sha1.h b/src/sha1.h index 2f92fe8d1..c04947373 100644 --- a/src/sha1.h +++ b/src/sha1.h @@ -4,8 +4,24 @@ Copyright (c) 2005 Michael D. Leonhard http://tamale.net/ -This file is licensed under the terms described in the -accompanying LICENSE file. +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ #ifndef SHA1_HEADER