1/*	$NetBSD: modify.c,v 1.1.1.3 2010/12/12 15:22:58 adam Exp $	*/
2
3/* modify.c - bdb backend modify routine */
4/* OpenLDAP: pkg/ldap/servers/slapd/back-bdb/modify.c,v 1.156.2.19 2010/04/14 23:09:01 quanah Exp */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2000-2010 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18
19#include "portable.h"
20
21#include <stdio.h>
22#include <ac/string.h>
23#include <ac/time.h>
24
25#include "back-bdb.h"
26
27static struct berval scbva[] = {
28	BER_BVC("glue"),
29	BER_BVNULL
30};
31
32static void
33bdb_modify_idxflags(
34	Operation *op,
35	AttributeDescription *desc,
36	int got_delete,
37	Attribute *newattrs,
38	Attribute *oldattrs )
39{
40	struct berval	ix_at;
41	AttrInfo	*ai;
42
43	/* check if modified attribute was indexed
44	 * but not in case of NOOP... */
45	ai = bdb_index_mask( op->o_bd, desc, &ix_at );
46	if ( ai ) {
47		if ( got_delete ) {
48			Attribute 	*ap;
49			struct berval	ix2;
50
51			ap = attr_find( oldattrs, desc );
52			if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
53
54			/* Find all other attrs that index to same slot */
55			for ( ap = newattrs; ap; ap = ap->a_next ) {
56				ai = bdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
57				if ( ai && ix2.bv_val == ix_at.bv_val )
58					ap->a_flags |= SLAP_ATTR_IXADD;
59			}
60
61		} else {
62			Attribute 	*ap;
63
64			ap = attr_find( newattrs, desc );
65			if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
66		}
67	}
68}
69
70int bdb_modify_internal(
71	Operation *op,
72	DB_TXN *tid,
73	Modifications *modlist,
74	Entry *e,
75	const char **text,
76	char *textbuf,
77	size_t textlen )
78{
79	int rc, err;
80	Modification	*mod;
81	Modifications	*ml;
82	Attribute	*save_attrs;
83	Attribute 	*ap;
84	int			glue_attr_delete = 0;
85	int			got_delete;
86
87	Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
88		e->e_id, e->e_dn, 0);
89
90	if ( !acl_check_modlist( op, e, modlist )) {
91		return LDAP_INSUFFICIENT_ACCESS;
92	}
93
94	/* save_attrs will be disposed of by bdb_cache_modify */
95	save_attrs = e->e_attrs;
96	e->e_attrs = attrs_dup( e->e_attrs );
97
98	for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
99		int match;
100		mod = &ml->sml_mod;
101		switch( mod->sm_op ) {
102		case LDAP_MOD_ADD:
103		case LDAP_MOD_REPLACE:
104			if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
105				value_match( &match, slap_schema.si_ad_structuralObjectClass,
106					slap_schema.si_ad_structuralObjectClass->
107						ad_type->sat_equality,
108					SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
109					&mod->sm_values[0], &scbva[0], text );
110				if ( !match ) glue_attr_delete = 1;
111			}
112		}
113		if ( glue_attr_delete )
114			break;
115	}
116
117	if ( glue_attr_delete ) {
118		Attribute	**app = &e->e_attrs;
119		while ( *app != NULL ) {
120			if ( !is_at_operational( (*app)->a_desc->ad_type )) {
121				Attribute *save = *app;
122				*app = (*app)->a_next;
123				attr_free( save );
124				continue;
125			}
126			app = &(*app)->a_next;
127		}
128	}
129
130	for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
131		mod = &ml->sml_mod;
132		got_delete = 0;
133
134		switch ( mod->sm_op ) {
135		case LDAP_MOD_ADD:
136			Debug(LDAP_DEBUG_ARGS,
137				"bdb_modify_internal: add %s\n",
138				mod->sm_desc->ad_cname.bv_val, 0, 0);
139			err = modify_add_values( e, mod, get_permissiveModify(op),
140				text, textbuf, textlen );
141			if( err != LDAP_SUCCESS ) {
142				Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
143					err, *text, 0);
144			}
145			break;
146
147		case LDAP_MOD_DELETE:
148			if ( glue_attr_delete ) {
149				err = LDAP_SUCCESS;
150				break;
151			}
152
153			Debug(LDAP_DEBUG_ARGS,
154				"bdb_modify_internal: delete %s\n",
155				mod->sm_desc->ad_cname.bv_val, 0, 0);
156			err = modify_delete_values( e, mod, get_permissiveModify(op),
157				text, textbuf, textlen );
158			assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
159			if( err != LDAP_SUCCESS ) {
160				Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
161					err, *text, 0);
162			} else {
163				got_delete = 1;
164			}
165			break;
166
167		case LDAP_MOD_REPLACE:
168			Debug(LDAP_DEBUG_ARGS,
169				"bdb_modify_internal: replace %s\n",
170				mod->sm_desc->ad_cname.bv_val, 0, 0);
171			err = modify_replace_values( e, mod, get_permissiveModify(op),
172				text, textbuf, textlen );
173			if( err != LDAP_SUCCESS ) {
174				Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
175					err, *text, 0);
176			} else {
177				got_delete = 1;
178			}
179			break;
180
181		case LDAP_MOD_INCREMENT:
182			Debug(LDAP_DEBUG_ARGS,
183				"bdb_modify_internal: increment %s\n",
184				mod->sm_desc->ad_cname.bv_val, 0, 0);
185			err = modify_increment_values( e, mod, get_permissiveModify(op),
186				text, textbuf, textlen );
187			if( err != LDAP_SUCCESS ) {
188				Debug(LDAP_DEBUG_ARGS,
189					"bdb_modify_internal: %d %s\n",
190					err, *text, 0);
191			} else {
192				got_delete = 1;
193			}
194			break;
195
196		case SLAP_MOD_SOFTADD:
197			Debug(LDAP_DEBUG_ARGS,
198				"bdb_modify_internal: softadd %s\n",
199				mod->sm_desc->ad_cname.bv_val, 0, 0);
200 			/* Avoid problems in index_add_mods()
201 			 * We need to add index if necessary.
202 			 */
203 			mod->sm_op = LDAP_MOD_ADD;
204
205			err = modify_add_values( e, mod, get_permissiveModify(op),
206				text, textbuf, textlen );
207
208 			mod->sm_op = SLAP_MOD_SOFTADD;
209
210 			if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
211 				err = LDAP_SUCCESS;
212 			}
213
214			if( err != LDAP_SUCCESS ) {
215				Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
216					err, *text, 0);
217			}
218 			break;
219
220		default:
221			Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
222				mod->sm_op, 0, 0);
223			*text = "Invalid modify operation";
224			err = LDAP_OTHER;
225			Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
226				err, *text, 0);
227		}
228
229		if ( err != LDAP_SUCCESS ) {
230			attrs_free( e->e_attrs );
231			e->e_attrs = save_attrs;
232			/* unlock entry, delete from cache */
233			return err;
234		}
235
236		/* If objectClass was modified, reset the flags */
237		if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
238			e->e_ocflags = 0;
239		}
240
241		if ( glue_attr_delete ) e->e_ocflags = 0;
242
243
244		/* check if modified attribute was indexed
245		 * but not in case of NOOP... */
246		if ( !op->o_noop ) {
247			bdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
248		}
249	}
250
251	/* check that the entry still obeys the schema */
252	ap = NULL;
253	rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
254		text, textbuf, textlen );
255	if ( rc != LDAP_SUCCESS || op->o_noop ) {
256		attrs_free( e->e_attrs );
257		/* clear the indexing flags */
258		for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
259			ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
260		}
261		e->e_attrs = save_attrs;
262
263		if ( rc != LDAP_SUCCESS ) {
264			Debug( LDAP_DEBUG_ANY,
265				"entry failed schema check: %s\n",
266				*text, 0, 0 );
267		}
268
269		/* if NOOP then silently revert to saved attrs */
270		return rc;
271	}
272
273	/* structuralObjectClass modified! */
274	if ( ap ) {
275		assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
276		if ( !op->o_noop ) {
277			bdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
278				1, e->e_attrs, save_attrs );
279		}
280	}
281
282	/* update the indices of the modified attributes */
283
284	/* start with deleting the old index entries */
285	for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
286		if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
287			struct berval *vals;
288			Attribute *a2;
289			ap->a_flags &= ~SLAP_ATTR_IXDEL;
290			a2 = attr_find( e->e_attrs, ap->a_desc );
291			if ( a2 ) {
292				/* need to detect which values were deleted */
293				int i, j;
294				struct berval tmp;
295				j = ap->a_numvals;
296				for ( i=0; i<j; ) {
297					rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
298						&ap->a_nvals[i], NULL, op->o_tmpmemctx );
299					/* Move deleted values to end of array */
300					if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
301						j--;
302						if ( i != j ) {
303							tmp = ap->a_nvals[j];
304							ap->a_nvals[j] = ap->a_nvals[i];
305							ap->a_nvals[i] = tmp;
306							tmp = ap->a_vals[j];
307							ap->a_vals[j] = ap->a_vals[i];
308							ap->a_vals[i] = tmp;
309						}
310						continue;
311					}
312					i++;
313				}
314				vals = &ap->a_nvals[j];
315			} else {
316				/* attribute was completely deleted */
317				vals = ap->a_nvals;
318			}
319			if ( !BER_BVISNULL( vals )) {
320				rc = bdb_index_values( op, tid, ap->a_desc,
321					vals, e->e_id, SLAP_INDEX_DELETE_OP );
322				if ( rc != LDAP_SUCCESS ) {
323					Debug( LDAP_DEBUG_ANY,
324						"%s: attribute \"%s\" index delete failure\n",
325						op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
326					attrs_free( e->e_attrs );
327					e->e_attrs = save_attrs;
328					return rc;
329				}
330			}
331		}
332	}
333
334	/* add the new index entries */
335	for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
336		if (ap->a_flags & SLAP_ATTR_IXADD) {
337			ap->a_flags &= ~SLAP_ATTR_IXADD;
338			rc = bdb_index_values( op, tid, ap->a_desc,
339				ap->a_nvals,
340				e->e_id, SLAP_INDEX_ADD_OP );
341			if ( rc != LDAP_SUCCESS ) {
342				Debug( LDAP_DEBUG_ANY,
343				       "%s: attribute \"%s\" index add failure\n",
344					op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
345				attrs_free( e->e_attrs );
346				e->e_attrs = save_attrs;
347				return rc;
348			}
349		}
350	}
351
352	return rc;
353}
354
355
356int
357bdb_modify( Operation *op, SlapReply *rs )
358{
359	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
360	Entry		*e = NULL;
361	EntryInfo	*ei = NULL;
362	int		manageDSAit = get_manageDSAit( op );
363	char textbuf[SLAP_TEXT_BUFLEN];
364	size_t textlen = sizeof textbuf;
365	DB_TXN	*ltid = NULL, *lt2;
366	struct bdb_op_info opinfo = {{{ 0 }}};
367	Entry		dummy = {0};
368
369	DB_LOCK		lock;
370
371	int		num_retries = 0;
372
373	LDAPControl **preread_ctrl = NULL;
374	LDAPControl **postread_ctrl = NULL;
375	LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
376	int num_ctrls = 0;
377
378	int rc;
379
380#ifdef LDAP_X_TXN
381	int settle = 0;
382#endif
383
384	Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
385		op->o_req_dn.bv_val, 0, 0 );
386
387#ifdef LDAP_X_TXN
388	if( op->o_txnSpec ) {
389		/* acquire connection lock */
390		ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
391		if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
392			rs->sr_text = "invalid transaction identifier";
393			rs->sr_err = LDAP_X_TXN_ID_INVALID;
394			goto txnReturn;
395		} else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
396			settle=1;
397			goto txnReturn;
398		}
399
400		if( op->o_conn->c_txn_backend == NULL ) {
401			op->o_conn->c_txn_backend = op->o_bd;
402
403		} else if( op->o_conn->c_txn_backend != op->o_bd ) {
404			rs->sr_text = "transaction cannot span multiple database contexts";
405			rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
406			goto txnReturn;
407		}
408
409		/* insert operation into transaction */
410
411		rs->sr_text = "transaction specified";
412		rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
413
414txnReturn:
415		/* release connection lock */
416		ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
417
418		if( !settle ) {
419			send_ldap_result( op, rs );
420			return rs->sr_err;
421		}
422	}
423#endif
424
425	ctrls[num_ctrls] = NULL;
426
427	/* Don't touch the opattrs, if this is a contextCSN update
428	 * initiated from updatedn */
429	if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
430		 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
431
432		slap_mods_opattrs( op, &op->orm_modlist, 1 );
433	}
434
435	if( 0 ) {
436retry:	/* transaction retry */
437		if ( dummy.e_attrs ) {
438			attrs_free( dummy.e_attrs );
439			dummy.e_attrs = NULL;
440		}
441		if( e != NULL ) {
442			bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
443			e = NULL;
444		}
445		Debug(LDAP_DEBUG_TRACE,
446			LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
447
448		rs->sr_err = TXN_ABORT( ltid );
449		ltid = NULL;
450		LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
451		opinfo.boi_oe.oe_key = NULL;
452		op->o_do_not_cache = opinfo.boi_acl_cache;
453		if( rs->sr_err != 0 ) {
454			rs->sr_err = LDAP_OTHER;
455			rs->sr_text = "internal error";
456			goto return_results;
457		}
458		if ( op->o_abandon ) {
459			rs->sr_err = SLAPD_ABANDON;
460			goto return_results;
461		}
462		bdb_trans_backoff( ++num_retries );
463	}
464
465	/* begin transaction */
466	rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid,
467		bdb->bi_db_opflags );
468	rs->sr_text = NULL;
469	if( rs->sr_err != 0 ) {
470		Debug( LDAP_DEBUG_TRACE,
471			LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
472			"%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
473		rs->sr_err = LDAP_OTHER;
474		rs->sr_text = "internal error";
475		goto return_results;
476	}
477
478	opinfo.boi_oe.oe_key = bdb;
479	opinfo.boi_txn = ltid;
480	opinfo.boi_err = 0;
481	opinfo.boi_acl_cache = op->o_do_not_cache;
482	LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
483
484	/* get entry or ancestor */
485	rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
486		&lock );
487
488	if ( rs->sr_err != 0 ) {
489		Debug( LDAP_DEBUG_TRACE,
490			LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
491			rs->sr_err, 0, 0 );
492		switch( rs->sr_err ) {
493		case DB_LOCK_DEADLOCK:
494		case DB_LOCK_NOTGRANTED:
495			goto retry;
496		case DB_NOTFOUND:
497			break;
498		case LDAP_BUSY:
499			rs->sr_text = "ldap server busy";
500			goto return_results;
501		default:
502			rs->sr_err = LDAP_OTHER;
503			rs->sr_text = "internal error";
504			goto return_results;
505		}
506	}
507
508	e = ei->bei_e;
509
510	/* acquire and lock entry */
511	/* FIXME: dn2entry() should return non-glue entry */
512	if (( rs->sr_err == DB_NOTFOUND ) ||
513		( !manageDSAit && e && is_entry_glue( e )))
514	{
515		if ( e != NULL ) {
516			rs->sr_matched = ch_strdup( e->e_dn );
517			rs->sr_ref = is_entry_referral( e )
518				? get_entry_referrals( op, e )
519				: NULL;
520			bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
521			e = NULL;
522
523		} else {
524			rs->sr_ref = referral_rewrite( default_referral, NULL,
525				&op->o_req_dn, LDAP_SCOPE_DEFAULT );
526		}
527
528		rs->sr_err = LDAP_REFERRAL;
529		send_ldap_result( op, rs );
530
531		if ( rs->sr_ref != default_referral ) {
532			ber_bvarray_free( rs->sr_ref );
533		}
534		free( (char *)rs->sr_matched );
535		rs->sr_ref = NULL;
536		rs->sr_matched = NULL;
537
538		goto done;
539	}
540
541	if ( !manageDSAit && is_entry_referral( e ) ) {
542		/* entry is a referral, don't allow modify */
543		rs->sr_ref = get_entry_referrals( op, e );
544
545		Debug( LDAP_DEBUG_TRACE,
546			LDAP_XSTRING(bdb_modify) ": entry is referral\n",
547			0, 0, 0 );
548
549		rs->sr_err = LDAP_REFERRAL;
550		rs->sr_matched = e->e_name.bv_val;
551		send_ldap_result( op, rs );
552
553		ber_bvarray_free( rs->sr_ref );
554		rs->sr_ref = NULL;
555		rs->sr_matched = NULL;
556		goto done;
557	}
558
559	if ( get_assert( op ) &&
560		( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
561	{
562		rs->sr_err = LDAP_ASSERTION_FAILED;
563		goto return_results;
564	}
565
566	if( op->o_preread ) {
567		if( preread_ctrl == NULL ) {
568			preread_ctrl = &ctrls[num_ctrls++];
569			ctrls[num_ctrls] = NULL;
570		}
571		if ( slap_read_controls( op, rs, e,
572			&slap_pre_read_bv, preread_ctrl ) )
573		{
574			Debug( LDAP_DEBUG_TRACE,
575				"<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
576				"failed!\n", 0, 0, 0 );
577			if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
578				/* FIXME: is it correct to abort
579				 * operation if control fails? */
580				goto return_results;
581			}
582		}
583	}
584
585	/* nested transaction */
586	rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
587	rs->sr_text = NULL;
588	if( rs->sr_err != 0 ) {
589		Debug( LDAP_DEBUG_TRACE,
590			LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
591			db_strerror(rs->sr_err), rs->sr_err, 0 );
592		rs->sr_err = LDAP_OTHER;
593		rs->sr_text = "internal error";
594		goto return_results;
595	}
596	/* Modify the entry */
597	dummy = *e;
598	rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
599		&dummy, &rs->sr_text, textbuf, textlen );
600
601	if( rs->sr_err != LDAP_SUCCESS ) {
602		Debug( LDAP_DEBUG_TRACE,
603			LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
604			rs->sr_err, 0, 0 );
605		if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
606			rs->sr_err = opinfo.boi_err;
607		}
608		/* Only free attrs if they were dup'd.  */
609		if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
610		switch( rs->sr_err ) {
611		case DB_LOCK_DEADLOCK:
612		case DB_LOCK_NOTGRANTED:
613			goto retry;
614		}
615		goto return_results;
616	}
617
618	/* change the entry itself */
619	rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
620	if ( rs->sr_err != 0 ) {
621		Debug( LDAP_DEBUG_TRACE,
622			LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
623			rs->sr_err, 0, 0 );
624		switch( rs->sr_err ) {
625		case DB_LOCK_DEADLOCK:
626		case DB_LOCK_NOTGRANTED:
627			goto retry;
628		}
629		rs->sr_text = "entry update failed";
630		goto return_results;
631	}
632
633	if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
634		rs->sr_err = LDAP_OTHER;
635		rs->sr_text = "txn_commit(2) failed";
636		goto return_results;
637	}
638
639	if( op->o_postread ) {
640		if( postread_ctrl == NULL ) {
641			postread_ctrl = &ctrls[num_ctrls++];
642			ctrls[num_ctrls] = NULL;
643		}
644		if( slap_read_controls( op, rs, &dummy,
645			&slap_post_read_bv, postread_ctrl ) )
646		{
647			Debug( LDAP_DEBUG_TRACE,
648				"<=- " LDAP_XSTRING(bdb_modify)
649				": post-read failed!\n", 0, 0, 0 );
650			if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
651				/* FIXME: is it correct to abort
652				 * operation if control fails? */
653				goto return_results;
654			}
655		}
656	}
657
658	if( op->o_noop ) {
659		if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
660			rs->sr_text = "txn_abort (no-op) failed";
661		} else {
662			rs->sr_err = LDAP_X_NO_OPERATION;
663			ltid = NULL;
664			/* Only free attrs if they were dup'd.  */
665			if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
666			goto return_results;
667		}
668	} else {
669		/* may have changed in bdb_modify_internal() */
670		e->e_ocflags = dummy.e_ocflags;
671		rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
672		switch( rc ) {
673		case DB_LOCK_DEADLOCK:
674		case DB_LOCK_NOTGRANTED:
675			goto retry;
676		}
677		dummy.e_attrs = NULL;
678
679		rs->sr_err = TXN_COMMIT( ltid, 0 );
680	}
681	ltid = NULL;
682	LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
683	opinfo.boi_oe.oe_key = NULL;
684
685	if( rs->sr_err != 0 ) {
686		Debug( LDAP_DEBUG_TRACE,
687			LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
688			op->o_noop ? "abort (no-op)" : "commit",
689			db_strerror(rs->sr_err), rs->sr_err );
690		rs->sr_err = LDAP_OTHER;
691		rs->sr_text = "commit failed";
692
693		goto return_results;
694	}
695
696	Debug( LDAP_DEBUG_TRACE,
697		LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
698		op->o_noop ? " (no-op)" : "",
699		dummy.e_id, op->o_req_dn.bv_val );
700
701	rs->sr_err = LDAP_SUCCESS;
702	rs->sr_text = NULL;
703	if( num_ctrls ) rs->sr_ctrls = ctrls;
704
705return_results:
706	if( dummy.e_attrs ) {
707		attrs_free( dummy.e_attrs );
708	}
709	send_ldap_result( op, rs );
710
711	if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
712		TXN_CHECKPOINT( bdb->bi_dbenv,
713			bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
714	}
715
716done:
717	slap_graduate_commit_csn( op );
718
719	if( ltid != NULL ) {
720		TXN_ABORT( ltid );
721	}
722	if ( opinfo.boi_oe.oe_key ) {
723		LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
724	}
725
726	if( e != NULL ) {
727		bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
728	}
729
730	if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
731		slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
732		slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
733	}
734	if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
735		slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
736		slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
737	}
738
739	rs->sr_text = NULL;
740
741	return rs->sr_err;
742}
743