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