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
54 /* taken from m_services_account */
55 static bool ReadCGIIRCExt(const char* extname, User* user, std::string& out)
57 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
61 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
64 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
65 std::string* addr = stringext->get(user);
78 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", user, host))
82 if (!ReadCGIIRCExt("cgiirc_webirc_ip", user, ip))
84 ip = user->GetIPString();
88 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
89 if (ip.c_str()[0] == ':')
90 ip.insert(ip.begin(),1,'0');
94 params.push_back(sasl_target);
95 params.push_back("SASL");
96 params.push_back(user->uuid);
97 params.push_back("*");
98 params.push_back("H");
99 params.push_back(host);
100 params.push_back(ip);
106 SaslAuthenticator(User* user_, const std::string& method)
107 : user(user_), state(SASL_INIT), state_announced(false)
111 parameterlist params;
112 params.push_back(sasl_target);
113 params.push_back("SASL");
114 params.push_back(user->uuid);
115 params.push_back("*");
116 params.push_back("S");
117 params.push_back(method);
119 if (method == "EXTERNAL" && IS_LOCAL(user_))
121 SocketCertificateRequest req(&((LocalUser*)user_)->eh, ServerInstance->Modules->Find("m_sasl.so"));
122 std::string fp = req.GetFingerprint();
125 params.push_back(fp);
131 SaslResult GetSaslResult(const std::string &result_)
142 /* checks for and deals with a state change. */
143 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
148 this->agent = msg[0];
149 this->state = SASL_COMM;
152 if (msg[0] != this->agent)
159 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
160 else if (msg[2] == "D")
162 this->state = SASL_DONE;
163 this->result = this->GetSaslResult(msg[3]);
165 else if (msg[2] == "M")
166 this->user->WriteNumeric(908, "%s %s :are available SASL mechanisms", this->user->nick.c_str(), msg[3].c_str());
168 ServerInstance->Logs->Log("m_sasl", DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
174 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
183 this->state = SASL_DONE;
184 this->result = SASL_ABORT;
187 bool SendClientMessage(const std::vector<std::string>& parameters)
189 if (this->state != SASL_COMM)
192 parameterlist params;
193 params.push_back(sasl_target);
194 params.push_back("SASL");
195 params.push_back(this->user->uuid);
196 params.push_back(this->agent);
197 params.push_back("C");
199 params.insert(params.end(), parameters.begin(), parameters.end());
203 if (parameters[0].c_str()[0] == '*')
212 void AnnounceState(void)
214 if (this->state_announced)
217 switch (this->result)
220 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
223 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
226 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
232 this->state_announced = true;
236 class CommandAuthenticate : public Command
239 SimpleExtItem<SaslAuthenticator>& authExt;
241 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
242 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
244 works_before_reg = true;
245 allow_empty_last_param = false;
248 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
250 /* Only allow AUTHENTICATE on unregistered clients */
251 if (user->registered != REG_ALL)
253 if (!cap.ext.get(user))
256 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
259 SaslAuthenticator *sasl = authExt.get(user);
261 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
262 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
264 sasl->AnnounceState();
272 class CommandSASL : public Command
275 SimpleExtItem<SaslAuthenticator>& authExt;
276 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
278 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
281 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
283 User* target = ServerInstance->FindNick(parameters[1]);
284 if ((!target) || (IS_SERVER(target)))
286 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
290 SaslAuthenticator *sasl = authExt.get(target);
294 SaslState state = sasl->ProcessInboundMessage(parameters);
295 if (state == SASL_DONE)
297 sasl->AnnounceState();
298 authExt.unset(target);
303 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
305 return ROUTE_BROADCAST;
309 class ModuleSASL : public Module
311 SimpleExtItem<SaslAuthenticator> authExt;
313 CommandAuthenticate auth;
317 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
324 Implementation eventlist[] = { I_OnEvent, I_OnUserConnect, I_OnRehash };
325 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
327 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
328 ServerInstance->Modules->AddServices(providelist, 3);
330 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
331 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!");
336 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
339 void OnUserConnect(LocalUser *user)
341 SaslAuthenticator *sasl_ = authExt.get(user);
351 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
354 void OnEvent(Event &ev)
360 MODULE_INIT(ModuleSASL)