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