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 * ---------------------------------------------------
18 /* $ModDesc: Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE. */
20 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
21 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
24 * Tracks SASL authentication state like charybdis does. --nenolod
26 class SaslAuthenticator
29 InspIRCd *ServerInstance;
37 SaslAuthenticator(User *user_, std::string method, Module *ctor)
38 : user(user_), state(SASL_INIT), state_announced(false)
41 params.push_back("*");
42 params.push_back("SASL");
43 params.push_back(user->uuid);
44 params.push_back("*");
45 params.push_back("S");
46 params.push_back(method);
48 ServerInstance->PI->SendEncapsulatedData(params);
51 SaslResult GetSaslResult(const std::string &result_)
62 /* checks for and deals with a state change. */
63 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
69 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
70 this->state = SASL_COMM;
73 if (msg[0] != this->agent)
77 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
80 this->state = SASL_DONE;
81 this->result = this->GetSaslResult(msg[3]);
88 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
97 this->state = SASL_DONE;
98 this->result = SASL_ABORT;
101 bool SendClientMessage(const std::vector<std::string>& parameters)
103 if (this->state != SASL_COMM)
106 parameterlist params;
107 params.push_back("*");
108 params.push_back("SASL");
109 params.push_back(this->user->uuid);
110 params.push_back(this->agent);
111 params.push_back("C");
113 params.insert(params.end(), parameters.begin(), parameters.end());
115 ServerInstance->PI->SendEncapsulatedData(params);
117 if (parameters[0][0] == '*')
126 void AnnounceState(void)
128 if (this->state_announced)
131 switch (this->result)
134 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
137 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
140 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
146 this->state_announced = true;
150 class CommandAuthenticate : public Command
153 SimpleExtItem<SaslAuthenticator>& authExt;
155 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
156 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
158 works_before_reg = true;
161 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
163 /* Only allow AUTHENTICATE on unregistered clients */
164 if (user->registered != REG_ALL)
166 if (!cap.ext.get(user))
169 SaslAuthenticator *sasl = authExt.get(user);
171 authExt.set(user, new SaslAuthenticator(user, parameters[0], creator));
172 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
174 sasl->AnnounceState();
182 class CommandSASL : public Command
185 SimpleExtItem<SaslAuthenticator>& authExt;
186 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
188 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
191 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
193 User* target = ServerInstance->FindNick(parameters[1]);
196 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
200 SaslAuthenticator *sasl = authExt.get(target);
204 SaslState state = sasl->ProcessInboundMessage(parameters);
205 if (state == SASL_DONE)
207 sasl->AnnounceState();
208 authExt.unset(target);
213 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
215 return ROUTE_BROADCAST;
219 class ModuleSASL : public Module
221 SimpleExtItem<SaslAuthenticator> authExt;
223 CommandAuthenticate auth;
227 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
229 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnSetConnectClass };
230 ServerInstance->Modules->Attach(eventlist, this, 3);
232 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
233 ServerInstance->Modules->AddServices(providelist, 3);
235 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
236 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!");
239 ModResult OnUserRegister(LocalUser *user)
241 SaslAuthenticator *sasl_ = authExt.get(user);
248 return MOD_RES_PASSTHRU;
251 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
253 if (myclass->config->getBool("requiresasl"))
255 const AccountExtItem* ext = GetAccountExtItem();
256 if (ext && !ext->get(user))
259 return MOD_RES_PASSTHRU;
268 return Version("Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE.",VF_VENDOR);
271 void OnEvent(Event &ev)
277 MODULE_INIT(ModuleSASL)