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;
46 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
49 if ( !acl_check_modlist( op, e, modlist )) {
50 return LDAP_INSUFFICIENT_ACCESS;
53 /* save_attrs will be disposed of by bdb_cache_modify */
54 save_attrs = e->e_attrs;
55 e->e_attrs = attrs_dup( e->e_attrs );
57 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 value_match( &match, slap_schema.si_ad_structuralObjectClass,
65 slap_schema.si_ad_structuralObjectClass->
66 ad_type->sat_equality,
67 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
68 &mod->sm_values[0], &scbva[0], text );
69 if ( !match ) glue_attr_delete = 1;
72 if ( glue_attr_delete )
76 if ( glue_attr_delete ) {
77 Attribute **app = &e->e_attrs;
78 while ( *app != NULL ) {
79 if ( !is_at_operational( (*app)->a_desc->ad_type )) {
80 Attribute *save = *app;
81 *app = (*app)->a_next;
85 app = &(*app)->a_next;
89 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
92 switch ( mod->sm_op ) {
94 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 ) {
98 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
103 case LDAP_MOD_DELETE:
104 if ( glue_attr_delete ) {
109 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
110 err = modify_delete_values( e, mod, get_permissiveModify(op),
111 text, textbuf, textlen );
112 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
113 if( err != LDAP_SUCCESS ) {
114 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
119 case LDAP_MOD_REPLACE:
120 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
121 err = modify_replace_values( e, mod, get_permissiveModify(op),
122 text, textbuf, textlen );
123 if( err != LDAP_SUCCESS ) {
124 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
129 case LDAP_MOD_INCREMENT:
130 Debug(LDAP_DEBUG_ARGS,
131 "bdb_modify_internal: increment\n", 0, 0, 0);
132 err = modify_increment_values( e, mod, get_permissiveModify(op),
133 text, textbuf, textlen );
134 if( err != LDAP_SUCCESS ) {
135 Debug(LDAP_DEBUG_ARGS,
136 "bdb_modify_internal: %d %s\n",
141 case SLAP_MOD_SOFTADD:
142 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
143 /* Avoid problems in index_add_mods()
144 * We need to add index if necessary.
146 mod->sm_op = LDAP_MOD_ADD;
148 err = modify_add_values( e, mod, get_permissiveModify(op),
149 text, textbuf, textlen );
151 mod->sm_op = SLAP_MOD_SOFTADD;
153 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
157 if( err != LDAP_SUCCESS ) {
158 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
164 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
166 *text = "Invalid modify operation";
168 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
172 if ( err != LDAP_SUCCESS ) {
173 attrs_free( e->e_attrs );
174 e->e_attrs = save_attrs;
175 /* unlock entry, delete from cache */
179 /* If objectClass was modified, reset the flags */
180 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
184 if ( glue_attr_delete ) e->e_ocflags = 0;
186 /* check if modified attribute was indexed
187 * but not in case of NOOP... */
188 err = bdb_index_is_indexed( op->o_bd, mod->sm_desc );
189 if ( err == LDAP_SUCCESS && !op->o_noop ) {
190 ap = attr_find( save_attrs, mod->sm_desc );
191 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
193 ap = attr_find( e->e_attrs, mod->sm_desc );
194 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
198 /* check that the entry still obeys the schema */
199 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
200 if ( rc != LDAP_SUCCESS || op->o_noop ) {
201 attrs_free( e->e_attrs );
202 /* clear the indexing flags */
203 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
206 e->e_attrs = save_attrs;
208 if ( rc != LDAP_SUCCESS ) {
209 Debug( LDAP_DEBUG_ANY,
210 "entry failed schema check: %s\n",
214 /* if NOOP then silently revert to saved attrs */
218 /* update the indices of the modified attributes */
220 /* start with deleting the old index entries */
221 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
222 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
223 rc = bdb_index_values( op, tid, ap->a_desc,
225 e->e_id, SLAP_INDEX_DELETE_OP );
226 if ( rc != LDAP_SUCCESS ) {
227 attrs_free( e->e_attrs );
228 e->e_attrs = save_attrs;
229 Debug( LDAP_DEBUG_ANY,
230 "Attribute index delete failure",
234 ap->a_flags &= ~SLAP_ATTR_IXDEL;
238 /* add the new index entries */
239 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
240 if (ap->a_flags & SLAP_ATTR_IXADD) {
241 rc = bdb_index_values( op, tid, ap->a_desc,
243 e->e_id, SLAP_INDEX_ADD_OP );
244 if ( rc != LDAP_SUCCESS ) {
245 attrs_free( e->e_attrs );
246 e->e_attrs = save_attrs;
247 Debug( LDAP_DEBUG_ANY,
248 "Attribute index add failure",
252 ap->a_flags &= ~SLAP_ATTR_IXADD;
261 bdb_modify( Operation *op, SlapReply *rs )
263 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
265 EntryInfo *ei = NULL;
266 int manageDSAit = get_manageDSAit( op );
267 char textbuf[SLAP_TEXT_BUFLEN];
268 size_t textlen = sizeof textbuf;
269 DB_TXN *ltid = NULL, *lt2;
270 struct bdb_op_info opinfo = {0};
274 u_int32_t locker = 0;
279 LDAPControl **preread_ctrl = NULL;
280 LDAPControl **postread_ctrl = NULL;
281 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
286 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
287 op->o_req_dn.bv_val, 0, 0 );
289 ctrls[num_ctrls] = NULL;
292 retry: /* transaction retry */
293 if ( dummy.e_attrs ) {
294 attrs_free( dummy.e_attrs );
295 dummy.e_attrs = NULL;
298 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
301 Debug(LDAP_DEBUG_TRACE,
302 LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
304 rs->sr_err = TXN_ABORT( ltid );
306 op->o_private = NULL;
307 op->o_do_not_cache = opinfo.boi_acl_cache;
308 if( rs->sr_err != 0 ) {
309 rs->sr_err = LDAP_OTHER;
310 rs->sr_text = "internal error";
313 ldap_pvt_thread_yield();
314 bdb_trans_backoff( ++num_retries );
317 /* begin transaction */
318 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
319 bdb->bi_db_opflags );
321 if( rs->sr_err != 0 ) {
322 Debug( LDAP_DEBUG_TRACE,
323 LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
324 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
325 rs->sr_err = LDAP_OTHER;
326 rs->sr_text = "internal error";
330 locker = TXN_ID ( ltid );
332 opinfo.boi_bdb = op->o_bd;
333 opinfo.boi_txn = ltid;
334 opinfo.boi_locker = locker;
336 opinfo.boi_acl_cache = op->o_do_not_cache;
337 op->o_private = &opinfo;
339 /* get entry or ancestor */
340 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
345 if ( rs->sr_err != 0 ) {
346 Debug( LDAP_DEBUG_TRACE,
347 LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
349 switch( rs->sr_err ) {
350 case DB_LOCK_DEADLOCK:
351 case DB_LOCK_NOTGRANTED:
354 if ( BER_BVISEMPTY( &op->o_req_ndn )) {
355 struct berval gluebv = BER_BVC("glue");
356 e = ch_calloc( 1, sizeof(Entry));
357 e->e_name.bv_val = ch_strdup( "" );
358 ber_dupbv( &e->e_nname, &e->e_name );
359 attr_merge_one( e, slap_schema.si_ad_objectClass,
361 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
369 rs->sr_text = "ldap server busy";
372 rs->sr_err = LDAP_OTHER;
373 rs->sr_text = "internal error";
378 /* acquire and lock entry */
379 /* FIXME: dn2entry() should return non-glue entry */
380 if (( rs->sr_err == DB_NOTFOUND ) ||
381 ( !manageDSAit && e && is_entry_glue( e )))
384 rs->sr_matched = ch_strdup( e->e_dn );
385 rs->sr_ref = is_entry_referral( e )
386 ? get_entry_referrals( op, e )
388 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
392 rs->sr_ref = referral_rewrite( default_referral, NULL,
393 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
396 rs->sr_err = LDAP_REFERRAL;
397 send_ldap_result( op, rs );
399 if ( rs->sr_ref != default_referral ) {
400 ber_bvarray_free( rs->sr_ref );
402 free( (char *)rs->sr_matched );
404 rs->sr_matched = NULL;
409 if ( !manageDSAit && is_entry_referral( e ) ) {
410 /* entry is a referral, don't allow modify */
411 rs->sr_ref = get_entry_referrals( op, e );
413 Debug( LDAP_DEBUG_TRACE,
414 LDAP_XSTRING(bdb_modify) ": entry is referral\n",
417 rs->sr_err = LDAP_REFERRAL;
418 rs->sr_matched = e->e_name.bv_val;
419 send_ldap_result( op, rs );
421 ber_bvarray_free( rs->sr_ref );
423 rs->sr_matched = NULL;
427 if ( get_assert( op ) &&
428 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
430 rs->sr_err = LDAP_ASSERTION_FAILED;
434 if( op->o_preread ) {
435 if( preread_ctrl == NULL ) {
436 preread_ctrl = &ctrls[num_ctrls++];
437 ctrls[num_ctrls] = NULL;
439 if ( slap_read_controls( op, rs, e,
440 &slap_pre_read_bv, preread_ctrl ) )
442 Debug( LDAP_DEBUG_TRACE,
443 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read failed!\n",
449 /* nested transaction */
450 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2, bdb->bi_db_opflags );
452 if( rs->sr_err != 0 ) {
453 Debug( LDAP_DEBUG_TRACE,
454 LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
455 db_strerror(rs->sr_err), rs->sr_err, 0 );
456 rs->sr_err = LDAP_OTHER;
457 rs->sr_text = "internal error";
460 /* Modify the entry */
462 rs->sr_err = bdb_modify_internal( op, lt2, op->oq_modify.rs_modlist,
463 &dummy, &rs->sr_text, textbuf, textlen );
465 if( rs->sr_err != LDAP_SUCCESS ) {
466 Debug( LDAP_DEBUG_TRACE,
467 LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
469 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
470 rs->sr_err = opinfo.boi_err;
472 /* Only free attrs if they were dup'd. */
473 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
474 switch( rs->sr_err ) {
475 case DB_LOCK_DEADLOCK:
476 case DB_LOCK_NOTGRANTED:
482 /* change the entry itself */
483 rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
484 if ( rs->sr_err != 0 ) {
485 Debug( LDAP_DEBUG_TRACE,
486 LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
488 switch( rs->sr_err ) {
489 case DB_LOCK_DEADLOCK:
490 case DB_LOCK_NOTGRANTED:
493 rs->sr_text = "entry update failed";
497 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
498 rs->sr_err = LDAP_OTHER;
499 rs->sr_text = "txn_commit(2) failed";
503 if( op->o_postread ) {
504 if( postread_ctrl == NULL ) {
505 postread_ctrl = &ctrls[num_ctrls++];
506 ctrls[num_ctrls] = NULL;
508 if( slap_read_controls( op, rs, &dummy,
509 &slap_post_read_bv, postread_ctrl ) )
511 Debug( LDAP_DEBUG_TRACE,
512 "<=- " LDAP_XSTRING(bdb_modify)
513 ": post-read failed!\n", 0, 0, 0 );
519 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
520 rs->sr_text = "txn_abort (no-op) failed";
522 rs->sr_err = LDAP_NO_OPERATION;
526 /* may have changed in bdb_modify_internal() */
527 e->e_ocflags = dummy.e_ocflags;
532 rc = bdb_cache_modify( e, dummy.e_attrs, bdb->bi_dbenv, locker, &lock );
534 case DB_LOCK_DEADLOCK:
535 case DB_LOCK_NOTGRANTED:
539 dummy.e_attrs = NULL;
541 rs->sr_err = TXN_COMMIT( ltid, 0 );
544 op->o_private = NULL;
546 if( rs->sr_err != 0 ) {
547 Debug( LDAP_DEBUG_TRACE,
548 LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
549 op->o_noop ? "abort (no-op)" : "commit",
550 db_strerror(rs->sr_err), rs->sr_err );
551 rs->sr_err = LDAP_OTHER;
552 rs->sr_text = "commit failed";
557 Debug( LDAP_DEBUG_TRACE,
558 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
559 op->o_noop ? " (no-op)" : "",
560 dummy.e_id, op->o_req_dn.bv_val );
562 rs->sr_err = LDAP_SUCCESS;
564 if( num_ctrls ) rs->sr_ctrls = ctrls;
567 if( dummy.e_attrs ) {
568 attrs_free( dummy.e_attrs );
570 send_ldap_result( op, rs );
572 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
573 ldap_pvt_thread_yield();
574 TXN_CHECKPOINT( bdb->bi_dbenv,
575 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
581 op->o_private = NULL;
585 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
588 if( preread_ctrl != NULL ) {
589 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
590 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
592 if( postread_ctrl != NULL ) {
593 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
594 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );