X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_pbkdf2.cpp;h=daafa7f8c72e9fc8de2275de03d17905522b5576;hb=e2b0f3dc9ef4d56c71d7abda13e6139ca092e387;hp=89bc39ef4cf8e40f13ef59b8d833e991fa949f0b;hpb=35b70631f0532a5828b04a8e0c02092a285f331a;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_pbkdf2.cpp b/src/modules/m_pbkdf2.cpp index 89bc39ef4..daafa7f8c 100644 --- a/src/modules/m_pbkdf2.cpp +++ b/src/modules/m_pbkdf2.cpp @@ -1,7 +1,10 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2014 Daniel Vassdal + * Copyright (C) 2018, 2020 Sadie Powell + * Copyright (C) 2018 linuxdaemon + * Copyright (C) 2014, 2020 Daniel Vassdal + * Copyright (C) 2014, 2016 Attila Molnar * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public @@ -43,7 +46,7 @@ class PBKDF2Hash std::string tok; ss.GetToken(tok); - this->iterations = ConvToInt(tok); + this->iterations = ConvToNum(tok); ss.GetToken(tok); this->hash = Base64ToBin(tok); @@ -123,7 +126,7 @@ class PBKDF2Provider : public HashProvider return false; std::string cmp = PBKDF2(input, hs.salt, hs.iterations, hs.length); - return (cmp == hs.hash); + return InspIRCd::TimingSafeCompare(cmp, hs.hash); } std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE @@ -139,39 +142,65 @@ class PBKDF2Provider : public HashProvider } }; +struct ProviderConfig +{ + unsigned long dkey_length; + unsigned long iterations; +}; + +typedef std::map ProviderConfigMap; + class ModulePBKDF2 : public Module { std::vector providers; + ProviderConfig globalconfig; + ProviderConfigMap providerconfigs; - void GetConfig() + ProviderConfig GetConfigForProvider(const std::string& name) const + { + ProviderConfigMap::const_iterator it = providerconfigs.find(name); + if (it == providerconfigs.end()) + return globalconfig; + + return it->second; + } + + void ConfigureProviders() { - // First set the common values - ConfigTag* tag = ServerInstance->Config->ConfValue("pbkdf2"); - unsigned int global_iterations = tag->getUInt("iterations", 12288, 1); - unsigned int global_dkey_length = tag->getUInt("length", 32, 1, 1024); for (std::vector::iterator i = providers.begin(); i != providers.end(); ++i) { PBKDF2Provider* pi = *i; - pi->iterations = global_iterations; - pi->dkey_length = global_dkey_length; + ProviderConfig config = GetConfigForProvider(pi->name); + pi->iterations = config.iterations; + pi->dkey_length = config.dkey_length; } + } + + void GetConfig() + { + // First set the common values + ConfigTag* tag = ServerInstance->Config->ConfValue("pbkdf2"); + ProviderConfig newglobal; + newglobal.iterations = tag->getUInt("iterations", 12288, 1); + newglobal.dkey_length = tag->getUInt("length", 32, 1, 1024); // Then the specific values + ProviderConfigMap newconfigs; ConfigTagList tags = ServerInstance->Config->ConfTags("pbkdf2prov"); for (ConfigIter i = tags.first; i != tags.second; ++i) { tag = i->second; std::string hash_name = "hash/" + tag->getString("hash"); - for (std::vector::iterator j = providers.begin(); j != providers.end(); ++j) - { - PBKDF2Provider* pi = *j; - if (pi->provider->name != hash_name) - continue; + ProviderConfig& config = newconfigs[hash_name]; - pi->iterations = tag->getUInt("iterations", global_iterations, 1); - pi->dkey_length = tag->getUInt("length", global_dkey_length, 1, 1024); - } + config.iterations = tag->getUInt("iterations", newglobal.iterations, 1); + config.dkey_length = tag->getUInt("length", newglobal.dkey_length, 1, 1024); } + + // Config is valid, apply it + providerconfigs.swap(newconfigs); + std::swap(globalconfig, newglobal); + ConfigureProviders(); } public: @@ -180,6 +209,14 @@ class ModulePBKDF2 : public Module stdalgo::delete_all(providers); } + void init() CXX11_OVERRIDE + { + // Let ourself know about any existing services. + const ModuleManager::DataProviderMap& dataproviders = ServerInstance->Modules->DataProviders; + for (ModuleManager::DataProviderMap::const_iterator it = dataproviders.begin(); it != dataproviders.end(); ++it) + OnServiceAdd(*it->second); + } + void OnServiceAdd(ServiceProvider& provider) CXX11_OVERRIDE { // Check if it's a hash provider @@ -194,7 +231,7 @@ class ModulePBKDF2 : public Module providers.push_back(prov); ServerInstance->Modules.AddService(*prov); - GetConfig(); + ConfigureProviders(); } void OnServiceDel(ServiceProvider& prov) CXX11_OVERRIDE @@ -219,7 +256,7 @@ class ModulePBKDF2 : public Module Version GetVersion() CXX11_OVERRIDE { - return Version("Implements PBKDF2 hashing", VF_VENDOR); + return Version("Allows other modules to generate PBKDF2 hashes.", VF_VENDOR); } };