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++)
 
 182     //after 5 seconds waiting thread still running. now killing it
 
 185         if (pthread_kill(thread, SIGINT))
 
 187             errorStr = "Cannot kill thread.";
 
 190         printfd(__FILE__, "AUTH_STRESS killed Run\n");
 
 194 users->DelNotifierUserAdd(&onAddUserNotifier);
 
 195 users->DelNotifierUserDel(&onDelUserNotifier);
 
 199 //-----------------------------------------------------------------------------
 
 200 bool AUTH_STRESS::IsRunning()
 
 204 //-----------------------------------------------------------------------------
 
 205 uint16_t AUTH_STRESS::GetStartPosition() const
 
 209 //-----------------------------------------------------------------------------
 
 210 uint16_t AUTH_STRESS::GetStopPosition() const
 
 214 //-----------------------------------------------------------------------------
 
 215 void AUTH_STRESS::SetUserNotifiers(USER_PTR u)
 
 217 // ---------- IP -------------------
 
 218 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
 
 219 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier;
 
 221 BeforeChgIPNotifier.SetAuthorizator(this);
 
 222 BeforeChgIPNotifier.SetUser(u);
 
 223 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
 
 225 AfterChgIPNotifier.SetAuthorizator(this);
 
 226 AfterChgIPNotifier.SetUser(u);
 
 227 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
 
 229 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
 
 230 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
 
 231 // ---------- IP end ---------------
 
 233 //-----------------------------------------------------------------------------
 
 234 void AUTH_STRESS::UnSetUserNotifiers(USER_PTR u)
 
 237 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
 
 238 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
 
 240 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
 
 241 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
 
 243 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
 
 244                   BeforeChgIPNotifierList.end(),
 
 245                   bind2nd(IsContainsUserIPB, u));
 
 247 if (ipBIter != BeforeChgIPNotifierList.end())
 
 249     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
 
 250     BeforeChgIPNotifierList.erase(ipBIter);
 
 253 ipAIter = find_if(AfterChgIPNotifierList.begin(),
 
 254                   AfterChgIPNotifierList.end(),
 
 255                   bind2nd(IsContainsUserIPA, u));
 
 257 if (ipAIter != AfterChgIPNotifierList.end())
 
 259     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
 
 260     AfterChgIPNotifierList.erase(ipAIter);
 
 264 //-----------------------------------------------------------------------------
 
 265 void AUTH_STRESS::GetUsers()
 
 268 printfd(__FILE__, "users->OpenSearch() usernum=%d\n", users->GetUserNum());
 
 269 int h = users->OpenSearch();
 
 272     printfd(__FILE__, "users->OpenSearch() error\n");
 
 278     if (users->SearchNext(h, &u))
 
 282     usersList.push_back(u);
 
 286 users->CloseSearch(h);
 
 288 //-----------------------------------------------------------------------------
 
 289 void AUTH_STRESS::Unauthorize(USER_PTR u) const
 
 291 if (!u->IsAuthorizedBy(this))
 
 294 printfd(__FILE__, "Unauthorized user %s\n", u->GetLogin().c_str());
 
 295 u->Unauthorize(this);
 
 297 //-----------------------------------------------------------------------------
 
 298 void AUTH_STRESS::Authorize(USER_PTR u) const
 
 300 USER_IPS ips = u->GetProperty().ips;
 
 301 if (ips.OnlyOneIP() && !u->IsAuthorizedBy(this))
 
 303     if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
 
 305         printfd(__FILE__, "Authorized user %s\n", u->GetLogin().c_str());
 
 309 //-----------------------------------------------------------------------------
 
 310 void AUTH_STRESS::AddUser(USER_PTR u)
 
 312 //printfd(__FILE__, "User added to list %s\n", u->GetLogin().c_str());
 
 314 usersList.push_back(u);
 
 316 //-----------------------------------------------------------------------------
 
 317 void AUTH_STRESS::DelUser(USER_PTR u)
 
 320 UnSetUserNotifiers(u);
 
 322 list<USER_PTR>::iterator users_iter;
 
 323 users_iter = usersList.begin();
 
 325 while (users_iter != usersList.end())
 
 327     if (u == *users_iter)
 
 329         usersList.erase(users_iter);
 
 330         printfd(__FILE__, "User removed from list %s\n", u->GetLogin().c_str());
 
 336 //-----------------------------------------------------------------------------
 
 337 int AUTH_STRESS::SendMessage(const STG_MSG &, uint32_t) const
 
 339 errorStr = "Authorization modele \'AUTH_STRESS\' does not support sending messages";
 
 342 //-----------------------------------------------------------------------------
 
 343 void * AUTH_STRESS::Run(void * d)
 
 346 ia = (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 //-----------------------------------------------------------------------------