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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2009/06/19 12:50:32 $
34 #include "stg/common.h"
35 #include "stg/user_property.h"
36 #include "stg/plugin_creator.h"
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>
52 bool operator()(varType notifier, USER_PTR user) const
54 return notifier.GetUser() == user;
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
62 //printf("BASE_CAPTURER * GetCapturer()\n");
63 return stressc.GetPlugin();
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
68 AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS()
69 : averageOnlineTime(0)
72 //-----------------------------------------------------------------------------
73 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
76 vector<PARAM_VALUE>::const_iterator pvi;
78 pv.param = "AverageOnlineTime";
79 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
82 errorStr = "Parameter \'" + pv.param + "\' not found.";
86 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
88 errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
94 //-----------------------------------------------------------------------------
95 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
97 return averageOnlineTime;
99 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 const string AUTH_STRESS::GetVersion() const
104 return "Stress authorizator v.0.1";
106 //-----------------------------------------------------------------------------
107 AUTH_STRESS::AUTH_STRESS()
109 pthread_mutex_init(&mutex, NULL);
112 //-----------------------------------------------------------------------------
113 void AUTH_STRESS::SetUsers(USERS * u)
117 //-----------------------------------------------------------------------------
118 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
122 //-----------------------------------------------------------------------------
123 int AUTH_STRESS::ParseSettings()
125 int ret = stressSettings.ParseSettings(settings);
127 errorStr = stressSettings.GetStrError();
130 //-----------------------------------------------------------------------------
131 const string & AUTH_STRESS::GetStrError() const
135 //-----------------------------------------------------------------------------
136 int AUTH_STRESS::Start()
141 list<USER_PTR>::iterator users_iter;
143 onAddUserNotifier.SetAuthorizator(this);
144 onDelUserNotifier.SetAuthorizator(this);
145 users->AddNotifierUserAdd(&onAddUserNotifier);
146 users->AddNotifierUserDel(&onDelUserNotifier);
150 if (pthread_create(&thread, NULL, Run, this))
152 errorStr = "Cannot create thread.";
157 users_iter = usersList.begin();
158 while (users_iter != usersList.end())
160 Authorize(*users_iter);
167 //-----------------------------------------------------------------------------
168 int AUTH_STRESS::Stop()
173 //5 seconds to thread stops itself
175 for (i = 0; i < 25; i++)
179 struct timespec ts = {0, 200000000};
180 nanosleep(&ts, NULL);
183 //after 5 seconds waiting thread still running. now killing it
186 if (pthread_kill(thread, SIGINT))
188 errorStr = "Cannot kill thread.";
191 printfd(__FILE__, "AUTH_STRESS killed Run\n");
195 users->DelNotifierUserAdd(&onAddUserNotifier);
196 users->DelNotifierUserDel(&onDelUserNotifier);
200 //-----------------------------------------------------------------------------
201 bool AUTH_STRESS::IsRunning()
205 //-----------------------------------------------------------------------------
206 uint16_t AUTH_STRESS::GetStartPosition() const
210 //-----------------------------------------------------------------------------
211 uint16_t AUTH_STRESS::GetStopPosition() const
215 //-----------------------------------------------------------------------------
216 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
218 // ---------- IP -------------------
219 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
220 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier;
222 BeforeChgIPNotifier.SetAuthorizator(this);
223 BeforeChgIPNotifier.SetUser(u);
224 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
226 AfterChgIPNotifier.SetAuthorizator(this);
227 AfterChgIPNotifier.SetUser(u);
228 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
230 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
231 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
232 // ---------- IP end ---------------
234 //-----------------------------------------------------------------------------
235 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
238 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
239 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
241 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
242 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
244 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
245 BeforeChgIPNotifierList.end(),
246 bind2nd(IsContainsUserIPB, u));
248 if (ipBIter != BeforeChgIPNotifierList.end())
250 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
251 BeforeChgIPNotifierList.erase(ipBIter);
254 ipAIter = find_if(AfterChgIPNotifierList.begin(),
255 AfterChgIPNotifierList.end(),
256 bind2nd(IsContainsUserIPA, u));
258 if (ipAIter != AfterChgIPNotifierList.end())
260 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
261 AfterChgIPNotifierList.erase(ipAIter);
265 //-----------------------------------------------------------------------------
266 void AUTH_STRESS::GetUsers()
269 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
270 int h = users->OpenSearch();
273 printfd(__FILE__, "users->OpenSearch() error\n");
279 if (users->SearchNext(h, &u))
283 usersList.push_back(u);
287 users->CloseSearch(h);
289 //-----------------------------------------------------------------------------
290 void AUTH_STRESS::Unauthorize(USER_PTR u) const
292 if (!u->IsAuthorizedBy(this))
295 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
296 u->Unauthorize(this);
298 //-----------------------------------------------------------------------------
299 void AUTH_STRESS::Authorize(USER_PTR u) const
301 USER_IPS ips = u->GetProperty().ips;
302 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
304 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
306 printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
310 //-----------------------------------------------------------------------------
311 void AUTH_STRESS::AddUser(USER_PTR u)
313 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
315 usersList.push_back(u);
317 //-----------------------------------------------------------------------------
318 void AUTH_STRESS::DelUser(USER_PTR u)
321 UnSetUserNotifiers(u);
323 list<USER_PTR>::iterator users_iter;
324 users_iter = usersList.begin();
326 while (users_iter != usersList.end())
328 if (u == *users_iter)
330 usersList.erase(users_iter);
331 printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
337 //-----------------------------------------------------------------------------
338 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
340 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
343 //-----------------------------------------------------------------------------
344 void * AUTH_STRESS::Run(void * d)
346 AUTH_STRESS * ia = static_cast<AUTH_STRESS *>(d);
348 ia->isRunning = true;
352 printfd(__FILE__, "AUTH_STRESS::Run - averageTime: %d\n", random() % (2*ia->stressSettings.GetAverageOnlineTime()));
354 list<USER_PTR>::iterator users_iter;
355 users_iter = ia->usersList.begin();
356 while (users_iter != ia->usersList.end())
358 if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 1)
360 ia->Authorize(*users_iter);
361 printfd(__FILE__, "AUTH_STRESS::Authorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
363 if (random() % (2*ia->stressSettings.GetAverageOnlineTime()) == 2)
365 ia->Unauthorize(*users_iter);
366 printfd(__FILE__, "AUTH_STRESS::Unauthorize - user: '%s'\n", (*users_iter)->GetLogin().c_str());
375 ia->isRunning = false;
378 //-----------------------------------------------------------------------------
379 template <typename varParamType>
380 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
382 auth->Unauthorize(user);
384 //-----------------------------------------------------------------------------
385 template <typename varParamType>
386 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
388 auth->Authorize(user);
390 //-----------------------------------------------------------------------------