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 User* u = ServerInstance->FindUUID(params[2]);
40 u->WriteNumeric(904, "%s :SASL authentication failed", u->nick.c_str());
42 SASLFallback(NULL, params);
47 * Tracks SASL authentication state like charybdis does. --nenolod
49 class SaslAuthenticator
58 /* taken from m_services_account */
59 static bool ReadCGIIRCExt(const char* extname, User* user, std::string& out)
61 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
65 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
68 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
69 std::string* addr = stringext->get(user);
82 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", user, host))
86 if (!ReadCGIIRCExt("cgiirc_webirc_ip", user, ip))
88 ip = user->GetIPString();
92 /* IP addresses starting with a : on irc are a Bad Thing (tm) */
93 if (ip.c_str()[0] == ':')
94 ip.insert(ip.begin(),1,'0');
98 params.push_back(sasl_target);
99 params.push_back("SASL");
100 params.push_back(user->uuid);
101 params.push_back("*");
102 params.push_back("H");
103 params.push_back(host);
104 params.push_back(ip);
106 LocalUser* lu = IS_LOCAL(user);
109 // NOTE: SaslAuthenticator instances are only created for local
110 // users so this parameter will always be appended.
111 SocketCertificateRequest req(&lu->eh, ServerInstance->Modules->Find("m_sasl.so"));
112 params.push_back(req.cert ? "S" : "P");
119 SaslAuthenticator(User* user_, const std::string& method)
120 : user(user_), state(SASL_INIT), state_announced(false)
124 parameterlist params;
125 params.push_back(sasl_target);
126 params.push_back("SASL");
127 params.push_back(user->uuid);
128 params.push_back("*");
129 params.push_back("S");
130 params.push_back(method);
132 if (method == "EXTERNAL" && IS_LOCAL(user_))
134 SocketCertificateRequest req(&((LocalUser*)user_)->eh, ServerInstance->Modules->Find("m_sasl.so"));
135 std::string fp = req.GetFingerprint();
138 params.push_back(fp);
144 SaslResult GetSaslResult(const std::string &result_)
155 /* checks for and deals with a state change. */
156 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
161 this->agent = msg[0];
162 this->state = SASL_COMM;
165 if (msg[0] != this->agent)
172 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
173 else if (msg[2] == "D")
175 this->state = SASL_DONE;
176 this->result = this->GetSaslResult(msg[3]);
178 else if (msg[2] == "M")
179 this->user->WriteNumeric(908, "%s %s :are available SASL mechanisms", this->user->nick.c_str(), msg[3].c_str());
181 ServerInstance->Logs->Log("m_sasl", DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
187 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
196 this->state = SASL_DONE;
197 this->result = SASL_ABORT;
200 bool SendClientMessage(const std::vector<std::string>& parameters)
202 if (this->state != SASL_COMM)
205 parameterlist params;
206 params.push_back(sasl_target);
207 params.push_back("SASL");
208 params.push_back(this->user->uuid);
209 params.push_back(this->agent);
210 params.push_back("C");
212 params.insert(params.end(), parameters.begin(), parameters.end());
216 if (parameters[0].c_str()[0] == '*')
225 void AnnounceState(void)
227 if (this->state_announced)
230 switch (this->result)
233 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
236 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
239 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
245 this->state_announced = true;
249 class CommandAuthenticate : public Command
252 SimpleExtItem<SaslAuthenticator>& authExt;
254 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
255 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
257 works_before_reg = true;
258 allow_empty_last_param = false;
261 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
263 /* Only allow AUTHENTICATE on unregistered clients */
264 if (user->registered != REG_ALL)
266 if (!cap.ext.get(user))
269 if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
272 SaslAuthenticator *sasl = authExt.get(user);
274 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
275 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
277 sasl->AnnounceState();
285 class CommandSASL : public Command
288 SimpleExtItem<SaslAuthenticator>& authExt;
289 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
291 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
294 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
296 User* target = ServerInstance->FindNick(parameters[1]);
297 if ((!target) || (IS_SERVER(target)))
299 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
303 SaslAuthenticator *sasl = authExt.get(target);
307 SaslState state = sasl->ProcessInboundMessage(parameters);
308 if (state == SASL_DONE)
310 sasl->AnnounceState();
311 authExt.unset(target);
316 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
318 return ROUTE_BROADCAST;
322 class ModuleSASL : public Module
324 SimpleExtItem<SaslAuthenticator> authExt;
326 CommandAuthenticate auth;
330 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
337 Implementation eventlist[] = { I_OnEvent, I_OnUserConnect, I_OnRehash };
338 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
340 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
341 ServerInstance->Modules->AddServices(providelist, 3);
343 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
344 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!");
349 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
352 void OnUserConnect(LocalUser *user)
354 SaslAuthenticator *sasl_ = authExt.get(user);
364 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
367 void OnEvent(Event &ev)
373 MODULE_INIT(ModuleSASL)