]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_cloaking.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_cloaking.cpp
index 756781ef052e6167dd5caf2dd70dc119b751d295..daa04cf9bd2a6a4da5d77cfd2bf3cc6dc41e3322 100644 (file)
@@ -1,13 +1,20 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2017, 2019 B00mX0r <b00mx0r@aureus.pw>
+ *   Copyright (C) 2017 Sheogorath <sheogorath@shivering-isles.com>
+ *   Copyright (C) 2016 Adam <Adam@anope.org>
+ *   Copyright (C) 2014 Thomas Fargeix <t.fargeix@gmail.com>
+ *   Copyright (C) 2013, 2018 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2013, 2016-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2011 jackmcbarn <jackmcbarn@inspircd.org>
  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
- *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
- *   Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
- *   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
- *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
- *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
+ *   Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2007 John Brooks <special@inspircd.org>
+ *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
+ *   Copyright (C) 2003, 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
  *
  * 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
@@ -49,6 +56,9 @@ struct CloakInfo
        // The number of parts of the hostname shown when using half cloaking.
        unsigned int domainparts;
 
+       // Whether to ignore the case of a hostname when cloaking it.
+       bool ignorecase;
+
        // The secret used for generating cloaks.
        std::string key;
 
@@ -58,9 +68,10 @@ struct CloakInfo
        // The suffix for IP cloaks (e.g. .IP).
        std::string suffix;
 
-       CloakInfo(CloakMode Mode, const std::string& Key, const std::string& Prefix, const std::string& Suffix, unsigned int DomainParts = 0)
+       CloakInfo(CloakMode Mode, const std::string& Key, const std::string& Prefix, const std::string& Suffix, bool IgnoreCase, unsigned int DomainParts = 0)
                : mode(Mode)
                , domainparts(DomainParts)
+               , ignorecase(IgnoreCase)
                , key(Key)
                , prefix(Prefix)
                , suffix(Suffix)
@@ -70,13 +81,25 @@ struct CloakInfo
 
 typedef std::vector<std::string> CloakList;
 
-/** Handles user mode +x
- */
+class CloakExtItem : public SimpleExtItem<CloakList>
+{
+ public:
+       CloakExtItem(Module* Creator)
+               : SimpleExtItem("cloaks", ExtensionItem::EXT_USER, Creator)
+       {
+       }
+
+       std::string ToHuman(const Extensible* container, void* item) const CXX11_OVERRIDE
+       {
+               return stdalgo::string::join(*static_cast<CloakList*>(item), ' ');
+       }
+};
+
 class CloakUser : public ModeHandler
 {
  public:
        bool active;
-       SimpleExtItem<CloakList> ext;
+       CloakExtItem ext;
        std::string debounce_uid;
        time_t debounce_ts;
        int debounce_count;
@@ -84,7 +107,7 @@ class CloakUser : public ModeHandler
        CloakUser(Module* source)
                : ModeHandler(source, "cloak", 'x', PARAM_NONE, MODETYPE_USER)
                , active(false)
-               , ext("cloaked_host", ExtensionItem::EXT_USER, source)
+               , ext(source)
                , debounce_ts(0)
                , debounce_count(0)
        {
@@ -136,8 +159,16 @@ class CloakUser : public ModeHandler
                        if (!cloaks)
                        {
                                /* Force creation of missing cloak */
-                               creator->OnUserConnect(user);
-                               cloaks = ext.get(user);
+                               try
+                               {
+                                       creator->OnUserConnect(user);
+                                       cloaks = ext.get(user);
+                               }
+                               catch (CoreException& modexcept)
+                               {
+                                       ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Exception caught when generating cloak: " + modexcept.GetReason());
+                                       return MODEACTION_DENY;
+                               }
                        }
 
                        // If we have a cloak then set the hostname.
@@ -210,24 +241,24 @@ class ModuleCloaking : public Module
        {
                // The position at which we found the last dot.
                std::string::const_reverse_iterator dotpos;
-       
+
                // The number of dots we have seen so far.
                unsigned int seendots = 0;
-       
+
                for (std::string::const_reverse_iterator iter = host.rbegin(); iter != host.rend(); ++iter)
                {
                        if (*iter != '.')
                                continue;
-       
+
                        // We have found a dot!
                        dotpos = iter;
                        seendots += 1;
-       
+
                        // Do we have enough segments to stop?
                        if (seendots >= domainparts)
                                break;
                }
-       
+
                // We only returns a domain part if more than one label is
                // present. See above for a full explanation.
                if (!seendots)
@@ -248,7 +279,10 @@ class ModuleCloaking : public Module
                input.append(1, id);
                input.append(info.key);
                input.append(1, '\0'); // null does not terminate a C++ string
-               input.append(item);
+               if (info.ignorecase)
+                       std::transform(item.begin(), item.end(), std::back_inserter(input), ::tolower);
+               else
+                       input.append(item);
 
                std::string rv = Hash->GenerateRaw(input).substr(0,len);
                for(size_t i = 0; i < len; i++)
@@ -368,12 +402,14 @@ class ModuleCloaking : public Module
                {
                        u->SetMode(cu, false);
 
-                       if (!IS_LOCAL(u))
+                       LocalUser* luser = IS_LOCAL(u);
+                       if (!luser)
                                return;
+
                        Modes::ChangeList modechangelist;
                        modechangelist.push_remove(&cu);
                        ClientProtocol::Events::Mode modeevent(ServerInstance->FakeClient, NULL, u, modechangelist);
-                       static_cast<LocalUser*>(u)->Send(modeevent);
+                       luser->Send(modeevent);
                }
                cu.active = false;
        }
@@ -388,20 +424,20 @@ class ModuleCloaking : public Module
                        {
                                case MODE_HALF_CLOAK:
                                        // Use old cloaking verification to stay compatible with 2.0
-                                       // But verify domainparts when use 3.0-only features
-                                       if (info.domainparts == 3)
+                                       // But verify domainparts and ignorecase when use 3.0-only features
+                                       if (info.domainparts == 3 && !info.ignorecase)
                                                testcloak = info.prefix + SegmentCloak(info, "*", 3, 8) + info.suffix;
                                        else
                                        {
                                                irc::sockets::sockaddrs sa;
-                                               testcloak = GenCloak(info, sa, "", testcloak + ConvToStr(info.domainparts));
+                                               testcloak = GenCloak(info, sa, "", testcloak + ConvToStr(info.domainparts)) + (info.ignorecase ? "-ci" : "");
                                        }
                                        break;
                                case MODE_OPAQUE:
-                                       testcloak = info.prefix + SegmentCloak(info, "*", 4, 8) + info.suffix;
+                                       testcloak = info.prefix + SegmentCloak(info, "*", 4, 8) + info.suffix + (info.ignorecase ? "-ci" : "");
                        }
                }
-               return Version("Provides masking of user hostnames", VF_COMMON|VF_VENDOR, testcloak);
+               return Version("Adds user mode x (cloak) which allows user hostnames to be hidden.", VF_COMMON|VF_VENDOR, testcloak);
        }
 
        void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
@@ -424,18 +460,19 @@ class ModuleCloaking : public Module
                        if (i == tags.first && key.length() < minkeylen)
                                throw ModuleException("Your cloaking key is not secure. It should be at least " + ConvToStr(minkeylen) + " characters long, at " + tag->getTagLocation());
 
+                       const bool ignorecase = tag->getBool("ignorecase");
                        const std::string mode = tag->getString("mode");
                        const std::string prefix = tag->getString("prefix");
                        const std::string suffix = tag->getString("suffix", ".IP");
                        if (stdalgo::string::equalsci(mode, "half"))
                        {
                                unsigned int domainparts = tag->getUInt("domainparts", 3, 1, 10);
-                               newcloaks.push_back(CloakInfo(MODE_HALF_CLOAK, key, prefix, suffix, domainparts));
+                               newcloaks.push_back(CloakInfo(MODE_HALF_CLOAK, key, prefix, suffix, ignorecase, domainparts));
                        }
                        else if (stdalgo::string::equalsci(mode, "full"))
-                               newcloaks.push_back(CloakInfo(MODE_OPAQUE, key, prefix, suffix));
+                               newcloaks.push_back(CloakInfo(MODE_OPAQUE, key, prefix, suffix, ignorecase));
                        else
-                               throw ModuleException(mode + " is an invalid value for <cloak:mode>; acceptable values are 'half' and 'full', at " + tag->getTagLocation()); 
+                               throw ModuleException(mode + " is an invalid value for <cloak:mode>; acceptable values are 'half' and 'full', at " + tag->getTagLocation());
                }
 
                // The cloak configuration was valid so we can apply it.
@@ -466,6 +503,24 @@ class ModuleCloaking : public Module
                return chost;
        }
 
+       void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
+       {
+               // Connecting users are handled in OnUserConnect not here.
+               if (user->registered != REG_ALL || user->quitting)
+                       return;
+
+               // Remove the cloaks and generate new ones.
+               cu.ext.unset(user);
+               OnUserConnect(user);
+
+               // If a user is using a cloak then update it.
+               if (user->IsModeSet(cu))
+               {
+                       CloakList* cloaklist = cu.ext.get(user);
+                       user->ChangeDisplayedHost(cloaklist->front());
+               }
+       }
+
        void OnUserConnect(LocalUser* dest) CXX11_OVERRIDE
        {
                if (cu.ext.get(dest))