]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
0ff2ecf4ca577ce9dd43854b7c34c65791dfd0fe
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *               <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *               <omster@gmail.com>
10  *     
11  * Written by Craig Edwards, Craig McLure, and others.
12  * This program is free but copyrighted software; see
13  *            the file COPYING for details.
14  *
15  * ---------------------------------------------------
16  */
17
18 #include <vector>
19 #include <string>
20 #include <stdlib.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include "users.h"
25 #include "modules.h"
26 #include "helperfuncs.h"
27 #include "dns.h"
28
29 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
30
31
32 /* We need this for checking our user hasnt /quit before we finish our lookup */
33 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
34
35 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST };
36
37 class CGIhost : public classbase
38 {
39 public:
40         std::string hostmask;
41         CGItype type;
42
43         CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST)
44         : hostmask(mask), type(t)
45         {
46         }
47 };
48
49 typedef std::vector<CGIhost> CGIHostlist;
50
51 class CGIResolver : public Resolver
52 {
53         std::string typ;
54         int theirfd;
55         userrec* them;
56         bool notify;
57  public:
58         CGIResolver(bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type)
59                 : Resolver(source, forward, ""), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
60
61         virtual void OnLookupComplete(const std::string &result)
62         {
63                 /* Check the user still exists */
64                 if ((them) && (them == fd_ref_table[theirfd]))
65                 {
66                         if (notify)
67                                 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick, them->host, result.c_str(), typ.c_str());
68
69                         strlcpy(them->host, result.c_str(), 16);
70                         strlcpy(them->dhost, result.c_str(), 16);
71                         strlcpy(them->ident, "~cgiirc", 8);
72                 }
73         }
74
75         virtual ~CGIResolver()
76         {
77         }
78 };
79
80 class ModuleCgiIRC : public Module
81 {
82         Server *Srv;
83         bool NotifyOpers;
84         CGIHostlist Hosts;
85 public:
86         ModuleCgiIRC(Server* Me) : Module::Module(Me)
87         {
88                 Srv = Me;
89                 OnRehash("");
90         }
91
92         void Implements(char* List)
93         {
94                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = 1;
95         }
96         
97         virtual Priority Prioritize()
98         {
99                 // We want to get here before m_cloaking and m_hostchange etc
100                 return PRIORITY_FIRST;
101         }
102
103         virtual void OnRehash(const std::string &parameter)
104         {
105                 ConfigReader Conf;
106                 
107                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
108                 
109                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
110                         NotifyOpers = true;
111                 
112                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
113                 {
114                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
115                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
116                         
117                         if(hostmask.length())
118                         {
119                                 Hosts.push_back(CGIhost(hostmask));
120                                 
121                                 if(type == "pass")
122                                         Hosts.back().type = PASS;
123                                 else if(type == "ident")
124                                         Hosts.back().type = IDENT;
125                                 else if(type == "passfirst")
126                                         Hosts.back().type = PASSFIRST;
127                         }
128                         else
129                         {
130                                 log(DEBUG, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
131                                 continue;
132                         }
133                 }
134         }
135
136         virtual void OnCleanup(int target_type, void* item)
137         {
138                 if(target_type == TYPE_USER)
139                 {
140                         userrec* user = (userrec*)item;
141                         std::string* realhost;
142                         std::string* realip;
143                         
144                         if(user->GetExt("cgiirc_realhost", realhost))
145                         {
146                                 delete realhost;
147                                 user->Shrink("cgiirc_realhost");
148                         }
149                         
150                         if(user->GetExt("cgiirc_realip", realip))
151                         {
152                                 delete realip;
153                                 user->Shrink("cgiirc_realip");
154                         }
155                 }
156         }
157         
158         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
159         {
160                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
161                 {
162                         std::string* data;
163                         
164                         if(user->GetExt(extname, data))
165                         {
166                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
167                         }
168                 }
169         }
170
171         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
172         {
173                 if(target_type == TYPE_USER)
174                 {
175                         userrec* dest = (userrec*)target;
176                         std::string* bleh;
177                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
178                         {
179                                 dest->Extend(extname, new std::string(extdata));
180                         }
181                 }
182         }
183
184         virtual void OnUserQuit(userrec* user, const std::string &message)
185         {
186                 OnCleanup(TYPE_USER, user);
187         }
188         
189
190         virtual void OnUserRegister(userrec* user)
191         {
192                 log(DEBUG, "m_cgiirc.so: User %s registering", user->nick);
193                 
194                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
195                 {
196                         log(DEBUG, "m_cgiirc.so: Matching %s against (%s or %s)", iter->hostmask.c_str(), user->host, inet_ntoa(user->ip4));
197                         
198                         if(Srv->MatchText(user->host, iter->hostmask) || Srv->MatchText(inet_ntoa(user->ip4), iter->hostmask))
199                         {
200                                 // Deal with it...
201                                 log(DEBUG, "m_cgiirc.so: Handling CGI:IRC user: %s (%s) matched %s", user->GetFullRealHost(), inet_ntoa(user->ip4), iter->hostmask.c_str());
202                                 
203                                 if(iter->type == PASS)
204                                 {
205                                         CheckPass(user); // We do nothing if it fails so...
206                                 }
207                                 else if(iter->type == PASSFIRST && !CheckPass(user))
208                                 {
209                                         // If the password lookup failed, try the ident
210                                         CheckIdent(user);       // If this fails too, do nothing
211                                 }
212                                 else if(iter->type == IDENT)
213                                 {
214                                         CheckIdent(user); // Nothing on failure.
215                                 }
216                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
217                                 {
218                                         // If the ident lookup fails, try the password.
219                                         CheckPass(user);
220                                 }
221                                 
222                                 return;
223                         }
224                 }
225         }
226
227         bool CheckPass(userrec* user)
228         {
229                 log(DEBUG, "m_cgiirc.so: CheckPass(%s) - %s", user->nick, user->password);
230                 
231                 if(IsValidHost(user->password))
232                 {
233                         user->Extend("cgiirc_realhost", new std::string(user->host));
234                         user->Extend("cgiirc_realip", new std::string(inet_ntoa(user->ip4)));
235                         strlcpy(user->host, user->password, 64);
236                         strlcpy(user->dhost, user->password, 64);
237                         
238                         if(inet_aton(user->password, &user->ip4))
239                         {
240                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
241                                 log(DEBUG, "m_cgiirc.so: Got an IP in the user's password");
242
243                                 if(NotifyOpers)
244                                         WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
245                         }
246                         else
247                         {
248                                 /* We got as resolved hostname in the password. */
249                                 log(DEBUG, "m_cgiirc.so: Got a hostname in the user's password");
250
251                                 try
252                                 {
253                                         CGIResolver* r = new CGIResolver(NotifyOpers, user->password, false, user, user->fd, "PASS");
254                                         Srv->AddResolver(r);
255                                 }
256                                 catch (ModuleException& e)
257                                 {
258                                         if (NotifyOpers)
259                                                 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
260                                 }
261                         }
262                         
263                         *user->password = 0;
264
265                         /*if(NotifyOpers)
266                                 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
267
268                         return true;
269                 }
270                 else
271                 {
272                         log(DEBUG, "m_cgiirc.so: User's password was not a valid host");
273                 }
274                 
275                 return false;
276         }
277         
278         bool CheckIdent(userrec* user)
279         {
280                 int ip[4];
281                 char* ident;
282                 char newip[16];
283                 int len = strlen(user->ident);
284                 
285                 if(len == 8)
286                         ident = user->ident;
287                 else if(len == 9 && *user->ident == '~')
288                         ident = user->ident+1;
289                 else
290                         return false;
291         
292                 for(int i = 0; i < 4; i++)
293                         if(!HexToInt(ip[i], ident + i*2))
294                                 return false;
295
296                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
297                         
298                 user->Extend("cgiirc_realhost", new std::string(user->host));
299                 user->Extend("cgiirc_realip", new std::string(inet_ntoa(user->ip4)));
300                 inet_aton(newip, &user->ip4);
301
302                 try
303                 {
304                         CGIResolver* r = new CGIResolver(NotifyOpers, newip, false, user, user->fd, "IDENT");
305                         Srv->AddResolver(r);
306                 }
307                 catch (ModuleException& e)
308                 {
309                         strlcpy(user->host, newip, 16);
310                         strlcpy(user->dhost, newip, 16);
311                         strlcpy(user->ident, "~cgiirc", 8);
312
313                         if(NotifyOpers)
314                                  WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
315                 }
316                 /*strlcpy(user->host, newip, 16);
317                 strlcpy(user->dhost, newip, 16);
318                 strlcpy(user->ident, "~cgiirc", 8);*/
319                 
320                 /*if(NotifyOpers)
321                         WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from ident", user->nick, user->host, newip);*/
322
323                 return true;
324         }
325         
326         bool IsValidHost(const std::string &host)
327         {
328                 if(!host.size())
329                         return false;
330         
331                 for(unsigned int i = 0; i < host.size(); i++)
332                 {
333                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
334                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
335                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
336                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
337                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
338                                         
339                                 continue;
340                         else
341                                 return false;
342                 }
343                 
344                 return true;
345         }
346
347         bool IsValidIP(const std::string &ip)
348         {
349                 if(ip.size() < 7 || ip.size() > 15)
350                         return false;
351         
352                 short sincedot = 0;
353                 short dots = 0;
354         
355                 for(unsigned int i = 0; i < ip.size(); i++)
356                 {
357                         if((dots <= 3) && (sincedot <= 3))
358                         {
359                                 if((ip[i] >= '0') && (ip[i] <= '9'))
360                                 {
361                                         sincedot++;
362                                 }
363                                 else if(ip[i] == '.')
364                                 {
365                                         sincedot = 0;
366                                         dots++;
367                                 }
368                         }
369                         else
370                         {
371                                 return false;
372                         
373                         }
374                 }
375                 
376                 if(dots != 3)
377                         return false;
378                 
379                 return true;
380         }
381         
382         bool HexToInt(int &out, const char* in)
383         {
384                 char ip[3];
385                 ip[0] = in[0];
386                 ip[1] = in[1];
387                 ip[2] = 0;
388                 out = strtol(ip, NULL, 16);
389                 
390                 if(out > 255 || out < 0)
391                         return false;
392
393                 return true;
394         }
395         
396         virtual ~ModuleCgiIRC()
397         {
398         }
399          
400         virtual Version GetVersion()
401         {
402                 return Version(1,0,0,0,VF_VENDOR);
403         }
404         
405 };
406
407 class ModuleCgiIRCFactory : public ModuleFactory
408 {
409  public:
410         ModuleCgiIRCFactory()
411         {
412         }
413         
414         ~ModuleCgiIRCFactory()
415         {
416         }
417         
418         virtual Module * CreateModule(Server* Me)
419         {
420                 return new ModuleCgiIRC(Me);
421         }
422         
423 };
424
425
426 extern "C" void * init_module( void )
427 {
428         return new ModuleCgiIRCFactory;
429 }