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