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