]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/inetaccess/inetaccess.h
Hide or add proper copy ctor and assignement operator, initialize
[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
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         : login(),
122           user(NULL),
123           phase(),
124           lastSendAlive(0),
125           rnd(random()),
126           port(0),
127           ctx(),
128           messagesToSend(),
129           protoVer(0),
130           password("NO PASSWORD")
131     {
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
137     #ifdef IA_DEBUG
138     aliveSent = false;
139     #endif
140     };
141
142     IA_USER(const IA_USER & u)
143         : login(u.login),
144           user(u.user),
145           phase(u.phase),
146           lastSendAlive(u.lastSendAlive),
147           rnd(u.rnd),
148           port(u.port),
149           ctx(),
150           messagesToSend(u.messagesToSend),
151           protoVer(u.protoVer),
152           password(u.password)
153     {
154     #ifdef IA_DEBUG
155     aliveSent  = u.aliveSent;
156     #endif
157     memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
158     };
159
160     IA_USER(const std::string & l,
161             CONST_USER_PTR u,
162             uint16_t p,
163             int ver)
164         : login(l),
165           user(u),
166           phase(),
167           lastSendAlive(0),
168           rnd(random()),
169           port(p),
170           ctx(),
171           messagesToSend(),
172           protoVer(ver),
173           password(user->GetProperty().password.Get())
174     {
175     unsigned char keyL[PASSWD_LEN];
176     memset(keyL, 0, PASSWD_LEN);
177     strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
178     Blowfish_Init(&ctx, keyL, PASSWD_LEN);
179
180     #ifdef IA_DEBUG
181     aliveSent = false;
182     #endif
183     }
184
185     std::string     login;
186     CONST_USER_PTR  user;
187     IA_PHASE        phase;
188     UTIME           lastSendAlive;
189     uint32_t        rnd;
190     uint16_t        port;
191     BLOWFISH_CTX    ctx;
192     std::list<STG_MSG> messagesToSend;
193     int             protoVer;
194     std::string     password;
195     #ifdef IA_DEBUG
196     bool            aliveSent;
197     #endif
198
199 private:
200     IA_USER & operator=(const IA_USER & rvalue);
201 };
202 //-----------------------------------------------------------------------------
203 class AUTH_IA_SETTINGS {
204 public:
205                     AUTH_IA_SETTINGS();
206     virtual         ~AUTH_IA_SETTINGS() {};
207     const std::string & GetStrError() const { return errorStr; };
208     int             ParseSettings(const MODULE_SETTINGS & s);
209     int             GetUserDelay() const { return userDelay; };
210     int             GetUserTimeout() const { return userTimeout; };
211     int             GetUserPort() const { return port; };
212     FREEMB          GetFreeMbShowType() const { return freeMbShowType; };
213
214 private:
215     int             userDelay;
216     int             userTimeout;
217     uint16_t        port;
218     std::string     errorStr;
219     FREEMB          freeMbShowType;
220 };
221 //-----------------------------------------------------------------------------
222 class AUTH_IA;
223 //-----------------------------------------------------------------------------
224 class DEL_USER_NOTIFIER: public NOTIFIER_BASE<USER_PTR> {
225 public:
226     DEL_USER_NOTIFIER(AUTH_IA & a) : auth(a) {}
227     virtual ~DEL_USER_NOTIFIER() {}
228
229     void Notify(const USER_PTR & user);
230 private:
231     DEL_USER_NOTIFIER(const DEL_USER_NOTIFIER & rvalue);
232     DEL_USER_NOTIFIER & operator=(const DEL_USER_NOTIFIER & rvalue);
233
234     AUTH_IA & auth;
235 };
236 //-----------------------------------------------------------------------------
237 class AUTH_IA :public AUTH {
238 friend class DEL_USER_NOTIFIER;
239 public:
240                         AUTH_IA();
241     virtual             ~AUTH_IA();
242
243     void                SetUsers(USERS * u) { users = u; }
244     void                SetStgSettings(const SETTINGS * s) { stgSettings = s; }
245     void                SetSettings(const MODULE_SETTINGS & s) { settings = s; }
246     int                 ParseSettings();
247
248     int                 Start();
249     int                 Stop();
250     int                 Reload() { return 0; }
251     bool                IsRunning() { return isRunningRunTimeouter || isRunningRun; }
252
253     const std::string & GetStrError() const { return errorStr; }
254     const std::string   GetVersion() const { return "InetAccess authorization plugin v.1.4"; }
255     uint16_t            GetStartPosition() const { return 50; }
256     uint16_t            GetStopPosition() const { return 50; }
257
258     int                 SendMessage(const STG_MSG & msg, uint32_t ip) const;
259
260 private:
261     AUTH_IA(const AUTH_IA & rvalue);
262     AUTH_IA & operator=(const AUTH_IA & rvalue);
263
264     static void *       Run(void *);
265     static void *       RunTimeouter(void * d);
266     int                 PrepareNet();
267     int                 FinalizeNet();
268     void                DelUser(USER_PTR u);
269     int                 RecvData(char * buffer, int bufferSize);
270     int                 CheckHeader(const char * buffer, int * protoVer);
271     int                 PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user);
272
273     int                 Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, uint32_t sip);
274     int                 Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip);
275     int                 Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip);
276
277     int                 Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip);
278     int                 Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip);
279     int                 Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip);
280
281     int                 Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t sip);
282     int                 Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip);
283     int                 Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t sip);
284
285     int                 Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, uint32_t sip);
286     int                 Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip);
287     int                 Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, uint32_t sip);
288
289     int                 Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
290                                               IA_USER * iaUser,
291                                               uint32_t sip,
292                                               map<uint32_t, IA_USER>::iterator it);
293     int                 Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
294                                               IA_USER * iaUser,
295                                               uint32_t sip,
296                                               map<uint32_t, IA_USER>::iterator it);
297     int                 Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
298                                               IA_USER * iaUser,
299                                               uint32_t sip,
300                                               map<uint32_t, IA_USER>::iterator it);
301
302     int                 Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
303     int                 Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
304     int                 Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
305
306     int                 Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
307     int                 Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
308     int                 Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
309
310     int                 Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
311     int                 Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
312     int                 Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
313
314     int                 Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
315     int                 Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
316     int                 Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
317
318     int                 Timeouter();
319
320     int                 SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text);
321     int                 Send(uint32_t ip, uint16_t port, const char * buffer, int len);
322     int                 RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user);
323     int                 RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
324     int                 RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
325
326     BLOWFISH_CTX        ctxS;        //for loginS
327
328     mutable std::string errorStr;
329     AUTH_IA_SETTINGS    iaSettings;
330     MODULE_SETTINGS     settings;
331
332     bool                nonstop;
333
334     bool                isRunningRun;
335     bool                isRunningRunTimeouter;
336
337     USERS *             users;
338     const SETTINGS *    stgSettings;
339
340     mutable std::map<uint32_t, IA_USER> ip2user;
341
342     pthread_t           recvThread;
343     pthread_t           timeouterThread;
344     mutable pthread_mutex_t mutex;
345
346     int                 listenSocket;
347
348     CONN_SYN_ACK_6      connSynAck6;
349     CONN_SYN_ACK_8      connSynAck8;
350
351     DISCONN_SYN_ACK_6   disconnSynAck6;
352     DISCONN_SYN_ACK_8   disconnSynAck8;
353
354     ALIVE_SYN_6         aliveSyn6;
355     ALIVE_SYN_8         aliveSyn8;
356     FIN_6               fin6;
357     FIN_8               fin8;
358
359     std::map<std::string, int> packetTypes;
360
361     STG_LOGGER &        WriteServLog;
362
363     uint32_t            enabledDirs;
364
365     DEL_USER_NOTIFIER   onDelUserNotifier;
366
367     class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
368         public:
369             UnauthorizeUser(AUTH_IA * a) : auth(a) {}
370             UnauthorizeUser(const UnauthorizeUser & rvalue) : auth(rvalue.auth) {}
371             void operator()(const std::pair<uint32_t, IA_USER> & p)
372             {
373                 auth->users->Unauthorize(p.second.user->GetLogin(), auth);
374             }
375         private:
376             UnauthorizeUser & operator=(const UnauthorizeUser & rvalue);
377
378             AUTH_IA * auth;
379     };
380
381 };
382 //-----------------------------------------------------------------------------
383 inline
384 void DEL_USER_NOTIFIER::Notify(const USER_PTR & user)
385 {
386     auth.DelUser(user);
387 }
388
389 #endif