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