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