]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Fix for bug #205 reported by nenolod (modules that erroneously check remote users...
[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 };
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
37         CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST)
38         : hostmask(mask), type(t)
39         {
40         }
41 };
42
43 typedef std::vector<CGIhost> CGIHostlist;
44
45 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
46  */
47 class CGIResolver : public Resolver
48 {
49         std::string typ;
50         int theirfd;
51         userrec* them;
52         bool notify;
53  public:
54         CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
55                 : Resolver(ServerInstance, source, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
56
57         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
58         {
59                 /* Check the user still exists */
60                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
61                 {
62                         if (notify)
63                                 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());
64
65                         strlcpy(them->host, result.c_str(), 63);
66                         strlcpy(them->dhost, result.c_str(), 63);
67                         strlcpy(them->ident, "~cgiirc", 8);
68                 }
69         }
70
71         virtual void OnError(ResolverError e, const std::string &errormessage)
72         {
73                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
74                 {
75                         if (notify)
76                                 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());
77                 }
78         }
79
80         virtual ~CGIResolver()
81         {
82         }
83 };
84
85 class ModuleCgiIRC : public Module
86 {
87         
88         bool NotifyOpers;
89         CGIHostlist Hosts;
90 public:
91         ModuleCgiIRC(InspIRCd* Me) : Module::Module(Me)
92         {
93                 
94                 OnRehash(NULL,"");
95         }
96
97         void Implements(char* List)
98         {
99                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = 1;
100         }
101         
102         virtual Priority Prioritize()
103         {
104                 // We want to get here before m_cloaking and m_hostchange etc
105                 return PRIORITY_FIRST;
106         }
107
108         virtual void OnRehash(userrec* user, const std::string &parameter)
109         {
110                 ConfigReader Conf(ServerInstance);
111                 
112                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
113                 
114                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
115                         NotifyOpers = true;
116                 
117                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
118                 {
119                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
120                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
121                         
122                         if(hostmask.length())
123                         {
124                                 Hosts.push_back(CGIhost(hostmask));
125                                 
126                                 if(type == "pass")
127                                         Hosts.back().type = PASS;
128                                 else if(type == "ident")
129                                         Hosts.back().type = IDENT;
130                                 else if(type == "passfirst")
131                                         Hosts.back().type = PASSFIRST;
132                         }
133                         else
134                         {
135                                 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
136                                 continue;
137                         }
138                 }
139         }
140
141         virtual void OnCleanup(int target_type, void* item)
142         {
143                 if(target_type == TYPE_USER)
144                 {
145                         userrec* user = (userrec*)item;
146                         std::string* realhost;
147                         std::string* realip;
148                         
149                         if(user->GetExt("cgiirc_realhost", realhost))
150                         {
151                                 delete realhost;
152                                 user->Shrink("cgiirc_realhost");
153                         }
154                         
155                         if(user->GetExt("cgiirc_realip", realip))
156                         {
157                                 delete realip;
158                                 user->Shrink("cgiirc_realip");
159                         }
160                 }
161         }
162         
163         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
164         {
165                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
166                 {
167                         std::string* data;
168                         
169                         if(user->GetExt(extname, data))
170                         {
171                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
172                         }
173                 }
174         }
175
176         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
177         {
178                 if(target_type == TYPE_USER)
179                 {
180                         userrec* dest = (userrec*)target;
181                         std::string* bleh;
182                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
183                         {
184                                 dest->Extend(extname, new std::string(extdata));
185                         }
186                 }
187         }
188
189         virtual void OnUserQuit(userrec* user, const std::string &message)
190         {
191                 OnCleanup(TYPE_USER, user);
192         }
193         
194
195         virtual int OnUserRegister(userrec* user)
196         {       
197                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
198                 {                       
199                         if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
200                         {
201                                 // Deal with it...
202                                 if(iter->type == PASS)
203                                 {
204                                         CheckPass(user); // We do nothing if it fails so...
205                                 }
206                                 else if(iter->type == PASSFIRST && !CheckPass(user))
207                                 {
208                                         // If the password lookup failed, try the ident
209                                         CheckIdent(user);       // If this fails too, do nothing
210                                 }
211                                 else if(iter->type == IDENT)
212                                 {
213                                         CheckIdent(user); // Nothing on failure.
214                                 }
215                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
216                                 {
217                                         // If the ident lookup fails, try the password.
218                                         CheckPass(user);
219                                 }
220                                 return 0;
221                         }
222                 }
223                 return 0;
224         }
225
226         bool CheckPass(userrec* user)
227         {
228                 if(IsValidHost(user->password))
229                 {
230                         user->Extend("cgiirc_realhost", new std::string(user->host));
231                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
232                         strlcpy(user->host, user->password, 64);
233                         strlcpy(user->dhost, user->password, 64);
234                         
235 #ifdef IPV6
236                         if (insp_aton(user->password, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr))
237 #else
238                         if (insp_aton(user->password, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr))
239 #endif
240                         {
241                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
242                                 if(NotifyOpers)
243                                         ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
244                         }
245                         else
246                         {
247                                 /* We got as resolved hostname in the password. */
248                                 try
249                                 {
250                                         bool cached;
251                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
252                                         ServerInstance->AddResolver(r, cached);
253                                 }
254                                 catch (ModuleException& e)
255                                 {
256                                         if (NotifyOpers)
257                                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
258                                 }
259                         }
260                         
261                         *user->password = 0;
262
263                         /*if(NotifyOpers)
264                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
265
266                         return true;
267                 }
268                 
269                 return false;
270         }
271         
272         bool CheckIdent(userrec* user)
273         {
274                 int ip[4];
275                 char* ident;
276                 char newip[16];
277                 int len = strlen(user->ident);
278                 
279                 if(len == 8)
280                         ident = user->ident;
281                 else if(len == 9 && *user->ident == '~')
282                         ident = user->ident+1;
283                 else
284                         return false;
285         
286                 for(int i = 0; i < 4; i++)
287                         if(!HexToInt(ip[i], ident + i*2))
288                                 return false;
289
290                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
291                         
292                 user->Extend("cgiirc_realhost", new std::string(user->host));
293                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
294 #ifdef IPV6
295                 insp_aton(newip, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr);
296 #else
297                 insp_aton(newip, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr);
298 #endif
299                                                                 
300                 try
301                 {
302                         bool cached;
303                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
304                         ServerInstance->AddResolver(r, cached);
305                 }
306                 catch (ModuleException& e)
307                 {
308                         strlcpy(user->host, newip, 16);
309                         strlcpy(user->dhost, newip, 16);
310                         strlcpy(user->ident, "~cgiirc", 8);
311
312                         if(NotifyOpers)
313                                  ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
314                 }
315                 /*strlcpy(user->host, newip, 16);
316                 strlcpy(user->dhost, newip, 16);
317                 strlcpy(user->ident, "~cgiirc", 8);*/
318
319                 return true;
320         }
321         
322         bool IsValidHost(const std::string &host)
323         {
324                 if(!host.size())
325                         return false;
326         
327                 for(unsigned int i = 0; i < host.size(); i++)
328                 {
329                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
330                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
331                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
332                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
333                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
334                                         
335                                 continue;
336                         else
337                                 return false;
338                 }
339                 
340                 return true;
341         }
342
343         bool IsValidIP(const std::string &ip)
344         {
345                 if(ip.size() < 7 || ip.size() > 15)
346                         return false;
347         
348                 short sincedot = 0;
349                 short dots = 0;
350         
351                 for(unsigned int i = 0; i < ip.size(); i++)
352                 {
353                         if((dots <= 3) && (sincedot <= 3))
354                         {
355                                 if((ip[i] >= '0') && (ip[i] <= '9'))
356                                 {
357                                         sincedot++;
358                                 }
359                                 else if(ip[i] == '.')
360                                 {
361                                         sincedot = 0;
362                                         dots++;
363                                 }
364                         }
365                         else
366                         {
367                                 return false;
368                         
369                         }
370                 }
371                 
372                 if(dots != 3)
373                         return false;
374                 
375                 return true;
376         }
377         
378         bool HexToInt(int &out, const char* in)
379         {
380                 char ip[3];
381                 ip[0] = in[0];
382                 ip[1] = in[1];
383                 ip[2] = 0;
384                 out = strtol(ip, NULL, 16);
385                 
386                 if(out > 255 || out < 0)
387                         return false;
388
389                 return true;
390         }
391         
392         virtual ~ModuleCgiIRC()
393         {
394         }
395          
396         virtual Version GetVersion()
397         {
398                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
399         }
400         
401 };
402
403 class ModuleCgiIRCFactory : public ModuleFactory
404 {
405  public:
406         ModuleCgiIRCFactory()
407         {
408         }
409         
410         ~ModuleCgiIRCFactory()
411         {
412         }
413         
414         virtual Module * CreateModule(InspIRCd* Me)
415         {
416                 return new ModuleCgiIRC(Me);
417         }
418         
419 };
420
421
422 extern "C" void * init_module( void )
423 {
424         return new ModuleCgiIRCFactory;
425 }