コード例 #1
0
        internal static DomainController FindOneWithCredentialValidation(DirectoryContext context, string siteName, LocatorOptions flag)
        {
            bool flag2 = false;
            bool flag3 = false;

            context = new DirectoryContext(context);
            DomainController dc = FindOneInternal(context, context.Name, siteName, flag);

            try
            {
                ValidateCredential(dc, context);
                flag3 = true;
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode != -2147016646)
                {
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                if ((flag & LocatorOptions.ForceRediscovery) != 0L)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFoundInDomain", new object[] { context.Name }), typeof(DomainController), null);
                }
                flag2 = true;
            }
            finally
            {
                if (!flag3)
                {
                    dc.Dispose();
                }
            }
            if (flag2)
            {
                flag3 = false;
                dc    = FindOneInternal(context, context.Name, siteName, flag | LocatorOptions.ForceRediscovery);
                try
                {
                    ValidateCredential(dc, context);
                    flag3 = true;
                }
                catch (COMException exception2)
                {
                    if (exception2.ErrorCode == -2147016646)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFoundInDomain", new object[] { context.Name }), typeof(DomainController), null);
                    }
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception2);
                }
                finally
                {
                    if (!flag3)
                    {
                        dc.Dispose();
                    }
                }
            }
            return(dc);
        }
コード例 #2
0
        internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle)
        {
            int              structSize    = Marshal.SizeOf(typeof(Guid));
            IntPtr           unmanagedGuid = (IntPtr)0;
            Guid             guid          = Guid.Empty;
            AdamInstance     adamServer    = null;
            DomainController dcServer      = null;

            unmanagedGuid = Marshal.AllocHGlobal(structSize);
            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context);
                    if (isADAM)
                    {
                        adamServer = AdamInstance.GetAdamInstance(newContext);
                        guid       = adamServer.NtdsaObjectGuid;
                    }
                    else
                    {
                        dcServer = DomainController.GetDomainController(newContext);
                        guid     = dcServer.NtdsaObjectGuid;
                    }

                    Marshal.StructureToPtr(guid, unmanagedGuid, false);
                }

                // call DsReplicaSyncW
                IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (functionPtr == (IntPtr)0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                UnsafeNativeMethods.DsReplicaSyncW dsReplicaSyncW = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaSyncW));

                int result = dsReplicaSyncW(dsHandle, partition, unmanagedGuid, (int)option);

                // check the result
                if (result != 0)
                {
                    if (!Partitions.Contains(partition))
                    {
                        throw new ArgumentException(SR.ServerNotAReplica, "partition");
                    }

                    string serverDownName = null;
                    // this is the error returned when the server that we want to sync from is down
                    if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                    {
                        serverDownName = sourceServer;
                    }
                    // this is the error returned when the server that we want to get synced is down
                    else if (result == ExceptionHelper.RPC_S_CALL_FAILED)
                    {
                        serverDownName = Name;
                    }

                    throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName);
                }
            }
            finally
            {
                if (unmanagedGuid != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(unmanagedGuid);
                }

                if (adamServer != null)
                {
                    adamServer.Dispose();
                }

                if (dcServer != null)
                {
                    dcServer.Dispose();
                }
            }
        }
コード例 #3
0
        internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle)
        {
            int              cb               = Marshal.SizeOf(typeof(Guid));
            IntPtr           zero             = IntPtr.Zero;
            Guid             empty            = Guid.Empty;
            AdamInstance     adamInstance     = null;
            DomainController domainController = null;

            zero = Marshal.AllocHGlobal(cb);
            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext context = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context);
                    if (isADAM)
                    {
                        adamInstance = AdamInstance.GetAdamInstance(context);
                        empty        = adamInstance.NtdsaObjectGuid;
                    }
                    else
                    {
                        domainController = DomainController.GetDomainController(context);
                        empty            = domainController.NtdsaObjectGuid;
                    }
                    Marshal.StructureToPtr(empty, zero, false);
                }
                IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (procAddress == IntPtr.Zero)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
                System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaSyncW));
                int errorCode = delegateForFunctionPointer(dsHandle, partition, zero, option);
                if (errorCode != 0)
                {
                    if (!this.Partitions.Contains(partition))
                    {
                        throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition");
                    }
                    string targetName = null;
                    if (errorCode == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                    {
                        targetName = sourceServer;
                    }
                    else if (errorCode == ExceptionHelper.RPC_S_CALL_FAILED)
                    {
                        targetName = this.Name;
                    }
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, targetName);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
                if (adamInstance != null)
                {
                    adamInstance.Dispose();
                }
                if (domainController != null)
                {
                    domainController.Dispose();
                }
            }
        }
コード例 #4
0
        internal void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string sourceServer, int option, LoadLibrarySafeHandle libHandle)
        {
            Guid             ntdsaObjectGuid;
            int              num              = Marshal.SizeOf(typeof(Guid));
            AdamInstance     adamInstance     = null;
            DomainController domainController = null;
            IntPtr           intPtr           = Marshal.AllocHGlobal(num);

            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, this.context);
                    if (!isADAM)
                    {
                        domainController = DomainController.GetDomainController(newDirectoryContext);
                        ntdsaObjectGuid  = domainController.NtdsaObjectGuid;
                    }
                    else
                    {
                        adamInstance    = AdamInstance.GetAdamInstance(newDirectoryContext);
                        ntdsaObjectGuid = adamInstance.NtdsaObjectGuid;
                    }
                    Marshal.StructureToPtr(ntdsaObjectGuid, intPtr, false);
                }
                IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (procAddress != (IntPtr)0)
                {
                    UnsafeNativeMethods.DsReplicaSyncW delegateForFunctionPointer = (UnsafeNativeMethods.DsReplicaSyncW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(UnsafeNativeMethods.DsReplicaSyncW));
                    int num1 = delegateForFunctionPointer(dsHandle, partition, intPtr, option);
                    if (num1 != 0)
                    {
                        if (this.Partitions.Contains(partition))
                        {
                            string name = null;
                            if (num1 != ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                            {
                                if (num1 == ExceptionHelper.RPC_S_CALL_FAILED)
                                {
                                    name = this.Name;
                                }
                            }
                            else
                            {
                                name = sourceServer;
                            }
                            throw ExceptionHelper.GetExceptionFromErrorCode(num1, name);
                        }
                        else
                        {
                            throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition");
                        }
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (intPtr != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(intPtr);
                }
                if (adamInstance != null)
                {
                    adamInstance.Dispose();
                }
                if (domainController != null)
                {
                    domainController.Dispose();
                }
            }
        }
コード例 #5
0
ファイル: DirectoryServer.cs プロジェクト: mikem8361/runtime
        internal unsafe void SyncReplicaHelper(IntPtr dsHandle, bool isADAM, string partition, string?sourceServer, int option, SafeLibraryHandle libHandle)
        {
            int              structSize    = Marshal.SizeOf(typeof(Guid));
            IntPtr           unmanagedGuid = (IntPtr)0;
            Guid             guid          = Guid.Empty;
            AdamInstance?    adamServer    = null;
            DomainController?dcServer      = null;

            unmanagedGuid = Marshal.AllocHGlobal(structSize);
            try
            {
                if (sourceServer != null)
                {
                    DirectoryContext newContext = Utils.GetNewDirectoryContext(sourceServer, DirectoryContextType.DirectoryServer, context);
                    if (isADAM)
                    {
                        adamServer = AdamInstance.GetAdamInstance(newContext);
                        guid       = adamServer.NtdsaObjectGuid;
                    }
                    else
                    {
                        dcServer = DomainController.GetDomainController(newContext);
                        guid     = dcServer.NtdsaObjectGuid;
                    }

                    Marshal.StructureToPtr(guid, unmanagedGuid, false);
                }

                // call DsReplicaSyncW
                var dsReplicaSyncW = (delegate * unmanaged <IntPtr, char *, IntPtr, int, int>)global::Interop.Kernel32.GetProcAddress(libHandle, "DsReplicaSyncW");
                if (dsReplicaSyncW == null)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError());
                }

                fixed(char *partitionPtr = partition)
                {
                    int result = dsReplicaSyncW(dsHandle, partitionPtr, unmanagedGuid, (int)option);

                    // check the result
                    if (result != 0)
                    {
                        if (!Partitions.Contains(partition))
                        {
                            throw new ArgumentException(SR.ServerNotAReplica, nameof(partition));
                        }

                        string?serverDownName = null;
                        // this is the error returned when the server that we want to sync from is down
                        if (result == ExceptionHelper.RPC_S_SERVER_UNAVAILABLE)
                        {
                            serverDownName = sourceServer;
                        }
                        // this is the error returned when the server that we want to get synced is down
                        else if (result == ExceptionHelper.RPC_S_CALL_FAILED)
                        {
                            serverDownName = Name;
                        }

                        throw ExceptionHelper.GetExceptionFromErrorCode(result, serverDownName);
                    }
                }
            }
            finally
            {
                if (unmanagedGuid != (IntPtr)0)
                {
                    Marshal.FreeHGlobal(unmanagedGuid);
                }

                adamServer?.Dispose();
                dcServer?.Dispose();
            }
        }