示例#1
0
        public static void RemoveDsServer(string serverDN, string serverToUseFqdn, bool fCommit)
        {
            if (fCommit)
            {
                ExTraceGlobals.ADTopologyTracer.TraceDebug <string, string>(0L, "Removing server {0} via server {1}", serverDN, serverToUseFqdn);
            }
            else
            {
                ExTraceGlobals.ADTopologyTracer.TraceDebug <string, string>(0L, "Checking for existence of server {0} via server {1}", serverDN, serverToUseFqdn);
            }
            SafeDsBindHandle safeDsBindHandle = null;

            try
            {
                uint num = NativeMethods.DsBind(serverToUseFqdn, null, out safeDsBindHandle);
                if (num != 0U)
                {
                    throw new ADExternalException(DirectoryStrings.ExceptionCannotBindToDC(serverToUseFqdn), new Win32Exception((int)num));
                }
                bool flag;
                num = NativeMethods.DsRemoveDsServer(safeDsBindHandle, serverDN, null, out flag, fCommit);
                if (num != 0U)
                {
                    throw new ADExternalException(DirectoryStrings.ExceptionCannotRemoveDsServer(serverDN), new Win32Exception((int)num));
                }
            }
            finally
            {
                if (safeDsBindHandle != null)
                {
                    safeDsBindHandle.Dispose();
                }
            }
        }
 internal static extern uint DsBindWithSpnEx([MarshalAs(UnmanagedType.LPWStr)] string DomainControllerName, [MarshalAs(UnmanagedType.LPWStr)] string DnsDomainName, IntPtr AuthIdentity, [MarshalAs(UnmanagedType.LPWStr)] string ServicePrincipalName, uint BindFlags, out SafeDsBindHandle phDS);
 internal static extern uint DsAddSidHistory(SafeDsBindHandle phDS, uint flags, [MarshalAs(UnmanagedType.LPWStr)] string srcDomain, [MarshalAs(UnmanagedType.LPWStr)] string srcPrincipal, [MarshalAs(UnmanagedType.LPWStr)] string srcDomainController, IntPtr srcDomainCreds, [MarshalAs(UnmanagedType.LPWStr)] string dstDomain, [MarshalAs(UnmanagedType.LPWStr)] string dstPrincipal);
 internal static extern uint DsRemoveDsServer(SafeDsBindHandle phDS, [MarshalAs(UnmanagedType.LPWStr)] string ServerDN, [MarshalAs(UnmanagedType.LPWStr)] string DomainDN, out bool pfLastDCInDomain, bool fCommit);
 internal static extern uint DsCrackNames(SafeDsBindHandle phDS, NativeMethods.DsNameFlags flags, ExtendedNameFormat formatOffered, ExtendedNameFormat formatDesired, uint cNames, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 4)] string[] rpNames, out SafeDsNameResultHandle ppResult);
 internal static extern uint DsBind([MarshalAs(UnmanagedType.LPWStr)] string DomainControllerName, [MarshalAs(UnmanagedType.LPWStr)] string DnsDomainName, out SafeDsBindHandle phDS);
示例#7
0
        private static bool TryDsCrackNames(string input, ExtendedNameFormat formatOffered, ExtendedNameFormat formatDesired, ADServerSettings serverSettings, out string result, out uint err)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (formatOffered != ExtendedNameFormat.NameCanonical && formatOffered != ExtendedNameFormat.NameFullyQualifiedDN)
            {
                throw new ArgumentException(DirectoryStrings.ExArgumentException("formatOffered", formatOffered.ToString()), "formatOffered");
            }
            if (formatDesired != ExtendedNameFormat.NameCanonical && formatDesired != ExtendedNameFormat.NameFullyQualifiedDN)
            {
                throw new ArgumentException(DirectoryStrings.ExArgumentException("formatDesired", formatDesired.ToString()), "formatDesired");
            }
            result = null;
            err    = 0U;
            SafeDsNameResultHandle safeDsNameResultHandle = null;
            SafeDsBindHandle       safeDsBindHandle       = null;

            NativeMethods.DsNameFlags dsNameFlags = NativeMethods.DsNameFlags.SyntacticalOnly;
            bool result2;

            try
            {
                if (formatOffered == ExtendedNameFormat.NameCanonical)
                {
                    string[] array = input.Split(new char[]
                    {
                        '/'
                    });
                    if (array.Length == 0)
                    {
                        return(false);
                    }
                    if (array.Length > 1 && array[1].Length > 0)
                    {
                        dsNameFlags = NativeMethods.DsNameFlags.NoFlags;
                        string text = array[0];
                        ExTraceGlobals.ADTopologyTracer.TraceDebug <string>(0L, "Calling DsBind for domain {0}", text ?? "<null>");
                        if (text.Length == 0)
                        {
                            return(false);
                        }
                        string domainControllerName = null;
                        string distinguishedName    = NativeHelpers.DistinguishedNameFromCanonicalName(text);
                        if (serverSettings == null)
                        {
                            serverSettings = ADSessionSettings.ExternalServerSettings;
                        }
                        if (serverSettings != null)
                        {
                            domainControllerName = serverSettings.GetPreferredDC(new ADObjectId(distinguishedName));
                        }
                        err = NativeMethods.DsBind(domainControllerName, text, out safeDsBindHandle);
                        if (err != 0U)
                        {
                            return(false);
                        }
                    }
                }
                if (safeDsBindHandle == null)
                {
                    safeDsBindHandle = new SafeDsBindHandle();
                }
                ExTraceGlobals.ADTopologyTracer.TraceDebug(0L, "Calling DsCrackNames with input={0}, formatOffered={1}, formatDesired={2}, flags={3}", new object[]
                {
                    input,
                    formatOffered,
                    formatDesired,
                    dsNameFlags
                });
                err = NativeMethods.DsCrackNames(safeDsBindHandle, dsNameFlags, formatOffered, formatDesired, 1U, new string[]
                {
                    input
                }, out safeDsNameResultHandle);
                if (err != 0U)
                {
                    result2 = false;
                }
                else
                {
                    NativeMethods.DsNameResult dsNameResult = new NativeMethods.DsNameResult();
                    Marshal.PtrToStructure(safeDsNameResultHandle.DangerousGetHandle(), dsNameResult);
                    uint cItems = dsNameResult.cItems;
                    if (cItems < 1U)
                    {
                        result2 = false;
                    }
                    else
                    {
                        NativeMethods.DsNameResultItem dsNameResultItem = new NativeMethods.DsNameResultItem();
                        Marshal.PtrToStructure(dsNameResult.rItems, dsNameResultItem);
                        if (dsNameResultItem.status != 0)
                        {
                            err     = (uint)dsNameResultItem.status;
                            result2 = false;
                        }
                        else if (dsNameResultItem.name == null)
                        {
                            result2 = false;
                        }
                        else
                        {
                            result  = dsNameResultItem.name;
                            result2 = true;
                        }
                    }
                }
            }
            finally
            {
                if (safeDsBindHandle != null)
                {
                    safeDsBindHandle.Dispose();
                }
                if (safeDsNameResultHandle != null)
                {
                    safeDsNameResultHandle.Dispose();
                }
            }
            return(result2);
        }