1 /* modify.c - bdb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 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 if( err != LDAP_SUCCESS ) {
157 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
164 case LDAP_MOD_REPLACE:
165 Debug(LDAP_DEBUG_ARGS,
166 "bdb_modify_internal: replace %s\n",
167 mod->sm_desc->ad_cname.bv_val, 0, 0);
168 err = modify_replace_values( e, mod, get_permissiveModify(op),
169 text, textbuf, textlen );
170 if( err != LDAP_SUCCESS ) {
171 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
178 case LDAP_MOD_INCREMENT:
179 Debug(LDAP_DEBUG_ARGS,
180 "bdb_modify_internal: increment %s\n",
181 mod->sm_desc->ad_cname.bv_val, 0, 0);
182 err = modify_increment_values( e, mod, get_permissiveModify(op),
183 text, textbuf, textlen );
184 if( err != LDAP_SUCCESS ) {
185 Debug(LDAP_DEBUG_ARGS,
186 "bdb_modify_internal: %d %s\n",
193 case SLAP_MOD_SOFTADD:
194 Debug(LDAP_DEBUG_ARGS,
195 "bdb_modify_internal: softadd %s\n",
196 mod->sm_desc->ad_cname.bv_val, 0, 0);
197 /* Avoid problems in index_add_mods()
198 * We need to add index if necessary.
200 mod->sm_op = LDAP_MOD_ADD;
202 err = modify_add_values( e, mod, get_permissiveModify(op),
203 text, textbuf, textlen );
205 mod->sm_op = SLAP_MOD_SOFTADD;
207 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
211 if( err != LDAP_SUCCESS ) {
212 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
217 case SLAP_MOD_SOFTDEL:
218 Debug(LDAP_DEBUG_ARGS,
219 "bdb_modify_internal: softdel %s\n",
220 mod->sm_desc->ad_cname.bv_val, 0, 0);
221 /* Avoid problems in index_delete_mods()
222 * We need to add index if necessary.
224 mod->sm_op = LDAP_MOD_DELETE;
226 err = modify_delete_values( e, mod, get_permissiveModify(op),
227 text, textbuf, textlen );
229 mod->sm_op = SLAP_MOD_SOFTDEL;
231 if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
235 if( err != LDAP_SUCCESS ) {
236 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
241 case SLAP_MOD_ADD_IF_NOT_PRESENT:
242 if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) {
248 Debug(LDAP_DEBUG_ARGS,
249 "bdb_modify_internal: add_if_not_present %s\n",
250 mod->sm_desc->ad_cname.bv_val, 0, 0);
251 /* Avoid problems in index_add_mods()
252 * We need to add index if necessary.
254 mod->sm_op = LDAP_MOD_ADD;
256 err = modify_add_values( e, mod, get_permissiveModify(op),
257 text, textbuf, textlen );
259 mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
261 if( err != LDAP_SUCCESS ) {
262 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
268 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
270 *text = "Invalid modify operation";
272 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
276 if ( err != LDAP_SUCCESS ) {
277 attrs_free( e->e_attrs );
278 e->e_attrs = save_attrs;
279 /* unlock entry, delete from cache */
283 /* If objectClass was modified, reset the flags */
284 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
288 if ( glue_attr_delete ) e->e_ocflags = 0;
291 /* check if modified attribute was indexed
292 * but not in case of NOOP... */
294 bdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
298 /* check that the entry still obeys the schema */
300 rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
301 text, textbuf, textlen );
302 if ( rc != LDAP_SUCCESS || op->o_noop ) {
303 attrs_free( e->e_attrs );
304 /* clear the indexing flags */
305 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
306 ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
308 e->e_attrs = save_attrs;
310 if ( rc != LDAP_SUCCESS ) {
311 Debug( LDAP_DEBUG_ANY,
312 "entry failed schema check: %s\n",
316 /* if NOOP then silently revert to saved attrs */
320 /* structuralObjectClass modified! */
322 assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
324 bdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
325 1, e->e_attrs, save_attrs );
329 /* update the indices of the modified attributes */
331 /* start with deleting the old index entries */
332 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
333 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
336 ap->a_flags &= ~SLAP_ATTR_IXDEL;
337 a2 = attr_find( e->e_attrs, ap->a_desc );
339 /* need to detect which values were deleted */
341 vals = op->o_tmpalloc( (ap->a_numvals + 1) *
342 sizeof(struct berval), op->o_tmpmemctx );
344 for ( i=0; i < ap->a_numvals; i++ ) {
345 rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
346 &ap->a_nvals[i], NULL, op->o_tmpmemctx );
347 /* Save deleted values */
348 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
349 vals[j++] = ap->a_nvals[i];
353 /* attribute was completely deleted */
357 if ( !BER_BVISNULL( vals )) {
358 rc = bdb_index_values( op, tid, ap->a_desc,
359 vals, e->e_id, SLAP_INDEX_DELETE_OP );
360 if ( rc != LDAP_SUCCESS ) {
361 Debug( LDAP_DEBUG_ANY,
362 "%s: attribute \"%s\" index delete failure\n",
363 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
364 attrs_free( e->e_attrs );
365 e->e_attrs = save_attrs;
368 if ( vals != ap->a_nvals )
369 op->o_tmpfree( vals, op->o_tmpmemctx );
374 /* add the new index entries */
375 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
376 if (ap->a_flags & SLAP_ATTR_IXADD) {
377 ap->a_flags &= ~SLAP_ATTR_IXADD;
378 rc = bdb_index_values( op, tid, ap->a_desc,
380 e->e_id, SLAP_INDEX_ADD_OP );
381 if ( rc != LDAP_SUCCESS ) {
382 Debug( LDAP_DEBUG_ANY,
383 "%s: attribute \"%s\" index add failure\n",
384 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
385 attrs_free( e->e_attrs );
386 e->e_attrs = save_attrs;
397 bdb_modify( Operation *op, SlapReply *rs )
399 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
401 EntryInfo *ei = NULL;
402 int manageDSAit = get_manageDSAit( op );
403 char textbuf[SLAP_TEXT_BUFLEN];
404 size_t textlen = sizeof textbuf;
405 DB_TXN *ltid = NULL, *lt2;
406 struct bdb_op_info opinfo = {{{ 0 }}};
413 LDAPControl **preread_ctrl = NULL;
414 LDAPControl **postread_ctrl = NULL;
415 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
424 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
425 op->o_req_dn.bv_val, 0, 0 );
428 if( op->o_txnSpec ) {
429 /* acquire connection lock */
430 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
431 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
432 rs->sr_text = "invalid transaction identifier";
433 rs->sr_err = LDAP_X_TXN_ID_INVALID;
435 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
440 if( op->o_conn->c_txn_backend == NULL ) {
441 op->o_conn->c_txn_backend = op->o_bd;
443 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
444 rs->sr_text = "transaction cannot span multiple database contexts";
445 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
449 /* insert operation into transaction */
451 rs->sr_text = "transaction specified";
452 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
455 /* release connection lock */
456 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
459 send_ldap_result( op, rs );
465 ctrls[num_ctrls] = NULL;
467 /* Don't touch the opattrs, if this is a contextCSN update
468 * initiated from updatedn */
469 if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
470 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
472 slap_mods_opattrs( op, &op->orm_modlist, 1 );
476 retry: /* transaction retry */
477 if ( dummy.e_attrs ) {
478 attrs_free( dummy.e_attrs );
479 dummy.e_attrs = NULL;
482 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
485 Debug(LDAP_DEBUG_TRACE,
486 LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
488 rs->sr_err = TXN_ABORT( ltid );
490 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
491 opinfo.boi_oe.oe_key = NULL;
492 op->o_do_not_cache = opinfo.boi_acl_cache;
493 if( rs->sr_err != 0 ) {
494 rs->sr_err = LDAP_OTHER;
495 rs->sr_text = "internal error";
498 if ( op->o_abandon ) {
499 rs->sr_err = SLAPD_ABANDON;
502 bdb_trans_backoff( ++num_retries );
505 /* begin transaction */
506 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
507 bdb->bi_db_opflags );
509 if( rs->sr_err != 0 ) {
510 Debug( LDAP_DEBUG_TRACE,
511 LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
512 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
513 rs->sr_err = LDAP_OTHER;
514 rs->sr_text = "internal error";
518 opinfo.boi_oe.oe_key = bdb;
519 opinfo.boi_txn = ltid;
521 opinfo.boi_acl_cache = op->o_do_not_cache;
522 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
524 /* get entry or ancestor */
525 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
528 if ( rs->sr_err != 0 ) {
529 Debug( LDAP_DEBUG_TRACE,
530 LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
532 switch( rs->sr_err ) {
533 case DB_LOCK_DEADLOCK:
534 case DB_LOCK_NOTGRANTED:
539 rs->sr_text = "ldap server busy";
542 rs->sr_err = LDAP_OTHER;
543 rs->sr_text = "internal error";
550 /* acquire and lock entry */
551 /* FIXME: dn2entry() should return non-glue entry */
552 if (( rs->sr_err == DB_NOTFOUND ) ||
553 ( !manageDSAit && e && is_entry_glue( e )))
556 rs->sr_matched = ch_strdup( e->e_dn );
557 rs->sr_ref = is_entry_referral( e )
558 ? get_entry_referrals( op, e )
560 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
564 rs->sr_ref = referral_rewrite( default_referral, NULL,
565 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
568 rs->sr_err = LDAP_REFERRAL;
569 send_ldap_result( op, rs );
571 if ( rs->sr_ref != default_referral ) {
572 ber_bvarray_free( rs->sr_ref );
574 free( (char *)rs->sr_matched );
576 rs->sr_matched = NULL;
581 if ( !manageDSAit && is_entry_referral( e ) ) {
582 /* entry is a referral, don't allow modify */
583 rs->sr_ref = get_entry_referrals( op, e );
585 Debug( LDAP_DEBUG_TRACE,
586 LDAP_XSTRING(bdb_modify) ": entry is referral\n",
589 rs->sr_err = LDAP_REFERRAL;
590 rs->sr_matched = e->e_name.bv_val;
591 send_ldap_result( op, rs );
593 ber_bvarray_free( rs->sr_ref );
595 rs->sr_matched = NULL;
599 if ( get_assert( op ) &&
600 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
602 rs->sr_err = LDAP_ASSERTION_FAILED;
606 if( op->o_preread ) {
607 if( preread_ctrl == NULL ) {
608 preread_ctrl = &ctrls[num_ctrls++];
609 ctrls[num_ctrls] = NULL;
611 if ( slap_read_controls( op, rs, e,
612 &slap_pre_read_bv, preread_ctrl ) )
614 Debug( LDAP_DEBUG_TRACE,
615 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
616 "failed!\n", 0, 0, 0 );
617 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
618 /* FIXME: is it correct to abort
619 * operation if control fails? */
625 /* nested transaction */
626 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2, bdb->bi_db_opflags );
628 if( rs->sr_err != 0 ) {
629 Debug( LDAP_DEBUG_TRACE,
630 LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
631 db_strerror(rs->sr_err), rs->sr_err, 0 );
632 rs->sr_err = LDAP_OTHER;
633 rs->sr_text = "internal error";
636 /* Modify the entry */
638 rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
639 &dummy, &rs->sr_text, textbuf, textlen );
641 if( rs->sr_err != LDAP_SUCCESS ) {
642 Debug( LDAP_DEBUG_TRACE,
643 LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
645 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
646 rs->sr_err = opinfo.boi_err;
648 /* Only free attrs if they were dup'd. */
649 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
650 switch( rs->sr_err ) {
651 case DB_LOCK_DEADLOCK:
652 case DB_LOCK_NOTGRANTED:
658 /* change the entry itself */
659 rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
660 if ( rs->sr_err != 0 ) {
661 Debug( LDAP_DEBUG_TRACE,
662 LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
664 switch( rs->sr_err ) {
665 case DB_LOCK_DEADLOCK:
666 case DB_LOCK_NOTGRANTED:
669 rs->sr_text = "entry update failed";
673 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
674 rs->sr_err = LDAP_OTHER;
675 rs->sr_text = "txn_commit(2) failed";
679 if( op->o_postread ) {
680 if( postread_ctrl == NULL ) {
681 postread_ctrl = &ctrls[num_ctrls++];
682 ctrls[num_ctrls] = NULL;
684 if( slap_read_controls( op, rs, &dummy,
685 &slap_post_read_bv, postread_ctrl ) )
687 Debug( LDAP_DEBUG_TRACE,
688 "<=- " LDAP_XSTRING(bdb_modify)
689 ": post-read failed!\n", 0, 0, 0 );
690 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
691 /* FIXME: is it correct to abort
692 * operation if control fails? */
699 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
700 rs->sr_text = "txn_abort (no-op) failed";
702 rs->sr_err = LDAP_X_NO_OPERATION;
704 /* Only free attrs if they were dup'd. */
705 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
709 /* may have changed in bdb_modify_internal() */
710 e->e_ocflags = dummy.e_ocflags;
711 rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
713 case DB_LOCK_DEADLOCK:
714 case DB_LOCK_NOTGRANTED:
717 dummy.e_attrs = NULL;
719 rs->sr_err = TXN_COMMIT( ltid, 0 );
722 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
723 opinfo.boi_oe.oe_key = NULL;
725 if( rs->sr_err != 0 ) {
726 Debug( LDAP_DEBUG_TRACE,
727 LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
728 op->o_noop ? "abort (no-op)" : "commit",
729 db_strerror(rs->sr_err), rs->sr_err );
730 rs->sr_err = LDAP_OTHER;
731 rs->sr_text = "commit failed";
736 Debug( LDAP_DEBUG_TRACE,
737 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
738 op->o_noop ? " (no-op)" : "",
739 dummy.e_id, op->o_req_dn.bv_val );
741 rs->sr_err = LDAP_SUCCESS;
743 if( num_ctrls ) rs->sr_ctrls = ctrls;
746 if( dummy.e_attrs ) {
747 attrs_free( dummy.e_attrs );
749 send_ldap_result( op, rs );
751 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
752 TXN_CHECKPOINT( bdb->bi_dbenv,
753 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
757 slap_graduate_commit_csn( op );
762 if ( opinfo.boi_oe.oe_key ) {
763 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
767 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
770 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
771 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
772 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
774 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
775 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
776 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );