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