urp_tombstone.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * urp_tombstone.c - Update Resolution Procedures - Tombstones
  8. */
  9. #include "slapi-plugin.h"
  10. #include "repl5.h"
  11. #include "urp.h"
  12. extern int slapi_log_urp;
  13. /*
  14. * Check if the entry is a tombstone.
  15. */
  16. int
  17. is_tombstone_entry(const Slapi_Entry* entry)
  18. {
  19. int flag;
  20. /* LP: This doesn't work very well with entries that we tombstone ourself */
  21. flag = slapi_entry_flag_is_set (entry, SLAPI_ENTRY_FLAG_TOMBSTONE);
  22. if (flag == 0)
  23. {
  24. /* This is slow */
  25. flag = slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE);
  26. }
  27. return flag;
  28. }
  29. PRBool
  30. get_tombstone_csn(const Slapi_Entry *entry, const CSN **delcsn)
  31. {
  32. PRBool ists = PR_FALSE;
  33. if (is_tombstone_entry(entry)) {
  34. ists = PR_TRUE;
  35. *delcsn = _get_deletion_csn((Slapi_Entry *)entry); /* cast away const */
  36. }
  37. return ists;
  38. }
  39. static int
  40. tombstone_to_glue_resolve_parent (
  41. Slapi_PBlock *pb,
  42. const char *sessionid,
  43. const Slapi_DN *parentdn,
  44. const char *parentuniqueid,
  45. CSN *opcsn)
  46. {
  47. /* Let's have a look at the parent of this entry... */
  48. if(!slapi_sdn_isempty(parentdn) && parentuniqueid!=NULL)
  49. {
  50. int op_result;
  51. Slapi_PBlock *newpb= slapi_pblock_new();
  52. slapi_search_internal_set_pb(
  53. newpb,
  54. slapi_sdn_get_dn(parentdn), /* JCM - This DN just identifies the backend to be searched. */
  55. LDAP_SCOPE_BASE,
  56. "objectclass=*",
  57. NULL, /*attrs*/
  58. 0, /*attrsonly*/
  59. NULL, /*Controls*/
  60. parentuniqueid, /*uniqueid*/
  61. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  62. 0);
  63. slapi_search_internal_pb(newpb);
  64. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
  65. switch(op_result)
  66. {
  67. case LDAP_SUCCESS:
  68. {
  69. Slapi_Entry **entries= NULL;
  70. /* OK, the tombstone entry parent exists. Is it also a tombstone? */
  71. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  72. if(entries!=NULL && entries[0]!=NULL)
  73. {
  74. if(is_tombstone_entry(entries[0]))
  75. {
  76. tombstone_to_glue (pb, sessionid, entries[0], parentdn, REASON_RESURRECT_ENTRY, opcsn);
  77. }
  78. }
  79. else
  80. {
  81. /* JCM - Couldn't find the entry! */
  82. }
  83. }
  84. break;
  85. default:
  86. /* So, the tombstone entry had a parent... but it's gone. */
  87. /* That's probably a bad thing. */
  88. break;
  89. }
  90. slapi_free_search_results_internal (newpb);
  91. slapi_pblock_destroy(newpb);
  92. }
  93. return 0;
  94. }
  95. /*
  96. * Convert a tombstone into a glue entry.
  97. */
  98. int
  99. tombstone_to_glue (
  100. Slapi_PBlock *pb,
  101. const char *sessionid,
  102. Slapi_Entry *tombstoneentry,
  103. const Slapi_DN *tombstonedn,
  104. const char *reason,
  105. CSN *opcsn)
  106. {
  107. Slapi_DN *parentdn;
  108. char *parentuniqueid;
  109. const char *tombstoneuniqueid;
  110. Slapi_Entry *addingentry;
  111. const char *addingdn;
  112. int op_result;
  113. /* JCMREPL
  114. * Nothing logged to the 5.0 Change Log
  115. * Add is logged to the 4.0 Change Log - Core server Add code
  116. * must attach the entry to the Operation
  117. */
  118. /* Resurrect the parent entry first */
  119. /* JCM - This DN calculation is odd. It could resolve to NULL
  120. * which won't help us identify the correct backend to search.
  121. */
  122. is_suffix_dn (pb, tombstonedn, &parentdn);
  123. parentuniqueid= slapi_entry_attr_get_charptr (tombstoneentry,
  124. SLAPI_ATTR_VALUE_PARENT_UNIQUEID); /* Allocated */
  125. tombstone_to_glue_resolve_parent (pb, sessionid, parentdn, parentuniqueid, opcsn);
  126. slapi_sdn_free(&parentdn);
  127. /* Submit an Add operation to turn the tombstone entry into glue. */
  128. /*
  129. * The tombstone is stored with an invalid DN, we must fix this.
  130. */
  131. addingentry = slapi_entry_dup(tombstoneentry);
  132. addingdn = slapi_sdn_get_dn(tombstonedn);
  133. slapi_entry_set_dn(addingentry,slapi_ch_strdup(addingdn)); /* consumes DN */
  134. if (!slapi_entry_attr_hasvalue(addingentry, ATTR_NSDS5_REPLCONFLICT, reason))
  135. {
  136. /* Add the reason of turning it to glue - The backend code will use it*/
  137. slapi_entry_add_string(addingentry, ATTR_NSDS5_REPLCONFLICT, reason);
  138. }
  139. tombstoneuniqueid= slapi_entry_get_uniqueid(tombstoneentry);
  140. op_result = urp_fixup_add_entry (addingentry, tombstoneuniqueid, parentuniqueid, opcsn, OP_FLAG_RESURECT_ENTRY);
  141. if (op_result == LDAP_SUCCESS)
  142. {
  143. slapi_log_error (slapi_log_urp, repl_plugin_name,
  144. "%s: Resurrected tombstone %s to glue reason '%s'\n", sessionid, addingdn, reason);
  145. }
  146. else
  147. {
  148. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  149. "%s: Can't resurrect tombstone %s to glue reason '%s', error=%d\n",
  150. sessionid, addingdn, reason, op_result);
  151. }
  152. slapi_entry_free (addingentry);
  153. return op_result;
  154. }
  155. int
  156. entry_to_tombstone ( Slapi_PBlock *pb, Slapi_Entry *entry )
  157. {
  158. Slapi_Operation *op;
  159. Slapi_Mods smods;
  160. CSN *opcsn;
  161. const char *uniqueid;
  162. int op_result = LDAP_SUCCESS;
  163. slapi_pblock_get ( pb, SLAPI_OPERATION, &op );
  164. opcsn = operation_get_csn ( op );
  165. uniqueid = slapi_entry_get_uniqueid ( entry );
  166. slapi_mods_init ( &smods, 2 );
  167. /* Remove objectclass=glue */
  168. slapi_mods_add ( &smods, LDAP_MOD_DELETE, SLAPI_ATTR_OBJECTCLASS, strlen("glue"), "glue");
  169. /* Remove any URP conflict since a tombstone shouldn't
  170. * be retrieved later for conflict removal.
  171. */
  172. slapi_mods_add ( &smods, LDAP_MOD_DELETE, ATTR_NSDS5_REPLCONFLICT, 0, NULL );
  173. op_result = urp_fixup_modify_entry (uniqueid, slapi_entry_get_dn_const (entry), opcsn, &smods, 0);
  174. slapi_mods_done ( &smods );
  175. /*
  176. * Delete the entry.
  177. */
  178. if ( op_result == LDAP_SUCCESS )
  179. {
  180. /*
  181. * Using internal delete operation since it would go
  182. * through the urp operations and trigger the recursive
  183. * fixup if applicable.
  184. */
  185. op_result = urp_fixup_delete_entry (uniqueid, slapi_entry_get_dn_const (entry), opcsn, 0);
  186. }
  187. return op_result;
  188. }