]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
Add extra /map info (connection uptime, and lag time) to /MAP for opers. Adds feature...
[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
87                                         /** Reset the Hash module, and send it our IV and hex table */
88                                         HashResetRequest(Sender, HashProvider).Send();
89                                         HashKeyRequest(Sender, HashProvider, iv).Send();
90                                         HashHexRequest(Sender, HashProvider, xtab[(*dest->host) % 4]);
91
92                                         /* Generate a cloak using specialized Hash */
93                                         std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
94
95                                         /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
96                                          * according to the DNS RFC) then tough titty, they get cloaked as an IP. 
97                                          * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
98                                          * vhost.
99                                          */
100 #ifdef IPV6
101                                         in6_addr testaddr;
102                                         in_addr testaddr2;
103                                         if ((dest->GetProtocolFamily() == AF_INET6) && (inet_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_INET) && (inet_aton(dest->host,&testaddr2) < 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 = ((!strchr(dest->host,':')) ? Cloak4(dest->host) : Cloak6(dest->host));
112 #else
113                                         in_addr testaddr;
114                                         if ((inet_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
115                                                 /* Invalid ipv4 address, and ipv4 user (resolved host) */
116                                                 b = hostcloak;
117                                         else
118                                                 /* Valid ipv4 address (not resolved) ipv4 user */
119                                                 b = Cloak4(dest->host);
120 #endif
121
122                                         dest->ChangeDisplayedHost(b.c_str());
123                                 }
124                                 
125                                 dest->SetMode('x',true);
126                                 return MODEACTION_ALLOW;
127                         }
128                 }
129                 else
130                 {
131                         if (dest->IsModeSet('x'))
132                         {
133                                 /* User is removing the mode, so just restore their real host
134                                  * and make it match the displayed one.
135                                  */
136                                 dest->ChangeDisplayedHost(dest->host);
137                                 dest->SetMode('x',false);
138                                 return MODEACTION_ALLOW;
139                         }
140                 }
141
142                 return MODEACTION_DENY;
143         }
144
145         std::string Cloak4(const char* ip)
146         {
147                 unsigned int iv[] = { key1, key2, key3, key4 };
148                 irc::sepstream seps(ip, '.');
149                 std::string ra[4];;
150                 std::string octet[4];
151                 int i[4];
152
153                 for (int j = 0; j < 4; j++)
154                 {
155                         octet[j] = seps.GetToken();
156                         i[j] = atoi(octet[j].c_str());
157                 }
158
159                 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
160                 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
161                 octet[1] = octet[0] + "." + octet[1];
162
163                 /* Reset the Hash module and send it our IV */
164                 HashResetRequest(Sender, HashProvider).Send();
165                 HashKeyRequest(Sender, HashProvider, iv).Send();
166
167                 /* Send the Hash module a different hex table for each octet group's Hash sum */
168                 for (int k = 0; k < 4; k++)
169                 {
170                         HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
171                         ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
172                 }
173                 /* Stick them all together */
174                 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
175         }
176
177         std::string Cloak6(const char* ip)
178         {
179                 /* Theyre using 4in6 (YUCK). Translate as ipv4 cloak */
180                 if (!strncmp(ip, "0::ffff:", 8))
181                         return Cloak4(ip + 8);
182
183                 /* If we get here, yes it really is an ipv6 ip */
184                 unsigned int iv[] = { key1, key2, key3, key4 };
185                 std::vector<std::string> hashies;
186                 std::string item = "";
187                 int rounds = 0;
188
189                 /* Reset the Hash module and send it our IV */
190                 HashResetRequest(Sender, HashProvider).Send();
191                 HashKeyRequest(Sender, HashProvider, iv).Send();
192
193                 for (const char* input = ip; *input; input++)
194                 {
195                         item += *input;
196                         if (item.length() > 5)
197                         {
198                                 /* Send the Hash module a different hex table for each octet group's Hash sum */
199                                 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
200                                 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,10));
201                                 item = "";
202                         }
203                         rounds++;
204                 }
205                 if (!item.empty())
206                 {
207                         /* Send the Hash module a different hex table for each octet group's Hash sum */
208                         HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
209                         hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,10));
210                         item = "";
211                 }
212                 /* Stick them all together */
213                 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
214         }
215         
216         void DoRehash()
217         {
218                 ConfigReader Conf(ServerInstance);
219                 key1 = key2 = key3 = key4 = 0;
220                 key1 = Conf.ReadInteger("cloak","key1",0,true);
221                 key2 = Conf.ReadInteger("cloak","key2",0,true);
222                 key3 = Conf.ReadInteger("cloak","key3",0,true);
223                 key4 = Conf.ReadInteger("cloak","key4",0,true);
224                 prefix = Conf.ReadValue("cloak","prefix",0);
225
226                 if (prefix.empty())
227                         prefix = ServerInstance->Config->Network;
228
229                 if (!key1 && !key2 && !key3 && !key4)
230                         throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
231         }
232 };
233
234
235 class ModuleCloaking : public Module
236 {
237  private:
238         
239         CloakUser* cu;
240         Module* HashModule;
241
242  public:
243         ModuleCloaking(InspIRCd* Me)
244                 : Module::Module(Me)
245         {
246                 ServerInstance->UseInterface("HashRequest");
247
248                 /* Attempt to locate the md5 service provider, bail if we can't find it */
249                 HashModule = ServerInstance->FindModule("m_md5.so");
250                 if (!HashModule)
251                         throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
252
253                 /* Create new mode handler object */
254                 cu = new CloakUser(ServerInstance, this, HashModule);
255
256                 /* Register it with the core */         
257                 if (!ServerInstance->AddMode(cu, 'x'))
258                         throw ModuleException("Could not add new modes!");
259
260                 OnRehash(NULL,"");
261         }
262         
263         virtual ~ModuleCloaking()
264         {
265                 ServerInstance->Modes->DelMode(cu);
266                 DELETE(cu);
267                 ServerInstance->DoneWithInterface("HashRequest");
268         }
269         
270         virtual Version GetVersion()
271         {
272                 // returns the version number of the module to be
273                 // listed in /MODULES
274                 return Version(1,1,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
275         }
276
277         virtual void OnRehash(userrec* user, const std::string &parameter)
278         {
279                 cu->DoRehash();
280         }
281
282         void Implements(char* List)
283         {
284                 List[I_OnRehash] = 1;
285         }
286 };
287
288 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
289
290 class ModuleCloakingFactory : public ModuleFactory
291 {
292  public:
293         ModuleCloakingFactory()
294         {
295         }
296         
297         ~ModuleCloakingFactory()
298         {
299         }
300         
301         virtual Module * CreateModule(InspIRCd* Me)
302         {
303                 return new ModuleCloaking(Me);
304         }
305         
306 };
307
308
309 extern "C" void * init_module( void )
310 {
311         return new ModuleCloakingFactory;
312 }