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
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 Event e((char*)¶ms, Creator, "send_encap");
52 e.Send(ServerInstance);
55 SaslResult GetSaslResult(std::string &result_)
66 /* checks for and deals with a state change. */
67 SaslState ProcessInboundMessage(std::deque<std::string> &msg)
73 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
74 this->state = SASL_COMM;
77 if (msg[2] != this->agent)
81 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
84 this->state = SASL_DONE;
85 this->result = this->GetSaslResult(msg[5]);
92 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
101 this->state = SASL_DONE;
102 this->result = SASL_ABORT;
105 bool SendClientMessage(const char* const* parameters, int pcnt)
107 if (this->state != SASL_COMM)
110 std::deque<std::string> params;
111 params.push_back("*");
112 params.push_back("SASL");
113 params.push_back(this->user->uuid);
114 params.push_back(this->agent);
115 params.push_back("C");
117 for (int i = 0; i < pcnt; ++i)
118 params.push_back(parameters[i]);
120 Event e((char*)¶ms, Creator, "send_encap");
121 e.Send(ServerInstance);
123 if (*parameters[0] == '*')
132 void AnnounceState(void)
134 if (this->state_announced)
137 switch (this->result)
140 this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick);
143 this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick);
146 this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick);
152 this->state_announced = true;
157 this->user->Shrink("sasl_authenticator");
158 this->AnnounceState();
162 class CommandAuthenticate : public Command
166 CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
168 this->source = "m_sasl.so";
171 CmdResult Handle (const char* const* parameters, int pcnt, User *user)
173 /* Only allow AUTHENTICATE on unregistered clients */
174 if (user->registered != REG_ALL)
176 if (!user->GetExt("sasl"))
179 SaslAuthenticator *sasl;
180 if (!(user->GetExt("sasl_authenticator", sasl)))
181 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
182 else if (sasl->SendClientMessage(parameters, pcnt) == false) // IAL abort extension --nenolod
190 class ModuleSASL : public Module
192 CommandAuthenticate* sasl;
195 ModuleSASL(InspIRCd* Me)
198 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
199 ServerInstance->Modules->Attach(eventlist, this, 5);
201 sasl = new CommandAuthenticate(ServerInstance, this);
202 ServerInstance->AddCommand(sasl);
204 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
205 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!");
208 virtual int OnUserRegister(User *user)
210 SaslAuthenticator *sasl_;
211 if (user->GetExt("sasl_authenticator", sasl_))
215 user->Shrink("sasl_authenticator");
221 virtual void OnCleanup(int target_type, void *item)
223 if (target_type == TYPE_USER)
224 OnUserDisconnect((User*)item);
227 virtual void OnUserDisconnect(User *user)
229 SaslAuthenticator *sasl_;
230 if (user->GetExt("sasl_authenticator", sasl_))
233 user->Shrink("sasl_authenticator");
237 virtual void OnPostConnect(User* user)
242 std::string* str = NULL;
244 if (user->GetExt("accountname", str))
246 std::deque<std::string> params;
247 params.push_back(user->uuid);
248 params.push_back("accountname");
249 params.push_back(*str);
250 Event e((char*)¶ms, this, "send_metadata");
251 e.Send(ServerInstance);
256 virtual ~ModuleSASL()
260 virtual Version GetVersion()
262 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
265 virtual void OnEvent(Event *ev)
267 GenericCapHandler(ev, "sasl", "sasl");
269 if (ev->GetEventID() == "encap_received")
271 std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
273 if ((*parameters)[1] != "SASL")
276 User* target = ServerInstance->FindNick((*parameters)[3]);
279 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
283 SaslAuthenticator *sasl_;
284 if (!target->GetExt("sasl_authenticator", sasl_))
287 SaslState state = sasl_->ProcessInboundMessage(*parameters);
288 if (state == SASL_DONE)
291 target->Shrink("sasl");
297 MODULE_INIT(ModuleSASL)