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