1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE. */
21 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
22 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
24 static std::string sasl_target = "*";
26 static void SendSASL(const parameterlist& params)
28 if (!ServerInstance->PI->SendEncapsulatedData(params))
30 SASLFallback(NULL, params);
35 * Tracks SASL authentication state like charybdis does. --nenolod
37 class SaslAuthenticator
47 SaslAuthenticator(User *user_, std::string method, Module *ctor)
48 : user(user_), state(SASL_INIT), state_announced(false)
51 params.push_back(sasl_target);
52 params.push_back("SASL");
53 params.push_back(user->uuid);
54 params.push_back("*");
55 params.push_back("S");
56 params.push_back(method);
61 SaslResult GetSaslResult(const std::string &result_)
72 /* checks for and deals with a state change. */
73 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
79 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
80 this->state = SASL_COMM;
83 if (msg[0] != this->agent)
87 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
90 this->state = SASL_DONE;
91 this->result = this->GetSaslResult(msg[3]);
98 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
107 this->state = SASL_DONE;
108 this->result = SASL_ABORT;
111 bool SendClientMessage(const std::vector<std::string>& parameters)
113 if (this->state != SASL_COMM)
116 parameterlist params;
117 params.push_back(sasl_target);
118 params.push_back("SASL");
119 params.push_back(this->user->uuid);
120 params.push_back(this->agent);
121 params.push_back("C");
123 params.insert(params.end(), parameters.begin(), parameters.end());
127 if (parameters[0][0] == '*')
136 void AnnounceState(void)
138 if (this->state_announced)
141 switch (this->result)
144 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
147 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
150 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
156 this->state_announced = true;
160 class CommandAuthenticate : public Command
163 SimpleExtItem<SaslAuthenticator>& authExt;
165 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
166 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
168 works_before_reg = true;
171 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
173 /* Only allow AUTHENTICATE on unregistered clients */
174 if (user->registered != REG_ALL)
176 if (!cap.ext.get(user))
179 SaslAuthenticator *sasl = authExt.get(user);
181 authExt.set(user, new SaslAuthenticator(user, parameters[0], creator));
182 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
184 sasl->AnnounceState();
192 class CommandSASL : public Command
195 SimpleExtItem<SaslAuthenticator>& authExt;
196 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
198 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
201 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
203 User* target = ServerInstance->FindNick(parameters[1]);
206 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
210 SaslAuthenticator *sasl = authExt.get(target);
214 SaslState state = sasl->ProcessInboundMessage(parameters);
215 if (state == SASL_DONE)
217 sasl->AnnounceState();
218 authExt.unset(target);
223 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
225 return ROUTE_BROADCAST;
229 class ModuleSASL : public Module
231 SimpleExtItem<SaslAuthenticator> authExt;
233 CommandAuthenticate auth;
237 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
244 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnRehash };
245 ServerInstance->Modules->Attach(eventlist, this, 3);
247 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
248 ServerInstance->Modules->AddServices(providelist, 3);
250 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
251 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!");
256 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
259 ModResult OnUserRegister(LocalUser *user)
261 SaslAuthenticator *sasl_ = authExt.get(user);
268 return MOD_RES_PASSTHRU;
273 return Version("Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE.",VF_VENDOR);
276 void OnEvent(Event &ev)
282 MODULE_INIT(ModuleSASL)