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