]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Tidy up keywords on module methods.
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 John Brooks <john.brooks@dereferenced.net>
6  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7  *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "xline.h"
28 #include "modules/dns.h"
29
30 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
31
32 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
33
34
35 /** Holds a CGI site's details
36  */
37 class CGIhost
38 {
39 public:
40         std::string hostmask;
41         CGItype type;
42         std::string password;
43
44         CGIhost(const std::string &mask, CGItype t, const std::string &spassword)
45         : hostmask(mask), type(t), password(spassword)
46         {
47         }
48 };
49 typedef std::vector<CGIhost> CGIHostlist;
50
51 /*
52  * WEBIRC
53  *  This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
54  *  Syntax: WEBIRC password client hostname ip
55  *  Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
56  *  is the resolved host of the client issuing the command and IP is the real IP of the client.
57  *
58  * How it works:
59  *  To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
60  *  and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
61  */
62 class CommandWebirc : public Command
63 {
64  public:
65         bool notify;
66         StringExtItem realhost;
67         StringExtItem realip;
68         LocalStringExt webirc_hostname;
69         LocalStringExt webirc_ip;
70
71         CGIHostlist Hosts;
72         CommandWebirc(Module* Creator)
73                 : Command(Creator, "WEBIRC", 4),
74                   realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
75                   webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
76                 {
77                         works_before_reg = true;
78                         this->syntax = "password client hostname ip";
79                 }
80                 CmdResult Handle(const std::vector<std::string> &parameters, User *user)
81                 {
82                         if(user->registered == REG_ALL)
83                                 return CMD_FAILURE;
84
85                         for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
86                         {
87                                 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
88                                 {
89                                         if(iter->type == WEBIRC && parameters[0] == iter->password)
90                                         {
91                                                 realhost.set(user, user->host);
92                                                 realip.set(user, user->GetIPString());
93
94                                                 bool host_ok = (parameters[2].length() < 64);
95                                                 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
96
97                                                 if (notify)
98                                                         ServerInstance->SNO->WriteGlobalSno('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(), newhost.c_str(), user->host.c_str());
99
100                                                 // Check if we're happy with the provided hostname. If it's problematic then make sure we won't set a host later, just the IP
101                                                 if (host_ok)
102                                                         webirc_hostname.set(user, parameters[2]);
103                                                 else
104                                                         webirc_hostname.unset(user);
105
106                                                 webirc_ip.set(user, parameters[3]);
107                                                 return CMD_SUCCESS;
108                                         }
109                                 }
110                         }
111
112                         ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
113                         return CMD_FAILURE;
114                 }
115 };
116
117
118 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
119  */
120 class CGIResolver : public DNS::Request
121 {
122         std::string typ;
123         std::string theiruid;
124         LocalIntExt& waiting;
125         bool notify;
126  public:
127         CGIResolver(DNS::Manager *mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
128                         const std::string &ttype, LocalIntExt& ext)
129                 : DNS::Request(mgr, me, source, DNS::QUERY_PTR), typ(ttype), theiruid(u->uuid),
130                 waiting(ext), notify(NotifyOpers)
131         {
132         }
133
134         void OnLookupComplete(const DNS::Query *r) CXX11_OVERRIDE
135         {
136                 /* Check the user still exists */
137                 User* them = ServerInstance->FindUUID(theiruid);
138                 if ((them) && (!them->quitting))
139                 {
140                         LocalUser* lu = IS_LOCAL(them);
141                         if (!lu)
142                                 return;
143
144                         const DNS::ResourceRecord &ans_record = r->answers[0];
145                         if (ans_record.rdata.empty() || ans_record.rdata.length() > 64)
146                                 return;
147
148                         if (notify)
149                                 ServerInstance->SNO->WriteGlobalSno('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(), ans_record.rdata.c_str(), typ.c_str());
150
151                         them->host = them->dhost = ans_record.rdata;
152                         them->InvalidateCache();
153                         lu->CheckLines(true);
154                 }
155         }
156
157         void OnError(const DNS::Query *r) CXX11_OVERRIDE
158         {
159                 if (!notify)
160                         return;
161
162                 User* them = ServerInstance->FindUUID(theiruid);
163                 if ((them) && (!them->quitting))
164                 {
165                         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());
166                 }
167         }
168
169         ~CGIResolver()
170         {
171                 User* them = ServerInstance->FindUUID(theiruid);
172                 if (!them)
173                         return;
174                 int count = waiting.get(them);
175                 if (count)
176                         waiting.set(them, count - 1);
177         }
178 };
179
180 class ModuleCgiIRC : public Module
181 {
182         CommandWebirc cmd;
183         LocalIntExt waiting;
184         dynamic_reference<DNS::Manager> DNS;
185
186         static void RecheckClass(LocalUser* user)
187         {
188                 user->MyClass = NULL;
189                 user->SetClass();
190                 user->CheckClass();
191         }
192
193         static void ChangeIP(LocalUser* user, const std::string& newip)
194         {
195                 ServerInstance->Users->RemoveCloneCounts(user);
196                 user->SetClientIP(newip.c_str());
197                 ServerInstance->Users->AddLocalClone(user);
198                 ServerInstance->Users->AddGlobalClone(user);
199         }
200
201         void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
202         {
203                 cmd.realhost.set(user, user->host);
204                 cmd.realip.set(user, user->GetIPString());
205                 ChangeIP(user, newip);
206                 user->host = user->dhost = user->GetIPString();
207                 user->InvalidateCache();
208                 RecheckClass(user);
209
210                 // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
211                 if (user->quitting || !DNS || user->MyClass->nouserdns)
212                         return;
213
214                 CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), waiting);
215                 try
216                 {
217                         waiting.set(user, waiting.get(user) + 1);
218                         this->DNS->Process(r);
219                 }
220                 catch (DNS::Exception &ex)
221                 {
222                         int count = waiting.get(user);
223                         if (count)
224                                 waiting.set(user, count - 1);
225                         delete r;
226                         if (cmd.notify)
227                                  ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname; %s", user->nick.c_str(), user->host.c_str(), ex.GetReason());
228                 }
229         }
230
231 public:
232         ModuleCgiIRC()
233                 : cmd(this)
234                 , waiting("cgiirc-delay", this)
235                 , DNS(this, "DNS")
236         {
237         }
238
239         void init() CXX11_OVERRIDE
240         {
241                 OnRehash(NULL);
242                 ServiceProvider* providerlist[] = { &cmd, &cmd.realhost, &cmd.realip, &cmd.webirc_hostname, &cmd.webirc_ip, &waiting };
243                 ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
244
245                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady };
246                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
247         }
248
249         void OnRehash(User* user) CXX11_OVERRIDE
250         {
251                 cmd.Hosts.clear();
252
253                 // Do we send an oper notice when a CGI:IRC has their host changed?
254                 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
255
256                 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
257                 for (ConfigIter i = tags.first; i != tags.second; ++i)
258                 {
259                         ConfigTag* tag = i->second;
260                         std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
261                         std::string type = tag->getString("type"); // What type of user-munging we do on this host.
262                         std::string password = tag->getString("password");
263
264                         if(hostmask.length())
265                         {
266                                 if (type == "webirc" && password.empty())
267                                 {
268                                         ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
269                                 }
270                                 else
271                                 {
272                                         CGItype cgitype;
273                                         if (type == "pass")
274                                                 cgitype = PASS;
275                                         else if (type == "ident")
276                                                 cgitype = IDENT;
277                                         else if (type == "passfirst")
278                                                 cgitype = PASSFIRST;
279                                         else if (type == "webirc")
280                                                 cgitype = WEBIRC;
281                                         else
282                                         {
283                                                 cgitype = PASS;
284                                                 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
285                                         }
286
287                                         cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
288                                 }
289                         }
290                         else
291                         {
292                                 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
293                                 continue;
294                         }
295                 }
296         }
297
298         ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
299         {
300                 if (waiting.get(user))
301                         return MOD_RES_DENY;
302
303                 std::string *webirc_ip = cmd.webirc_ip.get(user);
304                 if (!webirc_ip)
305                         return MOD_RES_PASSTHRU;
306
307                 ChangeIP(user, *webirc_ip);
308
309                 std::string* webirc_hostname = cmd.webirc_hostname.get(user);
310                 user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
311
312                 RecheckClass(user);
313                 if (user->quitting)
314                         return MOD_RES_DENY;
315
316                 user->CheckLines(true);
317                 if (user->quitting)
318                         return MOD_RES_DENY;
319
320                 cmd.webirc_hostname.unset(user);
321                 cmd.webirc_ip.unset(user);
322
323                 return MOD_RES_PASSTHRU;
324         }
325
326         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
327         {
328                 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
329                 {
330                         if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
331                         {
332                                 // Deal with it...
333                                 if(iter->type == PASS)
334                                 {
335                                         CheckPass(user); // We do nothing if it fails so...
336                                         user->CheckLines(true);
337                                 }
338                                 else if(iter->type == PASSFIRST && !CheckPass(user))
339                                 {
340                                         // If the password lookup failed, try the ident
341                                         CheckIdent(user);       // If this fails too, do nothing
342                                         user->CheckLines(true);
343                                 }
344                                 else if(iter->type == IDENT)
345                                 {
346                                         CheckIdent(user); // Nothing on failure.
347                                         user->CheckLines(true);
348                                 }
349                                 else if(iter->type == IDENTFIRST && !CheckIdent(user))
350                                 {
351                                         // If the ident lookup fails, try the password.
352                                         CheckPass(user);
353                                         user->CheckLines(true);
354                                 }
355                                 else if(iter->type == WEBIRC)
356                                 {
357                                         // We don't need to do anything here
358                                 }
359                                 return MOD_RES_PASSTHRU;
360                         }
361                 }
362                 return MOD_RES_PASSTHRU;
363         }
364
365         bool CheckPass(LocalUser* user)
366         {
367                 if(IsValidHost(user->password))
368                 {
369                         HandleIdentOrPass(user, user->password, true);
370                         user->password.clear();
371                         return true;
372                 }
373
374                 return false;
375         }
376
377         bool CheckIdent(LocalUser* user)
378         {
379                 const char* ident;
380                 in_addr newip;
381
382                 if (user->ident.length() == 8)
383                         ident = user->ident.c_str();
384                 else if (user->ident.length() == 9 && user->ident[0] == '~')
385                         ident = user->ident.c_str() + 1;
386                 else
387                         return false;
388
389                 errno = 0;
390                 unsigned long ipaddr = strtoul(ident, NULL, 16);
391                 if (errno)
392                         return false;
393                 newip.s_addr = htonl(ipaddr);
394                 std::string newipstr(inet_ntoa(newip));
395
396                 user->ident = "~cgiirc";
397                 HandleIdentOrPass(user, newipstr, false);
398
399                 return true;
400         }
401
402         bool IsValidHost(const std::string &host)
403         {
404                 if(!host.size() || host.size() > 64)
405                         return false;
406
407                 for(unsigned int i = 0; i < host.size(); i++)
408                 {
409                         if(     ((host[i] >= '0') && (host[i] <= '9')) ||
410                                         ((host[i] >= 'A') && (host[i] <= 'Z')) ||
411                                         ((host[i] >= 'a') && (host[i] <= 'z')) ||
412                                         ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
413                                         ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
414
415                                 continue;
416                         else
417                                 return false;
418                 }
419
420                 return true;
421         }
422
423         Version GetVersion() CXX11_OVERRIDE
424         {
425                 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
426         }
427
428 };
429
430 MODULE_INIT(ModuleCgiIRC)