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 $
 
  48 #include "ia_packets.h"
 
  50 #include "stg_logger.h"
 
  53 extern "C" PLUGIN * GetPlugin();
 
  55 #define IA_PROTO_VER    (6)
 
  57 //#define IA_DEBUG (1)
 
  58 //#define IA_PHASE_DEBUG (1)
 
  61 //-----------------------------------------------------------------------------
 
  86 //-----------------------------------------------------------------------------
 
 100     const UTIME & GetTime() const;
 
 102     #ifdef IA_PHASE_DEBUG
 
 103     void    SetUserLogin(const std::string & login);
 
 104     void    SetLogFileName(const std::string & logFileName);
 
 111     #ifdef IA_PHASE_DEBUG
 
 112     void WritePhaseChange(int newPhase);
 
 118 //-----------------------------------------------------------------------------
 
 125           password("NO PASSWORD")
 
 127         // +++ Preparing CTX +++
 
 128         unsigned char keyL[PASSWD_LEN];
 
 129         memset(keyL, 0, PASSWD_LEN);
 
 130         strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
 
 131         Blowfish_Init(&ctx, keyL, PASSWD_LEN);
 
 132         // --- Preparing CTX ---
 
 138     IA_USER(const IA_USER & u)
 
 141           lastSendAlive(u.lastSendAlive),
 
 144           messagesToSend(u.messagesToSend),
 
 145           protoVer(u.protoVer),
 
 149         aliveSent  = u.aliveSent;
 
 151         memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
 
 160     std::list<STG_MSG> messagesToSend;
 
 162     std::string     password;
 
 167 //-----------------------------------------------------------------------------
 
 168 class AUTH_IA_SETTINGS {
 
 171     virtual         ~AUTH_IA_SETTINGS() {};
 
 172     const std::string & GetStrError() const { return errorStr; };
 
 173     int             ParseSettings(const MODULE_SETTINGS & s);
 
 174     int             GetUserDelay() const { return userDelay; };
 
 175     int             GetUserTimeout() const { return userTimeout; };
 
 176     int             GetUserPort() const { return port; };
 
 177     FREEMB          GetFreeMbShowType() const { return freeMbShowType; };
 
 180     int             ParseIntInRange(const std::string & str, int min, int max, int * val);
 
 184     std::string     errorStr;
 
 185     FREEMB          freeMbShowType;
 
 187 //-----------------------------------------------------------------------------
 
 188 class AUTH_IA :public AUTH {
 
 189 friend class DEL_USER_NONIFIER;
 
 194     void                SetUsers(USERS * u) { users = u; }
 
 195     void                SetTariffs(TARIFFS *) {}
 
 196     void                SetAdmins(ADMINS *) {}
 
 197     void                SetTraffcounter(TRAFFCOUNTER *) {}
 
 198     void                SetStore(STORE *) {}
 
 199     void                SetStgSettings(const SETTINGS * s) { stgSettings = s; }
 
 200     void                SetSettings(const MODULE_SETTINGS & s) { settings = s; }
 
 205     int                 Reload() { return 0; }
 
 206     bool                IsRunning() { return isRunningRunTimeouter || isRunningRun; }
 
 208     const std::string & GetStrError() const { return errorStr; }
 
 209     const std::string   GetVersion() const { return "InetAccess authorization plugin v.1.4"; }
 
 210     uint16_t            GetStartPosition() const { return 50; }
 
 211     uint16_t            GetStopPosition() const { return 50; }
 
 213     int                 SendMessage(const STG_MSG & msg, uint32_t ip) const;
 
 216     static void *       Run(void *);
 
 217     static void *       RunTimeouter(void * d);
 
 220     void                DelUser(USER_PTR u);
 
 221     int                 RecvData(char * buffer, int bufferSize);
 
 222     int                 CheckHeader(const char * buffer, int * protoVer);
 
 223     int                 PacketProcessor(char * buff, int dataLen, uint32_t sip, uint16_t sport, int protoVer, USER_PTR * user);
 
 225     int                 Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, uint32_t sip);
 
 226     int                 Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip);
 
 227     int                 Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip);
 
 229     int                 Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip);
 
 230     int                 Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip);
 
 231     int                 Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip);
 
 233     int                 Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t sip);
 
 234     int                 Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip);
 
 235     int                 Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t sip);
 
 237     int                 Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, uint32_t sip);
 
 238     int                 Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip);
 
 239     int                 Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, uint32_t sip);
 
 241     int                 Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
 
 244                                               map<uint32_t, IA_USER>::iterator it);
 
 245     int                 Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
 
 248                                               map<uint32_t, IA_USER>::iterator it);
 
 249     int                 Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
 
 252                                               map<uint32_t, IA_USER>::iterator it);
 
 254     int                 Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
 
 255     int                 Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
 
 256     int                 Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
 
 258     int                 Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
 
 259     int                 Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
 
 260     int                 Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
 
 262     int                 Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
 
 263     int                 Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
 
 264     int                 Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
 
 266     int                 Send_FIN_6(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
 
 267     int                 Send_FIN_7(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
 
 268     int                 Send_FIN_8(IA_USER * iaUser, uint32_t sip, map<uint32_t, IA_USER>::iterator it);
 
 272     int                 SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text);
 
 273     int                 Send(uint32_t ip, uint16_t port, const char * buffer, int len);
 
 274     int                 RealSendMessage6(const STG_MSG & msg, uint32_t ip, IA_USER & user);
 
 275     int                 RealSendMessage7(const STG_MSG & msg, uint32_t ip, IA_USER & user);
 
 276     int                 RealSendMessage8(const STG_MSG & msg, uint32_t ip, IA_USER & user);
 
 278     bool                WaitPackets(int sd) const;
 
 280     BLOWFISH_CTX        ctxS;        //for loginS
 
 282     mutable std::string errorStr;
 
 283     AUTH_IA_SETTINGS    iaSettings;
 
 284     MODULE_SETTINGS     settings;
 
 289     bool                isRunningRunTimeouter;
 
 292     const SETTINGS *    stgSettings;
 
 294     mutable std::map<uint32_t, IA_USER> ip2user;
 
 296     pthread_t           recvThread;
 
 297     pthread_t           timeouterThread;
 
 298     mutable pthread_mutex_t mutex;
 
 302     CONN_SYN_ACK_6      connSynAck6;
 
 303     CONN_SYN_ACK_8      connSynAck8;
 
 305     DISCONN_SYN_ACK_6   disconnSynAck6;
 
 306     DISCONN_SYN_ACK_8   disconnSynAck8;
 
 308     ALIVE_SYN_6         aliveSyn6;
 
 309     ALIVE_SYN_8         aliveSyn8;
 
 313     std::map<std::string, int> packetTypes;
 
 315     STG_LOGGER &        WriteServLog;
 
 317     uint32_t            enabledDirs;
 
 319     class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
 
 321         DEL_USER_NONIFIER(AUTH_IA & a) : auth(a) {}
 
 322         virtual ~DEL_USER_NONIFIER() {}
 
 324         void Notify(const USER_PTR & user)
 
 333     class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
 
 335             UnauthorizeUser(AUTH_IA * a) : auth(a) {}
 
 336             void operator()(const std::pair<uint32_t, IA_USER> & p)
 
 338                 p.second.user->Unauthorize(auth);
 
 345 //-----------------------------------------------------------------------------