urp_glue.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * urp_glue.c - Update Resolution Procedures - Glue
  14. */
  15. #include "slapi-plugin.h"
  16. #include "repl5.h"
  17. #include "urp.h"
  18. #define RDNBUFSIZE 2048
  19. extern int slapi_log_urp;
  20. /*
  21. * Check if the entry is glue.
  22. */
  23. int
  24. is_glue_entry(const Slapi_Entry* entry)
  25. {
  26. /* JCMREPL - Is there a more efficient way to do this? */
  27. return slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "glue");
  28. }
  29. /* returns PR_TRUE if the entry is a glue entry, PR_FALSE otherwise
  30. sets the gluecsn if it is a glue entry - gluecsn may (but should not) be NULL */
  31. PRBool
  32. get_glue_csn(const Slapi_Entry *entry, const CSN **gluecsn)
  33. {
  34. PRBool isglue = PR_FALSE;
  35. Slapi_Attr *oc_attr = NULL;
  36. /* cast away const - entry */
  37. if (entry_attr_find_wsi((Slapi_Entry*)entry, SLAPI_ATTR_OBJECTCLASS, &oc_attr) == ATTRIBUTE_PRESENT)
  38. {
  39. Slapi_Value *glue_value = NULL;
  40. struct berval v;
  41. v.bv_val = "glue";
  42. v.bv_len = strlen(v.bv_val);
  43. if (attr_value_find_wsi(oc_attr, &v, &glue_value) == VALUE_PRESENT)
  44. {
  45. isglue = PR_TRUE;
  46. *gluecsn = value_get_csn(glue_value, CSN_TYPE_VALUE_UPDATED);
  47. }
  48. }
  49. return isglue;
  50. }
  51. /*
  52. * Submit a Modify operation to turn the Entry into Glue.
  53. */
  54. int
  55. entry_to_glue(char *sessionid, const Slapi_Entry* entry, const char *reason, CSN *opcsn)
  56. {
  57. int op_result = 0;
  58. const char *dn;
  59. const Slapi_DN *sdn;
  60. slapi_mods smods;
  61. Slapi_Attr *attr;
  62. dn = slapi_entry_get_dn_const (entry);
  63. sdn = slapi_entry_get_sdn_const (entry);
  64. slapi_mods_init(&smods, 4);
  65. /*
  66. richm: sometimes the entry is already a glue entry (how did that happen?)
  67. OR
  68. the entry is already objectclass extensibleObject or already has the
  69. conflict attribute and/or value
  70. */
  71. if (!slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "glue"))
  72. {
  73. slapi_mods_add_string( &smods, LDAP_MOD_ADD, SLAPI_ATTR_OBJECTCLASS, "glue" );
  74. if (!slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "extensibleobject"))
  75. slapi_mods_add_string( &smods, LDAP_MOD_ADD, SLAPI_ATTR_OBJECTCLASS, "extensibleobject" );
  76. }
  77. else
  78. {
  79. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  80. "%s: Target entry %s is already a glue entry reason %s\n",
  81. sessionid, dn, reason);
  82. }
  83. if (slapi_entry_attr_find (entry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0)
  84. {
  85. slapi_mods_add_string( &smods, LDAP_MOD_REPLACE, ATTR_NSDS5_REPLCONFLICT, reason);
  86. }
  87. else
  88. {
  89. slapi_mods_add_string( &smods, LDAP_MOD_ADD, ATTR_NSDS5_REPLCONFLICT, reason);
  90. }
  91. if (slapi_mods_get_num_mods(&smods) > 0)
  92. {
  93. op_result = urp_fixup_modify_entry (NULL, sdn, opcsn, &smods, 0);
  94. if (op_result == LDAP_SUCCESS)
  95. {
  96. slapi_log_error (slapi_log_urp, repl_plugin_name,
  97. "%s: Turned the entry %s to glue, reason %s\n",
  98. sessionid, dn, reason);
  99. }
  100. }
  101. slapi_mods_done(&smods);
  102. return op_result;
  103. }
  104. static const char *glue_entry =
  105. "dn: %s\n"
  106. "%s"
  107. "objectclass: top\n"
  108. "objectclass: extensibleObject\n" /* JCMREPL - To avoid schema checking. */
  109. "objectclass: glue\n"
  110. "nsuniqueid: %s\n" /* this uniqueid is set to Slapi_Entry in slapi_str2entry. */
  111. "%s: %s\n"; /* Add why it's been created */
  112. static int
  113. do_create_glue_entry(const Slapi_RDN *rdn, const Slapi_DN *superiordn, const char *uniqueid, const char *reason, CSN *opcsn)
  114. {
  115. int op_result= LDAP_OPERATIONS_ERROR;
  116. int rdnval_index = 0;
  117. int rdntype_len, rdnval_len, rdnpair_len, rdnstr_len, alloc_len;
  118. Slapi_Entry *e;
  119. Slapi_DN *sdn = NULL;
  120. Slapi_RDN *newrdn = slapi_rdn_new_rdn(rdn);
  121. char *estr, *rdnstr, *rdntype, *rdnval, *rdnpair;
  122. sdn = slapi_sdn_new_ndn_byval(slapi_sdn_get_ndn(superiordn));
  123. slapi_sdn_add_rdn(sdn,rdn);
  124. /* must take care of multi-valued rdn: split rdn into different lines introducing
  125. * '\n' between each type/value pair.
  126. */
  127. alloc_len = RDNBUFSIZE;
  128. rdnstr = slapi_ch_malloc(alloc_len);
  129. rdnpair = rdnstr;
  130. *rdnpair = '\0'; /* so that strlen(rdnstr) may return 0 the first time it's called */
  131. while ((rdnval_index = slapi_rdn_get_next(newrdn, rdnval_index, &rdntype, &rdnval)) != -1) {
  132. rdntype_len = strlen(rdntype);
  133. rdnval_len = strlen(rdnval);
  134. rdnpair_len = LDIF_SIZE_NEEDED(rdntype_len, rdnval_len);
  135. rdnstr_len = strlen(rdnstr);
  136. if ((rdnstr_len + rdnpair_len + 1) > alloc_len) {
  137. alloc_len += (rdnpair_len + 1);
  138. rdnstr = slapi_ch_realloc(rdnstr, alloc_len);
  139. rdnpair = &rdnstr[rdnstr_len];
  140. }
  141. slapi_ldif_put_type_and_value_with_options(&rdnpair, rdntype, rdnval, rdnval_len, LDIF_OPT_NOWRAP);
  142. *rdnpair = '\0';
  143. }
  144. estr= slapi_ch_smprintf(glue_entry, slapi_sdn_get_ndn(sdn), rdnstr, uniqueid,
  145. ATTR_NSDS5_REPLCONFLICT, reason);
  146. slapi_ch_free((void**)&rdnstr);
  147. slapi_rdn_done(newrdn);
  148. slapi_ch_free((void**)&newrdn);
  149. e = slapi_str2entry( estr, 0 );
  150. PR_ASSERT(e!=NULL);
  151. if (e) {
  152. op_result = urp_fixup_add_entry (e, NULL, NULL, opcsn, 0);
  153. }
  154. slapi_ch_free_string(&estr);
  155. slapi_sdn_free(&sdn);
  156. return op_result;
  157. }
  158. int
  159. create_glue_entry ( Slapi_PBlock *pb, char *sessionid, Slapi_DN *dn, const char *uniqueid, CSN *opcsn )
  160. {
  161. int op_result;
  162. const char *dnstr;
  163. if ( slapi_sdn_get_dn (dn) )
  164. dnstr = slapi_sdn_get_dn (dn);
  165. else
  166. dnstr = "";
  167. if ( NULL == uniqueid )
  168. {
  169. op_result = LDAP_OPERATIONS_ERROR;
  170. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  171. "%s: Can't create glue %s, uniqueid=NULL\n", sessionid, dnstr);
  172. }
  173. else
  174. {
  175. Slapi_Backend *backend;
  176. Slapi_DN *superiordn = slapi_sdn_new();
  177. Slapi_RDN *rdn= slapi_rdn_new();
  178. int done= 0;
  179. slapi_pblock_get( pb, SLAPI_BACKEND, &backend );
  180. slapi_sdn_get_backend_parent ( dn, superiordn, backend );
  181. slapi_rdn_set_dn_ext(rdn, slapi_sdn_get_dn(dn), SLAPI_RDN_SET_DN_SKIP_UNIQUEID);
  182. while(!done)
  183. {
  184. op_result= do_create_glue_entry(rdn, superiordn, uniqueid, "missingEntry", opcsn);
  185. switch(op_result)
  186. {
  187. case LDAP_SUCCESS:
  188. slapi_log_error ( SLAPI_LOG_FATAL, repl_plugin_name,
  189. "%s: Created glue entry %s uniqueid=%s reason missingEntry\n",
  190. sessionid, dnstr, uniqueid);
  191. done= 1;
  192. break;
  193. case LDAP_ALREADY_EXISTS:
  194. {
  195. struct slapi_operation_parameters *op_params;
  196. /* This is okay. While creating a glue, a real entry was added. */
  197. slapi_log_error ( SLAPI_LOG_FATAL, repl_plugin_name,
  198. "%s: Skipped creating glue entry %s uniqueid=%s reason Entry Already Exists\n",
  199. sessionid, dnstr, uniqueid);
  200. op_result = LDAP_SUCCESS;
  201. /* If we could not create a glue having the nsuniqueid,
  202. * we have to abandon it. */
  203. slapi_pblock_get(pb, SLAPI_OPERATION_PARAMETERS, &op_params);
  204. slapi_ch_free_string(&op_params->p.p_add.parentuniqueid);
  205. done= 1;
  206. break;
  207. }
  208. case LDAP_NO_SUCH_OBJECT:
  209. /* The parent is missing */
  210. {
  211. /* JCMREPL - Create the parent ... recursion?... but what's the uniqueid? */
  212. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  213. "%s: Can't created glue entry %s uniqueid=%s, error %d; "
  214. "Possibly, parent entry is a conflict entry.\n",
  215. sessionid, dnstr, uniqueid, op_result);
  216. done= 1;
  217. break;
  218. }
  219. default:
  220. slapi_log_error ( SLAPI_LOG_FATAL, repl_plugin_name,
  221. "%s: Can't created glue entry %s uniqueid=%s, error %d\n",
  222. sessionid, dnstr, uniqueid, op_result);
  223. break;
  224. }
  225. /* JCMREPL - Could get trapped in this loop forever! */
  226. }
  227. slapi_rdn_free ( &rdn );
  228. slapi_sdn_free ( &superiordn );
  229. }
  230. return op_result;
  231. }