2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7 * Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/hash.h"
29 /* $ModDesc: Provides masking of user hostnames */
33 /** 2.0 cloak of "half" of the hostname plus the full IP hash */
35 /** 2.0 cloak of IP hash, split at 2 common CIDR range points */
39 // lowercase-only encoding similar to base64, used for hash output
40 static const char base32[] = "0123456789abcdefghijklmnopqrstuv";
42 /** Handles user mode +x
44 class CloakUser : public ModeHandler
48 std::string debounce_uid;
52 CloakUser(Module* source)
53 : ModeHandler(source, "cloak", 'x', PARAM_NONE, MODETYPE_USER),
54 ext("cloaked_host", source), debounce_ts(0), debounce_count(0)
58 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
60 LocalUser* user = IS_LOCAL(dest);
62 /* For remote clients, we don't take any action, we just allow it.
63 * The local server where they are will set their cloak instead.
64 * This is fine, as we will receive it later.
68 dest->SetMode('x',adding);
69 return MODEACTION_ALLOW;
72 if (user->uuid == debounce_uid && debounce_ts == ServerInstance->Time())
74 // prevent spamming using /mode user +x-x+x-x+x-x
75 if (++debounce_count > 2)
76 return MODEACTION_DENY;
80 debounce_uid = user->uuid;
82 debounce_ts = ServerInstance->Time();
85 if (adding == user->IsModeSet('x'))
86 return MODEACTION_DENY;
88 /* don't allow this user to spam modechanges */
90 user->CommandFloodPenalty += 5000;
94 std::string* cloak = ext.get(user);
98 /* Force creation of missing cloak */
99 creator->OnUserConnect(user);
100 cloak = ext.get(user);
104 user->ChangeDisplayedHost(cloak->c_str());
105 user->SetMode('x',true);
106 return MODEACTION_ALLOW;
109 return MODEACTION_DENY;
113 /* User is removing the mode, so restore their real host
114 * and make it match the displayed one.
116 user->SetMode('x',false);
117 user->ChangeDisplayedHost(user->host.c_str());
118 return MODEACTION_ALLOW;
123 class CommandCloak : public Command
126 CommandCloak(Module* Creator) : Command(Creator, "CLOAK", 1)
132 CmdResult Handle(const std::vector<std::string> ¶meters, User *user);
135 class ModuleCloaking : public Module
145 dynamic_reference<HashProvider> Hash;
147 ModuleCloaking() : cu(this), mode(MODE_OPAQUE), ck(this), Hash(this, "hash/md5")
151 void init() CXX11_OVERRIDE
155 ServerInstance->Modules->AddService(cu);
156 ServerInstance->Modules->AddService(ck);
157 ServerInstance->Modules->AddService(cu.ext);
159 Implementation eventlist[] = { I_OnRehash, I_OnCheckBan, I_OnUserConnect, I_OnChangeHost };
160 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
163 /** This function takes a domain name string and returns just the last two domain parts,
164 * or the last domain part if only two are available. Failing that it just returns what it was given.
166 * For example, if it is passed "svn.inspircd.org" it will return ".inspircd.org".
167 * If it is passed "brainbox.winbot.co.uk" it will return ".co.uk",
168 * and if it is passed "localhost.localdomain" it will return ".localdomain".
170 * This is used to ensure a significant part of the host is always cloaked (see Bug #216)
172 std::string LastTwoDomainParts(const std::string &host)
175 std::string::size_type splitdot = host.length();
177 for (std::string::size_type x = host.length() - 1; x; --x)
188 if (splitdot == host.length())
191 return host.substr(splitdot);
195 * 2.0-style cloaking function
196 * @param item The item to cloak (part of an IP or hostname)
197 * @param id A unique ID for this type of item (to make it unique if the item matches)
198 * @param len The length of the output. Maximum for MD5 is 16 characters.
200 std::string SegmentCloak(const std::string& item, char id, int len)
203 input.reserve(key.length() + 3 + item.length());
206 input.append(1, '\0'); // null does not terminate a C++ string
209 std::string rv = Hash->sum(input).substr(0,len);
210 for(int i=0; i < len; i++)
212 // this discards 3 bits per byte. We have an
213 // overabundance of bits in the hash output, doesn't
214 // matter which ones we are discarding.
215 rv[i] = base32[rv[i] & 0x1F];
220 std::string SegmentIP(const irc::sockets::sockaddrs& ip, bool full)
223 int hop1, hop2, hop3;
226 if (ip.sa.sa_family == AF_INET6)
228 bindata = std::string((const char*)ip.in6.sin6_addr.s6_addr, 16);
234 // pfx s1.s2.s3. (xxxx.xxxx or s4) sfx
236 rv.reserve(prefix.length() + 26 + suffix.length());
240 bindata = std::string((const char*)&ip.in4.sin_addr, 4);
245 // pfx s1.s2. (xxx.xxx or s3) sfx
246 rv.reserve(prefix.length() + 15 + suffix.length());
250 rv.append(SegmentCloak(bindata, 10, len1));
253 rv.append(SegmentCloak(bindata, 11, len2));
258 rv.append(SegmentCloak(bindata, 12, len2));
265 rv.append(SegmentCloak(bindata, 13, 6));
271 if (ip.sa.sa_family == AF_INET6)
273 snprintf(buf, 50, ".%02x%02x.%02x%02x%s",
274 ip.in6.sin6_addr.s6_addr[2], ip.in6.sin6_addr.s6_addr[3],
275 ip.in6.sin6_addr.s6_addr[0], ip.in6.sin6_addr.s6_addr[1], suffix.c_str());
279 const unsigned char* ip4 = (const unsigned char*)&ip.in4.sin_addr;
280 snprintf(buf, 50, ".%d.%d%s", ip4[1], ip4[0], suffix.c_str());
287 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask) CXX11_OVERRIDE
289 LocalUser* lu = IS_LOCAL(user);
291 return MOD_RES_PASSTHRU;
294 std::string* cloak = cu.ext.get(user);
295 /* Check if they have a cloaked host, but are not using it */
296 if (cloak && *cloak != user->dhost)
298 const std::string cloakMask = user->nick + "!" + user->ident + "@" + *cloak;
299 if (InspIRCd::Match(cloakMask, mask))
302 return MOD_RES_PASSTHRU;
307 /* Needs to be after m_banexception etc. */
308 ServerInstance->Modules->SetPriority(this, I_OnCheckBan, PRIORITY_LAST);
311 // this unsets umode +x on every host change. If we are actually doing a +x
312 // mode change, we will call SetMode back to true AFTER the host change is done.
313 void OnChangeHost(User* u, const std::string& host) CXX11_OVERRIDE
315 if(u->IsModeSet('x'))
317 u->SetMode('x', false);
318 u->WriteServ("MODE %s -x", u->nick.c_str());
322 Version GetVersion() CXX11_OVERRIDE
324 std::string testcloak = "broken";
329 case MODE_HALF_CLOAK:
330 testcloak = prefix + SegmentCloak("*", 3, 8) + suffix;
333 testcloak = prefix + SegmentCloak("*", 4, 8) + suffix;
336 return Version("Provides masking of user hostnames", VF_COMMON|VF_VENDOR, testcloak);
339 void OnRehash(User* user) CXX11_OVERRIDE
341 ConfigTag* tag = ServerInstance->Config->ConfValue("cloak");
342 prefix = tag->getString("prefix");
343 suffix = tag->getString("suffix", ".IP");
345 std::string modestr = tag->getString("mode");
346 if (modestr == "half")
347 mode = MODE_HALF_CLOAK;
348 else if (modestr == "full")
351 throw ModuleException("Bad value for <cloak:mode>; must be half or full");
353 key = tag->getString("key");
354 if (key.empty() || key == "secret")
355 throw ModuleException("You have not defined cloak keys for m_cloaking. Define <cloak:key> as a network-wide secret.");
358 std::string GenCloak(const irc::sockets::sockaddrs& ip, const std::string& ipstr, const std::string& host)
364 case MODE_HALF_CLOAK:
367 chost = prefix + SegmentCloak(host, 1, 6) + LastTwoDomainParts(host);
368 if (chost.empty() || chost.length() > 50)
369 chost = SegmentIP(ip, false);
374 chost = SegmentIP(ip, true);
379 void OnUserConnect(LocalUser* dest) CXX11_OVERRIDE
381 std::string* cloak = cu.ext.get(dest);
385 cu.ext.set(dest, GenCloak(dest->client_sa, dest->GetIPString(), dest->host));
389 CmdResult CommandCloak::Handle(const std::vector<std::string> ¶meters, User *user)
391 ModuleCloaking* mod = (ModuleCloaking*)(Module*)creator;
392 irc::sockets::sockaddrs sa;
395 if (irc::sockets::aptosa(parameters[0], 0, sa))
396 cloak = mod->GenCloak(sa, parameters[0], parameters[0]);
398 cloak = mod->GenCloak(sa, "", parameters[0]);
400 user->WriteNotice("*** Cloak for " + parameters[0] + " is " + cloak);
405 MODULE_INIT(ModuleCloaking)