Microsoft KB Archive/269159

From BetaArchive Wiki

HOWTO: Use Visual Basic and ADsSecurity.dll to Properly Order ACEs in an ACL

Q269159



The information in this article applies to:


  • Microsoft Active Directory Service Interfaces, version 2.5
  • Windows Script Host, version 2.0
  • Microsoft Active Directory Services Interface, System Component
  • Microsoft Active Directory Services Interface, Microsoft Active Directory Client





SUMMARY

This article shows how to use ADsSecurity.dll to obtain a security descriptor (SD) for a file, Active Directory object, or registry key. This article demonstrates how, once a SD has been obtained, to retrieve the discretionary access control list (ACL) from that SD and then add an Access Control Entry (ACE) in the proper location. The article provides a simple algorithm along with a Microsoft Visual Basic code example illustrating how to properly order an ACL then write the ACL back to the SD.



MORE INFORMATION

The IADsAccessControlList::AddAce method adds the ACE at the top of the ACL. In some cases, adding an ACE at the top will create undesired security access. The proper order of ACEs in an ACL is as follows:

Access-denied ACEs that apply to the object itself

Access-denied ACEs that apply to a child of the object, such as a property set or property
Access-allowed ACEs that apply to the object itself

Access-allowed ACEs that apply to a child of the object, such as a property set or property

In the products listed at the beginning of this article, the IADsAccessControlList interface does not support a method that properly orders an ACL. The ACEs must be sorted into these five groups:

Access-denied on the object

Access-denied on a child or property
Access-allowed on the object
Access-allowed on a child or property

All inherited ACEs

The ordering for the inherited ACEs should not be altered. All inherited ACEs are added by the operating system and are ordered appropriately. A programmer can specify that an ACE should be inherited and Windows 2000 or Windows NT will take care of propagating the ACE to child objects.

There is an exception to the propagation rules. ACEs added to an object's ACL will not automatically get applied to existing objects in the tree. It is the responsibility of the programmer to walk the tree and propagate the ACE by adding the ACE to the existing objects. The ACE will be propagated on new objects in the subtree.

Algorithm for Sorting ACEs in an ACL

  1. Obtain the discretionary ACL (DACL) from the security descriptor.
  2. Check the IADsAccessControlEntry::AceFlags to see if the ACE was inherited (check for the ADS_ACEFLAG_INHERITED_ACE bit).
  3. Check the IADsAccessControlEntry::AceType to see what type of access the ACE grants and what the access is granted to (the object itself or properties of the object). The following list outlines the ACE type values and what they mean:
  4. Place the ACE in the appropriate temporary DACL based on the IADsAccessControlListEntry::AceType value.
  5. Rebuild the ACL from the separate ACLs in the following order:
  6. Set the new ACL to the same revision level as the old ACL.
  7. Replace the ACL on the security descriptor.

Steps to Use the Visual Basic Code Provided in this Article

  1. Register the ADsSecurity.dll.
  2. Start Visual Basic and create a standard EXE project.
  3. Check the References for the project. Make sure that the follow are selected:
  4. Make a command button on the form.
  5. Double click the command button, and then copy the following code and paste it into the Command1_Click() command handler.

Visual Basic Sample Code Illustrates How to Implement the Sorting Algorithm

Dim sec As New ADsSecurity
Dim sd As IADsSecurityDescriptor
Dim dacl As IADsAccessControlList
Dim ace As IADsAccessControlEntry
Dim newAce As New AccessControlEntry
'
' Declare temporary ACLs for sorting the original
' ACL
'
Dim newdacl As New AccessControlList
Dim ImpDenyDacl As New AccessControlList
Dim ImpDenyObjectDacl As New AccessControlList
Dim InheritedDacl As New AccessControlList
Dim ImpAllowDacl As New AccessControlList
Dim impAllowObjectDacl As New AccessControlList

Private Sub Command1_Click()
'
' Be sure to register ADsSecurity.Dll using RegSvr32 and
' adding the ADsSecurity2.5 Type Library to you references for
' the project.
'
' Using the ADsSecurity object, retrieve a SD for an object.  Just
' Replace the LDAP:// path with the path of an object that contains a
' SD.  For additional details on valid path strings, review
' the information stored in the Platform SDK ADSI samples starting with
' "<Platform SDK Root>\Samples\NetDs\ADSI\rtk.htm"
'
Set sec = CreateObject("ADsSecurity")<BR/>
' TODO :  replace the the servername and DN of the object you want to modify.
Set sd = sec.GetSecurityDescriptor("LDAP://MyDCname/cn=MyUser,cn=Users,dc=MyDom,dc=com")

'Displaying the ACE in the DACL --- it's the same way you display ACEs for File, File Share, Registry, Exchange, and Active Directory's ACL.

Set dacl = sd.DiscretionaryAcl
Debug.Print Date & Time & "Initial Values of DACL"
For Each ace In dacl
   Debug.Print ace.Trustee
   Debug.Print Hex(ace.AccessMask)
   Debug.Print Hex(ace.AceType)
   Debug.Print Hex(ace.AceFlags)
   Debug.Print Hex(ace.Flags)
Next

Debug.Print dacl.AceCount

'
' Initialize all of the new ACLs
'
' If you are doing this in VBSscript you will need to use
' The following methods of creating the ACL bins instead of
' using the Dim As New statements above. 
'
'Set newAce = CreateObject("AccessControlEntry")
'Set newdacl = CreateObject("AccessControlList")
'Set InheritedDacl = CreateObject("AccessControlList")
'Set ImpAllowDacl = CreateObject("AccessControlList")
'Set InhAllowDacl = CreateObject("AccessControlList")
'Set ImpDenyObjectDacl = CreateObject("AccessControlList")
'Set ImpAllowObjectDacl = CreateObject("AccessControlList")
'
'
' Create a new ace,  this one
' sets an extended right on the user object that allows the
' trustee to read and write the userAccountControl property of an
' user object.
'
' TODO : Replace the trustee with an appropriate trustee on the domain
' in question.
'
newAce.Trustee = "MyDomain\Myuser"
newAce.Flags = ADS_FLAG_OBJECT_TYPE_PRESENT
newAce.ObjectType = "{BF967A68-0DE6-11D0-A285-00AA003049E2}"
newAce.AccessMask = ADS_RIGHT_DS_READ_PROP Or ADS_RIGHT_DS_WRITE_PROP
newAce.AceType = ADS_ACETYPE_ACCESS_ALLOWED_OBJECT
newAce.AceFlags = ADS_ACEFLAG_INHERIT_ACE
'
' Place the new ace in the DACL
'
dacl.AddAce newAce
'
' Sift the DACL into 5 bins:
' Inherited Aces
' Implicit Deny Aces
' Implicit Deny Object Aces
' Implicit Allow Aces
' Implicit Allow object aces
'
For Each ace In dacl
  '
  ' Sort the original ACEs into their appropriate
  ' ACLs
  '
  Debug.Print ace.Trustee
  If ((ace.AceFlags And ADS_ACEFLAG_INHERITED_ACE) = ADS_ACEFLAG_INHERITED_ACE) Then
     '
     ' Don't really care about the order of inherited aces.  Since we are
     ' adding them to the top of a new list, when they are added back
     ' to the Dacl for the object, they will be in the same order as
     ' they were originally.  Just a positive side affect of adding items
     ' of a LIFO ( Last In First Out) type list.
     '
     InheritedDacl.AddAce ace
  Else
     '
     ' We have an Implicit ACE, lets put it the proper pool
     '
     Select Case ace.AceType
     Case ADS_ACETYPE_ACCESS_ALLOWED
        '
        ' We have an implicit allow ace
        '
        ImpAllowDacl.AddAce ace
     Case ADS_ACETYPE_ACCESS_DENIED
        '
        ' We have a implicit Deny ACE
        '
        ImpDenyDacl.AddAce ace
     Case ADS_ACETYPE_ACCESS_ALLOWED_OBJECT
        '
        ' We have an object allowed ace
        ' Does it apply to a property? or an Object?
        '
        impAllowObjectDacl.AddAce ace
     Case ADS_ACETYPE_ACCESS_DENIED_OBJECT
        '
        ' We have a object Deny ace
        '
        ImpDenyObjectDacl.AddAce ace
     Case Else
        '
        ' Missed a bin?
        '
        Debug.Print "Bad ace...." & Hex(ace.AceType)
     End Select
  End If
Next
'
' Combine the ACEs in the proper order
' Implicit Deny
' Implicit Deny Object
' Implicit Allow
' Implicit Allow Object
' Inherited aces
'
' Implicit Deny
'
For Each ace In ImpDenyDacl
  newdacl.AddAce ace
Next
'
' Implicit Deny Object
'
For Each ace In ImpDenyObjectDacl
  newdacl.AddAce ace
Next
'
' Implicit Allow
'
For Each ace In ImpAllowDacl
  newdacl.AddAce ace
Next
'
' Implicit Allow Object
'
For Each ace In impAllowObjectDacl
  newdacl.AddAce ace
Next
'
' Inherited Aces
'
For Each ace In InheritedDacl
  newdacl.AddAce ace
Next

sd.DiscretionaryAcl = newdacl
Debug.Print Date
For Each ace In newdacl
   Debug.Print ace.Trustee
   Debug.Print "Ace Mask: " & Hex(ace.AccessMask)
   Debug.Print "Ace Type: " & Hex(ace.AceType)
   Debug.Print "Ace Flags: " & Hex(ace.AceFlags)
   Debug.Print "Object Type value: " & Hex(ace.Flags)
   Debug.Print "Object Guid : " & ace.ObjectType
Next
'
' Set the appropriate revision level
' for the DACL
'
newdacl.AclRevision = dacl.AclRevision
'
' Replace the Security Descriptor
'
sec.SetSecurityDescriptor sd
' If this generates the error -214023559 or 80070539 check your TODO
' strings.  This error is most likely a problem with DNS name resolution. 



REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

Q269175 HOWTO: Use Visual C++ to Properly Order ACEs in an ACL

Q279682 HOWTO: Use ADsSecurity.dll to Add an Access Control Entry to an NTFS Folder

ADSI 2.5 can be found in the Platform SDK and is available for free download from the following Microsoft Web site:

http://www.microsoft.com/downloads/search.asp
Select Platform SDK as the product and select your operating system. Next, click Find It. Microsoft Platform SDK will appear in the list of available downloads.

Additional query words:

Keywords : kbADSI kbOSWinNT400 kbOSWin2000 kbVBp kbDSupport
Issue type : kbhowto
Technology : kbWSHSearch kbAudDeveloper kbActiveDirectory kbADSISearch kbWSH200 kbADSI250 kbADSISysComp


Last Reviewed: June 12, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.