$theTitle=wp_title(" - ", false); if($theTitle != "") { ?>
About Virtualization, VDI, SBC, Application Compatibility and anything else I feel like
30 Mar // php the_time('Y') ?>
Previously I discussed IDirectoryObject, today I will show how to change a user’s password with IDirectoryObject.
I didn’t find any documentation except a kb article describing how to use pure ldap to do it. Of course I could have used IADsUser::SetPassword but I decided not to because of the following reasons:
All example code I found was .NET based using the .NET wrappers for Active Directory and seemed to be meant for use in Adam rather than full Active Directory (it set port number to 389 and password mode to cleartext).
In the end it’s not very difficult but nonetheless it took me a while before I got it right.
We can write to the unicodePwd attribute which wants the password as a double quoted unicode string. If you look at this attribute with AdsiEdit you’ll see that the type is Octet String and that it can be written only.
I was tricked with Delphi’s QuotedStr function for a while because it doesn’t return a double but single quoted string π
Below a small snippet from the upcoming JwsclActiveDirectory that shows how to use it: (more…)
28 Mar // php the_time('Y') ?>
A few days ago I wrote about Using Windows Dialogs in your own programs, wouldn’t it be nice to be able to use Windows Resource Strings for the same reasons?
Loading a resource string is not difficult, let’s look at some examples:
1 2 3 4 5 6 7 8 9 10 11 12 | function LoadResourceString(const DllName: String; ResourceId: Integer): String; var hDLL: THandle; Buffer: array[0..MAX_PATH] of Char; begin hDLL := LoadLibrary(PChar(DllName)); if hDLL = 0 then Exit; if LoadString(hDll, ResourceId, Buffer, Length(Buffer)) > 0 then Result := Buffer; end; |
This uses the LoadString api to load a Resource String from an Executable or Dll by it’s resource Id. An Example might call might be:
1 | LoadResourceString('dsadmin.dll', 226) |
This loads the string with ResourceId 226 from dsadmin.dll(.mui):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | STRINGTABLE LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US { 224, "Windows cannot complete the password change for %2 because:\n%1" 225, "Windows cannot access object %2 because:\n%1The object may have been deleted by another administrator in this enterprise." 226, "The New and Confirm passwords must match. Please re-type them." 228, "Object %2 has been disabled." 229, "Windows cannot disable object %2 because:\n%1" 231, "Object %2 has been enabled." 232, "Windows cannot enable object %2 because:\n%1" 233, "Create a new object..." 237, "Mo&ve...\nMoves the selected object" 238, "The password for %2 cannot be set due to insufficient privileges. Windows will attempt to disable this account. If this attempt fails, the account will become a security risk. Contact an administrator as soon as possible to repair this. Before this user can log on, the password should be set, and the account must be enabled." } |
As you can see in this example, some resource strings have identifiers such as %1 and %2 which are used in the FormatMessage Api. How can we use that from Delphi?
I wrote a very simple wrapper for it:
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 | function FormatMsg(const Source: String; const Args: array of const): String; var i: Integer; ArgArray: array of Pointer; Buffer: PChar; begin for i := Low(Args) to High(Args) do begin SetLength(ArgArray, i+1); case Args[i].VType of vtExtended:; // not supported, should raise Exception else ArgArray[i] := Args[i].VPointer; end; end; if FormatMessage(FORMAT_MESSAGE_FROM_STRING or FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_ARGUMENT_ARRAY, PChar(Source), 0, 0, @Buffer, 0, @ArgArray[0]) > 0 then begin Result := Buffer; // replace \n (linefeed) with #13#10 Result := StringReplace(Result, '\n', #13#10, [rfReplaceAll]); LocalFree(DWORD(Buffer)); end else begin SetLength(Result, 0); end; end; |
And here is a usage example:
1 2 3 | Memo1.Lines.Add (FormatMsg( 'Windows cannot complete the password change for %2 because:\n%1', ['the password doesn''t meet complexity requirements', 'John Doe'])); |
The Result of this is:
Windows cannot complete the password change for John Doe because:
the password doesn’t meet complexity requirements
24 Mar // php the_time('Y') ?>
Today I reused a unit I wrote a long time ago for TSAdminEx that shows Resource Dialogs from DLL’s or Executables. I wrote it for a couple of reasons:
The code is hardly rocket science and could probably be improved and made more sophisticated but it works for me. I decided to share it since you may find it usefull.
Here is a small usage example that shows the Reset Password dialog from Active Directory Users & Computers. This dialog is in dsadmin.dll (on Windows Vista/7 you will find it in ds.admin.dll.mui in the language subfolder eg %systemroot%\system32\en-US but you can load it using just the dll name).
It looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 215 DIALOGEX 0, 0, 252, 139 STYLE DS_MODALFRAME | DS_CONTEXTHELP | WS_POPUP | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_CONTEXTHELP CAPTION "Reset Password" LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US FONT 8, "MS Shell Dlg" { CONTROL "&New password:", -1, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 7, 10, 79, 10 CONTROL "", 220, EDIT, ES_LEFT | ES_PASSWORD | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 92, 7, 153, 14 CONTROL "&Confirm password:", -1, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 7, 28, 79, 10 CONTROL "", 222, EDIT, ES_LEFT | ES_PASSWORD | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP, 92, 25, 153, 14 CONTROL "&User must change password at next logon", 261, BUTTON, BS_AUTOCHECKBOX | BS_LEFT | BS_TOP | BS_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 7, 46, 237, 10 CONTROL "The user must logoff and then logon again for the change to take effect.", -1, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 14, 61, 231, 8 CONTROL "", 8327, STATIC, SS_LEFT | WS_CHILD | WS_VISIBLE | WS_GROUP, 7, 76, 238, 16 CONTROL "Unlock the user's &account", 8328, BUTTON, BS_AUTOCHECKBOX | BS_LEFT | BS_TOP | BS_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 14, 96, 230, 10 CONTROL "OK", 1, BUTTON, BS_DEFPUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 140, 118, 50, 14 CONTROL "Cancel", 2, BUTTON, BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 195, 118, 50, 14 } |
22 Mar // php the_time('Y') ?>
Last time I talked briefly about IDirectoryObject and IDirectorySearch, let’s go into a little more detail today.
IDirectoryObject is an Interface that we can use to query anything in Active Directory, users, groups, organizational units, containers and so on.
I thought the best explanation would be to build a very small sample project, so let’s do that!
First we need some units, so please add the following units to your uses clause:
Next declare the following types:
1 2 3 4 5 6 7 | type // For Delphi < 2009 use WideString UString = {$IFDEF UNICODE}UnicodeString{$ELSE}WideString{$ENDIF}; // Array of ADS_ATTR_INFO records TAdsAttrInfoArray = array[0..ANYSIZE_ARRAY-1] of ADS_ATTR_INFO; PAdsAttrInfoArray = ^TAdsAttrInfoArray; |
12 Mar // php the_time('Y') ?>
If you have ever used Adsi you have probably used the IADs interface or derived interfaces such as IADsUser or IADsGroup (maybe even without realising this).
What you need to know is that these interfaces were created to support scripting languages such as VBScript. The reason is that these scripting language have no support at all for structures such as ADSVALUE and don’t work with Pointers.
A typical use of IADs interface would look like this (in Delphi and using Jwa):
1 2 3 4 5 6 7 8 9 10 | var IADsIntf: IADs; hr: HRESULT; begin hr := AdsGetObject( 'LDAP://CN=AUser,CN=Users,DC=domain,DC=local', //lpszPathName IID_IADs, // riid Pointer(IADsIntf)); // ppObject if SUCCEEDED(hr) then ... |
The IADs interfaces are fine when you are working with a single object but they are very, very slow, when working with many objects. I also find them a pain to work with as only a few AD attributes are present as properties. For other attributes you need to call the Get method which doesn’t always work, in which case you probably need to call the GetEx method. Even the GetEx method doesn’t always return the desired result as the property might not be in the Cache in which case we need to call the GetInfoEx method first and then Get or GetEx.
Active Directory has the nasty habit of failing when a an attribute is not set, so if you are reading a eg string attribute you probably expect an empty string but Active Directory returns a failure in such a case. And since Delphi declares Get(Ex) as SafeCall it will raise an Exception so you need to wrap it in try..except.
If we have obtained a value it will always be a variant that we probably need to convert to another type such as a string, datetime or an integer.
My results with implementing IADs interfaces in my Active Directory unit were bad: I wrote a test program that mimics Active Directory Users & Computer and enumerating a Container or OU with about 70 users takes 2-3 seconds. If you need to wait that long when expaning a Tree Node this in simply not acceptable. So I decided to completely drop the IADs interfaces and used the interfaces that are meant for higher level languages such as IDirectoryObject and IDirectorySearch. And guess what? Now my Delphi program, even when running in the debugger, is actually much faster than Active Directory Users & Computers!
To be fair to Microsoft, in the documentation of IDirectoryObject is the following note: The IDirectorySearch interface is a pure COM interface that provides a low overhead method that non-Automation clients can use to perform queries in the underlying directory.
In the next posts I will talk about IDirectoryObject and IDirectorySearch.
10 Mar // php the_time('Y') ?>
I am working on an Customer Management Console that will present all adminstrative tasks that customers will need in their environment in a single console.
It will handle Active Directory, Terminal Server and Citrix, Printers and will offer specific Views and Reporting. For the Active Directory stuff I decided to create some classes that enabled me to work with AD in a more Delphi OOP way.
In a series of Blog Posts I will write about interesting things or just random notes that I made while creating this stuff. The intention is to publish the whole unit in the Jedi Security Library when it’s finished.
Well I hope you that you’ll find some things of interest π
Part 1 is here .
10 Mar // php the_time('Y') ?>
If you are going to use the Active Directory Service Interface (ADSI) in Delphi, the first thing you will need is the typelibrary (TLB). This TLB is in the windows\system32 folder and has the name activeds.tlb.
We can import this tlb in Delphi (the procedure differs somewhat, depending on the Delphi version), but there are quite some problems with the resulting pas file of this import:
A version of the imported tlb is also in the Jedi Apilib (JwaAdsTLB) and basically it had the same errors. Because I was wondering how this would work in c++ I checked the SDK and found the header file Iads.h.