]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Add option to allow +q to +q users, +a to +a users.
[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 std::vector<std::string> &parameters, 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(InspIRCd::Match(user->host, iter->hostmask) || InspIRCd::MatchCIDR(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.c_str(), user->host.c_str(), parameters[2].c_str(), user->host.c_str());
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.c_str(), them->host.c_str(), result.c_str(), typ.c_str());
112
113                         them->host.assign(result,0, 64);
114                         them->dhost.assign(result, 0, 64);
115                         if (querytype)
116                                 them->SetSockAddr(them->GetProtocolFamily(), result.c_str(), them->GetPort());
117                         them->ident.assign("~cgiirc", 0, 8);
118                         them->InvalidateCache();
119                         them->CheckLines(true);
120                 }
121         }
122
123         virtual void OnError(ResolverError e, const std::string &errormessage)
124         {
125                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
126                 {
127                         if (notify)
128                                 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.c_str(), them->host.c_str(), typ.c_str());
129                 }
130         }
131
132         virtual ~CGIResolver()
133         {
134         }
135 };
136
137 class ModuleCgiIRC : public Module
138 {
139         CommandWebirc* mycommand;
140         bool NotifyOpers;
141         CGIHostlist Hosts;
142 public:
143         ModuleCgiIRC(InspIRCd* Me) : Module(Me)
144         {
145
146                 OnRehash(NULL,"");
147                 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
148                 ServerInstance->AddCommand(mycommand);
149
150                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
151                 ServerInstance->Modules->Attach(eventlist, this, 7);
152         }
153
154
155         virtual void Prioritize()
156         {
157                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
158         }
159
160         virtual void OnRehash(User* user, const std::string &parameter)
161         {
162                 ConfigReader Conf(ServerInstance);
163                 Hosts.clear();
164
165                 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
166
167                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
168                         NotifyOpers = true;
169
170                 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
171                 {
172                         std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
173                         std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
174                         std::string password = Conf.ReadValue("cgihost", "password", i);
175
176                         if(hostmask.length())
177                         {
178                                 if (type == "webirc" && !password.length()) {
179                                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
180                                 }
181                                 else
182                                 {
183                                         CGItype cgitype = INVALID;
184                                         if (type == "pass")
185                                                 cgitype = PASS;
186                                         else if (type == "ident")
187                                                 cgitype = IDENT;
188                                         else if (type == "passfirst")
189                                                 cgitype = PASSFIRST;
190                                         else if (type == "webirc")
191                                         {
192                                                 cgitype = WEBIRC;
193                                         }
194
195                                         if (cgitype == INVALID)
196                                                 cgitype = PASS;
197
198                                         Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
199                                 }
200                         }
201                         else
202                         {
203                                 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
204                                 continue;
205                         }
206                 }
207         }
208
209         virtual void OnCleanup(int target_type, void* item)
210         {
211                 if(target_type == TYPE_USER)
212                 {
213                         User* user = (User*)item;
214                         std::string* realhost;
215                         std::string* realip;
216
217                         if(user->GetExt("cgiirc_realhost", realhost))
218                         {
219                                 delete realhost;
220                                 user->Shrink("cgiirc_realhost");
221                         }
222
223                         if(user->GetExt("cgiirc_realip", realip))
224                         {
225                                 delete realip;
226                                 user->Shrink("cgiirc_realip");
227                         }
228                 }
229         }
230
231         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
232         {
233                 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
234                 {
235                         std::string* data;
236
237                         if(user->GetExt(extname, data))
238                         {
239                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
240                         }
241                 }
242         }
243
244         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
245         {
246                 if(target_type == TYPE_USER)
247                 {
248                         User* dest = (User*)target;
249                         std::string* bleh;
250                         if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
251                         {
252                                 dest->Extend(extname, new std::string(extdata));
253                         }
254                 }
255         }
256
257         virtual void OnUserDisconnect(User* user)
258         {
259                 OnCleanup(TYPE_USER, user);
260         }
261
262
263         virtual int OnUserRegister(User* user)
264         {
265                 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
266                 {
267                         if(InspIRCd::Match(user->host, iter->hostmask) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask))
268                         {
269                                 // Deal with it...
270                                 if(iter->type == PASS)
271                                 {
272                                         CheckPass(user); // We do nothing if it fails so...
273                                         user->CheckLines(true);
274                                 }
275                                 else if(iter->type == PASSFIRST && !CheckPass(user))
276                                 {
277                                         // If the password lookup failed, try the ident
278                                         CheckIdent(user);       // If this fails too, do nothing
279                                         user->CheckLines(true);
280                                 }
281                                 else if(iter->type == IDENT)
282                                 {
283                                         CheckIdent(user); // Nothing on failure.
284                                         user->CheckLines(true);
285                                 }
286                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
287                                 {
288                                         // If the ident lookup fails, try the password.
289                                         CheckPass(user);
290                                         user->CheckLines(true);
291                                 }
292                                 else if(iter->type == WEBIRC)
293                                 {
294                                         // We don't need to do anything here
295                                 }
296                                 return 0;
297                         }
298                 }
299                 return 0;
300         }
301
302         virtual void OnUserConnect(User* user)
303         {
304                 std::string *webirc_hostname, *webirc_ip;
305                 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
306                 {
307                         user->host.assign(*webirc_hostname, 0, 64);
308                         user->dhost.assign(*webirc_hostname, 0, 64);
309                         delete webirc_hostname;
310                         user->InvalidateCache();
311                         user->Shrink("cgiirc_webirc_hostname");
312                 }
313                 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
314                 {
315                         ServerInstance->Users->RemoveCloneCounts(user);
316                         user->SetSockAddr(user->GetProtocolFamily(), webirc_ip->c_str(), user->GetPort());
317                         delete webirc_ip;
318                         user->InvalidateCache();
319                         user->Shrink("cgiirc_webirc_ip");
320                         ServerInstance->Users->AddLocalClone(user);
321                         ServerInstance->Users->AddGlobalClone(user);
322                         user->CheckClass();
323                         user->CheckLines(true);
324                 }
325         }
326
327         bool CheckPass(User* user)
328         {
329                 if(IsValidHost(user->password))
330                 {
331                         user->Extend("cgiirc_realhost", new std::string(user->host));
332                         user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
333                         user->host.assign(user->password, 0, 64);
334                         user->dhost.assign(user->password, 0, 64);
335                         user->InvalidateCache();
336
337                         bool valid = false;
338                         ServerInstance->Users->RemoveCloneCounts(user);
339 #ifdef IPV6
340                         if (user->GetProtocolFamily() == AF_INET6)
341                                 valid = (inet_pton(AF_INET6, user->password.c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
342                         else
343                                 valid = (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr));
344 #else
345                         if (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr))
346                                 valid = true;
347 #endif
348                         ServerInstance->Users->AddLocalClone(user);
349                         ServerInstance->Users->AddGlobalClone(user);
350                         user->CheckClass();
351
352                         if (valid)
353                         {
354                                 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
355                                 if(NotifyOpers)
356                                         ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick.c_str(), user->host.c_str(), user->password.c_str());
357                         }
358                         else
359                         {
360                                 /* We got as resolved hostname in the password. */
361                                 try
362                                 {
363
364                                         bool cached;
365                                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
366                                         ServerInstance->AddResolver(r, cached);
367                                 }
368                                 catch (...)
369                                 {
370                                         if (NotifyOpers)
371                                                 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
372                                 }
373                         }
374
375                         user->password.clear();
376                         return true;
377                 }
378
379                 return false;
380         }
381
382         bool CheckIdent(User* user)
383         {
384                 int ip[4];
385                 const char* ident;
386                 char newip[16];
387                 int len = user->ident.length();
388
389                 if(len == 8)
390                         ident = user->ident.c_str();
391                 else if(len == 9 && user->ident[0] == '~')
392                         ident = user->ident.c_str() + 1;
393                 else
394                         return false;
395
396                 for(int i = 0; i < 4; i++)
397                         if(!HexToInt(ip[i], ident + i*2))
398                                 return false;
399
400                 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
401
402                 user->Extend("cgiirc_realhost", new std::string(user->host));
403                 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
404                 ServerInstance->Users->RemoveCloneCounts(user);
405                 user->SetSockAddr(user->GetProtocolFamily(), newip, user->GetPort());
406                 ServerInstance->Users->AddLocalClone(user);
407                 ServerInstance->Users->AddGlobalClone(user);
408                 user->CheckClass();
409                 try
410                 {
411                         user->host.assign(newip, 0, 16);
412                         user->dhost.assign(newip, 0, 16);
413                         user->ident.assign("~cgiirc", 0, 8);
414
415                         bool cached;
416                         CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
417                         ServerInstance->AddResolver(r, cached);
418                 }
419                 catch (...)
420                 {
421                         user->host.assign(newip, 0, 16);
422                         user->dhost.assign(newip, 0, 16);
423                         user->ident.assign("~cgiirc", 0, 8);
424                         user->InvalidateCache();
425
426                         if(NotifyOpers)
427                                  ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
428                 }
429
430                 return true;
431         }
432
433         bool IsValidHost(const std::string &host)
434         {
435                 if(!host.size())
436                         return false;
437
438                 for(unsigned int i = 0; i < host.size(); i++)
439                 {
440                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
441                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
442                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
443                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
444                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
445
446                                 continue;
447                         else
448                                 return false;
449                 }
450
451                 return true;
452         }
453
454         bool IsValidIP(const std::string &ip)
455         {
456                 if(ip.size() < 7 || ip.size() > 15)
457                         return false;
458
459                 short sincedot = 0;
460                 short dots = 0;
461
462                 for(unsigned int i = 0; i < ip.size(); i++)
463                 {
464                         if((dots <= 3) && (sincedot <= 3))
465                         {
466                                 if((ip[i] >= '0') && (ip[i] <= '9'))
467                                 {
468                                         sincedot++;
469                                 }
470                                 else if(ip[i] == '.')
471                                 {
472                                         sincedot = 0;
473                                         dots++;
474                                 }
475                         }
476                         else
477                         {
478                                 return false;
479
480                         }
481                 }
482
483                 if(dots != 3)
484                         return false;
485
486                 return true;
487         }
488
489         bool HexToInt(int &out, const char* in)
490         {
491                 char ip[3];
492                 ip[0] = in[0];
493                 ip[1] = in[1];
494                 ip[2] = 0;
495                 out = strtol(ip, NULL, 16);
496
497                 if(out > 255 || out < 0)
498                         return false;
499
500                 return true;
501         }
502
503         virtual ~ModuleCgiIRC()
504         {
505         }
506
507         virtual Version GetVersion()
508         {
509                 return Version("$Id$",VF_VENDOR,API_VERSION);
510         }
511
512 };
513
514 MODULE_INIT(ModuleCgiIRC)