1 /* delete.c - bdb backend delete routine */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
25 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
27 struct berval pdn = {0, NULL};
31 int manageDSAit = get_manageDSAit( op );
32 AttributeDescription *children = slap_schema.si_ad_children;
34 struct bdb_op_info opinfo;
40 Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
44 retry: /* transaction retry */
45 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
47 rc = txn_abort( ltid );
52 text = "internal error";
55 ldap_pvt_thread_yield();
59 /* begin transaction */
60 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
64 Debug( LDAP_DEBUG_TRACE,
65 "bdb_delete: txn_begin failed: %s (%d)\n",
66 db_strerror(rc), rc, 0 );
68 text = "internal error";
72 lockid = TXN_ID( ltid );
77 opinfo.boi_txn = ltid;
79 op->o_private = &opinfo;
81 if ( !be_issuffix( be, ndn->bv_val ) ) {
82 rc = dnParent( ndn->bv_val, &pdn.bv_val );
83 if ( rc != LDAP_SUCCESS ) {
84 text = "internal error";
87 if (pdn.bv_val && pdn.bv_val[0]) {
88 pdn.bv_len = ndn->bv_len - (pdn.bv_val - ndn->bv_val);
92 if( pdn.bv_len != 0 ) {
96 obj.data = pdn.bv_val-1;
97 obj.size = pdn.bv_len+1;
98 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
99 DB_LOCK_WRITE, &lock);
103 rc = bdb_dn2entry( be, ltid, &pdn, &p, NULL, 0 );
109 case DB_LOCK_DEADLOCK:
110 case DB_LOCK_NOTGRANTED:
114 text = "internal error";
119 Debug( LDAP_DEBUG_TRACE,
120 "<=- bdb_delete: parent does not exist\n",
123 text = "could not locate parent of entry";
127 /* check parent for "children" acl */
128 rc = access_allowed( be, conn, op, p,
129 children, NULL, ACL_WRITE );
131 bdb_entry_return( be, p );
134 switch( opinfo.boi_err ) {
135 case DB_LOCK_DEADLOCK:
136 case DB_LOCK_NOTGRANTED:
141 Debug( LDAP_DEBUG_TRACE,
142 "<=- bdb_delete: no access to parent\n",
144 rc = LDAP_INSUFFICIENT_ACCESS;
149 /* no parent, must be root to delete */
150 if( ! be_isroot( be, &op->o_ndn ) ) {
151 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
152 p = (Entry *)&slap_entry_root;
154 /* check parent for "children" acl */
155 rc = access_allowed( be, conn, op, p,
156 children, NULL, ACL_WRITE );
159 switch( opinfo.boi_err ) {
160 case DB_LOCK_DEADLOCK:
161 case DB_LOCK_NOTGRANTED:
166 Debug( LDAP_DEBUG_TRACE,
167 "<=- bdb_delete: no access "
168 "to parent\n", 0, 0, 0 );
169 rc = LDAP_INSUFFICIENT_ACCESS;
174 Debug( LDAP_DEBUG_TRACE,
175 "<=- bdb_delete: no parent "
176 "and not root\n", 0, 0, 0);
177 rc = LDAP_INSUFFICIENT_ACCESS;
187 rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
188 DB_LOCK_WRITE, &lock);
193 /* get entry for read/modify/write */
194 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
200 case DB_LOCK_DEADLOCK:
201 case DB_LOCK_NOTGRANTED:
205 text = "internal error";
210 char *matched_dn = NULL;
213 Debug( LDAP_DEBUG_ARGS,
214 "<=- bdb_delete: no such object %s\n",
217 if ( matched != NULL ) {
218 matched_dn = ch_strdup( matched->e_dn );
219 refs = is_entry_referral( matched )
220 ? get_entry_referrals( be, conn, op, matched )
222 bdb_entry_return( be, matched );
226 refs = referral_rewrite( default_referral,
227 NULL, dn, LDAP_SCOPE_DEFAULT );
230 send_ldap_result( conn, op, LDAP_REFERRAL,
231 matched_dn, NULL, refs, NULL );
233 ber_bvarray_free( refs );
240 if ( !manageDSAit && is_entry_referral( e ) ) {
241 /* parent is a referral, don't allow add */
242 /* parent is an alias, don't allow add */
243 BerVarray refs = get_entry_referrals( be,
246 Debug( LDAP_DEBUG_TRACE,
247 "bdb_delete: entry is referral\n",
250 send_ldap_result( conn, op, LDAP_REFERRAL,
251 e->e_dn, NULL, refs, NULL );
253 ber_bvarray_free( refs );
259 rc = bdb_dn2id_children( be, ltid, &e->e_nname );
260 if( rc != DB_NOTFOUND ) {
262 case DB_LOCK_DEADLOCK:
263 case DB_LOCK_NOTGRANTED:
266 Debug(LDAP_DEBUG_ARGS,
267 "<=- bdb_delete: non-leaf %s\n",
269 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
270 text = "subtree delete not supported";
273 Debug(LDAP_DEBUG_ARGS,
274 "<=- bdb_delete: has_children failed: %s (%d)\n",
275 db_strerror(rc), rc, 0 );
277 text = "internal error";
282 /* delete from dn2id */
283 rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
286 case DB_LOCK_DEADLOCK:
287 case DB_LOCK_NOTGRANTED:
292 Debug(LDAP_DEBUG_ARGS,
293 "<=- bdb_delete: dn2id failed: %s (%d)\n",
294 db_strerror(rc), rc, 0 );
295 text = "DN index delete failed";
299 /* delete from id2entry */
300 rc = bdb_id2entry_delete( be, ltid, e->e_id );
303 case DB_LOCK_DEADLOCK:
304 case DB_LOCK_NOTGRANTED:
309 Debug(LDAP_DEBUG_ARGS,
310 "<=- bdb_delete: id2entry failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
312 text = "entry delete failed";
316 /* delete indices for old attributes */
317 rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
318 if ( rc != LDAP_SUCCESS ) {
320 case DB_LOCK_DEADLOCK:
321 case DB_LOCK_NOTGRANTED:
326 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
328 text = "entry index delete failed";
332 #if 0 /* Do we want to reclaim deleted IDs? */
333 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
334 if ( e->e_id == bdb->bi_lastid ) {
335 bdb_last_id( be, ltid );
337 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
341 rc = txn_commit( ltid, 0 );
344 op->o_private = NULL;
347 Debug( LDAP_DEBUG_TRACE,
348 "bdb_delete: txn_commit failed: %s (%d)\n",
349 db_strerror(rc), rc, 0 );
351 text = "commit failed";
354 Debug( LDAP_DEBUG_TRACE,
355 "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
356 e->e_id, e->e_dn, 0 );
362 send_ldap_result( conn, op, LDAP_SUCCESS,
363 NULL, text, NULL, NULL );
365 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
366 ldap_pvt_thread_yield();
367 TXN_CHECKPOINT( bdb->bi_dbenv,
368 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
374 bdb_entry_return( be, e );
379 op->o_private = NULL;