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