]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
Refactor port binding, warning not yet tested fully
[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                 /* Only opers can change other users modes */
81                 if ((source != dest) && (!*source->oper))
82                         return MODEACTION_DENY;
83
84                 /* For remote clients, we dont take any action, we just allow it.
85                  * The local server where they are will set their cloak instead.
86                  */
87                 if (!IS_LOCAL(dest))
88                         return MODEACTION_ALLOW;
89
90                 if (adding)
91                 {
92                         if(!dest->IsModeSet('x'))
93                         {
94                                 /* The mode is being turned on - so attempt to
95                                  * allocate the user a cloaked host using a non-reversible
96                                  * algorithm (its simple, but its non-reversible so the
97                                  * simplicity doesnt really matter). This algorithm
98                                  * will not work if the user has only one level of domain
99                                  * naming in their hostname (e.g. if they are on a lan or
100                                  * are connecting via localhost) -- this doesnt matter much.
101                                  */
102
103                                 char* n1 = strchr(dest->host,'.');
104                                 char* n2 = strchr(dest->host,':');
105                         
106                                 if (n1 || n2)
107                                 {
108                                         /* InspIRCd users have two hostnames; A displayed
109                                          * hostname which can be modified by modules (e.g.
110                                          * to create vhosts, implement chghost, etc) and a
111                                          * 'real' hostname which you shouldnt write to.
112                                          */
113
114                                         unsigned int iv[] = { key1, key2, key3, key4 };
115                                         std::string a = LastTwoDomainParts(dest->host);
116                                         std::string b;
117
118                                         /** Reset the Hash module, and send it our IV and hex table */
119                                         HashResetRequest(Sender, HashProvider).Send();
120                                         HashKeyRequest(Sender, HashProvider, iv).Send();
121                                         HashHexRequest(Sender, HashProvider, xtab[(*dest->host) % 4]);
122
123                                         /* Generate a cloak using specialized Hash */
124                                         std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
125
126                                         /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
127                                          * according to the DNS RFC) then tough titty, they get cloaked as an IP. 
128                                          * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
129                                          * vhost.
130                                          */
131 #ifdef IPV6
132                                         in6_addr testaddr;
133                                         in_addr testaddr2;
134                                         if ((dest->GetProtocolFamily() == AF_INET6) && (inet_pton(AF_INET6,dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
135                                                 /* Invalid ipv6 address, and ipv6 user (resolved host) */
136                                                 b = hostcloak;
137                                         else if ((dest->GetProtocolFamily() == AF_INET) && (inet_aton(dest->host,&testaddr2) < 1) && (hostcloak.length() <= 64))
138                                                 /* Invalid ipv4 address, and ipv4 user (resolved host) */
139                                                 b = hostcloak;
140                                         else
141                                                 /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
142                                                 b = ((!strchr(dest->host,':')) ? Cloak4(dest->host) : Cloak6(dest->host));
143 #else
144                                         in_addr testaddr;
145                                         if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
146                                                 /* Invalid ipv4 address, and ipv4 user (resolved host) */
147                                                 b = hostcloak;
148                                         else
149                                                 /* Valid ipv4 address (not resolved) ipv4 user */
150                                                 b = Cloak4(dest->host);
151 #endif
152
153                                         dest->ChangeDisplayedHost(b.c_str());
154                                 }
155                                 
156                                 dest->SetMode('x',true);
157                                 return MODEACTION_ALLOW;
158                         }
159                 }
160                 else
161                 {
162                         if (dest->IsModeSet('x'))
163                         {
164                                 /* User is removing the mode, so just restore their real host
165                                  * and make it match the displayed one.
166                                  */
167                                 dest->ChangeDisplayedHost(dest->host);
168                                 dest->SetMode('x',false);
169                                 return MODEACTION_ALLOW;
170                         }
171                 }
172
173                 return MODEACTION_DENY;
174         }
175
176         std::string Cloak4(const char* ip)
177         {
178                 unsigned int iv[] = { key1, key2, key3, key4 };
179                 irc::sepstream seps(ip, '.');
180                 std::string ra[4];;
181                 std::string octet[4];
182                 int i[4];
183
184                 for (int j = 0; j < 4; j++)
185                 {
186                         octet[j] = seps.GetToken();
187                         i[j] = atoi(octet[j].c_str());
188                 }
189
190                 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
191                 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
192                 octet[1] = octet[0] + "." + octet[1];
193
194                 /* Reset the Hash module and send it our IV */
195                 HashResetRequest(Sender, HashProvider).Send();
196                 HashKeyRequest(Sender, HashProvider, iv).Send();
197
198                 /* Send the Hash module a different hex table for each octet group's Hash sum */
199                 for (int k = 0; k < 4; k++)
200                 {
201                         HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
202                         ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
203                 }
204                 /* Stick them all together */
205                 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
206         }
207
208         std::string Cloak6(const char* ip)
209         {
210                 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
211                 if (!strncmp(ip, "0::ffff:", 8))
212                         return Cloak4(ip + 8);
213
214                 /* If we get here, yes it really is an ipv6 ip */
215                 unsigned int iv[] = { key1, key2, key3, key4 };
216                 std::vector<std::string> hashies;
217                 std::string item = "";
218                 int rounds = 0;
219
220                 /* Reset the Hash module and send it our IV */
221                 HashResetRequest(Sender, HashProvider).Send();
222                 HashKeyRequest(Sender, HashProvider, iv).Send();
223
224                 for (const char* input = ip; *input; input++)
225                 {
226                         item += *input;
227                         if (item.length() > 7)
228                         {
229                                 /* Send the Hash module a different hex table for each octet group's Hash sum */
230                                 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
231                                 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,8));
232                                 item = "";
233                         }
234                         rounds++;
235                 }
236                 if (!item.empty())
237                 {
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));
241                         item = "";
242                 }
243                 /* Stick them all together */
244                 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
245         }
246         
247         void DoRehash()
248         {
249                 ConfigReader Conf(ServerInstance);
250                 key1 = key2 = key3 = key4 = 0;
251                 key1 = Conf.ReadInteger("cloak","key1",0,true);
252                 key2 = Conf.ReadInteger("cloak","key2",0,true);
253                 key3 = Conf.ReadInteger("cloak","key3",0,true);
254                 key4 = Conf.ReadInteger("cloak","key4",0,true);
255                 prefix = Conf.ReadValue("cloak","prefix",0);
256
257                 if (prefix.empty())
258                         prefix = ServerInstance->Config->Network;
259
260                 if (!key1 && !key2 && !key3 && !key4)
261                         throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
262         }
263 };
264
265
266 class ModuleCloaking : public Module
267 {
268  private:
269         
270         CloakUser* cu;
271         Module* HashModule;
272
273  public:
274         ModuleCloaking(InspIRCd* Me)
275                 : Module::Module(Me)
276         {
277                 ServerInstance->UseInterface("HashRequest");
278
279                 /* Attempt to locate the md5 service provider, bail if we can't find it */
280                 HashModule = ServerInstance->FindModule("m_md5.so");
281                 if (!HashModule)
282                         throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
283
284                 /* Create new mode handler object */
285                 cu = new CloakUser(ServerInstance, this, HashModule);
286
287                 /* Register it with the core */         
288                 if (!ServerInstance->AddMode(cu, 'x'))
289                         throw ModuleException("Could not add new modes!");
290
291                 OnRehash(NULL,"");
292         }
293         
294         virtual ~ModuleCloaking()
295         {
296                 ServerInstance->Modes->DelMode(cu);
297                 DELETE(cu);
298                 ServerInstance->DoneWithInterface("HashRequest");
299         }
300         
301         virtual Version GetVersion()
302         {
303                 // returns the version number of the module to be
304                 // listed in /MODULES
305                 return Version(1,1,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
306         }
307
308         virtual void OnRehash(userrec* user, const std::string &parameter)
309         {
310                 cu->DoRehash();
311         }
312
313         void Implements(char* List)
314         {
315                 List[I_OnRehash] = 1;
316         }
317 };
318
319 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
320
321 class ModuleCloakingFactory : public ModuleFactory
322 {
323  public:
324         ModuleCloakingFactory()
325         {
326         }
327         
328         ~ModuleCloakingFactory()
329         {
330         }
331         
332         virtual Module * CreateModule(InspIRCd* Me)
333         {
334                 return new ModuleCloaking(Me);
335         }
336         
337 };
338
339
340 extern "C" void * init_module( void )
341 {
342         return new ModuleCloakingFactory;
343 }