#include "LoginServ.h" #include "../../src/Utility.h" int ValidateKey(QUERY_RESULT* pResult, ClientData* pLir); void SendError(int errorCode, int verifCode, NLsocket sockId) { unsigned char data[10]={0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00}; memcpy(data, &ServerOpcode("STREAM_TERMINATOR"), 2); memcpy(data+2, &verifCode, 4); memcpy(data+6, &errorCode, 4); gLoginServer.mpLoginServer->Send(sockId, data, 10); } int HandleOpcodeVERIFY_SERIAL(Packet* pck, GwClient* pClient) { GetClientData(); if(!pMyClient->authenticated) { pMyClient->verifCode=pck->ReadLong(); pck->Skip(2); //We don't care if the key is Unicode we just want an ascii version uchar accessKey[52]; memset(accessKey, 0, 52); pck->Read(accessKey, 50); Util::StripUnicode(accessKey); pMyClient->pAccessKey=new char[26]; memset(pMyClient->pAccessKey, 0, 26); strncpy(pMyClient->pAccessKey, (char*)accessKey, 25); char query[200]; sprintf(query, "SELECT KeyStatus FROM access_keys WHERE AccessKey=\"%s\" LIMIT 1", accessKey); pMyClient->queryId=gLoginServer.mDatabase.Query(query, (long)strlen(query)); pMyClient->QueryProcess=ValidateKey; } return pClient->inBuffer.size(); } //The following function is called when the query result is available int ValidateKey(QUERY_RESULT* pResult, ClientData* pMyClient) { if(pResult->pResult==NULL) { logFile.Write("Could not validate key!\n"); SendError(46, pMyClient->verifCode, pMyClient->socketId); return -1; } //check if the key is available bool keyOk=false; if(!gLoginServer.mRequireValidAccessKey) { keyOk=true; } //The key is not activated else if(mysql_num_rows(pResult->pResult)==0) { SendError(102, pMyClient->verifCode, pMyClient->socketId); } //Check if the key is in-use or OK to use else { MYSQL_ROW row=mysql_fetch_row(pResult->pResult); int keyStatus=atoi(row[0]); switch(keyStatus) { //The key is not used case 0: keyOk=true; break; //The key is in-use case 1: SendError(103, pMyClient->verifCode, pMyClient->socketId); break; default: keyOk=true; break; } } //This key is valid for use if (keyOk) { GwClient* pClient=gLoginServer.mpLoginServer->AccessGwClient(pMyClient->socketId); pClient->WriteShort(ServerOpcode("SERIAL_KEY_INFORMATION")); pClient->WriteLong(pMyClient->verifCode); pClient->WriteLong(0xFFFFFFFF); pClient->WriteFill(0, 16); pClient->WriteLong(2); pClient->WriteLong(0); pMyClient->keyValidated=true; } return 0; }