]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Merge tag 'v2.0.25' into master.
[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 "modules/dns.h"
28 #include "modules/ssl.h"
29
30 enum
31 {
32         RPL_WHOISGATEWAY = 350
33 };
34
35 // We need this method up here so that it can be accessed from anywhere
36 static void ChangeIP(User* user, const std::string& newip)
37 {
38         ServerInstance->Users->RemoveCloneCounts(user);
39         user->SetClientIP(newip);
40         ServerInstance->Users->AddClone(user);
41 }
42
43 // Encapsulates information about a WebIRC host.
44 class WebIRCHost
45 {
46  private:
47         std::string hostmask;
48         std::string fingerprint;
49         std::string password;
50         std::string passhash;
51
52  public:
53         WebIRCHost(const std::string& mask, const std::string& fp, const std::string& pass, const std::string& hash)
54                 : hostmask(mask)
55                 , fingerprint(fp)
56                 , password(pass)
57                 , passhash(hash)
58         {
59         }
60
61         bool Matches(LocalUser* user, const std::string& pass) const
62         {
63                 // Did the user send a valid password?
64                 if (!password.empty() && !ServerInstance->PassCompare(user, password, pass, passhash))
65                         return false;
66
67                 // Does the user have a valid fingerprint?
68                 const std::string fp = SSLClientCert::GetFingerprint(&user->eh);
69                 if (!fingerprint.empty() && fp != fingerprint)
70                         return false;
71
72                 // Does the user's hostname match our hostmask?
73                 if (InspIRCd::Match(user->GetRealHost(), hostmask, ascii_case_insensitive_map))
74                         return true;
75
76                 // Does the user's IP address match our hostmask?
77                 return InspIRCd::MatchCIDR(user->GetIPString(), hostmask, ascii_case_insensitive_map);
78         }
79 };
80
81 /*
82  * WEBIRC
83  *  This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
84  *  Syntax: WEBIRC password gateway hostname ip
85  *  Where password is a shared key, gateway is the name of the WebIRC gateway and version (e.g. cgiirc), hostname
86  *  is the resolved host of the client issuing the command and IP is the real IP of the client.
87  *
88  * How it works:
89  *  To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
90  *  and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
91  */
92 class CommandWebIRC : public SplitCommand
93 {
94  public:
95         std::vector<WebIRCHost> hosts;
96         bool notify;
97         StringExtItem gateway;
98         StringExtItem realhost;
99         StringExtItem realip;
100
101         CommandWebIRC(Module* Creator)
102                 : SplitCommand(Creator, "WEBIRC", 4)
103                 , gateway("cgiirc_gateway", ExtensionItem::EXT_USER, Creator)
104                 , realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
105                 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
106         {
107                 allow_empty_last_param = false;
108                 works_before_reg = true;
109                 this->syntax = "password gateway hostname ip";
110         }
111
112         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
113         {
114                 if (user->registered == REG_ALL)
115                         return CMD_FAILURE;
116
117                 irc::sockets::sockaddrs ipaddr;
118                 if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
119                 {
120                         user->CommandFloodPenalty += 5000;
121                         ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC but gave an invalid IP address.", user->GetFullRealHost().c_str());
122                         return CMD_FAILURE;
123                 }
124
125                 for (std::vector<WebIRCHost>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
126                 {
127                         // If we don't match the host then skip to the next host.
128                         if (!iter->Matches(user, parameters[0]))
129                                 continue;
130
131                         // The user matched a WebIRC block!
132                         gateway.set(user, parameters[1]);
133                         realhost.set(user, user->GetRealHost());
134                         realip.set(user, user->GetIPString());
135
136                         if (notify)
137                                 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s is using a WebIRC gateway; changing their IP from %s to %s.",
138                                         user->nick.c_str(), user->GetIPString().c_str(), parameters[3].c_str());
139
140                         // Set the IP address sent via WEBIRC. We ignore the hostname and lookup
141                         // instead do our own DNS lookups because of unreliable gateways.
142                         ChangeIP(user, parameters[3]);
143                         return CMD_SUCCESS;
144                 }
145
146                 user->CommandFloodPenalty += 5000;
147                 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC but didn't match any configured WebIRC hosts.", user->GetFullRealHost().c_str());
148                 return CMD_FAILURE;
149         }
150 };
151
152 class ModuleCgiIRC : public Module, public Whois::EventListener
153 {
154         CommandWebIRC cmd;
155         std::vector<std::string> hosts;
156
157         static void RecheckClass(LocalUser* user)
158         {
159                 user->MyClass = NULL;
160                 user->SetClass();
161                 user->CheckClass();
162         }
163
164         void HandleIdent(LocalUser* user, const std::string& newip)
165         {
166                 cmd.realhost.set(user, user->GetRealHost());
167                 cmd.realip.set(user, user->GetIPString());
168
169                 if (cmd.notify)
170                         ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s is using an ident gateway; changing their IP from %s to %s.",
171                                 user->nick.c_str(), user->GetIPString().c_str(), newip.c_str());
172
173                 ChangeIP(user, newip);
174                 RecheckClass(user);
175         }
176
177 public:
178         ModuleCgiIRC()
179                 : Whois::EventListener(this)
180                 , cmd(this)
181         {
182         }
183
184         void init() CXX11_OVERRIDE
185         {
186                 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
187         }
188
189         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
190         {
191                 std::vector<std::string> identhosts;
192                 std::vector<WebIRCHost> webirchosts;
193
194                 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
195                 for (ConfigIter i = tags.first; i != tags.second; ++i)
196                 {
197                         ConfigTag* tag = i->second;
198
199                         // Ensure that we have the <cgihost:mask> parameter.
200                         const std::string mask = tag->getString("mask");
201                         if (mask.empty())
202                                 throw ModuleException("<cgihost:mask> is a mandatory field, at " + tag->getTagLocation());
203
204                         // Determine what lookup type this host uses.
205                         const std::string type = tag->getString("type");
206                         if (stdalgo::string::equalsci(type, "ident"))
207                         {
208                                 // The IP address should be looked up from the hex IP address.
209                                 identhosts.push_back(mask);
210                         }
211                         else if (stdalgo::string::equalsci(type, "webirc"))
212                         {
213                                 // The IP address will be received via the WEBIRC command.
214                                 const std::string fingerprint = tag->getString("fingerprint");
215                                 const std::string password = tag->getString("password");
216
217                                 // WebIRC blocks require a password.
218                                 if (fingerprint.empty() && password.empty())
219                                         throw ModuleException("When using <cgihost type=\"webirc\"> either the fingerprint or password field is required, at " + tag->getTagLocation());
220
221                                 webirchosts.push_back(WebIRCHost(mask, fingerprint, password, tag->getString("hash")));
222                         }
223                         else
224                         {
225                                 throw ModuleException(type + " is an invalid <cgihost:mask> type, at " + tag->getTagLocation()); 
226                         }
227                 }
228
229                 // The host configuration was valid so we can apply it.
230                 hosts.swap(identhosts);
231                 cmd.hosts.swap(webirchosts);
232
233                 // Do we send an oper notice when a m_cgiirc client has their IP changed?
234                 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
235         }
236
237         ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
238         {
239                 if (!cmd.realip.get(user))
240                         return MOD_RES_PASSTHRU;
241
242                 RecheckClass(user);
243                 if (user->quitting)
244                         return MOD_RES_DENY;
245
246                 user->CheckLines(true);
247                 if (user->quitting)
248                         return MOD_RES_DENY;
249
250                 return MOD_RES_PASSTHRU;
251         }
252
253         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
254         {
255                 // If <connect:webirc> is not set then we have nothing to do.
256                 const std::string webirc = myclass->config->getString("webirc");
257                 if (webirc.empty())
258                         return MOD_RES_PASSTHRU;
259
260                 // If the user is not connecting via a WebIRC gateway then they
261                 // cannot match this connect class.
262                 const std::string* gateway = cmd.gateway.get(user);
263                 if (!gateway)
264                         return MOD_RES_DENY;
265
266                 // If the gateway matches the <connect:webirc> constraint then
267                 // allow the check to continue. Otherwise, reject it.
268                 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
269         }
270
271         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
272         {
273                 for (std::vector<std::string>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
274                 {
275                         if (!InspIRCd::Match(user->GetRealHost(), *iter, ascii_case_insensitive_map) && !InspIRCd::MatchCIDR(user->GetIPString(), *iter, ascii_case_insensitive_map))
276                                 continue;
277
278                         CheckIdent(user); // Nothing on failure.
279                         user->CheckLines(true);
280                         break;
281                 }
282                 return MOD_RES_PASSTHRU;
283         }
284
285         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
286         {
287                 if (!whois.IsSelfWhois() && !whois.GetSource()->HasPrivPermission("users/auspex"))
288                         return;
289
290                 // If these fields are not set then the client is not using a gateway.
291                 const std::string* realhost = cmd.realhost.get(whois.GetTarget());
292                 const std::string* realip = cmd.realip.get(whois.GetTarget());
293                 if (!realhost || !realip)
294                         return;
295
296                 const std::string* gateway = cmd.gateway.get(whois.GetTarget());
297                 if (gateway)
298                         whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via the " + *gateway + " WebIRC gateway");
299                 else
300                         whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via an ident gateway");
301         }
302
303         bool CheckIdent(LocalUser* user)
304         {
305                 const char* ident;
306                 in_addr newip;
307
308                 if (user->ident.length() == 8)
309                         ident = user->ident.c_str();
310                 else if (user->ident.length() == 9 && user->ident[0] == '~')
311                         ident = user->ident.c_str() + 1;
312                 else
313                         return false;
314
315                 errno = 0;
316                 unsigned long ipaddr = strtoul(ident, NULL, 16);
317                 if (errno)
318                         return false;
319                 newip.s_addr = htonl(ipaddr);
320                 std::string newipstr(inet_ntoa(newip));
321
322                 user->ident = "~cgiirc";
323                 HandleIdent(user, newipstr);
324
325                 return true;
326         }
327
328         Version GetVersion() CXX11_OVERRIDE
329         {
330                 return Version("Enables forwarding the real IP address of a user from a gateway to the IRC server", VF_VENDOR);
331         }
332 };
333
334 MODULE_INIT(ModuleCgiIRC)