ldbm_modify.c 30 KB

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