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