Merge with default Experimental_Callback_Events
authorMarkus Schaber <markus@pep-security.net>
Sun, 19 Jun 2016 11:27:13 +0200
branchExperimental_Callback_Events
changeset 95b924fbb97a9e
parent 85 8939007c795b
parent 94 59e25c32911a
child 132 267c3fe7351c
Merge with default
pEpCOMServerAdapter.vcxproj
pEpCOMServerAdapter.vcxproj.filters
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GateKeeper.cpp	Sun Jun 19 11:27:13 2016 +0200
     1.3 @@ -0,0 +1,67 @@
     1.4 +#include "stdafx.h"
     1.5 +
     1.6 +#include "GateKeeper.h"
     1.7 +
     1.8 +using namespace std;
     1.9 +
    1.10 +const LPCTSTR GateKeeper::plugin_reg_path = _T("Software\\Microsoft\\Office\\Outlook\\Addins\\pEp");
    1.11 +const LPCTSTR GateKeeper::plugin_reg_value_name = _T("LoadBehavior");
    1.12 +
    1.13 +const time_t GateKeeper::cycle = 7200;   // 7200 sec is 2 h
    1.14 +const DWORD GateKeeper::waiting = 10000; // 10000 ms is 10 sec
    1.15 +
    1.16 +time_t GateKeeper::time_diff()
    1.17 +{
    1.18 +    try {
    1.19 +        static random_device rd;
    1.20 +        static mt19937 gen(rd());
    1.21 +
    1.22 +        uniform_int_distribution<time_t> dist(0, cycle);
    1.23 +
    1.24 +        return dist(gen);
    1.25 +    }
    1.26 +    catch (exception&) {
    1.27 +        assert(0);
    1.28 +        return 0;
    1.29 +    }
    1.30 +}
    1.31 +
    1.32 +void GateKeeper::keep()
    1.33 +{
    1.34 +    while (1) {
    1.35 +        keep_plugin();
    1.36 +
    1.37 +        now = time(NULL);
    1.38 +        assert(now != -1);
    1.39 +
    1.40 +        if (now > next) {
    1.41 +            next = now + GateKeeper::cycle;
    1.42 +            keep_updated();
    1.43 +        }
    1.44 +
    1.45 +        Sleep(waiting);
    1.46 +    }
    1.47 +}
    1.48 +
    1.49 +void GateKeeper::keep_plugin()
    1.50 +{
    1.51 +    if (!cu_open)
    1.52 +        return;
    1.53 +
    1.54 +    DWORD value;
    1.55 +    DWORD size;
    1.56 +
    1.57 +    LONG lResult = RegGetValue(cu, plugin_reg_path, plugin_reg_value_name, RRF_RT_REG_DWORD, NULL, &value, &size);
    1.58 +    if (lResult != ERROR_SUCCESS)
    1.59 +        return;
    1.60 +
    1.61 +    if (value != 3) {
    1.62 +        lResult = RegSetValue(cu, plugin_reg_path, RRF_RT_REG_DWORD, plugin_reg_value_name, 3);
    1.63 +        assert(lResult == ERROR_SUCCESS);
    1.64 +    }
    1.65 +}
    1.66 +
    1.67 +void GateKeeper::keep_updated()
    1.68 +{
    1.69 +
    1.70 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/GateKeeper.h	Sun Jun 19 11:27:13 2016 +0200
     2.3 @@ -0,0 +1,45 @@
     2.4 +class CpEpCOMServerAdapterModule;
     2.5 +
     2.6 +class GateKeeper {
     2.7 +public:
     2.8 +    GateKeeper(CpEpCOMServerAdapterModule * const self) : _self(self), now(time(NULL)), next(now + time_diff())
     2.9 +    {
    2.10 +        LONG lResult = RegOpenCurrentUser(KEY_READ, &cu);
    2.11 +        assert(lResult == ERROR_SUCCESS);
    2.12 +        if (lResult == ERROR_SUCCESS)
    2.13 +            cu_open = true;
    2.14 +        else
    2.15 +            cu_open = false;
    2.16 +    }
    2.17 +
    2.18 +    ~GateKeeper()
    2.19 +    {
    2.20 +        if (cu_open)
    2.21 +            RegCloseKey(cu);
    2.22 +    }
    2.23 +
    2.24 +    CpEpCOMServerAdapterModule * const module() const
    2.25 +    {
    2.26 +        return _self;
    2.27 +    }
    2.28 +
    2.29 +    void keep();
    2.30 +
    2.31 +protected:
    2.32 +    static const LPCTSTR plugin_reg_path;
    2.33 +    static const LPCTSTR plugin_reg_value_name;
    2.34 +    static const time_t cycle;
    2.35 +    static const DWORD waiting;
    2.36 +
    2.37 +    static time_t time_diff();
    2.38 +    void keep_plugin();
    2.39 +    void keep_updated();
    2.40 +
    2.41 +private:
    2.42 +    time_t now;
    2.43 +    time_t next;
    2.44 +    bool cu_open;
    2.45 +    HKEY cu;
    2.46 +
    2.47 +    CpEpCOMServerAdapterModule * const _self;
    2.48 +};
     3.1 --- a/pEpCOMServerAdapter.cpp	Sat May 28 21:02:06 2016 +0200
     3.2 +++ b/pEpCOMServerAdapter.cpp	Sun Jun 19 11:27:13 2016 +0200
     3.3 @@ -6,21 +6,43 @@
     3.4  #include "pEpCOMServerAdapter_i.h"
     3.5  #include "xdlldata.h"
     3.6  
     3.7 +#include "GateKeeper.h"
     3.8  
     3.9  using namespace ATL;
    3.10 -
    3.11 +using namespace std;
    3.12  
    3.13  class CpEpCOMServerAdapterModule : public ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >
    3.14  {
    3.15 -public :
    3.16 +public:
    3.17 +    CpEpCOMServerAdapterModule() : ATL::CAtlExeModuleT< CpEpCOMServerAdapterModule >(), gatekeeper_thread(NULL)
    3.18 +    {
    3.19 +        try {
    3.20 +            gatekeeper_thread = new thread(gatekeeper, this);
    3.21 +            gatekeeper_thread->detach();
    3.22 +        }
    3.23 +        catch (system_error&) {
    3.24 +
    3.25 +        }
    3.26 +    }
    3.27 +
    3.28  	DECLARE_LIBID(LIBID_pEpCOMServerAdapterLib)
    3.29  	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_PEPCOMSERVERADAPTER, "{B3480081-82C0-4EE4-9AA1-3F513C9D78DD}")
    3.30 +
    3.31 +protected:
    3.32 +    static void gatekeeper(CpEpCOMServerAdapterModule * const self);
    3.33 +
    3.34 +private:
    3.35 +    thread *gatekeeper_thread;
    3.36  };
    3.37  
    3.38 +void CpEpCOMServerAdapterModule::gatekeeper(CpEpCOMServerAdapterModule * const self)
    3.39 +{
    3.40 +    GateKeeper keeper(self);
    3.41 +    keeper.keep();
    3.42 +}
    3.43 +
    3.44  CpEpCOMServerAdapterModule _AtlModule;
    3.45  
    3.46 -
    3.47 -
    3.48  //
    3.49  extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
    3.50  								LPTSTR /*lpCmdLine*/, int nShowCmd)
     4.1 --- a/pEpCOMServerAdapter.vcxproj	Sat May 28 21:02:06 2016 +0200
     4.2 +++ b/pEpCOMServerAdapter.vcxproj	Sun Jun 19 11:27:13 2016 +0200
     4.3 @@ -85,7 +85,7 @@
     4.4        <SubSystem>Windows</SubSystem>
     4.5        <GenerateDebugInformation>true</GenerateDebugInformation>
     4.6        <RegisterOutput>false</RegisterOutput>
     4.7 -      <AdditionalDependencies>comsuppwd.lib;%(AdditionalDependencies)</AdditionalDependencies>
     4.8 +      <AdditionalDependencies>comsuppwd.lib;%(AdditionalDependencies);Wininet.lib</AdditionalDependencies>
     4.9        <PerUserRedirection>true</PerUserRedirection>
    4.10      </Link>
    4.11    </ItemDefinitionGroup>
    4.12 @@ -120,11 +120,12 @@
    4.13        <EnableCOMDATFolding>true</EnableCOMDATFolding>
    4.14        <OptimizeReferences>true</OptimizeReferences>
    4.15        <RegisterOutput>true</RegisterOutput>
    4.16 -      <AdditionalDependencies>comsuppw.lib;%(AdditionalDependencies)</AdditionalDependencies>
    4.17 +      <AdditionalDependencies>comsuppw.lib;%(AdditionalDependencies);Wininet.lib</AdditionalDependencies>
    4.18      </Link>
    4.19    </ItemDefinitionGroup>
    4.20    <ItemGroup>
    4.21      <ClCompile Include="CpEpEngine.cpp" />
    4.22 +    <ClCompile Include="GateKeeper.cpp" />
    4.23      <ClCompile Include="pEpCOMServerAdapter.cpp" />
    4.24      <ClCompile Include="pEpCOMServerAdapter_i.c">
    4.25        <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
    4.26 @@ -151,6 +152,7 @@
    4.27    </ItemGroup>
    4.28    <ItemGroup>
    4.29      <ClInclude Include="CpEpEngine.h" />
    4.30 +    <ClInclude Include="GateKeeper.h" />
    4.31      <ClInclude Include="locked_queue.hh" />
    4.32      <ClInclude Include="pEpCOMServerAdapter_i.h" />
    4.33      <ClInclude Include="pEp_utility.h" />
     5.1 --- a/pEpCOMServerAdapter.vcxproj.filters	Sat May 28 21:02:06 2016 +0200
     5.2 +++ b/pEpCOMServerAdapter.vcxproj.filters	Sun Jun 19 11:27:13 2016 +0200
     5.3 @@ -40,6 +40,9 @@
     5.4      <ClCompile Include="pEp_utility.cpp">
     5.5        <Filter>Source Files</Filter>
     5.6      </ClCompile>
     5.7 +    <ClCompile Include="GateKeeper.cpp">
     5.8 +      <Filter>Source Files</Filter>
     5.9 +    </ClCompile>
    5.10    </ItemGroup>
    5.11    <ItemGroup>
    5.12      <ClInclude Include="stdafx.h">
    5.13 @@ -72,6 +75,9 @@
    5.14      <ClInclude Include="_IpEpEngineEvents_CP.h">
    5.15        <Filter>Generated Files</Filter>
    5.16      </ClInclude>
    5.17 +    <ClInclude Include="GateKeeper.h">
    5.18 +      <Filter>Header Files</Filter>
    5.19 +    </ClInclude>
    5.20    </ItemGroup>
    5.21    <ItemGroup>
    5.22      <ResourceCompile Include="pEpCOMServerAdapter.rc">
     6.1 --- a/stdafx.h	Sat May 28 21:02:06 2016 +0200
     6.2 +++ b/stdafx.h	Sun Jun 19 11:27:13 2016 +0200
     6.3 @@ -27,6 +27,8 @@
     6.4  
     6.5  #include <comutil.h>
     6.6  
     6.7 +#include <Wininet.h>
     6.8 +
     6.9  #include <vector>
    6.10  #include <sstream>
    6.11  #include <string>
    6.12 @@ -35,6 +37,7 @@
    6.13  #include <thread>
    6.14  #include <mutex>
    6.15  #include <atomic>
    6.16 +#include <random>
    6.17  
    6.18  #include <assert.h>
    6.19