1 /* modify.c - bdb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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[] = {
30 int bdb_modify_internal(
33 Modifications *modlist,
42 Attribute *save_attrs;
44 int glue_attr_delete = 0;
47 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
50 if( get_manageDIT(op) ) {
51 AttributeDescription *entry = slap_schema.si_ad_entry;
52 if( !access_allowed( op, e, entry, NULL, ACL_MANAGE, NULL )) {
53 *text = "not authorized to manage entry";
54 return LDAP_INSUFFICIENT_ACCESS;
60 if ( !acl_check_modlist( op, e, modlist )) {
61 return LDAP_INSUFFICIENT_ACCESS;
64 /* save_attrs will be disposed of by bdb_cache_modify */
65 save_attrs = e->e_attrs;
66 e->e_attrs = attrs_dup( e->e_attrs );
68 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
71 switch( mod->sm_op ) {
73 case LDAP_MOD_REPLACE:
74 if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
75 value_match( &match, slap_schema.si_ad_structuralObjectClass,
76 slap_schema.si_ad_structuralObjectClass->
77 ad_type->sat_equality,
78 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
79 &mod->sm_values[0], &scbva[0], text );
80 if ( !match ) glue_attr_delete = 1;
83 if ( glue_attr_delete )
87 if ( glue_attr_delete ) {
88 Attribute **app = &e->e_attrs;
89 while ( *app != NULL ) {
90 if ( !is_at_operational( (*app)->a_desc->ad_type )) {
91 Attribute *save = *app;
92 *app = (*app)->a_next;
96 app = &(*app)->a_next;
100 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
103 switch ( mod->sm_op ) {
105 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
106 err = modify_add_values( e, mod, get_permissiveModify(op),
107 text, textbuf, textlen );
108 if( err != LDAP_SUCCESS ) {
109 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
114 case LDAP_MOD_DELETE:
115 if ( glue_attr_delete ) {
120 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
121 err = modify_delete_values( e, mod, get_permissiveModify(op),
122 text, textbuf, textlen );
123 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
124 if( err != LDAP_SUCCESS ) {
125 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
130 case LDAP_MOD_REPLACE:
131 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
132 err = modify_replace_values( e, mod, get_permissiveModify(op),
133 text, textbuf, textlen );
134 if( err != LDAP_SUCCESS ) {
135 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
140 case LDAP_MOD_INCREMENT:
141 Debug(LDAP_DEBUG_ARGS,
142 "bdb_modify_internal: increment\n", 0, 0, 0);
143 err = modify_increment_values( e, mod, get_permissiveModify(op),
144 text, textbuf, textlen );
145 if( err != LDAP_SUCCESS ) {
146 Debug(LDAP_DEBUG_ARGS,
147 "bdb_modify_internal: %d %s\n",
152 case SLAP_MOD_SOFTADD:
153 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
154 /* Avoid problems in index_add_mods()
155 * We need to add index if necessary.
157 mod->sm_op = LDAP_MOD_ADD;
159 err = modify_add_values( e, mod, get_permissiveModify(op),
160 text, textbuf, textlen );
162 mod->sm_op = SLAP_MOD_SOFTADD;
164 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
168 if( err != LDAP_SUCCESS ) {
169 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
175 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
177 *text = "Invalid modify operation";
179 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
183 if ( err != LDAP_SUCCESS ) {
184 attrs_free( e->e_attrs );
185 e->e_attrs = save_attrs;
186 /* unlock entry, delete from cache */
190 /* If objectClass was modified, reset the flags */
191 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
195 if ( glue_attr_delete ) e->e_ocflags = 0;
197 /* check if modified attribute was indexed
198 * but not in case of NOOP... */
199 err = bdb_index_is_indexed( op->o_bd, mod->sm_desc );
200 if ( err == LDAP_SUCCESS && !op->o_noop ) {
201 ap = attr_find( save_attrs, mod->sm_desc );
202 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
204 ap = attr_find( e->e_attrs, mod->sm_desc );
205 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
209 /* check that the entry still obeys the schema */
210 rc = entry_schema_check( op->o_bd, e, save_attrs, manage,
211 text, textbuf, textlen );
212 if ( rc != LDAP_SUCCESS || op->o_noop ) {
213 attrs_free( e->e_attrs );
214 /* clear the indexing flags */
215 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
218 e->e_attrs = save_attrs;
220 if ( rc != LDAP_SUCCESS ) {
221 Debug( LDAP_DEBUG_ANY,
222 "entry failed schema check: %s\n",
226 /* if NOOP then silently revert to saved attrs */
230 /* update the indices of the modified attributes */
232 /* start with deleting the old index entries */
233 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
234 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
235 rc = bdb_index_values( op, tid, ap->a_desc,
237 e->e_id, SLAP_INDEX_DELETE_OP );
238 if ( rc != LDAP_SUCCESS ) {
239 attrs_free( e->e_attrs );
240 e->e_attrs = save_attrs;
241 Debug( LDAP_DEBUG_ANY,
242 "Attribute index delete failure",
246 ap->a_flags &= ~SLAP_ATTR_IXDEL;
250 /* add the new index entries */
251 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
252 if (ap->a_flags & SLAP_ATTR_IXADD) {
253 rc = bdb_index_values( op, tid, ap->a_desc,
255 e->e_id, SLAP_INDEX_ADD_OP );
256 if ( rc != LDAP_SUCCESS ) {
257 attrs_free( e->e_attrs );
258 e->e_attrs = save_attrs;
259 Debug( LDAP_DEBUG_ANY,
260 "Attribute index add failure",
264 ap->a_flags &= ~SLAP_ATTR_IXADD;
273 bdb_modify( Operation *op, SlapReply *rs )
275 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
277 EntryInfo *ei = NULL;
278 int manageDSAit = get_manageDSAit( op );
279 char textbuf[SLAP_TEXT_BUFLEN];
280 size_t textlen = sizeof textbuf;
281 DB_TXN *ltid = NULL, *lt2;
282 struct bdb_op_info opinfo = {0};
286 u_int32_t locker = 0;
291 LDAPControl **preread_ctrl = NULL;
292 LDAPControl **postread_ctrl = NULL;
293 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
298 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
299 op->o_req_dn.bv_val, 0, 0 );
301 ctrls[num_ctrls] = NULL;
304 retry: /* transaction retry */
305 if ( dummy.e_attrs ) {
306 attrs_free( dummy.e_attrs );
307 dummy.e_attrs = NULL;
310 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
313 Debug(LDAP_DEBUG_TRACE,
314 LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
316 rs->sr_err = TXN_ABORT( ltid );
318 op->o_private = NULL;
319 op->o_do_not_cache = opinfo.boi_acl_cache;
320 if( rs->sr_err != 0 ) {
321 rs->sr_err = LDAP_OTHER;
322 rs->sr_text = "internal error";
325 if ( op->o_abandon ) {
326 rs->sr_err = SLAPD_ABANDON;
329 ldap_pvt_thread_yield();
330 bdb_trans_backoff( ++num_retries );
333 /* begin transaction */
334 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
335 bdb->bi_db_opflags );
337 if( rs->sr_err != 0 ) {
338 Debug( LDAP_DEBUG_TRACE,
339 LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
340 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
341 rs->sr_err = LDAP_OTHER;
342 rs->sr_text = "internal error";
346 locker = TXN_ID ( ltid );
348 opinfo.boi_bdb = op->o_bd;
349 opinfo.boi_txn = ltid;
350 opinfo.boi_locker = locker;
352 opinfo.boi_acl_cache = op->o_do_not_cache;
353 op->o_private = &opinfo;
355 /* get entry or ancestor */
356 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
361 if ( rs->sr_err != 0 ) {
362 Debug( LDAP_DEBUG_TRACE,
363 LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
365 switch( rs->sr_err ) {
366 case DB_LOCK_DEADLOCK:
367 case DB_LOCK_NOTGRANTED:
370 if ( BER_BVISEMPTY( &op->o_req_ndn )) {
371 struct berval gluebv = BER_BVC("glue");
372 e = ch_calloc( 1, sizeof(Entry));
373 e->e_name.bv_val = ch_strdup( "" );
374 ber_dupbv( &e->e_nname, &e->e_name );
375 attr_merge_one( e, slap_schema.si_ad_objectClass,
377 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
385 rs->sr_text = "ldap server busy";
388 rs->sr_err = LDAP_OTHER;
389 rs->sr_text = "internal error";
394 /* acquire and lock entry */
395 /* FIXME: dn2entry() should return non-glue entry */
396 if (( rs->sr_err == DB_NOTFOUND ) ||
397 ( !manageDSAit && e && is_entry_glue( e )))
400 rs->sr_matched = ch_strdup( e->e_dn );
401 rs->sr_ref = is_entry_referral( e )
402 ? get_entry_referrals( op, e )
404 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
408 rs->sr_ref = referral_rewrite( default_referral, NULL,
409 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
412 rs->sr_err = LDAP_REFERRAL;
413 send_ldap_result( op, rs );
415 if ( rs->sr_ref != default_referral ) {
416 ber_bvarray_free( rs->sr_ref );
418 free( (char *)rs->sr_matched );
420 rs->sr_matched = NULL;
425 if ( !manageDSAit && is_entry_referral( e ) ) {
426 /* entry is a referral, don't allow modify */
427 rs->sr_ref = get_entry_referrals( op, e );
429 Debug( LDAP_DEBUG_TRACE,
430 LDAP_XSTRING(bdb_modify) ": entry is referral\n",
433 rs->sr_err = LDAP_REFERRAL;
434 rs->sr_matched = e->e_name.bv_val;
435 send_ldap_result( op, rs );
437 ber_bvarray_free( rs->sr_ref );
439 rs->sr_matched = NULL;
443 if ( get_assert( op ) &&
444 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
446 rs->sr_err = LDAP_ASSERTION_FAILED;
450 if( op->o_preread ) {
451 if( preread_ctrl == NULL ) {
452 preread_ctrl = &ctrls[num_ctrls++];
453 ctrls[num_ctrls] = NULL;
455 if ( slap_read_controls( op, rs, e,
456 &slap_pre_read_bv, preread_ctrl ) )
458 Debug( LDAP_DEBUG_TRACE,
459 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read failed!\n",
465 /* nested transaction */
466 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2, bdb->bi_db_opflags );
468 if( rs->sr_err != 0 ) {
469 Debug( LDAP_DEBUG_TRACE,
470 LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
471 db_strerror(rs->sr_err), rs->sr_err, 0 );
472 rs->sr_err = LDAP_OTHER;
473 rs->sr_text = "internal error";
476 /* Modify the entry */
478 rs->sr_err = bdb_modify_internal( op, lt2, op->oq_modify.rs_modlist,
479 &dummy, &rs->sr_text, textbuf, textlen );
481 if( rs->sr_err != LDAP_SUCCESS ) {
482 Debug( LDAP_DEBUG_TRACE,
483 LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
485 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
486 rs->sr_err = opinfo.boi_err;
488 /* Only free attrs if they were dup'd. */
489 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
490 switch( rs->sr_err ) {
491 case DB_LOCK_DEADLOCK:
492 case DB_LOCK_NOTGRANTED:
498 /* change the entry itself */
499 rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
500 if ( rs->sr_err != 0 ) {
501 Debug( LDAP_DEBUG_TRACE,
502 LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
504 switch( rs->sr_err ) {
505 case DB_LOCK_DEADLOCK:
506 case DB_LOCK_NOTGRANTED:
509 rs->sr_text = "entry update failed";
513 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
514 rs->sr_err = LDAP_OTHER;
515 rs->sr_text = "txn_commit(2) failed";
519 if( op->o_postread ) {
520 if( postread_ctrl == NULL ) {
521 postread_ctrl = &ctrls[num_ctrls++];
522 ctrls[num_ctrls] = NULL;
524 if( slap_read_controls( op, rs, &dummy,
525 &slap_post_read_bv, postread_ctrl ) )
527 Debug( LDAP_DEBUG_TRACE,
528 "<=- " LDAP_XSTRING(bdb_modify)
529 ": post-read failed!\n", 0, 0, 0 );
535 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
536 rs->sr_text = "txn_abort (no-op) failed";
538 rs->sr_err = LDAP_NO_OPERATION;
542 /* may have changed in bdb_modify_internal() */
543 e->e_ocflags = dummy.e_ocflags;
548 rc = bdb_cache_modify( e, dummy.e_attrs, bdb->bi_dbenv, locker, &lock );
550 case DB_LOCK_DEADLOCK:
551 case DB_LOCK_NOTGRANTED:
555 dummy.e_attrs = NULL;
557 rs->sr_err = TXN_COMMIT( ltid, 0 );
560 op->o_private = NULL;
562 if( rs->sr_err != 0 ) {
563 Debug( LDAP_DEBUG_TRACE,
564 LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
565 op->o_noop ? "abort (no-op)" : "commit",
566 db_strerror(rs->sr_err), rs->sr_err );
567 rs->sr_err = LDAP_OTHER;
568 rs->sr_text = "commit failed";
573 Debug( LDAP_DEBUG_TRACE,
574 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
575 op->o_noop ? " (no-op)" : "",
576 dummy.e_id, op->o_req_dn.bv_val );
578 rs->sr_err = LDAP_SUCCESS;
580 if( num_ctrls ) rs->sr_ctrls = ctrls;
583 if( dummy.e_attrs ) {
584 attrs_free( dummy.e_attrs );
586 send_ldap_result( op, rs );
588 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
589 ldap_pvt_thread_yield();
590 TXN_CHECKPOINT( bdb->bi_dbenv,
591 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
597 op->o_private = NULL;
601 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
604 if( preread_ctrl != NULL ) {
605 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
606 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
608 if( postread_ctrl != NULL ) {
609 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
610 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );