-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathOperationDomainPaths.cpp
107 lines (89 loc) · 3.5 KB
/
OperationDomainPaths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <windows.h>
#include <lmshare.h>
#include <lmapibuf.h>
#include <iads.h>
#include <adshlp.h>
#include <atlBase.h>
#include <dsgetdc.h>
#pragma comment(lib,"activeds.lib")
#pragma comment(lib,"adsiid.lib")
#pragma comment(lib,"netapi32.lib")
#include "OperationDomainPaths.h"
#include "OperationSharePaths.h"
#include "InputOutput.h"
ClassFactory<OperationDomainPaths> * OperationDomainPaths::RegisteredFactory =
new ClassFactory<OperationDomainPaths>(GetCommand());
OperationDomainPaths::OperationDomainPaths(std::queue<std::wstring> & oArgList) : Operation(oArgList)
{
// exit if there are not enough arguments to part
std::vector<std::wstring> sSubArgs = ProcessAndCheckArgs(1, oArgList);
// initialize com only
static HRESULT hComInit = CoInitializeEx(NULL, 0);
// find a domain controller for the specified domain
PDOMAIN_CONTROLLER_INFO tDomainControllerInfo;
if (DsGetDcName(NULL, sSubArgs[0].c_str(), NULL, NULL,
DS_IS_FLAT_NAME | DS_RETURN_DNS_NAME | DS_TRY_NEXTCLOSEST_SITE | DS_FORCE_REDISCOVERY,
&tDomainControllerInfo) != ERROR_SUCCESS)
{
wprintf(L"ERROR: Could not locate domain controller for domain '%s'\n", sSubArgs[0].c_str());
exit(-1);
}
// create a string
std::wstring sPath = std::wstring(L"LDAP://") + (wcsrchr(tDomainControllerInfo->DomainControllerName, '\\') + 1);
// grab the dns suffix for later use
std::wstring sSuffix = tDomainControllerInfo->DomainName;
NetApiBufferFree(tDomainControllerInfo);
// bind to global catalog
CComPtr<IDirectorySearch> oSearch;
if (FAILED(ADsOpenObject(sPath.c_str(), NULL, NULL, ADS_SECURE_AUTHENTICATION,
IID_IDirectorySearch, (void**)&oSearch)))
{
wprintf(L"ERROR: Could not establish search for domain '%s'\n", sSubArgs[0].c_str());
exit(-1);
}
// setup preferences to search entire tree
ADS_SEARCHPREF_INFO SearchPref;
SearchPref.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPref.vValue.dwType = ADSTYPE_INTEGER;
SearchPref.vValue.Integer = ADS_SCOPE_SUBTREE;
// set the search preference.
if (FAILED(oSearch->SetSearchPreference(&SearchPref, 1)))
{
wprintf(L"ERROR: Could not set search preference for domain '%s'\n", sSubArgs[0].c_str());
exit(-1);
}
// create the search filter
WCHAR sSearchFilter[] = L"(&(objectCategory=computer)(|(operatingSystem=*server*)(operatingSystem=*ontap*)(operatingSystem=*netapp*))(!(userAccountControl:1.2.840.113556.1.4.803:=8192))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
// execute the search.
LPWSTR sAttributes[] = { L"cn" };
ADS_SEARCH_HANDLE hSearch;
if (FAILED(oSearch->ExecuteSearch(sSearchFilter, sAttributes, ARRAYSIZE(sAttributes), &hSearch)))
{
wprintf(L"ERROR: Could not execute search for domain '%s'\n", sSubArgs[0].c_str());
exit(-1);
}
// enumerate results
std::vector<std::wstring> sServers;
for (HRESULT hResult = oSearch->GetFirstRow(hSearch); hResult == S_OK; hResult = oSearch->GetNextRow(hSearch))
{
// get the data from the column
ADS_SEARCH_COLUMN oColumn;
if (FAILED(oSearch->GetColumn(hSearch, sAttributes[0], &oColumn)) ||
oColumn.dwADsType != ADSTYPE_CASE_IGNORE_STRING)
{
continue;
}
// add the server to our list
oArgList.push(L"/SharePaths");
oArgList.push(std::wstring(oColumn.pADsValues->CaseIgnoreString) + L"." + sSuffix +
((sSubArgs.size() == 2) ? (L":" + sSubArgs[1]) : L""));
// free the column.
oSearch->FreeColumn(&oColumn);
}
// close search handle
if (oSearch->CloseSearchHandle(hSearch) != NULL)
{
wprintf(L"ERROR: Could not close search for domain '%s'\n", sSubArgs[0].c_str());
exit(-1);
}
};