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
113 std::string mechlist;
114 const ServerTracker& servertracker;
115 UserCertificateAPI sslapi;
117 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
119 if (requiressl && sslapi && !sslapi->GetCertificate(user))
122 // Servers MUST NAK any sasl capability request if the authentication layer
124 return servertracker.IsOnline();
127 bool OnList(LocalUser* user) CXX11_OVERRIDE
129 if (requiressl && sslapi && !sslapi->GetCertificate(user))
132 // Servers MUST NOT advertise the sasl capability if the authentication layer
134 return servertracker.IsOnline();
137 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
144 SASLCap(Module* mod, const ServerTracker& tracker)
145 : Cap::Capability(mod, "sasl")
146 , servertracker(tracker)
151 void SetMechlist(const std::string& newmechlist)
153 if (mechlist == newmechlist)
156 mechlist = newmechlist;
161 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
162 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
164 static Events::ModuleEventProvider* saslevprov;
166 static void SendSASL(LocalUser* user, const std::string& agent, char mode, const std::vector<std::string>& parameters)
168 CommandBase::Params params;
169 params.push_back(user->uuid);
170 params.push_back(agent);
171 params.push_back(ConvToStr(mode));
172 params.insert(params.end(), parameters.begin(), parameters.end());
174 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
176 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
180 static ClientProtocol::EventProvider* g_protoev;
183 * Tracks SASL authentication state like charybdis does. --nenolod
185 class SaslAuthenticator
192 bool state_announced;
194 void SendHostIP(UserCertificateAPI& sslapi)
196 std::vector<std::string> params;
197 params.push_back(user->GetRealHost());
198 params.push_back(user->GetIPString());
199 params.push_back(sslapi && sslapi->GetCertificate(user) ? "S" : "P");
201 SendSASL(user, "*", 'H', params);
205 SaslAuthenticator(LocalUser* user_, const std::string& method, UserCertificateAPI& sslapi)
208 , state_announced(false)
212 std::vector<std::string> params;
213 params.push_back(method);
215 const std::string fp = sslapi ? sslapi->GetFingerprint(user) : "";
217 params.push_back(fp);
219 SendSASL(user, "*", 'S', params);
222 SaslResult GetSaslResult(const std::string &result_)
233 /* checks for and deals with a state change. */
234 SaslState ProcessInboundMessage(const CommandBase::Params& msg)
239 this->agent = msg[0];
240 this->state = SASL_COMM;
243 if (msg[0] != this->agent)
251 ClientProtocol::Message authmsg("AUTHENTICATE");
252 authmsg.PushParamRef(msg[3]);
254 ClientProtocol::Event authevent(*g_protoev, authmsg);
255 LocalUser* const localuser = IS_LOCAL(user);
257 localuser->Send(authevent);
259 else if (msg[2] == "D")
261 this->state = SASL_DONE;
262 this->result = this->GetSaslResult(msg[3]);
264 else if (msg[2] == "M")
265 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
267 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
273 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
280 bool SendClientMessage(const std::vector<std::string>& parameters)
282 if (this->state != SASL_COMM)
285 SendSASL(this->user, this->agent, 'C', parameters);
287 if (parameters[0].c_str()[0] == '*')
289 this->state = SASL_DONE;
290 this->result = SASL_ABORT;
297 void AnnounceState(void)
299 if (this->state_announced)
302 switch (this->result)
305 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
308 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
311 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
317 this->state_announced = true;
321 class CommandAuthenticate : public SplitCommand
324 // The maximum length of an AUTHENTICATE request.
325 static const size_t MAX_AUTHENTICATE_SIZE = 400;
328 SimpleExtItem<SaslAuthenticator>& authExt;
329 Cap::Capability& cap;
330 UserCertificateAPI sslapi;
332 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
333 : SplitCommand(Creator, "AUTHENTICATE", 1)
338 works_before_reg = true;
339 allow_empty_last_param = false;
342 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
348 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
351 if (parameters[0].length() > MAX_AUTHENTICATE_SIZE)
353 user->WriteNumeric(ERR_SASLTOOLONG, "SASL message too long");
357 SaslAuthenticator *sasl = authExt.get(user);
359 authExt.set(user, new SaslAuthenticator(user, parameters[0], sslapi));
360 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
362 sasl->AnnounceState();
370 class CommandSASL : public Command
373 SimpleExtItem<SaslAuthenticator>& authExt;
374 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
376 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
379 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
381 User* target = ServerInstance->FindUUID(parameters[1]);
384 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
388 SaslAuthenticator *sasl = authExt.get(target);
392 SaslState state = sasl->ProcessInboundMessage(parameters);
393 if (state == SASL_DONE)
395 sasl->AnnounceState();
396 authExt.unset(target);
401 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
403 return ROUTE_BROADCAST;
407 class ModuleSASL : public Module
409 SimpleExtItem<SaslAuthenticator> authExt;
410 ServerTracker servertracker;
412 CommandAuthenticate auth;
414 Events::ModuleEventProvider sasleventprov;
415 ClientProtocol::EventProvider protoev;
419 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
420 , servertracker(this)
421 , cap(this, servertracker)
422 , auth(this, authExt, cap)
423 , sasl(this, authExt)
424 , sasleventprov(this, "event/sasl")
425 , protoev(this, auth.name)
427 saslevprov = &sasleventprov;
428 g_protoev = &protoev;
431 void init() CXX11_OVERRIDE
433 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
434 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!");
437 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
439 ConfigTag* tag = ServerInstance->Config->ConfValue("sasl");
441 const std::string target = tag->getString("target");
443 throw ModuleException("<sasl:target> must be set to the name of your services server!");
445 cap.requiressl = tag->getBool("requiressl");
446 sasl_target = target;
447 servertracker.Reset();
450 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
452 if ((target == NULL) && (extname == "saslmechlist"))
453 cap.SetMechlist(extdata);
456 Version GetVersion() CXX11_OVERRIDE
458 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE", VF_VENDOR);
462 MODULE_INIT(ModuleSASL)