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