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