]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Fix test client error cheecking on result types
[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
29 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
30
31
32 /* We need this for checking our user hasnt /quit before we finish our lookup */
33 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
34
35 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST };
36
37 class CGIhost : public classbase
38 {
39 public:
40         std::string hostmask;
41         CGItype type;
42
43         CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST)
44         : hostmask(mask), type(t)
45         {
46         }
47 };
48
49 typedef std::vector<CGIhost> CGIHostlist;
50
51 class CGIResolver : public Resolver
52 {
53         std::string typ;
54         int theirfd;
55         userrec* them;
56         bool notify;
57  public:
58         CGIResolver(bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type)
59                 : Resolver(source, forward, ""), 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 == fd_ref_table[theirfd]))
65                 {
66                         if (notify)
67                                 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)
76         {
77                 if ((them) && (them == fd_ref_table[theirfd]))
78                 {
79                         if (notify)
80                                 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         Server *Srv;
92         bool NotifyOpers;
93         CGIHostlist Hosts;
94 public:
95         ModuleCgiIRC(Server* Me) : Module::Module(Me)
96         {
97                 Srv = Me;
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;
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                                 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 void OnUserRegister(userrec* user)
200         {
201                 log(DEBUG, "m_cgiirc.so: User %s registering, %s %s", user->nick,user->host,inet_ntoa(user->ip4));
202                 
203                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
204                 {
205                         log(DEBUG, "m_cgiirc.so: Matching %s against (%s or %s)", iter->hostmask.c_str(), user->host, inet_ntoa(user->ip4));
206                         
207                         if(Srv->MatchText(user->host, iter->hostmask) || Srv->MatchText(inet_ntoa(user->ip4), iter->hostmask))
208                         {
209                                 // Deal with it...
210                                 log(DEBUG, "m_cgiirc.so: Handling CGI:IRC user: %s (%s) matched %s", user->GetFullRealHost(), inet_ntoa(user->ip4), 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                                 
231                                 return;
232                         }
233                 }
234         }
235
236         bool CheckPass(userrec* user)
237         {
238                 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(inet_ntoa(user->ip4)));
244                         strlcpy(user->host, user->password, 64);
245                         strlcpy(user->dhost, user->password, 64);
246                         
247                         if(inet_aton(user->password, &user->ip4))
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                                 log(DEBUG, "m_cgiirc.so: Got an IP in the user's password");
251
252                                 if(NotifyOpers)
253                                         WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
254                         }
255                         else
256                         {
257                                 /* We got as resolved hostname in the password. */
258                                 log(DEBUG, "m_cgiirc.so: Got a hostname in the user's password");
259
260                                 try
261                                 {
262                                         CGIResolver* r = new CGIResolver(NotifyOpers, user->password, false, user, user->fd, "PASS");
263                                         Srv->AddResolver(r);
264                                 }
265                                 catch (ModuleException& e)
266                                 {
267                                         if (NotifyOpers)
268                                                 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
269                                 }
270                         }
271                         
272                         *user->password = 0;
273
274                         /*if(NotifyOpers)
275                                 WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
276
277                         return true;
278                 }
279                 else
280                 {
281                         log(DEBUG, "m_cgiirc.so: User's password was not a valid host");
282                 }
283                 
284                 return false;
285         }
286         
287         bool CheckIdent(userrec* user)
288         {
289                 int ip[4];
290                 char* ident;
291                 char newip[16];
292                 int len = strlen(user->ident);
293                 
294                 if(len == 8)
295                         ident = user->ident;
296                 else if(len == 9 && *user->ident == '~')
297                         ident = user->ident+1;
298                 else
299                         return false;
300         
301                 for(int i = 0; i < 4; i++)
302                         if(!HexToInt(ip[i], ident + i*2))
303                                 return false;
304
305                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
306                         
307                 user->Extend("cgiirc_realhost", new std::string(user->host));
308                 user->Extend("cgiirc_realip", new std::string(inet_ntoa(user->ip4)));
309                 inet_aton(newip, &user->ip4);
310
311                 try
312                 {
313                         log(DEBUG,"MAKE RESOLVER: %s %d %s",newip, user->fd, "IDENT");
314                         CGIResolver* r = new CGIResolver(NotifyOpers, newip, false, user, user->fd, "IDENT");
315                         Srv->AddResolver(r);
316                 }
317                 catch (ModuleException& e)
318                 {
319                         strlcpy(user->host, newip, 16);
320                         strlcpy(user->dhost, newip, 16);
321                         strlcpy(user->ident, "~cgiirc", 8);
322
323                         if(NotifyOpers)
324                                  WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
325                 }
326                 /*strlcpy(user->host, newip, 16);
327                 strlcpy(user->dhost, newip, 16);
328                 strlcpy(user->ident, "~cgiirc", 8);*/
329
330                 return true;
331         }
332         
333         bool IsValidHost(const std::string &host)
334         {
335                 if(!host.size())
336                         return false;
337         
338                 for(unsigned int i = 0; i < host.size(); i++)
339                 {
340                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
341                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
342                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
343                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
344                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
345                                         
346                                 continue;
347                         else
348                                 return false;
349                 }
350                 
351                 return true;
352         }
353
354         bool IsValidIP(const std::string &ip)
355         {
356                 if(ip.size() < 7 || ip.size() > 15)
357                         return false;
358         
359                 short sincedot = 0;
360                 short dots = 0;
361         
362                 for(unsigned int i = 0; i < ip.size(); i++)
363                 {
364                         if((dots <= 3) && (sincedot <= 3))
365                         {
366                                 if((ip[i] >= '0') && (ip[i] <= '9'))
367                                 {
368                                         sincedot++;
369                                 }
370                                 else if(ip[i] == '.')
371                                 {
372                                         sincedot = 0;
373                                         dots++;
374                                 }
375                         }
376                         else
377                         {
378                                 return false;
379                         
380                         }
381                 }
382                 
383                 if(dots != 3)
384                         return false;
385                 
386                 return true;
387         }
388         
389         bool HexToInt(int &out, const char* in)
390         {
391                 char ip[3];
392                 ip[0] = in[0];
393                 ip[1] = in[1];
394                 ip[2] = 0;
395                 out = strtol(ip, NULL, 16);
396                 
397                 if(out > 255 || out < 0)
398                         return false;
399
400                 return true;
401         }
402         
403         virtual ~ModuleCgiIRC()
404         {
405         }
406          
407         virtual Version GetVersion()
408         {
409                 return Version(1,0,0,0,VF_VENDOR);
410         }
411         
412 };
413
414 class ModuleCgiIRCFactory : public ModuleFactory
415 {
416  public:
417         ModuleCgiIRCFactory()
418         {
419         }
420         
421         ~ModuleCgiIRCFactory()
422         {
423         }
424         
425         virtual Module * CreateModule(Server* Me)
426         {
427                 return new ModuleCgiIRC(Me);
428         }
429         
430 };
431
432
433 extern "C" void * init_module( void )
434 {
435         return new ModuleCgiIRCFactory;
436 }