]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
437ce319000b1ff3fc6fede27c69a4434c056fe4
[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, bool &cached)
55                 : Resolver(ServerInstance, source, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
56
57         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
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                                         bool cached;
263                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
264                                         ServerInstance->AddResolver(r, cached);
265                                 }
266                                 catch (ModuleException& e)
267                                 {
268                                         if (NotifyOpers)
269                                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
270                                 }
271                         }
272                         
273                         *user->password = 0;
274
275                         /*if(NotifyOpers)
276                                 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
277
278                         return true;
279                 }
280                 else
281                 {
282                         ServerInstance->Log(DEBUG, "m_cgiirc.so: User's password was not a valid host");
283                 }
284                 
285                 return false;
286         }
287         
288         bool CheckIdent(userrec* user)
289         {
290                 int ip[4];
291                 char* ident;
292                 char newip[16];
293                 int len = strlen(user->ident);
294                 
295                 if(len == 8)
296                         ident = user->ident;
297                 else if(len == 9 && *user->ident == '~')
298                         ident = user->ident+1;
299                 else
300                         return false;
301         
302                 for(int i = 0; i < 4; i++)
303                         if(!HexToInt(ip[i], ident + i*2))
304                                 return false;
305
306                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
307                         
308                 user->Extend("cgiirc_realhost", new std::string(user->host));
309                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
310 #ifdef IPV6
311                 insp_aton(newip, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr);
312 #else
313                 insp_aton(newip, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr);
314 #endif
315                                                                 
316                 try
317                 {
318                         ServerInstance->Log(DEBUG,"MAKE RESOLVER: %s %d %s",newip, user->GetFd(), "IDENT");
319                         bool cached;
320                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
321                         ServerInstance->AddResolver(r, cached);
322                 }
323                 catch (ModuleException& e)
324                 {
325                         strlcpy(user->host, newip, 16);
326                         strlcpy(user->dhost, newip, 16);
327                         strlcpy(user->ident, "~cgiirc", 8);
328
329                         if(NotifyOpers)
330                                  ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
331                 }
332                 /*strlcpy(user->host, newip, 16);
333                 strlcpy(user->dhost, newip, 16);
334                 strlcpy(user->ident, "~cgiirc", 8);*/
335
336                 return true;
337         }
338         
339         bool IsValidHost(const std::string &host)
340         {
341                 if(!host.size())
342                         return false;
343         
344                 for(unsigned int i = 0; i < host.size(); i++)
345                 {
346                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
347                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
348                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
349                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
350                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
351                                         
352                                 continue;
353                         else
354                                 return false;
355                 }
356                 
357                 return true;
358         }
359
360         bool IsValidIP(const std::string &ip)
361         {
362                 if(ip.size() < 7 || ip.size() > 15)
363                         return false;
364         
365                 short sincedot = 0;
366                 short dots = 0;
367         
368                 for(unsigned int i = 0; i < ip.size(); i++)
369                 {
370                         if((dots <= 3) && (sincedot <= 3))
371                         {
372                                 if((ip[i] >= '0') && (ip[i] <= '9'))
373                                 {
374                                         sincedot++;
375                                 }
376                                 else if(ip[i] == '.')
377                                 {
378                                         sincedot = 0;
379                                         dots++;
380                                 }
381                         }
382                         else
383                         {
384                                 return false;
385                         
386                         }
387                 }
388                 
389                 if(dots != 3)
390                         return false;
391                 
392                 return true;
393         }
394         
395         bool HexToInt(int &out, const char* in)
396         {
397                 char ip[3];
398                 ip[0] = in[0];
399                 ip[1] = in[1];
400                 ip[2] = 0;
401                 out = strtol(ip, NULL, 16);
402                 
403                 if(out > 255 || out < 0)
404                         return false;
405
406                 return true;
407         }
408         
409         virtual ~ModuleCgiIRC()
410         {
411         }
412          
413         virtual Version GetVersion()
414         {
415                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
416         }
417         
418 };
419
420 class ModuleCgiIRCFactory : public ModuleFactory
421 {
422  public:
423         ModuleCgiIRCFactory()
424         {
425         }
426         
427         ~ModuleCgiIRCFactory()
428         {
429         }
430         
431         virtual Module * CreateModule(InspIRCd* Me)
432         {
433                 return new ModuleCgiIRC(Me);
434         }
435         
436 };
437
438
439 extern "C" void * init_module( void )
440 {
441         return new ModuleCgiIRCFactory;
442 }