]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.cpp
Документация помещена в каталог doc
[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 {
89 isRunning = false;
90 }
91 //-----------------------------------------------------------------------------
92 void AUTH_AO::SetUsers(USERS * u)
93 {
94 users = u;
95 }
96 //-----------------------------------------------------------------------------
97 void AUTH_AO::SetSettings(const MODULE_SETTINGS & s)
98 {
99 settings = s;
100 }
101 //-----------------------------------------------------------------------------
102 int AUTH_AO::ParseSettings()
103 {
104 return 0;
105 }
106 //-----------------------------------------------------------------------------
107 const string & AUTH_AO::GetStrError() const
108 {
109 return errorStr;
110 }
111 //-----------------------------------------------------------------------------
112 int AUTH_AO::Start()
113 {
114 GetUsers();
115
116 list<user_iter>::iterator users_iter;
117
118 onAddUserNotifier.SetAuthorizator(this);
119 onDelUserNotifier.SetAuthorizator(this);
120 users->AddNotifierUserAdd(&onAddUserNotifier);
121 users->AddNotifierUserDel(&onDelUserNotifier);
122
123 users_iter = usersList.begin();
124 while (users_iter != usersList.end())
125     {
126     UpdateUserAuthorization(*users_iter);
127     ++users_iter;
128     }
129 isRunning = true;
130 return 0;
131 }
132 //-----------------------------------------------------------------------------
133 int AUTH_AO::Stop()
134 {
135 if (!isRunning)
136     return 0;
137
138 users->DelNotifierUserAdd(&onAddUserNotifier);
139 users->DelNotifierUserDel(&onDelUserNotifier);
140
141 list<user_iter>::iterator users_iter;
142 users_iter = usersList.begin();
143 while (users_iter != usersList.end())
144     {
145     Unauthorize(*users_iter);
146     UnSetUserNotifiers(*users_iter);
147     ++users_iter;
148     }
149 isRunning = false;
150 return 0;
151 }
152 //-----------------------------------------------------------------------------
153 bool AUTH_AO::IsRunning()
154 {
155 return isRunning;
156 }
157 //-----------------------------------------------------------------------------
158 uint16_t AUTH_AO::GetStartPosition() const
159 {
160 return 70;
161 }
162 //-----------------------------------------------------------------------------
163 uint16_t AUTH_AO::GetStopPosition() const
164 {
165 return 70;
166 }
167 //-----------------------------------------------------------------------------
168 void AUTH_AO::SetUserNotifiers(user_iter u)
169 {
170 // ---------- AlwaysOnline -------------------
171 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier;
172 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier;
173
174 BeforeChgAONotifier.SetAuthorizator(this);
175 BeforeChgAONotifier.SetUser(u);
176 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
177
178 AfterChgAONotifier.SetAuthorizator(this);
179 AfterChgAONotifier.SetUser(u);
180 AfterChgAONotifierList.push_front(AfterChgAONotifier);
181
182 u->property.alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
183 u->property.alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
184 // ---------- AlwaysOnline end ---------------
185
186 // ---------- IP -------------------
187 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
188 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier;
189
190 BeforeChgIPNotifier.SetAuthorizator(this);
191 BeforeChgIPNotifier.SetUser(u);
192 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
193
194 AfterChgIPNotifier.SetAuthorizator(this);
195 AfterChgIPNotifier.SetUser(u);
196 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
197
198 u->property.ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
199 u->property.ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
200 // ---------- IP end ---------------
201 }
202 //-----------------------------------------------------------------------------
203 void AUTH_AO::UnSetUserNotifiers(user_iter u)
204 {
205 // ---      AlwaysOnline        ---
206 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
207 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
208
209 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
210 list<CHG_AFTER_NOTIFIER<int> >::iterator  aoAIter;
211
212 aoBIter = find_if(BeforeChgAONotifierList.begin(),
213                   BeforeChgAONotifierList.end(),
214                   bind2nd(IsContainsUserAOB, u));
215
216 if (aoBIter != BeforeChgAONotifierList.end())
217     {
218     aoBIter->GetUser()->property.alwaysOnline.DelBeforeNotifier(&(*aoBIter));
219     BeforeChgAONotifierList.erase(aoBIter);
220     }
221
222 aoAIter = find_if(AfterChgAONotifierList.begin(),
223                   AfterChgAONotifierList.end(),
224                   bind2nd(IsContainsUserAOA, u));
225
226 if (aoAIter != AfterChgAONotifierList.end())
227     {
228     aoAIter->GetUser()->property.alwaysOnline.DelAfterNotifier(&(*aoAIter));
229     AfterChgAONotifierList.erase(aoAIter);
230     }
231 // ---      AlwaysOnline end    ---
232
233 // ---          IP              ---
234 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
235 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
236
237 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
238 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
239
240 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
241                   BeforeChgIPNotifierList.end(),
242                   bind2nd(IsContainsUserIPB, u));
243
244 if (ipBIter != BeforeChgIPNotifierList.end())
245     {
246     ipBIter->GetUser()->property.ips.DelBeforeNotifier(&(*ipBIter));
247     BeforeChgIPNotifierList.erase(ipBIter);
248     }
249
250 ipAIter = find_if(AfterChgIPNotifierList.begin(),
251                   AfterChgIPNotifierList.end(),
252                   bind2nd(IsContainsUserIPA, u));
253
254 if (ipAIter != AfterChgIPNotifierList.end())
255     {
256     ipAIter->GetUser()->property.ips.DelAfterNotifier(&(*ipAIter));
257     AfterChgIPNotifierList.erase(ipAIter);
258     }
259 // ---          IP end          ---
260 }
261 //-----------------------------------------------------------------------------
262 void AUTH_AO::GetUsers()
263 {
264 user_iter u;
265 int h = users->OpenSearch();
266 if (!h)
267     {
268     printfd(__FILE__, "users->OpenSearch() error\n");
269     return;
270     }
271
272 while (1)
273     {
274     if (users->SearchNext(h, &u))
275         {
276         break;
277         }
278     usersList.push_back(u);
279     SetUserNotifiers(u);
280     }
281
282 users->CloseSearch(h);
283 }
284 //-----------------------------------------------------------------------------
285 void AUTH_AO::Unauthorize(user_iter u) const
286 {
287 u->Unauthorize(this);
288 }
289 //-----------------------------------------------------------------------------
290 void AUTH_AO::UpdateUserAuthorization(user_iter u) const
291 {
292 if (u->property.alwaysOnline)
293     {
294     USER_IPS ips = u->property.ips;
295     if (ips.OnlyOneIP())
296         {
297         if (u->Authorize(ips[0].ip, "", 0xFFffFFff, this) == 0)
298             {
299             }
300         }
301     }
302 }
303 //-----------------------------------------------------------------------------
304 void AUTH_AO::AddUser(user_iter u)
305 {
306 SetUserNotifiers(u);
307 usersList.push_back(u);
308 UpdateUserAuthorization(u);
309 }
310 //-----------------------------------------------------------------------------
311 void AUTH_AO::DelUser(user_iter u)
312 {
313 Unauthorize(u);
314 UnSetUserNotifiers(u);
315
316 list<user_iter>::iterator users_iter;
317 users_iter = usersList.begin();
318
319 while (users_iter != usersList.end())
320     {
321     if (u == *users_iter)
322         {
323         usersList.erase(users_iter);
324         break;
325         }
326     ++users_iter;
327     }
328 }
329 //-----------------------------------------------------------------------------
330 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
331 {
332 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
333 return -1;
334 }
335 //-----------------------------------------------------------------------------
336 template <typename varParamType>
337 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
338 {
339 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(*auth, &AUTH_AO::Unauthorize, user);
340 }
341 //-----------------------------------------------------------------------------
342 template <typename varParamType>
343 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
344 {
345 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(*auth, &AUTH_AO::UpdateUserAuthorization, user);
346 }
347 //-----------------------------------------------------------------------------
348