2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2016 Adam <Adam@anope.org>
5 * Copyright (C) 2014 Mantas Mikulėnas <grawity@gmail.com>
6 * Copyright (C) 2013-2016, 2018 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
9 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
12 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
14 * This file is part of InspIRCd. InspIRCd is free software: you can
15 * redistribute it and/or modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation, version 2.
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "modules/cap.h"
30 #include "modules/account.h"
31 #include "modules/sasl.h"
32 #include "modules/ssl.h"
33 #include "modules/server.h"
37 // From IRCv3 sasl-3.1
38 RPL_SASLSUCCESS = 903,
40 ERR_SASLTOOLONG = 905,
41 ERR_SASLABORTED = 906,
45 static std::string sasl_target;
48 : public ServerProtocol::LinkEventListener
50 // Stop GCC warnings about the deprecated OnServerSplit event.
51 using ServerProtocol::LinkEventListener::OnServerSplit;
55 void Update(const Server* server, bool linked)
57 if (sasl_target == "*")
60 if (InspIRCd::Match(server->GetName(), sasl_target))
62 ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
67 void OnServerLink(const Server* server) CXX11_OVERRIDE
72 void OnServerSplit(const Server* server, bool error) CXX11_OVERRIDE
74 Update(server, false);
78 ServerTracker(Module* mod)
79 : ServerProtocol::LinkEventListener(mod)
86 if (sasl_target == "*")
94 ProtocolInterface::ServerList servers;
95 ServerInstance->PI->GetServerList(servers);
96 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
98 const ProtocolInterface::ServerInfo& server = *i;
99 if (InspIRCd::Match(server.servername, sasl_target))
107 bool IsOnline() const { return online; }
110 class SASLCap : public Cap::Capability
112 std::string mechlist;
113 const ServerTracker& servertracker;
115 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
117 // Servers MUST NAK any sasl capability request if the authentication layer
119 return servertracker.IsOnline();
122 bool OnList(LocalUser* user) CXX11_OVERRIDE
124 // Servers MUST NOT advertise the sasl capability if the authentication layer
126 return servertracker.IsOnline();
129 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
135 SASLCap(Module* mod, const ServerTracker& tracker)
136 : Cap::Capability(mod, "sasl")
137 , servertracker(tracker)
141 void SetMechlist(const std::string& newmechlist)
143 if (mechlist == newmechlist)
146 mechlist = newmechlist;
151 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
152 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
154 static Events::ModuleEventProvider* saslevprov;
156 static void SendSASL(LocalUser* user, const std::string& agent, char mode, const std::vector<std::string>& parameters)
158 CommandBase::Params params;
159 params.push_back(user->uuid);
160 params.push_back(agent);
161 params.push_back(ConvToStr(mode));
162 params.insert(params.end(), parameters.begin(), parameters.end());
164 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
166 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
170 static ClientProtocol::EventProvider* g_protoev;
173 * Tracks SASL authentication state like charybdis does. --nenolod
175 class SaslAuthenticator
182 bool state_announced;
184 void SendHostIP(UserCertificateAPI& sslapi)
186 std::vector<std::string> params;
187 params.push_back(user->GetRealHost());
188 params.push_back(user->GetIPString());
189 params.push_back(sslapi && sslapi->GetCertificate(user) ? "S" : "P");
191 SendSASL(user, "*", 'H', params);
195 SaslAuthenticator(LocalUser* user_, const std::string& method, UserCertificateAPI& sslapi)
198 , state_announced(false)
202 std::vector<std::string> params;
203 params.push_back(method);
205 const std::string fp = sslapi ? sslapi->GetFingerprint(user) : "";
207 params.push_back(fp);
209 SendSASL(user, "*", 'S', params);
212 SaslResult GetSaslResult(const std::string &result_)
223 /* checks for and deals with a state change. */
224 SaslState ProcessInboundMessage(const CommandBase::Params& msg)
229 this->agent = msg[0];
230 this->state = SASL_COMM;
233 if (msg[0] != this->agent)
241 ClientProtocol::Message authmsg("AUTHENTICATE");
242 authmsg.PushParamRef(msg[3]);
244 ClientProtocol::Event authevent(*g_protoev, authmsg);
245 LocalUser* const localuser = IS_LOCAL(user);
247 localuser->Send(authevent);
249 else if (msg[2] == "D")
251 this->state = SASL_DONE;
252 this->result = this->GetSaslResult(msg[3]);
254 else if (msg[2] == "M")
255 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
257 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
263 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
270 bool SendClientMessage(const std::vector<std::string>& parameters)
272 if (this->state != SASL_COMM)
275 SendSASL(this->user, this->agent, 'C', parameters);
277 if (parameters[0].c_str()[0] == '*')
279 this->state = SASL_DONE;
280 this->result = SASL_ABORT;
287 void AnnounceState(void)
289 if (this->state_announced)
292 switch (this->result)
295 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
298 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
301 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
307 this->state_announced = true;
311 class CommandAuthenticate : public SplitCommand
314 // The maximum length of an AUTHENTICATE request.
315 static const size_t MAX_AUTHENTICATE_SIZE = 400;
318 SimpleExtItem<SaslAuthenticator>& authExt;
319 Cap::Capability& cap;
320 UserCertificateAPI sslapi;
322 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
323 : SplitCommand(Creator, "AUTHENTICATE", 1)
328 works_before_reg = true;
329 allow_empty_last_param = false;
332 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
338 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
341 if (parameters[0].length() > MAX_AUTHENTICATE_SIZE)
343 user->WriteNumeric(ERR_SASLTOOLONG, "SASL message too long");
347 SaslAuthenticator *sasl = authExt.get(user);
349 authExt.set(user, new SaslAuthenticator(user, parameters[0], sslapi));
350 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
352 sasl->AnnounceState();
360 class CommandSASL : public Command
363 SimpleExtItem<SaslAuthenticator>& authExt;
364 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
366 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
369 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
371 User* target = ServerInstance->FindUUID(parameters[1]);
374 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
378 SaslAuthenticator *sasl = authExt.get(target);
382 SaslState state = sasl->ProcessInboundMessage(parameters);
383 if (state == SASL_DONE)
385 sasl->AnnounceState();
386 authExt.unset(target);
391 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
393 return ROUTE_BROADCAST;
397 class ModuleSASL : public Module
399 SimpleExtItem<SaslAuthenticator> authExt;
400 ServerTracker servertracker;
402 CommandAuthenticate auth;
404 Events::ModuleEventProvider sasleventprov;
405 ClientProtocol::EventProvider protoev;
409 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
410 , servertracker(this)
411 , cap(this, servertracker)
412 , auth(this, authExt, cap)
413 , sasl(this, authExt)
414 , sasleventprov(this, "event/sasl")
415 , protoev(this, auth.name)
417 saslevprov = &sasleventprov;
418 g_protoev = &protoev;
421 void init() CXX11_OVERRIDE
423 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
424 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: m_services_account and m_cap are not loaded! m_sasl will NOT function correctly until these two modules are loaded!");
427 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
429 std::string target = ServerInstance->Config->ConfValue("sasl")->getString("target");
431 throw ModuleException("<sasl:target> must be set to the name of your services server!");
433 sasl_target = target;
434 servertracker.Reset();
437 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
439 if ((target == NULL) && (extname == "saslmechlist"))
440 cap.SetMechlist(extdata);
443 Version GetVersion() CXX11_OVERRIDE
445 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE", VF_VENDOR);
449 MODULE_INIT(ModuleSASL)