ldbm_modify.c 34 KB

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