]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Convert remaining InspIRCd::Log() calls to new logging system
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "inspircd.h"
15
16 #ifndef WINDOWS
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #endif
21
22 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
23
24 enum CGItype { INVALID, PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
25
26
27 /** Holds a CGI site's details
28  */
29 class CGIhost : public classbase
30 {
31 public:
32         std::string hostmask;
33         CGItype type;
34         std::string password;
35
36         CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &spassword ="")
37         : hostmask(mask), type(t), password(spassword)
38         {
39         }
40 };
41 typedef std::vector<CGIhost> CGIHostlist;
42
43 /*
44  * WEBIRC
45  *  This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
46  *  Syntax: WEBIRC password client hostname ip
47  *  Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
48  *  is the resolved host of the client issuing the command and IP is the real IP of the client.
49  *
50  * How it works:
51  *  To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
52  *  and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
53  */
54 class CommandWebirc : public Command
55 {
56         CGIHostlist Hosts;
57         bool notify;
58         public:
59                 CommandWebirc(InspIRCd* Instance, CGIHostlist &cHosts, bool bnotify) : Command(Instance, "WEBIRC", 0, 4, true), Hosts(cHosts), notify(bnotify)
60                 {
61                         this->source = "m_cgiirc.so";
62                         this->syntax = "password client hostname ip";
63                 }
64                 CmdResult Handle(const char* const* parameters, int pcnt, User *user)
65                 {
66                         if(user->registered == REG_ALL)
67                                 return CMD_FAILURE;
68                         
69                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
70                         {
71                                 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
72                                 {
73                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
74                                         {
75                                                 user->Extend("cgiirc_realhost", new std::string(user->host));
76                                                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
77                                                 if (notify)
78                                                         ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick, user->host, parameters[2], user->host);
79                                                 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
80                                                 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
81                                                 return CMD_LOCALONLY;
82                                         }
83                                 }
84                         }
85                         return CMD_FAILURE;
86                 }
87 };
88
89
90 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
91  */
92 class CGIResolver : public Resolver
93 {
94         std::string typ;
95         int theirfd;
96         User* them;
97         bool notify;
98  public:
99         CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
100                 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
101
102         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
103         {
104                 if (resultnum)
105                         return;
106
107                 /* Check the user still exists */
108                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
109                 {
110                         if (notify)
111                                 ServerInstance->SNO->WriteToSnoMask('A', "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());
112
113                         strlcpy(them->host, result.c_str(), 63);
114                         strlcpy(them->dhost, result.c_str(), 63);
115                         strlcpy(them->ident, "~cgiirc", 8);
116                         them->InvalidateCache();
117                 }
118         }
119
120         virtual void OnError(ResolverError e, const std::string &errormessage)
121         {
122                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
123                 {
124                         if (notify)
125                                 ServerInstance->SNO->WriteToSnoMask('A', "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());
126                 }
127         }
128
129         virtual ~CGIResolver()
130         {
131         }
132 };
133
134 class ModuleCgiIRC : public Module
135 {
136         CommandWebirc* mycommand;
137         bool NotifyOpers;
138         CGIHostlist Hosts;
139 public:
140         ModuleCgiIRC(InspIRCd* Me) : Module(Me)
141         {
142                 
143                 OnRehash(NULL,"");
144                 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
145                 ServerInstance->AddCommand(mycommand);
146
147                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
148                 ServerInstance->Modules->Attach(eventlist, this, 7);
149         }
150
151         
152         virtual void Prioritize()
153         {
154                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
155         }
156
157         virtual void OnRehash(User* user, const std::string &parameter)
158         {
159                 ConfigReader Conf(ServerInstance);
160                 
161                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
162                 
163                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
164                         NotifyOpers = true;
165                 
166                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
167                 {
168                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
169                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
170                         std::string password = Conf.ReadValue("cgihost", "password", i);
171                         
172                         if(hostmask.length())
173                         {
174                                 if (type == "webirc" && !password.length()) {
175                                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
176                                 }
177                                 else
178                                 {
179                                         CGItype cgitype = INVALID;
180                                         if (type == "pass")
181                                                 cgitype = PASS;
182                                         else if (type == "ident")
183                                                 cgitype = IDENT;
184                                         else if (type == "passfirst")
185                                                 cgitype = PASSFIRST;
186                                         else if (type == "webirc")
187                                         {
188                                                 cgitype = WEBIRC;
189                                         }
190
191                                         if (cgitype == INVALID)
192                                                 cgitype = PASS;
193
194                                         Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
195                                 }
196                         }
197                         else
198                         {
199                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
200                                 continue;
201                         }
202                 }
203         }
204
205         virtual void OnCleanup(int target_type, void* item)
206         {
207                 if(target_type == TYPE_USER)
208                 {
209                         User* user = (User*)item;
210                         std::string* realhost;
211                         std::string* realip;
212                         
213                         if(user->GetExt("cgiirc_realhost", realhost))
214                         {
215                                 delete realhost;
216                                 user->Shrink("cgiirc_realhost");
217                         }
218                         
219                         if(user->GetExt("cgiirc_realip", realip))
220                         {
221                                 delete realip;
222                                 user->Shrink("cgiirc_realip");
223                         }
224                 }
225         }
226         
227         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
228         {
229                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
230                 {
231                         std::string* data;
232                         
233                         if(user->GetExt(extname, data))
234                         {
235                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
236                         }
237                 }
238         }
239
240         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
241         {
242                 if(target_type == TYPE_USER)
243                 {
244                         User* dest = (User*)target;
245                         std::string* bleh;
246                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
247                         {
248                                 dest->Extend(extname, new std::string(extdata));
249                         }
250                 }
251         }
252
253         virtual void OnUserDisconnect(User* user)
254         {
255                 OnCleanup(TYPE_USER, user);
256         }
257         
258
259         virtual int OnUserRegister(User* user)
260         {       
261                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
262                 {                       
263                         if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
264                         {
265                                 // Deal with it...
266                                 if(iter->type == PASS)
267                                 {
268                                         CheckPass(user); // We do nothing if it fails so...
269                                 }
270                                 else if(iter->type == PASSFIRST && !CheckPass(user))
271                                 {
272                                         // If the password lookup failed, try the ident
273                                         CheckIdent(user);       // If this fails too, do nothing
274                                 }
275                                 else if(iter->type == IDENT)
276                                 {
277                                         CheckIdent(user); // Nothing on failure.
278                                 }
279                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
280                                 {
281                                         // If the ident lookup fails, try the password.
282                                         CheckPass(user);
283                                 }
284                                 else if(iter->type == WEBIRC)
285                                 {
286                                         // We don't need to do anything here
287                                 }
288                                 return 0;
289                         }
290                 }
291                 return 0;
292         }
293
294         virtual void OnUserConnect(User* user)
295         {
296                 std::string *webirc_hostname, *webirc_ip;
297                 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
298                 {
299                         strlcpy(user->host,webirc_hostname->c_str(),63);
300                         strlcpy(user->dhost,webirc_hostname->c_str(),63);
301                         delete webirc_hostname;
302                         user->InvalidateCache();
303                         user->Shrink("cgiirc_webirc_hostname");
304                 }
305                 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
306                 {
307                         bool valid=false;
308                         ServerInstance->Users->RemoveCloneCounts(user);
309 #ifdef IPV6
310                         valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0); 
311
312                         if(!valid)
313                                 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
314 #else
315                         if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
316                                 valid = true;
317 #endif
318
319                         delete webirc_ip;
320                         user->InvalidateCache();
321                         user->Shrink("cgiirc_webirc_ip");
322                         ServerInstance->Users->AddLocalClone(user);
323                         ServerInstance->Users->AddGlobalClone(user);
324                         user->CheckClass();
325                 }
326         }
327
328         bool CheckPass(User* user)
329         {
330                 if(IsValidHost(user->password))
331                 {
332                         user->Extend("cgiirc_realhost", new std::string(user->host));
333                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
334                         strlcpy(user->host, user->password, 64);
335                         strlcpy(user->dhost, user->password, 64);
336                         user->InvalidateCache();
337
338                         bool valid = false;
339                         ServerInstance->Users->RemoveCloneCounts(user);
340 #ifdef IPV6
341                         if (user->GetProtocolFamily() == AF_INET6)
342                                 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
343                         else
344                                 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
345 #else
346                         if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
347                                 valid = true;
348 #endif
349                         ServerInstance->Users->AddLocalClone(user);
350                         ServerInstance->Users->AddGlobalClone(user);
351                         user->CheckClass();
352
353                         if (valid)
354                         {
355                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
356                                 if(NotifyOpers)
357                                         ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
358                         }
359                         else
360                         {
361                                 /* We got as resolved hostname in the password. */
362                                 try
363                                 {
364
365                                         bool cached;
366                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
367                                         ServerInstance->AddResolver(r, cached);
368                                 }
369                                 catch (...)
370                                 {
371                                         if (NotifyOpers)
372                                                 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
373                                 }
374                         }
375                         
376                         *user->password = 0;
377
378                         /*if(NotifyOpers)
379                                 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
380
381                         return true;
382                 }
383                 
384                 return false;
385         }
386         
387         bool CheckIdent(User* user)
388         {
389                 int ip[4];
390                 char* ident;
391                 char newip[16];
392                 int len = strlen(user->ident);
393                 
394                 if(len == 8)
395                         ident = user->ident;
396                 else if(len == 9 && *user->ident == '~')
397                         ident = user->ident+1;
398                 else
399                         return false;
400         
401                 for(int i = 0; i < 4; i++)
402                         if(!HexToInt(ip[i], ident + i*2))
403                                 return false;
404
405                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
406                         
407                 user->Extend("cgiirc_realhost", new std::string(user->host));
408                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
409                 ServerInstance->Users->RemoveCloneCounts(user);
410 #ifdef IPV6
411                 if (user->GetProtocolFamily() == AF_INET6)
412                         inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
413                 else
414 #endif
415                 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
416                 ServerInstance->Users->AddLocalClone(user);
417                 ServerInstance->Users->AddGlobalClone(user);
418                 user->CheckClass();
419                 try
420                 {
421                         strlcpy(user->host, newip, 16);
422                         strlcpy(user->dhost, newip, 16);
423                         strlcpy(user->ident, "~cgiirc", 8);
424
425                         bool cached;
426                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
427                         ServerInstance->AddResolver(r, cached);
428                 }
429                 catch (...)
430                 {
431                         strlcpy(user->host, newip, 16);
432                         strlcpy(user->dhost, newip, 16);
433                         strlcpy(user->ident, "~cgiirc", 8);
434                         user->InvalidateCache();
435
436                         if(NotifyOpers)
437                                  ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
438                 }
439                 /*strlcpy(user->host, newip, 16);
440                 strlcpy(user->dhost, newip, 16);
441                 strlcpy(user->ident, "~cgiirc", 8);*/
442
443                 return true;
444         }
445         
446         bool IsValidHost(const std::string &host)
447         {
448                 if(!host.size())
449                         return false;
450         
451                 for(unsigned int i = 0; i < host.size(); i++)
452                 {
453                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
454                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
455                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
456                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
457                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
458                                         
459                                 continue;
460                         else
461                                 return false;
462                 }
463                 
464                 return true;
465         }
466
467         bool IsValidIP(const std::string &ip)
468         {
469                 if(ip.size() < 7 || ip.size() > 15)
470                         return false;
471         
472                 short sincedot = 0;
473                 short dots = 0;
474         
475                 for(unsigned int i = 0; i < ip.size(); i++)
476                 {
477                         if((dots <= 3) && (sincedot <= 3))
478                         {
479                                 if((ip[i] >= '0') && (ip[i] <= '9'))
480                                 {
481                                         sincedot++;
482                                 }
483                                 else if(ip[i] == '.')
484                                 {
485                                         sincedot = 0;
486                                         dots++;
487                                 }
488                         }
489                         else
490                         {
491                                 return false;
492                         
493                         }
494                 }
495                 
496                 if(dots != 3)
497                         return false;
498                 
499                 return true;
500         }
501         
502         bool HexToInt(int &out, const char* in)
503         {
504                 char ip[3];
505                 ip[0] = in[0];
506                 ip[1] = in[1];
507                 ip[2] = 0;
508                 out = strtol(ip, NULL, 16);
509                 
510                 if(out > 255 || out < 0)
511                         return false;
512
513                 return true;
514         }
515         
516         virtual ~ModuleCgiIRC()
517         {
518         }
519          
520         virtual Version GetVersion()
521         {
522                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
523         }
524         
525 };
526
527 MODULE_INIT(ModuleCgiIRC)