1
0

urp_glue.c 8.5 KB

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