public ActiveDirectorySiteLink(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport, ActiveDirectorySchedule?schedule)
        {
            ValidateArgument(context, siteLinkName, transport);

            //  work with copy of the context
            context = new DirectoryContext(context);

            this.context = context;
            _name        = siteLinkName;
            _transport   = transport;

            // bind to the rootdse to get the configurationnamingcontext
            DirectoryEntry de;

            try
            {
                de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string config = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext) !;

                string parentDN;
                if (transport == ActiveDirectoryTransportType.Rpc)
                {
                    parentDN = "CN=IP,CN=Inter-Site Transports,CN=Sites," + config;
                }
                else
                {
                    parentDN = "CN=SMTP,CN=Inter-Site Transports,CN=Sites," + config;
                }

                de = DirectoryEntryManager.GetDirectoryEntry(context, parentDN);
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                // this is the case where the context is a config set and we could not find an ADAM instance in that config set
                throw new ActiveDirectoryOperationException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, context.Name));
            }

            try
            {
                string rdn = "cn=" + _name;
                rdn         = Utils.GetEscapedPath(rdn);
                cachedEntry = de.Children.Add(rdn, "siteLink");
                cachedEntry.Properties["cost"].Value         = appDefaultCost;
                cachedEntry.Properties["replInterval"].Value = appDefaultInterval;
                if (schedule != null)
                {
                    cachedEntry.Properties[nameof(schedule)].Value = schedule.GetUnmanagedSchedule();
                }
            }
            catch (COMException e)
            {
                if (e.ErrorCode == unchecked ((int)0x80072030))
                {
                    // if it is ADAM and transport type is SMTP, throw NotSupportedException.
                    DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                    if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                    {
                        throw new NotSupportedException(SR.NotSupportTransportSMTP);
                    }
                }

                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                de.Dispose();
            }
        }
Example #2
0
        internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
        {
            if (context.ContextType != DirectoryContextType.ConfigurationSet)
            {
                // assuming it's an ADAM Instance
                // check that it is an ADAM server only (not AD)
                DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry        rootDSE           = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);

                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    directoryEntryMgr.RemoveIfExists(directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.RootDSE));
                    throw new ArgumentException(SR.TargetShouldBeServerORConfigSet, nameof(context));
                }

                string dnsHostName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);

                return(new AdamInstance(context, dnsHostName, directoryEntryMgr));
            }

            // Now this is the case where context is a Config Set
            // Here we need to search for the service connection points in the forest
            // (if the forest object was created by specifying the server, we stick to that, else search in a GC)
            DirectoryEntry rootEntry         = GetSearchRootEntry(Forest.GetCurrentForest());
            ArrayList      adamInstanceNames = new ArrayList();

            try
            {
                string entryName = (string)rootEntry.Properties["distinguishedName"].Value;
                // Search for computer "serviceConnectionObjects" where the keywords attribute
                // contains the specified keyword
                // set up the searcher object

                // build the filter
                StringBuilder str = new StringBuilder(15);
                str.Append("(&(");
                str.Append(PropertyManager.ObjectCategory);
                str.Append("=serviceConnectionPoint)");
                str.Append('(');
                str.Append(PropertyManager.Keywords);
                str.Append("=1.2.840.113556.1.4.1851)(");
                str.Append(PropertyManager.Keywords);
                str.Append('=');
                str.Append(Utils.GetEscapedFilterValue(context.Name)); // target = config set name
                str.Append("))");

                string   filter           = str.ToString();
                string[] propertiesToLoad = new string[1];

                propertiesToLoad[0] = PropertyManager.ServiceBindingInformation;

                ADSearcher             searcher = new ADSearcher(rootEntry, filter, propertiesToLoad, SearchScope.Subtree, false /*not paged search*/, false /*no cached results*/);
                SearchResultCollection resCol   = searcher.FindAll();

                try
                {
                    foreach (SearchResult res in resCol)
                    {
                        // the binding info contains two values
                        // "ldap://hostname:ldapport"
                        // and "ldaps://hostname:sslport"
                        // we need the "hostname:ldapport" value
                        string prefix = "ldap://";

                        foreach (string bindingInfo in res.Properties[PropertyManager.ServiceBindingInformation])
                        {
                            if ((bindingInfo.Length > prefix.Length) && (string.Equals(bindingInfo.Substring(0, prefix.Length), prefix, StringComparison.OrdinalIgnoreCase)))
                            {
                                adamInstanceNames.Add(bindingInfo.Substring(prefix.Length));
                            }
                        }
                    }
                }
                finally
                {
                    resCol.Dispose();
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                rootEntry.Dispose();
            }

            //
            // we have all the adam instance names in teh form of server:port from the scp
            // now we need to find one that is alive
            //
            return(FindAliveAdamInstance(null, context, adamInstanceNames));
        }
Example #3
0
        private AdamInstance GetRoleOwner(AdamRole role)
        {
            DirectoryEntry cachedDirectoryEntry     = null;
            string         adamDnsHostNameFromNTDSA = null;

            using (cachedDirectoryEntry)
            {
                try
                {
                    AdamRole adamRole = role;
                    switch (adamRole)
                    {
                    case AdamRole.SchemaRole:
                    {
                        cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.SchemaNamingContext);
                        break;
                    }

                    case AdamRole.NamingRole:
                    {
                        cachedDirectoryEntry = this.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.PartitionsContainer);
                        break;
                    }
                    }
                    cachedDirectoryEntry.RefreshCache();
                    adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(this.context, (string)PropertyManager.GetPropertyValue(this.context, cachedDirectoryEntry, PropertyManager.FsmoRoleOwner));
                }
                catch (COMException cOMException1)
                {
                    COMException cOMException = cOMException1;
                    throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException);
                }
            }
            DirectoryContext newDirectoryContext = Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, this.context);

            return(new AdamInstance(newDirectoryContext, adamDnsHostNameFromNTDSA));
        }
        public ReplicationConnection(DirectoryContext context, string name, DirectoryServer sourceServer, ActiveDirectorySchedule schedule, ActiveDirectoryTransportType transport)
        {
            ValidateArgument(context, name);
            if (sourceServer == null)
            {
                throw new ArgumentNullException("sourceServer");
            }
            if ((transport < ActiveDirectoryTransportType.Rpc) || (transport > ActiveDirectoryTransportType.Smtp))
            {
                throw new InvalidEnumArgumentException("value", (int)transport, typeof(ActiveDirectoryTransportType));
            }
            context = new DirectoryContext(context);
            this.ValidateTargetAndSourceServer(context, sourceServer);
            this.context        = context;
            this.connectionName = name;
            this.transport      = transport;
            DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                string str = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ServerName);
                string dn  = "CN=NTDS Settings," + str;
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dn);
                string escapedPath = Utils.GetEscapedPath("cn=" + this.connectionName);
                this.cachedDirectoryEntry = directoryEntry.Children.Add(escapedPath, "nTDSConnection");
                DirectoryContext context2 = sourceServer.Context;
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context2, WellKnownDN.RootDSE);
                string str4 = (string)PropertyManager.GetPropertyValue(context2, directoryEntry, PropertyManager.ServerName);
                str4 = "CN=NTDS Settings," + str4;
                this.cachedDirectoryEntry.Properties["fromServer"].Add(str4);
                if (schedule != null)
                {
                    this.cachedDirectoryEntry.Properties["schedule"].Value = schedule.GetUnmanagedSchedule();
                }
                string dNFromTransportType = Utils.GetDNFromTransportType(this.TransportType, context);
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, dNFromTransportType);
                try
                {
                    directoryEntry.Bind(true);
                }
                catch (COMException exception)
                {
                    if (((exception.ErrorCode == -2147016656) && Utils.CheckCapability(DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE), Capability.ActiveDirectoryApplicationMode)) && (transport == ActiveDirectoryTransportType.Smtp))
                    {
                        throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                    }
                    throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                this.cachedDirectoryEntry.Properties["transportType"].Add(dNFromTransportType);
                this.cachedDirectoryEntry.Properties["enabledConnection"].Value = false;
                this.cachedDirectoryEntry.Properties["options"].Value           = 0;
            }
            catch (COMException exception2)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, exception2);
            }
            finally
            {
                directoryEntry.Close();
            }
        }
Example #5
0
        public void MoveToAnotherSite(string siteName)
        {
            CheckIfDisposed();

            // validate siteName
            if (siteName == null)
            {
                throw new ArgumentNullException(nameof(siteName));
            }

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

            // the dc is really being moved to a different site
            if (Utils.Compare(SiteName, siteName) != 0)
            {
                DirectoryEntry?newParentEntry = null;
                try
                {
                    // Bind to the target site's server container
                    // Get the distinguished name for the site
                    string parentDN = "CN=Servers,CN=" + siteName + "," + directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SitesContainer);
                    newParentEntry = DirectoryEntryManager.GetDirectoryEntry(context, parentDN);

                    string serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;

                    DirectoryEntry serverEntry = directoryEntryMgr.GetCachedDirectoryEntry(serverName);

                    // force binding (needed otherwise S.DS throw an exception while releasing the COM interface pointer)
                    _ = (string?)PropertyManager.GetPropertyValue(context, serverEntry, PropertyManager.DistinguishedName);

                    // move the object to the servers container of the target site
                    serverEntry.MoveTo(newParentEntry);
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
                finally
                {
                    if (newParentEntry != null)
                    {
                        newParentEntry.Dispose();
                    }
                }

                // remove stale cached directory entries
                // invalidate the cached properties that get affected by this
                siteInfoModified = true;
                cachedSiteName   = null;

                if (cachedSiteObjectName != null)
                {
                    directoryEntryMgr.RemoveIfExists(cachedSiteObjectName);
                    cachedSiteObjectName = null;
                }

                if (cachedServerObjectName != null)
                {
                    directoryEntryMgr.RemoveIfExists(cachedServerObjectName);
                    cachedServerObjectName = null;
                }

                if (cachedNtdsaObjectName != null)
                {
                    directoryEntryMgr.RemoveIfExists(cachedNtdsaObjectName);
                    cachedNtdsaObjectName = null;
                }
            }
        }
Example #6
0
 internal static AdamInstance FindAnyAdamInstance(DirectoryContext context)
 {
     if (context.ContextType == DirectoryContextType.ConfigurationSet)
     {
         DirectoryEntry searchRootEntry = ConfigurationSet.GetSearchRootEntry(Forest.GetCurrentForest());
         ArrayList      arrayLists      = new ArrayList();
         try
         {
             try
             {
                 StringBuilder stringBuilder = new StringBuilder(15);
                 stringBuilder.Append("(&(");
                 stringBuilder.Append(PropertyManager.ObjectCategory);
                 stringBuilder.Append("=serviceConnectionPoint)");
                 stringBuilder.Append("(");
                 stringBuilder.Append(PropertyManager.Keywords);
                 stringBuilder.Append("=1.2.840.113556.1.4.1851)(");
                 stringBuilder.Append(PropertyManager.Keywords);
                 stringBuilder.Append("=");
                 stringBuilder.Append(Utils.GetEscapedFilterValue(context.Name));
                 stringBuilder.Append("))");
                 string   str = stringBuilder.ToString();
                 string[] serviceBindingInformation = new string[1];
                 serviceBindingInformation[0] = PropertyManager.ServiceBindingInformation;
                 ADSearcher             aDSearcher = new ADSearcher(searchRootEntry, str, serviceBindingInformation, SearchScope.Subtree, false, false);
                 SearchResultCollection searchResultCollections = aDSearcher.FindAll();
                 try
                 {
                     foreach (SearchResult item in searchResultCollections)
                     {
                         string      str1       = "ldap://";
                         IEnumerator enumerator = item.Properties[PropertyManager.ServiceBindingInformation].GetEnumerator();
                         try
                         {
                             while (enumerator.MoveNext())
                             {
                                 string str2 = item.ToString();
                                 if (str2.Length <= str1.Length || string.Compare(str2.Substring(0, str1.Length), str1, StringComparison.OrdinalIgnoreCase) != 0)
                                 {
                                     continue;
                                 }
                                 arrayLists.Add(str2.Substring(str1.Length));
                             }
                         }
                         finally
                         {
                             IDisposable disposable = enumerator as IDisposable;
                             if (disposable != null)
                             {
                                 disposable.Dispose();
                             }
                         }
                     }
                 }
                 finally
                 {
                     searchResultCollections.Dispose();
                 }
             }
             catch (COMException cOMException1)
             {
                 COMException cOMException = cOMException1;
                 throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
             }
         }
         finally
         {
             searchRootEntry.Dispose();
         }
         return(ConfigurationSet.FindAliveAdamInstance(null, context, arrayLists));
     }
     else
     {
         DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
         DirectoryEntry        cachedDirectoryEntry  = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
         if (Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
         {
             string propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.DnsHostName);
             return(new AdamInstance(context, propertyValue, directoryEntryManager));
         }
         else
         {
             directoryEntryManager.RemoveIfExists(directoryEntryManager.ExpandWellKnownDN(WellKnownDN.RootDSE));
             throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
         }
     }
 }
Example #7
0
        public static ActiveDirectoryInterSiteTransport FindByTransportType(DirectoryContext context, ActiveDirectoryTransportType transport)
        {
            DirectoryEntry directoryEntry;

            if (context != null)
            {
                if (context.Name != null || context.isRootDomain())
                {
                    if (context.Name == null || context.isRootDomain() || context.isServer() || context.isADAMConfigSet())
                    {
                        if (transport < ActiveDirectoryTransportType.Rpc || transport > ActiveDirectoryTransportType.Smtp)
                        {
                            throw new InvalidEnumArgumentException("value", (int)transport, typeof(ActiveDirectoryTransportType));
                        }
                        else
                        {
                            context = new DirectoryContext(context);
                            try
                            {
                                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                                string str           = string.Concat("CN=Inter-Site Transports,CN=Sites,", propertyValue);
                                if (transport != ActiveDirectoryTransportType.Rpc)
                                {
                                    str = string.Concat("CN=SMTP,", str);
                                }
                                else
                                {
                                    str = string.Concat("CN=IP,", str);
                                }
                                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
                            }
                            catch (COMException cOMException1)
                            {
                                COMException cOMException = cOMException1;
                                throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                            }
                            catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
                            {
                                object[] name = new object[1];
                                name[0] = context.Name;
                                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", name));
                            }
                            try
                            {
                                string[] strArrays = new string[1];
                                strArrays[0] = "options";
                                directoryEntry.RefreshCache(strArrays);
                            }
                            catch (COMException cOMException3)
                            {
                                COMException cOMException2 = cOMException3;
                                if (cOMException2.ErrorCode != -2147016656)
                                {
                                    throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                                }
                                else
                                {
                                    DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                                    if (!Utils.CheckCapability(directoryEntry1, Capability.ActiveDirectoryApplicationMode) || transport != ActiveDirectoryTransportType.Smtp)
                                    {
                                        object[] objArray = new object[1];
                                        objArray[0] = transport.ToString();
                                        throw new ActiveDirectoryObjectNotFoundException(Res.GetString("TransportNotFound", objArray), typeof(ActiveDirectoryInterSiteTransport), transport.ToString());
                                    }
                                    else
                                    {
                                        throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                                    }
                                }
                            }
                            return(new ActiveDirectoryInterSiteTransport(context, transport, directoryEntry));
                        }
                    }
                    else
                    {
                        throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
                    }
                }
                else
                {
                    throw new ArgumentException(Res.GetString("ContextNotAssociatedWithDomain"), "context");
                }
            }
            else
            {
                throw new ArgumentNullException("context");
            }
        }
        public static ActiveDirectorySchema GetSchema(DirectoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (((context.ContextType != DirectoryContextType.Forest) && (context.ContextType != DirectoryContextType.ConfigurationSet)) && (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(Res.GetString("NotADOrADAM"), "context");
            }
            if ((context.Name == null) && !context.isRootDomain())
            {
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ContextNotAssociatedWithDomain"), typeof(ActiveDirectorySchema), null);
            }
            if (((context.Name != null) && !context.isRootDomain()) && (!context.isADAMConfigSet() && !context.isServer()))
            {
                if (context.ContextType == DirectoryContextType.Forest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
            }
            context = new DirectoryContext(context);
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            string distinguishedName = null;

            try
            {
                DirectoryEntry cachedDirectoryEntry = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if (context.isServer() && !Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryOrADAM))
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
                }
                distinguishedName = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.SchemaNamingContext);
            }
            catch (COMException exception)
            {
                if (exception.ErrorCode != -2147016646)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, exception);
                }
                if (context.ContextType == DirectoryContextType.Forest)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ForestNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ServerNotFound", new object[] { context.Name }), typeof(ActiveDirectorySchema), null);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ActiveDirectorySchema), context.Name);
                }
                throw;
            }
            return(new ActiveDirectorySchema(context, distinguishedName, directoryEntryMgr));
        }
        private void ValidateTargetAndSourceServer(DirectoryContext context, DirectoryServer sourceServer)
        {
            bool           targetIsDC = false;
            DirectoryEntry targetDE   = null;
            DirectoryEntry sourceDE   = null;

            // first find out target is a dc or ADAM instance
            targetDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
            try
            {
                if (Utils.CheckCapability(targetDE, Capability.ActiveDirectory))
                {
                    targetIsDC = true;
                }
                else if (!Utils.CheckCapability(targetDE, Capability.ActiveDirectoryApplicationMode))
                {
                    // if it is also not an ADAM instance, it is invalid then
                    throw new ArgumentException(Res.GetString(Res.DirectoryContextNeedHost), "context");
                }

                if (targetIsDC && !(sourceServer is DomainController))
                {
                    // target and sourceServer are not of the same type
                    throw new ArgumentException(Res.GetString(Res.ConnectionSourcServerShouldBeDC), "sourceServer");
                }
                else if (!targetIsDC && (sourceServer is DomainController))
                {
                    // target and sourceServer are not of the same type
                    throw new ArgumentException(Res.GetString(Res.ConnectionSourcServerShouldBeADAM), "sourceServer");
                }

                sourceDE = DirectoryEntryManager.GetDirectoryEntry(sourceServer.Context, WellKnownDN.RootDSE);

                // now if they are both dc, we need to check whether they come from the same forest
                if (targetIsDC)
                {
                    string targetRoot = (string)PropertyManager.GetPropertyValue(context, targetDE, PropertyManager.RootDomainNamingContext);
                    string sourceRoot = (string)PropertyManager.GetPropertyValue(sourceServer.Context, sourceDE, PropertyManager.RootDomainNamingContext);
                    if (Utils.Compare(targetRoot, sourceRoot) != 0)
                    {
                        throw new ArgumentException(Res.GetString(Res.ConnectionSourcServerSameForest), "sourceServer");
                    }
                }
                else
                {
                    string targetRoot = (string)PropertyManager.GetPropertyValue(context, targetDE, PropertyManager.ConfigurationNamingContext);
                    string sourceRoot = (string)PropertyManager.GetPropertyValue(sourceServer.Context, sourceDE, PropertyManager.ConfigurationNamingContext);
                    if (Utils.Compare(targetRoot, sourceRoot) != 0)
                    {
                        throw new ArgumentException(Res.GetString(Res.ConnectionSourcServerSameConfigSet), "sourceServer");
                    }
                }
            }
            catch (COMException e)
            {
                ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                if (targetDE != null)
                {
                    targetDE.Close();
                }

                if (sourceDE != null)
                {
                    sourceDE.Close();
                }
            }
        }
        public ReplicationConnection(DirectoryContext context, string name, DirectoryServer sourceServer, ActiveDirectorySchedule schedule, ActiveDirectoryTransportType transport)
        {
            ValidateArgument(context, name);

            if (sourceServer == null)
            {
                throw new ArgumentNullException("sourceServer");
            }

            if (transport < ActiveDirectoryTransportType.Rpc || transport > ActiveDirectoryTransportType.Smtp)
            {
                throw new InvalidEnumArgumentException("value", (int)transport, typeof(ActiveDirectoryTransportType));
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            ValidateTargetAndSourceServer(context, sourceServer);

            this.context    = context;
            _connectionName = name;
            _transport      = transport;

            DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);

            try
            {
                string serverDN            = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ServerName);
                string connectionContainer = "CN=NTDS Settings," + serverDN;
                de = DirectoryEntryManager.GetDirectoryEntry(context, connectionContainer);

                // create the connection entry
                string rdn = "cn=" + _connectionName;
                rdn = Utils.GetEscapedPath(rdn);
                cachedDirectoryEntry = de.Children.Add(rdn, "nTDSConnection");

                // set all the properties

                // sourceserver property
                DirectoryContext sourceServerContext = sourceServer.Context;
                de = DirectoryEntryManager.GetDirectoryEntry(sourceServerContext, WellKnownDN.RootDSE);
                string serverName = (string)PropertyManager.GetPropertyValue(sourceServerContext, de, PropertyManager.ServerName);
                serverName = "CN=NTDS Settings," + serverName;

                cachedDirectoryEntry.Properties["fromServer"].Add(serverName);

                // schedule property
                if (schedule != null)
                {
                    cachedDirectoryEntry.Properties["schedule"].Value = schedule.GetUnmanagedSchedule();
                }

                // transporttype property
                string transportPath = Utils.GetDNFromTransportType(TransportType, context);
                // verify that the transport is supported
                de = DirectoryEntryManager.GetDirectoryEntry(context, transportPath);
                try
                {
                    de.Bind(true);
                }
                catch (COMException e)
                {
                    if (e.ErrorCode == unchecked ((int)0x80072030))
                    {
                        // if it is ADAM and transport type is SMTP, throw NotSupportedException.
                        DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                        if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                        {
                            throw new NotSupportedException(Res.GetString(Res.NotSupportTransportSMTP));
                        }
                    }

                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }

                cachedDirectoryEntry.Properties["transportType"].Add(transportPath);

                // enabledConnection property
                cachedDirectoryEntry.Properties["enabledConnection"].Value = false;

                // options
                cachedDirectoryEntry.Properties["options"].Value = 0;
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                de.Close();
            }
        }
Example #11
0
        public static Forest GetForest(DirectoryContext context)
        {
            DirectoryEntryManager?directoryEntryMgr = null;
            DirectoryEntry?       rootDSE           = null;
            string?rootDomainNC = null;

            // check that the argument is not null
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // contexttype should be Forest or DirectoryServer
            if ((context.ContextType != DirectoryContextType.Forest) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(SR.TargetShouldBeServerORForest, nameof(context));
            }

            if ((context.Name == null) && (!context.isRootDomain()))
            {
                throw new ActiveDirectoryObjectNotFoundException(SR.ContextNotAssociatedWithDomain, typeof(Forest), null);
            }

            if (context.Name != null)
            {
                // the target should be a valid forest name or a server
                if (!((context.isRootDomain()) || (context.isServer())))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ForestNotFound, typeof(Forest), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Forest), null);
                    }
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            directoryEntryMgr = new DirectoryEntryManager(context);
            // at this point we know that the target is either a
            // valid forest name or a server (may be a bogus server name -- to check bind to rootdse)
            // bind to the rootDSE of the forest specified in the context
            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Forest), null);
                }
                rootDomainNC = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext) !;
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.Forest)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ForestNotFound, typeof(Forest), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.DCNotFound, context.Name), typeof(Forest), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            // return forest object

            return(new Forest(context, Utils.GetDnsNameFromDN(rootDomainNC), directoryEntryMgr));
        }
        public void Save()
        {
            base.CheckIfDisposed();
            if (this.committed)
            {
                goto Label_021F;
            }
            bool flag = false;

            if (this.appType == ApplicationPartitionType.ADApplicationPartition)
            {
                try
                {
                    this.domainDNSEntry.CommitChanges();
                    goto Label_004B;
                }
                catch (COMException exception)
                {
                    if (exception.ErrorCode != -2147016663)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception);
                    }
                    flag = true;
                    goto Label_004B;
                }
            }
            flag = true;
Label_004B:
            if (flag)
            {
                try
                {
                    this.InitializeCrossRef(base.partitionName);
                    this.crossRefEntry.CommitChanges();
                }
                catch (COMException exception2)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception2);
                }
                try
                {
                    this.domainDNSEntry.CommitChanges();
                }
                catch (COMException exception3)
                {
                    DirectoryEntry parent = this.crossRefEntry.Parent;
                    try
                    {
                        parent.Children.Remove(this.crossRefEntry);
                    }
                    catch (COMException exception4)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(exception4);
                    }
                    throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception3);
                }
                try
                {
                    this.crossRefEntry.RefreshCache();
                }
                catch (COMException exception5)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception5);
                }
            }
            DirectoryEntry cachedDirectoryEntry = base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
            string         str = (string)PropertyManager.GetPropertyValue(base.context, cachedDirectoryEntry, PropertyManager.DsServiceName);

            if (this.appType == ApplicationPartitionType.ADApplicationPartition)
            {
                this.GetCrossRefEntry();
            }
            string         str2           = (string)PropertyManager.GetPropertyValue(base.context, this.crossRefEntry, PropertyManager.DistinguishedName);
            DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(this.GetNamingRoleOwner(), DirectoryContextType.DirectoryServer, base.context), WellKnownDN.RootDSE);

            try
            {
                directoryEntry.Properties[PropertyManager.ReplicateSingleObject].Value = str + ":" + str2;
                directoryEntry.CommitChanges();
            }
            catch (COMException exception6)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception6);
            }
            finally
            {
                directoryEntry.Dispose();
            }
            this.committed = true;
            if ((this.cachedDirectoryServers == null) && !this.securityRefDomainModified)
            {
                goto Label_024C;
            }
            if (this.cachedDirectoryServers != null)
            {
                this.crossRefEntry.Properties[PropertyManager.MsDSNCReplicaLocations].AddRange(this.cachedDirectoryServers.GetMultiValuedProperty());
            }
            if (this.securityRefDomainModified)
            {
                this.crossRefEntry.Properties[PropertyManager.MsDSSDReferenceDomain].Value = this.securityRefDomain;
            }
            try
            {
                this.crossRefEntry.CommitChanges();
                goto Label_024C;
            }
            catch (COMException exception7)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception7);
            }
Label_021F:
            if ((this.cachedDirectoryServers != null) || this.securityRefDomainModified)
            {
                try
                {
                    this.crossRefEntry.CommitChanges();
                }
                catch (COMException exception8)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception8);
                }
            }
Label_024C:
            this.cachedDirectoryServers    = null;
            this.securityRefDomainModified = false;
        }
        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));
        }
Example #14
0
        private ArrayList GetApplicationPartitions()
        {
            ArrayList      appNCs          = new ArrayList();
            DirectoryEntry rootDSE         = _directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
            DirectoryEntry partitionsEntry = _directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.PartitionsContainer);

            // search for all the "crossRef" objects that have the
            // ADS_SYSTEMFLAG_CR_NTDS_NC set and the SYSTEMFLAG_CR_NTDS_DOMAIN flag not set
            // (one-level search is good enough)
            // setup the directory searcher object

            // build the filter
            StringBuilder str = new StringBuilder(100);

            str.Append("(&(");
            str.Append(PropertyManager.ObjectCategory);
            str.Append("=crossRef)(");
            str.Append(PropertyManager.SystemFlags);
            str.Append(":1.2.840.113556.1.4.804:=");
            str.Append((int)SystemFlag.SystemFlagNtdsNC);
            str.Append(")(!(");
            str.Append(PropertyManager.SystemFlags);
            str.Append(":1.2.840.113556.1.4.803:=");
            str.Append((int)SystemFlag.SystemFlagNtdsDomain);
            str.Append(")))");

            string filter = str.ToString();

            string[] propertiesToLoad = new string[2];
            propertiesToLoad[0] = PropertyManager.NCName;
            propertiesToLoad[1] = PropertyManager.MsDSNCReplicaLocations;

            ADSearcher             searcher = new ADSearcher(partitionsEntry, filter, propertiesToLoad, SearchScope.OneLevel);
            SearchResultCollection resCol   = null;

            try
            {
                resCol = searcher.FindAll();

                string schemaNamingContext        = (string)PropertyManager.GetPropertyValue(_context, rootDSE, PropertyManager.SchemaNamingContext);
                string configurationNamingContext = (string)PropertyManager.GetPropertyValue(_context, rootDSE, PropertyManager.ConfigurationNamingContext);

                foreach (SearchResult res in resCol)
                {
                    // add the name of the appNC only if it is not
                    // the Schema or Configuration partition
                    string nCName = (string)PropertyManager.GetSearchResultPropertyValue(res, PropertyManager.NCName);

                    if ((!(nCName.Equals(schemaNamingContext))) && (!(nCName.Equals(configurationNamingContext))))
                    {
                        ResultPropertyValueCollection replicaLocations = res.Properties[PropertyManager.MsDSNCReplicaLocations];
                        if (replicaLocations.Count > 0)
                        {
                            string           replicaName  = Utils.GetAdamDnsHostNameFromNTDSA(_context, (string)replicaLocations[Utils.GetRandomIndex(replicaLocations.Count)]);
                            DirectoryContext appNCContext = Utils.GetNewDirectoryContext(replicaName, DirectoryContextType.DirectoryServer, _context);
                            appNCs.Add(new ApplicationPartition(appNCContext, nCName, null, ApplicationPartitionType.ADAMApplicationPartition, new DirectoryEntryManager(appNCContext)));
                        }
                    }
                }
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(_context, e);
            }
            finally
            {
                if (resCol != null)
                {
                    // call dispose on search result collection
                    resCol.Dispose();
                }
            }
            return(appNCs);
        }
        public static ActiveDirectorySiteLinkBridge FindByName(DirectoryContext context, string bridgeName, ActiveDirectoryTransportType transport)
        {
            DirectoryEntry directoryEntry;
            ActiveDirectorySiteLinkBridge activeDirectorySiteLinkBridge;

            ActiveDirectorySiteLinkBridge.ValidateArgument(context, bridgeName, transport);
            context = new DirectoryContext(context);
            try
            {
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                string str           = string.Concat("CN=Inter-Site Transports,CN=Sites,", propertyValue);
                if (transport != ActiveDirectoryTransportType.Rpc)
                {
                    str = string.Concat("CN=SMTP,", str);
                }
                else
                {
                    str = string.Concat("CN=IP,", str);
                }
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
            }
            catch (COMException cOMException1)
            {
                COMException cOMException = cOMException1;
                throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
            }
            catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
            {
                object[] name = new object[1];
                name[0] = context.Name;
                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", name));
            }
            try
            {
                try
                {
                    string[] strArrays = new string[1];
                    strArrays[0] = "distinguishedName";
                    ADSearcher   aDSearcher   = new ADSearcher(directoryEntry, string.Concat("(&(objectClass=siteLinkBridge)(objectCategory=SiteLinkBridge)(name=", Utils.GetEscapedFilterValue(bridgeName), "))"), strArrays, SearchScope.OneLevel, false, false);
                    SearchResult searchResult = aDSearcher.FindOne();
                    if (searchResult != null)
                    {
                        DirectoryEntry directoryEntry1 = searchResult.GetDirectoryEntry();
                        ActiveDirectorySiteLinkBridge activeDirectorySiteLinkBridge1 = new ActiveDirectorySiteLinkBridge(context, bridgeName, transport, true);
                        activeDirectorySiteLinkBridge1.cachedEntry = directoryEntry1;
                        activeDirectorySiteLinkBridge = activeDirectorySiteLinkBridge1;
                    }
                    else
                    {
                        Exception exception = new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLinkBridge), bridgeName);
                        throw exception;
                    }
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    if (cOMException2.ErrorCode != -2147016656)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                    }
                    else
                    {
                        DirectoryEntry directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                        if (!Utils.CheckCapability(directoryEntry2, Capability.ActiveDirectoryApplicationMode) || transport != ActiveDirectoryTransportType.Smtp)
                        {
                            throw new ActiveDirectoryObjectNotFoundException(Res.GetString("DSNotFound"), typeof(ActiveDirectorySiteLinkBridge), bridgeName);
                        }
                        else
                        {
                            throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                        }
                    }
                }
            }
            finally
            {
                directoryEntry.Dispose();
            }
            return(activeDirectorySiteLinkBridge);
        }
Example #16
0
        public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
        {
            // check that the argument is not null
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // target should ConfigurationSet or DirectoryServer
            if ((context.ContextType != DirectoryContextType.ConfigurationSet) &&
                (context.ContextType != DirectoryContextType.DirectoryServer))
            {
                throw new ArgumentException(SR.TargetShouldBeServerORConfigSet, nameof(context));
            }

            // target should be an adam config set or server
            if (((!context.isServer()) && (!context.isADAMConfigSet())))
            {
                // the target should be a server or an ADAM Config Set
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ConfigurationSet), context.Name);
                }
                else
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(ConfigurationSet), null);
                }
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            //
            // bind to rootdse of an adam instance (if target is already a server, verify that it is an adam instance)
            //
            DirectoryEntryManager directoryEntryMgr = new DirectoryEntryManager(context);
            DirectoryEntry        rootDSE           = null;
            string configSetName = null;

            try
            {
                rootDSE = directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                if ((context.isServer()) && (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode)))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(ConfigurationSet), null);
                }

                configSetName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext);
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    if (context.ContextType == DirectoryContextType.ConfigurationSet)
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ConfigurationSet), context.Name);
                    }
                    else
                    {
                        throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.AINotFound, context.Name), typeof(ConfigurationSet), null);
                    }
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                if (context.ContextType == DirectoryContextType.ConfigurationSet)
                {
                    // this is the case when we could not find an ADAM instance in that config set
                    throw new ActiveDirectoryObjectNotFoundException(SR.ConfigSetNotFound, typeof(ConfigurationSet), context.Name);
                }
                else
                {
                    throw;
                }
            }

            // return config set object
            return(new ConfigurationSet(context, configSetName, directoryEntryMgr));
        }
        public ActiveDirectorySiteLinkBridge(DirectoryContext context, string bridgeName, ActiveDirectoryTransportType transport)
        {
            DirectoryEntry directoryEntry;
            string         str;

            this.links = new ActiveDirectorySiteLinkCollection();
            ActiveDirectorySiteLinkBridge.ValidateArgument(context, bridgeName, transport);
            context        = new DirectoryContext(context);
            this.context   = context;
            this.name      = bridgeName;
            this.transport = transport;
            try
            {
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                if (transport != ActiveDirectoryTransportType.Rpc)
                {
                    str = string.Concat("CN=SMTP,CN=Inter-Site Transports,CN=Sites,", propertyValue);
                }
                else
                {
                    str = string.Concat("CN=IP,CN=Inter-Site Transports,CN=Sites,", propertyValue);
                }
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, str);
            }
            catch (COMException cOMException1)
            {
                COMException cOMException = cOMException1;
                throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
            }
            catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
            {
                object[] name = new object[1];
                name[0] = context.Name;
                throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", name));
            }
            try
            {
                try
                {
                    string escapedPath = string.Concat("cn=", this.name);
                    escapedPath      = Utils.GetEscapedPath(escapedPath);
                    this.cachedEntry = directoryEntry.Children.Add(escapedPath, "siteLinkBridge");
                }
                catch (COMException cOMException3)
                {
                    COMException cOMException2 = cOMException3;
                    if (cOMException2.ErrorCode == -2147016656)
                    {
                        DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                        if (Utils.CheckCapability(directoryEntry1, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                        {
                            throw new NotSupportedException(Res.GetString("NotSupportTransportSMTP"));
                        }
                    }
                    throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException2);
                }
            }
            finally
            {
                directoryEntry.Dispose();
            }
        }
        private DirectoryServer GetSchemaRoleOwner()
        {
            DirectoryServer server2;

            try
            {
                this.schemaEntry.RefreshCache();
                if (base.context.isADAMConfigSet())
                {
                    string adamDnsHostNameFromNTDSA = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    return(new AdamInstance(Utils.GetNewDirectoryContext(adamDnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), adamDnsHostNameFromNTDSA));
                }
                DirectoryServer server = null;
                if (Utils.CheckCapability(base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE), Capability.ActiveDirectory))
                {
                    string dnsHostNameFromNTDSA = Utils.GetDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    server = new DomainController(Utils.GetNewDirectoryContext(dnsHostNameFromNTDSA, DirectoryContextType.DirectoryServer, base.context), dnsHostNameFromNTDSA);
                }
                else
                {
                    string adamInstanceName = Utils.GetAdamDnsHostNameFromNTDSA(base.context, (string)PropertyManager.GetPropertyValue(base.context, this.schemaEntry, PropertyManager.FsmoRoleOwner));
                    server = new AdamInstance(Utils.GetNewDirectoryContext(adamInstanceName, DirectoryContextType.DirectoryServer, base.context), adamInstanceName);
                }
                server2 = server;
            }
            catch (COMException exception)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception);
            }
            return(server2);
        }
Example #19
0
        internal static string ExpandWellKnownDN(DirectoryContext context, WellKnownDN dn)
        {
            string?distinguishedName = null;

            switch (dn)
            {
            case WellKnownDN.RootDSE:
            {
                distinguishedName = "RootDSE";
                break;
            }

            case WellKnownDN.RootDomainNamingContext:
            {
                DirectoryEntry rootDSE = GetDirectoryEntry(context, "RootDSE");

                try
                {
                    distinguishedName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext) !;
                }
                finally
                {
                    rootDSE.Dispose();
                }
                break;
            }

            case WellKnownDN.DefaultNamingContext:
            {
                DirectoryEntry rootDSE = GetDirectoryEntry(context, "RootDSE");
                try
                {
                    distinguishedName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DefaultNamingContext) !;
                }
                finally
                {
                    rootDSE.Dispose();
                }
                break;
            }

            case WellKnownDN.SchemaNamingContext:
            {
                DirectoryEntry rootDSE = GetDirectoryEntry(context, "RootDSE");
                try
                {
                    distinguishedName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.SchemaNamingContext) !;
                }
                finally
                {
                    rootDSE.Dispose();
                }
                break;
            }

            case WellKnownDN.ConfigurationNamingContext:
            {
                DirectoryEntry rootDSE = GetDirectoryEntry(context, "RootDSE");
                try
                {
                    distinguishedName = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext) !;
                }
                finally
                {
                    rootDSE.Dispose();
                }
                break;
            }

            case WellKnownDN.PartitionsContainer:
            {
                distinguishedName = "CN=Partitions," + ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext);
                break;
            }

            case WellKnownDN.SitesContainer:
            {
                distinguishedName = "CN=Sites," + ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext);
                break;
            }

            case WellKnownDN.SystemContainer:
            {
                distinguishedName = "CN=System," + ExpandWellKnownDN(context, WellKnownDN.DefaultNamingContext);
                break;
            }

            case WellKnownDN.RidManager:
            {
                distinguishedName = "CN=RID Manager$," + ExpandWellKnownDN(context, WellKnownDN.SystemContainer);
                break;
            }

            case WellKnownDN.Infrastructure:
            {
                distinguishedName = "CN=Infrastructure," + ExpandWellKnownDN(context, WellKnownDN.DefaultNamingContext);
                break;
            }

            default:
                // should not happen
                throw new InvalidEnumArgumentException(nameof(dn), (int)dn, typeof(WellKnownDN));
            }
            return(distinguishedName);
        }
Example #20
0
        internal ReplicationConnectionCollection GetInboundConnectionsHelper()
        {
            if (_inbound == null)
            {
                // construct the replicationconnection collection
                _inbound = new ReplicationConnectionCollection();
                DirectoryContext newContext = Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context);

                // this is the first time that user tries to retrieve this property, so get it from the directory
                string         serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName;
                string         srchDN     = "CN=NTDS Settings," + serverName;
                DirectoryEntry de         = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context), srchDN);

                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection))",
                                                       new string[] { "cn" },
                                                       SearchScope.OneLevel);
                SearchResultCollection?srchResults = null;

                try
                {
                    srchResults = adSearcher.FindAll();
                    foreach (SearchResult r in srchResults)
                    {
                        ReplicationConnection con = new ReplicationConnection(newContext, r.GetDirectoryEntry(), (string)PropertyManager.GetSearchResultPropertyValue(r, PropertyManager.Cn) !);
                        _inbound.Add(con);
                    }
                }
                catch (COMException e)
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(newContext, e);
                }
                finally
                {
                    if (srchResults != null)
                    {
                        srchResults.Dispose();
                    }

                    de.Dispose();
                }
            }

            return(_inbound);
        }
Example #21
0
        public static GlobalCatalog GetGlobalCatalog(DirectoryContext context)
        {
            string gcDnsName       = null;
            bool   isGlobalCatalog = false;
            DirectoryEntryManager directoryEntryMgr = null;

            // check that the context argument is not null
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // target should be GC
            if (context.ContextType != DirectoryContextType.DirectoryServer)
            {
                throw new ArgumentException(SR.TargetShouldBeGC, nameof(context));
            }

            // target should be a server
            if (!(context.isServer()))
            {
                throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.GCNotFound, context.Name), typeof(GlobalCatalog), context.Name);
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            try
            {
                // Get dns name of the dc
                // by binding to root dse and getting the "dnsHostName" attribute
                // (also check that the "isGlobalCatalogReady" attribute is true)
                directoryEntryMgr = new DirectoryEntryManager(context);
                DirectoryEntry rootDSE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.GCNotFound, context.Name), typeof(GlobalCatalog), context.Name);
                }

                gcDnsName       = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.DnsHostName);
                isGlobalCatalog = (bool)bool.Parse((string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.IsGlobalCatalogReady));
                if (!isGlobalCatalog)
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.GCNotFound, context.Name), typeof(GlobalCatalog), context.Name);
                }
            }
            catch (COMException e)
            {
                int errorCode = e.ErrorCode;

                if (errorCode == unchecked ((int)0x8007203a))
                {
                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.GCNotFound, context.Name), typeof(GlobalCatalog), context.Name);
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            return(new GlobalCatalog(context, gcDnsName, directoryEntryMgr));
        }
Example #22
0
 public static ConfigurationSet GetConfigurationSet(DirectoryContext context)
 {
     if (context != null)
     {
         if (context.ContextType == DirectoryContextType.ConfigurationSet || context.ContextType == DirectoryContextType.DirectoryServer)
         {
             if (context.isServer() || context.isADAMConfigSet())
             {
                 context = new DirectoryContext(context);
                 DirectoryEntryManager directoryEntryManager = new DirectoryEntryManager(context);
                 string propertyValue = null;
                 try
                 {
                     DirectoryEntry cachedDirectoryEntry = directoryEntryManager.GetCachedDirectoryEntry(WellKnownDN.RootDSE);
                     if (!context.isServer() || Utils.CheckCapability(cachedDirectoryEntry, Capability.ActiveDirectoryApplicationMode))
                     {
                         propertyValue = (string)PropertyManager.GetPropertyValue(context, cachedDirectoryEntry, PropertyManager.ConfigurationNamingContext);
                     }
                     else
                     {
                         object[] name = new object[1];
                         name[0] = context.Name;
                         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name), typeof(ConfigurationSet), null);
                     }
                 }
                 catch (COMException cOMException1)
                 {
                     COMException cOMException = cOMException1;
                     int          errorCode    = cOMException.ErrorCode;
                     if (errorCode != -2147016646)
                     {
                         throw ExceptionHelper.GetExceptionFromCOMException(context, cOMException);
                     }
                     else
                     {
                         if (context.ContextType != DirectoryContextType.ConfigurationSet)
                         {
                             object[] objArray = new object[1];
                             objArray[0] = context.Name;
                             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", objArray), typeof(ConfigurationSet), null);
                         }
                         else
                         {
                             throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
                         }
                     }
                 }
                 catch (ActiveDirectoryObjectNotFoundException activeDirectoryObjectNotFoundException)
                 {
                     if (context.ContextType != DirectoryContextType.ConfigurationSet)
                     {
                         throw;
                     }
                     else
                     {
                         throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
                     }
                 }
                 return(new ConfigurationSet(context, propertyValue, directoryEntryManager));
             }
             else
             {
                 if (context.ContextType != DirectoryContextType.ConfigurationSet)
                 {
                     object[] name1 = new object[1];
                     name1[0] = context.Name;
                     throw new ActiveDirectoryObjectNotFoundException(Res.GetString("AINotFound", name1), typeof(ConfigurationSet), null);
                 }
                 else
                 {
                     throw new ActiveDirectoryObjectNotFoundException(Res.GetString("ConfigSetNotFound"), typeof(ConfigurationSet), context.Name);
                 }
             }
         }
         else
         {
             throw new ArgumentException(Res.GetString("TargetShouldBeServerORConfigSet"), "context");
         }
     }
     else
     {
         throw new ArgumentNullException("context");
     }
 }
        public static ActiveDirectorySiteLink FindByName(DirectoryContext context, string siteLinkName, ActiveDirectoryTransportType transport)
        {
            ValidateArgument(context, siteLinkName, transport);

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the configurationnamingcontext
            DirectoryEntry de;

            try
            {
                de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string config      = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext) !;
                string containerDN = "CN=Inter-Site Transports,CN=Sites," + config;
                if (transport == ActiveDirectoryTransportType.Rpc)
                {
                    containerDN = "CN=IP," + containerDN;
                }
                else
                {
                    containerDN = "CN=SMTP," + containerDN;
                }
                de = DirectoryEntryManager.GetDirectoryEntry(context, containerDN);
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                // this is the case where the context is a config set and we could not find an ADAM instance in that config set
                throw new ActiveDirectoryOperationException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, context.Name));
            }

            try
            {
                ADSearcher adSearcher = new ADSearcher(de,
                                                       "(&(objectClass=siteLink)(objectCategory=SiteLink)(name=" + Utils.GetEscapedFilterValue(siteLinkName) + "))",
                                                       new string[] { "distinguishedName" },
                                                       SearchScope.OneLevel,
                                                       false, /* don't need paged search */
                                                       false  /* don't need to cache result */
                                                       );
                SearchResult?srchResult = adSearcher.FindOne();
                if (srchResult == null)
                {
                    // no such sitelink object
                    Exception e = new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ActiveDirectorySiteLink), siteLinkName);
                    throw e;
                }
                else
                {
                    DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry();
                    // it is an existing site object
                    ActiveDirectorySiteLink link = new ActiveDirectorySiteLink(context, siteLinkName, transport, true, connectionEntry);
                    return(link);
                }
            }
            catch (COMException e)
            {
                if (e.ErrorCode == unchecked ((int)0x80072030))
                {
                    // if it is ADAM and transport type is SMTP, throw NotSupportedException.
                    DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                    if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                    {
                        throw new NotSupportedException(SR.NotSupportTransportSMTP);
                    }
                    else
                    {
                        // object is not found since we cannot even find the container in which to search
                        throw new ActiveDirectoryObjectNotFoundException(SR.DSNotFound, typeof(ActiveDirectorySiteLink), siteLinkName);
                    }
                }

                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            finally
            {
                de.Dispose();
            }
        }
Example #24
0
        public static ActiveDirectoryInterSiteTransport FindByTransportType(DirectoryContext context, ActiveDirectoryTransportType transport)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // if target is not specified, then we determin the target from the logon credential, so if it is a local user context, it should fail
            if ((context.Name == null) && (!context.isRootDomain()))
            {
                throw new ArgumentException(SR.ContextNotAssociatedWithDomain, "context");
            }

            // more validation for the context, if the target is not null, then it should be either forest name or server name
            if (context.Name != null)
            {
                if (!(context.isRootDomain() || context.isServer() || context.isADAMConfigSet()))
                {
                    throw new ArgumentException(SR.NotADOrADAM, "context");
                }
            }

            if (transport < ActiveDirectoryTransportType.Rpc || transport > ActiveDirectoryTransportType.Smtp)
            {
                throw new InvalidEnumArgumentException("value", (int)transport, typeof(ActiveDirectoryTransportType));
            }

            //  work with copy of the context
            context = new DirectoryContext(context);

            // bind to the rootdse to get the configurationnamingcontext
            DirectoryEntry de;

            try
            {
                de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                string config      = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext);
                string containerDN = "CN=Inter-Site Transports,CN=Sites," + config;
                if (transport == ActiveDirectoryTransportType.Rpc)
                {
                    containerDN = "CN=IP," + containerDN;
                }
                else
                {
                    containerDN = "CN=SMTP," + containerDN;
                }
                de = DirectoryEntryManager.GetDirectoryEntry(context, containerDN);
            }
            catch (COMException e)
            {
                throw ExceptionHelper.GetExceptionFromCOMException(context, e);
            }
            catch (ActiveDirectoryObjectNotFoundException)
            {
                // this is the case where the context is a config set and we could not find an ADAM instance in that config set
                throw new ActiveDirectoryOperationException(SR.Format(SR.ADAMInstanceNotFoundInConfigSet, context.Name));
            }

            try
            {
                de.RefreshCache(new string[] { "options" });
            }
            catch (COMException e)
            {
                if (e.ErrorCode == unchecked ((int)0x80072030))
                {
                    // if it is ADAM and transport type is SMTP, throw NotSupportedException.
                    DirectoryEntry tmpDE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
                    if (Utils.CheckCapability(tmpDE, Capability.ActiveDirectoryApplicationMode) && transport == ActiveDirectoryTransportType.Smtp)
                    {
                        throw new NotSupportedException(SR.NotSupportTransportSMTP);
                    }

                    throw new ActiveDirectoryObjectNotFoundException(SR.Format(SR.TransportNotFound, transport.ToString()), typeof(ActiveDirectoryInterSiteTransport), transport.ToString());
                }
                else
                {
                    throw ExceptionHelper.GetExceptionFromCOMException(context, e);
                }
            }

            return(new ActiveDirectoryInterSiteTransport(context, transport, de));
        }
        private void ValidateTargetAndSourceServer(DirectoryContext context, DirectoryServer sourceServer)
        {
            bool           flag           = false;
            DirectoryEntry rootDSE        = null;
            DirectoryEntry directoryEntry = null;

            rootDSE = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE);
            try
            {
                if (Utils.CheckCapability(rootDSE, Capability.ActiveDirectory))
                {
                    flag = true;
                }
                else if (!Utils.CheckCapability(rootDSE, Capability.ActiveDirectoryApplicationMode))
                {
                    throw new ArgumentException(Res.GetString("DirectoryContextNeedHost"), "context");
                }
                if (flag && !(sourceServer is DomainController))
                {
                    throw new ArgumentException(Res.GetString("ConnectionSourcServerShouldBeDC"), "sourceServer");
                }
                if (!flag && (sourceServer is DomainController))
                {
                    throw new ArgumentException(Res.GetString("ConnectionSourcServerShouldBeADAM"), "sourceServer");
                }
                directoryEntry = DirectoryEntryManager.GetDirectoryEntry(sourceServer.Context, WellKnownDN.RootDSE);
                if (flag)
                {
                    string str  = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.RootDomainNamingContext);
                    string str2 = (string)PropertyManager.GetPropertyValue(sourceServer.Context, directoryEntry, PropertyManager.RootDomainNamingContext);
                    if (Utils.Compare(str, str2) != 0)
                    {
                        throw new ArgumentException(Res.GetString("ConnectionSourcServerSameForest"), "sourceServer");
                    }
                }
                else
                {
                    string str3 = (string)PropertyManager.GetPropertyValue(context, rootDSE, PropertyManager.ConfigurationNamingContext);
                    string str4 = (string)PropertyManager.GetPropertyValue(sourceServer.Context, directoryEntry, PropertyManager.ConfigurationNamingContext);
                    if (Utils.Compare(str3, str4) != 0)
                    {
                        throw new ArgumentException(Res.GetString("ConnectionSourcServerSameConfigSet"), "sourceServer");
                    }
                }
            }
            catch (COMException exception)
            {
                ExceptionHelper.GetExceptionFromCOMException(context, exception);
            }
            finally
            {
                if (rootDSE != null)
                {
                    rootDSE.Close();
                }
                if (directoryEntry != null)
                {
                    directoryEntry.Close();
                }
            }
        }