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/>.
27 /* $ModDesc: Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE. */
29 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
30 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
32 static std::string sasl_target = "*";
34 static void SendSASL(const parameterlist& params)
36 if (!ServerInstance->PI->SendEncapsulatedData(params))
38 SASLFallback(NULL, params);
43 * Tracks SASL authentication state like charybdis does. --nenolod
45 class SaslAuthenticator
55 SaslAuthenticator(User* user_, const std::string& method)
56 : user(user_), state(SASL_INIT), state_announced(false)
59 params.push_back(sasl_target);
60 params.push_back("SASL");
61 params.push_back(user->uuid);
62 params.push_back("*");
63 params.push_back("S");
64 params.push_back(method);
66 if (method == "EXTERNAL" && IS_LOCAL(user_))
68 SocketCertificateRequest req(&((LocalUser*)user_)->eh, ServerInstance->Modules->Find("m_sasl.so"));
69 std::string fp = req.GetFingerprint();
78 SaslResult GetSaslResult(const std::string &result_)
89 /* checks for and deals with a state change. */
90 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
96 this->state = SASL_COMM;
99 if (msg[0] != this->agent)
106 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
107 else if (msg[2] == "D")
109 this->state = SASL_DONE;
110 this->result = this->GetSaslResult(msg[3]);
112 else if (msg[2] == "M")
113 this->user->WriteNumeric(908, "%s %s :are available SASL mechanisms", this->user->nick.c_str(), msg[3].c_str());
115 ServerInstance->Logs->Log("m_sasl", DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
121 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
130 this->state = SASL_DONE;
131 this->result = SASL_ABORT;
134 bool SendClientMessage(const std::vector<std::string>& parameters)
136 if (this->state != SASL_COMM)
139 parameterlist params;
140 params.push_back(sasl_target);
141 params.push_back("SASL");
142 params.push_back(this->user->uuid);
143 params.push_back(this->agent);
144 params.push_back("C");
146 params.insert(params.end(), parameters.begin(), parameters.end());
150 if (parameters[0][0] == '*')
159 void AnnounceState(void)
161 if (this->state_announced)
164 switch (this->result)
167 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
170 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
173 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
179 this->state_announced = true;
183 class CommandAuthenticate : public Command
186 SimpleExtItem<SaslAuthenticator>& authExt;
188 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
189 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
191 works_before_reg = true;
194 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
196 /* Only allow AUTHENTICATE on unregistered clients */
197 if (user->registered != REG_ALL)
199 if (!cap.ext.get(user))
202 SaslAuthenticator *sasl = authExt.get(user);
204 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
205 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
207 sasl->AnnounceState();
215 class CommandSASL : public Command
218 SimpleExtItem<SaslAuthenticator>& authExt;
219 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
221 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
224 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
226 User* target = ServerInstance->FindNick(parameters[1]);
227 if ((!target) || (IS_SERVER(target)))
229 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
233 SaslAuthenticator *sasl = authExt.get(target);
237 SaslState state = sasl->ProcessInboundMessage(parameters);
238 if (state == SASL_DONE)
240 sasl->AnnounceState();
241 authExt.unset(target);
246 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
248 return ROUTE_BROADCAST;
252 class ModuleSASL : public Module
254 SimpleExtItem<SaslAuthenticator> authExt;
256 CommandAuthenticate auth;
260 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
267 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnRehash };
268 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
270 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
271 ServerInstance->Modules->AddServices(providelist, 3);
273 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
274 ServerInstance->Logs->Log("m_sasl", 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!");
279 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
282 ModResult OnUserRegister(LocalUser *user)
284 SaslAuthenticator *sasl_ = authExt.get(user);
291 return MOD_RES_PASSTHRU;
296 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
299 void OnEvent(Event &ev)
305 MODULE_INIT(ModuleSASL)