]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
33e41355cfd7b37f8ac22cbae717e80a31974ba3
[user/henk/code/inspircd.git] / src / modules / m_cloaking.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 // Hostname cloaking (+x mode) module for inspircd.
18 // version 1.0.0.1 by brain (C. J. Edwards) Mar 2004.
19 //
20 // When loaded this module will automatically set the
21 // +x mode on all connecting clients.
22 //
23 // Setting +x on a client causes the module to change the
24 // dhost entry (displayed host) for each user who has the
25 // mode, cloaking their host. Unlike unreal, the algorithm
26 // is non-reversible as uncloaked hosts are passed along
27 // the server->server link, and all encoding of hosts is
28 // done locally on the server by this module.
29
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include "inspircd_config.h"
35 #ifdef HAS_STDINT
36 #include <stdint.h>
37 #endif
38 #include "inspircd.h"
39 #include "users.h"
40 #include "channels.h"
41 #include "modules.h"
42
43 /* $ModDesc: Provides masking of user hostnames */
44
45
46 /* The four core functions - F1 is optimized somewhat */
47
48 #define F1(x, y, z) (z ^ (x & (y ^ z)))
49 #define F2(x, y, z) F1(z, x, y)
50 #define F3(x, y, z) (x ^ y ^ z)
51 #define F4(x, y, z) (y ^ (x | ~z))
52 #define MD5STEP(f,w,x,y,z,in,s) (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
53
54 #ifndef HAS_STDINT
55 typedef unsigned int uint32_t;
56 #endif
57
58 typedef uint32_t word32; /* NOT unsigned long. We don't support 16 bit platforms, anyway. */
59 typedef unsigned char byte;
60
61 struct xMD5Context {
62         word32 buf[4];
63         word32 bytes[2];
64         word32 in[16];
65 };
66
67 class CloakUser : public ModeHandler
68 {
69         Server* Srv;
70         std::string prefix;
71         word32 key1;
72         word32 key2;
73         word32 key3;
74         word32 key4;
75         
76         void byteSwap(word32 *buf, unsigned words)
77         {
78                 byte *p = (byte *)buf;
79         
80                 do
81                 {
82                         *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
83                                 ((unsigned)p[1] << 8 | p[0]);
84                         p += 4;
85                 } while (--words);
86         }
87
88         void xMD5Init(struct xMD5Context *ctx)
89         {
90                 ctx->buf[0] = key1;
91                 ctx->buf[1] = key2;
92                 ctx->buf[2] = key3;
93                 ctx->buf[3] = key4;
94
95                 ctx->bytes[0] = 0;
96                 ctx->bytes[1] = 0;
97         }
98
99         void xMD5Update(struct xMD5Context *ctx, byte const *buf, int len)
100         {
101                 word32 t;
102
103                 /* Update byte count */
104
105                 t = ctx->bytes[0];
106                 if ((ctx->bytes[0] = t + len) < t)
107                         ctx->bytes[1]++;        /* Carry from low to high */
108
109                 t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
110                 if ((unsigned)t > (unsigned)len)
111                 {
112                         memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, len);
113                         return;
114                 }
115                 /* First chunk is an odd size */
116                 memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, (unsigned)t);
117                 byteSwap(ctx->in, 16);
118                 xMD5Transform(ctx->buf, ctx->in);
119                 buf += (unsigned)t;
120                 len -= (unsigned)t;
121
122                 /* Process data in 64-byte chunks */
123                 while (len >= 64)
124                 {
125                         memcpy(ctx->in, buf, 64);
126                         byteSwap(ctx->in, 16);
127                         xMD5Transform(ctx->buf, ctx->in);
128                         buf += 64;
129                         len -= 64;
130                 }
131
132                 /* Handle any remaining bytes of data. */
133                 memcpy(ctx->in, buf, len);
134         }
135
136         void xMD5Final(byte digest[16], struct xMD5Context *ctx)
137         {
138                 int count = (int)(ctx->bytes[0] & 0x3f); /* Bytes in ctx->in */
139                 byte *p = (byte *)ctx->in + count;      /* First unused byte */
140
141                 /* Set the first char of padding to 0x80.  There is always room. */
142                 *p++ = 0x80;
143
144                 /* Bytes of padding needed to make 56 bytes (-8..55) */
145                 count = 56 - 1 - count;
146
147                 if (count < 0)
148                 {       /* Padding forces an extra block */
149                         memset(p, 0, count+8);
150                         byteSwap(ctx->in, 16);
151                         xMD5Transform(ctx->buf, ctx->in);
152                         p = (byte *)ctx->in;
153                         count = 56;
154                 }
155                 memset(p, 0, count+8);
156                 byteSwap(ctx->in, 14);
157
158                 /* Append length in bits and transform */
159                 ctx->in[14] = ctx->bytes[0] << 3;
160                 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
161                 xMD5Transform(ctx->buf, ctx->in);
162
163                 byteSwap(ctx->buf, 4);
164                 memcpy(digest, ctx->buf, 16);
165                 memset(ctx, 0, sizeof(ctx));
166         }
167
168         void xMD5Transform(word32 buf[4], word32 const in[16])
169         {
170                 register word32 a, b, c, d;
171
172                 a = buf[0];
173                 b = buf[1];
174                 c = buf[2];
175                 d = buf[3];
176
177                 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
178                 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
179                 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
180                 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
181                 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
182                 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
183                 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
184                 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
185                 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
186                 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
187                 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
188                 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
189                 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
190                 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
191                 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
192                 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
193
194                 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
195                 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
196                 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
197                 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
198                 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
199                 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
200                 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
201                 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
202                 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
203                 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
204                 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
205                 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
206                 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
207                 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
208                 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
209                 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
210
211                 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
212                 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
213                 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
214                 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
215                 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
216                 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
217                 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
218                 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
219                 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
220                 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
221                 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
222                 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
223                 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
224                 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
225                 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
226                 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
227
228                 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
229                 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
230                 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
231                 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
232                 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
233                 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
234                 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
235                 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
236                 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
237                 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
238                 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
239                 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
240                 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
241                 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
242                 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
243                 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
244
245                 buf[0] += a;
246                 buf[1] += b;
247                 buf[2] += c;
248                 buf[3] += d;
249         }
250
251
252         void MyMD5(void *dest, void *orig, int len)
253         {
254                 struct xMD5Context context;
255         
256                 xMD5Init(&context);
257                 xMD5Update(&context, (const unsigned char*)orig, len);
258                 xMD5Final((unsigned char*)dest, &context);
259         }
260
261
262         void GenHash(char* src, char* dest)
263         {
264                 // purposefully lossy md5 - only gives them the most significant 4 bits
265                 // of every md5 output byte.
266                 int i = 0;
267                 unsigned char bytes[16];
268                 char hash[MAXBUF];
269                 *hash = 0;
270                 MyMD5((char*)bytes,src,strlen(src));
271                 for (i = 0; i < 16; i++)
272                 {
273                         const char* xtab = "F92E45D871BCA630";
274                         unsigned char hi = xtab[bytes[i] / 16];
275                         char hx[2];
276                         hx[0] = hi;
277                         hx[1] = '\0';
278                         strlcat(hash,hx,MAXBUF);
279                 }
280                 strlcpy(dest,hash,MAXBUF);
281         }
282         
283  public:
284         CloakUser(Server* Me) : ModeHandler('x', 0, 0, false, MODETYPE_USER, false), Srv(Me) { }
285
286         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
287         {
288                 /* Only opers can change other users modes */
289                 if ((source != dest) && (!*source->oper))
290                         return MODEACTION_DENY;
291                 
292                 if (adding)
293                 {
294                         if(!dest->IsModeSet('x'))
295                         {
296                                 /* The mode is being turned on - so attempt to
297                                  * allocate the user a cloaked host using a non-reversible
298                                  * algorithm (its simple, but its non-reversible so the
299                                  * simplicity doesnt really matter). This algorithm
300                                  * will not work if the user has only one level of domain
301                                  * naming in their hostname (e.g. if they are on a lan or
302                                  * are connecting via localhost) -- this doesnt matter much.
303                                  */
304                         
305                                 if (strchr(dest->host,'.'))
306                                 {
307                                         /* InspIRCd users have two hostnames; A displayed
308                                          * hostname which can be modified by modules (e.g.
309                                          * to create vhosts, implement chghost, etc) and a
310                                          * 'real' hostname which you shouldnt write to.
311                                          */
312                                 
313                                         std::string a = strstr(dest->host,".");
314                                         
315                                         char ra[64];
316                                         this->GenHash(dest->host,ra);
317                                         std::string b = "";
318                                         in_addr testaddr;
319                                         std::string hostcloak = prefix + "-" + std::string(ra) + a;
320                                 
321                                         /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
322                                          * according to the DNS RFC) then tough titty, they get cloaked as an IP. 
323                                          * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
324                                          * vhost.
325                                          */
326                                 
327                                         if ((!inet_aton(dest->host,&testaddr)) && (hostcloak.length() < 64))
328                                         {
329                                                 // if they have a hostname, make something appropriate
330                                                 b = hostcloak;
331                                         }
332                                         else
333                                         {
334                                                 // else, they have an ip
335                                                 b = std::string(ra) + "." + prefix + ".cloak";
336                                         }
337                                         Srv->Log(DEBUG,"cloak: allocated "+b);
338                                         Srv->ChangeHost(dest,b);
339                                 }
340                                 
341                                 dest->SetMode('x',true);
342                                 return MODEACTION_ALLOW;
343                         }
344                 }
345                 else
346                 {
347                         if (dest->IsModeSet('x'))
348                         {
349                                 /* User is removing the mode, so just restore their real host
350                                  * and make it match the displayed one.
351                                  */
352                                 Srv->ChangeHost(dest,dest->host);
353                                 dest->SetMode('x',false);
354                                 return MODEACTION_ALLOW;
355                         }
356                 }
357
358                 return MODEACTION_DENY;
359         }
360         
361         void DoRehash()
362         {
363                 ConfigReader Conf;
364                 key1 = key2 = key3 = key4 = 0;
365                 key1 = Conf.ReadInteger("cloak","key1",0,false);
366                 key2 = Conf.ReadInteger("cloak","key2",0,false);
367                 key3 = Conf.ReadInteger("cloak","key3",0,false);
368                 key4 = Conf.ReadInteger("cloak","key4",0,false);
369                 
370                 prefix = Conf.ReadValue("cloak","prefix",0);
371                 if (prefix == "")
372                 {
373                         prefix = Srv->GetNetworkName();
374                 }
375                 if (!key1 && !key2 && !key3 && !key4)
376                 {
377                         ModuleException ex("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
378                         throw (ex);
379                 }
380         }
381 };
382
383
384 class ModuleCloaking : public Module
385 {
386  private:
387         Server *Srv;
388         CloakUser* cu;
389
390  public:
391         ModuleCloaking(Server* Me)
392         : Module::Module(Me), Srv(Me)
393         {
394                 /* Create new mode handler object */
395                 cu = new CloakUser(Srv);
396
397                 /* Register it with the core */         
398                 Srv->AddMode(cu, 'x');
399
400                 OnRehash("");
401         }
402         
403         virtual ~ModuleCloaking()
404         {
405                 DELETE(cu);
406         }
407         
408         virtual Version GetVersion()
409         {
410                 // returns the version number of the module to be
411                 // listed in /MODULES
412                 return Version(1,0,0,2,VF_STATIC|VF_VENDOR);
413         }
414
415         virtual void OnRehash(const std::string &parameter)
416         {
417                 cu->DoRehash();
418         }
419
420         void Implements(char* List)
421         {
422                 List[I_OnRehash] = List[I_OnUserConnect] = 1;
423         }
424
425         virtual void OnUserConnect(userrec* user)
426         {
427                 // Heres the weird bit. When a user connects we must set +x on them, so
428                 // we're going to use the SendMode method of the Server class to send
429                 // the mode to the client. This is basically the same as sending an
430                 // SAMODE in unreal. Note that to the user it will appear as if they set
431                 // the mode on themselves.
432                 
433                 char* modes[2];                 // only two parameters
434                 modes[0] = user->nick;          // first parameter is the nick
435                 modes[1] = "+x";                // second parameter is the mode
436                 Srv->SendMode(modes,2,user);    // send these, forming the command "MODE <nick> +x"
437         }
438
439 };
440
441 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
442
443 class ModuleCloakingFactory : public ModuleFactory
444 {
445  public:
446         ModuleCloakingFactory()
447         {
448         }
449         
450         ~ModuleCloakingFactory()
451         {
452         }
453         
454         virtual Module * CreateModule(Server* Me)
455         {
456                 return new ModuleCloaking(Me);
457         }
458         
459 };
460
461
462 extern "C" void * init_module( void )
463 {
464         return new ModuleCloakingFactory;
465 }