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