map/*
+world/*
CMakeFiles/*
src/CMakeFiles/*
src/Makefile
- Slightly updated map format
- Player passwords
- All textures first searched from texture_path
+- Map directory ("map") has been renamed to "world" (just rename it to load an old world)
2011-04-24:
- Smooth lighting with simple ambient occlusion
#enable_damage = false
+#default_password =
+
+# Available privileges: build, teleport, settime, privs, shout
+#default_privs = build, shout
+
# Gives some stuff to players at the beginning
#give_initial_stuff = false
)
set(common_SRCS
+ auth.cpp
collision.cpp
nodemetadata.cpp
serverobject.cpp
--- /dev/null
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+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; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; 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.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "auth.h"
+#include <fstream>
+#include <jmutexautolock.h>
+//#include "main.h" // for g_settings
+#include <sstream>
+#include "strfnd.h"
+#include "debug.h"
+
+// Convert a privileges value into a human-readable string,
+// with each component separated by a comma.
+std::string privsToString(u64 privs)
+{
+ std::ostringstream os(std::ios_base::binary);
+ if(privs & PRIV_BUILD)
+ os<<"build,";
+ if(privs & PRIV_TELEPORT)
+ os<<"teleport,";
+ if(privs & PRIV_SETTIME)
+ os<<"settime,";
+ if(privs & PRIV_PRIVS)
+ os<<"privs,";
+ if(privs & PRIV_SHOUT)
+ os<<"shout,";
+ if(os.tellp())
+ {
+ // Drop the trailing comma. (Why on earth can't
+ // you truncate a C++ stream anyway???)
+ std::string tmp = os.str();
+ return tmp.substr(0, tmp.length() -1);
+ }
+ return os.str();
+}
+
+// Converts a comma-seperated list of privilege values into a
+// privileges value. The reverse of privsToString(). Returns
+// PRIV_INVALID if there is anything wrong with the input.
+u64 stringToPrivs(std::string str)
+{
+ u64 privs=0;
+ Strfnd f(str);
+ while(f.atend() == false)
+ {
+ std::string s = trim(f.next(","));
+ if(s == "build")
+ privs |= PRIV_BUILD;
+ else if(s == "teleport")
+ privs |= PRIV_TELEPORT;
+ else if(s == "settime")
+ privs |= PRIV_SETTIME;
+ else if(s == "privs")
+ privs |= PRIV_PRIVS;
+ else if(s == "shout")
+ privs |= PRIV_SHOUT;
+ else
+ return PRIV_INVALID;
+ }
+ return privs;
+}
+
+AuthManager::AuthManager(const std::string &authfilepath):
+ m_authfilepath(authfilepath)
+{
+ m_mutex.Init();
+
+ try{
+ load();
+ }
+ catch(SerializationError &e)
+ {
+ dstream<<"WARNING: AuthManager: creating "
+ <<m_authfilepath<<std::endl;
+ }
+}
+
+AuthManager::~AuthManager()
+{
+ save();
+}
+
+void AuthManager::load()
+{
+ JMutexAutoLock lock(m_mutex);
+
+ dstream<<"AuthManager: loading from "<<m_authfilepath<<std::endl;
+ std::ifstream is(m_authfilepath.c_str(), std::ios::binary);
+ if(is.good() == false)
+ {
+ dstream<<"AuthManager: failed loading from "<<m_authfilepath<<std::endl;
+ throw SerializationError("AuthManager::load(): Couldn't open file");
+ }
+
+ for(;;)
+ {
+ if(is.eof() || is.good() == false)
+ break;
+
+ // Read a line
+ std::string line;
+ std::getline(is, line, '\n');
+
+ std::istringstream iss(line);
+
+ // Read name
+ std::string name;
+ std::getline(iss, name, ':');
+
+ // Read password
+ std::string pwd;
+ std::getline(iss, pwd, ':');
+
+ // Read privileges
+ std::string stringprivs;
+ std::getline(iss, stringprivs, ':');
+ u64 privs = stringToPrivs(stringprivs);
+
+ // Store it
+ AuthData ad;
+ ad.pwd = pwd;
+ ad.privs = privs;
+ m_authdata[name] = ad;
+ }
+}
+
+void AuthManager::save()
+{
+ JMutexAutoLock lock(m_mutex);
+
+ dstream<<"AuthManager: saving to "<<m_authfilepath<<std::endl;
+ std::ofstream os(m_authfilepath.c_str(), std::ios::binary);
+ if(os.good() == false)
+ {
+ dstream<<"AuthManager: failed saving to "<<m_authfilepath<<std::endl;
+ throw SerializationError("AuthManager::save(): Couldn't open file");
+ }
+
+ for(core::map<std::string, AuthData>::Iterator
+ i = m_authdata.getIterator();
+ i.atEnd()==false; i++)
+ {
+ std::string name = i.getNode()->getKey();
+ if(name == "")
+ continue;
+ AuthData ad = i.getNode()->getValue();
+ os<<name<<":"<<ad.pwd<<":"<<privsToString(ad.privs)<<"\n";
+ }
+}
+
+bool AuthManager::exists(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ return false;
+ return true;
+}
+
+void AuthManager::set(const std::string &username, AuthData ad)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ m_authdata[username] = ad;
+}
+
+void AuthManager::add(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ m_authdata[username] = AuthData();
+}
+
+std::string AuthManager::getPassword(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ return n->getValue().pwd;
+}
+
+void AuthManager::setPassword(const std::string &username,
+ const std::string &password)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ AuthData ad = n->getValue();
+ ad.pwd = password;
+ n->setValue(ad);
+}
+
+u64 AuthManager::getPrivs(const std::string &username)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ return n->getValue().privs;
+}
+
+void AuthManager::setPrivs(const std::string &username, u64 privs)
+{
+ JMutexAutoLock lock(m_mutex);
+
+ core::map<std::string, AuthData>::Node *n;
+ n = m_authdata.find(username);
+ if(n == NULL)
+ throw AuthNotFoundException("");
+
+ AuthData ad = n->getValue();
+ ad.privs = privs;
+ n->setValue(ad);
+}
+
--- /dev/null
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+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; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; 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.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef AUTH_HEADER
+#define AUTH_HEADER
+
+#include <string>
+#include <jthread.h>
+#include <jmutex.h>
+#include "common_irrlicht.h"
+#include "exceptions.h"
+
+// Player privileges. These form a bitmask stored in the privs field
+// of the player, and define things they're allowed to do. See also
+// the static methods Player::privsToString and stringToPrivs that
+// convert these to human-readable form.
+const u64 PRIV_BUILD = 1; // Can build - i.e. modify the world
+const u64 PRIV_TELEPORT = 2; // Can teleport
+const u64 PRIV_SETTIME = 4; // Can set the time
+const u64 PRIV_PRIVS = 8; // Can grant and revoke privileges
+const u64 PRIV_SERVER = 16; // Can manage the server (e.g. shutodwn
+ // ,settings)
+const u64 PRIV_SHOUT = 32; // Can broadcast chat messages to all
+ // players
+
+// Default privileges - these can be overriden for new players using the
+// config option "default_privs" - however, this value still applies for
+// players that existed before the privileges system was added.
+const u64 PRIV_DEFAULT = PRIV_BUILD|PRIV_SHOUT;
+const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
+const u64 PRIV_INVALID = 0x8000000000000000ULL;
+
+// Convert a privileges value into a human-readable string,
+// with each component separated by a comma.
+std::string privsToString(u64 privs);
+
+// Converts a comma-seperated list of privilege values into a
+// privileges value. The reverse of privsToString(). Returns
+// PRIV_INVALID if there is anything wrong with the input.
+u64 stringToPrivs(std::string str);
+
+struct AuthData
+{
+ std::string pwd;
+ u64 privs;
+
+ AuthData():
+ privs(PRIV_DEFAULT)
+ {
+ }
+};
+
+class AuthNotFoundException : public BaseException
+{
+public:
+ AuthNotFoundException(const char *s):
+ BaseException(s)
+ {}
+};
+
+class AuthManager
+{
+public:
+ AuthManager(const std::string &authfilepath);
+ ~AuthManager();
+ void load();
+ void save();
+ bool exists(const std::string &username);
+ void set(const std::string &username, AuthData ad);
+ void add(const std::string &username);
+ std::string getPassword(const std::string &username);
+ void setPassword(const std::string &username,
+ const std::string &password);
+ u64 getPrivs(const std::string &username);
+ void setPrivs(const std::string &username, u64 privs);
+private:
+ JMutex m_mutex;
+ std::string m_authfilepath;
+ core::map<std::string, AuthData> m_authdata;
+};
+
+#endif
+
// to be processed even if the serialisation format has
// not been agreed yet, the same as TOCLIENT_INIT.
m_access_denied = true;
+ m_access_denied_reason = L"Unknown";
+ if(datasize >= 4)
+ {
+ std::string datastring((char*)&data[2], datasize-2);
+ std::istringstream is(datastring, std::ios_base::binary);
+ m_access_denied_reason = deSerializeWideString(is);
+ }
return;
}
return m_access_denied;
}
+ inline std::wstring accessDeniedReason()
+ {
+ return m_access_denied_reason;
+ }
+
private:
// Virtual methods from con::PeerHandler
std::string m_password;
bool m_access_denied;
+ std::wstring m_access_denied_reason;
InventoryContext m_inventory_context;
#define PROTOCOL_ID 0x4f457403
+#define PASSWORD_SIZE 28 // Maximum password length. Allows for
+ // base64-encoded SHA-1 (27+\0).
+
enum ToClientCommand
{
TOCLIENT_INIT = 0x10,
TOCLIENT_ACCESS_DENIED = 0x35,
/*
u16 command
+ u16 reason_length
+ wstring reason
*/
};
// Server stuff
g_settings.setDefault("enable_experimental", "false");
g_settings.setDefault("creative_mode", "false");
- g_settings.setDefault("enable_damage", "false"); //TODO: Set to true
+ g_settings.setDefault("enable_damage", "false"); //TODO: Set to true when healing is possible
g_settings.setDefault("give_initial_stuff", "false");
+ g_settings.setDefault("default_password", "");
+ g_settings.setDefault("default_privs", "build, shout");
g_settings.setDefault("objectdata_interval", "0.2");
g_settings.setDefault("active_object_range", "2");
testplayer.deSerialize(is);
}
- dstream<<"Loaded test player with name "<<testplayer.getName()<<std::endl;
+ if(!string_allowed(testplayer.getName(), PLAYERNAME_ALLOWED_CHARS))
+ {
+ dstream<<"Not loading player with invalid name: "
+ <<testplayer.getName()<<std::endl;
+ }
+
+ dstream<<"Loaded test player with name "<<testplayer.getName()
+ <<std::endl;
// Search for the player
std::string playername = testplayer.getName();
activateObjects(block);
// TODO: Do something
+ // TODO: Implement usage of ActiveBlockModifier
// Here's a quick demonstration
v3s16 p0;
Everything should bind to inside this single content
searching loop to keep things fast.
*/
+ // TODO: Implement usage of ActiveBlockModifier
v3s16 p0;
for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++)
private:
};
-/*
- Active block modifier interface
-*/
-
-class ServerEnvironment;
-
-class ActiveBlockModifier
-{
-public:
- ActiveBlockModifier(){};
- virtual ~ActiveBlockModifier(){};
-
- virtual u32 getTriggerContentCount(){ return 1;}
- virtual u8 getTriggerContent(u32 i) = 0;
- virtual float getActiveInterval() = 0;
- virtual u32 getActiveChance() = 0;
- virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
-};
-
/*
The server-side environment.
#include "serverobject.h"
class Server;
+class ActiveBlockModifier;
class ServerEnvironment : public Environment
{
/*
ActiveObjects
+ -------------------------------------------
*/
ServerActiveObject* getActiveObject(u16 id);
*/
ActiveObjectMessage getActiveObjectMessage();
+ /*
+ ActiveBlockModifiers
+ -------------------------------------------
+ */
+
+ void addActiveBlockModifier(ActiveBlockModifier *abm);
+
private:
/*
Remove all objects that satisfy (m_removed && m_known_by_count==0)
float m_game_time_fraction_counter;
};
+/*
+ Active block modifier interface.
+
+ These are fed into ServerEnvironment at initialization time;
+ ServerEnvironment handles deleting them.
+*/
+
+class ActiveBlockModifier
+{
+public:
+ ActiveBlockModifier(){};
+ virtual ~ActiveBlockModifier(){};
+
+ //virtual core::list<u8> update(ServerEnvironment *env) = 0;
+ virtual u32 getTriggerContentCount(){ return 1;}
+ virtual u8 getTriggerContent(u32 i) = 0;
+ virtual float getActiveInterval() = 0;
+ // chance of (1 / return value), 0 is disallowed
+ virtual u32 getActiveChance() = 0;
+ // This is called usually at interval for 1/chance of the nodes
+ virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
+};
+
#ifndef SERVER
#include "clientobject.h"
{
if(client.accessDenied())
{
- error_message = L"Access denied. Check your password and try again.";
- std::cout<<DTIME<<"Access denied."<<std::endl;
+ error_message = L"Access denied. Reason: "
+ +client.accessDeniedReason();
+ std::cout<<DTIME<<wide_to_narrow(error_message)<<std::endl;
}
else
{
core::rect<s32> rect(0, 0, 130, 30);
//rect += topleft_server + v2s32(size_server.X-40-130, 100+25);
rect += topleft_server + v2s32(40, 100+25);
- Environment->addButton(rect, this, 260, L"Delete map");
+ Environment->addButton(rect, this, 260, L"Delete world");
}
}
- NOTE: There is a simple move implementation now in collision.{h,cpp}\r
- NOTE: MovingObject will be deleted (MapBlockObject)\r
\r
+TODO: Add a long step function to objects that is called with the time\r
+ difference when block activates\r
+\r
Map:\r
----\r
\r
\r
Stuff to do before release:\r
---------------------------\r
-- Player default privileges and default password\r
-- Chat privilege\r
-- Some simple block-based dynamic stuff in the world (finish the\r
- ActiveBlockModifier stuff)\r
+- Make grass grow slower; utilize timestamp difference\r
- Protocol version field\r
- Consider getting some textures from cisoun's texture pack\r
-- Add a long step function to objects that is called with the time\r
- difference when block activates\r
+ - Ask from Cisoun\r
+- Make sure the fence implementation and data format is good\r
+ - Think about using same bits for material for fences and doors, for\r
+ example\r
+- Make sure server handles removing grass when a block is placed (etc)\r
+ - The client should not do it by itself\r
+- Add mouse inversion in config\r
+- Block cube placement around player's head\r
+- Move mineral to param2, increment map serialization version, add conversion\r
+\r
+Stuff to do after release:\r
+---------------------------\r
+- Finish the ActiveBlockModifier stuff and use it for something\r
\r
======================================================================\r
\r
port = 30000;\r
\r
// Map directory\r
- std::string map_dir = porting::path_userdata+"/map";\r
+ std::string map_dir = porting::path_userdata+"/world";\r
if(cmd_args.exists("map-dir"))\r
map_dir = cmd_args.get("map-dir");\r
else if(g_settings.exists("map-dir"))\r
g_settings.set("creative_mode", itos(menudata.creative_mode));\r
g_settings.set("enable_damage", itos(menudata.enable_damage));\r
\r
- // Check for valid parameters, restart menu if invalid.\r
+ /*// Check for valid parameters, restart menu if invalid.\r
if(playername == "")\r
{\r
error_message = L"Name required.";\r
continue;\r
}\r
- \r
+ // Check that name has only valid chars\r
+ if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)\r
+ {\r
+ error_message = L"Characters allowed: "\r
+ +narrow_to_wide(PLAYERNAME_ALLOWED_CHARS);\r
+ continue;\r
+ }*/\r
+\r
// Save settings\r
g_settings.set("name", playername);\r
g_settings.set("address", address);\r
#include "constants.h"
#include "utility.h"
-// Convert a privileges value into a human-readable string,
-// with each component separated by a comma.
-std::wstring privsToString(u64 privs)
-{
- std::wostringstream os(std::ios_base::binary);
- if(privs & PRIV_BUILD)
- os<<L"build,";
- if(privs & PRIV_TELEPORT)
- os<<L"teleport,";
- if(privs & PRIV_SETTIME)
- os<<L"settime,";
- if(privs & PRIV_PRIVS)
- os<<L"privs,";
- if(privs & PRIV_SHOUT)
- os<<L"shout,";
- if(os.tellp())
- {
- // Drop the trailing comma. (Why on earth can't
- // you truncate a C++ stream anyway???)
- std::wstring tmp = os.str();
- return tmp.substr(0, tmp.length() -1);
- }
- return os.str();
-}
-
-// Converts a comma-seperated list of privilege values into a
-// privileges value. The reverse of privsToString(). Returns
-// PRIV_INVALID if there is anything wrong with the input.
-u64 stringToPrivs(std::wstring str)
-{
- u64 privs=0;
- std::vector<std::wstring> pr;
- pr=str_split(str, ',');
- for(std::vector<std::wstring>::iterator i = pr.begin();
- i != pr.end(); ++i)
- {
- if(*i == L"build")
- privs |= PRIV_BUILD;
- else if(*i == L"teleport")
- privs |= PRIV_TELEPORT;
- else if(*i == L"settime")
- privs |= PRIV_SETTIME;
- else if(*i == L"privs")
- privs |= PRIV_PRIVS;
- else if(*i == L"shout")
- privs |= PRIV_SHOUT;
- else
- return PRIV_INVALID;
- }
- return privs;
-}
-
Player::Player():
touching_ground(false),
swimming_up(false),
craftresult_is_preview(true),
hp(20),
- privs(PRIV_DEFAULT),
peer_id(PEER_ID_INEXISTENT),
m_pitch(0),
m_yaw(0),
m_position(0,0,0)
{
updateName("<not set>");
- updatePassword("");
resetInventory();
}
Settings args;
args.setS32("version", 1);
args.set("name", m_name);
- args.set("password", m_password);
+ //args.set("password", m_password);
args.setFloat("pitch", m_pitch);
args.setFloat("yaw", m_yaw);
args.setV3F("position", m_position);
//args.getS32("version");
std::string name = args.get("name");
updateName(name.c_str());
- std::string password = "";
+ /*std::string password = "";
if(args.exists("password"))
password = args.get("password");
- updatePassword(password.c_str());
+ updatePassword(password.c_str());*/
m_pitch = args.getFloat("pitch");
m_yaw = args.getFloat("yaw");
m_position = args.getV3F("position");
}catch(SettingNotFoundException &e){
hp = 20;
}
- try{
+ /*try{
std::string sprivs = args.get("privs");
if(sprivs == "all")
{
}
}catch(SettingNotFoundException &e){
privs = PRIV_DEFAULT;
- }
+ }*/
inventory.deSerialize(is);
}
#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-_.,"
-
-// Player privileges. These form a bitmask stored in the privs field
-// of the player, and define things they're allowed to do. See also
-// the static methods Player::privsToString and stringToPrivs that
-// convert these to human-readable form.
-const u64 PRIV_BUILD = 1; // Can build - i.e. modify the world
-const u64 PRIV_TELEPORT = 2; // Can teleport
-const u64 PRIV_SETTIME = 4; // Can set the time
-const u64 PRIV_PRIVS = 8; // Can grant and revoke privileges
-const u64 PRIV_SERVER = 16; // Can manage the server (e.g. shutodwn
- // ,settings)
-const u64 PRIV_SHOUT = 32; // Can broadcast chat messages to all
- // players
-
-// Default privileges - these can be overriden for new players using the
-// config option "default_privs" - however, this value still applies for
-// players that existed before the privileges system was added.
-const u64 PRIV_DEFAULT = PRIV_BUILD|PRIV_SHOUT;
-const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
-const u64 PRIV_INVALID = 0x8000000000000000ULL;
-
-// Convert a privileges value into a human-readable string,
-// with each component separated by a comma.
-std::wstring privsToString(u64 privs);
-
-// Converts a comma-seperated list of privilege values into a
-// privileges value. The reverse of privsToString(). Returns
-// PRIV_INVALID if there is anything wrong with the input.
-u64 stringToPrivs(std::wstring str);
+
+#define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
class Map;
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) {};
protected:
char m_name[PLAYERNAME_SIZE];
- char m_password[PASSWORD_SIZE];
f32 m_pitch;
f32 m_yaw;
v3f m_speed;
):
m_env(new ServerMap(mapsavedir), this),
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, this),
+ m_authmanager(mapsavedir+"/auth.txt"),
m_thread(this),
m_emergethread(this),
m_time_counter(0),
}
}
- // Save map
+ // Save map, players and auth stuff
{
float &counter = m_savemap_timer;
counter += dtime;
{
counter = 0.0;
+ // Auth stuff
+ m_authmanager.save();
+
+ // Map
JMutexAutoLock lock(m_env_mutex);
-
if(((ServerMap*)(&m_env.getMap()))->isSavingEnabled() == true)
{
// Save only changed parts
playername[i] = data[3+i];
}
playername[PLAYERNAME_SIZE-1] = 0;
-
+
+ if(playername[0]=='\0')
+ {
+ derr_server<<DTIME<<"Server: Player has empty name"<<std::endl;
+ SendAccessDenied(m_con, peer_id,
+ L"Empty name");
+ return;
+ }
+
+ if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)
+ {
+ derr_server<<DTIME<<"Server: Player has invalid name"<<std::endl;
+ SendAccessDenied(m_con, peer_id,
+ L"Name contains unallowed characters");
+ return;
+ }
+
// Get password
char password[PASSWORD_SIZE];
if(datasize == 2+1+PLAYERNAME_SIZE)
}
password[PASSWORD_SIZE-1] = 0;
}
- Player *checkplayer = m_env.getPlayer(playername);
- if(checkplayer != NULL && strcmp(checkplayer->getPassword(),password))
+
+ std::string checkpwd;
+ if(m_authmanager.exists(playername))
+ {
+ checkpwd = m_authmanager.getPassword(playername);
+ }
+ else
+ {
+ checkpwd = g_settings.get("default_password");
+ }
+
+ if(password != checkpwd)
{
derr_server<<DTIME<<"Server: peer_id="<<peer_id
<<": supplied invalid password for "
<<playername<<std::endl;
- SendAccessDenied(m_con, peer_id);
+ SendAccessDenied(m_con, peer_id, L"Invalid password");
return;
}
+
+ // Add player to auth manager
+ if(m_authmanager.exists(playername) == false)
+ {
+ derr_server<<DTIME<<"Server: adding player "<<playername
+ <<" to auth manager"<<std::endl;
+ m_authmanager.add(playername);
+ m_authmanager.setPassword(playername, checkpwd);
+ m_authmanager.setPrivs(playername,
+ stringToPrivs(g_settings.get("default_privs")));
+ m_authmanager.save();
+ }
// Get player
Player *player = emergePlayer(playername, password, peer_id);
if(datasize != 2+PASSWORD_SIZE*2)
return;
- char password[PASSWORD_SIZE];
+ /*char password[PASSWORD_SIZE];
for(u32 i=0; i<PASSWORD_SIZE-1; i++)
password[i] = data[2+i];
- password[PASSWORD_SIZE-1] = 0;
- if(strcmp(player->getPassword(),password))
+ password[PASSWORD_SIZE-1] = 0;*/
+ std::string oldpwd;
+ for(u32 i=0; i<PASSWORD_SIZE-1; i++)
+ {
+ char c = data[2+i];
+ if(c == 0)
+ break;
+ oldpwd += c;
+ }
+ std::string newpwd;
+ for(u32 i=0; i<PASSWORD_SIZE-1; i++)
+ {
+ char c = data[2+PASSWORD_SIZE+i];
+ if(c == 0)
+ break;
+ newpwd += c;
+ }
+
+ std::string playername = player->getName();
+
+ if(m_authmanager.exists(playername) == false)
{
+ dstream<<"Server: playername not found in authmanager"<<std::endl;
+ // Wrong old password supplied!!
+ SendChatMessage(peer_id, L"playername not found in authmanager");
+ return;
+ }
+
+ std::string checkpwd = m_authmanager.getPassword(playername);
+
+ if(oldpwd != checkpwd)
+ {
+ dstream<<"Server: invalid old password"<<std::endl;
// Wrong old password supplied!!
SendChatMessage(peer_id, L"Invalid old password supplied. Password NOT changed.");
return;
}
- for(u32 i=0; i<PASSWORD_SIZE-1; i++)
- password[i] = data[30+i];
- player->updatePassword(password);
+
+ m_authmanager.setPassword(playername, newpwd);
+
+ dstream<<"Server: password change successful for "<<playername
+ <<std::endl;
SendChatMessage(peer_id, L"Password change successful");
}
else
con.Send(peer_id, 0, data, true);
}
-void Server::SendAccessDenied(con::Connection &con, u16 peer_id)
+void Server::SendAccessDenied(con::Connection &con, u16 peer_id,
+ const std::wstring &reason)
{
DSTACK(__FUNCTION_NAME);
std::ostringstream os(std::ios_base::binary);
writeU16(os, TOCLIENT_ACCESS_DENIED);
+ os<<serializeWideString(reason);
// Make data buffer
std::string s = os.str();
//player->peer_id = PEER_ID_INEXISTENT;
player->peer_id = peer_id;
player->updateName(name);
- player->updatePassword(password);
+ m_authmanager.add(name);
+ m_authmanager.setPassword(name, password);
+ m_authmanager.setPrivs(name,
+ stringToPrivs(g_settings.get("default_privs")));
if(g_settings.exists("default_privs"))
player->privs = g_settings.getU64("default_privs");
#include "porting.h"
#include "map.h"
#include "inventory.h"
+#include "auth.h"
/*
Some random functions
*/
static void SendHP(con::Connection &con, u16 peer_id, u8 hp);
- static void SendAccessDenied(con::Connection &con, u16 peer_id);
+ static void SendAccessDenied(con::Connection &con, u16 peer_id,
+ const std::wstring &reason);
/*
Non-static send methods
JMutex m_con_mutex;
// Connected clients (behind the con mutex)
core::map<u16, RemoteClient*> m_clients;
+
+ // User authentication
+ AuthManager m_authmanager;
/*
Threads
{
// Show our own real privs, without any adjustments
// made for admin status
- os<<L"-!- " + privsToString(ctx->player->privs);
+ os<<L"-!- " + narrow_to_wide(privsToString(ctx->player->privs));
return;
}
return;
}
- os<<L"-!- " + privsToString(tp->privs);
+ os<<L"-!- " + narrow_to_wide(privsToString(tp->privs));
}
void cmd_grantrevoke(std::wostringstream &os,
return;
}
- u64 newprivs = stringToPrivs(ctx->parms[2]);
+ u64 newprivs = stringToPrivs(wide_to_narrow(ctx->parms[2]));
if(newprivs == PRIV_INVALID)
{
os<<L"-!- Invalid privileges specified";
tp->privs &= ~newprivs;
os<<L"-!- Privileges change to ";
- os<<privsToString(tp->privs);
+ os<<narrow_to_wide(privsToString(tp->privs));
}
void cmd_time(std::wostringstream &os,
}
// Figure out path to map
- std::string map_dir = porting::path_userdata+"/map";
+ std::string map_dir = porting::path_userdata+"/world";
if(cmd_args.exists("map-dir"))
map_dir = cmd_args.get("map-dir");
else if(g_settings.exists("map-dir"))
return s;
}
-/*// Reads a string with the length as the first two bytes
-inline std::string deSerializeString(const std::string encoded)
+// Creates a string with the length as the first two bytes from wide string
+inline std::string serializeWideString(const std::wstring &plain)
{
- u16 s_size = readU16((u8*)&encoded.c_str()[0]);
- if(s_size > encoded.length() - 2)
- return "";
+ //assert(plain.size() <= 65535);
+ if(plain.size() > 65535)
+ throw SerializationError("String too long for serializeString");
+ char buf[2];
+ writeU16((u8*)buf, plain.size());
std::string s;
- s.reserve(s_size);
- s.append(&encoded.c_str()[2], s_size);
+ s.append(buf, 2);
+ for(u32 i=0; i<plain.size(); i++)
+ {
+ writeU16((u8*)buf, plain[i]);
+ s.append(buf, 2);
+ }
return s;
-}*/
+}
// Reads a string with the length as the first two bytes
inline std::string deSerializeString(std::istream &is)
return s;
}
+// Reads a wide string with the length as the first two bytes
+inline std::wstring deSerializeWideString(std::istream &is)
+{
+ char buf[2];
+ is.read(buf, 2);
+ if(is.gcount() != 2)
+ throw SerializationError("deSerializeString: size not read");
+ u16 s_size = readU16((u8*)buf);
+ if(s_size == 0)
+ return L"";
+ std::wstring s;
+ s.reserve(s_size);
+ for(u32 i=0; i<s_size; i++)
+ {
+ is.read(&buf[0], 2);
+ wchar_t c16 = readU16((u8*)buf);
+ s.append(&c16, 1);
+ }
+ return s;
+}
+
// Creates a string with the length as the first four bytes
inline std::string serializeLongString(const std::string &plain)
{
return s;
}
-/*// Reads a string with the length as the first four bytes
-inline std::string deSerializeLongString(const std::string encoded)
-{
- u32 s_size = readU32((u8*)&encoded.c_str()[0]);
- if(s_size > encoded.length() - 4)
- return "";
- std::string s;
- s.reserve(s_size);
- s.append(&encoded.c_str()[4], s_size);
- return s;
-}*/
-
// Reads a string with the length as the first four bytes
inline std::string deSerializeLongString(std::istream &is)
{