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