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 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
28 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
30 static std::string sasl_target = "*";
32 static void SendSASL(const parameterlist& params)
34 if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
36 SASLFallback(NULL, params);
41 * Tracks SASL authentication state like charybdis does. --nenolod
43 class SaslAuthenticator
53 SaslAuthenticator(User* user_, const std::string& method)
54 : user(user_), state(SASL_INIT), state_announced(false)
57 params.push_back(user->uuid);
58 params.push_back("*");
59 params.push_back("S");
60 params.push_back(method);
62 LocalUser* localuser = IS_LOCAL(user);
63 if (method == "EXTERNAL" && localuser)
65 std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
74 SaslResult GetSaslResult(const std::string &result_)
85 /* checks for and deals with a state change. */
86 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
92 this->state = SASL_COMM;
95 if (msg[0] != this->agent)
99 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
100 else if (msg[2] == "D")
102 this->state = SASL_DONE;
103 this->result = this->GetSaslResult(msg[3]);
105 else if (msg[2] == "M")
106 this->user->WriteNumeric(908, "%s %s :are available SASL mechanisms", this->user->nick.c_str(), msg[3].c_str());
108 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
114 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
123 this->state = SASL_DONE;
124 this->result = SASL_ABORT;
127 bool SendClientMessage(const std::vector<std::string>& parameters)
129 if (this->state != SASL_COMM)
132 parameterlist params;
133 params.push_back(this->user->uuid);
134 params.push_back(this->agent);
135 params.push_back("C");
137 params.insert(params.end(), parameters.begin(), parameters.end());
141 if (parameters[0][0] == '*')
150 void AnnounceState(void)
152 if (this->state_announced)
155 switch (this->result)
158 this->user->WriteNumeric(903, ":SASL authentication successful");
161 this->user->WriteNumeric(906, ":SASL authentication aborted");
164 this->user->WriteNumeric(904, ":SASL authentication failed");
170 this->state_announced = true;
174 class CommandAuthenticate : public Command
177 SimpleExtItem<SaslAuthenticator>& authExt;
179 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
180 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
182 works_before_reg = true;
185 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
187 /* Only allow AUTHENTICATE on unregistered clients */
188 if (user->registered != REG_ALL)
190 if (!cap.ext.get(user))
193 SaslAuthenticator *sasl = authExt.get(user);
195 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
196 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
198 sasl->AnnounceState();
206 class CommandSASL : public Command
209 SimpleExtItem<SaslAuthenticator>& authExt;
210 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
212 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
215 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
217 User* target = ServerInstance->FindNick(parameters[1]);
218 if ((!target) || (IS_SERVER(target)))
220 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
224 SaslAuthenticator *sasl = authExt.get(target);
228 SaslState state = sasl->ProcessInboundMessage(parameters);
229 if (state == SASL_DONE)
231 sasl->AnnounceState();
232 authExt.unset(target);
237 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
239 return ROUTE_BROADCAST;
243 class ModuleSASL : public Module
245 SimpleExtItem<SaslAuthenticator> authExt;
247 CommandAuthenticate auth;
252 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
256 void init() CXX11_OVERRIDE
258 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
259 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!");
262 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
264 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
267 ModResult OnUserRegister(LocalUser *user) CXX11_OVERRIDE
269 SaslAuthenticator *sasl_ = authExt.get(user);
276 return MOD_RES_PASSTHRU;
279 Version GetVersion() CXX11_OVERRIDE
281 return Version("Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE.",VF_VENDOR);
284 void OnEvent(Event &ev) CXX11_OVERRIDE
290 MODULE_INIT(ModuleSASL)