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