]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
Make m_pgsql return the number of affected rows for an UPDATE or INSERT query.
[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                 if (adding)
295                 {
296                         if(!dest->IsModeSet('x'))
297                         {
298                                 /* The mode is being turned on - so attempt to
299                                  * allocate the user a cloaked host using a non-reversible
300                                  * algorithm (its simple, but its non-reversible so the
301                                  * simplicity doesnt really matter). This algorithm
302                                  * will not work if the user has only one level of domain
303                                  * naming in their hostname (e.g. if they are on a lan or
304                                  * are connecting via localhost) -- this doesnt matter much.
305                                  */
306                         
307                                 if (strchr(dest->host,'.'))
308                                 {
309                                         /* InspIRCd users have two hostnames; A displayed
310                                          * hostname which can be modified by modules (e.g.
311                                          * to create vhosts, implement chghost, etc) and a
312                                          * 'real' hostname which you shouldnt write to.
313                                          */
314                                 
315                                         std::string a = strstr(dest->host,".");
316                                         
317                                         char ra[64];
318                                         this->GenHash(dest->host,ra);
319                                         std::string b = "";
320                                         in_addr testaddr;
321                                         std::string hostcloak = prefix + "-" + std::string(ra) + a;
322                                 
323                                         /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
324                                          * according to the DNS RFC) then tough titty, they get cloaked as an IP. 
325                                          * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
326                                          * vhost.
327                                          */
328                                 
329                                         if ((!inet_aton(dest->host,&testaddr)) && (hostcloak.length() < 64))
330                                         {
331                                                 // if they have a hostname, make something appropriate
332                                                 b = hostcloak;
333                                         }
334                                         else
335                                         {
336                                                 // else, they have an ip
337                                                 b = std::string(ra) + "." + prefix + ".cloak";
338                                         }
339                                         Srv->Log(DEBUG,"cloak: allocated "+b);
340                                         Srv->ChangeHost(dest,b);
341                                 }
342                                 
343                                 dest->SetMode('x',true);
344                                 return MODEACTION_ALLOW;
345                         }
346                 }
347                 else
348                 {
349                         if (dest->IsModeSet('x'))
350                         {
351                                 /* User is removing the mode, so just restore their real host
352                                  * and make it match the displayed one.
353                                  */
354                                 Srv->ChangeHost(dest,dest->host);
355                                 dest->SetMode('x',false);
356                                 return MODEACTION_ALLOW;
357                         }
358                 }
359
360                 return MODEACTION_DENY;
361         }
362         
363         void DoRehash()
364         {
365                 ConfigReader Conf;
366                 key1 = key2 = key3 = key4 = 0;
367                 key1 = Conf.ReadInteger("cloak","key1",0,false);
368                 key2 = Conf.ReadInteger("cloak","key2",0,false);
369                 key3 = Conf.ReadInteger("cloak","key3",0,false);
370                 key4 = Conf.ReadInteger("cloak","key4",0,false);
371                 
372                 prefix = Conf.ReadValue("cloak","prefix",0);
373                 if (prefix == "")
374                 {
375                         prefix = Srv->GetNetworkName();
376                 }
377                 if (!key1 && !key2 && !key3 && !key4)
378                 {
379                         ModuleException ex("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
380                         throw (ex);
381                 }
382         }
383 };
384
385
386 class ModuleCloaking : public Module
387 {
388  private:
389         Server *Srv;
390         CloakUser* cu;
391
392  public:
393         ModuleCloaking(Server* Me)
394         : Module::Module(Me), Srv(Me)
395         {
396                 /* Create new mode handler object */
397                 cu = new CloakUser(Srv);
398
399                 /* Register it with the core */         
400                 Srv->AddMode(cu, 'x');
401
402                 OnRehash("");
403         }
404         
405         virtual ~ModuleCloaking()
406         {
407                 DELETE(cu);
408         }
409         
410         virtual Version GetVersion()
411         {
412                 // returns the version number of the module to be
413                 // listed in /MODULES
414                 return Version(1,0,0,2,VF_STATIC|VF_VENDOR);
415         }
416
417         virtual void OnRehash(const std::string &parameter)
418         {
419                 cu->DoRehash();
420         }
421
422         void Implements(char* List)
423         {
424                 List[I_OnRehash] = List[I_OnUserConnect] = 1;
425         }
426
427         virtual void OnUserConnect(userrec* user)
428         {
429                 // Heres the weird bit. When a user connects we must set +x on them, so
430                 // we're going to use the SendMode method of the Server class to send
431                 // the mode to the client. This is basically the same as sending an
432                 // SAMODE in unreal. Note that to the user it will appear as if they set
433                 // the mode on themselves.
434                 
435                 const char* modes[2];           // only two parameters
436                 modes[0] = user->nick;          // first parameter is the nick
437                 modes[1] = "+x";                // second parameter is the mode
438                 Srv->SendMode(modes,2,user);    // send these, forming the command "MODE <nick> +x"
439         }
440
441 };
442
443 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
444
445 class ModuleCloakingFactory : public ModuleFactory
446 {
447  public:
448         ModuleCloakingFactory()
449         {
450         }
451         
452         ~ModuleCloakingFactory()
453         {
454         }
455         
456         virtual Module * CreateModule(Server* Me)
457         {
458                 return new ModuleCloaking(Me);
459         }
460         
461 };
462
463
464 extern "C" void * init_module( void )
465 {
466         return new ModuleCloakingFactory;
467 }