ldbm_modify.c 31 KB

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