#include "LoginServ.h" #include "AccountLogin.h" #include "../../src/Utility.h" #include "../../src/Item.h" #include void SendError(int errorCode, int verifCode, NLsocket sockId); int ValidateLogon(QUERY_RESULT* pResult, ClientData* pMyClient); void CheckAuthorization(uchar* pAuthData, ClientData* pMyClient); int CompleteQueries(QUERY_RESULT* pResult, ClientData* pMyClient); int WriteCharacterInfoStream(QUERY_RESULT* pResult, ClientData* pMyClient); void BuildAccountPermissions(GwClient* pClient, ClientData* pMyClient); int HandleOpcodeACCOUNT_LOGIN(Packet* pck, GwClient* pClient) { GetClientData(); AccountLogin1* pAl=new AccountLogin1; //Initialize variables for this section uchar password[23]; memset(password, 0, 23); memset(pAl->email, 0, 65*2); memset(pAl->passHash, 0, 32); //Read in the verif code and password from the packet pMyClient->verifCode=pck->ReadLong(); ushort passLength=pck->ReadByte(); pck->Skip(1); pck->Read(password, passLength); Util::StripUnicode(password); Util::Hash(password, pAl->passHash, passLength); //Read in the account name pck->SetBufferPosition(30); ushort accountNameLength=pck->ReadShort(); pck->Read(pAl->email, accountNameLength*2); //Clear the password memset(password, 0 ,23); //Create the new query char emailHex[300]; Util::BinToHex((uchar*)pAl->email, 65*2, emailHex); char query[500]; sprintf(query, "SELECT Password, AccessLevel, AccountID, Authorization, TimeAccountMarked, DurationOfMark, InterfaceDataSize, InterfaceData FROM accounts WHERE Email=%s LIMIT 1", emailHex); pMyClient->queryId=gLoginServer.mDatabase.Query(query, (long)strlen(query)); pMyClient->pTempData=(void*)pAl; pMyClient->QueryProcess=ValidateLogon; printf("Account Name: ", password); wprintf(pAl->email); printf("\n"); return pClient->inBuffer.size(); } int ValidateLogon(QUERY_RESULT* pResult, ClientData* pMyClient) { //The query failed if (pResult->pResult==NULL) { logFile.Write("Failed to validate login.\n"); SendError(56, pMyClient->verifCode, pMyClient->socketId); return -1; } AccountLogin1* pAl=(AccountLogin1*)pMyClient->pTempData; //Get the MySQL row MYSQL_ROW row=mysql_fetch_row(pResult->pResult); //If the email address does not exist if(mysql_num_rows(pResult->pResult) == 0) { //send back a reply that no account exists with that email SendError(21, pMyClient->verifCode, pMyClient->socketId); return -1; } //Get the account Id ulong accountId=atoi(row[2]); if(accountId == 0) { SendError(56, pMyClient->verifCode, pMyClient->socketId); return -1; } pMyClient->accountId=accountId; //Check the password hash if (row[0]!=NULL) { if(gLoginServer.mRequireValidPassword && memcmp(row[0], pAl->passHash, 32) == 0) { //Password incorrect SendError(11, pMyClient->verifCode, pMyClient->socketId); return -1; } } //Make sure this account isn't already logged in If it is kick the person who is already using the account STDEXT::hash_map::iterator ltr; for (ltr=gLoginServer.mClients.begin(); ltr!=gLoginServer.mClients.end(); ltr++) { if(ltr->second.authenticated) { if (ltr->second.socketId!=pMyClient->socketId && ltr->second.accountId==accountId) { gLoginServer.mpLoginServer->Kick(ltr->second.socketId); break; } } } //Get the account access level pMyClient->accessLevel=atoi(row[1]); //Check to see if the suspension has expired if (pMyClient->accessLevel<=25 && pMyClient->accessLevel>0) { time_t account_mark_time=0; int duration=0; account_mark_time=atoi(row[4]); duration=atoi(row[5]); if (difftime(time(NULL), account_mark_time)>duration) { pMyClient->accessLevel=50; char query[100]; sprintf(query, "UPDATE accounts SET AccessLevel=50 WHERE AccountID=%u LIMIT 1", accountId); gLoginServer.mDatabase.Query(query, (long)strlen(query)); } } //If account is banned if(pMyClient->accessLevel == 0) { SendError(45, pMyClient->verifCode, pMyClient->socketId); return -1; } //If account is suspended else if(pMyClient->accessLevel <= 25) { SendError(45, pMyClient->verifCode, pMyClient->socketId); return -1; } //Prevent normal accounts from playing when the server is locked. else if (pMyClient->accessLevel<=50 && gLoginServer.mServerLocked) { SendError(17, pMyClient->verifCode, pMyClient->socketId); return -1; } //TODO: Check for game servers! //All available game servers are full if(false) { SendError(18, pMyClient->verifCode, pMyClient->socketId); return -1; } //There are no game server available and the ignore flag is off if(false && !gLoginServer.mIgnoreNoGameServer) { SendError(167, pMyClient->verifCode, pMyClient->socketId); return -1; } //The client is now authenticated pMyClient->authenticated=true; //Set the transfer ids char random_long[4]; for (int n=0; n<4; n++){random_long[n]=rand()%256;} memcpy(&pMyClient->transferId1, random_long, 4); for (int n=0; n<4; n++){random_long[n]=rand()%256;} memcpy(&pMyClient->transferId2, random_long, 4); //Save the computer hash to the account's info char compHashHex[100]; Util::BinToHex(pMyClient->compHash, 32, compHashHex); char comp_query[150]; sprintf(comp_query, "UPDATE accounts SET CompHash=%s WHERE AccountID=\"%u\"@NOSTATUS", compHashHex, pMyClient->accountId); gLoginServer.mDatabase.Query(comp_query, (long)strlen(comp_query)); //Check the authorization status CheckAuthorization((uchar*)row[3], pMyClient); //Check for interface data if(atoi(row[6]) > 0) { pAl->dataSize=atoi(row[6]); memcpy(pAl->data, row[7], pAl->dataSize); } else { pAl->dataSize=0; } //Generate the query to grab all the characters char query[600]; sprintf(query, "SELECT Name, Location, HeightSex, HairSkinColor, ProfessionHairStyle, CampaignFace, Level, HeadArmor, ChestArmor, ArmsArmor, LegArmor, FeetArmor, SecondProfession, CharacterType, MissionID FROM characters WHERE OwnerAccountID=\"%u\"", pMyClient->accountId); pMyClient->queryId=gLoginServer.mDatabase.Query(query, (long)strlen(query)); pMyClient->QueryProcess=CompleteQueries; return 1; } int CompleteQueries(QUERY_RESULT* pResult, ClientData* pMyClient) { if(pResult->pResult == NULL) { SendError(56, pMyClient->verifCode, pMyClient->socketId); return -1; } AccountLogin1* pAl=(AccountLogin1*)pMyClient->pTempData; ulong numCharacters=(ulong)mysql_num_rows(pResult->pResult); if(numCharacters > 0) { AlCharacterInfo* tempData=new AlCharacterInfo[numCharacters]; //Iterate through all characters and save their information for (unsigned int i=0; ipResult); //Store the character name memcpy(tempData[i].name, row[0], 60); tempData[i].location=atoi(row[1]); tempData[i].heightSex=atoi(row[2]); tempData[i].hairSkinColor=atoi(row[3]); tempData[i].professionHairStyle=atoi(row[4]); tempData[i].campaignFace=atoi(row[5]); tempData[i].secondProfession=atoi(row[12]); tempData[i].characterType=atoi(row[13]); tempData[i].missionID=atoi(row[14]); tempData[i].level=atoi(row[6]); //Store the item armor id's for (int k=0; k<5; k++) { memcpy(tempData[i].armors[k], row[7+k], 12); } } //Create our structure pAl->characters=tempData; pAl->numCharacters=numCharacters; //Assemble the id's into a format readable by the Item Load function ITEM_ID* pItems=new ITEM_ID[numCharacters*5]; long count=0; for (unsigned int i=0; iqueryId=Item::Load(pItems, count); pMyClient->QueryProcess=WriteCharacterInfoStream; } //If we have no characters, we still have to write the remainder of the character select stream else { WriteCharacterInfoStream(0, pMyClient); } return 1; } int WriteCharacterInfoStream(QUERY_RESULT* pResult, ClientData* pMyClient) { if (pResult != NULL && pResult->pResult==NULL) { logFile.Write("[!] Getting items for account characters failed!\n"); SendError(56, pMyClient->verifCode, pMyClient->socketId); return -1; } //Get our buffer from the network system GwClient* pClient=gLoginServer.mpLoginServer->AccessGwClient(pMyClient->socketId); AccountLogin1* pCharacters=(AccountLogin1*)pMyClient->pTempData; //We have items for characters that need processing or we just have characters if(pResult != NULL) { //Convert the result set to items long numCharacterItems=0; Item* pCharacterItems=Item::ConvertResultSet(pResult->pResult, &numCharacterItems); bool storedDefaultName=false; //Output a character packet for each character on the account for (unsigned int i=0; i<(unsigned long)pCharacters->numCharacters; i++) { AlCharacterInfo* pActor=&pCharacters->characters[i]; pClient->WriteShort(ServerOpcode("CHARACTER_INFO")); pClient->WriteLong(pMyClient->verifCode); pClient->WriteFill(0, 20); //Write character name length pClient->WriteShort((ushort)Util::wstrlen(pActor->name)); //Save the default name if needed if(!storedDefaultName) { memcpy(pMyClient->characterName, pActor->name, 60); storedDefaultName=true; } //Add the character name to the packet pClient->Write(pActor->name, Util::wstrlen(pActor->name)*2); //Write more data pClient->WriteShort(0x3e); pClient->WriteShort(6); //Write the characters location pClient->WriteShort(pActor->location); //More unknown data uchar udata[4]={0x38, 0x34, 0x31, 0x30}; pClient->Write(udata, 4); //Height sex pClient->WriteByte(pActor->heightSex); //Hair & Skin color pClient->WriteByte(pActor->hairSkinColor); //Prof & Hair style pClient->WriteByte(pActor->professionHairStyle); //Campaign & Face pClient->WriteByte(pActor->campaignFace); //Character GUID (Not used) pClient->WriteFill(0, 16); //Set the level uchar characterCampaign=pActor->campaignFace; uchar character_level=0; character_level=(pActor->level << 4) + ((characterCampaign >> 4)+1); pClient->WriteByte(character_level); //Set a byte containing: high level, character type, secondary profession, show helm unsigned char secondary_profession=0; //If needed set high level bit if (pActor->level>15){myBitSet(secondary_profession, 1);} //Set the PvP Only bit if (pActor->characterType>0){myBitSet(secondary_profession, 0x02);} //Set the secondary profession if (pActor->secondProfession>10){pActor->secondProfession=1;} secondary_profession+=(pActor->secondProfession << 2); //No clue myBitSet(secondary_profession, 0x40); //Set the show helm bit myBitSet(secondary_profession, 0x80); pClient->WriteByte(secondary_profession); //Add more unknown data uchar ud[7]={0x5C, 0x5C, 0x05, 0x5C, 0x5C, 0x5C, 0x5C}; pClient->Write(ud, 7); //Add the item data, item ID and colors for (int k=0; k<5; k++) { if(pCharacterItems != NULL) { for (unsigned int j=0; j<(unsigned int)numCharacterItems; j++) { if (memcmp(pActor->armors[k], &pCharacterItems[j], 12) == 0) { Item* pItem=&pCharacterItems[j]; pClient->WriteShort(pItem->GetRawItemData()->gwId); unsigned char col[3]; col[0]=pItem->GetRawItemData()->color[1]; col[1]=pItem->GetRawItemData()->color[2]; col[2]=pItem->GetRawItemData()->color[0]; pClient->Write(col, 3); } } } else { pClient->WriteShort(0); pClient->WriteFill(0, 3); } } } //Clean up if (numCharacterItems>0){delete [] pCharacterItems;} if (pCharacters->characters!=NULL){delete [] pCharacters->characters;} } //Add the account info. This packet contains the interface positioning data if (pCharacters != NULL && pCharacters->dataSize > 0) { //We have interface positioning data in the db =) memcpy(pCharacters->data, &ServerOpcode("ACCOUNT_DATA"), 2); memcpy(pCharacters->data+2, &pMyClient->verifCode, 4); pClient->Write(pCharacters->data, pCharacters->dataSize); } else { //We don't have interface positioning data in the db =( memcpy(gLoginServer.mpDefaultInterface, &ServerOpcode("ACCOUNT_DATA"), 2); memcpy(gLoginServer.mpDefaultInterface+2, &pMyClient->verifCode, 4); pClient->Write(gLoginServer.mpDefaultInterface, gLoginServer.mDefaultInterfaceSize); } //This packet always comes before the account permissions packet //Anyway, build the account permissions packet and add it to the stream pClient->WriteShort(ServerOpcode("ACCOUNT_FRIENDSLIST_END")); pClient->WriteLong(pMyClient->verifCode); pClient->WriteLong(1); BuildAccountPermissions(pClient, pMyClient); pClient->WriteShort(ServerOpcode("STREAM_TERMINATOR")); pClient->WriteLong(pMyClient->verifCode); pClient->WriteLong(0); return 0; } void BuildAccountPermissions(GwClient* pClient, ClientData* pMyClient) { pClient->WriteShort(ServerOpcode("ACCOUNT_PERMISSIONS")); pClient->WriteLong(pMyClient->verifCode); pClient->WriteFill(0, 4); pClient->WriteLong(4); pClient->WriteByte((uchar)pMyClient->ownedCampaigns); pClient->WriteFill(0, 9); pClient->WriteByte(pMyClient->ownsEotn); pClient->WriteFill(0, 41); unsigned char yu[]={0x04, 0x00, 0x01, 0x00, 0x06, 0x00}; pClient->Write(yu, 6); pClient->WriteByte(0x0c);//EULA accepted //0x09 }