Back to index

openldap  2.4.31
modify.c
Go to the documentation of this file.
00001 /* modify.c - bdb backend modify routine */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2000-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 #include <ac/string.h>
00021 #include <ac/time.h>
00022 
00023 #include "back-bdb.h"
00024 
00025 static struct berval scbva[] = {
00026        BER_BVC("glue"),
00027        BER_BVNULL
00028 };
00029 
00030 static void
00031 bdb_modify_idxflags(
00032        Operation *op,
00033        AttributeDescription *desc,
00034        int got_delete,
00035        Attribute *newattrs,
00036        Attribute *oldattrs )
00037 {
00038        struct berval ix_at;
00039        AttrInfo      *ai;
00040 
00041        /* check if modified attribute was indexed
00042         * but not in case of NOOP... */
00043        ai = bdb_index_mask( op->o_bd, desc, &ix_at );
00044        if ( ai ) {
00045               if ( got_delete ) {
00046                      Attribute     *ap;
00047                      struct berval ix2;
00048 
00049                      ap = attr_find( oldattrs, desc );
00050                      if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
00051 
00052                      /* Find all other attrs that index to same slot */
00053                      for ( ap = newattrs; ap; ap = ap->a_next ) {
00054                             ai = bdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
00055                             if ( ai && ix2.bv_val == ix_at.bv_val )
00056                                    ap->a_flags |= SLAP_ATTR_IXADD;
00057                      }
00058 
00059               } else {
00060                      Attribute     *ap;
00061 
00062                      ap = attr_find( newattrs, desc );
00063                      if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
00064               }
00065        }
00066 }
00067 
00068 int bdb_modify_internal(
00069        Operation *op,
00070        DB_TXN *tid,
00071        Modifications *modlist,
00072        Entry *e,
00073        const char **text,
00074        char *textbuf,
00075        size_t textlen )
00076 {
00077        int rc, err;
00078        Modification  *mod;
00079        Modifications *ml;
00080        Attribute     *save_attrs;
00081        Attribute     *ap;
00082        int                  glue_attr_delete = 0;
00083        int                  got_delete;
00084 
00085        Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
00086               e->e_id, e->e_dn, 0);
00087 
00088        if ( !acl_check_modlist( op, e, modlist )) {
00089               return LDAP_INSUFFICIENT_ACCESS;
00090        }
00091 
00092        /* save_attrs will be disposed of by bdb_cache_modify */
00093        save_attrs = e->e_attrs;
00094        e->e_attrs = attrs_dup( e->e_attrs );
00095 
00096        for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
00097               int match;
00098               mod = &ml->sml_mod;
00099               switch( mod->sm_op ) {
00100               case LDAP_MOD_ADD:
00101               case LDAP_MOD_REPLACE:
00102                      if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
00103                             value_match( &match, slap_schema.si_ad_structuralObjectClass,
00104                                    slap_schema.si_ad_structuralObjectClass->
00105                                           ad_type->sat_equality,
00106                                    SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
00107                                    &mod->sm_values[0], &scbva[0], text );
00108                             if ( !match ) glue_attr_delete = 1;
00109                      }
00110               }
00111               if ( glue_attr_delete )
00112                      break;
00113        }
00114 
00115        if ( glue_attr_delete ) {
00116               Attribute     **app = &e->e_attrs;
00117               while ( *app != NULL ) {
00118                      if ( !is_at_operational( (*app)->a_desc->ad_type )) {
00119                             Attribute *save = *app;
00120                             *app = (*app)->a_next;
00121                             attr_free( save );
00122                             continue;
00123                      }
00124                      app = &(*app)->a_next;
00125               }
00126        }
00127 
00128        for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
00129               mod = &ml->sml_mod;
00130               got_delete = 0;
00131 
00132               switch ( mod->sm_op ) {
00133               case LDAP_MOD_ADD:
00134                      Debug(LDAP_DEBUG_ARGS,
00135                             "bdb_modify_internal: add %s\n",
00136                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00137                      err = modify_add_values( e, mod, get_permissiveModify(op),
00138                             text, textbuf, textlen );
00139                      if( err != LDAP_SUCCESS ) {
00140                             Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00141                                    err, *text, 0);
00142                      }
00143                      break;
00144 
00145               case LDAP_MOD_DELETE:
00146                      if ( glue_attr_delete ) {
00147                             err = LDAP_SUCCESS;
00148                             break;
00149                      }
00150 
00151                      Debug(LDAP_DEBUG_ARGS,
00152                             "bdb_modify_internal: delete %s\n",
00153                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00154                      err = modify_delete_values( e, mod, get_permissiveModify(op),
00155                             text, textbuf, textlen );
00156                      if( err != LDAP_SUCCESS ) {
00157                             Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00158                                    err, *text, 0);
00159                      } else {
00160                             got_delete = 1;
00161                      }
00162                      break;
00163 
00164               case LDAP_MOD_REPLACE:
00165                      Debug(LDAP_DEBUG_ARGS,
00166                             "bdb_modify_internal: replace %s\n",
00167                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00168                      err = modify_replace_values( e, mod, get_permissiveModify(op),
00169                             text, textbuf, textlen );
00170                      if( err != LDAP_SUCCESS ) {
00171                             Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00172                                    err, *text, 0);
00173                      } else {
00174                             got_delete = 1;
00175                      }
00176                      break;
00177 
00178               case LDAP_MOD_INCREMENT:
00179                      Debug(LDAP_DEBUG_ARGS,
00180                             "bdb_modify_internal: increment %s\n",
00181                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00182                      err = modify_increment_values( e, mod, get_permissiveModify(op),
00183                             text, textbuf, textlen );
00184                      if( err != LDAP_SUCCESS ) {
00185                             Debug(LDAP_DEBUG_ARGS,
00186                                    "bdb_modify_internal: %d %s\n",
00187                                    err, *text, 0);
00188                      } else {
00189                             got_delete = 1;
00190                      }
00191                      break;
00192 
00193               case SLAP_MOD_SOFTADD:
00194                      Debug(LDAP_DEBUG_ARGS,
00195                             "bdb_modify_internal: softadd %s\n",
00196                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00197                      /* Avoid problems in index_add_mods()
00198                       * We need to add index if necessary.
00199                       */
00200                      mod->sm_op = LDAP_MOD_ADD;
00201 
00202                      err = modify_add_values( e, mod, get_permissiveModify(op),
00203                             text, textbuf, textlen );
00204 
00205                      mod->sm_op = SLAP_MOD_SOFTADD;
00206 
00207                      if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
00208                             err = LDAP_SUCCESS;
00209                      }
00210 
00211                      if( err != LDAP_SUCCESS ) {
00212                             Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00213                                    err, *text, 0);
00214                      }
00215                      break;
00216 
00217               case SLAP_MOD_SOFTDEL:
00218                      Debug(LDAP_DEBUG_ARGS,
00219                             "bdb_modify_internal: softdel %s\n",
00220                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00221                      /* Avoid problems in index_delete_mods()
00222                       * We need to add index if necessary.
00223                       */
00224                      mod->sm_op = LDAP_MOD_DELETE;
00225 
00226                      err = modify_delete_values( e, mod, get_permissiveModify(op),
00227                             text, textbuf, textlen );
00228 
00229                      mod->sm_op = SLAP_MOD_SOFTDEL;
00230 
00231                      if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
00232                             err = LDAP_SUCCESS;
00233                      }
00234 
00235                      if( err != LDAP_SUCCESS ) {
00236                             Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00237                                    err, *text, 0);
00238                      }
00239                      break;
00240 
00241               case SLAP_MOD_ADD_IF_NOT_PRESENT:
00242                      if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) {
00243                             /* skip */
00244                             err = LDAP_SUCCESS;
00245                             break;
00246                      }
00247 
00248                      Debug(LDAP_DEBUG_ARGS,
00249                             "bdb_modify_internal: add_if_not_present %s\n",
00250                             mod->sm_desc->ad_cname.bv_val, 0, 0);
00251                      /* Avoid problems in index_add_mods()
00252                       * We need to add index if necessary.
00253                       */
00254                      mod->sm_op = LDAP_MOD_ADD;
00255 
00256                      err = modify_add_values( e, mod, get_permissiveModify(op),
00257                             text, textbuf, textlen );
00258 
00259                      mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
00260 
00261                      if( err != LDAP_SUCCESS ) {
00262                             Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00263                                    err, *text, 0);
00264                      }
00265                      break;
00266 
00267               default:
00268                      Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
00269                             mod->sm_op, 0, 0);
00270                      *text = "Invalid modify operation";
00271                      err = LDAP_OTHER;
00272                      Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
00273                             err, *text, 0);
00274               }
00275 
00276               if ( err != LDAP_SUCCESS ) {
00277                      attrs_free( e->e_attrs );
00278                      e->e_attrs = save_attrs;
00279                      /* unlock entry, delete from cache */
00280                      return err; 
00281               }
00282 
00283               /* If objectClass was modified, reset the flags */
00284               if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
00285                      e->e_ocflags = 0;
00286               }
00287 
00288               if ( glue_attr_delete ) e->e_ocflags = 0;
00289 
00290 
00291               /* check if modified attribute was indexed
00292                * but not in case of NOOP... */
00293               if ( !op->o_noop ) {
00294                      bdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
00295               }
00296        }
00297 
00298        /* check that the entry still obeys the schema */
00299        ap = NULL;
00300        rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
00301               text, textbuf, textlen );
00302        if ( rc != LDAP_SUCCESS || op->o_noop ) {
00303               attrs_free( e->e_attrs );
00304               /* clear the indexing flags */
00305               for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
00306                      ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
00307               }
00308               e->e_attrs = save_attrs;
00309 
00310               if ( rc != LDAP_SUCCESS ) {
00311                      Debug( LDAP_DEBUG_ANY,
00312                             "entry failed schema check: %s\n",
00313                             *text, 0, 0 );
00314               }
00315 
00316               /* if NOOP then silently revert to saved attrs */
00317               return rc;
00318        }
00319 
00320        /* structuralObjectClass modified! */
00321        if ( ap ) {
00322               assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
00323               if ( !op->o_noop ) {
00324                      bdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
00325                             1, e->e_attrs, save_attrs );
00326               }
00327        }
00328 
00329        /* update the indices of the modified attributes */
00330 
00331        /* start with deleting the old index entries */
00332        for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
00333               if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
00334                      struct berval *vals;
00335                      Attribute *a2;
00336                      ap->a_flags &= ~SLAP_ATTR_IXDEL;
00337                      a2 = attr_find( e->e_attrs, ap->a_desc );
00338                      if ( a2 ) {
00339                             /* need to detect which values were deleted */
00340                             int i, j;
00341                             vals = op->o_tmpalloc( (ap->a_numvals + 1) *
00342                                    sizeof(struct berval), op->o_tmpmemctx );
00343                             j = 0;
00344                             for ( i=0; i < ap->a_numvals; i++ ) {
00345                                    rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
00346                                           &ap->a_nvals[i], NULL, op->o_tmpmemctx );
00347                                    /* Save deleted values */
00348                                    if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
00349                                           vals[j++] = ap->a_nvals[i];
00350                             }
00351                             BER_BVZERO(vals+j);
00352                      } else {
00353                             /* attribute was completely deleted */
00354                             vals = ap->a_nvals;
00355                      }
00356                      rc = 0;
00357                      if ( !BER_BVISNULL( vals )) {
00358                             rc = bdb_index_values( op, tid, ap->a_desc,
00359                                    vals, e->e_id, SLAP_INDEX_DELETE_OP );
00360                             if ( rc != LDAP_SUCCESS ) {
00361                                    Debug( LDAP_DEBUG_ANY,
00362                                           "%s: attribute \"%s\" index delete failure\n",
00363                                           op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
00364                                    attrs_free( e->e_attrs );
00365                                    e->e_attrs = save_attrs;
00366                             }
00367                      }
00368                      if ( vals != ap->a_nvals )
00369                             op->o_tmpfree( vals, op->o_tmpmemctx );
00370                      if ( rc ) return rc;
00371               }
00372        }
00373 
00374        /* add the new index entries */
00375        for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
00376               if (ap->a_flags & SLAP_ATTR_IXADD) {
00377                      ap->a_flags &= ~SLAP_ATTR_IXADD;
00378                      rc = bdb_index_values( op, tid, ap->a_desc,
00379                             ap->a_nvals,
00380                             e->e_id, SLAP_INDEX_ADD_OP );
00381                      if ( rc != LDAP_SUCCESS ) {
00382                             Debug( LDAP_DEBUG_ANY,
00383                                    "%s: attribute \"%s\" index add failure\n",
00384                                    op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
00385                             attrs_free( e->e_attrs );
00386                             e->e_attrs = save_attrs;
00387                             return rc;
00388                      }
00389               }
00390        }
00391 
00392        return rc;
00393 }
00394 
00395 
00396 int
00397 bdb_modify( Operation *op, SlapReply *rs )
00398 {
00399        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
00400        Entry         *e = NULL;
00401        EntryInfo     *ei = NULL;
00402        int           manageDSAit = get_manageDSAit( op );
00403        char textbuf[SLAP_TEXT_BUFLEN];
00404        size_t textlen = sizeof textbuf;
00405        DB_TXN *ltid = NULL, *lt2;
00406        struct bdb_op_info opinfo = {{{ 0 }}};
00407        Entry         dummy = {0};
00408 
00409        DB_LOCK              lock;
00410 
00411        int           num_retries = 0;
00412 
00413        LDAPControl **preread_ctrl = NULL;
00414        LDAPControl **postread_ctrl = NULL;
00415        LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
00416        int num_ctrls = 0;
00417 
00418        int rc;
00419 
00420 #ifdef LDAP_X_TXN
00421        int settle = 0;
00422 #endif
00423 
00424        Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
00425               op->o_req_dn.bv_val, 0, 0 );
00426 
00427 #ifdef LDAP_X_TXN
00428        if( op->o_txnSpec ) {
00429               /* acquire connection lock */
00430               ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
00431               if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
00432                      rs->sr_text = "invalid transaction identifier";
00433                      rs->sr_err = LDAP_X_TXN_ID_INVALID;
00434                      goto txnReturn;
00435               } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
00436                      settle=1;
00437                      goto txnReturn;
00438               }
00439 
00440               if( op->o_conn->c_txn_backend == NULL ) {
00441                      op->o_conn->c_txn_backend = op->o_bd;
00442 
00443               } else if( op->o_conn->c_txn_backend != op->o_bd ) {
00444                      rs->sr_text = "transaction cannot span multiple database contexts";
00445                      rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
00446                      goto txnReturn;
00447               }
00448 
00449               /* insert operation into transaction */
00450 
00451               rs->sr_text = "transaction specified";
00452               rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
00453 
00454 txnReturn:
00455               /* release connection lock */
00456               ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
00457 
00458               if( !settle ) {
00459                      send_ldap_result( op, rs );
00460                      return rs->sr_err;
00461               }
00462        }
00463 #endif
00464 
00465        ctrls[num_ctrls] = NULL;
00466 
00467        /* Don't touch the opattrs, if this is a contextCSN update
00468         * initiated from updatedn */
00469        if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
00470                op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
00471 
00472               slap_mods_opattrs( op, &op->orm_modlist, 1 );
00473        }
00474 
00475        if( 0 ) {
00476 retry: /* transaction retry */
00477               if ( dummy.e_attrs ) {
00478                      attrs_free( dummy.e_attrs );
00479                      dummy.e_attrs = NULL;
00480               }
00481               if( e != NULL ) {
00482                      bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
00483                      e = NULL;
00484               }
00485               Debug(LDAP_DEBUG_TRACE,
00486                      LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
00487 
00488               rs->sr_err = TXN_ABORT( ltid );
00489               ltid = NULL;
00490               LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
00491               opinfo.boi_oe.oe_key = NULL;
00492               op->o_do_not_cache = opinfo.boi_acl_cache;
00493               if( rs->sr_err != 0 ) {
00494                      rs->sr_err = LDAP_OTHER;
00495                      rs->sr_text = "internal error";
00496                      goto return_results;
00497               }
00498               if ( op->o_abandon ) {
00499                      rs->sr_err = SLAPD_ABANDON;
00500                      goto return_results;
00501               }
00502               bdb_trans_backoff( ++num_retries );
00503        }
00504 
00505        /* begin transaction */
00506        rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
00507               bdb->bi_db_opflags );
00508        rs->sr_text = NULL;
00509        if( rs->sr_err != 0 ) {
00510               Debug( LDAP_DEBUG_TRACE,
00511                      LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
00512                      "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
00513               rs->sr_err = LDAP_OTHER;
00514               rs->sr_text = "internal error";
00515               goto return_results;
00516        }
00517 
00518        opinfo.boi_oe.oe_key = bdb;
00519        opinfo.boi_txn = ltid;
00520        opinfo.boi_err = 0;
00521        opinfo.boi_acl_cache = op->o_do_not_cache;
00522        LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
00523 
00524        /* get entry or ancestor */
00525        rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
00526               &lock );
00527 
00528        if ( rs->sr_err != 0 ) {
00529               Debug( LDAP_DEBUG_TRACE,
00530                      LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
00531                      rs->sr_err, 0, 0 );
00532               switch( rs->sr_err ) {
00533               case DB_LOCK_DEADLOCK:
00534               case DB_LOCK_NOTGRANTED:
00535                      goto retry;
00536               case DB_NOTFOUND:
00537                      break;
00538               case LDAP_BUSY:
00539                      rs->sr_text = "ldap server busy";
00540                      goto return_results;
00541               default:
00542                      rs->sr_err = LDAP_OTHER;
00543                      rs->sr_text = "internal error";
00544                      goto return_results;
00545               }
00546        }
00547 
00548        e = ei->bei_e;
00549 
00550        /* acquire and lock entry */
00551        /* FIXME: dn2entry() should return non-glue entry */
00552        if (( rs->sr_err == DB_NOTFOUND ) ||
00553               ( !manageDSAit && e && is_entry_glue( e )))
00554        {
00555               if ( e != NULL ) {
00556                      rs->sr_matched = ch_strdup( e->e_dn );
00557                      rs->sr_ref = is_entry_referral( e )
00558                             ? get_entry_referrals( op, e )
00559                             : NULL;
00560                      bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
00561                      e = NULL;
00562 
00563               } else {
00564                      rs->sr_ref = referral_rewrite( default_referral, NULL,
00565                             &op->o_req_dn, LDAP_SCOPE_DEFAULT );
00566               }
00567 
00568               rs->sr_err = LDAP_REFERRAL;
00569               send_ldap_result( op, rs );
00570 
00571               if ( rs->sr_ref != default_referral ) {
00572                      ber_bvarray_free( rs->sr_ref );
00573               }
00574               free( (char *)rs->sr_matched );
00575               rs->sr_ref = NULL;
00576               rs->sr_matched = NULL;
00577 
00578               goto done;
00579        }
00580 
00581        if ( !manageDSAit && is_entry_referral( e ) ) {
00582               /* entry is a referral, don't allow modify */
00583               rs->sr_ref = get_entry_referrals( op, e );
00584 
00585               Debug( LDAP_DEBUG_TRACE,
00586                      LDAP_XSTRING(bdb_modify) ": entry is referral\n",
00587                      0, 0, 0 );
00588 
00589               rs->sr_err = LDAP_REFERRAL;
00590               rs->sr_matched = e->e_name.bv_val;
00591               send_ldap_result( op, rs );
00592 
00593               ber_bvarray_free( rs->sr_ref );
00594               rs->sr_ref = NULL;
00595               rs->sr_matched = NULL;
00596               goto done;
00597        }
00598 
00599        if ( get_assert( op ) &&
00600               ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
00601        {
00602               rs->sr_err = LDAP_ASSERTION_FAILED;
00603               goto return_results;
00604        }
00605 
00606        if( op->o_preread ) {
00607               if( preread_ctrl == NULL ) {
00608                      preread_ctrl = &ctrls[num_ctrls++];
00609                      ctrls[num_ctrls] = NULL;
00610               }
00611               if ( slap_read_controls( op, rs, e,
00612                      &slap_pre_read_bv, preread_ctrl ) )
00613               {
00614                      Debug( LDAP_DEBUG_TRACE,
00615                             "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
00616                             "failed!\n", 0, 0, 0 );
00617                      if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
00618                             /* FIXME: is it correct to abort
00619                              * operation if control fails? */
00620                             goto return_results;
00621                      }
00622               }
00623        }
00624 
00625        /* nested transaction */
00626        rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
00627        rs->sr_text = NULL;
00628        if( rs->sr_err != 0 ) {
00629               Debug( LDAP_DEBUG_TRACE,
00630                      LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
00631                      db_strerror(rs->sr_err), rs->sr_err, 0 );
00632               rs->sr_err = LDAP_OTHER;
00633               rs->sr_text = "internal error";
00634               goto return_results;
00635        }
00636        /* Modify the entry */
00637        dummy = *e;
00638        rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
00639               &dummy, &rs->sr_text, textbuf, textlen );
00640 
00641        if( rs->sr_err != LDAP_SUCCESS ) {
00642               Debug( LDAP_DEBUG_TRACE,
00643                      LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
00644                      rs->sr_err, 0, 0 );
00645               if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
00646                      rs->sr_err = opinfo.boi_err;
00647               }
00648               /* Only free attrs if they were dup'd.  */
00649               if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
00650               switch( rs->sr_err ) {
00651               case DB_LOCK_DEADLOCK:
00652               case DB_LOCK_NOTGRANTED:
00653                      goto retry;
00654               }
00655               goto return_results;
00656        }
00657 
00658        /* change the entry itself */
00659        rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
00660        if ( rs->sr_err != 0 ) {
00661               Debug( LDAP_DEBUG_TRACE,
00662                      LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
00663                      rs->sr_err, 0, 0 );
00664               switch( rs->sr_err ) {
00665               case DB_LOCK_DEADLOCK:
00666               case DB_LOCK_NOTGRANTED:
00667                      goto retry;
00668               }
00669               rs->sr_text = "entry update failed";
00670               goto return_results;
00671        }
00672 
00673        if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
00674               rs->sr_err = LDAP_OTHER;
00675               rs->sr_text = "txn_commit(2) failed";
00676               goto return_results;
00677        }
00678 
00679        if( op->o_postread ) {
00680               if( postread_ctrl == NULL ) {
00681                      postread_ctrl = &ctrls[num_ctrls++];
00682                      ctrls[num_ctrls] = NULL;
00683               }
00684               if( slap_read_controls( op, rs, &dummy,
00685                      &slap_post_read_bv, postread_ctrl ) )
00686               {
00687                      Debug( LDAP_DEBUG_TRACE,
00688                             "<=- " LDAP_XSTRING(bdb_modify)
00689                             ": post-read failed!\n", 0, 0, 0 );
00690                      if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
00691                             /* FIXME: is it correct to abort
00692                              * operation if control fails? */
00693                             goto return_results;
00694                      }
00695               }
00696        }
00697 
00698        if( op->o_noop ) {
00699               if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
00700                      rs->sr_text = "txn_abort (no-op) failed";
00701               } else {
00702                      rs->sr_err = LDAP_X_NO_OPERATION;
00703                      ltid = NULL;
00704                      /* Only free attrs if they were dup'd.  */
00705                      if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
00706                      goto return_results;
00707               }
00708        } else {
00709               /* may have changed in bdb_modify_internal() */
00710               e->e_ocflags = dummy.e_ocflags;
00711               rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
00712               switch( rc ) {
00713               case DB_LOCK_DEADLOCK:
00714               case DB_LOCK_NOTGRANTED:
00715                      goto retry;
00716               }
00717               dummy.e_attrs = NULL;
00718 
00719               rs->sr_err = TXN_COMMIT( ltid, 0 );
00720        }
00721        ltid = NULL;
00722        LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
00723        opinfo.boi_oe.oe_key = NULL;
00724 
00725        if( rs->sr_err != 0 ) {
00726               Debug( LDAP_DEBUG_TRACE,
00727                      LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
00728                      op->o_noop ? "abort (no-op)" : "commit",
00729                      db_strerror(rs->sr_err), rs->sr_err );
00730               rs->sr_err = LDAP_OTHER;
00731               rs->sr_text = "commit failed";
00732 
00733               goto return_results;
00734        }
00735 
00736        Debug( LDAP_DEBUG_TRACE,
00737               LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
00738               op->o_noop ? " (no-op)" : "",
00739               dummy.e_id, op->o_req_dn.bv_val );
00740 
00741        rs->sr_err = LDAP_SUCCESS;
00742        rs->sr_text = NULL;
00743        if( num_ctrls ) rs->sr_ctrls = ctrls;
00744 
00745 return_results:
00746        if( dummy.e_attrs ) {
00747               attrs_free( dummy.e_attrs );
00748        }
00749        send_ldap_result( op, rs );
00750 
00751        if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
00752               TXN_CHECKPOINT( bdb->bi_dbenv,
00753                      bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
00754        }
00755 
00756 done:
00757        slap_graduate_commit_csn( op );
00758 
00759        if( ltid != NULL ) {
00760               TXN_ABORT( ltid );
00761        }
00762        if ( opinfo.boi_oe.oe_key ) {
00763               LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
00764        }
00765 
00766        if( e != NULL ) {
00767               bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
00768        }
00769 
00770        if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
00771               slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
00772               slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
00773        }
00774        if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
00775               slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
00776               slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
00777        }
00778 
00779        rs->sr_text = NULL;
00780 
00781        return rs->sr_err;
00782 }