]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
Dynamically determine the size of the eventlist[] passed to Attach()
[user/henk/code/inspircd.git] / src / modules / m_sasl.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "m_cap.h"
23 #include "account.h"
24 #include "sasl.h"
25
26 /* $ModDesc: Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE. */
27
28 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
29 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
30
31 static std::string sasl_target = "*";
32
33 static void SendSASL(const parameterlist& params)
34 {
35         if (!ServerInstance->PI->SendEncapsulatedData(params))
36         {
37                 SASLFallback(NULL, params);
38         }
39 }
40
41 /**
42  * Tracks SASL authentication state like charybdis does. --nenolod
43  */
44 class SaslAuthenticator
45 {
46  private:
47         std::string agent;
48         User *user;
49         SaslState state;
50         SaslResult result;
51         bool state_announced;
52
53  public:
54         SaslAuthenticator(User *user_, std::string method, Module *ctor)
55                 : user(user_), state(SASL_INIT), state_announced(false)
56         {
57                 parameterlist params;
58                 params.push_back(sasl_target);
59                 params.push_back("SASL");
60                 params.push_back(user->uuid);
61                 params.push_back("*");
62                 params.push_back("S");
63                 params.push_back(method);
64
65                 SendSASL(params);
66         }
67
68         SaslResult GetSaslResult(const std::string &result_)
69         {
70                 if (result_ == "F")
71                         return SASL_FAIL;
72
73                 if (result_ == "A")
74                         return SASL_ABORT;
75
76                 return SASL_OK;
77         }
78
79         /* checks for and deals with a state change. */
80         SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
81         {
82                 switch (this->state)
83                 {
84                  case SASL_INIT:
85                         this->agent = msg[0];
86                         this->user->Write("AUTHENTICATE %s", msg[3].c_str());
87                         this->state = SASL_COMM;
88                         break;
89                  case SASL_COMM:
90                         if (msg[0] != this->agent)
91                                 return this->state;
92
93                         if (msg[2] != "D")
94                                 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
95                         else
96                         {
97                                 this->state = SASL_DONE;
98                                 this->result = this->GetSaslResult(msg[3]);
99                         }
100
101                         break;
102                  case SASL_DONE:
103                         break;
104                  default:
105                         ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
106                         break;
107                 }
108
109                 return this->state;
110         }
111
112         void Abort(void)
113         {
114                 this->state = SASL_DONE;
115                 this->result = SASL_ABORT;
116         }
117
118         bool SendClientMessage(const std::vector<std::string>& parameters)
119         {
120                 if (this->state != SASL_COMM)
121                         return true;
122
123                 parameterlist params;
124                 params.push_back(sasl_target);
125                 params.push_back("SASL");
126                 params.push_back(this->user->uuid);
127                 params.push_back(this->agent);
128                 params.push_back("C");
129
130                 params.insert(params.end(), parameters.begin(), parameters.end());
131
132                 SendSASL(params);
133
134                 if (parameters[0][0] == '*')
135                 {
136                         this->Abort();
137                         return false;
138                 }
139
140                 return true;
141         }
142
143         void AnnounceState(void)
144         {
145                 if (this->state_announced)
146                         return;
147
148                 switch (this->result)
149                 {
150                  case SASL_OK:
151                         this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
152                         break;
153                  case SASL_ABORT:
154                         this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
155                         break;
156                  case SASL_FAIL:
157                         this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
158                         break;
159                  default:
160                         break;
161                 }
162
163                 this->state_announced = true;
164         }
165 };
166
167 class CommandAuthenticate : public Command
168 {
169  public:
170         SimpleExtItem<SaslAuthenticator>& authExt;
171         GenericCap& cap;
172         CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
173                 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
174         {
175                 works_before_reg = true;
176         }
177
178         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
179         {
180                 /* Only allow AUTHENTICATE on unregistered clients */
181                 if (user->registered != REG_ALL)
182                 {
183                         if (!cap.ext.get(user))
184                                 return CMD_FAILURE;
185
186                         SaslAuthenticator *sasl = authExt.get(user);
187                         if (!sasl)
188                                 authExt.set(user, new SaslAuthenticator(user, parameters[0], creator));
189                         else if (sasl->SendClientMessage(parameters) == false)  // IAL abort extension --nenolod
190                         {
191                                 sasl->AnnounceState();
192                                 authExt.unset(user);
193                         }
194                 }
195                 return CMD_FAILURE;
196         }
197 };
198
199 class CommandSASL : public Command
200 {
201  public:
202         SimpleExtItem<SaslAuthenticator>& authExt;
203         CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
204         {
205                 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
206         }
207
208         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
209         {
210                 User* target = ServerInstance->FindNick(parameters[1]);
211                 if (!target)
212                 {
213                         ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
214                         return CMD_FAILURE;
215                 }
216
217                 SaslAuthenticator *sasl = authExt.get(target);
218                 if (!sasl)
219                         return CMD_FAILURE;
220
221                 SaslState state = sasl->ProcessInboundMessage(parameters);
222                 if (state == SASL_DONE)
223                 {
224                         sasl->AnnounceState();
225                         authExt.unset(target);
226                 }
227                 return CMD_SUCCESS;
228         }
229
230         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
231         {
232                 return ROUTE_BROADCAST;
233         }
234 };
235
236 class ModuleSASL : public Module
237 {
238         SimpleExtItem<SaslAuthenticator> authExt;
239         GenericCap cap;
240         CommandAuthenticate auth;
241         CommandSASL sasl;
242  public:
243         ModuleSASL()
244                 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
245         {
246         }
247
248         void init()
249         {
250                 OnRehash(NULL);
251                 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnRehash };
252                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
253
254                 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
255                 ServerInstance->Modules->AddServices(providelist, 3);
256
257                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
258                         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!");
259         }
260
261         void OnRehash(User*)
262         {
263                 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
264         }
265
266         ModResult OnUserRegister(LocalUser *user)
267         {
268                 SaslAuthenticator *sasl_ = authExt.get(user);
269                 if (sasl_)
270                 {
271                         sasl_->Abort();
272                         authExt.unset(user);
273                 }
274
275                 return MOD_RES_PASSTHRU;
276         }
277
278         Version GetVersion()
279         {
280                 return Version("Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE.",VF_VENDOR);
281         }
282
283         void OnEvent(Event &ev)
284         {
285                 cap.HandleEvent(ev);
286         }
287 };
288
289 MODULE_INIT(ModuleSASL)