ldbm_modrdn.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. /* modrdn.c - ldbm backend modrdn routine */
  42. #include "back-ldbm.h"
  43. static const char *moddn_get_newdn(Slapi_PBlock *pb, Slapi_DN *dn_olddn, Slapi_DN *dn_newrdn, Slapi_DN *dn_newsuperiordn);
  44. static void moddn_unlock_and_return_entries(backend *be,struct backentry **targetentry, struct backentry **existingentry);
  45. static int moddn_newrdn_mods(Slapi_PBlock *pb, const char *olddn, struct backentry *ec, Slapi_Mods *smods, Slapi_Mods *smods_wsi, int is_repl_op);
  46. static IDList *moddn_get_children(back_txn *ptxn, Slapi_PBlock *pb, backend *be, struct backentry *parententry, Slapi_DN *parentdn, struct backentry ***child_entries, struct backentry ***child_entry_copies);
  47. static int moddn_rename_children(back_txn *ptxn, Slapi_PBlock *pb, backend *be, IDList *children, Slapi_DN *dn_parentdn, Slapi_DN *dn_newsuperiordn, struct backentry *child_entries[], struct backentry *child_entry_copies[]);
  48. static int modrdn_rename_entry_update_indexes(back_txn *ptxn, Slapi_PBlock *pb, struct ldbminfo *li, struct backentry *e, struct backentry *ec, Slapi_Mods *smods1, Slapi_Mods *smods2, Slapi_Mods *smods3);
  49. static void mods_remove_nsuniqueid(Slapi_Mods *smods);
  50. int
  51. ldbm_back_modrdn( Slapi_PBlock *pb )
  52. {
  53. backend *be;
  54. ldbm_instance *inst;
  55. struct ldbminfo *li;
  56. struct backentry *e= NULL;
  57. struct backentry *ec= NULL;
  58. int ec_in_cache= 0;
  59. back_txn txn;
  60. back_txnid parent_txn;
  61. int retval = -1;
  62. char *msg;
  63. Slapi_Entry *postentry = NULL;
  64. char *errbuf = NULL;
  65. int disk_full = 0;
  66. int retry_count = 0;
  67. int ldap_result_code= LDAP_SUCCESS;
  68. char *ldap_result_message= NULL;
  69. char *ldap_result_matcheddn= NULL;
  70. struct backentry *parententry= NULL;
  71. struct backentry *newparententry= NULL;
  72. struct backentry *existingentry= NULL;
  73. modify_context parent_modify_context = {0};
  74. modify_context newparent_modify_context = {0};
  75. IDList *children= NULL;
  76. struct backentry **child_entries= NULL;
  77. struct backentry **child_entry_copies= NULL;
  78. Slapi_DN dn_olddn;
  79. Slapi_DN dn_newdn;
  80. Slapi_DN dn_newrdn;
  81. Slapi_DN dn_newsuperiordn;
  82. Slapi_DN dn_parentdn;
  83. int rc;
  84. int isroot;
  85. LDAPMod **mods;
  86. Slapi_Mods smods_operation_wsi = {0};
  87. Slapi_Mods smods_generated = {0};
  88. Slapi_Mods smods_generated_wsi = {0};
  89. Slapi_Operation *operation;
  90. int dblock_acquired= 0;
  91. int is_replicated_operation= 0;
  92. int is_fixup_operation = 0;
  93. entry_address new_addr;
  94. entry_address *old_addr;
  95. entry_address oldparent_addr;
  96. entry_address *newsuperior_addr;
  97. char *dn;
  98. char ebuf[BUFSIZ];
  99. CSN *opcsn = NULL;
  100. slapi_sdn_init(&dn_newdn);
  101. slapi_sdn_init(&dn_parentdn);
  102. slapi_pblock_get( pb, SLAPI_MODRDN_TARGET, &dn );
  103. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  104. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  105. slapi_pblock_get( pb, SLAPI_PARENT_TXN, (void**)&parent_txn );
  106. slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  107. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  108. slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation );
  109. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  110. if (pb->pb_conn)
  111. {
  112. slapi_log_error (SLAPI_LOG_TRACE, "ldbm_back_modrdn", "enter conn=%" NSPRIu64 " op=%d\n", pb->pb_conn->c_connid, operation->o_opid);
  113. }
  114. inst = (ldbm_instance *) be->be_instance_info;
  115. {
  116. char *newrdn, *newsuperiordn;
  117. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  118. slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR, &newsuperiordn );
  119. slapi_sdn_init_dn_byref(&dn_olddn,dn);
  120. slapi_sdn_init_dn_byref(&dn_newrdn,newrdn);
  121. slapi_sdn_init_dn_byref(&dn_newsuperiordn,newsuperiordn);
  122. slapi_sdn_get_parent(&dn_olddn,&dn_parentdn);
  123. }
  124. /* if old and new superior are equals, newsuperior should not be set
  125. * Here we have to reset newsuperiordn in order to save processing and
  126. * avoid later deadlock when trying to fetch twice the same entry
  127. */
  128. if (slapi_sdn_compare(&dn_newsuperiordn, &dn_parentdn) == 0)
  129. {
  130. slapi_sdn_done(&dn_newsuperiordn);
  131. slapi_sdn_init_dn_byref(&dn_newsuperiordn,NULL);
  132. }
  133. /* Replicated Operations are allowed to change the superior */
  134. if ( !is_replicated_operation && !slapi_sdn_isempty(&dn_newsuperiordn))
  135. {
  136. slapi_send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  137. "server does not support moving of entries", 0, NULL );
  138. return( -1 );
  139. }
  140. dblayer_txn_init(li,&txn);
  141. /* The dblock serializes writes to the database,
  142. * which reduces deadlocking in the db code,
  143. * which means that we run faster.
  144. *
  145. * But, this lock is re-enterant for the fixup
  146. * operations that the URP code in the Replication
  147. * plugin generates.
  148. *
  149. * Also some URP post-op operations are called after
  150. * the backend has committed the change and released
  151. * the dblock. Acquire the dblock again for them
  152. * if OP_FLAG_ACTION_INVOKE_FOR_REPLOP is set.
  153. */
  154. if(SERIALLOCK(li) && (!operation_is_flag_set(operation,OP_FLAG_REPL_FIXUP) || operation_is_flag_set(operation,OP_FLAG_ACTION_INVOKE_FOR_REPLOP)))
  155. {
  156. dblayer_lock_backend(be);
  157. dblock_acquired= 1;
  158. }
  159. /* Work out what the new name of the entry will be */
  160. {
  161. const char *newdn= moddn_get_newdn(pb,&dn_olddn,&dn_newrdn,&dn_newsuperiordn);
  162. slapi_sdn_set_dn_passin(&dn_newdn,newdn);
  163. }
  164. rc= 0;
  165. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  166. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
  167. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY);
  168. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_TARGET_ENTRY);
  169. while(rc!=0)
  170. {
  171. /* JCM - copying entries can be expensive... should optimize */
  172. /*
  173. * Some present state information is passed through the PBlock to the
  174. * backend pre-op plugin. To ensure a consistent snapshot of this state
  175. * we wrap the reading of the entry with the dblock.
  176. */
  177. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY))
  178. {
  179. const char *newdn = NULL;
  180. char * newrdn = NULL;
  181. /* see if an entry with the new name already exists */
  182. done_with_pblock_entry(pb,SLAPI_MODRDN_EXISTING_ENTRY); /* Could be through this multiple times */
  183. slapi_sdn_done(&dn_newrdn);
  184. slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
  185. slapi_sdn_init_dn_byref(&dn_newrdn,newrdn);
  186. newdn= moddn_get_newdn(pb,&dn_olddn,&dn_newrdn,&dn_newsuperiordn);
  187. slapi_sdn_set_dn_passin(&dn_newdn,newdn);
  188. new_addr.dn = (char*)slapi_sdn_get_ndn (&dn_newdn);
  189. new_addr.uniqueid = NULL;
  190. ldap_result_code= get_copy_of_entry(pb, &new_addr, &txn, SLAPI_MODRDN_EXISTING_ENTRY, 0);
  191. }
  192. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY))
  193. {
  194. /* find and lock the old parent entry */
  195. done_with_pblock_entry(pb,SLAPI_MODRDN_PARENT_ENTRY); /* Could be through this multiple times */
  196. oldparent_addr.dn = (char*)slapi_sdn_get_ndn (&dn_parentdn);
  197. oldparent_addr.uniqueid = NULL;
  198. ldap_result_code= get_copy_of_entry(pb, &oldparent_addr, &txn, SLAPI_MODRDN_PARENT_ENTRY, !is_replicated_operation);
  199. }
  200. if(slapi_sdn_get_ndn(&dn_newsuperiordn)!=NULL && slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY))
  201. {
  202. /* JCM - Could check that this really is a new superior, and not the same old one. Compare parentdn & newsuperior */
  203. /* find and lock the new parent entry */
  204. done_with_pblock_entry(pb,SLAPI_MODRDN_NEWPARENT_ENTRY); /* Could be through this multiple times */
  205. /* JCMREPL - If this is a replicated operation then should fetch new superior with uniqueid */
  206. slapi_pblock_get (pb, SLAPI_MODRDN_NEWSUPERIOR_ADDRESS, &newsuperior_addr);
  207. ldap_result_code= get_copy_of_entry(pb, newsuperior_addr, &txn, SLAPI_MODRDN_NEWPARENT_ENTRY, !is_replicated_operation);
  208. }
  209. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_TARGET_ENTRY))
  210. {
  211. /* find and lock the entry we are about to modify */
  212. done_with_pblock_entry(pb,SLAPI_MODRDN_TARGET_ENTRY); /* Could be through this multiple times */
  213. slapi_pblock_get (pb, SLAPI_TARGET_ADDRESS, &old_addr);
  214. ldap_result_code= get_copy_of_entry(pb, old_addr, &txn, SLAPI_MODRDN_TARGET_ENTRY, !is_replicated_operation);
  215. if(ldap_result_code==LDAP_OPERATIONS_ERROR)
  216. {
  217. /* JCM - Usually the call to find_entry2modify would generate the result code. */
  218. /* JCM !!! */
  219. goto error_return;
  220. }
  221. }
  222. /* Call the Backend Pre ModRDN plugins */
  223. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  224. rc= plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODRDN_FN);
  225. if(rc==-1)
  226. {
  227. /*
  228. * Plugin indicated some kind of failure,
  229. * or that this Operation became a No-Op.
  230. */
  231. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  232. goto error_return;
  233. }
  234. /*
  235. * (rc!=-1) means that the plugin changed things, so we go around
  236. * the loop once again to get the new present state.
  237. */
  238. /* JCMREPL - Warning: A Plugin could cause an infinite loop by always returning a result code that requires some action. */
  239. }
  240. /* find and lock the entry we are about to modify */
  241. /* JCMREPL - Argh, what happens about the stinking referrals? */
  242. slapi_pblock_get (pb, SLAPI_TARGET_ADDRESS, &old_addr);
  243. e = find_entry2modify( pb, be, old_addr, NULL );
  244. if ( e == NULL )
  245. {
  246. ldap_result_code= -1;
  247. goto error_return; /* error result sent by find_entry2modify() */
  248. }
  249. /* Check that an entry with the same DN doesn't already exist. */
  250. {
  251. Slapi_Entry *entry;
  252. slapi_pblock_get( pb, SLAPI_MODRDN_EXISTING_ENTRY, &entry);
  253. if((entry != NULL) &&
  254. /* allow modrdn even if the src dn and dest dn are identical */
  255. (0 != slapi_sdn_compare((const Slapi_DN *)&dn_newdn,
  256. (const Slapi_DN *)&dn_olddn)))
  257. {
  258. ldap_result_code= LDAP_ALREADY_EXISTS;
  259. goto error_return;
  260. }
  261. }
  262. /* Fetch and lock the parent of the entry that is moving */
  263. oldparent_addr.dn = (char*)slapi_sdn_get_dn (&dn_parentdn);
  264. oldparent_addr.uniqueid = NULL;
  265. parententry = find_entry2modify_only( pb, be, &oldparent_addr, NULL );
  266. modify_init(&parent_modify_context,parententry);
  267. /* Fetch and lock the new parent of the entry that is moving */
  268. if(slapi_sdn_get_ndn(&dn_newsuperiordn)!=NULL)
  269. {
  270. slapi_pblock_get (pb, SLAPI_MODRDN_NEWSUPERIOR_ADDRESS, &newsuperior_addr);
  271. newparententry = find_entry2modify_only( pb, be, newsuperior_addr, NULL);
  272. modify_init(&newparent_modify_context,newparententry);
  273. }
  274. opcsn = operation_get_csn (operation);
  275. if (!is_fixup_operation)
  276. {
  277. if ( opcsn == NULL && operation->o_csngen_handler)
  278. {
  279. /*
  280. * Current op is a user request. Opcsn will be assigned
  281. * if the dn is in an updatable replica.
  282. */
  283. opcsn = entry_assign_operation_csn ( pb, e->ep_entry, parententry ? parententry->ep_entry : NULL );
  284. }
  285. if ( opcsn != NULL )
  286. {
  287. entry_set_maxcsn (e->ep_entry, opcsn);
  288. }
  289. }
  290. /*
  291. * Now that we have the old entry, we reset the old DN and recompute
  292. * the new DN. Why? Because earlier when we computed the new DN, we did
  293. * not have the old entry, so we used the DN that was presented as the
  294. * target DN in the ModRDN operation itself, and we would prefer to
  295. * preserve the case and spacing that are in the actual entry's DN
  296. * instead. Otherwise, a ModRDN operation will potentially change an
  297. * entry's entire DN (at least with respect to case and spacing).
  298. */
  299. slapi_sdn_copy( slapi_entry_get_sdn_const( e->ep_entry ), &dn_olddn );
  300. if (newparententry != NULL) {
  301. /* don't forget we also want to preserve case of new superior */
  302. slapi_sdn_copy(slapi_entry_get_sdn_const(newparententry->ep_entry), &dn_newsuperiordn);
  303. }
  304. slapi_sdn_set_dn_passin(&dn_newdn,
  305. moddn_get_newdn(pb, &dn_olddn, &dn_newrdn, &dn_newsuperiordn));
  306. /* Check that we're allowed to add an entry below the new superior */
  307. if ( newparententry == NULL )
  308. {
  309. /* There may not be a new parent because we don't intend there to be one. */
  310. if(slapi_sdn_get_ndn(&dn_newsuperiordn)!=NULL)
  311. {
  312. /* If the new entry is to be a suffix, and we're root, then it's OK that the new parent doesn't exist */
  313. if(!(slapi_dn_isbesuffix(pb,slapi_sdn_get_ndn(&dn_newdn)) && isroot))
  314. {
  315. /* Here means that we didn't find the parent */
  316. int err = 0;
  317. Slapi_DN ancestordn = {0};
  318. struct backentry *ancestorentry;
  319. ancestorentry= dn2ancestor(be,&dn_newdn,&ancestordn,&txn,&err);
  320. cache_return( &inst->inst_cache, &ancestorentry );
  321. ldap_result_matcheddn= slapi_ch_strdup((char *) slapi_sdn_get_dn(&ancestordn));
  322. ldap_result_code= LDAP_NO_SUCH_OBJECT;
  323. LDAPDebug( LDAP_DEBUG_TRACE, "New superior does not exist matched %s, newsuperior = %s\n",
  324. ldap_result_matcheddn == NULL ? "NULL" : ldap_result_matcheddn, slapi_sdn_get_ndn(&dn_newsuperiordn), 0 );
  325. slapi_sdn_done(&ancestordn);
  326. goto error_return;
  327. }
  328. }
  329. }
  330. else
  331. {
  332. ldap_result_code= plugin_call_acl_plugin (pb, newparententry->ep_entry, NULL, NULL, SLAPI_ACL_ADD, ACLPLUGIN_ACCESS_DEFAULT, &errbuf );
  333. if ( ldap_result_code != LDAP_SUCCESS )
  334. {
  335. ldap_result_message= errbuf;
  336. LDAPDebug( LDAP_DEBUG_TRACE, "No access to new superior.\n", 0, 0, 0 );
  337. goto error_return;
  338. }
  339. }
  340. /* Check that the target entry has a parent */
  341. if ( parententry == NULL )
  342. {
  343. /* If the entry a suffix, and we're root, then it's OK that the parent doesn't exist */
  344. if(!(slapi_dn_isbesuffix(pb,slapi_sdn_get_ndn(&dn_olddn)) && isroot))
  345. {
  346. /* Here means that we didn't find the parent */
  347. if (parententry && parententry->ep_entry)
  348. {
  349. ldap_result_matcheddn = slapi_ch_strdup((char *) slapi_entry_get_dn(parententry->ep_entry));
  350. }
  351. else
  352. {
  353. ldap_result_matcheddn = "NULL";
  354. }
  355. ldap_result_code= LDAP_NO_SUCH_OBJECT;
  356. LDAPDebug( LDAP_DEBUG_TRACE, "Parent does not exist matched %s, parentdn = %s\n",
  357. ldap_result_matcheddn, slapi_sdn_get_ndn(&dn_parentdn), 0 );
  358. goto error_return;
  359. }
  360. }
  361. /* Replicated Operations are allowed to rename entries with children */
  362. if ( !is_replicated_operation && slapi_entry_has_children( e->ep_entry ))
  363. {
  364. ldap_result_code = LDAP_NOT_ALLOWED_ON_NONLEAF;
  365. goto error_return;
  366. }
  367. /*
  368. * JCM - All the child entries must be locked in the cache, so the size of
  369. * subtree that can be renamed is limited by the cache size.
  370. */
  371. /* Save away a copy of the entry, before modifications */
  372. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
  373. /* create a copy of the entry and apply the changes to it */
  374. if ( (ec = backentry_dup( e )) == NULL )
  375. {
  376. ldap_result_code= LDAP_OPERATIONS_ERROR;
  377. goto error_return;
  378. }
  379. /* JCMACL - Should be performed before the child check. */
  380. /* JCMACL - Why is the check performed against the copy, rather than the existing entry? */
  381. /*ldap_result_code = plugin_call_acl_plugin (pb, ec->ep_entry, NULL , NULL , SLAPI_ACL_WRITE, ACLPLUGIN_ACCESS_DEFAULT, &errbuf );*/
  382. ldap_result_code = plugin_call_acl_plugin (pb, ec->ep_entry,
  383. NULL /*attr*/, NULL /*value*/, SLAPI_ACL_WRITE,
  384. ACLPLUGIN_ACCESS_MODRDN, &errbuf );
  385. if ( ldap_result_code != LDAP_SUCCESS )
  386. {
  387. goto error_return;
  388. }
  389. slapi_entry_set_sdn( ec->ep_entry, &dn_newdn );
  390. /* create it in the cache - prevents others from creating it */
  391. if (( cache_add_tentative( &inst->inst_cache, ec, NULL ) != 0 ) &&
  392. /* allow modrdn even if the src dn and dest dn are identical */
  393. ( 0 != slapi_sdn_compare((const Slapi_DN *)&dn_newdn,
  394. (const Slapi_DN *)&dn_olddn)) )
  395. {
  396. /* somebody must've created it between dn2entry() and here */
  397. /* JCMREPL - Hmm... we can't permit this to happen...? */
  398. ldap_result_code= LDAP_ALREADY_EXISTS;
  399. goto error_return;
  400. }
  401. ec_in_cache= 1;
  402. /* Build the list of modifications required to the existing entry */
  403. {
  404. slapi_mods_init(&smods_generated,4);
  405. slapi_mods_init(&smods_generated_wsi,4);
  406. ldap_result_code = moddn_newrdn_mods(pb, slapi_sdn_get_ndn(&dn_olddn), ec, &smods_generated, &smods_generated_wsi,
  407. is_replicated_operation);
  408. if (ldap_result_code != LDAP_SUCCESS) {
  409. if (ldap_result_code == LDAP_UNWILLING_TO_PERFORM)
  410. ldap_result_message = "Modification of old rdn attribute type not allowed.";
  411. goto error_return;
  412. }
  413. /*
  414. * Remove the old entrydn index entry, and add the new one.
  415. */
  416. slapi_mods_add( &smods_generated, LDAP_MOD_DELETE, "entrydn", strlen(backentry_get_ndn(e)), backentry_get_ndn(e));
  417. slapi_mods_add( &smods_generated, LDAP_MOD_REPLACE, "entrydn", strlen(backentry_get_ndn(ec)), backentry_get_ndn(ec));
  418. /*
  419. * Update parentid if we have a new superior.
  420. */
  421. if(slapi_sdn_get_dn(&dn_newsuperiordn)!=NULL) {
  422. char buf[40]; /* Enough for an ID */
  423. if (parententry != NULL) {
  424. sprintf( buf, "%lu", (u_long)parententry->ep_id );
  425. slapi_mods_add_string(&smods_generated, LDAP_MOD_DELETE, "parentid", buf);
  426. }
  427. if (newparententry != NULL) {
  428. sprintf( buf, "%lu", (u_long)newparententry->ep_id );
  429. slapi_mods_add_string(&smods_generated, LDAP_MOD_REPLACE, "parentid", buf);
  430. }
  431. }
  432. }
  433. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  434. slapi_mods_init_byref(&smods_operation_wsi,mods);
  435. /*
  436. * We are about to pass the last abandon test, so from now on we are
  437. * committed to finish this operation. Set status to "will complete"
  438. * before we make our last abandon check to avoid race conditions in
  439. * the code that processes abandon operations.
  440. */
  441. if (operation) {
  442. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  443. }
  444. if ( slapi_op_abandoned( pb ) ) {
  445. goto error_return;
  446. }
  447. /*
  448. * First, we apply the generated mods that do not involve any state information.
  449. */
  450. if ( entry_apply_mods( ec->ep_entry, slapi_mods_get_ldapmods_byref(&smods_generated) ) != 0 )
  451. {
  452. ldap_result_code= LDAP_OPERATIONS_ERROR;
  453. LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_modrdn: entry_apply_mods failed for entry %s\n",
  454. escape_string(slapi_entry_get_dn_const(ec->ep_entry), ebuf), 0, 0);
  455. goto error_return;
  456. }
  457. /*
  458. * Now we apply the generated mods that do involve state information.
  459. */
  460. if (slapi_mods_get_num_mods(&smods_generated_wsi)>0)
  461. {
  462. if (entry_apply_mods_wsi(ec->ep_entry, &smods_generated_wsi, operation_get_csn(operation), is_replicated_operation)!=0)
  463. {
  464. ldap_result_code= LDAP_OPERATIONS_ERROR;
  465. LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_modrdn: entry_apply_mods_wsi failed for entry %s\n",
  466. escape_string(slapi_entry_get_dn_const(ec->ep_entry), ebuf), 0, 0);
  467. goto error_return;
  468. }
  469. }
  470. /*
  471. * Now we apply the operation mods that do involve state information.
  472. * (Operational attributes).
  473. * The following block looks redundent to the one above. But it may
  474. * be necessary - check the comment for version 1.3.16.22.2.76 of
  475. * this file and compare that version with its previous one.
  476. */
  477. if (slapi_mods_get_num_mods(&smods_operation_wsi)>0)
  478. {
  479. if (entry_apply_mods_wsi(ec->ep_entry, &smods_operation_wsi, operation_get_csn(operation), is_replicated_operation)!=0)
  480. {
  481. ldap_result_code= LDAP_OPERATIONS_ERROR;
  482. LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_modrdn: entry_apply_mods_wsi (operational attributes) failed for entry %s\n",
  483. escape_string(slapi_entry_get_dn_const(ec->ep_entry), ebuf), 0, 0);
  484. goto error_return;
  485. }
  486. }
  487. /* check that the entry still obeys the schema */
  488. if ( slapi_entry_schema_check( pb, ec->ep_entry ) != 0 ) {
  489. ldap_result_code = LDAP_OBJECT_CLASS_VIOLATION;
  490. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  491. goto error_return;
  492. }
  493. /* Check attribute syntax if any new values are being added for the new RDN */
  494. if (slapi_mods_get_num_mods(&smods_operation_wsi)>0)
  495. {
  496. if (slapi_mods_syntax_check(pb, smods_generated_wsi.mods, 0) != 0)
  497. {
  498. ldap_result_code = LDAP_INVALID_SYNTAX;
  499. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  500. goto error_return;
  501. }
  502. }
  503. /*
  504. * Update the DN CSN of the entry.
  505. */
  506. entry_add_dncsn(ec->ep_entry,operation_get_csn(operation));
  507. entry_add_rdn_csn(ec->ep_entry,operation_get_csn(operation));
  508. /*
  509. * If the entry has a new superior then the subordinate count
  510. * of the parents must be updated.
  511. */
  512. if(slapi_sdn_get_dn(&dn_newsuperiordn)!=NULL)
  513. {
  514. /*
  515. * Update the subordinate count of the parents to reflect the moved child.
  516. */
  517. if ( parententry!=NULL )
  518. {
  519. retval = parent_update_on_childchange(&parent_modify_context,2,NULL); /* 2==delete */
  520. /* The parent modify context now contains info needed later */
  521. if (0 != retval)
  522. {
  523. goto error_return;
  524. }
  525. }
  526. if ( newparententry!=NULL )
  527. {
  528. retval = parent_update_on_childchange(&newparent_modify_context,1,NULL); /* 1==add */
  529. /* The newparent modify context now contains info needed later */
  530. if (0 != retval)
  531. {
  532. goto error_return;
  533. }
  534. }
  535. }
  536. /*
  537. * If the entry has children then we're going to have to rename them all.
  538. */
  539. if (slapi_entry_has_children( e->ep_entry ))
  540. {
  541. /* JCM - This is where the subtree lock will appear */
  542. children= moddn_get_children(&txn, pb, be, e, &dn_olddn, &child_entries, &child_entry_copies);
  543. /* JCM - Shouldn't we perform an access control check on all the children. */
  544. /* JCMREPL - But, the replication client has total rights over its subtree, so no access check needed. */
  545. /* JCM - A subtree move could break ACIs, static groups, and dynamic groups. */
  546. }
  547. /*
  548. * So, we believe that no code up till here actually added anything
  549. * to persistent store. From now on, we're transacted
  550. */
  551. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++)
  552. {
  553. if (retry_count > 0)
  554. {
  555. dblayer_txn_abort(li,&txn);
  556. /* We're re-trying */
  557. LDAPDebug( LDAP_DEBUG_TRACE, "Modrdn Retrying Transaction\n", 0, 0, 0 );
  558. }
  559. retval = dblayer_txn_begin(li,parent_txn,&txn);
  560. if (0 != retval) {
  561. ldap_result_code= LDAP_OPERATIONS_ERROR;
  562. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  563. goto error_return;
  564. }
  565. /*
  566. * Update the indexes for the entry.
  567. */
  568. retval = modrdn_rename_entry_update_indexes(&txn, pb, li, e, ec, &smods_generated, &smods_generated_wsi, &smods_operation_wsi);
  569. if (DB_LOCK_DEADLOCK == retval)
  570. {
  571. /* Retry txn */
  572. continue;
  573. }
  574. if (retval != 0 )
  575. {
  576. LDAPDebug( LDAP_DEBUG_TRACE, "modrdn_rename_entry_update_indexes failed, err=%d %s\n",
  577. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  578. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  579. ldap_result_code= LDAP_OPERATIONS_ERROR;
  580. }
  581. /*
  582. * add new name to index
  583. */
  584. {
  585. char **rdns;
  586. int i;
  587. if ( (rdns = ldap_explode_rdn( slapi_sdn_get_dn(&dn_newrdn), 0 )) != NULL )
  588. {
  589. for ( i = 0; rdns[i] != NULL; i++ )
  590. {
  591. char *type;
  592. Slapi_Value *svp[2];
  593. Slapi_Value sv;
  594. memset(&sv,0,sizeof(Slapi_Value));
  595. if ( slapi_rdn2typeval( rdns[i], &type, &sv.bv ) != 0 )
  596. {
  597. char ebuf[ BUFSIZ ];
  598. LDAPDebug( LDAP_DEBUG_ANY, "modrdn: rdn2typeval (%s) failed\n",
  599. escape_string( rdns[i], ebuf ), 0, 0 );
  600. goto error_return;
  601. }
  602. svp[0] = &sv;
  603. svp[1] = NULL;
  604. retval = index_addordel_values_sv( be, type, svp, NULL, ec->ep_id, BE_INDEX_ADD, &txn );
  605. if (DB_LOCK_DEADLOCK == retval)
  606. {
  607. /* Retry txn */
  608. continue;
  609. }
  610. if (retval != 0 )
  611. {
  612. LDAPDebug( LDAP_DEBUG_ANY, "modrdn: could not add new value to index, err=%d %s\n",
  613. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  614. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  615. }
  616. }
  617. slapi_ldap_value_free( rdns );
  618. if (DB_LOCK_DEADLOCK == retval)
  619. {
  620. /* Retry txn */
  621. goto error_return;
  622. }
  623. }
  624. }
  625. if (slapi_sdn_get_dn(&dn_newsuperiordn)!=NULL)
  626. {
  627. /* Push out the db modifications from the parent entry */
  628. retval = modify_update_all(be, pb, &parent_modify_context, &txn);
  629. if (DB_LOCK_DEADLOCK == retval)
  630. {
  631. /* Retry txn */
  632. continue;
  633. }
  634. if (0 != retval)
  635. {
  636. LDAPDebug( LDAP_DEBUG_TRACE, "moddn: could not update parent, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  637. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  638. }
  639. /* Push out the db modifications from the new parent entry */
  640. if(retval==0)
  641. {
  642. retval = modify_update_all(be, pb, &newparent_modify_context, &txn);
  643. if (DB_LOCK_DEADLOCK == retval)
  644. {
  645. /* Retry txn */
  646. continue;
  647. }
  648. if (0 != retval)
  649. {
  650. LDAPDebug( LDAP_DEBUG_TRACE, "moddn: could not update parent, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  651. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  652. }
  653. }
  654. }
  655. /*
  656. * Update ancestorid index.
  657. */
  658. if (slapi_sdn_get_dn(&dn_newsuperiordn)!=NULL) {
  659. retval = ldbm_ancestorid_move_subtree(be, &dn_olddn, &dn_newdn, e->ep_id, children, &txn);
  660. if (retval != 0) {
  661. if (retval == DB_LOCK_DEADLOCK) continue;
  662. if (retval == DB_RUNRECOVERY || LDBM_OS_ERR_IS_DISKFULL(retval))
  663. disk_full = 1;
  664. goto error_return;
  665. }
  666. }
  667. /*
  668. * If the entry has children, then rename them all.
  669. */
  670. if (children!=NULL)
  671. {
  672. retval= moddn_rename_children( &txn, pb, be, children, &dn_olddn, &dn_newdn, child_entries, child_entry_copies);
  673. }
  674. if (DB_LOCK_DEADLOCK == retval)
  675. {
  676. /* Retry txn */
  677. continue;
  678. }
  679. if (retval != 0)
  680. {
  681. if (retval == DB_RUNRECOVERY || LDBM_OS_ERR_IS_DISKFULL(retval))
  682. disk_full = 1;
  683. goto error_return;
  684. }
  685. break; /* retval==0, Done, Terminate the loop */
  686. }
  687. if (retry_count == RETRY_TIMES)
  688. {
  689. /* Failed */
  690. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in modrdn\n", 0, 0, 0 );
  691. ldap_result_code= LDAP_OPERATIONS_ERROR;
  692. goto error_return;
  693. }
  694. postentry = slapi_entry_dup( ec->ep_entry );
  695. if(parententry!=NULL)
  696. {
  697. modify_switch_entries( &parent_modify_context,be);
  698. }
  699. if(newparententry!=NULL)
  700. {
  701. modify_switch_entries( &newparent_modify_context,be);
  702. }
  703. retval = dblayer_txn_commit(li,&txn);
  704. if (0 != retval)
  705. {
  706. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  707. ldap_result_code= LDAP_OPERATIONS_ERROR;
  708. goto error_return;
  709. }
  710. if(children!=NULL)
  711. {
  712. int i=0;
  713. for (; child_entries[i]!=NULL; i++) {
  714. cache_unlock_entry( &inst->inst_cache, child_entries[i]) ;
  715. cache_return( &inst->inst_cache, &(child_entries[i]) );
  716. cache_return( &inst->inst_cache, &(child_entry_copies[i]) );
  717. }
  718. }
  719. retval= 0;
  720. #if 0 /* this new entry in the cache can be used for future; don't remove it */
  721. /* remove from cache so that memory can be freed by cache_return */
  722. if (ec_in_cache) {
  723. cache_remove(&inst->inst_cache, ec);
  724. }
  725. #endif
  726. goto common_return;
  727. error_return:
  728. /* result already sent above - just free stuff */
  729. if ( NULL != postentry )
  730. {
  731. slapi_entry_free( postentry );
  732. postentry= NULL;
  733. }
  734. if( ec!=NULL ) {
  735. if (ec_in_cache) {
  736. cache_remove(&inst->inst_cache, ec);
  737. } else {
  738. backentry_free( &ec );
  739. }
  740. }
  741. if(children!=NULL)
  742. {
  743. int i=0;
  744. for(;child_entries[i]!=NULL;i++) {
  745. cache_unlock_entry(&inst->inst_cache, child_entries[i]);
  746. cache_return(&inst->inst_cache, &(child_entries[i]));
  747. if (child_entry_copies[i] != NULL) {
  748. cache_remove(&inst->inst_cache, child_entry_copies[i]);
  749. cache_return( &inst->inst_cache, &(child_entry_copies[i]) );
  750. }
  751. }
  752. }
  753. if (retval == DB_RUNRECOVERY) {
  754. dblayer_remember_disk_filled(li);
  755. ldbm_nasty("ModifyDN",82,retval);
  756. disk_full = 1;
  757. }
  758. if (disk_full)
  759. {
  760. retval = return_on_disk_full(li);
  761. }
  762. else
  763. {
  764. /* It is specifically OK to make this call even when no transaction was in progress */
  765. dblayer_txn_abort(li,&txn); /* abort crashes in case disk full */
  766. retval= SLAPI_FAIL_GENERAL;
  767. }
  768. common_return:
  769. /* Free up the resource we don't need any more */
  770. if(ec_in_cache) {
  771. cache_return( &inst->inst_cache, &ec );
  772. }
  773. /*
  774. * The bepostop is called even if the operation fails.
  775. */
  776. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_MODRDN_FN);
  777. if (ldap_result_code!=-1)
  778. {
  779. slapi_send_ldap_result( pb, ldap_result_code, ldap_result_matcheddn,
  780. ldap_result_message, 0,NULL );
  781. }
  782. slapi_mods_done(&smods_operation_wsi);
  783. slapi_mods_done(&smods_generated);
  784. slapi_mods_done(&smods_generated_wsi);
  785. moddn_unlock_and_return_entries(be,&e,&existingentry);
  786. slapi_ch_free((void**)&child_entries);
  787. slapi_ch_free((void**)&child_entry_copies);
  788. if (ldap_result_matcheddn && 0 != strcmp(ldap_result_matcheddn, "NULL"))
  789. slapi_ch_free((void**)&ldap_result_matcheddn);
  790. idl_free(children);
  791. slapi_sdn_done(&dn_olddn);
  792. slapi_sdn_done(&dn_newdn);
  793. slapi_sdn_done(&dn_newrdn);
  794. slapi_sdn_done(&dn_newsuperiordn);
  795. slapi_sdn_done(&dn_parentdn);
  796. modify_term(&parent_modify_context,be);
  797. modify_term(&newparent_modify_context,be);
  798. done_with_pblock_entry(pb,SLAPI_MODRDN_EXISTING_ENTRY);
  799. done_with_pblock_entry(pb,SLAPI_MODRDN_PARENT_ENTRY);
  800. done_with_pblock_entry(pb,SLAPI_MODRDN_NEWPARENT_ENTRY);
  801. done_with_pblock_entry(pb,SLAPI_MODRDN_TARGET_ENTRY);
  802. if(dblock_acquired)
  803. {
  804. dblayer_unlock_backend(be);
  805. }
  806. slapi_ch_free((void**)&errbuf);
  807. if (retval == 0 && opcsn != NULL && !is_fixup_operation)
  808. {
  809. slapi_pblock_set(pb, SLAPI_URP_NAMING_COLLISION_DN, slapi_ch_strdup (dn));
  810. }
  811. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, postentry );
  812. if (pb->pb_conn)
  813. {
  814. slapi_log_error (SLAPI_LOG_TRACE, "ldbm_back_modrdn", "leave conn=%" NSPRIu64 " op=%d\n", pb->pb_conn->c_connid, operation->o_opid);
  815. }
  816. return retval;
  817. }
  818. /*
  819. * Work out what the new DN of the entry will be.
  820. */
  821. static const char *
  822. moddn_get_newdn(Slapi_PBlock *pb, Slapi_DN *dn_olddn, Slapi_DN *dn_newrdn, Slapi_DN *dn_newsuperiordn)
  823. {
  824. char *newdn;
  825. const char *newrdn= slapi_sdn_get_dn(dn_newrdn);
  826. const char *newsuperiordn= slapi_sdn_get_dn(dn_newsuperiordn);
  827. if( newsuperiordn!=NULL)
  828. {
  829. /* construct the new dn */
  830. if(slapi_dn_isroot(newsuperiordn))
  831. {
  832. newdn= slapi_ch_strdup(newrdn);
  833. }
  834. else
  835. {
  836. newdn= slapi_dn_plus_rdn(newsuperiordn, newrdn); /* JCM - Use Slapi_RDN */
  837. }
  838. }
  839. else
  840. {
  841. /* construct the new dn */
  842. char *pdn;
  843. const char *dn= slapi_sdn_get_dn(dn_olddn);
  844. pdn = slapi_dn_beparent( pb, dn );
  845. if ( pdn != NULL )
  846. {
  847. newdn= slapi_dn_plus_rdn(pdn, newrdn); /* JCM - Use Slapi_RDN */
  848. }
  849. else
  850. {
  851. newdn= slapi_ch_strdup(newrdn);
  852. }
  853. slapi_ch_free( (void**)&pdn );
  854. }
  855. return newdn;
  856. }
  857. /*
  858. * Return the entries to the cache.
  859. */
  860. static void
  861. moddn_unlock_and_return_entries(
  862. backend *be,
  863. struct backentry **targetentry,
  864. struct backentry **existingentry)
  865. {
  866. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  867. /* Something bad happened so we should give back all the entries */
  868. if ( *targetentry!=NULL ) {
  869. cache_unlock_entry(&inst->inst_cache, *targetentry);
  870. cache_return( &inst->inst_cache, targetentry );
  871. *targetentry= NULL;
  872. }
  873. if ( *existingentry!=NULL ) {
  874. cache_return( &inst->inst_cache, existingentry );
  875. *existingentry= NULL;
  876. }
  877. }
  878. /*
  879. * JCM - There was a problem with multi-valued RDNs where
  880. * JCM - there was an intersection of the two sets RDN Components
  881. * JCM - and the deleteoldrdn flag was set. A value was deleted
  882. * JCM - but not re-added because the value is found to already
  883. * JCM - exist.
  884. *
  885. * This function returns 1 if it is necessary to add an RDN value
  886. * to the entry. This is necessary if either:
  887. * 1 the attribute or the value is not present in the entry, or
  888. * 2 the attribute is present, deleteoldrdn is set, and the RDN value
  889. * is in the deleted list.
  890. *
  891. * For example, suppose you rename cn=a to cn=a+sn=b. The cn=a value
  892. * is removed from the entry and then readded.
  893. */
  894. static int
  895. moddn_rdn_add_needed (
  896. struct backentry *ec,
  897. char *type,
  898. struct berval *bvp,
  899. int deleteoldrdn,
  900. Slapi_Mods *smods_wsi
  901. )
  902. {
  903. Slapi_Attr *attr;
  904. LDAPMod *mod;
  905. if (slapi_entry_attr_find(ec->ep_entry, type, &attr) != 0 ||
  906. slapi_attr_value_find( attr, bvp ) != 0 )
  907. {
  908. return 1;
  909. }
  910. if (deleteoldrdn == 0) return 0;
  911. /* in a multi-valued RDN, the RDN value might have been already
  912. * put on the smods_wsi list to be deleted, yet might still be
  913. * in the target RDN.
  914. */
  915. for (mod = slapi_mods_get_first_mod(smods_wsi);
  916. mod != NULL;
  917. mod = slapi_mods_get_next_mod(smods_wsi)) {
  918. if (SLAPI_IS_MOD_DELETE(mod->mod_op) &&
  919. (strcasecmp(mod->mod_type, type) == 0) &&
  920. (mod->mod_bvalues != NULL) &&
  921. (slapi_attr_value_cmp(attr, *mod->mod_bvalues, bvp) == 0)) {
  922. return 1;
  923. }
  924. }
  925. return 0;
  926. }
  927. /*
  928. * Build the list of modifications to apply to the Existing Entry
  929. * With State Information:
  930. * - delete old rdn values from the entry if deleteoldrdn is set
  931. * - add new rdn values to the entry
  932. * Without State Information
  933. * - No changes
  934. */
  935. static int
  936. moddn_newrdn_mods(Slapi_PBlock *pb, const char *olddn, struct backentry *ec, Slapi_Mods *smods, Slapi_Mods *smods_wsi, int is_repl_op)
  937. {
  938. char ebuf[BUFSIZ];
  939. char **rdns = NULL;
  940. char **dns = NULL;
  941. int deleteoldrdn;
  942. char *type = NULL;
  943. char *dn = NULL;
  944. char *newrdn = NULL;
  945. int i;
  946. struct berval *bvps[2];
  947. struct berval bv;
  948. bvps[0] = &bv;
  949. bvps[1] = NULL;
  950. /* slapi_pblock_get( pb, SLAPI_MODRDN_TARGET, &dn ); */
  951. slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
  952. slapi_pblock_get( pb, SLAPI_MODRDN_DELOLDRDN, &deleteoldrdn );
  953. /*
  954. * This loop removes the old RDN of the existing entry.
  955. */
  956. if (deleteoldrdn) {
  957. int baddn = 0; /* set to true if could not parse dn */
  958. int badrdn = 0; /* set to true if could not parse rdn */
  959. dn = slapi_ch_strdup(olddn);
  960. dns = ldap_explode_dn( dn, 0 );
  961. if ( dns != NULL )
  962. {
  963. rdns = ldap_explode_rdn( dns[0], 0 );
  964. if ( rdns != NULL )
  965. {
  966. for ( i = 0; rdns[i] != NULL; i++ )
  967. {
  968. /* delete from entry attributes */
  969. if ( deleteoldrdn && slapi_rdn2typeval( rdns[i], &type, &bv ) == 0 )
  970. {
  971. /* check if user is allowed to modify the specified attribute */
  972. /*
  973. * It would be better to do this check in the front end
  974. * end inside op_shared_rename(), but unfortunately we
  975. * don't have access to the target entry there.
  976. */
  977. if (!op_shared_is_allowed_attr (type, is_repl_op))
  978. {
  979. slapi_ldap_value_free( rdns );
  980. slapi_ldap_value_free( dns );
  981. slapi_ch_free_string(&dn);
  982. return LDAP_UNWILLING_TO_PERFORM;
  983. }
  984. if (strcasecmp (type, SLAPI_ATTR_UNIQUEID) != 0)
  985. slapi_mods_add_modbvps( smods_wsi, LDAP_MOD_DELETE, type, bvps );
  986. }
  987. }
  988. slapi_ldap_value_free( rdns );
  989. }
  990. else
  991. {
  992. badrdn = 1;
  993. }
  994. slapi_ldap_value_free( dns );
  995. }
  996. else
  997. {
  998. baddn = 1;
  999. }
  1000. slapi_ch_free_string(&dn);
  1001. if ( baddn || badrdn )
  1002. {
  1003. LDAPDebug( LDAP_DEBUG_TRACE, "moddn_newrdn_mods failed: olddn=%s baddn=%d badrdn=%d\n",
  1004. escape_string(olddn, ebuf), baddn, badrdn);
  1005. return LDAP_OPERATIONS_ERROR;
  1006. }
  1007. }
  1008. /*
  1009. * add new RDN values to the entry (non-normalized)
  1010. */
  1011. rdns = ldap_explode_rdn( newrdn, 0 );
  1012. if ( rdns != NULL )
  1013. {
  1014. for ( i = 0; rdns[i] != NULL; i++ )
  1015. {
  1016. if ( slapi_rdn2typeval( rdns[i], &type, &bv ) != 0) {
  1017. continue;
  1018. }
  1019. /* add to entry if it's not already there or if was
  1020. * already deleted
  1021. */
  1022. if (moddn_rdn_add_needed(ec, type, &bv,
  1023. deleteoldrdn,
  1024. smods_wsi) == 1) {
  1025. slapi_mods_add_modbvps( smods_wsi, LDAP_MOD_ADD, type, bvps );
  1026. }
  1027. }
  1028. slapi_ldap_value_free( rdns );
  1029. }
  1030. else
  1031. {
  1032. LDAPDebug( LDAP_DEBUG_TRACE, "moddn_newrdn_mods failed: could not parse new rdn %s\n",
  1033. escape_string(newrdn, ebuf), 0, 0);
  1034. return LDAP_OPERATIONS_ERROR;
  1035. }
  1036. return LDAP_SUCCESS;
  1037. }
  1038. static void
  1039. mods_remove_nsuniqueid(Slapi_Mods *smods)
  1040. {
  1041. int i;
  1042. LDAPMod **mods = slapi_mods_get_ldapmods_byref(smods);
  1043. for ( i = 0; mods[i] != NULL; i++ ) {
  1044. if (!strcasecmp(mods[i]->mod_type, SLAPI_ATTR_UNIQUEID)) {
  1045. mods[i]->mod_op = LDAP_MOD_IGNORE;
  1046. }
  1047. }
  1048. }
  1049. /*
  1050. * Update the indexes to reflect the DN change made.
  1051. * e is the entry before, ec the entry after.
  1052. * mods contains the list of attribute change made.
  1053. */
  1054. static int
  1055. modrdn_rename_entry_update_indexes(back_txn *ptxn, Slapi_PBlock *pb, struct ldbminfo *li, struct backentry *e, struct backentry *ec, Slapi_Mods *smods1, Slapi_Mods *smods2, Slapi_Mods *smods3)
  1056. {
  1057. backend *be;
  1058. ldbm_instance *inst;
  1059. int retval= 0;
  1060. char *msg;
  1061. Slapi_Operation *operation;
  1062. int is_ruv = 0; /* True if the current entry is RUV */
  1063. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  1064. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  1065. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  1066. inst = (ldbm_instance *) be->be_instance_info;
  1067. /*
  1068. * Update the ID to Entry index.
  1069. * Note that id2entry_add replaces the entry, so the Entry ID stays the same.
  1070. */
  1071. retval = id2entry_add( be, ec, ptxn );
  1072. if (DB_LOCK_DEADLOCK == retval)
  1073. {
  1074. /* Retry txn */
  1075. goto error_return;
  1076. }
  1077. if (retval != 0)
  1078. {
  1079. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add failed, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  1080. goto error_return;
  1081. }
  1082. if(smods1!=NULL && slapi_mods_get_num_mods(smods1)>0)
  1083. {
  1084. /*
  1085. * update the indexes: lastmod, rdn, etc.
  1086. */
  1087. retval = index_add_mods( be, (const LDAPMod **)slapi_mods_get_ldapmods_byref(smods1), e, ec, ptxn );
  1088. if (DB_LOCK_DEADLOCK == retval)
  1089. {
  1090. /* Retry txn */
  1091. goto error_return;
  1092. }
  1093. if (retval != 0)
  1094. {
  1095. LDAPDebug( LDAP_DEBUG_TRACE, "index_add_mods 1 failed, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  1096. goto error_return;
  1097. }
  1098. }
  1099. if(smods2!=NULL && slapi_mods_get_num_mods(smods2)>0)
  1100. {
  1101. /*
  1102. * smods2 contains the state generated mods. One of them might be the removal of a "nsuniqueid" rdn component
  1103. * previously gnerated through a conflict resolution. We need to make sure we don't remove the index for "nsuniqueid"
  1104. * so let's get it out from the mods before calling index_add_mods...
  1105. */
  1106. mods_remove_nsuniqueid(smods2);
  1107. /*
  1108. * update the indexes: lastmod, rdn, etc.
  1109. */
  1110. retval = index_add_mods( be, (const LDAPMod **)slapi_mods_get_ldapmods_byref(smods2), e, ec, ptxn );
  1111. if (DB_LOCK_DEADLOCK == retval)
  1112. {
  1113. /* Retry txn */
  1114. goto error_return;
  1115. }
  1116. if (retval != 0)
  1117. {
  1118. LDAPDebug( LDAP_DEBUG_TRACE, "index_add_mods 2 failed, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  1119. goto error_return;
  1120. }
  1121. }
  1122. if(smods3!=NULL && slapi_mods_get_num_mods(smods3)>0)
  1123. {
  1124. /*
  1125. * update the indexes: lastmod, rdn, etc.
  1126. */
  1127. retval = index_add_mods( be, (const LDAPMod **)slapi_mods_get_ldapmods_byref(smods3), e, ec, ptxn );
  1128. if (DB_LOCK_DEADLOCK == retval)
  1129. {
  1130. /* Retry txn */
  1131. goto error_return;
  1132. }
  1133. if (retval != 0)
  1134. {
  1135. LDAPDebug( LDAP_DEBUG_TRACE, "index_add_mods 3 failed, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  1136. goto error_return;
  1137. }
  1138. }
  1139. /*
  1140. * Remove the old entry from the Virtual List View indexes.
  1141. * Add the new entry to the Virtual List View indexes.
  1142. * If ruv, we don't have to update vlv.
  1143. */
  1144. if (!is_ruv)
  1145. {
  1146. retval= vlv_update_all_indexes(ptxn, be, pb, e, ec);
  1147. if (DB_LOCK_DEADLOCK == retval)
  1148. {
  1149. /* Abort and re-try */
  1150. goto error_return;
  1151. }
  1152. if (retval != 0)
  1153. {
  1154. LDAPDebug( LDAP_DEBUG_TRACE, "vlv_update_all_indexes failed, err=%d %s\n", retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  1155. goto error_return;
  1156. }
  1157. }
  1158. if (cache_replace( &inst->inst_cache, e, ec ) != 0 ) {
  1159. retval= -1;
  1160. goto error_return;
  1161. }
  1162. error_return:
  1163. return retval;
  1164. }
  1165. /*
  1166. */
  1167. static int
  1168. moddn_rename_child_entry(
  1169. back_txn *ptxn,
  1170. Slapi_PBlock *pb,
  1171. struct ldbminfo *li,
  1172. struct backentry *e,
  1173. struct backentry *ec,
  1174. int parentdncomps,
  1175. char **newsuperiordns,
  1176. int newsuperiordncomps,
  1177. CSN *opcsn)
  1178. {
  1179. /*
  1180. * Construct the new DN for the entry by taking the old DN
  1181. * excluding the old parent entry DN, and adding the new
  1182. * superior entry DN.
  1183. *
  1184. * ldap_explode_dn is probably a bit slow, but it knows about
  1185. * DN escaping which is pretty complicated, and we wouldn't
  1186. * want to reimplement that here.
  1187. *
  1188. * JCM - This was written before Slapi_RDN... so this could be made much neater.
  1189. */
  1190. int retval;
  1191. char *olddn;
  1192. char *newdn;
  1193. char **olddns;
  1194. int olddncomps= 0;
  1195. int need= 1; /* For the '\0' */
  1196. int i;
  1197. olddn = slapi_entry_get_dn(ec->ep_entry);
  1198. olddns = ldap_explode_dn( olddn, 0 );
  1199. for(;olddns[olddncomps]!=NULL;olddncomps++);
  1200. for(i=0;i<olddncomps-parentdncomps;i++)
  1201. {
  1202. need+= strlen(olddns[i]) + 2; /* For the ", " */
  1203. }
  1204. for(i=0;i<newsuperiordncomps;i++)
  1205. {
  1206. need+= strlen(newsuperiordns[i]) + 2; /* For the ", " */
  1207. }
  1208. need--; /* We don't have a comma on the end of the last component */
  1209. newdn= slapi_ch_malloc(need);
  1210. newdn[0]= '\0';
  1211. for(i=0;i<olddncomps-parentdncomps;i++)
  1212. {
  1213. strcat(newdn,olddns[i]);
  1214. strcat(newdn,", ");
  1215. }
  1216. for(i=0;i<newsuperiordncomps;i++)
  1217. {
  1218. strcat(newdn,newsuperiordns[i]);
  1219. if(i<newsuperiordncomps-1)
  1220. {
  1221. /* We don't have a comma on the end of the last component */
  1222. strcat(newdn,", ");
  1223. }
  1224. }
  1225. slapi_ldap_value_free( olddns );
  1226. slapi_entry_set_dn( ec->ep_entry, newdn );
  1227. add_update_entrydn_operational_attributes (ec);
  1228. /*
  1229. * Update the DN CSN of the entry.
  1230. */
  1231. {
  1232. entry_add_dncsn(e->ep_entry, opcsn);
  1233. entry_add_rdn_csn(e->ep_entry, opcsn);
  1234. entry_set_maxcsn(e->ep_entry, opcsn);
  1235. }
  1236. {
  1237. Slapi_Mods smods;
  1238. slapi_mods_init(&smods, 2);
  1239. slapi_mods_add( &smods, LDAP_MOD_DELETE, "entrydn", strlen( backentry_get_ndn(e) ), backentry_get_ndn(e) );
  1240. slapi_mods_add( &smods, LDAP_MOD_REPLACE, "entrydn", strlen( backentry_get_ndn(ec) ), backentry_get_ndn(ec) );
  1241. /*
  1242. * Update all the indexes.
  1243. */
  1244. retval= modrdn_rename_entry_update_indexes(ptxn, pb, li, e, ec, &smods, NULL, NULL); /* JCMREPL - Should the children get updated modifiersname and lastmodifiedtime? */
  1245. slapi_mods_done(&smods);
  1246. }
  1247. return retval;
  1248. }
  1249. /*
  1250. * Rename all the children of an entry who's name has changed.
  1251. */
  1252. static int
  1253. moddn_rename_children(
  1254. back_txn *ptxn,
  1255. Slapi_PBlock *pb,
  1256. backend *be,
  1257. IDList *children,
  1258. Slapi_DN *dn_parentdn,
  1259. Slapi_DN *dn_newsuperiordn,
  1260. struct backentry *child_entries[],
  1261. struct backentry *child_entry_copies[])
  1262. {
  1263. /* Iterate over the children list renaming every child */
  1264. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  1265. Slapi_Operation *operation;
  1266. CSN *opcsn;
  1267. int retval= 0, i;
  1268. char **newsuperiordns;
  1269. int newsuperiordncomps= 0;
  1270. int parentdncomps= 0;
  1271. /*
  1272. * Break down the parent entry dn into its components.
  1273. */
  1274. {
  1275. char **parentdns;
  1276. parentdns = ldap_explode_dn( slapi_sdn_get_dn(dn_parentdn), 0 );
  1277. for(;parentdns[parentdncomps]!=NULL;parentdncomps++);
  1278. slapi_ldap_value_free( parentdns );
  1279. }
  1280. /*
  1281. * Break down the new superior entry dn into its components.
  1282. */
  1283. newsuperiordns = ldap_explode_dn( slapi_sdn_get_dn(dn_newsuperiordn), 0 );
  1284. for(;newsuperiordns[newsuperiordncomps]!=NULL;newsuperiordncomps++);
  1285. /*
  1286. * Iterate over the child entries renaming them.
  1287. */
  1288. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  1289. opcsn = operation_get_csn (operation);
  1290. for (i = 0; retval == 0 && child_entries[i] != NULL; i++) {
  1291. retval= moddn_rename_child_entry(ptxn, pb, li, child_entries[i], child_entry_copies[i], parentdncomps, newsuperiordns, newsuperiordncomps, opcsn );
  1292. }
  1293. if (retval != 0) {
  1294. while (child_entries[i] != NULL) {
  1295. backentry_free(&(child_entry_copies[i]));
  1296. i++;
  1297. }
  1298. }
  1299. slapi_ldap_value_free( newsuperiordns );
  1300. return retval;
  1301. }
  1302. /*
  1303. * Get an IDList of all the children of an entry.
  1304. */
  1305. static IDList *
  1306. moddn_get_children(back_txn *ptxn, Slapi_PBlock *pb, backend *be, struct backentry *parententry, Slapi_DN *dn_parentdn, struct backentry ***child_entries, struct backentry ***child_entry_copies)
  1307. {
  1308. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  1309. int err= 0;
  1310. IDList *candidates;
  1311. IDList *result_idl = NULL;
  1312. char filterstr[20];
  1313. Slapi_Filter *filter;
  1314. NIDS nids;
  1315. int entrynumber= 0;
  1316. ID id;
  1317. idl_iterator sr_current; /* the current position in the search results */
  1318. struct backentry *e= NULL;
  1319. /* Fetch a candidate list of all the entries below the entry being moved */
  1320. strcpy( filterstr, "objectclass=*" );
  1321. filter = slapi_str2filter( filterstr );
  1322. candidates= subtree_candidates(pb, be, slapi_sdn_get_ndn(dn_parentdn), parententry, filter, 1 /* ManageDSAIT */, NULL /* allids_before_scopingp */, &err);
  1323. slapi_filter_free(filter,1);
  1324. if (candidates!=NULL)
  1325. {
  1326. sr_current = idl_iterator_init(candidates);
  1327. result_idl= idl_alloc(candidates->b_nids);
  1328. do
  1329. {
  1330. id = idl_iterator_dereference_increment(&sr_current, candidates);
  1331. if ( id!=NOID )
  1332. {
  1333. int err= 0;
  1334. e = id2entry( be, id, NULL, &err );
  1335. if (e!=NULL)
  1336. {
  1337. /* The subtree search will have included the parent entry in the result set */
  1338. if (e!=parententry)
  1339. {
  1340. /* Check that the candidate entry is really below the base. */
  1341. if(slapi_dn_issuffix( backentry_get_ndn(e), slapi_sdn_get_ndn(dn_parentdn)))
  1342. {
  1343. idl_append(result_idl,id);
  1344. }
  1345. }
  1346. cache_return(&inst->inst_cache, &e);
  1347. }
  1348. }
  1349. } while (id!=NOID);
  1350. idl_free(candidates);
  1351. }
  1352. nids = result_idl ? result_idl->b_nids : 0;
  1353. *child_entries= (struct backentry**)slapi_ch_calloc(sizeof(struct backentry*),nids+1);
  1354. *child_entry_copies= (struct backentry**)slapi_ch_calloc(sizeof(struct backentry*),nids+1);
  1355. sr_current = idl_iterator_init(result_idl);
  1356. do {
  1357. id = idl_iterator_dereference_increment(&sr_current, result_idl);
  1358. if ( id!=NOID ) {
  1359. e= cache_find_id( &inst->inst_cache, id );
  1360. if ( e != NULL ) {
  1361. cache_lock_entry(&inst->inst_cache, e);
  1362. (*child_entries)[entrynumber]= e;
  1363. (*child_entry_copies)[entrynumber]= backentry_dup(e);
  1364. entrynumber++;
  1365. }
  1366. }
  1367. } while (id!=NOID);
  1368. return result_idl;
  1369. }