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