1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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)
67 : ModeHandler(source, 'x', PARAM_NONE, MODETYPE_USER), HashProvider(Hash),
68 ext("cloaked_host", source)
72 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
74 /* For remote clients, we dont take any action, we just allow it.
75 * The local server where they are will set their cloak instead.
76 * This is fine, as we will recieve it later.
80 dest->SetMode('x',adding);
81 return MODEACTION_ALLOW;
84 /* don't allow this user to spam modechanges */
85 dest->IncreasePenalty(5);
89 if(!dest->IsModeSet('x'))
91 /* The mode is being turned on - so attempt to
92 * allocate the user a cloaked host using a non-reversible
93 * algorithm (its simple, but its non-reversible so the
94 * simplicity doesnt really matter). This algorithm
95 * will not work if the user has only one level of domain
96 * naming in their hostname (e.g. if they are on a lan or
97 * are connecting via localhost) -- this doesnt matter much.
100 std::string* cloak = ext.get(dest);
104 /* Force creation of missing cloak */
105 creator->OnUserConnect(dest);
106 cloak = ext.get(dest);
110 dest->ChangeDisplayedHost(cloak->c_str());
111 dest->SetMode('x',true);
112 return MODEACTION_ALLOW;
118 if (dest->IsModeSet('x'))
120 /* User is removing the mode, so just restore their real host
121 * and make it match the displayed one.
123 dest->ChangeDisplayedHost(dest->host.c_str());
124 dest->SetMode('x',false);
125 return MODEACTION_ALLOW;
129 return MODEACTION_DENY;
132 std::string Cloak4(const char* ip)
134 unsigned int iv[] = { key1, key2, key3, key4 };
135 irc::sepstream seps(ip, '.');
137 std::string octet[4];
140 for (int j = 0; j < 4; j++)
142 seps.GetToken(octet[j]);
143 i[j] = atoi(octet[j].c_str());
146 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
147 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
148 octet[1] = octet[0] + "." + octet[1];
150 /* Reset the Hash module and send it our IV */
151 HashResetRequest(creator, HashProvider).Send();
152 HashKeyRequest(creator, HashProvider, iv).Send();
154 /* Send the Hash module a different hex table for each octet group's Hash sum */
155 for (int k = 0; k < 4; k++)
157 HashHexRequest(creator, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
158 ra[k] = std::string(HashSumRequest(creator, HashProvider, octet[k]).Send()).substr(0,6);
160 /* Stick them all together */
161 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
164 std::string Cloak6(const char* ip)
166 unsigned int iv[] = { key1, key2, key3, key4 };
167 std::vector<std::string> hashies;
171 /* Reset the Hash module and send it our IV */
172 HashResetRequest(creator, HashProvider).Send();
173 HashKeyRequest(creator, HashProvider, iv).Send();
175 for (const char* input = ip; *input; input++)
178 if (item.length() > 7)
180 /* Send the Hash module a different hex table for each octet group's Hash sum */
181 HashHexRequest(creator, HashProvider, xtab[(key1+rounds) % 4]).Send();
182 hashies.push_back(std::string(HashSumRequest(creator, HashProvider, item).Send()).substr(0,8));
189 /* Send the Hash module a different hex table for each octet group's Hash sum */
190 HashHexRequest(creator, HashProvider, xtab[(key1+rounds) % 4]).Send();
191 hashies.push_back(std::string(HashSumRequest(creator, HashProvider, item).Send()).substr(0,8));
194 /* Stick them all together */
195 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
200 ConfigReader Conf(ServerInstance);
203 /* These are *not* using the need_positive parameter of ReadInteger -
204 * that will limit the valid values to only the positive values in a
205 * signed int. Instead, accept any value that fits into an int and
206 * cast it to an unsigned int. That will, a bit oddly, give us the full
207 * spectrum of an unsigned integer. - Special */
208 key1 = key2 = key3 = key4 = 0;
209 key1 = (unsigned int) Conf.ReadInteger("cloak","key1",0,false);
210 key2 = (unsigned int) Conf.ReadInteger("cloak","key2",0,false);
211 key3 = (unsigned int) Conf.ReadInteger("cloak","key3",0,false);
212 key4 = (unsigned int) Conf.ReadInteger("cloak","key4",0,false);
213 prefix = Conf.ReadValue("cloak","prefix",0);
214 ipalways = Conf.ReadFlag("cloak", "ipalways", 0);
215 lowercase = Conf.ReadFlag("cloak", "lowercase", 0);
219 xtab[0] = "F92E45D871BCA630";
220 xtab[1] = "A1B9D80C72E653F4";
221 xtab[2] = "1ABC078934DEF562";
222 xtab[3] = "ABCDEF5678901234";
226 xtab[0] = "f92e45d871bca630";
227 xtab[1] = "a1b9d80c72e653f4";
228 xtab[2] = "1abc078934def562";
229 xtab[3] = "abcdef5678901234";
233 prefix = ServerInstance->Config->Network;
235 if (!key1 || !key2 || !key3 || !key4)
239 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
241 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
243 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
245 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
247 throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
253 class ModuleCloaking : public Module
259 ModuleCloaking(InspIRCd* Me)
262 /* Attempt to locate the md5 service provider, bail if we can't find it */
263 Module* HashModule = ServerInstance->Modules->Find("m_md5.so");
265 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
267 cu = new CloakUser(ServerInstance, this, HashModule);
273 catch (ModuleException &e)
279 /* Register it with the core */
280 if (!ServerInstance->Modes->AddMode(cu))
283 throw ModuleException("Could not add new modes!");
286 ServerInstance->Modules->UseInterface("HashRequest");
287 Extensible::Register(&cu->ext);
289 Implementation eventlist[] = { I_OnRehash, I_OnCheckBan, I_OnUserConnect };
290 ServerInstance->Modules->Attach(eventlist, this, 3);
292 CloakExistingUsers();
295 void CloakExistingUsers()
298 for (std::vector<User*>::iterator u = ServerInstance->Users->local_users.begin(); u != ServerInstance->Users->local_users.end(); u++)
300 cloak = cu->ext.get(*u);
308 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
311 std::string* cloak = cu->ext.get(user);
312 /* Check if they have a cloaked host, but are not using it */
313 if (cloak && *cloak != user->dhost)
315 snprintf(cmask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), cloak->c_str());
316 if (InspIRCd::Match(cmask,mask))
319 return MOD_RES_PASSTHRU;
324 /* Needs to be after m_banexception etc. */
325 ServerInstance->Modules->SetPriority(this, I_OnCheckBan, PRIORITY_LAST);
327 /* but before m_conn_umodes, so host is generated ready to apply */
328 Module *um = ServerInstance->Modules->Find("m_conn_umodes.so");
329 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_AFTER, &um);
334 ServerInstance->Modes->DelMode(cu);
336 ServerInstance->Modules->DoneWithInterface("HashRequest");
341 // returns the version number of the module to be
342 // listed in /MODULES
343 return Version("$Id$", VF_COMMON|VF_VENDOR,API_VERSION);
346 void OnRehash(User* user)
351 void OnUserConnect(User* dest)
353 std::string* cloak = cu->ext.get(dest);
357 if (dest->host.find('.') != std::string::npos || dest->host.find(':') != std::string::npos)
359 unsigned int iv[] = { cu->key1, cu->key2, cu->key3, cu->key4 };
360 std::string a = cu->LastTwoDomainParts(dest->host);
363 /* InspIRCd users have two hostnames; A displayed
364 * hostname which can be modified by modules (e.g.
365 * to create vhosts, implement chghost, etc) and a
366 * 'real' hostname which you shouldnt write to.
369 /* 2008/08/18: add <cloak:ipalways> which always cloaks
370 * the IP, for anonymity. --nenolod
374 /** Reset the Hash module, and send it our IV and hex table */
375 HashResetRequest(this, cu->HashProvider).Send();
376 HashKeyRequest(this, cu->HashProvider, iv).Send();
377 HashHexRequest(this, cu->HashProvider, cu->xtab[(dest->host[0]) % 4]);
379 /* Generate a cloak using specialized Hash */
380 std::string hostcloak = cu->prefix + "-" + std::string(HashSumRequest(this, cu->HashProvider, dest->host.c_str()).Send()).substr(0,8) + a;
382 /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
383 * according to the DNS RFC) then tough titty, they get cloaked as an IP.
384 * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
387 std::string testaddr;
389 if (!irc::sockets::satoap(&dest->client_sa, testaddr, testport) && (hostcloak.length() <= 64))
390 /* not a valid address, must have been a host, so cloak as a host */
392 else if (dest->client_sa.sa.sa_family == AF_INET6)
393 b = cu->Cloak6(dest->GetIPString());
395 b = cu->Cloak4(dest->GetIPString());
399 if (dest->client_sa.sa.sa_family == AF_INET6)
400 b = cu->Cloak6(dest->GetIPString());
402 b = cu->Cloak4(dest->GetIPString());
411 MODULE_INIT(ModuleCloaking)