]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sasl.cpp
Add debug
[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)
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[1];
73                         this->user->WriteServ("AUTHENTICATE %s", msg[4].c_str());
74                         this->state = SASL_COMM;
75                         break;
76                  case SASL_COMM:
77                         if (msg[1] != this->agent)
78                                 return this->state;
79
80                         if (msg[3] != "D")
81                                 this->user->WriteServ("AUTHENTICATE %s", msg[4].c_str());
82                         else
83                         {
84                                 this->state = SASL_DONE;
85                                 this->result = this->GetSaslResult(msg[4]);
86                                 this->AnnounceState();
87                         }
88
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 SendClientMessage(const char* const* parameters, int pcnt)
99         {
100                 if (this->state != SASL_COMM)
101                         return;
102
103                 std::deque<std::string> params;
104                 params.push_back("*");
105                 params.push_back("SASL");
106                 params.push_back(this->user->uuid);
107                 params.push_back(this->agent);
108                 params.push_back("C");
109
110                 for (int i = 0; i < pcnt; ++i)
111                         params.push_back(parameters[i]);                
112
113                 Event e((char*)&params, Creator, "send_encap");
114                 e.Send(ServerInstance);
115         }
116
117         void AnnounceState(void)
118         {
119                 if (this->state_announced)
120                         return;
121
122                 switch (this->result)
123                 {
124                  case SASL_OK:
125                         this->user->WriteServ("903 %s :SASL authentication successful", this->user->nick);
126                         break;
127                  case SASL_ABORT:
128                         this->user->WriteServ("906 %s :SASL authentication aborted", this->user->nick);
129                         break;
130                  case SASL_FAIL:
131                         this->user->WriteServ("904 %s :SASL authentication failed", this->user->nick);
132                         break;
133                  default:
134                         break;
135                 }
136         }
137
138         ~SaslAuthenticator()
139         {
140                 this->AnnounceState();
141         }
142 };
143
144 class CommandAuthenticate : public Command
145 {
146         Module* Creator;
147  public:
148         CommandAuthenticate (InspIRCd* Instance, Module* creator) : Command(Instance,"AUTHENTICATE", 0, 1, true), Creator(creator)
149         {
150                 this->source = "m_sasl.so";
151         }
152
153         CmdResult Handle (const char* const* parameters, int pcnt, User *user)
154         {
155                 /* Only allow AUTHENTICATE on unregistered clients */
156                 if (user->registered != REG_ALL)
157                 {
158                         if (!user->GetExt("sasl"))
159                                 return CMD_FAILURE;
160
161                         SaslAuthenticator *sasl;
162                         if (!(user->GetExt("sasl_authenticator", sasl)))
163                                 sasl = new SaslAuthenticator(user, parameters[0], ServerInstance, Creator);
164                         else
165                                 sasl->SendClientMessage(parameters, pcnt);
166                 }
167                 return CMD_FAILURE;
168         }
169 };
170
171
172 class ModuleSASL : public Module
173 {
174         CommandAuthenticate* sasl;
175  public:
176         
177         ModuleSASL(InspIRCd* Me)
178                 : Module(Me)
179         {
180                 Implementation eventlist[] = { I_OnEvent, I_OnUserRegister, I_OnPostConnect };
181                 ServerInstance->Modules->Attach(eventlist, this, 3);
182
183                 sasl = new CommandAuthenticate(ServerInstance, this);
184                 ServerInstance->AddCommand(sasl);
185
186                 if (!ServerInstance->Modules->Find("m_services_account.so") || !ServerInstance->Modules->Find("m_cap.so"))
187                         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!");
188         }
189
190         virtual int OnUserRegister(User *user)
191         {
192                 if (user->GetExt("sasl"))
193                 {
194                         user->WriteServ("906 %s :SASL authentication aborted", user->nick);
195                         user->Shrink("sasl");
196                 }
197
198                 return 0;
199         }
200
201         virtual void OnPostConnect(User* user)
202         {
203                 if (!IS_LOCAL(user))
204                         return;
205
206                 std::string* str = NULL;
207
208                 if (user->GetExt("accountname", str))
209                 {
210                         std::deque<std::string> params;
211                         params.push_back(user->uuid);
212                         params.push_back("accountname");
213                         params.push_back(*str);
214                         Event e((char*)&params, this, "send_metadata");
215                         e.Send(ServerInstance);
216                 }
217                 return;
218         }
219
220         virtual ~ModuleSASL()
221         {
222         }
223
224         virtual Version GetVersion()
225         {
226                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
227         }
228
229         virtual void OnEvent(Event *ev)
230         {
231                 GenericCapHandler(ev, "sasl", "sasl");
232
233                 if (ev->GetEventID() == "encap_received")
234                 {
235                         std::deque<std::string>* parameters = (std::deque<std::string>*)ev->GetData();
236
237                         if ((*parameters)[1] != "SASL")
238                                 return;
239
240                         User* target = ServerInstance->FindNick((*parameters)[2]);
241                         if (!target)
242                         {
243                                 ServerInstance->Logs->Log("m_sasl", DEBUG,"User not found in sasl ENCAP event: %s", (*parameters)[2].c_str());
244                                 return;
245                         }
246
247                         SaslAuthenticator *sasl;
248                         if (!target->GetExt("sasl_authenticator", sasl))
249                                 return;
250
251                         SaslState state = sasl->ProcessInboundMessage(*parameters);
252                         if (state == SASL_DONE)
253                         {
254                                 target->Shrink("sasl_authenticator");
255                                 delete sasl;
256                         }
257                 }
258         }
259 };
260
261 MODULE_INIT(ModuleSASL)