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