]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
MetaData rework
[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                 parameterlist 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(const 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(const std::vector<std::string> &msg)
67         {
68                 switch (this->state)
69                 {
70                  case SASL_INIT:
71                         this->agent = msg[0];
72                         this->user->Write("AUTHENTICATE %s", msg[3].c_str());
73                         this->state = SASL_COMM;
74                         break;
75                  case SASL_COMM:
76                         if (msg[0] != this->agent)
77                                 return this->state;
78
79                         if (msg[2] != "D")
80                                 this->user->Write("AUTHENTICATE %s", msg[3].c_str());
81                         else
82                         {
83                                 this->state = SASL_DONE;
84                                 this->result = this->GetSaslResult(msg[3]);
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                 parameterlist 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 class CommandSASL : public Command
187 {
188         Module* Creator;
189  public:
190         CommandSASL(InspIRCd* Instance, Module* creator) : Command(Instance, "SASL", 0, 2), Creator(creator)
191         {
192                 this->source = "m_sasl.so";
193                 this->disabled = true; // should not be called by users
194         }
195
196         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
197         {
198                 User* target = ServerInstance->FindNick(parameters[1]);
199                 if (!target)
200                 {
201                         ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", parameters[1].c_str());
202                         return CMD_FAILURE;
203                 }
204
205                 SaslAuthenticator *sasl;
206                 if (!target->GetExt("sasl_authenticator", sasl))
207                         return CMD_FAILURE;
208
209                 SaslState state = sasl->ProcessInboundMessage(parameters);
210                 if (state == SASL_DONE)
211                 {
212                         delete sasl;
213                         target->Shrink("sasl");
214                 }
215                 return CMD_SUCCESS;
216         }
217 };
218
219 class ModuleSASL : public Module
220 {
221         CommandAuthenticate auth;
222         CommandSASL sasl;
223  public:
224
225         ModuleSASL(InspIRCd* Me)
226                 : Module(Me), auth(Me, this), sasl(Me, this)
227         {
228                 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
229                 ServerInstance->Modules->Attach(eventlist, this, 5);
230
231                 ServerInstance->AddCommand(&auth);
232                 ServerInstance->AddCommand(&sasl);
233
234                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
235                         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!");
236         }
237
238         virtual int OnUserRegister(User *user)
239         {
240                 SaslAuthenticator *sasl_;
241                 if (user->GetExt("sasl_authenticator", sasl_))
242                 {
243                         sasl_->Abort();
244                         delete sasl_;
245                         user->Shrink("sasl_authenticator");
246                 }
247
248                 return 0;
249         }
250
251         virtual void OnCleanup(int target_type, void *item)
252         {
253                 if (target_type == TYPE_USER)
254                         OnUserDisconnect((User*)item);
255         }
256
257         virtual void OnUserDisconnect(User *user)
258         {
259                 SaslAuthenticator *sasl_;
260                 if (user->GetExt("sasl_authenticator", sasl_))
261                 {
262                         delete sasl_;
263                         user->Shrink("sasl_authenticator");
264                 }
265         }
266
267         virtual void OnPostConnect(User* user)
268         {
269                 if (!IS_LOCAL(user))
270                         return;
271
272                 std::string* str = NULL;
273
274                 if (user->GetExt("accountname", str))
275                         ServerInstance->PI->SendMetaData(user, "accountname", *str);
276
277                 return;
278         }
279
280         virtual ~ModuleSASL()
281         {
282         }
283
284         virtual Version GetVersion()
285         {
286                 return Version("$Id$",VF_VENDOR,API_VERSION);
287         }
288
289         virtual void OnEvent(Event *ev)
290         {
291                 GenericCapHandler(ev, "sasl", "sasl");
292         }
293 };
294
295 MODULE_INIT(ModuleSASL)