]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.cpp
Extra debug information removed from ao plugin
[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 "stg/user.h"
34 #include "stg/users.h"
35 #include "stg/user_property.h"
36 #include "stg/common.h"
37 #include "ao.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 printfd(__FILE__, "AUTH_AO::Start()\n");
101 GetUsers();
102
103 users->AddNotifierUserAdd(&onAddUserNotifier);
104 users->AddNotifierUserDel(&onDelUserNotifier);
105
106 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
107
108 isRunning = true;
109
110 return 0;
111 }
112 //-----------------------------------------------------------------------------
113 int AUTH_AO::Stop()
114 {
115 printfd(__FILE__, "AUTH_AO::Stop()\n");
116 if (!isRunning)
117     return 0;
118
119 users->DelNotifierUserAdd(&onAddUserNotifier);
120 users->DelNotifierUserDel(&onDelUserNotifier);
121
122 list<USER_PTR>::iterator users_iter;
123 users_iter = usersList.begin();
124 while (users_iter != usersList.end())
125     {
126     Unauthorize(*users_iter);
127     UnSetUserNotifiers(*users_iter);
128     ++users_iter;
129     }
130 isRunning = false;
131 return 0;
132 }
133 //-----------------------------------------------------------------------------
134 void AUTH_AO::SetUserNotifiers(USER_PTR u)
135 {
136 // ---------- AlwaysOnline -------------------
137 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
138 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier(*this, u);
139
140 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
141 AfterChgAONotifierList.push_front(AfterChgAONotifier);
142
143 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
144 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
145 // ---------- AlwaysOnline end ---------------
146
147 // ---------- IP -------------------
148 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
149 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier(*this, u);
150
151 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
152 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
153
154 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
155 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
156 // ---------- IP end ---------------
157 }
158 //-----------------------------------------------------------------------------
159 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
160 {
161 // ---      AlwaysOnline        ---
162 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
163 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
164
165 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
166 list<CHG_AFTER_NOTIFIER<int> >::iterator  aoAIter;
167
168 aoBIter = find_if(BeforeChgAONotifierList.begin(),
169                   BeforeChgAONotifierList.end(),
170                   bind2nd(IsContainsUserAOB, u));
171
172 if (aoBIter != BeforeChgAONotifierList.end())
173     {
174     aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
175     BeforeChgAONotifierList.erase(aoBIter);
176     }
177
178 aoAIter = find_if(AfterChgAONotifierList.begin(),
179                   AfterChgAONotifierList.end(),
180                   bind2nd(IsContainsUserAOA, u));
181
182 if (aoAIter != AfterChgAONotifierList.end())
183     {
184     aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
185     AfterChgAONotifierList.erase(aoAIter);
186     }
187 // ---      AlwaysOnline end    ---
188
189 // ---          IP              ---
190 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
191 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
192
193 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
194 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
195
196 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
197                   BeforeChgIPNotifierList.end(),
198                   bind2nd(IsContainsUserIPB, u));
199
200 if (ipBIter != BeforeChgIPNotifierList.end())
201     {
202     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
203     BeforeChgIPNotifierList.erase(ipBIter);
204     }
205
206 ipAIter = find_if(AfterChgIPNotifierList.begin(),
207                   AfterChgIPNotifierList.end(),
208                   bind2nd(IsContainsUserIPA, u));
209
210 if (ipAIter != AfterChgIPNotifierList.end())
211     {
212     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
213     AfterChgIPNotifierList.erase(ipAIter);
214     }
215 // ---          IP end          ---
216 }
217 //-----------------------------------------------------------------------------
218 void AUTH_AO::GetUsers()
219 {
220 USER_PTR u;
221 int h = users->OpenSearch();
222 if (!h)
223     {
224     printfd(__FILE__, "users->OpenSearch() error\n");
225     return;
226     }
227
228 while (!users->SearchNext(h, &u))
229     {
230     usersList.push_back(u);
231     SetUserNotifiers(u);
232     }
233
234 users->CloseSearch(h);
235 }
236 //-----------------------------------------------------------------------------
237 void AUTH_AO::Unauthorize(USER_PTR u) const
238 {
239 u->Unauthorize(this);
240 }
241 //-----------------------------------------------------------------------------
242 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
243 {
244 if (u->GetProperty().alwaysOnline)
245     {
246     USER_IPS ips = u->GetProperty().ips;
247     if (ips.OnlyOneIP())
248         {
249         if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
250             {
251             }
252         }
253     }
254 }
255 //-----------------------------------------------------------------------------
256 void AUTH_AO::AddUser(USER_PTR u)
257 {
258 SetUserNotifiers(u);
259 usersList.push_back(u);
260 UpdateUserAuthorization(u);
261 }
262 //-----------------------------------------------------------------------------
263 void AUTH_AO::DelUser(USER_PTR u)
264 {
265 Unauthorize(u);
266 UnSetUserNotifiers(u);
267 usersList.remove(u);
268 }
269 //-----------------------------------------------------------------------------
270 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
271 {
272 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
273 return -1;
274 }
275 //-----------------------------------------------------------------------------
276 template <typename varParamType>
277 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
278 {
279 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
280 auth.Unauthorize(user);
281 }
282 //-----------------------------------------------------------------------------
283 template <typename varParamType>
284 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
285 {
286 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
287 auth.UpdateUserAuthorization(user);
288 }
289 //-----------------------------------------------------------------------------