📄 ldapauth9x.cpp
字号:
// Set the search preference
hr = pSearchBase->SetSearchPreference( &SearchPrefs, dwNumPrefs);
if (FAILED(hr))
return hr;
// Set attributes to return
CONST DWORD dwAttrNameSize = 1;
LPOLESTR pszAttribute[dwAttrNameSize] = {L"ADsPath"};
// Execute the search
wprintf(pszSearchFilter);
wprintf(L"\n");
hr = pSearchBase->ExecuteSearch(pszSearchFilter,
pszAttribute,
dwAttrNameSize,
&hSearch
);
if (SUCCEEDED(hr))
{
// Call IDirectorySearch::GetNextRow() to retrieve the next row
//of data
while( pSearchBase->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
{
// loop through the array of passed column names,
// print the data for each column
for (DWORD x = 0; x < dwAttrNameSize; x++)
{
// Get the data for this column
hr = pSearchBase->GetColumn( hSearch, pszAttribute[x], &col );
if ( SUCCEEDED(hr) )
{
// Print the data for the column and free the column
// Note the attribute we asked for is type CaseIgnoreString.
wcscpy(szADsPath, col.pADsValues->CaseIgnoreString);
hr = ADsOpenObject(szADsPath,
pwszUser,
pwszPassword,
ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
IID_IADs,
(void**)ppUser);
if (SUCCEEDED(hr))
{
wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString);
hrObj = S_OK;
gbsMember=SysAllocString(col.pADsValues->CaseIgnoreString);
}
pSearchBase->FreeColumn( &col );
}
else
hr = E_FAIL;
}
}
// Close the search handle to clean up
pSearchBase->CloseSearchHandle(hSearch);
}
if (FAILED(hrObj))
hr = hrObj;
return hr;
}
HRESULT FindGroup(IDirectorySearch *pSearchBase, //Container to search
LPOLESTR szFindUser, LPOLESTR pwszUser, LPOLESTR pwszPassword, //Name of user to find.
IADs **ppUser,LPOLESTR szGroup) //Return a pointer to the user
{
HRESULT hrObj = E_FAIL;
HRESULT hr = E_FAIL;
if ((!pSearchBase)||(!szFindUser))
return E_INVALIDARG;
//Create search filter
LPOLESTR pszSearchFilter = new OLECHAR[MAX_PATH];
LPOLESTR szADsPath = new OLECHAR[MAX_PATH];
wcscpy(pszSearchFilter, L"(&(objectClass=group)(cn=");
wcscat(pszSearchFilter,szGroup);
wcscat(pszSearchFilter, L"))");
//Search entire subtree from root.
ADS_SEARCHPREF_INFO SearchPrefs;
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
DWORD dwNumPrefs = 1;
// COL for iterations
ADS_SEARCH_COLUMN col;
// Handle used for searching
ADS_SEARCH_HANDLE hSearch;
// Set the search preference
hr = pSearchBase->SetSearchPreference( &SearchPrefs, dwNumPrefs);
if (FAILED(hr))
return hr;
// Set attributes to return
CONST DWORD dwAttrNameSize = 1;
LPOLESTR pszAttribute[dwAttrNameSize] = {L"ADsPath"};
// Execute the search
hr = pSearchBase->ExecuteSearch(pszSearchFilter,
pszAttribute,
dwAttrNameSize,
&hSearch
);
if (SUCCEEDED(hr))
{
// Call IDirectorySearch::GetNextRow() to retrieve the next row
//of data
while( pSearchBase->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
{
// loop through the array of passed column names,
// print the data for each column
for (DWORD x = 0; x < dwAttrNameSize; x++)
{
// Get the data for this column
hr = pSearchBase->GetColumn( hSearch, pszAttribute[x], &col );
if ( SUCCEEDED(hr) )
{
// Print the data for the column and free the column
// Note the attribute we asked for is type CaseIgnoreString.
wcscpy(szADsPath, col.pADsValues->CaseIgnoreString);
hr = ADsOpenObject(szADsPath,
pwszUser,
pwszPassword,
ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
IID_IADs,
(void**)ppUser);
if (SUCCEEDED(hr))
{
wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString);
hrObj = S_OK;
gbsGroup=SysAllocString(col.pADsValues->CaseIgnoreString);
}
pSearchBase->FreeColumn( &col );
}
else
hr = E_FAIL;
}
}
// Close the search handle to clean up
pSearchBase->CloseSearchHandle(hSearch);
}
if (FAILED(hrObj))
hr = hrObj;
return hr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/* RecursiveIsMember() - Recursively scans the members of passed IADsGroup ptr
and any groups that belong to the passed ptr- for membership
of Passed group.
Will return a TRUE if the member is found in the passed group,
or if the passed member is a member of any group which is a member
of the passed group.
Parameters
IADsGroup * pADsGroup - Group from which to verify members.
LPWSTR pwszMember - LDAP path for object to verify membership.
BOOL bVerbose - IF TRUE, will output verbose information for the scan.
OPTIONAL Parameters
LPOLESTR pwszUser - User Name and Password, if the parameters are not passed,
LPOLESTER pwszPassword - binding will use ADsGetObject; if the parameters are
- specified, ADsOpenObject is used, passing user name and password.
*/
BOOL RecursiveIsMember(IADsGroup * pADsGroup,LPWSTR pwszMemberGUID,LPWSTR pwszMemberPath,
BOOL bVerbose, LPOLESTR pwszUser, LPOLESTR pwszPassword)
{
HRESULT hr = S_OK; // COM Result Code
IADsMembers * pADsMembers = NULL; // Ptr to Members of the IADsGroup
BOOL fContinue = TRUE; // Looping Variable
IEnumVARIANT * pEnumVariant = NULL; // Ptr to the Enum variant
IUnknown * pUnknown = NULL; // IUnknown for getting the ENUM initially
VARIANT VariantArray[FETCH_NUM]; // Variant array for temp holding returned data
ULONG ulElementsFetched = NULL; // Number of elements retrieved
BSTR bsGroupPath = NULL;
BOOL bRet = FALSE;
if(!pADsGroup || !pwszMemberGUID || !pwszMemberPath)
{
return FALSE;
}
// Get the path of the object passed in
hr = pADsGroup->get_ADsPath(&bsGroupPath);
if (!SUCCEEDED(hr))
return hr;
if (bVerbose)
{
WCHAR pwszOutput[2048];
wsprintf(pwszOutput,L"Checking the Group:\n\n%s\n\n for the member:\n\n%s\n\n",bsGroupPath,pwszMemberPath);
PrintBanner(pwszOutput);
}
// Get an interface pointer to the IADsCollection of members
hr = pADsGroup->Members(&pADsMembers);
if (SUCCEEDED(hr))
{
// Query the IADsCollection of members for a new ENUM Interface
// Be aware that the enum comes back as an IUnknown *
hr = pADsMembers->get__NewEnum(&pUnknown);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -