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