]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
7867c245d3520329943a4cf2b3ac84f1db1a0201
[user/henk/code/inspircd.git] / src / modules / m_sasl.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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
18 /* $ModDesc: Provides support for IRC Authentication Layer (aka: atheme SASL) via AUTHENTICATE. */
19
20 enum SaslState { SASL_INIT, SASL_COMM, SASL_DONE };
21 enum SaslResult { SASL_OK, SASL_FAIL, SASL_ABORT };
22
23 /**
24  * Tracks SASL authentication state like charybdis does. --nenolod
25  */
26 class SaslAuthenticator : public classbase
27 {
28  private:
29         InspIRCd *ServerInstance;
30         Module *Creator;
31         std::string agent;
32         User *user;
33         SaslState state;
34         SaslResult result;
35         bool state_announced;
36
37  public:
38         SaslAuthenticator(User *user_, std::string method, InspIRCd *instance, Module *ctor)
39                 : ServerInstance(instance), Creator(ctor), user(user_), state(SASL_INIT), state_announced(false)
40         {
41                 this->user->Extend("sasl_authenticator", this);
42
43                 std::deque<std::string> params;
44                 params.push_back("*");
45                 params.push_back("SASL");
46                 params.push_back(user->uuid);
47                 params.push_back("*");
48                 params.push_back("S");
49                 params.push_back(method);
50
51                 ServerInstance->PI->SendEncapsulatedData(params);
52         }
53
54         SaslResult GetSaslResult(std::string &result_)
55         {
56                 if (result_ == "F")
57                         return SASL_FAIL;
58
59                 if (result_ == "A")
60                         return SASL_ABORT;
61
62                 return SASL_OK;
63         }
64
65         /* checks for and deals with a state change. */
66         SaslState ProcessInboundMessage(std::deque<std::string> &msg)
67         {
68                 switch (this->state)
69                 {
70                  case SASL_INIT:
71                         this->agent = msg[2];
72                         this->user->Write("AUTHENTICATE %s", msg[5].c_str());
73                         this->state = SASL_COMM;
74                         break;
75                  case SASL_COMM:
76                         if (msg[2] != this->agent)
77                                 return this->state;
78
79                         if (msg[4] != "D")
80                                 this->user->Write("AUTHENTICATE %s", msg[5].c_str());
81                         else
82                         {
83                                 this->state = SASL_DONE;
84                                 this->result = this->GetSaslResult(msg[5]);
85                         }
86
87                         break;
88                  case SASL_DONE:
89                         break;
90                  default:
91                         ServerInstance->Logs->Log("m_sasl", DEFAULT, "WTF: SaslState is not a known state (%d)", this->state);
92                         break;
93                 }
94
95                 return this->state;
96         }
97
98         void Abort(void)
99         {
100                 this->state = SASL_DONE;
101                 this->result = SASL_ABORT;
102         }
103
104         bool SendClientMessage(const std::vector<std::string>& parameters)
105         {
106                 if (this->state != SASL_COMM)
107                         return true;
108
109                 std::deque<std::string> params;
110                 params.push_back("*");
111                 params.push_back("SASL");
112                 params.push_back(this->user->uuid);
113                 params.push_back(this->agent);
114                 params.push_back("C");
115
116                 params.insert(params.end(), parameters.begin(), parameters.end());
117
118                 ServerInstance->PI->SendEncapsulatedData(params);
119
120                 if (parameters[0][0] == '*')
121                 {
122                         this->Abort();
123                         return false;
124                 }
125
126                 return true;
127         }
128
129         void AnnounceState(void)
130         {
131                 if (this->state_announced)
132                         return;
133
134                 switch (this->result)
135                 {
136                  case SASL_OK:
137                         this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick.c_str());
138                         break;
139                  case SASL_ABORT:
140                         this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick.c_str());
141                         break;
142                  case SASL_FAIL:
143                         this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick.c_str());
144                         break;
145                  default:
146                         break;
147                 }
148
149                 this->state_announced = true;
150         }
151
152         ~SaslAuthenticator()
153         {
154                 this->user->Shrink("sasl_authenticator");
155                 this->AnnounceState();
156         }
157 };
158
159 class CommandAuthenticate : public Command
160 {
161         Module* Creator;
162  public:
163         CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
164         {
165                 this->source = "m_sasl.so";
166         }
167
168         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
169         {
170                 /* Only allow AUTHENTICATE on unregistered clients */
171                 if (user->registered != REG_ALL)
172                 {
173                         if (!user->GetExt("sasl"))
174                                 return CMD_FAILURE;
175
176                         SaslAuthenticator *sasl;
177                         if (!(user->GetExt("sasl_authenticator", sasl)))
178                                 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
179                         else if (sasl->SendClientMessage(parameters) == false)  // IAL abort extension --nenolod
180                                 delete sasl;
181                 }
182                 return CMD_FAILURE;
183         }
184 };
185
186
187 class ModuleSASL : public Module
188 {
189         CommandAuthenticate sasl;
190  public:
191
192         ModuleSASL(InspIRCd* Me)
193                 : Module(Me), sasl(Me, this)
194         {
195                 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
196                 ServerInstance->Modules->Attach(eventlist, this, 5);
197
198                 ServerInstance->AddCommand(&sasl);
199
200                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
201                         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!");
202         }
203
204         virtual int OnUserRegister(User *user)
205         {
206                 SaslAuthenticator *sasl_;
207                 if (user->GetExt("sasl_authenticator", sasl_))
208                 {
209                         sasl_->Abort();
210                         delete sasl_;
211                         user->Shrink("sasl_authenticator");
212                 }
213
214                 return 0;
215         }
216
217         virtual void OnCleanup(int target_type, void *item)
218         {
219                 if (target_type == TYPE_USER)
220                         OnUserDisconnect((User*)item);
221         }
222
223         virtual void OnUserDisconnect(User *user)
224         {
225                 SaslAuthenticator *sasl_;
226                 if (user->GetExt("sasl_authenticator", sasl_))
227                 {
228                         delete sasl_;
229                         user->Shrink("sasl_authenticator");
230                 }
231         }
232
233         virtual void OnPostConnect(User* user)
234         {
235                 if (!IS_LOCAL(user))
236                         return;
237
238                 std::string* str = NULL;
239
240                 if (user->GetExt("accountname", str))
241                         ServerInstance->PI->SendMetaData(user, TYPE_USER, "accountname", *str);
242
243                 return;
244         }
245
246         virtual ~ModuleSASL()
247         {
248         }
249
250         virtual Version GetVersion()
251         {
252                 return Version("$Id$",VF_VENDOR,API_VERSION);
253         }
254
255         virtual void OnEvent(Event *ev)
256         {
257                 GenericCapHandler(ev, "sasl", "sasl");
258
259                 if (ev->GetEventID() == "encap_received")
260                 {
261                         std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
262
263                         if ((*parameters)[1] != "SASL")
264                                 return;
265
266                         User* target = ServerInstance->FindNick((*parameters)[3]);
267                         if (!target)
268                         {
269                                 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
270                                 return;
271                         }
272
273                         SaslAuthenticator *sasl_;
274                         if (!target->GetExt("sasl_authenticator", sasl_))
275                                 return;
276
277                         SaslState state = sasl_->ProcessInboundMessage(*parameters);
278                         if (state == SASL_DONE)
279                         {
280                                 delete sasl_;
281                                 target->Shrink("sasl");
282                         }
283                 }
284         }
285 };
286
287 MODULE_INIT(ModuleSASL)