]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
16faf4f295e95826cd54f62fdc18c51fa7bb4afa
[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         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 void Prioritize()
146         {
147                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
148         }
149
150         virtual void OnRehash(User* user, const std::string &parameter)
151         {
152                 ConfigReader Conf(ServerInstance);
153                 
154                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
155                 
156                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
157                         NotifyOpers = true;
158                 
159                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
160                 {
161                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
162                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
163                         std::string password = Conf.ReadValue("cgihost", "password", i);
164                         
165                         if(hostmask.length())
166                         {
167                                 if (type == "webirc" && !password.length()) {
168                                                 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
169                                 }
170                                 else
171                                 {
172                                         CGItype cgitype = INVALID;
173                                         if (type == "pass")
174                                                 cgitype = PASS;
175                                         else if (type == "ident")
176                                                 cgitype = IDENT;
177                                         else if (type == "passfirst")
178                                                 cgitype = PASSFIRST;
179                                         else if (type == "webirc")
180                                         {
181                                                 cgitype = WEBIRC;
182                                         }
183
184                                         if (cgitype == INVALID)
185                                                 cgitype = PASS;
186
187                                         Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
188                                 }
189                         }
190                         else
191                         {
192                                 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
193                                 continue;
194                         }
195                 }
196         }
197
198         virtual void OnCleanup(int target_type, void* item)
199         {
200                 if(target_type == TYPE_USER)
201                 {
202                         User* user = (User*)item;
203                         std::string* realhost;
204                         std::string* realip;
205                         
206                         if(user->GetExt("cgiirc_realhost", realhost))
207                         {
208                                 delete realhost;
209                                 user->Shrink("cgiirc_realhost");
210                         }
211                         
212                         if(user->GetExt("cgiirc_realip", realip))
213                         {
214                                 delete realip;
215                                 user->Shrink("cgiirc_realip");
216                         }
217                 }
218         }
219         
220         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
221         {
222                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
223                 {
224                         std::string* data;
225                         
226                         if(user->GetExt(extname, data))
227                         {
228                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
229                         }
230                 }
231         }
232
233         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
234         {
235                 if(target_type == TYPE_USER)
236                 {
237                         User* dest = (User*)target;
238                         std::string* bleh;
239                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
240                         {
241                                 dest->Extend(extname, new std::string(extdata));
242                         }
243                 }
244         }
245
246         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
247         {
248                 OnCleanup(TYPE_USER, user);
249         }
250         
251
252         virtual int OnUserRegister(User* user)
253         {       
254                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
255                 {                       
256                         if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
257                         {
258                                 // Deal with it...
259                                 if(iter->type == PASS)
260                                 {
261                                         CheckPass(user); // We do nothing if it fails so...
262                                 }
263                                 else if(iter->type == PASSFIRST && !CheckPass(user))
264                                 {
265                                         // If the password lookup failed, try the ident
266                                         CheckIdent(user);       // If this fails too, do nothing
267                                 }
268                                 else if(iter->type == IDENT)
269                                 {
270                                         CheckIdent(user); // Nothing on failure.
271                                 }
272                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
273                                 {
274                                         // If the ident lookup fails, try the password.
275                                         CheckPass(user);
276                                 }
277                                 else if(iter->type == WEBIRC)
278                                 {
279                                         // We don't need to do anything here
280                                 }
281                                 return 0;
282                         }
283                 }
284                 return 0;
285         }
286
287         virtual void OnUserConnect(User* user)
288         {
289                 std::string *webirc_hostname, *webirc_ip;
290                 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
291                 {
292                         strlcpy(user->host,webirc_hostname->c_str(),63);
293                         strlcpy(user->dhost,webirc_hostname->c_str(),63);
294                         delete webirc_hostname;
295                         user->InvalidateCache();
296                         user->Shrink("cgiirc_webirc_hostname");
297                 }
298                 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
299                 {
300                         bool valid=false;
301                         user->RemoveCloneCounts();
302 #ifdef IPV6
303                         valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0); 
304
305                         if(!valid)
306                                 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
307 #else
308                         if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
309                                 valid = true;
310 #endif
311
312                         delete webirc_ip;
313                         user->InvalidateCache();
314                         user->Shrink("cgiirc_webirc_ip");
315                         ServerInstance->AddLocalClone(user);
316                         ServerInstance->AddGlobalClone(user);
317                         user->CheckClass();
318                 }
319         }
320
321         bool CheckPass(User* user)
322         {
323                 if(IsValidHost(user->password))
324                 {
325                         user->Extend("cgiirc_realhost", new std::string(user->host));
326                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
327                         strlcpy(user->host, user->password, 64);
328                         strlcpy(user->dhost, user->password, 64);
329                         user->InvalidateCache();
330
331                         bool valid = false;
332                         user->RemoveCloneCounts();
333 #ifdef IPV6
334                         if (user->GetProtocolFamily() == AF_INET6)
335                                 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
336                         else
337                                 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
338 #else
339                         if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
340                                 valid = true;
341 #endif
342                         ServerInstance->AddLocalClone(user);
343                         ServerInstance->AddGlobalClone(user);
344                         user->CheckClass();
345
346                         if (valid)
347                         {
348                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
349                                 if(NotifyOpers)
350                                         ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
351                         }
352                         else
353                         {
354                                 /* We got as resolved hostname in the password. */
355                                 try
356                                 {
357
358                                         bool cached;
359                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
360                                         ServerInstance->AddResolver(r, cached);
361                                 }
362                                 catch (...)
363                                 {
364                                         if (NotifyOpers)
365                                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
366                                 }
367                         }
368                         
369                         *user->password = 0;
370
371                         /*if(NotifyOpers)
372                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
373
374                         return true;
375                 }
376                 
377                 return false;
378         }
379         
380         bool CheckIdent(User* user)
381         {
382                 int ip[4];
383                 char* ident;
384                 char newip[16];
385                 int len = strlen(user->ident);
386                 
387                 if(len == 8)
388                         ident = user->ident;
389                 else if(len == 9 && *user->ident == '~')
390                         ident = user->ident+1;
391                 else
392                         return false;
393         
394                 for(int i = 0; i < 4; i++)
395                         if(!HexToInt(ip[i], ident + i*2))
396                                 return false;
397
398                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
399                         
400                 user->Extend("cgiirc_realhost", new std::string(user->host));
401                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
402                 user->RemoveCloneCounts();
403 #ifdef IPV6
404                 if (user->GetProtocolFamily() == AF_INET6)
405                         inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
406                 else
407 #endif
408                 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
409                 ServerInstance->AddLocalClone(user);
410                 ServerInstance->AddGlobalClone(user);
411                 user->CheckClass();
412                 try
413                 {
414                         strlcpy(user->host, newip, 16);
415                         strlcpy(user->dhost, newip, 16);
416                         strlcpy(user->ident, "~cgiirc", 8);
417
418                         bool cached;
419                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
420                         ServerInstance->AddResolver(r, cached);
421                 }
422                 catch (...)
423                 {
424                         strlcpy(user->host, newip, 16);
425                         strlcpy(user->dhost, newip, 16);
426                         strlcpy(user->ident, "~cgiirc", 8);
427                         user->InvalidateCache();
428
429                         if(NotifyOpers)
430                                  ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
431                 }
432                 /*strlcpy(user->host, newip, 16);
433                 strlcpy(user->dhost, newip, 16);
434                 strlcpy(user->ident, "~cgiirc", 8);*/
435
436                 return true;
437         }
438         
439         bool IsValidHost(const std::string &host)
440         {
441                 if(!host.size())
442                         return false;
443         
444                 for(unsigned int i = 0; i < host.size(); i++)
445                 {
446                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
447                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
448                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
449                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
450                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
451                                         
452                                 continue;
453                         else
454                                 return false;
455                 }
456                 
457                 return true;
458         }
459
460         bool IsValidIP(const std::string &ip)
461         {
462                 if(ip.size() < 7 || ip.size() > 15)
463                         return false;
464         
465                 short sincedot = 0;
466                 short dots = 0;
467         
468                 for(unsigned int i = 0; i < ip.size(); i++)
469                 {
470                         if((dots <= 3) && (sincedot <= 3))
471                         {
472                                 if((ip[i] >= '0') && (ip[i] <= '9'))
473                                 {
474                                         sincedot++;
475                                 }
476                                 else if(ip[i] == '.')
477                                 {
478                                         sincedot = 0;
479                                         dots++;
480                                 }
481                         }
482                         else
483                         {
484                                 return false;
485                         
486                         }
487                 }
488                 
489                 if(dots != 3)
490                         return false;
491                 
492                 return true;
493         }
494         
495         bool HexToInt(int &out, const char* in)
496         {
497                 char ip[3];
498                 ip[0] = in[0];
499                 ip[1] = in[1];
500                 ip[2] = 0;
501                 out = strtol(ip, NULL, 16);
502                 
503                 if(out > 255 || out < 0)
504                         return false;
505
506                 return true;
507         }
508         
509         virtual ~ModuleCgiIRC()
510         {
511         }
512          
513         virtual Version GetVersion()
514         {
515                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
516         }
517         
518 };
519
520 MODULE_INIT(ModuleCgiIRC)