1 /* modify.c - bdb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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>
26 int bdb_modify_internal(
29 Modifications *modlist,
38 Attribute *save_attrs;
40 int glue_attr_delete = 0;
43 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify_internal: 0x%08lx: %s\n",
44 e->e_id, e->e_dn, 0 );
46 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
50 if ( !acl_check_modlist( op, e, modlist )) {
51 return LDAP_INSUFFICIENT_ACCESS;
54 /* save_attrs will be disposed of by bdb_cache_modify */
55 save_attrs = e->e_attrs;
56 e->e_attrs = attrs_dup( e->e_attrs );
58 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
60 switch( mod->sm_op ) {
62 case LDAP_MOD_REPLACE:
63 if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
64 /* sc modify is internally allowed only to make an entry a glue */
68 if ( glue_attr_delete )
72 if ( glue_attr_delete ) {
73 Attribute **app = &e->e_attrs;
74 while ( *app != NULL ) {
75 if ( !is_at_operational( (*app)->a_desc->ad_type )) {
76 Attribute *save = *app;
77 *app = (*app)->a_next;
81 app = &(*app)->a_next;
85 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
88 switch ( mod->sm_op ) {
91 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify_internal: add\n", 0,0,0);
93 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
95 err = modify_add_values( e, mod, get_permissiveModify(op),
96 text, textbuf, textlen );
97 if( err != LDAP_SUCCESS ) {
99 LDAP_LOG ( OPERATION, ERR,
100 "bdb_modify_internal: %d %s\n", err, *text, 0 );
102 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
108 case LDAP_MOD_DELETE:
109 if ( glue_attr_delete )
112 LDAP_LOG ( OPERATION, DETAIL1,
113 "bdb_modify_internal: delete\n", 0, 0, 0 );
115 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
117 err = modify_delete_values( e, mod, get_permissiveModify(op),
118 text, textbuf, textlen );
119 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
120 if( err != LDAP_SUCCESS ) {
122 LDAP_LOG ( OPERATION, ERR,
123 "bdb_modify_internal: %d %s\n", err, *text, 0 );
125 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
131 case LDAP_MOD_REPLACE:
133 LDAP_LOG ( OPERATION, DETAIL1,
134 "bdb_modify_internal: replace\n", 0, 0, 0 );
136 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
138 err = modify_replace_values( e, mod, get_permissiveModify(op),
139 text, textbuf, textlen );
140 if( err != LDAP_SUCCESS ) {
142 LDAP_LOG ( OPERATION, ERR,
143 "bdb_modify_internal: %d %s\n", err, *text, 0 );
145 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
151 case LDAP_MOD_INCREMENT:
153 LDAP_LOG ( OPERATION, DETAIL1,
154 "bdb_modify_internal: increment\n", 0, 0, 0 );
156 Debug(LDAP_DEBUG_ARGS,
157 "bdb_modify_internal: increment\n", 0, 0, 0);
159 err = modify_increment_values( e, mod, get_permissiveModify(op),
160 text, textbuf, textlen );
161 if( err != LDAP_SUCCESS ) {
163 LDAP_LOG ( OPERATION, ERR,
164 "bdb_modify_internal: %d %s\n", err, *text, 0 );
166 Debug(LDAP_DEBUG_ARGS,
167 "bdb_modify_internal: %d %s\n",
173 case SLAP_MOD_SOFTADD:
175 LDAP_LOG ( OPERATION, DETAIL1,
176 "bdb_modify_internal: softadd\n",0,0,0 );
178 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
180 /* Avoid problems in index_add_mods()
181 * We need to add index if necessary.
183 mod->sm_op = LDAP_MOD_ADD;
185 err = modify_add_values( e, mod, get_permissiveModify(op),
186 text, textbuf, textlen );
188 mod->sm_op = SLAP_MOD_SOFTADD;
190 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
194 if( err != LDAP_SUCCESS ) {
196 LDAP_LOG ( OPERATION, ERR,
197 "bdb_modify_internal: %d %s\n", err, *text, 0 );
199 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
207 LDAP_LOG ( OPERATION, ERR,
208 "bdb_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
210 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
213 *text = "Invalid modify operation";
216 LDAP_LOG ( OPERATION, ERR,
217 "bdb_modify_internal: %d %s\n", err, *text, 0 );
219 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
224 if ( err != LDAP_SUCCESS ) {
225 attrs_free( e->e_attrs );
226 e->e_attrs = save_attrs;
227 /* unlock entry, delete from cache */
231 /* If objectClass was modified, reset the flags */
232 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
236 if ( glue_attr_delete ) {
240 /* check if modified attribute was indexed
241 * but not in case of NOOP... */
242 err = bdb_index_is_indexed( op->o_bd, mod->sm_desc );
243 if ( err == LDAP_SUCCESS && !op->o_noop ) {
244 ap = attr_find( save_attrs, mod->sm_desc );
245 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
247 ap = attr_find( e->e_attrs, mod->sm_desc );
248 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
252 /* check that the entry still obeys the schema */
253 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
254 if ( rc != LDAP_SUCCESS || op->o_noop ) {
255 attrs_free( e->e_attrs );
256 e->e_attrs = save_attrs;
258 if ( rc != LDAP_SUCCESS ) {
260 LDAP_LOG ( OPERATION, ERR, "bdb_modify_internal: "
261 "entry failed schema check %s\n",
264 Debug( LDAP_DEBUG_ANY,
265 "entry failed schema check: %s\n",
270 /* if NOOP then silently revert to saved attrs */
274 /* update the indices of the modified attributes */
276 /* start with deleting the old index entries */
277 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
278 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
279 rc = bdb_index_values( op, tid, ap->a_desc,
281 e->e_id, SLAP_INDEX_DELETE_OP );
282 if ( rc != LDAP_SUCCESS ) {
283 attrs_free( e->e_attrs );
284 e->e_attrs = save_attrs;
286 LDAP_LOG ( OPERATION, ERR,
287 "bdb_modify_internal: attribute index delete failure\n",
290 Debug( LDAP_DEBUG_ANY,
291 "Attribute index delete failure",
296 ap->a_flags &= ~SLAP_ATTR_IXDEL;
300 /* add the new index entries */
301 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
302 if (ap->a_flags & SLAP_ATTR_IXADD) {
303 rc = bdb_index_values( op, tid, ap->a_desc,
305 e->e_id, SLAP_INDEX_ADD_OP );
306 if ( rc != LDAP_SUCCESS ) {
307 attrs_free( e->e_attrs );
308 e->e_attrs = save_attrs;
310 LDAP_LOG ( OPERATION, ERR,
311 "bdb_modify_internal: attribute index add failure\n",
314 Debug( LDAP_DEBUG_ANY,
315 "Attribute index add failure",
320 ap->a_flags &= ~SLAP_ATTR_IXADD;
329 bdb_modify( Operation *op, SlapReply *rs )
331 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
333 EntryInfo *ei = NULL;
334 int manageDSAit = get_manageDSAit( op );
335 char textbuf[SLAP_TEXT_BUFLEN];
336 size_t textlen = sizeof textbuf;
337 DB_TXN *ltid = NULL, *lt2;
338 struct bdb_op_info opinfo;
341 u_int32_t locker = 0;
346 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
350 struct psid_entry *pm_list, *pm_prev;
352 EntryInfo *suffix_ei;
354 int ctxcsn_added = 0;
357 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", op->o_req_dn.bv_val, 0, 0 );
359 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", op->o_req_dn.bv_val, 0, 0 );
363 retry: /* transaction retry */
365 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
369 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
371 Debug(LDAP_DEBUG_TRACE,
372 "bdb_modify: retrying...\n", 0, 0, 0);
375 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
376 while ( pm_list != NULL ) {
377 LDAP_LIST_REMOVE ( pm_list, ps_link );
379 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
383 rs->sr_err = TXN_ABORT( ltid );
385 op->o_private = NULL;
386 op->o_do_not_cache = opinfo.boi_acl_cache;
387 if( rs->sr_err != 0 ) {
388 rs->sr_err = LDAP_OTHER;
389 rs->sr_text = "internal error";
392 ldap_pvt_thread_yield();
393 bdb_trans_backoff( ++num_retries );
396 /* begin transaction */
397 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
398 bdb->bi_db_opflags );
400 if( rs->sr_err != 0 ) {
402 LDAP_LOG ( OPERATION, DETAIL1,
403 "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
405 Debug( LDAP_DEBUG_TRACE,
406 "bdb_modify: txn_begin failed: %s (%d)\n",
407 db_strerror(rs->sr_err), rs->sr_err, 0 );
409 rs->sr_err = LDAP_OTHER;
410 rs->sr_text = "internal error";
414 locker = TXN_ID ( ltid );
416 opinfo.boi_bdb = op->o_bd;
417 opinfo.boi_txn = ltid;
418 opinfo.boi_locker = locker;
420 opinfo.boi_acl_cache = op->o_do_not_cache;
421 op->o_private = &opinfo;
423 /* get entry or ancestor */
424 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
427 if ( rs->sr_err != 0 ) {
429 LDAP_LOG ( OPERATION, DETAIL1,
430 "bdb_modify: dn2entry failed: (%d)\n", rs->sr_err, 0, 0 );
432 Debug( LDAP_DEBUG_TRACE,
433 "bdb_modify: dn2entry failed (%d)\n",
436 switch( rs->sr_err ) {
437 case DB_LOCK_DEADLOCK:
438 case DB_LOCK_NOTGRANTED:
443 rs->sr_text = "ldap server busy";
446 rs->sr_err = LDAP_OTHER;
447 rs->sr_text = "internal error";
453 /* acquire and lock entry */
454 /* FIXME: dn2entry() should return non-glue entry */
455 if (( rs->sr_err == DB_NOTFOUND ) || ( !manageDSAit && e && is_entry_glue( e ))) {
457 rs->sr_matched = ch_strdup( e->e_dn );
458 rs->sr_ref = is_entry_referral( e )
459 ? get_entry_referrals( op, e )
461 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
465 BerVarray deref = NULL;
466 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
468 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
470 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
471 ber_bvarray_add( &deref, &tmpbv );
474 deref = default_referral;
476 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
477 LDAP_SCOPE_DEFAULT );
480 rs->sr_err = LDAP_REFERRAL;
481 send_ldap_result( op, rs );
483 if ( rs->sr_ref != default_referral ) {
484 ber_bvarray_free( rs->sr_ref );
486 free( (char *)rs->sr_matched );
488 rs->sr_matched = NULL;
493 if ( !manageDSAit && is_entry_referral( e ) ) {
494 /* entry is a referral, don't allow modify */
495 rs->sr_ref = get_entry_referrals( op, e );
498 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: entry is referral\n", 0, 0, 0 );
500 Debug( LDAP_DEBUG_TRACE,
501 "bdb_modify: entry is referral\n",
505 rs->sr_err = LDAP_REFERRAL;
506 rs->sr_matched = e->e_name.bv_val;
507 send_ldap_result( op, rs );
509 ber_bvarray_free( rs->sr_ref );
511 rs->sr_matched = NULL;
515 if ( get_assert( op ) &&
516 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
518 rs->sr_err = LDAP_ASSERTION_FAILED;
522 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop && !op->o_no_psearch ) {
523 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
524 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
525 bdb_psearch(op, rs, ps_list, e, LDAP_PSEARCH_BY_PREMODIFY );
527 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
530 if( op->o_preread ) {
531 if ( slap_read_controls( op, rs, e,
532 &slap_pre_read_bv, &ctrls[num_ctrls] ) )
535 LDAP_LOG ( OPERATION, DETAIL1,
536 "<=- bdb_modify: pre-read failed!\n", 0, 0, 0 );
538 Debug( LDAP_DEBUG_TRACE,
539 "<=- bdb_modify: pre-read failed!\n", 0, 0, 0 );
543 ctrls[++num_ctrls] = NULL;
544 op->o_preread = 0; /* prevent redo on retry */
547 /* nested transaction */
548 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
549 bdb->bi_db_opflags );
551 if( rs->sr_err != 0 ) {
553 LDAP_LOG ( OPERATION, ERR,
554 "bdb_modify: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
556 Debug( LDAP_DEBUG_TRACE,
557 "bdb_modify: txn_begin(2) failed: %s (%d)\n",
558 db_strerror(rs->sr_err), rs->sr_err, 0 );
560 rs->sr_err = LDAP_OTHER;
561 rs->sr_text = "internal error";
564 /* Modify the entry */
566 rs->sr_err = bdb_modify_internal( op, lt2, op->oq_modify.rs_modlist,
567 &dummy, &rs->sr_text, textbuf, textlen );
569 if( rs->sr_err != LDAP_SUCCESS ) {
571 LDAP_LOG ( OPERATION, ERR,
572 "bdb_modify: modify failed (%d)\n", rs->sr_err, 0, 0 );
574 Debug( LDAP_DEBUG_TRACE,
575 "bdb_modify: modify failed (%d)\n",
578 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
579 rs->sr_err = opinfo.boi_err;
581 switch( rs->sr_err ) {
582 case DB_LOCK_DEADLOCK:
583 case DB_LOCK_NOTGRANTED:
589 if( op->o_postread ) {
590 if( slap_read_controls( op, rs, e,
591 &slap_post_read_bv, &ctrls[num_ctrls] ) )
594 LDAP_LOG ( OPERATION, DETAIL1,
595 "<=- bdb_modify: post-read failed!\n", 0, 0, 0 );
597 Debug( LDAP_DEBUG_TRACE,
598 "<=- bdb_modify: post-read failed!\n", 0, 0, 0 );
602 ctrls[++num_ctrls] = NULL;
603 op->o_postread = 0; /* prevent redo on retry */
604 /* FIXME: should read entry on the last retry */
607 /* change the entry itself */
608 rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
609 if ( rs->sr_err != 0 ) {
611 LDAP_LOG ( OPERATION, ERR,
612 "bdb_modify: id2entry update failed (%d)\n", rs->sr_err, 0, 0 );
614 Debug( LDAP_DEBUG_TRACE,
615 "bdb_modify: id2entry update failed (%d)\n",
618 switch( rs->sr_err ) {
619 case DB_LOCK_DEADLOCK:
620 case DB_LOCK_NOTGRANTED:
623 rs->sr_text = "entry update failed";
627 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
628 rs->sr_err = LDAP_OTHER;
629 rs->sr_text = "txn_commit(2) failed";
633 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
634 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
635 &ctxcsn_e, &ctxcsn_added, locker );
645 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
646 rs->sr_text = "txn_abort (no-op) failed";
648 rs->sr_err = LDAP_NO_OPERATION;
652 bdb_cache_modify( e, dummy.e_attrs, bdb->bi_dbenv, locker, &lock );
654 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
655 if ( ctxcsn_added ) {
656 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
657 (struct berval *)&slap_ldapsync_cn_bv, locker );
661 if ( rs->sr_err == LDAP_SUCCESS ) {
662 /* Loop through in-scope entries for each psearch spec */
663 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
664 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
665 bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_MODIFY );
667 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
668 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
669 while ( pm_list != NULL ) {
670 bdb_psearch(op, rs, pm_list->ps_op,
671 e, LDAP_PSEARCH_BY_SCOPEOUT);
672 LDAP_LIST_REMOVE ( pm_list, ps_link );
674 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
679 rs->sr_err = TXN_COMMIT( ltid, 0 );
682 op->o_private = NULL;
684 if( rs->sr_err != 0 ) {
686 LDAP_LOG ( OPERATION, ERR,
687 "bdb_modify: txn_%s failed %s (%d)\n",
688 op->o_noop ? "abort (no_op)" : "commit",
689 db_strerror(rs->sr_err), rs->sr_err );
691 Debug( LDAP_DEBUG_TRACE,
692 "bdb_modify: txn_%s failed: %s (%d)\n",
693 op->o_noop ? "abort (no-op)" : "commit",
694 db_strerror(rs->sr_err), rs->sr_err );
696 rs->sr_err = LDAP_OTHER;
697 rs->sr_text = "commit failed";
703 LDAP_LOG ( OPERATION, DETAIL1,
704 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
705 op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
707 Debug( LDAP_DEBUG_TRACE,
708 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
709 op->o_noop ? " (no-op)" : "",
713 rs->sr_err = LDAP_SUCCESS;
715 if( num_ctrls ) rs->sr_ctrls = ctrls;
718 send_ldap_result( op, rs );
720 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
721 ldap_pvt_thread_yield();
722 TXN_CHECKPOINT( bdb->bi_dbenv,
723 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
728 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
729 while ( pm_list != NULL ) {
730 LDAP_LIST_REMOVE ( pm_list, ps_link );
732 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
736 op->o_private = NULL;
740 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);