urp_tombstone.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * urp_tombstone.c - Update Resolution Procedures - Tombstones
  43. */
  44. #include "slapi-plugin.h"
  45. #include "repl5.h"
  46. #include "urp.h"
  47. extern int slapi_log_urp;
  48. /*
  49. * Check if the entry is a tombstone.
  50. */
  51. int
  52. is_tombstone_entry(const Slapi_Entry* entry)
  53. {
  54. int flag;
  55. /* LP: This doesn't work very well with entries that we tombstone ourself */
  56. flag = slapi_entry_flag_is_set (entry, SLAPI_ENTRY_FLAG_TOMBSTONE);
  57. if (flag == 0)
  58. {
  59. /* This is slow */
  60. flag = slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE);
  61. }
  62. return flag;
  63. }
  64. PRBool
  65. get_tombstone_csn(const Slapi_Entry *entry, const CSN **delcsn)
  66. {
  67. PRBool ists = PR_FALSE;
  68. if (is_tombstone_entry(entry)) {
  69. ists = PR_TRUE;
  70. *delcsn = _get_deletion_csn((Slapi_Entry *)entry); /* cast away const */
  71. }
  72. return ists;
  73. }
  74. static int
  75. tombstone_to_glue_resolve_parent (
  76. Slapi_PBlock *pb,
  77. const char *sessionid,
  78. const Slapi_DN *parentdn,
  79. const char *parentuniqueid,
  80. CSN *opcsn)
  81. {
  82. /* Let's have a look at the parent of this entry... */
  83. if(!slapi_sdn_isempty(parentdn) && parentuniqueid!=NULL)
  84. {
  85. int op_result;
  86. Slapi_PBlock *newpb= slapi_pblock_new();
  87. slapi_search_internal_set_pb(
  88. newpb,
  89. slapi_sdn_get_dn(parentdn), /* JCM - This DN just identifies the backend to be searched. */
  90. LDAP_SCOPE_BASE,
  91. "objectclass=*",
  92. NULL, /*attrs*/
  93. 0, /*attrsonly*/
  94. NULL, /*Controls*/
  95. parentuniqueid, /*uniqueid*/
  96. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  97. 0);
  98. slapi_search_internal_pb(newpb);
  99. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
  100. switch(op_result)
  101. {
  102. case LDAP_SUCCESS:
  103. {
  104. Slapi_Entry **entries= NULL;
  105. /* OK, the tombstone entry parent exists. Is it also a tombstone? */
  106. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  107. if(entries!=NULL && entries[0]!=NULL)
  108. {
  109. if(is_tombstone_entry(entries[0]))
  110. {
  111. tombstone_to_glue (pb, sessionid, entries[0], parentdn, REASON_RESURRECT_ENTRY, opcsn);
  112. }
  113. }
  114. else
  115. {
  116. /* JCM - Couldn't find the entry! */
  117. }
  118. }
  119. break;
  120. default:
  121. /* So, the tombstone entry had a parent... but it's gone. */
  122. /* That's probably a bad thing. */
  123. break;
  124. }
  125. slapi_free_search_results_internal (newpb);
  126. slapi_pblock_destroy(newpb);
  127. }
  128. return 0;
  129. }
  130. /*
  131. * Convert a tombstone into a glue entry.
  132. */
  133. int
  134. tombstone_to_glue (
  135. Slapi_PBlock *pb,
  136. const char *sessionid,
  137. Slapi_Entry *tombstoneentry,
  138. const Slapi_DN *tombstonedn,
  139. const char *reason,
  140. CSN *opcsn)
  141. {
  142. Slapi_DN *parentdn;
  143. char *parentuniqueid;
  144. const char *tombstoneuniqueid;
  145. Slapi_Entry *addingentry;
  146. const char *addingdn;
  147. int op_result;
  148. /* JCMREPL
  149. * Nothing logged to the 5.0 Change Log
  150. * Add is logged to the 4.0 Change Log - Core server Add code
  151. * must attach the entry to the Operation
  152. */
  153. /* Resurrect the parent entry first */
  154. /* JCM - This DN calculation is odd. It could resolve to NULL
  155. * which won't help us identify the correct backend to search.
  156. */
  157. is_suffix_dn (pb, tombstonedn, &parentdn);
  158. parentuniqueid= slapi_entry_attr_get_charptr (tombstoneentry,
  159. SLAPI_ATTR_VALUE_PARENT_UNIQUEID); /* Allocated */
  160. tombstone_to_glue_resolve_parent (pb, sessionid, parentdn, parentuniqueid, opcsn);
  161. slapi_sdn_free(&parentdn);
  162. /* Submit an Add operation to turn the tombstone entry into glue. */
  163. /*
  164. * The tombstone is stored with an invalid DN, we must fix this.
  165. */
  166. addingentry = slapi_entry_dup(tombstoneentry);
  167. addingdn = slapi_sdn_get_dn(tombstonedn);
  168. slapi_entry_set_dn(addingentry,slapi_ch_strdup(addingdn)); /* consumes DN */
  169. if (!slapi_entry_attr_hasvalue(addingentry, ATTR_NSDS5_REPLCONFLICT, reason))
  170. {
  171. /* Add the reason of turning it to glue - The backend code will use it*/
  172. slapi_entry_add_string(addingentry, ATTR_NSDS5_REPLCONFLICT, reason);
  173. }
  174. tombstoneuniqueid= slapi_entry_get_uniqueid(tombstoneentry);
  175. op_result = urp_fixup_add_entry (addingentry, tombstoneuniqueid, parentuniqueid, opcsn, OP_FLAG_RESURECT_ENTRY);
  176. if (op_result == LDAP_SUCCESS)
  177. {
  178. slapi_log_error (slapi_log_urp, repl_plugin_name,
  179. "%s: Resurrected tombstone %s to glue reason '%s'\n", sessionid, addingdn, reason);
  180. }
  181. else
  182. {
  183. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  184. "%s: Can't resurrect tombstone %s to glue reason '%s', error=%d\n",
  185. sessionid, addingdn, reason, op_result);
  186. }
  187. slapi_entry_free (addingentry);
  188. return op_result;
  189. }
  190. int
  191. entry_to_tombstone ( Slapi_PBlock *pb, Slapi_Entry *entry )
  192. {
  193. Slapi_Operation *op;
  194. Slapi_Mods smods;
  195. CSN *opcsn;
  196. const char *uniqueid;
  197. int op_result = LDAP_SUCCESS;
  198. slapi_pblock_get ( pb, SLAPI_OPERATION, &op );
  199. opcsn = operation_get_csn ( op );
  200. uniqueid = slapi_entry_get_uniqueid ( entry );
  201. slapi_mods_init ( &smods, 2 );
  202. /* Remove objectclass=glue */
  203. slapi_mods_add ( &smods, LDAP_MOD_DELETE, SLAPI_ATTR_OBJECTCLASS, strlen("glue"), "glue");
  204. /* Remove any URP conflict since a tombstone shouldn't
  205. * be retrieved later for conflict removal.
  206. */
  207. slapi_mods_add ( &smods, LDAP_MOD_DELETE, ATTR_NSDS5_REPLCONFLICT, 0, NULL );
  208. op_result = urp_fixup_modify_entry (uniqueid, slapi_entry_get_dn_const (entry), opcsn, &smods, 0);
  209. slapi_mods_done ( &smods );
  210. /*
  211. * Delete the entry.
  212. */
  213. if ( op_result == LDAP_SUCCESS )
  214. {
  215. /*
  216. * Using internal delete operation since it would go
  217. * through the urp operations and trigger the recursive
  218. * fixup if applicable.
  219. */
  220. op_result = urp_fixup_delete_entry (uniqueid, slapi_entry_get_dn_const (entry), opcsn, 0);
  221. }
  222. return op_result;
  223. }