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
93 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
95 // Requesting this cap is allowed anytime
99 // But removing it can only be done when unregistered
100 return (user->registered != REG_ALL);
103 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
110 : Cap::Capability(mod, "sasl")
114 void SetMechlist(const std::string& newmechlist)
116 if (mechlist == newmechlist)
119 mechlist = newmechlist;
124 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
125 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
127 static Events::ModuleEventProvider* saslevprov;
129 static void SendSASL(const parameterlist& params)
131 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
133 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
138 * Tracks SASL authentication state like charybdis does. --nenolod
140 class SaslAuthenticator
147 bool state_announced;
150 SaslAuthenticator(User* user_, const std::string& method)
151 : user(user_), state(SASL_INIT), state_announced(false)
153 parameterlist params;
154 params.push_back(user->uuid);
155 params.push_back("*");
156 params.push_back("S");
157 params.push_back(method);
159 LocalUser* localuser = IS_LOCAL(user);
160 if (method == "EXTERNAL" && localuser)
162 std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
165 params.push_back(fp);
171 SaslResult GetSaslResult(const std::string &result_)
182 /* checks for and deals with a state change. */
183 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
188 this->agent = msg[0];
189 this->state = SASL_COMM;
192 if (msg[0] != this->agent)
199 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
200 else if (msg[2] == "D")
202 this->state = SASL_DONE;
203 this->result = this->GetSaslResult(msg[3]);
205 else if (msg[2] == "M")
206 this->user->WriteNumeric(908, msg[3], "are available SASL mechanisms");
208 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
214 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
223 this->state = SASL_DONE;
224 this->result = SASL_ABORT;
227 bool SendClientMessage(const std::vector<std::string>& parameters)
229 if (this->state != SASL_COMM)
232 parameterlist params;
233 params.push_back(this->user->uuid);
234 params.push_back(this->agent);
235 params.push_back("C");
237 params.insert(params.end(), parameters.begin(), parameters.end());
241 if (parameters[0][0] == '*')
250 void AnnounceState(void)
252 if (this->state_announced)
255 switch (this->result)
258 this->user->WriteNumeric(903, "SASL authentication successful");
261 this->user->WriteNumeric(906, "SASL authentication aborted");
264 this->user->WriteNumeric(904, "SASL authentication failed");
270 this->state_announced = true;
274 class CommandAuthenticate : public Command
277 SimpleExtItem<SaslAuthenticator>& authExt;
278 Cap::Capability& cap;
279 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
280 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
282 works_before_reg = true;
285 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
291 SaslAuthenticator *sasl = authExt.get(user);
293 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
294 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
296 sasl->AnnounceState();
304 class CommandSASL : public Command
307 SimpleExtItem<SaslAuthenticator>& authExt;
308 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
310 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
313 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
315 User* target = ServerInstance->FindUUID(parameters[1]);
318 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
322 SaslAuthenticator *sasl = authExt.get(target);
326 SaslState state = sasl->ProcessInboundMessage(parameters);
327 if (state == SASL_DONE)
329 sasl->AnnounceState();
330 authExt.unset(target);
335 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
337 return ROUTE_BROADCAST;
341 class ModuleSASL : public Module
343 SimpleExtItem<SaslAuthenticator> authExt;
344 ServerTracker servertracker;
346 CommandAuthenticate auth;
348 Events::ModuleEventProvider sasleventprov;
352 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
353 , servertracker(this)
355 , auth(this, authExt, cap)
356 , sasl(this, authExt)
357 , sasleventprov(this, "event/sasl")
359 saslevprov = &sasleventprov;
362 void init() CXX11_OVERRIDE
364 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
365 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!");
368 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
370 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
371 servertracker.Reset();
374 ModResult OnUserRegister(LocalUser *user) CXX11_OVERRIDE
376 SaslAuthenticator *sasl_ = authExt.get(user);
383 return MOD_RES_PASSTHRU;
386 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
388 if ((target == NULL) && (extname == "saslmechlist"))
389 cap.SetMechlist(extdata);
392 Version GetVersion() CXX11_OVERRIDE
394 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
398 MODULE_INIT(ModuleSASL)