1 /* modify.c - bdb backend modify routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 int bdb_modify_internal(
20 Modifications *modlist,
29 Attribute *save_attrs;
33 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify_internal: 0x%08lx: %s\n",
34 e->e_id, e->e_dn, 0 );
36 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
40 if ( !acl_check_modlist( op, e, modlist )) {
41 return LDAP_INSUFFICIENT_ACCESS;
44 /* save_attrs will be disposed of by bdb_cache_modify */
45 save_attrs = e->e_attrs;
46 e->e_attrs = attrs_dup( e->e_attrs );
48 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
51 switch ( mod->sm_op ) {
54 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify_internal: add\n", 0,0,0);
56 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
58 err = modify_add_values( e, mod, get_permissiveModify(op),
59 text, textbuf, textlen );
60 if( err != LDAP_SUCCESS ) {
62 LDAP_LOG ( OPERATION, ERR,
63 "bdb_modify_internal: %d %s\n", err, *text, 0 );
65 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
73 LDAP_LOG ( OPERATION, DETAIL1,
74 "bdb_modify_internal: delete\n", 0, 0, 0 );
76 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
78 err = modify_delete_values( e, mod, get_permissiveModify(op),
79 text, textbuf, textlen );
80 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
81 if( err != LDAP_SUCCESS ) {
83 LDAP_LOG ( OPERATION, ERR,
84 "bdb_modify_internal: %d %s\n", err, *text, 0 );
86 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
92 case LDAP_MOD_REPLACE:
94 LDAP_LOG ( OPERATION, DETAIL1,
95 "bdb_modify_internal: replace\n", 0, 0, 0 );
97 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
99 err = modify_replace_values( e, mod, get_permissiveModify(op),
100 text, textbuf, textlen );
101 if( err != LDAP_SUCCESS ) {
103 LDAP_LOG ( OPERATION, ERR,
104 "bdb_modify_internal: %d %s\n", err, *text, 0 );
106 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
112 case SLAP_MOD_SOFTADD:
114 LDAP_LOG ( OPERATION, DETAIL1,
115 "bdb_modify_internal: softadd\n",0,0,0 );
117 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
119 /* Avoid problems in index_add_mods()
120 * We need to add index if necessary.
122 mod->sm_op = LDAP_MOD_ADD;
124 err = modify_add_values( e, mod, get_permissiveModify(op),
125 text, textbuf, textlen );
127 mod->sm_op = SLAP_MOD_SOFTADD;
129 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
133 if( err != LDAP_SUCCESS ) {
135 LDAP_LOG ( OPERATION, ERR,
136 "bdb_modify_internal: %d %s\n", err, *text, 0 );
138 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
146 LDAP_LOG ( OPERATION, ERR,
147 "bdb_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
149 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
152 *text = "Invalid modify operation";
155 LDAP_LOG ( OPERATION, ERR,
156 "bdb_modify_internal: %d %s\n", err, *text, 0 );
158 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
163 if ( err != LDAP_SUCCESS ) {
164 attrs_free( e->e_attrs );
165 e->e_attrs = save_attrs;
166 /* unlock entry, delete from cache */
170 /* If objectClass was modified, reset the flags */
171 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
175 /* check if modified attribute was indexed
176 * but not in case of NOOP... */
177 err = bdb_index_is_indexed( op->o_bd, mod->sm_desc );
178 if ( err == LDAP_SUCCESS && !op->o_noop ) {
179 ap = attr_find( save_attrs, mod->sm_desc );
180 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
182 ap = attr_find( e->e_attrs, mod->sm_desc );
183 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
187 /* check that the entry still obeys the schema */
188 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
189 if ( rc != LDAP_SUCCESS || op->o_noop ) {
190 attrs_free( e->e_attrs );
191 e->e_attrs = save_attrs;
193 if ( rc != LDAP_SUCCESS ) {
195 LDAP_LOG ( OPERATION, ERR, "bdb_modify_internal: "
196 "entry failed schema check %s\n",
199 Debug( LDAP_DEBUG_ANY,
200 "entry failed schema check: %s\n",
205 /* if NOOP then silently revert to saved attrs */
209 /* update the indices of the modified attributes */
211 /* start with deleting the old index entries */
212 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
213 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
214 rc = bdb_index_values( op, tid, ap->a_desc,
216 e->e_id, SLAP_INDEX_DELETE_OP );
217 if ( rc != LDAP_SUCCESS ) {
218 attrs_free( e->e_attrs );
219 e->e_attrs = save_attrs;
221 LDAP_LOG ( OPERATION, ERR,
222 "bdb_modify_internal: attribute index delete failure\n",
225 Debug( LDAP_DEBUG_ANY,
226 "Attribute index delete failure",
231 ap->a_flags &= ~SLAP_ATTR_IXDEL;
235 /* add the new index entries */
236 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
237 if (ap->a_flags & SLAP_ATTR_IXADD) {
238 rc = bdb_index_values( op, tid, ap->a_desc,
240 e->e_id, SLAP_INDEX_ADD_OP );
241 if ( rc != LDAP_SUCCESS ) {
242 attrs_free( e->e_attrs );
243 e->e_attrs = save_attrs;
245 LDAP_LOG ( OPERATION, ERR,
246 "bdb_modify_internal: attribute index add failure\n",
249 Debug( LDAP_DEBUG_ANY,
250 "Attribute index add failure",
255 ap->a_flags &= ~SLAP_ATTR_IXADD;
264 bdb_modify( Operation *op, SlapReply *rs )
266 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
268 EntryInfo *ei = NULL;
269 int manageDSAit = get_manageDSAit( op );
270 char textbuf[SLAP_TEXT_BUFLEN];
271 size_t textlen = sizeof textbuf;
272 DB_TXN *ltid = NULL, *lt2;
273 struct bdb_op_info opinfo;
276 u_int32_t locker = 0;
281 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
283 struct psid_entry *pm_list, *pm_prev;
287 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", op->o_req_dn.bv_val, 0, 0 );
289 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", op->o_req_dn.bv_val, 0, 0 );
293 retry: /* transaction retry */
295 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
299 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
301 Debug(LDAP_DEBUG_TRACE,
302 "bdb_modify: retrying...\n", 0, 0, 0);
305 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
306 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
307 while ( pm_list != NULL ) {
308 LDAP_LIST_REMOVE ( pm_list, ps_link );
310 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
315 rs->sr_err = TXN_ABORT( ltid );
317 op->o_private = NULL;
318 op->o_do_not_cache = opinfo.boi_acl_cache;
319 if( rs->sr_err != 0 ) {
320 rs->sr_err = LDAP_OTHER;
321 rs->sr_text = "internal error";
324 ldap_pvt_thread_yield();
327 /* begin transaction */
328 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
329 bdb->bi_db_opflags );
331 if( rs->sr_err != 0 ) {
333 LDAP_LOG ( OPERATION, DETAIL1,
334 "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
336 Debug( LDAP_DEBUG_TRACE,
337 "bdb_modify: txn_begin failed: %s (%d)\n",
338 db_strerror(rs->sr_err), rs->sr_err, 0 );
340 rs->sr_err = LDAP_OTHER;
341 rs->sr_text = "internal error";
345 locker = TXN_ID ( ltid );
347 opinfo.boi_bdb = op->o_bd;
348 opinfo.boi_txn = ltid;
349 opinfo.boi_locker = locker;
351 opinfo.boi_acl_cache = op->o_do_not_cache;
352 op->o_private = &opinfo;
354 /* get entry or ancestor */
355 rs->sr_err = bdb_dn2entry( op->o_bd, ltid, &op->o_req_ndn, &ei, 1,
356 locker, &lock, op->o_tmpmemctx );
358 if ( rs->sr_err != 0 ) {
360 LDAP_LOG ( OPERATION, DETAIL1,
361 "bdb_modify: dn2entry failed: (%d)\n", rs->sr_err, 0, 0 );
363 Debug( LDAP_DEBUG_TRACE,
364 "bdb_modify: dn2entry failed (%d)\n",
367 switch( rs->sr_err ) {
368 case DB_LOCK_DEADLOCK:
369 case DB_LOCK_NOTGRANTED:
374 rs->sr_text = "ldap server busy";
377 rs->sr_err = LDAP_OTHER;
378 rs->sr_text = "internal error";
384 /* acquire and lock entry */
385 if ( rs->sr_err == DB_NOTFOUND ) {
387 rs->sr_matched = ch_strdup( e->e_dn );
388 rs->sr_ref = is_entry_referral( e )
389 ? get_entry_referrals( op, e )
391 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
395 rs->sr_ref = referral_rewrite( default_referral,
396 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
399 rs->sr_err = LDAP_REFERRAL;
400 send_ldap_result( op, rs );
402 ber_bvarray_free( rs->sr_ref );
403 free( (char *)rs->sr_matched );
405 rs->sr_matched = NULL;
410 if ( !manageDSAit && is_entry_referral( e ) ) {
411 /* entry is a referral, don't allow modify */
412 rs->sr_ref = get_entry_referrals( op, e );
415 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: entry is referral\n", 0, 0, 0 );
417 Debug( LDAP_DEBUG_TRACE,
418 "bdb_modify: entry is referral\n",
422 rs->sr_err = LDAP_REFERRAL;
423 rs->sr_matched = e->e_name.bv_val;
424 send_ldap_result( op, rs );
426 ber_bvarray_free( rs->sr_ref );
428 rs->sr_matched = NULL;
432 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
433 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
434 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
435 bdb_psearch(op, rs, ps_list, e, LDAP_PSEARCH_BY_PREMODIFY );
440 /* nested transaction */
441 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
442 bdb->bi_db_opflags );
444 if( rs->sr_err != 0 ) {
446 LDAP_LOG ( OPERATION, ERR,
447 "bdb_modify: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
449 Debug( LDAP_DEBUG_TRACE,
450 "bdb_modify: txn_begin(2) failed: %s (%d)\n",
451 db_strerror(rs->sr_err), rs->sr_err, 0 );
453 rs->sr_err = LDAP_OTHER;
454 rs->sr_text = "internal error";
457 /* Modify the entry */
459 rs->sr_err = bdb_modify_internal( op, lt2, op->oq_modify.rs_modlist,
460 &dummy, &rs->sr_text, textbuf, textlen );
462 if( rs->sr_err != LDAP_SUCCESS ) {
464 LDAP_LOG ( OPERATION, ERR,
465 "bdb_modify: modify failed (%d)\n", rs->sr_err, 0, 0 );
467 Debug( LDAP_DEBUG_TRACE,
468 "bdb_modify: modify failed (%d)\n",
471 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
472 rs->sr_err = opinfo.boi_err;
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 ) {
486 LDAP_LOG ( OPERATION, ERR,
487 "bdb_modify: id2entry update failed (%d)\n", rs->sr_err, 0, 0 );
489 Debug( LDAP_DEBUG_TRACE,
490 "bdb_modify: id2entry update failed (%d)\n",
493 switch( rs->sr_err ) {
494 case DB_LOCK_DEADLOCK:
495 case DB_LOCK_NOTGRANTED:
498 rs->sr_text = "entry update failed";
501 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
502 rs->sr_err = LDAP_OTHER;
503 rs->sr_text = "txn_commit(2) failed";
508 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
509 rs->sr_text = "txn_abort (no-op) failed";
512 rs->sr_err = LDAP_SUCCESS;
515 bdb_cache_modify( e, dummy.e_attrs, bdb->bi_dbenv, locker, &lock );
516 rs->sr_err = TXN_COMMIT( ltid, 0 );
519 op->o_private = NULL;
521 if( rs->sr_err != 0 ) {
523 LDAP_LOG ( OPERATION, ERR,
524 "bdb_modify: txn_%s failed %s (%d)\n",
525 op->o_noop ? "abort (no_op)" : "commit", db_strerror(rs->sr_err), rs->sr_err );
527 Debug( LDAP_DEBUG_TRACE,
528 "bdb_modify: txn_%s failed: %s (%d)\n",
529 op->o_noop ? "abort (no-op)" : "commit",
530 db_strerror(rs->sr_err), rs->sr_err );
532 rs->sr_err = LDAP_OTHER;
533 rs->sr_text = "commit failed";
537 LDAP_LOG ( OPERATION, DETAIL1,
538 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
539 op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
541 Debug( LDAP_DEBUG_TRACE,
542 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
543 op->o_noop ? " (no-op)" : "",
546 rs->sr_err = LDAP_SUCCESS;
551 send_ldap_result( op, rs );
553 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
554 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
555 /* Loop through in-scope entries for each psearch spec */
556 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
557 bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_MODIFY );
559 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
560 while ( pm_list != NULL ) {
561 bdb_psearch(op, rs, pm_list->ps_op,
562 e, LDAP_PSEARCH_BY_SCOPEOUT);
563 LDAP_LIST_REMOVE ( pm_list, ps_link );
565 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
571 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
572 ldap_pvt_thread_yield();
573 TXN_CHECKPOINT( bdb->bi_dbenv,
574 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
579 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
580 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
581 while ( pm_list != NULL ) {
582 LDAP_LIST_REMOVE ( pm_list, ps_link );
584 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
589 op->o_private = NULL;
593 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
595 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );