ldbm_modify.c 23 KB

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