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"
27 class SASLCap : public Cap::Capability
31 bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
33 // Requesting this cap is allowed anytime
37 // But removing it can only be done when unregistered
38 return (user->registered != REG_ALL);
41 const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
48 : Cap::Capability(mod, "sasl")
52 void SetMechlist(const std::string& newmechlist)
54 if (mechlist == newmechlist)
57 mechlist = newmechlist;
62 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
63 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
65 static std::string sasl_target = "*";
66 static Events::ModuleEventProvider* saslevprov;
68 static void SendSASL(const parameterlist& params)
70 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
72 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
77 * Tracks SASL authentication state like charybdis does. --nenolod
79 class SaslAuthenticator
89 SaslAuthenticator(User* user_, const std::string& method)
90 : user(user_), state(SASL_INIT), state_announced(false)
93 params.push_back(user->uuid);
94 params.push_back("*");
95 params.push_back("S");
96 params.push_back(method);
98 LocalUser* localuser = IS_LOCAL(user);
99 if (method == "EXTERNAL" && localuser)
101 std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
104 params.push_back(fp);
110 SaslResult GetSaslResult(const std::string &result_)
121 /* checks for and deals with a state change. */
122 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
127 this->agent = msg[0];
128 this->state = SASL_COMM;
131 if (msg[0] != this->agent)
138 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
139 else if (msg[2] == "D")
141 this->state = SASL_DONE;
142 this->result = this->GetSaslResult(msg[3]);
144 else if (msg[2] == "M")
145 this->user->WriteNumeric(908, msg[3], "are available SASL mechanisms");
147 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
153 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
162 this->state = SASL_DONE;
163 this->result = SASL_ABORT;
166 bool SendClientMessage(const std::vector<std::string>& parameters)
168 if (this->state != SASL_COMM)
171 parameterlist params;
172 params.push_back(this->user->uuid);
173 params.push_back(this->agent);
174 params.push_back("C");
176 params.insert(params.end(), parameters.begin(), parameters.end());
180 if (parameters[0][0] == '*')
189 void AnnounceState(void)
191 if (this->state_announced)
194 switch (this->result)
197 this->user->WriteNumeric(903, "SASL authentication successful");
200 this->user->WriteNumeric(906, "SASL authentication aborted");
203 this->user->WriteNumeric(904, "SASL authentication failed");
209 this->state_announced = true;
213 class CommandAuthenticate : public Command
216 SimpleExtItem<SaslAuthenticator>& authExt;
217 Cap::Capability& cap;
218 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
219 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
221 works_before_reg = true;
224 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
230 SaslAuthenticator *sasl = authExt.get(user);
232 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
233 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
235 sasl->AnnounceState();
243 class CommandSASL : public Command
246 SimpleExtItem<SaslAuthenticator>& authExt;
247 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
249 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
252 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
254 User* target = ServerInstance->FindUUID(parameters[1]);
257 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
261 SaslAuthenticator *sasl = authExt.get(target);
265 SaslState state = sasl->ProcessInboundMessage(parameters);
266 if (state == SASL_DONE)
268 sasl->AnnounceState();
269 authExt.unset(target);
274 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
276 return ROUTE_BROADCAST;
280 class ModuleSASL : public Module
282 SimpleExtItem<SaslAuthenticator> authExt;
284 CommandAuthenticate auth;
286 Events::ModuleEventProvider sasleventprov;
290 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
292 , auth(this, authExt, cap)
293 , sasl(this, authExt)
294 , sasleventprov(this, "event/sasl")
296 saslevprov = &sasleventprov;
299 void init() CXX11_OVERRIDE
301 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
302 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!");
305 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
307 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
310 ModResult OnUserRegister(LocalUser *user) CXX11_OVERRIDE
312 SaslAuthenticator *sasl_ = authExt.get(user);
319 return MOD_RES_PASSTHRU;
322 void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
324 if ((target == NULL) && (extname == "saslmechlist"))
325 cap.SetMechlist(extdata);
328 Version GetVersion() CXX11_OVERRIDE
330 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
334 MODULE_INIT(ModuleSASL)