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 #include "stg/store.h"
 
  24 #include "stg/module_settings.h"
 
  25 #include "stg/notifer.h"
 
  26 #include "stg/user_ips.h"
 
  28 #include "stg/users.h"
 
  29 #include "stg/user_property.h"
 
  30 #include "stg/ia_packets.h"
 
  31 #include "stg/blowfish.h"
 
  32 #include "stg/logger.h"
 
  33 #include "stg/utime.h"
 
  34 #include "stg/logger.h"
 
  48 //#define IA_DEBUG (1)
 
  49 //#define IA_PHASE_DEBUG (1)
 
  52 //-----------------------------------------------------------------------------
 
  77 //-----------------------------------------------------------------------------
 
  90     const UTIME & GetTime() const;
 
  93     void    SetUserLogin(const std::string & login);
 
  94     void    SetLogFileName(const std::string & logFileName);
 
 101     #ifdef IA_PHASE_DEBUG
 
 102     void WritePhaseChange(int newPhase);
 
 108 //-----------------------------------------------------------------------------
 
 110     using ConstUserPtr = const STG::User*;
 
 114           rnd(static_cast<uint32_t>(random())),
 
 117           password("NO PASSWORD")
 
 119     unsigned char keyL[PASSWD_LEN];
 
 120     memset(keyL, 0, PASSWD_LEN);
 
 121     strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
 
 122     Blowfish_Init(&ctx, keyL, PASSWD_LEN);
 
 129     IA_USER(const IA_USER & u)
 
 133           lastSendAlive(u.lastSendAlive),
 
 137           messagesToSend(u.messagesToSend),
 
 138           protoVer(u.protoVer),
 
 142     aliveSent  = u.aliveSent;
 
 144     memcpy(&ctx, &u.ctx, sizeof(BLOWFISH_CTX));
 
 147     IA_USER(const std::string & l,
 
 154           rnd(static_cast<uint32_t>(random())),
 
 158           password(user->GetProperties().password.Get())
 
 160     unsigned char keyL[PASSWD_LEN];
 
 161     memset(keyL, 0, PASSWD_LEN);
 
 162     strncpy((char *)keyL, password.c_str(), PASSWD_LEN);
 
 163     Blowfish_Init(&ctx, keyL, PASSWD_LEN);
 
 177     std::vector<STG::Message> messagesToSend;
 
 179     std::string     password;
 
 185     IA_USER & operator=(const IA_USER & rvalue);
 
 187 //-----------------------------------------------------------------------------
 
 188 class AUTH_IA_SETTINGS {
 
 191     virtual         ~AUTH_IA_SETTINGS() {}
 
 192     const std::string & GetStrError() const { return errorStr; }
 
 193     int             ParseSettings(const STG::ModuleSettings & s);
 
 194     UTIME           GetUserDelay() const { return UTIME(userDelay); }
 
 195     UTIME           GetUserTimeout() const { return UTIME(userTimeout); }
 
 196     uint16_t        GetUserPort() const { return port; }
 
 197     FREEMB          GetFreeMbShowType() const { return freeMbShowType; }
 
 198     bool            LogProtocolErrors() const { return logProtocolErrors; }
 
 204     std::string     errorStr;
 
 205     FREEMB          freeMbShowType;
 
 206     bool            logProtocolErrors;
 
 208 //-----------------------------------------------------------------------------
 
 210 using UserPtr = STG::User*;
 
 211 //-----------------------------------------------------------------------------
 
 212 class DEL_USER_NOTIFIER: public STG::NotifierBase<UserPtr> {
 
 214     explicit DEL_USER_NOTIFIER(AUTH_IA & a) : auth(a) {}
 
 215     virtual ~DEL_USER_NOTIFIER() {}
 
 217     void Notify(const UserPtr & user);
 
 219     DEL_USER_NOTIFIER(const DEL_USER_NOTIFIER & rvalue);
 
 220     DEL_USER_NOTIFIER & operator=(const DEL_USER_NOTIFIER & rvalue);
 
 224 //-----------------------------------------------------------------------------
 
 225 class AUTH_IA : public STG::Auth {
 
 226 friend class DEL_USER_NOTIFIER;
 
 231     void                SetUsers(STG::Users * u) override { users = u; }
 
 232     void                SetStgSettings(const STG::Settings * s) override { stgSettings = s; }
 
 233     void                SetSettings(const STG::ModuleSettings & s) override { settings = s; }
 
 234     int                 ParseSettings() override;
 
 236     int                 Start() override;
 
 238     int                 Reload(const STG::ModuleSettings & ms) override;
 
 239     bool                IsRunning() override { return isRunningRunTimeouter || isRunningRun; }
 
 241     const std::string & GetStrError() const override { return errorStr; }
 
 242     std::string         GetVersion() const override { return "InetAccess authorization plugin v.1.4"; }
 
 243     uint16_t            GetStartPosition() const override { return 30; }
 
 244     uint16_t            GetStopPosition() const override { return 30; }
 
 246     int                 SendMessage(const STG::Message & msg, uint32_t ip) const override;
 
 249     AUTH_IA(const AUTH_IA & rvalue);
 
 250     AUTH_IA & operator=(const AUTH_IA & rvalue);
 
 252     static void *       Run(void *);
 
 253     static void *       RunTimeouter(void * d);
 
 256     void                DelUser(UserPtr u);
 
 257     int                 RecvData(char * buffer, int bufferSize);
 
 258     int                 CheckHeader(const char * buffer, uint32_t sip, int * protoVer);
 
 259     int                 PacketProcessor(void * buff, size_t dataLen, uint32_t sip, uint16_t sport, int protoVer, UserPtr user);
 
 261     int                 Process_CONN_SYN_6(CONN_SYN_6 * connSyn, IA_USER * iaUser, uint32_t sip);
 
 262     int                 Process_CONN_SYN_7(CONN_SYN_7 * connSyn, IA_USER * iaUser, uint32_t sip);
 
 263     int                 Process_CONN_SYN_8(CONN_SYN_8 * connSyn, IA_USER * iaUser, uint32_t sip);
 
 265     int                 Process_CONN_ACK_6(CONN_ACK_6 * connAck, IA_USER * iaUser, uint32_t sip);
 
 266     int                 Process_CONN_ACK_7(CONN_ACK_7 * connAck, IA_USER * iaUser, uint32_t sip);
 
 267     int                 Process_CONN_ACK_8(CONN_ACK_8 * connAck, IA_USER * iaUser, uint32_t sip);
 
 269     int                 Process_ALIVE_ACK_6(ALIVE_ACK_6 * aliveAck, IA_USER * iaUser, uint32_t sip);
 
 270     int                 Process_ALIVE_ACK_7(ALIVE_ACK_7 * aliveAck, IA_USER * iaUser, uint32_t sip);
 
 271     int                 Process_ALIVE_ACK_8(ALIVE_ACK_8 * aliveAck, IA_USER * iaUser, uint32_t sip);
 
 273     int                 Process_DISCONN_SYN_6(DISCONN_SYN_6 * disconnSyn, IA_USER * iaUser, uint32_t sip);
 
 274     int                 Process_DISCONN_SYN_7(DISCONN_SYN_7 * disconnSyn, IA_USER * iaUser, uint32_t sip);
 
 275     int                 Process_DISCONN_SYN_8(DISCONN_SYN_8 * disconnSyn, IA_USER * iaUser, uint32_t sip);
 
 277     int                 Process_DISCONN_ACK_6(DISCONN_ACK_6 * disconnSyn,
 
 280                                               std::map<uint32_t, IA_USER>::iterator it);
 
 281     int                 Process_DISCONN_ACK_7(DISCONN_ACK_7 * disconnSyn,
 
 284                                               std::map<uint32_t, IA_USER>::iterator it);
 
 285     int                 Process_DISCONN_ACK_8(DISCONN_ACK_8 * disconnSyn,
 
 288                                               std::map<uint32_t, IA_USER>::iterator it);
 
 290     int                 Send_CONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
 
 291     int                 Send_CONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
 
 292     int                 Send_CONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
 
 294     int                 Send_ALIVE_SYN_6(IA_USER * iaUser, uint32_t sip);
 
 295     int                 Send_ALIVE_SYN_7(IA_USER * iaUser, uint32_t sip);
 
 296     int                 Send_ALIVE_SYN_8(IA_USER * iaUser, uint32_t sip);
 
 298     int                 Send_DISCONN_SYN_ACK_6(IA_USER * iaUser, uint32_t sip);
 
 299     int                 Send_DISCONN_SYN_ACK_7(IA_USER * iaUser, uint32_t sip);
 
 300     int                 Send_DISCONN_SYN_ACK_8(IA_USER * iaUser, uint32_t sip);
 
 302     int                 Send_FIN_6(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it);
 
 303     int                 Send_FIN_7(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it);
 
 304     int                 Send_FIN_8(IA_USER * iaUser, uint32_t sip, std::map<uint32_t, IA_USER>::iterator it);
 
 308     int                 SendError(uint32_t ip, uint16_t port, int protoVer, const std::string & text);
 
 309     int                 Send(uint32_t ip, uint16_t port, const char * buffer, size_t len);
 
 310     int                 RealSendMessage6(const STG::Message & msg, uint32_t ip, IA_USER & user);
 
 311     int                 RealSendMessage7(const STG::Message & msg, uint32_t ip, IA_USER & user);
 
 312     int                 RealSendMessage8(const STG::Message & msg, uint32_t ip, IA_USER & user);
 
 314     BLOWFISH_CTX        ctxS;        //for loginS
 
 316     mutable std::string errorStr;
 
 317     AUTH_IA_SETTINGS    iaSettings;
 
 318     STG::ModuleSettings settings;
 
 323     bool                isRunningRunTimeouter;
 
 326     const STG::Settings *    stgSettings;
 
 328     mutable std::map<uint32_t, IA_USER> ip2user;
 
 330     pthread_t           recvThread;
 
 331     pthread_t           timeouterThread;
 
 332     mutable pthread_mutex_t mutex;
 
 336     CONN_SYN_ACK_6      connSynAck6;
 
 337     CONN_SYN_ACK_8      connSynAck8;
 
 339     DISCONN_SYN_ACK_6   disconnSynAck6;
 
 340     DISCONN_SYN_ACK_8   disconnSynAck8;
 
 342     ALIVE_SYN_6         aliveSyn6;
 
 343     ALIVE_SYN_8         aliveSyn8;
 
 347     std::map<std::string, int> packetTypes;
 
 349     uint32_t            enabledDirs;
 
 351     DEL_USER_NOTIFIER   onDelUserNotifier;
 
 353     STG::PluginLogger   logger;
 
 355     friend class UnauthorizeUser;
 
 357 //-----------------------------------------------------------------------------
 
 358 class UnauthorizeUser : std::unary_function<const std::pair<uint32_t, IA_USER> &, void> {
 
 360         explicit UnauthorizeUser(AUTH_IA * a) : auth(a) {}
 
 361         UnauthorizeUser(const UnauthorizeUser & rvalue) : auth(rvalue.auth) {}
 
 362         void operator()(const std::pair<uint32_t, IA_USER> & p)
 
 364             auth->users->Unauthorize(p.second.user->GetLogin(), auth);
 
 367         UnauthorizeUser & operator=(const UnauthorizeUser & rvalue);
 
 371 //-----------------------------------------------------------------------------
 
 373 void DEL_USER_NOTIFIER::Notify(const UserPtr & user)