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