1 /* modify.c - mdb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2017 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 = mdb_index_mask( op->o_bd, desc, &ix_at );
49 ap = attr_find( oldattrs, desc );
50 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
53 * If using 32bit hashes, or substring index, must account for
54 * possible index collisions. If no substring index, and using
55 * 64bit hashes, assume we don't need to check for collisions.
57 * In 2.5 use refcounts and avoid all of this mess.
59 if (!slap_hash64(-1) || (ai->ai_indexmask & SLAP_INDEX_SUBSTR)) {
60 /* Find all other attrs that index to same slot */
61 for ( ap = newattrs; ap; ap = ap->a_next ) {
62 ai = mdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
63 if ( ai && ix2.bv_val == ix_at.bv_val )
64 ap->a_flags |= SLAP_ATTR_IXADD;
71 ap = attr_find( newattrs, desc );
72 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
77 int mdb_modify_internal(
80 Modifications *modlist,
86 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
90 Attribute *save_attrs;
91 Attribute *ap, *aold, *anew;
92 int glue_attr_delete = 0;
93 int softop, chkpresent;
96 MDB_cursor *mvc = NULL;
98 Debug( LDAP_DEBUG_TRACE, "mdb_modify_internal: 0x%08lx: %s\n",
101 if ( !acl_check_modlist( op, e, modlist )) {
102 return LDAP_INSUFFICIENT_ACCESS;
105 /* save_attrs will be disposed of by caller */
106 save_attrs = e->e_attrs;
107 e->e_attrs = attrs_dup( e->e_attrs );
109 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
112 switch( mod->sm_op ) {
114 case LDAP_MOD_REPLACE:
115 if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
116 value_match( &match, slap_schema.si_ad_structuralObjectClass,
117 slap_schema.si_ad_structuralObjectClass->
118 ad_type->sat_equality,
119 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
120 &mod->sm_values[0], &scbva[0], text );
121 if ( !match ) glue_attr_delete = 1;
124 if ( glue_attr_delete )
128 if ( glue_attr_delete ) {
129 Attribute **app = &e->e_attrs;
130 while ( *app != NULL ) {
131 if ( !is_at_operational( (*app)->a_desc->ad_type )) {
132 Attribute *save = *app;
133 *app = (*app)->a_next;
137 app = &(*app)->a_next;
141 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
145 aold = attr_find( e->e_attrs, mod->sm_desc );
147 a_flags = aold->a_flags;
151 switch ( mod->sm_op ) {
155 Debug(LDAP_DEBUG_ARGS,
156 "mdb_modify_internal: add %s\n",
157 mod->sm_desc->ad_cname.bv_val, 0, 0);
160 err = modify_add_values( e, mod, get_permissiveModify(op),
161 text, textbuf, textlen );
164 mod->sm_op = SLAP_MOD_SOFTADD;
165 if ( err == LDAP_TYPE_OR_VALUE_EXISTS )
169 mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
172 if( err != LDAP_SUCCESS ) {
173 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
177 anew = attr_find( e->e_attrs, mod->sm_desc );
180 /* check for big multivalued attrs */
181 if ( anew->a_numvals > mdb->mi_multi_hi )
182 anew->a_flags |= SLAP_ATTR_BIG_MULTI;
183 if ( anew->a_flags & SLAP_ATTR_BIG_MULTI ) {
185 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
189 /* if prev was set, just add new values */
190 if (a_flags & SLAP_ATTR_BIG_MULTI ) {
191 anew = (Attribute *)mod;
194 anew->a_nvals = anew->a_vals;
196 err = mdb_mval_put(op, mvc, e->e_id, anew);
197 if (a_flags & SLAP_ATTR_BIG_MULTI ) {
198 /* Undo nvals tweak */
199 if (anew->a_nvals == anew->a_vals)
200 anew->a_nvals = NULL;
206 case LDAP_MOD_DELETE:
207 if ( glue_attr_delete ) {
213 Debug(LDAP_DEBUG_ARGS,
214 "mdb_modify_internal: delete %s\n",
215 mod->sm_desc->ad_cname.bv_val, 0, 0);
217 err = modify_delete_values( e, mod, get_permissiveModify(op),
218 text, textbuf, textlen );
221 mod->sm_op = SLAP_MOD_SOFTDEL;
222 if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
228 if( err != LDAP_SUCCESS ) {
229 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
234 /* check for big multivalued attrs */
235 if (a_flags & SLAP_ATTR_BIG_MULTI) {
238 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
242 if ( mod->sm_numvals ) {
243 anew = attr_find( e->e_attrs, mod->sm_desc );
245 if ( anew->a_numvals < mdb->mi_multi_lo ) {
246 anew->a_flags ^= SLAP_ATTR_BIG_MULTI;
249 anew = (Attribute *)mod;
256 /* delete all values */
258 anew->a_desc = mod->sm_desc;
261 err = mdb_mval_del( op, mvc, e->e_id, anew );
266 case LDAP_MOD_REPLACE:
267 Debug(LDAP_DEBUG_ARGS,
268 "mdb_modify_internal: replace %s\n",
269 mod->sm_desc->ad_cname.bv_val, 0, 0);
270 err = modify_replace_values( e, mod, get_permissiveModify(op),
271 text, textbuf, textlen );
272 if( err != LDAP_SUCCESS ) {
273 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
277 if (a_flags & SLAP_ATTR_BIG_MULTI) {
280 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
284 /* delete all values */
286 anew->a_desc = mod->sm_desc;
288 err = mdb_mval_del( op, mvc, e->e_id, anew );
292 anew = attr_find( e->e_attrs, mod->sm_desc );
293 if (mod->sm_numvals >= mdb->mi_multi_lo) {
294 anew->a_flags |= SLAP_ATTR_BIG_MULTI;
296 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
300 err = mdb_mval_put(op, mvc, e->e_id, anew);
302 /* revert back to normal attr */
303 anew->a_flags &= ~SLAP_ATTR_BIG_MULTI;
308 case LDAP_MOD_INCREMENT:
309 Debug(LDAP_DEBUG_ARGS,
310 "mdb_modify_internal: increment %s\n",
311 mod->sm_desc->ad_cname.bv_val, 0, 0);
312 err = modify_increment_values( e, mod, get_permissiveModify(op),
313 text, textbuf, textlen );
314 if( err != LDAP_SUCCESS ) {
315 Debug(LDAP_DEBUG_ARGS,
316 "mdb_modify_internal: %d %s\n",
323 case SLAP_MOD_SOFTADD:
324 Debug(LDAP_DEBUG_ARGS,
325 "mdb_modify_internal: softadd %s\n",
326 mod->sm_desc->ad_cname.bv_val, 0, 0);
327 /* Avoid problems in index_add_mods()
328 * We need to add index if necessary.
330 mod->sm_op = LDAP_MOD_ADD;
334 case SLAP_MOD_SOFTDEL:
335 Debug(LDAP_DEBUG_ARGS,
336 "mdb_modify_internal: softdel %s\n",
337 mod->sm_desc->ad_cname.bv_val, 0, 0);
338 /* Avoid problems in index_delete_mods()
339 * We need to add index if necessary.
341 mod->sm_op = LDAP_MOD_DELETE;
345 case SLAP_MOD_ADD_IF_NOT_PRESENT:
346 if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) {
352 Debug(LDAP_DEBUG_ARGS,
353 "mdb_modify_internal: add_if_not_present %s\n",
354 mod->sm_desc->ad_cname.bv_val, 0, 0);
355 /* Avoid problems in index_add_mods()
356 * We need to add index if necessary.
358 mod->sm_op = LDAP_MOD_ADD;
363 Debug(LDAP_DEBUG_ANY, "mdb_modify_internal: invalid op %d\n",
365 *text = "Invalid modify operation";
367 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
371 if ( err != LDAP_SUCCESS ) {
372 attrs_free( e->e_attrs );
373 e->e_attrs = save_attrs;
374 /* unlock entry, delete from cache */
378 /* If objectClass was modified, reset the flags */
379 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
383 if ( glue_attr_delete ) e->e_ocflags = 0;
386 /* check if modified attribute was indexed
387 * but not in case of NOOP... */
389 mdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
393 /* check that the entry still obeys the schema */
395 rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
396 text, textbuf, textlen );
397 if ( rc != LDAP_SUCCESS || op->o_noop ) {
398 attrs_free( e->e_attrs );
399 /* clear the indexing flags */
400 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
401 ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
403 e->e_attrs = save_attrs;
405 if ( rc != LDAP_SUCCESS ) {
406 Debug( LDAP_DEBUG_ANY,
407 "entry failed schema check: %s\n",
411 /* if NOOP then silently revert to saved attrs */
415 /* structuralObjectClass modified! */
417 assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
419 mdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
420 1, e->e_attrs, save_attrs );
424 /* update the indices of the modified attributes */
426 /* start with deleting the old index entries */
427 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
428 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
431 ap->a_flags &= ~SLAP_ATTR_IXDEL;
432 a2 = attr_find( e->e_attrs, ap->a_desc );
434 /* need to detect which values were deleted */
436 /* let add know there were deletes */
437 if ( a2->a_flags & SLAP_ATTR_IXADD )
438 a2->a_flags |= SLAP_ATTR_IXDEL;
439 vals = op->o_tmpalloc( (ap->a_numvals + 1) *
440 sizeof(struct berval), op->o_tmpmemctx );
442 for ( i=0; i < ap->a_numvals; i++ ) {
443 rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
444 &ap->a_nvals[i], NULL, op->o_tmpmemctx );
445 /* Save deleted values */
446 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
447 vals[j++] = ap->a_nvals[i];
451 /* attribute was completely deleted */
455 if ( !BER_BVISNULL( vals )) {
456 rc = mdb_index_values( op, tid, ap->a_desc,
457 vals, e->e_id, SLAP_INDEX_DELETE_OP );
458 if ( rc != LDAP_SUCCESS ) {
459 Debug( LDAP_DEBUG_ANY,
460 "%s: attribute \"%s\" index delete failure\n",
461 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
462 attrs_free( e->e_attrs );
463 e->e_attrs = save_attrs;
466 if ( vals != ap->a_nvals )
467 op->o_tmpfree( vals, op->o_tmpmemctx );
472 /* add the new index entries */
473 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
474 if (ap->a_flags & SLAP_ATTR_IXADD) {
475 ap->a_flags &= ~SLAP_ATTR_IXADD;
476 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
477 /* if any values were deleted, we must readd index
478 * for all remaining values.
480 ap->a_flags &= ~SLAP_ATTR_IXDEL;
481 rc = mdb_index_values( op, tid, ap->a_desc,
483 e->e_id, SLAP_INDEX_ADD_OP );
486 /* if this was only an add, we only need to index
489 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
491 if ( ml->sml_desc != ap->a_desc || !ml->sml_numvals )
494 switch( ml->sml_op ) {
496 case LDAP_MOD_REPLACE:
497 case LDAP_MOD_INCREMENT:
498 case SLAP_MOD_SOFTADD:
499 case SLAP_MOD_ADD_IF_NOT_PRESENT:
500 if ( ml->sml_op == LDAP_MOD_INCREMENT )
502 else if ( ml->sml_nvalues )
503 vals = ml->sml_nvalues;
505 vals = ml->sml_values;
506 rc = mdb_index_values( op, tid, ap->a_desc,
507 vals, e->e_id, SLAP_INDEX_ADD_OP );
513 /* This attr was affected by a modify of a subtype, so
514 * there was no direct match in the modlist. Just readd
518 rc = mdb_index_values( op, tid, ap->a_desc,
520 e->e_id, SLAP_INDEX_ADD_OP );
523 if ( rc != LDAP_SUCCESS ) {
524 Debug( LDAP_DEBUG_ANY,
525 "%s: attribute \"%s\" index add failure\n",
526 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
527 attrs_free( e->e_attrs );
528 e->e_attrs = save_attrs;
539 mdb_modify( Operation *op, SlapReply *rs )
541 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
543 int manageDSAit = get_manageDSAit( op );
544 char textbuf[SLAP_TEXT_BUFLEN];
545 size_t textlen = sizeof textbuf;
547 mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
550 LDAPControl **preread_ctrl = NULL;
551 LDAPControl **postread_ctrl = NULL;
552 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
554 int numads = mdb->mi_numads;
556 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(mdb_modify) ": %s\n",
557 op->o_req_dn.bv_val, 0, 0 );
560 if( op->o_txnSpec && txn_preop( op, rs ))
564 ctrls[num_ctrls] = NULL;
566 /* begin transaction */
567 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
569 if( rs->sr_err != 0 ) {
570 Debug( LDAP_DEBUG_TRACE,
571 LDAP_XSTRING(mdb_modify) ": txn_begin failed: "
572 "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
573 rs->sr_err = LDAP_OTHER;
574 rs->sr_text = "internal error";
579 /* Don't touch the opattrs, if this is a contextCSN update
580 * initiated from updatedn */
581 if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
582 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
584 slap_mods_opattrs( op, &op->orm_modlist, 1 );
587 /* get entry or ancestor */
588 rs->sr_err = mdb_dn2entry( op, txn, NULL, &op->o_req_ndn, &e, NULL, 1 );
590 if ( rs->sr_err != 0 ) {
591 Debug( LDAP_DEBUG_TRACE,
592 LDAP_XSTRING(mdb_modify) ": dn2entry failed (%d)\n",
594 switch( rs->sr_err ) {
598 rs->sr_text = "ldap server busy";
601 rs->sr_err = LDAP_OTHER;
602 rs->sr_text = "internal error";
607 /* acquire and lock entry */
608 /* FIXME: dn2entry() should return non-glue entry */
609 if (( rs->sr_err == MDB_NOTFOUND ) ||
610 ( !manageDSAit && e && is_entry_glue( e )))
613 rs->sr_matched = ch_strdup( e->e_dn );
614 if ( is_entry_referral( e )) {
615 BerVarray ref = get_entry_referrals( op, e );
616 rs->sr_ref = referral_rewrite( ref, &e->e_name,
617 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
618 ber_bvarray_free( ref );
622 mdb_entry_return( op, e );
626 rs->sr_ref = referral_rewrite( default_referral, NULL,
627 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
630 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
631 rs->sr_err = LDAP_REFERRAL;
632 send_ldap_result( op, rs );
636 if ( !manageDSAit && is_entry_referral( e ) ) {
637 /* entry is a referral, don't allow modify */
638 rs->sr_ref = get_entry_referrals( op, e );
640 Debug( LDAP_DEBUG_TRACE,
641 LDAP_XSTRING(mdb_modify) ": entry is referral\n",
644 rs->sr_err = LDAP_REFERRAL;
645 rs->sr_matched = e->e_name.bv_val;
646 rs->sr_flags = REP_REF_MUSTBEFREED;
647 send_ldap_result( op, rs );
648 rs->sr_matched = NULL;
652 if ( get_assert( op ) &&
653 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
655 rs->sr_err = LDAP_ASSERTION_FAILED;
659 if( op->o_preread ) {
660 if( preread_ctrl == NULL ) {
661 preread_ctrl = &ctrls[num_ctrls++];
662 ctrls[num_ctrls] = NULL;
664 if ( slap_read_controls( op, rs, e,
665 &slap_pre_read_bv, preread_ctrl ) )
667 Debug( LDAP_DEBUG_TRACE,
668 "<=- " LDAP_XSTRING(mdb_modify) ": pre-read "
669 "failed!\n", 0, 0, 0 );
670 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
671 /* FIXME: is it correct to abort
672 * operation if control fails? */
678 /* Modify the entry */
680 rs->sr_err = mdb_modify_internal( op, txn, op->orm_modlist,
681 &dummy, &rs->sr_text, textbuf, textlen );
683 if( rs->sr_err != LDAP_SUCCESS ) {
684 Debug( LDAP_DEBUG_TRACE,
685 LDAP_XSTRING(mdb_modify) ": modify failed (%d)\n",
687 /* Only free attrs if they were dup'd. */
688 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
692 /* change the entry itself */
693 rs->sr_err = mdb_id2entry_update( op, txn, NULL, &dummy );
694 if ( rs->sr_err != 0 ) {
695 Debug( LDAP_DEBUG_TRACE,
696 LDAP_XSTRING(mdb_modify) ": id2entry update failed " "(%d)\n",
698 if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) {
699 rs->sr_text = "entry too big";
701 rs->sr_err = LDAP_OTHER;
702 rs->sr_text = "entry update failed";
707 if( op->o_postread ) {
708 if( postread_ctrl == NULL ) {
709 postread_ctrl = &ctrls[num_ctrls++];
710 ctrls[num_ctrls] = NULL;
712 if( slap_read_controls( op, rs, &dummy,
713 &slap_post_read_bv, postread_ctrl ) )
715 Debug( LDAP_DEBUG_TRACE,
716 "<=- " LDAP_XSTRING(mdb_modify)
717 ": post-read failed!\n", 0, 0, 0 );
718 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
719 /* FIXME: is it correct to abort
720 * operation if control fails? */
726 /* Only free attrs if they were dup'd. */
727 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
728 if( moi == &opinfo ) {
729 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
730 opinfo.moi_oe.oe_key = NULL;
732 mdb->mi_numads = numads;
733 mdb_txn_abort( txn );
734 rs->sr_err = LDAP_X_NO_OPERATION;
738 rs->sr_err = mdb_txn_commit( txn );
740 mdb->mi_numads = numads;
745 if( rs->sr_err != 0 ) {
746 Debug( LDAP_DEBUG_ANY,
747 LDAP_XSTRING(mdb_modify) ": txn_%s failed: %s (%d)\n",
748 op->o_noop ? "abort (no-op)" : "commit",
749 mdb_strerror(rs->sr_err), rs->sr_err );
750 rs->sr_err = LDAP_OTHER;
751 rs->sr_text = "commit failed";
756 Debug( LDAP_DEBUG_TRACE,
757 LDAP_XSTRING(mdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
758 op->o_noop ? " (no-op)" : "",
759 dummy.e_id, op->o_req_dn.bv_val );
761 rs->sr_err = LDAP_SUCCESS;
763 if( num_ctrls ) rs->sr_ctrls = ctrls;
766 if( dummy.e_attrs ) {
767 attrs_free( dummy.e_attrs );
769 send_ldap_result( op, rs );
772 if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
773 TXN_CHECKPOINT( mdb->bi_dbenv,
774 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
779 slap_graduate_commit_csn( op );
781 if( moi == &opinfo ) {
783 mdb->mi_numads = numads;
784 mdb_txn_abort( txn );
786 if ( opinfo.moi_oe.oe_key ) {
787 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
794 mdb_entry_return( op, e );
797 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
798 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
799 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
801 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
802 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
803 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );