]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
More factory conversion
[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                                 Hosts.push_back(CGIhost(hostmask));
166                                 
167                                 if(type == "pass")
168                                         Hosts.back().type = PASS;
169                                 else if(type == "ident")
170                                         Hosts.back().type = IDENT;
171                                 else if(type == "passfirst")
172                                         Hosts.back().type = PASSFIRST;
173                                 else if(type == "webirc") {
174                                         Hosts.back().type = WEBIRC;
175                                         if(password.length())
176                                                 Hosts.back().password=password;
177                                         else
178                                                 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
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 #ifdef IPV6
293                         valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0); 
294
295                         if(!valid)
296                                 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
297 #else
298                         if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
299                                 valid = true;
300 #endif
301
302                         delete webirc_ip;
303                         user->InvalidateCache();
304                         user->Shrink("cgiirc_webirc_ip");
305                 }
306         }
307
308         bool CheckPass(userrec* user)
309         {
310                 if(IsValidHost(user->password))
311                 {
312                         user->Extend("cgiirc_realhost", new std::string(user->host));
313                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
314                         strlcpy(user->host, user->password, 64);
315                         strlcpy(user->dhost, user->password, 64);
316                         user->InvalidateCache();
317
318                         bool valid = false;
319 #ifdef IPV6
320                         if (user->GetProtocolFamily() == AF_INET6)
321                                 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
322                         else
323                                 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
324 #else
325                         if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
326                                 valid = true;
327 #endif
328                         if (valid)
329                         {
330                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
331                                 if(NotifyOpers)
332                                         ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
333                         }
334                         else
335                         {
336                                 /* We got as resolved hostname in the password. */
337                                 try
338                                 {
339
340                                         bool cached;
341                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
342                                         ServerInstance->AddResolver(r, cached);
343                                 }
344                                 catch (...)
345                                 {
346                                         if (NotifyOpers)
347                                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
348                                 }
349                         }
350                         
351                         *user->password = 0;
352
353                         /*if(NotifyOpers)
354                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
355
356                         return true;
357                 }
358                 
359                 return false;
360         }
361         
362         bool CheckIdent(userrec* user)
363         {
364                 int ip[4];
365                 char* ident;
366                 char newip[16];
367                 int len = strlen(user->ident);
368                 
369                 if(len == 8)
370                         ident = user->ident;
371                 else if(len == 9 && *user->ident == '~')
372                         ident = user->ident+1;
373                 else
374                         return false;
375         
376                 for(int i = 0; i < 4; i++)
377                         if(!HexToInt(ip[i], ident + i*2))
378                                 return false;
379
380                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
381                         
382                 user->Extend("cgiirc_realhost", new std::string(user->host));
383                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
384 #ifdef IPV6
385                 if (user->GetProtocolFamily() == AF_INET6)
386                         inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
387                 else
388 #endif
389                 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
390                                         
391                 try
392                 {
393                         strlcpy(user->host, newip, 16);
394                         strlcpy(user->dhost, newip, 16);
395                         strlcpy(user->ident, "~cgiirc", 8);
396
397                         bool cached;
398                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
399                         ServerInstance->AddResolver(r, cached);
400                 }
401                 catch (...)
402                 {
403                         strlcpy(user->host, newip, 16);
404                         strlcpy(user->dhost, newip, 16);
405                         strlcpy(user->ident, "~cgiirc", 8);
406                         user->InvalidateCache();
407
408                         if(NotifyOpers)
409                                  ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
410                 }
411                 /*strlcpy(user->host, newip, 16);
412                 strlcpy(user->dhost, newip, 16);
413                 strlcpy(user->ident, "~cgiirc", 8);*/
414
415                 return true;
416         }
417         
418         bool IsValidHost(const std::string &host)
419         {
420                 if(!host.size())
421                         return false;
422         
423                 for(unsigned int i = 0; i < host.size(); i++)
424                 {
425                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
426                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
427                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
428                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
429                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
430                                         
431                                 continue;
432                         else
433                                 return false;
434                 }
435                 
436                 return true;
437         }
438
439         bool IsValidIP(const std::string &ip)
440         {
441                 if(ip.size() < 7 || ip.size() > 15)
442                         return false;
443         
444                 short sincedot = 0;
445                 short dots = 0;
446         
447                 for(unsigned int i = 0; i < ip.size(); i++)
448                 {
449                         if((dots <= 3) && (sincedot <= 3))
450                         {
451                                 if((ip[i] >= '0') && (ip[i] <= '9'))
452                                 {
453                                         sincedot++;
454                                 }
455                                 else if(ip[i] == '.')
456                                 {
457                                         sincedot = 0;
458                                         dots++;
459                                 }
460                         }
461                         else
462                         {
463                                 return false;
464                         
465                         }
466                 }
467                 
468                 if(dots != 3)
469                         return false;
470                 
471                 return true;
472         }
473         
474         bool HexToInt(int &out, const char* in)
475         {
476                 char ip[3];
477                 ip[0] = in[0];
478                 ip[1] = in[1];
479                 ip[2] = 0;
480                 out = strtol(ip, NULL, 16);
481                 
482                 if(out > 255 || out < 0)
483                         return false;
484
485                 return true;
486         }
487         
488         virtual ~ModuleCgiIRC()
489         {
490         }
491          
492         virtual Version GetVersion()
493         {
494                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
495         }
496         
497 };
498
499 MODULE_INIT(ModuleCgiIRC);