]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.cpp
84e97a0d87a81ab149406e7b30f28dc14057bdcc
[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 <unistd.h>
28
29 #include <csignal>
30 #include <algorithm> // for_each
31 #include <functional> // mem_fun_ref
32
33 #include "ao.h"
34 #include "user.h"
35 #include "users.h"
36 #include "user_property.h"
37 #include "../../../eventloop.h"
38
39 class AO_CREATOR
40 {
41 private:
42     AUTH_AO * ao;
43
44 public:
45     AO_CREATOR()
46         : ao(new AUTH_AO())
47         {
48         };
49     ~AO_CREATOR()
50         {
51         delete ao;
52         };
53
54     AUTH_AO * GetPlugin()
55         {
56         return ao;
57         };
58 };
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 AO_CREATOR aoc;
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 template <typename varType>
67 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
68 {
69 public:
70     bool operator()(varType notifier, USER_PTR user) const
71         {
72         return notifier.GetUser() == user;
73         };
74 };
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 PLUGIN * GetPlugin()
79 {
80 return aoc.GetPlugin();
81 }
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 const string AUTH_AO::GetVersion() const
86 {
87 return "Always Online authorizator v.1.0";
88 }
89 //-----------------------------------------------------------------------------
90 AUTH_AO::AUTH_AO()
91     : users(NULL),
92       isRunning(false),
93       onAddUserNotifier(*this),
94       onDelUserNotifier(*this)
95 {
96 }
97 //-----------------------------------------------------------------------------
98 int AUTH_AO::Start()
99 {
100 GetUsers();
101
102 users->AddNotifierUserAdd(&onAddUserNotifier);
103 users->AddNotifierUserDel(&onDelUserNotifier);
104
105 /*list<USER_PTR>::iterator it = usersList.begin();
106 while (it != usersList.end())
107     {
108     UpdateUserAuthorization(*it);
109     ++it;
110     }*/
111 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
112
113 isRunning = true;
114
115 return 0;
116 }
117 //-----------------------------------------------------------------------------
118 int AUTH_AO::Stop()
119 {
120 if (!isRunning)
121     return 0;
122
123 users->DelNotifierUserAdd(&onAddUserNotifier);
124 users->DelNotifierUserDel(&onDelUserNotifier);
125
126 list<USER_PTR>::iterator users_iter;
127 users_iter = usersList.begin();
128 while (users_iter != usersList.end())
129     {
130     Unauthorize(*users_iter);
131     UnSetUserNotifiers(*users_iter);
132     ++users_iter;
133     }
134 isRunning = false;
135 return 0;
136 }
137 //-----------------------------------------------------------------------------
138 bool AUTH_AO::IsRunning()
139 {
140 return isRunning;
141 }
142 //-----------------------------------------------------------------------------
143 uint16_t AUTH_AO::GetStartPosition() const
144 {
145 return 70;
146 }
147 //-----------------------------------------------------------------------------
148 uint16_t AUTH_AO::GetStopPosition() const
149 {
150 return 70;
151 }
152 //-----------------------------------------------------------------------------
153 void AUTH_AO::SetUserNotifiers(USER_PTR u)
154 {
155 // ---------- AlwaysOnline -------------------
156 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
157 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier(*this, u);
158
159 /*BeforeChgAONotifier.SetAuthorizator(this);
160 BeforeChgAONotifier.SetUser(u);*/
161 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
162
163 /*AfterChgAONotifier.SetAuthorizator(this);
164 AfterChgAONotifier.SetUser(u);*/
165 AfterChgAONotifierList.push_front(AfterChgAONotifier);
166
167 u->GetProperty().alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
168 u->GetProperty().alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
169 // ---------- AlwaysOnline end ---------------
170
171 // ---------- IP -------------------
172 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
173 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier(*this, u);
174
175 /*BeforeChgIPNotifier.SetAuthorizator(this);
176 BeforeChgIPNotifier.SetUser(u);*/
177 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
178
179 /*AfterChgIPNotifier.SetAuthorizator(this);
180 AfterChgIPNotifier.SetUser(u);*/
181 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
182
183 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
184 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
185 // ---------- IP end ---------------
186 }
187 //-----------------------------------------------------------------------------
188 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
189 {
190 // ---      AlwaysOnline        ---
191 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
192 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
193
194 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
195 list<CHG_AFTER_NOTIFIER<int> >::iterator  aoAIter;
196
197 aoBIter = find_if(BeforeChgAONotifierList.begin(),
198                   BeforeChgAONotifierList.end(),
199                   bind2nd(IsContainsUserAOB, u));
200
201 if (aoBIter != BeforeChgAONotifierList.end())
202     {
203     aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
204     BeforeChgAONotifierList.erase(aoBIter);
205     }
206
207 aoAIter = find_if(AfterChgAONotifierList.begin(),
208                   AfterChgAONotifierList.end(),
209                   bind2nd(IsContainsUserAOA, u));
210
211 if (aoAIter != AfterChgAONotifierList.end())
212     {
213     aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
214     AfterChgAONotifierList.erase(aoAIter);
215     }
216 // ---      AlwaysOnline end    ---
217
218 // ---          IP              ---
219 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
220 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
221
222 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
223 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
224
225 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
226                   BeforeChgIPNotifierList.end(),
227                   bind2nd(IsContainsUserIPB, u));
228
229 if (ipBIter != BeforeChgIPNotifierList.end())
230     {
231     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
232     BeforeChgIPNotifierList.erase(ipBIter);
233     }
234
235 ipAIter = find_if(AfterChgIPNotifierList.begin(),
236                   AfterChgIPNotifierList.end(),
237                   bind2nd(IsContainsUserIPA, u));
238
239 if (ipAIter != AfterChgIPNotifierList.end())
240     {
241     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
242     AfterChgIPNotifierList.erase(ipAIter);
243     }
244 // ---          IP end          ---
245 }
246 //-----------------------------------------------------------------------------
247 void AUTH_AO::GetUsers()
248 {
249 USER_PTR u;
250 int h = users->OpenSearch();
251 if (!h)
252     {
253     printfd(__FILE__, "users->OpenSearch() error\n");
254     return;
255     }
256
257 while (1)
258     {
259     if (users->SearchNext(h, &u))
260         {
261         break;
262         }
263     usersList.push_back(u);
264     SetUserNotifiers(u);
265     }
266
267 users->CloseSearch(h);
268 }
269 //-----------------------------------------------------------------------------
270 void AUTH_AO::Unauthorize(USER_PTR u) const
271 {
272 u->Unauthorize(this);
273 }
274 //-----------------------------------------------------------------------------
275 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
276 {
277 if (u->GetProperty().alwaysOnline)
278     {
279     USER_IPS ips = u->GetProperty().ips;
280     if (ips.OnlyOneIP())
281         {
282         if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
283             {
284             }
285         }
286     }
287 }
288 //-----------------------------------------------------------------------------
289 void AUTH_AO::AddUser(USER_PTR u)
290 {
291 SetUserNotifiers(u);
292 usersList.push_back(u);
293 UpdateUserAuthorization(u);
294 }
295 //-----------------------------------------------------------------------------
296 void AUTH_AO::DelUser(USER_PTR u)
297 {
298 Unauthorize(u);
299 UnSetUserNotifiers(u);
300
301 list<USER_PTR>::iterator users_iter;
302 users_iter = usersList.begin();
303
304 while (users_iter != usersList.end())
305     {
306     if (u == *users_iter)
307         {
308         usersList.erase(users_iter);
309         break;
310         }
311     ++users_iter;
312     }
313 }
314 //-----------------------------------------------------------------------------
315 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
316 {
317 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
318 return -1;
319 }
320 //-----------------------------------------------------------------------------
321 template <typename varParamType>
322 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
323 {
324 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
325 }
326 //-----------------------------------------------------------------------------
327 template <typename varParamType>
328 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
329 {
330 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
331 }
332 //-----------------------------------------------------------------------------