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