void ProtocolLogin::onRecvFirstMessage(NetworkMessage& msg) { if( #if defined(WINDOWS) && !defined(_CONSOLE) !GUI::getInstance()->m_connections || #endif g_game.getGameState() == GAMESTATE_SHUTDOWN) { getConnection()->close(); return; } uint32_t clientIp = getConnection()->getIP(); msg.get<uint16_t>(); uint16_t version = msg.get<uint16_t>(); msg.skip(12); #ifdef _MULTIPLATFORM77 if(!RSA_decrypt(msg)) { getConnection()->close(); return; } uint32_t key[4] = {msg.get<uint32_t>(), msg.get<uint32_t>(), msg.get<uint32_t>(), msg.get<uint32_t>()}; enableXTEAEncryption(); setXTEAKey(key); #endif uint32_t name = msg.get<uint32_t>(); std::string password = msg.getString(); if(!name) { if(!g_config.getBool(ConfigManager::ACCOUNT_MANAGER)) { disconnectClient(0x0A, "Invalid account name."); return; } name = 1; password = "******"; } if(!g_config.getBool(ConfigManager::MANUAL_ADVANCED_CONFIG)) { if(version < CLIENT_VERSION_MIN || version > CLIENT_VERSION_MAX) { disconnectClient(0x0A, CLIENT_VERSION_STRING); return; } else if(version < g_config.getNumber(ConfigManager::VERSION_MIN) || version > g_config.getNumber(ConfigManager::VERSION_MAX)) { disconnectClient(0x0A, g_config.getString(ConfigManager::VERSION_MSG).c_str()); return; } } #ifdef CLIENT_VERSION_DATA if(sprSignature != CLIENT_VERSION_SPR) { disconnectClient(0x0A, CLIENT_VERSION_DATA); return; } if(datSignature != CLIENT_VERSION_DAT) { disconnectClient(0x0A, CLIENT_VERSION_DATA); return; } if(picSignature != CLIENT_VERSION_PIC) { disconnectClient(0x0A, CLIENT_VERSION_DATA); return; } #endif if(g_game.getGameState() < GAMESTATE_NORMAL) { disconnectClient(0x0A, "Server is just starting up, please wait."); return; } if(g_game.getGameState() == GAMESTATE_MAINTAIN) { disconnectClient(0x0A, "Server is under maintenance, please re-connect in a while."); return; } if(ConnectionManager::getInstance()->isDisabled(clientIp, protocolId)) { disconnectClient(0x0A, "Too many connections attempts from your IP address, please try again later."); return; } if(IOBan::getInstance()->isIpBanished(clientIp)) { disconnectClient(0x0A, "Your IP is banished!"); return; } uint32_t id = 1; if(!IOLoginData::getInstance()->getAccountId(name, id)) { ConnectionManager::getInstance()->addAttempt(clientIp, protocolId, false); disconnectClient(0x0A, "Invalid account id."); return; } Account account = IOLoginData::getInstance()->loadAccount(id); if(!encryptTest(account.salt + password, account.password)) { ConnectionManager::getInstance()->addAttempt(clientIp, protocolId, false); disconnectClient(0x0A, "Invalid password."); return; } Ban ban; ban.value = account.number; ban.type = BAN_ACCOUNT; if(IOBan::getInstance()->getData(ban) && !IOLoginData::getInstance()->hasFlag(account.number, PlayerFlag_CannotBeBanned)) { bool deletion = ban.expires < 0; std::string name_ = "Automatic "; if(!ban.adminId) name_ += (deletion ? "deletion" : "banishment"); else IOLoginData::getInstance()->getNameByGuid(ban.adminId, name_, true); std::stringstream ss; ss << "Your account has been " << (deletion ? "deleted" : "banished") << " at:\n" << formatDateEx(ban.added, "%d %b %Y").c_str() << " by: " << name_.c_str() << "\nReason:\n" << getReason(ban.reason).c_str() << ".\nComment:\n" << ban.comment.c_str() << ".\nYour " << (deletion ? "account won't be undeleted" : "banishment will be lifted at:\n") << (deletion ? "" : formatDateEx(ban.expires).c_str()); disconnectClient(0x0A, ss.str().c_str()); return; } // remove premium days #ifndef __LOGIN_SERVER__ IOLoginData::getInstance()->removePremium(account); if(!g_config.getBool(ConfigManager::ACCOUNT_MANAGER) && !account.charList.size()) { disconnectClient(0x0A, std::string("This account does not contain any character yet.\nCreate a new character on the " + g_config.getString(ConfigManager::SERVER_NAME) + " website at " + g_config.getString(ConfigManager::URL) + ".").c_str()); return; } #else Characters charList; for(Characters::iterator it = account.charList.begin(); it != account.charList.end(); ++it) { if(version >= it->second.server->getVersionMin() && version <= it->second.server->getVersionMax()) charList[it->first] = it->second; } IOLoginData::getInstance()->removePremium(account); if(!g_config.getBool(ConfigManager::ACCOUNT_MANAGER) && !charList.size()) { disconnectClient(0x0A, std::string("This account does not contain any character on this client yet.\nCreate a new character on the " + g_config.getString(ConfigManager::SERVER_NAME) + " website at " + g_config.getString(ConfigManager::URL) + ".").c_str()); return; } #endif ConnectionManager::getInstance()->addAttempt(clientIp, protocolId, true); if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false)) { TRACK_MESSAGE(output); output->put<char>(0x14); uint32_t serverIp = serverIps.front().first; for(std::list<std::pair<uint32_t, uint32_t> >::iterator it = serverIps.begin(); it != serverIps.end(); ++it) { if((it->first & it->second) != (clientIp & it->second)) continue; serverIp = it->first; break; } char motd[1300]; sprintf(motd, "%d\n%s", g_game.getMotdId(), g_config.getString(ConfigManager::MOTD).c_str()); output->putString(motd); //Add char list output->put<char>(0x64); if(g_config.getBool(ConfigManager::ACCOUNT_MANAGER) && account.number != 1) { output->put<char>(account.charList.size() + 1); output->putString("Account Manager"); output->putString(g_config.getString(ConfigManager::SERVER_NAME)); output->put<uint32_t>(serverIp); output->put<uint16_t>(g_config.getNumber(ConfigManager::GAME_PORT)); } else output->put<char>((uint8_t)account.charList.size()); #ifndef __LOGIN_SERVER__ for(Characters::iterator it = account.charList.begin(); it != account.charList.end(); ++it) { output->putString((*it)); if(g_config.getBool(ConfigManager::ON_OR_OFF_CHARLIST) && !g_config.getBool(ConfigManager::CHARLIST_INFO)) { if(g_game.getPlayerByName((*it))) output->putString("Online"); else output->putString("Offline"); } else if(g_config.getBool(ConfigManager::CHARLIST_INFO)) { std::stringstream str; Player *player = g_game.getPlayerByName((*it)); bool v = false; if(g_config.getBool(ConfigManager::ON_OR_OFF_CHARLIST)) { if(player) str << "On"; else str << "Off"; str << "/"; } if(!player) { v = true; player = g_game.getPlayerByNameEx((*it)); } str << player->getLevel(); str << "/"; str << player->getVocation()->getName(); output->putString(str.str()); if(v) delete player; } else output->putString(g_config.getString(ConfigManager::SERVER_NAME)); output->put<uint32_t>(serverIp); output->put<uint16_t>(g_config.getNumber(ConfigManager::GAME_PORT)); } #else for(Characters::iterator it = charList.begin(); it != charList.end(); ++it) { output->putString(it->second.name); if(!g_config.getBool(ConfigManager::ON_OR_OFF_CHARLIST) || it->second.status < 0) output->putString(it->second.server->getName()); else if(it->second.status) output->putString("Online"); else output->putString("Offline"); output->put<uint32_t>(it->second.server->getAddress()); IntegerVec games = it->second.server->getPorts(); output->put<uint16_t>(games[random_range(0, games.size() - 1)]); } #endif //Add premium days if(g_config.getBool(ConfigManager::FREE_PREMIUM)) output->put<uint16_t>(GRATIS_PREMIUM); else output->put<uint16_t>(account.premiumDays); OutputMessagePool::getInstance()->send(output); } getConnection()->close(); }
void Spectators::handle(ProtocolGame* client, const std::string& text, uint16_t channelId) { if(!owner) return; SpectatorList::iterator sit = spectators.find(client); if(sit == spectators.end()) return; PrivateChatChannel* channel = g_chat.getPrivateChannel(owner->getPlayer()); if(text[0] == '/') { StringVec t = explodeString(text.substr(1, text.length()), " ", true, 1); toLowerCaseString(t[0]); if(t[0] == "show") { std::stringstream s; s << spectators.size() << " spectators. "; for(SpectatorList::const_iterator it = spectators.begin(); it != spectators.end(); ++it) { if(it != spectators.begin()) s << " ,"; s << it->second.first; } s << "."; client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, s.str(), NULL, 0); } else if(t[0] == "name") { if(t.size() > 1) { if(t[1].length() > 2) { if(t[1].length() < 26) { t[1] += " [G]"; bool found = false; for(SpectatorList::iterator iit = spectators.begin(); iit != spectators.end(); ++iit) { if(asLowerCaseString(iit->second.first) != asLowerCaseString(t[1])) continue; found = true; break; } if(!found) { client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Your name has been set to " + t[1] + ".", NULL, 0); if(!auth && channel) sendChannelMessage("", sit->second.first + " is now known as " + t[1] + ".", MSG_GAMEMASTER_CHANNEL, channel->getId()); StringVec::iterator mit = std::find(mutes.begin(), mutes.end(), asLowerCaseString(sit->second.first)); if(mit != mutes.end()) (*mit) = asLowerCaseString(t[1]); sit->second.first = t[1]; sit->second.second = false; } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Specified name is already taken.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Specified name is too long.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Specified name is too short.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Not enough param(s) given.", NULL, 0); } else if(t[0] == "auth") { if(t.size() > 1) { StringVec _t = explodeString(t[1], " ", true, 1); if(_t.size() > 1) { Database* db = Database::getInstance(); DBQuery query; query << "SELECT `id`, `salt`, `password` FROM `accounts` WHERE `name` " << db->getStringComparer() << db->escapeString(_t[0]) << " LIMIT 1"; if(DBResult* result = db->storeQuery(query.str())) { std::string password = result->getDataString("salt") + _t[1], hash = result->getDataString("password"); uint32_t id = result->getDataInt("id"); result->free(); if(encryptTest(password, hash)) { query.str(""); query << "SELECT `name` FROM `players` WHERE `account_id` = " << id << " ORDER BY `level` DESC LIMIT 1"; if((result = db->storeQuery(query.str()))) { std::string nickname = result->getDataString("name"); result->free(); client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "You have authenticated as " + nickname + ".", NULL, 0); if(channel) sendChannelMessage("", sit->second.first + " authenticated as " + nickname + ".", MSG_GAMEMASTER_CHANNEL, channel->getId()); StringVec::iterator mit = std::find(mutes.begin(), mutes.end(), asLowerCaseString(sit->second.first)); if(mit != mutes.end()) (*mit) = asLowerCaseString(nickname); sit->second.first = nickname; sit->second.second = true; } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Your account has no characters yet.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Invalid password.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Invalid account name.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Not enough param(s) given.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Not enough param(s) given.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "Command not found.", NULL, 0); return; } if(!auth || sit->second.second) { StringVec::const_iterator mit = std::find(mutes.begin(), mutes.end(), asLowerCaseString(sit->second.first)); if(mit == mutes.end()) { if(channel && channel->getId() == channelId) channel->talk(sit->second.first, MSG_CHANNEL_HIGHLIGHT, text); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "You are muted.", NULL, 0); } else client->sendCreatureSay(owner->getPlayer(), MSG_PRIVATE, "This chat is protected, you have to authenticate first.", NULL, 0); }
static int doTest( CSSM_CSP_HANDLE cspHand, CSSM_ALGORITHMS keyAlg, // RSA/DSA CSSM_ALGORITHMS sigAlg, unsigned sigLoops, // may be zero unsigned encrLoops, // ditto; it will be zero for DSA CSSM_BOOL rsaBlinding, CSSM_DATA_PTR ptext, unsigned maxPtextSize, uint32 keySizeInBits, // 0 --> random per alg CSSM_BOOL pubRefKeys, CSSM_BOOL privRefKeys, CSSM_BOOL bareCsp, // for other workarounds CSSM_BOOL quiet, CSSM_BOOL verbose) { CSSM_KEY cdsaGenPubKey; CSSM_KEY cdsaGenPrivKey; // only used to create bsafeDerivePrivKey CSSM_KEY cdsaTempKey; // raw key if privRefKeys true CSSM_KEY cdsaDerivePrivKey; // same as bsafeGenPrivKey BU_KEY bsafeGenPubKey; BU_KEY bsafeGenPrivKey; // only used to create cdsaDerivePrivKey BU_KEY bsafeDerivePrivKey; // same as cdsaGenPrivKey unsigned actKeySizeBits; CSSM_RETURN crtn; int rtn; if(!keySizeInBits) { /* random key size */ actKeySizeBits = randKeySizeBits(keyAlg, OT_Encrypt); } else { /* caller/user specified */ actKeySizeBits = keySizeInBits; } if(verbose) { printf(" ...generating %s key pair, keySize %d bits...\n", algToStr(keyAlg), actKeySizeBits); } /* * Generate two keypairs */ if(keyAlg == CSSM_ALGID_DSA) { CSSM_BOOL doGenParams; if(bareCsp || CSPDL_DSA_GEN_PARAMS) { doGenParams = CSSM_TRUE; } else { /* CSPDL - no gen params */ doGenParams = CSSM_FALSE; } crtn = cspGenDSAKeyPair(cspHand, "foo", 3, actKeySizeBits, &cdsaGenPubKey, pubRefKeys, CSSM_KEYUSE_ANY, CSSM_KEYBLOB_RAW_FORMAT_NONE, &cdsaGenPrivKey, privRefKeys, CSSM_KEYUSE_SIGN, CSSM_KEYBLOB_RAW_FORMAT_NONE, doGenParams, // genParams NULL); // params } else { crtn = cspGenKeyPair(cspHand, keyAlg, "foo", 3, actKeySizeBits, &cdsaGenPubKey, pubRefKeys, CSSM_KEYUSE_ANY, CSSM_KEYBLOB_RAW_FORMAT_NONE, &cdsaGenPrivKey, privRefKeys, CSSM_KEYUSE_ANY, CSSM_KEYBLOB_RAW_FORMAT_NONE, CSSM_FALSE); // genSeed not used } if(crtn) { return testError(quiet); } crtn = buGenKeyPair(actKeySizeBits, keyAlg, &bsafeGenPubKey, &bsafeGenPrivKey); if(crtn) { return testError(quiet); } /* * Convert private keys to other library. * NOTE: the reason we're only converting private keys is solely due to the * fact that BSAFE does not handle PKCS1 formatted public key blobs. Very odd. * But it's too much of a pain to re-implement that wheel here, and SSL and * cert handling in general verify the CSP's PKCS1-style public key handling. */ if(privRefKeys) { /* first generate a temporary raw CDSA key */ crtn = buBsafePrivKeyToCdsa(keyAlg, actKeySizeBits, bsafeGenPrivKey, &cdsaTempKey); if(crtn) { return testError(quiet); } /* convert it to the ref key we'll actually use */ crtn = cspRawKeyToRef(cspHand, &cdsaTempKey, &cdsaDerivePrivKey); cspFreeKey(cspHand, &cdsaTempKey); } else { crtn = buBsafePrivKeyToCdsa(keyAlg, actKeySizeBits, bsafeGenPrivKey, &cdsaDerivePrivKey); } if(crtn) { return testError(quiet); } if(privRefKeys) { /* we have a CDSA priv ref key; convert it to raw format */ crtn = cspRefKeyToRaw(cspHand, &cdsaGenPrivKey, &cdsaTempKey); if(crtn) { return testError(quiet); } /* now convert it to BSAFE */ crtn = buCdsaPrivKeyToBsafe(&cdsaTempKey, &bsafeDerivePrivKey); cspFreeKey(cspHand, &cdsaTempKey); } else { crtn = buCdsaPrivKeyToBsafe(&cdsaGenPrivKey, &bsafeDerivePrivKey); } if(crtn) { return testError(quiet); } if(sigLoops) { rtn = sigTest(cspHand, sigLoops, bsafeDerivePrivKey, &cdsaGenPubKey, &cdsaDerivePrivKey, bsafeGenPubKey, ptext, maxPtextSize, sigAlg, rsaBlinding, quiet, verbose); if(rtn) { return rtn; } } if(encrLoops) { rtn = encryptTest(cspHand, encrLoops, bsafeDerivePrivKey, &cdsaGenPubKey, &cdsaDerivePrivKey, bsafeGenPubKey, ptext, maxPtextSize, rsaBlinding, quiet, verbose); if(rtn) { return rtn; } } /* free all six keys */ buFreeKey(bsafeGenPubKey); buFreeKey(bsafeGenPrivKey); buFreeKey(bsafeDerivePrivKey); cspFreeKey(cspHand, &cdsaGenPubKey); cspFreeKey(cspHand, &cdsaGenPrivKey); cspFreeKey(cspHand, &cdsaDerivePrivKey); return 0; }
bool ProtocolLogin::parseFirstPacket(NetworkMessage& msg) { if(server.game().getGameState() == GAME_STATE_SHUTDOWN) { getConnection()->close(); return false; } uint32_t clientIp = getConnection()->getIP(); /*uint16_t operatingSystem = msg.GetU16();*/msg.SkipBytes(2); uint16_t version = msg.GetU16(); msg.SkipBytes(12); if(!RSA_decrypt(msg)) { getConnection()->close(); return false; } uint32_t key[4] = {msg.GetU32(), msg.GetU32(), msg.GetU32(), msg.GetU32()}; enableXTEAEncryption(); setXTEAKey(key); std::string name = msg.GetString(), password = msg.GetString(); if(name.empty()) { if(!server.configManager().getBool(ConfigManager::ACCOUNT_MANAGER)) { disconnectClient(0x0A, "Invalid account name."); return false; } name = "1"; password = "******"; } if(version < CLIENT_VERSION_MIN || version > CLIENT_VERSION_MAX) { disconnectClient(0x0A, CLIENT_VERSION_STRING); return false; } if(server.game().getGameState() < GAME_STATE_NORMAL) { disconnectClient(0x0A, "Server is just starting up, please wait."); return false; } if(server.game().getGameState() == GAME_STATE_MAINTAIN) { disconnectClient(0x0A, "Server is under maintenance, please re-connect in a while."); return false; } if(ConnectionManager::getInstance()->isDisabled(clientIp, protocolId)) { disconnectClient(0x0A, "Too many connections attempts from your IP address, please try again later."); return false; } if(IOBan::getInstance()->isIpBanished(clientIp)) { disconnectClient(0x0A, "Your IP is banished!"); return false; } uint32_t id = 1; if(!IOLoginData::getInstance()->getAccountId(name, id)) { ConnectionManager::getInstance()->addAttempt(clientIp, protocolId, false); disconnectClient(0x0A, "Invalid account name."); return false; } AccountP account = IOLoginData::getInstance()->loadAccount(id); if (account == nullptr) { disconnectClient(0x0A, "Invalid account name."); return false; } if(!encryptTest(password, account->getPassword())) { ConnectionManager::getInstance()->addAttempt(clientIp, protocolId, false); disconnectClient(0x0A, "Invalid password."); return false; } Ban ban; ban.value = account->getId(); ban.type = BAN_ACCOUNT; if(IOBan::getInstance()->getData(ban) && !IOLoginData::getInstance()->hasFlag(account->getId(), PlayerFlag_CannotBeBanned)) { bool deletion = ban.expires < 0; std::string name_ = "Automatic "; if(!ban.adminId) name_ += (deletion ? "deletion" : "banishment"); else IOLoginData::getInstance()->getNameByGuid(ban.adminId, name_, true); char buffer[500 + ban.comment.length()]; sprintf(buffer, "Your account has been %s at:\n%s by: %s,\nfor the following reason:\n%s.\nThe action taken was:\n%s.\nThe comment given was:\n%s.\nYour %s%s.", (deletion ? "deleted" : "banished"), formatDateShort(ban.added).c_str(), name_.c_str(), getReason(ban.reason).c_str(), getAction(ban.action, false).c_str(), ban.comment.c_str(), (deletion ? "account won't be undeleted" : "banishment will be lifted at:\n"), (deletion ? "." : formatDateShort(ban.expires, true).c_str())); disconnectClient(0x0A, buffer); return false; } const Account::Characters& characters = account->getCharacters(); if(!server.configManager().getBool(ConfigManager::ACCOUNT_MANAGER) && characters.empty()) { disconnectClient(0x0A, std::string("This account does not contain any character yet.\nCreate a new character on the " + server.configManager().getString(ConfigManager::SERVER_NAME) + " website at " + server.configManager().getString(ConfigManager::URL) + ".").c_str()); return false; } ConnectionManager::getInstance()->addAttempt(clientIp, protocolId, true); if(OutputMessage_ptr output = OutputMessagePool::getInstance()->getOutputMessage(this, false)) { TRACK_MESSAGE(output); output->AddByte(0x14); char motd[1300]; sprintf(motd, "%d\n%s", server.game().getMotdId(), server.configManager().getString(ConfigManager::MOTD).c_str()); output->AddString(motd); uint32_t serverIp = serverIps[0].first; for(IpList::iterator it = serverIps.begin(); it != serverIps.end(); ++it) { if((it->first & it->second) != (clientIp & it->second)) continue; serverIp = it->first; break; } //Add char list output->AddByte(0x64); if(server.configManager().getBool(ConfigManager::ACCOUNT_MANAGER) && id != 1) { output->AddByte(characters.size() + 1); output->AddString("Account Manager"); output->AddString(server.configManager().getString(ConfigManager::SERVER_NAME)); output->AddU32(serverIp); output->AddU16(server.configManager().getNumber(ConfigManager::GAME_PORT)); } else output->AddByte((uint8_t)characters.size()); for (auto it = characters.cbegin(); it != characters.cend(); ++it) { auto& character = *it; #ifndef __LOGIN_SERVER__ output->AddString(character->getName()); output->AddString(character->getType()); output->AddU32(serverIp); output->AddU16(server.configManager().getNumber(ConfigManager::GAME_PORT)); #else if(version < it->second->getVersionMin() || version > it->second->getVersionMax()) continue; output->AddString(it->first); output->AddString(it->second->getName()); output->AddU32(it->second->getAddress()); output->AddU16(it->second->getPort()); #endif } Days premiumDays = account->getPremiumDays(); if (premiumDays.count() >= std::numeric_limits<uint16_t>::max()) { output->AddU16(std::numeric_limits<uint16_t>::max()); } else { output->AddU16(static_cast<uint16_t>(premiumDays.count())); } OutputMessagePool::getInstance()->send(output); } getConnection()->close(); return true; }