1 /* modify.c - mdb 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 = 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 /* Don't touch the opattrs, if this is a contextCSN update
461 * initiated from updatedn */
462 if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
463 op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
465 slap_mods_opattrs( op, &op->orm_modlist, 1 );
468 /* begin transaction */
469 rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
471 if( rs->sr_err != 0 ) {
472 Debug( LDAP_DEBUG_TRACE,
473 LDAP_XSTRING(mdb_modify) ": txn_begin failed: "
474 "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
475 rs->sr_err = LDAP_OTHER;
476 rs->sr_text = "internal error";
482 /* get entry or ancestor */
483 rs->sr_err = mdb_dn2entry( op, txn, &op->o_req_ndn, &e, 1 );
485 if ( rs->sr_err != 0 ) {
486 Debug( LDAP_DEBUG_TRACE,
487 LDAP_XSTRING(mdb_modify) ": dn2entry failed (%d)\n",
489 switch( rs->sr_err ) {
493 rs->sr_text = "ldap server busy";
496 rs->sr_err = LDAP_OTHER;
497 rs->sr_text = "internal error";
502 /* acquire and lock entry */
503 /* FIXME: dn2entry() should return non-glue entry */
504 if (( rs->sr_err == MDB_NOTFOUND ) ||
505 ( !manageDSAit && e && is_entry_glue( e )))
508 rs->sr_matched = ch_strdup( e->e_dn );
509 if ( is_entry_referral( e )) {
510 BerVarray ref = get_entry_referrals( op, e );
511 rs->sr_ref = referral_rewrite( ref, &e->e_name,
512 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
513 ber_bvarray_free( ref );
517 mdb_entry_return( op, e );
521 rs->sr_ref = referral_rewrite( default_referral, NULL,
522 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
525 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
526 rs->sr_err = LDAP_REFERRAL;
527 send_ldap_result( op, rs );
531 if ( !manageDSAit && is_entry_referral( e ) ) {
532 /* entry is a referral, don't allow modify */
533 rs->sr_ref = get_entry_referrals( op, e );
535 Debug( LDAP_DEBUG_TRACE,
536 LDAP_XSTRING(mdb_modify) ": entry is referral\n",
539 rs->sr_err = LDAP_REFERRAL;
540 rs->sr_matched = e->e_name.bv_val;
541 rs->sr_flags = REP_REF_MUSTBEFREED;
542 send_ldap_result( op, rs );
543 rs->sr_matched = NULL;
547 if ( get_assert( op ) &&
548 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
550 rs->sr_err = LDAP_ASSERTION_FAILED;
554 if( op->o_preread ) {
555 if( preread_ctrl == NULL ) {
556 preread_ctrl = &ctrls[num_ctrls++];
557 ctrls[num_ctrls] = NULL;
559 if ( slap_read_controls( op, rs, e,
560 &slap_pre_read_bv, preread_ctrl ) )
562 Debug( LDAP_DEBUG_TRACE,
563 "<=- " LDAP_XSTRING(mdb_modify) ": pre-read "
564 "failed!\n", 0, 0, 0 );
565 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
566 /* FIXME: is it correct to abort
567 * operation if control fails? */
573 /* Modify the entry */
575 rs->sr_err = mdb_modify_internal( op, txn, op->orm_modlist,
576 &dummy, &rs->sr_text, textbuf, textlen );
578 if( rs->sr_err != LDAP_SUCCESS ) {
579 Debug( LDAP_DEBUG_TRACE,
580 LDAP_XSTRING(mdb_modify) ": modify failed (%d)\n",
582 /* Only free attrs if they were dup'd. */
583 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
587 /* change the entry itself */
588 rs->sr_err = mdb_id2entry_update( op, txn, &dummy );
589 if ( rs->sr_err != 0 ) {
590 Debug( LDAP_DEBUG_TRACE,
591 LDAP_XSTRING(mdb_modify) ": id2entry update failed " "(%d)\n",
593 rs->sr_text = "entry update failed";
597 if( op->o_postread ) {
598 if( postread_ctrl == NULL ) {
599 postread_ctrl = &ctrls[num_ctrls++];
600 ctrls[num_ctrls] = NULL;
602 if( slap_read_controls( op, rs, &dummy,
603 &slap_post_read_bv, postread_ctrl ) )
605 Debug( LDAP_DEBUG_TRACE,
606 "<=- " LDAP_XSTRING(mdb_modify)
607 ": post-read failed!\n", 0, 0, 0 );
608 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
609 /* FIXME: is it correct to abort
610 * operation if control fails? */
616 /* Only free attrs if they were dup'd. */
617 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
618 if( moi == &opinfo ) {
619 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
620 opinfo.moi_oe.oe_key = NULL;
622 mdb_txn_abort( txn );
623 rs->sr_err = LDAP_X_NO_OPERATION;
627 rs->sr_err = mdb_txn_commit( txn );
632 if( rs->sr_err != 0 ) {
633 Debug( LDAP_DEBUG_TRACE,
634 LDAP_XSTRING(mdb_modify) ": txn_%s failed: %s (%d)\n",
635 op->o_noop ? "abort (no-op)" : "commit",
636 mdb_strerror(rs->sr_err), rs->sr_err );
637 rs->sr_err = LDAP_OTHER;
638 rs->sr_text = "commit failed";
643 Debug( LDAP_DEBUG_TRACE,
644 LDAP_XSTRING(mdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
645 op->o_noop ? " (no-op)" : "",
646 dummy.e_id, op->o_req_dn.bv_val );
648 rs->sr_err = LDAP_SUCCESS;
650 if( num_ctrls ) rs->sr_ctrls = ctrls;
653 if( dummy.e_attrs ) {
654 attrs_free( dummy.e_attrs );
656 send_ldap_result( op, rs );
659 if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
660 TXN_CHECKPOINT( mdb->bi_dbenv,
661 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
666 slap_graduate_commit_csn( op );
668 if( moi == &opinfo ) {
670 mdb_txn_abort( txn );
672 if ( opinfo.moi_oe.oe_key ) {
673 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
678 mdb_entry_return( op, e );
681 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
682 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
683 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
685 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
686 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
687 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );