1 /* modify.c - bdb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 static struct berval scbva[] = {
33 AttributeDescription *desc,
41 /* check if modified attribute was indexed
42 * but not in case of NOOP... */
43 ai = bdb_index_mask( op->o_bd, desc, &ix_at );
49 ap = attr_find( oldattrs, desc );
50 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
52 /* Find all other attrs that index to same slot */
53 for ( ap = newattrs; ap; ap = ap->a_next ) {
54 ai = bdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
55 if ( ai && ix2.bv_val == ix_at.bv_val )
56 ap->a_flags |= SLAP_ATTR_IXADD;
62 ap = attr_find( newattrs, desc );
63 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
68 int bdb_modify_internal(
71 Modifications *modlist,
80 Attribute *save_attrs;
82 int glue_attr_delete = 0;
85 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
88 if ( !acl_check_modlist( op, e, modlist )) {
89 return LDAP_INSUFFICIENT_ACCESS;
92 /* save_attrs will be disposed of by bdb_cache_modify */
93 save_attrs = e->e_attrs;
94 e->e_attrs = attrs_dup( e->e_attrs );
96 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
99 switch( mod->sm_op ) {
101 case LDAP_MOD_REPLACE:
102 if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
103 value_match( &match, slap_schema.si_ad_structuralObjectClass,
104 slap_schema.si_ad_structuralObjectClass->
105 ad_type->sat_equality,
106 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
107 &mod->sm_values[0], &scbva[0], text );
108 if ( !match ) glue_attr_delete = 1;
111 if ( glue_attr_delete )
115 if ( glue_attr_delete ) {
116 Attribute **app = &e->e_attrs;
117 while ( *app != NULL ) {
118 if ( !is_at_operational( (*app)->a_desc->ad_type )) {
119 Attribute *save = *app;
120 *app = (*app)->a_next;
124 app = &(*app)->a_next;
128 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
132 switch ( mod->sm_op ) {
134 Debug(LDAP_DEBUG_ARGS,
135 "bdb_modify_internal: add %s\n",
136 mod->sm_desc->ad_cname.bv_val, 0, 0);
137 err = modify_add_values( e, mod, get_permissiveModify(op),
138 text, textbuf, textlen );
139 if( err != LDAP_SUCCESS ) {
140 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
145 case LDAP_MOD_DELETE:
146 if ( glue_attr_delete ) {
151 Debug(LDAP_DEBUG_ARGS,
152 "bdb_modify_internal: delete %s\n",
153 mod->sm_desc->ad_cname.bv_val, 0, 0);
154 err = modify_delete_values( e, mod, get_permissiveModify(op),
155 text, textbuf, textlen );
156 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
157 if( err != LDAP_SUCCESS ) {
158 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
165 case LDAP_MOD_REPLACE:
166 Debug(LDAP_DEBUG_ARGS,
167 "bdb_modify_internal: replace %s\n",
168 mod->sm_desc->ad_cname.bv_val, 0, 0);
169 err = modify_replace_values( e, mod, get_permissiveModify(op),
170 text, textbuf, textlen );
171 if( err != LDAP_SUCCESS ) {
172 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
179 case LDAP_MOD_INCREMENT:
180 Debug(LDAP_DEBUG_ARGS,
181 "bdb_modify_internal: increment %s\n",
182 mod->sm_desc->ad_cname.bv_val, 0, 0);
183 err = modify_increment_values( e, mod, get_permissiveModify(op),
184 text, textbuf, textlen );
185 if( err != LDAP_SUCCESS ) {
186 Debug(LDAP_DEBUG_ARGS,
187 "bdb_modify_internal: %d %s\n",
194 case SLAP_MOD_SOFTADD:
195 Debug(LDAP_DEBUG_ARGS,
196 "bdb_modify_internal: softadd %s\n",
197 mod->sm_desc->ad_cname.bv_val, 0, 0);
198 /* Avoid problems in index_add_mods()
199 * We need to add index if necessary.
201 mod->sm_op = LDAP_MOD_ADD;
203 err = modify_add_values( e, mod, get_permissiveModify(op),
204 text, textbuf, textlen );
206 mod->sm_op = SLAP_MOD_SOFTADD;
208 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
212 if( err != LDAP_SUCCESS ) {
213 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
219 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
221 *text = "Invalid modify operation";
223 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
227 if ( err != LDAP_SUCCESS ) {
228 attrs_free( e->e_attrs );
229 e->e_attrs = save_attrs;
230 /* unlock entry, delete from cache */
234 /* If objectClass was modified, reset the flags */
235 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
239 if ( glue_attr_delete ) e->e_ocflags = 0;
242 /* check if modified attribute was indexed
243 * but not in case of NOOP... */
245 bdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
249 /* check that the entry still obeys the schema */
251 rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
252 text, textbuf, textlen );
253 if ( rc != LDAP_SUCCESS || op->o_noop ) {
254 attrs_free( e->e_attrs );
255 /* clear the indexing flags */
256 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
257 ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
259 e->e_attrs = save_attrs;
261 if ( rc != LDAP_SUCCESS ) {
262 Debug( LDAP_DEBUG_ANY,
263 "entry failed schema check: %s\n",
267 /* if NOOP then silently revert to saved attrs */
271 /* structuralObjectClass modified! */
273 assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
275 bdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
276 1, e->e_attrs, save_attrs );
280 /* update the indices of the modified attributes */
282 /* start with deleting the old index entries */
283 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
284 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
287 ap->a_flags &= ~SLAP_ATTR_IXDEL;
288 a2 = attr_find( e->e_attrs, ap->a_desc );
290 /* need to detect which values were deleted */
295 rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
296 &ap->a_nvals[i], NULL, op->o_tmpmemctx );
297 /* Move deleted values to end of array */
298 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
301 tmp = ap->a_nvals[j];
302 ap->a_nvals[j] = ap->a_nvals[i];
303 ap->a_nvals[i] = tmp;
305 ap->a_vals[j] = ap->a_vals[i];
312 vals = &ap->a_nvals[j];
314 /* attribute was completely deleted */
317 if ( !BER_BVISNULL( vals )) {
318 rc = bdb_index_values( op, tid, ap->a_desc,
319 vals, e->e_id, SLAP_INDEX_DELETE_OP );
320 if ( rc != LDAP_SUCCESS ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: attribute \"%s\" index delete failure\n",
323 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
324 attrs_free( e->e_attrs );
325 e->e_attrs = save_attrs;
332 /* add the new index entries */
333 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
334 if (ap->a_flags & SLAP_ATTR_IXADD) {
335 ap->a_flags &= ~SLAP_ATTR_IXADD;
336 rc = bdb_index_values( op, tid, ap->a_desc,
338 e->e_id, SLAP_INDEX_ADD_OP );
339 if ( rc != LDAP_SUCCESS ) {
340 Debug( LDAP_DEBUG_ANY,
341 "%s: attribute \"%s\" index add failure\n",
342 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
343 attrs_free( e->e_attrs );
344 e->e_attrs = save_attrs;
355 bdb_modify( Operation *op, SlapReply *rs )
357 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
359 EntryInfo *ei = NULL;
360 int manageDSAit = get_manageDSAit( op );
361 char textbuf[SLAP_TEXT_BUFLEN];
362 size_t textlen = sizeof textbuf;
363 DB_TXN *ltid = NULL, *lt2;
364 struct bdb_op_info opinfo = {{{ 0 }}};
372 LDAPControl **preread_ctrl = NULL;
373 LDAPControl **postread_ctrl = NULL;
374 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
383 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
384 op->o_req_dn.bv_val, 0, 0 );
387 if( op->o_txnSpec ) {
388 /* acquire connection lock */
389 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
390 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
391 rs->sr_text = "invalid transaction identifier";
392 rs->sr_err = LDAP_X_TXN_ID_INVALID;
394 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
399 if( op->o_conn->c_txn_backend == NULL ) {
400 op->o_conn->c_txn_backend = op->o_bd;
402 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
403 rs->sr_text = "transaction cannot span multiple database contexts";
404 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
408 /* insert operation into transaction */
410 rs->sr_text = "transaction specified";
411 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
414 /* release connection lock */
415 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
418 send_ldap_result( op, rs );
424 ctrls[num_ctrls] = NULL;
426 slap_mods_opattrs( op, &op->orm_modlist, 1 );
429 retry: /* transaction retry */
430 if ( dummy.e_attrs ) {
431 attrs_free( dummy.e_attrs );
432 dummy.e_attrs = NULL;
435 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
438 Debug(LDAP_DEBUG_TRACE,
439 LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
441 rs->sr_err = TXN_ABORT( ltid );
443 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
444 opinfo.boi_oe.oe_key = NULL;
445 op->o_do_not_cache = opinfo.boi_acl_cache;
446 if( rs->sr_err != 0 ) {
447 rs->sr_err = LDAP_OTHER;
448 rs->sr_text = "internal error";
451 if ( op->o_abandon ) {
452 rs->sr_err = SLAPD_ABANDON;
455 bdb_trans_backoff( ++num_retries );
458 /* begin transaction */
459 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
460 bdb->bi_db_opflags );
462 if( rs->sr_err != 0 ) {
463 Debug( LDAP_DEBUG_TRACE,
464 LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
465 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
466 rs->sr_err = LDAP_OTHER;
467 rs->sr_text = "internal error";
471 opinfo.boi_oe.oe_key = bdb;
472 opinfo.boi_txn = ltid;
474 opinfo.boi_acl_cache = op->o_do_not_cache;
475 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
477 /* get entry or ancestor */
478 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
481 if ( rs->sr_err != 0 ) {
482 Debug( LDAP_DEBUG_TRACE,
483 LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
485 switch( rs->sr_err ) {
486 case DB_LOCK_DEADLOCK:
487 case DB_LOCK_NOTGRANTED:
490 if ( BER_BVISEMPTY( &op->o_req_ndn )) {
491 struct berval gluebv = BER_BVC("glue");
492 e = ch_calloc( 1, sizeof(Entry));
493 e->e_name.bv_val = ch_strdup( "" );
494 ber_dupbv( &e->e_nname, &e->e_name );
495 attr_merge_one( e, slap_schema.si_ad_objectClass,
497 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
505 rs->sr_text = "ldap server busy";
508 rs->sr_err = LDAP_OTHER;
509 rs->sr_text = "internal error";
518 /* acquire and lock entry */
519 /* FIXME: dn2entry() should return non-glue entry */
520 if (( rs->sr_err == DB_NOTFOUND ) ||
521 ( !manageDSAit && e && is_entry_glue( e )))
524 rs->sr_matched = ch_strdup( e->e_dn );
525 rs->sr_ref = is_entry_referral( e )
526 ? get_entry_referrals( op, e )
528 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
532 rs->sr_ref = referral_rewrite( default_referral, NULL,
533 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
536 rs->sr_err = LDAP_REFERRAL;
537 send_ldap_result( op, rs );
539 if ( rs->sr_ref != default_referral ) {
540 ber_bvarray_free( rs->sr_ref );
542 free( (char *)rs->sr_matched );
544 rs->sr_matched = NULL;
549 if ( !manageDSAit && is_entry_referral( e ) ) {
550 /* entry is a referral, don't allow modify */
551 rs->sr_ref = get_entry_referrals( op, e );
553 Debug( LDAP_DEBUG_TRACE,
554 LDAP_XSTRING(bdb_modify) ": entry is referral\n",
557 rs->sr_err = LDAP_REFERRAL;
558 rs->sr_matched = e->e_name.bv_val;
559 send_ldap_result( op, rs );
561 ber_bvarray_free( rs->sr_ref );
563 rs->sr_matched = NULL;
567 if ( get_assert( op ) &&
568 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
570 rs->sr_err = LDAP_ASSERTION_FAILED;
574 if( op->o_preread ) {
575 if( preread_ctrl == NULL ) {
576 preread_ctrl = &ctrls[num_ctrls++];
577 ctrls[num_ctrls] = NULL;
579 if ( slap_read_controls( op, rs, e,
580 &slap_pre_read_bv, preread_ctrl ) )
582 Debug( LDAP_DEBUG_TRACE,
583 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
584 "failed!\n", 0, 0, 0 );
585 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
586 /* FIXME: is it correct to abort
587 * operation if control fails? */
593 /* nested transaction */
594 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2, bdb->bi_db_opflags );
596 if( rs->sr_err != 0 ) {
597 Debug( LDAP_DEBUG_TRACE,
598 LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
599 db_strerror(rs->sr_err), rs->sr_err, 0 );
600 rs->sr_err = LDAP_OTHER;
601 rs->sr_text = "internal error";
604 /* Modify the entry */
606 rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
607 &dummy, &rs->sr_text, textbuf, textlen );
609 if( rs->sr_err != LDAP_SUCCESS ) {
610 Debug( LDAP_DEBUG_TRACE,
611 LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
613 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
614 rs->sr_err = opinfo.boi_err;
616 /* Only free attrs if they were dup'd. */
617 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
618 switch( rs->sr_err ) {
619 case DB_LOCK_DEADLOCK:
620 case DB_LOCK_NOTGRANTED:
626 /* change the entry itself */
627 rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
628 if ( rs->sr_err != 0 ) {
629 Debug( LDAP_DEBUG_TRACE,
630 LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
632 switch( rs->sr_err ) {
633 case DB_LOCK_DEADLOCK:
634 case DB_LOCK_NOTGRANTED:
637 rs->sr_text = "entry update failed";
641 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
642 rs->sr_err = LDAP_OTHER;
643 rs->sr_text = "txn_commit(2) failed";
647 if( op->o_postread ) {
648 if( postread_ctrl == NULL ) {
649 postread_ctrl = &ctrls[num_ctrls++];
650 ctrls[num_ctrls] = NULL;
652 if( slap_read_controls( op, rs, &dummy,
653 &slap_post_read_bv, postread_ctrl ) )
655 Debug( LDAP_DEBUG_TRACE,
656 "<=- " LDAP_XSTRING(bdb_modify)
657 ": post-read failed!\n", 0, 0, 0 );
658 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
659 /* FIXME: is it correct to abort
660 * operation if control fails? */
667 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
668 rs->sr_text = "txn_abort (no-op) failed";
670 rs->sr_err = LDAP_X_NO_OPERATION;
672 /* Only free attrs if they were dup'd. */
673 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
677 /* may have changed in bdb_modify_internal() */
678 e->e_ocflags = dummy.e_ocflags;
683 attrs_free( dummy.e_attrs );
686 rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
688 case DB_LOCK_DEADLOCK:
689 case DB_LOCK_NOTGRANTED:
693 dummy.e_attrs = NULL;
695 rs->sr_err = TXN_COMMIT( ltid, 0 );
698 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
699 opinfo.boi_oe.oe_key = NULL;
701 if( rs->sr_err != 0 ) {
702 Debug( LDAP_DEBUG_TRACE,
703 LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
704 op->o_noop ? "abort (no-op)" : "commit",
705 db_strerror(rs->sr_err), rs->sr_err );
706 rs->sr_err = LDAP_OTHER;
707 rs->sr_text = "commit failed";
712 Debug( LDAP_DEBUG_TRACE,
713 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
714 op->o_noop ? " (no-op)" : "",
715 dummy.e_id, op->o_req_dn.bv_val );
717 rs->sr_err = LDAP_SUCCESS;
719 if( num_ctrls ) rs->sr_ctrls = ctrls;
722 if( dummy.e_attrs ) {
723 attrs_free( dummy.e_attrs );
725 send_ldap_result( op, rs );
727 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
728 TXN_CHECKPOINT( bdb->bi_dbenv,
729 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
733 slap_graduate_commit_csn( op );
738 if ( opinfo.boi_oe.oe_key ) {
739 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
743 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
746 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
747 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
748 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
750 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
751 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
752 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );