<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indented! &#187; Trust</title>
	<atom:link href="http://www.indented.co.uk/index.php/tag/trust/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indented.co.uk</link>
	<description></description>
	<lastBuildDate>Fri, 02 Jul 2010 10:45:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Listing Trusts</title>
		<link>http://www.indented.co.uk/index.php/2009/08/27/listing-trusts/</link>
		<comments>http://www.indented.co.uk/index.php/2009/08/27/listing-trusts/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 10:01:11 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[VbScript]]></category>
		<category><![CDATA[Trust]]></category>

		<guid isPermaLink="false">http://www.highorbit.co.uk/?p=1144</guid>
		<description><![CDATA[A script to enumerate trust information from an Active Directory forest. Const ADS_SCOPE_SUBTREE = 2 ' Trust Type ' http://msdn.microsoft.com/en-us/library/cc223771(PROT.10).aspx Dim objTrustTypes Set objTrustTypes = CreateObject(&#34;Scripting.Dictionary&#34;) objTrustTypes.Add 4, &#34;DCE&#34; objTrustTypes.Add 3, &#34;MIT&#34; objTrustTypes.Add 2, &#34;UpLevel&#34; objTrustTypes.Add 1, &#34;DownLevel&#34; ' Trust Attributes ' http://msdn.microsoft.com/en-us/library/cc223779(PROT.10).aspx Dim objTrustAttributes Set objTrustAttributes = CreateObject(&#34;Scripting.Dictionary&#34;) objTrustAttributes.Add 128, &#34;UsesRC4Encryption&#34; objTrustAttributes.Add 64, &#34;TreatAsExternal&#34; [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>A script to enumerate trust information from an Active Directory forest.<br />
<span id="more-1144"></span></p>
<pre class="brush: vb;">
Const ADS_SCOPE_SUBTREE = 2

' Trust Type
' http://msdn.microsoft.com/en-us/library/cc223771(PROT.10).aspx
Dim objTrustTypes
Set objTrustTypes = CreateObject(&quot;Scripting.Dictionary&quot;)
objTrustTypes.Add 4, &quot;DCE&quot;
objTrustTypes.Add 3, &quot;MIT&quot;
objTrustTypes.Add 2, &quot;UpLevel&quot;
objTrustTypes.Add 1, &quot;DownLevel&quot;

' Trust Attributes
' http://msdn.microsoft.com/en-us/library/cc223779(PROT.10).aspx
Dim objTrustAttributes
Set objTrustAttributes = CreateObject(&quot;Scripting.Dictionary&quot;)
objTrustAttributes.Add 128, &quot;UsesRC4Encryption&quot;
objTrustAttributes.Add 64, &quot;TreatAsExternal&quot;
objTrustAttributes.Add 32, &quot;WithinForest&quot;
objTrustAttributes.Add 16, &quot;CrossOrganisation&quot;
objTrustAttributes.Add 8, &quot;ForestTransitive&quot;
objTrustAttributes.Add 4, &quot;QuarantinedDomain&quot;
objTrustAttributes.Add 2, &quot;UpLevelOnly&quot;
objTrustAttributes.Add 1, &quot;NonTransitive&quot;

' Trust Direction
' http://msdn.microsoft.com/en-us/library/cc223768(PROT.10).aspx
Dim objTrustDirection
Set objTrustDirection = CreateObject(&quot;Scripting.Dictionary&quot;)
objTrustDirection.Add 3, &quot;BiDirectional&quot;
objTrustDirection.Add 2, &quot;Outbound&quot;
objTrustDirection.Add 1, &quot;Inbound&quot;
objTrustDirection.Add 0, &quot;Disabled&quot;

Dim objConnection : Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
objConnection.Provider = &quot;ADsDSOObject&quot;
objConnection.Open &quot;Active Directory Provider&quot;

Dim objCommand : Set objCommand = CreateObject(&quot;ADODB.Command&quot;)
objCommand.ActiveConnection = objConnection

Dim objRootDSE : Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
objCommand.CommandText = &quot;SELECT distinguishedName, name, trustType, &quot; &amp; _
  &quot;trustAttributes, trustDirection, trustPartner, whenCreated &quot; &amp; _
  &quot;FROM 'GC://&quot; &amp; objRootDSE.Get(&quot;rootDomainNamingContext&quot;) &amp; _
  &quot;' WHERE objectClass='trustedDomain'&quot;

objCommand.Properties(&quot;Page Size&quot;) = 1000
objCommand.Properties(&quot;Timeout&quot;) = 600
objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE
objCommand.Properties(&quot;Cache Results&quot;) = False

Dim objRecordSet : Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
  WScript.Echo &quot;Trusted Domain: &quot; &amp; objRecordSet.Fields(&quot;name&quot;).Value
  WScript.Echo &quot;Trust Type: &quot; &amp; _
    objTrustTypes(objRecordSet.Fields(&quot;trustType&quot;).Value)

  Dim dblFlag
  Dim strFlags : strFlags = &quot;&quot;
  For Each dblFlag in objTrustAttributes
    If objRecordSet.Fields(&quot;trustAttributes&quot;).Value And dblFlag Then
      strFlags = strFlags &amp; objTrustAttributes(dblFlag) &amp; &quot; &quot;
    End If
  Next
  WScript.Echo &quot;Trust Attributes: &quot; &amp; strFlags

  WScript.Echo &quot;Trust Direction: &quot; &amp; _
    objTrustDirection(objRecordSet.Fields(&quot;trustDirection&quot;).Value)
  WScript.Echo &quot;Trust Partner: &quot; &amp; objRecordSet.Fields(&quot;trustPartner&quot;).Value
  WScript.Echo &quot;Distinguished Name: &quot; &amp; _
    objRecordSet.Fields(&quot;distinguishedName&quot;).Value
  WScript.Echo &quot;Created: &quot; &amp; objRecordSet.Fields(&quot;whenCreated&quot;).Value

  objRecordSet.MoveNext
Wend

objConnection.Close
</pre>
<h3>Usage example</h3>
<p>In the past I have used the script above to monitor trust settings across a forest. The following script uses the trust information to build a text file storing trust configuration sends an e-mail if that configuration changes.</p>
<pre class="brush: vb;">
Option Explicit

' Script to get trusts, compare with stored configuration and notify if changed

Sub ShowUsage
  Dim strUsage
  strUsage = &quot;Usage:&quot; &amp; vbCrLf &amp; vbCrLf &amp; _
    WScript.ScriptName &amp; _
    &quot; /Command:[Update | Notify] [/MailServer:&lt;serverName&gt;] &quot; &amp; _
    &quot;[/Recipient:&lt;address&gt;]&quot; &amp; vbCrLf &amp; vbCrLf &amp; _
    &quot;Arguments:&quot; &amp; vbCrLf &amp; vbCrLf &amp; _
    &quot;    Command      Update - Updates the contents of the text &quot; &amp; _
    &quot;file with data from the global catalog&quot; &amp; vbCrLf &amp; _
    &quot;                 Notify - Notifies the recipient using mailserver &quot; &amp; _
    &quot;if the trust data changes&quot; &amp; vbCrLf &amp; _
    &quot;    MailServer   Server used to send mail. Default: localhost&quot; &amp; vbCrLf &amp; _
    &quot;    Recipient    Email address of person or group to notify&quot; &amp; vbCrLf

  WScript.Echo strUsage
  WScript.Quit
End Sub

Function GetTrusts
  ' Returns a Scripting.Dictionary object containing details of the Trust
  ' Format:
  ' Key: DistinguishedName
  ' Data: Array(
  '           Trusted Domain,
  '           Type,
  '           Attributes,
  '           Direction,
  '           Partner,
  '           Created,
  '           Changed )

  Const ADS_SCOPE_SUBTREE = 2

  ' Trust Type
  ' http://msdn.microsoft.com/en-us/library/cc223771(PROT.10).aspx
  Dim objTrustTypes
  Set objTrustTypes = CreateObject(&quot;Scripting.Dictionary&quot;)
  objTrustTypes.Add 4, &quot;DCE&quot;
  objTrustTypes.Add 3, &quot;MIT&quot;
  objTrustTypes.Add 2, &quot;UpLevel&quot;
  objTrustTypes.Add 1, &quot;DownLevel&quot;

  ' Trust Attributes
  ' http://msdn.microsoft.com/en-us/library/cc223779(PROT.10).aspx
  Dim objTrustAttributes
  Set objTrustAttributes = CreateObject(&quot;Scripting.Dictionary&quot;)
  objTrustAttributes.Add 128, &quot;UsesRC4Encryption&quot;
  objTrustAttributes.Add 64, &quot;TreatAsExternal&quot;
  objTrustAttributes.Add 32, &quot;WithinForest&quot;
  objTrustAttributes.Add 16, &quot;CrossOrganisation&quot;
  objTrustAttributes.Add 8, &quot;ForestTransitive&quot;
  objTrustAttributes.Add 4, &quot;QuarantinedDomain&quot;
  objTrustAttributes.Add 2, &quot;UpLevelOnly&quot;
  objTrustAttributes.Add 1, &quot;NonTransitive&quot;

  ' Trust Direction
  ' http://msdn.microsoft.com/en-us/library/cc223768(PROT.10).aspx
  Dim objTrustDirection
  Set objTrustDirection = CreateObject(&quot;Scripting.Dictionary&quot;)
  objTrustDirection.Add 3, &quot;BiDirectional&quot;
  objTrustDirection.Add 2, &quot;Outbound&quot;
  objTrustDirection.Add 1, &quot;Inbound&quot;
  objTrustDirection.Add 0, &quot;Disabled&quot;

  Dim objConnection : Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
  objConnection.Provider = &quot;ADsDSOObject&quot;
  objConnection.Open &quot;Active Directory Provider&quot;

  Dim objCommand : Set objCommand = CreateObject(&quot;ADODB.Command&quot;)
  objCommand.ActiveConnection = objConnection

  Dim objRootDSE : Set objRootDSE = GetObject(&quot;LDAP://RootDSE&quot;)
  objCommand.CommandText = &quot;SELECT distinguishedName, name, &quot; &amp; _
    &quot;trustType, trustAttributes, trustDirection, trustPartner, &quot; &amp; _
    &quot;whenCreated, whenChanged &quot; &amp; _
    &quot;FROM 'GC://&quot; &amp; objRootDSE.Get(&quot;rootDomainNamingContext&quot;) &amp; _
    &quot;' WHERE objectClass='trustedDomain'&quot;

  objCommand.Properties(&quot;Page Size&quot;) = 1000
  objCommand.Properties(&quot;Timeout&quot;) = 600
  objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE
  objCommand.Properties(&quot;Cache Results&quot;) = False

  Dim objRecordSet : Set objRecordSet = objCommand.Execute

  Dim objTrusts : Set objTrusts = CreateObject(&quot;Scripting.Dictionary&quot;)

  While Not objRecordSet.EOF
    Dim dblFlag
    Dim strAttributes : strAttributes = &quot;&quot;
    For Each dblFlag in objTrustAttributes
      If objRecordSet.Fields(&quot;trustAttributes&quot;).Value And dblFlag Then
        strAttributes = strAttributes &amp; objTrustAttributes(dblFlag) &amp; &quot; &quot;
      End If
    Next

    objTrusts.Add objRecordSet.Fields(&quot;distinguishedName&quot;).Valuem, Array( _
      objRecordSet.Fields(&quot;name&quot;).Value, _
      objTrustTypes(objRecordSet.Fields(&quot;trustType&quot;).Value), _
      strAttributes, _
      objTrustDirection(objRecordSet.Fields(&quot;trustDirection&quot;).Value), _
      objRecordSet.Fields(&quot;trustPartner&quot;).Value, _
      objRecordSet.Fields(&quot;whenCreated&quot;).Value, _
      objRecordSet.Fields(&quot;whenChanged&quot;).Value)

    objRecordSet.MoveNext
  Wend

  objConnection.Close

  Set objRecordSet = Nothing
  Set objCommand = Nothing
  Set objConnection = Nothing

  Set GetTrusts = objTrusts
End Function

Sub SendMail(strRecipient, strBody, strMailServer)

  Set objMail = CreateObject(&quot;CDO.Message&quot;)
  objMail.Subject = &quot;Trust monitor&quot;

  objMail.From = strRecipient
  objMail.To = strRecipient

  objMail.TextBody = strBody

  objMail.Configuration.Fields.Item _
    (&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;) = 2
  objMail.Configuration.Fields.Item _
    (&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;) = _
    strMailServer
  objMail.Configuration.Fields.Item _
    (&quot;http://schemas.microsoft.com/cdo/configuration/smtpserverport&quot;) = 25

  objMail.Configuration.Fields.Update
  objMail.Send
End Sub

'
' Main Code
'

Dim objFileSystem
Set objFileSystem = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Dim objFile

Dim objTrusts : Set objTrusts = GetTrusts

If LCase(WScript.Arguments.Named(&quot;command&quot;)) = &quot;update&quot; Then
  Set objFile = objFileSystem.OpenTextFile(&quot;Trusts.txt&quot;, 2, True, 0)

  Dim strDN
  For Each strDN in objTrusts
    objFile.WriteLine strDN &amp; vbTab &amp; Join(objTrusts(strDN), vbTab)
  Next

ElseIf LCase(WScript.Arguments.Named(&quot;command&quot;)) = &quot;notify&quot; Then

  strRecipient = WScript.Arguments.Named(&quot;recipient&quot;)

  If strRecipient = &quot;&quot; Then
    WScript.Echo &quot;ERROR: No recipient defined&quot;
    ShowUsage
  End If

  strMailServer = WScript.Arguments.Named(&quot;mailserver&quot;)

  If strMailServer = &quot;&quot; Then
    strMailServer = &quot;localhost&quot;
  End If

  If objFileSystem.FileExists(&quot;Trusts.txt&quot;) Then
    Set objFile = objFileSystem.OpenTextFile(&quot;Trusts.txt&quot;, 1, False, 0)

    Dim objTrustsInFile
    Set objTrustsInFile = CreateObject(&quot;Scripting.Dictionary&quot;)

    Dim arrTrustData()
    Do While Not objFile.AtEndOfStream
      Dim arrTrustInFile : arrTrustInFile = Split(objFile.ReadLine, vbTab)

      ReDim arrTrustData(0)
      Dim i
      For i = 1 to UBound(arrTrustInFile)
        ReDim Preserve arrTrustData(i - 1)
        arrTrustData(i - 1) = arrTrustInFile(i)
      Next

      objTrustsInFile.Add arrTrustInFile(0), arrTrustData
    Loop
  End If

  Dim strTrust

  ' Comparison - Check for new Trusts

  Dim objNewTrusts : Set objNewTrusts = CreateObject(&quot;Scripting.Dictionary&quot;)

  For Each strTrust in objTrusts
    If Not objTrustsInFile.Exists(strTrust) Then
      objNewTrusts.Add strTrust, objTrusts(strTrust)
    End If
  Next

  ' Comparison - Check for removed Trusts

  Dim objRemovedTrusts
  Set objRemovedTrusts = CreateObject(&quot;Scripting.Dictionary&quot;)

  For Each strTrust in objTrustsInFile
    If Not objTrusts.Exists(strTrust) Then
      objRemovedTrusts.Add strTrust, objTrustsInFile(strTrust)
    End If
  Next

  ' Data: Array(
  '           Trusted Domain,
  '           Type,
  '           Attributes,
  '           Direction,
  '           Partner,
  '           Created,
  '           Changed )

  Dim strMessageBody
  Dim booNotify : booNotify = False
  If objNewTrusts.Count &gt; 0 Then
    booNotify = True
    strMessageBody = &quot;New Trusts:&quot; &amp; vbCrLf &amp; vbCrLf
    For Each strTrust in objNewTrusts
      strMessageBody = strMessageBody &amp; &quot;DN: &quot; &amp; strTrust &amp; vbCrLf &amp; _
        &quot;Trusted Domain: &quot; &amp; objNewTrusts(strTrust)(0) &amp; vbCrLf &amp; _
        &quot;Type: &quot; &amp; objNewTrusts(strTrust)(1) &amp; vbCrLf &amp; _
        &quot;Attributes: &quot; &amp; objNewTrusts(strTrust)(2) &amp; vbCrLf &amp; _
        &quot;Direction: &quot; &amp; objNewTrusts(strTrust)(3) &amp; vbCrLf &amp; _
        &quot;Partner: &quot; &amp; objNewTrusts(strTrust)(4) &amp; vbCrLf &amp; _
        &quot;Created: &quot; &amp; objNewTrusts(strTrust)(5) &amp; vbCrLf &amp; _
        &quot;Changed: &quot; &amp; objNewTrusts(strTrust)(6) &amp; vbCrLf &amp; vbCrLf
    Next
  End If
  If objRemovedTrusts.Count &gt; 0 Then
    booNotify = True
    For Each strTrust in objRemovedTrusts
      strMessageBody = strMessageBody &amp; &quot;DN: &quot; &amp; strTrust &amp; vbCrLf &amp; _
        &quot;Trusted Domain: &quot; &amp; objRemovedTrusts(strTrust)(0) &amp; vbCrLf &amp; _
        &quot;Type: &quot; &amp; objRemovedTrusts(strTrust)(1) &amp; vbCrLf &amp; _
        &quot;Attributes: &quot; &amp; objRemovedTrusts(strTrust)(2) &amp; vbCrLf &amp; _
        &quot;Direction: &quot; &amp; objRemovedTrusts(strTrust)(3) &amp; vbCrLf &amp; _
        &quot;Partner: &quot; &amp; objRemovedTrusts(strTrust)(4) &amp; vbCrLf &amp; _
        &quot;Created: &quot; &amp; objRemovedTrusts(strTrust)(5) &amp; vbCrLf &amp; _
        &quot;Changed: &quot; &amp; objRemovedTrusts(strTrust)(6) &amp; vbCrLf &amp; vbCrLf
    Next
  End If
  If booNotify = True Then
    strMessageBody = strMessageBody &amp; _
      &quot;If these trusts are correct please run &quot; &amp; _
      WScript.ScriptName &amp; &quot; /Command:Update&quot;

    SendMail strRecipient, strMessageBody, strMailServer
  End If
Else

  ShowUsage

End If
</pre>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2009/08/27/listing-trusts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
