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