ldbm_modrdn.c 65 KB

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