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