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"
28 static std::string sasl_target;
30 class ServerTracker : public SpanningTreeEventListener
34 void Update(const Server* server, bool linked)
36 if (sasl_target == "*")
39 if (InspIRCd::Match(server->GetName(), sasl_target))
41 ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
46 void OnServerLink(const Server* server) CXX11_OVERRIDE
51 void OnServerSplit(const Server* server) CXX11_OVERRIDE
53 Update(server, false);
57 ServerTracker(Module* mod)
58 : SpanningTreeEventListener(mod)
65 if (sasl_target == "*")
73 ProtocolInterface::ServerList servers;
74 ServerInstance->PI->GetServerList(servers);
75 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
77 const ProtocolInterface::ServerInfo& server = *i;
78 if (InspIRCd::Match(server.servername, sasl_target))
86 bool IsOnline() const { return online; }
89 class SASLCap : public Cap::Capability
92 const ServerTracker& servertracker;
94 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
96 // Requesting this cap is allowed anytime
100 // But removing it can only be done when unregistered
101 return (user->registered != REG_ALL);
104 bool OnList(LocalUser* user) CXX11_OVERRIDE
106 return servertracker.IsOnline();
109 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
115 SASLCap(Module* mod, const ServerTracker& tracker)
116 : Cap::Capability(mod, "sasl")
117 , servertracker(tracker)
121 void SetMechlist(const std::string& newmechlist)
123 if (mechlist == newmechlist)
126 mechlist = newmechlist;
131 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
132 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
134 static Events::ModuleEventProvider* saslevprov;
136 static void SendSASL(const parameterlist& params)
138 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
140 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
145 * Tracks SASL authentication state like charybdis does. --nenolod
147 class SaslAuthenticator
154 bool state_announced;
157 SaslAuthenticator(User* user_, const std::string& method)
158 : user(user_), state(SASL_INIT), state_announced(false)
160 parameterlist params;
161 params.push_back(user->uuid);
162 params.push_back("*");
163 params.push_back("S");
164 params.push_back(method);
166 LocalUser* localuser = IS_LOCAL(user);
167 if (method == "EXTERNAL" && localuser)
169 std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
172 params.push_back(fp);
178 SaslResult GetSaslResult(const std::string &result_)
189 /* checks for and deals with a state change. */
190 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
195 this->agent = msg[0];
196 this->state = SASL_COMM;
199 if (msg[0] != this->agent)
206 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
207 else if (msg[2] == "D")
209 this->state = SASL_DONE;
210 this->result = this->GetSaslResult(msg[3]);
212 else if (msg[2] == "M")
213 this->user->WriteNumeric(908, msg[3], "are available SASL mechanisms");
215 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
221 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
230 this->state = SASL_DONE;
231 this->result = SASL_ABORT;
234 bool SendClientMessage(const std::vector<std::string>& parameters)
236 if (this->state != SASL_COMM)
239 parameterlist params;
240 params.push_back(this->user->uuid);
241 params.push_back(this->agent);
242 params.push_back("C");
244 params.insert(params.end(), parameters.begin(), parameters.end());
248 if (parameters[0].c_str()[0] == '*')
257 void AnnounceState(void)
259 if (this->state_announced)
262 switch (this->result)
265 this->user->WriteNumeric(903, "SASL authentication successful");
268 this->user->WriteNumeric(906, "SASL authentication aborted");
271 this->user->WriteNumeric(904, "SASL authentication failed");
277 this->state_announced = true;
281 class CommandAuthenticate : public Command
284 SimpleExtItem<SaslAuthenticator>& authExt;
285 Cap::Capability& cap;
286 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
287 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
289 works_before_reg = true;
292 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
298 SaslAuthenticator *sasl = authExt.get(user);
300 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
301 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
303 sasl->AnnounceState();
311 class CommandSASL : public Command
314 SimpleExtItem<SaslAuthenticator>& authExt;
315 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
317 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
320 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
322 User* target = ServerInstance->FindUUID(parameters[1]);
325 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
329 SaslAuthenticator *sasl = authExt.get(target);
333 SaslState state = sasl->ProcessInboundMessage(parameters);
334 if (state == SASL_DONE)
336 sasl->AnnounceState();
337 authExt.unset(target);
342 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
344 return ROUTE_BROADCAST;
348 class ModuleSASL : public Module
350 SimpleExtItem<SaslAuthenticator> authExt;
351 ServerTracker servertracker;
353 CommandAuthenticate auth;
355 Events::ModuleEventProvider sasleventprov;
359 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
360 , servertracker(this)
361 , cap(this, servertracker)
362 , auth(this, authExt, cap)
363 , sasl(this, authExt)
364 , sasleventprov(this, "event/sasl")
366 saslevprov = &sasleventprov;
369 void init() CXX11_OVERRIDE
371 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
372 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: m_services_account.so and m_cap.so are not loaded! m_sasl.so will NOT function correctly until these two modules are loaded!");
375 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
377 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
378 servertracker.Reset();
381 ModResult OnUserRegister(LocalUser *user) CXX11_OVERRIDE
383 SaslAuthenticator *sasl_ = authExt.get(user);
390 return MOD_RES_PASSTHRU;
393 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
395 if ((target == NULL) && (extname == "saslmechlist"))
396 cap.SetMechlist(extdata);
399 Version GetVersion() CXX11_OVERRIDE
401 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
405 MODULE_INIT(ModuleSASL)