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)
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->WriteServ("AUTHENTICATE %s", msg[4].c_str());
74 this->state = SASL_COMM;
77 if (msg[1] != this->agent)
81 this->user->WriteServ("AUTHENTICATE %s", msg[4].c_str());
84 this->state = SASL_DONE;
85 this->result = this->GetSaslResult(msg[4]);
86 this->AnnounceState();
91 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
98 void SendClientMessage(const char* const* parameters, int pcnt)
100 if (this->state != SASL_COMM)
103 std::deque<std::string> params;
104 params.push_back("*");
105 params.push_back("SASL");
106 params.push_back(this->user->uuid);
107 params.push_back(this->agent);
108 params.push_back("C");
110 for (int i = 0; i < pcnt; ++i)
111 params.push_back(parameters[i]);
113 Event e((char*)¶ms, Creator, "send_encap");
114 e.Send(ServerInstance);
117 void AnnounceState(void)
119 if (this->state_announced)
122 switch (this->result)
125 this->user->WriteServ("903 %s :SASL authentication successful", this->user->nick);
128 this->user->WriteServ("906 %s :SASL authentication aborted", this->user->nick);
131 this->user->WriteServ("904 %s :SASL authentication failed", this->user->nick);
140 this->AnnounceState();
144 class CommandAuthenticate : public Command
148 CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
150 this->source = "m_sasl.so";
153 CmdResult Handle (const char* const* parameters, int pcnt, User *user)
155 /* Only allow AUTHENTICATE on unregistered clients */
156 if (user->registered != REG_ALL)
158 if (!user->GetExt("sasl"))
161 SaslAuthenticator *sasl;
162 if (!(user->GetExt("sasl_authenticator", sasl)))
163 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
165 sasl->SendClientMessage(parameters, pcnt);
172 class ModuleSASL : public Module
174 CommandAuthenticate* sasl;
177 ModuleSASL(InspIRCd* Me)
180 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect };
181 ServerInstance->Modules->Attach(eventlist, this, 3);
183 sasl = new CommandAuthenticate(ServerInstance, this);
184 ServerInstance->AddCommand(sasl);
186 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
187 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!");
190 virtual int OnUserRegister(User *user)
192 if (user->GetExt("sasl"))
194 user->WriteServ("906 %s :SASL authentication aborted", user->nick);
195 user->Shrink("sasl");
201 virtual void OnPostConnect(User* user)
206 std::string* str = NULL;
208 if (user->GetExt("accountname", str))
210 std::deque<std::string> params;
211 params.push_back(user->uuid);
212 params.push_back("accountname");
213 params.push_back(*str);
214 Event e((char*)¶ms, this, "send_metadata");
215 e.Send(ServerInstance);
220 virtual ~ModuleSASL()
224 virtual Version GetVersion()
226 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
229 virtual void OnEvent(Event *ev)
231 GenericCapHandler(ev, "sasl", "sasl");
233 if (ev->GetEventID() == "encap_received")
235 std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
237 if ((*parameters)[1] != "SASL")
240 User* target = ServerInstance->FindNick((*parameters)[2]);
243 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[2].c_str());
247 SaslAuthenticator *sasl;
248 if (!target->GetExt("sasl_authenticator", sasl))
251 SaslState state = sasl->ProcessInboundMessage(*parameters);
252 if (state == SASL_DONE)
254 target->Shrink("sasl_authenticator");
261 MODULE_INIT(ModuleSASL)