urp_glue.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. const Slapi_DN *sdn;
  89. slapi_mods smods;
  90. Slapi_Attr *attr;
  91. dn = slapi_entry_get_dn_const (entry);
  92. sdn = slapi_entry_get_sdn_const (entry);
  93. slapi_mods_init(&smods, 4);
  94. /*
  95. richm: sometimes the entry is already a glue entry (how did that happen?)
  96. OR
  97. the entry is already objectclass extensibleObject or already has the
  98. conflict attribute and/or value
  99. */
  100. if (!slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "glue"))
  101. {
  102. slapi_mods_add_string( &smods, LDAP_MOD_ADD, SLAPI_ATTR_OBJECTCLASS, "glue" );
  103. if (!slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "extensibleobject"))
  104. slapi_mods_add_string( &smods, LDAP_MOD_ADD, SLAPI_ATTR_OBJECTCLASS, "extensibleobject" );
  105. }
  106. else
  107. {
  108. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  109. "%s: Target entry %s is already a glue entry reason %s\n",
  110. sessionid, dn, reason);
  111. }
  112. if (slapi_entry_attr_find (entry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0)
  113. {
  114. slapi_mods_add_string( &smods, LDAP_MOD_REPLACE, ATTR_NSDS5_REPLCONFLICT, reason);
  115. }
  116. else
  117. {
  118. slapi_mods_add_string( &smods, LDAP_MOD_ADD, ATTR_NSDS5_REPLCONFLICT, reason);
  119. }
  120. if (slapi_mods_get_num_mods(&smods) > 0)
  121. {
  122. op_result = urp_fixup_modify_entry (NULL, sdn, opcsn, &smods, 0);
  123. if (op_result == LDAP_SUCCESS)
  124. {
  125. slapi_log_error (slapi_log_urp, repl_plugin_name,
  126. "%s: Turned the entry %s to glue, reason %s\n",
  127. sessionid, dn, reason);
  128. }
  129. }
  130. slapi_mods_done(&smods);
  131. return op_result;
  132. }
  133. static const char *glue_entry =
  134. "dn: %s\n"
  135. "%s"
  136. "objectclass: top\n"
  137. "objectclass: extensibleObject\n" /* JCMREPL - To avoid schema checking. */
  138. "objectclass: glue\n"
  139. "nsuniqueid: %s\n"
  140. "%s: %s\n"; /* Add why it's been created */
  141. static int
  142. do_create_glue_entry(const Slapi_RDN *rdn, const Slapi_DN *superiordn, const char *uniqueid, const char *reason, CSN *opcsn)
  143. {
  144. int op_result= LDAP_OPERATIONS_ERROR;
  145. int rdnval_index = 0;
  146. int rdntype_len, rdnval_len, rdnpair_len, rdnstr_len, alloc_len;
  147. Slapi_Entry *e;
  148. Slapi_DN *sdn = NULL;
  149. Slapi_RDN *newrdn = slapi_rdn_new_rdn(rdn);
  150. char *estr, *rdnstr, *rdntype, *rdnval, *rdnpair;
  151. sdn = slapi_sdn_new_dn_byval(slapi_sdn_get_ndn(superiordn));
  152. slapi_sdn_add_rdn(sdn,rdn);
  153. /* must take care of multi-valued rdn: split rdn into different lines introducing
  154. * '\n' between each type/value pair.
  155. */
  156. alloc_len = RDNBUFSIZE;
  157. rdnstr = slapi_ch_malloc(alloc_len);
  158. rdnpair = rdnstr;
  159. *rdnpair = '\0'; /* so that strlen(rdnstr) may return 0 the first time it's called */
  160. while ((rdnval_index = slapi_rdn_get_next(newrdn, rdnval_index, &rdntype, &rdnval)) != -1) {
  161. rdntype_len = strlen(rdntype);
  162. rdnval_len = strlen(rdnval);
  163. rdnpair_len = LDIF_SIZE_NEEDED(rdntype_len, rdnval_len);
  164. rdnstr_len = strlen(rdnstr);
  165. if ((rdnstr_len + rdnpair_len + 1) > alloc_len) {
  166. alloc_len += (rdnpair_len + 1);
  167. rdnstr = slapi_ch_realloc(rdnstr, alloc_len);
  168. rdnpair = &rdnstr[rdnstr_len];
  169. }
  170. slapi_ldif_put_type_and_value_with_options(&rdnpair, rdntype,
  171. rdnval, rdnval_len, LDIF_OPT_NOWRAP);
  172. *rdnpair = '\0';
  173. }
  174. estr= slapi_ch_smprintf(glue_entry, slapi_sdn_get_ndn(sdn), rdnstr, uniqueid,
  175. ATTR_NSDS5_REPLCONFLICT, reason);
  176. slapi_ch_free((void**)&rdnstr);
  177. slapi_rdn_done(newrdn);
  178. slapi_ch_free((void**)&newrdn);
  179. e = slapi_str2entry( estr, 0 );
  180. PR_ASSERT(e!=NULL);
  181. if ( e!=NULL )
  182. {
  183. slapi_entry_set_uniqueid (e, slapi_ch_strdup(uniqueid));
  184. op_result = urp_fixup_add_entry (e, NULL, NULL, opcsn, 0);
  185. }
  186. slapi_ch_free_string(&estr);
  187. slapi_sdn_free(&sdn);
  188. return op_result;
  189. }
  190. int
  191. create_glue_entry ( Slapi_PBlock *pb, char *sessionid, Slapi_DN *dn, const char *uniqueid, CSN *opcsn )
  192. {
  193. int op_result;
  194. const char *dnstr;
  195. if ( slapi_sdn_get_dn (dn) )
  196. dnstr = slapi_sdn_get_dn (dn);
  197. else
  198. dnstr = "";
  199. if ( NULL == uniqueid )
  200. {
  201. op_result = LDAP_OPERATIONS_ERROR;
  202. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name,
  203. "%s: Can't create glue %s, uniqueid=NULL\n", sessionid, dnstr);
  204. }
  205. else
  206. {
  207. Slapi_Backend *backend;
  208. Slapi_DN *superiordn = slapi_sdn_new();
  209. Slapi_RDN *rdn= slapi_rdn_new();
  210. int done= 0;
  211. slapi_pblock_get( pb, SLAPI_BACKEND, &backend );
  212. slapi_sdn_get_backend_parent ( dn, superiordn, backend );
  213. slapi_sdn_get_rdn ( dn, rdn );
  214. while(!done)
  215. {
  216. op_result= do_create_glue_entry(rdn, superiordn, uniqueid, "missingEntry", opcsn);
  217. switch(op_result)
  218. {
  219. case LDAP_SUCCESS:
  220. slapi_log_error ( SLAPI_LOG_FATAL, repl_plugin_name,
  221. "%s: Created glue entry %s uniqueid=%s reason missingEntry\n",
  222. sessionid, dnstr, uniqueid);
  223. done= 1;
  224. break;
  225. case LDAP_NO_SUCH_OBJECT:
  226. /* The parent is missing */
  227. {
  228. /* JCMREPL - Create the parent ... recursion?... but what's the uniqueid? */
  229. PR_ASSERT(0); /* JCMREPL */
  230. }
  231. default:
  232. slapi_log_error ( SLAPI_LOG_FATAL, repl_plugin_name,
  233. "%s: Can't created glue entry %s uniqueid=%s, error %d\n",
  234. sessionid, dnstr, uniqueid, op_result);
  235. break;
  236. }
  237. /* JCMREPL - Could get trapped in this loop forever! */
  238. }
  239. slapi_rdn_free ( &rdn );
  240. slapi_sdn_free ( &superiordn );
  241. }
  242. return op_result;
  243. }