示例#1
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));
        }
示例#2
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);
        }