limi.eu

set user’s profile path in Active Directory

set user’s profile path in Active Directory

With the following function you can set user’s profile path in Active Directory.

Example:

if (SetProfilePath(L"Thomas",L"OU=Office,DC=dom,DC=my-company,DC=com",
        L"serverprofiles",L"admin",L"secret"))
{... };

In the end it’s pretty simple.

#include <stdio.h>
#include <tchar.h>
#include <iads.h>
#include <activeds.h>

BOOL SetProfilePath(LPWSTR sUserName, LPWSTR sOU, LPWSTR sProfilePath
        LPWSTR sAdminUserName, LPWSTR sAdminPwd)
{
    LPWSTR sLDAP_Path = (LPWSTR)malloc(1024*sizeof(LPWSTR));
    HRESULT hr;
    IADsUser *pUser = NULL;
    BSTR bstrProfilePath;

    // First, bind to the destination container.
    wsprintf(sLDAP_Path, L"LDAP://CN=%s,%s", sUserName, sOU);
    CoInitialize(NULL);
    hr = ADsOpenObject(sLDAP_Path,
        sAdminUserName,
        sAdminPwd,
        ADS_SECURE_AUTHENTICATION,
        IID_IADsUser,
        (void**) &pUser );

    if ( !SUCCEEDED(hr) )
    {
        if(pUser) pUser->Release();
        CoUninitialize();
        free(sLDAP_Path);
        return FALSE;
    }

    // Second, set profile path.
    bstrProfilePath = SysAllocString(strProfilePath);
    hr = pUser->put_Profile(bstrProfilePath);
    SysFreeString(bstrProfilePath);

    // make things happen
    hr = pUser->SetInfo();

    if (pUser) pUser->Release();
        CoUninitialize();

    if (SUCCEEDED(hr) )
    {
        return TRUE;
    } else {
        return FALSE;
    }
}

In a similar way you can use all the other methods of a IADsUser, e.g. set the user’s full name or password. Those functions were used in a modified version of pGina 2.x together with my enhanced SSH plugin for pGina in various student labs I managed at the Dresden University of Technology from 2006 until 2014, when they switched to Windows 7.