1 /* modify.c - mdb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2013 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;
52 /* Find all other attrs that index to same slot */
53 for ( ap = newattrs; ap; ap = ap->a_next ) {
54 ai = mdb_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 mdb_modify_internal(
71 Modifications *modlist,
80 Attribute *save_attrs;
82 int glue_attr_delete = 0;
85 Debug( LDAP_DEBUG_TRACE, "mdb_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 caller */
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 "mdb_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, "mdb_modify_internal: %d %s\n",
145 case LDAP_MOD_DELETE:
146 if ( glue_attr_delete ) {
151 Debug(LDAP_DEBUG_ARGS,
152 "mdb_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, "mdb_modify_internal: %d %s\n",
164 case LDAP_MOD_REPLACE:
165 Debug(LDAP_DEBUG_ARGS,
166 "mdb_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, "mdb_modify_internal: %d %s\n",
178 case LDAP_MOD_INCREMENT:
179 Debug(LDAP_DEBUG_ARGS,
180 "mdb_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 "mdb_modify_internal: %d %s\n",
193 case SLAP_MOD_SOFTADD:
194 Debug(LDAP_DEBUG_ARGS,
195 "mdb_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, "mdb_modify_internal: %d %s\n",
217 case SLAP_MOD_SOFTDEL:
218 Debug(LDAP_DEBUG_ARGS,
219 "mdb_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, "mdb_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 "mdb_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, "mdb_modify_internal: %d %s\n",
268 Debug(LDAP_DEBUG_ANY, "mdb_modify_internal: invalid op %d\n",
270 *text = "Invalid modify operation";
272 Debug(LDAP_DEBUG_ARGS, "mdb_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 mdb_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 mdb_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 = mdb_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 = mdb_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 mdb_modify( Operation *op, SlapReply *rs )
399 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
401 int manageDSAit = get_manageDSAit( op );
402 char textbuf[SLAP_TEXT_BUFLEN];
403 size_t textlen = sizeof textbuf;
405 mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
408 LDAPControl **preread_ctrl = NULL;
409 LDAPControl **postread_ctrl = NULL;
410 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
417 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(mdb_modify) ": %s\n",
418 op->o_req_dn.bv_val, 0, 0 );
421 if( op->o_txnSpec ) {
422 /* acquire connection lock */
423 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
424 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
425 rs->sr_text = "invalid transaction identifier";
426 rs->sr_err = LDAP_X_TXN_ID_INVALID;
428 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
433 if( op->o_conn->c_txn_backend == NULL ) {
434 op->o_conn->c_txn_backend = op->o_bd;
436 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
437 rs->sr_text = "transaction cannot span multiple database contexts";
438 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
442 /* insert operation into transaction */
444 rs->sr_text = "transaction specified";
445 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
448 /* release connection lock */
449 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
452 send_ldap_result( op, rs );
458 ctrls[num_ctrls] = NULL;
460 /* begin transaction */
461 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
463 if( rs->sr_err != 0 ) {
464 Debug( LDAP_DEBUG_TRACE,
465 LDAP_XSTRING(mdb_modify) ": txn_begin failed: "
466 "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
467 rs->sr_err = LDAP_OTHER;
468 rs->sr_text = "internal error";
473 /* Don't touch the opattrs, if this is a contextCSN update
474 * initiated from updatedn */
475 if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
476 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
478 slap_mods_opattrs( op, &op->orm_modlist, 1 );
481 /* get entry or ancestor */
482 rs->sr_err = mdb_dn2entry( op, txn, NULL, &op->o_req_ndn, &e, NULL, 1 );
484 if ( rs->sr_err != 0 ) {
485 Debug( LDAP_DEBUG_TRACE,
486 LDAP_XSTRING(mdb_modify) ": dn2entry failed (%d)\n",
488 switch( rs->sr_err ) {
492 rs->sr_text = "ldap server busy";
495 rs->sr_err = LDAP_OTHER;
496 rs->sr_text = "internal error";
501 /* acquire and lock entry */
502 /* FIXME: dn2entry() should return non-glue entry */
503 if (( rs->sr_err == MDB_NOTFOUND ) ||
504 ( !manageDSAit && e && is_entry_glue( e )))
507 rs->sr_matched = ch_strdup( e->e_dn );
508 if ( is_entry_referral( e )) {
509 BerVarray ref = get_entry_referrals( op, e );
510 rs->sr_ref = referral_rewrite( ref, &e->e_name,
511 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
512 ber_bvarray_free( ref );
516 mdb_entry_return( op, e );
520 rs->sr_ref = referral_rewrite( default_referral, NULL,
521 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
524 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
525 rs->sr_err = LDAP_REFERRAL;
526 send_ldap_result( op, rs );
530 if ( !manageDSAit && is_entry_referral( e ) ) {
531 /* entry is a referral, don't allow modify */
532 rs->sr_ref = get_entry_referrals( op, e );
534 Debug( LDAP_DEBUG_TRACE,
535 LDAP_XSTRING(mdb_modify) ": entry is referral\n",
538 rs->sr_err = LDAP_REFERRAL;
539 rs->sr_matched = e->e_name.bv_val;
540 rs->sr_flags = REP_REF_MUSTBEFREED;
541 send_ldap_result( op, rs );
542 rs->sr_matched = NULL;
546 if ( get_assert( op ) &&
547 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
549 rs->sr_err = LDAP_ASSERTION_FAILED;
553 if( op->o_preread ) {
554 if( preread_ctrl == NULL ) {
555 preread_ctrl = &ctrls[num_ctrls++];
556 ctrls[num_ctrls] = NULL;
558 if ( slap_read_controls( op, rs, e,
559 &slap_pre_read_bv, preread_ctrl ) )
561 Debug( LDAP_DEBUG_TRACE,
562 "<=- " LDAP_XSTRING(mdb_modify) ": pre-read "
563 "failed!\n", 0, 0, 0 );
564 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
565 /* FIXME: is it correct to abort
566 * operation if control fails? */
572 /* Modify the entry */
574 rs->sr_err = mdb_modify_internal( op, txn, op->orm_modlist,
575 &dummy, &rs->sr_text, textbuf, textlen );
577 if( rs->sr_err != LDAP_SUCCESS ) {
578 Debug( LDAP_DEBUG_TRACE,
579 LDAP_XSTRING(mdb_modify) ": modify failed (%d)\n",
581 /* Only free attrs if they were dup'd. */
582 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
586 /* change the entry itself */
587 rs->sr_err = mdb_id2entry_update( op, txn, NULL, &dummy );
588 if ( rs->sr_err != 0 ) {
589 Debug( LDAP_DEBUG_TRACE,
590 LDAP_XSTRING(mdb_modify) ": id2entry update failed " "(%d)\n",
592 rs->sr_text = "entry update failed";
596 if( op->o_postread ) {
597 if( postread_ctrl == NULL ) {
598 postread_ctrl = &ctrls[num_ctrls++];
599 ctrls[num_ctrls] = NULL;
601 if( slap_read_controls( op, rs, &dummy,
602 &slap_post_read_bv, postread_ctrl ) )
604 Debug( LDAP_DEBUG_TRACE,
605 "<=- " LDAP_XSTRING(mdb_modify)
606 ": post-read failed!\n", 0, 0, 0 );
607 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
608 /* FIXME: is it correct to abort
609 * operation if control fails? */
615 /* Only free attrs if they were dup'd. */
616 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
617 if( moi == &opinfo ) {
618 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
619 opinfo.moi_oe.oe_key = NULL;
621 mdb_txn_abort( txn );
622 rs->sr_err = LDAP_X_NO_OPERATION;
626 rs->sr_err = mdb_txn_commit( txn );
631 if( rs->sr_err != 0 ) {
632 Debug( LDAP_DEBUG_ANY,
633 LDAP_XSTRING(mdb_modify) ": txn_%s failed: %s (%d)\n",
634 op->o_noop ? "abort (no-op)" : "commit",
635 mdb_strerror(rs->sr_err), rs->sr_err );
636 rs->sr_err = LDAP_OTHER;
637 rs->sr_text = "commit failed";
642 Debug( LDAP_DEBUG_TRACE,
643 LDAP_XSTRING(mdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
644 op->o_noop ? " (no-op)" : "",
645 dummy.e_id, op->o_req_dn.bv_val );
647 rs->sr_err = LDAP_SUCCESS;
649 if( num_ctrls ) rs->sr_ctrls = ctrls;
652 if( dummy.e_attrs ) {
653 attrs_free( dummy.e_attrs );
655 send_ldap_result( op, rs );
658 if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
659 TXN_CHECKPOINT( mdb->bi_dbenv,
660 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
665 slap_graduate_commit_csn( op );
667 if( moi == &opinfo ) {
669 mdb_txn_abort( txn );
671 if ( opinfo.moi_oe.oe_key ) {
672 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
679 mdb_entry_return( op, e );
682 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
683 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
684 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
686 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
687 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
688 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );