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 }}};
371 LDAPControl **preread_ctrl = NULL;
372 LDAPControl **postread_ctrl = NULL;
373 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
382 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
383 op->o_req_dn.bv_val, 0, 0 );
386 if( op->o_txnSpec ) {
387 /* acquire connection lock */
388 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
389 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
390 rs->sr_text = "invalid transaction identifier";
391 rs->sr_err = LDAP_X_TXN_ID_INVALID;
393 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
398 if( op->o_conn->c_txn_backend == NULL ) {
399 op->o_conn->c_txn_backend = op->o_bd;
401 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
402 rs->sr_text = "transaction cannot span multiple database contexts";
403 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
407 /* insert operation into transaction */
409 rs->sr_text = "transaction specified";
410 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
413 /* release connection lock */
414 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
417 send_ldap_result( op, rs );
423 ctrls[num_ctrls] = NULL;
425 /* Don't touch the opattrs, if this is a contextCSN update
426 * initiated from updatedn */
427 if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
428 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
430 slap_mods_opattrs( op, &op->orm_modlist, 1 );
434 retry: /* transaction retry */
435 if ( dummy.e_attrs ) {
436 attrs_free( dummy.e_attrs );
437 dummy.e_attrs = NULL;
440 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
443 Debug(LDAP_DEBUG_TRACE,
444 LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
446 rs->sr_err = TXN_ABORT( ltid );
448 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
449 opinfo.boi_oe.oe_key = NULL;
450 op->o_do_not_cache = opinfo.boi_acl_cache;
451 if( rs->sr_err != 0 ) {
452 rs->sr_err = LDAP_OTHER;
453 rs->sr_text = "internal error";
456 if ( op->o_abandon ) {
457 rs->sr_err = SLAPD_ABANDON;
460 bdb_trans_backoff( ++num_retries );
463 /* begin transaction */
464 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
465 bdb->bi_db_opflags );
467 if( rs->sr_err != 0 ) {
468 Debug( LDAP_DEBUG_TRACE,
469 LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
470 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
471 rs->sr_err = LDAP_OTHER;
472 rs->sr_text = "internal error";
476 opinfo.boi_oe.oe_key = bdb;
477 opinfo.boi_txn = ltid;
479 opinfo.boi_acl_cache = op->o_do_not_cache;
480 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
482 /* get entry or ancestor */
483 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
486 if ( rs->sr_err != 0 ) {
487 Debug( LDAP_DEBUG_TRACE,
488 LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
490 switch( rs->sr_err ) {
491 case DB_LOCK_DEADLOCK:
492 case DB_LOCK_NOTGRANTED:
497 rs->sr_text = "ldap server busy";
500 rs->sr_err = LDAP_OTHER;
501 rs->sr_text = "internal error";
508 /* acquire and lock entry */
509 /* FIXME: dn2entry() should return non-glue entry */
510 if (( rs->sr_err == DB_NOTFOUND ) ||
511 ( !manageDSAit && e && is_entry_glue( e )))
514 rs->sr_matched = ch_strdup( e->e_dn );
515 rs->sr_ref = is_entry_referral( e )
516 ? get_entry_referrals( op, e )
518 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
522 rs->sr_ref = referral_rewrite( default_referral, NULL,
523 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
526 rs->sr_err = LDAP_REFERRAL;
527 send_ldap_result( op, rs );
529 if ( rs->sr_ref != default_referral ) {
530 ber_bvarray_free( rs->sr_ref );
532 free( (char *)rs->sr_matched );
534 rs->sr_matched = NULL;
539 if ( !manageDSAit && is_entry_referral( e ) ) {
540 /* entry is a referral, don't allow modify */
541 rs->sr_ref = get_entry_referrals( op, e );
543 Debug( LDAP_DEBUG_TRACE,
544 LDAP_XSTRING(bdb_modify) ": entry is referral\n",
547 rs->sr_err = LDAP_REFERRAL;
548 rs->sr_matched = e->e_name.bv_val;
549 send_ldap_result( op, rs );
551 ber_bvarray_free( rs->sr_ref );
553 rs->sr_matched = NULL;
557 if ( get_assert( op ) &&
558 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
560 rs->sr_err = LDAP_ASSERTION_FAILED;
564 if( op->o_preread ) {
565 if( preread_ctrl == NULL ) {
566 preread_ctrl = &ctrls[num_ctrls++];
567 ctrls[num_ctrls] = NULL;
569 if ( slap_read_controls( op, rs, e,
570 &slap_pre_read_bv, preread_ctrl ) )
572 Debug( LDAP_DEBUG_TRACE,
573 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
574 "failed!\n", 0, 0, 0 );
575 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
576 /* FIXME: is it correct to abort
577 * operation if control fails? */
583 /* nested transaction */
584 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2, bdb->bi_db_opflags );
586 if( rs->sr_err != 0 ) {
587 Debug( LDAP_DEBUG_TRACE,
588 LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
589 db_strerror(rs->sr_err), rs->sr_err, 0 );
590 rs->sr_err = LDAP_OTHER;
591 rs->sr_text = "internal error";
594 /* Modify the entry */
596 rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
597 &dummy, &rs->sr_text, textbuf, textlen );
599 if( rs->sr_err != LDAP_SUCCESS ) {
600 Debug( LDAP_DEBUG_TRACE,
601 LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
603 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
604 rs->sr_err = opinfo.boi_err;
606 /* Only free attrs if they were dup'd. */
607 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
608 switch( rs->sr_err ) {
609 case DB_LOCK_DEADLOCK:
610 case DB_LOCK_NOTGRANTED:
616 /* change the entry itself */
617 rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
618 if ( rs->sr_err != 0 ) {
619 Debug( LDAP_DEBUG_TRACE,
620 LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
622 switch( rs->sr_err ) {
623 case DB_LOCK_DEADLOCK:
624 case DB_LOCK_NOTGRANTED:
627 rs->sr_text = "entry update failed";
631 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
632 rs->sr_err = LDAP_OTHER;
633 rs->sr_text = "txn_commit(2) failed";
637 if( op->o_postread ) {
638 if( postread_ctrl == NULL ) {
639 postread_ctrl = &ctrls[num_ctrls++];
640 ctrls[num_ctrls] = NULL;
642 if( slap_read_controls( op, rs, &dummy,
643 &slap_post_read_bv, postread_ctrl ) )
645 Debug( LDAP_DEBUG_TRACE,
646 "<=- " LDAP_XSTRING(bdb_modify)
647 ": post-read failed!\n", 0, 0, 0 );
648 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
649 /* FIXME: is it correct to abort
650 * operation if control fails? */
657 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
658 rs->sr_text = "txn_abort (no-op) failed";
660 rs->sr_err = LDAP_X_NO_OPERATION;
662 /* Only free attrs if they were dup'd. */
663 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
667 /* may have changed in bdb_modify_internal() */
668 e->e_ocflags = dummy.e_ocflags;
669 rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
671 case DB_LOCK_DEADLOCK:
672 case DB_LOCK_NOTGRANTED:
675 dummy.e_attrs = NULL;
677 rs->sr_err = TXN_COMMIT( ltid, 0 );
680 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
681 opinfo.boi_oe.oe_key = NULL;
683 if( rs->sr_err != 0 ) {
684 Debug( LDAP_DEBUG_TRACE,
685 LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
686 op->o_noop ? "abort (no-op)" : "commit",
687 db_strerror(rs->sr_err), rs->sr_err );
688 rs->sr_err = LDAP_OTHER;
689 rs->sr_text = "commit failed";
694 Debug( LDAP_DEBUG_TRACE,
695 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
696 op->o_noop ? " (no-op)" : "",
697 dummy.e_id, op->o_req_dn.bv_val );
699 rs->sr_err = LDAP_SUCCESS;
701 if( num_ctrls ) rs->sr_ctrls = ctrls;
704 if( dummy.e_attrs ) {
705 attrs_free( dummy.e_attrs );
707 send_ldap_result( op, rs );
709 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
710 TXN_CHECKPOINT( bdb->bi_dbenv,
711 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
715 slap_graduate_commit_csn( op );
720 if ( opinfo.boi_oe.oe_key ) {
721 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
725 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
728 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
729 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
730 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
732 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
733 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
734 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );