1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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;
38 SaslAuthenticator(User *user_, std::string method, InspIRCd *instance, Module *ctor)
39 : ServerInstance(instance), Creator(ctor), user(user_), state(SASL_INIT), state_announced(false)
41 this->user->Extend("sasl_authenticator", this);
43 std::deque<std::string> params;
44 params.push_back("*");
45 params.push_back("SASL");
46 params.push_back(user->uuid);
47 params.push_back("*");
48 params.push_back("S");
49 params.push_back(method);
51 ServerInstance->PI->SendEncapsulatedData(params);
54 SaslResult GetSaslResult(std::string &result_)
65 /* checks for and deals with a state change. */
66 SaslState ProcessInboundMessage(std::deque<std::string> &msg)
72 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
73 this->state = SASL_COMM;
76 if (msg[2] != this->agent)
80 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
83 this->state = SASL_DONE;
84 this->result = this->GetSaslResult(msg[5]);
91 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
100 this->state = SASL_DONE;
101 this->result = SASL_ABORT;
104 bool SendClientMessage(const std::vector<std::string>& parameters)
106 if (this->state != SASL_COMM)
109 std::deque<std::string> params;
110 params.push_back("*");
111 params.push_back("SASL");
112 params.push_back(this->user->uuid);
113 params.push_back(this->agent);
114 params.push_back("C");
116 params.insert(params.end(), parameters.begin(), parameters.end());
118 ServerInstance->PI->SendEncapsulatedData(params);
120 if (parameters[0][0] == '*')
129 void AnnounceState(void)
131 if (this->state_announced)
134 switch (this->result)
137 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
140 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
143 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
149 this->state_announced = true;
154 this->user->Shrink("sasl_authenticator");
155 this->AnnounceState();
159 class CommandAuthenticate : public Command
163 CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
165 this->source = "m_sasl.so";
168 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
170 /* Only allow AUTHENTICATE on unregistered clients */
171 if (user->registered != REG_ALL)
173 if (!user->GetExt("sasl"))
176 SaslAuthenticator *sasl;
177 if (!(user->GetExt("sasl_authenticator", sasl)))
178 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
179 else if (sasl->SendClientMessage(parameters) == false) // IAL abort extension --nenolod
187 class ModuleSASL : public Module
189 CommandAuthenticate* sasl;
192 ModuleSASL(InspIRCd* Me)
195 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
196 ServerInstance->Modules->Attach(eventlist, this, 5);
198 sasl = new CommandAuthenticate(ServerInstance, this);
199 ServerInstance->AddCommand(sasl);
201 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
202 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!");
205 virtual int OnUserRegister(User *user)
207 SaslAuthenticator *sasl_;
208 if (user->GetExt("sasl_authenticator", sasl_))
212 user->Shrink("sasl_authenticator");
218 virtual void OnCleanup(int target_type, void *item)
220 if (target_type == TYPE_USER)
221 OnUserDisconnect((User*)item);
224 virtual void OnUserDisconnect(User *user)
226 SaslAuthenticator *sasl_;
227 if (user->GetExt("sasl_authenticator", sasl_))
230 user->Shrink("sasl_authenticator");
234 virtual void OnPostConnect(User* user)
239 std::string* str = NULL;
241 if (user->GetExt("accountname", str))
242 ServerInstance->PI->SendMetaData(user, TYPE_USER, "accountname", *str);
247 virtual ~ModuleSASL()
251 virtual Version GetVersion()
253 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
256 virtual void OnEvent(Event *ev)
258 GenericCapHandler(ev, "sasl", "sasl");
260 if (ev->GetEventID() == "encap_received")
262 std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
264 if ((*parameters)[1] != "SASL")
267 User* target = ServerInstance->FindNick((*parameters)[3]);
270 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
274 SaslAuthenticator *sasl_;
275 if (!target->GetExt("sasl_authenticator", sasl_))
278 SaslState state = sasl_->ProcessInboundMessage(*parameters);
279 if (state == SASL_DONE)
282 target->Shrink("sasl");
288 MODULE_INIT(ModuleSASL)