2010-10-07 15:35:36 +02:00
/*
2011-01-01 15:01:13 +01:00
* Copyright (C) 2008-2011 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.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, see <http://www.gnu.org/licenses/>.
2010-06-06 23:08:23 +02:00
*/
/** \file
\ingroup Trinityd
*/
#include "Common.h"
2010-07-29 01:22:45 +02:00
#include "Configuration/Config.h"
2010-06-06 23:08:23 +02:00
#include "Database/DatabaseEnv.h"
#include "AccountMgr.h"
#include "Log.h"
#include "RASocket.h"
#include "Util.h"
#include "World.h"
2010-12-10 09:02:59 +01:00
#include "SHA1.h"
2010-06-06 23:08:23 +02:00
2010-12-10 01:01:44 +02:00
RASocket :: RASocket ()
2010-06-06 23:08:23 +02:00
{
2010-12-23 23:25:44 +01:00
iMinLevel = sConfig -> GetIntDefault ( "RA.MinLevel" , 3 );
2010-06-06 23:08:23 +02:00
}
RASocket ::~ RASocket ()
{
}
2010-12-10 01:01:44 +02:00
int RASocket :: open ( void * )
2010-06-06 23:08:23 +02:00
{
2010-12-10 09:02:59 +01:00
ACE_INET_Addr remote_addr ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( peer (). get_remote_addr ( remote_addr ) == - 1 )
{
2010-12-23 23:25:44 +01:00
sLog -> outError ( "RASocket::open: peer().get_remote_addr error is %s" , ACE_OS :: strerror ( errno ));
2010-12-10 09:02:59 +01:00
return - 1 ;
}
2010-12-10 01:01:44 +02:00
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Incoming connection from %s" , remote_addr . get_host_addr ());
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
return activate ();
2010-12-10 01:01:44 +02:00
}
int RASocket :: handle_close ( ACE_HANDLE , ACE_Reactor_Mask )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Closing connection" );
2010-12-10 09:02:59 +01:00
peer (). close_reader ();
wait ();
destroy ();
return 0 ;
2010-06-06 23:08:23 +02:00
}
2010-12-10 01:01:44 +02:00
int RASocket :: send ( const std :: string & line )
2010-06-06 23:08:23 +02:00
{
2010-12-15 10:44:06 +01:00
return size_t ( peer (). send ( line . c_str (), line . length ())) == line . length () ? 0 : - 1 ;
2010-12-10 01:01:44 +02:00
}
int RASocket :: recv_line ( ACE_Message_Block & buffer )
{
2010-12-10 09:02:59 +01:00
char byte ;
for (;;)
{
ssize_t n = peer (). recv ( & byte , sizeof ( byte ));
if ( n < 0 )
{
return - 1 ;
}
if ( n == 0 )
{
// EOF, connection was closed
errno = ECONNRESET ;
return - 1 ;
}
ACE_ASSERT ( n == sizeof ( byte ));
if ( byte == '\n' )
break ;
else if ( byte == '\r' ) /* Ignore CR */
continue ;
else if ( buffer . copy ( & byte , sizeof ( byte )) == - 1 )
return - 1 ;
}
const char null_term = '\0' ;
if ( buffer . copy ( & null_term , sizeof ( null_term )) == - 1 )
return - 1 ;
return 0 ;
2010-06-06 23:08:23 +02:00
}
2010-12-10 01:01:44 +02:00
int RASocket :: recv_line ( std :: string & out_line )
2010-06-06 23:08:23 +02:00
{
2010-12-10 09:02:59 +01:00
char buf [ 4096 ];
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
ACE_Data_Block db ( sizeof ( buf ),
ACE_Message_Block :: MB_DATA ,
buf ,
0 ,
0 ,
ACE_Message_Block :: DONT_DELETE ,
0 );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
ACE_Message_Block message_block ( & db ,
ACE_Message_Block :: DONT_DELETE ,
0 );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( recv_line ( message_block ) == - 1 )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Recv error %s" , ACE_OS :: strerror ( errno ));
2010-12-10 09:02:59 +01:00
return - 1 ;
}
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
out_line = message_block . rd_ptr ();
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
return 0 ;
2010-12-10 01:01:44 +02:00
}
int RASocket :: process_command ( const std :: string & command )
{
2010-12-10 09:02:59 +01:00
if ( command . length () == 0 )
return 0 ;
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Got command: %s" , command . c_str ());
2010-12-10 09:02:59 +01:00
// handle quit, exit and logout commands to terminate connection
if ( command == "quit" || command == "exit" || command == "logout" ) {
( void ) send ( "Bye \r\n " );
return - 1 ;
}
CliCommandHolder * cmd = new CliCommandHolder ( this , command . c_str (), & RASocket :: zprint , & RASocket :: commandFinished );
2010-12-23 23:25:44 +01:00
sWorld -> QueueCliCommand ( cmd );
2010-12-10 09:02:59 +01:00
// wait for result
ACE_Message_Block * mb ;
for (;;)
{
if ( getq ( mb ) == - 1 )
return - 1 ;
if ( mb -> msg_type () == ACE_Message_Block :: MB_BREAK )
{
mb -> release ();
break ;
}
2010-12-15 10:44:06 +01:00
if ( size_t ( peer (). send ( mb -> rd_ptr (), mb -> length ())) != mb -> length ())
2010-12-10 09:02:59 +01:00
{
mb -> release ();
return - 1 ;
}
mb -> release ();
}
return 0 ;
2010-12-10 01:01:44 +02:00
}
int RASocket :: check_access_level ( const std :: string & user )
{
2010-12-10 09:02:59 +01:00
std :: string safe_user = user ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
AccountMgr :: normalizeString ( safe_user );
2011-06-30 14:52:44 +02:00
LoginDatabase . EscapeString ( safe_user );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
QueryResult result = LoginDatabase . PQuery ( "SELECT a.id, aa.gmlevel, aa.RealmID FROM account a LEFT JOIN account_access aa ON (a.id = aa.id) WHERE a.username = '%s'" , safe_user . c_str ());
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( ! result )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "User %s does not exist in database" , user . c_str ());
2010-12-10 09:02:59 +01:00
return - 1 ;
}
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
Field * fields = result -> Fetch ();
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( fields [ 1 ]. GetUInt32 () < iMinLevel )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "User %s has no privilege to login" , user . c_str ());
2010-12-10 09:02:59 +01:00
return - 1 ;
}
else if ( fields [ 2 ]. GetInt32 () != - 1 )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "User %s has to be assigned on all realms (with RealmID = '-1')" , user . c_str ());
2010-12-10 09:02:59 +01:00
return - 1 ;
}
2010-06-06 23:08:23 +02:00
2010-12-10 09:02:59 +01:00
return 0 ;
2010-12-10 01:01:44 +02:00
}
int RASocket :: check_password ( const std :: string & user , const std :: string & pass )
{
2010-12-10 09:02:59 +01:00
std :: string safe_user = user ;
AccountMgr :: normalizeString ( safe_user );
2011-06-30 14:52:44 +02:00
LoginDatabase . EscapeString ( safe_user );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
std :: string safe_pass = pass ;
AccountMgr :: normalizeString ( safe_pass );
2011-06-30 14:52:44 +02:00
LoginDatabase . EscapeString ( safe_pass );
2010-12-10 01:01:44 +02:00
2011-09-08 10:49:58 +02:00
std :: string hash = AccountMgr :: CalculateShaPassHash ( safe_user , safe_pass );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
QueryResult check = LoginDatabase . PQuery (
"SELECT 1 FROM account WHERE username = '%s' AND sha_pass_hash = '%s'" ,
safe_user . c_str (), hash . c_str ());
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( ! check )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Wrong password for user: %s" , user . c_str ());
2010-12-10 09:02:59 +01:00
return - 1 ;
}
return 0 ;
2010-12-10 01:01:44 +02:00
}
int RASocket :: authenticate ()
{
2010-12-10 09:02:59 +01:00
if ( send ( std :: string ( "Username: " )) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
std :: string user ;
if ( recv_line ( user ) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( send ( std :: string ( "Password: " )) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
std :: string pass ;
if ( recv_line ( pass ) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Login attempt for user: %s" , user . c_str ());
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( check_access_level ( user ) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( check_password ( user , pass ) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "User login: %s" , user . c_str ());
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
return 0 ;
2010-12-10 01:01:44 +02:00
}
2011-08-01 04:01:10 +02:00
int RASocket :: subnegotiate ()
{
char buf [ 1024 ];
ACE_Data_Block db ( sizeof ( buf ),
ACE_Message_Block :: MB_DATA ,
buf ,
0 ,
0 ,
ACE_Message_Block :: DONT_DELETE ,
0 );
ACE_Message_Block message_block ( & db ,
ACE_Message_Block :: DONT_DELETE ,
0 );
const size_t recv_size = message_block . space ();
2011-08-02 01:55:18 +02:00
// Wait a maximum of 1000ms for negotiation packet - not all telnet clients may send it
2011-08-03 12:49:29 +02:00
ACE_Time_Value waitTime = ACE_Time_Value ( 1 );
2011-08-01 04:01:10 +02:00
const ssize_t n = peer (). recv ( message_block . wr_ptr (),
2011-08-03 12:49:29 +02:00
recv_size , & waitTime );
2011-08-01 04:01:10 +02:00
if ( n <= 0 )
return int ( n );
if ( n >= 1024 )
{
sLog -> outRemote ( "RASocket::subnegotiate: allocated buffer 1024 bytes was too small for negotiation packet, size: %u" , n );
return - 1 ;
}
buf [ n ] = '\0' ;
#ifdef _DEBUG
for ( uint8 i = 0 ; i < n ; )
{
uint8 iac = buf [ i ];
if ( iac == 0xFF ) // "Interpret as Command" (IAC)
{
uint8 command = buf [ ++ i ];
std :: stringstream ss ;
switch ( command )
{
case 0xFB : // WILL
ss << "WILL " ;
break ;
case 0xFC : // WON'T
ss << "WON'T " ;
break ;
case 0xFD : // DO
ss << "DO " ;
break ;
case 0xFE : // DON'T
ss << "DON'T " ;
break ;
default :
return - 1 ; // not allowed
}
uint8 param = buf [ ++ i ];
ss << uint32 ( param );
sLog -> outRemote ( ss . str (). c_str ());
}
++ i ;
}
#endif
//! Just send back end of subnegotiation packet
uint8 const reply [ 2 ] = { 0xFF , 0xF0 };
return peer (). send ( reply , 2 );
}
2010-12-10 01:01:44 +02:00
int RASocket :: svc ( void )
{
2011-08-02 01:55:18 +02:00
//! Subnegotiation may differ per client - do not react on it
subnegotiate ();
2011-08-01 04:01:10 +02:00
2010-12-10 09:02:59 +01:00
if ( send ( "Authentication required \r\n " ) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( authenticate () == - 1 )
{
( void ) send ( "Authentication failed \r\n " );
return - 1 ;
}
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
// send motd
2010-12-23 23:25:44 +01:00
if ( send ( std :: string ( sWorld -> GetMotd ()) + " \r\n " ) == - 1 )
2010-12-10 09:02:59 +01:00
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
for (;;)
{
// show prompt
const char * tc_prompt = "TC> " ;
2010-12-15 10:44:06 +01:00
if ( size_t ( peer (). send ( tc_prompt , strlen ( tc_prompt ))) != strlen ( tc_prompt ))
2010-12-10 09:02:59 +01:00
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
std :: string line ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( recv_line ( line ) == - 1 )
return - 1 ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( process_command ( line ) == - 1 )
return - 1 ;
}
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
return 0 ;
2010-12-10 01:01:44 +02:00
}
void RASocket :: zprint ( void * callbackArg , const char * szText )
{
2010-12-10 09:02:59 +01:00
if ( ! szText || ! callbackArg )
return ;
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
RASocket * socket = static_cast < RASocket *> ( callbackArg );
size_t sz = strlen ( szText );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
ACE_Message_Block * mb = new ACE_Message_Block ( sz );
mb -> copy ( szText , sz );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
if ( socket -> putq ( mb , const_cast < ACE_Time_Value *> ( & ACE_Time_Value :: zero )) == - 1 )
{
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Failed to enqueue message, queue is full or closed. Error is %s" , ACE_OS :: strerror ( errno ));
2010-12-10 09:02:59 +01:00
mb -> release ();
}
2010-06-06 23:08:23 +02:00
}
2010-07-23 09:26:23 +02:00
2010-08-21 23:08:54 +02:00
void RASocket :: commandFinished ( void * callbackArg , bool /*success*/ )
2010-07-23 09:26:23 +02:00
{
2010-12-10 09:02:59 +01:00
if ( ! callbackArg )
return ;
2010-08-08 19:45:53 +02:00
2010-12-10 09:02:59 +01:00
RASocket * socket = static_cast < RASocket *> ( callbackArg );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
ACE_Message_Block * mb = new ACE_Message_Block ();
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
mb -> msg_type ( ACE_Message_Block :: MB_BREAK );
2010-12-10 01:01:44 +02:00
2010-12-10 09:02:59 +01:00
// the message is 0 size control message to tell that command output is finished
// hence we don't put timeout, because it shouldn't increase queue size and shouldn't block
if ( socket -> putq ( mb ) == - 1 )
{
// getting here is bad, command can't be marked as complete
2010-12-23 23:25:44 +01:00
sLog -> outRemote ( "Failed to enqueue command end message. Error is %s" , ACE_OS :: strerror ( errno ));
2010-12-10 09:02:59 +01:00
mb -> release ();
}
2010-12-10 01:01:44 +02:00
}