]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
Include connection security with the SASL host information.
[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 "m_cap.h"
23 #include "account.h"
24 #include "sasl.h"
25 #include "ssl.h"
26
27 /* $ModDesc: Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE. */
28
29 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
30 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
31
32 static std::string sasl_target = "*";
33
34 static void SendSASL(const parameterlist& params)
35 {
36         if (!ServerInstance->PI->SendEncapsulatedData(params))
37         {
38                 SASLFallback(NULL, params);
39         }
40 }
41
42 /**
43  * Tracks SASL authentication state like charybdis does. --nenolod
44  */
45 class SaslAuthenticator
46 {
47  private:
48         std::string agent;
49         User *user;
50         SaslState state;
51         SaslResult result;
52         bool state_announced;
53
54         /* taken from m_services_account */
55         static bool ReadCGIIRCExt(const char* extname, User* user, std::string& out)
56         {
57                 ExtensionItem* wiext = ServerInstance->Extensions.GetItem(extname);
58                 if (!wiext)
59                         return false;
60
61                 if (wiext->creator->ModuleSourceFile != "m_cgiirc.so")
62                         return false;
63
64                 StringExtItem* stringext = static_cast<StringExtItem*>(wiext);
65                 std::string* addr = stringext->get(user);
66                 if (!addr)
67                         return false;
68
69                 out = *addr;
70                 return true;
71         }
72
73
74         void SendHostIP()
75         {
76                 std::string host, ip;
77
78                 if (!ReadCGIIRCExt("cgiirc_webirc_hostname", user, host))
79                 {
80                         host = user->host;
81                 }
82                 if (!ReadCGIIRCExt("cgiirc_webirc_ip", user, ip))
83                 {
84                         ip = user->GetIPString();
85                 }
86                 else
87                 {
88                         /* IP addresses starting with a : on irc are a Bad Thing (tm) */
89                         if (ip.c_str()[0] == ':')
90                                 ip.insert(ip.begin(),1,'0');
91                 }
92
93                 parameterlist params;
94                 params.push_back(sasl_target);
95                 params.push_back("SASL");
96                 params.push_back(user->uuid);
97                 params.push_back("*");
98                 params.push_back("H");
99                 params.push_back(host);
100                 params.push_back(ip);
101
102                 LocalUser* lu = IS_LOCAL(user);
103                 if (lu)
104                 {
105                         // NOTE: SaslAuthenticator instances are only created for local
106                         // users so this parameter will always be appended.
107                         SocketCertificateRequest req(&lu->eh, ServerInstance->Modules->Find("m_sasl.so"));
108                         params.push_back(req.cert ? "S" : "P");
109                 }
110
111                 SendSASL(params);
112         }
113
114  public:
115         SaslAuthenticator(User* user_, const std::string& method)
116                 : user(user_), state(SASL_INIT), state_announced(false)
117         {
118                 SendHostIP();
119
120                 parameterlist params;
121                 params.push_back(sasl_target);
122                 params.push_back("SASL");
123                 params.push_back(user->uuid);
124                 params.push_back("*");
125                 params.push_back("S");
126                 params.push_back(method);
127
128                 if (method == "EXTERNAL" && IS_LOCAL(user_))
129                 {
130                         SocketCertificateRequest req(&((LocalUser*)user_)->eh, ServerInstance->Modules->Find("m_sasl.so"));
131                         std::string fp = req.GetFingerprint();
132
133                         if (fp.size())
134                                 params.push_back(fp);
135                 }
136
137                 SendSASL(params);
138         }
139
140         SaslResult GetSaslResult(const std::string &result_)
141         {
142                 if (result_ == "F")
143                         return SASL_FAIL;
144
145                 if (result_ == "A")
146                         return SASL_ABORT;
147
148                 return SASL_OK;
149         }
150
151         /* checks for and deals with a state change. */
152         SaslState ProcessInboundMessage(const std::vector<std::string> &msg)
153         {
154                 switch (this->state)
155                 {
156                  case SASL_INIT:
157                         this->agent = msg[0];
158                         this->state = SASL_COMM;
159                         /* fall through */
160                  case SASL_COMM:
161                         if (msg[0] != this->agent)
162                                 return this->state;
163
164                         if (msg.size() < 4)
165                                 return this->state;
166
167                         if (msg[2] == "C")
168                                 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
169                         else if (msg[2] == "D")
170                         {
171                                 this->state = SASL_DONE;
172                                 this->result = this->GetSaslResult(msg[3]);
173                         }
174                         else if (msg[2] == "M")
175                                 this->user->WriteNumeric(908, "%s %s :are available SASL mechanisms", this->user->nick.c_str(), msg[3].c_str());
176                         else
177                                 ServerInstance->Logs->Log("m_sasl", DEFAULT, "Services sent an unknown SASL message \"%s\" \"%s\"", msg[2].c_str(), msg[3].c_str());
178
179                         break;
180                  case SASL_DONE:
181                         break;
182                  default:
183                         ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
184                         break;
185                 }
186
187                 return this->state;
188         }
189
190         void Abort(void)
191         {
192                 this->state = SASL_DONE;
193                 this->result = SASL_ABORT;
194         }
195
196         bool SendClientMessage(const std::vector<std::string>& parameters)
197         {
198                 if (this->state != SASL_COMM)
199                         return true;
200
201                 parameterlist params;
202                 params.push_back(sasl_target);
203                 params.push_back("SASL");
204                 params.push_back(this->user->uuid);
205                 params.push_back(this->agent);
206                 params.push_back("C");
207
208                 params.insert(params.end(), parameters.begin(), parameters.end());
209
210                 SendSASL(params);
211
212                 if (parameters[0].c_str()[0] == '*')
213                 {
214                         this->Abort();
215                         return false;
216                 }
217
218                 return true;
219         }
220
221         void AnnounceState(void)
222         {
223                 if (this->state_announced)
224                         return;
225
226                 switch (this->result)
227                 {
228                  case SASL_OK:
229                         this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
230                         break;
231                  case SASL_ABORT:
232                         this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
233                         break;
234                  case SASL_FAIL:
235                         this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
236                         break;
237                  default:
238                         break;
239                 }
240
241                 this->state_announced = true;
242         }
243 };
244
245 class CommandAuthenticate : public Command
246 {
247  public:
248         SimpleExtItem<SaslAuthenticator>& authExt;
249         GenericCap& cap;
250         CommandAuthenticate(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext, GenericCap& Cap)
251                 : Command(Creator, "AUTHENTICATE", 1), authExt(ext), cap(Cap)
252         {
253                 works_before_reg = true;
254                 allow_empty_last_param = false;
255         }
256
257         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
258         {
259                 /* Only allow AUTHENTICATE on unregistered clients */
260                 if (user->registered != REG_ALL)
261                 {
262                         if (!cap.ext.get(user))
263                                 return CMD_FAILURE;
264
265                         if (parameters[0].find(' ') != std::string::npos || parameters[0][0] == ':')
266                                 return CMD_FAILURE;
267
268                         SaslAuthenticator *sasl = authExt.get(user);
269                         if (!sasl)
270                                 authExt.set(user, new SaslAuthenticator(user, parameters[0]));
271                         else if (sasl->SendClientMessage(parameters) == false)  // IAL abort extension --nenolod
272                         {
273                                 sasl->AnnounceState();
274                                 authExt.unset(user);
275                         }
276                 }
277                 return CMD_FAILURE;
278         }
279 };
280
281 class CommandSASL : public Command
282 {
283  public:
284         SimpleExtItem<SaslAuthenticator>& authExt;
285         CommandSASL(Module* Creator, SimpleExtItem<SaslAuthenticator>& ext) : Command(Creator, "SASL", 2), authExt(ext)
286         {
287                 this->flags_needed = FLAG_SERVERONLY; // should not be called by users
288         }
289
290         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
291         {
292                 User* target = ServerInstance->FindNick(parameters[1]);
293                 if ((!target) || (IS_SERVER(target)))
294                 {
295                         ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
296                         return CMD_FAILURE;
297                 }
298
299                 SaslAuthenticator *sasl = authExt.get(target);
300                 if (!sasl)
301                         return CMD_FAILURE;
302
303                 SaslState state = sasl->ProcessInboundMessage(parameters);
304                 if (state == SASL_DONE)
305                 {
306                         sasl->AnnounceState();
307                         authExt.unset(target);
308                 }
309                 return CMD_SUCCESS;
310         }
311
312         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
313         {
314                 return ROUTE_BROADCAST;
315         }
316 };
317
318 class ModuleSASL : public Module
319 {
320         SimpleExtItem<SaslAuthenticator> authExt;
321         GenericCap cap;
322         CommandAuthenticate auth;
323         CommandSASL sasl;
324  public:
325         ModuleSASL()
326                 : authExt("sasl_auth", this), cap(this, "sasl"), auth(this, authExt, cap), sasl(this, authExt)
327         {
328         }
329
330         void init()
331         {
332                 OnRehash(NULL);
333                 Implementation eventlist[] = { I_OnEvent, I_OnUserConnect, I_OnRehash };
334                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
335
336                 ServiceProvider* providelist[] = { &auth, &sasl, &authExt };
337                 ServerInstance->Modules->AddServices(providelist, 3);
338
339                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
340                         ServerInstance->Logs->Log("m_sasl", 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!");
341         }
342
343         void OnRehash(User*)
344         {
345                 sasl_target = ServerInstance->Config->ConfValue("sasl")->getString("target", "*");
346         }
347
348         void OnUserConnect(LocalUser *user)
349         {
350                 SaslAuthenticator *sasl_ = authExt.get(user);
351                 if (sasl_)
352                 {
353                         sasl_->Abort();
354                         authExt.unset(user);
355                 }
356         }
357
358         Version GetVersion()
359         {
360                 return Version("Provides support for IRC Authentication Layer (aka: SASL) via AUTHENTICATE.", VF_VENDOR);
361         }
362
363         void OnEvent(Event &ev)
364         {
365                 cap.HandleEvent(ev);
366         }
367 };
368
369 MODULE_INIT(ModuleSASL)