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