Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to retrieve all groups a user belongs to... in C++

Tags:

c++

ldap

winapi

I need to find all the groups a particular user is a member of. I'm using C++, not Powershell, if this is the wrong forum I apologize.

From what I've found on the web I need to retrieve the memberOf property, but I get an error that the property doesn't exist. Any help would be appreciated. Here's the code:

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;
}
like image 254
user1375218 Avatar asked May 04 '12 14:05

user1375218


2 Answers

Unless you're set on using AD directly, it's probably easier to use the Windows Net* functions for the job:

#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;
}
like image 150
Jerry Coffin Avatar answered Oct 03 '22 15:10

Jerry Coffin


Thanks for the reply, I think I found what I was looking for in 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;
}
like image 42
user1375218 Avatar answered Oct 03 '22 14:10

user1375218