]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/stress/stress.cpp
Added license header.
[stg.git] / projects / stargazer / plugins / authorization / stress / stress.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.5 $
23  $Date: 2009/06/19 12:50:32 $
24  $Author: faust $
25  */
26
27 #include <stdio.h>
28 #include <unistd.h>
29
30 #include <csignal>
31 #include <algorithm>
32
33 #include "stg/user.h"
34 #include "stg/common.h"
35 #include "stg/user_property.h"
36 #include "stg/plugin_creator.h"
37
38 #include "stress.h"
39
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 PLUGIN_CREATOR<AUTH_STRESS> stressc;
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
48 template <typename varType>
49 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
50 {
51 public:
52     bool operator()(varType notifier, USER_PTR user) const
53         {
54         return notifier.GetUser() == user;
55         };
56 };
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 PLUGIN * GetPlugin()
61 {
62 //printf("BASE_CAPTURER * GetCapturer()\n");
63 return stressc.GetPlugin();
64 }
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS()
69     : averageOnlineTime(0)
70 {
71 }
72 //-----------------------------------------------------------------------------
73 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
74 {
75 PARAM_VALUE pv;
76 vector<PARAM_VALUE>::const_iterator pvi;
77
78 pv.param = "AverageOnlineTime";
79 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
81     {
82     errorStr = "Parameter \'" + pv.param + "\' not found.";
83     return -1;
84     }
85
86 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
87     {
88     errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
89     return -1;
90     }
91
92 return 0;
93 }
94 //-----------------------------------------------------------------------------
95 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
96 {
97 return averageOnlineTime;
98 }
99 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 const string AUTH_STRESS::GetVersion() const
103 {
104 return "Stress authorizator v.0.1";
105 }
106 //-----------------------------------------------------------------------------
107 AUTH_STRESS::AUTH_STRESS()
108 {
109 pthread_mutex_init(&mutex, NULL);
110 isRunning = false;
111 }
112 //-----------------------------------------------------------------------------
113 void AUTH_STRESS::SetUsers(USERS * u)
114 {
115 users = u;
116 }
117 //-----------------------------------------------------------------------------
118 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
119 {
120 settings = s;
121 }
122 //-----------------------------------------------------------------------------
123 int AUTH_STRESS::ParseSettings()
124 {
125 int ret = stressSettings.ParseSettings(settings);
126 if (ret)
127     errorStr = stressSettings.GetStrError();
128 return ret;
129 }
130 //-----------------------------------------------------------------------------
131 const string & AUTH_STRESS::GetStrError() const
132 {
133 return errorStr;
134 }
135 //-----------------------------------------------------------------------------
136 int AUTH_STRESS::Start()
137 {
138 GetUsers();
139 nonstop = true;
140
141 list<USER_PTR>::iterator users_iter;
142
143 onAddUserNotifier.SetAuthorizator(this);
144 onDelUserNotifier.SetAuthorizator(this);
145 users->AddNotifierUserAdd(&onAddUserNotifier);
146 users->AddNotifierUserDel(&onDelUserNotifier);
147
148 if (!isRunning)
149     {
150     if (pthread_create(&thread, NULL, Run, this))
151         {
152         errorStr = "Cannot create thread.";
153         return -1;
154         }
155     }
156
157 users_iter = usersList.begin();
158 while (users_iter != usersList.end())
159     {
160     Authorize(*users_iter);
161     users_iter++;
162     }
163
164 //isRunning = true;
165 return 0;
166 }
167 //-----------------------------------------------------------------------------
168 int AUTH_STRESS::Stop()
169 {
170 nonstop = false;
171 if (isRunning)
172     {
173     //5 seconds to thread stops itself
174     int i;
175     for (i = 0; i < 25; i++)
176         {
177         if (!isRunning)
178             break;
179         struct timespec ts = {0, 200000000};
180         nanosleep(&ts, NULL);
181         }
182
183     //after 5 seconds waiting thread still running. now killing it
184     if (isRunning)
185         {
186         if (pthread_kill(thread, SIGINT))
187             {
188             errorStr = "Cannot kill thread.";
189             return -1;
190             }
191         printfd(__FILE__, "AUTH_STRESS killed Run\n");
192         }
193     }
194
195 users->DelNotifierUserAdd(&onAddUserNotifier);
196 users->DelNotifierUserDel(&onDelUserNotifier);
197
198 return 0;
199 }
200 //-----------------------------------------------------------------------------
201 bool AUTH_STRESS::IsRunning()
202 {
203 return isRunning;
204 }
205 //-----------------------------------------------------------------------------
206 uint16_t AUTH_STRESS::GetStartPosition() const
207 {
208 return 30;
209 }
210 //-----------------------------------------------------------------------------
211 uint16_t AUTH_STRESS::GetStopPosition() const
212 {
213 return 30;
214 }
215 //-----------------------------------------------------------------------------
216 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
217 {
218 // ---------- IP -------------------
219 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
220 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier;
221
222 BeforeChgIPNotifier.SetAuthorizator(this);
223 BeforeChgIPNotifier.SetUser(u);
224 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
225
226 AfterChgIPNotifier.SetAuthorizator(this);
227 AfterChgIPNotifier.SetUser(u);
228 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
229
230 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
231 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
232 // ---------- IP end ---------------
233 }
234 //-----------------------------------------------------------------------------
235 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
236 {
237 // ---          IP              ---
238 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
239 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
240
241 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
242 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
243
244 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
245                   BeforeChgIPNotifierList.end(),
246                   bind2nd(IsContainsUserIPB, u));
247
248 if (ipBIter != BeforeChgIPNotifierList.end())
249     {
250     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
251     BeforeChgIPNotifierList.erase(ipBIter);
252     }
253
254 ipAIter = find_if(AfterChgIPNotifierList.begin(),
255                   AfterChgIPNotifierList.end(),
256                   bind2nd(IsContainsUserIPA, u));
257
258 if (ipAIter != AfterChgIPNotifierList.end())
259     {
260     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
261     AfterChgIPNotifierList.erase(ipAIter);
262     }
263 // ---          IP end          ---
264 }
265 //-----------------------------------------------------------------------------
266 void AUTH_STRESS::GetUsers()
267 {
268 USER_PTR u;
269 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
270 int h = users->OpenSearch();
271 if (!h)
272     {
273     printfd(__FILE__, "users->OpenSearch() error\n");
274     return;
275     }
276
277 while (1)
278     {
279     if (users->SearchNext(h, &u))
280         {
281         break;
282         }
283     usersList.push_back(u);
284     SetUserNotifiers(u);
285     }
286
287 users->CloseSearch(h);
288 }
289 //-----------------------------------------------------------------------------
290 void AUTH_STRESS::Unauthorize(USER_PTR u) const
291 {
292 if (!u->IsAuthorizedBy(this))
293     return;
294
295 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
296 u->Unauthorize(this);
297 }
298 //-----------------------------------------------------------------------------
299 void AUTH_STRESS::Authorize(USER_PTR u) const
300 {
301 USER_IPS ips = u->GetProperty().ips;
302 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
303     {
304     if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
305         {
306         printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
307         }
308     }
309 }
310 //-----------------------------------------------------------------------------
311 void AUTH_STRESS::AddUser(USER_PTR u)
312 {
313 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
314 SetUserNotifiers(u);
315 usersList.push_back(u);
316 }
317 //-----------------------------------------------------------------------------
318 void AUTH_STRESS::DelUser(USER_PTR u)
319 {
320 Unauthorize(u);
321 UnSetUserNotifiers(u);
322
323 list<USER_PTR>::iterator users_iter;
324 users_iter = usersList.begin();
325
326 while (users_iter != usersList.end())
327     {
328     if (u == *users_iter)
329         {
330         usersList.erase(users_iter);
331         printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
332         break;
333         }
334     users_iter++;
335     }
336 }
337 //-----------------------------------------------------------------------------
338 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
339 {
340 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
341 return -1;
342 }
343 //-----------------------------------------------------------------------------
344 void * AUTH_STRESS::Run(void * d)
345 {
346 AUTH_STRESS * ia;
347 ia = (AUTH_STRESS *)d;
348
349 ia->isRunning = true;
350
351 while (ia->nonstop)
352     {
353     printfd(__FILE__, "AUTH_STRESS::Run - averageTime: %d\n", random() % (2*ia->stressSettings.GetAverageOnlineTime()));
354
355     list<USER_PTR>::iterator users_iter;
356     users_iter = ia->usersList.begin();
357     while (users_iter != ia->usersList.end())
358         {
359         if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 1)
360             {
361             ia->Authorize(*users_iter);
362             printfd(__FILE__, "AUTH_STRESS::Authorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
363             }
364         if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 2)
365             {
366             ia->Unauthorize(*users_iter);
367             printfd(__FILE__, "AUTH_STRESS::Unauthorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
368             }
369
370         users_iter++;
371         }
372
373     sleep(1);
374     }
375
376 ia->isRunning = false;
377 return NULL;
378 }
379 //-----------------------------------------------------------------------------
380 template <typename varParamType>
381 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
382 {
383 auth->Unauthorize(user);
384 }
385 //-----------------------------------------------------------------------------
386 template <typename varParamType>
387 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
388 {
389 auth->Authorize(user);
390 }
391 //-----------------------------------------------------------------------------