public ActiveDirectorySubnet(DirectoryContext context, string subnetName) { ValidateArgument(context, subnetName); // work with copy of the context context = new DirectoryContext(context); this.context = context; _name = subnetName; // bind to the rootdse to get the configurationnamingcontext DirectoryEntry de = null; try { de = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); string config = (string)PropertyManager.GetPropertyValue(context, de, PropertyManager.ConfigurationNamingContext); string subnetn = "CN=Subnets,CN=Sites," + config; // bind to the subnet container de = DirectoryEntryManager.GetDirectoryEntry(context, subnetn); string rdn = "cn=" + _name; rdn = Utils.GetEscapedPath(rdn); cachedEntry = de.Children.Add(rdn, "subnet"); } catch (COMException e) { 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)); } finally { if (de != null) { de.Dispose(); } } }
private static DirectoryEntry GetSearchRootEntry(Forest forest) { DirectoryEntry rootEntry; DirectoryContext forestContext = forest.GetDirectoryContext(); bool isServer = false; bool isGC = false; AuthenticationTypes authType = Utils.DefaultAuthType; if (forestContext.ContextType == DirectoryContextType.DirectoryServer) { // // the forest object was created by specifying a server name // so we will stick to that server for the search. We need to determine // whether or not the server is a DC or GC // isServer = true; DirectoryEntry rootDSE = DirectoryEntryManager.GetDirectoryEntry(forestContext, WellKnownDN.RootDSE); string isGCReady = (string)PropertyManager.GetPropertyValue(forestContext, rootDSE, PropertyManager.IsGlobalCatalogReady); isGC = (Utils.Compare(isGCReady, "TRUE") == 0); } if (isServer) { authType |= AuthenticationTypes.ServerBind; if (isGC) { rootEntry = new DirectoryEntry("GC://" + forestContext.GetServerName(), forestContext.UserName, forestContext.Password, authType); } else { rootEntry = new DirectoryEntry("LDAP://" + forestContext.GetServerName(), forestContext.UserName, forestContext.Password, authType); } } else { // need to find any GC in the forest rootEntry = new DirectoryEntry("GC://" + forest.Name, forestContext.UserName, forestContext.Password, authType); } return(rootEntry); }
public static GlobalCatalog GetGlobalCatalog(DirectoryContext context) { string globalCatalogName = null; DirectoryEntryManager directoryEntryMgr = null; if (context == null) { throw new ArgumentNullException("context"); } if (context.ContextType != DirectoryContextType.DirectoryServer) { throw new ArgumentException(Res.GetString("TargetShouldBeGC"), "context"); } if (!context.isServer()) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", new object[] { context.Name }), typeof(GlobalCatalog), context.Name); } context = new DirectoryContext(context); try { directoryEntryMgr = new DirectoryEntryManager(context); DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, WellKnownDN.RootDSE); if (!Utils.CheckCapability(directoryEntry, Capability.ActiveDirectory)) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", new object[] { context.Name }), typeof(GlobalCatalog), context.Name); } globalCatalogName = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.DnsHostName); if (!bool.Parse((string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.IsGlobalCatalogReady))) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", new object[] { context.Name }), typeof(GlobalCatalog), context.Name); } } catch (COMException exception) { if (exception.ErrorCode == -2147016646) { throw new ActiveDirectoryObjectNotFoundException(Res.GetString("GCNotFound", new object[] { context.Name }), typeof(GlobalCatalog), context.Name); } throw ExceptionHelper.GetExceptionFromCOMException(context, exception); } return(new GlobalCatalog(context, globalCatalogName, directoryEntryMgr)); }
internal ReplicationConnectionCollection GetOutboundConnectionsHelper() { // this is the first time that user tries to retrieve this property, so get it from the directory if (_outbound == null) { // search base is the site container string siteName = (this is DomainController) ? ((DomainController)this).SiteObjectName : ((AdamInstance)this).SiteObjectName; DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context), siteName); string serverName = (this is DomainController) ? ((DomainController)this).ServerObjectName : ((AdamInstance)this).ServerObjectName; ADSearcher adSearcher = new ADSearcher(de, "(&(objectClass=nTDSConnection)(objectCategory=nTDSConnection)(fromServer=CN=NTDS Settings," + serverName + "))", new string[] { "objectClass", "cn" }, SearchScope.Subtree); SearchResultCollection?results = null; DirectoryContext newContext = Utils.GetNewDirectoryContext(Name, DirectoryContextType.DirectoryServer, context); try { results = adSearcher.FindAll(); _outbound = new ReplicationConnectionCollection(); foreach (SearchResult result in results) { ReplicationConnection con = new ReplicationConnection(newContext, result.GetDirectoryEntry(), (string)result.Properties["cn"][0] !); _outbound.Add(con); } } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(newContext, e); } finally { results?.Dispose(); de.Dispose(); } } return(_outbound); }
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 { srchResults?.Dispose(); de.Dispose(); } } return(_inbound); }
protected override void OnRemoveComplete(int index, object value) { if (this.isForNC) { try { if (this.crossRefEntry != null) { string str = (value is DomainController) ? ((DomainController)value).NtdsaObjectName : ((AdamInstance)value).NtdsaObjectName; this.crossRefEntry.Properties[PropertyManager.MsDSNCReplicaLocations].Remove(str); } return; } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } } DirectoryServer server = (DirectoryServer)value; string name = server.Name; string dn = (server is DomainController) ? ((DomainController)server).ServerObjectName : ((AdamInstance)server).ServerObjectName; try { if (this.changeList.Contains(name)) { ((DirectoryEntry)this.changeList[name]).Properties["bridgeheadTransportList"].Clear(); } else { DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, dn); directoryEntry.Properties["bridgeheadTransportList"].Clear(); this.changeList.Add(name, directoryEntry); } } catch (COMException exception2) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2); } }
public void SeizeRoleOwnership(AdamRole role) { string dn = null; base.CheckIfDisposed(); switch (role) { case AdamRole.SchemaRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SchemaNamingContext); break; case AdamRole.NamingRole: dn = base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer); break; default: throw new InvalidEnumArgumentException("role", (int)role, typeof(AdamRole)); } DirectoryEntry directoryEntry = null; try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(base.context, dn); directoryEntry.Properties[PropertyManager.FsmoRoleOwner].Value = this.NtdsaObjectName; directoryEntry.CommitChanges(); } catch (COMException exception) { throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromCOMException(base.context, exception); } finally { if (directoryEntry != null) { directoryEntry.Dispose(); } } this.cachedRoles = null; }
private void InitializeCrossRef(string distinguishedName) { if (this.crossRefEntry == null) { DirectoryEntry directoryEntry = null; try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(Utils.GetNewDirectoryContext(this.GetNamingRoleOwner(), DirectoryContextType.DirectoryServer, base.context), WellKnownDN.PartitionsContainer); string name = "CN={" + Guid.NewGuid() + "}"; this.crossRefEntry = directoryEntry.Children.Add(name, "crossRef"); string adamHostNameAndPortsFromNTDSA = null; if (this.appType == ApplicationPartitionType.ADAMApplicationPartition) { DirectoryEntry cachedDirectoryEntry = base.directoryEntryMgr.GetCachedDirectoryEntry(WellKnownDN.RootDSE); string dn = (string)PropertyManager.GetPropertyValue(base.context, cachedDirectoryEntry, PropertyManager.DsServiceName); adamHostNameAndPortsFromNTDSA = Utils.GetAdamHostNameAndPortsFromNTDSA(base.context, dn); } else { adamHostNameAndPortsFromNTDSA = base.context.Name; } this.crossRefEntry.Properties[PropertyManager.DnsRoot].Value = adamHostNameAndPortsFromNTDSA; this.crossRefEntry.Properties[PropertyManager.Enabled].Value = false; this.crossRefEntry.Properties[PropertyManager.NCName].Value = distinguishedName; } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception); } finally { if (directoryEntry != null) { directoryEntry.Dispose(); } } } }
private System.DirectoryServices.ActiveDirectory.ForestMode GetForestMode() { System.DirectoryServices.ActiveDirectory.ForestMode mode; DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.RootDSE); try { if (!directoryEntry.Properties.Contains(PropertyManager.ForestFunctionality)) { return(System.DirectoryServices.ActiveDirectory.ForestMode.Windows2000Forest); } return((System.DirectoryServices.ActiveDirectory.ForestMode) int.Parse((string)directoryEntry.Properties[PropertyManager.ForestFunctionality].Value, NumberFormatInfo.InvariantInfo)); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } finally { directoryEntry.Dispose(); } return(mode); }
protected override void OnRemoveComplete(int index, object value) { ActiveDirectorySubnet subnet = (ActiveDirectorySubnet)value; string key = (string)PropertyManager.GetPropertyValue(subnet.context, subnet.cachedEntry, PropertyManager.DistinguishedName); try { if (this.changeList.Contains(key)) { ((DirectoryEntry)this.changeList[key]).Properties["siteObject"].Clear(); } else { DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, this.MakePath(key)); directoryEntry.Properties["siteObject"].Clear(); this.changeList.Add(key, directoryEntry); } } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } }
protected override void OnRemoveComplete(int index, object value) { ActiveDirectorySubnet subnet = (ActiveDirectorySubnet)value; string dn = (string)PropertyManager.GetPropertyValue(subnet.context, subnet.cachedEntry, PropertyManager.DistinguishedName); try { if (changeList.Contains(dn)) { ((DirectoryEntry)changeList[dn]).Properties["siteObject"].Clear(); } else { DirectoryEntry de = DirectoryEntryManager.GetDirectoryEntry(_context, MakePath(dn)); de.Properties["siteObject"].Clear(); changeList.Add(dn, de); } } catch (COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(_context, e); } }
public void Delete() { base.CheckIfDisposed(); if (!this.committed) { throw new InvalidOperationException(Res.GetString("CannotPerformOperationOnUncommittedObject")); } DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(base.context, base.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer)); try { this.GetCrossRefEntry(); directoryEntry.Children.Remove(this.crossRefEntry); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(base.context, exception); } finally { directoryEntry.Dispose(); } }
public void SeizeRoleOwnership(AdamRole role) { string str = null; base.CheckIfDisposed(); AdamRole adamRole = role; if (adamRole == AdamRole.SchemaRole) { str = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.SchemaNamingContext); } else if (adamRole == AdamRole.NamingRole) { str = this.directoryEntryMgr.ExpandWellKnownDN(WellKnownDN.PartitionsContainer); } else { throw new InvalidEnumArgumentException("role", (int)role, typeof(AdamRole)); } DirectoryEntry directoryEntry = null; using (directoryEntry) { try { directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, str); directoryEntry.Properties[PropertyManager.FsmoRoleOwner].Value = this.NtdsaObjectName; directoryEntry.CommitChanges(); } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } } this.cachedRoles = null; }
private static DirectoryEntry GetSearchRootEntry(Forest forest) { DirectoryEntry directoryEntry; DirectoryContext directoryContext = forest.GetDirectoryContext(); bool flag = false; bool flag1 = false; AuthenticationTypes defaultAuthType = Utils.DefaultAuthType; if (directoryContext.ContextType == DirectoryContextType.DirectoryServer) { flag = true; DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(directoryContext, WellKnownDN.RootDSE); string propertyValue = (string)PropertyManager.GetPropertyValue(directoryContext, directoryEntry1, PropertyManager.IsGlobalCatalogReady); flag1 = Utils.Compare(propertyValue, "TRUE") == 0; } if (!flag) { directoryEntry = new DirectoryEntry(string.Concat("GC://", forest.Name), directoryContext.UserName, directoryContext.Password, defaultAuthType); } else { if (DirectoryContext.ServerBindSupported) { defaultAuthType = defaultAuthType | AuthenticationTypes.ServerBind; } if (!flag1) { directoryEntry = new DirectoryEntry(string.Concat("LDAP://", directoryContext.GetServerName()), directoryContext.UserName, directoryContext.Password, defaultAuthType); } else { directoryEntry = new DirectoryEntry(string.Concat("GC://", directoryContext.GetServerName()), directoryContext.UserName, directoryContext.Password, defaultAuthType); } } return(directoryEntry); }
protected override void OnRemoveComplete(int index, object value) { ActiveDirectorySubnet activeDirectorySubnet = (ActiveDirectorySubnet)value; string propertyValue = (string)PropertyManager.GetPropertyValue(activeDirectorySubnet.context, activeDirectorySubnet.cachedEntry, PropertyManager.DistinguishedName); try { if (!this.changeList.Contains(propertyValue)) { DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, this.MakePath(propertyValue)); directoryEntry.Properties["siteObject"].Clear(); this.changeList.Add(propertyValue, directoryEntry); } else { ((DirectoryEntry)this.changeList[propertyValue]).Properties["siteObject"].Clear(); } } catch (COMException cOMException1) { COMException cOMException = cOMException1; throw ExceptionHelper.GetExceptionFromCOMException(this.context, cOMException); } }
public static ActiveDirectorySubnet FindByName(DirectoryContext context, string subnetName) { ValidateArgument(context, subnetName); // 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 subnetdn = "CN=Subnets,CN=Sites," + config; de = DirectoryEntryManager.GetDirectoryEntry(context, subnetdn); } 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(Res.GetString(Res.ADAMInstanceNotFoundInConfigSet, context.Name)); } try { ADSearcher adSearcher = new ADSearcher(de, "(&(objectClass=subnet)(objectCategory=subnet)(name=" + Utils.GetEscapedFilterValue(subnetName) + "))", 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 subnet object Exception e = new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySubnet), subnetName); throw e; } else { string siteName = null; DirectoryEntry connectionEntry = srchResult.GetDirectoryEntry(); // try to get the site that this subnet lives in if (connectionEntry.Properties.Contains("siteObject")) { NativeComInterfaces.IAdsPathname pathCracker = (NativeComInterfaces.IAdsPathname) new NativeComInterfaces.Pathname(); // need to turn off the escaping for name pathCracker.EscapedMode = NativeComInterfaces.ADS_ESCAPEDMODE_OFF_EX; string tmp = (string)connectionEntry.Properties["siteObject"][0]; // escaping manipulation pathCracker.Set(tmp, NativeComInterfaces.ADS_SETTYPE_DN); string rdn = pathCracker.Retrieve(NativeComInterfaces.ADS_FORMAT_LEAF); Debug.Assert(rdn != null && Utils.Compare(rdn, 0, 3, "CN=", 0, 3) == 0); siteName = rdn.Substring(3); } // it is an existing subnet object ActiveDirectorySubnet subnet = null; if (siteName == null) { subnet = new ActiveDirectorySubnet(context, subnetName, null, true); } else { subnet = new ActiveDirectorySubnet(context, subnetName, siteName, true); } subnet.cachedEntry = connectionEntry; return(subnet); } } catch (COMException e) { if (e.ErrorCode == unchecked ((int)0x80072030)) { // object is not found since we cannot even find the container in which to search throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.DSNotFound), typeof(ActiveDirectorySubnet), subnetName); } else { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } } finally { if (de != null) { de.Dispose(); } } }
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)); }
public void MoveToAnotherSite(string siteName) { CheckIfDisposed(); // validate siteName if (siteName == null) { throw new ArgumentNullException("siteName"); } if (siteName.Length == 0) { throw new ArgumentException(SR.EmptyStringParameter, "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 dn = (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; } } }
internal IntPtr GetReplicationInfoHelper(IntPtr dsHandle, int type, int secondaryType, string partition, ref bool advanced, int context, LoadLibrarySafeHandle libHandle) { IntPtr info = (IntPtr)0; int result = 0; bool needToTryAgain = true; IntPtr functionPtr; // first try to use the DsReplicaGetInfo2W API which does not exist on win2k machine // call DsReplicaGetInfo2W functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfo2W"); if (functionPtr == (IntPtr)0) { // a win2k machine which does not have it. functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaGetInfoW dsReplicaGetInfoW = (UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaGetInfoW)); result = dsReplicaGetInfoW(dsHandle, secondaryType, partition, (IntPtr)0, ref info); advanced = false; needToTryAgain = false; } else { UnsafeNativeMethods.DsReplicaGetInfo2W dsReplicaGetInfo2W = (UnsafeNativeMethods.DsReplicaGetInfo2W)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaGetInfo2W)); result = dsReplicaGetInfo2W(dsHandle, type, partition, (IntPtr)0, null, null, 0, context, ref info); } // check the result if (needToTryAgain && result == DS_REPL_NOTSUPPORTED) { // this is the case that client is xp/win2k3, dc is win2k functionPtr = UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (functionPtr == (IntPtr)0) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } UnsafeNativeMethods.DsReplicaGetInfoW dsReplicaGetInfoW = (UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(UnsafeNativeMethods.DsReplicaGetInfoW)); result = dsReplicaGetInfoW(dsHandle, secondaryType, partition, (IntPtr)0, ref info); advanced = false; } if (result != 0) { if (partition != null) { // this is the case of meta data if (type == (int)DS_REPL_INFO_TYPE.DS_REPL_INFO_METADATA_2_FOR_OBJ) { if (result == ExceptionHelper.ERROR_DS_DRA_BAD_DN || result == ExceptionHelper.ERROR_DS_NAME_UNPARSEABLE) { throw new ArgumentException(ExceptionHelper.GetErrorMessage(result, false), "objectPath"); } DirectoryEntry verifyEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, partition); try { verifyEntry.RefreshCache(new string[] { "name" }); } catch (COMException e) { if (e.ErrorCode == unchecked ((int)0x80072020) | // dir_error on server side e.ErrorCode == unchecked ((int)0x80072030)) // object not exists { throw new ArgumentException(SR.DSNoObject, "objectPath"); } else if (e.ErrorCode == unchecked ((int)0x80005000) | // bad path name e.ErrorCode == unchecked ((int)0x80072032)) // ERROR_DS_INVALID_DN_SYNTAX { throw new ArgumentException(SR.DSInvalidPath, "objectPath"); } } } else { if (!Partitions.Contains(partition)) { throw new ArgumentException(SR.ServerNotAReplica, "partition"); } } } throw ExceptionHelper.GetExceptionFromErrorCode(result, Name); } return(info); }
public DirectoryEntry GetDirectoryEntry() { CheckIfDisposed(); return(DirectoryEntryManager.GetDirectoryEntry(_context, WellKnownDN.ConfigurationNamingContext)); }
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 = null; 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(String.Format(CultureInfo.CurrentCulture, 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["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(); } }
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(String.Format(CultureInfo.CurrentCulture, 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(); } }
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(Res.GetString(Res.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(Res.GetString(Res.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(Res.GetString(Res.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(Res.GetString(Res.NotSupportTransportSMTP)); } throw new ActiveDirectoryObjectNotFoundException(Res.GetString(Res.TransportNotFound, transport.ToString()), typeof(ActiveDirectoryInterSiteTransport), transport.ToString()); } else { throw ExceptionHelper.GetExceptionFromCOMException(context, e); } } return(new ActiveDirectoryInterSiteTransport(context, transport, de)); }
public override DirectoryEntry GetDirectoryEntry() { base.CheckIfDisposed(); return(DirectoryEntryManager.GetDirectoryEntry(this.context, base.Name)); }
internal static DirectoryEntry GetDirectoryEntry(DirectoryContext context, WellKnownDN dn) { return(DirectoryEntryManager.GetDirectoryEntry(context, DirectoryEntryManager.ExpandWellKnownDN(context, dn))); }
public override DirectoryEntry GetDirectoryEntry() { CheckIfDisposed(); return(DirectoryEntryManager.GetDirectoryEntry(context, Name)); }
internal ActiveDirectorySchema(DirectoryContext context, string distinguishedName, DirectoryEntryManager directoryEntryMgr) : base(context, distinguishedName) { this.directoryEntryMgr = directoryEntryMgr; _schemaEntry = DirectoryEntryManager.GetDirectoryEntry(context, distinguishedName); }
public void Save() { this.CheckIfDisposed(); if (!this.isBound) { try { if (this.schemaEntry == null) { this.schemaEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, WellKnownDN.SchemaNamingContext); } string escapedPath = Utils.GetEscapedPath("CN=" + this.commonName); this.classEntry = this.schemaEntry.Children.Add(escapedPath, "classSchema"); } catch (COMException exception) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception); } catch (ActiveDirectoryObjectNotFoundException) { throw new ActiveDirectoryOperationException(Res.GetString("ADAMInstanceNotFoundInConfigSet", new object[] { this.context.Name })); } this.SetProperty(PropertyManager.LdapDisplayName, this.ldapDisplayName); this.SetProperty(PropertyManager.GovernsID, this.oid); this.SetProperty(PropertyManager.Description, this.description); if (this.possibleSuperiors != null) { this.classEntry.Properties[PropertyManager.PossibleSuperiors].AddRange(this.possibleSuperiors.GetMultiValuedProperty()); } if (this.mandatoryProperties != null) { this.classEntry.Properties[PropertyManager.MustContain].AddRange(this.mandatoryProperties.GetMultiValuedProperty()); } if (this.optionalProperties != null) { this.classEntry.Properties[PropertyManager.MayContain].AddRange(this.optionalProperties.GetMultiValuedProperty()); } if (this.subClassOf != null) { this.SetProperty(PropertyManager.SubClassOf, this.subClassOf.Name); } else { this.SetProperty(PropertyManager.SubClassOf, "top"); } this.SetProperty(PropertyManager.ObjectClassCategory, this.type); if (this.schemaGuidBinaryForm != null) { this.SetProperty(PropertyManager.SchemaIDGuid, this.schemaGuidBinaryForm); } if (this.defaultSDSddlForm != null) { this.SetProperty(PropertyManager.DefaultSecurityDescriptor, this.defaultSDSddlForm); } } try { this.classEntry.CommitChanges(); if (this.schema == null) { ActiveDirectorySchema schema = ActiveDirectorySchema.GetSchema(this.context); bool flag = false; DirectoryServer schemaRoleOwner = null; try { schemaRoleOwner = schema.SchemaRoleOwner; if (Utils.Compare(schemaRoleOwner.Name, this.context.GetServerName()) != 0) { DirectoryContext context = Utils.GetNewDirectoryContext(schemaRoleOwner.Name, DirectoryContextType.DirectoryServer, this.context); this.schema = ActiveDirectorySchema.GetSchema(context); } else { flag = true; this.schema = schema; } } finally { if (schemaRoleOwner != null) { schemaRoleOwner.Dispose(); } if (!flag) { schema.Dispose(); } } } this.schema.RefreshSchema(); } catch (COMException exception2) { throw ExceptionHelper.GetExceptionFromCOMException(this.context, exception2); } this.isDefunctOnServer = this.isDefunct; this.commonName = null; this.oid = null; this.description = null; this.descriptionInitialized = false; this.possibleSuperiors = null; this.auxiliaryClasses = null; this.possibleInferiors = null; this.mandatoryProperties = null; this.optionalProperties = null; this.subClassOf = null; this.typeInitialized = false; this.schemaGuidBinaryForm = null; this.defaultSDSddlForm = null; this.defaultSDSddlFormInitialized = false; this.propertiesFromSchemaContainerInitialized = false; this.isBound = true; }
internal static string ExpandWellKnownDN(DirectoryContext context, WellKnownDN dn) { string propertyValue = null; WellKnownDN wellKnownDN = dn; if (wellKnownDN == WellKnownDN.RootDSE) { propertyValue = "RootDSE"; } else if (wellKnownDN == WellKnownDN.DefaultNamingContext) { DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(context, "RootDSE"); try { propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry, PropertyManager.DefaultNamingContext); } finally { directoryEntry.Dispose(); } } else if (wellKnownDN == WellKnownDN.SchemaNamingContext) { DirectoryEntry directoryEntry1 = DirectoryEntryManager.GetDirectoryEntry(context, "RootDSE"); try { propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry1, PropertyManager.SchemaNamingContext); } finally { directoryEntry1.Dispose(); } } else if (wellKnownDN == WellKnownDN.ConfigurationNamingContext) { DirectoryEntry directoryEntry2 = DirectoryEntryManager.GetDirectoryEntry(context, "RootDSE"); try { propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry2, PropertyManager.ConfigurationNamingContext); } finally { directoryEntry2.Dispose(); } } else if (wellKnownDN == WellKnownDN.PartitionsContainer) { propertyValue = string.Concat("CN=Partitions,", DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext)); } else if (wellKnownDN == WellKnownDN.SitesContainer) { propertyValue = string.Concat("CN=Sites,", DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.ConfigurationNamingContext)); } else if (wellKnownDN == WellKnownDN.SystemContainer) { propertyValue = string.Concat("CN=System,", DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.DefaultNamingContext)); } else if (wellKnownDN == WellKnownDN.RidManager) { propertyValue = string.Concat("CN=RID Manager$,", DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.SystemContainer)); } else if (wellKnownDN == WellKnownDN.Infrastructure) { propertyValue = string.Concat("CN=Infrastructure,", DirectoryEntryManager.ExpandWellKnownDN(context, WellKnownDN.DefaultNamingContext)); } else if (wellKnownDN == WellKnownDN.RootDomainNamingContext) { DirectoryEntry directoryEntry3 = DirectoryEntryManager.GetDirectoryEntry(context, "RootDSE"); try { propertyValue = (string)PropertyManager.GetPropertyValue(context, directoryEntry3, PropertyManager.RootDomainNamingContext); } finally { directoryEntry3.Dispose(); } } else { throw new InvalidEnumArgumentException("dn", (int)dn, typeof(WellKnownDN)); } return(propertyValue); }
internal IntPtr GetReplicationInfoHelper(IntPtr dsHandle, int type, int secondaryType, string partition, ref bool advanced, int context, LoadLibrarySafeHandle libHandle) { IntPtr zero = IntPtr.Zero; int errorCode = 0; bool flag = true; IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfo2W"); if (procAddress == IntPtr.Zero) { procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (procAddress == IntPtr.Zero) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfoW delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfoW)); errorCode = delegateForFunctionPointer(dsHandle, secondaryType, partition, IntPtr.Zero, ref zero); advanced = false; flag = false; } else { System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfo2W infow = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfo2W)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfo2W)); errorCode = infow(dsHandle, type, partition, IntPtr.Zero, null, null, 0, context, ref zero); } if (flag && (errorCode == 50)) { procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(libHandle, "DsReplicaGetInfoW"); if (procAddress == IntPtr.Zero) { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfoW ow2 = (System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfoW)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.DsReplicaGetInfoW)); errorCode = ow2(dsHandle, secondaryType, partition, IntPtr.Zero, ref zero); advanced = false; } if (errorCode == 0) { return(zero); } if (partition != null) { if (type == 9) { if ((errorCode == ExceptionHelper.ERROR_DS_DRA_BAD_DN) || (errorCode == ExceptionHelper.ERROR_DS_NAME_UNPARSEABLE)) { throw new ArgumentException(ExceptionHelper.GetErrorMessage(errorCode, false), "objectPath"); } DirectoryEntry directoryEntry = DirectoryEntryManager.GetDirectoryEntry(this.context, partition); try { directoryEntry.RefreshCache(new string[] { "name" }); } catch (COMException exception) { if ((exception.ErrorCode == -2147016672) | (exception.ErrorCode == -2147016656)) { throw new ArgumentException(Res.GetString("DSNoObject"), "objectPath"); } if ((exception.ErrorCode == -2147463168) | (exception.ErrorCode == -2147016654)) { throw new ArgumentException(Res.GetString("DSInvalidPath"), "objectPath"); } } } else if (!this.Partitions.Contains(partition)) { throw new ArgumentException(Res.GetString("ServerNotAReplica"), "partition"); } } throw ExceptionHelper.GetExceptionFromErrorCode(errorCode, this.Name); }