ldbm_modify.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. /* modify.c - ldbm backend modify routine */
  14. #include "back-ldbm.h"
  15. extern char *numsubordinates;
  16. extern char *hassubordinates;
  17. static void remove_illegal_mods(LDAPMod **mods);
  18. static int mods_have_effect (Slapi_Entry *entry, Slapi_Mods *smods);
  19. #define MOD_SET_ERROR(rc, error, count) \
  20. { \
  21. (rc) = (error); \
  22. (count) = RETRY_TIMES; /* otherwise, the transaction may not be aborted */ \
  23. }
  24. /* Modify context structure constructor, sans allocation */
  25. void modify_init(modify_context *mc,struct backentry *old_entry)
  26. {
  27. /* Store the old entry */
  28. PR_ASSERT(NULL == mc->old_entry);
  29. PR_ASSERT(NULL == mc->new_entry);
  30. mc->old_entry = old_entry;
  31. mc->attr_encrypt = 1;
  32. }
  33. int modify_apply_mods(modify_context *mc, Slapi_Mods *smods)
  34. {
  35. return modify_apply_mods_ignore_error(mc, smods, -1);
  36. }
  37. int modify_apply_mods_ignore_error(modify_context *mc, Slapi_Mods *smods, int error)
  38. {
  39. int ret = 0;
  40. /* Make a copy of the entry */
  41. PR_ASSERT(mc->old_entry != NULL);
  42. PR_ASSERT(mc->new_entry == NULL);
  43. mc->new_entry = backentry_dup(mc->old_entry);
  44. PR_ASSERT(smods!=NULL);
  45. if ( mods_have_effect (mc->new_entry->ep_entry, smods) ) {
  46. ret = entry_apply_mods_ignore_error( mc->new_entry->ep_entry, slapi_mods_get_ldapmods_byref(smods), error);
  47. }
  48. mc->smods= smods;
  49. if (error == ret) {
  50. ret = LDAP_SUCCESS;
  51. }
  52. return ret;
  53. }
  54. /* Modify context structure destructor */
  55. int modify_term(modify_context *mc,struct backend *be)
  56. {
  57. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  58. slapi_mods_free(&mc->smods);
  59. /* Unlock and return entries */
  60. if (mc->old_entry) {
  61. cache_unlock_entry(&inst->inst_cache, mc->old_entry);
  62. CACHE_RETURN( &(inst->inst_cache), &(mc->old_entry) );
  63. mc->old_entry= NULL;
  64. }
  65. CACHE_RETURN(&(inst->inst_cache), &(mc->new_entry));
  66. mc->new_entry= NULL;
  67. return 0;
  68. }
  69. /* Modify context structure member to switch entries in the cache */
  70. int modify_switch_entries(modify_context *mc,backend *be)
  71. {
  72. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  73. int ret = 0;
  74. if (mc->old_entry && mc->new_entry) {
  75. ret = cache_replace(&(inst->inst_cache), mc->old_entry, mc->new_entry);
  76. if (ret) {
  77. LDAPDebug(LDAP_DEBUG_CACHE, "modify_switch_entries: replacing %s with %s failed (%d)\n",
  78. slapi_entry_get_dn(mc->old_entry->ep_entry),
  79. slapi_entry_get_dn(mc->new_entry->ep_entry), ret);
  80. }
  81. }
  82. return ret;
  83. }
  84. /*
  85. * Switch the new with the old(original) - undoing modify_switch_entries()
  86. * This expects modify_term() to be called next, as the old "new" entry
  87. * is now gone(replaced by the original entry).
  88. */
  89. int
  90. modify_unswitch_entries(modify_context *mc,backend *be)
  91. {
  92. struct backentry *tmp_be;
  93. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  94. int ret = 0;
  95. if (mc->old_entry && mc->new_entry &&
  96. cache_is_in_cache(&inst->inst_cache, mc->new_entry)) {
  97. /* switch the entries, and reset the new, new, entry */
  98. tmp_be = mc->new_entry;
  99. mc->new_entry = mc->old_entry;
  100. mc->new_entry->ep_state = 0;
  101. if (cache_has_otherref(&(inst->inst_cache), mc->new_entry)) {
  102. /* some other thread refers the entry */
  103. CACHE_RETURN(&(inst->inst_cache), &(mc->new_entry));
  104. } else {
  105. /* don't call CACHE_RETURN, that frees the entry! */
  106. mc->new_entry->ep_refcnt = 0;
  107. }
  108. mc->old_entry = tmp_be;
  109. ret = cache_replace(&(inst->inst_cache), mc->old_entry, mc->new_entry);
  110. if (ret == 0) {
  111. /*
  112. * The new entry was originally locked, so since we did the
  113. * switch we need to unlock the "new" entry, and return the
  114. * "old" one. modify_term() will then return the "new" entry.
  115. */
  116. cache_unlock_entry(&inst->inst_cache, mc->new_entry);
  117. cache_lock_entry(&inst->inst_cache, mc->old_entry);
  118. } else {
  119. LDAPDebug(LDAP_DEBUG_CACHE, "modify_unswitch_entries: replacing %s with %s failed (%d)\n",
  120. slapi_entry_get_dn(mc->old_entry->ep_entry),
  121. slapi_entry_get_dn(mc->new_entry->ep_entry), ret);
  122. }
  123. }
  124. return ret;
  125. }
  126. /* This routine does that part of a modify operation which involves
  127. updating the on-disk data: updates idices, id2entry.
  128. Copes properly with DB_LOCK_DEADLOCK. The caller must be able to cope with
  129. DB_LOCK_DEADLOCK returned.
  130. The caller is presumed to proceed as follows:
  131. Find the entry you want to modify;
  132. Lock it for modify;
  133. Make a copy of it; (call backentry_dup() )
  134. Apply modifications to the copy in memory (call entry_apply_mods() )
  135. begin transaction;
  136. Do any other mods to on-disk data you want
  137. Call this routine;
  138. Commit transaction;
  139. You pass it environment data: struct ldbminfo, pb (not sure why, but the vlv code seems to need it)
  140. the copy of the entry before modfication, the entry after modification;
  141. an LDAPMods array containing the modifications performed
  142. */
  143. int modify_update_all(backend *be, Slapi_PBlock *pb,
  144. modify_context *mc,
  145. back_txn *txn)
  146. {
  147. static char *function_name = "modify_update_all";
  148. Slapi_Operation *operation;
  149. int is_ruv = 0; /* True if the current entry is RUV */
  150. int retval = 0;
  151. if (pb) { /* pb could be NULL if it's called from import */
  152. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  153. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  154. }
  155. /*
  156. * Update the ID to Entry index.
  157. * Note that id2entry_add replaces the entry, so the Entry ID stays the same.
  158. */
  159. retval = id2entry_add_ext( be, mc->new_entry, txn, mc->attr_encrypt, NULL );
  160. if ( 0 != retval ) {
  161. if (DB_LOCK_DEADLOCK != retval)
  162. {
  163. ldbm_nasty(function_name,66,retval);
  164. }
  165. goto error;
  166. }
  167. retval = index_add_mods( be, slapi_mods_get_ldapmods_byref(mc->smods), mc->old_entry, mc->new_entry, txn );
  168. if ( 0 != retval ) {
  169. if (DB_LOCK_DEADLOCK != retval)
  170. {
  171. ldbm_nasty(function_name,65,retval);
  172. }
  173. goto error;
  174. }
  175. /*
  176. * Remove the old entry from the Virtual List View indexes.
  177. * Add the new entry to the Virtual List View indexes.
  178. * Because the VLV code calls slapi_filter_test(), which requires a pb (why?),
  179. * we allow the caller sans pb to get everything except vlv indexing.
  180. */
  181. if (NULL != pb && !is_ruv) {
  182. retval= vlv_update_all_indexes(txn, be, pb, mc->old_entry, mc->new_entry);
  183. if ( 0 != retval ) {
  184. if (DB_LOCK_DEADLOCK != retval)
  185. {
  186. ldbm_nasty(function_name,64,retval);
  187. }
  188. goto error;
  189. }
  190. }
  191. error:
  192. return retval;
  193. }
  194. /**
  195. Apply the mods to the ec entry. Check for syntax, schema problems.
  196. Check for abandon.
  197. Return code:
  198. -1 - error - result code and message are set appropriately
  199. 0 - successfully applied and checked
  200. 1 - not an error - no mods to apply or op abandoned
  201. */
  202. static int
  203. modify_apply_check_expand(
  204. Slapi_PBlock *pb,
  205. Slapi_Operation *operation,
  206. LDAPMod **mods, /* list of mods to apply */
  207. struct backentry *e, /* original "before" entry */
  208. struct backentry *ec, /* "after" entry with mods applied */
  209. Slapi_Entry **postentry,
  210. int *ldap_result_code,
  211. char **ldap_result_message
  212. )
  213. {
  214. int rc = 0;
  215. int i;
  216. int repl_op;
  217. int change_entry = 0;
  218. Slapi_Mods smods = {0};
  219. CSN *csn = operation_get_csn(operation);
  220. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  221. slapi_mods_init_byref( &smods, mods );
  222. if ( (change_entry = mods_have_effect (ec->ep_entry, &smods)) ) {
  223. *ldap_result_code = entry_apply_mods_wsi(ec->ep_entry, &smods, csn,
  224. operation_is_flag_set(operation, OP_FLAG_REPLICATED));
  225. /*
  226. * XXXmcs: it would be nice to get back an error message from
  227. * the above call so we could pass it along to the client, e.g.,
  228. * "duplicate value for attribute givenName."
  229. */
  230. } else {
  231. Slapi_Entry *epostop = NULL;
  232. /* If the entry was not actually changed, we still need to
  233. * set the SLAPI_ENTRY_POST_OP field in the pblock (post-op
  234. * plugins expect that field to be present for all modify
  235. * operations that return LDAP_SUCCESS).
  236. */
  237. slapi_pblock_get ( pb, SLAPI_ENTRY_POST_OP, &epostop );
  238. slapi_entry_free ( epostop ); /* free existing one, if any */
  239. slapi_pblock_set ( pb, SLAPI_ENTRY_POST_OP, slapi_entry_dup( e->ep_entry ) );
  240. *postentry = NULL; /* to avoid free in main error cleanup code */
  241. }
  242. if ( !change_entry || *ldap_result_code != 0 ) {
  243. /* change_entry == 0 is not an error just a no-op */
  244. rc = change_entry ? -1 : 1;
  245. goto done;
  246. }
  247. /*
  248. * If the objectClass attribute type was modified in any way, expand
  249. * the objectClass values to reflect the inheritance hierarchy.
  250. */
  251. for ( i = 0; mods && mods[i]; ++i ) {
  252. if ( 0 == strcasecmp( SLAPI_ATTR_OBJECTCLASS, mods[i]->mod_type )) {
  253. slapi_schema_expand_objectclasses( ec->ep_entry );
  254. break;
  255. }
  256. }
  257. /*
  258. * We are about to pass the last abandon test, so from now on we are
  259. * committed to finish this operation. Set status to "will complete"
  260. * before we make our last abandon check to avoid race conditions in
  261. * the code that processes abandon operations.
  262. */
  263. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  264. if ( slapi_op_abandoned( pb ) ) {
  265. rc = 1;
  266. goto done;
  267. }
  268. /* multimaster replication can result in a schema violation,
  269. * although the individual operations on each master were valid
  270. * It is too late to resolve this. But we can check schema and
  271. * add a replication conflict attribute.
  272. */
  273. /* check that the entry still obeys the schema */
  274. if ((operation_is_flag_set(operation,OP_FLAG_ACTION_SCHEMA_CHECK)) &&
  275. slapi_entry_schema_check_ext( pb, ec->ep_entry, 1 ) != 0 ) {
  276. if(repl_op){
  277. Slapi_Attr *attr;
  278. Slapi_Mods smods;
  279. LDAPMod **lmods;
  280. if (slapi_entry_attr_find (ec->ep_entry, ATTR_NSDS5_REPLCONFLICT, &attr) == 0)
  281. {
  282. /* add value */
  283. Slapi_Value *val = slapi_value_new_string("Schema violation");
  284. slapi_attr_add_value(attr,val);
  285. slapi_value_free(&val);
  286. } else {
  287. /* Add new attribute */
  288. slapi_entry_add_string (ec->ep_entry, ATTR_NSDS5_REPLCONFLICT, "Schema violation");
  289. }
  290. /* the replconflict attribute is indexed and the index is built from the mods,
  291. * so we need to extend the mods */
  292. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &lmods);
  293. slapi_mods_init_passin(&smods, lmods);
  294. slapi_mods_add_string (&smods, LDAP_MOD_ADD, ATTR_NSDS5_REPLCONFLICT, "Schema violation");
  295. lmods = slapi_mods_get_ldapmods_passout(&smods);
  296. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, lmods);
  297. slapi_mods_done(&smods);
  298. } else {
  299. *ldap_result_code = LDAP_OBJECT_CLASS_VIOLATION;
  300. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, ldap_result_message);
  301. rc = -1;
  302. goto done;
  303. }
  304. }
  305. if(!repl_op){
  306. /* check attribute syntax for the new values */
  307. if (slapi_mods_syntax_check(pb, mods, 0) != 0) {
  308. *ldap_result_code = LDAP_INVALID_SYNTAX;
  309. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, ldap_result_message);
  310. rc = -1;
  311. goto done;
  312. }
  313. /*
  314. * make sure the entry contains all values in the RDN.
  315. * if not, the modification must have removed them.
  316. */
  317. if ( ! slapi_entry_rdn_values_present( ec->ep_entry ) ) {
  318. *ldap_result_code= LDAP_NOT_ALLOWED_ON_RDN;
  319. rc = -1;
  320. goto done;
  321. }
  322. }
  323. done:
  324. slapi_mods_done( &smods );
  325. return rc;
  326. }
  327. int
  328. ldbm_back_modify( Slapi_PBlock *pb )
  329. {
  330. backend *be;
  331. ldbm_instance *inst = NULL;
  332. struct ldbminfo *li;
  333. struct backentry *e = NULL, *ec = NULL;
  334. struct backentry *original_entry = NULL, *tmpentry = NULL;
  335. Slapi_Entry *postentry = NULL;
  336. LDAPMod **mods = NULL;
  337. LDAPMod **mods_original = NULL;
  338. Slapi_Mods smods = {0};
  339. back_txn txn;
  340. back_txnid parent_txn;
  341. modify_context ruv_c = {0};
  342. int ruv_c_init = 0;
  343. int retval = -1;
  344. char *msg;
  345. char *errbuf = NULL;
  346. int retry_count = 0;
  347. int disk_full = 0;
  348. int ldap_result_code= LDAP_SUCCESS;
  349. char *ldap_result_message= NULL;
  350. int rc = 0;
  351. Slapi_Operation *operation;
  352. entry_address *addr;
  353. int is_fixup_operation= 0;
  354. int is_ruv = 0; /* True if the current entry is RUV */
  355. CSN *opcsn = NULL;
  356. int repl_op;
  357. int opreturn = 0;
  358. int mod_count = 0;
  359. int not_an_error = 0;
  360. int fixup_tombstone = 0;
  361. int ec_locked = 0;
  362. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  363. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  364. slapi_pblock_get( pb, SLAPI_TARGET_ADDRESS, &addr );
  365. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  366. slapi_pblock_get( pb, SLAPI_TXN, (void**)&parent_txn );
  367. slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  368. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  369. fixup_tombstone = operation_is_flag_set(operation, OP_FLAG_TOMBSTONE_FIXUP);
  370. dblayer_txn_init(li,&txn); /* must do this before first goto error_return */
  371. /* the calls to perform searches require the parent txn if any
  372. so set txn to the parent_txn until we begin the child transaction */
  373. if (parent_txn) {
  374. txn.back_txn_txn = parent_txn;
  375. } else {
  376. parent_txn = txn.back_txn_txn;
  377. slapi_pblock_set( pb, SLAPI_TXN, parent_txn );
  378. }
  379. if (NULL == operation)
  380. {
  381. ldap_result_code = LDAP_OPERATIONS_ERROR;
  382. goto error_return;
  383. }
  384. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  385. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  386. inst = (ldbm_instance *) be->be_instance_info;
  387. if (NULL == addr)
  388. {
  389. goto error_return;
  390. }
  391. if (inst && inst->inst_ref_count) {
  392. slapi_counter_increment(inst->inst_ref_count);
  393. } else {
  394. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  395. "ldbm_modify: instance \"%s\" does not exist.\n",
  396. inst ? inst->inst_name : "null instance");
  397. goto error_return;
  398. }
  399. /* no need to check the dn syntax as this is a replicated op */
  400. if(!repl_op){
  401. ldap_result_code = slapi_dn_syntax_check(pb, slapi_sdn_get_dn(addr->sdn), 1);
  402. if (ldap_result_code)
  403. {
  404. ldap_result_code = LDAP_INVALID_DN_SYNTAX;
  405. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  406. goto error_return;
  407. }
  408. }
  409. /* The dblock serializes writes to the database,
  410. * which reduces deadlocking in the db code,
  411. * which means that we run faster.
  412. *
  413. * But, this lock is re-enterant for the fixup
  414. * operations that the URP code in the Replication
  415. * plugin generates.
  416. *
  417. * SERIALLOCK is moved to dblayer_txn_begin along with exposing be
  418. * transaction to plugins (see slapi_back_transaction_* APIs).
  419. *
  420. if(SERIALLOCK(li) && !operation_is_flag_set(operation,OP_FLAG_REPL_FIXUP)) {
  421. dblayer_lock_backend(be);
  422. dblock_acquired= 1;
  423. }
  424. */
  425. if ( MANAGE_ENTRY_BEFORE_DBLOCK(li)) {
  426. /* find and lock the entry we are about to modify */
  427. if ( (e = find_entry2modify( pb, be, addr, &txn )) == NULL ) {
  428. ldap_result_code= -1;
  429. goto error_return; /* error result sent by find_entry2modify() */
  430. }
  431. }
  432. txn.back_txn_txn = NULL; /* ready to create the child transaction */
  433. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  434. int cache_rc = 0;
  435. int new_mod_count = 0;
  436. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  437. /* don't release SERIAL LOCK */
  438. dblayer_txn_abort_ext(li, &txn, PR_FALSE);
  439. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  440. /*
  441. * Since be_txn_preop functions could have modified the entry/mods,
  442. * We need to grab the current mods, free them, and restore the
  443. * originals. Same thing for the entry.
  444. */
  445. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  446. ldap_mods_free(mods, 1);
  447. slapi_pblock_set(pb, SLAPI_MODIFY_MODS, copy_mods(mods_original));
  448. /* reset ec set cache in id2entry_add_ext */
  449. if (ec) {
  450. /* must duplicate ec before returning it to cache,
  451. * which could free the entry. */
  452. if ((tmpentry = backentry_dup(original_entry?original_entry:ec)) == NULL) {
  453. ldap_result_code= LDAP_OPERATIONS_ERROR;
  454. goto error_return;
  455. }
  456. if (cache_is_in_cache(&inst->inst_cache, ec)) {
  457. CACHE_REMOVE(&inst->inst_cache, ec);
  458. }
  459. CACHE_RETURN(&inst->inst_cache, &ec);
  460. slapi_pblock_set( pb, SLAPI_MODIFY_EXISTING_ENTRY, original_entry->ep_entry );
  461. ec = original_entry;
  462. original_entry = tmpentry;
  463. tmpentry = NULL;
  464. }
  465. if (ruv_c_init) {
  466. /* reset the ruv txn stuff */
  467. modify_term(&ruv_c, be);
  468. ruv_c_init = 0;
  469. }
  470. LDAPDebug0Args(LDAP_DEBUG_BACKLDBM,
  471. "Modify Retrying Transaction\n");
  472. #ifndef LDBM_NO_BACKOFF_DELAY
  473. {
  474. PRIntervalTime interval;
  475. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  476. DS_Sleep(interval);
  477. }
  478. #endif
  479. }
  480. /* Nothing above here modifies persistent store, everything after here is subject to the transaction */
  481. /* dblayer_txn_begin holds SERIAL lock,
  482. * which should be outside of locking the entry (find_entry2modify) */
  483. if (0 == retry_count) {
  484. /* First time, hold SERIAL LOCK */
  485. retval = dblayer_txn_begin(be, parent_txn, &txn);
  486. } else {
  487. /* Otherwise, no SERIAL LOCK */
  488. retval = dblayer_txn_begin_ext(li, parent_txn, &txn, PR_FALSE);
  489. }
  490. if (0 != retval) {
  491. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  492. ldap_result_code= LDAP_OPERATIONS_ERROR;
  493. goto error_return;
  494. }
  495. /* stash the transaction for plugins */
  496. slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn);
  497. if (0 == retry_count) { /* just once */
  498. if ( !MANAGE_ENTRY_BEFORE_DBLOCK(li)) {
  499. /* find and lock the entry we are about to modify */
  500. if ( (e = find_entry2modify( pb, be, addr, &txn )) == NULL ) {
  501. ldap_result_code= -1;
  502. goto error_return; /* error result sent by find_entry2modify() */
  503. }
  504. }
  505. if ( !is_fixup_operation && !fixup_tombstone)
  506. {
  507. if (!repl_op && slapi_entry_flag_is_set(e->ep_entry, SLAPI_ENTRY_FLAG_TOMBSTONE))
  508. {
  509. ldap_result_code = LDAP_UNWILLING_TO_PERFORM;
  510. ldap_result_message = "Operation not allowed on tombstone entry.";
  511. slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_modify",
  512. "Attempt to modify a tombstone entry %s\n",
  513. slapi_sdn_get_dn(slapi_entry_get_sdn_const( e->ep_entry )));
  514. goto error_return;
  515. }
  516. opcsn = operation_get_csn (operation);
  517. if (NULL == opcsn && operation->o_csngen_handler)
  518. {
  519. /*
  520. * Current op is a user request. Opcsn will be assigned
  521. * if the dn is in an updatable replica.
  522. */
  523. opcsn = entry_assign_operation_csn ( pb, e->ep_entry, NULL );
  524. }
  525. if (opcsn)
  526. {
  527. entry_set_maxcsn (e->ep_entry, opcsn);
  528. }
  529. }
  530. /* Save away a copy of the entry, before modifications */
  531. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
  532. if ( (ldap_result_code = plugin_call_acl_mods_access( pb, e->ep_entry, mods, &errbuf)) != LDAP_SUCCESS ) {
  533. ldap_result_message= errbuf;
  534. goto error_return;
  535. }
  536. /* create a copy of the entry and apply the changes to it */
  537. if ( (ec = backentry_dup( e )) == NULL ) {
  538. ldap_result_code= LDAP_OPERATIONS_ERROR;
  539. goto error_return;
  540. }
  541. if(!repl_op){
  542. remove_illegal_mods(mods);
  543. }
  544. /* ec is the entry that our bepreop should get to mess with */
  545. slapi_pblock_set( pb, SLAPI_MODIFY_EXISTING_ENTRY, ec->ep_entry );
  546. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  547. opreturn = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODIFY_FN);
  548. if (opreturn ||
  549. (slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code) && ldap_result_code) ||
  550. (slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn) && opreturn)) {
  551. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  552. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  553. if (!ldap_result_code) {
  554. LDAPDebug0Args(LDAP_DEBUG_ANY, "ldbm_back_modify: SLAPI_PLUGIN_BE_PRE_MODIFY_FN "
  555. "returned error but did not set SLAPI_RESULT_CODE\n");
  556. ldap_result_code = LDAP_OPERATIONS_ERROR;
  557. }
  558. if (SLAPI_PLUGIN_NOOP == opreturn) {
  559. not_an_error = 1;
  560. rc = opreturn = LDAP_SUCCESS;
  561. } else if (!opreturn) {
  562. opreturn = SLAPI_PLUGIN_FAILURE;
  563. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  564. }
  565. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  566. goto error_return;
  567. }
  568. /* The Plugin may have messed about with some of the PBlock parameters... ie. mods */
  569. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  570. /* apply the mods, check for syntax, schema problems, etc. */
  571. if (modify_apply_check_expand(pb, operation, mods, e, ec, &postentry,
  572. &ldap_result_code, &ldap_result_message)) {
  573. goto error_return;
  574. }
  575. /* the schema check could have added a repl conflict mod
  576. * get the mods again */
  577. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  578. slapi_mods_init_byref(&smods,mods);
  579. mod_count = slapi_mods_get_num_mods(&smods);
  580. /*
  581. * Grab a copy of the mods and the entry in case the be_txn_preop changes
  582. * the them. If we have a failure, then we need to reset the mods to their
  583. * their original state;
  584. */
  585. mods_original = copy_mods(mods);
  586. if ( (original_entry = backentry_dup( ec )) == NULL ) {
  587. ldap_result_code= LDAP_OPERATIONS_ERROR;
  588. goto error_return;
  589. }
  590. } /* if (0 == retry_count) just once */
  591. /* call the transaction pre modify plugins just after creating the transaction */
  592. retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN);
  593. if (retval) {
  594. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN plugin "
  595. "returned error code %d\n", retval );
  596. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  597. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  598. if (SLAPI_PLUGIN_NOOP == retval) {
  599. not_an_error = 1;
  600. rc = retval = LDAP_SUCCESS;
  601. }
  602. if (!opreturn) {
  603. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  604. }
  605. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  606. goto error_return;
  607. }
  608. /* the mods might have been changed, so get the latest */
  609. slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
  610. /* make sure the betxnpreop did not alter any of the mods that
  611. had already previously been applied */
  612. slapi_mods_done(&smods);
  613. slapi_mods_init_byref(&smods,mods);
  614. new_mod_count = slapi_mods_get_num_mods(&smods);
  615. if (new_mod_count < mod_count) {
  616. LDAPDebug2Args( LDAP_DEBUG_ANY, "Error: BE_TXN_PRE_MODIFY plugin has removed "
  617. "mods from the original list - mod count was [%d] now [%d] "
  618. "mods will not be applied - mods list changes must be done "
  619. "in the BE_PRE_MODIFY plugin, not the BE_TXN_PRE_MODIFY\n",
  620. mod_count, new_mod_count );
  621. } else if (new_mod_count > mod_count) { /* apply the new betxnpremod mods */
  622. /* apply the mods, check for syntax, schema problems, etc. */
  623. if (modify_apply_check_expand(pb, operation, &mods[mod_count], e, ec, &postentry,
  624. &ldap_result_code, &ldap_result_message)) {
  625. goto error_return;
  626. }
  627. } /* else if new_mod_count == mod_count then betxnpremod plugin did nothing */
  628. /*
  629. * Update the ID to Entry index.
  630. * Note that id2entry_add replaces the entry, so the Entry ID
  631. * stays the same.
  632. */
  633. retval = id2entry_add_ext( be, ec, &txn, 1, &cache_rc );
  634. if (DB_LOCK_DEADLOCK == retval)
  635. {
  636. /* Abort and re-try */
  637. continue;
  638. }
  639. if (0 != retval) {
  640. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add failed, err=%d %s\n",
  641. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  642. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  643. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  644. goto error_return;
  645. }
  646. retval = index_add_mods( be, mods, e, ec, &txn );
  647. if (DB_LOCK_DEADLOCK == retval)
  648. {
  649. /* Abort and re-try */
  650. continue;
  651. }
  652. if (0 != retval) {
  653. LDAPDebug( LDAP_DEBUG_ANY, "index_add_mods failed, err=%d %s\n",
  654. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  655. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  656. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  657. goto error_return;
  658. }
  659. /*
  660. * Remove the old entry from the Virtual List View indexes.
  661. * Add the new entry to the Virtual List View indexes.
  662. * If the entry is ruv, no need to update vlv.
  663. */
  664. if (!is_ruv) {
  665. retval= vlv_update_all_indexes(&txn, be, pb, e, ec);
  666. if (DB_LOCK_DEADLOCK == retval)
  667. {
  668. /* Abort and re-try */
  669. continue;
  670. }
  671. if (0 != retval) {
  672. LDAPDebug( LDAP_DEBUG_ANY,
  673. "vlv_update_index failed, err=%d %s\n",
  674. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  675. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  676. MOD_SET_ERROR(ldap_result_code,
  677. LDAP_OPERATIONS_ERROR, retry_count);
  678. goto error_return;
  679. }
  680. }
  681. if (!is_ruv && !is_fixup_operation && !NO_RUV_UPDATE(li)) {
  682. ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
  683. if (-1 == ruv_c_init) {
  684. LDAPDebug( LDAP_DEBUG_ANY,
  685. "ldbm_back_modify: ldbm_txn_ruv_modify_context "
  686. "failed to construct RUV modify context\n",
  687. 0, 0, 0);
  688. ldap_result_code= LDAP_OPERATIONS_ERROR;
  689. retval = 0;
  690. goto error_return;
  691. }
  692. }
  693. if (ruv_c_init) {
  694. retval = modify_update_all( be, pb, &ruv_c, &txn );
  695. if (DB_LOCK_DEADLOCK == retval) {
  696. /* Abort and re-try */
  697. continue;
  698. }
  699. if (0 != retval) {
  700. LDAPDebug( LDAP_DEBUG_ANY,
  701. "modify_update_all failed, err=%d %s\n", retval,
  702. (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  703. if (LDBM_OS_ERR_IS_DISKFULL(retval))
  704. disk_full = 1;
  705. ldap_result_code= LDAP_OPERATIONS_ERROR;
  706. goto error_return;
  707. }
  708. }
  709. if (0 == retval) {
  710. break;
  711. }
  712. }
  713. if (retry_count == RETRY_TIMES) {
  714. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in modify\n", 0, 0, 0 );
  715. ldap_result_code= LDAP_BUSY;
  716. goto error_return;
  717. }
  718. if (ruv_c_init) {
  719. if (modify_switch_entries(&ruv_c, be) != 0 ) {
  720. ldap_result_code= LDAP_OPERATIONS_ERROR;
  721. LDAPDebug( LDAP_DEBUG_ANY,
  722. "ldbm_back_modify: modify_switch_entries failed\n", 0, 0, 0);
  723. goto error_return;
  724. }
  725. }
  726. if (cache_replace( &inst->inst_cache, e, ec ) != 0 ) {
  727. MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  728. goto error_return;
  729. }
  730. /* e uncached */
  731. /* we must return both e (which has been deleted) and new entry ec to cache */
  732. /* cache_replace removes e from the cache hash tables */
  733. cache_unlock_entry( &inst->inst_cache, e );
  734. CACHE_RETURN( &inst->inst_cache, &e );
  735. /* lock new entry in cache to prevent usage until we are complete */
  736. cache_lock_entry( &inst->inst_cache, ec );
  737. ec_locked = 1;
  738. postentry = slapi_entry_dup( ec->ep_entry );
  739. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, postentry );
  740. /* invalidate virtual cache */
  741. ec->ep_entry->e_virtual_watermark = 0;
  742. /*
  743. * LP Fix of crash when the commit will fail:
  744. * If the commit fail, the common error path will
  745. * try to unlock the entry again and crash (PR_ASSERT
  746. * in debug mode.
  747. * By just setting e to NULL, we avoid this. It's OK since
  748. * we don't use e after that in the normal case.
  749. */
  750. e = NULL;
  751. /* call the transaction post modify plugins just before the commit */
  752. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  753. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  754. "returned error code %d\n", retval );
  755. if (!ldap_result_code) {
  756. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  757. }
  758. if (!opreturn) {
  759. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  760. }
  761. if (!opreturn) {
  762. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  763. }
  764. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  765. goto error_return;
  766. }
  767. /* Release SERIAL LOCK */
  768. retval = dblayer_txn_commit(be, &txn);
  769. /* after commit - txn is no longer valid - replace SLAPI_TXN with parent */
  770. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  771. if (0 != retval) {
  772. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  773. ldap_result_code= LDAP_OPERATIONS_ERROR;
  774. goto error_return;
  775. }
  776. rc= 0;
  777. goto common_return;
  778. error_return:
  779. if ( postentry != NULL )
  780. {
  781. slapi_entry_free( postentry );
  782. postentry = NULL;
  783. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, NULL );
  784. }
  785. if (retval == DB_RUNRECOVERY) {
  786. dblayer_remember_disk_filled(li);
  787. ldbm_nasty("Modify",81,retval);
  788. disk_full = 1;
  789. }
  790. if (disk_full) {
  791. rc= return_on_disk_full(li);
  792. } else {
  793. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  794. /* make sure SLAPI_RESULT_CODE and SLAPI_PLUGIN_OPRETURN are set */
  795. int val = 0;
  796. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &val);
  797. if (!val) {
  798. if (!ldap_result_code) {
  799. ldap_result_code = LDAP_OPERATIONS_ERROR;
  800. }
  801. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  802. }
  803. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &val );
  804. if (!val) {
  805. opreturn = -1;
  806. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  807. }
  808. /* call the transaction post modify plugins just before the abort */
  809. /* plugins called before abort should check for the OPRETURN or RESULT_CODE
  810. and skip processing if they don't want do anything - some plugins that
  811. keep track of a counter (usn, dna) may want to "rollback" the counter
  812. in this case */
  813. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN))) {
  814. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN plugin "
  815. "returned error code %d\n", retval );
  816. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  817. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  818. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  819. if (!opreturn) {
  820. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  821. }
  822. }
  823. /* It is safer not to abort when the transaction is not started. */
  824. /* Release SERIAL LOCK */
  825. dblayer_txn_abort(be, &txn); /* abort crashes in case disk full */
  826. /* txn is no longer valid - reset the txn pointer to the parent */
  827. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  828. }
  829. if (!not_an_error) {
  830. rc = SLAPI_FAIL_GENERAL;
  831. }
  832. }
  833. /* if ec is in cache, remove it, then add back e if we still have it */
  834. if (inst && cache_is_in_cache(&inst->inst_cache, ec)) {
  835. CACHE_REMOVE( &inst->inst_cache, ec );
  836. /* if ec was in cache, e was not - add back e */
  837. if (e) {
  838. if (CACHE_ADD( &inst->inst_cache, e, NULL ) < 0) {
  839. LDAPDebug1Arg(LDAP_DEBUG_CACHE, "ldbm_modify: CACHE_ADD %s failed\n",
  840. slapi_entry_get_dn(e->ep_entry));
  841. }
  842. }
  843. }
  844. common_return:
  845. slapi_mods_done(&smods);
  846. if (inst) {
  847. if (ec_locked || cache_is_in_cache(&inst->inst_cache, ec)) {
  848. cache_unlock_entry(&inst->inst_cache, ec);
  849. } else if (e) {
  850. /* if ec was not in cache, cache_replace was not done.
  851. * i.e., e was not unlocked. */
  852. cache_unlock_entry(&inst->inst_cache, e);
  853. CACHE_RETURN(&inst->inst_cache, &e);
  854. }
  855. CACHE_RETURN(&inst->inst_cache, &ec);
  856. if (inst->inst_ref_count) {
  857. slapi_counter_decrement(inst->inst_ref_count);
  858. }
  859. }
  860. /* result code could be used in the bepost plugin functions. */
  861. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  862. /* The bepostop is called even if the operation fails. */
  863. if (!disk_full)
  864. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_MODIFY_FN);
  865. if (ruv_c_init) {
  866. modify_term(&ruv_c, be);
  867. }
  868. if(ldap_result_code!=-1)
  869. {
  870. if (not_an_error) {
  871. /* This is mainly used by urp. Solved conflict is not an error.
  872. * And we don't want the supplier to halt sending the updates. */
  873. ldap_result_code = LDAP_SUCCESS;
  874. }
  875. slapi_send_ldap_result( pb, ldap_result_code, NULL, ldap_result_message, 0, NULL );
  876. }
  877. /* free our backups */
  878. ldap_mods_free(mods_original, 1);
  879. backentry_free(&original_entry);
  880. backentry_free(&tmpentry);
  881. slapi_ch_free_string(&errbuf);
  882. return rc;
  883. }
  884. /* Function removes mods which are not allowed over-the-wire */
  885. static void
  886. remove_illegal_mods(LDAPMod **mods)
  887. {
  888. int i, j;
  889. LDAPMod *tmp;
  890. /* remove any attempts by the user to modify these attrs */
  891. for ( i = 0; (mods != NULL) && (mods[i] != NULL); i++ ) {
  892. if ( strcasecmp( mods[i]->mod_type, numsubordinates ) == 0
  893. || strcasecmp( mods[i]->mod_type, hassubordinates ) == 0 )
  894. {
  895. tmp = mods[i];
  896. for ( j = i; mods[j] != NULL; j++ ) {
  897. mods[j] = mods[j + 1];
  898. }
  899. slapi_ch_free( (void**)&(tmp->mod_type) );
  900. if ( tmp->mod_bvalues != NULL ) {
  901. ber_bvecfree( tmp->mod_bvalues );
  902. }
  903. slapi_ch_free( (void**)&tmp );
  904. i--;
  905. }
  906. }
  907. }
  908. /* A mod has no effect if it is trying to replace a non-existing
  909. * attribute with null value
  910. */
  911. static int
  912. mods_have_effect (Slapi_Entry *entry, Slapi_Mods *smods)
  913. {
  914. LDAPMod *mod;
  915. Slapi_Attr *attr;
  916. int have_effect = 1;
  917. int j;
  918. /* Mods have effect if there is at least a non-replace mod or
  919. * a non-null-value mod.
  920. */
  921. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  922. if ( (mod = smods->mods[j]) != NULL ) {
  923. if ( ((mod->mod_op & LDAP_MOD_REPLACE) == 0) ||
  924. (mod->mod_vals.modv_bvals &&
  925. strcasecmp (mod->mod_type, "modifiersname") &&
  926. strcasecmp (mod->mod_type, "modifytime") ) ) {
  927. goto done;
  928. }
  929. }
  930. }
  931. if ( entry && entry->e_sdn.dn ) {
  932. for ( j = 0; j < smods->num_mods - 1; j++ ) {
  933. if ((mod = smods->mods[j]) != NULL) {
  934. for ( attr = entry->e_attrs; attr; attr = attr->a_next ) {
  935. /* Mods have effect if at least a null-value-mod is
  936. * to actually remove an existing attribute
  937. */
  938. if ( strcasecmp ( mod->mod_type, attr->a_type ) == 0 ) {
  939. have_effect = 1; /* found one - mod has effect */
  940. goto done;
  941. }
  942. /* this mod type was not found in the entry - if we don't
  943. find one of the other mod types, or if there are no more
  944. mod types to look for, this mod does not apply */
  945. have_effect = 0;
  946. }
  947. }
  948. }
  949. }
  950. done:
  951. /* Return true would let the flow continue along the old path before
  952. * this function was added
  953. */
  954. return have_effect;
  955. }