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