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