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 * ---------------------------------------------------
17 /* $ModDesc: Provides masking of user hostnames */
18 /* $ModDep: m_hash.h */
20 /** Handles user mode +x
22 class CloakUser : public ModeHandler
34 /** This function takes a domain name string and returns just the last two domain parts,
35 * or the last domain part if only two are available. Failing that it just returns what it was given.
37 * For example, if it is passed "svn.inspircd.org" it will return ".inspircd.org".
38 * If it is passed "brainbox.winbot.co.uk" it will return ".co.uk",
39 * and if it is passed "localhost.localdomain" it will return ".localdomain".
41 * This is used to ensure a significant part of the host is always cloaked (see Bug #216)
43 std::string LastTwoDomainParts(const std::string &host)
46 std::string::size_type splitdot = host.length();
48 for (std::string::size_type x = host.length() - 1; x; --x)
59 if (splitdot == host.length())
62 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)
73 return MODEACTION_DENY;
75 /* For remote clients, we dont take any action, we just allow it.
76 * The local server where they are will set their cloak instead.
79 return MODEACTION_ALLOW;
81 /* don't allow this user to spam modechanges */
82 dest->IncreasePenalty(5);
86 if(!dest->IsModeSet('x'))
88 /* The mode is being turned on - so attempt to
89 * allocate the user a cloaked host using a non-reversible
90 * algorithm (its simple, but its non-reversible so the
91 * simplicity doesnt really matter). This algorithm
92 * will not work if the user has only one level of domain
93 * naming in their hostname (e.g. if they are on a lan or
94 * are connecting via localhost) -- this doesnt matter much.
97 char* n1 = strchr(dest->host,'.');
98 char* n2 = strchr(dest->host,':');
102 unsigned int iv[] = { key1, key2, key3, key4 };
103 std::string a = LastTwoDomainParts(dest->host);
106 /* InspIRCd users have two hostnames; A displayed
107 * hostname which can be modified by modules (e.g.
108 * to create vhosts, implement chghost, etc) and a
109 * 'real' hostname which you shouldnt write to.
112 /* 2008/08/18: add <cloak:ipalways> which always cloaks
113 * the IP, for anonymity. --nenolod
117 /** Reset the Hash module, and send it our IV and hex table */
118 HashResetRequest(Sender, HashProvider).Send();
119 HashKeyRequest(Sender, HashProvider, iv).Send();
120 HashHexRequest(Sender, HashProvider, xtab[(*dest->host) % 4]);
122 /* Generate a cloak using specialized Hash */
123 std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
125 /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
126 * according to the DNS RFC) then tough titty, they get cloaked as an IP.
127 * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
133 if ((dest->GetProtocolFamily() == AF_INET6) && (inet_pton(AF_INET6,dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
134 /* Invalid ipv6 address, and ipv6 user (resolved host) */
136 else if ((dest->GetProtocolFamily() == AF_INET) && (inet_aton(dest->host,&testaddr2) < 1) && (hostcloak.length() <= 64))
137 /* Invalid ipv4 address, and ipv4 user (resolved host) */
140 /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
141 b = ((!strchr(dest->host,':')) ? Cloak4(dest->host) : Cloak6(dest->host));
144 if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
145 /* Invalid ipv4 address, and ipv4 user (resolved host) */
148 /* Valid ipv4 address (not resolved) ipv4 user */
149 b = Cloak4(dest->host);
155 if (dest->GetProtocolFamily() == AF_INET6)
156 b = Cloak6(dest->GetIPString());
158 if (dest->GetProtocolFamily() == AF_INET)
159 b = Cloak4(dest->GetIPString());
162 dest->ChangeDisplayedHost(b.c_str());
165 dest->SetMode('x',true);
166 return MODEACTION_ALLOW;
171 if (dest->IsModeSet('x'))
173 /* User is removing the mode, so just restore their real host
174 * and make it match the displayed one.
176 dest->ChangeDisplayedHost(dest->host);
177 dest->SetMode('x',false);
178 return MODEACTION_ALLOW;
182 return MODEACTION_DENY;
185 std::string Cloak4(const char* ip)
187 unsigned int iv[] = { key1, key2, key3, key4 };
188 irc::sepstream seps(ip, '.');
190 std::string octet[4];
193 for (int j = 0; j < 4; j++)
195 seps.GetToken(octet[j]);
196 i[j] = atoi(octet[j].c_str());
199 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
200 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
201 octet[1] = octet[0] + "." + octet[1];
203 /* Reset the Hash module and send it our IV */
204 HashResetRequest(Sender, HashProvider).Send();
205 HashKeyRequest(Sender, HashProvider, iv).Send();
207 /* Send the Hash module a different hex table for each octet group's Hash sum */
208 for (int k = 0; k < 4; k++)
210 HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
211 ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
213 /* Stick them all together */
214 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
217 std::string Cloak6(const char* ip)
219 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
220 if (!strncmp(ip, "0::ffff:", 8))
221 return Cloak4(ip + 8);
223 /* If we get here, yes it really is an ipv6 ip */
224 unsigned int iv[] = { key1, key2, key3, key4 };
225 std::vector<std::string> hashies;
229 /* Reset the Hash module and send it our IV */
230 HashResetRequest(Sender, HashProvider).Send();
231 HashKeyRequest(Sender, HashProvider, iv).Send();
233 for (const char* input = ip; *input; input++)
236 if (item.length() > 7)
238 /* Send the Hash module a different hex table for each octet group's Hash sum */
239 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
240 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
247 /* Send the Hash module a different hex table for each octet group's Hash sum */
248 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
249 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
252 /* Stick them all together */
253 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
258 ConfigReader Conf(ServerInstance);
261 /* These are *not* using the need_positive parameter of ReadInteger -
262 * that will limit the valid values to only the positive values in a
263 * signed int. Instead, accept any value that fits into an int and
264 * cast it to an unsigned int. That will, a bit oddly, give us the full
265 * spectrum of an unsigned integer. - Special */
266 key1 = key2 = key3 = key4 = 0;
267 key1 = (unsigned int) Conf.ReadInteger("cloak","key1",0,false);
268 key2 = (unsigned int) Conf.ReadInteger("cloak","key2",0,false);
269 key3 = (unsigned int) Conf.ReadInteger("cloak","key3",0,false);
270 key4 = (unsigned int) Conf.ReadInteger("cloak","key4",0,false);
271 prefix = Conf.ReadValue("cloak","prefix",0);
272 ipalways = Conf.ReadFlag("cloak", "ipalways", 0);
273 lowercase = Conf.ReadFlag("cloak", "lowercase", 0);
277 xtab[0] = "F92E45D871BCA630";
278 xtab[1] = "A1B9D80C72E653F4";
279 xtab[2] = "1ABC078934DEF562";
280 xtab[3] = "ABCDEF5678901234";
284 xtab[0] = "f92e45d871bca630";
285 xtab[1] = "a1b9d80c72e653f4";
286 xtab[2] = "1abc078934def562";
287 xtab[3] = "abcdef5678901234";
291 prefix = ServerInstance->Config->Network;
293 if (!key1 || !key2 || !key3 || !key4)
297 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
299 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
301 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
303 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
305 throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
311 class ModuleCloaking : public Module
319 ModuleCloaking(InspIRCd* Me)
322 /* Attempt to locate the md5 service provider, bail if we can't find it */
323 HashModule = ServerInstance->Modules->Find("m_md5.so");
325 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
327 cu = new CloakUser(ServerInstance, this, HashModule);
333 catch (CoreException &e)
339 /* Register it with the core */
340 if (!ServerInstance->Modes->AddMode(cu))
343 throw ModuleException("Could not add new modes!");
346 ServerInstance->Modules->UseInterface("HashRequest");
348 Implementation eventlist[] = { I_OnRehash };
349 ServerInstance->Modules->Attach(eventlist, this, 1);
352 virtual ~ModuleCloaking()
354 ServerInstance->Modes->DelMode(cu);
356 ServerInstance->Modules->DoneWithInterface("HashRequest");
359 virtual Version GetVersion()
361 // returns the version number of the module to be
362 // listed in /MODULES
363 return Version(1,2,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
366 virtual void OnRehash(User* user, const std::string ¶meter)
373 MODULE_INIT(ModuleCloaking)