ldbm_modify.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. int ret = 0;
  66. /* Make a copy of the entry */
  67. PR_ASSERT(mc->old_entry != NULL);
  68. PR_ASSERT(mc->new_entry == NULL);
  69. mc->new_entry = backentry_dup(mc->old_entry);
  70. PR_ASSERT(smods!=NULL);
  71. if ( mods_have_effect (mc->new_entry->ep_entry, smods) ) {
  72. ret = entry_apply_mods( mc->new_entry->ep_entry, slapi_mods_get_ldapmods_byref(smods));
  73. }
  74. mc->smods= smods;
  75. return ret;
  76. }
  77. /* Modify context structure destructor */
  78. int modify_term(modify_context *mc,struct backend *be)
  79. {
  80. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  81. slapi_mods_free(&mc->smods);
  82. /* Unlock and return entries */
  83. if (NULL != mc->old_entry) {
  84. cache_unlock_entry(&inst->inst_cache, mc->old_entry);
  85. CACHE_RETURN( &(inst->inst_cache), &(mc->old_entry) );
  86. mc->old_entry= NULL;
  87. }
  88. if (mc->new_entry_in_cache) {
  89. CACHE_RETURN( &(inst->inst_cache), &(mc->new_entry) );
  90. } else {
  91. backentry_free(&(mc->new_entry));
  92. }
  93. mc->new_entry= NULL;
  94. return 0;
  95. }
  96. /* Modify context structure member to switch entries in the cache */
  97. int modify_switch_entries(modify_context *mc,backend *be)
  98. {
  99. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  100. int ret = 0;
  101. if (mc->old_entry!=NULL && mc->new_entry!=NULL) {
  102. ret = cache_replace(&(inst->inst_cache), mc->old_entry, mc->new_entry);
  103. if (ret == 0) mc->new_entry_in_cache = 1;
  104. }
  105. return ret;
  106. }
  107. /* This routine does that part of a modify operation which involves
  108. updating the on-disk data: updates idices, id2entry.
  109. Copes properly with DB_LOCK_DEADLOCK. The caller must be able to cope with
  110. DB_LOCK_DEADLOCK returned.
  111. The caller is presumed to proceed as follows:
  112. Find the entry you want to modify;
  113. Lock it for modify;
  114. Make a copy of it; (call backentry_dup() )
  115. Apply modifications to the copy in memory (call entry_apply_mods() )
  116. begin transaction;
  117. Do any other mods to on-disk data you want
  118. Call this routine;
  119. Commit transaction;
  120. You pass it environment data: struct ldbminfo, pb (not sure why, but the vlv code seems to need it)
  121. the copy of the entry before modfication, the entry after modification;
  122. an LDAPMods array containing the modifications performed
  123. */
  124. int modify_update_all(backend *be, Slapi_PBlock *pb,
  125. modify_context *mc,
  126. back_txn *txn)
  127. {
  128. static char *function_name = "modify_update_all";
  129. Slapi_Operation *operation;
  130. int is_ruv = 0; /* True if the current entry is RUV */
  131. int retval = 0;
  132. if (pb) { /* pb could be NULL if it's called from import */
  133. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  134. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  135. }
  136. /*
  137. * Update the ID to Entry index.
  138. * Note that id2entry_add replaces the entry, so the Entry ID stays the same.
  139. */
  140. retval = id2entry_add_ext( be, mc->new_entry, txn, mc->attr_encrypt );
  141. if ( 0 != retval ) {
  142. if (DB_LOCK_DEADLOCK != retval)
  143. {
  144. ldbm_nasty(function_name,66,retval);
  145. }
  146. goto error;
  147. }
  148. retval = index_add_mods( be, slapi_mods_get_ldapmods_byref(mc->smods), mc->old_entry, mc->new_entry, txn );
  149. if ( 0 != retval ) {
  150. if (DB_LOCK_DEADLOCK != retval)
  151. {
  152. ldbm_nasty(function_name,65,retval);
  153. }
  154. goto error;
  155. }
  156. /*
  157. * Remove the old entry from the Virtual List View indexes.
  158. * Add the new entry to the Virtual List View indexes.
  159. * Because the VLV code calls slapi_filter_test(), which requires a pb (why?),
  160. * we allow the caller sans pb to get everything except vlv indexing.
  161. */
  162. if (NULL != pb && !is_ruv) {
  163. retval= vlv_update_all_indexes(txn, be, pb, mc->old_entry, mc->new_entry);
  164. if ( 0 != retval ) {
  165. if (DB_LOCK_DEADLOCK != retval)
  166. {
  167. ldbm_nasty(function_name,64,retval);
  168. }
  169. goto error;
  170. }
  171. }
  172. error:
  173. return retval;
  174. }
  175. int
  176. ldbm_back_modify( Slapi_PBlock *pb )
  177. {
  178. backend *be;
  179. ldbm_instance *inst = NULL;
  180. struct ldbminfo *li;
  181. struct backentry *e = NULL, *ec = NULL;
  182. Slapi_Entry *postentry = NULL;
  183. LDAPMod **mods;
  184. Slapi_Mods smods = {0};
  185. back_txn txn;
  186. back_txnid parent_txn;
  187. modify_context ruv_c = {0};
  188. int ruv_c_init = 0;
  189. int retval = -1;
  190. char *msg;
  191. char *errbuf = NULL;
  192. int retry_count = 0;
  193. int disk_full = 0;
  194. int ldap_result_code= LDAP_SUCCESS;
  195. char *ldap_result_message= NULL;
  196. int rc = 0;
  197. Slapi_Operation *operation;
  198. int dblock_acquired= 0;
  199. entry_address *addr;
  200. int change_entry = 0;
  201. int ec_in_cache = 0;
  202. int is_fixup_operation= 0;
  203. int is_ruv = 0; /* True if the current entry is RUV */
  204. CSN *opcsn = NULL;
  205. int i = 0;
  206. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  207. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  208. slapi_pblock_get( pb, SLAPI_TARGET_ADDRESS, &addr );
  209. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  210. slapi_pblock_get( pb, SLAPI_TXN, (void**)&parent_txn );
  211. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  212. if (NULL == operation)
  213. {
  214. ldap_result_code = LDAP_OPERATIONS_ERROR;
  215. goto error_return;
  216. }
  217. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  218. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  219. inst = (ldbm_instance *) be->be_instance_info;
  220. dblayer_txn_init(li,&txn);
  221. /* the calls to search for entries require the parent txn if any
  222. so set txn to the parent_txn until we begin the child transaction */
  223. txn.back_txn_txn = parent_txn;
  224. if (NULL == addr)
  225. {
  226. goto error_return;
  227. }
  228. ldap_result_code = slapi_dn_syntax_check(pb, slapi_sdn_get_dn(addr->sdn), 1);
  229. if (ldap_result_code)
  230. {
  231. ldap_result_code = LDAP_INVALID_DN_SYNTAX;
  232. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  233. goto error_return;
  234. }
  235. /* The dblock serializes writes to the database,
  236. * which reduces deadlocking in the db code,
  237. * which means that we run faster.
  238. *
  239. * But, this lock is re-enterant for the fixup
  240. * operations that the URP code in the Replication
  241. * plugin generates.
  242. */
  243. if(SERIALLOCK(li) && !operation_is_flag_set(operation,OP_FLAG_REPL_FIXUP)) {
  244. dblayer_lock_backend(be);
  245. dblock_acquired= 1;
  246. }
  247. /* find and lock the entry we are about to modify */
  248. if ( (e = find_entry2modify( pb, be, addr, &txn )) == NULL ) {
  249. ldap_result_code= -1;
  250. goto error_return; /* error result sent by find_entry2modify() */
  251. }
  252. if ( !is_fixup_operation )
  253. {
  254. opcsn = operation_get_csn (operation);
  255. if (NULL == opcsn && operation->o_csngen_handler)
  256. {
  257. /*
  258. * Current op is a user request. Opcsn will be assigned
  259. * if the dn is in an updatable replica.
  260. */
  261. opcsn = entry_assign_operation_csn ( pb, e->ep_entry, NULL );
  262. }
  263. if (opcsn)
  264. {
  265. entry_set_maxcsn (e->ep_entry, opcsn);
  266. }
  267. }
  268. /* Save away a copy of the entry, before modifications */
  269. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
  270. if ( (ldap_result_code = plugin_call_acl_mods_access( pb, e->ep_entry, mods, &errbuf)) != LDAP_SUCCESS ) {
  271. ldap_result_message= errbuf;
  272. goto error_return;
  273. }
  274. /* create a copy of the entry and apply the changes to it */
  275. if ( (ec = backentry_dup( e )) == NULL ) {
  276. ldap_result_code= LDAP_OPERATIONS_ERROR;
  277. goto error_return;
  278. }
  279. remove_illegal_mods(mods);
  280. /* ec is the entry that our bepreop should get to mess with */
  281. slapi_pblock_set( pb, SLAPI_MODIFY_EXISTING_ENTRY, ec->ep_entry );
  282. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  283. plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODIFY_FN);
  284. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  285. /* The Plugin may have messed about with some of the PBlock parameters... ie. mods */
  286. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  287. slapi_mods_init_byref(&smods,mods);
  288. {
  289. CSN *csn = operation_get_csn(operation);
  290. if ( (change_entry = mods_have_effect (ec->ep_entry, &smods)) ) {
  291. ldap_result_code = entry_apply_mods_wsi(ec->ep_entry, &smods, csn, operation_is_flag_set(operation,OP_FLAG_REPLICATED));
  292. /*
  293. * XXXmcs: it would be nice to get back an error message from
  294. * the above call so we could pass it along to the client, e.g.,
  295. * "duplicate value for attribute givenName."
  296. */
  297. } else {
  298. /* If the entry was not actually changed, we still need to
  299. * set the SLAPI_ENTRY_POST_OP field in the pblock (post-op
  300. * plugins expect that field to be present for all modify
  301. * operations that return LDAP_SUCCESS).
  302. */
  303. postentry = slapi_entry_dup( e->ep_entry );
  304. slapi_pblock_set ( pb, SLAPI_ENTRY_POST_OP, postentry );
  305. postentry = NULL; /* avoid removal/free in error_return code */
  306. }
  307. if ( !change_entry || ldap_result_code != 0 ) {
  308. /* change_entry == 0 is not an error, but we need to free lock etc */
  309. goto error_return;
  310. }
  311. }
  312. /*
  313. * If the objectClass attribute type was modified in any way, expand
  314. * the objectClass values to reflect the inheritance hierarchy.
  315. */
  316. for ( i = 0; mods[i] != NULL; ++i ) {
  317. if ( 0 == strcasecmp( SLAPI_ATTR_OBJECTCLASS, mods[i]->mod_type )) {
  318. slapi_schema_expand_objectclasses( ec->ep_entry );
  319. break;
  320. }
  321. }
  322. /*
  323. * We are about to pass the last abandon test, so from now on we are
  324. * committed to finish this operation. Set status to "will complete"
  325. * before we make our last abandon check to avoid race conditions in
  326. * the code that processes abandon operations.
  327. */
  328. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  329. if ( slapi_op_abandoned( pb ) ) {
  330. goto error_return;
  331. }
  332. /* check that the entry still obeys the schema */
  333. if ( (operation_is_flag_set(operation,OP_FLAG_ACTION_SCHEMA_CHECK)) &&
  334. slapi_entry_schema_check( pb, ec->ep_entry ) != 0 ) {
  335. ldap_result_code= LDAP_OBJECT_CLASS_VIOLATION;
  336. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  337. goto error_return;
  338. }
  339. /* check attribute syntax for the new values */
  340. if (slapi_mods_syntax_check(pb, mods, 0) != 0)
  341. {
  342. ldap_result_code = LDAP_INVALID_SYNTAX;
  343. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  344. goto error_return;
  345. }
  346. /*
  347. * make sure the entry contains all values in the RDN.
  348. * if not, the modification must have removed them.
  349. */
  350. if ( ! slapi_entry_rdn_values_present( ec->ep_entry ) ) {
  351. ldap_result_code= LDAP_NOT_ALLOWED_ON_RDN;
  352. goto error_return;
  353. }
  354. if (!is_ruv && !is_fixup_operation) {
  355. ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
  356. if (-1 == ruv_c_init) {
  357. LDAPDebug( LDAP_DEBUG_ANY,
  358. "ldbm_back_modify: ldbm_txn_ruv_modify_context "
  359. "failed to construct RUV modify context\n",
  360. 0, 0, 0);
  361. ldap_result_code= LDAP_OPERATIONS_ERROR;
  362. retval = 0;
  363. goto error_return;
  364. }
  365. }
  366. txn.back_txn_txn = NULL; /* ready to create the child transaction */
  367. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  368. if (retry_count > 0) {
  369. dblayer_txn_abort(li,&txn);
  370. /* txn is no longer valid - reset slapi_txn to the parent */
  371. txn.back_txn_txn = NULL;
  372. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  373. LDAPDebug( LDAP_DEBUG_TRACE, "Modify Retrying Transaction\n", 0, 0, 0 );
  374. #ifndef LDBM_NO_BACKOFF_DELAY
  375. {
  376. PRIntervalTime interval;
  377. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  378. DS_Sleep(interval);
  379. }
  380. #endif
  381. }
  382. /* Nothing above here modifies persistent store, everything after here is subject to the transaction */
  383. retval = dblayer_txn_begin(li,parent_txn,&txn);
  384. if (0 != retval) {
  385. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  386. ldap_result_code= LDAP_OPERATIONS_ERROR;
  387. goto error_return;
  388. }
  389. /* stash the transaction for plugins */
  390. slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn);
  391. /* call the transaction pre modify plugins just after creating the transaction */
  392. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN))) {
  393. LDAPDebug1Arg( LDAP_DEBUG_ANY, "SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN plugin "
  394. "returned error code %d\n", retval );
  395. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  396. goto error_return;
  397. }
  398. /*
  399. * Update the ID to Entry index.
  400. * Note that id2entry_add replaces the entry, so the Entry ID stays the same.
  401. */
  402. retval = id2entry_add( be, ec, &txn );
  403. if (DB_LOCK_DEADLOCK == retval)
  404. {
  405. /* Abort and re-try */
  406. continue;
  407. }
  408. if (0 != retval) {
  409. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add failed, err=%d %s\n",
  410. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  411. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  412. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  413. goto error_return;
  414. }
  415. ec_in_cache = 1;
  416. retval = index_add_mods( be, mods, e, ec, &txn );
  417. if (DB_LOCK_DEADLOCK == retval)
  418. {
  419. /* Abort and re-try */
  420. continue;
  421. }
  422. if (0 != retval) {
  423. LDAPDebug( LDAP_DEBUG_ANY, "index_add_mods failed, err=%d %s\n",
  424. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  425. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  426. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  427. goto error_return;
  428. }
  429. /*
  430. * Remove the old entry from the Virtual List View indexes.
  431. * Add the new entry to the Virtual List View indexes.
  432. * If the entry is ruv, no need to update vlv.
  433. */
  434. if (!is_ruv) {
  435. retval= vlv_update_all_indexes(&txn, be, pb, e, ec);
  436. if (DB_LOCK_DEADLOCK == retval)
  437. {
  438. /* Abort and re-try */
  439. continue;
  440. }
  441. if (0 != retval) {
  442. LDAPDebug( LDAP_DEBUG_ANY,
  443. "vlv_update_index failed, err=%d %s\n",
  444. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  445. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  446. MOD_SET_ERROR(ldap_result_code,
  447. LDAP_OPERATIONS_ERROR, retry_count);
  448. goto error_return;
  449. }
  450. }
  451. if (ruv_c_init) {
  452. retval = modify_update_all( be, pb, &ruv_c, &txn );
  453. if (DB_LOCK_DEADLOCK == retval) {
  454. /* Abort and re-try */
  455. continue;
  456. }
  457. if (0 != retval) {
  458. LDAPDebug( LDAP_DEBUG_ANY,
  459. "modify_update_all failed, err=%d %s\n", retval,
  460. (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  461. if (LDBM_OS_ERR_IS_DISKFULL(retval))
  462. disk_full = 1;
  463. ldap_result_code= LDAP_OPERATIONS_ERROR;
  464. goto error_return;
  465. }
  466. }
  467. if (0 == retval) {
  468. break;
  469. }
  470. }
  471. if (retry_count == RETRY_TIMES) {
  472. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in modify\n", 0, 0, 0 );
  473. ldap_result_code= LDAP_OPERATIONS_ERROR;
  474. goto error_return;
  475. }
  476. if (ruv_c_init) {
  477. if (modify_switch_entries(&ruv_c, be) != 0 ) {
  478. ldap_result_code= LDAP_OPERATIONS_ERROR;
  479. LDAPDebug( LDAP_DEBUG_ANY,
  480. "ldbm_back_modify: modify_switch_entries failed\n", 0, 0, 0);
  481. goto error_return;
  482. }
  483. }
  484. if (cache_replace( &inst->inst_cache, e, ec ) != 0 ) {
  485. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  486. goto error_return;
  487. }
  488. postentry = slapi_entry_dup( ec->ep_entry );
  489. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, postentry );
  490. /* invalidate virtual cache */
  491. ec->ep_entry->e_virtual_watermark = 0;
  492. /* we must return both e (which has been deleted) and new entry ec */
  493. cache_unlock_entry( &inst->inst_cache, e );
  494. CACHE_RETURN( &inst->inst_cache, &e );
  495. /*
  496. * LP Fix of crash when the commit will fail:
  497. * If the commit fail, the common error path will
  498. * try to unlock the entry again and crash (PR_ASSERT
  499. * in debug mode.
  500. * By just setting e to NULL, we avoid this. It's OK since
  501. * we don't use e after that in the normal case.
  502. */
  503. e = NULL;
  504. /* call the transaction post modify plugins just before the commit */
  505. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  506. LDAPDebug1Arg( LDAP_DEBUG_ANY, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  507. "returned error code %d\n", retval );
  508. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  509. goto error_return;
  510. }
  511. retval = dblayer_txn_commit(li,&txn);
  512. /* after commit - txn is no longer valid - replace SLAPI_TXN with parent */
  513. txn.back_txn_txn = NULL;
  514. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  515. if (0 != retval) {
  516. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  517. ldap_result_code= LDAP_OPERATIONS_ERROR;
  518. goto error_return;
  519. }
  520. rc= 0;
  521. goto common_return;
  522. error_return:
  523. if (ec_in_cache)
  524. {
  525. CACHE_REMOVE( &inst->inst_cache, ec );
  526. }
  527. else
  528. {
  529. backentry_free(&ec);
  530. }
  531. if ( postentry != NULL )
  532. {
  533. slapi_entry_free( postentry );
  534. postentry = NULL;
  535. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, NULL );
  536. }
  537. if (e!=NULL) {
  538. cache_unlock_entry( &inst->inst_cache, e);
  539. CACHE_RETURN( &inst->inst_cache, &e);
  540. }
  541. if (retval == DB_RUNRECOVERY) {
  542. dblayer_remember_disk_filled(li);
  543. ldbm_nasty("Modify",81,retval);
  544. disk_full = 1;
  545. }
  546. if (disk_full) {
  547. rc= return_on_disk_full(li);
  548. } else if (ldap_result_code != LDAP_SUCCESS) {
  549. if (retry_count > 0) {
  550. /* It is safer not to abort when the transaction is not started. */
  551. dblayer_txn_abort(li,&txn); /* abort crashes in case disk full */
  552. /* txn is no longer valid - reset the txn pointer to the parent */
  553. txn.back_txn_txn = NULL;
  554. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  555. }
  556. rc= SLAPI_FAIL_GENERAL;
  557. }
  558. common_return:
  559. slapi_mods_done(&smods);
  560. if (ec_in_cache)
  561. {
  562. CACHE_RETURN( &inst->inst_cache, &ec );
  563. }
  564. /* result code could be used in the bepost plugin functions. */
  565. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  566. /* The bepostop is called even if the operation fails. */
  567. if (!disk_full)
  568. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_MODIFY_FN);
  569. if (ruv_c_init) {
  570. modify_term(&ruv_c, be);
  571. }
  572. if(dblock_acquired)
  573. {
  574. dblayer_unlock_backend(be);
  575. }
  576. if(ldap_result_code!=-1)
  577. {
  578. slapi_send_ldap_result( pb, ldap_result_code, NULL, ldap_result_message, 0, NULL );
  579. }
  580. slapi_ch_free_string(&errbuf);
  581. return rc;
  582. }
  583. /* Function removes mods which are not allowed over-the-wire */
  584. static void
  585. remove_illegal_mods(LDAPMod **mods)
  586. {
  587. int i, j;
  588. LDAPMod *tmp;
  589. /* remove any attempts by the user to modify these attrs */
  590. for ( i = 0; mods[i] != NULL; i++ ) {
  591. if ( strcasecmp( mods[i]->mod_type, numsubordinates ) == 0
  592. || strcasecmp( mods[i]->mod_type, hassubordinates ) == 0 )
  593. {
  594. tmp = mods[i];
  595. for ( j = i; mods[j] != NULL; j++ ) {
  596. mods[j] = mods[j + 1];
  597. }
  598. slapi_ch_free( (void**)&(tmp->mod_type) );
  599. if ( tmp->mod_bvalues != NULL ) {
  600. ber_bvecfree( tmp->mod_bvalues );
  601. }
  602. slapi_ch_free( (void**)&tmp );
  603. i--;
  604. }
  605. }
  606. }
  607. /* A mod has no effect if it is trying to replace a non-existing
  608. * attribute with null value
  609. */
  610. static int
  611. mods_have_effect (Slapi_Entry *entry, Slapi_Mods *smods)
  612. {
  613. LDAPMod *mod;
  614. Slapi_Attr *attr;
  615. int have_effect = 1;
  616. int j;
  617. /* Mods have effect if there is at least a non-replace mod or
  618. * a non-null-value mod.
  619. */
  620. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  621. if ( (mod = smods->mods[j]) != NULL ) {
  622. if ( ((mod->mod_op & LDAP_MOD_REPLACE) == 0) ||
  623. (mod->mod_vals.modv_bvals &&
  624. strcasecmp (mod->mod_type, "modifiersname") &&
  625. strcasecmp (mod->mod_type, "modifytime") ) ) {
  626. goto done;
  627. }
  628. }
  629. }
  630. if ( entry && entry->e_sdn.dn ) {
  631. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  632. if ((mod = smods->mods[j]) != NULL) {
  633. for ( attr = entry->e_attrs; attr; attr = attr->a_next ) {
  634. /* Mods have effect if at least a null-value-mod is
  635. * to actually remove an existing attribute
  636. */
  637. if ( strcasecmp ( mod->mod_type, attr->a_type ) == 0 ) {
  638. have_effect = 1; /* found one - mod has effect */
  639. goto done;
  640. }
  641. /* this mod type was not found in the entry - if we don't
  642. find one of the other mod types, or if there are no more
  643. mod types to look for, this mod does not apply */
  644. have_effect = 0;
  645. }
  646. }
  647. }
  648. }
  649. done:
  650. /* Return true would let the flow continue along the old path before
  651. * this function was added
  652. */
  653. return have_effect;
  654. }