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