1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 // Hostname cloaking (+x mode) module for inspircd.
20 // version 1.0.0.1 by brain (C. J. Edwards) Mar 2004.
22 // When loaded this module will automatically set the
23 // +x mode on all connecting clients.
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.
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
41 /* $ModDesc: Provides masking of user hostnames */
44 /* The four core functions - F1 is optimized somewhat */
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)
52 typedef uint32_t word32; /* NOT unsigned long. We don't support 16 bit platforms, anyway. */
53 typedef unsigned char byte;
61 class ModuleCloaking : public Module
72 void byteSwap(word32 *buf, unsigned words)
74 byte *p = (byte *)buf;
78 *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
79 ((unsigned)p[1] << 8 | p[0]);
84 void xMD5Init(struct xMD5Context *ctx)
95 void xMD5Update(struct xMD5Context *ctx, byte const *buf, int len)
99 /* Update byte count */
102 if ((ctx->bytes[0] = t + len) < t)
103 ctx->bytes[1]++; /* Carry from low to high */
105 t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
106 if ((unsigned)t > (unsigned)len)
108 memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, len);
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);
118 /* Process data in 64-byte chunks */
121 memcpy(ctx->in, buf, 64);
122 byteSwap(ctx->in, 16);
123 xMD5Transform(ctx->buf, ctx->in);
128 /* Handle any remaining bytes of data. */
129 memcpy(ctx->in, buf, len);
132 void xMD5Final(byte digest[16], struct xMD5Context *ctx)
134 int count = (int)(ctx->bytes[0] & 0x3f); /* Bytes in ctx->in */
135 byte *p = (byte *)ctx->in + count; /* First unused byte */
137 /* Set the first char of padding to 0x80. There is always room. */
140 /* Bytes of padding needed to make 56 bytes (-8..55) */
141 count = 56 - 1 - count;
144 { /* Padding forces an extra block */
145 memset(p, 0, count+8);
146 byteSwap(ctx->in, 16);
147 xMD5Transform(ctx->buf, ctx->in);
151 memset(p, 0, count+8);
152 byteSwap(ctx->in, 14);
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);
159 byteSwap(ctx->buf, 4);
160 memcpy(digest, ctx->buf, 16);
161 memset(ctx, 0, sizeof(ctx));
164 void xMD5Transform(word32 buf[4], word32 const in[16])
166 register word32 a, b, c, d;
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);
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);
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);
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);
248 void MyMD5(void *dest, void *orig, int len)
250 struct xMD5Context context;
253 xMD5Update(&context, (const unsigned char*)orig, len);
254 xMD5Final((unsigned char*)dest, &context);
258 void GenHash(char* src, char* dest)
260 // purposefully lossy md5 - only gives them the most significant 4 bits
261 // of every md5 output byte.
263 unsigned char bytes[16];
266 MyMD5((char*)bytes,src,strlen(src));
267 for (i = 0; i < 16; i++)
269 const char* xtab = "F92E45D871BCA630";
270 unsigned char hi = xtab[bytes[i] / 16];
274 strlcat(hash,hx,MAXBUF);
276 strlcpy(dest,hash,MAXBUF);
280 ModuleCloaking(Server* Me)
283 // We must take a copy of the Server class to work with
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);
294 virtual ~ModuleCloaking()
298 virtual Version GetVersion()
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);
305 virtual void OnRehash(const std::string ¶meter)
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);
316 prefix = Srv->GetNetworkName();
318 if (!key1 && !key2 && !key3 && !key4)
320 ModuleException ex("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
324 /*ctx->buf[0] = 0x67452301;
325 ctx->buf[1] = 0xefcdab89;
326 ctx->buf[2] = 0x98badcfe;
327 ctx->buf[3] = 0x10325476;*/
330 void Implements(char* List)
332 List[I_OnRehash] = List[I_OnExtendedMode] = List[I_OnUserConnect] = 1;
335 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
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))
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;
350 // we've now determined that this is our mode character...
351 // is the user adding the mode to their list or removing it?
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,'.'))
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,".");
369 this->GenHash(dest->host,ra);
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
378 if ((!inet_aton(dest->host,&testaddr)) && (hostcloak.length() < 64))
380 // if they have a hostname, make something appropriate
385 // else, they have an ip
386 b = std::string(ra) + "." + prefix + ".cloak";
388 Srv->Log(DEBUG,"cloak: allocated "+b);
389 Srv->ChangeHost(dest,b);
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);
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.
406 // this mode isn't ours, we have to bail and return 0 to not handle it.
411 virtual void OnUserConnect(userrec* user)
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.
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"
427 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
429 class ModuleCloakingFactory : public ModuleFactory
432 ModuleCloakingFactory()
436 ~ModuleCloakingFactory()
440 virtual Module * CreateModule(Server* Me)
442 return new ModuleCloaking(Me);
448 extern "C" void * init_module( void )
450 return new ModuleCloakingFactory;