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 $
41 #include "stg/os_int.h"
43 #include "stg/store.h"
44 #include "stg/notifer.h"
45 #include "stg/user_ips.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 #include "stg/logger.h"
54 extern "C" PLUGIN * GetPlugin();
56 #define IA_PROTO_VER (6)
58 //#define IA_DEBUG (1)
59 //#define IA_PHASE_DEBUG (1)
62 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
101 const UTIME & GetTime() const;
103 #ifdef IA_PHASE_DEBUG
104 void SetUserLogin(const std::string & login);
105 void SetLogFileName(const std::string & logFileName);
112 #ifdef IA_PHASE_DEBUG
113 void WritePhaseChange(int newPhase);
119 //-----------------------------------------------------------------------------
131 password("NO PASSWORD")
133 unsigned char keyL[PASSWD_LEN];
134 memset(keyL, 0, PASSWD_LEN);
135 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
136 Blowfish_Init(&ctx, keyL, PASSWD_LEN);
143 IA_USER(const IA_USER & u)
147 lastSendAlive(u.lastSendAlive),
151 messagesToSend(u.messagesToSend),
152 protoVer(u.protoVer),
156 aliveSent = u.aliveSent;
158 memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
161 IA_USER(const std::string & l,
174 password(user->GetProperty().password.Get())
176 unsigned char keyL[PASSWD_LEN];
177 memset(keyL, 0, PASSWD_LEN);
178 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
179 Blowfish_Init(&ctx, keyL, PASSWD_LEN);
193 std::list<STG_MSG> messagesToSend;
195 std::string password;
201 IA_USER & operator=(const IA_USER & rvalue);
203 //-----------------------------------------------------------------------------
204 class AUTH_IA_SETTINGS {
207 virtual ~AUTH_IA_SETTINGS() {};
208 const std::string & GetStrError() const { return errorStr; };
209 int ParseSettings(const MODULE_SETTINGS & s);
210 int GetUserDelay() const { return userDelay; };
211 int GetUserTimeout() const { return userTimeout; };
212 int GetUserPort() const { return port; };
213 FREEMB GetFreeMbShowType() const { return freeMbShowType; };
219 std::string errorStr;
220 FREEMB freeMbShowType;
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
225 class DEL_USER_NOTIFIER: public NOTIFIER_BASE<USER_PTR> {
227 DEL_USER_NOTIFIER(AUTH_IA & a) : auth(a) {}
228 virtual ~DEL_USER_NOTIFIER() {}
230 void Notify(const USER_PTR & user);
232 DEL_USER_NOTIFIER(const DEL_USER_NOTIFIER & rvalue);
233 DEL_USER_NOTIFIER & operator=(const DEL_USER_NOTIFIER & rvalue);
237 //-----------------------------------------------------------------------------
238 class AUTH_IA :public AUTH {
239 friend class DEL_USER_NOTIFIER;
244 void SetUsers(USERS * u) { users = u; }
245 void SetStgSettings(const SETTINGS * s) { stgSettings = s; }
246 void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
251 int Reload() { return 0; }
252 bool IsRunning() { return isRunningRunTimeouter || isRunningRun; }
254 const std::string & GetStrError() const { return errorStr; }
255 const std::string GetVersion() const { return "InetAccess authorization plugin v.1.4"; }
256 uint16_t GetStartPosition() const { return 30; }
257 uint16_t GetStopPosition() const { return 30; }
259 int SendMessage(const STG_MSG & msg, uint32_t ip) const;
262 AUTH_IA(const AUTH_IA & rvalue);
263 AUTH_IA & operator=(const AUTH_IA & rvalue);
265 static void * Run(void *);
266 static void * RunTimeouter(void * d);
269 void DelUser(USER_PTR u);
270 int RecvData(char * buffer, int bufferSize);
271 int CheckHeader(const char * buffer, int * protoVer);
272 int PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR user);
274 int Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, uint32_t sip);
275 int Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip);
276 int Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip);
278 int Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip);
279 int Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip);
280 int Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip);
282 int Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t sip);
283 int Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip);
284 int Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t sip);
286 int Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, uint32_t sip);
287 int Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip);
288 int Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, uint32_t sip);
290 int Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
293 map<uint32_t, IA_USER>::iterator it);
294 int Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
297 map<uint32_t, IA_USER>::iterator it);
298 int Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
301 map<uint32_t, IA_USER>::iterator it);
303 int Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
304 int Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
305 int Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
307 int Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
308 int Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
309 int Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
311 int Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
312 int Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
313 int Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
315 int Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
316 int Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
317 int Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
321 int SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text);
322 int Send(uint32_t ip, uint16_t port, const char * buffer, int len);
323 int RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user);
324 int RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
325 int RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
327 BLOWFISH_CTX ctxS; //for loginS
329 mutable std::string errorStr;
330 AUTH_IA_SETTINGS iaSettings;
331 MODULE_SETTINGS settings;
336 bool isRunningRunTimeouter;
339 const SETTINGS * stgSettings;
341 mutable std::map<uint32_t, IA_USER> ip2user;
343 pthread_t recvThread;
344 pthread_t timeouterThread;
345 mutable pthread_mutex_t mutex;
349 CONN_SYN_ACK_6 connSynAck6;
350 CONN_SYN_ACK_8 connSynAck8;
352 DISCONN_SYN_ACK_6 disconnSynAck6;
353 DISCONN_SYN_ACK_8 disconnSynAck8;
355 ALIVE_SYN_6 aliveSyn6;
356 ALIVE_SYN_8 aliveSyn8;
360 std::map<std::string, int> packetTypes;
362 uint32_t enabledDirs;
364 DEL_USER_NOTIFIER onDelUserNotifier;
366 PLUGIN_LOGGER logger;
368 friend class UnauthorizeUser;
370 //-----------------------------------------------------------------------------
371 class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
373 UnauthorizeUser(AUTH_IA * a) : auth(a) {}
374 UnauthorizeUser(const UnauthorizeUser & rvalue) : auth(rvalue.auth) {}
375 void operator()(const std::pair<uint32_t, IA_USER> & p)
377 auth->users->Unauthorize(p.second.user->GetLogin(), auth);
380 UnauthorizeUser & operator=(const UnauthorizeUser & rvalue);
384 //-----------------------------------------------------------------------------
386 void DEL_USER_NOTIFIER::Notify(const USER_PTR & user)