]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/inetaccess/inetaccess.h
Exclude DEL_USER_NOTIFIER from AUTH_IA (possible freandship fix)
[stg.git] / projects / stargazer / plugins / authorization / inetaccess / inetaccess.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.34 $
23  $Date: 2010/09/10 06:39:19 $
24  $Author: faust $
25  */
26
27 #ifndef INETACCESS_H
28 #define INETACCESS_H
29
30 #include <sys/time.h>
31 #include <pthread.h>
32
33 #include <cstring>
34 #include <ctime>
35 #include <string>
36 #include <map>
37 #include <list>
38 #include <functional>
39 #include <utility>
40
41 #include "os_int.h"
42 #include "auth.h"
43 #include "store.h"
44 #include "notifer.h"
45 #include "user_ips.h"
46 #include "user.h"
47 #include "users.h"
48 #include "ia_packets.h"
49 #include "blowfish.h"
50 #include "stg_logger.h"
51 #include "utime.h"
52
53 extern "C" PLUGIN * GetPlugin();
54
55 #define IA_PROTO_VER    (6)
56
57 //#define IA_DEBUG (1)
58 //#define IA_PHASE_DEBUG (1)
59
60 class AUTH_IA;
61 //-----------------------------------------------------------------------------
62 enum FREEMB {
63     freeMb0 = 0,
64     freeMb1,
65     freeMb2,
66     freeMb3,
67     freeMb4,
68     freeMb5,
69     freeMb6,
70     freeMb7,
71     freeMb8,
72     freeMb9,
73     freeMb10,
74     freeMb11,
75     freeMb12,
76     freeMb13,
77     freeMb14,
78     freeMb15,
79     freeMb16,
80     freeMb17,
81     freeMb18,
82     freeMb19,
83     freeMbCash = 100,
84     freeMbNone = 101
85 };
86 //-----------------------------------------------------------------------------
87 class IA_PHASE {
88 public:
89     IA_PHASE();
90     ~IA_PHASE();
91
92     void    SetPhase1();
93     void    SetPhase2();
94     void    SetPhase3();
95     void    SetPhase4();
96     void    SetPhase5();
97     int     GetPhase() const;
98
99     void    UpdateTime();
100     const UTIME & GetTime() const;
101
102     #ifdef IA_PHASE_DEBUG
103     void    SetUserLogin(const std::string & login);
104     void    SetLogFileName(const std::string & logFileName);
105     #endif
106
107 private:
108     int             phase;
109     UTIME           phaseTime;
110
111     #ifdef IA_PHASE_DEBUG
112     void WritePhaseChange(int newPhase);
113     std::string log;
114     std::string login;
115     FILE * flog;
116     #endif
117 };
118 //-----------------------------------------------------------------------------
119 struct IA_USER {
120     IA_USER()
121         : lastSendAlive(0),
122           rnd(random()),
123           port(0),
124           protoVer(0),
125           password("NO PASSWORD")
126     {
127         // +++ Preparing CTX +++
128         unsigned char keyL[PASSWD_LEN];
129         memset(keyL, 0, PASSWD_LEN);
130         strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
131         Blowfish_Init(&ctx, keyL, PASSWD_LEN);
132         // --- Preparing CTX ---
133         #ifdef IA_DEBUG
134         aliveSent = false;
135         #endif
136     };
137
138     IA_USER(const IA_USER & u)
139         : user(u.user),
140           phase(u.phase),
141           lastSendAlive(u.lastSendAlive),
142           rnd(u.rnd),
143           port(u.port),
144           messagesToSend(u.messagesToSend),
145           protoVer(u.protoVer),
146           password(u.password)
147     {
148         #ifdef IA_DEBUG
149         aliveSent  = u.aliveSent;
150         #endif
151         memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
152     };
153
154     USER_PTR        user;
155     IA_PHASE        phase;
156     UTIME           lastSendAlive;
157     uint32_t        rnd;
158     uint16_t        port;
159     BLOWFISH_CTX    ctx;
160     std::list<STG_MSG> messagesToSend;
161     int             protoVer;
162     std::string     password;
163     #ifdef IA_DEBUG
164     bool            aliveSent;
165     #endif
166 };
167 //-----------------------------------------------------------------------------
168 class AUTH_IA_SETTINGS {
169 public:
170                     AUTH_IA_SETTINGS();
171     virtual         ~AUTH_IA_SETTINGS() {};
172     const std::string & GetStrError() const { return errorStr; };
173     int             ParseSettings(const MODULE_SETTINGS & s);
174     int             GetUserDelay() const { return userDelay; };
175     int             GetUserTimeout() const { return userTimeout; };
176     int             GetUserPort() const { return port; };
177     FREEMB          GetFreeMbShowType() const { return freeMbShowType; };
178
179 private:
180     int             ParseIntInRange(const std::string & str, int min, int max, int * val);
181     int             userDelay;
182     int             userTimeout;
183     uint16_t        port;
184     std::string     errorStr;
185     FREEMB          freeMbShowType;
186 };
187 //-----------------------------------------------------------------------------
188 class AUTH_IA;
189 //-----------------------------------------------------------------------------
190 class DEL_USER_NOTIFIER: public NOTIFIER_BASE<USER_PTR> {
191 public:
192     DEL_USER_NOTIFIER(AUTH_IA & a) : auth(a) {}
193     virtual ~DEL_USER_NOTIFIER() {}
194
195     void Notify(const USER_PTR & user);
196 private:
197     AUTH_IA & auth;
198 };
199 //-----------------------------------------------------------------------------
200 class AUTH_IA :public AUTH {
201 friend class DEL_USER_NOTIFIER;
202 public:
203                         AUTH_IA();
204     virtual             ~AUTH_IA();
205
206     void                SetUsers(USERS * u) { users = u; }
207     void                SetTariffs(TARIFFS *) {}
208     void                SetAdmins(ADMINS *) {}
209     void                SetTraffcounter(TRAFFCOUNTER *) {}
210     void                SetStore(STORE *) {}
211     void                SetStgSettings(const SETTINGS * s) { stgSettings = s; }
212     void                SetSettings(const MODULE_SETTINGS & s) { settings = s; }
213     int                 ParseSettings();
214
215     int                 Start();
216     int                 Stop();
217     int                 Reload() { return 0; }
218     bool                IsRunning() { return isRunningRunTimeouter || isRunningRun; }
219
220     const std::string & GetStrError() const { return errorStr; }
221     const std::string   GetVersion() const { return "InetAccess authorization plugin v.1.4"; }
222     uint16_t            GetStartPosition() const { return 50; }
223     uint16_t            GetStopPosition() const { return 50; }
224
225     int                 SendMessage(const STG_MSG & msg, uint32_t ip) const;
226
227 private:
228     static void *       Run(void *);
229     static void *       RunTimeouter(void * d);
230     int                 PrepareNet();
231     int                 FinalizeNet();
232     void                DelUser(USER_PTR u);
233     int                 RecvData(char * buffer, int bufferSize);
234     int                 CheckHeader(const char * buffer, int * protoVer);
235     int                 PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR * user);
236
237     int                 Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, uint32_t sip);
238     int                 Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip);
239     int                 Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip);
240
241     int                 Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip);
242     int                 Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip);
243     int                 Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip);
244
245     int                 Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t sip);
246     int                 Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip);
247     int                 Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t sip);
248
249     int                 Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, uint32_t sip);
250     int                 Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip);
251     int                 Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, uint32_t sip);
252
253     int                 Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
254                                               IA_USER * iaUser,
255                                               uint32_t sip,
256                                               map<uint32_t, IA_USER>::iterator it);
257     int                 Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
258                                               IA_USER * iaUser,
259                                               uint32_t sip,
260                                               map<uint32_t, IA_USER>::iterator it);
261     int                 Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
262                                               IA_USER * iaUser,
263                                               uint32_t sip,
264                                               map<uint32_t, IA_USER>::iterator it);
265
266     int                 Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
267     int                 Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
268     int                 Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
269
270     int                 Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
271     int                 Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
272     int                 Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
273
274     int                 Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
275     int                 Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
276     int                 Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
277
278     int                 Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
279     int                 Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
280     int                 Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
281
282     int                 Timeouter();
283
284     int                 SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text);
285     int                 Send(uint32_t ip, uint16_t port, const char * buffer, int len);
286     int                 RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user);
287     int                 RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
288     int                 RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
289
290     bool                WaitPackets(int sd) const;
291
292     BLOWFISH_CTX        ctxS;        //for loginS
293
294     mutable std::string errorStr;
295     AUTH_IA_SETTINGS    iaSettings;
296     MODULE_SETTINGS     settings;
297
298     bool                nonstop;
299
300     bool                isRunningRun;
301     bool                isRunningRunTimeouter;
302
303     USERS *             users;
304     const SETTINGS *    stgSettings;
305
306     mutable std::map<uint32_t, IA_USER> ip2user;
307
308     pthread_t           recvThread;
309     pthread_t           timeouterThread;
310     mutable pthread_mutex_t mutex;
311
312     int                 listenSocket;
313
314     CONN_SYN_ACK_6      connSynAck6;
315     CONN_SYN_ACK_8      connSynAck8;
316
317     DISCONN_SYN_ACK_6   disconnSynAck6;
318     DISCONN_SYN_ACK_8   disconnSynAck8;
319
320     ALIVE_SYN_6         aliveSyn6;
321     ALIVE_SYN_8         aliveSyn8;
322     FIN_6               fin6;
323     FIN_8               fin8;
324
325     std::map<std::string, int> packetTypes;
326
327     STG_LOGGER &        WriteServLog;
328
329     uint32_t            enabledDirs;
330
331     DEL_USER_NOTIFIER   onDelUserNotifier;
332
333     class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
334         public:
335             UnauthorizeUser(AUTH_IA * a) : auth(a) {}
336             void operator()(const std::pair<uint32_t, IA_USER> & p)
337             {
338                 p.second.user->Unauthorize(auth);
339             }
340         private:
341             AUTH_IA * auth;
342     };
343
344 };
345 //-----------------------------------------------------------------------------
346 inline
347 void DEL_USER_NOTIFIER::Notify(const USER_PTR & user)
348 {
349     auth.DelUser(user);
350 }
351
352 #endif