]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.cpp
Check IP in USERS_IMPL::Authorize as early as possible
[stg.git] / projects / stargazer / users_impl.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  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.61 $
27  $Date: 2010/09/13 05:56:42 $
28  $Author: faust $
29  */
30
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34
35 #include <pthread.h>
36 #include <csignal>
37 #include <cassert>
38 #include <algorithm>
39 #include <utility>
40 #include <string>
41 #include <vector>
42
43 #include "stg/settings.h"
44 #include "stg/common.h"
45 #include "users_impl.h"
46 #include "stg_timer.h"
47
48 using namespace std;
49
50 extern const volatile time_t stgTime;
51
52 //#define USERS_DEBUG 1
53
54 //-----------------------------------------------------------------------------
55 USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st, TARIFFS * t, const ADMIN * sa)
56     : USERS(),
57       users(),
58       usersToDelete(),
59       /*userIPNotifiersBefore(),
60       userIPNotifiersAfter(),*/
61       ipIndex(),
62       loginIndex(),
63       settings(s),
64       tariffs(t),
65       store(st),
66       sysAdmin(sa),
67       WriteServLog(GetStgLogger()),
68       nonstop(false),
69       isRunning(false),
70       mutex(),
71       thread(),
72       handle(0),
73       searchDescriptors(),
74       onAddNotifiers(),
75       onDelNotifiers(),
76       onAddNotifiersImpl(),
77       onDelNotifiersImpl()
78 {
79 pthread_mutexattr_t attr;
80 pthread_mutexattr_init(&attr);
81 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
82 pthread_mutex_init(&mutex, &attr);
83 }
84 //-----------------------------------------------------------------------------
85 USERS_IMPL::~USERS_IMPL()
86 {
87 pthread_mutex_destroy(&mutex);
88 }
89 //-----------------------------------------------------------------------------
90 int USERS_IMPL::FindByNameNonLock(const string & login, user_iter * user)
91 {
92 map<string, user_iter>::iterator iter;
93 iter = loginIndex.find(login);
94 if (iter != loginIndex.end())
95     {
96     if (user)
97         *user = iter->second;
98     return 0;
99     }
100 return -1;
101 }
102 //-----------------------------------------------------------------------------
103 int USERS_IMPL::FindByName(const string & login, USER_PTR * user)
104 {
105 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
106 user_iter u;
107 int res = FindByNameNonLock(login, &u);
108 if (res)
109     return -1;
110 *user = &(*u);
111 return 0;
112 }
113 //-----------------------------------------------------------------------------
114 bool USERS_IMPL::TariffInUse(const string & tariffName) const
115 {
116 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
117 list<USER_IMPL>::const_iterator iter;
118 iter = users.begin();
119 while (iter != users.end())
120     {
121     if (iter->GetProperty().tariffName.Get() == tariffName)
122         return true;
123     ++iter;
124     }
125 return false;
126 }
127 //-----------------------------------------------------------------------------
128 int USERS_IMPL::Add(const string & login, const ADMIN * admin)
129 {
130 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
131 const PRIV * priv = admin->GetPriv();
132
133 if (!priv->userAddDel)
134     {
135     WriteServLog("%s tried to add user \'%s\'. Access denied.",
136          admin->GetLogStr().c_str(), login.c_str());
137     /*errorStr = "Admin \'" + admin->GetLogin() +
138                "\': tried to add user \'" + ud->login + "\'. Access denied.";*/
139     return -1;
140     }
141
142 //////
143 if (store->AddUser(login))
144     {
145     //TODO
146     //WriteServLog("Admin \'%s\': tried to add user \'%s\'. Access denied.",
147     //     admin->GetLogin().c_str(), ud->login.c_str());
148     return -1;
149     }
150 //////
151
152 USER_IMPL u(settings, store, tariffs, sysAdmin, this);
153
154 /*struct tm * tms;
155 time_t t = stgTime;
156
157 tms = localtime(&t);
158
159 tms->tm_hour = 0;
160 tms->tm_min = 0;
161 tms->tm_sec = 0;
162
163 if (settings->GetDayResetTraff() > tms->tm_mday)
164     tms->tm_mon -= 1;
165
166 tms->tm_mday = settings->GetDayResetTraff();*/
167
168 u.SetLogin(login);
169
170 u.SetPassiveTimeAsNewUser();
171
172 u.WriteConf();
173 u.WriteStat();
174
175 WriteServLog("%s User \'%s\' added.",
176          admin->GetLogStr().c_str(), login.c_str());
177
178 u.OnAdd();
179
180 users.push_front(u);
181
182 AddUserIntoIndexes(users.begin());
183 SetUserNotifiers(users.begin());
184
185     {
186     // Fire all "on add" notifiers
187     set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onAddNotifiers.begin();
188     while (ni != onAddNotifiers.end())
189         {
190         (*ni)->Notify(&users.front());
191         ++ni;
192         }
193     }
194
195     {
196     // Fire all "on add" implementation notifiers
197     set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onAddNotifiersImpl.begin();
198     while (ni != onAddNotifiersImpl.end())
199         {
200         (*ni)->Notify(&users.front());
201         ++ni;
202         }
203     }
204
205 return 0;
206 }
207 //-----------------------------------------------------------------------------
208 void USERS_IMPL::Del(const string & login, const ADMIN * admin)
209 {
210 const PRIV * priv = admin->GetPriv();
211 user_iter u;
212
213 if (!priv->userAddDel)
214     {
215     WriteServLog("%s tried to remove user \'%s\'. Access denied.",
216          admin->GetLogStr().c_str(), login.c_str());
217     return;
218     }
219
220
221     {
222     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
223
224     if (FindByNameNonLock(login, &u))
225         {
226         WriteServLog("%s tried to delete user \'%s\': not found.",
227                      admin->GetLogStr().c_str(),
228                      login.c_str());
229         return;
230         }
231
232     u->SetDeleted();
233     }
234
235     {
236     set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onDelNotifiers.begin();
237     while (ni != onDelNotifiers.end())
238         {
239         (*ni)->Notify(&(*u));
240         ++ni;
241         }
242     }
243
244     {
245     set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onDelNotifiersImpl.begin();
246     while (ni != onDelNotifiersImpl.end())
247         {
248         (*ni)->Notify(&(*u));
249         ++ni;
250         }
251     }
252
253     {
254     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
255
256     u->OnDelete();
257
258     USER_TO_DEL utd;
259     utd.iter = u;
260     utd.delTime = stgTime;
261     usersToDelete.push_back(utd);
262
263     UnSetUserNotifiers(u);
264     DelUserFromIndexes(u);
265
266     WriteServLog("%s User \'%s\' deleted.",
267              admin->GetLogStr().c_str(), login.c_str());
268
269     }
270 }
271 //-----------------------------------------------------------------------------
272 bool USERS_IMPL::Authorize(const std::string & login, uint32_t ip,
273                            uint32_t enabledDirs, const AUTH * auth)
274 {
275 user_iter iter;
276 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
277 if (FindByNameNonLock(login, &iter))
278     {
279     WriteServLog("Attempt to authorize non-existant user '%s'", login.c_str());
280     return false;
281     }
282
283 if (FindByIPIdx(ip, iter))
284     {
285     if (iter->GetLogin() != login)
286         {
287         WriteServLog("Attempt to authorize user '%s' from ip %s which already occupied by '%s'",
288                      login.c_str(), inet_ntostring(ip).c_str(),
289                      iter->GetLogin().c_str());
290         return false;
291         }
292     if (iter->Authorize(ip, enabledDirs, auth))
293         return false;
294     return true;
295     }
296
297 if (iter->Authorize(ip, enabledDirs, auth))
298     return false;
299
300 AddToIPIdx(iter);
301 return true;
302 }
303 //-----------------------------------------------------------------------------
304 bool USERS_IMPL::Unauthorize(const std::string & login, const AUTH * auth)
305 {
306 user_iter iter;
307 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
308 if (FindByNameNonLock(login, &iter))
309     {
310     WriteServLog("Attempt to unauthorize non-existant user '%s'", login.c_str());
311     return false;
312     }
313
314 uint32_t ip = iter->GetCurrIP();
315
316 iter->Unauthorize(auth);
317
318 if (!iter->GetAuthorized())
319     DelFromIPIdx(ip);
320
321 return true;
322 }
323 //-----------------------------------------------------------------------------
324 int USERS_IMPL::ReadUsers()
325 {
326 vector<string> usersList;
327 usersList.clear();
328 if (store->GetUsersList(&usersList) < 0)
329     {
330     WriteServLog(store->GetStrError().c_str());
331     return -1;
332     }
333
334 user_iter ui;
335
336 for (unsigned int i = 0; i < usersList.size(); i++)
337     {
338     USER_IMPL u(settings, store, tariffs, sysAdmin, this);
339
340     u.SetLogin(usersList[i]);
341     users.push_front(u);
342     ui = users.begin();
343
344     AddUserIntoIndexes(ui);
345     SetUserNotifiers(ui);
346
347     if (ui->ReadConf() < 0)
348         return -1;
349
350     if (ui->ReadStat() < 0)
351         return -1;
352     }
353
354 return 0;
355 }
356 //-----------------------------------------------------------------------------
357 void * USERS_IMPL::Run(void * d)
358 {
359 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
360 USERS_IMPL * us = (USERS_IMPL*) d;
361
362 struct tm t;
363 time_t tt = stgTime;
364 localtime_r(&tt, &t);
365
366 int min = t.tm_min;
367 int day = t.tm_mday;
368
369 printfd(__FILE__,"Day = %d Min = %d\n", day, min);
370
371 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
372 string monFile = us->settings->GetMonitorDir() + "/users_r";
373 printfd(__FILE__, "Monitor=%d file USERS %s\n", us->settings->GetMonitoring(), monFile.c_str());
374
375 us->isRunning = true;
376 while (us->nonstop)
377     {
378     //printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
379     //printfd(__FILE__,"New Day.    old = %2d current = %2d\n", day, t->tm_mday);
380
381     for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER_IMPL::Run));
382
383     tt = stgTime;
384     localtime_r(&tt, &t);
385
386     if (min != t.tm_min)
387         {
388         printfd(__FILE__,"Sec = %d\n", stgTime);
389         printfd(__FILE__,"New Minute. old = %d current = %d\n", min, t.tm_min);
390         min = t.tm_min;
391
392         us->NewMinute(t);
393         }
394
395     if (day != t.tm_mday)
396         {
397         printfd(__FILE__,"Sec = %d\n", stgTime);
398         printfd(__FILE__,"New Day. old = %d current = %d\n", day, t.tm_mday);
399         day = t.tm_mday;
400         us->NewDay(t);
401         }
402
403     if (us->settings->GetMonitoring() && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
404         {
405         //printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
406         touchTime = stgTime;
407         TouchFile(monFile.c_str());
408         }
409
410     stgUsleep(100000);
411     } //while (us->nonstop)
412
413 user_iter ui = us->users.begin();
414 while (ui != us->users.end())
415     {
416     us->UnSetUserNotifiers(ui);
417     us->DelUserFromIndexes(ui);
418     ++ui;
419     }
420
421 list<USER_TO_DEL>::iterator iter;
422 iter = us->usersToDelete.begin();
423 while (iter != us->usersToDelete.end())
424     {
425     iter->delTime -= 2 * userDeleteDelayTime;
426     ++iter;
427     }
428 us->RealDelUser();
429
430 us->isRunning = false;
431
432 return NULL;
433 }
434 //-----------------------------------------------------------------------------
435 void USERS_IMPL::NewMinute(const struct tm & t)
436 {
437 //Write traff, reset session traff. Fake disconnect-connect
438 if (t.tm_hour == 23 && t.tm_min == 59)
439     {
440     printfd(__FILE__,"MidnightResetSessionStat\n");
441     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::MidnightResetSessionStat));
442     }
443
444 if (TimeToWriteDetailStat(t))
445     {
446     //printfd(__FILE__, "USER::WriteInetStat\n");
447     int usersCnt = 0;
448
449     // ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run
450     list<USER_IMPL>::iterator usr = users.begin();
451     while (usr != users.end())
452         {
453         usersCnt++;
454         usr->WriteDetailStat();
455         ++usr;
456         if (usersCnt % 10 == 0)
457             for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
458         }
459     }
460
461 RealDelUser();
462 }
463 //-----------------------------------------------------------------------------
464 void USERS_IMPL::NewDay(const struct tm & t)
465 {
466 struct tm t1;
467 time_t tt = stgTime;
468 localtime_r(&tt, &t1);
469 int dayFee = settings->GetDayFee();
470
471 if (dayFee == 0)
472     dayFee = DaysInCurrentMonth();
473
474 printfd(__FILE__, "DayFee = %d\n", dayFee);
475 printfd(__FILE__, "Today = %d DayResetTraff = %d\n", t1.tm_mday, settings->GetDayResetTraff());
476 printfd(__FILE__, "DayFeeIsLastDay = %d\n", settings->GetDayFeeIsLastDay());
477
478 if (!settings->GetDayFeeIsLastDay())
479     {
480     printfd(__FILE__, "DayResetTraff - 1 -\n");
481     DayResetTraff(t1);
482     //printfd(__FILE__, "DayResetTraff - 1 - 1 -\n");
483     }
484
485 if (settings->GetSpreadFee())
486     {
487     printfd(__FILE__, "Spread DayFee\n");
488     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFeeSpread));
489     }
490 else
491     {
492     if (t.tm_mday == dayFee)
493         {
494         printfd(__FILE__, "DayFee\n");
495         for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFee));
496         }
497     }
498
499 if (settings->GetDayFeeIsLastDay())
500     {
501     printfd(__FILE__, "DayResetTraff - 2 -\n");
502     DayResetTraff(t1);
503     }
504 }
505 //-----------------------------------------------------------------------------
506 void USERS_IMPL::DayResetTraff(const struct tm & t1)
507 {
508 int dayResetTraff = settings->GetDayResetTraff();
509 if (dayResetTraff == 0)
510     dayResetTraff = DaysInCurrentMonth();
511 if (t1.tm_mday == dayResetTraff)
512     {
513     printfd(__FILE__, "ResetTraff\n");
514     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessNewMonth));
515     //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
516     }
517 }
518 //-----------------------------------------------------------------------------
519 int USERS_IMPL::Start()
520 {
521 if (ReadUsers())
522     {
523     WriteServLog("USERS: Error: Cannot read users!");
524     return -1;
525     }
526
527 nonstop = true;
528 if (pthread_create(&thread, NULL, Run, this))
529     {
530     WriteServLog("USERS: Error: Cannot start thread!");
531     return -1;
532     }
533 return 0;
534 }
535 //-----------------------------------------------------------------------------
536 int USERS_IMPL::Stop()
537 {
538 printfd(__FILE__, "USERS::Stop()\n");
539
540 if (!isRunning)
541     {
542     //printfd(__FILE__, "Alredy stopped\n");
543     return 0;
544     }
545
546 nonstop = false;
547
548 //5 seconds to thread stops itself
549 struct timespec ts = {0, 200000000};
550 for (size_t i = 0; i < 25 * (users.size() / 50 + 1); i++)
551     {
552     if (!isRunning)
553         break;
554
555     nanosleep(&ts, NULL);
556     }
557
558 //after 5 seconds waiting thread still running. now kill it
559 if (isRunning)
560     {
561     printfd(__FILE__, "kill USERS thread.\n");
562     //TODO pthread_cancel()
563     if (pthread_kill(thread, SIGINT))
564         {
565         //errorStr = "Cannot kill USERS thread.";
566         //printfd(__FILE__, "Cannot kill USERS thread.\n");
567         //return 0;
568         }
569     printfd(__FILE__, "USERS killed\n");
570     }
571
572 printfd(__FILE__, "Before USERS::Run()\n");
573 for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
574
575 // 'cause bind2st accepts only constant first param
576 for (list<USER_IMPL>::iterator it = users.begin();
577      it != users.end();
578      ++it)
579     it->WriteDetailStat(true);
580
581 for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteStat));
582 //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
583
584 printfd(__FILE__, "USERS::Stop()\n");
585 return 0;
586 }
587 //-----------------------------------------------------------------------------
588 void USERS_IMPL::RealDelUser()
589 {
590 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
591
592 printfd(__FILE__, "RealDelUser() users to del: %d\n", usersToDelete.size());
593
594 list<USER_TO_DEL>::iterator iter;
595 iter = usersToDelete.begin();
596 while (iter != usersToDelete.end())
597     {
598     printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
599     if (iter->delTime + userDeleteDelayTime < stgTime)
600         {
601         printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
602         if (store->DelUser(iter->iter->GetLogin()))
603             {
604             WriteServLog("Error removing user \'%s\' from database.", iter->iter->GetLogin().c_str());
605             }
606         users.erase(iter->iter);
607         usersToDelete.erase(iter++);
608         }
609     else
610         {
611         ++iter;
612         }
613     }
614 return;
615 }
616 //-----------------------------------------------------------------------------
617 void USERS_IMPL::AddToIPIdx(user_iter user)
618 {
619 printfd(__FILE__, "USERS: Add IP Idx\n");
620 uint32_t ip = user->GetCurrIP();
621 //assert(ip && "User has non-null ip");
622 if (!ip)
623     return; // User has disconnected
624
625 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
626
627 const map<uint32_t, user_iter>::iterator it(
628         ipIndex.lower_bound(ip)
629 );
630
631 assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
632
633 ipIndex.insert(it, std::make_pair(ip, user));
634 }
635 //-----------------------------------------------------------------------------
636 void USERS_IMPL::DelFromIPIdx(uint32_t ip)
637 {
638 printfd(__FILE__, "USERS: Del IP Idx\n");
639 assert(ip && "User has non-null ip");
640
641 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
642
643 const map<uint32_t, user_iter>::iterator it(
644         ipIndex.find(ip)
645 );
646
647 if (it == ipIndex.end())
648     return;
649
650 ipIndex.erase(it);
651 }
652 //-----------------------------------------------------------------------------
653 bool USERS_IMPL::FindByIPIdx(uint32_t ip, user_iter & iter) const
654 {
655 map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
656 if (it == ipIndex.end())
657     return false;
658 iter = it->second;
659 return true;
660 }
661 //-----------------------------------------------------------------------------
662 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const
663 {
664 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
665
666 user_iter iter;
667 if (FindByIPIdx(ip, iter))
668     {
669     *usr = &(*iter);
670     return 0;
671     }
672
673 return -1;
674 }
675 //-----------------------------------------------------------------------------
676 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_IMPL ** usr) const
677 {
678 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
679
680 user_iter iter;
681 if (FindByIPIdx(ip, iter))
682     {
683     *usr = &(*iter);
684     return 0;
685     }
686
687 return -1;
688 }
689 //-----------------------------------------------------------------------------
690 bool USERS_IMPL::IsIPInIndex(uint32_t ip) const
691 {
692 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
693
694 map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
695
696 return it != ipIndex.end();
697 }
698 //-----------------------------------------------------------------------------
699 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
700 {
701 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
702 onAddNotifiers.insert(n);
703 }
704 //-----------------------------------------------------------------------------
705 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
706 {
707 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
708 onAddNotifiers.erase(n);
709 }
710 //-----------------------------------------------------------------------------
711 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
712 {
713 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
714 onDelNotifiers.insert(n);
715 }
716 //-----------------------------------------------------------------------------
717 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
718 {
719 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
720 onDelNotifiers.erase(n);
721 }
722 //-----------------------------------------------------------------------------
723 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
724 {
725 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
726 onAddNotifiersImpl.insert(n);
727 }
728 //-----------------------------------------------------------------------------
729 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
730 {
731 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
732 onAddNotifiersImpl.erase(n);
733 }
734 //-----------------------------------------------------------------------------
735 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
736 {
737 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
738 onDelNotifiersImpl.insert(n);
739 }
740 //-----------------------------------------------------------------------------
741 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
742 {
743 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
744 onDelNotifiersImpl.erase(n);
745 }
746 //-----------------------------------------------------------------------------
747 int USERS_IMPL::OpenSearch()
748 {
749 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
750 handle++;
751 searchDescriptors[handle] = users.begin();
752 return handle;
753 }
754 //-----------------------------------------------------------------------------
755 int USERS_IMPL::SearchNext(int h, USER_PTR * user)
756 {
757     USER_IMPL * ptr = NULL;
758     if (SearchNext(h, &ptr))
759         return -1;
760     *user = ptr;
761     return 0;
762 }
763 //-----------------------------------------------------------------------------
764 int USERS_IMPL::SearchNext(int h, USER_IMPL ** user)
765 {
766 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
767
768 if (searchDescriptors.find(h) == searchDescriptors.end())
769     {
770     WriteServLog("USERS. Incorrect search handle.");
771     return -1;
772     }
773
774 if (searchDescriptors[h] == users.end())
775     return -1;
776
777 while (searchDescriptors[h]->GetDeleted())
778     {
779     ++searchDescriptors[h];
780     if (searchDescriptors[h] == users.end())
781         {
782         return -1;
783         }
784     }
785
786 *user = &(*searchDescriptors[h]);
787
788 ++searchDescriptors[h];
789
790 return 0;
791 }
792 //-----------------------------------------------------------------------------
793 int USERS_IMPL::CloseSearch(int h)
794 {
795 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
796 if (searchDescriptors.find(h) != searchDescriptors.end())
797     {
798     searchDescriptors.erase(searchDescriptors.find(h));
799     return 0;
800     }
801
802 WriteServLog("USERS. Incorrect search handle.");
803 return -1;
804 }
805 //-----------------------------------------------------------------------------
806 void USERS_IMPL::SetUserNotifiers(user_iter user)
807 {
808 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
809
810 /*PROPERTY_NOTIFER_IP_BEFORE nb(*this, user);
811 PROPERTY_NOTIFER_IP_AFTER  na(*this, user);
812
813 userIPNotifiersBefore.push_front(nb);
814 userIPNotifiersAfter.push_front(na);
815
816 user->AddCurrIPBeforeNotifier(&(*userIPNotifiersBefore.begin()));
817 user->AddCurrIPAfterNotifier(&(*userIPNotifiersAfter.begin()));*/
818 }
819 //-----------------------------------------------------------------------------
820 void USERS_IMPL::UnSetUserNotifiers(user_iter user)
821 {
822 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
823
824 /*list<PROPERTY_NOTIFER_IP_BEFORE>::iterator  bi;
825 list<PROPERTY_NOTIFER_IP_AFTER>::iterator   ai;
826
827 bi = userIPNotifiersBefore.begin();
828 while (bi != userIPNotifiersBefore.end())
829     {
830     if (bi->GetUser() == user)
831         {
832         bi->GetUser()->DelCurrIPBeforeNotifier(&(*bi));
833         userIPNotifiersBefore.erase(bi);
834         //printfd(__FILE__, "Notifier Before removed. User %s\n", bi->GetUser()->GetLogin().c_str());
835         break;
836         }
837     ++bi;
838     }
839
840 ai = userIPNotifiersAfter.begin();
841 while (ai != userIPNotifiersAfter.end())
842     {
843     if (ai->GetUser() == user)
844         {
845         ai->GetUser()->DelCurrIPAfterNotifier(&(*ai));
846         userIPNotifiersAfter.erase(ai);
847         //printfd(__FILE__, "Notifier After removed. User %s\n", ai->GetUser()->GetLogin().c_str());
848         break;
849         }
850     ++ai;
851     }*/
852 }
853 //-----------------------------------------------------------------------------
854 void USERS_IMPL::AddUserIntoIndexes(user_iter user)
855 {
856 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
857 loginIndex.insert(make_pair(user->GetLogin(), user));
858 }
859 //-----------------------------------------------------------------------------
860 void USERS_IMPL::DelUserFromIndexes(user_iter user)
861 {
862 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
863 loginIndex.erase(user->GetLogin());
864 }
865 //-----------------------------------------------------------------------------
866 bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t)
867 {
868 int statTime = settings->GetDetailStatWritePeriod();
869
870 switch (statTime)
871     {
872     case dsPeriod_1:
873         if (t.tm_min == 0)
874             return true;
875         break;
876     case dsPeriod_1_2:
877         if (t.tm_min % 30 == 0)
878             return true;
879         break;
880     case dsPeriod_1_4:
881         if (t.tm_min % 15 == 0)
882             return true;
883         break;
884     case dsPeriod_1_6:
885         if (t.tm_min % 10 == 0)
886             return true;
887         break;
888     }
889 return false;
890 }