]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
1ad51b1641b7e2b40b4215292983f9c6d12ed86c
[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 : 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)
194         {
195                 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
196                 ServerInstance->Modules->Attach(eventlist, this, 5);
197
198                 sasl = new CommandAuthenticate(ServerInstance, this);
199                 ServerInstance->AddCommand(sasl);
200
201                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
202                         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!");
203         }
204
205         virtual int OnUserRegister(User *user)
206         {
207                 SaslAuthenticator *sasl_;
208                 if (user->GetExt("sasl_authenticator", sasl_))
209                 {
210                         sasl_->Abort();
211                         delete sasl_;
212                         user->Shrink("sasl_authenticator");
213                 }
214
215                 return 0;
216         }
217
218         virtual void OnCleanup(int target_type, void *item)
219         {
220                 if (target_type == TYPE_USER)
221                         OnUserDisconnect((User*)item);
222         }
223
224         virtual void OnUserDisconnect(User *user)
225         {
226                 SaslAuthenticator *sasl_;
227                 if (user->GetExt("sasl_authenticator", sasl_))
228                 {
229                         delete sasl_;
230                         user->Shrink("sasl_authenticator");
231                 }
232         }
233
234         virtual void OnPostConnect(User* user)
235         {
236                 if (!IS_LOCAL(user))
237                         return;
238
239                 std::string* str = NULL;
240
241                 if (user->GetExt("accountname", str))
242                         ServerInstance->PI->SendMetaData(user, TYPE_USER, "accountname", *str);
243
244                 return;
245         }
246
247         virtual ~ModuleSASL()
248         {
249         }
250
251         virtual Version GetVersion()
252         {
253                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
254         }
255
256         virtual void OnEvent(Event *ev)
257         {
258                 GenericCapHandler(ev, "sasl", "sasl");
259
260                 if (ev->GetEventID() == "encap_received")
261                 {
262                         std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
263
264                         if ((*parameters)[1] != "SASL")
265                                 return;
266
267                         User* target = ServerInstance->FindNick((*parameters)[3]);
268                         if (!target)
269                         {
270                                 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
271                                 return;
272                         }
273
274                         SaslAuthenticator *sasl_;
275                         if (!target->GetExt("sasl_authenticator", sasl_))
276                                 return;
277
278                         SaslState state = sasl_->ProcessInboundMessage(*parameters);
279                         if (state == SASL_DONE)
280                         {
281                                 delete sasl_;
282                                 target->Shrink("sasl");
283                         }
284                 }
285         }
286 };
287
288 MODULE_INIT(ModuleSASL)