]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
Merge insp20
[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 "modules/cap.h"
23 #include "modules/account.h"
24 #include "modules/sasl.h"
25 #include "modules/ssl.h"
26 #include "modules/spanningtree.h"
27
28 static std::string sasl_target;
29
30 class ServerTracker : public SpanningTreeEventListener
31 {
32         bool online;
33
34         void Update(const Server* server, bool linked)
35         {
36                 if (sasl_target == "*")
37                         return;
38
39                 if (InspIRCd::Match(server->GetName(), sasl_target))
40                 {
41                         ServerInstance->Logs->Log(MODNAME, LOG_VERBOSE, "SASL target server \"%s\" %s", sasl_target.c_str(), (linked ? "came online" : "went offline"));
42                         online = linked;
43                 }
44         }
45
46         void OnServerLink(const Server* server) CXX11_OVERRIDE
47         {
48                 Update(server, true);
49         }
50
51         void OnServerSplit(const Server* server) CXX11_OVERRIDE
52         {
53                 Update(server, false);
54         }
55
56  public:
57         ServerTracker(Module* mod)
58                 : SpanningTreeEventListener(mod)
59         {
60                 Reset();
61         }
62
63         void Reset()
64         {
65                 if (sasl_target == "*")
66                 {
67                         online = true;
68                         return;
69                 }
70
71                 online = false;
72
73                 ProtocolInterface::ServerList servers;
74                 ServerInstance->PI->GetServerList(servers);
75                 for (ProtocolInterface::ServerList::const_iterator i = servers.begin(); i != servers.end(); ++i)
76                 {
77                         const ProtocolInterface::ServerInfo& server = *i;
78                         if (InspIRCd::Match(server.servername, sasl_target))
79                         {
80                                 online = true;
81                                 break;
82                         }
83                 }
84         }
85
86         bool IsOnline() const { return online; }
87 };
88
89 class SASLCap : public Cap::Capability
90 {
91         std::string mechlist;
92         const ServerTracker& servertracker;
93
94         bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
95         {
96                 // Requesting this cap is allowed anytime
97                 if (adding)
98                         return true;
99
100                 // But removing it can only be done when unregistered
101                 return (user->registered != REG_ALL);
102         }
103
104         bool OnList(LocalUser* user) CXX11_OVERRIDE
105         {
106                 return servertracker.IsOnline();
107         }
108
109         const std::string* GetValue(LocalUser* user) const CXX11_OVERRIDE
110         {
111                 return &mechlist;
112         }
113
114  public:
115         SASLCap(Module* mod, const ServerTracker& tracker)
116                 : Cap::Capability(mod, "sasl")
117                 , servertracker(tracker)
118         {
119         }
120
121         void SetMechlist(const std::string& newmechlist)
122         {
123                 if (mechlist == newmechlist)
124                         return;
125
126                 mechlist = newmechlist;
127                 NotifyValueChange();
128         }
129 };
130
131 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
132 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
133
134 static Events::ModuleEventProvider* saslevprov;
135
136 static void SendSASL(const parameterlist& params)
137 {
138         if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
139         {
140                 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
141         }
142 }
143
144 /**
145  * Tracks SASL authentication state like charybdis does. --nenolod
146  */
147 class SaslAuthenticator
148 {
149  private:
150         std::string agent;
151         User *user;
152         SaslState state;
153         SaslResult result;
154         bool state_announced;
155
156  public:
157         SaslAuthenticator(User* user_, const std::string& method)
158                 : user(user_), state(SASL_INIT), state_announced(false)
159         {
160                 parameterlist params;
161                 params.push_back(user->uuid);
162                 params.push_back("*");
163                 params.push_back("S");
164                 params.push_back(method);
165
166                 LocalUser* localuser = IS_LOCAL(user);
167                 if (method == "EXTERNAL" && localuser)
168                 {
169                         std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
170
171                         if (fp.size())
172                                 params.push_back(fp);
173                 }
174
175                 SendSASL(params);
176         }
177
178         SaslResult GetSaslResult(const std::string &result_)
179         {
180                 if (result_ == "F")
181                         return SASL_FAIL;
182
183                 if (result_ == "A")
184                         return SASL_ABORT;
185
186                 return SASL_OK;
187         }
188
189         /* checks for and deals with a state change. */
190         SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
191         {
192                 switch (this->state)
193                 {
194                  case SASL_INIT:
195                         this->agent = msg[0];
196                         this->state = SASL_COMM;
197                         /* fall through */
198                  case SASL_COMM:
199                         if (msg[0] != this->agent)
200                                 return this->state;
201
202                         if (msg.size() < 4)
203                                 return this->state;
204
205                         if (msg[2] == "C")
206                                 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
207                         else if (msg[2] == "D")
208                         {
209                                 this->state = SASL_DONE;
210                                 this->result = this->GetSaslResult(msg[3]);
211                         }
212                         else if (msg[2] == "M")
213                                 this->user->WriteNumeric(908, msg[3], "are available SASL mechanisms");
214                         else
215                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
216
217                         break;
218                  case SASL_DONE:
219                         break;
220                  default:
221                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
222                         break;
223                 }
224
225                 return this->state;
226         }
227
228         void Abort(void)
229         {
230                 this->state = SASL_DONE;
231                 this->result = SASL_ABORT;
232         }
233
234         bool SendClientMessage(const std::vector<std::string>& parameters)
235         {
236                 if (this->state != SASL_COMM)
237                         return true;
238
239                 parameterlist params;
240                 params.push_back(this->user->uuid);
241                 params.push_back(this->agent);
242                 params.push_back("C");
243
244                 params.insert(params.end(), parameters.begin(), parameters.end());
245
246                 SendSASL(params);
247
248                 if (parameters[0].c_str()[0] == '*')
249                 {
250                         this->Abort();
251                         return false;
252                 }
253
254                 return true;
255         }
256
257         void AnnounceState(void)
258         {
259                 if (this->state_announced)
260                         return;
261
262                 switch (this->result)
263                 {
264                  case SASL_OK:
265                         this->user->WriteNumeric(903, "SASL authentication successful");
266                         break;
267                  case SASL_ABORT:
268                         this->user->WriteNumeric(906, "SASL authentication aborted");
269                         break;
270                  case SASL_FAIL:
271                         this->user->WriteNumeric(904, "SASL authentication failed");
272                         break;
273                  default:
274                         break;
275                 }
276
277                 this->state_announced = true;
278         }
279 };
280
281 class CommandAuthenticate : public Command
282 {
283  public:
284         SimpleExtItem<SaslAuthenticator>& authExt;
285         Cap::Capability& cap;
286         CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
287                 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
288         {
289                 works_before_reg = true;
290         }
291
292         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
293         {
294                 {
295                         if (!cap.get(user))
296                                 return CMD_FAILURE;
297
298                         SaslAuthenticator *sasl = authExt.get(user);
299                         if (!sasl)
300                                 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
301                         else if (sasl->SendClientMessage(parameters) == false)  // IAL abort extension --nenolod
302                         {
303                                 sasl->AnnounceState();
304                                 authExt.unset(user);
305                         }
306                 }
307                 return CMD_FAILURE;
308         }
309 };
310
311 class CommandSASL : public Command
312 {
313  public:
314         SimpleExtItem<SaslAuthenticator>& authExt;
315         CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
316         {
317                 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
318         }
319
320         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
321         {
322                 User* target = ServerInstance->FindUUID(parameters[1]);
323                 if (!target)
324                 {
325                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
326                         return CMD_FAILURE;
327                 }
328
329                 SaslAuthenticator *sasl = authExt.get(target);
330                 if (!sasl)
331                         return CMD_FAILURE;
332
333                 SaslState state = sasl->ProcessInboundMessage(parameters);
334                 if (state == SASL_DONE)
335                 {
336                         sasl->AnnounceState();
337                         authExt.unset(target);
338                 }
339                 return CMD_SUCCESS;
340         }
341
342         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
343         {
344                 return ROUTE_BROADCAST;
345         }
346 };
347
348 class ModuleSASL : public Module
349 {
350         SimpleExtItem<SaslAuthenticator> authExt;
351         ServerTracker servertracker;
352         SASLCap cap;
353         CommandAuthenticate auth;
354         CommandSASL sasl;
355         Events::ModuleEventProvider sasleventprov;
356
357  public:
358         ModuleSASL()
359                 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
360                 , servertracker(this)
361                 , cap(this, servertracker)
362                 , auth(this, authExt, cap)
363                 , sasl(this, authExt)
364                 , sasleventprov(this, "event/sasl")
365         {
366                 saslevprov = &sasleventprov;
367         }
368
369         void init() CXX11_OVERRIDE
370         {
371                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
372                         ServerInstance->Logs->Log(MODNAME, LOG_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!");
373         }
374
375         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
376         {
377                 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
378                 servertracker.Reset();
379         }
380
381         ModResult OnUserRegister(LocalUser *user) CXX11_OVERRIDE
382         {
383                 SaslAuthenticator *sasl_ = authExt.get(user);
384                 if (sasl_)
385                 {
386                         sasl_->Abort();
387                         authExt.unset(user);
388                 }
389
390                 return MOD_RES_PASSTHRU;
391         }
392
393         void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata) CXX11_OVERRIDE
394         {
395                 if ((target == NULL) && (extname == "saslmechlist"))
396                         cap.SetMechlist(extdata);
397         }
398
399         Version GetVersion() CXX11_OVERRIDE
400         {
401                 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
402         }
403 };
404
405 MODULE_INIT(ModuleSASL)