1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Provides masking of user hostnames */
18 /* $ModDep: m_hash.h */
20 /** Handles user mode +x
22 class CloakUser : public ModeHandler
35 /** This function takes a domain name string and returns just the last two domain parts,
36 * or the last domain part if only two are available. Failing that it just returns what it was given.
38 * For example, if it is passed "svn.inspircd.org" it will return ".inspircd.org".
39 * If it is passed "brainbox.winbot.co.uk" it will return ".co.uk",
40 * and if it is passed "localhost.localdomain" it will return ".localdomain".
42 * This is used to ensure a significant part of the host is always cloaked (see Bug #216)
44 std::string LastTwoDomainParts(const std::string &host)
47 std::string::size_type splitdot = host.length();
49 for (std::string::size_type x = host.length() - 1; x; --x)
60 if (splitdot == host.length())
63 return host.substr(splitdot);
66 CloakUser(InspIRCd* Instance, Module* source, Module* Hash) : ModeHandler(Instance, 'x', 0, 0, false, MODETYPE_USER, false), Sender(source), HashProvider(Hash)
70 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
72 /* For remote clients, we dont take any action, we just allow it.
73 * The local server where they are will set their cloak instead.
74 * This is fine, as we will recieve it later.
78 dest->SetMode('x',adding);
79 return MODEACTION_ALLOW;
82 /* don't allow this user to spam modechanges */
83 dest->IncreasePenalty(5);
87 if(!dest->IsModeSet('x'))
89 /* The mode is being turned on - so attempt to
90 * allocate the user a cloaked host using a non-reversible
91 * algorithm (its simple, but its non-reversible so the
92 * simplicity doesnt really matter). This algorithm
93 * will not work if the user has only one level of domain
94 * naming in their hostname (e.g. if they are on a lan or
95 * are connecting via localhost) -- this doesnt matter much.
100 if (!dest->GetExt("cloaked_host", cloak))
102 /* Force creation of missing cloak */
103 Sender->OnUserConnect(dest);
105 if (dest->GetExt("cloaked_host", cloak))
107 dest->ChangeDisplayedHost(cloak->c_str());
108 dest->SetMode('x',true);
109 return MODEACTION_ALLOW;
115 if (dest->IsModeSet('x'))
117 /* User is removing the mode, so just restore their real host
118 * and make it match the displayed one.
120 dest->ChangeDisplayedHost(dest->host.c_str());
121 dest->SetMode('x',false);
122 return MODEACTION_ALLOW;
126 return MODEACTION_DENY;
129 std::string Cloak4(const char* ip)
131 unsigned int iv[] = { key1, key2, key3, key4 };
132 irc::sepstream seps(ip, '.');
134 std::string octet[4];
137 for (int j = 0; j < 4; j++)
139 seps.GetToken(octet[j]);
140 i[j] = atoi(octet[j].c_str());
143 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
144 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
145 octet[1] = octet[0] + "." + octet[1];
147 /* Reset the Hash module and send it our IV */
148 HashResetRequest(Sender, HashProvider).Send();
149 HashKeyRequest(Sender, HashProvider, iv).Send();
151 /* Send the Hash module a different hex table for each octet group's Hash sum */
152 for (int k = 0; k < 4; k++)
154 HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
155 ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
157 /* Stick them all together */
158 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
161 std::string Cloak6(const char* ip)
163 unsigned int iv[] = { key1, key2, key3, key4 };
164 std::vector<std::string> hashies;
168 /* Reset the Hash module and send it our IV */
169 HashResetRequest(Sender, HashProvider).Send();
170 HashKeyRequest(Sender, HashProvider, iv).Send();
172 for (const char* input = ip; *input; input++)
175 if (item.length() > 7)
177 /* Send the Hash module a different hex table for each octet group's Hash sum */
178 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
179 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
186 /* Send the Hash module a different hex table for each octet group's Hash sum */
187 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
188 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
191 /* Stick them all together */
192 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
197 ConfigReader Conf(ServerInstance);
200 /* These are *not* using the need_positive parameter of ReadInteger -
201 * that will limit the valid values to only the positive values in a
202 * signed int. Instead, accept any value that fits into an int and
203 * cast it to an unsigned int. That will, a bit oddly, give us the full
204 * spectrum of an unsigned integer. - Special */
205 key1 = key2 = key3 = key4 = 0;
206 key1 = (unsigned int) Conf.ReadInteger("cloak","key1",0,false);
207 key2 = (unsigned int) Conf.ReadInteger("cloak","key2",0,false);
208 key3 = (unsigned int) Conf.ReadInteger("cloak","key3",0,false);
209 key4 = (unsigned int) Conf.ReadInteger("cloak","key4",0,false);
210 prefix = Conf.ReadValue("cloak","prefix",0);
211 ipalways = Conf.ReadFlag("cloak", "ipalways", 0);
212 lowercase = Conf.ReadFlag("cloak", "lowercase", 0);
216 xtab[0] = "F92E45D871BCA630";
217 xtab[1] = "A1B9D80C72E653F4";
218 xtab[2] = "1ABC078934DEF562";
219 xtab[3] = "ABCDEF5678901234";
223 xtab[0] = "f92e45d871bca630";
224 xtab[1] = "a1b9d80c72e653f4";
225 xtab[2] = "1abc078934def562";
226 xtab[3] = "abcdef5678901234";
230 prefix = ServerInstance->Config->Network;
232 if (!key1 || !key2 || !key3 || !key4)
236 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
238 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
240 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
242 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
244 throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
250 class ModuleCloaking : public Module
258 ModuleCloaking(InspIRCd* Me)
261 /* Attempt to locate the md5 service provider, bail if we can't find it */
262 HashModule = ServerInstance->Modules->Find("m_md5.so");
264 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
266 cu = new CloakUser(ServerInstance, this, HashModule);
272 catch (ModuleException &e)
278 /* Register it with the core */
279 if (!ServerInstance->Modes->AddMode(cu))
282 throw ModuleException("Could not add new modes!");
285 ServerInstance->Modules->UseInterface("HashRequest");
287 Implementation eventlist[] = { I_OnRehash, I_OnUserDisconnect, I_OnCleanup, I_OnCheckBan, I_OnUserConnect, I_OnSyncUserMetaData, I_OnCleanup };
288 ServerInstance->Modules->Attach(eventlist, this, 6);
290 CloakExistingUsers();
293 void OnSyncUserMetaData(User* user, Module* proto,void* opaque, const std::string &extname, bool displayable)
295 if ((displayable) && (extname == "cloaked_host"))
298 if (user->GetExt("cloaked_host", cloak))
299 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *cloak);
303 void CloakExistingUsers()
306 for (std::vector<User*>::iterator u = ServerInstance->Users->local_users.begin(); u != ServerInstance->Users->local_users.end(); u++)
308 if (!(*u)->GetExt("cloaked_host", cloak))
315 virtual int OnCheckBan(User* user, Channel* chan)
319 /* Check if they have a cloaked host, but are not using it */
320 if (user->GetExt("cloaked_host", tofree) && *tofree != user->dhost)
322 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), tofree->c_str());
323 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
325 if (InspIRCd::Match(mask,i->data))
334 /* Needs to be after m_banexception etc. */
335 ServerInstance->Modules->SetPriority(this, I_OnCheckBan, PRIO_LAST);
337 /* but before m_conn_umodes, so host is generated ready to apply */
338 Module *um = ServerInstance->Modules->Find("m_conn_umodes.so");
339 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_AFTER, &um);
342 virtual void OnUserDisconnect(User* user)
345 if (user->GetExt("cloaked_host", tofree))
348 user->Shrink("cloaked_host");
352 virtual void OnCleanup(int target_type, void* item)
354 if (target_type == TYPE_USER)
355 OnUserDisconnect((User*)item);
358 virtual ~ModuleCloaking()
360 ServerInstance->Modes->DelMode(cu);
362 ServerInstance->Modules->DoneWithInterface("HashRequest");
365 virtual Version GetVersion()
367 // returns the version number of the module to be
368 // listed in /MODULES
369 return Version("$Id$", VF_COMMON|VF_VENDOR,API_VERSION);
372 virtual void OnRehash(User* user, const std::string ¶meter)
377 virtual void OnUserConnect(User* dest)
380 if (dest->GetExt("cloaked_host", tofree))
383 if (dest->host.find('.') != std::string::npos || dest->host.find(':') != std::string::npos)
385 unsigned int iv[] = { cu->key1, cu->key2, cu->key3, cu->key4 };
386 std::string a = cu->LastTwoDomainParts(dest->host);
389 /* InspIRCd users have two hostnames; A displayed
390 * hostname which can be modified by modules (e.g.
391 * to create vhosts, implement chghost, etc) and a
392 * 'real' hostname which you shouldnt write to.
395 /* 2008/08/18: add <cloak:ipalways> which always cloaks
396 * the IP, for anonymity. --nenolod
400 /** Reset the Hash module, and send it our IV and hex table */
401 HashResetRequest(this, cu->HashProvider).Send();
402 HashKeyRequest(this, cu->HashProvider, iv).Send();
403 HashHexRequest(this, cu->HashProvider, cu->xtab[(dest->host[0]) % 4]);
405 /* Generate a cloak using specialized Hash */
406 std::string hostcloak = cu->prefix + "-" + std::string(HashSumRequest(this, cu->HashProvider, dest->host.c_str()).Send()).substr(0,8) + a;
408 /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
409 * according to the DNS RFC) then tough titty, they get cloaked as an IP.
410 * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
416 if ((dest->GetProtocolFamily() == AF_INET6) && (inet_pton(AF_INET6,dest->host.c_str(),&testaddr) < 1) && (hostcloak.length() <= 64))
417 /* Invalid ipv6 address, and ipv6 user (resolved host) */
419 else if ((dest->GetProtocolFamily() == AF_INET) && (inet_aton(dest->host.c_str(),&testaddr2) < 1) && (hostcloak.length() <= 64))
420 /* Invalid ipv4 address, and ipv4 user (resolved host) */
423 /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
424 b = ((!strchr(dest->host.c_str(),':')) ? cu->Cloak4(dest->host.c_str()) : cu->Cloak6(dest->host.c_str()));
427 if ((inet_aton(dest->host.c_str(),&testaddr) < 1) && (hostcloak.length() <= 64))
428 /* Invalid ipv4 address, and ipv4 user (resolved host) */
431 /* Valid ipv4 address (not resolved) ipv4 user */
432 b = cu->Cloak4(dest->host.c_str());
438 if (dest->GetProtocolFamily() == AF_INET6)
439 b = cu->Cloak6(dest->GetIPString());
441 if (dest->GetProtocolFamily() == AF_INET)
442 b = cu->Cloak4(dest->GetIPString());
445 dest->Extend("cloaked_host", new std::string(b));
451 MODULE_INIT(ModuleCloaking)