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