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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/09/10 06:39:19 $
30 #include "stg/os_int.h"
32 #include "stg/store.h"
33 #include "stg/notifer.h"
34 #include "stg/user_ips.h"
36 #include "stg/users.h"
37 #include "stg/ia_packets.h"
38 #include "stg/blowfish.h"
39 #include "stg/logger.h"
40 #include "stg/utime.h"
41 #include "stg/logger.h"
54 #define IA_PROTO_VER (6)
56 //#define IA_DEBUG (1)
57 //#define IA_PHASE_DEBUG (1)
60 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
98 const UTIME & GetTime() const;
100 #ifdef IA_PHASE_DEBUG
101 void SetUserLogin(const std::string & login);
102 void SetLogFileName(const std::string & logFileName);
109 #ifdef IA_PHASE_DEBUG
110 void WritePhaseChange(int newPhase);
116 //-----------------------------------------------------------------------------
123 rnd(static_cast<uint32_t>(random())),
128 password("NO PASSWORD")
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);
140 IA_USER(const IA_USER & u)
144 lastSendAlive(u.lastSendAlive),
148 messagesToSend(u.messagesToSend),
149 protoVer(u.protoVer),
153 aliveSent = u.aliveSent;
155 memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
158 IA_USER(const std::string & l,
166 rnd(static_cast<uint32_t>(random())),
171 password(user->GetProperty().password.Get())
173 unsigned char keyL[PASSWD_LEN];
174 memset(keyL, 0, PASSWD_LEN);
175 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
176 Blowfish_Init(&ctx, keyL, PASSWD_LEN);
190 std::list<STG_MSG> messagesToSend;
192 std::string password;
198 IA_USER & operator=(const IA_USER & rvalue);
200 //-----------------------------------------------------------------------------
201 class AUTH_IA_SETTINGS {
204 virtual ~AUTH_IA_SETTINGS() {}
205 const std::string & GetStrError() const { return errorStr; }
206 int ParseSettings(const MODULE_SETTINGS & s);
207 UTIME GetUserDelay() const { return UTIME(userDelay); }
208 UTIME GetUserTimeout() const { return UTIME(userTimeout); }
209 uint16_t GetUserPort() const { return port; }
210 FREEMB GetFreeMbShowType() const { return freeMbShowType; }
211 bool LogProtocolErrors() const { return logProtocolErrors; }
217 std::string errorStr;
218 FREEMB freeMbShowType;
219 bool logProtocolErrors;
221 //-----------------------------------------------------------------------------
223 //-----------------------------------------------------------------------------
224 class DEL_USER_NOTIFIER: public NOTIFIER_BASE<USER_PTR> {
226 explicit DEL_USER_NOTIFIER(AUTH_IA & a) : auth(a) {}
227 virtual ~DEL_USER_NOTIFIER() {}
229 void Notify(const USER_PTR & user);
231 DEL_USER_NOTIFIER(const DEL_USER_NOTIFIER & rvalue);
232 DEL_USER_NOTIFIER & operator=(const DEL_USER_NOTIFIER & rvalue);
236 //-----------------------------------------------------------------------------
237 class AUTH_IA :public AUTH {
238 friend class DEL_USER_NOTIFIER;
243 void SetUsers(USERS * u) { users = u; }
244 void SetStgSettings(const SETTINGS * s) { stgSettings = s; }
245 void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
250 int Reload(const MODULE_SETTINGS & ms);
251 bool IsRunning() { return isRunningRunTimeouter || isRunningRun; }
253 const std::string & GetStrError() const { return errorStr; }
254 std::string GetVersion() const { return "InetAccess authorization plugin v.1.4"; }
255 uint16_t GetStartPosition() const { return 30; }
256 uint16_t GetStopPosition() const { return 30; }
258 int SendMessage(const STG_MSG & msg, uint32_t ip) const;
261 AUTH_IA(const AUTH_IA & rvalue);
262 AUTH_IA & operator=(const AUTH_IA & rvalue);
264 static void * Run(void *);
265 static void * RunTimeouter(void * d);
268 void DelUser(USER_PTR u);
269 int RecvData(char * buffer, int bufferSize);
270 int CheckHeader(const char * buffer, uint32_t sip, int * protoVer);
271 int PacketProcessor(void * buff, size_t dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user);
273 int Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, uint32_t sip);
274 int Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip);
275 int Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip);
277 int Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip);
278 int Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip);
279 int Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip);
281 int Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t sip);
282 int Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip);
283 int Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t sip);
285 int Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, uint32_t sip);
286 int Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip);
287 int Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, uint32_t sip);
289 int Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
292 std::map<uint32_t, IA_USER>::iterator it);
293 int Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
296 std::map<uint32_t, IA_USER>::iterator it);
297 int Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
300 std::map<uint32_t, IA_USER>::iterator it);
302 int Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
303 int Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
304 int Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
306 int Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
307 int Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
308 int Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
310 int Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
311 int Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
312 int Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
314 int Send_FIN_6(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it);
315 int Send_FIN_7(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it);
316 int Send_FIN_8(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it);
320 int SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text);
321 int Send(uint32_t ip, uint16_t port, const char * buffer, size_t len);
322 int RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user);
323 int RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
324 int RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
326 BLOWFISH_CTX ctxS; //for loginS
328 mutable std::string errorStr;
329 AUTH_IA_SETTINGS iaSettings;
330 MODULE_SETTINGS settings;
335 bool isRunningRunTimeouter;
338 const SETTINGS * stgSettings;
340 mutable std::map<uint32_t, IA_USER> ip2user;
342 pthread_t recvThread;
343 pthread_t timeouterThread;
344 mutable pthread_mutex_t mutex;
348 CONN_SYN_ACK_6 connSynAck6;
349 CONN_SYN_ACK_8 connSynAck8;
351 DISCONN_SYN_ACK_6 disconnSynAck6;
352 DISCONN_SYN_ACK_8 disconnSynAck8;
354 ALIVE_SYN_6 aliveSyn6;
355 ALIVE_SYN_8 aliveSyn8;
359 std::map<std::string, int> packetTypes;
361 uint32_t enabledDirs;
363 DEL_USER_NOTIFIER onDelUserNotifier;
365 PLUGIN_LOGGER logger;
367 friend class UnauthorizeUser;
369 //-----------------------------------------------------------------------------
370 class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
372 explicit UnauthorizeUser(AUTH_IA * a) : auth(a) {}
373 UnauthorizeUser(const UnauthorizeUser & rvalue) : auth(rvalue.auth) {}
374 void operator()(const std::pair<uint32_t, IA_USER> & p)
376 auth->users->Unauthorize(p.second.user->GetLogin(), auth);
379 UnauthorizeUser & operator=(const UnauthorizeUser & rvalue);
383 //-----------------------------------------------------------------------------
385 void DEL_USER_NOTIFIER::Notify(const USER_PTR & user)