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
36 SaslAuthenticator(User *user_, std::string method, Module *ctor)
37 : user(user_), state(SASL_INIT), state_announced(false)
40 params.push_back("*");
41 params.push_back("SASL");
42 params.push_back(user->uuid);
43 params.push_back("*");
44 params.push_back("S");
45 params.push_back(method);
47 ServerInstance->PI->SendEncapsulatedData(params);
50 SaslResult GetSaslResult(const std::string &result_)
61 /* checks for and deals with a state change. */
62 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
68 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
69 this->state = SASL_COMM;
72 if (msg[0] != this->agent)
76 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
79 this->state = SASL_DONE;
80 this->result = this->GetSaslResult(msg[3]);
87 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
96 this->state = SASL_DONE;
97 this->result = SASL_ABORT;
100 bool SendClientMessage(const std::vector<std::string>& parameters)
102 if (this->state != SASL_COMM)
105 parameterlist params;
106 params.push_back("*");
107 params.push_back("SASL");
108 params.push_back(this->user->uuid);
109 params.push_back(this->agent);
110 params.push_back("C");
112 params.insert(params.end(), parameters.begin(), parameters.end());
114 ServerInstance->PI->SendEncapsulatedData(params);
116 if (parameters[0][0] == '*')
125 void AnnounceState(void)
127 if (this->state_announced)
130 switch (this->result)
133 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
136 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
139 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
145 this->state_announced = true;
149 class CommandAuthenticate : public Command
152 SimpleExtItem<SaslAuthenticator>& authExt;
154 CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
155 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
157 works_before_reg = true;
160 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
162 /* Only allow AUTHENTICATE on unregistered clients */
163 if (user->registered != REG_ALL)
165 if (!cap.ext.get(user))
168 SaslAuthenticator *sasl = authExt.get(user);
170 authExt.set(user, new SaslAuthenticator(user, parameters[0], creator));
171 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
173 sasl->AnnounceState();
181 class CommandSASL : public Command
184 SimpleExtItem<SaslAuthenticator>& authExt;
185 CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
187 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
190 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
192 User* target = ServerInstance->FindNick(parameters[1]);
195 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
199 SaslAuthenticator *sasl = authExt.get(target);
203 SaslState state = sasl->ProcessInboundMessage(parameters);
204 if (state == SASL_DONE)
206 sasl->AnnounceState();
207 authExt.unset(target);
212 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
214 return ROUTE_BROADCAST;
218 class ModuleSASL : public Module
220 SimpleExtItem<SaslAuthenticator> authExt;
222 CommandAuthenticate auth;
226 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
228 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister };
229 ServerInstance->Modules->Attach(eventlist, this, 2);
231 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
232 ServerInstance->Modules->AddServices(providelist, 3);
234 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
235 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!");
238 ModResult OnUserRegister(LocalUser *user)
240 SaslAuthenticator *sasl_ = authExt.get(user);
247 return MOD_RES_PASSTHRU;
252 return Version("Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE.",VF_VENDOR);
255 void OnEvent(Event &ev)
261 MODULE_INIT(ModuleSASL)