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 = e->e_attrs;
45 e->e_attrs = attrs_dup( e->e_attrs );
47 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
50 switch ( mod->sm_op ) {
53 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify_internal: add\n", 0,0,0);
55 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
57 err = modify_add_values( e, mod, get_permissiveModify(op),
58 text, textbuf, textlen );
59 if( err != LDAP_SUCCESS ) {
61 LDAP_LOG ( OPERATION, ERR,
62 "bdb_modify_internal: %d %s\n", err, *text, 0 );
64 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
72 LDAP_LOG ( OPERATION, DETAIL1,
73 "bdb_modify_internal: delete\n", 0, 0, 0 );
75 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
77 err = modify_delete_values( e, mod, get_permissiveModify(op),
78 text, textbuf, textlen );
79 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
80 if( err != LDAP_SUCCESS ) {
82 LDAP_LOG ( OPERATION, ERR,
83 "bdb_modify_internal: %d %s\n", err, *text, 0 );
85 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
91 case LDAP_MOD_REPLACE:
93 LDAP_LOG ( OPERATION, DETAIL1,
94 "bdb_modify_internal: replace\n", 0, 0, 0 );
96 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
98 err = modify_replace_values( e, mod, get_permissiveModify(op),
99 text, textbuf, textlen );
100 if( err != LDAP_SUCCESS ) {
102 LDAP_LOG ( OPERATION, ERR,
103 "bdb_modify_internal: %d %s\n", err, *text, 0 );
105 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
111 case SLAP_MOD_SOFTADD:
113 LDAP_LOG ( OPERATION, DETAIL1,
114 "bdb_modify_internal: softadd\n",0,0,0 );
116 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
118 /* Avoid problems in index_add_mods()
119 * We need to add index if necessary.
121 mod->sm_op = LDAP_MOD_ADD;
123 err = modify_add_values( e, mod, get_permissiveModify(op),
124 text, textbuf, textlen );
125 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
129 if( err != LDAP_SUCCESS ) {
131 LDAP_LOG ( OPERATION, ERR,
132 "bdb_modify_internal: %d %s\n", err, *text, 0 );
134 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
142 LDAP_LOG ( OPERATION, ERR,
143 "bdb_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
145 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
148 *text = "Invalid modify operation";
151 LDAP_LOG ( OPERATION, ERR,
152 "bdb_modify_internal: %d %s\n", err, *text, 0 );
154 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
159 if ( err != LDAP_SUCCESS ) {
160 attrs_free( e->e_attrs );
161 e->e_attrs = save_attrs;
162 /* unlock entry, delete from cache */
166 /* If objectClass was modified, reset the flags */
167 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
171 /* check if modified attribute was indexed
172 * but not in case of NOOP... */
173 err = bdb_index_is_indexed( op->o_bd, mod->sm_desc );
174 if ( err == LDAP_SUCCESS && !op->o_noop ) {
175 ap = attr_find( save_attrs, mod->sm_desc );
176 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
178 ap = attr_find( e->e_attrs, mod->sm_desc );
179 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
183 /* check that the entry still obeys the schema */
184 rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
185 if ( rc != LDAP_SUCCESS || op->o_noop ) {
186 attrs_free( e->e_attrs );
187 e->e_attrs = save_attrs;
189 if ( rc != LDAP_SUCCESS ) {
191 LDAP_LOG ( OPERATION, ERR, "bdb_modify_internal: "
192 "entry failed schema check %s\n",
195 Debug( LDAP_DEBUG_ANY,
196 "entry failed schema check: %s\n",
201 /* if NOOP then silently revert to saved attrs */
205 /* update the indices of the modified attributes */
207 /* start with deleting the old index entries */
208 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
209 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
210 rc = bdb_index_values( op->o_bd, tid, ap->a_desc,
212 e->e_id, SLAP_INDEX_DELETE_OP );
213 if ( rc != LDAP_SUCCESS ) {
214 attrs_free( e->e_attrs );
215 e->e_attrs = save_attrs;
217 LDAP_LOG ( OPERATION, ERR,
218 "bdb_modify_internal: attribute index delete failure\n",
221 Debug( LDAP_DEBUG_ANY,
222 "Attribute index delete failure",
227 ap->a_flags &= ~SLAP_ATTR_IXDEL;
231 /* add the new index entries */
232 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
233 if (ap->a_flags & SLAP_ATTR_IXADD) {
234 rc = bdb_index_values( op->o_bd, tid, ap->a_desc,
236 e->e_id, SLAP_INDEX_ADD_OP );
237 if ( rc != LDAP_SUCCESS ) {
238 attrs_free( e->e_attrs );
239 e->e_attrs = save_attrs;
241 LDAP_LOG ( OPERATION, ERR,
242 "bdb_modify_internal: attribute index add failure\n",
245 Debug( LDAP_DEBUG_ANY,
246 "Attribute index add failure",
251 ap->a_flags &= ~SLAP_ATTR_IXADD;
255 /* If we've done repeated mods on a cached entry, then e_attrs
256 * is no longer contiguous with the entry.
258 if( (void *) save_attrs != (void *) (e+1)) {
259 attrs_free( save_attrs );
267 bdb_modify( Operation *op, SlapReply *rs )
269 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
270 Entry *matched = NULL;
272 int manageDSAit = get_manageDSAit( op );
273 char textbuf[SLAP_TEXT_BUFLEN];
274 size_t textlen = sizeof textbuf;
276 struct bdb_op_info opinfo;
278 u_int32_t locker = 0;
283 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
285 struct psid_entry *pm_list, *pm_prev;
289 LDAP_LOG ( OPERATION, ENTRY, "bdb_modify: %s\n", op->o_req_dn.bv_val, 0, 0 );
291 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", op->o_req_dn.bv_val, 0, 0 );
295 retry: /* transaction retry */
297 bdb_cache_delete_entry(&bdb->bi_cache, e);
298 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
301 LDAP_LOG ( OPERATION, DETAIL1, "bdb_modify: retrying...\n", 0, 0, 0 );
303 Debug(LDAP_DEBUG_TRACE,
304 "bdb_modify: retrying...\n", 0, 0, 0);
307 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
308 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
309 while ( pm_list != NULL ) {
310 LDAP_LIST_REMOVE ( pm_list, ps_link );
312 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
317 rs->sr_err = TXN_ABORT( ltid );
319 op->o_private = NULL;
320 op->o_do_not_cache = opinfo.boi_acl_cache;
321 if( rs->sr_err != 0 ) {
322 rs->sr_err = LDAP_OTHER;
323 rs->sr_text = "internal error";
326 ldap_pvt_thread_yield();
329 /* begin transaction */
330 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
331 bdb->bi_db_opflags );
333 if( rs->sr_err != 0 ) {
335 LDAP_LOG ( OPERATION, DETAIL1,
336 "bdb_modify: txn_begin failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
338 Debug( LDAP_DEBUG_TRACE,
339 "bdb_modify: txn_begin failed: %s (%d)\n",
340 db_strerror(rs->sr_err), rs->sr_err, 0 );
342 rs->sr_err = LDAP_OTHER;
343 rs->sr_text = "internal error";
347 locker = TXN_ID ( ltid );
349 opinfo.boi_bdb = op->o_bd;
350 opinfo.boi_txn = ltid;
351 opinfo.boi_locker = locker;
353 opinfo.boi_acl_cache = op->o_do_not_cache;
354 op->o_private = &opinfo;
357 rs->sr_err = bdb_dn2entry_w( op->o_bd, ltid, &op->o_req_ndn, &e, &matched, 0, locker, &lock );
359 if ( rs->sr_err != 0 ) {
361 LDAP_LOG ( OPERATION, DETAIL1,
362 "bdb_modify: dn2entry failed: (%d)\n", rs->sr_err, 0, 0 );
364 Debug( LDAP_DEBUG_TRACE,
365 "bdb_modify: dn2entry failed (%d)\n",
368 switch( rs->sr_err ) {
369 case DB_LOCK_DEADLOCK:
370 case DB_LOCK_NOTGRANTED:
375 rs->sr_text = "ldap server busy";
378 rs->sr_err = LDAP_OTHER;
379 rs->sr_text = "internal error";
384 /* acquire and lock entry */
386 if ( matched != NULL ) {
387 rs->sr_matched = ch_strdup( matched->e_dn );
388 rs->sr_ref = is_entry_referral( matched )
389 ? get_entry_referrals( op, matched )
391 bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, matched);
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 /* Modify the entry */
441 rs->sr_err = bdb_modify_internal( op, ltid, op->oq_modify.rs_modlist, e,
442 &rs->sr_text, textbuf, textlen );
444 if( rs->sr_err != LDAP_SUCCESS ) {
446 LDAP_LOG ( OPERATION, ERR,
447 "bdb_modify: modify failed (%d)\n", rs->sr_err, 0, 0 );
449 Debug( LDAP_DEBUG_TRACE,
450 "bdb_modify: modify failed (%d)\n",
453 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
454 rs->sr_err = opinfo.boi_err;
456 switch( rs->sr_err ) {
457 case DB_LOCK_DEADLOCK:
458 case DB_LOCK_NOTGRANTED:
464 /* change the entry itself */
465 rs->sr_err = bdb_id2entry_update( op->o_bd, ltid, e );
466 if ( rs->sr_err != 0 ) {
468 LDAP_LOG ( OPERATION, ERR,
469 "bdb_modify: id2entry update failed (%d)\n", rs->sr_err, 0, 0 );
471 Debug( LDAP_DEBUG_TRACE,
472 "bdb_modify: id2entry update failed (%d)\n",
475 switch( rs->sr_err ) {
476 case DB_LOCK_DEADLOCK:
477 case DB_LOCK_NOTGRANTED:
480 rs->sr_text = "entry update failed";
485 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
486 rs->sr_text = "txn_abort (no-op) failed";
489 rs->sr_err = LDAP_SUCCESS;
492 rs->sr_err = TXN_COMMIT( ltid, 0 );
495 op->o_private = NULL;
497 if( rs->sr_err != 0 ) {
499 LDAP_LOG ( OPERATION, ERR,
500 "bdb_modify: txn_%s failed %s (%d)\n",
501 op->o_noop ? "abort (no_op)" : "commit", db_strerror(rs->sr_err), rs->sr_err );
503 Debug( LDAP_DEBUG_TRACE,
504 "bdb_modify: txn_%s failed: %s (%d)\n",
505 op->o_noop ? "abort (no-op)" : "commit",
506 db_strerror(rs->sr_err), rs->sr_err );
508 rs->sr_err = LDAP_OTHER;
509 rs->sr_text = "commit failed";
513 LDAP_LOG ( OPERATION, DETAIL1,
514 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
515 op->o_noop ? " (no_op)" : "", e->e_id, e->e_dn );
517 Debug( LDAP_DEBUG_TRACE,
518 "bdb_modify: updated%s id=%08lx dn=\"%s\"\n",
519 op->o_noop ? " (no-op)" : "",
522 rs->sr_err = LDAP_SUCCESS;
527 send_ldap_result( op, rs );
529 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
530 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
531 /* Loop through in-scope entries for each psearch spec */
532 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
533 bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_MODIFY );
535 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
536 while ( pm_list != NULL ) {
537 bdb_psearch(op, rs, pm_list->ps_op,
538 e, LDAP_PSEARCH_BY_SCOPEOUT);
539 LDAP_LIST_REMOVE ( pm_list, ps_link );
541 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
547 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
548 ldap_pvt_thread_yield();
549 TXN_CHECKPOINT( bdb->bi_dbenv,
550 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
555 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
556 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
557 while ( pm_list != NULL ) {
558 LDAP_LIST_REMOVE ( pm_list, ps_link );
560 pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
565 op->o_private = NULL;
569 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
571 return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );