/// <summary> /// Creates a ModifyDnResponse packet. /// </summary> /// <param name="context">The user context which contains message ID.</param> /// <param name="resultCode">Result code of previous request, as specified in RFC 2251.</param> /// <param name="matchedDn">Matched DN.</param> /// <param name="errorMessage">Error message for result code. Required.</param> /// <param name="referral">Referral. Optional. Used for LDAP v3 only.</param> /// <returns>The packet that contains the response.</returns> internal override AdtsModifyDnResponsePacket CreateModifyDnResponse( AdtsLdapContext context, MsLdap.ResultCode resultCode, string matchedDn, string errorMessage, string[] referral) { ModifyDNResponse modifyDnResponse = new ModifyDNResponse( new LDAPResult_resultCode((long)resultCode), new LDAPDN(matchedDn ?? string.Empty), new LDAPString(errorMessage ?? string.Empty), CreateReferral(referral)); LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp(); operation.SetData(LDAPMessage_protocolOp.modDNResponse, modifyDnResponse); LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null); AdtsModifyDnResponsePacket packet = new AdtsModifyDnResponsePacket(); packet.ldapMessagev3 = message; packet.messageId = context.MessageId; return(packet); }
/// <summary> /// Moves and / or renames an entry in the directory. /// </summary> /// <param name="distinguishedName">The distinguished name of the entry to move or rename.</param> /// <param name="newParentDistinguishedName">The distinguished name of the entry's new parent entry in the directory (if moving), or its current parent entry (if renaming).</param> /// <param name="newCommonName">The new common name of entry.</param> /// <returns>True if moved or renamed, false otherwise.</returns> public bool MoveRenameEntry(string distinguishedName, string newParentDistinguishedName, string newCommonName) { if (!string.IsNullOrWhiteSpace(distinguishedName) && !string.IsNullOrWhiteSpace(newParentDistinguishedName) && !string.IsNullOrWhiteSpace(newCommonName)) { // Prepend the CN= if not already included. if (!newCommonName.StartsWith("CN=")) { newCommonName = "CN=" + newCommonName; } ModifyDNRequest request = new ModifyDNRequest(distinguishedName, newParentDistinguishedName, newCommonName); try { ModifyDNResponse response = (ModifyDNResponse)connection.SendRequest(request); // Check that a response was received. if (response != null) { // A response was received. if (response.ResultCode == ResultCode.Success) { return(true); } } else { // A response was not received. return(false); } } catch { } } return(false); }
public void TestMoveAndRenameUser() { using (LdapConnection connection = GetConnection()) { string ouName1 = "ProtocolsGroup7.1"; string dn1 = "ou=" + ouName1; string ouName2 = "ProtocolsGroup7.2"; string dn2 = "ou=" + ouName2; string userDn1 = "cn=protocolUser7.1" + "," + dn1; string userDn2 = "cn=protocolUser7.2" + "," + dn2; DeleteEntry(connection, userDn1); DeleteEntry(connection, userDn2); DeleteEntry(connection, dn1); DeleteEntry(connection, dn2); try { AddOrganizationalUnit(connection, dn1); SearchResultEntry sre = SearchOrganizationalUnit(connection, LdapConfiguration.Configuration.Domain, ouName1); Assert.NotNull(sre); AddOrganizationalUnit(connection, dn2); sre = SearchOrganizationalUnit(connection, LdapConfiguration.Configuration.Domain, ouName2); Assert.NotNull(sre); AddOrganizationalRole(connection, userDn1); string user1Root = dn1 + "," + LdapConfiguration.Configuration.Domain; string user2Root = dn2 + "," + LdapConfiguration.Configuration.Domain; sre = SearchUser(connection, user1Root, "protocolUser7.1"); Assert.NotNull(sre); ModifyDNRequest modDnRequest = new ModifyDNRequest(userDn1 + "," + LdapConfiguration.Configuration.Domain, dn2 + "," + LdapConfiguration.Configuration.Domain, "cn=protocolUser7.2"); ModifyDNResponse modDnResponse = (ModifyDNResponse)connection.SendRequest(modDnRequest); Assert.Equal(ResultCode.Success, modDnResponse.ResultCode); sre = SearchUser(connection, user1Root, "protocolUser7.1"); Assert.Null(sre); sre = SearchUser(connection, user2Root, "protocolUser7.2"); Assert.NotNull(sre); } finally { DeleteEntry(connection, userDn1); DeleteEntry(connection, userDn2); DeleteEntry(connection, dn1); DeleteEntry(connection, dn2); } } }
public void DRSR_DRSInterDomainMove_LDAP_Move_Object_From_Child_To_Parent() { DrsrTestChecker.Check(); EnvironmentConfig.Machine parentDcType = EnvironmentConfig.Machine.WritableDC1; DsServer parentDc = (DsServer)EnvironmentConfig.MachineStore[parentDcType]; EnvironmentConfig.Machine childDcType = EnvironmentConfig.Machine.CDC; DsServer childDc = (DsServer)EnvironmentConfig.MachineStore[childDcType]; DsUser user = EnvironmentConfig.UserStore[EnvironmentConfig.User.ParentDomainAdmin]; string srcObjDn = ldapAdapter.TestAddUserObj(childDc); BaseTestSite.Log.Add(LogEntryKind.Comment, "LDAP add a new object: {0}.", srcObjDn); string newObjRdn = DrsrHelper.GetRDNFromDN(srcObjDn); string tgtParentObjDn = "CN=Users," + DrsrHelper.GetNamingContextDN(parentDc.Domain, NamingContext.DomainNC); string newObjDn = newObjRdn + "," + tgtParentObjDn; bool bNewObjAdded = ldapAdapter.IsObjectExist(parentDc, newObjDn); if (bNewObjAdded) { ResultCode rCode = ldapAdapter.DeleteObject(parentDc, newObjDn); BaseTestSite.Assume.AreEqual <ResultCode>(ResultCode.Success, rCode, "LDAP: {0} should be removed.", newObjDn); } using (LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(childDc.DnsHostName))) // connecting to the child domain in which the object exists { connection.Credential = new System.Net.NetworkCredential(user.Username, user.Password, parentDc.Domain.DNSName); connection.SessionOptions.ProtocolVersion = 3; connection.SessionOptions.SspiFlag = connection.SessionOptions.SspiFlag | 1; //Set Delegate flag. connection.AuthType = AuthType.Kerberos; connection.Bind(); try { BaseTestSite.Log.Add(LogEntryKind.Comment, "LDAP ModifyDN: moving object {0} to {1}.", srcObjDn, newObjDn); ModifyDNRequest modDnRequest = new ModifyDNRequest(srcObjDn, tgtParentObjDn, newObjRdn); modDnRequest.Controls.Add(new CrossDomainMoveControl(parentDc.DnsHostName)); // parent domain to which the object need to move ModifyDNResponse modDnResponse = (ModifyDNResponse)connection.SendRequest(modDnRequest); BaseTestSite.Assert.AreEqual <ResultCode>(ResultCode.Success, modDnResponse.ResultCode, "LDAP ModifyDN: server should invoke IDL_DRSInterDomainMove on target Dc and move the object."); bool bOldObjDeleted = ldapAdapter.IsObjectExist(childDc, srcObjDn); bNewObjAdded = ldapAdapter.IsObjectExist(parentDc, newObjDn); BaseTestSite.Assert.IsFalse(bOldObjDeleted, "LDAP ModifyDN: the old object should be deleted from source DC."); BaseTestSite.Assert.IsTrue(bNewObjAdded, "LDAP ModifyDN: the new object should be added into target DC."); } catch (DirectoryOperationException e) { BaseTestSite.Log.Add(LogEntryKind.Debug, "LDAP ModifyDN: ResultCode:{0}", e.Response.ResultCode.ToString()); BaseTestSite.Log.Add(LogEntryKind.Debug, "LDAP ModifyDN: ErrorMessage:{0}", e.Response.ErrorMessage.ToString()); BaseTestSite.Assert.AreEqual <ResultCode>(ResultCode.Success, e.Response.ResultCode, "LDAP ModifyDN: server should invoke the IDL_DRSInterDomainMove on target Dc and move the object."); } } }
public override LdapResultCompleteStatus Handle(SafeHandle handle, Native.Native.LdapResultType resType, IntPtr msg, out DirectoryResponse response) { response = default; switch (resType) { case LdapForNet.Native.Native.LdapResultType.LDAP_RES_MODDN: response = new ModifyDNResponse(); return(LdapResultCompleteStatus.Complete); default: return(LdapResultCompleteStatus.Unknown); } }
/// <summary> /// Creates a ModifyDnResponse packet. /// </summary> /// <param name="context">The user context which contains message ID.</param> /// <param name="resultCode">Result code of previous request, as specified in RFC 2251.</param> /// <param name="matchedDn">Matched DN.</param> /// <param name="errorMessage">Error message for result code. Required.</param> /// <param name="referral">Referral. Optional. Used for LDAP v3 only.</param> /// <returns>The packet that contains the response.</returns> internal override AdtsModifyDnResponsePacket CreateModifyDnResponse( AdtsLdapContext context, MsLdap.ResultCode resultCode, string matchedDn, string errorMessage, string[] referral) { ModifyDNResponse modifyDnResponse = new ModifyDNResponse( new LDAPResult_resultCode((long)resultCode), new LDAPDN(matchedDn ?? string.Empty), new LDAPString(errorMessage ?? string.Empty), CreateReferral(referral)); LDAPMessage_protocolOp operation = new LDAPMessage_protocolOp(); operation.SetData(LDAPMessage_protocolOp.modDNResponse, modifyDnResponse); LDAPMessage message = new LDAPMessage(new MessageID(context.MessageId), operation, null); AdtsModifyDnResponsePacket packet = new AdtsModifyDnResponsePacket(); packet.ldapMessagev3 = message; packet.messageId = context.MessageId; return packet; }