1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
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;
83 if(!dest->IsModeSet('x'))
85 /* The mode is being turned on - so attempt to
86 * allocate the user a cloaked host using a non-reversible
87 * algorithm (its simple, but its non-reversible so the
88 * simplicity doesnt really matter). This algorithm
89 * will not work if the user has only one level of domain
90 * naming in their hostname (e.g. if they are on a lan or
91 * are connecting via localhost) -- this doesnt matter much.
94 char* n1 = strchr(dest->host,'.');
95 char* n2 = strchr(dest->host,':');
99 unsigned int iv[] = { key1, key2, key3, key4 };
100 std::string a = LastTwoDomainParts(dest->host);
103 /* InspIRCd users have two hostnames; A displayed
104 * hostname which can be modified by modules (e.g.
105 * to create vhosts, implement chghost, etc) and a
106 * 'real' hostname which you shouldnt write to.
109 /* 2007/08/18: add <cloak:ipalways> which always cloaks
110 * the IP, for anonymity. --nenolod
114 /** Reset the Hash module, and send it our IV and hex table */
115 HashResetRequest(Sender, HashProvider).Send();
116 HashKeyRequest(Sender, HashProvider, iv).Send();
117 HashHexRequest(Sender, HashProvider, xtab[(*dest->host) % 4]);
119 /* Generate a cloak using specialized Hash */
120 std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
122 /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
123 * according to the DNS RFC) then tough titty, they get cloaked as an IP.
124 * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
130 if ((dest->GetProtocolFamily() == AF_INET6) && (inet_pton(AF_INET6,dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
131 /* Invalid ipv6 address, and ipv6 user (resolved host) */
133 else if ((dest->GetProtocolFamily() == AF_INET) && (inet_aton(dest->host,&testaddr2) < 1) && (hostcloak.length() <= 64))
134 /* Invalid ipv4 address, and ipv4 user (resolved host) */
137 /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
138 b = ((!strchr(dest->host,':')) ? Cloak4(dest->host) : Cloak6(dest->host));
141 if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
142 /* Invalid ipv4 address, and ipv4 user (resolved host) */
145 /* Valid ipv4 address (not resolved) ipv4 user */
146 b = Cloak4(dest->host);
152 if (dest->GetProtocolFamily() == AF_INET6)
153 b = Cloak6(dest->GetIPString());
155 if (dest->GetProtocolFamily() == AF_INET)
156 b = Cloak4(dest->GetIPString());
159 dest->ChangeDisplayedHost(b.c_str());
162 dest->SetMode('x',true);
163 return MODEACTION_ALLOW;
168 if (dest->IsModeSet('x'))
170 /* User is removing the mode, so just restore their real host
171 * and make it match the displayed one.
173 dest->ChangeDisplayedHost(dest->host);
174 dest->SetMode('x',false);
175 return MODEACTION_ALLOW;
179 return MODEACTION_DENY;
182 std::string Cloak4(const char* ip)
184 unsigned int iv[] = { key1, key2, key3, key4 };
185 irc::sepstream seps(ip, '.');
187 std::string octet[4];
190 for (int j = 0; j < 4; j++)
192 seps.GetToken(octet[j]);
193 i[j] = atoi(octet[j].c_str());
196 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
197 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
198 octet[1] = octet[0] + "." + octet[1];
200 /* Reset the Hash module and send it our IV */
201 HashResetRequest(Sender, HashProvider).Send();
202 HashKeyRequest(Sender, HashProvider, iv).Send();
204 /* Send the Hash module a different hex table for each octet group's Hash sum */
205 for (int k = 0; k < 4; k++)
207 HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
208 ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
210 /* Stick them all together */
211 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
214 std::string Cloak6(const char* ip)
216 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
217 if (!strncmp(ip, "0::ffff:", 8))
218 return Cloak4(ip + 8);
220 /* If we get here, yes it really is an ipv6 ip */
221 unsigned int iv[] = { key1, key2, key3, key4 };
222 std::vector<std::string> hashies;
226 /* Reset the Hash module and send it our IV */
227 HashResetRequest(Sender, HashProvider).Send();
228 HashKeyRequest(Sender, HashProvider, iv).Send();
230 for (const char* input = ip; *input; input++)
233 if (item.length() > 7)
235 /* Send the Hash module a different hex table for each octet group's Hash sum */
236 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
237 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
244 /* Send the Hash module a different hex table for each octet group's Hash sum */
245 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
246 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
249 /* Stick them all together */
250 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
255 ConfigReader Conf(ServerInstance);
258 /* These are *not* using the need_positive parameter of ReadInteger -
259 * that will limit the valid values to only the positive values in a
260 * signed int. Instead, accept any value that fits into an int and
261 * cast it to an unsigned int. That will, a bit oddly, give us the full
262 * spectrum of an unsigned integer. - Special */
263 key1 = key2 = key3 = key4 = 0;
264 key1 = (unsigned int) Conf.ReadInteger("cloak","key1",0,false);
265 key2 = (unsigned int) Conf.ReadInteger("cloak","key2",0,false);
266 key3 = (unsigned int) Conf.ReadInteger("cloak","key3",0,false);
267 key4 = (unsigned int) Conf.ReadInteger("cloak","key4",0,false);
268 prefix = Conf.ReadValue("cloak","prefix",0);
269 ipalways = Conf.ReadFlag("cloak", "ipalways", 0);
270 lowercase = Conf.ReadFlag("cloak", "lowercase", 0);
274 xtab[0] = "F92E45D871BCA630";
275 xtab[1] = "A1B9D80C72E653F4";
276 xtab[2] = "1ABC078934DEF562";
277 xtab[3] = "ABCDEF5678901234";
281 xtab[0] = "f92e45d871bca630";
282 xtab[1] = "a1b9d80c72e653f4";
283 xtab[2] = "1abc078934def562";
284 xtab[3] = "abcdef5678901234";
288 prefix = ServerInstance->Config->Network;
290 if (!key1 || !key2 || !key3 || !key4)
294 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
296 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
298 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
300 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
302 throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
308 class ModuleCloaking : public Module
316 ModuleCloaking(InspIRCd* Me)
319 ServerInstance->Modules->UseInterface("HashRequest");
321 /* Attempt to locate the md5 service provider, bail if we can't find it */
322 HashModule = ServerInstance->Modules->Find("m_md5.so");
324 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
326 /* Create new mode handler object */
327 cu = new CloakUser(ServerInstance, this, HashModule);
329 /* Register it with the core */
330 if (!ServerInstance->AddMode(cu, 'x'))
331 throw ModuleException("Could not add new modes!");
336 virtual ~ModuleCloaking()
338 ServerInstance->Modes->DelMode(cu);
340 ServerInstance->Modules->DoneWithInterface("HashRequest");
343 virtual Version GetVersion()
345 // returns the version number of the module to be
346 // listed in /MODULES
347 return Version(1,1,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
350 virtual void OnRehash(userrec* user, const std::string ¶meter)
355 void Implements(char* List)
357 List[I_OnRehash] = 1;
361 MODULE_INIT(ModuleCloaking)