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