urp_glue.c 8.4 KB

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