anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

enc_old.cpp (2928B)

      1 /* Include file for high-level encryption routines.
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #include "module.h"
     13 #include "modules/encryption.h"
     14 
     15 static ServiceReference<Encryption::Provider> md5("Encryption::Provider", "md5");
     16 
     17 class OldMD5Provider : public Encryption::Provider
     18 {
     19  public:
     20 	OldMD5Provider(Module *creator) : Encryption::Provider(creator, "oldmd5") { }
     21 
     22 	Encryption::Context *CreateContext(Encryption::IV *iv) anope_override
     23 	{
     24 		if (md5)
     25 			return md5->CreateContext(iv);
     26 		return NULL;
     27 	}
     28 
     29 	Encryption::IV GetDefaultIV() anope_override
     30 	{
     31 		if (md5)
     32 			return md5->GetDefaultIV();
     33 		return Encryption::IV(static_cast<const uint32_t *>(NULL), 0);
     34 	}
     35 };
     36 
     37 class EOld : public Module
     38 {
     39 	OldMD5Provider oldmd5provider;
     40 
     41 	inline static char XTOI(char c) { return c > 9 ? c - 'A' + 10 : c - '0'; }
     42 
     43  public:
     44 	EOld(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, ENCRYPTION | VENDOR),
     45 		oldmd5provider(this)
     46 	{
     47 
     48 		ModuleManager::LoadModule("enc_md5", User::Find(creator, true));
     49 		if (!md5)
     50 			throw ModuleException("Unable to find md5 reference");
     51 
     52 	}
     53 
     54 	EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) anope_override
     55 	{
     56 		if (!md5)
     57 			return EVENT_CONTINUE;
     58 
     59 		Encryption::Context *context = md5->CreateContext();
     60 		context->Update(reinterpret_cast<const unsigned char *>(src.c_str()), src.length());
     61 		context->Finalize();
     62 
     63 		Encryption::Hash hash = context->GetFinalizedHash();
     64 
     65 		char digest[32], digest2[16];
     66 		memset(digest, 0, sizeof(digest));
     67 		if (hash.second > sizeof(digest))
     68 			throw CoreException("Hash too large");
     69 		memcpy(digest, hash.first, hash.second);
     70 
     71 		for (int i = 0; i < 32; i += 2)
     72 			digest2[i / 2] = XTOI(digest[i]) << 4 | XTOI(digest[i + 1]);
     73 
     74 		Anope::string buf = "oldmd5:" + Anope::Hex(digest2, sizeof(digest2));
     75 
     76 		Log(LOG_DEBUG_2) << "(enc_old) hashed password from [" << src << "] to [" << buf << "]";
     77 		dest = buf;
     78 		delete context;
     79 		return EVENT_ALLOW;
     80 	}
     81 
     82 	void OnCheckAuthentication(User *, IdentifyRequest *req) anope_override
     83 	{
     84 		const NickAlias *na = NickAlias::Find(req->GetAccount());
     85 		if (na == NULL)
     86 			return;
     87 		NickCore *nc = na->nc;
     88 
     89 		size_t pos = nc->pass.find(':');
     90 		if (pos == Anope::string::npos)
     91 			return;
     92 		Anope::string hash_method(nc->pass.begin(), nc->pass.begin() + pos);
     93 		if (!hash_method.equals_cs("oldmd5"))
     94 			return;
     95 
     96 		Anope::string buf;
     97 		this->OnEncrypt(req->GetPassword(), buf);
     98 		if (nc->pass.equals_cs(buf))
     99 		{
    100 			/* if we are NOT the first module in the list,
    101 			 * we want to re-encrypt the pass with the new encryption
    102 			 */
    103 			if (ModuleManager::FindFirstOf(ENCRYPTION) != this)
    104 				Anope::Encrypt(req->GetPassword(), nc->pass);
    105 			req->Success(this);
    106 		}
    107 	}
    108 };
    109 
    110 MODULE_INIT(EOld)