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)
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();
93 ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
100 void SendClientMessage(const char* const* parameters, int pcnt)
102 if (this->state != SASL_COMM)
105 std::deque<std::string> params;
106 params.push_back("*");
107 params.push_back("SASL");
108 params.push_back(this->user->uuid);
109 params.push_back(this->agent);
110 params.push_back("C");
112 for (int i = 0; i < pcnt; ++i)
113 params.push_back(parameters[i]);
115 Event e((char*)¶ms, Creator, "send_encap");
116 e.Send(ServerInstance);
119 void AnnounceState(void)
121 if (this->state_announced)
124 switch (this->result)
127 this->user->WriteServ("903 %s :SASL authentication successful", this->user->nick);
130 this->user->WriteServ("906 %s :SASL authentication aborted", this->user->nick);
133 this->user->WriteServ("904 %s :SASL authentication failed", this->user->nick);
142 this->AnnounceState();
146 class CommandAuthenticate : public Command
150 CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
152 this->source = "m_sasl.so";
155 CmdResult Handle (const char* const* parameters, int pcnt, User *user)
157 /* Only allow AUTHENTICATE on unregistered clients */
158 if (user->registered != REG_ALL)
160 if (!user->GetExt("sasl"))
163 SaslAuthenticator *sasl;
164 if (!(user->GetExt("sasl_authenticator", sasl)))
165 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
167 sasl->SendClientMessage(parameters, pcnt);
174 class ModuleSASL : public Module
176 CommandAuthenticate* sasl;
179 ModuleSASL(InspIRCd* Me)
182 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect };
183 ServerInstance->Modules->Attach(eventlist, this, 3);
185 sasl = new CommandAuthenticate(ServerInstance, this);
186 ServerInstance->AddCommand(sasl);
188 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
189 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!");
192 virtual int OnUserRegister(User *user)
194 if (user->GetExt("sasl"))
196 user->WriteServ("906 %s :SASL authentication aborted", user->nick);
197 user->Shrink("sasl");
203 virtual void OnPostConnect(User* user)
208 std::string* str = NULL;
210 if (user->GetExt("accountname", str))
212 std::deque<std::string> params;
213 params.push_back(user->uuid);
214 params.push_back("accountname");
215 params.push_back(*str);
216 Event e((char*)¶ms, this, "send_metadata");
217 e.Send(ServerInstance);
222 virtual ~ModuleSASL()
226 virtual Version GetVersion()
228 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
231 virtual void OnEvent(Event *ev)
233 GenericCapHandler(ev, "sasl", "sasl");
235 if (ev->GetEventID() == "encap_received")
237 std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
239 if ((*parameters)[1] != "SASL")
242 User* target = ServerInstance->FindNick((*parameters)[2]);
245 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[2].c_str());
249 SaslAuthenticator *sasl;
250 if (!target->GetExt("sasl_authenticator", sasl))
253 SaslState state = sasl->ProcessInboundMessage(*parameters);
254 if (state == SASL_DONE)
256 target->Shrink("sasl_authenticator");
263 MODULE_INIT(ModuleSASL)