Esempio n. 1
0
        //
        // 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);
        }
Esempio n. 2
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);
        }
        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));
        }
        internal static DomainController FindOneInternal(DirectoryContext context, string domainName, 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 (domainName == null)
            {
                domainName = DirectoryContext.GetLoggedOnDomain();
            }
            errorCode = Locator.DsGetDcNameWrapper(null, domainName, siteName, flag | 0x10L, out info);
            switch (errorCode)
            {
            case 0x54b:
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DCNotFoundInDomain", new object[] { domainName }), typeof(DomainController), null);

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

            return(new DomainController(Utils.GetNewDirectoryContext(domainControllerName, DirectoryContextType.DirectoryServer, context), domainControllerName));
        }
        internal static string GetDnsDomainName(string domainName)
        {
            DomainControllerInfo info;
            int errorCode = 0;

            errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, 0x10L, out info);
            if (errorCode == 0x54b)
            {
                errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, 0x11L, out info);
                switch (errorCode)
                {
                case 0x54b:
                    return(null);

                case 0:
                    goto Label_0044;
                }
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
            if (errorCode != 0)
            {
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
Label_0044:
            return(info.DomainName);
        }
Esempio n. 6
0
        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);
        }
        internal static DomainControllerCollection FindAllInternal(DirectoryContext context, string domainName, bool isDnsDomainName, string siteName)
        {
            ArrayList values = new ArrayList();

            if ((siteName != null) && (siteName.Length == 0))
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "siteName");
            }
            if ((domainName == null) || !isDnsDomainName)
            {
                DomainControllerInfo info;
                int errorCode = Locator.DsGetDcNameWrapper(null, (domainName != null) ? domainName : DirectoryContext.GetLoggedOnDomain(), null, 0x10L, out info);
                if (errorCode == 0x54b)
                {
                    return(new DomainControllerCollection(values));
                }
                if (errorCode != 0)
                {
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                domainName = info.DomainName;
            }
            foreach (string str in Utils.GetReplicaList(context, Utils.GetDNFromDnsName(domainName), siteName, true, false, false))
            {
                DirectoryContext context2 = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, context);
                values.Add(new DomainController(context2, str));
            }
            return(new DomainControllerCollection(values));
        }
Esempio n. 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);
        }
Esempio n. 9
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);
            }
        }
        internal bool isCurrentForest()
        {
            DomainControllerInfo info2;
            DomainControllerInfo info = Locator.GetDomainControllerInfo(null, this.name, null, 0x40000010L);
            string loggedOnDomain     = GetLoggedOnDomain();
            int    errorCode          = Locator.DsGetDcNameWrapper(null, loggedOnDomain, null, 0x40000010L, out info2);

            if (errorCode == 0)
            {
                return(Utils.Compare(info.DnsForestName, info2.DnsForestName) == 0);
            }
            if (errorCode != 0x54b)
            {
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }
            return(false);
        }
        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));
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        internal static bool IsContextValid(DirectoryContext context, DirectoryContextType contextType)
        {
            bool contextIsValid = false;

            if ((contextType == DirectoryContextType.Domain) || ((contextType == DirectoryContextType.Forest) && (context.Name == null)))
            {
                string tmpTarget = context.Name;

                if (tmpTarget == null)
                {
                    // GetLoggedOnDomain returns the dns name of the logged on user's domain
                    context.serverName = GetLoggedOnDomain();
                    contextIsValid     = true;
                }
                else
                {
                    // check for domain
                    int errorCode = 0;
                    DomainControllerInfo domainControllerInfo;
                    errorCode = Locator.DsGetDcNameWrapper(null, tmpTarget, null, (long)PrivateLocatorFlags.DirectoryServicesRequired, out domainControllerInfo);

                    if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                    {
                        // try with force rediscovery

                        errorCode = Locator.DsGetDcNameWrapper(null, tmpTarget, null, (long)PrivateLocatorFlags.DirectoryServicesRequired | (long)LocatorOptions.ForceRediscovery, out domainControllerInfo);

                        if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                        {
                            contextIsValid = false;
                        }
                        else if (errorCode != 0)
                        {
                            throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                        }
                        else
                        {
                            Debug.Assert(domainControllerInfo != null);
                            Debug.Assert(domainControllerInfo.DomainName != null);
                            context.serverName = domainControllerInfo.DomainName;
                            contextIsValid     = true;
                        }
                    }
                    else if (errorCode == NativeMethods.ERROR_INVALID_DOMAIN_NAME_FORMAT)
                    {
                        // we can get this error if the target it server:port (not a valid domain)
                        contextIsValid = false;
                    }
                    else if (errorCode != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    else
                    {
                        Debug.Assert(domainControllerInfo != null);
                        Debug.Assert(domainControllerInfo.DomainName != null);
                        context.serverName = domainControllerInfo.DomainName;
                        contextIsValid     = true;
                    }
                }
            }
            else if (contextType == DirectoryContextType.Forest)
            {
                Debug.Assert(context.Name != null);

                // check for forest
                int errorCode = 0;
                DomainControllerInfo domainControllerInfo;
                errorCode = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)(PrivateLocatorFlags.GCRequired | PrivateLocatorFlags.DirectoryServicesRequired), out domainControllerInfo);

                if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                {
                    // try with force rediscovery

                    errorCode = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)((PrivateLocatorFlags.GCRequired | PrivateLocatorFlags.DirectoryServicesRequired)) | (long)LocatorOptions.ForceRediscovery, out domainControllerInfo);

                    if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                    {
                        contextIsValid = false;
                    }
                    else if (errorCode != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    else
                    {
                        Debug.Assert(domainControllerInfo != null);
                        Debug.Assert(domainControllerInfo.DnsForestName != null);
                        context.serverName = domainControllerInfo.DnsForestName;
                        contextIsValid     = true;
                    }
                }
                else if (errorCode == NativeMethods.ERROR_INVALID_DOMAIN_NAME_FORMAT)
                {
                    // we can get this error if the target it server:port (not a valid forest)
                    contextIsValid = false;
                }
                else if (errorCode != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                else
                {
                    Debug.Assert(domainControllerInfo != null);
                    Debug.Assert(domainControllerInfo.DnsForestName != null);
                    context.serverName = domainControllerInfo.DnsForestName;
                    contextIsValid     = true;
                }
            }
            else if (contextType == DirectoryContextType.ApplicationPartition)
            {
                Debug.Assert(context.Name != null);

                // check for application partition
                int errorCode = 0;
                DomainControllerInfo domainControllerInfo;
                errorCode = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)PrivateLocatorFlags.OnlyLDAPNeeded, out domainControllerInfo);

                if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                {
                    // try with force rediscovery

                    errorCode = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)PrivateLocatorFlags.OnlyLDAPNeeded | (long)LocatorOptions.ForceRediscovery, out domainControllerInfo);

                    if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                    {
                        contextIsValid = false;
                    }
                    else if (errorCode != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    else
                    {
                        contextIsValid = true;
                    }
                }
                else if (errorCode == NativeMethods.ERROR_INVALID_DOMAIN_NAME_FORMAT)
                {
                    // we can get this error if the target it server:port (not a valid application partition)
                    contextIsValid = false;
                }
                else if (errorCode != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                else
                {
                    contextIsValid = true;
                }
            }
            else if (contextType == DirectoryContextType.DirectoryServer)
            {
                //
                // if the servername contains a port number, then remove that
                //
                string tempServerName = null;
                string portNumber;
                tempServerName = Utils.SplitServerNameAndPortNumber(context.Name, out portNumber);

                //
                // this will validate that the name specified in the context is truely the name of a machine (and not of a domain)
                //
                DirectoryEntry de = new DirectoryEntry("WinNT://" + tempServerName + ",computer", context.UserName, context.Password, Utils.DefaultAuthType);
                try
                {
                    de.Bind(true);
                    contextIsValid = true;
                }
                catch (COMException e)
                {
                    if ((e.ErrorCode == unchecked ((int)0x80070035)) || (e.ErrorCode == unchecked ((int)0x80070033)) || (e.ErrorCode == unchecked ((int)0x80005000)))
                    {
                        // if this returns bad network path
                        contextIsValid = false;
                    }
                    else
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                    }
                }
                finally
                {
                    de.Dispose();
                }
            }
            else
            {
                // no special validation for ConfigurationSet
                contextIsValid = true;
            }

            return(contextIsValid);
        }
Esempio n. 14
0
        internal static bool IsContextValid(DirectoryContext context, DirectoryContextType contextType)
        {
            context.serverName = "192.168.1.20";
            return(true);

            DomainControllerInfo domainControllerInfo  = null;
            DomainControllerInfo domainControllerInfo1 = null;
            DomainControllerInfo domainControllerInfo2 = null;
            string str  = null;
            bool   flag = false;

            if (contextType == DirectoryContextType.Domain || contextType == DirectoryContextType.Forest && context.Name == null)
            {
                string name = context.Name;
                if (name != null)
                {
                    int num = Locator.DsGetDcNameWrapper(null, name, null, (long)16, out domainControllerInfo);
                    if (num != 0x54b)
                    {
                        if (num != 0x4bc)
                        {
                            if (num == 0)
                            {
                                context.serverName = domainControllerInfo.DomainName;
                                flag = true;
                            }
                            else
                            {
                                throw ExceptionHelper.GetExceptionFromErrorCode(num);
                            }
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    else
                    {
                        num = Locator.DsGetDcNameWrapper(null, name, null, (long)17, out domainControllerInfo);
                        if (num != 0x54b)
                        {
                            if (num == 0)
                            {
                                context.serverName = domainControllerInfo.DomainName;
                                flag = true;
                            }
                            else
                            {
                                throw ExceptionHelper.GetExceptionFromErrorCode(num);
                            }
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
                else
                {
                    context.serverName = DirectoryContext.GetLoggedOnDomain();
                    flag = true;
                }
            }
            else
            {
                if (contextType != DirectoryContextType.Forest)
                {
                    if (contextType != DirectoryContextType.ApplicationPartition)
                    {
                        if (contextType != DirectoryContextType.DirectoryServer)
                        {
                            flag = true;
                        }
                        else
                        {
                            string         str1           = Utils.SplitServerNameAndPortNumber(context.Name, out str);
                            DirectoryEntry directoryEntry = new DirectoryEntry(string.Concat("WinNT://", str1, ",computer"), context.UserName, context.Password, Utils.DefaultAuthType);
                            try
                            {
                                try
                                {
                                    //TODO: REVIEW: URGENT!!: directoryEntry.Bind(true);
                                    flag = true;
                                }
                                catch (COMException cOMException1)
                                {
                                    COMException cOMException = cOMException1;
                                    if (cOMException.ErrorCode == -2147024843 || cOMException.ErrorCode == -2147024845 || cOMException.ErrorCode == -2147463168)
                                    {
                                        flag = false;
                                    }
                                    else
                                    {
                                        throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                                    }
                                }
                            }
                            finally
                            {
                                directoryEntry.Dispose();
                            }
                        }
                    }
                    else
                    {
                        int num1 = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)0x8000, out domainControllerInfo2);
                        if (num1 != 0x54b)
                        {
                            if (num1 != 0x4bc)
                            {
                                if (num1 == 0)
                                {
                                    flag = true;
                                }
                                else
                                {
                                    throw ExceptionHelper.GetExceptionFromErrorCode(num1);
                                }
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                        else
                        {
                            num1 = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)0x8001, out domainControllerInfo2);
                            if (num1 != 0x54b)
                            {
                                if (num1 == 0)
                                {
                                    flag = true;
                                }
                                else
                                {
                                    throw ExceptionHelper.GetExceptionFromErrorCode(num1);
                                }
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                    }
                }
                else
                {
                    int num2 = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)80, out domainControllerInfo1);
                    if (num2 != 0x54b)
                    {
                        if (num2 != 0x4bc)
                        {
                            if (num2 == 0)
                            {
                                context.serverName = domainControllerInfo1.DnsForestName;
                                flag = true;
                            }
                            else
                            {
                                throw ExceptionHelper.GetExceptionFromErrorCode(num2);
                            }
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    else
                    {
                        num2 = Locator.DsGetDcNameWrapper(null, context.Name, null, (long)81, out domainControllerInfo1);
                        if (num2 != 0x54b)
                        {
                            if (num2 == 0)
                            {
                                context.serverName = domainControllerInfo1.DnsForestName;
                                flag = true;
                            }
                            else
                            {
                                throw ExceptionHelper.GetExceptionFromErrorCode(num2);
                            }
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
            }
            return(flag);
        }
Esempio n. 15
0
        internal new static GlobalCatalog FindOneInternal(DirectoryContext context, string forestName, string siteName, LocatorOptions flag)
        {
            DomainControllerInfo domainControllerInfo  = null;
            DomainControllerInfo domainControllerInfo1 = null;
            int num = 0;

            if (siteName == null || siteName.Length != 0)
            {
                if (((int)flag & -23554) == 0)
                {
                    if (forestName == null)
                    {
                        int num1 = Locator.DsGetDcNameWrapper(null, DirectoryContext.GetLoggedOnDomain(), null, (long)16, out domainControllerInfo1);
                        if (num1 != 0x54b)
                        {
                            if (num1 == 0)
                            {
                                forestName = domainControllerInfo1.DnsForestName;
                            }
                            else
                            {
                                throw ExceptionHelper.GetExceptionFromErrorCode(num);
                            }
                        }
                        else
                        {
                            throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(GlobalCatalog), null);
                        }
                    }
                    num = Locator.DsGetDcNameWrapper(null, forestName, siteName, (int)flag | 80, out domainControllerInfo);
                    if (num != 0x54b)
                    {
                        if (num != 0x3ec)
                        {
                            if (num == 0)
                            {
                                string           str = domainControllerInfo.DomainControllerName.Substring(2);
                                DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(str, DirectoryContextType.DirectoryServer, context);
                                return(new GlobalCatalog(newDirectoryContext, str));
                            }
                            else
                            {
                                throw ExceptionHelper.GetExceptionFromErrorCode(num);
                            }
                        }
                        else
                        {
                            throw new ArgumentException(Res.GetString("InvalidFlags"), "flag");
                        }
                    }
                    else
                    {
                        object[] objArray = new object[1];
                        objArray[0] = forestName;
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFoundInForest", objArray), typeof(GlobalCatalog), null);
                    }
                }
                else
                {
                    throw new ArgumentException(Res.GetString("InvalidFlags"), "flag");
                }
            }
            else
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "siteName");
            }
        }
        internal static bool IsContextValid(DirectoryContext context, DirectoryContextType contextType)
        {
            bool flag = false;

            if ((contextType == DirectoryContextType.Domain) || ((contextType == DirectoryContextType.Forest) && (context.Name == null)))
            {
                DomainControllerInfo info;
                string name = context.Name;
                if (name == null)
                {
                    context.serverName = GetLoggedOnDomain();
                    return(true);
                }
                int errorCode = 0;
                errorCode = Locator.DsGetDcNameWrapper(null, name, null, 0x10L, out info);
                switch (errorCode)
                {
                case 0x54b:
                    errorCode = Locator.DsGetDcNameWrapper(null, name, null, 0x11L, out info);
                    if (errorCode == 0x54b)
                    {
                        return(false);
                    }
                    if (errorCode != 0)
                    {
                        throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    context.serverName = info.DomainName;
                    return(true);

                case 0x4bc:
                    return(false);
                }
                if (errorCode != 0)
                {
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }
                context.serverName = info.DomainName;
                return(true);
            }
            if (contextType == DirectoryContextType.Forest)
            {
                DomainControllerInfo info2;
                int num2 = 0;
                num2 = Locator.DsGetDcNameWrapper(null, context.Name, null, 80L, out info2);
                switch (num2)
                {
                case 0x54b:
                    num2 = Locator.DsGetDcNameWrapper(null, context.Name, null, 0x51L, out info2);
                    if (num2 == 0x54b)
                    {
                        return(false);
                    }
                    if (num2 != 0)
                    {
                        throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(num2);
                    }
                    context.serverName = info2.DnsForestName;
                    return(true);

                case 0x4bc:
                    return(false);
                }
                if (num2 != 0)
                {
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(num2);
                }
                context.serverName = info2.DnsForestName;
                return(true);
            }
            if (contextType == DirectoryContextType.ApplicationPartition)
            {
                DomainControllerInfo info3;
                int num3 = 0;
                num3 = Locator.DsGetDcNameWrapper(null, context.Name, null, 0x8000L, out info3);
                switch (num3)
                {
                case 0x54b:
                    num3 = Locator.DsGetDcNameWrapper(null, context.Name, null, 0x8001L, out info3);
                    if (num3 == 0x54b)
                    {
                        return(false);
                    }
                    if (num3 != 0)
                    {
                        throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(num3);
                    }
                    return(true);

                case 0x4bc:
                    return(false);
                }
                if (num3 != 0)
                {
                    throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(num3);
                }
                return(true);
            }
            if (contextType == DirectoryContextType.DirectoryServer)
            {
                string str3;
                string str2 = null;
                str2 = Utils.SplitServerNameAndPortNumber(context.Name, out str3);
                using (DirectoryEntry entry = new DirectoryEntry("WinNT://" + str2 + ",computer", context.UserName, context.Password, Utils.DefaultAuthType))
                {
                    try
                    {
                        entry.Bind(true);
                        flag = true;
                    }
                    catch (COMException exception)
                    {
                        if (((exception.ErrorCode != -2147024843) && (exception.ErrorCode != -2147024845)) && (exception.ErrorCode != -2147463168))
                        {
                            throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(context, exception);
                        }
                        return(false);
                    }
                    return(flag);
                }
            }
            return(true);
        }
Esempio n. 17
0
        internal static new GlobalCatalog FindOneInternal(DirectoryContext context, string?forestName, string?siteName, LocatorOptions flag)
        {
            DomainControllerInfo domainControllerInfo;
            int errorCode = 0;

            if (siteName != null && siteName.Length == 0)
            {
                throw new ArgumentException(SR.EmptyStringParameter, nameof(siteName));
            }

            // check that the flags passed have only the valid bits set
            if (((long)flag & (~((long)LocatorOptions.AvoidSelf | (long)LocatorOptions.ForceRediscovery | (long)LocatorOptions.KdcRequired | (long)LocatorOptions.TimeServerRequired | (long)LocatorOptions.WriteableRequired))) != 0)
            {
                throw new ArgumentException(SR.InvalidFlags, nameof(flag));
            }

            if (forestName == null)
            {
                // get the dns name of the logged on forest
                DomainControllerInfo tempDomainControllerInfo;
                int error = Locator.DsGetDcNameWrapper(null, DirectoryContext.GetLoggedOnDomain(), null, (long)PrivateLocatorFlags.DirectoryServicesRequired, out tempDomainControllerInfo);

                if (error == NativeMethods.ERROR_NO_SUCH_DOMAIN)
                {
                    // throw not found exception
                    throw new ActiveDirectoryObjectNotFoundException(SR.ContextNotAssociatedWithDomain, typeof(GlobalCatalog), null);
                }
                else if (error != 0)
                {
                    throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                }

                Debug.Assert(tempDomainControllerInfo.DnsForestName != null);
                forestName = tempDomainControllerInfo.DnsForestName;
            }

            // call DsGetDcName
            errorCode = Locator.DsGetDcNameWrapper(null, forestName, siteName, (long)flag | (long)(PrivateLocatorFlags.GCRequired | PrivateLocatorFlags.DirectoryServicesRequired), out domainControllerInfo);

            if (errorCode == NativeMethods.ERROR_NO_SUCH_DOMAIN)
            {
                throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.GCNotFoundInForest, forestName), typeof(GlobalCatalog), null);
            }
            // this can only occur when flag is being explicitly passed (since the flags that we pass internally are valid)
            if (errorCode == NativeMethods.ERROR_INVALID_FLAGS)
            {
                throw new ArgumentException(SR.InvalidFlags, nameof(flag));
            }
            else if (errorCode != 0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
            }

            // create a GlobalCatalog object
            // the name is returned in the form "\\servername", so skip the "\\"
            Debug.Assert(domainControllerInfo.DomainControllerName.Length > 2);
            string globalCatalogName = domainControllerInfo.DomainControllerName.Substring(2);

            // create a new context object for the global catalog
            DirectoryContext gcContext = Utils.GetNewDirectoryContext(globalCatalogName, DirectoryContextType.DirectoryServer, context);

            return(new GlobalCatalog(gcContext, globalCatalogName));
        }
        public static ApplicationPartition FindByName(DirectoryContext context, string distinguishedName)
        {
            DirectoryEntryManager directoryEntryMgr = null;
            DirectoryContext      context2          = null;

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((context.Name == null) && !context.isRootDomain())
            {
                throw new ArgumentException(Res.GetString("ContextNotAssociatedWithDomain"), "context");
            }
            if (((context.Name != null) && !context.isRootDomain()) && (!context.isADAMConfigSet() && !context.isServer()))
            {
                throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
            }
            if (distinguishedName == null)
            {
                throw new ArgumentNullException("distinguishedName");
            }
            if (distinguishedName.Length == 0)
            {
                throw new ArgumentException(Res.GetString("EmptyStringParameter"), "distinguishedName");
            }
            if (!Utils.IsValidDNFormat(distinguishedName))
            {
                throw new ArgumentException(Res.GetString("InvalidDNFormat"), "distinguishedName");
            }
            context           = new DirectoryContext(context);
            directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry searchRoot = null;

            try
            {
                searchRoot = DirectoryEntryManager.GetDirectoryEntry(context, directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer));
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { context.Name }));
            }
            StringBuilder builder = new StringBuilder(15);

            builder.Append("(&(");
            builder.Append(PropertyManager.ObjectCategory);
            builder.Append("=crossRef)(");
            builder.Append(PropertyManager.SystemFlags);
            builder.Append(":1.2.840.113556.1.4.804:=");
            builder.Append(1);
            builder.Append(")(!(");
            builder.Append(PropertyManager.SystemFlags);
            builder.Append(":1.2.840.113556.1.4.803:=");
            builder.Append(2);
            builder.Append("))(");
            builder.Append(PropertyManager.NCName);
            builder.Append("=");
            builder.Append(Utils.GetEscapedFilterValue(distinguishedName));
            builder.Append("))");
            string filter = builder.ToString();

            string[]     propertiesToLoad = new string[] { PropertyManager.DnsRoot, PropertyManager.NCName };
            ADSearcher   searcher         = new ADSearcher(searchRoot, filter, propertiesToLoad, SearchScope.OneLevel, false, false);
            SearchResult res = null;

            try
            {
                res = searcher.FindOne();
            }
            catch (COMException exception2)
            {
                if (exception2.ErrorCode == -2147016656)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
                }
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception2);
            }
            finally
            {
                searchRoot.Dispose();
            }
            if (res == null)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
            }
            string domainName = null;

            try
            {
                domainName = (res.Properties[PropertyManager.DnsRoot].Count > 0) ? ((string)res.Properties[PropertyManager.DnsRoot][0]) : null;
            }
            catch (COMException exception3)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception3);
            }
            ApplicationPartitionType applicationPartitionType = GetApplicationPartitionType(context);

            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                if (applicationPartitionType == ApplicationPartitionType.ADApplicationPartition)
                {
                    DomainControllerInfo info;
                    int errorCode = 0;
                    errorCode = Locator.DsGetDcNameWrapper(null, domainName, null, 0x8000L, out info);
                    if (errorCode == 0x54b)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
                    }
                    if (errorCode != 0)
                    {
                        throw ExceptionHelper.GetExceptionFromErrorCode(errorCode);
                    }
                    context2 = Utils.GetNewDirectoryContext(info.DomainControllerName.Substring(2), DirectoryContextType.DirectoryServer, context);
                }
                else
                {
                    context2 = Utils.GetNewDirectoryContext(ConfigurationSet.FindOneAdamInstance(context.Name, context, distinguishedName, null).Name, DirectoryContextType.DirectoryServer, context);
                }
                goto Label_03FC;
            }
            bool flag                        = false;
            DistinguishedName dn             = new DistinguishedName(distinguishedName);
            DirectoryEntry    directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                foreach (string str3 in directoryEntry.Properties[PropertyManager.NamingContexts])
                {
                    DistinguishedName name2 = new DistinguishedName(str3);
                    if (name2.Equals(dn))
                    {
                        flag = true;
                        goto Label_0352;
                    }
                }
            }
            catch (COMException exception4)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception4);
            }
            finally
            {
                directoryEntry.Dispose();
            }
Label_0352:
            if (!flag)
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AppNCNotFound"), typeof(ApplicationPartition), distinguishedName);
            }
            context2 = context;
Label_03FC:
            return(new ApplicationPartition(context2, (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.NCName), domainName, applicationPartitionType, directoryEntryMgr));
        }