private ArrayList GetSites()
 {
     ArrayList list = new ArrayList();
     int errorCode = 0;
     IntPtr zero = IntPtr.Zero;
     IntPtr authIdentity = IntPtr.Zero;
     IntPtr ptr3 = IntPtr.Zero;
     try
     {
         this.GetDSHandle(out zero, out authIdentity);
         IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListSitesW");
         if (procAddress == IntPtr.Zero)
         {
             throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
         }
         System.DirectoryServices.ActiveDirectory.NativeMethods.DsListSites delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.NativeMethods.DsListSites) Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.NativeMethods.DsListSites));
         errorCode = delegateForFunctionPointer(zero, out ptr3);
         if (errorCode == 0)
         {
             try
             {
                 DsNameResult structure = new DsNameResult();
                 Marshal.PtrToStructure(ptr3, structure);
                 IntPtr items = structure.items;
                 for (int i = 0; i < structure.itemCount; i++)
                 {
                     DsNameResultItem item = new DsNameResultItem();
                     Marshal.PtrToStructure(items, item);
                     if (item.status == 0)
                     {
                         string siteName = Utils.GetDNComponents(item.name)[0].Value;
                         list.Add(new ActiveDirectorySite(this.context, siteName, true));
                     }
                     items = (IntPtr) (((long) items) + Marshal.SizeOf(item));
                 }
                 return list;
             }
             finally
             {
                 if (ptr3 != IntPtr.Zero)
                 {
                     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(ptr3);
                 }
             }
         }
         throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, this.context.GetServerName());
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             Utils.FreeDSHandle(zero, DirectoryContext.ADHandle);
         }
         if (authIdentity != IntPtr.Zero)
         {
             Utils.FreeAuthIdentity(authIdentity, DirectoryContext.ADHandle);
         }
     }
     return list;
 }
Esempio n. 2
0
        private ArrayList GetRoles()
        {
            ArrayList roleList = new ArrayList();
            int result = 0;
            IntPtr rolesPtr = IntPtr.Zero;

            GetDSHandle();
            // Get the roles
            // call DsListRoles
            IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListRolesW");
            if (functionPtr == (IntPtr)0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            NativeMethods.DsListRoles dsListRoles = (NativeMethods.DsListRoles)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(NativeMethods.DsListRoles));

            result = dsListRoles(_dsHandle, out rolesPtr);
            if (result == 0)
            {
                try
                {
                    DsNameResult dsNameResult = new DsNameResult();
                    Marshal.PtrToStructure(rolesPtr, dsNameResult);
                    IntPtr currentItem = dsNameResult.items;
                    for (int i = 0; i < dsNameResult.itemCount; i++)
                    {
                        DsNameResultItem dsNameResultItem = new DsNameResultItem();
                        Marshal.PtrToStructure(currentItem, dsNameResultItem);

                        // check if the role owner is this dc
                        if (dsNameResultItem.status == NativeMethods.DsNameNoError)
                        {
                            if (dsNameResultItem.name.Equals(NtdsaObjectName))
                            {
                                // add this role to the array
                                // the index of the item in the result signifies
                                // which role owner it is
                                roleList.Add((ActiveDirectoryRole)i);
                            }
                        }
                        currentItem = IntPtr.Add(currentItem, Marshal.SizeOf(dsNameResultItem));
                    }
                }
                finally
                {
                    // free the DsNameResult structure
                    if (rolesPtr != IntPtr.Zero)
                    {
                        // call DsFreeNameResult
                        functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
                        if (functionPtr == (IntPtr)0)
                        {
                            throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                        }
                        UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResult = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsFreeNameResultW));
                        dsFreeNameResult(rolesPtr);
                    }
                }
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(result, Name);
            }
            return roleList;
        }
Esempio n. 3
0
		internal static string GetDNFromDnsName(string dnsName)
		{
			string str = null;
			IntPtr zero = IntPtr.Zero;
			IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsCrackNamesW");
			if (procAddress != (IntPtr)0)
			{
				NativeMethods.DsCrackNames delegateForFunctionPointer = (NativeMethods.DsCrackNames)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(NativeMethods.DsCrackNames));
				IntPtr hGlobalUni = Marshal.StringToHGlobalUni(string.Concat(dnsName, "/"));
				IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
				Marshal.WriteIntPtr(intPtr, hGlobalUni);
				int num = delegateForFunctionPointer(IntPtr.Zero, 1, 7, 1, 1, intPtr, out zero);
				if (num != 0)
				{
					if (num != 6)
					{
						throw ExceptionHelper.GetExceptionFromErrorCode(num);
					}
					else
					{
						throw new ArgumentException(Res.GetString("InvalidDNFormat"));
					}
				}
				else
				{
					try
					{
						DsNameResult dsNameResult = new DsNameResult();
						Marshal.PtrToStructure(zero, dsNameResult);
						if (dsNameResult.itemCount >= 1 && dsNameResult.items != IntPtr.Zero)
						{
							DsNameResultItem dsNameResultItem = new DsNameResultItem();
							Marshal.PtrToStructure(dsNameResult.items, dsNameResultItem);
							str = dsNameResultItem.name;
						}
					}
					finally
					{
						if (intPtr != (IntPtr)0)
						{
							Marshal.FreeHGlobal(intPtr);
						}
						if (hGlobalUni != (IntPtr)0)
						{
							Marshal.FreeHGlobal(hGlobalUni);
						}
						if (zero != IntPtr.Zero)
						{
							procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
							if (procAddress != (IntPtr)0)
							{
								UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResultW = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsFreeNameResultW));
								dsFreeNameResultW(zero);
							}
							else
							{
								throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
							}
						}
					}
					return str;
				}
			}
			else
			{
				throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
			}
		}
        internal static string GetDnsNameFromDN(string distinguishedName)
        {
            int errorCode = 0;
            string name = null;
            IntPtr zero = IntPtr.Zero;
            IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsCrackNamesW");
            if (procAddress == IntPtr.Zero)
            {
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            System.DirectoryServices.ActiveDirectory.NativeMethods.DsCrackNames delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.NativeMethods.DsCrackNames) Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.NativeMethods.DsCrackNames));
            IntPtr val = Marshal.StringToHGlobalUni(distinguishedName);
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
            Marshal.WriteIntPtr(ptr, val);
            errorCode = delegateForFunctionPointer(IntPtr.Zero, 1, 1, 7, 1, ptr, out zero);
            switch (errorCode)
            {
                case 0:
                    try
                    {
                        DsNameResult structure = new DsNameResult();
                        Marshal.PtrToStructure(zero, structure);
                        if ((structure.itemCount < 1) || !(structure.items != IntPtr.Zero))
                        {
                            return name;
                        }
                        DsNameResultItem item = new DsNameResultItem();
                        Marshal.PtrToStructure(structure.items, item);
                        if ((item.status == 6) || (item.name == null))
                        {
                            throw new ArgumentException(Res.GetString("InvalidDNFormat"), "distinguishedName");
                        }
                        if (item.status != 0)
                        {
                            throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                        }
                        if ((item.name.Length - 1) == item.name.IndexOf('/'))
                        {
                            name = item.name.Substring(0, item.name.Length - 1);
                        }
                        name = item.name;
                    }
                    finally
                    {
                        if (ptr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(ptr);
                        }
                        if (val != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(val);
                        }
                        if (zero != IntPtr.Zero)
                        {
                            procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
                            if (procAddress == IntPtr.Zero)
                            {
                                throw System.DirectoryServices.ActiveDirectory.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);
                        }
                    }
                    break;

                case 6:
                    throw new ArgumentException(Res.GetString("InvalidDNFormat"), "distinguishedName");
            }
            throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
        }
Esempio n. 5
0
        internal static string GetDNFromDnsName(string dnsName)
        {
            int result = 0;
            string dn = null;
            IntPtr results = IntPtr.Zero;

            Debug.Assert(dnsName != null);

            // call DsCrackNamesW
            IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsCrackNamesW");
            if (functionPtr == (IntPtr)0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            NativeMethods.DsCrackNames dsCrackNames = (NativeMethods.DsCrackNames)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(NativeMethods.DsCrackNames));
            IntPtr name = Marshal.StringToHGlobalUni(dnsName + "/");
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
            Marshal.WriteIntPtr(ptr, name);
            result = dsCrackNames(IntPtr.Zero, NativeMethods.DS_NAME_FLAG_SYNTACTICAL_ONLY,
                         NativeMethods.DS_CANONICAL_NAME, NativeMethods.DS_FQDN_1779_NAME, 1, ptr, out results);
            if (result == 0)
            {
                try
                {
                    DsNameResult dsNameResult = new DsNameResult();
                    Marshal.PtrToStructure(results, dsNameResult);

                    if ((dsNameResult.itemCount >= 1) && (dsNameResult.items != IntPtr.Zero))
                    {
                        DsNameResultItem dsNameResultItem = new DsNameResultItem();
                        Marshal.PtrToStructure(dsNameResult.items, dsNameResultItem);
                        dn = dsNameResultItem.name;
                    }
                }
                finally
                {
                    if (ptr != (IntPtr)0)
                        Marshal.FreeHGlobal(ptr);

                    if (name != (IntPtr)0)
                        Marshal.FreeHGlobal(name);
                    // free the results
                    if (results != IntPtr.Zero)
                    {
                        // 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(results);
                    }
                }
            }
            else if (result == NativeMethods.DS_NAME_ERROR_NO_SYNTACTICAL_MAPPING)
            {
                throw new ArgumentException(Res.GetString(Res.InvalidDNFormat));
            }
            else
            {
                // it is only syntatic mapping, we don't go on the wire
                throw ExceptionHelper.GetExceptionFromErrorCode(result);
            }

            return dn;
        }
Esempio n. 6
0
        private ArrayList GetSites()
        {
            ArrayList sites = new ArrayList();
            int result = 0;
            IntPtr dsHandle = IntPtr.Zero;
            IntPtr authIdentity = IntPtr.Zero;
            IntPtr sitesPtr = IntPtr.Zero;

            try
            {
                // get the directory handle
                GetDSHandle(out dsHandle, out authIdentity);

                // Get the sites within the forest
                // call DsListSites
                IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListSitesW");
                if (functionPtr == (IntPtr)0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                NativeMethods.DsListSites dsListSites = (NativeMethods.DsListSites)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(NativeMethods.DsListSites));

                result = dsListSites(dsHandle, out sitesPtr);
                if (result == 0)
                {
                    try
                    {
                        DsNameResult dsNameResult = new DsNameResult();
                        Marshal.PtrToStructure(sitesPtr, dsNameResult);
                        IntPtr currentItem = dsNameResult.items;

                        for (int i = 0; i < dsNameResult.itemCount; i++)
                        {
                            DsNameResultItem dsNameResultItem = new DsNameResultItem();

                            Marshal.PtrToStructure(currentItem, dsNameResultItem);
                            if (dsNameResultItem.status == NativeMethods.DsNameNoError)
                            {
                                string siteName = Utils.GetDNComponents(dsNameResultItem.name)[0].Value;
                                // an existing site
                                sites.Add(new ActiveDirectorySite(_context, siteName, true));
                            }

                            currentItem = IntPtr.Add(currentItem, Marshal.SizeOf(dsNameResultItem));
                        }
                    }
                    finally
                    {
                        // free the DsNameResult structure
                        if (sitesPtr != IntPtr.Zero)
                        {
                            // 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(sitesPtr);
                        }
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(result, _context.GetServerName());
                }
            }
            finally
            {
                // DsUnbind
                if (dsHandle != (IntPtr)0)
                {
                    Utils.FreeDSHandle(dsHandle, DirectoryContext.ADHandle);
                }

                // free the credentials object
                if (authIdentity != (IntPtr)0)
                {
                    Utils.FreeAuthIdentity(authIdentity, DirectoryContext.ADHandle);
                }
            }

            return sites;
        }
Esempio n. 7
0
		private ArrayList GetRoles()
		{
			ArrayList arrayLists = new ArrayList();
			IntPtr zero = IntPtr.Zero;
			this.GetDSHandle();
			IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsListRolesW");
			if (procAddress != (IntPtr)0)
			{
				NativeMethods.DsListRoles delegateForFunctionPointer = (NativeMethods.DsListRoles)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(NativeMethods.DsListRoles));
				int num = delegateForFunctionPointer(this.dsHandle, out zero);
				if (num != 0)
				{
					throw ExceptionHelper.GetExceptionFromErrorCode(num, base.Name);
				}
				else
				{
					try
					{
						DsNameResult dsNameResult = new DsNameResult();
						Marshal.PtrToStructure(zero, dsNameResult);
						IntPtr intPtr = dsNameResult.items;
						for (int i = 0; i < dsNameResult.itemCount; i++)
						{
							DsNameResultItem dsNameResultItem = new DsNameResultItem();
							Marshal.PtrToStructure(intPtr, dsNameResultItem);
							if (dsNameResultItem.status == 0 && dsNameResultItem.name.Equals(this.NtdsaObjectName))
							{
								arrayLists.Add((ActiveDirectoryRole)i);
							}
							intPtr = (IntPtr)((long)intPtr + (long)Marshal.SizeOf(dsNameResultItem));
						}
					}
					finally
					{
						if (zero != IntPtr.Zero)
						{
							procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
							if (procAddress != (IntPtr)0)
							{
								UnsafeNativeMethods.DsFreeNameResultW dsFreeNameResultW = (UnsafeNativeMethods.DsFreeNameResultW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsFreeNameResultW));
								dsFreeNameResultW(zero);
							}
							else
							{
								throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
							}
						}
					}
					return arrayLists;
				}
			}
			else
			{
				throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
			}
		}