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