urp_tombstone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = entry_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. char *sessionid,
  78. const Slapi_DN *parentdn,
  79. const char *parentuniqueid,
  80. CSN *opcsn,
  81. Slapi_DN **newparentdn)
  82. {
  83. /* Let's have a look at the parent of this entry... */
  84. if(!slapi_sdn_isempty(parentdn) && parentuniqueid!=NULL)
  85. {
  86. int op_result;
  87. Slapi_PBlock *newpb= slapi_pblock_new();
  88. slapi_search_internal_set_pb(
  89. newpb,
  90. slapi_sdn_get_dn(parentdn), /* JCM - This DN just identifies the backend to be searched. */
  91. LDAP_SCOPE_BASE,
  92. "objectclass=*",
  93. NULL, /*attrs*/
  94. 0, /*attrsonly*/
  95. NULL, /*Controls*/
  96. parentuniqueid, /*uniqueid*/
  97. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  98. 0);
  99. slapi_search_internal_pb(newpb);
  100. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_RESULT, &op_result);
  101. switch(op_result)
  102. {
  103. case LDAP_SUCCESS:
  104. {
  105. Slapi_Entry **entries= NULL;
  106. /* OK, the tombstone entry parent exists. Is it also a tombstone? */
  107. slapi_pblock_get(newpb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  108. if(entries!=NULL && entries[0]!=NULL)
  109. {
  110. if(is_tombstone_entry(entries[0]))
  111. {
  112. tombstone_to_glue(pb, sessionid, entries[0], parentdn,
  113. REASON_RESURRECT_ENTRY, opcsn, newparentdn);
  114. }
  115. }
  116. else
  117. {
  118. /* JCM - Couldn't find the entry! */
  119. }
  120. }
  121. break;
  122. default:
  123. /* So, the tombstone entry had a parent... but it's gone. */
  124. /* That's probably a bad thing. */
  125. break;
  126. }
  127. slapi_free_search_results_internal (newpb);
  128. slapi_pblock_destroy(newpb);
  129. }
  130. return 0;
  131. }
  132. /*
  133. * Convert a tombstone into a glue entry.
  134. */
  135. int
  136. tombstone_to_glue (
  137. Slapi_PBlock *pb,
  138. char *sessionid,
  139. Slapi_Entry *tombstoneentry,
  140. const Slapi_DN *gluedn, /* does not start with uniqueid= */
  141. const char *reason,
  142. CSN *opcsn,
  143. Slapi_DN **newparentdn)
  144. {
  145. Slapi_DN *parentdn;
  146. char *parentuniqueid;
  147. const char *tombstoneuniqueid;
  148. Slapi_Entry *addingentry = NULL;
  149. Slapi_Entry *addingentry_bakup = NULL;
  150. const char *addingdn;
  151. int op_result;
  152. int rdn_is_conflict = 0;
  153. /* JCMREPL
  154. * Nothing logged to the 5.0 Change Log
  155. * Add is logged to the 4.0 Change Log - Core server Add code
  156. * must attach the entry to the Operation
  157. */
  158. /* Resurrect the parent entry first */
  159. /* JCM - This DN calculation is odd. It could resolve to NULL
  160. * which won't help us identify the correct backend to search.
  161. */
  162. is_suffix_dn_ext (pb, gluedn, &parentdn, 1 /* is_tombstone */);
  163. parentuniqueid = slapi_entry_attr_get_charptr (tombstoneentry, SLAPI_ATTR_VALUE_PARENT_UNIQUEID); /* Allocated */
  164. tombstone_to_glue_resolve_parent (pb, sessionid, parentdn, parentuniqueid, opcsn, newparentdn);
  165. /* Submit an Add operation to turn the tombstone entry into glue. */
  166. /*
  167. * The tombstone is stored with an invalid DN, we must fix this.
  168. */
  169. addingentry = slapi_entry_dup(tombstoneentry);
  170. if (newparentdn && *newparentdn && slapi_sdn_compare(parentdn, *newparentdn)) {
  171. /* If the parents are resolved, the tombstone's DN is going to be different... */
  172. /* Update DN in addingentry */
  173. Slapi_RDN *rdn = slapi_rdn_new();
  174. slapi_rdn_set_dn_ext(rdn, slapi_sdn_get_dn(gluedn), SLAPI_RDN_SET_DN_INCLUDE_UNIQUEID);
  175. addingdn = slapi_moddn_get_newdn(slapi_entry_get_sdn(addingentry), slapi_rdn_get_rdn(rdn),
  176. slapi_sdn_get_dn(*newparentdn));
  177. slapi_rdn_free(&rdn);
  178. slapi_sdn_init_normdn_passin(*newparentdn, addingdn); /* to return the new parentdn to the caller */
  179. } else {
  180. slapi_sdn_free(newparentdn); /* no change in parentdn; returning NULL */
  181. addingdn = slapi_sdn_get_dn(gluedn);
  182. }
  183. slapi_sdn_set_normdn_byval(slapi_entry_get_sdn(addingentry), addingdn);
  184. /* not just e_sdn, e_rsdn needs to be updated. */
  185. slapi_rdn_set_all_dn(slapi_entry_get_srdn(addingentry), slapi_entry_get_dn_const(addingentry));
  186. rdn_is_conflict = slapi_rdn_is_conflict(slapi_entry_get_srdn(addingentry));
  187. if (!slapi_entry_attr_hasvalue(addingentry, ATTR_NSDS5_REPLCONFLICT, reason))
  188. {
  189. /* Add the reason of turning it to glue - The backend code will use it*/
  190. slapi_entry_add_string(addingentry, ATTR_NSDS5_REPLCONFLICT, reason);
  191. }
  192. tombstoneuniqueid= slapi_entry_get_uniqueid(tombstoneentry);
  193. /*
  194. * addingentry and parentuniqueid are consumed in urp_fixup_add_entry,
  195. * regardless of the result.
  196. * Note: addingentry is not really consumed in ldbm_back_add.
  197. * tombstoneentry from DB/entry cache is duplicated and turned to be a glue.
  198. * This addingentry is freed in op_shared_add.
  199. */
  200. addingentry_bakup = slapi_entry_dup(addingentry);
  201. op_result = urp_fixup_add_entry (addingentry, tombstoneuniqueid, slapi_ch_strdup(parentuniqueid), opcsn, OP_FLAG_RESURECT_ENTRY);
  202. if ((LDAP_ALREADY_EXISTS == op_result) && !rdn_is_conflict) {
  203. /* conflict -- there's already the same named entry added.
  204. * But this to-be-glued entry needs to be added since this is a parent of child entries...
  205. * So, rename this tombstone parententry a conflict, glue entry.
  206. * Instead of "fixup_add", we have to "fixup_rename"...
  207. * */
  208. char *conflictrdn = get_rdn_plus_uniqueid(sessionid, addingdn, tombstoneuniqueid);
  209. if (conflictrdn) {
  210. addingentry = addingentry_bakup;
  211. addingentry_bakup = NULL;
  212. if (!slapi_entry_attr_hasvalue(addingentry, ATTR_NSDS5_REPLCONFLICT, reason)) {
  213. /* Add the reason of turning it to glue - The backend code will use it*/
  214. slapi_entry_add_string(addingentry, ATTR_NSDS5_REPLCONFLICT, reason);
  215. }
  216. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  217. "%s: Can't resurrect tombstone to glue reason '%s'. "
  218. "Try with conflict dn %s, error=%d\n",
  219. sessionid, reason, addingdn, op_result);
  220. op_result = urp_fixup_rename_entry(addingentry, (const char *)conflictrdn, parentuniqueid,
  221. OP_FLAG_RESURECT_ENTRY|OP_FLAG_TOMBSTONE_ENTRY);
  222. slapi_ch_free_string(&conflictrdn);
  223. slapi_entry_free(addingentry);
  224. addingentry = NULL;
  225. }
  226. }
  227. slapi_entry_free(addingentry_bakup);
  228. slapi_ch_free_string(&parentuniqueid);
  229. if (op_result == LDAP_SUCCESS)
  230. {
  231. slapi_log_error (/*slapi_log_urp*/SLAPI_LOG_FATAL, repl_plugin_name,
  232. "%s: Resurrected tombstone %s to glue reason '%s'\n", sessionid, addingdn, reason);
  233. }
  234. else if (LDAP_ALREADY_EXISTS == op_result)
  235. {
  236. slapi_log_error(/*slapi_log_urp*/SLAPI_LOG_FATAL, repl_plugin_name,
  237. "%s: No need to turn tombstone %s to glue; it was already resurrected.\n",
  238. sessionid, addingdn);
  239. op_result = LDAP_SUCCESS;
  240. }
  241. else
  242. {
  243. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  244. "%s: Can't resurrect tombstone %s to glue reason '%s', error=%d\n",
  245. sessionid, addingdn, reason, op_result);
  246. }
  247. slapi_sdn_free(&parentdn);
  248. return op_result;
  249. }
  250. int
  251. entry_to_tombstone ( Slapi_PBlock *pb, Slapi_Entry *entry )
  252. {
  253. Slapi_Operation *op;
  254. Slapi_Mods smods;
  255. CSN *opcsn;
  256. const char *uniqueid;
  257. int op_result = LDAP_SUCCESS;
  258. slapi_pblock_get ( pb, SLAPI_OPERATION, &op );
  259. opcsn = operation_get_csn ( op );
  260. uniqueid = slapi_entry_get_uniqueid ( entry );
  261. slapi_mods_init ( &smods, 2 );
  262. /* Remove objectclass=glue */
  263. slapi_mods_add ( &smods, LDAP_MOD_DELETE, SLAPI_ATTR_OBJECTCLASS, strlen("glue"), "glue");
  264. /* Remove any URP conflict since a tombstone shouldn't
  265. * be retrieved later for conflict removal.
  266. */
  267. slapi_mods_add ( &smods, LDAP_MOD_DELETE, ATTR_NSDS5_REPLCONFLICT, 0, NULL );
  268. op_result = urp_fixup_modify_entry (uniqueid,
  269. slapi_entry_get_sdn_const (entry),
  270. opcsn, &smods, 0);
  271. slapi_mods_done ( &smods );
  272. /*
  273. * Delete the entry.
  274. */
  275. if ( op_result == LDAP_SUCCESS )
  276. {
  277. /*
  278. * Using internal delete operation since it would go
  279. * through the urp operations and trigger the recursive
  280. * fixup if applicable.
  281. */
  282. op_result = urp_fixup_delete_entry (uniqueid, slapi_entry_get_dn_const (entry), opcsn, 0);
  283. }
  284. return op_result;
  285. }