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_SASLABORTED = 906,
37 static std::string sasl_target;
39 class ServerTracker : public SpanningTreeEventListener
43 void Update(const Server* server, bool linked)
45 if (sasl_target == "*")
48 if (InspIRCd::Match(server->GetName(), sasl_target))
50 ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
55 void OnServerLink(const Server* server) CXX11_OVERRIDE
60 void OnServerSplit(const Server* server) CXX11_OVERRIDE
62 Update(server, false);
66 ServerTracker(Module* mod)
67 : SpanningTreeEventListener(mod)
74 if (sasl_target == "*")
82 ProtocolInterface::ServerList servers;
83 ServerInstance->PI->GetServerList(servers);
84 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
86 const ProtocolInterface::ServerInfo& server = *i;
87 if (InspIRCd::Match(server.servername, sasl_target))
95 bool IsOnline() const { return online; }
98 class SASLCap : public Cap::Capability
100 std::string mechlist;
101 const ServerTracker& servertracker;
103 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
105 // Requesting this cap is allowed anytime
109 // But removing it can only be done when unregistered
110 return (user->registered != REG_ALL);
113 bool OnList(LocalUser* user) CXX11_OVERRIDE
115 return servertracker.IsOnline();
118 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
124 SASLCap(Module* mod, const ServerTracker& tracker)
125 : Cap::Capability(mod, "sasl")
126 , servertracker(tracker)
130 void SetMechlist(const std::string& newmechlist)
132 if (mechlist == newmechlist)
135 mechlist = newmechlist;
140 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
141 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
143 static Events::ModuleEventProvider* saslevprov;
145 static void SendSASL(const parameterlist& params)
147 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
149 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
154 * Tracks SASL authentication state like charybdis does. --nenolod
156 class SaslAuthenticator
163 bool state_announced;
165 /* taken from m_services_account */
166 static bool ReadCGIIRCExt(const char* extname, User* user, std::string& out)
168 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
172 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
175 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
176 std::string* addr = stringext->get(user);
187 std::string host, ip;
189 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", user, host))
193 if (!ReadCGIIRCExt("cgiirc_webirc_ip", user, ip))
195 ip = user->GetIPString();
199 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
200 if (ip.c_str()[0] == ':')
201 ip.insert(ip.begin(),1,'0');
204 parameterlist params;
205 params.push_back(sasl_target);
206 params.push_back("SASL");
207 params.push_back(user->uuid);
208 params.push_back("*");
209 params.push_back("H");
210 params.push_back(host);
211 params.push_back(ip);
217 SaslAuthenticator(User* user_, const std::string& method)
218 : user(user_), state(SASL_INIT), state_announced(false)
222 parameterlist params;
223 params.push_back(user->uuid);
224 params.push_back("*");
225 params.push_back("S");
226 params.push_back(method);
228 LocalUser* localuser = IS_LOCAL(user);
231 std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
234 params.push_back(fp);
240 SaslResult GetSaslResult(const std::string &result_)
251 /* checks for and deals with a state change. */
252 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
257 this->agent = msg[0];
258 this->state = SASL_COMM;
261 if (msg[0] != this->agent)
268 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
269 else if (msg[2] == "D")
271 this->state = SASL_DONE;
272 this->result = this->GetSaslResult(msg[3]);
274 else if (msg[2] == "M")
275 this->user->WriteNumeric(RPL_SASLMECHS, msg[3], "are available SASL mechanisms");
277 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
283 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
292 this->state = SASL_DONE;
293 this->result = SASL_ABORT;
296 bool SendClientMessage(const std::vector<std::string>& parameters)
298 if (this->state != SASL_COMM)
301 parameterlist params;
302 params.push_back(this->user->uuid);
303 params.push_back(this->agent);
304 params.push_back("C");
306 params.insert(params.end(), parameters.begin(), parameters.end());
310 if (parameters[0].c_str()[0] == '*')
319 void AnnounceState(void)
321 if (this->state_announced)
324 switch (this->result)
327 this->user->WriteNumeric(RPL_SASLSUCCESS, "SASL authentication successful");
330 this->user->WriteNumeric(ERR_SASLABORTED, "SASL authentication aborted");
333 this->user->WriteNumeric(ERR_SASLFAIL, "SASL authentication failed");
339 this->state_announced = true;
343 class CommandAuthenticate : public Command
346 SimpleExtItem<SaslAuthenticator>& authExt;
347 Cap::Capability& cap;
348 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
349 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
351 works_before_reg = true;
352 allow_empty_last_param = false;
355 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
361 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
364 SaslAuthenticator *sasl = authExt.get(user);
366 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
367 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
369 sasl->AnnounceState();
377 class CommandSASL : public Command
380 SimpleExtItem<SaslAuthenticator>& authExt;
381 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
383 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
386 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
388 User* target = ServerInstance->FindUUID(parameters[1]);
391 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
395 SaslAuthenticator *sasl = authExt.get(target);
399 SaslState state = sasl->ProcessInboundMessage(parameters);
400 if (state == SASL_DONE)
402 sasl->AnnounceState();
403 authExt.unset(target);
408 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
410 return ROUTE_BROADCAST;
414 class ModuleSASL : public Module
416 SimpleExtItem<SaslAuthenticator> authExt;
417 ServerTracker servertracker;
419 CommandAuthenticate auth;
421 Events::ModuleEventProvider sasleventprov;
425 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
426 , servertracker(this)
427 , cap(this, servertracker)
428 , auth(this, authExt, cap)
429 , sasl(this, authExt)
430 , sasleventprov(this, "event/sasl")
432 saslevprov = &sasleventprov;
435 void init() CXX11_OVERRIDE
437 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
438 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!");
441 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
443 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
444 servertracker.Reset();
447 void OnUserConnect(LocalUser *user) CXX11_OVERRIDE
449 SaslAuthenticator *sasl_ = authExt.get(user);
457 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
459 if ((target == NULL) && (extname == "saslmechlist"))
460 cap.SetMechlist(extdata);
463 Version GetVersion() CXX11_OVERRIDE
465 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
469 MODULE_INIT(ModuleSASL)