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