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 char* const* parameters, int pcnt)
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 for (int i = 0; i < pcnt; ++i)
117 params.push_back(parameters[i]);
119 ServerInstance->PI->SendEncapsulatedData(params);
121 if (*parameters[0] == '*')
130 void AnnounceState(void)
132 if (this->state_announced)
135 switch (this->result)
138 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick);
141 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick);
144 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick);
150 this->state_announced = true;
155 this->user->Shrink("sasl_authenticator");
156 this->AnnounceState();
160 class CommandAuthenticate : public Command
164 CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
166 this->source = "m_sasl.so";
169 CmdResult Handle (const char* const* parameters, int pcnt, User *user)
171 /* Only allow AUTHENTICATE on unregistered clients */
172 if (user->registered != REG_ALL)
174 if (!user->GetExt("sasl"))
177 SaslAuthenticator *sasl;
178 if (!(user->GetExt("sasl_authenticator", sasl)))
179 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
180 else if (sasl->SendClientMessage(parameters, pcnt) == false) // IAL abort extension --nenolod
188 class ModuleSASL : public Module
190 CommandAuthenticate* sasl;
193 ModuleSASL(InspIRCd* Me)
196 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
197 ServerInstance->Modules->Attach(eventlist, this, 5);
199 sasl = new CommandAuthenticate(ServerInstance, this);
200 ServerInstance->AddCommand(sasl);
202 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
203 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!");
206 virtual int OnUserRegister(User *user)
208 SaslAuthenticator *sasl_;
209 if (user->GetExt("sasl_authenticator", sasl_))
213 user->Shrink("sasl_authenticator");
219 virtual void OnCleanup(int target_type, void *item)
221 if (target_type == TYPE_USER)
222 OnUserDisconnect((User*)item);
225 virtual void OnUserDisconnect(User *user)
227 SaslAuthenticator *sasl_;
228 if (user->GetExt("sasl_authenticator", sasl_))
231 user->Shrink("sasl_authenticator");
235 virtual void OnPostConnect(User* user)
240 std::string* str = NULL;
242 if (user->GetExt("accountname", str))
243 ServerInstance->PI->SendMetaData(user, TYPE_USER, "accountname", *str);
248 virtual ~ModuleSASL()
252 virtual Version GetVersion()
254 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
257 virtual void OnEvent(Event *ev)
259 GenericCapHandler(ev, "sasl", "sasl");
261 if (ev->GetEventID() == "encap_received")
263 std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
265 if ((*parameters)[1] != "SASL")
268 User* target = ServerInstance->FindNick((*parameters)[3]);
271 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
275 SaslAuthenticator *sasl_;
276 if (!target->GetExt("sasl_authenticator", sasl_))
279 SaslState state = sasl_->ProcessInboundMessage(*parameters);
280 if (state == SASL_DONE)
283 target->Shrink("sasl");
289 MODULE_INIT(ModuleSASL)