// *************************************************************** // Function: CreateAccount.cpp // *************************************************************** #include "Main.h" #include "LocalData.h" void SendError(int errorCode, int numLogins, short networkId); int ProcessVerifyKey(NetMessage* pData, LOCAL_DATA* pLir); //************************************ // Name: ValidateKey // // Processes the "get key" database query. Makes sure the key is ok to use. // // Parameters: // pResult - Database query result // pLir - Player's data // // Return: // //************************************ int ValidateKey(QUERY_RESULT* pResult, LOCAL_DATA* pLir) { if (pResult->pResult==NULL) { gCoreLog.warning("Could not validate key!"); SendError(46, pLir->loginAttempts, pLir->networkId); return -1; } //check if the key is available bool key_ok=false; if (!gServerConfig.requireValidKey) { key_ok=true; } //The key is not activated else if (mysql_num_rows(pResult->pResult)==0) { SendError(102, pLir->loginAttempts, pLir->networkId); } //Check if the key is in-use or OK to use else { MYSQL_ROW row=mysql_fetch_row(pResult->pResult); int key_status=atoi(row[0]); switch(key_status) { //The key is not used case 0: if (true) { key_ok=true; } break; //The key is in-use case 1: if (true) { SendError(103, pLir->loginAttempts, pLir->networkId); } break; default: key_ok=true; break; } } //This key is valid for use if (key_ok) { CompiledPacket pck; pck.AddShort(ServerOpcode(12)); pck.AddLong(pLir->loginAttempts); pck.AddLong(0xFFFFFFFF); pck.AddShort(0); pck.AddByte(0); pck.AddByte(0); pck.AddLong(0); pck.AddLong(0); pck.AddLong(0); pck.AddLong(2); pck.AddLong(0); Util::SendCompiledPacket(&pck, pLir->networkId, gpLoginServer); BIT_SET(pLir->bitfield, LD_KEY_VALIDATED); } return 0; } //************************************ // Name: HandleOpcode_VERIFY_SERIAL // // Handles the verify key request from the client. // // Parameters: // pData - Document // pLir - Document // // Return: // Size of packet. //************************************ int HandleOpcode_VERIFY_SERIAL(NetMessage* pData, LOCAL_DATA* pLir) { if (!BIT_TEST(pLir->bitfield, LD_AUTHENTICATED)) { ProcessVerifyKey(pData, pLir); } else { gpLoginServer->Kick(pData->networkId); } return pData->dataSize; } //************************************ // Name: ProcessVerifyKey // // Extracts the access key from the packet and queries the database for that key's status. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // //************************************ int ProcessVerifyKey(NetMessage* pData, LOCAL_DATA* pLir) { //Extract the login attempts pLir->loginAttempts=Read_uLong(pData->pData+2); //Extract the key and add a query to see if it is on the active list unsigned char unicode_key[60]; memset(unicode_key, 0, 60); memcpy(unicode_key, pData->pData+8, 50); Util::StripUnicode(unicode_key); if (pLir->pAccessKey!=NULL){delete [] pLir->pAccessKey;} char* pKey=new char[26]; strcpy(pKey, (char*)unicode_key); pLir->pAccessKey=pKey; gCoreLog.debug("Access Key: %s", unicode_key); char query[200]; sprintf(query, "SELECT KeyStatus FROM access_keys WHERE AccessKey=\"%s\" LIMIT 1", unicode_key); pLir->queryId=gDatabase.Query(query, (long)strlen(query)); pLir->QueryProcess=ValidateKey; return 0; } //************************************ // Name: VerifyAccountSuccess // // Checks the account insertion query to make sure the account was created before displaying the proper message. // // Parameters: // pResult - Database query result // pLir - Player's data // // Return: // //************************************ int VerifyAccountSuccess(QUERY_RESULT* pResult, LOCAL_DATA* pLir) { if (pResult->affectedRows==0) { gCoreLog.warning("Could not verify account!"); SendError(46, pLir->loginAttempts, pLir->networkId); return -1; } //The account was not created if (pResult->affectedRows==0) { SendError(56, pLir->loginAttempts, pLir->networkId); } //The account was created else { CompiledPacket pck; pck.AddShort(ServerOpcode(11)); pck.AddLong(pLir->loginAttempts); pck.AddLong(0); Util::SendCompiledPacket(&pck, pLir->networkId, gpLoginServer); } //Clean up delete [] pLir->pEmail; pLir->pEmail=NULL; BIT_CLR(pLir->bitfield, LD_KEY_VALIDATED); delete [] pLir->pAccessKey; pLir->pAccessKey=NULL; return 0; } //************************************ // Name: ValidateEmail // // Processes the email check query. Makes certain the email address requested is not in-use. // // Parameters: // pResult - Datbase query result // pLir - Player's data // // Return: // //************************************ int ValidateEmail(QUERY_RESULT* pResult, LOCAL_DATA* pLir) { if (pResult->pResult==NULL) { gCoreLog.warning("Failed to validate email!"); SendError(46, pLir->loginAttempts, pLir->networkId); return -1; } //The email address is already in use if (mysql_num_rows(pResult->pResult)>0) { SendError(105, pLir->loginAttempts, pLir->networkId); } //The email address is not in use else { //Set the key to in-use char query[150]; sprintf(query, "UPDATE access_keys SET KeyStatus=%d WHERE AccessKey=\"%s\"", 1, pLir->pAccessKey); gDatabase.Query(query, (long)strlen(query)); //Insert the account sprintf(query, "INSERT INTO accounts(Email) VALUES(\"%s\")", pLir->pEmail); pLir->queryId=gDatabase.Query(query, (long)strlen(query), true); pLir->QueryProcess=VerifyAccountSuccess; } return 0; } //************************************ // Name: HandleOpcode_CREATE_ACCOUNT // // Handles the account creation request. Queries to see if the requested email is available. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_CREATE_ACCOUNT(NetMessage* pData, LOCAL_DATA* pLir) { if (BIT_TEST(pLir->bitfield, LD_KEY_VALIDATED)) { //Extract the login attempts pLir->loginAttempts=Read_uLong(pData->pData+2); //Extract the email address char email[200]; memset(email, 0, 200); memcpy(email, pData->pData+8, Read_uShort(pData->pData+6)*2); Util::StripUnicode(email); if (pLir->pEmail!=NULL){delete [] pLir->pEmail;} char *e=new char[strlen(email)+2]; strcpy(e, email); pLir->pEmail=e; //Check if the email is in use char query[100]; sprintf(query, "SELECT email FROM accounts WHERE email=\"%s\" LIMIT 1", e); pLir->queryId=gDatabase.Query(query, (long)strlen(query)); pLir->QueryProcess=ValidateEmail; } else { gpLoginServer->Kick(pData->networkId); } return pData->dataSize; }