@@ -20,11 +20,13 @@
#include "Stdafx.h"
#include "FilterListener.h"
#include <mydlp_common.h>
#include "MiniFilterController.h"

using namespace System::Runtime::InteropServices;
using namespace MyDLP::EndPoint::Core;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
using namespace System;

namespace MyDLPEP
{
@@ -179,8 +181,8 @@ DWORD ListenerWorker(__in PMYDLPMF_THREAD_CONTEXT Context)

#pragma warning(push)
#pragma warning(disable:4127) // conditional expression is constant

while (TRUE) {
while (TRUE)
{
#pragma warning(pop)
result = GetQueuedCompletionStatus( Context->Completion, &outSize, &key, &pOvlp, INFINITE );
message = CONTAINING_RECORD( pOvlp, MYDLPMF_MESSAGE, Ovlp );
@@ -195,47 +197,56 @@ DWORD ListenerWorker(__in PMYDLPMF_THREAD_CONTEXT Context)
notification = (PMYDLPMF_NOTIFICATION)&message->Notification;

//Archive inbound files if enabled
if(notification->Type == POSTCREATE) {
if(notification->Type == POSTCREATE)
{
fileNotification = (PMYDLPMF_FILE_NOTIFICATION)notification;
listener = MyDLPEP::FilterListener::getInstance();
action = listener->HandleFileOpen(fileNotification ->FileName);


}
//Control write outbound files
} else if (notification->Type == PREWRITE) {
else if (notification->Type == PREWRITE)
{
writeNotification = (PMYDLPMF_WRITE_NOTIFICATION)notification;
listener = MyDLPEP::FilterListener::getInstance();

if (writeNotification->BytesToScan > MYDLPMF_READ_BUFFER_SIZE)
{
MyDLP::EndPoint::Core::Logger::GetInstance()->Error("ListenerWorker error writeNotification->BytesToScan > MYDLPMF_READ_BUFFER_SIZE");
}
action = listener->HandleFileWrite(writeNotification->FileName, writeNotification->Contents, writeNotification->BytesToScan);

//Notify file outbound file operation ending
} else if (notification->Type == PRECLEANUP) {
action = listener->HandleFileWrite(writeNotification->FileName, writeNotification->Contents, writeNotification->BytesToScan);
}
//Notify file outbound file operation ending
else if (notification->Type == PRECLEANUP)
{
fileNotification = (PMYDLPMF_FILE_NOTIFICATION)notification;
listener = MyDLPEP::FilterListener::getInstance();
listener->HandleFileCleanup(fileNotification->FileName);

listener->HandleFileCleanup(fileNotification->FileName);
}
//Send configuration to minifilter
} else if (notification->Type == CONF){
confRequest = true;

else if (notification->Type == CONF)
{
confRequest = true;
}
//USBSAC if enabled
} else if (notification->Type == USBSAC){
else if (notification->Type == USBSAC)
{
if(MyDLP::EndPoint::Core::USBController::IsUsbBlocked())
{
action = FileOperation::Action::BLOCK;
}
else
{
action = FileOperation::Action::ALLOW;
}
}
}
//USBMOUNT if USBSAC enabled
} else if (notification->Type == USBMOUNT){
else if (notification->Type == USBMOUNT)
{
MyDLP::EndPoint::Core::Logger::GetInstance()->Debug("New USB mass strorage mounted");
MyDLP::EndPoint::Core::USBController::GetUSBStorages();
//threr is no actual file operation just convenience
//there is no actual file operation just convenience
action = FileOperation::Action::NOACTION;
}

@@ -271,15 +282,31 @@ DWORD ListenerWorker(__in PMYDLPMF_THREAD_CONTEXT Context)
}
}
else
{
{
MyDLP::EndPoint::Core::Logger::GetInstance()->Info("Filter conf init");

//Check config errors
if (MyDLPEP::MiniFilterController::configAttempt > 5)
{
//Catastrophic failure: stoping service MyDLP EP Win
//MyDLP Ep Win will be restarted by MyDLP Watchdog service

MyDLP::EndPoint::Core::Logger::GetInstance()->Error("Fatal error in filter conf init, maximum attempts exceeded");

//Stop engine first
MyDLP::EndPoint::Core::Engine::Stop();
Environment::Exit(-1);

}
MyDLPEP::MiniFilterController::configAttempt++;

confRequest = 0;
result = FALSE;
confMessage.ReplyHeader.Status = 0;
confMessage.ReplyHeader.MessageId = message->MessageHeader.MessageId;

//Set Confuration of minifilter
MyDLP::EndPoint::Core::Configuration::SetPids();
confMessage.Reply.Pid = MyDLP::EndPoint::Core::Configuration::ErlPid;
MyDLP::EndPoint::Core::Logger::GetInstance()->Info("New USBSerialAC:" + MyDLP::EndPoint::Core::Configuration::UsbSerialAccessControl);
MyDLP::EndPoint::Core::Logger::GetInstance()->Info("New ArchiveInbound:" + MyDLP::EndPoint::Core::Configuration::ArchiveInbound);
@@ -47,6 +47,9 @@ namespace MyDLPEP
void MiniFilterController::Start()
{
Logger::GetInstance()->Debug("Installing mydlpmf service");

configAttempt = 0;

const LPCTSTR DRV_NAME = _T("MyDLPMF");
RegistryKey ^key = nullptr;

@@ -119,8 +122,11 @@ namespace MyDLPEP
CloseServiceHandle(hSCManager);
Logger::GetInstance()->Error("Unable to start MyDLPMF service, win error no" + gcnew Int32(GetLastError()));
return;
} else
}
else
{
Logger::GetInstance()->Error("MyLDLPMF service already running");
}
}
else
{
@@ -29,10 +29,11 @@ namespace MyDLPEP

private:
MiniFilterController( void );
static MiniFilterController ^controller = nullptr;
static MiniFilterController ^controller = nullptr;

public:
static MiniFilterController ^GetInstance();
static int configAttempt = 0;
void Start();
void Stop();
};
@@ -24,6 +24,7 @@
using System.Diagnostics;
using System.Timers;
using System.ServiceProcess;
using Microsoft.Win32;

namespace MyDLP.EndPoint.Service
{
@@ -55,6 +56,31 @@ public void Start()
{
//notify logger that we are in main service
Logger.GetInstance().InitializeMainLogger(serviceLogger);

//Keep watchdog tied up during debugging
if (System.Environment.UserInteractive == false)
{

ServiceController service = new ServiceController("mydlpepwatchdog");
try
{
if (!service.Status.Equals(ServiceControllerStatus.Running) && !service.Status.Equals(ServiceControllerStatus.StartPending))
{
Logger.GetInstance().Info("Starting mydlpepwatchdog at start up");
service.Start();
}
}
catch (Exception e)
{
Logger.GetInstance().Error("Unable to start watchdog" + e.Message);
}
//enable watchdog check

Logger.GetInstance().Info("Watchdog check enabled");
watchdogTimer = new Timer(watchdogTimerPeriod);
watchdogTimer.Elapsed += new ElapsedEventHandler(OnTimedWatchdogEvent);
watchdogTimer.Enabled = true;
}

Logger.GetInstance().Info("Starting mydlpepwin service");

@@ -69,9 +95,9 @@ public void Start()

Configuration.GetUserConf();
Configuration.StartTime = DateTime.Now;
engine = new Engine();
engine.Start();
Configuration.setPids();
Engine.Start();
Configuration.SetPids();

Logger.GetInstance().Debug("mydlpepwin tries to install mydlpmf");
MyDLPEP.MiniFilterController.GetInstance().Start();
@@ -92,6 +118,7 @@ public void Start()
if (!testSuccess)
{
Logger.GetInstance().Error("Seap connection test failed");
Stop();
}

if (Configuration.PrinterMonitor)
@@ -104,31 +131,23 @@ public void Start()
Core.USBController.Activate();
Core.USBController.GetUSBStorages();
}
}

//Keep watchdog tied up during debugging
if (System.Environment.UserInteractive == false)
{
//enable watchdog check

Logger.GetInstance().Info("Watchdog check enabled");
watchdogTimer = new Timer(watchdogTimerPeriod);
watchdogTimer.Elapsed += new ElapsedEventHandler(OnTimedWatchdogEvent);
watchdogTimer.Enabled = true;
}
}

//initialize configuration timer
Logger.GetInstance().Info("Configuration check enabled");
confTimer = new Timer(confCheckTimerPeriod);
confTimer.Elapsed += new ElapsedEventHandler(OnTimedConfCheckEvent);
confTimer.Enabled = true;

//GetLoggedOnUser
//This is not working hidden form required
//SystemEvents.SessionSwitch += new SessionSwitchEventHandler(Configuration.LoggedOnUserChangeHandler);
}

public void Stop()
{
MyDLPEP.MiniFilterController.GetInstance().Stop();
engine.Stop();
Engine.Stop();

if (Configuration.UsbSerialAccessControl)
{
@@ -156,9 +175,9 @@ private void OnTimedWatchdogEvent(object source, ElapsedEventArgs e)
service.Start();
}
}
catch
catch (Exception ex)
{
//todo:
Logger.GetInstance().Error("Unable to start watchdog" + ex.Message);
}
}

@@ -168,9 +187,15 @@ private void OnTimedConfCheckEvent(object source, ElapsedEventArgs e)
bool oldPrinterMonitor = Configuration.PrinterMonitor;
bool oldArchiveInbound = Configuration.ArchiveInbound;

if (Configuration.GetLoggedOnUser() == "NO OWNER")
{
Configuration.ResetLoggedOnUser();
}

if (SeapClient.HasNewConfiguration())
{
Logger.GetInstance().Info("New configuration notified.");

Configuration.GetUserConf();

if (Configuration.UsbSerialAccessControl && !oldUSBSerialAC)
@@ -60,12 +60,8 @@ public void Start()
serviceLogger.WriteEntry("Unable to get mydlp registry configuration, mydlpepwatchdog service stopped", EventLogEntryType.Error);
Logger.GetInstance().Error("Unable to get mydlp registry configuration, mydlpepwatchdog service stopped");
Environment.Exit(1);
}
else
{
Configuration.setPids();
}

}

Configuration.GetUserConf();

watchdogTimer = new Timer(watchdogTimerPeriod);
@@ -78,6 +74,9 @@ public void Start()
private void OnTimedWatchdogEvent(object source, ElapsedEventArgs e)
{
Logger.GetInstance().CheckLogLimit();

//Check upto date pids in file
Configuration.SetPids();

bool error = false;

@@ -135,7 +134,7 @@ private void OnTimedWatchdogEvent(object source, ElapsedEventArgs e)
service.WaitForStatus(ServiceControllerStatus.Running);
//Wait for proper initialisation of java and erlang
System.Threading.Thread.Sleep(20000);
Configuration.setPids();
Configuration.SetPids();
}
}
catch (Exception ex)