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