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 $
40 #include "base_auth.h"
41 #include "base_store.h"
44 #include "../../../user.h"
45 #include "../../../users.h"
46 #include "ia_packets.h"
48 #include "stg_logger.h"
53 extern "C" BASE_PLUGIN * GetPlugin();
55 #define IA_PROTO_VER (6)
57 //#define IA_DEBUG (1)
58 //#define IA_PHASE_DEBUG (1)
61 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
102 const UTIME & GetTime() const;
104 #ifdef IA_PHASE_DEBUG
105 void SetUserLogin(const string & login);
106 void SetLogFileName(const string & logFileName);
113 #ifdef IA_PHASE_DEBUG
114 void WritePhaseChange(int newPhase);
120 //-----------------------------------------------------------------------------
128 password("NO PASSWORD")
130 // +++ Preparing CTX +++
131 unsigned char keyL[PASSWD_LEN]; // ðÁÒÏÌØ ÄÌÑ ÛÉÆÒÏ×ËÉ
132 memset(keyL, 0, PASSWD_LEN);
133 strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
134 Blowfish_Init(&ctx, keyL, PASSWD_LEN);
135 // --- Preparing CTX ---
141 IA_USER(const IA_USER & u)
144 lastSendAlive(u.lastSendAlive),
147 messagesToSend(u.messagesToSend),
148 protoVer(u.protoVer),
152 aliveSent = u.aliveSent;
154 memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
165 list<STG_MSG> messagesToSend;
172 //-----------------------------------------------------------------------------
173 class AUTH_IA_SETTINGS
177 virtual ~AUTH_IA_SETTINGS() {};
178 const string& GetStrError() const { return errorStr; };
179 int ParseSettings(const MODULE_SETTINGS & s);
180 int GetUserDelay() const { return userDelay; };
181 int GetUserTimeout() const { return userTimeout; };
182 int GetUserPort() const { return port; };
183 FREEMB GetFreeMbShowType() const { return freeMbShowType; };
186 int ParseIntInRange(const string & str, int min, int max, int * val);
191 FREEMB freeMbShowType;
193 //-----------------------------------------------------------------------------
194 class AUTH_IA :public BASE_AUTH
200 void SetUsers(USERS * u) { users = u; };
201 void SetTariffs(TARIFFS *){};
202 void SetAdmins(ADMINS *){};
203 void SetTraffcounter(TRAFFCOUNTER *){};
204 void SetStore(BASE_STORE *){};
205 void SetStgSettings(const SETTINGS * s) { stgSettings = s; };
206 void SetSettings(const MODULE_SETTINGS & s) { settings = s; };
211 int Reload() { return 0; };
212 bool IsRunning() { return isRunningRunTimeouter || isRunningRun; };
214 const string & GetStrError() const { return errorStr; };
215 const string GetVersion() const { return "InetAccess authorization plugin v.1.4"; };
216 uint16_t GetStartPosition() const { return 50; };
217 uint16_t GetStopPosition() const { return 50; };
219 void DelUser(user_iter u);
221 int SendMessage(const STG_MSG & msg, uint32_t ip) const;
224 static void * Run(void *);
225 static void * RunTimeouter(void * d);
228 int RecvData(char * buffer, int bufferSize);
229 int CheckHeader(const char * buffer, int * protoVer);
230 int PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, user_iter * user);
232 int Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip);
233 int Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip);
234 int Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, user_iter * user, uint32_t sip);
236 int Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, user_iter * user, uint32_t sip);
237 int Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, user_iter * user, uint32_t sip);
238 int Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, user_iter * user, uint32_t sip);
240 int Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, user_iter * user, uint32_t sip);
241 int Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, user_iter * user, uint32_t sip);
242 int Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, user_iter * user, uint32_t sip);
244 int Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, user_iter * user, uint32_t sip);
245 int Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, user_iter * user, uint32_t sip);
246 int Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, user_iter * user, uint32_t sip);
248 int Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
252 map<uint32_t, IA_USER>::iterator it);
253 int Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
257 map<uint32_t, IA_USER>::iterator it);
258 int Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
262 map<uint32_t, IA_USER>::iterator it);
264 int Send_CONN_SYN_ACK_6(IA_USER * iaUser, user_iter * user, uint32_t sip);
265 int Send_CONN_SYN_ACK_7(IA_USER * iaUser, user_iter * user, uint32_t sip);
266 int Send_CONN_SYN_ACK_8(IA_USER * iaUser, user_iter * user, uint32_t sip);
268 int Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
269 int Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
270 int Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
272 int Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
273 int Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
274 int Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
276 int Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
277 int Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
278 int Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
282 int SendError(uint32_t ip, uint16_t port, int protoVer, const string & text);
283 int Send(uint32_t ip, uint16_t port, const char * buffer, int len);
284 int RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user);
285 int RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
286 int RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
288 bool WaitPackets(int sd) const;
290 BLOWFISH_CTX ctxS; //for loginS
292 mutable string errorStr;
293 AUTH_IA_SETTINGS iaSettings;
294 MODULE_SETTINGS settings;
299 bool isRunningRunTimeouter;
302 const SETTINGS * stgSettings;
304 mutable map<uint32_t, IA_USER> ip2user;
306 pthread_t recvThread;
307 pthread_t timeouterThread;
308 mutable pthread_mutex_t mutex;
312 CONN_SYN_ACK_6 connSynAck6;
313 CONN_SYN_ACK_8 connSynAck8;
315 DISCONN_SYN_ACK_6 disconnSynAck6;
316 DISCONN_SYN_ACK_8 disconnSynAck8;
318 ALIVE_SYN_6 aliveSyn6;
319 ALIVE_SYN_8 aliveSyn8;
323 map<string, int> packetTypes;
325 STG_LOGGER & WriteServLog;
327 uint32_t enabledDirs;
329 class DEL_USER_NONIFIER: public NOTIFIER_BASE<user_iter>
332 DEL_USER_NONIFIER(AUTH_IA & a) : auth(a) {};
333 virtual ~DEL_USER_NONIFIER(){};
335 void Notify(const user_iter & user)
344 class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
346 UnauthorizeUser(AUTH_IA * a) : auth(a) {};
347 void operator()(const std::pair<uint32_t, IA_USER> & p)
349 p.second.user->Unauthorize(auth);
356 //-----------------------------------------------------------------------------