Пример #1
0
        private void GetDomains()
        {
            // for ADAM, there is no concept of domain, we just return empty collection which is good enough
            if (!IsADAM)
            {
                string serverName = cachedEntry.Options.GetCurrentServerName();
                DomainController dc = DomainController.GetDomainController(Utils.GetNewDirectoryContext(serverName, DirectoryContextType.DirectoryServer, context));
                IntPtr handle = dc.Handle;

                Debug.Assert(handle != (IntPtr)0);

                IntPtr info = (IntPtr)0;
                // call DsReplicaSyncAllW
                IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListDomainsInSiteW");
                if (functionPtr == (IntPtr)0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                UnsafeNativeMethods.DsListDomainsInSiteW dsListDomainsInSiteW = (UnsafeNativeMethods.DsListDomainsInSiteW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsListDomainsInSiteW));

                int result = dsListDomainsInSiteW(handle, (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName), ref info);
                if (result != 0)
                    throw ExceptionHelper.GetExceptionFromErrorCode(result, serverName);

                try
                {
                    DS_NAME_RESULT names = new DS_NAME_RESULT();
                    Marshal.PtrToStructure(info, names);
                    int count = names.cItems;
                    IntPtr val = names.rItems;
                    if (count > 0)
                    {
                        Debug.Assert(val != (IntPtr)0);
                        int status = Marshal.ReadInt32(val);
                        IntPtr tmpPtr = (IntPtr)0;
                        for (int i = 0; i < count; i++)
                        {
                            tmpPtr = IntPtr.Add(val, Marshal.SizeOf(typeof(DS_NAME_RESULT_ITEM)) * i);
                            DS_NAME_RESULT_ITEM nameResult = new DS_NAME_RESULT_ITEM();
                            Marshal.PtrToStructure(tmpPtr, nameResult);
                            if (nameResult.status == DS_NAME_ERROR.DS_NAME_NO_ERROR || nameResult.status == DS_NAME_ERROR.DS_NAME_ERROR_DOMAIN_ONLY)
                            {
                                string domainName = Marshal.PtrToStringUni(nameResult.pName);
                                if (domainName != null && domainName.Length > 0)
                                {
                                    string d = Utils.GetDnsNameFromDN(domainName);
                                    Domain domain = new Domain(Utils.GetNewDirectoryContext(d, DirectoryContextType.Domain, context), d);
                                    _domains.Add(domain);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    // call DsFreeNameResultW
                    functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
                    if (functionPtr == (IntPtr)0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                    }
                    UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResultW = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsFreeNameResultW));

                    dsFreeNameResultW(info);
                }
            }
        }
Пример #2
0
		private void GetDomains()
		{
			if (!this.IsADAM)
			{
				string currentServerName = this.servers[0].Name; //TODO: REVIEW: this.cachedEntry.Options.GetCurrentServerName();
				DomainController domainController = DomainController.GetDomainController(Utils.GetNewDirectoryContext(currentServerName, DirectoryContextType.DirectoryServer, this.context));
				IntPtr handle = domainController.Handle;
				IntPtr intPtr = (IntPtr)0;
				IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListDomainsInSiteW");
				if (procAddress != (IntPtr)0)
				{
					UnsafeNativeMethods.DsListDomainsInSiteW delegateForFunctionPointer = (UnsafeNativeMethods.DsListDomainsInSiteW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsListDomainsInSiteW));
					int propertyValue = delegateForFunctionPointer(handle, (string)PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName), ref intPtr);
					if (propertyValue == 0)
					{
						try
						{
							DS_NAME_RESULT dSNAMERESULT = new DS_NAME_RESULT();
							Marshal.PtrToStructure(intPtr, dSNAMERESULT);
							int num = dSNAMERESULT.cItems;
							IntPtr intPtr1 = dSNAMERESULT.rItems;
							if (num > 0)
							{
								Marshal.ReadInt32(intPtr1);
								for (int i = 0; i < num; i++)
								{
									IntPtr intPtr2 = (IntPtr)((long)intPtr1 + (long)(Marshal.SizeOf(typeof(DS_NAME_RESULT_ITEM)) * i));
									DS_NAME_RESULT_ITEM dSNAMERESULTITEM = new DS_NAME_RESULT_ITEM();
									Marshal.PtrToStructure(intPtr2, dSNAMERESULTITEM);
									if (dSNAMERESULTITEM.status == DS_NAME_ERROR.DS_NAME_NO_ERROR || dSNAMERESULTITEM.status == DS_NAME_ERROR.DS_NAME_ERROR_DOMAIN_ONLY)
									{
										string stringUni = Marshal.PtrToStringUni(dSNAMERESULTITEM.pName);
										if (stringUni != null && stringUni.Length > 0)
										{
											string dnsNameFromDN = Utils.GetDnsNameFromDN(stringUni);
											Domain domain = new Domain(Utils.GetNewDirectoryContext(dnsNameFromDN, DirectoryContextType.Domain, this.context), dnsNameFromDN);
											this.domains.Add(domain);
										}
									}
								}
							}
						}
						finally
						{
							procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
							if (procAddress != (IntPtr)0)
							{
								UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResultW = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsFreeNameResultW));
								dsFreeNameResultW(intPtr);
							}
							else
							{
								throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
							}
						}
					}
					else
					{
						throw ExceptionHelper.GetExceptionFromErrorCode(propertyValue, currentServerName);
					}
				}
				else
				{
					throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
				}
			}
		}
 private void GetDomains()
 {
     if (!this.IsADAM)
     {
         string currentServerName = this.cachedEntry.Options.GetCurrentServerName();
         IntPtr handle = DomainController.GetDomainController(Utils.GetNewDirectoryContext(currentServerName, DirectoryContextType.DirectoryServer, this.context)).Handle;
         IntPtr zero = IntPtr.Zero;
         IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListDomainsInSiteW");
         if (procAddress == IntPtr.Zero)
         {
             throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
         }
         System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsListDomainsInSiteW delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsListDomainsInSiteW) Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsListDomainsInSiteW));
         int errorCode = delegateForFunctionPointer(handle, (string) PropertyManager.GetPropertyValue(this.context, this.cachedEntry, PropertyManager.DistinguishedName), ref zero);
         if (errorCode != 0)
         {
             throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, currentServerName);
         }
         try
         {
             DS_NAME_RESULT structure = new DS_NAME_RESULT();
             Marshal.PtrToStructure(zero, structure);
             int cItems = structure.cItems;
             IntPtr rItems = structure.rItems;
             if (cItems > 0)
             {
                 Marshal.ReadInt32(rItems);
                 IntPtr ptr = IntPtr.Zero;
                 for (int i = 0; i < cItems; i++)
                 {
                     ptr = (IntPtr) (((long) rItems) + (Marshal.SizeOf(typeof(DS_NAME_RESULT_ITEM)) * i));
                     DS_NAME_RESULT_ITEM ds_name_result_item = new DS_NAME_RESULT_ITEM();
                     Marshal.PtrToStructure(ptr, ds_name_result_item);
                     if ((ds_name_result_item.status == DS_NAME_ERROR.DS_NAME_NO_ERROR) || (ds_name_result_item.status == DS_NAME_ERROR.DS_NAME_ERROR_DOMAIN_ONLY))
                     {
                         string distinguishedName = Marshal.PtrToStringUni(ds_name_result_item.pName);
                         if ((distinguishedName != null) && (distinguishedName.Length > 0))
                         {
                             string dnsNameFromDN = Utils.GetDnsNameFromDN(distinguishedName);
                             Domain domain = new Domain(Utils.GetNewDirectoryContext(dnsNameFromDN, DirectoryContextType.Domain, this.context), dnsNameFromDN);
                             this.domains.Add(domain);
                         }
                     }
                 }
             }
         }
         finally
         {
             procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
             if (procAddress == IntPtr.Zero)
             {
                 throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
             }
             System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsFreeNameResultW tw = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsFreeNameResultW) Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsFreeNameResultW));
             tw(zero);
         }
     }
 }