ldbm_modrdn.c 82 KB

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