PDA

View Full Version : EQEmuLoginServer - Creates account upon login


pfyon
08-11-2010, 11:24 PM
This might be useful for small servers for just friends. When someone logs in and the username does not already exist in the DB, it will create a new user with username/password given (and email address nobody@localhost).

Tested on Ubuntu 10.04 with rev 1620. The revision numbers in the diff are because I was comitting against my own svn.


Index: Client.cpp
================================================== =================
--- Client.cpp (revision 60)
+++ Client.cpp (working copy)
@@ -225,11 +225,29 @@
_HeapDeleteCharBuffer(e_buffer);
#endif

- bool result;
+ bool result = false;
if(server.db->GetLoginDataFromAccountName(e_user, d_pass_hash, d_account_id) == false)
{
log->Log(log_client_error, "Error logging in, user %s does not exist in the database.", e_user.c_str());
- result = false;
+ if(server.db->CreateLoginAccount(e_user.c_str(), e_hash.c_str()))
+ {
+ //Made the account properly
+ if(server.db->GetLoginDataFromAccountName(e_user, d_pass_hash, d_account_id) == false)
+ {
+ //Somehow did not authenticate properly
+ log->Log(log_client_error, "Error logging in, user %s does not exist in the database.", e_user.c_str());
+ result = false;
+ } else
+ {
+ if(d_pass_hash.compare(e_hash) == 0)
+ {
+ result = true;
+ } else
+ {
+ result = false;
+ }
+ }
+ }
}
else
{



Index: DatabaseMySQL.h
================================================== =================
--- DatabaseMySQL.h (revision 56)
+++ DatabaseMySQL.h (working copy)
@@ -88,6 +88,11 @@
* Creates new world registration for unregistered servers and returns new id
*/
virtual bool CreateWorldRegistration(string long_name, string short_name, unsigned int &id);
+
+ /**
+ * Creates a new login account in the player accounts table
+ */
+ virtual bool CreateLoginAccount(string name, string password);
protected:
string user, pass, host, port, name;
MYSQL *db;



Index: DatabaseMySQL.cpp
================================================== =================
--- DatabaseMySQL.cpp (revision 56)
+++ DatabaseMySQL.cpp (working copy)
@@ -97,6 +97,36 @@
return false;
}

+bool DatabaseMySQL::CreateLoginAccount(string name, string password)
+{
+ if(!db)
+ {
+ return false;
+ }
+
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ char escaped_name[101];
+ char escaped_password[101];
+ unsigned long length;
+ length = mysql_real_escape_string(db, escaped_name, name.substr(0, 50).c_str(), name.substr(0, 100).length());
+ escaped_name[length+1] = 0;
+ length = mysql_real_escape_string(db, escaped_password, password.substr(0, 50).c_str(), password.substr(0, 100).length());
+ escaped_password[length+1] = 0;
+
+ stringstream query(stringstream::in | stringstream::out);
+ query << "INSERT INTO " << server.options.GetAccountTable() << " (AccountName, AccountPassword, AccountCreateDate, AccountEmail)";
+ query << "VALUES ('" << escaped_name << "', '" << escaped_password << "', now(), 'nobody@localhost')";
+
+ if(mysql_query(db, query.str().c_str()) != 0)
+ {
+ log->Log(log_database, "Error creating login account: %s", query.str().c_str());
+ return false;
+ }
+
+ return true;
+}
+
bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, unsigned int &id, string &desc, unsigned int &list_id,
unsigned int &trusted, string &list_desc, string &account, string &password)
{



Index: Database.h
================================================== =================
--- Database.h (revision 56)
+++ Database.h (working copy)
@@ -73,6 +73,11 @@
* Creates new world registration for unregistered servers and returns new id
*/
virtual bool CreateWorldRegistration(string long_name, string short_name, unsigned int &id) { return false; }
+
+ /**
+ * Creates a new login account in the player accounts table
+ */
+ virtual bool CreateLoginAccount(string name, string password) { return false;}
protected:
string user, pass, host, port, name;
};

Swiftsong_Lorekeeper
08-26-2010, 07:36 PM
That is a pretty cool tool. I'm new to running my own server so I was curious, if you have time, that you could explain what to do with these :p I would like to install and be able to not have to worry about manually inputting account information.