ldbm_modify.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. txn.back_txn_txn = NULL; /* ready to create the child transaction */
  380. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  381. int cache_rc = 0;
  382. int new_mod_count = 0;
  383. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  384. /* don't release SERIAL LOCK */
  385. dblayer_txn_abort_ext(li, &txn, PR_FALSE);
  386. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  387. /*
  388. * Since be_txn_preop functions could have modified the entry/mods,
  389. * We need to grab the current mods, free them, and restore the
  390. * originals. Same thing for the entry.
  391. */
  392. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  393. ldap_mods_free(mods, 1);
  394. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, copy_mods(mods_original));
  395. /* ec is not really added to the cache until cache_replace, so we
  396. don't have to worry about the cache here */
  397. backentry_free(&ec);
  398. slapi_pblock_set( pb, SLAPI_MODIFY_EXISTING_ENTRY, original_entry->ep_entry );
  399. ec = original_entry;
  400. if ( (original_entry = backentry_dup( ec )) == NULL ) {
  401. ldap_result_code= LDAP_OPERATIONS_ERROR;
  402. goto error_return;
  403. }
  404. LDAPDebug0Args(LDAP_DEBUG_BACKLDBM,
  405. "Modify Retrying Transaction\n");
  406. #ifndef LDBM_NO_BACKOFF_DELAY
  407. {
  408. PRIntervalTime interval;
  409. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  410. DS_Sleep(interval);
  411. }
  412. #endif
  413. }
  414. /* Nothing above here modifies persistent store, everything after here is subject to the transaction */
  415. /* dblayer_txn_begin holds SERIAL lock,
  416. * which should be outside of locking the entry (find_entry2modify) */
  417. if (0 == retry_count) {
  418. /* First time, hold SERIAL LOCK */
  419. retval = dblayer_txn_begin(be, parent_txn, &txn);
  420. } else {
  421. /* Otherwise, no SERIAL LOCK */
  422. retval = dblayer_txn_begin_ext(li, parent_txn, &txn, PR_FALSE);
  423. }
  424. if (0 != retval) {
  425. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  426. ldap_result_code= LDAP_OPERATIONS_ERROR;
  427. goto error_return;
  428. }
  429. /* stash the transaction for plugins */
  430. slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn);
  431. if (0 == retry_count) { /* just once */
  432. /* find and lock the entry we are about to modify */
  433. if ( (e = find_entry2modify( pb, be, addr, &txn )) == NULL ) {
  434. ldap_result_code= -1;
  435. goto error_return; /* error result sent by find_entry2modify() */
  436. }
  437. if ( !is_fixup_operation )
  438. {
  439. opcsn = operation_get_csn (operation);
  440. if (NULL == opcsn && operation->o_csngen_handler)
  441. {
  442. /*
  443. * Current op is a user request. Opcsn will be assigned
  444. * if the dn is in an updatable replica.
  445. */
  446. opcsn = entry_assign_operation_csn ( pb, e->ep_entry, NULL );
  447. }
  448. if (opcsn)
  449. {
  450. entry_set_maxcsn (e->ep_entry, opcsn);
  451. }
  452. }
  453. /* Save away a copy of the entry, before modifications */
  454. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
  455. if ( (ldap_result_code = plugin_call_acl_mods_access( pb, e->ep_entry, mods, &errbuf)) != LDAP_SUCCESS ) {
  456. ldap_result_message= errbuf;
  457. goto error_return;
  458. }
  459. /* create a copy of the entry and apply the changes to it */
  460. if ( (ec = backentry_dup( e )) == NULL ) {
  461. ldap_result_code= LDAP_OPERATIONS_ERROR;
  462. goto error_return;
  463. }
  464. if(!repl_op){
  465. remove_illegal_mods(mods);
  466. }
  467. /* ec is the entry that our bepreop should get to mess with */
  468. slapi_pblock_set( pb, SLAPI_MODIFY_EXISTING_ENTRY, ec->ep_entry );
  469. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  470. opreturn = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODIFY_FN);
  471. if (opreturn ||
  472. (slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code) && ldap_result_code) ||
  473. (slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn) && opreturn)) {
  474. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  475. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  476. if (!ldap_result_code) {
  477. LDAPDebug0Args(LDAP_DEBUG_ANY, "ldbm_back_modify: SLAPI_PLUGIN_BE_PRE_MODIFY_FN "
  478. "returned error but did not set SLAPI_RESULT_CODE\n");
  479. ldap_result_code = LDAP_OPERATIONS_ERROR;
  480. }
  481. if (SLAPI_PLUGIN_NOOP == opreturn) {
  482. not_an_error = 1;
  483. rc = opreturn = LDAP_SUCCESS;
  484. goto error_return;
  485. } else if (!opreturn) {
  486. opreturn = -1;
  487. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  488. }
  489. }
  490. /* The Plugin may have messed about with some of the PBlock parameters... ie. mods */
  491. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  492. slapi_mods_init_byref(&smods,mods);
  493. mod_count = slapi_mods_get_num_mods(&smods);
  494. /* apply the mods, check for syntax, schema problems, etc. */
  495. if (modify_apply_check_expand(pb, operation, mods, e, ec, &postentry,
  496. &ldap_result_code, &ldap_result_message)) {
  497. goto error_return;
  498. }
  499. if (!is_ruv && !is_fixup_operation) {
  500. ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
  501. if (-1 == ruv_c_init) {
  502. LDAPDebug( LDAP_DEBUG_ANY,
  503. "ldbm_back_modify: ldbm_txn_ruv_modify_context "
  504. "failed to construct RUV modify context\n",
  505. 0, 0, 0);
  506. ldap_result_code= LDAP_OPERATIONS_ERROR;
  507. retval = 0;
  508. goto error_return;
  509. }
  510. }
  511. /*
  512. * Grab a copy of the mods and the entry in case the be_txn_preop changes
  513. * the them. If we have a failure, then we need to reset the mods to their
  514. * their original state;
  515. */
  516. mods_original = copy_mods(mods);
  517. if ( (original_entry = backentry_dup( ec )) == NULL ) {
  518. ldap_result_code= LDAP_OPERATIONS_ERROR;
  519. goto error_return;
  520. }
  521. } /* if (0 == retry_count) just once */
  522. /* call the transaction pre modify plugins just after creating the transaction */
  523. retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN);
  524. if (retval) {
  525. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN plugin "
  526. "returned error code %d\n", retval );
  527. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  528. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  529. if (SLAPI_PLUGIN_NOOP == retval) {
  530. not_an_error = 1;
  531. rc = retval = LDAP_SUCCESS;
  532. }
  533. if (!opreturn) {
  534. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  535. }
  536. goto error_return;
  537. }
  538. /* the mods might have been changed, so get the latest */
  539. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  540. /* make sure the betxnpreop did not alter any of the mods that
  541. had already previously been applied */
  542. slapi_mods_done(&smods);
  543. slapi_mods_init_byref(&smods,mods);
  544. new_mod_count = slapi_mods_get_num_mods(&smods);
  545. if (new_mod_count < mod_count) {
  546. LDAPDebug2Args( LDAP_DEBUG_ANY, "Error: BE_TXN_PRE_MODIFY plugin has removed "
  547. "mods from the original list - mod count was [%d] now [%d] "
  548. "mods will not be applied - mods list changes must be done "
  549. "in the BE_PRE_MODIFY plugin, not the BE_TXN_PRE_MODIFY\n",
  550. mod_count, new_mod_count );
  551. } else if (new_mod_count > mod_count) { /* apply the new betxnpremod mods */
  552. /* apply the mods, check for syntax, schema problems, etc. */
  553. if (modify_apply_check_expand(pb, operation, &mods[mod_count], e, ec, &postentry,
  554. &ldap_result_code, &ldap_result_message)) {
  555. goto error_return;
  556. }
  557. } /* else if new_mod_count == mod_count then betxnpremod plugin did nothing */
  558. /*
  559. * Update the ID to Entry index.
  560. * Note that id2entry_add replaces the entry, so the Entry ID
  561. * stays the same.
  562. */
  563. retval = id2entry_add_ext( be, ec, &txn, 1, &cache_rc );
  564. if (DB_LOCK_DEADLOCK == retval)
  565. {
  566. /* Abort and re-try */
  567. continue;
  568. }
  569. if (0 != retval) {
  570. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add failed, err=%d %s\n",
  571. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  572. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  573. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  574. goto error_return;
  575. }
  576. retval = index_add_mods( be, mods, e, ec, &txn );
  577. if (DB_LOCK_DEADLOCK == retval)
  578. {
  579. /* Abort and re-try */
  580. continue;
  581. }
  582. if (0 != retval) {
  583. LDAPDebug( LDAP_DEBUG_ANY, "index_add_mods failed, err=%d %s\n",
  584. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  585. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  586. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  587. goto error_return;
  588. }
  589. /*
  590. * Remove the old entry from the Virtual List View indexes.
  591. * Add the new entry to the Virtual List View indexes.
  592. * If the entry is ruv, no need to update vlv.
  593. */
  594. if (!is_ruv) {
  595. retval= vlv_update_all_indexes(&txn, be, pb, e, ec);
  596. if (DB_LOCK_DEADLOCK == retval)
  597. {
  598. /* Abort and re-try */
  599. continue;
  600. }
  601. if (0 != retval) {
  602. LDAPDebug( LDAP_DEBUG_ANY,
  603. "vlv_update_index failed, err=%d %s\n",
  604. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  605. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  606. MOD_SET_ERROR(ldap_result_code,
  607. LDAP_OPERATIONS_ERROR, retry_count);
  608. goto error_return;
  609. }
  610. }
  611. if (ruv_c_init) {
  612. retval = modify_update_all( be, pb, &ruv_c, &txn );
  613. if (DB_LOCK_DEADLOCK == retval) {
  614. /* Abort and re-try */
  615. continue;
  616. }
  617. if (0 != retval) {
  618. LDAPDebug( LDAP_DEBUG_ANY,
  619. "modify_update_all failed, err=%d %s\n", retval,
  620. (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  621. if (LDBM_OS_ERR_IS_DISKFULL(retval))
  622. disk_full = 1;
  623. ldap_result_code= LDAP_OPERATIONS_ERROR;
  624. goto error_return;
  625. }
  626. }
  627. if (0 == retval) {
  628. break;
  629. }
  630. }
  631. if (retry_count == RETRY_TIMES) {
  632. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in modify\n", 0, 0, 0 );
  633. ldap_result_code= LDAP_BUSY;
  634. goto error_return;
  635. }
  636. if (ruv_c_init) {
  637. if (modify_switch_entries(&ruv_c, be) != 0 ) {
  638. ldap_result_code= LDAP_OPERATIONS_ERROR;
  639. LDAPDebug( LDAP_DEBUG_ANY,
  640. "ldbm_back_modify: modify_switch_entries failed\n", 0, 0, 0);
  641. goto error_return;
  642. }
  643. }
  644. if (cache_replace( &inst->inst_cache, e, ec ) != 0 ) {
  645. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  646. goto error_return;
  647. }
  648. /* lock new entry in cache to prevent usage until we are complete */
  649. cache_lock_entry( &inst->inst_cache, ec );
  650. ec_in_cache = 1;
  651. postentry = slapi_entry_dup( ec->ep_entry );
  652. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, postentry );
  653. /* invalidate virtual cache */
  654. ec->ep_entry->e_virtual_watermark = 0;
  655. /* we must return both e (which has been deleted) and new entry ec */
  656. /* cache_replace removes e from the caches */
  657. cache_unlock_entry( &inst->inst_cache, e );
  658. CACHE_RETURN( &inst->inst_cache, &e );
  659. /*
  660. * LP Fix of crash when the commit will fail:
  661. * If the commit fail, the common error path will
  662. * try to unlock the entry again and crash (PR_ASSERT
  663. * in debug mode.
  664. * By just setting e to NULL, we avoid this. It's OK since
  665. * we don't use e after that in the normal case.
  666. */
  667. e = NULL;
  668. /* call the transaction post modify plugins just before the commit */
  669. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  670. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  671. "returned error code %d\n", retval );
  672. if (!ldap_result_code) {
  673. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  674. }
  675. if (!opreturn) {
  676. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  677. }
  678. if (!opreturn) {
  679. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  680. }
  681. goto error_return;
  682. }
  683. /* Release SERIAL LOCK */
  684. retval = dblayer_txn_commit(be, &txn);
  685. /* after commit - txn is no longer valid - replace SLAPI_TXN with parent */
  686. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  687. if (0 != retval) {
  688. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  689. ldap_result_code= LDAP_OPERATIONS_ERROR;
  690. goto error_return;
  691. }
  692. rc= 0;
  693. goto common_return;
  694. error_return:
  695. if ( postentry != NULL )
  696. {
  697. slapi_entry_free( postentry );
  698. postentry = NULL;
  699. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, NULL );
  700. }
  701. if (retval == DB_RUNRECOVERY) {
  702. dblayer_remember_disk_filled(li);
  703. ldbm_nasty("Modify",81,retval);
  704. disk_full = 1;
  705. }
  706. if (disk_full) {
  707. rc= return_on_disk_full(li);
  708. } else {
  709. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  710. /* make sure SLAPI_RESULT_CODE and SLAPI_PLUGIN_OPRETURN are set */
  711. int val = 0;
  712. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &val);
  713. if (!val) {
  714. if (!ldap_result_code) {
  715. ldap_result_code = LDAP_OPERATIONS_ERROR;
  716. }
  717. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  718. }
  719. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &val );
  720. if (!val) {
  721. opreturn = -1;
  722. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  723. }
  724. /* call the transaction post modify plugins just before the abort */
  725. /* plugins called before abort should check for the OPRETURN or RESULT_CODE
  726. and skip processing if they don't want do anything - some plugins that
  727. keep track of a counter (usn, dna) may want to "rollback" the counter
  728. in this case */
  729. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  730. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  731. "returned error code %d\n", retval );
  732. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  733. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  734. if (!opreturn) {
  735. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  736. }
  737. }
  738. /* It is safer not to abort when the transaction is not started. */
  739. /* Release SERIAL LOCK */
  740. dblayer_txn_abort(be, &txn); /* abort crashes in case disk full */
  741. /* txn is no longer valid - reset the txn pointer to the parent */
  742. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  743. }
  744. if (!not_an_error) {
  745. rc = SLAPI_FAIL_GENERAL;
  746. }
  747. }
  748. /* if ec is in cache, remove it, then add back e if we still have it */
  749. if (inst && ec_in_cache) {
  750. CACHE_REMOVE( &inst->inst_cache, ec );
  751. /* if ec was in cache, e was not - add back e */
  752. if (e) {
  753. CACHE_ADD( &inst->inst_cache, e, NULL );
  754. cache_lock_entry( &inst->inst_cache, e );
  755. }
  756. }
  757. common_return:
  758. slapi_mods_done(&smods);
  759. if (inst && ec_in_cache)
  760. {
  761. cache_unlock_entry( &inst->inst_cache, ec);
  762. CACHE_RETURN( &inst->inst_cache, &ec );
  763. }
  764. else
  765. {
  766. backentry_free(&ec);
  767. }
  768. if (inst) {
  769. if (e) {
  770. cache_unlock_entry( &inst->inst_cache, e);
  771. CACHE_RETURN( &inst->inst_cache, &e);
  772. }
  773. if (inst->inst_ref_count) {
  774. slapi_counter_decrement(inst->inst_ref_count);
  775. }
  776. }
  777. /* result code could be used in the bepost plugin functions. */
  778. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  779. /* The bepostop is called even if the operation fails. */
  780. if (!disk_full)
  781. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_MODIFY_FN);
  782. if (ruv_c_init) {
  783. modify_term(&ruv_c, be);
  784. }
  785. if(ldap_result_code!=-1)
  786. {
  787. if (not_an_error) {
  788. /* This is mainly used by urp. Solved conflict is not an error.
  789. * And we don't want the supplier to halt sending the updates. */
  790. ldap_result_code = LDAP_SUCCESS;
  791. }
  792. slapi_send_ldap_result( pb, ldap_result_code, NULL, ldap_result_message, 0, NULL );
  793. }
  794. /* free our backups */
  795. ldap_mods_free(mods_original, 1);
  796. backentry_free(&original_entry);
  797. slapi_ch_free_string(&errbuf);
  798. return rc;
  799. }
  800. /* Function removes mods which are not allowed over-the-wire */
  801. static void
  802. remove_illegal_mods(LDAPMod **mods)
  803. {
  804. int i, j;
  805. LDAPMod *tmp;
  806. /* remove any attempts by the user to modify these attrs */
  807. for ( i = 0; mods[i] != NULL; i++ ) {
  808. if ( strcasecmp( mods[i]->mod_type, numsubordinates ) == 0
  809. || strcasecmp( mods[i]->mod_type, hassubordinates ) == 0 )
  810. {
  811. tmp = mods[i];
  812. for ( j = i; mods[j] != NULL; j++ ) {
  813. mods[j] = mods[j + 1];
  814. }
  815. slapi_ch_free( (void**)&(tmp->mod_type) );
  816. if ( tmp->mod_bvalues != NULL ) {
  817. ber_bvecfree( tmp->mod_bvalues );
  818. }
  819. slapi_ch_free( (void**)&tmp );
  820. i--;
  821. }
  822. }
  823. }
  824. /* A mod has no effect if it is trying to replace a non-existing
  825. * attribute with null value
  826. */
  827. static int
  828. mods_have_effect (Slapi_Entry *entry, Slapi_Mods *smods)
  829. {
  830. LDAPMod *mod;
  831. Slapi_Attr *attr;
  832. int have_effect = 1;
  833. int j;
  834. /* Mods have effect if there is at least a non-replace mod or
  835. * a non-null-value mod.
  836. */
  837. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  838. if ( (mod = smods->mods[j]) != NULL ) {
  839. if ( ((mod->mod_op & LDAP_MOD_REPLACE) == 0) ||
  840. (mod->mod_vals.modv_bvals &&
  841. strcasecmp (mod->mod_type, "modifiersname") &&
  842. strcasecmp (mod->mod_type, "modifytime") ) ) {
  843. goto done;
  844. }
  845. }
  846. }
  847. if ( entry && entry->e_sdn.dn ) {
  848. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  849. if ((mod = smods->mods[j]) != NULL) {
  850. for ( attr = entry->e_attrs; attr; attr = attr->a_next ) {
  851. /* Mods have effect if at least a null-value-mod is
  852. * to actually remove an existing attribute
  853. */
  854. if ( strcasecmp ( mod->mod_type, attr->a_type ) == 0 ) {
  855. have_effect = 1; /* found one - mod has effect */
  856. goto done;
  857. }
  858. /* this mod type was not found in the entry - if we don't
  859. find one of the other mod types, or if there are no more
  860. mod types to look for, this mod does not apply */
  861. have_effect = 0;
  862. }
  863. }
  864. }
  865. }
  866. done:
  867. /* Return true would let the flow continue along the old path before
  868. * this function was added
  869. */
  870. return have_effect;
  871. }