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