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 );
187 mval_fail: strncpy( textbuf, mdb_strerror( err ), textlen );
192 /* if prev was set, just add new values */
193 if (a_flags & SLAP_ATTR_BIG_MULTI ) {
194 anew = (Attribute *)mod;
197 anew->a_nvals = anew->a_vals;
199 err = mdb_mval_put(op, mvc, e->e_id, anew);
200 if (a_flags & SLAP_ATTR_BIG_MULTI ) {
201 /* Undo nvals tweak */
202 if (anew->a_nvals == anew->a_vals)
203 anew->a_nvals = NULL;
211 case LDAP_MOD_DELETE:
212 if ( glue_attr_delete ) {
218 Debug(LDAP_DEBUG_ARGS,
219 "mdb_modify_internal: delete %s\n",
220 mod->sm_desc->ad_cname.bv_val, 0, 0);
222 err = modify_delete_values( e, mod, get_permissiveModify(op),
223 text, textbuf, textlen );
226 mod->sm_op = SLAP_MOD_SOFTDEL;
227 if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
233 if( err != LDAP_SUCCESS ) {
234 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
239 /* check for big multivalued attrs */
240 if (a_flags & SLAP_ATTR_BIG_MULTI) {
243 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
247 if ( mod->sm_numvals ) {
248 anew = attr_find( e->e_attrs, mod->sm_desc );
250 if ( anew->a_numvals < mdb->mi_multi_lo ) {
251 anew->a_flags ^= SLAP_ATTR_BIG_MULTI;
254 anew = (Attribute *)mod;
261 /* delete all values */
263 anew->a_desc = mod->sm_desc;
266 err = mdb_mval_del( op, mvc, e->e_id, anew );
273 case LDAP_MOD_REPLACE:
274 Debug(LDAP_DEBUG_ARGS,
275 "mdb_modify_internal: replace %s\n",
276 mod->sm_desc->ad_cname.bv_val, 0, 0);
277 err = modify_replace_values( e, mod, get_permissiveModify(op),
278 text, textbuf, textlen );
279 if( err != LDAP_SUCCESS ) {
280 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
284 if (a_flags & SLAP_ATTR_BIG_MULTI) {
287 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
291 /* delete all values */
293 anew->a_desc = mod->sm_desc;
295 err = mdb_mval_del( op, mvc, e->e_id, anew );
299 anew = attr_find( e->e_attrs, mod->sm_desc );
300 if (mod->sm_numvals > mdb->mi_multi_hi) {
301 anew->a_flags |= SLAP_ATTR_BIG_MULTI;
303 err = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
307 err = mdb_mval_put(op, mvc, e->e_id, anew);
311 /* revert back to normal attr */
312 anew->a_flags &= ~SLAP_ATTR_BIG_MULTI;
317 case LDAP_MOD_INCREMENT:
318 Debug(LDAP_DEBUG_ARGS,
319 "mdb_modify_internal: increment %s\n",
320 mod->sm_desc->ad_cname.bv_val, 0, 0);
321 err = modify_increment_values( e, mod, get_permissiveModify(op),
322 text, textbuf, textlen );
323 if( err != LDAP_SUCCESS ) {
324 Debug(LDAP_DEBUG_ARGS,
325 "mdb_modify_internal: %d %s\n",
332 case SLAP_MOD_SOFTADD:
333 Debug(LDAP_DEBUG_ARGS,
334 "mdb_modify_internal: softadd %s\n",
335 mod->sm_desc->ad_cname.bv_val, 0, 0);
336 /* Avoid problems in index_add_mods()
337 * We need to add index if necessary.
339 mod->sm_op = LDAP_MOD_ADD;
344 case SLAP_MOD_SOFTDEL:
345 Debug(LDAP_DEBUG_ARGS,
346 "mdb_modify_internal: softdel %s\n",
347 mod->sm_desc->ad_cname.bv_val, 0, 0);
348 /* Avoid problems in index_delete_mods()
349 * We need to add index if necessary.
351 mod->sm_op = LDAP_MOD_DELETE;
355 case SLAP_MOD_ADD_IF_NOT_PRESENT:
356 if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) {
362 Debug(LDAP_DEBUG_ARGS,
363 "mdb_modify_internal: add_if_not_present %s\n",
364 mod->sm_desc->ad_cname.bv_val, 0, 0);
365 /* Avoid problems in index_add_mods()
366 * We need to add index if necessary.
368 mod->sm_op = LDAP_MOD_ADD;
374 Debug(LDAP_DEBUG_ANY, "mdb_modify_internal: invalid op %d\n",
376 *text = "Invalid modify operation";
378 Debug(LDAP_DEBUG_ARGS, "mdb_modify_internal: %d %s\n",
382 if ( err != LDAP_SUCCESS ) {
383 attrs_free( e->e_attrs );
384 e->e_attrs = save_attrs;
385 /* unlock entry, delete from cache */
389 /* If objectClass was modified, reset the flags */
390 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
394 if ( glue_attr_delete ) e->e_ocflags = 0;
397 /* check if modified attribute was indexed
398 * but not in case of NOOP... */
400 mdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
404 /* check that the entry still obeys the schema */
406 rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
407 text, textbuf, textlen );
408 if ( rc != LDAP_SUCCESS || op->o_noop ) {
409 attrs_free( e->e_attrs );
410 /* clear the indexing flags */
411 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
412 ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
414 e->e_attrs = save_attrs;
416 if ( rc != LDAP_SUCCESS ) {
417 Debug( LDAP_DEBUG_ANY,
418 "entry failed schema check: %s\n",
422 /* if NOOP then silently revert to saved attrs */
426 /* structuralObjectClass modified! */
428 assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
430 mdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
431 1, e->e_attrs, save_attrs );
435 /* update the indices of the modified attributes */
437 /* start with deleting the old index entries */
438 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
439 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
442 ap->a_flags &= ~SLAP_ATTR_IXDEL;
443 a2 = attr_find( e->e_attrs, ap->a_desc );
445 /* need to detect which values were deleted */
447 /* let add know there were deletes */
448 if ( a2->a_flags & SLAP_ATTR_IXADD )
449 a2->a_flags |= SLAP_ATTR_IXDEL;
450 vals = op->o_tmpalloc( (ap->a_numvals + 1) *
451 sizeof(struct berval), op->o_tmpmemctx );
453 for ( i=0; i < ap->a_numvals; i++ ) {
454 rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
455 &ap->a_nvals[i], NULL, op->o_tmpmemctx );
456 /* Save deleted values */
457 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
458 vals[j++] = ap->a_nvals[i];
462 /* attribute was completely deleted */
466 if ( !BER_BVISNULL( vals )) {
467 rc = mdb_index_values( op, tid, ap->a_desc,
468 vals, e->e_id, SLAP_INDEX_DELETE_OP );
469 if ( rc != LDAP_SUCCESS ) {
470 Debug( LDAP_DEBUG_ANY,
471 "%s: attribute \"%s\" index delete failure\n",
472 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
473 attrs_free( e->e_attrs );
474 e->e_attrs = save_attrs;
477 if ( vals != ap->a_nvals )
478 op->o_tmpfree( vals, op->o_tmpmemctx );
483 /* add the new index entries */
484 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
485 if (ap->a_flags & SLAP_ATTR_IXADD) {
486 ap->a_flags &= ~SLAP_ATTR_IXADD;
487 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
488 /* if any values were deleted, we must readd index
489 * for all remaining values.
491 ap->a_flags &= ~SLAP_ATTR_IXDEL;
492 rc = mdb_index_values( op, tid, ap->a_desc,
494 e->e_id, SLAP_INDEX_ADD_OP );
497 /* if this was only an add, we only need to index
500 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
502 if ( ml->sml_desc != ap->a_desc || !ml->sml_numvals )
505 switch( ml->sml_op ) {
507 case LDAP_MOD_REPLACE:
508 case LDAP_MOD_INCREMENT:
509 case SLAP_MOD_SOFTADD:
510 case SLAP_MOD_ADD_IF_NOT_PRESENT:
511 if ( ml->sml_op == LDAP_MOD_INCREMENT )
513 else if ( ml->sml_nvalues )
514 vals = ml->sml_nvalues;
516 vals = ml->sml_values;
517 rc = mdb_index_values( op, tid, ap->a_desc,
518 vals, e->e_id, SLAP_INDEX_ADD_OP );
524 /* This attr was affected by a modify of a subtype, so
525 * there was no direct match in the modlist. Just readd
529 rc = mdb_index_values( op, tid, ap->a_desc,
531 e->e_id, SLAP_INDEX_ADD_OP );
534 if ( rc != LDAP_SUCCESS ) {
535 Debug( LDAP_DEBUG_ANY,
536 "%s: attribute \"%s\" index add failure\n",
537 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
538 attrs_free( e->e_attrs );
539 e->e_attrs = save_attrs;
550 mdb_modify( Operation *op, SlapReply *rs )
552 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
554 int manageDSAit = get_manageDSAit( op );
555 char textbuf[SLAP_TEXT_BUFLEN];
556 size_t textlen = sizeof textbuf;
558 mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
561 LDAPControl **preread_ctrl = NULL;
562 LDAPControl **postread_ctrl = NULL;
563 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
565 int numads = mdb->mi_numads;
567 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(mdb_modify) ": %s\n",
568 op->o_req_dn.bv_val, 0, 0 );
571 if( op->o_txnSpec && txn_preop( op, rs ))
575 ctrls[num_ctrls] = NULL;
577 /* begin transaction */
578 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
580 if( rs->sr_err != 0 ) {
581 Debug( LDAP_DEBUG_TRACE,
582 LDAP_XSTRING(mdb_modify) ": txn_begin failed: "
583 "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
584 rs->sr_err = LDAP_OTHER;
585 rs->sr_text = "internal error";
590 /* Don't touch the opattrs, if this is a contextCSN update
591 * initiated from updatedn */
592 if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
593 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
595 slap_mods_opattrs( op, &op->orm_modlist, 1 );
598 /* get entry or ancestor */
599 rs->sr_err = mdb_dn2entry( op, txn, NULL, &op->o_req_ndn, &e, NULL, 1 );
601 if ( rs->sr_err != 0 ) {
602 Debug( LDAP_DEBUG_TRACE,
603 LDAP_XSTRING(mdb_modify) ": dn2entry failed (%d)\n",
605 switch( rs->sr_err ) {
609 rs->sr_text = "ldap server busy";
612 rs->sr_err = LDAP_OTHER;
613 rs->sr_text = "internal error";
618 /* acquire and lock entry */
619 /* FIXME: dn2entry() should return non-glue entry */
620 if (( rs->sr_err == MDB_NOTFOUND ) ||
621 ( !manageDSAit && e && is_entry_glue( e )))
624 rs->sr_matched = ch_strdup( e->e_dn );
625 if ( is_entry_referral( e )) {
626 BerVarray ref = get_entry_referrals( op, e );
627 rs->sr_ref = referral_rewrite( ref, &e->e_name,
628 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
629 ber_bvarray_free( ref );
633 mdb_entry_return( op, e );
637 rs->sr_ref = referral_rewrite( default_referral, NULL,
638 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
641 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
642 rs->sr_err = LDAP_REFERRAL;
643 send_ldap_result( op, rs );
647 if ( !manageDSAit && is_entry_referral( e ) ) {
648 /* entry is a referral, don't allow modify */
649 rs->sr_ref = get_entry_referrals( op, e );
651 Debug( LDAP_DEBUG_TRACE,
652 LDAP_XSTRING(mdb_modify) ": entry is referral\n",
655 rs->sr_err = LDAP_REFERRAL;
656 rs->sr_matched = e->e_name.bv_val;
657 rs->sr_flags = REP_REF_MUSTBEFREED;
658 send_ldap_result( op, rs );
659 rs->sr_matched = NULL;
663 if ( get_assert( op ) &&
664 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
666 rs->sr_err = LDAP_ASSERTION_FAILED;
670 if( op->o_preread ) {
671 if( preread_ctrl == NULL ) {
672 preread_ctrl = &ctrls[num_ctrls++];
673 ctrls[num_ctrls] = NULL;
675 if ( slap_read_controls( op, rs, e,
676 &slap_pre_read_bv, preread_ctrl ) )
678 Debug( LDAP_DEBUG_TRACE,
679 "<=- " LDAP_XSTRING(mdb_modify) ": pre-read "
680 "failed!\n", 0, 0, 0 );
681 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
682 /* FIXME: is it correct to abort
683 * operation if control fails? */
689 /* Modify the entry */
691 rs->sr_err = mdb_modify_internal( op, txn, op->orm_modlist,
692 &dummy, &rs->sr_text, textbuf, textlen );
694 if( rs->sr_err != LDAP_SUCCESS ) {
695 Debug( LDAP_DEBUG_TRACE,
696 LDAP_XSTRING(mdb_modify) ": modify failed (%d)\n",
698 /* Only free attrs if they were dup'd. */
699 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
703 /* change the entry itself */
704 rs->sr_err = mdb_id2entry_update( op, txn, NULL, &dummy );
705 if ( rs->sr_err != 0 ) {
706 Debug( LDAP_DEBUG_TRACE,
707 LDAP_XSTRING(mdb_modify) ": id2entry update failed " "(%d)\n",
709 if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ) {
710 rs->sr_text = "entry too big";
712 rs->sr_err = LDAP_OTHER;
713 rs->sr_text = "entry update failed";
718 if( op->o_postread ) {
719 if( postread_ctrl == NULL ) {
720 postread_ctrl = &ctrls[num_ctrls++];
721 ctrls[num_ctrls] = NULL;
723 if( slap_read_controls( op, rs, &dummy,
724 &slap_post_read_bv, postread_ctrl ) )
726 Debug( LDAP_DEBUG_TRACE,
727 "<=- " LDAP_XSTRING(mdb_modify)
728 ": post-read failed!\n", 0, 0, 0 );
729 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
730 /* FIXME: is it correct to abort
731 * operation if control fails? */
737 /* Only free attrs if they were dup'd. */
738 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
739 if( moi == &opinfo ) {
740 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
741 opinfo.moi_oe.oe_key = NULL;
743 mdb->mi_numads = numads;
744 mdb_txn_abort( txn );
745 rs->sr_err = LDAP_X_NO_OPERATION;
749 rs->sr_err = mdb_txn_commit( txn );
751 mdb->mi_numads = numads;
756 if( rs->sr_err != 0 ) {
757 Debug( LDAP_DEBUG_ANY,
758 LDAP_XSTRING(mdb_modify) ": txn_%s failed: %s (%d)\n",
759 op->o_noop ? "abort (no-op)" : "commit",
760 mdb_strerror(rs->sr_err), rs->sr_err );
761 rs->sr_err = LDAP_OTHER;
762 rs->sr_text = "commit failed";
767 Debug( LDAP_DEBUG_TRACE,
768 LDAP_XSTRING(mdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
769 op->o_noop ? " (no-op)" : "",
770 dummy.e_id, op->o_req_dn.bv_val );
772 rs->sr_err = LDAP_SUCCESS;
774 if( num_ctrls ) rs->sr_ctrls = ctrls;
777 if( dummy.e_attrs ) {
778 attrs_free( dummy.e_attrs );
780 send_ldap_result( op, rs );
783 if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
784 TXN_CHECKPOINT( mdb->bi_dbenv,
785 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
790 slap_graduate_commit_csn( op );
792 if( moi == &opinfo ) {
794 mdb->mi_numads = numads;
795 mdb_txn_abort( txn );
797 if ( opinfo.moi_oe.oe_key ) {
798 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
805 mdb_entry_return( op, e );
808 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
809 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
810 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
812 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
813 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
814 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );