需要在C ++中检索用户所属的所有组

时间:2012-05-04 14:41:13

标签: c++ winapi ldap

我需要找到特定用户所属的所有组。我正在使用C ++,而不是Powershell,如果这是我道歉的错误论坛。

根据我在网络上发现的内容,我需要检索memberOf属性,但是我收到一个错误,该属性不存在。任何帮助,将不胜感激。这是代码:

HRESULT hrObj = E_FAIL;
HRESULT hr = E_FAIL;
ADS_SEARCHPREF_INFO SearchPrefs;
//  COL for iterations
ADS_SEARCH_COLUMN col;
//  Handle used for searching
ADS_SEARCH_HANDLE hSearch;

//  Search entire subtree from root.
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;

//  Set the search preference.
DWORD dwNumPrefs = 1;
hr = pSearchBase->SetSearchPreference(&SearchPrefs, dwNumPrefs);
if (FAILED(hr))
{
    return hr;
}

//  Create search filter.
LPWSTR pszFormat = L"(&(objectCategory=person)(objectClass=user)(sAMAccountName=%s))";
int len = wcslen(pszFormat) + wcslen(szFindUser) + 1;
LPWSTR pszSearchFilter = new WCHAR[len];
if(NULL == pszSearchFilter)
{
    return E_OUTOFMEMORY;
}

swprintf_s(pszSearchFilter, len, pszFormat, szFindUser);

//  Set attributes to return.
LPWSTR pszAttribute[NUM_ATTRIBUTES] = {L"ADsPath"};

//  Execute the search.
hr = pSearchBase->ExecuteSearch(pszSearchFilter,
                                pszAttribute,
                                NUM_ATTRIBUTES,
                                &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 and
        //  print the data for each column.
        for (DWORD x = 0; x < NUM_ATTRIBUTES; 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.
                //  Be aware that the requested attribute is type CaseIgnoreString.
                if (ADSTYPE_CASE_IGNORE_STRING == col.dwADsType)
                {
                    IADs *pADS;
                    hr = ADsOpenObject( col.pADsValues->CaseIgnoreString,
                        L"Administrator",
                        L"passW0rd",
                        ADS_SECURE_AUTHENTICATION,
                        IID_IADs,
                        (void**)&pADS);

                    VARIANT var;
                    VariantInit(&var);
                    if (SUCCEEDED(hr))
                    {
                        hr = pADS->GetEx(L"memberOf", &var);  <-- FAILS!!!
                        wprintf(L"Found User.\n",szFindUser); 
                        wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString); 
                        hrObj = S_OK;
                    }
                }

                pSearchBase->FreeColumn( &col );
            }
            else
            {
                hr = E_FAIL;
            }
        }
    }
    //  Close the search handle to cleanup.
    pSearchBase->CloseSearchHandle(hSearch);
}

delete pszSearchFilter;

if (FAILED(hrObj))
{
    hr = hrObj;
}

2 个答案:

答案 0 :(得分:7)

除非您直接使用AD,否则使用Windows Net *功能可能更容易:

#include <windows.h>
#include <lm.h>
#include <stdio.h>

int main() {
    wchar_t user[256];
    DWORD size = sizeof(user)/sizeof(user[0]);
    GetUserNameW(user, &size);

    printf("User: %S\n", user);

    printf("Local groups: \n");

    LPBYTE buffer;
    DWORD entries, total_entries;

    NetUserGetLocalGroups(NULL, user, 0, LG_INCLUDE_INDIRECT, &buffer, MAX_PREFERRED_LENGTH, &entries, &total_entries);

    LOCALGROUP_USERS_INFO_0 *groups = (LOCALGROUP_USERS_INFO_0*)buffer;
    for (int i=0; i<entries; i++)
        printf("\t%S\n", groups[i].lgrui0_name);
    NetApiBufferFree(buffer);

    printf("Global groups: \n");

    NetUserGetGroups(NULL, user, 0, &buffer, MAX_PREFERRED_LENGTH, &entries, &total_entries);

    GROUP_USERS_INFO_0 *ggroups = (GROUP_USERS_INFO_0*)buffer;
    for (int i=0; i<entries; i++)
        printf("\t%S\n", ggroups[i].grui0_name);
    NetApiBufferFree(buffer);

    return 0;
}

答案 1 :(得分:3)

感谢您的回复,我想我找到了我在MSDN中寻找的内容。

HRESULT CheckUserGroups(IADsUser *pUser)
{

    IADsMembers *pGroups;
    HRESULT hr = S_OK;

    hr = pUser->Groups(&pGroups);
    pUser->Release();
    if (FAILED(hr)) return hr;

    IUnknown *pUnk;
    hr = pGroups->get__NewEnum(&pUnk);
    if (FAILED(hr)) return hr;
    pGroups->Release();

    IEnumVARIANT *pEnum;
    hr = pUnk->QueryInterface(IID_IEnumVARIANT,(void**)&pEnum);
    if (FAILED(hr)) return hr;

    pUnk->Release();

    // Enumerate.
    BSTR bstr;
    VARIANT var;
    IADs *pADs;
    ULONG lFetch;
    IDispatch *pDisp;

    VariantInit(&var);
    hr = pEnum->Next(1, &var, &lFetch);
    while(hr == S_OK)
    {
        if (lFetch == 1)
        {
             pDisp = V_DISPATCH(&var);
             pDisp->QueryInterface(IID_IADs, (void**)&pADs);
             pADs->get_Name(&bstr);
             printf("Group belonged: %S\n",bstr);
             SysFreeString(bstr);
             pADs->Release();
        }
        VariantClear(&var);
        pDisp=NULL;
        hr = pEnum->Next(1, &var, &lFetch);
    };
    hr = pEnum->Release();
    return S_OK;
}