]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
Removed a pointless check in ./configure --clean that made it only work with one...
[user/henk/code/inspircd.git] / src / modules / m_cloaking.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd_config.h"
15 #include "configreader.h"
16 #include "inspircd.h"
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20
21 #include "m_hash.h"
22
23 /* $ModDesc: Provides masking of user hostnames */
24 /* $ModDep: m_hash.h */
25
26 /* Used to vary the output a little more depending on the cloak keys */
27 static const char* xtab[] = {"F92E45D871BCA630", "A1B9D80C72E653F4", "1ABC078934DEF562", "ABCDEF5678901234"};
28
29 /** Handles user mode +x
30  */
31 class CloakUser : public ModeHandler
32 {
33         
34         std::string prefix;
35         unsigned int key1;
36         unsigned int key2;
37         unsigned int key3;
38         unsigned int key4;
39         Module* Sender;
40         Module* HashProvider;
41         
42  public:
43         CloakUser(InspIRCd* Instance, Module* Source, Module* Hash) : ModeHandler(Instance, 'x', 0, 0, false, MODETYPE_USER, false), Sender(Source), HashProvider(Hash)
44         {
45         }
46
47         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
48         {
49                 /* Only opers can change other users modes */
50                 if ((source != dest) && (!*source->oper))
51                         return MODEACTION_DENY;
52
53                 /* For remote clients, we dont take any action, we just allow it.
54                  * The local server where they are will set their cloak instead.
55                  */
56                 if (!IS_LOCAL(dest))
57                         return MODEACTION_ALLOW;
58
59                 if (adding)
60                 {
61                         if(!dest->IsModeSet('x'))
62                         {
63                                 /* The mode is being turned on - so attempt to
64                                  * allocate the user a cloaked host using a non-reversible
65                                  * algorithm (its simple, but its non-reversible so the
66                                  * simplicity doesnt really matter). This algorithm
67                                  * will not work if the user has only one level of domain
68                                  * naming in their hostname (e.g. if they are on a lan or
69                                  * are connecting via localhost) -- this doesnt matter much.
70                                  */
71
72                                 char* n1 = strchr(dest->host,'.');
73                                 char* n2 = strchr(dest->host,':');
74                         
75                                 if (n1 || n2)
76                                 {
77                                         /* InspIRCd users have two hostnames; A displayed
78                                          * hostname which can be modified by modules (e.g.
79                                          * to create vhosts, implement chghost, etc) and a
80                                          * 'real' hostname which you shouldnt write to.
81                                          */
82
83                                         unsigned int iv[] = { key1, key2, key3, key4 };
84                                         std::string a = (n1 ? n1 : n2);
85                                         std::string b;
86                                         insp_inaddr testaddr;
87
88                                         /** Reset the Hash module, and send it our IV and hex table */
89                                         HashResetRequest(Sender, HashProvider).Send();
90                                         HashKeyRequest(Sender, HashProvider, iv).Send();
91                                         HashHexRequest(Sender, HashProvider, xtab[(*dest->host) % 4]);
92
93                                         /* Generate a cloak using specialized Hash */
94                                         std::string hostcloak = prefix + "-" + std::string(HashSumRequest(Sender, HashProvider, dest->host).Send()).substr(0,8) + a;
95
96                                         /* Fix by brain - if the cloaked host is > the max length of a host (64 bytes
97                                          * according to the DNS RFC) then tough titty, they get cloaked as an IP. 
98                                          * Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
99                                          * vhost.
100                                          */
101
102                                         if ((insp_aton(dest->host,&testaddr) < 1) && (hostcloak.length() <= 64))
103                                         {
104                                                 // if they have a hostname, make something appropriate
105                                                 b = hostcloak;
106                                         }
107                                         else
108                                         {
109                                                 b = ((b.find(':') == std::string::npos) ? Cloak4(dest->host) : Cloak6(dest->host));
110                                         }
111                                         dest->ChangeDisplayedHost(b.c_str());
112                                 }
113                                 
114                                 dest->SetMode('x',true);
115                                 return MODEACTION_ALLOW;
116                         }
117                 }
118                 else
119                 {
120                         if (dest->IsModeSet('x'))
121                         {
122                                 /* User is removing the mode, so just restore their real host
123                                  * and make it match the displayed one.
124                                  */
125                                 dest->ChangeDisplayedHost(dest->host);
126                                 dest->SetMode('x',false);
127                                 return MODEACTION_ALLOW;
128                         }
129                 }
130
131                 return MODEACTION_DENY;
132         }
133
134         std::string Cloak4(const char* ip)
135         {
136                 unsigned int iv[] = { key1, key2, key3, key4 };
137                 irc::sepstream seps(ip, '.');
138                 std::string ra[4];;
139                 std::string octet[4];
140                 int i[4];
141
142                 for (int j = 0; j < 4; j++)
143                 {
144                         octet[j] = seps.GetToken();
145                         i[j] = atoi(octet[j].c_str());
146                 }
147
148                 octet[3] = octet[0] + "." + octet[1] + "." + octet[2] + "." + octet[3];
149                 octet[2] = octet[0] + "." + octet[1] + "." + octet[2];
150                 octet[1] = octet[0] + "." + octet[1];
151
152                 /* Reset the Hash module and send it our IV */
153                 HashResetRequest(Sender, HashProvider).Send();
154                 HashKeyRequest(Sender, HashProvider, iv).Send();
155
156                 /* Send the Hash module a different hex table for each octet group's Hash sum */
157                 for (int k = 0; k < 4; k++)
158                 {
159                         HashHexRequest(Sender, HashProvider, xtab[(iv[k]+i[k]) % 4]).Send();
160                         ra[k] = std::string(HashSumRequest(Sender, HashProvider, octet[k]).Send()).substr(0,6);
161                 }
162                 /* Stick them all together */
163                 return std::string().append(ra[0]).append(".").append(ra[1]).append(".").append(ra[2]).append(".").append(ra[3]);
164         }
165
166         std::string Cloak6(const char* ip)
167         {
168                 unsigned int iv[] = { key1, key2, key3, key4 };
169                 std::vector<std::string> hashies;
170                 std::string item = "";
171                 int rounds = 0;
172
173                 /* Reset the Hash module and send it our IV */
174                 HashResetRequest(Sender, HashProvider).Send();
175                 HashKeyRequest(Sender, HashProvider, iv).Send();
176
177                 for (const char* input = ip; *input; input++)
178                 {
179                         item += *input;
180                         if (item.length() > 5)
181                         {
182                                 /* Send the Hash module a different hex table for each octet group's Hash sum */
183                                 HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
184                                 hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,10));
185                                 item = "";
186                         }
187                         rounds++;
188                 }
189                 if (!item.empty())
190                 {
191                         /* Send the Hash module a different hex table for each octet group's Hash sum */
192                         HashHexRequest(Sender, HashProvider, xtab[(key1+rounds) % 4]).Send();
193                         hashies.push_back(std::string(HashSumRequest(Sender, HashProvider, item).Send()).substr(0,10));
194                         item = "";
195                 }
196                 /* Stick them all together */
197                 return irc::stringjoiner(":", hashies, 0, hashies.size() - 1).GetJoined();
198         }
199         
200         void DoRehash()
201         {
202                 ConfigReader Conf(ServerInstance);
203                 key1 = key2 = key3 = key4 = 0;
204                 key1 = Conf.ReadInteger("cloak","key1",0,true);
205                 key2 = Conf.ReadInteger("cloak","key2",0,true);
206                 key3 = Conf.ReadInteger("cloak","key3",0,true);
207                 key4 = Conf.ReadInteger("cloak","key4",0,true);
208                 prefix = Conf.ReadValue("cloak","prefix",0);
209
210                 if (prefix.empty())
211                         prefix = ServerInstance->Config->Network;
212
213                 if (!key1 && !key2 && !key3 && !key4)
214                         throw ModuleException("You have not defined cloak keys for m_cloaking!!! THIS IS INSECURE AND SHOULD BE CHECKED!");
215         }
216 };
217
218
219 class ModuleCloaking : public Module
220 {
221  private:
222         
223         CloakUser* cu;
224         Module* HashModule;
225
226  public:
227         ModuleCloaking(InspIRCd* Me)
228                 : Module::Module(Me)
229         {
230                 ServerInstance->UseInterface("HashRequest");
231
232                 /* Attempt to locate the md5 service provider, bail if we can't find it */
233                 HashModule = ServerInstance->FindModule("m_md5.so");
234                 if (!HashModule)
235                         throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_cloaking.so.");
236
237                 /* Create new mode handler object */
238                 cu = new CloakUser(ServerInstance, this, HashModule);
239
240                 /* Register it with the core */         
241                 ServerInstance->AddMode(cu, 'x');
242
243                 OnRehash("");
244         }
245         
246         virtual ~ModuleCloaking()
247         {
248                 ServerInstance->Modes->DelMode(cu);
249                 DELETE(cu);
250                 ServerInstance->DoneWithInterface("HashRequest");
251         }
252         
253         virtual Version GetVersion()
254         {
255                 // returns the version number of the module to be
256                 // listed in /MODULES
257                 return Version(1,1,0,2,VF_COMMON|VF_VENDOR,API_VERSION);
258         }
259
260         virtual void OnRehash(const std::string &parameter)
261         {
262                 cu->DoRehash();
263         }
264
265         void Implements(char* List)
266         {
267                 List[I_OnRehash] = 1;
268         }
269 };
270
271 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
272
273 class ModuleCloakingFactory : public ModuleFactory
274 {
275  public:
276         ModuleCloakingFactory()
277         {
278         }
279         
280         ~ModuleCloakingFactory()
281         {
282         }
283         
284         virtual Module * CreateModule(InspIRCd* Me)
285         {
286                 return new ModuleCloaking(Me);
287         }
288         
289 };
290
291
292 extern "C" void * init_module( void )
293 {
294         return new ModuleCloakingFactory;
295 }