diff --git a/SecurityAgent/SecurityAgent.cpp b/SecurityAgent/SecurityAgent.cpp index 6d031971..7233a3bf 100644 --- a/SecurityAgent/SecurityAgent.cpp +++ b/SecurityAgent/SecurityAgent.cpp @@ -57,24 +57,11 @@ namespace Plugin { const string _callsign; }; - void SecurityAgent::TokenDispatcher::Tokenize::Procedure(Core::IPCChannel& source, Core::ProxyType& data) { - Core::ProxyType message = Core::proxy_cast(data); - - ASSERT (message.IsValid() == true); - - if (message.IsValid() == true) { - string token; - if (_parent->CreateToken(message->Parameters().Length(), message->Parameters().Value(), token) == Core::ERROR_NONE) { - message->Response().Set(static_cast(token.length()), reinterpret_cast(token.c_str())); - source.ReportResponse(data); - } - else { - TRACE(Trace::Fatal, ("Could not create a security token.")); - } - } - } - - SecurityAgent::SecurityAgent() : _dispatcher(nullptr) + SecurityAgent::SecurityAgent() + : _secretKey() + , _acl() + , _dispatcher(nullptr) + , _engine() { RegisterAll(); @@ -97,6 +84,8 @@ namespace Plugin { _skipURL = static_cast(service->WebPrefix().length()); Core::File aclFile(service->PersistentPath() + config.ACL.Value(), true); + PluginHost::ISubSystem* subSystem = service->SubSystems(); + if (aclFile.Exists() == false) { aclFile = service->DataPath() + config.ACL.Value(); } @@ -114,29 +103,36 @@ namespace Plugin { } } - PluginHost::ISubSystem* subSystem = service->SubSystems(); - + ASSERT(_dispatcher == nullptr); ASSERT(subSystem != nullptr); - if (subSystem != nullptr) { - Core::Sink information(service->Callsign()); - - if (subSystem->IsActive(PluginHost::ISubSystem::SECURITY) != false) { - SYSLOG(Logging::Startup, (_T("Security is not defined as External !!"))); - } + string connector = config.Connector.Value(); - subSystem->Set(PluginHost::ISubSystem::SECURITY, &information); - subSystem->Release(); + if (connector.empty() == true) { + connector = service->VolatilePath() + _T("token"); } + _engine = Core::ProxyType::Create(&Core::IWorkerPool::Instance()); + _dispatcher.reset(new TokenDispatcher(Core::NodeId(connector.c_str()), service->ProxyStubPath(), this, _engine)); - ASSERT(_dispatcher == nullptr); + if (_dispatcher != nullptr) { - string connector = config.Connector.Value(); + if (_dispatcher->IsListening() == false) { + _dispatcher.reset(nullptr); + _engine.Release(); + } else { + if (subSystem != nullptr) { + Core::SystemInfo::SetEnvironment(_T("SECURITYAGENT_PATH"), config.Connector.Value(), true); + Core::Sink information(service->Callsign()); - if (connector.empty() == true) { - connector = service->VolatilePath() + _T("token"); + if (subSystem->IsActive(PluginHost::ISubSystem::SECURITY) != false) { + SYSLOG(Logging::Startup, (_T("Security is not defined as External !!"))); + } + + subSystem->Set(PluginHost::ISubSystem::SECURITY, &information); + subSystem->Release(); + } + } } - _dispatcher = new TokenDispatcher(Core::NodeId(connector.c_str()), this); // On success return empty, to indicate there is no error text. return _T(""); @@ -148,9 +144,6 @@ namespace Plugin { ASSERT(subSystem != nullptr); - delete _dispatcher; - _dispatcher = nullptr; - if (subSystem != nullptr) { subSystem->Set(PluginHost::ISubSystem::NOT_SECURITY, nullptr); subSystem->Release(); diff --git a/SecurityAgent/SecurityAgent.h b/SecurityAgent/SecurityAgent.h index cece0238..ae30a731 100644 --- a/SecurityAgent/SecurityAgent.h +++ b/SecurityAgent/SecurityAgent.h @@ -33,52 +33,54 @@ namespace Plugin { public PluginHost::JSONRPC, public PluginHost::IWeb { private: - class TokenDispatcher { - private: + class TokenDispatcher : public RPC::Communicator { + public: + TokenDispatcher() = delete; TokenDispatcher(const TokenDispatcher&) = delete; TokenDispatcher& operator=(const TokenDispatcher&) = delete; - private: - class Tokenize : public Core::IIPCServer { - private: - Tokenize(const Tokenize&) = delete; - Tokenize& operator=(const Tokenize&) = delete; - - public: - Tokenize(PluginHost::IAuthenticate* parent) : _parent(parent) - { + TokenDispatcher( + const Core::NodeId& source, + const std::string& proxyStubPath, + PluginHost::IAuthenticate* parentInterface, + const Core::ProxyType& engine + ) + : RPC::Communicator(source, proxyStubPath, Core::ProxyType(engine)) + , _parentInterface(parentInterface) + { + if(_parentInterface != nullptr){ + _parentInterface->AddRef(); } - virtual ~Tokenize() - { + engine->Announcements(Announcement()); + Open(Core::infinite); + } + ~TokenDispatcher() override + { + if(_parentInterface != nullptr){ + _parentInterface->Release(); } - public: - void Procedure(Core::IPCChannel& source, Core::ProxyType& data) override; - - private: - PluginHost::IAuthenticate* _parent; - }; + Close(Core::infinite); + } - public: - TokenDispatcher(const Core::NodeId& endPoint, PluginHost::IAuthenticate* officer) - : _channel(endPoint, 1024) + private: + void* Aquire(const string&, const uint32_t interfaceId, const uint32_t versionId) override { - Core::SystemInfo::SetEnvironment(_T("SECURITYAGENT_PATH"), endPoint.QualifiedName().c_str()); + void* result = nullptr; - _channel.CreateFactory(1); - _channel.Register(IPC::SecurityAgent::TokenData::Id(), Core::ProxyType(Core::ProxyType::Create(officer))); + if (((versionId == 1) || (versionId == static_cast(~0))) && ((interfaceId == PluginHost::IAuthenticate::ID) || (interfaceId == Core::IUnknown::ID))) { + + _parentInterface->AddRef(); - _channel.Open(0); - } - ~TokenDispatcher() - { - _channel.Close(Core::infinite); - _channel.Unregister(IPC::SecurityAgent::TokenData::Id()); - _channel.DestroyFactory(); + + TRACE(Trace::Information, ("SecurityAgent interface(IAuthenticate) aquired => %p", this)); + result = _parentInterface; + } + return (result); } private: - Core::IPCChannelClientType _channel; + PluginHost::IAuthenticate* _parentInterface; }; class Config : public Core::JSON::Container { @@ -162,7 +164,8 @@ namespace Plugin { uint8_t _secretKey[Crypto::SHA256::Length]; AccessControlList _acl; uint8_t _skipURL; - TokenDispatcher* _dispatcher; + std::unique_ptr _dispatcher; + Core::ProxyType _engine; }; } // namespace Plugin