]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
In the grand tradition of huge fucking commits:
[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.h"
15 #include "m_hash.h"
16
17 /* $ModDesc: Provides masking of user hostnames */
18 /* $ModDep: m_hash.h */
19
20 /** Handles user mode +x
21  */
22 class CloakUser : public ModeHandler
23 {
24         std::string prefix;
25         unsigned int key1;
26         unsigned int key2;
27         unsigned int key3;
28         unsigned int key4;
29         bool ipalways;
30         Module* Sender;
31         Module* HashProvider;
32         const char *xtab[4];
33
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.
36          *
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".
40          * 
41          * This is used to ensure a significant part of the host is always cloaked (see Bug #216)
42          */
43         std::string LastTwoDomainParts(const std::string &host)
44         {
45                 int dots = 0;
46                 std::string::size_type splitdot = host.length();
47
48                 for (std::string::size_type x = host.length() - 1; x; --x)
49                 {
50                         if (host[x] == '.')
51                         {
52                                 splitdot = x;
53                                 dots++;
54                         }
55                         if (dots >= 3)
56                                 break;
57                 }
58
59                 if (splitdot == host.length())
60                         return host;
61                 else
62                         return host.substr(splitdot);
63         }
64         
65  public:
66         CloakUser(InspIRCd* Instance, Module* Source, Module* Hash) : ModeHandler(Instance, 'x', 0, 0, false, MODETYPE_USER, false), Sender(Source), HashProvider(Hash)
67         {
68         }
69
70         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
71         {
72                 if (source != dest)
73                         return MODEACTION_DENY;
74
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.
77                  */
78                 if (!IS_LOCAL(dest))
79                         return MODEACTION_ALLOW;
80
81                 if (adding)
82                 {
83                         if(!dest->IsModeSet('x'))
84                         {
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.
92                                  */
93
94                                 char* n1 = strchr(dest->host,'.');
95                                 char* n2 = strchr(dest->host,':');
96                         
97                                 if (n1 || n2)
98                                 {
99                                         unsigned int iv[] = { key1, key2, key3, key4 };
100                                         std::string a = LastTwoDomainParts(dest->host);
101                                         std::string b;
102
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.
107                                          */
108
109                                         /* 2007/08/18: add <cloak:ipalways> which always cloaks
110                                          * the IP, for anonymity. --nenolod
111                                          */
112                                         if (!ipalways)
113                                         {
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]);
118
119                                                 /* Generate a cloak using specialized Hash */
120                                                 std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
121
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
125                                                  * vhost.
126                                                  */
127 #ifdef IPV6
128                                                 in6_addr testaddr;
129                                                 in_addr testaddr2;
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) */
132                                                         b = hostcloak;
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) */
135                                                         b = hostcloak;
136                                                 else
137                                                         /* Valid ipv6 or ipv4 address (not resolved) ipv4 or ipv6 user */
138                                                         b = ((!strchr(dest->host,':')) ? Cloak4(dest->host) : Cloak6(dest->host));
139 #else
140                                                 in_addr testaddr;
141                                                 if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
142                                                         /* Invalid ipv4 address, and ipv4 user (resolved host) */
143                                                         b = hostcloak;
144                                                 else
145                                                         /* Valid ipv4 address (not resolved) ipv4 user */
146                                                         b = Cloak4(dest->host);
147 #endif
148                                         }
149                                         else
150                                         {
151 #ifdef IPV6
152                                                 if (dest->GetProtocolFamily() == AF_INET6)
153                                                         b = Cloak6(dest->GetIPString());
154 #endif
155                                                 if (dest->GetProtocolFamily() == AF_INET)
156                                                         b = Cloak4(dest->GetIPString());
157                                         }
158
159                                         dest->ChangeDisplayedHost(b.c_str());
160                                 }
161                                 
162                                 dest->SetMode('x',true);
163                                 return MODEACTION_ALLOW;
164                         }
165                 }
166                 else
167                 {
168                         if (dest->IsModeSet('x'))
169                         {
170                                 /* User is removing the mode, so just restore their real host
171                                  * and make it match the displayed one.
172                                  */
173                                 dest->ChangeDisplayedHost(dest->host);
174                                 dest->SetMode('x',false);
175                                 return MODEACTION_ALLOW;
176                         }
177                 }
178
179                 return MODEACTION_DENY;
180         }
181
182         std::string Cloak4(const char* ip)
183         {
184                 unsigned int iv[] = { key1, key2, key3, key4 };
185                 irc::sepstream seps(ip, '.');
186                 std::string ra[4];;
187                 std::string octet[4];
188                 int i[4];
189
190                 for (int j = 0; j < 4; j++)
191                 {
192                         seps.GetToken(octet[j]);
193                         i[j] = atoi(octet[j].c_str());
194                 }
195
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];
199
200                 /* Reset the Hash module and send it our IV */
201                 HashResetRequest(Sender, HashProvider).Send();
202                 HashKeyRequest(Sender, HashProvider, iv).Send();
203
204                 /* Send the Hash module a different hex table for each octet group's Hash sum */
205                 for (int k = 0; k < 4; k++)
206                 {
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);
209                 }
210                 /* Stick them all together */
211                 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
212         }
213
214         std::string Cloak6(const char* ip)
215         {
216                 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
217                 if (!strncmp(ip, "0::ffff:", 8))
218                         return Cloak4(ip + 8);
219
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;
223                 std::string item;
224                 int rounds = 0;
225
226                 /* Reset the Hash module and send it our IV */
227                 HashResetRequest(Sender, HashProvider).Send();
228                 HashKeyRequest(Sender, HashProvider, iv).Send();
229
230                 for (const char* input = ip; *input; input++)
231                 {
232                         item += *input;
233                         if (item.length() > 7)
234                         {
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));
238                                 item.clear();
239                         }
240                         rounds++;
241                 }
242                 if (!item.empty())
243                 {
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));
247                         item.clear();
248                 }
249                 /* Stick them all together */
250                 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
251         }
252         
253         void DoRehash()
254         {
255                 ConfigReader Conf(ServerInstance);
256                 bool lowercase;
257                 
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);
271                 
272                 if (!lowercase)
273                 {
274                         xtab[0] = "F92E45D871BCA630";
275                         xtab[1] = "A1B9D80C72E653F4";
276                         xtab[2] = "1ABC078934DEF562";
277                         xtab[3] = "ABCDEF5678901234";
278                 }
279                 else
280                 {
281                         xtab[0] = "f92e45d871bca630";
282                         xtab[1] = "a1b9d80c72e653f4";
283                         xtab[2] = "1abc078934def562";
284                         xtab[3] = "abcdef5678901234";
285                 }
286
287                 if (prefix.empty())
288                         prefix = ServerInstance->Config->Network;
289
290                 if (!key1 || !key2 || !key3 || !key4)
291                 {
292                         std::string detail;
293                         if (!key1)
294                                 detail = "<cloak:key1> is not valid, it may be set to a too high/low value, or it may not exist.";
295                         else if (!key2)
296                                 detail = "<cloak:key2> is not valid, it may be set to a too high/low value, or it may not exist.";
297                         else if (!key3)
298                                 detail = "<cloak:key3> is not valid, it may be set to a too high/low value, or it may not exist.";
299                         else if (!key4)
300                                 detail = "<cloak:key4> is not valid, it may be set to a too high/low value, or it may not exist.";
301
302                         throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED! - " + detail);
303                 }
304         }
305 };
306
307
308 class ModuleCloaking : public Module
309 {
310  private:
311         
312         CloakUser* cu;
313         Module* HashModule;
314
315  public:
316         ModuleCloaking(InspIRCd* Me)
317                 : Module(Me)
318         {
319                 ServerInstance->Modules->UseInterface("HashRequest");
320
321                 /* Attempt to locate the md5 service provider, bail if we can't find it */
322                 HashModule = ServerInstance->Modules->Find("m_md5.so");
323                 if (!HashModule)
324                         throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
325
326                 /* Create new mode handler object */
327                 cu = new CloakUser(ServerInstance, this, HashModule);
328
329                 /* Register it with the core */         
330                 if (!ServerInstance->AddMode(cu, 'x'))
331                         throw ModuleException("Could not add new modes!");
332
333                 OnRehash(NULL,"");
334         }
335         
336         virtual ~ModuleCloaking()
337         {
338                 ServerInstance->Modes->DelMode(cu);
339                 DELETE(cu);
340                 ServerInstance->Modules->DoneWithInterface("HashRequest");
341         }
342         
343         virtual Version GetVersion()
344         {
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);
348         }
349
350         virtual void OnRehash(User* user, const std::string &parameter)
351         {
352                 cu->DoRehash();
353         }
354
355         void Implements(char* List)
356         {
357                 List[I_OnRehash] = 1;
358         }
359 };
360
361 MODULE_INIT(ModuleCloaking)