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/spanningtree.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 SpanningTreeEventListener
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 : SpanningTreeEventListener(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(const parameterlist& params)
148 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
150 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
155 * Tracks SASL authentication state like charybdis does. --nenolod
157 class SaslAuthenticator
164 bool state_announced;
168 parameterlist params;
169 params.push_back(sasl_target);
170 params.push_back("SASL");
171 params.push_back(user->uuid);
172 params.push_back("*");
173 params.push_back("H");
174 params.push_back(user->host);
175 params.push_back(user->GetIPString());
181 SaslAuthenticator(LocalUser* user_, const std::string& method)
182 : user(user_), state(SASL_INIT), state_announced(false)
186 parameterlist params;
187 params.push_back(user->uuid);
188 params.push_back("*");
189 params.push_back("S");
190 params.push_back(method);
192 const std::string fp = SSLClientCert::GetFingerprint(&user->eh);
194 params.push_back(fp);
199 SaslResult GetSaslResult(const std::string &result_)
210 /* checks for and deals with a state change. */
211 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
216 this->agent = msg[0];
217 this->state = SASL_COMM;
220 if (msg[0] != this->agent)
227 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
228 else if (msg[2] == "D")
230 this->state = SASL_DONE;
231 this->result = this->GetSaslResult(msg[3]);
233 else if (msg[2] == "M")
234 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
236 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
242 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
249 bool SendClientMessage(const std::vector<std::string>& parameters)
251 if (this->state != SASL_COMM)
254 parameterlist params;
255 params.push_back(this->user->uuid);
256 params.push_back(this->agent);
257 params.push_back("C");
259 params.insert(params.end(), parameters.begin(), parameters.end());
263 if (parameters[0].c_str()[0] == '*')
265 this->state = SASL_DONE;
266 this->result = SASL_ABORT;
273 void AnnounceState(void)
275 if (this->state_announced)
278 switch (this->result)
281 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
284 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
287 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
293 this->state_announced = true;
297 class CommandAuthenticate : public SplitCommand
300 // The maximum length of an AUTHENTICATE request.
301 static const size_t MAX_AUTHENTICATE_SIZE = 400;
304 SimpleExtItem<SaslAuthenticator>& authExt;
305 Cap::Capability& cap;
306 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
307 : SplitCommand(Creator, "AUTHENTICATE", 1)
311 works_before_reg = true;
312 allow_empty_last_param = false;
315 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
321 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
324 if (parameters[0].length() > MAX_AUTHENTICATE_SIZE)
326 user->WriteNumeric(ERR_SASLTOOLONG, "SASL message too long");
330 SaslAuthenticator *sasl = authExt.get(user);
332 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
333 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
335 sasl->AnnounceState();
343 class CommandSASL : public Command
346 SimpleExtItem<SaslAuthenticator>& authExt;
347 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
349 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
352 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
354 User* target = ServerInstance->FindUUID(parameters[1]);
357 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
361 SaslAuthenticator *sasl = authExt.get(target);
365 SaslState state = sasl->ProcessInboundMessage(parameters);
366 if (state == SASL_DONE)
368 sasl->AnnounceState();
369 authExt.unset(target);
374 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
376 return ROUTE_BROADCAST;
380 class ModuleSASL : public Module
382 SimpleExtItem<SaslAuthenticator> authExt;
383 ServerTracker servertracker;
385 CommandAuthenticate auth;
387 Events::ModuleEventProvider sasleventprov;
391 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
392 , servertracker(this)
393 , cap(this, servertracker)
394 , auth(this, authExt, cap)
395 , sasl(this, authExt)
396 , sasleventprov(this, "event/sasl")
398 saslevprov = &sasleventprov;
401 void init() CXX11_OVERRIDE
403 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
404 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!");
407 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
409 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
410 servertracker.Reset();
413 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
415 if ((target == NULL) && (extname == "saslmechlist"))
416 cap.SetMechlist(extdata);
419 Version GetVersion() CXX11_OVERRIDE
421 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
425 MODULE_INIT(ModuleSASL)