ldbm_modify.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. /* modify.c - ldbm backend modify routine */
  43. #include "back-ldbm.h"
  44. extern char *numsubordinates;
  45. extern char *hassubordinates;
  46. static void remove_illegal_mods(LDAPMod **mods);
  47. static int mods_have_effect (Slapi_Entry *entry, Slapi_Mods *smods);
  48. #define MOD_SET_ERROR(rc, error, count) \
  49. { \
  50. (rc) = (error); \
  51. (count) = RETRY_TIMES; /* otherwise, the transaction may not be aborted */ \
  52. }
  53. /* Modify context structure constructor, sans allocation */
  54. void modify_init(modify_context *mc,struct backentry *old_entry)
  55. {
  56. /* Store the old entry */
  57. PR_ASSERT(NULL == mc->old_entry);
  58. PR_ASSERT(NULL == mc->new_entry);
  59. mc->old_entry = old_entry;
  60. mc->attr_encrypt = 1;
  61. }
  62. int modify_apply_mods(modify_context *mc, Slapi_Mods *smods)
  63. {
  64. return modify_apply_mods_ignore_error(mc, smods, -1);
  65. }
  66. int modify_apply_mods_ignore_error(modify_context *mc, Slapi_Mods *smods, int error)
  67. {
  68. int ret = 0;
  69. /* Make a copy of the entry */
  70. PR_ASSERT(mc->old_entry != NULL);
  71. PR_ASSERT(mc->new_entry == NULL);
  72. mc->new_entry = backentry_dup(mc->old_entry);
  73. PR_ASSERT(smods!=NULL);
  74. if ( mods_have_effect (mc->new_entry->ep_entry, smods) ) {
  75. ret = entry_apply_mods_ignore_error( mc->new_entry->ep_entry, slapi_mods_get_ldapmods_byref(smods), error);
  76. }
  77. mc->smods= smods;
  78. return ret;
  79. }
  80. /* Modify context structure destructor */
  81. int modify_term(modify_context *mc,struct backend *be)
  82. {
  83. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  84. slapi_mods_free(&mc->smods);
  85. /* Unlock and return entries */
  86. if (mc->old_entry) {
  87. cache_unlock_entry(&inst->inst_cache, mc->old_entry);
  88. CACHE_RETURN( &(inst->inst_cache), &(mc->old_entry) );
  89. mc->old_entry= NULL;
  90. }
  91. CACHE_RETURN( &(inst->inst_cache), &(mc->new_entry) );
  92. mc->new_entry= NULL;
  93. return 0;
  94. }
  95. /* Modify context structure member to switch entries in the cache */
  96. int modify_switch_entries(modify_context *mc,backend *be)
  97. {
  98. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  99. int ret = 0;
  100. if (mc->old_entry && mc->new_entry) {
  101. ret = cache_replace(&(inst->inst_cache), mc->old_entry, mc->new_entry);
  102. if (ret){
  103. LDAPDebug(LDAP_DEBUG_CACHE, "modify_switch_entries: replacing %s with %s failed (%d)\n",
  104. slapi_entry_get_dn(mc->old_entry->ep_entry),
  105. slapi_entry_get_dn(mc->new_entry->ep_entry), ret);
  106. }
  107. }
  108. return ret;
  109. }
  110. /*
  111. * Switch the new with the old(original) - undoing modify_switch_entries()
  112. * This expects modify_term() to be called next, as the old "new" entry
  113. * is now gone(replaced by the original entry).
  114. */
  115. int
  116. modify_unswitch_entries(modify_context *mc,backend *be)
  117. {
  118. struct backentry *tmp_be;
  119. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  120. int ret = 0;
  121. if (mc->old_entry && mc->new_entry &&
  122. cache_is_in_cache(&inst->inst_cache, mc->new_entry)) {
  123. /* switch the entries, and reset the new, new, entry */
  124. tmp_be = mc->new_entry;
  125. mc->new_entry = mc->old_entry;
  126. mc->new_entry->ep_state = 0;
  127. if (cache_has_otherref(&(inst->inst_cache), mc->new_entry)) {
  128. /* some other thread refers the entry */
  129. CACHE_RETURN(&(inst->inst_cache), &(mc->new_entry));
  130. } else {
  131. /* don't call CACHE_RETURN, that frees the entry! */
  132. mc->new_entry->ep_refcnt = 0;
  133. }
  134. mc->old_entry = tmp_be;
  135. ret = cache_replace(&(inst->inst_cache), mc->old_entry, mc->new_entry);
  136. if (ret == 0) {
  137. /*
  138. * The new entry was originally locked, so since we did the
  139. * switch we need to unlock the "new" entry, and return the
  140. * "old" one. modify_term() will then return the "new" entry.
  141. */
  142. cache_unlock_entry(&inst->inst_cache, mc->new_entry);
  143. cache_lock_entry(&inst->inst_cache, mc->old_entry);
  144. } else {
  145. LDAPDebug(LDAP_DEBUG_CACHE, "modify_unswitch_entries: replacing %s with %s failed (%d)\n",
  146. slapi_entry_get_dn(mc->old_entry->ep_entry),
  147. slapi_entry_get_dn(mc->new_entry->ep_entry), ret);
  148. }
  149. }
  150. return ret;
  151. }
  152. /* This routine does that part of a modify operation which involves
  153. updating the on-disk data: updates idices, id2entry.
  154. Copes properly with DB_LOCK_DEADLOCK. The caller must be able to cope with
  155. DB_LOCK_DEADLOCK returned.
  156. The caller is presumed to proceed as follows:
  157. Find the entry you want to modify;
  158. Lock it for modify;
  159. Make a copy of it; (call backentry_dup() )
  160. Apply modifications to the copy in memory (call entry_apply_mods() )
  161. begin transaction;
  162. Do any other mods to on-disk data you want
  163. Call this routine;
  164. Commit transaction;
  165. You pass it environment data: struct ldbminfo, pb (not sure why, but the vlv code seems to need it)
  166. the copy of the entry before modfication, the entry after modification;
  167. an LDAPMods array containing the modifications performed
  168. */
  169. int modify_update_all(backend *be, Slapi_PBlock *pb,
  170. modify_context *mc,
  171. back_txn *txn)
  172. {
  173. static char *function_name = "modify_update_all";
  174. Slapi_Operation *operation;
  175. int is_ruv = 0; /* True if the current entry is RUV */
  176. int retval = 0;
  177. if (pb) { /* pb could be NULL if it's called from import */
  178. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  179. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  180. }
  181. /*
  182. * Update the ID to Entry index.
  183. * Note that id2entry_add replaces the entry, so the Entry ID stays the same.
  184. */
  185. retval = id2entry_add_ext( be, mc->new_entry, txn, mc->attr_encrypt, NULL );
  186. if ( 0 != retval ) {
  187. if (DB_LOCK_DEADLOCK != retval)
  188. {
  189. ldbm_nasty(function_name,66,retval);
  190. }
  191. goto error;
  192. }
  193. retval = index_add_mods( be, slapi_mods_get_ldapmods_byref(mc->smods), mc->old_entry, mc->new_entry, txn );
  194. if ( 0 != retval ) {
  195. if (DB_LOCK_DEADLOCK != retval)
  196. {
  197. ldbm_nasty(function_name,65,retval);
  198. }
  199. goto error;
  200. }
  201. /*
  202. * Remove the old entry from the Virtual List View indexes.
  203. * Add the new entry to the Virtual List View indexes.
  204. * Because the VLV code calls slapi_filter_test(), which requires a pb (why?),
  205. * we allow the caller sans pb to get everything except vlv indexing.
  206. */
  207. if (NULL != pb && !is_ruv) {
  208. retval= vlv_update_all_indexes(txn, be, pb, mc->old_entry, mc->new_entry);
  209. if ( 0 != retval ) {
  210. if (DB_LOCK_DEADLOCK != retval)
  211. {
  212. ldbm_nasty(function_name,64,retval);
  213. }
  214. goto error;
  215. }
  216. }
  217. error:
  218. return retval;
  219. }
  220. /**
  221. Apply the mods to the ec entry. Check for syntax, schema problems.
  222. Check for abandon.
  223. Return code:
  224. -1 - error - result code and message are set appropriately
  225. 0 - successfully applied and checked
  226. 1 - not an error - no mods to apply or op abandoned
  227. */
  228. static int
  229. modify_apply_check_expand(
  230. Slapi_PBlock *pb,
  231. Slapi_Operation *operation,
  232. LDAPMod **mods, /* list of mods to apply */
  233. struct backentry *e, /* original "before" entry */
  234. struct backentry *ec, /* "after" entry with mods applied */
  235. Slapi_Entry **postentry,
  236. int *ldap_result_code,
  237. char **ldap_result_message
  238. )
  239. {
  240. int rc = 0;
  241. int i;
  242. int repl_op;
  243. int change_entry = 0;
  244. Slapi_Mods smods = {0};
  245. CSN *csn = operation_get_csn(operation);
  246. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  247. slapi_mods_init_byref( &smods, mods );
  248. if ( (change_entry = mods_have_effect (ec->ep_entry, &smods)) ) {
  249. *ldap_result_code = entry_apply_mods_wsi(ec->ep_entry, &smods, csn,
  250. operation_is_flag_set(operation, OP_FLAG_REPLICATED));
  251. /*
  252. * XXXmcs: it would be nice to get back an error message from
  253. * the above call so we could pass it along to the client, e.g.,
  254. * "duplicate value for attribute givenName."
  255. */
  256. } else {
  257. Slapi_Entry *epostop = NULL;
  258. /* If the entry was not actually changed, we still need to
  259. * set the SLAPI_ENTRY_POST_OP field in the pblock (post-op
  260. * plugins expect that field to be present for all modify
  261. * operations that return LDAP_SUCCESS).
  262. */
  263. slapi_pblock_get ( pb, SLAPI_ENTRY_POST_OP, &epostop );
  264. slapi_entry_free ( epostop ); /* free existing one, if any */
  265. slapi_pblock_set ( pb, SLAPI_ENTRY_POST_OP, slapi_entry_dup( e->ep_entry ) );
  266. *postentry = NULL; /* to avoid free in main error cleanup code */
  267. }
  268. if ( !change_entry || *ldap_result_code != 0 ) {
  269. /* change_entry == 0 is not an error just a no-op */
  270. rc = change_entry ? -1 : 1;
  271. goto done;
  272. }
  273. /*
  274. * If the objectClass attribute type was modified in any way, expand
  275. * the objectClass values to reflect the inheritance hierarchy.
  276. */
  277. for ( i = 0; mods && mods[i]; ++i ) {
  278. if ( 0 == strcasecmp( SLAPI_ATTR_OBJECTCLASS, mods[i]->mod_type )) {
  279. slapi_schema_expand_objectclasses( ec->ep_entry );
  280. break;
  281. }
  282. }
  283. /*
  284. * We are about to pass the last abandon test, so from now on we are
  285. * committed to finish this operation. Set status to "will complete"
  286. * before we make our last abandon check to avoid race conditions in
  287. * the code that processes abandon operations.
  288. */
  289. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  290. if ( slapi_op_abandoned( pb ) ) {
  291. rc = 1;
  292. goto done;
  293. }
  294. /* if this is a replicated op, we don't need to perform these checks */
  295. if(!repl_op){
  296. /* check that the entry still obeys the schema */
  297. if ((operation_is_flag_set(operation,OP_FLAG_ACTION_SCHEMA_CHECK)) &&
  298. slapi_entry_schema_check( pb, ec->ep_entry ) != 0 ) {
  299. *ldap_result_code = LDAP_OBJECT_CLASS_VIOLATION;
  300. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, ldap_result_message);
  301. rc = -1;
  302. goto done;
  303. }
  304. /* check attribute syntax for the new values */
  305. if (slapi_mods_syntax_check(pb, mods, 0) != 0) {
  306. *ldap_result_code = LDAP_INVALID_SYNTAX;
  307. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, ldap_result_message);
  308. rc = -1;
  309. goto done;
  310. }
  311. /*
  312. * make sure the entry contains all values in the RDN.
  313. * if not, the modification must have removed them.
  314. */
  315. if ( ! slapi_entry_rdn_values_present( ec->ep_entry ) ) {
  316. *ldap_result_code= LDAP_NOT_ALLOWED_ON_RDN;
  317. rc = -1;
  318. goto done;
  319. }
  320. }
  321. done:
  322. slapi_mods_done( &smods );
  323. return rc;
  324. }
  325. int
  326. ldbm_back_modify( Slapi_PBlock *pb )
  327. {
  328. backend *be;
  329. ldbm_instance *inst = NULL;
  330. struct ldbminfo *li;
  331. struct backentry *e = NULL, *ec = NULL;
  332. struct backentry *original_entry = NULL, *tmpentry = NULL;;
  333. Slapi_Entry *postentry = NULL;
  334. LDAPMod **mods = NULL;
  335. LDAPMod **mods_original = NULL;
  336. Slapi_Mods smods = {0};
  337. back_txn txn;
  338. back_txnid parent_txn;
  339. modify_context ruv_c = {0};
  340. int ruv_c_init = 0;
  341. int retval = -1;
  342. char *msg;
  343. char *errbuf = NULL;
  344. int retry_count = 0;
  345. int disk_full = 0;
  346. int ldap_result_code= LDAP_SUCCESS;
  347. char *ldap_result_message= NULL;
  348. int rc = 0;
  349. Slapi_Operation *operation;
  350. int dblock_acquired= 0;
  351. entry_address *addr;
  352. int is_fixup_operation= 0;
  353. int is_ruv = 0; /* True if the current entry is RUV */
  354. CSN *opcsn = NULL;
  355. int repl_op;
  356. int opreturn = 0;
  357. int mod_count = 0;
  358. int ec_locked = 0;
  359. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  360. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  361. slapi_pblock_get( pb, SLAPI_TARGET_ADDRESS, &addr );
  362. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  363. slapi_pblock_get( pb, SLAPI_TXN, (void**)&parent_txn );
  364. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  365. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  366. dblayer_txn_init(li,&txn); /* must do this before first goto error_return */
  367. /* the calls to perform searches require the parent txn if any
  368. so set txn to the parent_txn until we begin the child transaction */
  369. if (parent_txn) {
  370. txn.back_txn_txn = parent_txn;
  371. } else {
  372. parent_txn = txn.back_txn_txn;
  373. slapi_pblock_set( pb, SLAPI_TXN, parent_txn );
  374. }
  375. if (NULL == operation)
  376. {
  377. ldap_result_code = LDAP_OPERATIONS_ERROR;
  378. goto error_return;
  379. }
  380. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  381. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  382. inst = (ldbm_instance *) be->be_instance_info;
  383. if (NULL == addr)
  384. {
  385. goto error_return;
  386. }
  387. /* no need to check the dn syntax as this is a replicated op */
  388. if(!repl_op){
  389. ldap_result_code = slapi_dn_syntax_check(pb, slapi_sdn_get_dn(addr->sdn), 1);
  390. if (ldap_result_code)
  391. {
  392. ldap_result_code = LDAP_INVALID_DN_SYNTAX;
  393. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  394. goto error_return;
  395. }
  396. }
  397. /* The dblock serializes writes to the database,
  398. * which reduces deadlocking in the db code,
  399. * which means that we run faster.
  400. *
  401. * But, this lock is re-enterant for the fixup
  402. * operations that the URP code in the Replication
  403. * plugin generates.
  404. */
  405. if(SERIALLOCK(li) && (!is_fixup_operation || is_ruv)) {
  406. dblayer_lock_backend(be);
  407. dblock_acquired= 1;
  408. }
  409. /* find and lock the entry we are about to modify */
  410. if ( (e = find_entry2modify( pb, be, addr, &txn )) == NULL ) {
  411. ldap_result_code= -1;
  412. goto error_return; /* error result sent by find_entry2modify() */
  413. }
  414. if (!is_fixup_operation) {
  415. if (!repl_op && slapi_entry_flag_is_set(e->ep_entry, SLAPI_ENTRY_FLAG_TOMBSTONE)) {
  416. ldap_result_code = LDAP_UNWILLING_TO_PERFORM;
  417. ldap_result_message = "Operation not allowed on tombstone entry.";
  418. slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_modify",
  419. "Attempt to modify a tombstone entry %s\n", slapi_sdn_get_dn(slapi_entry_get_sdn_const( e->ep_entry )));
  420. goto error_return;
  421. }
  422. opcsn = operation_get_csn (operation);
  423. if (NULL == opcsn && operation->o_csngen_handler)
  424. {
  425. /*
  426. * Current op is a user request. Opcsn will be assigned
  427. * if the dn is in an updatable replica.
  428. */
  429. opcsn = entry_assign_operation_csn ( pb, e->ep_entry, NULL );
  430. }
  431. if (opcsn)
  432. {
  433. entry_set_maxcsn (e->ep_entry, opcsn);
  434. }
  435. }
  436. /* Save away a copy of the entry, before modifications */
  437. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
  438. if ( (ldap_result_code = plugin_call_acl_mods_access( pb, e->ep_entry, mods, &errbuf)) != LDAP_SUCCESS ) {
  439. ldap_result_message= errbuf;
  440. goto error_return;
  441. }
  442. /* create a copy of the entry and apply the changes to it */
  443. if ( (ec = backentry_dup( e )) == NULL ) {
  444. ldap_result_code= LDAP_OPERATIONS_ERROR;
  445. goto error_return;
  446. }
  447. if(!repl_op){
  448. remove_illegal_mods(mods);
  449. }
  450. /* ec is the entry that our bepreop should get to mess with */
  451. slapi_pblock_set( pb, SLAPI_MODIFY_EXISTING_ENTRY, ec->ep_entry );
  452. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  453. if ((opreturn = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODIFY_FN)) ||
  454. (slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code) && ldap_result_code) ||
  455. (slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn) && opreturn)) {
  456. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  457. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  458. if (!ldap_result_code) {
  459. LDAPDebug0Args(LDAP_DEBUG_ANY, "ldbm_back_modify: SLAPI_PLUGIN_BE_PRE_MODIFY_FN "
  460. "returned error but did not set SLAPI_RESULT_CODE\n");
  461. ldap_result_code = LDAP_OPERATIONS_ERROR;
  462. }
  463. if (!opreturn) {
  464. opreturn = -1;
  465. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  466. }
  467. }
  468. /* The Plugin may have messed about with some of the PBlock parameters... ie. mods */
  469. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  470. slapi_mods_init_byref(&smods,mods);
  471. mod_count = slapi_mods_get_num_mods(&smods);
  472. /* apply the mods, check for syntax, schema problems, etc. */
  473. if (modify_apply_check_expand(pb, operation, mods, e, ec, &postentry,
  474. &ldap_result_code, &ldap_result_message)) {
  475. goto error_return;
  476. }
  477. /*
  478. * Grab a copy of the mods and the entry in case the be_txn_preop changes
  479. * the them. If we have a failure, then we need to reset the mods to their
  480. * their original state;
  481. */
  482. mods_original = copy_mods(mods);
  483. if ( (original_entry = backentry_dup( ec )) == NULL ) {
  484. ldap_result_code= LDAP_OPERATIONS_ERROR;
  485. goto error_return;
  486. }
  487. txn.back_txn_txn = NULL; /* ready to create the child transaction */
  488. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  489. int cache_rc = 0;
  490. int new_mod_count = 0;
  491. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  492. dblayer_txn_abort(li,&txn);
  493. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  494. /*
  495. * Since be_txn_preop functions could have modified the entry/mods,
  496. * We need to grab the current mods, free them, and restore the
  497. * originals. Same thing for the entry.
  498. */
  499. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  500. ldap_mods_free(mods, 1);
  501. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, copy_mods(mods_original));
  502. /* reset ec set cache in id2entry_add_ext */
  503. if (ec) {
  504. /* must duplicate ec before returning it to cache,
  505. * which could free the entry. */
  506. if ( (tmpentry = backentry_dup( ec )) == NULL ) {
  507. ldap_result_code= LDAP_OPERATIONS_ERROR;
  508. goto error_return;
  509. }
  510. if (cache_is_in_cache(&inst->inst_cache, ec)) {
  511. CACHE_REMOVE(&inst->inst_cache, ec);
  512. }
  513. CACHE_RETURN(&inst->inst_cache, &ec);
  514. ec = original_entry;
  515. original_entry = tmpentry;
  516. tmpentry = NULL;
  517. }
  518. if (ruv_c_init) {
  519. /* reset the ruv txn stuff */
  520. modify_term(&ruv_c, be);
  521. ruv_c_init = 0;
  522. }
  523. LDAPDebug0Args(LDAP_DEBUG_BACKLDBM,
  524. "Modify Retrying Transaction\n");
  525. #ifndef LDBM_NO_BACKOFF_DELAY
  526. {
  527. PRIntervalTime interval;
  528. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  529. DS_Sleep(interval);
  530. }
  531. #endif
  532. }
  533. /* Nothing above here modifies persistent store, everything after here is subject to the transaction */
  534. retval = dblayer_txn_begin(li,parent_txn,&txn);
  535. if (0 != retval) {
  536. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  537. ldap_result_code= LDAP_OPERATIONS_ERROR;
  538. goto error_return;
  539. }
  540. /* stash the transaction for plugins */
  541. slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn);
  542. /* call the transaction pre modify plugins just after creating the transaction */
  543. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN))) {
  544. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN plugin "
  545. "returned error code %d\n", retval );
  546. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  547. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  548. if (!opreturn) {
  549. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  550. }
  551. goto error_return;
  552. }
  553. /* the mods might have been changed, so get the latest */
  554. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  555. /* make sure the betxnpreop did not alter any of the mods that
  556. had already previously been applied */
  557. slapi_mods_done(&smods);
  558. slapi_mods_init_byref(&smods,mods);
  559. new_mod_count = slapi_mods_get_num_mods(&smods);
  560. if (new_mod_count < mod_count) {
  561. LDAPDebug2Args( LDAP_DEBUG_ANY, "Error: BE_TXN_PRE_MODIFY plugin has removed "
  562. "mods from the original list - mod count was [%d] now [%d] "
  563. "mods will not be applied - mods list changes must be done "
  564. "in the BE_PRE_MODIFY plugin, not the BE_TXN_PRE_MODIFY\n",
  565. mod_count, new_mod_count );
  566. } else if (new_mod_count > mod_count) { /* apply the new betxnpremod mods */
  567. /* apply the mods, check for syntax, schema problems, etc. */
  568. if (modify_apply_check_expand(pb, operation, &mods[mod_count], e, ec, &postentry,
  569. &ldap_result_code, &ldap_result_message)) {
  570. goto error_return;
  571. }
  572. } /* else if new_mod_count == mod_count then betxnpremod plugin did nothing */
  573. /*
  574. * Update the ID to Entry index.
  575. * Note that id2entry_add replaces the entry, so the Entry ID
  576. * stays the same.
  577. */
  578. retval = id2entry_add_ext( be, ec, &txn, 1, &cache_rc );
  579. if (DB_LOCK_DEADLOCK == retval)
  580. {
  581. /* Abort and re-try */
  582. continue;
  583. }
  584. if (0 != retval) {
  585. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add failed, err=%d %s\n",
  586. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  587. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  588. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  589. goto error_return;
  590. }
  591. retval = index_add_mods( be, mods, e, ec, &txn );
  592. if (DB_LOCK_DEADLOCK == retval)
  593. {
  594. /* Abort and re-try */
  595. continue;
  596. }
  597. if (0 != retval) {
  598. LDAPDebug( LDAP_DEBUG_ANY, "index_add_mods failed, err=%d %s\n",
  599. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  600. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  601. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  602. goto error_return;
  603. }
  604. /*
  605. * Remove the old entry from the Virtual List View indexes.
  606. * Add the new entry to the Virtual List View indexes.
  607. * If the entry is ruv, no need to update vlv.
  608. */
  609. if (!is_ruv) {
  610. retval= vlv_update_all_indexes(&txn, be, pb, e, ec);
  611. if (DB_LOCK_DEADLOCK == retval)
  612. {
  613. /* Abort and re-try */
  614. continue;
  615. }
  616. if (0 != retval) {
  617. LDAPDebug( LDAP_DEBUG_ANY,
  618. "vlv_update_index failed, err=%d %s\n",
  619. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  620. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  621. MOD_SET_ERROR(ldap_result_code,
  622. LDAP_OPERATIONS_ERROR, retry_count);
  623. goto error_return;
  624. }
  625. }
  626. if (!is_ruv && !is_fixup_operation) {
  627. ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
  628. if (-1 == ruv_c_init) {
  629. LDAPDebug( LDAP_DEBUG_ANY,
  630. "ldbm_back_modify: ldbm_txn_ruv_modify_context "
  631. "failed to construct RUV modify context\n",
  632. 0, 0, 0);
  633. ldap_result_code= LDAP_OPERATIONS_ERROR;
  634. retval = 0;
  635. goto error_return;
  636. }
  637. }
  638. if (ruv_c_init) {
  639. retval = modify_update_all( be, pb, &ruv_c, &txn );
  640. if (DB_LOCK_DEADLOCK == retval) {
  641. /* Abort and re-try */
  642. continue;
  643. }
  644. if (0 != retval) {
  645. LDAPDebug( LDAP_DEBUG_ANY,
  646. "modify_update_all failed, err=%d %s\n", retval,
  647. (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  648. if (LDBM_OS_ERR_IS_DISKFULL(retval))
  649. disk_full = 1;
  650. ldap_result_code= LDAP_OPERATIONS_ERROR;
  651. goto error_return;
  652. }
  653. }
  654. if (0 == retval) {
  655. break;
  656. }
  657. }
  658. if (retry_count == RETRY_TIMES) {
  659. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in modify\n", 0, 0, 0 );
  660. ldap_result_code= LDAP_BUSY;
  661. goto error_return;
  662. }
  663. if (ruv_c_init) {
  664. if (modify_switch_entries(&ruv_c, be) != 0 ) {
  665. ldap_result_code= LDAP_OPERATIONS_ERROR;
  666. LDAPDebug( LDAP_DEBUG_ANY,
  667. "ldbm_back_modify: modify_switch_entries failed\n", 0, 0, 0);
  668. goto error_return;
  669. }
  670. }
  671. if (cache_replace( &inst->inst_cache, e, ec ) != 0 ) {
  672. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  673. goto error_return;
  674. }
  675. /* e uncached */
  676. /* we must return both e (which has been deleted) and new entry ec */
  677. /* cache_replace removes e from the caches */
  678. cache_unlock_entry( &inst->inst_cache, e );
  679. CACHE_RETURN( &inst->inst_cache, &e );
  680. /* lock new entry in cache to prevent usage until we are complete */
  681. cache_lock_entry( &inst->inst_cache, ec );
  682. ec_locked = 1;
  683. postentry = slapi_entry_dup( ec->ep_entry );
  684. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, postentry );
  685. /* invalidate virtual cache */
  686. ec->ep_entry->e_virtual_watermark = 0;
  687. /*
  688. * LP Fix of crash when the commit will fail:
  689. * If the commit fail, the common error path will
  690. * try to unlock the entry again and crash (PR_ASSERT
  691. * in debug mode.
  692. * By just setting e to NULL, we avoid this. It's OK since
  693. * we don't use e after that in the normal case.
  694. */
  695. e = NULL;
  696. /* call the transaction post modify plugins just before the commit */
  697. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  698. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  699. "returned error code %d\n", retval );
  700. if (!ldap_result_code) {
  701. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  702. }
  703. if (!opreturn) {
  704. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  705. }
  706. if (!opreturn) {
  707. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  708. }
  709. goto error_return;
  710. }
  711. retval = dblayer_txn_commit(li,&txn);
  712. /* after commit - txn is no longer valid - replace SLAPI_TXN with parent */
  713. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  714. if (0 != retval) {
  715. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  716. ldap_result_code= LDAP_OPERATIONS_ERROR;
  717. goto error_return;
  718. }
  719. rc= 0;
  720. goto common_return;
  721. error_return:
  722. if ( postentry != NULL )
  723. {
  724. slapi_entry_free( postentry );
  725. postentry = NULL;
  726. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, NULL );
  727. }
  728. if (retval == DB_RUNRECOVERY) {
  729. dblayer_remember_disk_filled(li);
  730. ldbm_nasty("Modify",81,retval);
  731. disk_full = 1;
  732. }
  733. if (disk_full) {
  734. rc= return_on_disk_full(li);
  735. } else {
  736. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  737. /* make sure SLAPI_RESULT_CODE and SLAPI_PLUGIN_OPRETURN are set */
  738. int val = 0;
  739. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &val);
  740. if (!val) {
  741. if (!ldap_result_code) {
  742. ldap_result_code = LDAP_OPERATIONS_ERROR;
  743. }
  744. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  745. }
  746. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &val );
  747. if (!val) {
  748. opreturn = -1;
  749. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  750. }
  751. /* call the transaction post modify plugins just before the abort */
  752. /* plugins called before abort should check for the OPRETURN or RESULT_CODE
  753. and skip processing if they don't want do anything - some plugins that
  754. keep track of a counter (usn, dna) may want to "rollback" the counter
  755. in this case */
  756. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  757. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  758. "returned error code %d\n", retval );
  759. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  760. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  761. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  762. if (!opreturn) {
  763. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  764. }
  765. }
  766. /* It is safer not to abort when the transaction is not started. */
  767. dblayer_txn_abort(li,&txn); /* abort crashes in case disk full */
  768. /* txn is no longer valid - reset the txn pointer to the parent */
  769. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  770. }
  771. rc= SLAPI_FAIL_GENERAL;
  772. }
  773. /* if ec is in cache, remove it, then add back e if we still have it */
  774. if (inst && cache_is_in_cache(&inst->inst_cache, ec)) {
  775. CACHE_REMOVE( &inst->inst_cache, ec );
  776. /* if ec was in cache, e was not - add back e */
  777. if (e) {
  778. if (CACHE_ADD( &inst->inst_cache, e, NULL ) < 0) {
  779. LDAPDebug1Arg( LDAP_DEBUG_CACHE, "ldbm_modify: CACHE_ADD %s failed\n",
  780. slapi_entry_get_dn(e->ep_entry));
  781. }
  782. }
  783. }
  784. common_return:
  785. slapi_mods_done(&smods);
  786. if(inst){
  787. if (ec_locked || cache_is_in_cache( &inst->inst_cache, ec))
  788. {
  789. cache_unlock_entry( &inst->inst_cache, ec);
  790. } else if (e) {
  791. /* if ec was not in cache, cache_replace was not done.
  792. * i.e., e was not unlocked. */
  793. cache_unlock_entry( &inst->inst_cache, e);
  794. CACHE_RETURN( &inst->inst_cache, &e);
  795. }
  796. CACHE_RETURN(&inst->inst_cache, &ec);
  797. }
  798. /* result code could be used in the bepost plugin functions. */
  799. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  800. /* The bepostop is called even if the operation fails. */
  801. if (!disk_full)
  802. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_MODIFY_FN);
  803. if (ruv_c_init) {
  804. modify_term(&ruv_c, be);
  805. }
  806. if(dblock_acquired)
  807. {
  808. dblayer_unlock_backend(be);
  809. }
  810. if(ldap_result_code!=-1)
  811. {
  812. slapi_send_ldap_result( pb, ldap_result_code, NULL, ldap_result_message, 0, NULL );
  813. }
  814. /* free our backups */
  815. ldap_mods_free(mods_original, 1);
  816. backentry_free(&original_entry);
  817. backentry_free(&tmpentry);
  818. slapi_ch_free_string(&errbuf);
  819. return rc;
  820. }
  821. /* Function removes mods which are not allowed over-the-wire */
  822. static void
  823. remove_illegal_mods(LDAPMod **mods)
  824. {
  825. int i, j;
  826. LDAPMod *tmp;
  827. /* remove any attempts by the user to modify these attrs */
  828. for ( i = 0; (mods != NULL) && (mods[i] != NULL); i++ ) {
  829. if ( strcasecmp( mods[i]->mod_type, numsubordinates ) == 0
  830. || strcasecmp( mods[i]->mod_type, hassubordinates ) == 0 )
  831. {
  832. tmp = mods[i];
  833. for ( j = i; mods[j] != NULL; j++ ) {
  834. mods[j] = mods[j + 1];
  835. }
  836. slapi_ch_free( (void**)&(tmp->mod_type) );
  837. if ( tmp->mod_bvalues != NULL ) {
  838. ber_bvecfree( tmp->mod_bvalues );
  839. }
  840. slapi_ch_free( (void**)&tmp );
  841. i--;
  842. }
  843. }
  844. }
  845. /* A mod has no effect if it is trying to replace a non-existing
  846. * attribute with null value
  847. */
  848. static int
  849. mods_have_effect (Slapi_Entry *entry, Slapi_Mods *smods)
  850. {
  851. LDAPMod *mod;
  852. Slapi_Attr *attr;
  853. int have_effect = 1;
  854. int j;
  855. /* Mods have effect if there is at least a non-replace mod or
  856. * a non-null-value mod.
  857. */
  858. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  859. if ( (mod = smods->mods[j]) != NULL ) {
  860. if ( ((mod->mod_op & LDAP_MOD_REPLACE) == 0) ||
  861. (mod->mod_vals.modv_bvals &&
  862. strcasecmp (mod->mod_type, "modifiersname") &&
  863. strcasecmp (mod->mod_type, "modifytime") ) ) {
  864. goto done;
  865. }
  866. }
  867. }
  868. if ( entry && entry->e_sdn.dn ) {
  869. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  870. if ((mod = smods->mods[j]) != NULL) {
  871. for ( attr = entry->e_attrs; attr; attr = attr->a_next ) {
  872. /* Mods have effect if at least a null-value-mod is
  873. * to actually remove an existing attribute
  874. */
  875. if ( strcasecmp ( mod->mod_type, attr->a_type ) == 0 ) {
  876. have_effect = 1; /* found one - mod has effect */
  877. goto done;
  878. }
  879. /* this mod type was not found in the entry - if we don't
  880. find one of the other mod types, or if there are no more
  881. mod types to look for, this mod does not apply */
  882. have_effect = 0;
  883. }
  884. }
  885. }
  886. }
  887. done:
  888. /* Return true would let the flow continue along the old path before
  889. * this function was added
  890. */
  891. return have_effect;
  892. }