internal static string GetDnsDomainName(string domainName)
        {
            DomainControllerInfo domainControllerInfo;
            int errorCode = 0;

            //
            // Locator.DsGetDcNameWrapper internally passes the ReturnDNSName flag when calling DsGetDcName
            //
            errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, (long)PrivateLocatorFlags.DirectoryServicesRequired, out domainControllerInfo);
            if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
            {
                // try again with force rediscovery
                errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, (long)((long)PrivateLocatorFlags.DirectoryServicesRequired | (long)LocatorOptions.ForceRediscovery), out domainControllerInfo);
                if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                {
                    return(null);
                }
                else if (errorCode != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
            }
            else if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }

            Debug.Assert(domainControllerInfo != null);
            Debug.Assert(domainControllerInfo.DomainName != null);

            return(domainControllerInfo.DomainName);
        }
        private DirectoryServer FindDirectoryServerInternal(string siteName, bool forceRediscovery)
        {
            DomainControllerInfo info;
            LocatorOptions       options = 0L;
            int errorCode = 0;

            if ((siteName != null) && (siteName.Length == 0))
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "siteName");
            }
            if (!this.committed)
            {
                throw new InvalidOperationException(Res.GetString("CannotPerformOperationOnUncommittedObject"));
            }
            if (forceRediscovery)
            {
                options = LocatorOptions.ForceRediscovery;
            }
            errorCode = Locator.DsGetDcNameWrapper(null, this.dnsName, siteName, options | 0x8000L, out info);
            if (errorCode == 0x54b)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ReplicaNotFound"), typeof(DirectoryServer), null);
            }
            if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
            string domainControllerName = info.DomainControllerName.Substring(2);

            return(new DomainController(Utils.GetNewDirectoryContext(domainControllerName, DirectoryContextType.DirectoryServer, base.context), domainControllerName));
        }
        //
        // this method is called when the forest name is explicitly specified
        // and we want to check if that matches the current logged on forest
        //
        internal bool isCurrentForest()
        {
            bool result = false;

            Debug.Assert(_name != null);
            DomainControllerInfo domainControllerInfo = Locator.GetDomainControllerInfo(null, _name, null, (long)(PrivateLocatorFlags.DirectoryServicesRequired | PrivateLocatorFlags.ReturnDNSName));

            DomainControllerInfo currentDomainControllerInfo;
            string loggedOnDomain = GetLoggedOnDomain();

            int errorCode = Locator.DsGetDcNameWrapper(null, loggedOnDomain, null, (long)(PrivateLocatorFlags.DirectoryServicesRequired | PrivateLocatorFlags.ReturnDNSName), out currentDomainControllerInfo);

            if (errorCode == 0)
            {
                Debug.Assert(domainControllerInfo.DnsForestName != null);
                Debug.Assert(currentDomainControllerInfo.DnsForestName != null);

                result = (Utils.Compare(domainControllerInfo.DnsForestName, currentDomainControllerInfo.DnsForestName) == 0);
            }
            //
            // if there is no forest associated with the logged on domain, then we return false
            //
            else if (errorCode != NativeMethods.ERROR_NO_SUCH_DOMAIN)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }

            return(result);
        }
Beispiel #4
0
        private static void GetLibraryHandle()
        {
            string systemDirectory = Environment.SystemDirectory;
            IntPtr intPtr          = UnsafeNativeMethods.LoadLibrary(string.Concat(systemDirectory, "\\ntdsapi.dll"));

            if (intPtr != (IntPtr)0)
            {
                DirectoryContext.ADHandle = new LoadLibrarySafeHandle(intPtr);
                DirectoryInfo parent = Directory.GetParent(systemDirectory);
                intPtr = UnsafeNativeMethods.LoadLibrary(string.Concat(parent.FullName, "\\ADAM\\ntdsapi.dll"));
                if (intPtr != (IntPtr)0)
                {
                    DirectoryContext.ADAMHandle = new LoadLibrarySafeHandle(intPtr);
                    return;
                }
                else
                {
                    DirectoryContext.ADAMHandle = DirectoryContext.ADHandle;
                    return;
                }
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
        }
        private static void GetLibraryHandle()
        {
            // first get AD handle
            string systemPath = Environment.SystemDirectory;
            IntPtr tempHandle = UnsafeNativeMethods.LoadLibrary(systemPath + "\\ntdsapi.dll");

            if (tempHandle == (IntPtr)0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            else
            {
                ADHandle = new LoadLibrarySafeHandle(tempHandle);
            }

            // not get the ADAM handle
            // got to the windows\adam directory
            DirectoryInfo windowsDirectory = Directory.GetParent(systemPath);

            tempHandle = UnsafeNativeMethods.LoadLibrary(windowsDirectory.FullName + "\\ADAM\\ntdsapi.dll");
            if (tempHandle == (IntPtr)0)
            {
                ADAMHandle = ADHandle;
            }
            else
            {
                ADAMHandle = new LoadLibrarySafeHandle(tempHandle);
            }
        }
        internal ForestTrustDomainInformation(int flag, LSA_FOREST_TRUST_DOMAIN_INFO domainInfo, LARGE_INTEGER time)
        {
            this.status  = (ForestTrustDomainStatus)flag;
            this.dnsName = Marshal.PtrToStringUni(domainInfo.DNSNameBuffer, domainInfo.DNSNameLength / 2);
            this.nbName  = Marshal.PtrToStringUni(domainInfo.NetBIOSNameBuffer, domainInfo.NetBIOSNameLength / 2);
            IntPtr intPtr     = (IntPtr)0;
            int    stringSidW = UnsafeNativeMethods.ConvertSidToStringSidW(domainInfo.sid, ref intPtr);

            if (stringSidW != 0)
            {
                try
                {
                    this.sid = Marshal.PtrToStringUni(intPtr);
                }
                finally
                {
                    UnsafeNativeMethods.LocalFree(intPtr);
                }
                this.time = time;
                return;
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
        }
Beispiel #7
0
        internal static string GetDnsDomainName(string domainName)
        {
            DomainControllerInfo domainControllerInfo = null;
            int num = Locator.DsGetDcNameWrapper(null, domainName, null, (long)16, out domainControllerInfo);

            if (num != 0x54b)
            {
                if (num != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(num);
                }
            }
            else
            {
                num = Locator.DsGetDcNameWrapper(null, domainName, null, (long)17, out domainControllerInfo);
                if (num != 0x54b)
                {
                    if (num != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(num);
                    }
                }
                else
                {
                    return(null);
                }
            }
            return(domainControllerInfo.DomainName);
        }
Beispiel #8
0
        internal static ArrayList EnumerateDomainControllers(DirectoryContext context, string domainName, string siteName, long dcFlags)
        {
            Hashtable            hashtables;
            DomainControllerInfo domainControllerInfo = null;
            ArrayList            arrayLists           = new ArrayList();

            if (siteName == null)
            {
                int num = Locator.DsGetDcNameWrapper(null, domainName, null, dcFlags & (long)0x9040, out domainControllerInfo);
                if (num != 0)
                {
                    if (num != 0x54b)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(num);
                    }
                    else
                    {
                        return(arrayLists);
                    }
                }
                else
                {
                    siteName = domainControllerInfo.ClientSiteName;
                }
            }
            if (!DirectoryContext.DnsgetdcSupported)
            {
                hashtables = Locator.DnsQueryWrapper(domainName, null, dcFlags);
                if (siteName != null)
                {
                    foreach (string key in Locator.DnsQueryWrapper(domainName, siteName, dcFlags).Keys)
                    {
                        if (hashtables.Contains(key))
                        {
                            continue;
                        }
                        hashtables.Add(key, null);
                    }
                }
            }
            else
            {
                hashtables = Locator.DnsGetDcWrapper(domainName, siteName, dcFlags);
            }
            foreach (string str in hashtables.Keys)
            {
                DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, context);
                if ((dcFlags & (long)64) == (long)0)
                {
                    arrayLists.Add(new DomainController(newDirectoryContext, str));
                }
                else
                {
                    arrayLists.Add(new GlobalCatalog(newDirectoryContext, str));
                }
            }
            return(arrayLists);
        }
Beispiel #9
0
        private static IntPtr GetTrustedDomainInfo(DirectoryContext targetContext, string? targetName, bool isForest)
        {
            PolicySafeHandle? policyHandle = null;
            IntPtr buffer = (IntPtr)0;
            bool impersonated = false;
            string? serverName = null;

            try
            {
                try
                {
                    serverName = Utils.GetPolicyServerName(targetContext, isForest, false, targetName);
                    impersonated = Utils.Impersonate(targetContext);
                    try
                    {
                        policyHandle = new PolicySafeHandle(Utils.GetPolicyHandle(serverName));
                    }
                    catch (ActiveDirectoryOperationException)
                    {
                        if (impersonated)
                        {
                            Utils.Revert();
                            impersonated = false;
                        }
                        // try anonymous
                        Utils.ImpersonateAnonymous();
                        impersonated = true;
                        policyHandle = new PolicySafeHandle(Utils.GetPolicyHandle(serverName));
                    }
                    catch (UnauthorizedAccessException)
                    {
                        if (impersonated)
                        {
                            Utils.Revert();
                            impersonated = false;
                        }
                        // try anonymous
                        Utils.ImpersonateAnonymous();
                        impersonated = true;
                        policyHandle = new PolicySafeHandle(Utils.GetPolicyHandle(serverName));
                    }

                    int result = UnsafeNativeMethods.LsaQueryInformationPolicy(policyHandle, policyDnsDomainInformation, out buffer);
                    if (result != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(UnsafeNativeMethods.LsaNtStatusToWinError(result), serverName);
                    }

                    return buffer;
                }
                finally
                {
                    if (impersonated)
                        Utils.Revert();
                }
            }
            catch { throw; }
        }
Beispiel #10
0
        internal static ArrayList EnumerateDomainControllers(DirectoryContext context, string?domainName, string?siteName, long dcFlags)
        {
            Hashtable?allDCs = null;
            ArrayList dcs    = new ArrayList();

            //
            // this api obtains the list of DCs/GCs based on dns records. The DCs/GCs that have registered
            // non site specific records for the domain/forest are returned. Additionally DCs/GCs that have registered site specific records
            // (site is either specified or defaulted to the site of the local machine) are also returned in this list.
            //

            if (siteName == null)
            {
                //
                // if the site name is not specified then we get the site specific records for the local machine's site (in the context of the domain/forest/application partition that is specified)
                // (sitename could still be null if the machine is not in any site for the specified domain/forest, in that case we don't look for any site specific records)
                //
                DomainControllerInfo domainControllerInfo;

                int errorCode = DsGetDcNameWrapper(null, domainName, null, dcFlags & (long)(PrivateLocatorFlags.GCRequired | PrivateLocatorFlags.DSWriteableRequired | PrivateLocatorFlags.OnlyLDAPNeeded), out domainControllerInfo);
                if (errorCode == 0)
                {
                    siteName = domainControllerInfo.ClientSiteName;
                }
                else if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                {
                    // return an empty collection
                    return(dcs);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
            }

            // this will get both the non site specific and the site specific records
            allDCs = DnsGetDcWrapper(domainName, siteName, dcFlags);

            foreach (string dcName in allDCs.Keys)
            {
                DirectoryContext dcContext = Utils.GetNewDirectoryContext(dcName, DirectoryContextType.DirectoryServer, context);

                if ((dcFlags & (long)PrivateLocatorFlags.GCRequired) != 0)
                {
                    // add a GlobalCatalog object
                    dcs.Add(new GlobalCatalog(dcContext, dcName));
                }
                else
                {
                    // add a domain controller object
                    dcs.Add(new DomainController(dcContext, dcName));
                }
            }

            return(dcs);
        }
Beispiel #11
0
        internal void SyncReplicaAllHelper(IntPtr handle, SyncReplicaFromAllServersCallback syncAllFunctionPointer, string partition, SyncFromAllServersOptions option, SyncUpdateCallback callback, LoadLibrarySafeHandle libHandle)
        {
            IntPtr errorInfo = (IntPtr)0;

            if (!Partitions.Contains(partition))
            {
                throw new ArgumentException(SR.ServerNotAReplica, "partition");
            }

            // we want to return the dn instead of DNS guid
            // call DsReplicaSyncAllW
            IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncAllW");

            if (functionPtr == (IntPtr)0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            UnsafeNativeMethods.DsReplicaSyncAllW dsReplicaSyncAllW = (UnsafeNativeMethods.DsReplicaSyncAllW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncAllW));

            int result = dsReplicaSyncAllW(handle, partition, (int)option | DS_REPSYNCALL_ID_SERVERS_BY_DN, syncAllFunctionPointer, (IntPtr)0, ref errorInfo);

            try
            {
                // error happens during the synchronization
                if (errorInfo != (IntPtr)0)
                {
                    SyncFromAllServersOperationException e = ExceptionHelper.CreateSyncAllException(errorInfo, false);
                    if (e == null)
                    {
                        return;
                    }
                    else
                    {
                        throw e;
                    }
                }
                else
                {
                    // API does not return error infor occurred during synchronization, but result is not success.
                    if (result != 0)
                    {
                        throw new SyncFromAllServersOperationException(ExceptionHelper.GetErrorMessage(result, false));
                    }
                }
            }
            finally
            {
                // release the memory
                if (errorInfo != (IntPtr)0)
                {
                    UnsafeNativeMethods.LocalFree(errorInfo);
                }
            }
        }
        internal static DomainControllerInfo GetDomainControllerInfo(string computerName, string domainName, string siteName, long flags)
        {
            DomainControllerInfo info;
            int errorCode = 0;

            errorCode = DsGetDcNameWrapper(computerName, domainName, siteName, flags, out info);
            if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, domainName);
            }
            return(info);
        }
        internal static ArrayList EnumerateDomainControllers(DirectoryContext context, string domainName, string siteName, long dcFlags)
        {
            Hashtable hashtable = null;
            ArrayList list      = new ArrayList();

            if (siteName == null)
            {
                DomainControllerInfo info;
                int errorCode = DsGetDcNameWrapper(null, domainName, null, dcFlags & 0x9040L, out info);
                switch (errorCode)
                {
                case 0:
                    siteName = info.ClientSiteName;
                    goto Label_003C;

                case 0x54b:
                    return(list);
                }
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
Label_003C:
            if (DirectoryContext.DnsgetdcSupported)
            {
                hashtable = DnsGetDcWrapper(domainName, siteName, dcFlags);
            }
            else
            {
                hashtable = DnsQueryWrapper(domainName, null, dcFlags);
                if (siteName != null)
                {
                    foreach (string str in DnsQueryWrapper(domainName, siteName, dcFlags).Keys)
                    {
                        if (!hashtable.Contains(str))
                        {
                            hashtable.Add(str, null);
                        }
                    }
                }
            }
            foreach (string str2 in hashtable.Keys)
            {
                DirectoryContext context2 = Utils.GetNewDirectoryContext(str2, DirectoryContextType.DirectoryServer, context);
                if ((dcFlags & 0x40L) != 0L)
                {
                    list.Add(new GlobalCatalog(context2, str2));
                }
                else
                {
                    list.Add(new DomainController(context2, str2));
                }
            }
            return(list);
        }
Beispiel #14
0
 private unsafe void FreeReplicaInfo(DS_REPL_INFO_TYPE type, IntPtr value, SafeLibraryHandle libHandle)
 {
     if (value != (IntPtr)0)
     {
         // call DsReplicaFreeInfo
         var dsReplicaFreeInfo = (delegate * unmanaged <int, IntPtr, int>)global::Interop.Kernel32.GetProcAddress(libHandle, "DsReplicaFreeInfo");
         if (dsReplicaFreeInfo == null)
         {
             throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
         }
         dsReplicaFreeInfo((int)type, value);
     }
 }
 private void FreeReplicaInfo(DS_REPL_INFO_TYPE type, IntPtr value, LoadLibrarySafeHandle libHandle)
 {
     if (value != IntPtr.Zero)
     {
         IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaFreeInfo");
         if (procAddress == IntPtr.Zero)
         {
             throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
         }
         System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaFreeInfo delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaFreeInfo)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaFreeInfo));
         delegateForFunctionPointer((int)type, value);
     }
 }
Beispiel #16
0
        internal static DomainControllerInfo GetDomainControllerInfo(string computerName, string domainName, string siteName, long flags)
        {
            DomainControllerInfo domainControllerInfo = null;
            int num = Locator.DsGetDcNameWrapper(computerName, domainName, siteName, flags, out domainControllerInfo);

            if (num == 0)
            {
                return(domainControllerInfo);
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(num, domainName);
            }
        }
Beispiel #17
0
        private static void ValidateTrust(PolicySafeHandle handle, LSA_UNICODE_STRING trustedDomainName, string? sourceName, string? targetName, bool isForest, int direction, string serverName)
        {
            IntPtr buffer = (IntPtr)0;

            // get trust information
            int result = UnsafeNativeMethods.LsaQueryTrustedDomainInfoByName(handle, trustedDomainName, TRUSTED_INFORMATION_CLASS.TrustedDomainInformationEx, ref buffer);
            if (result != 0)
            {
                int win32Error = UnsafeNativeMethods.LsaNtStatusToWinError(result);
                // 2 ERROR_FILE_NOT_FOUND <--> 0xc0000034 STATUS_OBJECT_NAME_NOT_FOUND
                if (win32Error == STATUS_OBJECT_NAME_NOT_FOUND)
                {
                    if (isForest)
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.ForestTrustDoesNotExist, sourceName, targetName), typeof(ForestTrustRelationshipInformation), null);
                    else
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DomainTrustDoesNotExist, sourceName, targetName), typeof(TrustRelationshipInformation), null);
                }
                else
                    throw ExceptionHelper.GetExceptionFromErrorCode(win32Error, serverName);
            }

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

            try
            {
                TRUSTED_DOMAIN_INFORMATION_EX domainInfo = new TRUSTED_DOMAIN_INFORMATION_EX();
                Marshal.PtrToStructure(buffer, domainInfo);

                // validate this is the trust that the user refers to
                ValidateTrustAttribute(domainInfo, isForest, sourceName, targetName);

                // validate trust direction if applicable
                if (direction != 0)
                {
                    if ((direction & domainInfo.TrustDirection) == 0)
                    {
                        if (isForest)
                            throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.WrongTrustDirection, sourceName, targetName, (TrustDirection)direction), typeof(ForestTrustRelationshipInformation), null);
                        else
                            throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.WrongTrustDirection, sourceName, targetName, (TrustDirection)direction), typeof(TrustRelationshipInformation), null);
                    }
                }
            }
            finally
            {
                if (buffer != (IntPtr)0)
                    UnsafeNativeMethods.LsaFreeMemory(buffer);
            }
        }
Beispiel #18
0
        private void FreeReplicaInfo(DS_REPL_INFO_TYPE type, IntPtr value, LoadLibrarySafeHandle libHandle)
        {
            if (value != (IntPtr)0)
            {
                // call DsReplicaFreeInfo
                IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaFreeInfo");
                if (functionPtr == (IntPtr)0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                UnsafeNativeMethods.DsReplicaFreeInfo dsReplicaFreeInfo = (UnsafeNativeMethods.DsReplicaFreeInfo)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaFreeInfo));

                dsReplicaFreeInfo((int)type, value);
            }
        }
Beispiel #19
0
        internal void SyncReplicaAllHelper(IntPtr handle, SyncReplicaFromAllServersCallback syncAllFunctionPointer, string partition, SyncFromAllServersOptions option, SyncUpdateCallback callback, LoadLibrarySafeHandle libHandle)
        {
            IntPtr intPtr = (IntPtr)0;

            if (this.Partitions.Contains(partition))
            {
                IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncAllW");
                if (procAddress != (IntPtr)0)
                {
                    UnsafeNativeMethods.DsReplicaSyncAllW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncAllW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncAllW));
                    int num = delegateForFunctionPointer(handle, partition, (int)option | 4, syncAllFunctionPointer, (IntPtr)0, ref intPtr);
                    try
                    {
                        if (intPtr == (IntPtr)0)
                        {
                            if (num != 0)
                            {
                                throw new SyncFromAllServersOperationException(ExceptionHelper.GetErrorMessage(num, false));
                            }
                        }
                        else
                        {
                            SyncFromAllServersOperationException syncFromAllServersOperationException = ExceptionHelper.CreateSyncAllException(intPtr, false);
                            if (syncFromAllServersOperationException != null)
                            {
                                throw syncFromAllServersOperationException;
                            }
                        }
                    }
                    finally
                    {
                        if (intPtr != (IntPtr)0)
                        {
                            UnsafeNativeMethods.LocalFree(intPtr);
                        }
                    }
                    return;
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
            }
            else
            {
                throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition");
            }
        }
        internal void CheckConsistencyHelper(IntPtr dsHandle, LoadLibrarySafeHandle libHandle)
        {
            IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaConsistencyCheck");

            if (procAddress == IntPtr.Zero)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaConsistencyCheck delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaConsistencyCheck)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaConsistencyCheck));
            int errorCode = delegateForFunctionPointer(dsHandle, 0, 0);

            if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, this.Name);
            }
        }
Beispiel #21
0
        internal unsafe void CheckConsistencyHelper(IntPtr dsHandle, SafeLibraryHandle libHandle)
        {
            // call DsReplicaConsistencyCheck
            var replicaConsistencyCheck = (delegate * unmanaged <IntPtr, int, int, int>)global::Interop.Kernel32.GetProcAddress(libHandle, "DsReplicaConsistencyCheck");

            if (replicaConsistencyCheck == null)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }

            int result = replicaConsistencyCheck(dsHandle, 0, 0);

            if (result != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(result, Name);
            }
        }
Beispiel #22
0
        internal ForestTrustDomainInformation(int flag, LSA_FOREST_TRUST_DOMAIN_INFO domainInfo, LARGE_INTEGER time)
        {
            _status     = (ForestTrustDomainStatus)flag;
            DnsName     = Marshal.PtrToStringUni(domainInfo.DNSNameBuffer, domainInfo.DNSNameLength / 2);
            NetBiosName = Marshal.PtrToStringUni(domainInfo.NetBIOSNameBuffer, domainInfo.NetBIOSNameLength / 2);

            string sidLocal;

            global::Interop.BOOL result = global::Interop.Advapi32.ConvertSidToStringSid(domainInfo.sid, out sidLocal);
            if (result == global::Interop.BOOL.FALSE)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }

            DomainSid = sidLocal;
            this.time = time;
        }
Beispiel #23
0
        private unsafe 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

                /*DWORD DsListSites(
                 *  HANDLE hDs,
                 *  PDS_NAME_RESULT* ppSites
                 *  );*/
                var dsListSites = (delegate * unmanaged <IntPtr, IntPtr *, int>)global::Interop.Kernel32.GetProcAddress(DirectoryContext.ADHandle, "DsListSitesW");
                if (dsListSites == null)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError());
                }

                result = dsListSites(dsHandle, &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));
                            }
Beispiel #24
0
        internal void CheckConsistencyHelper(IntPtr dsHandle, LoadLibrarySafeHandle libHandle)
        {
            // call DsReplicaConsistencyCheck
            IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaConsistencyCheck");

            if (functionPtr == (IntPtr)0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            UnsafeNativeMethods.DsReplicaConsistencyCheck replicaConsistencyCheck = (UnsafeNativeMethods.DsReplicaConsistencyCheck)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaConsistencyCheck));

            int result = replicaConsistencyCheck(dsHandle, 0, 0);

            if (result != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(result, Name);
            }
        }
        internal static GlobalCatalog FindOneInternal(DirectoryContext context, string forestName, string siteName, LocatorOptions flag)
        {
            DomainControllerInfo info;
            int errorCode = 0;

            if ((siteName != null) && (siteName.Length == 0))
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "siteName");
            }
            if ((flag & ~(LocatorOptions.AvoidSelf | LocatorOptions.ForceRediscovery | LocatorOptions.KdcRequired | LocatorOptions.TimeServerRequired | LocatorOptions.WriteableRequired)) != 0L)
            {
                throw new ArgumentException(Res.GetString("InvalidFlags"), "flag");
            }
            if (forestName == null)
            {
                DomainControllerInfo info2;
                int num2 = Locator.DsGetDcNameWrapper(null, DirectoryContext.GetLoggedOnDomain(), null, 0x10L, out info2);
                if (num2 == 0x54b)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(GlobalCatalog), null);
                }
                if (num2 != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                forestName = info2.DnsForestName;
            }
            errorCode = Locator.DsGetDcNameWrapper(null, forestName, siteName, flag | 80L, out info);
            switch (errorCode)
            {
            case 0x54b:
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFoundInForest", new object[] { forestName }), typeof(GlobalCatalog), null);

            case 0x3ec:
                throw new ArgumentException(Res.GetString("InvalidFlags"), "flag");
            }
            if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
            string globalCatalogName = info.DomainControllerName.Substring(2);

            return(new GlobalCatalog(Utils.GetNewDirectoryContext(globalCatalogName, DirectoryContextType.DirectoryServer, context), globalCatalogName));
        }
Beispiel #26
0
        internal bool isCurrentForest()
        {
            DomainControllerInfo domainControllerInfo = null;
            bool flag = false;
            DomainControllerInfo domainControllerInfo1 = Locator.GetDomainControllerInfo(null, this.name, null, (long)0x40000010);
            string loggedOnDomain = DirectoryContext.GetLoggedOnDomain();
            int    num            = Locator.DsGetDcNameWrapper(null, loggedOnDomain, null, (long)0x40000010, out domainControllerInfo);

            if (num != 0)
            {
                if (num != 0x54b)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(num);
                }
            }
            else
            {
                flag = Utils.Compare(domainControllerInfo1.DnsForestName, domainControllerInfo.DnsForestName) == 0;
            }
            return(flag);
        }
        private static void ValidateTrust(PolicySafeHandle handle, LSA_UNICODE_STRING trustedDomainName, string sourceName, string targetName, bool isForest, int direction, string serverName)
        {
            IntPtr zero   = IntPtr.Zero;
            int    status = UnsafeNativeMethods.LsaQueryTrustedDomainInfoByName(handle, trustedDomainName, TRUSTED_INFORMATION_CLASS.TrustedDomainInformationEx, ref zero);

            if (status != 0)
            {
                int errorCode = UnsafeNativeMethods.LsaNtStatusToWinError(status);
                if (errorCode != STATUS_OBJECT_NAME_NOT_FOUND)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, serverName);
                }
                if (isForest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestTrustDoesNotExist", new object[] { sourceName, targetName }), typeof(ForestTrustRelationshipInformation), null);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DomainTrustDoesNotExist", new object[] { sourceName, targetName }), typeof(TrustRelationshipInformation), null);
            }
            try
            {
                TRUSTED_DOMAIN_INFORMATION_EX structure = new TRUSTED_DOMAIN_INFORMATION_EX();
                Marshal.PtrToStructure(zero, structure);
                ValidateTrustAttribute(structure, isForest, sourceName, targetName);
                if ((direction != 0) && ((direction & structure.TrustDirection) == 0))
                {
                    if (isForest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("WrongTrustDirection", new object[] { sourceName, targetName, (TrustDirection)direction }), typeof(ForestTrustRelationshipInformation), null);
                    }
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("WrongTrustDirection", new object[] { sourceName, targetName, (TrustDirection)direction }), typeof(TrustRelationshipInformation), null);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    UnsafeNativeMethods.LsaFreeMemory(zero);
                }
            }
        }
        internal void SyncReplicaAllHelper(IntPtr handle, SyncReplicaFromAllServersCallback syncAllFunctionPointer, string partition, SyncFromAllServersOptions option, SyncUpdateCallback callback, LoadLibrarySafeHandle libHandle)
        {
            IntPtr zero = IntPtr.Zero;

            if (!this.Partitions.Contains(partition))
            {
                throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition");
            }
            IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncAllW");

            if (procAddress == IntPtr.Zero)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncAllW delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncAllW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncAllW));
            int errorCode = delegateForFunctionPointer(handle, partition, ((int)option) | 4, syncAllFunctionPointer, IntPtr.Zero, ref zero);

            try
            {
                if (zero != IntPtr.Zero)
                {
                    SyncFromAllServersOperationException exception = ExceptionHelper.CreateSyncAllException(zero, false);
                    if (exception != null)
                    {
                        throw exception;
                    }
                }
                else if (errorCode != 0)
                {
                    throw new SyncFromAllServersOperationException(ExceptionHelper.GetErrorMessage(errorCode, false));
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.LocalFree(zero);
                }
            }
        }
Beispiel #29
0
        internal void CheckConsistencyHelper(IntPtr dsHandle, LoadLibrarySafeHandle libHandle)
        {
            IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaConsistencyCheck");

            if (procAddress != (IntPtr)0)
            {
                UnsafeNativeMethods.DsReplicaConsistencyCheck delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaConsistencyCheck)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaConsistencyCheck));
                int num = delegateForFunctionPointer(dsHandle, 0, 0);
                if (num == 0)
                {
                    return;
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(num, this.Name);
                }
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
        }
        internal ForestTrustDomainInformation(int flag, LSA_FOREST_TRUST_DOMAIN_INFO domainInfo, LARGE_INTEGER time)
        {
            _status     = (ForestTrustDomainStatus)flag;
            DnsName     = Marshal.PtrToStringUni(domainInfo.DNSNameBuffer, domainInfo.DNSNameLength / 2);
            NetBiosName = Marshal.PtrToStringUni(domainInfo.NetBIOSNameBuffer, domainInfo.NetBIOSNameLength / 2);
            IntPtr ptr    = (IntPtr)0;
            int    result = UnsafeNativeMethods.ConvertSidToStringSidW(domainInfo.sid, ref ptr);

            if (result == 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }

            try
            {
                DomainSid = Marshal.PtrToStringUni(ptr) !;
            }
            finally
            {
                UnsafeNativeMethods.LocalFree(ptr);
            }

            this.time = time;
        }