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;
156 /* taken from m_services_account */
157 static bool ReadCGIIRCExt(const char* extname, User* user, std::string& out)
159 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
163 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
166 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
167 std::string* addr = stringext->get(user);
178 std::string host, ip;
180 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", user, host))
184 if (!ReadCGIIRCExt("cgiirc_webirc_ip", user, ip))
186 ip = user->GetIPString();
190 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
191 if (ip.c_str()[0] == ':')
192 ip.insert(ip.begin(),1,'0');
195 parameterlist params;
196 params.push_back(sasl_target);
197 params.push_back("SASL");
198 params.push_back(user->uuid);
199 params.push_back("*");
200 params.push_back("H");
201 params.push_back(host);
202 params.push_back(ip);
208 SaslAuthenticator(User* user_, const std::string& method)
209 : user(user_), state(SASL_INIT), state_announced(false)
213 parameterlist params;
214 params.push_back(user->uuid);
215 params.push_back("*");
216 params.push_back("S");
217 params.push_back(method);
219 LocalUser* localuser = IS_LOCAL(user);
220 if (method == "EXTERNAL" && localuser)
222 std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
225 params.push_back(fp);
231 SaslResult GetSaslResult(const std::string &result_)
242 /* checks for and deals with a state change. */
243 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
248 this->agent = msg[0];
249 this->state = SASL_COMM;
252 if (msg[0] != this->agent)
259 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
260 else if (msg[2] == "D")
262 this->state = SASL_DONE;
263 this->result = this->GetSaslResult(msg[3]);
265 else if (msg[2] == "M")
266 this->user->WriteNumeric(908, msg[3], "are available SASL mechanisms");
268 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
274 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
283 this->state = SASL_DONE;
284 this->result = SASL_ABORT;
287 bool SendClientMessage(const std::vector<std::string>& parameters)
289 if (this->state != SASL_COMM)
292 parameterlist params;
293 params.push_back(this->user->uuid);
294 params.push_back(this->agent);
295 params.push_back("C");
297 params.insert(params.end(), parameters.begin(), parameters.end());
301 if (parameters[0].c_str()[0] == '*')
310 void AnnounceState(void)
312 if (this->state_announced)
315 switch (this->result)
318 this->user->WriteNumeric(903, "SASL authentication successful");
321 this->user->WriteNumeric(906, "SASL authentication aborted");
324 this->user->WriteNumeric(904, "SASL authentication failed");
330 this->state_announced = true;
334 class CommandAuthenticate : public Command
337 SimpleExtItem<SaslAuthenticator>& authExt;
338 Cap::Capability& cap;
339 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
340 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
342 works_before_reg = true;
343 allow_empty_last_param = false;
346 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
352 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
355 SaslAuthenticator *sasl = authExt.get(user);
357 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
358 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
360 sasl->AnnounceState();
368 class CommandSASL : public Command
371 SimpleExtItem<SaslAuthenticator>& authExt;
372 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
374 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
377 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
379 User* target = ServerInstance->FindUUID(parameters[1]);
382 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
386 SaslAuthenticator *sasl = authExt.get(target);
390 SaslState state = sasl->ProcessInboundMessage(parameters);
391 if (state == SASL_DONE)
393 sasl->AnnounceState();
394 authExt.unset(target);
399 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
401 return ROUTE_BROADCAST;
405 class ModuleSASL : public Module
407 SimpleExtItem<SaslAuthenticator> authExt;
408 ServerTracker servertracker;
410 CommandAuthenticate auth;
412 Events::ModuleEventProvider sasleventprov;
416 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
417 , servertracker(this)
418 , cap(this, servertracker)
419 , auth(this, authExt, cap)
420 , sasl(this, authExt)
421 , sasleventprov(this, "event/sasl")
423 saslevprov = &sasleventprov;
426 void init() CXX11_OVERRIDE
428 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
429 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!");
432 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
434 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
435 servertracker.Reset();
438 void OnUserConnect(LocalUser *user) CXX11_OVERRIDE
440 SaslAuthenticator *sasl_ = authExt.get(user);
448 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
450 if ((target == NULL) && (extname == "saslmechlist"))
451 cap.SetMechlist(extdata);
454 Version GetVersion() CXX11_OVERRIDE
456 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
460 MODULE_INIT(ModuleSASL)