]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
fb7ac134f1941a85206ae662eb34a0129eb098e8
[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 #include "inspircd_config.h"
18 #ifdef HAS_STDINT
19 #include <stdint.h>
20 #endif
21 #include "configreader.h"
22 #include "inspircd.h"
23 #include "users.h"
24 #include "channels.h"
25 #include "modules.h"
26
27 /* $ModDesc: Provides masking of user hostnames */
28
29 /* The four core functions - F1 is optimized somewhat */
30
31 #define F1(x, y, z) (z ^ (x & (y ^ z)))
32 #define F2(x, y, z) F1(z, x, y)
33 #define F3(x, y, z) (x ^ y ^ z)
34 #define F4(x, y, z) (y ^ (x | ~z))
35 #define MD5STEP(f,w,x,y,z,in,s) (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
36
37 #ifndef HAS_STDINT
38 typedef unsigned int uint32_t;
39 #endif
40
41 typedef uint32_t word32; /* NOT unsigned long. We don't support 16 bit platforms, anyway. */
42 typedef unsigned char byte;
43
44 /** An MD5 context, used by m_cloaking
45  */
46 class xMD5Context : public classbase
47 {
48  public:
49         word32 buf[4];
50         word32 bytes[2];
51         word32 in[16];
52 };
53
54 /** Handles user mode +x
55  */
56 class CloakUser : public ModeHandler
57 {
58         
59         std::string prefix;
60         word32 key1;
61         word32 key2;
62         word32 key3;
63         word32 key4;
64         
65         void byteSwap(word32 *buf, unsigned words)
66         {
67                 byte *p = (byte *)buf;
68         
69                 do
70                 {
71                         *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
72                                 ((unsigned)p[1] << 8 | p[0]);
73                         p += 4;
74                 } while (--words);
75         }
76
77         void xMD5Init(struct xMD5Context *ctx)
78         {
79                 ctx->buf[0] = key1;
80                 ctx->buf[1] = key2;
81                 ctx->buf[2] = key3;
82                 ctx->buf[3] = key4;
83
84                 ctx->bytes[0] = 0;
85                 ctx->bytes[1] = 0;
86         }
87
88         void xMD5Update(struct xMD5Context *ctx, byte const *buf, int len)
89         {
90                 word32 t;
91
92                 /* Update byte count */
93
94                 t = ctx->bytes[0];
95                 if ((ctx->bytes[0] = t + len) < t)
96                         ctx->bytes[1]++;        /* Carry from low to high */
97
98                 t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
99                 if ((unsigned)t > (unsigned)len)
100                 {
101                         memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, len);
102                         return;
103                 }
104                 /* First chunk is an odd size */
105                 memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, (unsigned)t);
106                 byteSwap(ctx->in, 16);
107                 xMD5Transform(ctx->buf, ctx->in);
108                 buf += (unsigned)t;
109                 len -= (unsigned)t;
110
111                 /* Process data in 64-byte chunks */
112                 while (len >= 64)
113                 {
114                         memcpy(ctx->in, buf, 64);
115                         byteSwap(ctx->in, 16);
116                         xMD5Transform(ctx->buf, ctx->in);
117                         buf += 64;
118                         len -= 64;
119                 }
120
121                 /* Handle any remaining bytes of data. */
122                 memcpy(ctx->in, buf, len);
123         }
124
125         void xMD5Final(byte digest[16], struct xMD5Context *ctx)
126         {
127                 int count = (int)(ctx->bytes[0] & 0x3f); /* Bytes in ctx->in */
128                 byte *p = (byte *)ctx->in + count;      /* First unused byte */
129
130                 /* Set the first char of padding to 0x80.  There is always room. */
131                 *p++ = 0x80;
132
133                 /* Bytes of padding needed to make 56 bytes (-8..55) */
134                 count = 56 - 1 - count;
135
136                 if (count < 0)
137                 {       /* Padding forces an extra block */
138                         memset(p, 0, count+8);
139                         byteSwap(ctx->in, 16);
140                         xMD5Transform(ctx->buf, ctx->in);
141                         p = (byte *)ctx->in;
142                         count = 56;
143                 }
144                 memset(p, 0, count+8);
145                 byteSwap(ctx->in, 14);
146
147                 /* Append length in bits and transform */
148                 ctx->in[14] = ctx->bytes[0] << 3;
149                 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
150                 xMD5Transform(ctx->buf, ctx->in);
151
152                 byteSwap(ctx->buf, 4);
153                 memcpy(digest, ctx->buf, 16);
154                 memset(ctx, 0, sizeof(ctx));
155         }
156
157         void xMD5Transform(word32 buf[4], word32 const in[16])
158         {
159                 register word32 a, b, c, d;
160
161                 a = buf[0];
162                 b = buf[1];
163                 c = buf[2];
164                 d = buf[3];
165
166                 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
167                 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
168                 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
169                 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
170                 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
171                 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
172                 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
173                 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
174                 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
175                 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
176                 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
177                 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
178                 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
179                 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
180                 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
181                 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
182
183                 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
184                 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
185                 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
186                 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
187                 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
188                 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
189                 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
190                 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
191                 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
192                 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
193                 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
194                 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
195                 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
196                 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
197                 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
198                 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
199
200                 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
201                 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
202                 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
203                 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
204                 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
205                 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
206                 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
207                 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
208                 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
209                 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
210                 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
211                 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
212                 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
213                 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
214                 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
215                 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
216
217                 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
218                 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
219                 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
220                 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
221                 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
222                 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
223                 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
224                 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
225                 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
226                 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
227                 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
228                 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
229                 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
230                 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
231                 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
232                 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
233
234                 buf[0] += a;
235                 buf[1] += b;
236                 buf[2] += c;
237                 buf[3] += d;
238         }
239
240
241         void MyMD5(void *dest, const void *orig, int len)
242         {
243                 struct xMD5Context context;
244         
245                 xMD5Init(&context);
246                 xMD5Update(&context, (const unsigned char*)orig, len);
247                 xMD5Final((unsigned char*)dest, &context);
248         }
249
250
251         void GenHash(const char* src, char* dest, int xtable = 0)
252         {
253                 // purposefully lossy md5 - only gives them the most significant 4 bits
254                 // of every md5 output byte.
255                 int i = 0;
256                 unsigned char bytes[16];
257                 char hash[MAXBUF];
258                 *hash = 0;
259                 MyMD5(bytes, src, strlen(src));
260                 for (i = 0; i < 16; i++)
261                 {
262                         const char* xtab[] = {"F92E45D871BCA630", "A1B9D80C72E653F4", "1ABC078934DEF562", "ABCDEF5678901234"};
263                         char hx[2] = { xtab[xtable][bytes[i] / 16], 0};
264                         strlcat(hash,hx,MAXBUF);
265                 }
266                 strlcpy(dest,hash,MAXBUF);
267         }
268         
269  public:
270         CloakUser(InspIRCd* Instance) : ModeHandler(Instance, 'x', 0, 0, false, MODETYPE_USER, false) { }
271
272         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
273         {
274                 /* Only opers can change other users modes */
275                 if ((source != dest) && (!*source->oper))
276                         return MODEACTION_DENY;
277
278                 /* For remote clients, we dont take any action, we just allow it.
279                  * The local server where they are will set their cloak instead.
280                  */
281                 if (!IS_LOCAL(dest))
282                         return MODEACTION_ALLOW;
283
284                 if (adding)
285                 {
286                         if(!dest->IsModeSet('x'))
287                         {
288                                 /* The mode is being turned on - so attempt to
289                                  * allocate the user a cloaked host using a non-reversible
290                                  * algorithm (its simple, but its non-reversible so the
291                                  * simplicity doesnt really matter). This algorithm
292                                  * will not work if the user has only one level of domain
293                                  * naming in their hostname (e.g. if they are on a lan or
294                                  * are connecting via localhost) -- this doesnt matter much.
295                                  */
296                         
297                                 if (strchr(dest->host,'.') || strchr(dest->host,':'))
298                                 {
299                                         /* InspIRCd users have two hostnames; A displayed
300                                          * hostname which can be modified by modules (e.g.
301                                          * to create vhosts, implement chghost, etc) and a
302                                          * 'real' hostname which you shouldnt write to.
303                                          */
304
305                                         char* n = strstr(dest->host,".");
306                                         if (!n)
307                                                 n = strstr(dest->host,":");
308
309                                         std::string a = n;
310                                         
311                                         char ra[64];
312                                         this->GenHash(dest->host,ra);
313                                         std::string b;
314                                         insp_inaddr testaddr;
315                                         std::string hostcloak = prefix + "-" + ra + a;
316                                 
317                                         /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
318                                          * according to the DNS RFC) then tough titty, they get cloaked as an IP. 
319                                          * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
320                                          * vhost.
321                                          */
322
323                                         if ((insp_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
324                                         {
325                                                 // if they have a hostname, make something appropriate
326                                                 b = hostcloak;
327                                         }
328                                         else
329                                         {
330                                                 b = ((b.find(':') == std::string::npos) ? Cloak4(dest->host) : Cloak6(dest->host));
331                                         }
332                                         ServerInstance->Log(DEBUG,"cloak: allocated "+b);
333                                         dest->ChangeDisplayedHost(b.c_str());
334                                 }
335                                 
336                                 dest->SetMode('x',true);
337                                 return MODEACTION_ALLOW;
338                         }
339                 }
340                 else
341                 {
342                         if (dest->IsModeSet('x'))
343                         {
344                                 /* User is removing the mode, so just restore their real host
345                                  * and make it match the displayed one.
346                                  */
347                                 dest->ChangeDisplayedHost(dest->host);
348                                 dest->SetMode('x',false);
349                                 return MODEACTION_ALLOW;
350                         }
351                 }
352
353                 return MODEACTION_DENY;
354         }
355
356         std::string Cloak4(const char* ip)
357         {
358                 irc::sepstream seps(ip, '.');
359                 char ra1[64], ra2[64], ra3[64], ra4[64];
360                 int i1, i2, i3, i4;
361                 std::string octet1 = seps.GetToken();
362                 std::string octet2 = seps.GetToken();
363                 std::string octet3 = seps.GetToken();
364                 std::string octet4 = seps.GetToken();
365                 i1 = atoi(octet1.c_str());
366                 i2 = atoi(octet2.c_str());
367                 i3 = atoi(octet3.c_str());
368                 i4 = atoi(octet4.c_str());
369                 octet4 = octet1 + "." + octet2 + "." + octet3 + "." + octet4;
370                 octet3 = octet1 + "." + octet2 + "." + octet3;
371                 octet2 = octet1 + "." + octet2;
372                 this->GenHash(octet1.c_str(),ra1, (key1+i1) % 4);
373                 this->GenHash(octet2.c_str(),ra2, (key2+i2) % 4);
374                 this->GenHash(octet3.c_str(),ra3, (key3+i3) % 4);
375                 this->GenHash(octet4.c_str(),ra4, (key4+i4) % 4);
376                 /* This is safe as we know the length generated by our genhash is always 16 */
377                 ra1[8] = ra2[8] = ra3[8] = ra4[8] = 0;
378                 return std::string().append(ra1).append(".").append(ra2).append(".").append(ra3).append(".").append(ra4);
379         }
380
381         std::string Cloak6(const char* ip)
382         {
383                 std::vector<std::string> hashies;
384                 std::string item = "";
385                 int rounds = 0;
386                 for (const char* input = ip; *input; input++)
387                 {
388                         item += *input;
389                         if (item.length() > 5)
390                         {
391                                 char ra[64];
392                                 this->GenHash(item.c_str(), ra, (key1+rounds) % 4);
393                                 ra[9] = 0;
394                                 hashies.push_back(ra);
395                                 item = "";
396                         }
397                         rounds++;
398                 }
399                 if (!item.empty())
400                 {
401                         char ra[64];
402                         this->GenHash(item.c_str(), ra, (key1+rounds) % 4);
403                         ra[9] = 0;
404                         hashies.push_back(ra);
405                 }
406                 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
407         }
408
409         /* XXX: Uncomment and call to use the test suite
410         void TestSuite()
411         {
412                 printf("%s %s\n", "192.168.1.1", Cloak4("192.168.1.1").c_str());
413                 printf("%s %s\n", "192.168.1.2", Cloak4("192.168.1.2").c_str());
414                 printf("%s %s\n", "192.168.10.1", Cloak4("192.168.10.1").c_str());
415                 printf("%s %s\n", "192.168.10.1", Cloak4("192.168.10.2").c_str());
416                 printf("%s %s\n", "192.169.1.1", Cloak4("192.169.1.1").c_str());
417                 printf("%s %s\n", "192.169.2.1", Cloak4("192.169.2.1").c_str());
418                 printf("%s %s\n", "200.168.1.1", Cloak4("200.168.1.1").c_str());
419                 printf("%s %s\n", "200.168.1.3", Cloak4("200.168.1.3").c_str());
420                 printf("%s %s\n", "200.168.3.3", Cloak4("200.168.3.3").c_str());
421                 printf("%s %s\n", "200.169.4.3", Cloak4("200.169.4.3").c_str());
422                 printf("---\n");
423                 printf("%s %s\n", "9a05:2f00:3f11::5f12::1", Cloak6("9a05:2f00:3f11::5f12::1").c_str());
424                 printf("%s %s\n", "9a05:2f00:3f11::5f12::2", Cloak6("9a05:2f00:3f11::5f12::2").c_str());
425                 printf("%s %s\n", "9a05:2f00:3f11::5a12::1", Cloak6("9a05:2f00:3f11::5a12::1").c_str());
426                 printf("%s %s\n", "9a05:2f00:3f11::5a12::2", Cloak6("9a05:2f00:3f11::5a12::2").c_str());
427                 printf("%s %s\n", "9a05:3f01:3f11::5f12::1", Cloak6("9a05:3f01:3f11::5f12::1").c_str());
428                 printf("%s %s\n", "9a05:4f00:3f11::5f13::2", Cloak6("9a05:4f00:3f11::5f13::2").c_str());
429                 printf("%s %s\n", "ffff:2f00:3f11::5f12::1", Cloak6("ffff:2f00:3f11::5f12::1").c_str());
430                 printf("%s %s\n", "ffff:2f00:3f11::5f13::2", Cloak6("ffff:2f00:3f11::5f13::2").c_str());
431                 exit(0);
432         }
433         */
434         
435         void DoRehash()
436         {
437                 ConfigReader Conf(ServerInstance);
438                 key1 = key2 = key3 = key4 = 0;
439                 key1 = Conf.ReadInteger("cloak","key1",0,false);
440                 key2 = Conf.ReadInteger("cloak","key2",0,false);
441                 key3 = Conf.ReadInteger("cloak","key3",0,false);
442                 key4 = Conf.ReadInteger("cloak","key4",0,false);
443                 
444                 prefix = Conf.ReadValue("cloak","prefix",0);
445                 if (prefix == "")
446                 {
447                         prefix = ServerInstance->Config->Network;
448                 }
449                 if (!key1 && !key2 && !key3 && !key4)
450                 {
451                         ModuleException ex("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
452                         throw (ex);
453                 }
454         }
455 };
456
457
458 class ModuleCloaking : public Module
459 {
460  private:
461         
462         CloakUser* cu;
463
464  public:
465         ModuleCloaking(InspIRCd* Me)
466                 : Module::Module(Me)
467         {
468                 /* Create new mode handler object */
469                 cu = new CloakUser(ServerInstance);
470
471                 /* Register it with the core */         
472                 ServerInstance->AddMode(cu, 'x');
473
474                 OnRehash("");
475         }
476         
477         virtual ~ModuleCloaking()
478         {
479                 ServerInstance->Modes->DelMode(cu);
480                 DELETE(cu);
481         }
482         
483         virtual Version GetVersion()
484         {
485                 // returns the version number of the module to be
486                 // listed in /MODULES
487                 return Version(1,1,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
488         }
489
490         virtual void OnRehash(const std::string &parameter)
491         {
492                 cu->DoRehash();
493         }
494
495         void Implements(char* List)
496         {
497                 List[I_OnRehash] = 1;
498         }
499 };
500
501 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
502
503 class ModuleCloakingFactory : public ModuleFactory
504 {
505  public:
506         ModuleCloakingFactory()
507         {
508         }
509         
510         ~ModuleCloakingFactory()
511         {
512         }
513         
514         virtual Module * CreateModule(InspIRCd* Me)
515         {
516                 return new ModuleCloaking(Me);
517         }
518         
519 };
520
521
522 extern "C" void * init_module( void )
523 {
524         return new ModuleCloakingFactory;
525 }