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