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