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 $
32 #include "../../../user.h"
42 printfd(__FILE__, "constructor STRESS_CREATOR\n");
43 dc = new AUTH_STRESS();
47 printfd(__FILE__, "destructor STRESS_CREATOR\n");
51 BASE_PLUGIN * GetPlugin()
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>
68 bool operator()(varType notifier, user_iter user) const
70 return notifier.GetUser() == user;
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 BASE_PLUGIN * GetPlugin()
78 //printf("BASE_CAPTURER * GetCapturer()\n");
79 return stressc.GetPlugin();
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 AUTH_STRESS_SETTINGS::AUTH_STRESS_SETTINGS()
85 : averageOnlineTime(0)
88 //-----------------------------------------------------------------------------
89 int AUTH_STRESS_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val)
91 if (str2x(str.c_str(), *val))
93 errorStr = "Incorrect value \'" + str + "\'.";
96 if (*val < min || *val > max)
98 errorStr = "Value \'" + str + "\' out of range.";
103 //-----------------------------------------------------------------------------
104 int AUTH_STRESS_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
107 vector<PARAM_VALUE>::const_iterator pvi;
109 pv.param = "AverageOnlineTime";
110 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
111 if (pvi == s.moduleParams.end())
113 errorStr = "Parameter \'" + pv.param + "\' not found.";
117 if (ParseIntInRange(pvi->value[0], 5, 10*3600, &averageOnlineTime))
119 errorStr = "Cannot parse parameter \'" + pv.param + "\': " + errorStr;
125 //-----------------------------------------------------------------------------
126 int AUTH_STRESS_SETTINGS::GetAverageOnlineTime() const
128 return averageOnlineTime;
130 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
133 const string AUTH_STRESS::GetVersion() const
135 return "Stress authorizator v.0.1";
137 //-----------------------------------------------------------------------------
138 AUTH_STRESS::AUTH_STRESS()
140 pthread_mutex_init(&mutex, NULL);
143 //-----------------------------------------------------------------------------
144 void AUTH_STRESS::SetUsers(USERS * u)
148 //-----------------------------------------------------------------------------
149 void AUTH_STRESS::SetSettings(const MODULE_SETTINGS & s)
153 //-----------------------------------------------------------------------------
154 int AUTH_STRESS::ParseSettings()
156 int ret = stressSettings.ParseSettings(settings);
158 errorStr = stressSettings.GetStrError();
161 //-----------------------------------------------------------------------------
162 const string & AUTH_STRESS::GetStrError() const
166 //-----------------------------------------------------------------------------
167 int AUTH_STRESS::Start()
172 list<user_iter>::iterator users_iter;
174 onAddUserNotifier.SetAuthorizator(this);
175 onDelUserNotifier.SetAuthorizator(this);
176 users->AddNotifierUserAdd(&onAddUserNotifier);
177 users->AddNotifierUserDel(&onDelUserNotifier);
181 if (pthread_create(&thread, NULL, Run, this))
183 errorStr = "Cannot create thread.";
188 users_iter = usersList.begin();
189 while (users_iter != usersList.end())
191 Authorize(*users_iter);
198 //-----------------------------------------------------------------------------
199 int AUTH_STRESS::Stop()
204 //5 seconds to thread stops itself
206 for (i = 0; i < 25; i++)
213 //after 5 seconds waiting thread still running. now killing it
216 if (pthread_kill(thread, SIGINT))
218 errorStr = "Cannot kill thread.";
221 printfd(__FILE__, "AUTH_STRESS killed Run\n");
225 users->DelNotifierUserAdd(&onAddUserNotifier);
226 users->DelNotifierUserDel(&onDelUserNotifier);
230 //-----------------------------------------------------------------------------
231 bool AUTH_STRESS::IsRunning()
235 //-----------------------------------------------------------------------------
236 uint16_t AUTH_STRESS::GetStartPosition() const
240 //-----------------------------------------------------------------------------
241 uint16_t AUTH_STRESS::GetStopPosition() const
245 //-----------------------------------------------------------------------------
246 void AUTH_STRESS::SetUserNotifiers(user_iter u)
248 // ---------- IP -------------------
249 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
250 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier;
252 BeforeChgIPNotifier.SetAuthorizator(this);
253 BeforeChgIPNotifier.SetUser(u);
254 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
256 AfterChgIPNotifier.SetAuthorizator(this);
257 AfterChgIPNotifier.SetUser(u);
258 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
260 u->property.ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
261 u->property.ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
262 // ---------- IP end ---------------
264 //-----------------------------------------------------------------------------
265 void AUTH_STRESS::UnSetUserNotifiers(user_iter u)
268 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
269 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
271 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
272 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
274 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
275 BeforeChgIPNotifierList.end(),
276 bind2nd(IsContainsUserIPB, u));
278 if (ipBIter != BeforeChgIPNotifierList.end())
280 ipBIter->GetUser()->property.ips.DelBeforeNotifier(&(*ipBIter));
281 BeforeChgIPNotifierList.erase(ipBIter);
284 ipAIter = find_if(AfterChgIPNotifierList.begin(),
285 AfterChgIPNotifierList.end(),
286 bind2nd(IsContainsUserIPA, u));
288 if (ipAIter != AfterChgIPNotifierList.end())
290 ipAIter->GetUser()->property.ips.DelAfterNotifier(&(*ipAIter));
291 AfterChgIPNotifierList.erase(ipAIter);
295 //-----------------------------------------------------------------------------
296 void AUTH_STRESS::GetUsers()
299 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
300 int h = users->OpenSearch();
303 printfd(__FILE__, "users->OpenSearch() error\n");
309 if (users->SearchNext(h, &u))
313 usersList.push_back(u);
317 users->CloseSearch(h);
319 //-----------------------------------------------------------------------------
320 void AUTH_STRESS::Unauthorize(user_iter u) const
322 if (!u->IsAuthorizedBy(this))
325 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
326 u->Unauthorize(this);
328 //-----------------------------------------------------------------------------
329 void AUTH_STRESS::Authorize(user_iter u) const
331 USER_IPS ips = u->property.ips;
332 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
334 if (u->Authorize(ips[0].ip, "", 0xFFffFFff, this) == 0)
336 printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
340 //-----------------------------------------------------------------------------
341 void AUTH_STRESS::AddUser(user_iter u)
343 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
345 usersList.push_back(u);
347 //-----------------------------------------------------------------------------
348 void AUTH_STRESS::DelUser(user_iter u)
351 UnSetUserNotifiers(u);
353 list<user_iter>::iterator users_iter;
354 users_iter = usersList.begin();
356 while (users_iter != usersList.end())
358 if (u == *users_iter)
360 usersList.erase(users_iter);
361 printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
367 //-----------------------------------------------------------------------------
368 int AUTH_STRESS::SendMessage(const STG_MSG & msg, uint32_t ip) const
370 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
373 //-----------------------------------------------------------------------------
374 void * AUTH_STRESS::Run(void * d)
377 ia = (AUTH_STRESS *)d;
379 ia->isRunning = true;
383 printfd(__FILE__, "AUTH_STRESS::Run\n");
385 list<user_iter>::iterator users_iter;
386 users_iter = ia->usersList.begin();
387 while (users_iter != ia->usersList.end())
389 if (random() % 2*ia->stressSettings.GetAverageOnlineTime() == 1)
391 ia->Authorize(*users_iter);
392 printfd(__FILE__, "AUTH_STRESS::Authorize\n");
394 if (random() % 2*ia->stressSettings.GetAverageOnlineTime() == 2)
396 ia->Unauthorize(*users_iter);
397 printfd(__FILE__, "AUTH_STRESS::Unauthorize\n");
406 ia->isRunning = false;
410 //-----------------------------------------------------------------------------
411 template <typename varParamType>
412 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType & oldValue, const varParamType & newValue)
414 auth->Unauthorize(user);
416 //-----------------------------------------------------------------------------
417 template <typename varParamType>
418 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType & oldValue, const varParamType & newValue)
420 auth->Unauthorize(user);
422 //-----------------------------------------------------------------------------