]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
259c3bd162002c4c8f6b302b2359633f03c611eb
[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 char* const* parameters, int pcnt)
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                 for (int i = 0; i < pcnt; ++i)
117                         params.push_back(parameters[i]);                
118
119                 ServerInstance->PI->SendEncapsulatedData(params);
120
121                 if (*parameters[0] == '*')
122                 {
123                         this->Abort();
124                         return false;
125                 }
126
127                 return true;
128         }
129
130         void AnnounceState(void)
131         {
132                 if (this->state_announced)
133                         return;
134
135                 switch (this->result)
136                 {
137                  case SASL_OK:
138                         this->user->WriteNumeric(903, "%s :SASL authentication successful", this->user->nick);
139                         break;
140                  case SASL_ABORT:
141                         this->user->WriteNumeric(906, "%s :SASL authentication aborted", this->user->nick);
142                         break;
143                  case SASL_FAIL:
144                         this->user->WriteNumeric(904, "%s :SASL authentication failed", this->user->nick);
145                         break;
146                  default:
147                         break;
148                 }
149
150                 this->state_announced = true;
151         }
152
153         ~SaslAuthenticator()
154         {
155                 this->user->Shrink("sasl_authenticator");
156                 this->AnnounceState();
157         }
158 };
159
160 class CommandAuthenticate : public Command
161 {
162         Module* Creator;
163  public:
164         CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
165         {
166                 this->source = "m_sasl.so";
167         }
168
169         CmdResult Handle (const char* const* parameters, int pcnt, User *user)
170         {
171                 /* Only allow AUTHENTICATE on unregistered clients */
172                 if (user->registered != REG_ALL)
173                 {
174                         if (!user->GetExt("sasl"))
175                                 return CMD_FAILURE;
176
177                         SaslAuthenticator *sasl;
178                         if (!(user->GetExt("sasl_authenticator", sasl)))
179                                 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
180                         else if (sasl->SendClientMessage(parameters, pcnt) == false)    // IAL abort extension --nenolod
181                                 delete sasl;
182                 }
183                 return CMD_FAILURE;
184         }
185 };
186
187
188 class ModuleSASL : public Module
189 {
190         CommandAuthenticate* sasl;
191  public:
192         
193         ModuleSASL(InspIRCd* Me)
194                 : Module(Me)
195         {
196                 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect, I_OnUserDisconnect, I_OnCleanup };
197                 ServerInstance->Modules->Attach(eventlist, this, 5);
198
199                 sasl = new CommandAuthenticate(ServerInstance, this);
200                 ServerInstance->AddCommand(sasl);
201
202                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
203                         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!");
204         }
205
206         virtual int OnUserRegister(User *user)
207         {
208                 SaslAuthenticator *sasl_;
209                 if (user->GetExt("sasl_authenticator", sasl_))
210                 {
211                         sasl_->Abort();
212                         delete sasl_;
213                         user->Shrink("sasl_authenticator");
214                 }
215
216                 return 0;
217         }
218
219         virtual void OnCleanup(int target_type, void *item)
220         {
221                 if (target_type == TYPE_USER)
222                         OnUserDisconnect((User*)item);
223         }
224
225         virtual void OnUserDisconnect(User *user)
226         {
227                 SaslAuthenticator *sasl_;
228                 if (user->GetExt("sasl_authenticator", sasl_))
229                 {
230                         delete sasl_;
231                         user->Shrink("sasl_authenticator");
232                 }
233         }
234
235         virtual void OnPostConnect(User* user)
236         {
237                 if (!IS_LOCAL(user))
238                         return;
239
240                 std::string* str = NULL;
241
242                 if (user->GetExt("accountname", str))
243                         ServerInstance->PI->SendMetaData(user, TYPE_USER, "accountname", *str);
244
245                 return;
246         }
247
248         virtual ~ModuleSASL()
249         {
250         }
251
252         virtual Version GetVersion()
253         {
254                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
255         }
256
257         virtual void OnEvent(Event *ev)
258         {
259                 GenericCapHandler(ev, "sasl", "sasl");
260
261                 if (ev->GetEventID() == "encap_received")
262                 {
263                         std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
264
265                         if ((*parameters)[1] != "SASL")
266                                 return;
267
268                         User* target = ServerInstance->FindNick((*parameters)[3]);
269                         if (!target)
270                         {
271                                 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[3].c_str());
272                                 return;
273                         }
274
275                         SaslAuthenticator *sasl_;
276                         if (!target->GetExt("sasl_authenticator", sasl_))
277                                 return;
278
279                         SaslState state = sasl_->ProcessInboundMessage(*parameters);
280                         if (state == SASL_DONE)
281                         {
282                                 delete sasl_;
283                                 target->Shrink("sasl");
284                         }
285                 }
286         }
287 };
288
289 MODULE_INIT(ModuleSASL)