]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cgiirc.cpp
Allow the maximum length of a chanfilter message to be configured.
[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
29 enum
30 {
31         // InspIRCd-specific.
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                         WriteLog("Connecting user %s (%s) tried to use WEBIRC but gave an invalid IP address.",
122                                 user->uuid.c_str(), user->GetIPString().c_str());
123                         return CMD_FAILURE;
124                 }
125
126                 for (std::vector<WebIRCHost>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
127                 {
128                         // If we don't match the host then skip to the next host.
129                         if (!iter->Matches(user, parameters[0]))
130                                 continue;
131
132                         // The user matched a WebIRC block!
133                         gateway.set(user, parameters[1]);
134                         realhost.set(user, user->GetRealHost());
135                         realip.set(user, user->GetIPString());
136
137                         WriteLog("Connecting user %s is using a WebIRC gateway; changing their IP from %s to %s.",
138                                 user->uuid.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                 WriteLog("Connecting user %s (%s) tried to use WEBIRC but didn't match any configured WebIRC hosts.",
148                         user->uuid.c_str(), user->GetIPString().c_str());
149                 return CMD_FAILURE;
150         }
151
152         void WriteLog(const char* message, ...) CUSTOM_PRINTF(2, 3)
153         {
154                 std::string buffer;
155                 VAFORMAT(buffer, message, message);
156
157                 // If we are sending a snotice then the message will already be
158                 // written to the logfile.
159                 if (notify)
160                         ServerInstance->SNO->WriteGlobalSno('w', buffer);
161                 else
162                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, buffer);
163         }
164 };
165
166 class ModuleCgiIRC : public Module, public Whois::EventListener
167 {
168         CommandWebIRC cmd;
169         std::vector<std::string> hosts;
170
171         static void RecheckClass(LocalUser* user)
172         {
173                 user->MyClass = NULL;
174                 user->SetClass();
175                 user->CheckClass();
176         }
177
178         void HandleIdent(LocalUser* user, const std::string& newip)
179         {
180                 cmd.realhost.set(user, user->GetRealHost());
181                 cmd.realip.set(user, user->GetIPString());
182
183                 cmd.WriteLog("Connecting user %s is using an ident gateway; changing their IP from %s to %s.",
184                         user->uuid.c_str(), user->GetIPString().c_str(), newip.c_str());
185                 ChangeIP(user, newip);
186                 RecheckClass(user);
187         }
188
189 public:
190         ModuleCgiIRC()
191                 : Whois::EventListener(this)
192                 , cmd(this)
193         {
194         }
195
196         void init() CXX11_OVERRIDE
197         {
198                 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
199         }
200
201         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
202         {
203                 std::vector<std::string> identhosts;
204                 std::vector<WebIRCHost> webirchosts;
205
206                 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
207                 for (ConfigIter i = tags.first; i != tags.second; ++i)
208                 {
209                         ConfigTag* tag = i->second;
210
211                         // Ensure that we have the <cgihost:mask> parameter.
212                         const std::string mask = tag->getString("mask");
213                         if (mask.empty())
214                                 throw ModuleException("<cgihost:mask> is a mandatory field, at " + tag->getTagLocation());
215
216                         // Determine what lookup type this host uses.
217                         const std::string type = tag->getString("type");
218                         if (stdalgo::string::equalsci(type, "ident"))
219                         {
220                                 // The IP address should be looked up from the hex IP address.
221                                 identhosts.push_back(mask);
222                         }
223                         else if (stdalgo::string::equalsci(type, "webirc"))
224                         {
225                                 // The IP address will be received via the WEBIRC command.
226                                 const std::string fingerprint = tag->getString("fingerprint");
227                                 const std::string password = tag->getString("password");
228
229                                 // WebIRC blocks require a password.
230                                 if (fingerprint.empty() && password.empty())
231                                         throw ModuleException("When using <cgihost type=\"webirc\"> either the fingerprint or password field is required, at " + tag->getTagLocation());
232
233                                 webirchosts.push_back(WebIRCHost(mask, fingerprint, password, tag->getString("hash")));
234                         }
235                         else
236                         {
237                                 throw ModuleException(type + " is an invalid <cgihost:mask> type, at " + tag->getTagLocation()); 
238                         }
239                 }
240
241                 // The host configuration was valid so we can apply it.
242                 hosts.swap(identhosts);
243                 cmd.hosts.swap(webirchosts);
244
245                 // Do we send an oper notice when a m_cgiirc client has their IP changed?
246                 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
247         }
248
249         ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
250         {
251                 if (!cmd.realip.get(user))
252                         return MOD_RES_PASSTHRU;
253
254                 RecheckClass(user);
255                 if (user->quitting)
256                         return MOD_RES_DENY;
257
258                 user->CheckLines(true);
259                 if (user->quitting)
260                         return MOD_RES_DENY;
261
262                 return MOD_RES_PASSTHRU;
263         }
264
265         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
266         {
267                 // If <connect:webirc> is not set then we have nothing to do.
268                 const std::string webirc = myclass->config->getString("webirc");
269                 if (webirc.empty())
270                         return MOD_RES_PASSTHRU;
271
272                 // If the user is not connecting via a WebIRC gateway then they
273                 // cannot match this connect class.
274                 const std::string* gateway = cmd.gateway.get(user);
275                 if (!gateway)
276                         return MOD_RES_DENY;
277
278                 // If the gateway matches the <connect:webirc> constraint then
279                 // allow the check to continue. Otherwise, reject it.
280                 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
281         }
282
283         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
284         {
285                 for (std::vector<std::string>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
286                 {
287                         if (!InspIRCd::Match(user->GetRealHost(), *iter, ascii_case_insensitive_map) && !InspIRCd::MatchCIDR(user->GetIPString(), *iter, ascii_case_insensitive_map))
288                                 continue;
289
290                         CheckIdent(user); // Nothing on failure.
291                         user->CheckLines(true);
292                         break;
293                 }
294                 return MOD_RES_PASSTHRU;
295         }
296
297         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
298         {
299                 if (!whois.IsSelfWhois() && !whois.GetSource()->HasPrivPermission("users/auspex"))
300                         return;
301
302                 // If these fields are not set then the client is not using a gateway.
303                 const std::string* realhost = cmd.realhost.get(whois.GetTarget());
304                 const std::string* realip = cmd.realip.get(whois.GetTarget());
305                 if (!realhost || !realip)
306                         return;
307
308                 const std::string* gateway = cmd.gateway.get(whois.GetTarget());
309                 if (gateway)
310                         whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via the " + *gateway + " WebIRC gateway");
311                 else
312                         whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via an ident gateway");
313         }
314
315         bool CheckIdent(LocalUser* user)
316         {
317                 const char* ident;
318                 in_addr newip;
319
320                 if (user->ident.length() == 8)
321                         ident = user->ident.c_str();
322                 else if (user->ident.length() == 9 && user->ident[0] == '~')
323                         ident = user->ident.c_str() + 1;
324                 else
325                         return false;
326
327                 errno = 0;
328                 unsigned long ipaddr = strtoul(ident, NULL, 16);
329                 if (errno)
330                         return false;
331                 newip.s_addr = htonl(ipaddr);
332                 std::string newipstr(inet_ntoa(newip));
333
334                 user->ident = "~cgiirc";
335                 HandleIdent(user, newipstr);
336
337                 return true;
338         }
339
340         Version GetVersion() CXX11_OVERRIDE
341         {
342                 return Version("Enables forwarding the real IP address of a user from a gateway to the IRC server", VF_VENDOR);
343         }
344 };
345
346 MODULE_INIT(ModuleCgiIRC)