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