urp_glue.c 6.5 KB

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