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