1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 : public classbase
29 InspIRCd *ServerInstance;
37 SaslAuthenticator(User *user_, std::string method, InspIRCd *instance, Module *ctor)
38 : ServerInstance(instance), user(user_), state(SASL_INIT), state_announced(false)
40 this->user->Extend("sasl_authenticator", this);
43 params.push_back("*");
44 params.push_back("SASL");
45 params.push_back(user->uuid);
46 params.push_back("*");
47 params.push_back("S");
48 params.push_back(method);
50 ServerInstance->PI->SendEncapsulatedData(params);
53 SaslResult GetSaslResult(const std::string &result_)
64 /* checks for and deals with a state change. */
65 SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
71 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
72 this->state = SASL_COMM;
75 if (msg[0] != this->agent)
79 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
82 this->state = SASL_DONE;
83 this->result = this->GetSaslResult(msg[3]);
90 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
99 this->state = SASL_DONE;
100 this->result = SASL_ABORT;
103 bool SendClientMessage(const std::vector<std::string>& parameters)
105 if (this->state != SASL_COMM)
108 parameterlist params;
109 params.push_back("*");
110 params.push_back("SASL");
111 params.push_back(this->user->uuid);
112 params.push_back(this->agent);
113 params.push_back("C");
115 params.insert(params.end(), parameters.begin(), parameters.end());
117 ServerInstance->PI->SendEncapsulatedData(params);
119 if (parameters[0][0] == '*')
128 void AnnounceState(void)
130 if (this->state_announced)
133 switch (this->result)
136 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
139 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
142 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
148 this->state_announced = true;
153 this->user->Shrink("sasl_authenticator");
154 this->AnnounceState();
158 class CommandAuthenticate : public Command
161 CommandAuthenticate (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator, "AUTHENTICATE", 0, 1, true)
165 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
167 /* Only allow AUTHENTICATE on unregistered clients */
168 if (user->registered != REG_ALL)
170 if (!user->GetExt("sasl"))
173 SaslAuthenticator *sasl;
174 if (!(user->GetExt("sasl_authenticator", sasl)))
175 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, creator);
176 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
183 class CommandSASL : public Command
186 CommandSASL(InspIRCd* Instance, Module* Creator) : Command(Instance, Creator, "SASL", 0, 2)
188 this->disabled = true; // 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;
201 if (!target->GetExt("sasl_authenticator", sasl))
204 SaslState state = sasl->ProcessInboundMessage(parameters);
205 if (state == SASL_DONE)
208 target->Shrink("sasl");
214 class ModuleSASL : public Module
216 CommandAuthenticate auth;
220 ModuleSASL(InspIRCd* Me)
221 : Module(Me), auth(Me, this), sasl(Me, this)
223 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
224 ServerInstance->Modules->Attach(eventlist, this, 5);
226 ServerInstance->AddCommand(&auth);
227 ServerInstance->AddCommand(&sasl);
229 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
230 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!");
233 virtual ModResult OnUserRegister(User *user)
235 SaslAuthenticator *sasl_;
236 if (user->GetExt("sasl_authenticator", sasl_))
240 user->Shrink("sasl_authenticator");
243 return MOD_RES_PASSTHRU;
246 virtual void OnCleanup(int target_type, void *item)
248 if (target_type == TYPE_USER)
249 OnUserDisconnect((User*)item);
252 virtual void OnUserDisconnect(User *user)
254 SaslAuthenticator *sasl_;
255 if (user->GetExt("sasl_authenticator", sasl_))
258 user->Shrink("sasl_authenticator");
262 virtual void OnPostConnect(User* user)
267 std::string* str = NULL;
269 if (user->GetExt("accountname", str))
270 ServerInstance->PI->SendMetaData(user, "accountname", *str);
275 virtual ~ModuleSASL()
279 virtual Version GetVersion()
281 return Version("$Id$",VF_VENDOR,API_VERSION);
284 virtual void OnEvent(Event *ev)
286 GenericCapHandler(ev, "sasl", "sasl");
290 MODULE_INIT(ModuleSASL)