2008-10-02 16:23:55 -05:00
/*
2009-02-04 12:42:26 +01:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2009-02-04 12:04:12 +01:00
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-10-14 11:57:03 -05:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* 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
2008-10-14 11:57:03 -05:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Database/DatabaseEnv.h"
2009-06-04 21:41:07 +02:00
#include "Policies/SingletonImp.h"
2009-10-17 15:51:44 -07:00
2009-06-04 21:41:07 +02:00
#include "AccountMgr.h"
2008-10-20 14:11:58 -05:00
#include "ObjectAccessor.h"
2008-10-02 16:23:55 -05:00
#include "Player.h"
#include "Util.h"
2009-09-02 18:14:10 -05:00
#include "Auth/Sha1.h"
2009-10-17 15:51:44 -07:00
2009-07-31 11:29:28 +08:00
extern DatabaseType loginDatabase ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
INSTANTIATE_SINGLETON_1 ( AccountMgr );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
AccountMgr :: AccountMgr ()
{}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
AccountMgr ::~ AccountMgr ()
{}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
AccountOpResult AccountMgr :: CreateAccount ( std :: string username , std :: string password )
{
if ( utf8length ( username ) > MAX_ACCOUNT_STR )
return AOR_NAME_TOO_LONG ; // username's too long
2009-10-17 15:51:44 -07:00
2009-05-21 19:06:09 +02:00
normalizeString ( username );
normalizeString ( password );
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
if ( GetId ( username ))
2008-10-02 16:23:55 -05:00
{
return AOR_NAME_ALREDY_EXIST ; // username does already exist
}
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
if ( ! loginDatabase . PExecute ( "INSERT INTO account(username,sha_pass_hash,joindate) VALUES('%s','%s',NOW())" , username . c_str (), CalculateShaPassHash ( username , password ). c_str ()))
2008-10-02 16:23:55 -05:00
return AOR_DB_INTERNAL_ERROR ; // unexpected error
2009-07-31 11:29:28 +08:00
loginDatabase . Execute ( "INSERT INTO realmcharacters (realmid, acctid, numchars) SELECT realmlist.id, account.id, 0 FROM realmlist,account LEFT JOIN realmcharacters ON acctid=account.id WHERE acctid IS NULL" );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return AOR_OK ; // everything's fine
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
AccountOpResult AccountMgr :: DeleteAccount ( uint32 accid )
{
2009-07-31 11:29:28 +08:00
QueryResult * result = loginDatabase . PQuery ( "SELECT 1 FROM account WHERE id='%d'" , accid );
2008-10-02 16:23:55 -05:00
if ( ! result )
return AOR_NAME_NOT_EXIST ; // account doesn't exist
delete result ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
result = CharacterDatabase . PQuery ( "SELECT guid FROM characters WHERE account='%d'" , accid );
if ( result )
{
do
{
Field * fields = result -> Fetch ();
uint32 guidlo = fields [ 0 ]. GetUInt32 ();
uint64 guid = MAKE_NEW_GUID ( guidlo , 0 , HIGHGUID_PLAYER );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// kick if player currently
2009-07-01 18:36:03 -05:00
if ( Player * p = ObjectAccessor :: GetObjectInWorld ( guid , ( Player * ) NULL ))
2008-10-02 16:23:55 -05:00
{
WorldSession * s = p -> GetSession ();
s -> KickPlayer (); // mark session to remove at next session list update
s -> LogoutPlayer ( false ); // logout player without waiting next session list update
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Player :: DeleteFromDB ( guid , accid , false ); // no need to update realm characters
} while ( result -> NextRow ());
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
delete result ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// table realm specific but common for all characters of account for realm
CharacterDatabase . PExecute ( "DELETE FROM character_tutorial WHERE account = '%u'" , accid );
2009-12-06 23:43:36 +01:00
CharacterDatabase . PExecute ( "DELETE FROM account_data WHERE account = '%u'" , accid );
2009-10-17 15:51:44 -07:00
2009-07-31 11:29:28 +08:00
loginDatabase . BeginTransaction ();
2009-10-17 15:51:44 -07:00
2009-02-04 10:33:18 -06:00
bool res =
2009-07-31 11:29:28 +08:00
loginDatabase . PExecute ( "DELETE FROM account WHERE id='%d'" , accid ) &&
2009-12-21 21:08:29 -06:00
loginDatabase . PExecute ( "DELETE FROM account_access WHERE id ='%d'" , accid ) &&
2009-07-31 11:29:28 +08:00
loginDatabase . PExecute ( "DELETE FROM realmcharacters WHERE acctid='%d'" , accid );
2009-10-17 15:51:44 -07:00
2009-07-31 11:29:28 +08:00
loginDatabase . CommitTransaction ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if ( ! res )
return AOR_DB_INTERNAL_ERROR ; // unexpected error;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return AOR_OK ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
AccountOpResult AccountMgr :: ChangeUsername ( uint32 accid , std :: string new_uname , std :: string new_passwd )
{
2009-07-31 11:29:28 +08:00
QueryResult * result = loginDatabase . PQuery ( "SELECT 1 FROM account WHERE id='%d'" , accid );
2008-10-02 16:23:55 -05:00
if ( ! result )
return AOR_NAME_NOT_EXIST ; // account doesn't exist
delete result ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if ( utf8length ( new_uname ) > MAX_ACCOUNT_STR )
return AOR_NAME_TOO_LONG ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if ( utf8length ( new_passwd ) > MAX_ACCOUNT_STR )
return AOR_PASS_TOO_LONG ;
2009-10-17 15:51:44 -07:00
2009-05-21 19:06:09 +02:00
normalizeString ( new_uname );
normalizeString ( new_passwd );
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
std :: string safe_new_uname = new_uname ;
loginDatabase . escape_string ( safe_new_uname );
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
if ( ! loginDatabase . PExecute ( "UPDATE account SET v='0',s='0',username='%s',sha_pass_hash='%s' WHERE id='%d'" , safe_new_uname . c_str (),
CalculateShaPassHash ( new_uname , new_passwd ). c_str (), accid ))
2008-10-02 16:23:55 -05:00
return AOR_DB_INTERNAL_ERROR ; // unexpected error
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return AOR_OK ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
AccountOpResult AccountMgr :: ChangePassword ( uint32 accid , std :: string new_passwd )
{
2009-09-02 18:14:10 -05:00
std :: string username ;
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
if ( ! GetName ( accid , username ))
2008-10-02 16:23:55 -05:00
return AOR_NAME_NOT_EXIST ; // account doesn't exist
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if ( utf8length ( new_passwd ) > MAX_ACCOUNT_STR )
return AOR_PASS_TOO_LONG ;
2009-10-17 15:51:44 -07:00
2009-05-21 19:06:09 +02:00
normalizeString ( new_passwd );
2009-10-17 15:51:44 -07:00
2009-09-02 16:22:32 -05:00
// also reset s and v to force update at next realmd login
2009-09-02 18:14:10 -05:00
if ( ! loginDatabase . PExecute ( "UPDATE account SET v='0', s='0', sha_pass_hash='%s' WHERE id='%d'" ,
CalculateShaPassHash ( username , new_passwd ). c_str (), accid ))
2008-10-02 16:23:55 -05:00
return AOR_DB_INTERNAL_ERROR ; // unexpected error
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return AOR_OK ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint32 AccountMgr :: GetId ( std :: string username )
{
2009-07-31 11:29:28 +08:00
loginDatabase . escape_string ( username );
QueryResult * result = loginDatabase . PQuery ( "SELECT id FROM account WHERE username = '%s'" , username . c_str ());
2008-10-02 16:23:55 -05:00
if ( ! result )
return 0 ;
else
{
uint32 id = ( * result )[ 0 ]. GetUInt32 ();
delete result ;
return id ;
}
}
2009-10-17 15:51:44 -07:00
2008-10-20 12:59:45 -05:00
uint32 AccountMgr :: GetSecurity ( uint32 acc_id )
{
2009-12-21 21:08:29 -06:00
QueryResult * result = loginDatabase . PQuery ( "SELECT gmlevel FROM account_access WHERE id = '%u'" , acc_id );
2008-10-20 12:59:45 -05:00
if ( result )
{
uint32 sec = ( * result )[ 0 ]. GetUInt32 ();
delete result ;
return sec ;
}
2009-10-17 15:51:44 -07:00
2008-10-20 12:59:45 -05:00
return 0 ;
}
2009-10-17 15:51:44 -07:00
2009-12-22 15:35:44 -03:00
uint32 AccountMgr :: GetSecurity ( uint32 acc_id , int32 realm_id )
{
QueryResult * result = ( realm_id == - 1 )
? loginDatabase . PQuery ( "SELECT gmlevel FROM account_access WHERE id = '%u' AND RealmID = '%d'" , acc_id , realm_id )
: loginDatabase . PQuery ( "SELECT gmlevel FROM account_access WHERE id = '%u' AND (RealmID = '%d' OR RealmID = '-1')" , acc_id , realm_id );
if ( result )
{
uint32 sec = ( * result )[ 0 ]. GetUInt32 ();
delete result ;
return sec ;
}
return 0 ;
}
2008-10-20 12:59:45 -05:00
bool AccountMgr :: GetName ( uint32 acc_id , std :: string & name )
{
2009-07-31 11:29:28 +08:00
QueryResult * result = loginDatabase . PQuery ( "SELECT username FROM account WHERE id = '%u'" , acc_id );
2008-10-20 12:59:45 -05:00
if ( result )
{
name = ( * result )[ 0 ]. GetCppString ();
delete result ;
return true ;
}
2009-10-17 15:51:44 -07:00
2008-10-20 12:59:45 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool AccountMgr :: CheckPassword ( uint32 accid , std :: string passwd )
{
2009-09-02 18:14:10 -05:00
std :: string username ;
if ( ! GetName ( accid , username ))
return false ;
2009-10-17 15:51:44 -07:00
2009-05-21 19:06:09 +02:00
normalizeString ( passwd );
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
QueryResult * result = loginDatabase . PQuery ( "SELECT 1 FROM account WHERE id='%d' AND sha_pass_hash='%s'" , accid , CalculateShaPassHash ( username , passwd ). c_str ());
2008-10-02 16:23:55 -05:00
if ( result )
{
delete result ;
return true ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2009-05-21 19:06:09 +02:00
bool AccountMgr :: normalizeString ( std :: string & utf8str )
2008-10-02 16:23:55 -05:00
{
wchar_t wstr_buf [ MAX_ACCOUNT_STR + 1 ];
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
size_t wstr_len = MAX_ACCOUNT_STR ;
if ( ! Utf8toWStr ( utf8str , wstr_buf , wstr_len ))
return false ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
std :: transform ( & wstr_buf [ 0 ], wstr_buf + wstr_len , & wstr_buf [ 0 ], wcharToUpperOnlyLatin );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return WStrToUtf8 ( wstr_buf , wstr_len , utf8str );
}
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
std :: string AccountMgr :: CalculateShaPassHash ( std :: string & name , std :: string & password )
{
Sha1Hash sha ;
sha . Initialize ();
sha . UpdateData ( name );
sha . UpdateData ( ":" );
sha . UpdateData ( password );
sha . Finalize ();
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
std :: string encoded ;
hexEncodeByteArray ( sha . GetDigest (), sha . GetLength (), encoded );
2009-10-17 15:51:44 -07:00
2009-09-02 18:14:10 -05:00
return encoded ;
}