mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
06.12.2025 d7e652c1013c88be63967d2c849bd066f38cdd0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2010 Sun Microsystems, Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */
package org.opends.server.replication.protocol;
 
import static org.opends.server.replication.protocol.OperationContext.*;
import static org.opends.server.replication.protocol.ProtocolVersion.*;
 
import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;
 
import org.forgerock.i18n.LocalizedIllegalArgumentException;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.RDN;
import org.opends.server.core.ModifyDNOperation;
import org.opends.server.core.ModifyDNOperationBasis;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.replication.common.CSN;
import org.opends.server.types.LDAPException;
import org.opends.server.types.Modification;
import org.opends.server.types.operation.PostOperationModifyDNOperation;
 
/** Message used to send Modify DN information. */
public class ModifyDNMsg extends ModifyCommonMsg
{
  private String newRDN;
  private String newSuperior;
  private boolean deleteOldRdn;
  private String newSuperiorEntryUUID;
 
  /**
   * Construct a new Modify DN message.
   *
   * @param operation The operation to use for building the message
   */
  public ModifyDNMsg(PostOperationModifyDNOperation operation)
  {
    super((OperationContext) operation.getAttachment(SYNCHROCONTEXT),
        operation.getEntryDN());
 
    encodedMods = encodeMods(operation.getModifications());
 
    ModifyDnContext ctx =
      (ModifyDnContext) operation.getAttachment(SYNCHROCONTEXT);
    newSuperiorEntryUUID = ctx.getNewSuperiorEntryUUID();
 
    deleteOldRdn = operation.deleteOldRDN();
    final ByteString rawNewSuperior = operation.getRawNewSuperior();
    newSuperior = rawNewSuperior != null ? rawNewSuperior.toString() : null;
    newRDN = operation.getRawNewRDN().toString();
  }
 
  /**
   * Construct a new Modify DN message (no mods).
   * Note: Keep this constructor version to support already written tests, not
   * using mods.
   *
   * @param dn The dn to use for building the message.
   * @param csn The CSN to use for building the message.
   * @param entryUUID          The unique id to use for building the message.
   * @param newSuperiorEntryUUID The new parent unique id to use for building
   *                     the message.
   * @param deleteOldRdn boolean indicating if old rdn must be deleted to use
   *                     for building the message.
   * @param newSuperior  The new Superior entry to use for building the message.
   * @param newRDN       The new Rdn to use for building the message.
   */
  public ModifyDNMsg(DN dn, CSN csn, String entryUUID,
                     String newSuperiorEntryUUID, boolean deleteOldRdn,
                     String newSuperior, String newRDN)
  {
    super(new ModifyDnContext(csn, entryUUID, newSuperiorEntryUUID), dn);
 
    this.newSuperiorEntryUUID = newSuperiorEntryUUID;
    this.deleteOldRdn = deleteOldRdn;
    this.newSuperior = newSuperior;
    this.newRDN = newRDN;
  }
 
  /**
   * Construct a new Modify DN message (with mods).
   *
   * @param dn The dn to use for building the message.
   * @param csn The CSNto use for building the message.
   * @param entryUUID The unique id to use for building the message.
   * @param newSuperiorEntryUUID The new parent unique id to use for building
   *                     the message.
   * @param deleteOldRdn boolean indicating if old rdn must be deleted to use
   *                     for building the message.
   * @param newSuperior  The new Superior entry to use for building the message.
   * @param newRDN       The new Rdn to use for building the message.
   * @param mods         The mod of the operation.
   */
  public ModifyDNMsg(DN dn, CSN csn, String entryUUID,
      String newSuperiorEntryUUID, boolean deleteOldRdn, String newSuperior,
      String newRDN, List<Modification> mods)
  {
    this(dn, csn, entryUUID, newSuperiorEntryUUID, deleteOldRdn,
        newSuperior, newRDN);
    this.encodedMods = encodeMods(mods);
  }
 
  /**
   * Creates a new ModifyDN message from a byte[].
   *
   * @param in The byte[] from which the operation must be read.
   * @throws DataFormatException The input byte[] is not a valid ModifyDNMsg.
   */
  ModifyDNMsg(byte[] in) throws DataFormatException
  {
    final ByteArrayScanner scanner = new ByteArrayScanner(in);
    decodeHeader(scanner, MSG_TYPE_MODIFYDN, MSG_TYPE_MODIFYDN_V1);
 
    if (protocolVersion <= 3)
    {
      decodeBody_V123(scanner, in[0]);
    }
    else
    {
      decodeBody_V4(scanner);
    }
 
    if (protocolVersion==ProtocolVersion.getCurrentVersion())
    {
      bytes = in;
    }
  }
 
  @Override
  public ModifyDNOperation createOperation(InternalClientConnection connection,
      DN newDN) throws LDAPException, IOException
  {
    ModifyDNOperation moddn =  new ModifyDNOperationBasis(connection,
        InternalClientConnection.nextOperationID(),
        InternalClientConnection.nextMessageID(), null,
        ByteString.valueOfUtf8(newDN.toString()),
        ByteString.valueOfUtf8(newRDN),
        deleteOldRdn,
        (newSuperior == null ? null : ByteString.valueOfUtf8(newSuperior)));
 
    for (Modification mod : decodeMods(encodedMods))
    {
      moddn.addModification(mod);
    }
 
    ModifyDnContext ctx = new ModifyDnContext(getCSN(), getEntryUUID(),
        newSuperiorEntryUUID);
    moddn.setAttachment(SYNCHROCONTEXT, ctx);
    return moddn;
  }
 
  // ============
  // Msg Encoding
  // ============
 
  @Override
  public byte[] getBytes_V1()
  {
    final ByteArrayBuilder builder = encodeHeader_V1(MSG_TYPE_MODIFYDN_V1);
    builder.appendString(newRDN);
    builder.appendString(newSuperior);
    builder.appendString(newSuperiorEntryUUID);
    builder.appendBoolean(deleteOldRdn);
    return builder.toByteArray();
  }
 
  @Override
  public byte[] getBytes_V23()
  {
    final ByteArrayBuilder builder = encodeHeader(MSG_TYPE_MODIFYDN, REPLICATION_PROTOCOL_V3);
    builder.appendString(newRDN);
    builder.appendString(newSuperior);
    builder.appendString(newSuperiorEntryUUID);
    builder.appendBoolean(deleteOldRdn);
    builder.appendZeroTerminatedByteArray(encodedMods);
    return builder.toByteArray();
  }
 
  @Override
  public byte[] getBytes_V45(short protocolVersion)
  {
    final ByteArrayBuilder builder = encodeHeader(MSG_TYPE_MODIFYDN, protocolVersion);
    builder.appendString(newRDN);
    builder.appendString(newSuperior);
    builder.appendString(newSuperiorEntryUUID);
    builder.appendBoolean(deleteOldRdn);
    builder.appendIntUTF8(encodedMods.length);
    builder.appendZeroTerminatedByteArray(encodedMods);
    builder.appendIntUTF8(encodedEclIncludes.length);
    builder.appendZeroTerminatedByteArray(encodedEclIncludes);
    return builder.toByteArray();
  }
 
  // ============
  // Msg decoding
  // ============
 
  private void decodeBody_V123(ByteArrayScanner scanner, byte msgType)
      throws DataFormatException
  {
    newRDN = scanner.nextString();
    newSuperior = scanner.nextString();
    newSuperiorEntryUUID = scanner.nextString();
    deleteOldRdn = scanner.nextBoolean();
 
    // For easiness (no additional method), simply compare PDU type to
    // know if we have to read the mods of V2
    if (msgType == MSG_TYPE_MODIFYDN)
    {
      encodedMods = scanner.remainingBytesZeroTerminated();
    }
  }
 
  private void decodeBody_V4(ByteArrayScanner scanner)
      throws DataFormatException
  {
    newRDN = scanner.nextString();
    newSuperior = scanner.nextString();
    newSuperiorEntryUUID = scanner.nextString();
    deleteOldRdn = scanner.nextBoolean();
 
    final int modsLen = scanner.nextIntUTF8();
    encodedMods = scanner.nextByteArray(modsLen);
    scanner.skipZeroSeparator();
 
    final int eclAttrLen = scanner.nextIntUTF8();
    encodedEclIncludes = scanner.nextByteArray(eclAttrLen);
  }
 
  @Override
  public String toString()
  {
    if (protocolVersion >= REPLICATION_PROTOCOL_V1)
    {
      return "ModifyDNMsg content: " +
        " protocolVersion: " + protocolVersion +
        " dn: " + dn +
        " csn: " + csn +
        " uniqueId: " + entryUUID +
        " newRDN: " + newRDN +
        " newSuperior: " + newSuperior +
        " deleteOldRdn: " + deleteOldRdn +
        " assuredFlag: " + assuredFlag +
        (protocolVersion >= REPLICATION_PROTOCOL_V2 ?
          " assuredMode: " + assuredMode +
          " safeDataLevel: " + safeDataLevel
          : "");
    }
    return "!!! Unknown version: " + protocolVersion + "!!!";
  }
 
  /**
   * Set the new superior.
   * @param string the new superior.
   */
  public void setNewSuperior(String string)
  {
    newSuperior = string;
  }
 
  /**
   * Get the new superior.
   *
   * @return The new superior.
   */
  public String getNewSuperior()
  {
    return newSuperior;
  }
 
  /**
   * Get the new superior id.
   *
   * @return The new superior id.
   */
  public String getNewSuperiorEntryUUID()
  {
    return newSuperiorEntryUUID;
  }
 
  /**
   * Get the delete old rdn option.
   *
   * @return The delete old rdn option.
   */
  public boolean deleteOldRdn()
  {
    return deleteOldRdn;
  }
 
  /**
   * Set the new superior id.
   *
   * @param newSup The new superior id.
   */
  public void setNewSuperiorEntryUUID(String newSup)
  {
    newSuperiorEntryUUID = newSup;
  }
 
  /**
   * Set the delete old rdn option.
   *
   * @param delete The delete old rdn option.
   */
  public void  setDeleteOldRdn(boolean delete)
  {
    deleteOldRdn = delete;
  }
 
  /**
   * Get the delete old rdn option.
   * @return true if delete old rdn option
   */
  public boolean getDeleteOldRdn()
  {
    return deleteOldRdn;
  }
 
  /**
   * Get the new RDN of this operation.
   *
   * @return The new RDN of this operation.
   */
  public String getNewRDN()
  {
    return newRDN;
  }
 
  /**
   * Set the new RDN of this operation.
   * @param newRDN the new RDN of this operation.
   */
  public void setNewRDN(String newRDN)
  {
    this.newRDN = newRDN;
  }
 
  /**
   * Computes and return the new DN that the entry should have after this operation.
   *
   * @return the newDN.
   * @throws LocalizedIllegalArgumentException
   *           in case of decoding problems.
   */
  private DN computeNewDN() throws LocalizedIllegalArgumentException
  {
    if (newSuperior != null)
    {
      return DN.valueOf(newRDN + "," + newSuperior);
    }
    final DN parentDn = getDN().parent();
    return parentDn.child(RDN.valueOf(newRDN));
  }
 
  /**
   * Check if this MSG will change the DN of the target entry to be
   * the same as the dn given as a parameter.
   * @param targetDn the DN to use when checking if this MSG will change
   *                 the DN of the entry to a given DN.
   * @return A boolean indicating if the modify DN MSG will change the DN of
   *         the target entry to be the same as the dn given as a parameter.
   */
  public boolean newDNIsParent(DN targetDn)
  {
    try
    {
      DN newDN = computeNewDN();
      return newDN.isSuperiorOrEqualTo(targetDn);
    }
    catch (LocalizedIllegalArgumentException e)
    {
      // The DN was not a correct DN, and therefore does not a parent of the
      // DN given as a parameter.
      return false;
    }
  }
 
  /**
   * Check if the new dn of this ModifyDNMsg is the same as the targetDN
   * given in parameter.
   *
   * @param targetDN The targetDN to use to check for equality.
   *
   * @return A boolean indicating if the targetDN if the same as the new DN of
   *         the ModifyDNMsg.
   */
  public boolean newDNIsEqual(DN targetDN)
  {
    try
    {
      DN newDN = computeNewDN();
      return newDN.equals(targetDN);
    }
    catch (LocalizedIllegalArgumentException e)
    {
      // The DN was not a correct DN, and therefore does not match the
      // DN given as a parameter.
      return false;
    }
  }
 
  /**
   * Check if the new parent of the modifyDNMsg is the same as the targetDN
   * given in parameter.
   *
   * @param targetDN the targetDN to use when checking equality.
   *
   * @return A boolean indicating if the new parent of the modifyDNMsg is the
   *         same as the targetDN.
   */
  public boolean newParentIsEqual(DN targetDN)
  {
    try
    {
      DN newSuperiorDN = newSuperior != null ? DN.valueOf(newSuperior) : DN.rootDN();
      return newSuperiorDN.equals(targetDN);
    }
    catch (LocalizedIllegalArgumentException e)
    {
      // The newsuperior was not a correct DN, and therefore does not match the
      // DN given as a parameter.
      return false;
    }
  }
 
  @Override
  public int size()
  {
    return encodedMods.length + newRDN.length() +
      encodedEclIncludes.length + headerSize();
  }
}