Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmauro committed Oct 20, 2018
1 parent 27cc25f commit 1a38d42
Show file tree
Hide file tree
Showing 41 changed files with 12,579 additions and 0 deletions.
1,493 changes: 1,493 additions & 0 deletions AdsHelper.cpp

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions AdsHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2014-2015 Mauro H. Leggieri, Buenos Aires, Argentina.
* All rights reserved.
*
**/

#ifndef _ADS_HELPER_H
#define _ADS_HELPER_H

#include <Defines.h>
#include <Windows.h>
#include <objbase.h>
#include <Iads.h>
#include <ActiveDS.h>
#include "Sid.h"
#include <ArrayList.h>

//-----------------------------------------------------------

class CAdsHelper : public MX::CBaseMemObj
{
public:
CAdsHelper();
~CAdsHelper();

#if defined(TRAPMINEENTERPRISESERVICE) || defined(TRAPMINESERVICE)
HRESULT InitializeFromRegistry();
#endif //TRAPMINEENTERPRISESERVICE || TRAPMINESERVICE
HRESULT Initialize(_In_opt_z_ LPCWSTR szServerAddressW=NULL, _In_opt_z_ LPCWSTR szUsernameW=NULL,
_In_opt_z_ LPCWSTR szPasswordW=NULL);

VOID SetCancelEvent(_In_ HANDLE hEvent);
VOID SetQueryTimeoutMs(_In_ DWORD dwTimeoutMs);

HRESULT GetAllUsers(_Inout_ MX::TArrayListWithFree<LPWSTR> &aUsersList);
HRESULT GetAllGroups(_Inout_ MX::TArrayListWithFree<LPWSTR> &aGroupsList);
HRESULT GetAllComputers(_Inout_ MX::TArrayListWithFree<LPWSTR> &aComputersList);

HRESULT GetSidFromADsPath(_In_z_ LPCWSTR szADsPathW, _Inout_ CSid &cSid);
HRESULT GetDomainFromUrl(_In_z_ LPCWSTR szUrlW, _Inout_ MX::CStringW &cStrDomainW);
HRESULT GetDomainFromDn(_In_z_ LPCWSTR szDistinguishedNameW, _Inout_ MX::CStringW &cStrDomainW);

HRESULT GetRootADSPath(_Inout_ MX::CStringW &cStrRootPathW);

HRESULT GetComputerSids(_Out_ CSid **lplpComputerSid, _Inout_ MX::TArrayListWithDelete<CSid*> &aGroupSids);

HRESULT EnumerateContainerFolders(_In_z_ LPCWSTR szParentW, _Inout_ MX::TArrayListWithFree<LPWSTR> &aChildrenList);
HRESULT GetContainerMembers(_In_z_ LPCWSTR szParentW, _Inout_ MX::TArrayListWithFree<LPWSTR> &aMembersList);

static HRESULT GetUrlFromDn(_In_ LPCWSTR szDnW, _Inout_ MX::CStringW &cStrW);
static HRESULT GetUrlFromDn(_Inout_ MX::CStringW &cStrW);
static LPCWSTR GetDnFromUrl(_In_ LPCWSTR szUrlW);

private:
static BOOL EscapeSlashes(_Inout_ MX::CStringW &cStrW);
HRESULT AdsOpen(_In_z_ LPCWSTR szPathNameW, _In_ REFIID riid, __deref_out LPVOID *ppObject);
BOOL IsCancelled();

private:
MX::CStringW cStrServerAddressW, cStrUserNameW, cStrPasswordW;
HRESULT hResComInit;
HANDLE hCancelEvent;
DWORD dwQueryTimeoutMs;
};

//-----------------------------------------------------------

#endif //_ADS_HELPER_H
21 changes: 21 additions & 0 deletions AutoComInit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "AutoComInit.h"

//-----------------------------------------------------------

CAutoComInit::CAutoComInit()
{
hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
return;
}

CAutoComInit::~CAutoComInit()
{
if (SUCCEEDED(hRes))
::CoUninitialize();
return;
}

HRESULT CAutoComInit::InitResult() const
{
return (SUCCEEDED(hRes) || hRes == RPC_E_CHANGED_MODE) ? S_OK : hRes;
}
29 changes: 29 additions & 0 deletions AutoComInit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2014-2015 Mauro H. Leggieri, Buenos Aires, Argentina.
* All rights reserved.
*
**/

#ifndef _AUTO_COM_INITIALIZE_H
#define _AUTO_COM_INITIALIZE_H

#include <Windows.h>
#include <Ole2.h>

//-----------------------------------------------------------

class CAutoComInit
{
public:
CAutoComInit();
~CAutoComInit();

HRESULT InitResult() const;

private:
HRESULT hRes;
};

//-----------------------------------------------------------

#endif //_AUTO_COM_INITIALIZE_H
82 changes: 82 additions & 0 deletions Console.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2014-2015 Mauro H. Leggieri, Buenos Aires, Argentina.
* All rights reserved.
*
**/

#include "Console.h"
#include <WaitableObjects.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>

//-----------------------------------------------------------

static LONG volatile nEnabled = 0;

//-----------------------------------------------------------

namespace Console {

VOID Initialize(_In_ BOOL bAppIsInteractive)
{
#pragma warning(suppress: 6031)
_setmode(_fileno(stdout), _O_U16TEXT);
_InterlockedExchange(&nEnabled, (bAppIsInteractive != FALSE) ? 1 : 0);
return;
}

VOID Print(_In_ eConsoleColor nColor, _In_ LPCWSTR szFormatW, ...)
{
static LONG volatile nMutex = 0;
static HANDLE hConsoleOut = NULL;
CONSOLE_SCREEN_BUFFER_INFO sCsbi;
va_list args;

if (__InterlockedRead(&nEnabled) != 0)
{
MX::CFastLock cLock(&nMutex);

if (__InterlockedRead(&nEnabled) != 0)
{
if (hConsoleOut == NULL)
hConsoleOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (nColor != ccNormal)
{
::GetConsoleScreenBufferInfo(hConsoleOut, &sCsbi);
switch (nColor)
{
case ccError:
::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_RED|FOREGROUND_INTENSITY);
break;
case ccSuccess:
::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
break;
case ccYellow:
::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
break;
case ccBlue:
::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_BLUE|FOREGROUND_INTENSITY);
break;
}
}
va_start(args, szFormatW);
vwprintf_s(szFormatW, args);
va_end(args);
if (nColor != ccNormal)
::SetConsoleTextAttribute(hConsoleOut, sCsbi.wAttributes);
}
}
return;
}

VOID PrintError(_In_ HRESULT hRes)
{
if (SUCCEEDED(hRes))
Console::Print(eConsoleColor::ccSuccess, L"OK");
else
Console::Print(eConsoleColor::ccError, L"ERROR: 0x%08X", hRes);
return;
}

}; //namespace Console
31 changes: 31 additions & 0 deletions Console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2014-2015 Mauro H. Leggieri, Buenos Aires, Argentina.
* All rights reserved.
*
**/

#ifndef _CONSOLE_H
#define _CONSOLE_H

#include <Defines.h>

//-----------------------------------------------------------

typedef enum {
ccNormal, ccError, ccSuccess, ccYellow, ccBlue
} eConsoleColor;

//-----------------------------------------------------------

namespace Console {

VOID Initialize(_In_ BOOL bAppIsInteractive);

VOID Print(_In_ eConsoleColor nColor, _In_ LPCWSTR szFormatW, ...);
VOID PrintError(_In_ HRESULT hRes);

}; //namespace Console

//-----------------------------------------------------------

#endif //_CONSOLE_H
Loading

0 comments on commit 1a38d42

Please sign in to comment.