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