internal TrustRelationshipInformation(DirectoryContext context, string source, TrustObject obj) { string netbiosDomainName; this.context = context; this.source = source; TrustRelationshipInformation trustRelationshipInformation = this; if (obj.DnsDomainName == null) { netbiosDomainName = obj.NetbiosDomainName; } else { netbiosDomainName = obj.DnsDomainName; } trustRelationshipInformation.target = netbiosDomainName; if ((obj.Flags & 2) == 0 || (obj.Flags & 32) == 0) { if ((obj.Flags & 2) == 0) { if ((obj.Flags & 32) != 0) { this.direction = TrustDirection.Inbound; } } else { this.direction = TrustDirection.Outbound; } } else { this.direction = TrustDirection.Bidirectional; } this.type = obj.TrustType; }
internal TrustRelationshipInformationCollection(DirectoryContext context, string source, ArrayList trusts) { for (int i = 0; i < trusts.Count; i++) { TrustObject item = (TrustObject)trusts[i]; if (item.TrustType != TrustType.Forest && item.TrustType != (TrustType.ParentChild | TrustType.CrossLink | TrustType.External | TrustType.Forest | TrustType.Kerberos | TrustType.Unknown)) { TrustRelationshipInformation trustRelationshipInformation = new TrustRelationshipInformation(context, source, item); this.Add(trustRelationshipInformation); } } }
internal TrustRelationshipInformationCollection(DirectoryContext context, string source, ArrayList trusts) { for (int i = 0; i < trusts.Count; i++) { TrustObject obj2 = (TrustObject)trusts[i]; if ((obj2.TrustType != TrustType.Forest) && (obj2.TrustType != (TrustType.Unknown | TrustType.ParentChild))) { TrustRelationshipInformation info = new TrustRelationshipInformation(context, source, obj2); this.Add(info); } } }
internal TrustRelationshipInformationCollection(DirectoryContext context, string source, ArrayList trusts) { for (int i = 0; i < trusts.Count; i++) { TrustObject obj = (TrustObject)trusts[i]; // we don't need self and forest trust if ((obj.TrustType == TrustType.Forest) || ((int)obj.TrustType == 7)) { continue; } TrustRelationshipInformation info = new TrustRelationshipInformation(context, source, obj); Add(info); } }
internal TrustRelationshipInformation(DirectoryContext context, string source, TrustObject obj) { this.context = context; this.source = source; this.target = (obj.DnsDomainName == null) ? obj.NetbiosDomainName : obj.DnsDomainName; if (((obj.Flags & 2) != 0) && ((obj.Flags & 0x20) != 0)) { this.direction = System.DirectoryServices.ActiveDirectory.TrustDirection.Bidirectional; } else if ((obj.Flags & 2) != 0) { this.direction = System.DirectoryServices.ActiveDirectory.TrustDirection.Outbound; } else if ((obj.Flags & 0x20) != 0) { this.direction = System.DirectoryServices.ActiveDirectory.TrustDirection.Inbound; } this.type = obj.TrustType; }
private ArrayList GetTrustsHelper(string targetDomainName) { string name; ArrayList arrayLists; IntPtr intPtr = (IntPtr)0; int num = 0; ArrayList arrayLists1 = new ArrayList(); ArrayList arrayLists2 = new ArrayList(); new TrustRelationshipInformationCollection(); int num1 = 0; string stringUni = null; int num2 = 0; if (!this.context.isServer()) { name = DomainController.FindOne(this.context).Name; } else { name = this.context.Name; } bool flag = Utils.Impersonate(this.context); try { try { num2 = UnsafeNativeMethods.DsEnumerateDomainTrustsW(name, 35, out intPtr, out num); } finally { if (flag) { Utils.Revert(); } } } catch { throw; } if (num2 == 0) { try { if (intPtr != (IntPtr)0 && num != 0) { int num3 = 0; for (int i = 0; i < num; i++) { IntPtr intPtr1 = (IntPtr)((long)intPtr + (long)(i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS)))); DS_DOMAIN_TRUSTS dSDOMAINTRUST = new DS_DOMAIN_TRUSTS(); Marshal.PtrToStructure(intPtr1, dSDOMAINTRUST); arrayLists1.Add(dSDOMAINTRUST); } for (int j = 0; j < arrayLists1.Count; j++) { DS_DOMAIN_TRUSTS item = (DS_DOMAIN_TRUSTS)arrayLists1[j]; if ((item.Flags & 42) != 0 && item.TrustType != TrustHelper.TRUST_TYPE_DOWNLEVEL) { TrustObject trustObject = new TrustObject(); trustObject.TrustType = TrustType.Unknown; if (item.DnsDomainName != (IntPtr)0) { trustObject.DnsDomainName = Marshal.PtrToStringUni(item.DnsDomainName); } if (item.NetbiosDomainName != (IntPtr)0) { trustObject.NetbiosDomainName = Marshal.PtrToStringUni(item.NetbiosDomainName); } trustObject.Flags = item.Flags; trustObject.TrustAttributes = item.TrustAttributes; trustObject.OriginalIndex = j; trustObject.ParentIndex = item.ParentIndex; if (targetDomainName != null) { bool flag1 = false; if (trustObject.DnsDomainName == null || Utils.Compare(targetDomainName, trustObject.DnsDomainName) != 0) { if (trustObject.NetbiosDomainName != null && Utils.Compare(targetDomainName, trustObject.NetbiosDomainName) == 0) { flag1 = true; } } else { flag1 = true; } if (!flag1 && (trustObject.Flags & 8) == 0) { goto Label0; } } if ((trustObject.Flags & 8) == 0) { if (item.TrustType == 3) { trustObject.TrustType = TrustType.Kerberos; } } else { num1 = num3; if ((trustObject.Flags & 4) == 0) { DS_DOMAIN_TRUSTS item1 = (DS_DOMAIN_TRUSTS)arrayLists1[trustObject.ParentIndex]; if (item1.DnsDomainName != (IntPtr)0) { stringUni = Marshal.PtrToStringUni(item1.DnsDomainName); } } trustObject.TrustType = TrustType.ParentChild | TrustType.CrossLink | TrustType.External | TrustType.Forest | TrustType.Kerberos | TrustType.Unknown; } num3++; arrayLists2.Add(trustObject); } Label0: continue; } for (int k = 0; k < arrayLists2.Count; k++) { TrustObject trustObject1 = (TrustObject)arrayLists2[k]; if (k != num1 && trustObject1.TrustType != TrustType.Kerberos) { if (stringUni == null || Utils.Compare(stringUni, trustObject1.DnsDomainName) != 0) { if ((trustObject1.Flags & 1) == 0) { if ((trustObject1.TrustAttributes & 8) == 0) { trustObject1.TrustType = TrustType.External; } else { trustObject1.TrustType = TrustType.Forest; } } else { if (trustObject1.ParentIndex != ((TrustObject)arrayLists2[num1]).OriginalIndex) { if ((trustObject1.Flags & 4) == 0 || (((TrustObject)arrayLists2[num1]).Flags & 4) == 0) { trustObject1.TrustType = TrustType.CrossLink; } else { string str = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDomainNamingContext); string dnsNameFromDN = Utils.GetDnsNameFromDN(str); DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(this.context.Name, DirectoryContextType.Forest, this.context); if (newDirectoryContext.isRootDomain() || Utils.Compare(trustObject1.DnsDomainName, dnsNameFromDN) == 0) { trustObject1.TrustType = TrustType.TreeRoot; } else { trustObject1.TrustType = TrustType.CrossLink; } } } else { trustObject1.TrustType = TrustType.ParentChild; } } } else { trustObject1.TrustType = TrustType.ParentChild; } } } } arrayLists = arrayLists2; } finally { if (intPtr != (IntPtr)0) { UnsafeNativeMethods.NetApiBufferFree(intPtr); } } return arrayLists; } else { throw ExceptionHelper.GetExceptionFromErrorCode(num2, name); } }
private ArrayList GetTrustsHelper(string targetDomainName) { string serverName = null; IntPtr domains = (IntPtr)0; int count = 0; ArrayList unmanagedTrustList = new ArrayList(); ArrayList tmpTrustList = new ArrayList(); TrustRelationshipInformationCollection collection = new TrustRelationshipInformationCollection(); int localDomainIndex = 0; string localDomainParent = null; int error = 0; bool impersonated = false; // first decide which server to go to if (context.isServer()) { serverName = context.Name; } else { serverName = DomainController.FindOne(context).Name; } // impersonate appropriately impersonated = Utils.Impersonate(context); // call the DS API to get trust domain information try { try { error = UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_IN_FOREST | (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND | (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND, out domains, out count); } finally { if (impersonated) Utils.Revert(); } } catch { throw; } // check the result if (error != 0) throw ExceptionHelper.GetExceptionFromErrorCode(error, serverName); try { // now enumerate through the collection if (domains != (IntPtr)0 && count != 0) { IntPtr addr = (IntPtr)0; int j = 0; for (int i = 0; i < count; i++) { // get the unmanaged trust object addr = IntPtr.Add(domains, +i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS))); DS_DOMAIN_TRUSTS unmanagedTrust = new DS_DOMAIN_TRUSTS(); Marshal.PtrToStructure(addr, unmanagedTrust); unmanagedTrustList.Add(unmanagedTrust); } for (int i = 0; i < unmanagedTrustList.Count; i++) { DS_DOMAIN_TRUSTS unmanagedTrust = (DS_DOMAIN_TRUSTS)unmanagedTrustList[i]; // make sure this is the trust object that we want if ((unmanagedTrust.Flags & (int)(DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY | DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND | DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND)) == 0) { // Not interested in indirectly trusted domains. continue; } // we don't want to have the NT4 trust to be returned if (unmanagedTrust.TrustType == TrustHelper.TRUST_TYPE_DOWNLEVEL) continue; TrustObject obj = new TrustObject(); obj.TrustType = TrustType.Unknown; if (unmanagedTrust.DnsDomainName != (IntPtr)0) obj.DnsDomainName = Marshal.PtrToStringUni(unmanagedTrust.DnsDomainName); if (unmanagedTrust.NetbiosDomainName != (IntPtr)0) obj.NetbiosDomainName = Marshal.PtrToStringUni(unmanagedTrust.NetbiosDomainName); obj.Flags = unmanagedTrust.Flags; obj.TrustAttributes = unmanagedTrust.TrustAttributes; obj.OriginalIndex = i; obj.ParentIndex = unmanagedTrust.ParentIndex; // check whether it is the case that we are only interested in the trust with target as specified if (targetDomainName != null) { bool sameTarget = false; // check whether it is the same target if (obj.DnsDomainName != null && Utils.Compare(targetDomainName, obj.DnsDomainName) == 0) sameTarget = true; else if (obj.NetbiosDomainName != null && Utils.Compare(targetDomainName, obj.NetbiosDomainName) == 0) sameTarget = true; // we only want to need local domain and specified target domain trusts if (!sameTarget && (obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY) == 0) continue; } // local domain case if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_PRIMARY) != 0) { localDomainIndex = j; // verify whether this is already the root if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_TREE_ROOT) == 0) { // get the parent domain name DS_DOMAIN_TRUSTS parentTrust = (DS_DOMAIN_TRUSTS)unmanagedTrustList[obj.ParentIndex]; if (parentTrust.DnsDomainName != (IntPtr)0) localDomainParent = Marshal.PtrToStringUni(parentTrust.DnsDomainName); } // this is the trust type SELF obj.TrustType = (TrustType)7; } // this is the case of MIT kerberos trust else if (unmanagedTrust.TrustType == 3) { obj.TrustType = TrustType.Kerberos; } j++; tmpTrustList.Add(obj); } // now determine the trust type for (int i = 0; i < tmpTrustList.Count; i++) { TrustObject tmpObject = (TrustObject)tmpTrustList[i]; // local domain case, trust type has been determined if (i == localDomainIndex) continue; if (tmpObject.TrustType == TrustType.Kerberos) continue; // parent domain if (localDomainParent != null && Utils.Compare(localDomainParent, tmpObject.DnsDomainName) == 0) { tmpObject.TrustType = TrustType.ParentChild; continue; } if ((tmpObject.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_IN_FOREST) != 0) { // child domain if (tmpObject.ParentIndex == ((TrustObject)tmpTrustList[localDomainIndex]).OriginalIndex) { tmpObject.TrustType = TrustType.ParentChild; } // tree root else if ((tmpObject.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_TREE_ROOT) != 0 && (((TrustObject)tmpTrustList[localDomainIndex]).Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_TREE_ROOT) != 0) { string tmpForestName = null; string rootDomainNC = directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDomainNamingContext); tmpForestName = Utils.GetDnsNameFromDN(rootDomainNC); // only if either the local domain or tmpObject is the tree root, will this trust relationship be a Root, otherwise it is cross link DirectoryContext tmpContext = Utils.GetNewDirectoryContext(context.Name, DirectoryContextType.Forest, context); if (tmpContext.isRootDomain() || Utils.Compare(tmpObject.DnsDomainName, tmpForestName) == 0) { tmpObject.TrustType = TrustType.TreeRoot; } else { tmpObject.TrustType = TrustType.CrossLink; } } else { tmpObject.TrustType = TrustType.CrossLink; } continue; } // external trust or forest trust if ((tmpObject.TrustAttributes & (int)TRUST_ATTRIBUTE.TRUST_ATTRIBUTE_FOREST_TRANSITIVE) != 0) { // should not happen as we specify DS_DOMAIN_IN_FOREST when enumerating the trust, so forest trust will not be returned tmpObject.TrustType = TrustType.Forest; } else { tmpObject.TrustType = TrustType.External; } } } return tmpTrustList; } finally { if (domains != (IntPtr)0) UnsafeNativeMethods.NetApiBufferFree(domains); } }
internal TrustRelationshipInformation(DirectoryContext context, string source, TrustObject obj) { // security context this.context = context; // source this.source = source; // target this.target = (obj.DnsDomainName == null ? obj.NetbiosDomainName : obj.DnsDomainName); // direction if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND) != 0 && (obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND) != 0) { direction = TrustDirection.Bidirectional; } else if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_OUTBOUND) != 0) { direction = TrustDirection.Outbound; } else if ((obj.Flags & (int)DS_DOMAINTRUST_FLAG.DS_DOMAIN_DIRECT_INBOUND) != 0) { direction = TrustDirection.Inbound; } // type this.type = obj.TrustType; }
private ArrayList GetTrustsHelper(string targetDomainName) { string serverName = null; ArrayList list3; IntPtr zero = IntPtr.Zero; int count = 0; ArrayList list = new ArrayList(); ArrayList list2 = new ArrayList(); new TrustRelationshipInformationCollection(); int num2 = 0; string str2 = null; int errorCode = 0; bool flag = false; if (base.context.isServer()) { serverName = base.context.Name; } else { serverName = DomainController.FindOne(base.context).Name; } flag = Utils.Impersonate(base.context); try { try { errorCode = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsEnumerateDomainTrustsW(serverName, 0x23, out zero, out count); } finally { if (flag) { Utils.Revert(); } } } catch { throw; } if (errorCode != 0) { throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, serverName); } try { if ((zero != IntPtr.Zero) && (count != 0)) { IntPtr ptr = IntPtr.Zero; int num4 = 0; for (int i = 0; i < count; i++) { ptr = (IntPtr) (((long) zero) + (i * Marshal.SizeOf(typeof(DS_DOMAIN_TRUSTS)))); DS_DOMAIN_TRUSTS structure = new DS_DOMAIN_TRUSTS(); Marshal.PtrToStructure(ptr, structure); list.Add(structure); } for (int j = 0; j < list.Count; j++) { DS_DOMAIN_TRUSTS ds_domain_trusts2 = (DS_DOMAIN_TRUSTS) list[j]; if (((ds_domain_trusts2.Flags & 0x2a) != 0) && (ds_domain_trusts2.TrustType != TrustHelper.TRUST_TYPE_DOWNLEVEL)) { TrustObject obj2 = new TrustObject { TrustType = TrustType.Unknown }; if (ds_domain_trusts2.DnsDomainName != IntPtr.Zero) { obj2.DnsDomainName = Marshal.PtrToStringUni(ds_domain_trusts2.DnsDomainName); } if (ds_domain_trusts2.NetbiosDomainName != IntPtr.Zero) { obj2.NetbiosDomainName = Marshal.PtrToStringUni(ds_domain_trusts2.NetbiosDomainName); } obj2.Flags = ds_domain_trusts2.Flags; obj2.TrustAttributes = ds_domain_trusts2.TrustAttributes; obj2.OriginalIndex = j; obj2.ParentIndex = ds_domain_trusts2.ParentIndex; if (targetDomainName != null) { bool flag2 = false; if ((obj2.DnsDomainName != null) && (Utils.Compare(targetDomainName, obj2.DnsDomainName) == 0)) { flag2 = true; } else if ((obj2.NetbiosDomainName != null) && (Utils.Compare(targetDomainName, obj2.NetbiosDomainName) == 0)) { flag2 = true; } if (!flag2 && ((obj2.Flags & 8) == 0)) { continue; } } if ((obj2.Flags & 8) != 0) { num2 = num4; if ((obj2.Flags & 4) == 0) { DS_DOMAIN_TRUSTS ds_domain_trusts3 = (DS_DOMAIN_TRUSTS) list[obj2.ParentIndex]; if (ds_domain_trusts3.DnsDomainName != IntPtr.Zero) { str2 = Marshal.PtrToStringUni(ds_domain_trusts3.DnsDomainName); } } obj2.TrustType = TrustType.Unknown | TrustType.ParentChild; } else if (ds_domain_trusts2.TrustType == 3) { obj2.TrustType = TrustType.Kerberos; } num4++; list2.Add(obj2); } } for (int k = 0; k < list2.Count; k++) { TrustObject obj3 = (TrustObject) list2[k]; if ((k != num2) && (obj3.TrustType != TrustType.Kerberos)) { if ((str2 != null) && (Utils.Compare(str2, obj3.DnsDomainName) == 0)) { obj3.TrustType = TrustType.ParentChild; } else if ((obj3.Flags & 1) != 0) { if (obj3.ParentIndex == ((TrustObject) list2[num2]).OriginalIndex) { obj3.TrustType = TrustType.ParentChild; } else if (((obj3.Flags & 4) != 0) && ((((TrustObject) list2[num2]).Flags & 4) != 0)) { string dnsNameFromDN = null; dnsNameFromDN = Utils.GetDnsNameFromDN(base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDomainNamingContext)); if (Utils.GetNewDirectoryContext(base.context.Name, DirectoryContextType.Forest, base.context).isRootDomain() || (Utils.Compare(obj3.DnsDomainName, dnsNameFromDN) == 0)) { obj3.TrustType = TrustType.TreeRoot; } else { obj3.TrustType = TrustType.CrossLink; } } else { obj3.TrustType = TrustType.CrossLink; } } else if ((obj3.TrustAttributes & 8) != 0) { obj3.TrustType = TrustType.Forest; } else { obj3.TrustType = TrustType.External; } } } } list3 = list2; } finally { if (zero != IntPtr.Zero) { System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.NetApiBufferFree(zero); } } return list3; }