root/trunk/src/trinityrealm/AuthSocket.cpp @ 44

Revision 44, 31.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/** \file
22    \ingroup realmd
23*/
24
25#include "Common.h"
26#include "Database/DatabaseEnv.h"
27#include "ByteBuffer.h"
28#include "Config/ConfigEnv.h"
29#include "Log.h"
30#include "RealmList.h"
31#include "AuthSocket.h"
32#include "AuthCodes.h"
33#include <openssl/md5.h>
34#include "Auth/Sha1.h"
35//#include "Util.h" -- for commented utf8ToUpperOnlyLatin
36
37extern RealmList m_realmList;
38
39extern DatabaseType dbRealmServer;
40
41#define ChunkSize 2048
42
43enum eAuthCmd
44{
45    //AUTH_NO_CMD                 = 0xFF,
46    AUTH_LOGON_CHALLENGE        = 0x00,
47    AUTH_LOGON_PROOF            = 0x01,
48    //AUTH_RECONNECT_CHALLENGE    = 0x02,
49    //AUTH_RECONNECT_PROOF        = 0x03,
50    //update srv =4
51    REALM_LIST                  = 0x10,
52    XFER_INITIATE               = 0x30,
53    XFER_DATA                   = 0x31,
54    XFER_ACCEPT                 = 0x32,
55    XFER_RESUME                 = 0x33,
56    XFER_CANCEL                 = 0x34
57};
58
59enum eStatus
60{
61    STATUS_CONNECTED = 0,
62    STATUS_AUTHED
63};
64
65// GCC have alternative #pragma pack(N) syntax and old gcc version not support pack(push,N), also any gcc version not support it at some paltform
66#if defined( __GNUC__ )
67#pragma pack(1)
68#else
69#pragma pack(push,1)
70#endif
71
72typedef struct AUTH_LOGON_CHALLENGE_C
73{
74    uint8   cmd;
75    uint8   error;
76    uint16  size;
77    uint8   gamename[4];
78    uint8   version1;
79    uint8   version2;
80    uint8   version3;
81    uint16  build;
82    uint8   platform[4];
83    uint8   os[4];
84    uint8   country[4];
85    uint32  timezone_bias;
86    uint32  ip;
87    uint8   I_len;
88    uint8   I[1];
89} sAuthLogonChallenge_C;
90
91//typedef sAuthLogonChallenge_C sAuthReconnectChallenge_C;
92/*
93typedef struct
94{
95    uint8   cmd;
96    uint8   error;
97    uint8   unk2;
98    uint8   B[32];
99    uint8   g_len;
100    uint8   g[1];
101    uint8   N_len;
102    uint8   N[32];
103    uint8   s[32];
104    uint8   unk3[16];
105} sAuthLogonChallenge_S;
106*/
107
108typedef struct AUTH_LOGON_PROOF_C
109{
110    uint8   cmd;
111    uint8   A[32];
112    uint8   M1[20];
113    uint8   crc_hash[20];
114    uint8   number_of_keys;
115    uint8   unk;                                            // Added in 1.12.x client branch
116} sAuthLogonProof_C;
117/*
118typedef struct
119{
120    uint16  unk1;
121    uint32  unk2;
122    uint8   unk3[4];
123    uint16  unk4[20];
124}  sAuthLogonProofKey_C;
125*/
126typedef struct AUTH_LOGON_PROOF_S
127{
128    uint8   cmd;
129    uint8   error;
130    uint8   M2[20];
131    uint32  unk1;
132    uint32  unk2;
133    uint16  unk3;
134} sAuthLogonProof_S;
135
136typedef struct XFER_INIT
137{
138    uint8 cmd;                                              // XFER_INITIATE
139    uint8 fileNameLen;                                      // strlen(fileName);
140    uint8 fileName[1];                                      // fileName[fileNameLen]
141    uint64 file_size;                                       // file size (bytes)
142    uint8 md5[MD5_DIGEST_LENGTH];                           // MD5
143}XFER_INIT;
144
145typedef struct XFER_DATA
146{
147    uint8 opcode;
148    uint16 data_size;
149    uint8 data[ChunkSize];
150}XFER_DATA_STRUCT;
151
152typedef struct AuthHandler
153{
154    eAuthCmd cmd;
155    uint32 status;
156    bool (AuthSocket::*handler)(void);
157}AuthHandler;
158
159// GCC have alternative #pragma pack() syntax and old gcc version not support pack(pop), also any gcc version not support it at some paltform
160#if defined( __GNUC__ )
161#pragma pack()
162#else
163#pragma pack(pop)
164#endif
165
166/// Launch a thread to transfer a patch to the client
167class PatcherRunnable: public ZThread::Runnable
168{
169    public:
170        PatcherRunnable(class AuthSocket *);
171        void run();
172
173    private:
174        AuthSocket * mySocket;
175};
176
177typedef struct PATCH_INFO
178{
179    uint8 md5[MD5_DIGEST_LENGTH];
180}PATCH_INFO;
181
182/// Caches MD5 hash of client patches present on the server
183class Patcher
184{
185    public:
186        typedef std::map<std::string, PATCH_INFO*> Patches;
187        ~Patcher();
188        Patcher();
189        Patches::const_iterator begin() const { return _patches.begin(); }
190        Patches::const_iterator end() const { return _patches.end(); }
191        void LoadPatchMD5(char*);
192        bool GetHash(char * pat,uint8 mymd5[16]);
193
194    private:
195        void LoadPatchesInfo();
196        Patches _patches;
197};
198
199const AuthHandler table[] =
200{
201    { AUTH_LOGON_CHALLENGE,     STATUS_CONNECTED, &AuthSocket::_HandleLogonChallenge},
202    { AUTH_LOGON_PROOF,         STATUS_CONNECTED, &AuthSocket::_HandleLogonProof    },
203    { REALM_LIST,               STATUS_AUTHED,    &AuthSocket::_HandleRealmList     },
204    { XFER_ACCEPT,              STATUS_CONNECTED, &AuthSocket::_HandleXferAccept    },
205    { XFER_RESUME,              STATUS_CONNECTED, &AuthSocket::_HandleXferResume    },
206    { XFER_CANCEL,              STATUS_CONNECTED, &AuthSocket::_HandleXferCancel    }
207};
208
209#define AUTH_TOTAL_COMMANDS sizeof(table)/sizeof(AuthHandler)
210
211///Holds the MD5 hash of client patches present on the server
212Patcher PatchesCache;
213
214/// Constructor - set the N and g values for SRP6
215AuthSocket::AuthSocket(ISocketHandler &h) : TcpSocket(h)
216{
217    N.SetHexStr("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7");
218    g.SetDword(7);
219    _authed = false;
220    pPatch=NULL;
221
222    _accountSecurityLevel = SEC_PLAYER;
223}
224
225/// Close patch file descriptor before leaving
226AuthSocket::~AuthSocket()
227{
228    if(pPatch)
229        fclose(pPatch);
230}
231
232/// Accept the connection and set the s random value for SRP6
233void AuthSocket::OnAccept()
234{
235    sLog.outBasic("Accepting connection from '%s:%d'",
236        GetRemoteAddress().c_str(), GetRemotePort());
237
238    s.SetRand(s_BYTE_SIZE * 8);
239}
240
241/// Read the packet from the client
242void AuthSocket::OnRead()
243{
244    ///- Read the packet
245    TcpSocket::OnRead();
246    uint8 _cmd;
247    while (1)
248    {
249        if (!ibuf.GetLength())
250            return;
251
252        ///- Get the command out of it
253        ibuf.SoftRead((char *)&_cmd, 1);                    // UQ1: No longer exists in new net code ???
254        //ibuf.Read((char *)&_cmd, 1);
255        /*char *command = (char *)malloc(1);
256
257        ibuf.Read(command, 1);
258
259        _cmd = (uint8)command;*/
260        //      assert(0);
261        size_t i;
262
263        ///- Circle through known commands and call the correct command handler
264        for (i=0;i<AUTH_TOTAL_COMMANDS; i++)
265        {
266            if ((uint8)table[i].cmd == _cmd &&
267                (table[i].status == STATUS_CONNECTED ||
268                (_authed && table[i].status == STATUS_AUTHED)))
269            {
270                DEBUG_LOG("[Auth] got data for cmd %u ibuf length %u", (uint32)_cmd, ibuf.GetLength());
271
272                if (!(*this.*table[i].handler)())
273                {
274                    DEBUG_LOG("Command handler failed for cmd %u ibuf length %u", (uint32)_cmd, ibuf.GetLength());
275                    return;
276                }
277                break;
278            }
279        }
280
281        ///- Report unknown commands in the debug log
282        if (i==AUTH_TOTAL_COMMANDS)
283        {
284            DEBUG_LOG("[Auth] got unknown packet %u", (uint32)_cmd);
285            return;
286        }
287    }
288}
289
290/// Make the SRP6 calculation from hash in dB
291void AuthSocket::_SetVSFields(std::string rI)
292{
293    BigNumber I;
294    I.SetHexStr(rI.c_str());
295
296    //In case of leading zeroes in the rI hash, restore them
297    uint8 mDigest[SHA_DIGEST_LENGTH];
298    memset(mDigest,0,SHA_DIGEST_LENGTH);
299    if (I.GetNumBytes() <= SHA_DIGEST_LENGTH)
300        memcpy(mDigest,I.AsByteArray(),I.GetNumBytes());
301
302    std::reverse(mDigest,mDigest+SHA_DIGEST_LENGTH);
303
304    Sha1Hash sha;
305    sha.UpdateData(s.AsByteArray(), s.GetNumBytes());
306    sha.UpdateData(mDigest, SHA_DIGEST_LENGTH);
307    sha.Finalize();
308    BigNumber x;
309    x.SetBinary(sha.GetDigest(), sha.GetLength());
310    v = g.ModExp(x, N);
311    // No SQL injection (username escaped)
312    const char *v_hex, *s_hex;
313    v_hex = v.AsHexStr();
314    s_hex = s.AsHexStr();
315    dbRealmServer.PExecute("UPDATE account SET v = '%s', s = '%s' WHERE username = '%s'",v_hex,s_hex, _safelogin.c_str() );
316    OPENSSL_free((void*)v_hex);
317    OPENSSL_free((void*)s_hex);
318}
319
320/// Logon Challenge command handler
321bool AuthSocket::_HandleLogonChallenge()
322{
323    DEBUG_LOG("Entering _HandleLogonChallenge");
324    if (ibuf.GetLength() < sizeof(sAuthLogonChallenge_C))
325        return false;
326
327    ///- Read the first 4 bytes (header) to get the length of the remaining of the packet
328    std::vector<uint8> buf;
329    buf.resize(4);
330
331    ibuf.Read((char *)&buf[0], 4);
332
333    EndianConvert(*((uint16*)(buf[0])));
334    uint16 remaining = ((sAuthLogonChallenge_C *)&buf[0])->size;
335    DEBUG_LOG("[AuthChallenge] got header, body is %#04x bytes", remaining);
336
337    if ((remaining < sizeof(sAuthLogonChallenge_C) - buf.size()) || (ibuf.GetLength() < remaining))
338        return false;
339
340    //No big fear of memory outage (size is int16, i.e. < 65536)
341    buf.resize(remaining + buf.size() + 1);
342    buf[buf.size() - 1] = 0;
343    sAuthLogonChallenge_C *ch = (sAuthLogonChallenge_C*)&buf[0];
344
345    // BigEndian code, nop in little endian case
346    // size already converted
347    EndianConvert(*((uint32*)(&ch->gamename[0])));
348    EndianConvert(ch->build);
349    EndianConvert(*((uint32*)(&ch->platform[0])));
350    EndianConvert(*((uint32*)(&ch->os[0])));
351    EndianConvert(*((uint32*)(&ch->country[0])));
352    EndianConvert(ch->timezone_bias);
353    EndianConvert(ch->ip);
354
355    ///- Read the remaining of the packet
356    ibuf.Read((char *)&buf[4], remaining);
357    DEBUG_LOG("[AuthChallenge] got full packet, %#04x bytes", ch->size);
358    DEBUG_LOG("[AuthChallenge] name(%d): '%s'", ch->I_len, ch->I);
359
360    ByteBuffer pkt;
361
362    _login = (const char*)ch->I;
363
364    ///- Normalize account name
365    //utf8ToUpperOnlyLatin(_login); -- client already send account in expected form
366
367    //Escape the user login to avoid further SQL injection
368    //Memory will be freed on AuthSocket object destruction
369    _safelogin=_login;
370    dbRealmServer.escape_string(_safelogin);
371
372    ///- Check if the client has one of the expected version numbers
373    bool valid_version=false;
374    int accepted_versions[]=EXPECTED_TRINITY_CLIENT_BUILD;
375    for(int i=0;accepted_versions[i];i++)
376        if(ch->build==accepted_versions[i])
377    {
378        valid_version=true;
379        break;
380    }
381
382    /// <ul><li> if this is a valid version
383    if(valid_version)
384    {
385        pkt << (uint8) AUTH_LOGON_CHALLENGE;
386        pkt << (uint8) 0x00;
387
388        ///- Verify that this IP is not in the ip_banned table
389        // No SQL injection possible (paste the IP address as passed by the socket)
390        dbRealmServer.Execute("DELETE FROM ip_banned WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
391
392        std::string address = GetRemoteAddress();
393        dbRealmServer.escape_string(address);
394        QueryResult *result = dbRealmServer.PQuery(  "SELECT * FROM ip_banned WHERE ip = '%s'",address.c_str());
395        if(result)
396        {
397            pkt << (uint8)REALM_AUTH_ACCOUNT_BANNED;
398            sLog.outBasic("[AuthChallenge] Banned ip %s tries to login!",GetRemoteAddress().c_str ());
399            delete result;
400        }
401        else
402        {
403            ///- Get the account details from the account table
404            // No SQL injection (escaped user name)
405
406            result = dbRealmServer.PQuery("SELECT sha_pass_hash,id,locked,last_ip,gmlevel FROM account WHERE username = '%s'",_safelogin.c_str ());
407            if( result )
408            {
409                ///- If the IP is 'locked', check that the player comes indeed from the correct IP address
410                bool locked = false;
411                if((*result)[2].GetUInt8() == 1)            // if ip is locked
412                {
413                    DEBUG_LOG("[AuthChallenge] Account '%s' is locked to IP - '%s'", _login.c_str(), (*result)[3].GetString());
414                    DEBUG_LOG("[AuthChallenge] Player address is '%s'", GetRemoteAddress().c_str());
415                    if ( strcmp((*result)[3].GetString(),GetRemoteAddress().c_str()) )
416                    {
417                        DEBUG_LOG("[AuthChallenge] Account IP differs");
418                        pkt << (uint8) REALM_AUTH_ACCOUNT_FREEZED;
419                        locked=true;
420                    }
421                    else
422                    {
423                        DEBUG_LOG("[AuthChallenge] Account IP matches");
424                    }
425                }
426                else
427                {
428                    DEBUG_LOG("[AuthChallenge] Account '%s' is not locked to ip", _login.c_str());
429                }
430
431                if (!locked)
432                {
433                    //set expired bans to inactive
434                    dbRealmServer.Execute("UPDATE account_banned SET active = 0 WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
435                    ///- If the account is banned, reject the logon attempt
436                    QueryResult *banresult = dbRealmServer.PQuery("SELECT bandate,unbandate FROM account_banned WHERE id = %u AND active = 1", (*result)[1].GetUInt32());
437                    if(banresult)
438                    {
439                        if((*banresult)[0].GetUInt64() == (*banresult)[1].GetUInt64())
440                        {
441                            pkt << (uint8) REALM_AUTH_ACCOUNT_BANNED;
442                            sLog.outBasic("[AuthChallenge] Banned account %s tries to login!",_login.c_str ());
443                        }
444                        else
445                        {
446                            pkt << (uint8) REALM_AUTH_ACCOUNT_FREEZED;
447                            sLog.outBasic("[AuthChallenge] Temporarily banned account %s tries to login!",_login.c_str ());
448                        }
449
450                        delete banresult;
451                    }
452                    else
453                    {
454                        ///- Get the password from the account table, upper it, and make the SRP6 calculation
455                        std::string rI = (*result)[0].GetCppString();
456                        _SetVSFields(rI);
457
458                        b.SetRand(19 * 8);
459                        BigNumber gmod=g.ModExp(b, N);
460                        B = ((v * 3) + gmod) % N;
461
462                        ASSERT(gmod.GetNumBytes() <= 32);
463
464                        BigNumber unk3;
465                        unk3.SetRand(16*8);
466
467                        ///- Fill the response packet with the result
468                        pkt << (uint8)REALM_AUTH_SUCCESS;
469
470                        // B may be calculated < 32B so we force minnimal length to 32B
471                        pkt.append(B.AsByteArray(32), 32);   // 32 bytes
472                        pkt << (uint8)1;
473                        pkt.append(g.AsByteArray(), 1);
474                        pkt << (uint8)32;
475                        pkt.append(N.AsByteArray(), 32);
476                        pkt.append(s.AsByteArray(), s.GetNumBytes());   // 32 bytes
477                        pkt.append(unk3.AsByteArray(), 16);
478                        pkt << (uint8)0;                    // Added in 1.12.x client branch
479
480                        uint8 secLevel = (*result)[4].GetUInt8();
481                        _accountSecurityLevel = secLevel <= SEC_ADMINISTRATOR ? AccountTypes(secLevel) : SEC_ADMINISTRATOR;
482
483                        std::string localeName;
484                        localeName.resize(4);
485                        for(int i = 0; i <4; ++i)
486                            localeName[i] = ch->country[4-i-1];
487
488                        _localization = GetLocaleByName(localeName);
489
490                        sLog.outBasic("[AuthChallenge] account %s is using '%c%c%c%c' locale (%u)", _login.c_str (), ch->country[3],ch->country[2],ch->country[1],ch->country[0], _localization);
491                    }
492                }
493                delete result;
494            }
495            else                                            //no account
496            {
497                pkt<< (uint8) REALM_AUTH_NO_MATCH;
498            }
499        }
500    }                                                       //valid version
501    else
502        ///<li> else
503    {
504        ///- Check if we have the apropriate patch on the disk
505        char tmp[64];
506        // No buffer overflow (fixed length of arguments)
507        sprintf(tmp,"./patches/%d%c%c%c%c.mpq",ch->build,ch->country[3],
508            ch->country[2],ch->country[1],ch->country[0]);
509        // This will be closed at the destruction of the AuthSocket (client deconnection)
510        FILE *pFile=fopen(tmp,"rb");
511        if(!pFile)
512        {
513            pkt << (uint8) AUTH_LOGON_CHALLENGE;
514            pkt << (uint8) 0x00;
515            pkt << (uint8) REALM_AUTH_WRONG_BUILD_NUMBER;
516            DEBUG_LOG("[AuthChallenge] %u is not a valid client version!", ch->build);
517            DEBUG_LOG("[AuthChallenge] Patch %s not found",tmp);
518        }else
519        {                                                   //have patch
520            pPatch=pFile;
521            XFER_INIT xferh;
522
523            ///- Get the MD5 hash of the patch file (get it from preloaded Patcher cache or calculate it)
524            if(PatchesCache.GetHash(tmp,(uint8*)&xferh.md5))
525            {
526                DEBUG_LOG("\n[AuthChallenge] Found precached patch info for patch %s",tmp);
527            }
528            else
529            {                                               //calculate patch md5
530                printf("\n[AuthChallenge] Patch info for %s was not cached.",tmp);
531                PatchesCache.LoadPatchMD5(tmp);
532                PatchesCache.GetHash(tmp,(uint8*)&xferh.md5);
533            }
534
535            ///- Send a packet to the client with the file length and MD5 hash
536            uint8 data[2]={AUTH_LOGON_PROOF,REALM_AUTH_UPDATE_CLIENT};
537            SendBuf((const char*)data,sizeof(data));
538
539            memcpy(&xferh,"0\x05Patch",7);
540            xferh.cmd=XFER_INITIATE;
541            fseek(pPatch,0,SEEK_END);
542            xferh.file_size=ftell(pPatch);
543
544            SendBuf((const char*)&xferh,sizeof(xferh));
545            return true;
546        }
547    }
548    /// </ul>
549    SendBuf((char const*)pkt.contents(), pkt.size());
550    return true;
551}
552
553/// Logon Proof command handler
554bool AuthSocket::_HandleLogonProof()
555{
556    DEBUG_LOG("Entering _HandleLogonProof");
557    ///- Read the packet
558    if (ibuf.GetLength() < sizeof(sAuthLogonProof_C))
559        return false;
560
561    sAuthLogonProof_C lp;
562    ibuf.Read((char *)&lp, sizeof(sAuthLogonProof_C));
563
564    ///- Continue the SRP6 calculation based on data received from the client
565    BigNumber A;
566    A.SetBinary(lp.A, 32);
567
568    Sha1Hash sha;
569    sha.UpdateBigNumbers(&A, &B, NULL);
570    sha.Finalize();
571    BigNumber u;
572    u.SetBinary(sha.GetDigest(), 20);
573    BigNumber S = (A * (v.ModExp(u, N))).ModExp(b, N);
574
575    uint8 t[32];
576    uint8 t1[16];
577    uint8 vK[40];
578    memcpy(t, S.AsByteArray(), 32);
579    for (int i = 0; i < 16; i++)
580    {
581        t1[i] = t[i*2];
582    }
583    sha.Initialize();
584    sha.UpdateData(t1, 16);
585    sha.Finalize();
586    for (int i = 0; i < 20; i++)
587    {
588        vK[i*2] = sha.GetDigest()[i];
589    }
590    for (int i = 0; i < 16; i++)
591    {
592        t1[i] = t[i*2+1];
593    }
594    sha.Initialize();
595    sha.UpdateData(t1, 16);
596    sha.Finalize();
597    for (int i = 0; i < 20; i++)
598    {
599        vK[i*2+1] = sha.GetDigest()[i];
600    }
601    K.SetBinary(vK, 40);
602
603    uint8 hash[20];
604
605    sha.Initialize();
606    sha.UpdateBigNumbers(&N, NULL);
607    sha.Finalize();
608    memcpy(hash, sha.GetDigest(), 20);
609    sha.Initialize();
610    sha.UpdateBigNumbers(&g, NULL);
611    sha.Finalize();
612    for (int i = 0; i < 20; i++)
613    {
614        hash[i] ^= sha.GetDigest()[i];
615    }
616    BigNumber t3;
617    t3.SetBinary(hash, 20);
618
619    sha.Initialize();
620    sha.UpdateData(_login);
621    sha.Finalize();
622    uint8 t4[SHA_DIGEST_LENGTH];
623    memcpy(t4, sha.GetDigest(), SHA_DIGEST_LENGTH);
624
625    sha.Initialize();
626    sha.UpdateBigNumbers(&t3, NULL);
627    sha.UpdateData(t4, SHA_DIGEST_LENGTH);
628    sha.UpdateBigNumbers(&s, &A, &B, &K, NULL);
629    sha.Finalize();
630    BigNumber M;
631    M.SetBinary(sha.GetDigest(), 20);
632
633    ///- Check if SRP6 results match (password is correct), else send an error
634    if (!memcmp(M.AsByteArray(), lp.M1, 20))
635    {
636        sLog.outBasic("User '%s' successfully authenticated", _login.c_str());
637
638        ///- Update the sessionkey, last_ip, last login time and reset number of failed logins in the account table for this account
639        // No SQL injection (escaped user name) and IP address as received by socket
640        const char* K_hex = K.AsHexStr();
641        dbRealmServer.PExecute("UPDATE account SET sessionkey = '%s', last_ip = '%s', last_login = NOW(), locale = '%u', failed_logins = 0 WHERE username = '%s'", K_hex, GetRemoteAddress().c_str(),  _localization, _safelogin.c_str() );
642        OPENSSL_free((void*)K_hex);
643
644        ///- Finish SRP6 and send the final result to the client
645        sha.Initialize();
646        sha.UpdateBigNumbers(&A, &M, &K, NULL);
647        sha.Finalize();
648
649        sAuthLogonProof_S proof;
650        memcpy(proof.M2, sha.GetDigest(), 20);
651        proof.cmd = AUTH_LOGON_PROOF;
652        proof.error = 0;
653        proof.unk1 = 0x00800000;
654        proof.unk2 = 0x00;
655        proof.unk3 = 0x00;
656
657        SendBuf((char *)&proof, sizeof(proof));
658
659        ///- Set _authed to true!
660        _authed = true;
661    }
662    else
663    {
664        char data[4]={AUTH_LOGON_PROOF,REALM_AUTH_NO_MATCH,3,0};
665        SendBuf(data,sizeof(data));
666        sLog.outBasic("[AuthChallenge] account %s tried to login with wrong password!",_login.c_str ());
667
668        uint32 MaxWrongPassCount = sConfig.GetIntDefault("WrongPass.MaxCount", 0);
669        if(MaxWrongPassCount > 0)
670        {
671            //Increment number of failed logins by one and if it reaches the limit temporarily ban that account or IP
672            dbRealmServer.PExecute("UPDATE account SET failed_logins = failed_logins + 1 WHERE username = '%s'",_safelogin.c_str());
673
674            if(QueryResult *loginfail = dbRealmServer.PQuery("SELECT id, failed_logins FROM account WHERE username = '%s'", _safelogin.c_str()))
675            {
676                Field* fields = loginfail->Fetch();
677                uint32 failed_logins = fields[1].GetUInt32();
678
679                if( failed_logins >= MaxWrongPassCount )
680                {
681                    uint32 WrongPassBanTime = sConfig.GetIntDefault("WrongPass.BanTime", 600);
682                    bool WrongPassBanType = sConfig.GetBoolDefault("WrongPass.BanType", false);
683
684                    if(WrongPassBanType)
685                    {
686                        uint32 acc_id = fields[0].GetUInt32();
687                        dbRealmServer.PExecute("INSERT INTO account_banned VALUES ('%u',UNIX_TIMESTAMP(),UNIX_TIMESTAMP()+'%u','Trinity realm','Failed login autoban',1)",
688                            acc_id, WrongPassBanTime);
689                        sLog.outBasic("[AuthChallenge] account %s got banned for '%u' seconds because it failed to authenticate '%u' times",
690                            _login.c_str(), WrongPassBanTime, failed_logins);
691                    }
692                    else
693                    {
694                        std::string current_ip = GetRemoteAddress();
695                        dbRealmServer.escape_string(current_ip);
696                        dbRealmServer.PExecute("INSERT INTO ip_banned VALUES ('%s',UNIX_TIMESTAMP(),UNIX_TIMESTAMP()+'%u','Trinity realm','Failed login autoban')",
697                            current_ip.c_str(), WrongPassBanTime);
698                        sLog.outBasic("[AuthChallenge] IP %s got banned for '%u' seconds because account %s failed to authenticate '%u' times",
699                            current_ip.c_str(), WrongPassBanTime, _login.c_str(), failed_logins);
700                    }
701                }
702                delete loginfail;
703            }
704        }
705    }
706    return true;
707}
708
709/// %Realm List command handler
710bool AuthSocket::_HandleRealmList()
711{
712    DEBUG_LOG("Entering _HandleRealmList");
713    if (ibuf.GetLength() < 5)
714        return false;
715
716    ibuf.Remove(5);
717
718    ///- Get the user id (else close the connection)
719    // No SQL injection (escaped user name)
720
721    QueryResult *result = dbRealmServer.PQuery("SELECT id,sha_pass_hash FROM account WHERE username = '%s'",_safelogin.c_str());
722    if(!result)
723    {
724        sLog.outError("[ERROR] user %s tried to login and we cannot find him in the database.",_login.c_str());
725        SetCloseAndDelete();
726        return false;
727    }
728
729    uint32 id = (*result)[0].GetUInt32();
730    std::string rI = (*result)[1].GetCppString();
731    delete result;
732
733    ///- Update realm list if need
734    m_realmList.UpdateIfNeed();
735
736    ///- Circle through realms in the RealmList and construct the return packet (including # of user characters in each realm)
737    ByteBuffer pkt;
738    pkt << (uint32) 0;
739    pkt << (uint16) m_realmList.size();
740    RealmList::RealmMap::const_iterator i;
741    for( i = m_realmList.begin(); i != m_realmList.end(); i++ )
742    {
743        uint8 AmountOfCharacters;
744
745        // No SQL injection. id of realm is controlled by the database.
746        result = dbRealmServer.PQuery( "SELECT numchars FROM realmcharacters WHERE realmid = '%d' AND acctid='%u'",i->second.m_ID,id);
747        if( result )
748        {
749            Field *fields = result->Fetch();
750            AmountOfCharacters = fields[0].GetUInt8();
751            delete result;
752        }
753        else
754            AmountOfCharacters = 0;
755
756        uint8 lock = (i->second.allowedSecurityLevel > _accountSecurityLevel) ? 1 : 0;
757
758        pkt << i->second.icon;                             // realm type
759        pkt << lock;                                       // if 1, then realm locked
760        pkt << i->second.color;                            // if 2, then realm is offline
761        pkt << i->first;
762        pkt << i->second.address;
763        pkt << i->second.populationLevel;
764        pkt << AmountOfCharacters;
765        pkt << i->second.timezone;                          // realm category
766        pkt << (uint8) 0x2C;                                // unk, may be realm number/id?
767    }
768    pkt << (uint8) 0x10;
769    pkt << (uint8) 0x00;
770
771    ByteBuffer hdr;
772    hdr << (uint8) REALM_LIST;
773    hdr << (uint16)pkt.size();
774    hdr.append(pkt);
775
776    SendBuf((char const*)hdr.contents(), hdr.size());
777
778    // Set check field before possible relogin to realm
779    _SetVSFields(rI);
780    return true;
781}
782
783/// Resume patch transfer
784bool AuthSocket::_HandleXferResume()
785{
786    DEBUG_LOG("Entering _HandleXferResume");
787    ///- Check packet length and patch existence
788    if (ibuf.GetLength()<9 || !pPatch)
789    {
790        sLog.outError("Error while resuming patch transfer (wrong packet)");
791        return false;
792    }
793
794    ///- Launch a PatcherRunnable thread starting at given patch file offset
795    uint64 start;
796    ibuf.Remove(1);
797    ibuf.Read((char*)&start,sizeof(start));
798    fseek(pPatch,start,0);
799
800    ZThread::Thread u(new PatcherRunnable(this));
801    return true;
802}
803
804/// Cancel patch transfer
805bool AuthSocket::_HandleXferCancel()
806{
807    DEBUG_LOG("Entering _HandleXferCancel");
808
809    ///- Close and delete the socket
810    ibuf.Remove(1);                                         //clear input buffer
811
812    //ZThread::Thread::sleep(15);
813    SetCloseAndDelete();
814
815    return true;
816}
817
818/// Accept patch transfer
819bool AuthSocket::_HandleXferAccept()
820{
821    DEBUG_LOG("Entering _HandleXferAccept");
822
823    ///- Check packet length and patch existence
824    if (!pPatch)
825    {
826        sLog.outError("Error while accepting patch transfer (wrong packet)");
827        return false;
828    }
829
830    ///- Launch a PatcherRunnable thread, starting at the begining of the patch file
831    ibuf.Remove(1);                                         //clear input buffer
832    fseek(pPatch,0,0);
833
834    ZThread::Thread u(new PatcherRunnable(this));
835
836    return true;
837}
838
839/// Check if there is lag on the connection to the client
840bool AuthSocket::IsLag()
841{
842    return (TCP_BUFSIZE_READ-GetOutputLength()< 2*ChunkSize);
843}
844
845PatcherRunnable::PatcherRunnable(class AuthSocket * as)
846{
847    mySocket=as;
848}
849
850/// Send content of patch file to the client
851void PatcherRunnable::run()
852{
853    XFER_DATA_STRUCT xfdata;
854    xfdata.opcode = XFER_DATA;
855
856    while(!feof(mySocket->pPatch) && mySocket->Ready())
857    {
858        ///- Wait until output buffer is reasonably empty
859        while(mySocket->Ready() && mySocket->IsLag())
860        {
861            ZThread::Thread::sleep(1);
862        }
863        ///- And send content of the patch file to the client
864        xfdata.data_size=fread(&xfdata.data,1,ChunkSize,mySocket->pPatch);
865        mySocket->SendBuf((const char*)&xfdata,xfdata.data_size +(sizeof(XFER_DATA_STRUCT)-ChunkSize));
866    }
867}
868
869/// Preload MD5 hashes of existing patch files on server
870#ifndef _WIN32
871#include <sys/dir.h>
872#include <errno.h>
873void Patcher::LoadPatchesInfo()
874{
875    DIR * dirp;
876    //int errno;
877    struct dirent * dp;
878    dirp = opendir("./patches/");
879    if(!dirp)
880        return;
881    while (dirp)
882    {
883        errno = 0;
884        if ((dp = readdir(dirp)) != NULL)
885        {
886            int l=strlen(dp->d_name);
887            if(l<8)continue;
888            if(!memcmp(&dp->d_name[l-4],".mpq",4))
889                LoadPatchMD5(dp->d_name);
890        }
891        else
892        {
893            if(errno != 0)
894            {
895                closedir(dirp);
896                return;
897            }
898            break;
899        }
900    }
901
902    if(dirp)
903        closedir(dirp);
904}
905
906#else
907void Patcher::LoadPatchesInfo()
908{
909    WIN32_FIND_DATA fil;
910    HANDLE hFil=FindFirstFile("./patches/*.mpq",&fil);
911    if(hFil==INVALID_HANDLE_VALUE)
912        return;                                             //no patches were found
913
914    LoadPatchMD5(fil.cFileName);
915
916    while(FindNextFile(hFil,&fil))
917        LoadPatchMD5(fil.cFileName);
918}
919#endif
920
921/// Calculate and store MD5 hash for a given patch file
922void Patcher::LoadPatchMD5(char * szFileName)
923{
924    ///- Try to open the patch file
925    std::string path = "./patches/";
926    path += szFileName;
927    FILE * pPatch=fopen(path.c_str(),"rb");
928    sLog.outDebug("Loading patch info from %s\n",path.c_str());
929    if(!pPatch)
930    {
931        sLog.outError("Error loading patch %s\n",path.c_str());
932        return;
933    }
934
935    ///- Calculate the MD5 hash
936    MD5_CTX ctx;
937    MD5_Init(&ctx);
938    uint8* buf = new uint8[512*1024];
939
940    while (!feof(pPatch))
941    {
942        size_t read = fread(buf, 1, 512*1024, pPatch);
943        MD5_Update(&ctx, buf, read);
944    }
945    delete [] buf;
946    fclose(pPatch);
947
948    ///- Store the result in the internal patch hash map
949    _patches[path] = new PATCH_INFO;
950    MD5_Final((uint8 *)&_patches[path]->md5 , &ctx);
951}
952
953/// Get cached MD5 hash for a given patch file
954bool Patcher::GetHash(char * pat,uint8 mymd5[16])
955{
956    for( Patches::iterator i = _patches.begin(); i != _patches.end(); i++ )
957        if(!stricmp(pat,i->first.c_str () ))
958    {
959        memcpy(mymd5,i->second->md5,16);
960        return true;
961    }
962
963    return false;
964}
965
966/// Launch the patch hashing mechanism on object creation
967Patcher::Patcher()
968{
969    LoadPatchesInfo();
970}
971
972/// Empty and delete the patch map on termination
973Patcher::~Patcher()
974{
975    for(Patches::iterator i = _patches.begin(); i != _patches.end(); i++ )
976        delete i->second;
977}
Note: See TracBrowser for help on using the browser.