]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
671588d186d2712c5b40de71760ed895bb47e902
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.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 <vector>
15 #include <string>
16 #include <stdlib.h>
17 #include "users.h"
18 #include "modules.h"
19 #include "dns.h"
20 #include "inspircd.h"
21 #ifndef WINDOWS
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #endif
26
27 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
28
29 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
30
31
32 /** Holds a CGI site's details
33  */
34 class CGIhost : public classbase
35 {
36 public:
37         std::string hostmask;
38         CGItype type;
39         std::string password;
40
41         CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &password ="")
42         : hostmask(mask), type(t), password(password)
43         {
44         }
45 };
46 typedef std::vector<CGIhost> CGIHostlist;
47
48 class cmd_webirc : public command_t
49 {
50         InspIRCd* Me;
51         CGIHostlist Hosts;
52         bool notify;
53         public:
54                 cmd_webirc(InspIRCd* Me, CGIHostlist &Hosts, bool notify) : command_t(Me, "WEBIRC", 0, 4, true), Hosts(Hosts), notify(notify)
55                 {
56                         this->source = "m_cgiirc.so";
57                         this->syntax = "password client hostname ip";
58                 }
59                 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
60                 {
61                         if(user->registered == REG_ALL)
62                                 return CMD_FAILURE;
63                         
64                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
65                         {
66                                 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
67                                 {
68                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
69                                         {
70                                                 user->Extend("cgiirc_realhost", new std::string(user->host));
71                                                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
72                                                 if (notify)
73                                                         ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick, user->host, parameters[2], user->host);
74                                                 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
75                                                 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
76                                                 return CMD_LOCALONLY;
77                                         }
78                                 }
79                         }
80                         return CMD_FAILURE;
81                 }
82 };
83
84
85 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
86  */
87 class CGIResolver : public Resolver
88 {
89         std::string typ;
90         int theirfd;
91         userrec* them;
92         bool notify;
93  public:
94         CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
95                 : Resolver(ServerInstance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
96
97         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
98         {
99                 /* Check the user still exists */
100                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
101                 {
102                         if (notify)
103                                 ServerInstance->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());
104
105                         strlcpy(them->host, result.c_str(), 63);
106                         strlcpy(them->dhost, result.c_str(), 63);
107                         strlcpy(them->ident, "~cgiirc", 8);
108                         them->InvalidateCache();
109                 }
110         }
111
112         virtual void OnError(ResolverError e, const std::string &errormessage)
113         {
114                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
115                 {
116                         if (notify)
117                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but their host can't be resolved from their %s!", them->nick, them->host,typ.c_str());
118                 }
119         }
120
121         virtual ~CGIResolver()
122         {
123         }
124 };
125
126 class ModuleCgiIRC : public Module
127 {
128         cmd_webirc* mycommand;
129         bool NotifyOpers;
130         CGIHostlist Hosts;
131 public:
132         ModuleCgiIRC(InspIRCd* Me) : Module(Me)
133         {
134                 
135                 OnRehash(NULL,"");
136                 mycommand=new cmd_webirc(Me, Hosts, NotifyOpers);
137                 ServerInstance->AddCommand(mycommand);
138         }
139
140         void Implements(char* List)
141         {
142                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = List[I_OnUserConnect] = 1;
143         }
144         
145         virtual Priority Prioritize()
146         {
147                 // We want to get here before m_cloaking and m_hostchange etc
148                 return PRIORITY_FIRST;
149         }
150
151         virtual void OnRehash(userrec* user, const std::string &parameter)
152         {
153                 ConfigReader Conf(ServerInstance);
154                 
155                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
156                 
157                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
158                         NotifyOpers = true;
159                 
160                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
161                 {
162                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
163                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
164                         std::string password = Conf.ReadValue("cgihost", "password", i);
165                         
166                         if(hostmask.length())
167                         {
168                                 Hosts.push_back(CGIhost(hostmask));
169                                 
170                                 if(type == "pass")
171                                         Hosts.back().type = PASS;
172                                 else if(type == "ident")
173                                         Hosts.back().type = IDENT;
174                                 else if(type == "passfirst")
175                                         Hosts.back().type = PASSFIRST;
176                                 else if(type == "webirc") {
177                                         Hosts.back().type = WEBIRC;
178                                         if(password.length())
179                                                 Hosts.back().password=password;
180                                         else
181                                                 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
182                                 }
183                         }
184                         else
185                         {
186                                 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
187                                 continue;
188                         }
189                 }
190         }
191
192         virtual void OnCleanup(int target_type, void* item)
193         {
194                 if(target_type == TYPE_USER)
195                 {
196                         userrec* user = (userrec*)item;
197                         std::string* realhost;
198                         std::string* realip;
199                         
200                         if(user->GetExt("cgiirc_realhost", realhost))
201                         {
202                                 delete realhost;
203                                 user->Shrink("cgiirc_realhost");
204                         }
205                         
206                         if(user->GetExt("cgiirc_realip", realip))
207                         {
208                                 delete realip;
209                                 user->Shrink("cgiirc_realip");
210                         }
211                 }
212         }
213         
214         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
215         {
216                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
217                 {
218                         std::string* data;
219                         
220                         if(user->GetExt(extname, data))
221                         {
222                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
223                         }
224                 }
225         }
226
227         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
228         {
229                 if(target_type == TYPE_USER)
230                 {
231                         userrec* dest = (userrec*)target;
232                         std::string* bleh;
233                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
234                         {
235                                 dest->Extend(extname, new std::string(extdata));
236                         }
237                 }
238         }
239
240         virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
241         {
242                 OnCleanup(TYPE_USER, user);
243         }
244         
245
246         virtual int OnUserRegister(userrec* user)
247         {       
248                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
249                 {                       
250                         if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
251                         {
252                                 // Deal with it...
253                                 if(iter->type == PASS)
254                                 {
255                                         CheckPass(user); // We do nothing if it fails so...
256                                 }
257                                 else if(iter->type == PASSFIRST && !CheckPass(user))
258                                 {
259                                         // If the password lookup failed, try the ident
260                                         CheckIdent(user);       // If this fails too, do nothing
261                                 }
262                                 else if(iter->type == IDENT)
263                                 {
264                                         CheckIdent(user); // Nothing on failure.
265                                 }
266                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
267                                 {
268                                         // If the ident lookup fails, try the password.
269                                         CheckPass(user);
270                                 }
271                                 else if(iter->type == WEBIRC)
272                                 {
273                                         // We don't need to do anything here
274                                 }
275                                 return 0;
276                         }
277                 }
278                 return 0;
279         }
280
281         virtual void OnUserConnect(userrec* user)
282         {
283                 std::string *webirc_hostname, *webirc_ip;
284                 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
285                 {
286                         strlcpy(user->host,webirc_hostname->c_str(),63);
287                         strlcpy(user->dhost,webirc_hostname->c_str(),63);
288                         delete webirc_hostname;
289                         user->InvalidateCache();
290                         user->Shrink("cgiirc_webirc_hostname");
291                 }
292                 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
293                 {
294                         bool valid=false;
295 #ifdef IPV6
296                         valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0); 
297
298                         if(!valid)
299                                 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
300 #else
301                         if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
302                                 valid = true;
303 #endif
304
305                         delete webirc_ip;
306                         user->InvalidateCache();
307                         user->Shrink("cgiirc_webirc_ip");
308                 }
309         }
310
311         bool CheckPass(userrec* user)
312         {
313                 if(IsValidHost(user->password))
314                 {
315                         user->Extend("cgiirc_realhost", new std::string(user->host));
316                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
317                         strlcpy(user->host, user->password, 64);
318                         strlcpy(user->dhost, user->password, 64);
319                         user->InvalidateCache();
320
321                         bool valid = false;
322 #ifdef IPV6
323                         if (user->GetProtocolFamily() == AF_INET6)
324                                 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
325                         else
326                                 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
327 #else
328                         if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
329                                 valid = true;
330 #endif
331                         if (valid)
332                         {
333                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
334                                 if(NotifyOpers)
335                                         ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
336                         }
337                         else
338                         {
339                                 /* We got as resolved hostname in the password. */
340                                 try
341                                 {
342
343                                         bool cached;
344                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
345                                         ServerInstance->AddResolver(r, cached);
346                                 }
347                                 catch (ModuleException& e)
348                                 {
349                                         if (NotifyOpers)
350                                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
351                                 }
352                         }
353                         
354                         *user->password = 0;
355
356                         /*if(NotifyOpers)
357                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
358
359                         return true;
360                 }
361                 
362                 return false;
363         }
364         
365         bool CheckIdent(userrec* user)
366         {
367                 int ip[4];
368                 char* ident;
369                 char newip[16];
370                 int len = strlen(user->ident);
371                 
372                 if(len == 8)
373                         ident = user->ident;
374                 else if(len == 9 && *user->ident == '~')
375                         ident = user->ident+1;
376                 else
377                         return false;
378         
379                 for(int i = 0; i < 4; i++)
380                         if(!HexToInt(ip[i], ident + i*2))
381                                 return false;
382
383                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
384                         
385                 user->Extend("cgiirc_realhost", new std::string(user->host));
386                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
387 #ifdef IPV6
388                 if (user->GetProtocolFamily() == AF_INET6)
389                         inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
390                 else
391                         inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
392 #else
393                 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
394 #endif
395                                         
396                 try
397                 {
398                         strlcpy(user->host, newip, 16);
399                         strlcpy(user->dhost, newip, 16);
400                         strlcpy(user->ident, "~cgiirc", 8);
401
402                         bool cached;
403                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
404                         ServerInstance->AddResolver(r, cached);
405                 }
406                 catch (ModuleException& e)
407                 {
408                         strlcpy(user->host, newip, 16);
409                         strlcpy(user->dhost, newip, 16);
410                         strlcpy(user->ident, "~cgiirc", 8);
411                         user->InvalidateCache();
412
413                         if(NotifyOpers)
414                                  ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
415                 }
416                 /*strlcpy(user->host, newip, 16);
417                 strlcpy(user->dhost, newip, 16);
418                 strlcpy(user->ident, "~cgiirc", 8);*/
419
420                 return true;
421         }
422         
423         bool IsValidHost(const std::string &host)
424         {
425                 if(!host.size())
426                         return false;
427         
428                 for(unsigned int i = 0; i < host.size(); i++)
429                 {
430                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
431                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
432                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
433                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
434                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
435                                         
436                                 continue;
437                         else
438                                 return false;
439                 }
440                 
441                 return true;
442         }
443
444         bool IsValidIP(const std::string &ip)
445         {
446                 if(ip.size() < 7 || ip.size() > 15)
447                         return false;
448         
449                 short sincedot = 0;
450                 short dots = 0;
451         
452                 for(unsigned int i = 0; i < ip.size(); i++)
453                 {
454                         if((dots <= 3) && (sincedot <= 3))
455                         {
456                                 if((ip[i] >= '0') && (ip[i] <= '9'))
457                                 {
458                                         sincedot++;
459                                 }
460                                 else if(ip[i] == '.')
461                                 {
462                                         sincedot = 0;
463                                         dots++;
464                                 }
465                         }
466                         else
467                         {
468                                 return false;
469                         
470                         }
471                 }
472                 
473                 if(dots != 3)
474                         return false;
475                 
476                 return true;
477         }
478         
479         bool HexToInt(int &out, const char* in)
480         {
481                 char ip[3];
482                 ip[0] = in[0];
483                 ip[1] = in[1];
484                 ip[2] = 0;
485                 out = strtol(ip, NULL, 16);
486                 
487                 if(out > 255 || out < 0)
488                         return false;
489
490                 return true;
491         }
492         
493         virtual ~ModuleCgiIRC()
494         {
495         }
496          
497         virtual Version GetVersion()
498         {
499                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
500         }
501         
502 };
503
504 class ModuleCgiIRCFactory : public ModuleFactory
505 {
506  public:
507         ModuleCgiIRCFactory()
508         {
509         }
510         
511         ~ModuleCgiIRCFactory()
512         {
513         }
514         
515         virtual Module * CreateModule(InspIRCd* Me)
516         {
517                 return new ModuleCgiIRC(Me);
518         }
519         
520 };
521
522
523 extern "C" DllExport void * init_module( void )
524 {
525         return new ModuleCgiIRCFactory;
526 }