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;
40 class ServerTracker : public ServerEventListener
44 void Update(const Server* server, bool linked)
46 if (sasl_target == "*")
49 if (InspIRCd::Match(server->GetName(), sasl_target))
51 ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
56 void OnServerLink(const Server* server) CXX11_OVERRIDE
61 void OnServerSplit(const Server* server) CXX11_OVERRIDE
63 Update(server, false);
67 ServerTracker(Module* mod)
68 : ServerEventListener(mod)
75 if (sasl_target == "*")
83 ProtocolInterface::ServerList servers;
84 ServerInstance->PI->GetServerList(servers);
85 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
87 const ProtocolInterface::ServerInfo& server = *i;
88 if (InspIRCd::Match(server.servername, sasl_target))
96 bool IsOnline() const { return online; }
99 class SASLCap : public Cap::Capability
101 std::string mechlist;
102 const ServerTracker& servertracker;
104 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
106 // Requesting this cap is allowed anytime
110 // But removing it can only be done when unregistered
111 return (user->registered != REG_ALL);
114 bool OnList(LocalUser* user) CXX11_OVERRIDE
116 return servertracker.IsOnline();
119 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
125 SASLCap(Module* mod, const ServerTracker& tracker)
126 : Cap::Capability(mod, "sasl")
127 , servertracker(tracker)
131 void SetMechlist(const std::string& newmechlist)
133 if (mechlist == newmechlist)
136 mechlist = newmechlist;
141 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
142 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
144 static Events::ModuleEventProvider* saslevprov;
146 static void SendSASL(LocalUser* user, const std::string& agent, char mode, const std::vector<std::string>& parameters)
148 CommandBase::Params params;
149 params.push_back(user->uuid);
150 params.push_back(agent);
151 params.push_back(ConvToStr(mode));
152 params.insert(params.end(), parameters.begin(), parameters.end());
154 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
156 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
160 static ClientProtocol::EventProvider* g_protoev;
163 * Tracks SASL authentication state like charybdis does. --nenolod
165 class SaslAuthenticator
172 bool state_announced;
176 std::vector<std::string> params;
177 params.push_back(user->GetRealHost());
178 params.push_back(user->GetIPString());
179 params.push_back(SSLIOHook::IsSSL(&user->eh) ? "S" : "P");
181 SendSASL(user, "*", 'H', params);
185 SaslAuthenticator(LocalUser* user_, const std::string& method)
186 : user(user_), state(SASL_INIT), state_announced(false)
190 std::vector<std::string> params;
191 params.push_back(method);
193 const std::string fp = SSLClientCert::GetFingerprint(&user->eh);
195 params.push_back(fp);
197 SendSASL(user, "*", 'S', params);
200 SaslResult GetSaslResult(const std::string &result_)
211 /* checks for and deals with a state change. */
212 SaslState ProcessInboundMessage(const CommandBase::Params& msg)
217 this->agent = msg[0];
218 this->state = SASL_COMM;
221 if (msg[0] != this->agent)
229 ClientProtocol::Message authmsg("AUTHENTICATE");
230 authmsg.PushParamRef(msg[3]);
232 ClientProtocol::Event authevent(*g_protoev, authmsg);
233 LocalUser* const localuser = IS_LOCAL(user);
235 localuser->Send(authevent);
237 else if (msg[2] == "D")
239 this->state = SASL_DONE;
240 this->result = this->GetSaslResult(msg[3]);
242 else if (msg[2] == "M")
243 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
245 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
251 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
258 bool SendClientMessage(const std::vector<std::string>& parameters)
260 if (this->state != SASL_COMM)
263 SendSASL(this->user, this->agent, 'C', parameters);
265 if (parameters[0].c_str()[0] == '*')
267 this->state = SASL_DONE;
268 this->result = SASL_ABORT;
275 void AnnounceState(void)
277 if (this->state_announced)
280 switch (this->result)
283 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
286 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
289 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
295 this->state_announced = true;
299 class CommandAuthenticate : public SplitCommand
302 // The maximum length of an AUTHENTICATE request.
303 static const size_t MAX_AUTHENTICATE_SIZE = 400;
306 SimpleExtItem<SaslAuthenticator>& authExt;
307 Cap::Capability& cap;
308 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
309 : SplitCommand(Creator, "AUTHENTICATE", 1)
313 works_before_reg = true;
314 allow_empty_last_param = false;
317 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
323 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
326 if (parameters[0].length() > MAX_AUTHENTICATE_SIZE)
328 user->WriteNumeric(ERR_SASLTOOLONG, "SASL message too long");
332 SaslAuthenticator *sasl = authExt.get(user);
334 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
335 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
337 sasl->AnnounceState();
345 class CommandSASL : public Command
348 SimpleExtItem<SaslAuthenticator>& authExt;
349 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
351 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
354 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
356 User* target = ServerInstance->FindUUID(parameters[1]);
359 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
363 SaslAuthenticator *sasl = authExt.get(target);
367 SaslState state = sasl->ProcessInboundMessage(parameters);
368 if (state == SASL_DONE)
370 sasl->AnnounceState();
371 authExt.unset(target);
376 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
378 return ROUTE_BROADCAST;
382 class ModuleSASL : public Module
384 SimpleExtItem<SaslAuthenticator> authExt;
385 ServerTracker servertracker;
387 CommandAuthenticate auth;
389 Events::ModuleEventProvider sasleventprov;
390 ClientProtocol::EventProvider protoev;
394 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
395 , servertracker(this)
396 , cap(this, servertracker)
397 , auth(this, authExt, cap)
398 , sasl(this, authExt)
399 , sasleventprov(this, "event/sasl")
400 , protoev(this, auth.name)
402 saslevprov = &sasleventprov;
403 g_protoev = &protoev;
406 void init() CXX11_OVERRIDE
408 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
409 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!");
412 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
414 std::string target = ServerInstance->Config->ConfValue("sasl")->getString("target");
416 throw ModuleException("<sasl:target> must be set to the name of your services server!");
418 sasl_target = target;
419 servertracker.Reset();
422 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
424 if ((target == NULL) && (extname == "saslmechlist"))
425 cap.SetMechlist(extdata);
428 Version GetVersion() CXX11_OVERRIDE
430 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
434 MODULE_INIT(ModuleSASL)