Active Directory Service Interfaces
Active Directory Service Interfaces
Interfaces
Author: Deepak Shenoy
[email protected]
Agni Software Pvt. Ltd.
Introduction
Prerequisites
What is ADSI?
Why should you use ADSI?
Where is ADSI used?
ADSI Architecture
Overview
COM Object Model
ADSI Schema
Providers
Conclusion
References
1. Microsoft Exchange Server: Allows queries for email ids, names and a
number of other attributes of people or groups across an enterprise.
2. Microsoft Site Server: Stores the list of users as an Active Directory.
3. Microsoft Windows NT: This gives you uniform access to users (earlier
through User Manager for Domains), services (earlier through service
manager) and network resources (computers, printers etc.)
Active Directory also provides bridges to access other similar directory services, like
LDAP, NDS (Novell Directory Services) etc.
Active Directory is also an integral part of Windows 2000. In a large organization,
Windows NT server has been used as a Primary or Backup Domain Controller, but it
was difficult to integrate many such controllers to be able to provide security and
privileges across the organization. Active Directory makes it simpler by allowing you
to structure your organization into units which seamlessly (or so they say) integrate
with each other. Which also means lesser problems when you expand, add more
servers, more users etc.
To know more about how you can use Active Directory effectively in your
organization, please visit
http://www.microsoft.com/windows2000/library/technologies/activedirectory/defaul
t.asp
Here's a list of places ADSI would be best used in:
These are just a few applications, and I'm sure there will be more as time goes on.
What is ADSI?
Directory Services and Namespaces
There could be many objects within a namespace (users in an email server, files in
a file system) which need to be uniquely identifiable by name. But you might have
a user in the email server with the same name as a file in the file system:and they
have no idea about each others existance. To maintain uniqueness, object names
are prefixed with the name of the Directory Service they belong to. This name
identifies a "namespace", with a namespace identifier like "WinNT:", "LDAP:" etc.
(The ":" means that it's a namespace, and therefore, a directory service)
var
Container : IADsContainer;
NewObject : IADs;
User : IADsUser;
hr : HREsult;
begin
// COM must be initialized
CoInitialize(nil);
// Cleanup.
Container._Release;
NewObject._Release;
User._Release;
CoUninitialize;
end;
This doesn't sound very great, you might say. What's the difference between doing
this and using native Windows NT calls to add users? First, this is COM based so if
Microsoft decides to change the entire implementation of the user architecture,
your applications are safe, because there will still be an ADSI provider supporting
the same interfaces. (Note: this kind of thing might not be about to happen.)
Second, consider your gains in extensibility. You can now add a user on a Netware
Server using very similar code, except you'd have to use the ADSI provider for
Netware. As we will see later, the code will follow a similar pattern for doing
different activities: creating a web site, adding an email user etc.
Take a look at the next section for more benefits.
Feature Benefit
Overview
Most directory services are hierarchical in nature and thus lend themselves to a
hierarchical object model. ADSI abstracts this concept by defining Container
Interfaces and leaf interfaces. A container object (that implements a Container
Interface) will contain zero or more ADSI objects - which could be other containers
or leaf objects. As I've said before, access to directory services is through ADSI
providers - so each provider is identified by a unique namespace identifier. A
provider implements a Namespace object which is a COM Object that is a one-
stop-shop: you can access any object in the namespace through this object.
These common methods do not expose all the functionalities of a provider - simply
because the domain of a "directory service provider" is too large to be able to
abstract everything. To allow providers to extend functionality, ADSI supplies a
schema model that I will describe later.
Each form could contain many components in it, each of which has the standard
properties (Name, Tag) at least. So a Form is analogous to a "container" in ADSI.
In congruence, Screen is also a container.
(This is only an example to demonstrate the ADSI object model. It might not be the
best way to have your application support ADSI.)
If this was implemented, you can create any form by calling
Screen.OpenDSObject('Form:MainForm');. With ADSI, there are libraries
present so that you can open an object directly, given its path. You don't need to
create the namespace container in order to create an object. A few functions here
are:
ADsGetObject(Path, Interfacename, Object) - returns an instance of the object
who's name is Path. To avoid round trips with QueryInterface, (See Effective COM
by Don Box et. al) the second parameter is an Interface ID that determines which
interface will be returned.
ADsOpenObject - Similar to ADsGetObject, except you can log on using a
different identity to get the parameters. The security model in ADSI works with NT
User security, so you can have role based security work for you.
ADsBuildEnumerator, ADsEnumerateNext, ADsFreeEnumerator - Helper
functions for the IEnumVARIANT interface, which allows Visual Basic developers
to use the for each syntax.
Using ADSI in Delphi
In this section I will talk about how you can use ADSI in Delphi. I've used a number
of samples from the Windows Platform SDK as a reference. If you take a look at
this, most examples you will find will use late binding : Visual Basic code or
VBScript/ASP code. I'll use a combination of Late and Early Binding - Delphi can use
both - to demonstrate various features.
With this information at hand, lets consider a few ADsPaths that could identify
objects.
To find all the providers installed on your machine, you can enumerate the
namespaces in "ADs:". Here is some Visual Basic code that does it:
Set x = GetObject("ADs:")
For Each provider In x
provider.Name
Next
Since we do not have the "For Each" syntax in Delphi, I will use the helper functions
provided by ADSI to enumerate containers.
var x : IADsContainer;
e : IEnumVariant;
hr, i : integer;
varArr : OleVariant;
lNumElements : ULONG;
item : IADs;
begin
hr := ADsGetObject( 'ADs:', IID_IADsContainer, x); // bind to the object
hr := ADsBuildEnumerator(x,e); // start enumerating
while SUCCEEDED(hr) do
begin
// get the next contained object
hr := ADsEnumerateNext(e,1,varArr,lNumElements);
if (lNumElements<=0) then // are we done?
break;
if (lNumElements=0) then
break;
IDispatch(varArr).QueryInterface(IADs, obj);
if obj<>nil then
begin
Func(obj);
end;
varArr := NULL;
end;
// do not call ADsFreeEnumerator(e); since e will be released
by Delphi
end;
You may also use ADsOpenObject for binding, the only difference being that you
can choose to bind as a different user instead.
hr := ADsOpenObject('IIS://localhost', 'Admin','Admin',
ADS_SECURE_AUTHENTICATION , IADs, obj );
Connection Caching
ADSI caches connections to servers - on all objects not yet destroyed. So, if you
will bind to many objects, you can create a dummy object that binds to some object
on the server, perform your set of operations using a different set of objects and
finally, released the dummy object.
Accessing properties.
Once Binding is established, you will want to get or set properties of the object. The
steps involved are:
(b) and (c) are required because ADSI caches the properties on the client side and
updates the server only when you call SetInfo. (Saves a lot of network traffic this
way).
Here's an example.
// Late Binding
var obj : Variant;
begin
obj := ADsHlp.GetObject('WinNT://AGNISOFT/Deepak'); // bind to the object
obj.Put( 'FullName', 'Deepak'); // set properties
obj.SetInfo;
obj := NULL; // release the object
end;
Properties could also have multiple values, like additional phone numbers. You
would use the PutEx and GetEx functions to set or access these properties.
obj.PutEx(ADS_PROPERTY_DELETE, 'otherHomePhone',
VarArrayOf(['111-1111', '222-2222']));
obj.SetInfo; //now there will be only '333-3333'
obj.PutEx(ADS_PROPERTY_UPDATE, 'otherHomePhone',
VarArrayOf(['888-8888','999-9999']));
obj.SetInfo; //now there will be '888-8888'
//and '999-9999'
1. Using ADO
Active Data Objects is Microsoft's latest Database Access solution. It works with
OLE DB providers, and ADSI comes with a provider named "ADsDSOObject". In
Delphi, all you need to do is to drop a TADOConnection and set its provider to
"ADsDSOObject". Then, drop a TADOQuery, connect it to the TADOConnection
and query ADSI - this is a method for simple searching.
Here's how I've got all the users, their user names and their last names from my
machine's Active Directory.
The ADSI provider is read-only at this time. Microsoft plans to ship a read-write
provider in future. To modify data, you can:
At this point, only the LDAP and the NDS providers support searching using ADO.
You cannot use ADO to search for user in a Windows NT (Or 2000) domain.
If you don't want to use ADO, you can use the IDirectorySearch Interface. The
steps involved are:
Security
ADSI supports Authentication using a login name and a password. If you use
ADsGetObject then the user currently logged on is used to authenticate the login.
You can specify a user name by using ADsOpenObject.
Example:
hr := ADsOpenObject('IIS://localhost', 'testuser','pwd',
ADS_SECURE_AUTHENTICATION , IADs, obj );
Role based security to properties - You might need to secure ADSI itself -
control who can read/write certain properties etc. The properties of ADSI are
controlled by Access Control Entries (ACEs) in Windows 2000. You can create an
ACE and add it to the Discretionary Access Control List (DACL) of the
SecurityDescriptor of an object. ( Use obj.Get('ntSecurityDescriptor') to get the
security descriptor) The ACE can allow or deny access to one or all properties of an
object. The security is inherited - so if you change the access control list of a
container, all its descendants will inherit it.
Supporting ADSI
In the Windows 2000 application specification, it is recommended that you use
ADSI in your applications in specific instances.
1. If your application will use a known directory service, you must use ADSI to get
or set properties in the Active Directory service. For instance, if you need to add
or modify a user, you must use ADSI to do so.
To do this, you need to be able to extend ADSI. I'll talk about two ways you could
extend ADSI. But first, lets see how the ADSI Schema works.
ADSI Schema
The predefined ADSI objects have very few properties: these may not be enough
for a particular provider. So, ADSI allows your provider to extend the basic interface
by adding properties to objects within your namespace. The schema objects are
special ADSI objects : They allow you to :
1. What kind of objects will be present (eg. in WinNT:, you have Users, Groups,
Services etc),
2. What properties these objects will have (For Users in WinNT:, FullName,
Description etc. are properties) and which properties are mandatory and which
are optional.
3. The syntax of these properties (FullName is a String, UserFlags is an
integer etc.)
1. Get the ADsPath of the schema - The IADs.Get_Schema call does the job
2. create the schema object and browse it.
Here's an example that gets the list of Mandatory and optional properties of all
Users in the LDAP namespace.
ADSI Extensions
ISVs or corporate developers can extend the object semantics by adding interfaces
to the existing ADSI interfaces. ADSI combines the COM Aggregation model and
directory technology to bring a powerful extension model. You can thus support
more functions than provided by any of the standard ADSI interfaces.
Anyone that needs to extend ADSI can do so by writing an extension - which is
nothing but a COM object. A backup vendor, for instance, could write an extension
that supports "Backup" and "Restore" functions that extend the IADsComputer
interface - This would be useful for administrator to write scripts for automatically
backing up computers to a tape drive.
Writing Providers
To provide access to a totally new namespace, you must implement an ADSI
provider. An ADSI provider could just be a single COM DLL containing many COM
classes that you implement. Any provider needs to support:
1. A top level namespace object that supports IADsOpenDSObject and
IParseDisplayName. (And of course, IADs) YOu will need to parse any
AdsPath given and detect syntax errors, if any.
2. A few other interfaces-IADsPropertyList, IADsPropertyEntry,
IADsPropertyValue, IDirectoryObject. (I won't go into detail on these
interfaces) IDispatch - this is very important because you must support
late binding.
3. IADsContainer on all object containers.
4. IEnumVariant on all enumeratable objects (containers, collections).- You
need to support those Visual Basic and VBScript users using for each.
5. A schema class container object with appropriate class, syntax and
property objects for your namespace.
This seems like quite a big task, and though there is a sample in the Windows 2000
platform SDK, it isn't quite easy. I have written a sample in order to make it easier
for you to begin - it's available along with this article. Please check
www.agnisoft.com/adsi for updates.
Conclusion.
Windows 2000 supports Active Directory natively-there are four providers
(WinNT:, LDAP:, IIS:, NDS:) already present in the Windows 2000 server install. At
the time of writing this paper, the other applications supporting ADSI are Microsoft
Exchange Server 5.5 and Microsoft Site Server 3.0. Many third-party products are
coming out with ADSI support.
There will be a lot of focus on ADSI in the future - you will see a number of
components that will give you easier access to ADSI objects. You can begin to
perform administrative tasks using ADSI, and identify areas of your applications
where it will be better to use ADSI rather than a native Directory provider.
Active Directory is not something to be ignored because it forms a part of the most
recent operating system that you will support - Windows 2000.
References
1. ADSI White Papers - part of the Windows Platform SDK
2. Windows 2000 Developers Readiness Kit.
3. Newsgroups : (at msnews.microsoft.com)
- microsoft.public.adsi.general
- microsoft.public.platformsdk.adsi