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
45 void Update(const Server* server, bool linked)
47 if (sasl_target == "*")
50 if (InspIRCd::Match(server->GetName(), sasl_target))
52 ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
57 void OnServerLink(const Server* server) CXX11_OVERRIDE
62 void OnServerSplit(const Server* server) CXX11_OVERRIDE
64 Update(server, false);
68 ServerTracker(Module* mod)
69 : ServerProtocol::LinkEventListener(mod)
76 if (sasl_target == "*")
84 ProtocolInterface::ServerList servers;
85 ServerInstance->PI->GetServerList(servers);
86 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
88 const ProtocolInterface::ServerInfo& server = *i;
89 if (InspIRCd::Match(server.servername, sasl_target))
97 bool IsOnline() const { return online; }
100 class SASLCap : public Cap::Capability
102 std::string mechlist;
103 const ServerTracker& servertracker;
105 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
107 // Requesting this cap is allowed anytime
111 // But removing it can only be done when unregistered
112 return (user->registered != REG_ALL);
115 bool OnList(LocalUser* user) CXX11_OVERRIDE
117 return servertracker.IsOnline();
120 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
126 SASLCap(Module* mod, const ServerTracker& tracker)
127 : Cap::Capability(mod, "sasl")
128 , servertracker(tracker)
132 void SetMechlist(const std::string& newmechlist)
134 if (mechlist == newmechlist)
137 mechlist = newmechlist;
142 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
143 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
145 static Events::ModuleEventProvider* saslevprov;
147 static void SendSASL(LocalUser* user, const std::string& agent, char mode, const std::vector<std::string>& parameters)
149 CommandBase::Params params;
150 params.push_back(user->uuid);
151 params.push_back(agent);
152 params.push_back(ConvToStr(mode));
153 params.insert(params.end(), parameters.begin(), parameters.end());
155 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
157 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
161 static ClientProtocol::EventProvider* g_protoev;
164 * Tracks SASL authentication state like charybdis does. --nenolod
166 class SaslAuthenticator
173 bool state_announced;
175 void SendHostIP(UserCertificateAPI& sslapi)
177 std::vector<std::string> params;
178 params.push_back(user->GetRealHost());
179 params.push_back(user->GetIPString());
180 params.push_back(sslapi && sslapi->GetCertificate(user) ? "S" : "P");
182 SendSASL(user, "*", 'H', params);
186 SaslAuthenticator(LocalUser* user_, const std::string& method, UserCertificateAPI& sslapi)
189 , state_announced(false)
193 std::vector<std::string> params;
194 params.push_back(method);
196 const std::string fp = sslapi ? sslapi->GetFingerprint(user) : "";
198 params.push_back(fp);
200 SendSASL(user, "*", 'S', params);
203 SaslResult GetSaslResult(const std::string &result_)
214 /* checks for and deals with a state change. */
215 SaslState ProcessInboundMessage(const CommandBase::Params& msg)
220 this->agent = msg[0];
221 this->state = SASL_COMM;
224 if (msg[0] != this->agent)
232 ClientProtocol::Message authmsg("AUTHENTICATE");
233 authmsg.PushParamRef(msg[3]);
235 ClientProtocol::Event authevent(*g_protoev, authmsg);
236 LocalUser* const localuser = IS_LOCAL(user);
238 localuser->Send(authevent);
240 else if (msg[2] == "D")
242 this->state = SASL_DONE;
243 this->result = this->GetSaslResult(msg[3]);
245 else if (msg[2] == "M")
246 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
248 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
254 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
261 bool SendClientMessage(const std::vector<std::string>& parameters)
263 if (this->state != SASL_COMM)
266 SendSASL(this->user, this->agent, 'C', parameters);
268 if (parameters[0].c_str()[0] == '*')
270 this->state = SASL_DONE;
271 this->result = SASL_ABORT;
278 void AnnounceState(void)
280 if (this->state_announced)
283 switch (this->result)
286 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
289 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
292 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
298 this->state_announced = true;
302 class CommandAuthenticate : public SplitCommand
305 // The maximum length of an AUTHENTICATE request.
306 static const size_t MAX_AUTHENTICATE_SIZE = 400;
309 SimpleExtItem<SaslAuthenticator>& authExt;
310 Cap::Capability& cap;
311 UserCertificateAPI sslapi;
313 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
314 : SplitCommand(Creator, "AUTHENTICATE", 1)
319 works_before_reg = true;
320 allow_empty_last_param = false;
323 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
329 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
332 if (parameters[0].length() > MAX_AUTHENTICATE_SIZE)
334 user->WriteNumeric(ERR_SASLTOOLONG, "SASL message too long");
338 SaslAuthenticator *sasl = authExt.get(user);
340 authExt.set(user, new SaslAuthenticator(user, parameters[0], sslapi));
341 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
343 sasl->AnnounceState();
351 class CommandSASL : public Command
354 SimpleExtItem<SaslAuthenticator>& authExt;
355 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
357 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
360 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
362 User* target = ServerInstance->FindUUID(parameters[1]);
365 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
369 SaslAuthenticator *sasl = authExt.get(target);
373 SaslState state = sasl->ProcessInboundMessage(parameters);
374 if (state == SASL_DONE)
376 sasl->AnnounceState();
377 authExt.unset(target);
382 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
384 return ROUTE_BROADCAST;
388 class ModuleSASL : public Module
390 SimpleExtItem<SaslAuthenticator> authExt;
391 ServerTracker servertracker;
393 CommandAuthenticate auth;
395 Events::ModuleEventProvider sasleventprov;
396 ClientProtocol::EventProvider protoev;
400 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
401 , servertracker(this)
402 , cap(this, servertracker)
403 , auth(this, authExt, cap)
404 , sasl(this, authExt)
405 , sasleventprov(this, "event/sasl")
406 , protoev(this, auth.name)
408 saslevprov = &sasleventprov;
409 g_protoev = &protoev;
412 void init() CXX11_OVERRIDE
414 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
415 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!");
418 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
420 std::string target = ServerInstance->Config->ConfValue("sasl")->getString("target");
422 throw ModuleException("<sasl:target> must be set to the name of your services server!");
424 sasl_target = target;
425 servertracker.Reset();
428 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
430 if ((target == NULL) && (extname == "saslmechlist"))
431 cap.SetMechlist(extdata);
434 Version GetVersion() CXX11_OVERRIDE
436 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE", VF_VENDOR);
440 MODULE_INIT(ModuleSASL)