2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "modules/cap.h"
23 #include "modules/account.h"
24 #include "modules/sasl.h"
25 #include "modules/ssl.h"
26 #include "modules/server.h"
30 // From IRCv3 sasl-3.1
31 RPL_SASLSUCCESS = 903,
33 ERR_SASLTOOLONG = 905,
34 ERR_SASLABORTED = 906,
38 static std::string sasl_target;
41 : public ServerProtocol::LinkEventListener
43 // Stop GCC warnings about the deprecated OnServerSplit event.
44 using ServerProtocol::LinkEventListener::OnServerSplit;
48 void Update(const Server* server, bool linked)
50 if (sasl_target == "*")
53 if (InspIRCd::Match(server->GetName(), sasl_target))
55 ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
60 void OnServerLink(const Server* server) CXX11_OVERRIDE
65 void OnServerSplit(const Server* server, bool error) CXX11_OVERRIDE
67 Update(server, false);
71 ServerTracker(Module* mod)
72 : ServerProtocol::LinkEventListener(mod)
79 if (sasl_target == "*")
87 ProtocolInterface::ServerList servers;
88 ServerInstance->PI->GetServerList(servers);
89 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
91 const ProtocolInterface::ServerInfo& server = *i;
92 if (InspIRCd::Match(server.servername, sasl_target))
100 bool IsOnline() const { return online; }
103 class SASLCap : public Cap::Capability
105 std::string mechlist;
106 const ServerTracker& servertracker;
108 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
110 // Servers MUST NAK any sasl capability request if the authentication layer
112 return servertracker.IsOnline();
115 bool OnList(LocalUser* user) CXX11_OVERRIDE
117 // Servers MUST NOT advertise the sasl capability if the authentication layer
119 return servertracker.IsOnline();
122 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
128 SASLCap(Module* mod, const ServerTracker& tracker)
129 : Cap::Capability(mod, "sasl")
130 , servertracker(tracker)
134 void SetMechlist(const std::string& newmechlist)
136 if (mechlist == newmechlist)
139 mechlist = newmechlist;
144 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
145 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
147 static Events::ModuleEventProvider* saslevprov;
149 static void SendSASL(LocalUser* user, const std::string& agent, char mode, const std::vector<std::string>& parameters)
151 CommandBase::Params params;
152 params.push_back(user->uuid);
153 params.push_back(agent);
154 params.push_back(ConvToStr(mode));
155 params.insert(params.end(), parameters.begin(), parameters.end());
157 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
159 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
163 static ClientProtocol::EventProvider* g_protoev;
166 * Tracks SASL authentication state like charybdis does. --nenolod
168 class SaslAuthenticator
175 bool state_announced;
177 void SendHostIP(UserCertificateAPI& sslapi)
179 std::vector<std::string> params;
180 params.push_back(user->GetRealHost());
181 params.push_back(user->GetIPString());
182 params.push_back(sslapi && sslapi->GetCertificate(user) ? "S" : "P");
184 SendSASL(user, "*", 'H', params);
188 SaslAuthenticator(LocalUser* user_, const std::string& method, UserCertificateAPI& sslapi)
191 , state_announced(false)
195 std::vector<std::string> params;
196 params.push_back(method);
198 const std::string fp = sslapi ? sslapi->GetFingerprint(user) : "";
200 params.push_back(fp);
202 SendSASL(user, "*", 'S', params);
205 SaslResult GetSaslResult(const std::string &result_)
216 /* checks for and deals with a state change. */
217 SaslState ProcessInboundMessage(const CommandBase::Params& msg)
222 this->agent = msg[0];
223 this->state = SASL_COMM;
226 if (msg[0] != this->agent)
234 ClientProtocol::Message authmsg("AUTHENTICATE");
235 authmsg.PushParamRef(msg[3]);
237 ClientProtocol::Event authevent(*g_protoev, authmsg);
238 LocalUser* const localuser = IS_LOCAL(user);
240 localuser->Send(authevent);
242 else if (msg[2] == "D")
244 this->state = SASL_DONE;
245 this->result = this->GetSaslResult(msg[3]);
247 else if (msg[2] == "M")
248 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
250 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
256 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
263 bool SendClientMessage(const std::vector<std::string>& parameters)
265 if (this->state != SASL_COMM)
268 SendSASL(this->user, this->agent, 'C', parameters);
270 if (parameters[0].c_str()[0] == '*')
272 this->state = SASL_DONE;
273 this->result = SASL_ABORT;
280 void AnnounceState(void)
282 if (this->state_announced)
285 switch (this->result)
288 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
291 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
294 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
300 this->state_announced = true;
304 class CommandAuthenticate : public SplitCommand
307 // The maximum length of an AUTHENTICATE request.
308 static const size_t MAX_AUTHENTICATE_SIZE = 400;
311 SimpleExtItem<SaslAuthenticator>& authExt;
312 Cap::Capability& cap;
313 UserCertificateAPI sslapi;
315 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
316 : SplitCommand(Creator, "AUTHENTICATE", 1)
321 works_before_reg = true;
322 allow_empty_last_param = false;
325 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
331 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
334 if (parameters[0].length() > MAX_AUTHENTICATE_SIZE)
336 user->WriteNumeric(ERR_SASLTOOLONG, "SASL message too long");
340 SaslAuthenticator *sasl = authExt.get(user);
342 authExt.set(user, new SaslAuthenticator(user, parameters[0], sslapi));
343 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
345 sasl->AnnounceState();
353 class CommandSASL : public Command
356 SimpleExtItem<SaslAuthenticator>& authExt;
357 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
359 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
362 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
364 User* target = ServerInstance->FindUUID(parameters[1]);
367 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
371 SaslAuthenticator *sasl = authExt.get(target);
375 SaslState state = sasl->ProcessInboundMessage(parameters);
376 if (state == SASL_DONE)
378 sasl->AnnounceState();
379 authExt.unset(target);
384 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
386 return ROUTE_BROADCAST;
390 class ModuleSASL : public Module
392 SimpleExtItem<SaslAuthenticator> authExt;
393 ServerTracker servertracker;
395 CommandAuthenticate auth;
397 Events::ModuleEventProvider sasleventprov;
398 ClientProtocol::EventProvider protoev;
402 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
403 , servertracker(this)
404 , cap(this, servertracker)
405 , auth(this, authExt, cap)
406 , sasl(this, authExt)
407 , sasleventprov(this, "event/sasl")
408 , protoev(this, auth.name)
410 saslevprov = &sasleventprov;
411 g_protoev = &protoev;
414 void init() CXX11_OVERRIDE
416 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
417 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!");
420 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
422 std::string target = ServerInstance->Config->ConfValue("sasl")->getString("target");
424 throw ModuleException("<sasl:target> must be set to the name of your services server!");
426 sasl_target = target;
427 servertracker.Reset();
430 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
432 if ((target == NULL) && (extname == "saslmechlist"))
433 cap.SetMechlist(extdata);
436 Version GetVersion() CXX11_OVERRIDE
438 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE", VF_VENDOR);
442 MODULE_INIT(ModuleSASL)