]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
e53124aa0069e2afc7eeb437ad305d21d2d7435a
[user/henk/code/inspircd.git] / src / modules / m_cloaking.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd_config.h"
15 #include "configreader.h"
16 #include "inspircd.h"
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20
21 #include "m_hash.h"
22
23 /* $ModDesc: Provides masking of user hostnames */
24 /* $ModDep: m_hash.h */
25
26 /* Used to vary the output a little more depending on the cloak keys */
27 static const char* xtab[] = {"F92E45D871BCA630", "A1B9D80C72E653F4", "1ABC078934DEF562", "ABCDEF5678901234"};
28
29 /** Handles user mode +x
30  */
31 class CloakUser : public ModeHandler
32 {
33         
34         std::string prefix;
35         unsigned int key1;
36         unsigned int key2;
37         unsigned int key3;
38         unsigned int key4;
39         Module* Sender;
40         Module* HashProvider;
41
42         /** This function takes a domain name string and returns just the last two domain parts,
43          * or the last domain part if only two are available. Failing that it just returns what it was given.
44          *
45          * For example, if it is passed "svn.inspircd.org" it will return ".inspircd.org".
46          * If it is passed "brainbox.winbot.co.uk" it will return ".co.uk",
47          * and if it is passed "localhost.localdomain" it will return ".localdomain".
48          * 
49          * This is used to ensure a significant part of the host is always cloaked (see Bug #216)
50          */
51         std::string LastTwoDomainParts(const std::string &host)
52         {
53                 int dots = 0;
54                 std::string::size_type splitdot = host.length();
55
56                 for (std::string::size_type x = host.length() - 1; x; --x)
57                 {
58                         if (host[x] == '.')
59                         {
60                                 splitdot = x;
61                                 dots++;
62                         }
63                         if (dots >= 3)
64                                 break;
65                 }
66
67                 if (splitdot == host.length())
68                         return host;
69                 else
70                         return host.substr(splitdot);
71         }
72         
73  public:
74         CloakUser(InspIRCd* Instance, Module* Source, Module* Hash) : ModeHandler(Instance, 'x', 0, 0, false, MODETYPE_USER, false), Sender(Source), HashProvider(Hash)
75         {
76         }
77
78         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
79         {
80                 if (source != dest)
81                         return MODEACTION_DENY;
82
83                 /* For remote clients, we dont take any action, we just allow it.
84                  * The local server where they are will set their cloak instead.
85                  */
86                 if (!IS_LOCAL(dest))
87                         return MODEACTION_ALLOW;
88
89                 if (adding)
90                 {
91                         if(!dest->IsModeSet('x'))
92                         {
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.
100                                  */
101
102                                 char* n1 = strchr(dest->host,'.');
103                                 char* n2 = strchr(dest->host,':');
104                         
105                                 if (n1 || n2)
106                                 {
107                                         /* InspIRCd users have two hostnames; A displayed
108                                          * hostname which can be modified by modules (e.g.
109                                          * to create vhosts, implement chghost, etc) and a
110                                          * 'real' hostname which you shouldnt write to.
111                                          */
112
113                                         unsigned int iv[] = { key1, key2, key3, key4 };
114                                         std::string a = LastTwoDomainParts(dest->host);
115                                         std::string b;
116
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]);
121
122                                         /* Generate a cloak using specialized Hash */
123                                         std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
124
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
128                                          * vhost.
129                                          */
130 #ifdef IPV6
131                                         in6_addr testaddr;
132                                         in_addr testaddr2;
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) */
135                                                 b = hostcloak;
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) */
138                                                 b = hostcloak;
139                                         else
140                                                 /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
141                                                 b = ((!strchr(dest->host,':')) ? Cloak4(dest->host) : Cloak6(dest->host));
142 #else
143                                         in_addr testaddr;
144                                         if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
145                                                 /* Invalid ipv4 address, and ipv4 user (resolved host) */
146                                                 b = hostcloak;
147                                         else
148                                                 /* Valid ipv4 address (not resolved) ipv4 user */
149                                                 b = Cloak4(dest->host);
150 #endif
151
152                                         dest->ChangeDisplayedHost(b.c_str());
153                                 }
154                                 
155                                 dest->SetMode('x',true);
156                                 return MODEACTION_ALLOW;
157                         }
158                 }
159                 else
160                 {
161                         if (dest->IsModeSet('x'))
162                         {
163                                 /* User is removing the mode, so just restore their real host
164                                  * and make it match the displayed one.
165                                  */
166                                 dest->ChangeDisplayedHost(dest->host);
167                                 dest->SetMode('x',false);
168                                 return MODEACTION_ALLOW;
169                         }
170                 }
171
172                 return MODEACTION_DENY;
173         }
174
175         std::string Cloak4(const char* ip)
176         {
177                 unsigned int iv[] = { key1, key2, key3, key4 };
178                 irc::sepstream seps(ip, '.');
179                 std::string ra[4];;
180                 std::string octet[4];
181                 int i[4];
182
183                 for (int j = 0; j < 4; j++)
184                 {
185                         octet[j] = seps.GetToken();
186                         i[j] = atoi(octet[j].c_str());
187                 }
188
189                 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
190                 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
191                 octet[1] = octet[0] + "." + octet[1];
192
193                 /* Reset the Hash module and send it our IV */
194                 HashResetRequest(Sender, HashProvider).Send();
195                 HashKeyRequest(Sender, HashProvider, iv).Send();
196
197                 /* Send the Hash module a different hex table for each octet group's Hash sum */
198                 for (int k = 0; k < 4; k++)
199                 {
200                         HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
201                         ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
202                 }
203                 /* Stick them all together */
204                 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
205         }
206
207         std::string Cloak6(const char* ip)
208         {
209                 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
210                 if (!strncmp(ip, "0::ffff:", 8))
211                         return Cloak4(ip + 8);
212
213                 /* If we get here, yes it really is an ipv6 ip */
214                 unsigned int iv[] = { key1, key2, key3, key4 };
215                 std::vector<std::string> hashies;
216                 std::string item = "";
217                 int rounds = 0;
218
219                 /* Reset the Hash module and send it our IV */
220                 HashResetRequest(Sender, HashProvider).Send();
221                 HashKeyRequest(Sender, HashProvider, iv).Send();
222
223                 for (const char* input = ip; *input; input++)
224                 {
225                         item += *input;
226                         if (item.length() > 7)
227                         {
228                                 /* Send the Hash module a different hex table for each octet group's Hash sum */
229                                 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
230                                 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
231                                 item = "";
232                         }
233                         rounds++;
234                 }
235                 if (!item.empty())
236                 {
237                         /* Send the Hash module a different hex table for each octet group's Hash sum */
238                         HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
239                         hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
240                         item = "";
241                 }
242                 /* Stick them all together */
243                 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
244         }
245         
246         void DoRehash()
247         {
248                 ConfigReader Conf(ServerInstance);
249                 key1 = key2 = key3 = key4 = 0;
250                 key1 = Conf.ReadInteger("cloak","key1",0,true);
251                 key2 = Conf.ReadInteger("cloak","key2",0,true);
252                 key3 = Conf.ReadInteger("cloak","key3",0,true);
253                 key4 = Conf.ReadInteger("cloak","key4",0,true);
254                 prefix = Conf.ReadValue("cloak","prefix",0);
255
256                 if (prefix.empty())
257                         prefix = ServerInstance->Config->Network;
258
259                 if (!key1 && !key2 && !key3 && !key4)
260                         throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
261         }
262 };
263
264
265 class ModuleCloaking : public Module
266 {
267  private:
268         
269         CloakUser* cu;
270         Module* HashModule;
271
272  public:
273         ModuleCloaking(InspIRCd* Me)
274                 : Module::Module(Me)
275         {
276                 ServerInstance->UseInterface("HashRequest");
277
278                 /* Attempt to locate the md5 service provider, bail if we can't find it */
279                 HashModule = ServerInstance->FindModule("m_md5.so");
280                 if (!HashModule)
281                         throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
282
283                 /* Create new mode handler object */
284                 cu = new CloakUser(ServerInstance, this, HashModule);
285
286                 /* Register it with the core */         
287                 if (!ServerInstance->AddMode(cu, 'x'))
288                         throw ModuleException("Could not add new modes!");
289
290                 OnRehash(NULL,"");
291         }
292         
293         virtual ~ModuleCloaking()
294         {
295                 ServerInstance->Modes->DelMode(cu);
296                 DELETE(cu);
297                 ServerInstance->DoneWithInterface("HashRequest");
298         }
299         
300         virtual Version GetVersion()
301         {
302                 // returns the version number of the module to be
303                 // listed in /MODULES
304                 return Version(1,1,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
305         }
306
307         virtual void OnRehash(userrec* user, const std::string &parameter)
308         {
309                 cu->DoRehash();
310         }
311
312         void Implements(char* List)
313         {
314                 List[I_OnRehash] = 1;
315         }
316 };
317
318 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
319
320 class ModuleCloakingFactory : public ModuleFactory
321 {
322  public:
323         ModuleCloakingFactory()
324         {
325         }
326         
327         ~ModuleCloakingFactory()
328         {
329         }
330         
331         virtual Module * CreateModule(InspIRCd* Me)
332         {
333                 return new ModuleCloaking(Me);
334         }
335         
336 };
337
338
339 extern "C" void * init_module( void )
340 {
341         return new ModuleCloakingFactory;
342 }