]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
m_sasl Create SASLCap which subclasses Cap::Capability and implements OnRequest()
[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
27 class SASLCap : public Cap::Capability
28 {
29         bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
30         {
31                 // Requesting this cap is allowed anytime
32                 if (adding)
33                         return true;
34
35                 // But removing it can only be done when unregistered
36                 return (user->registered != REG_ALL);
37         }
38
39  public:
40         SASLCap(Module* mod)
41                 : Cap::Capability(mod, "sasl")
42         {
43         }
44 };
45
46 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
47 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
48
49 static std::string sasl_target = "*";
50 static Events::ModuleEventProvider* saslevprov;
51
52 static void SendSASL(const parameterlist& params)
53 {
54         if (!ServerInstance->PI->SendEncapsulatedData(sasl_target, "SASL", params))
55         {
56                 FOREACH_MOD_CUSTOM(*saslevprov, SASLEventListener, OnSASLAuth, (params));
57         }
58 }
59
60 /**
61  * Tracks SASL authentication state like charybdis does. --nenolod
62  */
63 class SaslAuthenticator
64 {
65  private:
66         std::string agent;
67         User *user;
68         SaslState state;
69         SaslResult result;
70         bool state_announced;
71
72  public:
73         SaslAuthenticator(User* user_, const std::string& method)
74                 : user(user_), state(SASL_INIT), state_announced(false)
75         {
76                 parameterlist params;
77                 params.push_back(user->uuid);
78                 params.push_back("*");
79                 params.push_back("S");
80                 params.push_back(method);
81
82                 LocalUser* localuser = IS_LOCAL(user);
83                 if (method == "EXTERNAL" && localuser)
84                 {
85                         std::string fp = SSLClientCert::GetFingerprint(&localuser->eh);
86
87                         if (fp.size())
88                                 params.push_back(fp);
89                 }
90
91                 SendSASL(params);
92         }
93
94         SaslResult GetSaslResult(const std::string &result_)
95         {
96                 if (result_ == "F")
97                         return SASL_FAIL;
98
99                 if (result_ == "A")
100                         return SASL_ABORT;
101
102                 return SASL_OK;
103         }
104
105         /* checks for and deals with a state change. */
106         SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
107         {
108                 switch (this->state)
109                 {
110                  case SASL_INIT:
111                         this->agent = msg[0];
112                         this->state = SASL_COMM;
113                         /* fall through */
114                  case SASL_COMM:
115                         if (msg[0] != this->agent)
116                                 return this->state;
117
118                         if (msg.size() < 4)
119                                 return this->state;
120
121                         if (msg[2] == "C")
122                                 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
123                         else if (msg[2] == "D")
124                         {
125                                 this->state = SASL_DONE;
126                                 this->result = this->GetSaslResult(msg[3]);
127                         }
128                         else if (msg[2] == "M")
129                                 this->user->WriteNumeric(908, "%s %s :are available SASL mechanisms", this->user->nick.c_str(), msg[3].c_str());
130                         else
131                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
132
133                         break;
134                  case SASL_DONE:
135                         break;
136                  default:
137                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
138                         break;
139                 }
140
141                 return this->state;
142         }
143
144         void Abort(void)
145         {
146                 this->state = SASL_DONE;
147                 this->result = SASL_ABORT;
148         }
149
150         bool SendClientMessage(const std::vector<std::string>& parameters)
151         {
152                 if (this->state != SASL_COMM)
153                         return true;
154
155                 parameterlist params;
156                 params.push_back(this->user->uuid);
157                 params.push_back(this->agent);
158                 params.push_back("C");
159
160                 params.insert(params.end(), parameters.begin(), parameters.end());
161
162                 SendSASL(params);
163
164                 if (parameters[0][0] == '*')
165                 {
166                         this->Abort();
167                         return false;
168                 }
169
170                 return true;
171         }
172
173         void AnnounceState(void)
174         {
175                 if (this->state_announced)
176                         return;
177
178                 switch (this->result)
179                 {
180                  case SASL_OK:
181                         this->user->WriteNumeric(903, ":SASL authentication successful");
182                         break;
183                  case SASL_ABORT:
184                         this->user->WriteNumeric(906, ":SASL authentication aborted");
185                         break;
186                  case SASL_FAIL:
187                         this->user->WriteNumeric(904, ":SASL authentication failed");
188                         break;
189                  default:
190                         break;
191                 }
192
193                 this->state_announced = true;
194         }
195 };
196
197 class CommandAuthenticate : public Command
198 {
199  public:
200         SimpleExtItem<SaslAuthenticator>& authExt;
201         Cap::Capability& cap;
202         CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, Cap::Capability& Cap)
203                 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
204         {
205                 works_before_reg = true;
206         }
207
208         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
209         {
210                 /* Only allow AUTHENTICATE on unregistered clients */
211                 if (user->registered != REG_ALL)
212                 {
213                         if (!cap.get(user))
214                                 return CMD_FAILURE;
215
216                         SaslAuthenticator *sasl = authExt.get(user);
217                         if (!sasl)
218                                 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
219                         else if (sasl->SendClientMessage(parameters) == false)  // IAL abort extension --nenolod
220                         {
221                                 sasl->AnnounceState();
222                                 authExt.unset(user);
223                         }
224                 }
225                 return CMD_FAILURE;
226         }
227 };
228
229 class CommandSASL : public Command
230 {
231  public:
232         SimpleExtItem<SaslAuthenticator>& authExt;
233         CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
234         {
235                 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
236         }
237
238         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
239         {
240                 User* target = ServerInstance->FindNick(parameters[1]);
241                 if ((!target) || (IS_SERVER(target)))
242                 {
243                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User not found in sasl ENCAP event: %s", parameters[1].c_str());
244                         return CMD_FAILURE;
245                 }
246
247                 SaslAuthenticator *sasl = authExt.get(target);
248                 if (!sasl)
249                         return CMD_FAILURE;
250
251                 SaslState state = sasl->ProcessInboundMessage(parameters);
252                 if (state == SASL_DONE)
253                 {
254                         sasl->AnnounceState();
255                         authExt.unset(target);
256                 }
257                 return CMD_SUCCESS;
258         }
259
260         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
261         {
262                 return ROUTE_BROADCAST;
263         }
264 };
265
266 class ModuleSASL : public Module
267 {
268         SimpleExtItem<SaslAuthenticator> authExt;
269         SASLCap cap;
270         CommandAuthenticate auth;
271         CommandSASL sasl;
272         Events::ModuleEventProvider sasleventprov;
273
274  public:
275         ModuleSASL()
276                 : authExt("sasl_auth", ExtensionItem::EXT_USER, this)
277                 , cap(this)
278                 , auth(this, authExt, cap)
279                 , sasl(this, authExt)
280                 , sasleventprov(this, "event/sasl")
281         {
282                 saslevprov = &sasleventprov;
283         }
284
285         void init() CXX11_OVERRIDE
286         {
287                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
288                         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!");
289         }
290
291         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
292         {
293                 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
294         }
295
296         ModResult OnUserRegister(LocalUser *user) CXX11_OVERRIDE
297         {
298                 SaslAuthenticator *sasl_ = authExt.get(user);
299                 if (sasl_)
300                 {
301                         sasl_->Abort();
302                         authExt.unset(user);
303                 }
304
305                 return MOD_RES_PASSTHRU;
306         }
307
308         Version GetVersion() CXX11_OVERRIDE
309         {
310                 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
311         }
312 };
313
314 MODULE_INIT(ModuleSASL)