1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
18 /* $ModDesc: Provides masking of user hostnames */
19 /* $ModDep: m_hash.h */
21 /** Handles user mode +x
23 class CloakUser : public ModeHandler
36 /** This function takes a domain name string and returns just the last two domain parts,
37 * or the last domain part if only two are available. Failing that it just returns what it was given.
39 * For example, if it is passed "svn.inspircd.org" it will return ".inspircd.org".
40 * If it is passed "brainbox.winbot.co.uk" it will return ".co.uk",
41 * and if it is passed "localhost.localdomain" it will return ".localdomain".
43 * This is used to ensure a significant part of the host is always cloaked (see Bug #216)
45 std::string LastTwoDomainParts(const std::string &host)
48 std::string::size_type splitdot = host.length();
50 for (std::string::size_type x = host.length() - 1; x; --x)
61 if (splitdot == host.length())
64 return host.substr(splitdot);
67 CloakUser(InspIRCd* Instance, Module* source, Module* Hash) : ModeHandler(Instance, 'x', 0, 0, false, MODETYPE_USER, false), Sender(source), HashProvider(Hash)
71 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
74 return MODEACTION_DENY;
76 /* For remote clients, we dont take any action, we just allow it.
77 * The local server where they are will set their cloak instead.
78 * This is fine, as we will recieve it later.
82 dest->SetMode('x',adding);
83 return MODEACTION_ALLOW;
86 /* don't allow this user to spam modechanges */
87 dest->IncreasePenalty(5);
91 if(!dest->IsModeSet('x'))
93 /* The mode is being turned on - so attempt to
94 * allocate the user a cloaked host using a non-reversible
95 * algorithm (its simple, but its non-reversible so the
96 * simplicity doesnt really matter). This algorithm
97 * will not work if the user has only one level of domain
98 * naming in their hostname (e.g. if they are on a lan or
99 * are connecting via localhost) -- this doesnt matter much.
104 if (dest->GetExt("cloaked_host", cloak))
106 /* Cloaked host has been set before on this user, don't bother to recalculate and waste cpu */
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);
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 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
164 if (!strncmp(ip, "0::ffff:", 8))
165 return Cloak4(ip + 8);
167 /* If we get here, yes it really is an ipv6 ip */
168 unsigned int iv[] = { key1, key2, key3, key4 };
169 std::vector<std::string> hashies;
173 /* Reset the Hash module and send it our IV */
174 HashResetRequest(Sender, HashProvider).Send();
175 HashKeyRequest(Sender, HashProvider, iv).Send();
177 for (const char* input = ip; *input; input++)
180 if (item.length() > 7)
182 /* Send the Hash module a different hex table for each octet group's Hash sum */
183 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
184 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
191 /* Send the Hash module a different hex table for each octet group's Hash sum */
192 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
193 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
196 /* Stick them all together */
197 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
202 ConfigReader Conf(ServerInstance);
205 /* These are *not* using the need_positive parameter of ReadInteger -
206 * that will limit the valid values to only the positive values in a
207 * signed int. Instead, accept any value that fits into an int and
208 * cast it to an unsigned int. That will, a bit oddly, give us the full
209 * spectrum of an unsigned integer. - Special */
210 key1 = key2 = key3 = key4 = 0;
211 key1 = (unsigned int) Conf.ReadInteger("cloak","key1",0,false);
212 key2 = (unsigned int) Conf.ReadInteger("cloak","key2",0,false);
213 key3 = (unsigned int) Conf.ReadInteger("cloak","key3",0,false);
214 key4 = (unsigned int) Conf.ReadInteger("cloak","key4",0,false);
215 prefix = Conf.ReadValue("cloak","prefix",0);
216 ipalways = Conf.ReadFlag("cloak", "ipalways", 0);
217 lowercase = Conf.ReadFlag("cloak", "lowercase", 0);
221 xtab[0] = "F92E45D871BCA630";
222 xtab[1] = "A1B9D80C72E653F4";
223 xtab[2] = "1ABC078934DEF562";
224 xtab[3] = "ABCDEF5678901234";
228 xtab[0] = "f92e45d871bca630";
229 xtab[1] = "a1b9d80c72e653f4";
230 xtab[2] = "1abc078934def562";
231 xtab[3] = "abcdef5678901234";
235 prefix = ServerInstance->Config->Network;
237 if (!key1 || !key2 || !key3 || !key4)
241 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
243 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
245 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
247 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
249 throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
255 class ModuleCloaking : public Module
263 ModuleCloaking(InspIRCd* Me)
266 /* Attempt to locate the md5 service provider, bail if we can't find it */
267 HashModule = ServerInstance->Modules->Find("m_md5.so");
269 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
271 cu = new CloakUser(ServerInstance, this, HashModule);
277 catch (ModuleException &e)
283 /* Register it with the core */
284 if (!ServerInstance->Modes->AddMode(cu))
287 throw ModuleException("Could not add new modes!");
290 ServerInstance->Modules->UseInterface("HashRequest");
292 Implementation eventlist[] = { I_OnRehash, I_OnUserDisconnect, I_OnCleanup, I_OnCheckBan, I_OnUserConnect, I_OnSyncUserMetaData };
293 ServerInstance->Modules->Attach(eventlist, this, 6);
296 void OnSyncUserMetaData(User* user, Module* proto,void* opaque, const std::string &extname, bool displayable)
298 if ((displayable) && (extname == "cloaked_host"))
301 if (user->GetExt("cloaked_host", cloak))
302 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *cloak);
307 virtual int OnCheckBan(User* user, Channel* chan)
311 /* Check if they have a cloaked host, but are not using it */
312 if (user->GetExt("cloaked_host", tofree) && *tofree != user->dhost)
314 snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, tofree->c_str());
315 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
317 if (match(mask,i->data))
326 /* Needs to be after m_banexception etc. */
327 ServerInstance->Modules->SetPriority(this, I_OnCheckBan, PRIO_LAST);
330 virtual void OnUserDisconnect(User* user)
333 if (user->GetExt("cloaked_host", tofree))
337 virtual void OnCleanup(int target_type, void* item)
339 if (target_type == TYPE_USER)
340 OnUserDisconnect((User*)item);
343 virtual ~ModuleCloaking()
345 ServerInstance->Modes->DelMode(cu);
347 ServerInstance->Modules->DoneWithInterface("HashRequest");
350 virtual Version GetVersion()
352 // returns the version number of the module to be
353 // listed in /MODULES
354 return Version(1,2,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
357 virtual void OnRehash(User* user, const std::string ¶meter)
362 virtual void OnUserConnect(User* dest)
364 char* n1 = strchr(dest->host,'.');
365 char* n2 = strchr(dest->host,':');
369 unsigned int iv[] = { cu->key1, cu->key2, cu->key3, cu->key4 };
370 std::string a = cu->LastTwoDomainParts(dest->host);
373 /* InspIRCd users have two hostnames; A displayed
374 * hostname which can be modified by modules (e.g.
375 * to create vhosts, implement chghost, etc) and a
376 * 'real' hostname which you shouldnt write to.
379 /* 2008/08/18: add <cloak:ipalways> which always cloaks
380 * the IP, for anonymity. --nenolod
384 /** Reset the Hash module, and send it our IV and hex table */
385 HashResetRequest(this, cu->HashProvider).Send();
386 HashKeyRequest(this, cu->HashProvider, iv).Send();
387 HashHexRequest(this, cu->HashProvider, cu->xtab[(*dest->host) % 4]);
389 /* Generate a cloak using specialized Hash */
390 std::string hostcloak = cu->prefix + "-" + std::string(HashSumRequest(this, cu->HashProvider, dest->host).Send()).substr(0,8) + a;
392 /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
393 * according to the DNS RFC) then tough titty, they get cloaked as an IP.
394 * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
400 if ((dest->GetProtocolFamily() == AF_INET6) && (inet_pton(AF_INET6,dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
401 /* Invalid ipv6 address, and ipv6 user (resolved host) */
403 else if ((dest->GetProtocolFamily() == AF_INET) && (inet_aton(dest->host,&testaddr2) < 1) && (hostcloak.length() <= 64))
404 /* Invalid ipv4 address, and ipv4 user (resolved host) */
407 /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
408 b = ((!strchr(dest->host,':')) ? cu->Cloak4(dest->host) : cu->Cloak6(dest->host));
411 if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
412 /* Invalid ipv4 address, and ipv4 user (resolved host) */
415 /* Valid ipv4 address (not resolved) ipv4 user */
416 b = cu->Cloak4(dest->host);
422 if (dest->GetProtocolFamily() == AF_INET6)
423 b = cu->Cloak6(dest->GetIPString());
425 if (dest->GetProtocolFamily() == AF_INET)
426 b = cu->Cloak4(dest->GetIPString());
429 dest->Extend("cloaked_host", new std::string(b));
435 MODULE_INIT(ModuleCloaking)