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(Module* source, Module* Hash)
67 : ModeHandler(source, "cloak", '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, '.');
136 std::string octet[4];
139 for (int j = 0; j < 4; j++)
141 seps.GetToken(octet[j]);
142 i[j] = atoi(octet[j].c_str());
145 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
146 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
147 octet[1] = octet[0] + "." + octet[1];
149 /* Reset the Hash module and send it our IV */
153 /* Send the Hash module a different hex table for each octet group's Hash sum */
154 for (int k = 0; k < 4; k++)
156 HashRequestIV hash(creator, HashProvider, iv, xtab[(iv[k]+i[k]) % 4], octet[k]);
157 rv.append(hash.result.substr(0,6));
161 /* Stick them all together */
165 std::string Cloak6(const char* ip)
167 unsigned int iv[] = { key1, key2, key3, key4 };
168 std::vector<std::string> hashies;
172 /* Reset the Hash module and send it our IV */
174 for (const char* input = ip; *input; input++)
177 if (item.length() > 7)
179 HashRequestIV hash(creator, HashProvider, iv, xtab[(key1+rounds) % 4], item);
180 hashies.push_back(hash.result.substr(0,8));
187 HashRequestIV hash(creator, HashProvider, iv, xtab[(key1+rounds) % 4], item);
188 hashies.push_back(hash.result.substr(0,8));
190 /* Stick them all together */
191 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
199 /* These are *not* using the need_positive parameter of ReadInteger -
200 * that will limit the valid values to only the positive values in a
201 * signed int. Instead, accept any value that fits into an int and
202 * cast it to an unsigned int. That will, a bit oddly, give us the full
203 * spectrum of an unsigned integer. - Special */
204 key1 = key2 = key3 = key4 = 0;
205 key1 = (unsigned int) Conf.ReadInteger("cloak","key1",0,false);
206 key2 = (unsigned int) Conf.ReadInteger("cloak","key2",0,false);
207 key3 = (unsigned int) Conf.ReadInteger("cloak","key3",0,false);
208 key4 = (unsigned int) Conf.ReadInteger("cloak","key4",0,false);
209 prefix = Conf.ReadValue("cloak","prefix",0);
210 ipalways = Conf.ReadFlag("cloak", "ipalways", 0);
211 lowercase = Conf.ReadFlag("cloak", "lowercase", 0);
215 xtab[0] = "F92E45D871BCA630";
216 xtab[1] = "A1B9D80C72E653F4";
217 xtab[2] = "1ABC078934DEF562";
218 xtab[3] = "ABCDEF5678901234";
222 xtab[0] = "f92e45d871bca630";
223 xtab[1] = "a1b9d80c72e653f4";
224 xtab[2] = "1abc078934def562";
225 xtab[3] = "abcdef5678901234";
229 prefix = ServerInstance->Config->Network;
231 if (!key1 || !key2 || !key3 || !key4)
235 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
237 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
239 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
241 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
243 throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
249 class ModuleCloaking : public Module
257 /* Attempt to locate the md5 service provider, bail if we can't find it */
258 Module* HashModule = ServerInstance->Modules->Find("m_md5.so");
260 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
262 cu = new CloakUser(this, HashModule);
268 catch (ModuleException &e)
274 /* Register it with the core */
275 if (!ServerInstance->Modes->AddMode(cu))
278 throw ModuleException("Could not add new modes!");
281 ServerInstance->Modules->UseInterface("HashRequest");
282 Extensible::Register(&cu->ext);
284 Implementation eventlist[] = { I_OnRehash, I_OnCheckBan, I_OnUserConnect };
285 ServerInstance->Modules->Attach(eventlist, this, 3);
287 CloakExistingUsers();
290 void CloakExistingUsers()
293 for (std::vector<User*>::iterator u = ServerInstance->Users->local_users.begin(); u != ServerInstance->Users->local_users.end(); u++)
295 cloak = cu->ext.get(*u);
303 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
306 std::string* cloak = cu->ext.get(user);
307 /* Check if they have a cloaked host, but are not using it */
308 if (cloak && *cloak != user->dhost)
310 snprintf(cmask, MAXBUF, "%s!%s@%s", user->nick.c_str(), user->ident.c_str(), cloak->c_str());
311 if (InspIRCd::Match(cmask,mask))
314 return MOD_RES_PASSTHRU;
319 /* Needs to be after m_banexception etc. */
320 ServerInstance->Modules->SetPriority(this, I_OnCheckBan, PRIORITY_LAST);
322 /* but before m_conn_umodes, so host is generated ready to apply */
323 Module *um = ServerInstance->Modules->Find("m_conn_umodes.so");
324 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_AFTER, &um);
330 ServerInstance->Modules->DoneWithInterface("HashRequest");
335 // returns the version number of the module to be
336 // listed in /MODULES
337 return Version("Provides masking of user hostnames", VF_COMMON|VF_VENDOR,API_VERSION);
340 void OnRehash(User* user)
345 void OnUserConnect(User* dest)
347 std::string* cloak = cu->ext.get(dest);
351 if (dest->host.find('.') != std::string::npos || dest->host.find(':') != std::string::npos)
353 unsigned int iv[] = { cu->key1, cu->key2, cu->key3, cu->key4 };
354 std::string a = cu->LastTwoDomainParts(dest->host);
357 /* InspIRCd users have two hostnames; A displayed
358 * hostname which can be modified by modules (e.g.
359 * to create vhosts, implement chghost, etc) and a
360 * 'real' hostname which you shouldnt write to.
363 /* 2008/08/18: add <cloak:ipalways> which always cloaks
364 * the IP, for anonymity. --nenolod
368 /** Reset the Hash module, and send it our IV and hex table */
369 HashRequestIV hash(this, cu->HashProvider, iv, cu->xtab[(dest->host[0]) % 4], dest->host);
371 /* Generate a cloak using specialized Hash */
372 std::string hostcloak = cu->prefix + "-" + hash.result.substr(0,8) + a;
374 /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
375 * according to the DNS RFC) then tough titty, they get cloaked as an IP.
376 * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
379 std::string testaddr;
381 if (!irc::sockets::satoap(&dest->client_sa, testaddr, testport) && (hostcloak.length() <= 64))
382 /* not a valid address, must have been a host, so cloak as a host */
384 else if (dest->client_sa.sa.sa_family == AF_INET6)
385 b = cu->Cloak6(dest->GetIPString());
387 b = cu->Cloak4(dest->GetIPString());
391 if (dest->client_sa.sa.sa_family == AF_INET6)
392 b = cu->Cloak6(dest->GetIPString());
394 b = cu->Cloak4(dest->GetIPString());
403 MODULE_INIT(ModuleCloaking)