]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.cpp
Break internal classes to interface and implementation
[stg.git] / projects / stargazer / plugins / authorization / ao / ao.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22 $Revision: 1.30 $
23 $Date: 2010/03/04 12:29:06 $
24 $Author: faust $
25 */
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <signal.h>
30
31 #include "ao.h"
32 #include "../../../user.h"
33 #include "../../../eventloop.h"
34
35 class AO_CREATOR
36 {
37 private:
38     AUTH_AO * ao;
39
40 public:
41     AO_CREATOR()
42         : ao(new AUTH_AO())
43         {
44         };
45     ~AO_CREATOR()
46         {
47         delete ao;
48         };
49
50     AUTH_AO * GetPlugin()
51         {
52         return ao;
53         };
54 };
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 AO_CREATOR aoc;
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
63 template <typename varType>
64 class IS_CONTAINS_USER: public binary_function<varType, user_iter, bool>
65 {
66 public:
67     bool operator()(varType notifier, user_iter user) const
68         {
69         return notifier.GetUser() == user;
70         };
71 };
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 BASE_PLUGIN * GetPlugin()
76 {
77 return aoc.GetPlugin();
78 }
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 const string AUTH_AO::GetVersion() const
83 {
84 return "Always Online authorizator v.1.0";
85 }
86 //-----------------------------------------------------------------------------
87 AUTH_AO::AUTH_AO()
88     : users(NULL),
89       isRunning(false),
90       onAddUserNotifier(*this),
91       onDelUserNotifier(*this)
92 {
93 }
94 //-----------------------------------------------------------------------------
95 void AUTH_AO::SetUsers(USERS * u)
96 {
97 users = u;
98 }
99 //-----------------------------------------------------------------------------
100 void AUTH_AO::SetSettings(const MODULE_SETTINGS & s)
101 {
102 settings = s;
103 }
104 //-----------------------------------------------------------------------------
105 int AUTH_AO::ParseSettings()
106 {
107 return 0;
108 }
109 //-----------------------------------------------------------------------------
110 const string & AUTH_AO::GetStrError() const
111 {
112 return errorStr;
113 }
114 //-----------------------------------------------------------------------------
115 int AUTH_AO::Start()
116 {
117 GetUsers();
118
119 list<user_iter>::iterator users_iter;
120
121 /*onAddUserNotifier.SetAuthorizator(this);
122 onDelUserNotifier.SetAuthorizator(this);*/
123 users->AddNotifierUserAdd(&onAddUserNotifier);
124 users->AddNotifierUserDel(&onDelUserNotifier);
125
126 users_iter = usersList.begin();
127 while (users_iter != usersList.end())
128     {
129     UpdateUserAuthorization(*users_iter);
130     ++users_iter;
131     }
132 isRunning = true;
133 return 0;
134 }
135 //-----------------------------------------------------------------------------
136 int AUTH_AO::Stop()
137 {
138 if (!isRunning)
139     return 0;
140
141 users->DelNotifierUserAdd(&onAddUserNotifier);
142 users->DelNotifierUserDel(&onDelUserNotifier);
143
144 list<user_iter>::iterator users_iter;
145 users_iter = usersList.begin();
146 while (users_iter != usersList.end())
147     {
148     Unauthorize(*users_iter);
149     UnSetUserNotifiers(*users_iter);
150     ++users_iter;
151     }
152 isRunning = false;
153 return 0;
154 }
155 //-----------------------------------------------------------------------------
156 bool AUTH_AO::IsRunning()
157 {
158 return isRunning;
159 }
160 //-----------------------------------------------------------------------------
161 uint16_t AUTH_AO::GetStartPosition() const
162 {
163 return 70;
164 }
165 //-----------------------------------------------------------------------------
166 uint16_t AUTH_AO::GetStopPosition() const
167 {
168 return 70;
169 }
170 //-----------------------------------------------------------------------------
171 void AUTH_AO::SetUserNotifiers(user_iter u)
172 {
173 // ---------- AlwaysOnline -------------------
174 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
175 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier(*this, u);
176
177 /*BeforeChgAONotifier.SetAuthorizator(this);
178 BeforeChgAONotifier.SetUser(u);*/
179 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
180
181 /*AfterChgAONotifier.SetAuthorizator(this);
182 AfterChgAONotifier.SetUser(u);*/
183 AfterChgAONotifierList.push_front(AfterChgAONotifier);
184
185 u->property.alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
186 u->property.alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
187 // ---------- AlwaysOnline end ---------------
188
189 // ---------- IP -------------------
190 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
191 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier(*this, u);
192
193 /*BeforeChgIPNotifier.SetAuthorizator(this);
194 BeforeChgIPNotifier.SetUser(u);*/
195 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
196
197 /*AfterChgIPNotifier.SetAuthorizator(this);
198 AfterChgIPNotifier.SetUser(u);*/
199 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
200
201 u->property.ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
202 u->property.ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
203 // ---------- IP end ---------------
204 }
205 //-----------------------------------------------------------------------------
206 void AUTH_AO::UnSetUserNotifiers(user_iter u)
207 {
208 // ---      AlwaysOnline        ---
209 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
210 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
211
212 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
213 list<CHG_AFTER_NOTIFIER<int> >::iterator  aoAIter;
214
215 aoBIter = find_if(BeforeChgAONotifierList.begin(),
216                   BeforeChgAONotifierList.end(),
217                   bind2nd(IsContainsUserAOB, u));
218
219 if (aoBIter != BeforeChgAONotifierList.end())
220     {
221     aoBIter->GetUser()->property.alwaysOnline.DelBeforeNotifier(&(*aoBIter));
222     BeforeChgAONotifierList.erase(aoBIter);
223     }
224
225 aoAIter = find_if(AfterChgAONotifierList.begin(),
226                   AfterChgAONotifierList.end(),
227                   bind2nd(IsContainsUserAOA, u));
228
229 if (aoAIter != AfterChgAONotifierList.end())
230     {
231     aoAIter->GetUser()->property.alwaysOnline.DelAfterNotifier(&(*aoAIter));
232     AfterChgAONotifierList.erase(aoAIter);
233     }
234 // ---      AlwaysOnline end    ---
235
236 // ---          IP              ---
237 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
238 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
239
240 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
241 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
242
243 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
244                   BeforeChgIPNotifierList.end(),
245                   bind2nd(IsContainsUserIPB, u));
246
247 if (ipBIter != BeforeChgIPNotifierList.end())
248     {
249     ipBIter->GetUser()->property.ips.DelBeforeNotifier(&(*ipBIter));
250     BeforeChgIPNotifierList.erase(ipBIter);
251     }
252
253 ipAIter = find_if(AfterChgIPNotifierList.begin(),
254                   AfterChgIPNotifierList.end(),
255                   bind2nd(IsContainsUserIPA, u));
256
257 if (ipAIter != AfterChgIPNotifierList.end())
258     {
259     ipAIter->GetUser()->property.ips.DelAfterNotifier(&(*ipAIter));
260     AfterChgIPNotifierList.erase(ipAIter);
261     }
262 // ---          IP end          ---
263 }
264 //-----------------------------------------------------------------------------
265 void AUTH_AO::GetUsers()
266 {
267 user_iter u;
268 int h = users->OpenSearch();
269 if (!h)
270     {
271     printfd(__FILE__, "users->OpenSearch() error\n");
272     return;
273     }
274
275 while (1)
276     {
277     if (users->SearchNext(h, &u))
278         {
279         break;
280         }
281     usersList.push_back(u);
282     SetUserNotifiers(u);
283     }
284
285 users->CloseSearch(h);
286 }
287 //-----------------------------------------------------------------------------
288 void AUTH_AO::Unauthorize(user_iter u) const
289 {
290 u->Unauthorize(this);
291 }
292 //-----------------------------------------------------------------------------
293 void AUTH_AO::UpdateUserAuthorization(user_iter u) const
294 {
295 if (u->property.alwaysOnline)
296     {
297     USER_IPS ips = u->property.ips;
298     if (ips.OnlyOneIP())
299         {
300         if (u->Authorize(ips[0].ip, "", 0xFFffFFff, this) == 0)
301             {
302             }
303         }
304     }
305 }
306 //-----------------------------------------------------------------------------
307 void AUTH_AO::AddUser(user_iter u)
308 {
309 SetUserNotifiers(u);
310 usersList.push_back(u);
311 UpdateUserAuthorization(u);
312 }
313 //-----------------------------------------------------------------------------
314 void AUTH_AO::DelUser(user_iter u)
315 {
316 Unauthorize(u);
317 UnSetUserNotifiers(u);
318
319 list<user_iter>::iterator users_iter;
320 users_iter = usersList.begin();
321
322 while (users_iter != usersList.end())
323     {
324     if (u == *users_iter)
325         {
326         usersList.erase(users_iter);
327         break;
328         }
329     ++users_iter;
330     }
331 }
332 //-----------------------------------------------------------------------------
333 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
334 {
335 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
336 return -1;
337 }
338 //-----------------------------------------------------------------------------
339 template <typename varParamType>
340 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
341 {
342 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
343 }
344 //-----------------------------------------------------------------------------
345 template <typename varParamType>
346 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
347 {
348 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
349 }
350 //-----------------------------------------------------------------------------
351