]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.h
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / users_impl.h
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.31 $
23 $Date: 2010/10/07 20:04:48 $
24 $Author: faust $
25 */
26
27
28 #ifndef USERS_IMPL_H
29 #define USERS_IMPL_H
30
31 #include <pthread.h>
32
33 #include <ctime>
34 #include <string>
35 #include <map>
36 #include <list>
37 #include <set>
38
39 #include "stg/os_int.h"
40 #include "stg/store.h"
41 #include "stg/users.h"
42 #include "stg/user.h"
43 #include "stg/tariffs.h"
44 #include "stg/logger.h"
45 #include "stg/notifer.h"
46 #include "stg/noncopyable.h"
47 #include "actions.h"
48 #include "eventloop.h"
49 #include "settings_impl.h"
50 #include "user_impl.h"
51
52 const int userDeleteDelayTime = 120;
53
54 typedef std::list<USER_IMPL>::iterator user_iter;
55 typedef std::list<USER_IMPL>::const_iterator const_user_iter;
56
57 class USERS_IMPL;
58 //-----------------------------------------------------------------------------
59 struct USER_TO_DEL {
60 USER_TO_DEL()
61     : iter(),
62       delTime(0)
63 {}
64
65 std::list<USER_IMPL>::iterator iter;
66 time_t  delTime;
67 };
68 //-----------------------------------------------------------------------------
69 class USERS_IMPL : private NONCOPYABLE, public USERS {
70     friend class PROPERTY_NOTIFER_IP_BEFORE;
71     friend class PROPERTY_NOTIFER_IP_AFTER;
72
73 public:
74     USERS_IMPL(SETTINGS_IMPL * s, STORE * store,
75                TARIFFS * tariffs, SERVICES & svcs,
76                const ADMIN * sysAdmin);
77     virtual ~USERS_IMPL();
78
79     int             FindByName(const std::string & login, USER_PTR * user);
80     int             FindByName(const std::string & login, CONST_USER_PTR * user) const;
81
82     bool            Exists(const std::string & login) const;
83
84     bool            TariffInUse(const std::string & tariffName) const;
85
86     void            AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
87     void            DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
88
89     void            AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
90     void            DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
91
92     void            AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> *);
93     void            DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> *);
94
95     void            AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> *);
96     void            DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> *);
97
98     int             Add(const std::string & login, const ADMIN * admin);
99     void            Del(const std::string & login, const ADMIN * admin);
100
101     bool            Authorize(const std::string & login, uint32_t ip,
102                               uint32_t enabledDirs, const AUTH * auth);
103     bool            Unauthorize(const std::string & login,
104                                 const AUTH * auth,
105                                 const std::string & reason = std::string());
106
107     int             ReadUsers();
108     size_t          Count() const { return users.size(); }
109
110     int             FindByIPIdx(uint32_t ip, USER_PTR * user) const;
111     int             FindByIPIdx(uint32_t ip, USER_IMPL ** user) const;
112     bool            IsIPInIndex(uint32_t ip) const;
113     bool            IsIPInUse(uint32_t ip, const std::string & login, CONST_USER_PTR * user) const;
114
115     int             OpenSearch();
116     int             SearchNext(int handler, USER_PTR * user);
117     int             SearchNext(int handler, USER_IMPL ** user);
118     int             CloseSearch(int handler);
119
120     int             Start();
121     int             Stop();
122
123 private:
124     USERS_IMPL(const USERS_IMPL & rvalue);
125     USERS_IMPL & operator=(const USERS_IMPL & rvalue);
126
127     void            AddToIPIdx(user_iter user);
128     void            DelFromIPIdx(uint32_t ip);
129     bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
130
131     int             FindByNameNonLock(const std::string & login, user_iter * user);
132     int             FindByNameNonLock(const std::string & login, const_user_iter * user) const;
133
134     void            RealDelUser();
135     void            ProcessActions();
136
137     void            AddUserIntoIndexes(user_iter user);
138     void            DelUserFromIndexes(user_iter user);
139
140     static void *   Run(void *);
141     void            NewMinute(const struct tm & t);
142     void            NewDay(const struct tm & t);
143     void            DayResetTraff(const struct tm & t);
144
145     bool            TimeToWriteDetailStat(const struct tm & t);
146
147     std::list<USER_IMPL>                  users;
148     std::list<USER_TO_DEL>                usersToDelete;
149
150     std::map<uint32_t, user_iter>         ipIndex;
151     std::map<std::string, user_iter>      loginIndex;
152
153     SETTINGS_IMPL *     settings;
154     TARIFFS *           tariffs;
155     SERVICES &          m_services;
156     STORE *             store;
157     const ADMIN *       sysAdmin;
158     STG_LOGGER &        WriteServLog;
159
160     bool                nonstop;
161     bool                isRunning;
162
163     mutable pthread_mutex_t mutex;
164     pthread_t               thread;
165     mutable unsigned int    handle;
166
167     mutable std::map<int, user_iter>  searchDescriptors;
168
169     std::set<NOTIFIER_BASE<USER_PTR>*> onAddNotifiers;
170     std::set<NOTIFIER_BASE<USER_PTR>*> onDelNotifiers;
171     std::set<NOTIFIER_BASE<USER_IMPL_PTR>*> onAddNotifiersImpl;
172     std::set<NOTIFIER_BASE<USER_IMPL_PTR>*> onDelNotifiersImpl;
173 };
174 //-----------------------------------------------------------------------------
175 /*inline
176 void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
177                                         const uint32_t &)
178 {
179 if (!oldValue)
180     return;
181
182 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::DelFromIPIdx, oldValue);
183 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
184 users.DelFromIPIdx(oldValue);
185 }
186 //-----------------------------------------------------------------------------
187 inline
188 void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &,
189                                        const uint32_t & newValue)
190 {
191 if (!newValue)
192     return;
193
194 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::AddToIPIdx, user);
195 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
196 users.AddToIPIdx(user);
197 }*/
198 //-----------------------------------------------------------------------------
199 #endif