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