+ rv.append(prefix);
+ rv.append(SegmentCloak(bindata, 10, len1));
+ rv.append(1, '.');
+ bindata.erase(hop1);
+ rv.append(SegmentCloak(bindata, 11, len2));
+ if (hop2)
+ {
+ rv.append(1, '.');
+ bindata.erase(hop2);
+ rv.append(SegmentCloak(bindata, 12, len2));
+ }
+
+ if (full)
+ {
+ rv.append(1, '.');
+ bindata.erase(hop3);
+ rv.append(SegmentCloak(bindata, 13, 6));
+ rv.append(suffix);
+ }
+ else
+ {
+ char buf[50];
+ if (ip.sa.sa_family == AF_INET6)
+ {
+ snprintf(buf, 50, ".%02x%02x.%02x%02x%s",
+ ip.in6.sin6_addr.s6_addr[2], ip.in6.sin6_addr.s6_addr[3],
+ ip.in6.sin6_addr.s6_addr[0], ip.in6.sin6_addr.s6_addr[1], suffix.c_str());
+ }
+ else
+ {
+ const unsigned char* ip4 = (const unsigned char*)&ip.in4.sin_addr;
+ snprintf(buf, 50, ".%d.%d%s", ip4[1], ip4[0], suffix.c_str());
+ }
+ rv.append(buf);
+ }
+ return rv;
+ }
+
+ ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
+ {
+ LocalUser* lu = IS_LOCAL(user);
+ if (!lu)
+ return MOD_RES_PASSTHRU;
+
+ OnUserConnect(lu);
+ std::string* cloak = cu.ext.get(user);
+ /* Check if they have a cloaked host, but are not using it */
+ if (cloak && *cloak != user->dhost)
+ {
+ char cmask[MAXBUF];
+ snprintf(cmask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), cloak->c_str());
+ if (InspIRCd::Match(cmask,mask))
+ return MOD_RES_DENY;
+ }
+ return MOD_RES_PASSTHRU;
+ }
+
+ void Prioritize()
+ {
+ /* Needs to be after m_banexception etc. */
+ ServerInstance->Modules->SetPriority(this, I_OnCheckBan, PRIORITY_LAST);
+ }
+
+ // this unsets umode +x on every host change. If we are actually doing a +x
+ // mode change, we will call SetMode back to true AFTER the host change is done.
+ void OnChangeHost(User* u, const std::string& host)
+ {
+ if(u->IsModeSet('x'))
+ {
+ u->SetMode('x', false);
+ u->WriteServ("MODE %s -x", u->nick.c_str());
+ }
+ }
+
+ ~ModuleCloaking()
+ {
+ }
+
+ Version GetVersion()
+ {
+ std::string testcloak = "broken";
+ if (Hash)
+ {
+ switch (mode)
+ {
+ case MODE_COMPAT_HOST:
+ testcloak = prefix + "-" + Hash->sumIV(compatkey, xtab[0], "*").substr(0,10);
+ break;
+ case MODE_COMPAT_IPONLY:
+ testcloak = Hash->sumIV(compatkey, xtab[0], "*").substr(0,10);
+ break;
+ case MODE_HALF_CLOAK:
+ testcloak = prefix + SegmentCloak("*", 3, 8) + suffix;
+ break;
+ case MODE_OPAQUE:
+ testcloak = prefix + SegmentCloak("*", 4, 8) + suffix;
+ }
+ }
+ return Version("Provides masking of user hostnames", VF_COMMON|VF_VENDOR, testcloak);
+ }
+
+ void OnRehash(User* user)
+ {
+ ConfigTag* tag = ServerInstance->Config->ConfValue("cloak");
+ prefix = tag->getString("prefix");
+ suffix = tag->getString("suffix", ".IP");
+
+ std::string modestr = tag->getString("mode");
+ if (modestr == "compat-host")
+ mode = MODE_COMPAT_HOST;
+ else if (modestr == "compat-ip")
+ mode = MODE_COMPAT_IPONLY;
+ else if (modestr == "half")
+ mode = MODE_HALF_CLOAK;
+ else if (modestr == "full")
+ mode = MODE_OPAQUE;
+ else
+ throw ModuleException("Bad value for <cloak:mode>; must be one of compat-host, compat-ip, half, full");
+
+ if (mode == MODE_COMPAT_HOST || mode == MODE_COMPAT_IPONLY)
+ {
+ bool lowercase = tag->getBool("lowercase");
+
+ /* These are *not* using the need_positive parameter of ReadInteger -
+ * that will limit the valid values to only the positive values in a
+ * signed int. Instead, accept any value that fits into an int and
+ * cast it to an unsigned int. That will, a bit oddly, give us the full
+ * spectrum of an unsigned integer. - Special
+ *
+ * We must limit the keys or else we get different results on
+ * amd64/x86 boxes. - psychon */
+ const unsigned int limit = 0x80000000;
+ compatkey[0] = (unsigned int) tag->getInt("key1");
+ compatkey[1] = (unsigned int) tag->getInt("key2");
+ compatkey[2] = (unsigned int) tag->getInt("key3");
+ compatkey[3] = (unsigned int) tag->getInt("key4");
+
+ if (!lowercase)
+ {
+ xtab[0] = "F92E45D871BCA630";
+ xtab[1] = "A1B9D80C72E653F4";
+ xtab[2] = "1ABC078934DEF562";
+ xtab[3] = "ABCDEF5678901234";
+ }
+ else
+ {
+ xtab[0] = "f92e45d871bca630";
+ xtab[1] = "a1b9d80c72e653f4";
+ xtab[2] = "1abc078934def562";
+ xtab[3] = "abcdef5678901234";
+ }
+
+ if (prefix.empty())
+ prefix = ServerInstance->Config->Network;
+
+ if (!compatkey[0] || !compatkey[1] || !compatkey[2] || !compatkey[3] ||
+ compatkey[0] >= limit || compatkey[1] >= limit || compatkey[2] >= limit || compatkey[3] >= limit)
+ {
+ std::string detail;
+ if (!compatkey[0] || compatkey[0] >= limit)
+ detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
+ else if (!compatkey[1] || compatkey[1] >= limit)
+ detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
+ else if (!compatkey[2] || compatkey[2] >= limit)
+ detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
+ else if (!compatkey[3] || compatkey[3] >= limit)
+ detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
+
+ throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
+ }
+ }
+ else
+ {
+ key = tag->getString("key");
+ if (key.empty() || key == "secret")
+ throw ModuleException("You have not defined cloak keys for m_cloaking. Define <cloak:key> as a network-wide secret.");
+ }
+ }
+
+ std::string GenCloak(const irc::sockets::sockaddrs& ip, const std::string& ipstr, const std::string& host)
+ {
+ std::string chost;
+
+ switch (mode)
+ {
+ case MODE_COMPAT_HOST:
+ {
+ if (ipstr != host)
+ {
+ std::string tail = LastTwoDomainParts(host);
+
+ // xtab is not used here due to a bug in 1.2 cloaking
+ chost = prefix + "-" + Hash->sumIV(compatkey, "0123456789abcdef", host).substr(0,8) + tail;
+
+ /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
+ * according to the DNS RFC) then they get cloaked as an IP.
+ */
+ if (chost.length() <= 64)
+ break;
+ }
+ // fall through to IP cloak
+ }
+ case MODE_COMPAT_IPONLY:
+ if (ip.sa.sa_family == AF_INET6)
+ chost = CompatCloak6(ipstr.c_str());
+ else
+ chost = CompatCloak4(ipstr.c_str());
+ break;
+ case MODE_HALF_CLOAK:
+ {
+ if (ipstr != host)
+ chost = prefix + SegmentCloak(host, 1, 6) + LastTwoDomainParts(host);
+ if (chost.empty() || chost.length() > 50)
+ chost = SegmentIP(ip, false);
+ break;
+ }
+ case MODE_OPAQUE:
+ default:
+ chost = SegmentIP(ip, true);
+ }
+ return chost;
+ }
+
+ void OnUserConnect(LocalUser* dest)
+ {
+ std::string* cloak = cu.ext.get(dest);
+ if (cloak)
+ return;
+
+ cu.ext.set(dest, GenCloak(dest->client_sa, dest->GetIPString(), dest->host));
+ }
+};
+
+CmdResult CommandCloak::Handle(const std::vector<std::string> ¶meters, User *user)