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