Back to index

openldap  2.4.31
delete.c
Go to the documentation of this file.
00001 /* delete.c - mdb backend delete 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 
00022 #include "lutil.h"
00023 #include "back-mdb.h"
00024 
00025 int
00026 mdb_delete( Operation *op, SlapReply *rs )
00027 {
00028        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00029        struct berval pdn = {0, NULL};
00030        Entry  *e = NULL;
00031        Entry  *p = NULL;
00032        int           manageDSAit = get_manageDSAit( op );
00033        AttributeDescription *children = slap_schema.si_ad_children;
00034        AttributeDescription *entry = slap_schema.si_ad_entry;
00035        MDB_txn              *txn = NULL;
00036        MDB_cursor    *mc;
00037        mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
00038 
00039        LDAPControl **preread_ctrl = NULL;
00040        LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
00041        int num_ctrls = 0;
00042 
00043        int    parent_is_glue = 0;
00044        int parent_is_leaf = 0;
00045 
00046 #ifdef LDAP_X_TXN
00047        int settle = 0;
00048 #endif
00049 
00050        Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_delete) ": %s\n",
00051               op->o_req_dn.bv_val, 0, 0 );
00052 
00053 #ifdef LDAP_X_TXN
00054        if( op->o_txnSpec ) {
00055               /* acquire connection lock */
00056               ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
00057               if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
00058                      rs->sr_text = "invalid transaction identifier";
00059                      rs->sr_err = LDAP_X_TXN_ID_INVALID;
00060                      goto txnReturn;
00061               } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
00062                      settle=1;
00063                      goto txnReturn;
00064               }
00065 
00066               if( op->o_conn->c_txn_backend == NULL ) {
00067                      op->o_conn->c_txn_backend = op->o_bd;
00068 
00069               } else if( op->o_conn->c_txn_backend != op->o_bd ) {
00070                      rs->sr_text = "transaction cannot span multiple database contexts";
00071                      rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
00072                      goto txnReturn;
00073               }
00074 
00075               /* insert operation into transaction */
00076 
00077               rs->sr_text = "transaction specified";
00078               rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
00079 
00080 txnReturn:
00081               /* release connection lock */
00082               ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
00083 
00084               if( !settle ) {
00085                      send_ldap_result( op, rs );
00086                      return rs->sr_err;
00087               }
00088        }
00089 #endif
00090 
00091        ctrls[num_ctrls] = 0;
00092 
00093        /* allocate CSN */
00094        if ( BER_BVISNULL( &op->o_csn ) ) {
00095               struct berval csn;
00096               char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
00097 
00098               csn.bv_val = csnbuf;
00099               csn.bv_len = sizeof(csnbuf);
00100               slap_get_csn( op, &csn, 1 );
00101        }
00102 
00103        /* begin transaction */
00104        rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
00105        rs->sr_text = NULL;
00106        if( rs->sr_err != 0 ) {
00107               Debug( LDAP_DEBUG_TRACE,
00108                      LDAP_XSTRING(mdb_delete) ": txn_begin failed: "
00109                      "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
00110               rs->sr_err = LDAP_OTHER;
00111               rs->sr_text = "internal error";
00112               goto return_results;
00113        }
00114 
00115        txn = moi->moi_txn;
00116 
00117        if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
00118               dnParent( &op->o_req_ndn, &pdn );
00119        }
00120 
00121        rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mc );
00122        if ( rs->sr_err ) {
00123               rs->sr_err = LDAP_OTHER;
00124               rs->sr_text = "internal error";
00125               goto return_results;
00126        }
00127        /* get parent */
00128        rs->sr_err = mdb_dn2entry( op, txn, mc, &pdn, &p, 1 );
00129        switch( rs->sr_err ) {
00130        case 0:
00131        case MDB_NOTFOUND:
00132               break;
00133        case LDAP_BUSY:
00134               rs->sr_text = "ldap server busy";
00135               goto return_results;
00136        default:
00137               rs->sr_err = LDAP_OTHER;
00138               rs->sr_text = "internal error";
00139               goto return_results;
00140        }
00141        if ( rs->sr_err == MDB_NOTFOUND ) {
00142               Debug( LDAP_DEBUG_ARGS,
00143                      "<=- " LDAP_XSTRING(mdb_delete) ": no such object %s\n",
00144                      op->o_req_dn.bv_val, 0, 0);
00145 
00146               if ( p && !BER_BVISEMPTY( &p->e_name )) {
00147                      rs->sr_matched = ch_strdup( p->e_name.bv_val );
00148                      if ( is_entry_referral( p )) {
00149                             BerVarray ref = get_entry_referrals( op, p );
00150                             rs->sr_ref = referral_rewrite( ref, &p->e_name,
00151                                    &op->o_req_dn, LDAP_SCOPE_DEFAULT );
00152                             ber_bvarray_free( ref );
00153                      } else {
00154                             rs->sr_ref = NULL;
00155                      }
00156               } else {
00157                      rs->sr_ref = referral_rewrite( default_referral, NULL,
00158                                    &op->o_req_dn, LDAP_SCOPE_DEFAULT );
00159               }
00160               if ( p ) {
00161                      mdb_entry_return( op, p );
00162                      p = NULL;
00163               }
00164 
00165               rs->sr_err = LDAP_REFERRAL;
00166               rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
00167               goto return_results;
00168        }
00169 
00170        /* get entry */
00171        rs->sr_err = mdb_dn2entry( op, txn, mc, &op->o_req_ndn, &e, 0 );
00172        switch( rs->sr_err ) {
00173        case MDB_NOTFOUND:
00174               e = p;
00175               p = NULL;
00176        case 0:
00177               break;
00178        case LDAP_BUSY:
00179               rs->sr_text = "ldap server busy";
00180               goto return_results;
00181        default:
00182               rs->sr_err = LDAP_OTHER;
00183               rs->sr_text = "internal error";
00184               goto return_results;
00185        }
00186 
00187        /* FIXME : dn2entry() should return non-glue entry */
00188        if ( rs->sr_err == MDB_NOTFOUND || ( !manageDSAit && is_entry_glue( e ))) {
00189               Debug( LDAP_DEBUG_ARGS,
00190                      "<=- " LDAP_XSTRING(mdb_delete) ": no such object %s\n",
00191                      op->o_req_dn.bv_val, 0, 0);
00192 
00193               rs->sr_matched = ch_strdup( e->e_dn );
00194               if ( is_entry_referral( e )) {
00195                      BerVarray ref = get_entry_referrals( op, e );
00196                      rs->sr_ref = referral_rewrite( ref, &e->e_name,
00197                             &op->o_req_dn, LDAP_SCOPE_DEFAULT );
00198                      ber_bvarray_free( ref );
00199               } else {
00200                      rs->sr_ref = NULL;
00201               }
00202               mdb_entry_return( op, e );
00203               e = NULL;
00204 
00205               rs->sr_err = LDAP_REFERRAL;
00206               rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
00207               goto return_results;
00208        }
00209 
00210        if ( pdn.bv_len != 0 ) {
00211               /* check parent for "children" acl */
00212               rs->sr_err = access_allowed( op, p,
00213                      children, NULL, ACL_WDEL, NULL );
00214 
00215               if ( !rs->sr_err  ) {
00216                      Debug( LDAP_DEBUG_TRACE,
00217                             "<=- " LDAP_XSTRING(mdb_delete) ": no write "
00218                             "access to parent\n", 0, 0, 0 );
00219                      rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00220                      rs->sr_text = "no write access to parent";
00221                      goto return_results;
00222               }
00223 
00224        } else {
00225               /* no parent, must be root to delete */
00226               if( ! be_isroot( op ) ) {
00227                      if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
00228                             || be_shadow_update( op ) ) {
00229                             p = (Entry *)&slap_entry_root;
00230 
00231                             /* check parent for "children" acl */
00232                             rs->sr_err = access_allowed( op, p,
00233                                    children, NULL, ACL_WDEL, NULL );
00234 
00235                             p = NULL;
00236 
00237                             if ( !rs->sr_err  ) {
00238                                    Debug( LDAP_DEBUG_TRACE,
00239                                           "<=- " LDAP_XSTRING(mdb_delete)
00240                                           ": no access to parent\n",
00241                                           0, 0, 0 );
00242                                    rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00243                                    rs->sr_text = "no write access to parent";
00244                                    goto return_results;
00245                             }
00246 
00247                      } else {
00248                             Debug( LDAP_DEBUG_TRACE,
00249                                    "<=- " LDAP_XSTRING(mdb_delete)
00250                                    ": no parent and not root\n", 0, 0, 0 );
00251                             rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00252                             goto return_results;
00253                      }
00254               }
00255        }
00256 
00257        if ( get_assert( op ) &&
00258               ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
00259        {
00260               rs->sr_err = LDAP_ASSERTION_FAILED;
00261               goto return_results;
00262        }
00263 
00264        rs->sr_err = access_allowed( op, e,
00265               entry, NULL, ACL_WDEL, NULL );
00266 
00267        if ( !rs->sr_err  ) {
00268               Debug( LDAP_DEBUG_TRACE,
00269                      "<=- " LDAP_XSTRING(mdb_delete) ": no write access "
00270                      "to entry\n", 0, 0, 0 );
00271               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00272               rs->sr_text = "no write access to entry";
00273               goto return_results;
00274        }
00275 
00276        if ( !manageDSAit && is_entry_referral( e ) ) {
00277               /* entry is a referral, don't allow delete */
00278               rs->sr_ref = get_entry_referrals( op, e );
00279 
00280               Debug( LDAP_DEBUG_TRACE,
00281                      LDAP_XSTRING(mdb_delete) ": entry is referral\n",
00282                      0, 0, 0 );
00283 
00284               rs->sr_err = LDAP_REFERRAL;
00285               rs->sr_matched = ch_strdup( e->e_name.bv_val );
00286               rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
00287               goto return_results;
00288        }
00289 
00290        /* pre-read */
00291        if( op->o_preread ) {
00292               if( preread_ctrl == NULL ) {
00293                      preread_ctrl = &ctrls[num_ctrls++];
00294                      ctrls[num_ctrls] = NULL;
00295               }
00296               if( slap_read_controls( op, rs, e,
00297                      &slap_pre_read_bv, preread_ctrl ) )
00298               {
00299                      Debug( LDAP_DEBUG_TRACE,
00300                             "<=- " LDAP_XSTRING(mdb_delete) ": pre-read "
00301                             "failed!\n", 0, 0, 0 );
00302                      if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
00303                             /* FIXME: is it correct to abort
00304                              * operation if control fails? */
00305                             goto return_results;
00306                      }
00307               }
00308        }
00309 
00310        rs->sr_text = NULL;
00311 
00312        /* Can't do it if we have kids */
00313        rs->sr_err = mdb_dn2id_children( op, txn, e );
00314        if( rs->sr_err != MDB_NOTFOUND ) {
00315               switch( rs->sr_err ) {
00316               case 0:
00317                      Debug(LDAP_DEBUG_ARGS,
00318                             "<=- " LDAP_XSTRING(mdb_delete)
00319                             ": non-leaf %s\n",
00320                             op->o_req_dn.bv_val, 0, 0);
00321                      rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
00322                      rs->sr_text = "subordinate objects must be deleted first";
00323                      break;
00324               default:
00325                      Debug(LDAP_DEBUG_ARGS,
00326                             "<=- " LDAP_XSTRING(mdb_delete)
00327                             ": has_children failed: %s (%d)\n",
00328                             mdb_strerror(rs->sr_err), rs->sr_err, 0 );
00329                      rs->sr_err = LDAP_OTHER;
00330                      rs->sr_text = "internal error";
00331               }
00332               goto return_results;
00333        }
00334 
00335        /* delete from dn2id */
00336        rs->sr_err = mdb_dn2id_delete( op, mc, e->e_id );
00337        mdb_cursor_close( mc );
00338        if ( rs->sr_err != 0 ) {
00339               Debug(LDAP_DEBUG_TRACE,
00340                      "<=- " LDAP_XSTRING(mdb_delete) ": dn2id failed: "
00341                      "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
00342               rs->sr_text = "DN index delete failed";
00343               rs->sr_err = LDAP_OTHER;
00344               goto return_results;
00345        }
00346 
00347        /* delete indices for old attributes */
00348        rs->sr_err = mdb_index_entry_del( op, txn, e );
00349        if ( rs->sr_err != LDAP_SUCCESS ) {
00350               Debug(LDAP_DEBUG_TRACE,
00351                      "<=- " LDAP_XSTRING(mdb_delete) ": index failed: "
00352                      "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
00353               rs->sr_text = "entry index delete failed";
00354               rs->sr_err = LDAP_OTHER;
00355               goto return_results;
00356        }
00357 
00358        /* fixup delete CSN */
00359        if ( !SLAP_SHADOW( op->o_bd )) {
00360               struct berval vals[2];
00361 
00362               assert( !BER_BVISNULL( &op->o_csn ) );
00363               vals[0] = op->o_csn;
00364               BER_BVZERO( &vals[1] );
00365               rs->sr_err = mdb_index_values( op, txn, slap_schema.si_ad_entryCSN,
00366                      vals, 0, SLAP_INDEX_ADD_OP );
00367               if ( rs->sr_err != LDAP_SUCCESS ) {
00368                      rs->sr_text = "entryCSN index update failed";
00369                      rs->sr_err = LDAP_OTHER;
00370                      goto return_results;
00371               }
00372        }
00373 
00374        /* delete from id2entry */
00375        rs->sr_err = mdb_id2entry_delete( op->o_bd, txn, e );
00376        if ( rs->sr_err != 0 ) {
00377               Debug( LDAP_DEBUG_TRACE,
00378                      "<=- " LDAP_XSTRING(mdb_delete) ": id2entry failed: "
00379                      "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
00380               rs->sr_text = "entry delete failed";
00381               rs->sr_err = LDAP_OTHER;
00382               goto return_results;
00383        }
00384 
00385        if ( pdn.bv_len != 0 ) {
00386               parent_is_glue = is_entry_glue(p);
00387               rs->sr_err = mdb_dn2id_children( op, txn, p );
00388               if ( rs->sr_err != MDB_NOTFOUND ) {
00389                      switch( rs->sr_err ) {
00390                      case 0:
00391                             break;
00392                      default:
00393                             Debug(LDAP_DEBUG_ARGS,
00394                                    "<=- " LDAP_XSTRING(mdb_delete)
00395                                    ": has_children failed: %s (%d)\n",
00396                                    mdb_strerror(rs->sr_err), rs->sr_err, 0 );
00397                             rs->sr_err = LDAP_OTHER;
00398                             rs->sr_text = "internal error";
00399                             goto return_results;
00400                      }
00401                      parent_is_leaf = 1;
00402               }
00403               mdb_entry_return( op, p );
00404               p = NULL;
00405        }
00406 
00407        if( moi == &opinfo ) {
00408               LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
00409               opinfo.moi_oe.oe_key = NULL;
00410               if( op->o_noop ) {
00411                      mdb_txn_abort( txn );
00412                      rs->sr_err = LDAP_X_NO_OPERATION;
00413                      txn = NULL;
00414                      goto return_results;
00415               } else {
00416                      rs->sr_err = mdb_txn_commit( txn );
00417               }
00418               txn = NULL;
00419        }
00420 
00421        if( rs->sr_err != 0 ) {
00422               Debug( LDAP_DEBUG_TRACE,
00423                      LDAP_XSTRING(mdb_delete) ": txn_%s failed: %s (%d)\n",
00424                      op->o_noop ? "abort (no-op)" : "commit",
00425                      mdb_strerror(rs->sr_err), rs->sr_err );
00426               rs->sr_err = LDAP_OTHER;
00427               rs->sr_text = "commit failed";
00428 
00429               goto return_results;
00430        }
00431 
00432        Debug( LDAP_DEBUG_TRACE,
00433               LDAP_XSTRING(mdb_delete) ": deleted%s id=%08lx dn=\"%s\"\n",
00434               op->o_noop ? " (no-op)" : "",
00435               e->e_id, op->o_req_dn.bv_val );
00436        rs->sr_err = LDAP_SUCCESS;
00437        rs->sr_text = NULL;
00438        if( num_ctrls ) rs->sr_ctrls = ctrls;
00439 
00440 return_results:
00441        if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
00442               op->o_delete_glue_parent = 1;
00443        }
00444 
00445        if ( p != NULL ) {
00446               mdb_entry_return( op, p );
00447        }
00448 
00449        /* free entry */
00450        if( e != NULL ) {
00451               mdb_entry_return( op, e );
00452        }
00453 
00454        if( moi == &opinfo ) {
00455               if( txn != NULL ) {
00456                      mdb_txn_abort( txn );
00457               }
00458               if ( opinfo.moi_oe.oe_key ) {
00459                      LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
00460               }
00461        }
00462 
00463        send_ldap_result( op, rs );
00464        slap_graduate_commit_csn( op );
00465 
00466        if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
00467               slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
00468               slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
00469        }
00470 
00471 #if 0
00472        if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
00473               TXN_CHECKPOINT( mdb->bi_dbenv,
00474                      mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
00475        }
00476 #endif
00477        return rs->sr_err;
00478 }