]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Fix previous commit
[openldap] / servers / slapd / back-bdb / delete.c
1 /* delete.c - bdb backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_delete(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         struct berval   *dn,
22         struct berval   *ndn
23 )
24 {
25         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26         Entry   *matched;
27         struct berval   pdn = {0, NULL};
28         Entry   *e, *p = NULL;
29         int     rc;
30         const char *text;
31         int             manageDSAit = get_manageDSAit( op );
32         AttributeDescription *children = slap_schema.si_ad_children;
33         DB_TXN          *ltid = NULL;
34         struct bdb_op_info opinfo;
35 #if 0
36         u_int32_t       lockid;
37         DB_LOCK         lock;
38 #endif
39
40         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
41                 dn->bv_val, 0, 0 );
42
43         if( 0 ) {
44 retry:  /* transaction retry */
45                 if( e != NULL ) {
46                         bdb_cache_return_entry_w(&bdb->bi_cache, e);
47                 }
48                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
49                         0, 0, 0 );
50                 rc = txn_abort( ltid );
51                 ltid = NULL;
52                 op->o_private = NULL;
53                 if( rc != 0 ) {
54                         rc = LDAP_OTHER;
55                         text = "internal error";
56                         goto return_results;
57                 }
58                 ldap_pvt_thread_yield();
59         }
60
61         if( bdb->bi_txn ) {
62                 /* begin transaction */
63                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
64                         bdb->bi_db_opflags );
65                 text = NULL;
66                 if( rc != 0 ) {
67                         Debug( LDAP_DEBUG_TRACE,
68                                 "bdb_delete: txn_begin failed: %s (%d)\n",
69                                 db_strerror(rc), rc, 0 );
70                         rc = LDAP_OTHER;
71                         text = "internal error";
72                         goto return_results;
73                 }
74 #if 0
75                 lockid = TXN_ID( ltid );
76 #endif
77         }
78
79         opinfo.boi_bdb = be;
80         opinfo.boi_txn = ltid;
81         opinfo.boi_err = 0;
82         op->o_private = &opinfo;
83
84         if ( !be_issuffix( be, ndn ) ) {
85                 rc = dnParent( ndn, &pdn );
86                 if ( rc != LDAP_SUCCESS ) {
87                         text = "internal error";
88                         goto return_results;
89                 }
90         }
91
92         if( pdn.bv_len != 0 ) {
93 #if 0
94                 if ( ltid ) {
95                         DBT obj;
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);
100                 }
101 #endif
102                 /* get parent */
103                 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0 );
104
105                 switch( rc ) {
106                 case 0:
107                 case DB_NOTFOUND:
108                         break;
109                 case DB_LOCK_DEADLOCK:
110                 case DB_LOCK_NOTGRANTED:
111                         goto retry;
112                 default:
113                         rc = LDAP_OTHER;
114                         text = "internal error";
115                         goto return_results;
116                 }
117
118                 if( p == NULL) {
119                         Debug( LDAP_DEBUG_TRACE,
120                                 "<=- bdb_delete: parent does not exist\n",
121                                 0, 0, 0);
122                         rc = LDAP_OTHER;
123                         text = "could not locate parent of entry";
124                         goto return_results;
125                 }
126
127                 /* check parent for "children" acl */
128                 rc = access_allowed( be, conn, op, p,
129                         children, NULL, ACL_WRITE );
130
131                 bdb_cache_return_entry_r(&bdb->bi_cache, p);
132                 p = NULL;
133
134                 switch( opinfo.boi_err ) {
135                 case DB_LOCK_DEADLOCK:
136                 case DB_LOCK_NOTGRANTED:
137                         goto retry;
138                 }
139
140                 if ( !rc  ) {
141                         Debug( LDAP_DEBUG_TRACE,
142                                 "<=- bdb_delete: no access to parent\n",
143                                 0, 0, 0 );
144                         rc = LDAP_INSUFFICIENT_ACCESS;
145                         goto return_results;
146                 }
147
148         } else {
149                 /* no parent, must be root to delete */
150                 if( ! be_isroot( be, &op->o_ndn ) ) {
151                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
152                                 || be_isupdate( be, &op->o_ndn ) ) {
153                                 p = (Entry *)&slap_entry_root;
154
155                                 /* check parent for "children" acl */
156                                 rc = access_allowed( be, conn, op, p,
157                                         children, NULL, ACL_WRITE );
158                                 p = NULL;
159
160                                 switch( opinfo.boi_err ) {
161                                 case DB_LOCK_DEADLOCK:
162                                 case DB_LOCK_NOTGRANTED:
163                                         goto retry;
164                                 }
165
166                                 if ( !rc  ) {
167                                         Debug( LDAP_DEBUG_TRACE,
168                                                 "<=- bdb_delete: no access "
169                                                 "to parent\n", 0, 0, 0 );
170                                         rc = LDAP_INSUFFICIENT_ACCESS;
171                                         goto return_results;
172                                 }
173
174                         } else {
175                                 Debug( LDAP_DEBUG_TRACE,
176                                         "<=- bdb_delete: no parent "
177                                         "and not root\n", 0, 0, 0);
178                                 rc = LDAP_INSUFFICIENT_ACCESS;
179                                 goto return_results;
180                         }
181                 }
182
183 #if 0
184                 if ( ltid ) {
185                         DBT obj;
186                         obj.data = ",";
187                         obj.size = 1;
188                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
189                                 DB_LOCK_WRITE, &lock);
190                 }
191 #endif
192         }
193
194         /* get entry for read/modify/write */
195         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW );
196
197         switch( rc ) {
198         case 0:
199         case DB_NOTFOUND:
200                 break;
201         case DB_LOCK_DEADLOCK:
202         case DB_LOCK_NOTGRANTED:
203                 goto retry;
204         default:
205                 rc = LDAP_OTHER;
206                 text = "internal error";
207                 goto return_results;
208         }
209
210         if ( e == NULL ) {
211                 char *matched_dn = NULL;
212                 BerVarray refs;
213
214                 Debug( LDAP_DEBUG_ARGS,
215                         "<=- bdb_delete: no such object %s\n",
216                         dn->bv_val, 0, 0);
217
218                 if ( matched != NULL ) {
219                         matched_dn = ch_strdup( matched->e_dn );
220                         refs = is_entry_referral( matched )
221                                 ? get_entry_referrals( be, conn, op, matched )
222                                 : NULL;
223                         bdb_cache_return_entry_r(&bdb->bi_cache, matched );
224                         matched = NULL;
225
226                 } else {
227                         refs = referral_rewrite( default_referral,
228                                 NULL, dn, LDAP_SCOPE_DEFAULT );
229                 }
230
231                 send_ldap_result( conn, op, LDAP_REFERRAL,
232                         matched_dn, NULL, refs, NULL );
233
234                 ber_bvarray_free( refs );
235                 free( matched_dn );
236
237                 rc = -1;
238                 goto done;
239         }
240
241         if ( !manageDSAit && is_entry_referral( e ) ) {
242                 /* parent is a referral, don't allow add */
243                 /* parent is an alias, don't allow add */
244                 BerVarray refs = get_entry_referrals( be,
245                         conn, op, e );
246
247                 Debug( LDAP_DEBUG_TRACE,
248                         "bdb_delete: entry is referral\n",
249                         0, 0, 0 );
250
251                 send_ldap_result( conn, op, LDAP_REFERRAL,
252                         e->e_dn, NULL, refs, NULL );
253
254                 ber_bvarray_free( refs );
255
256                 rc = 1;
257                 goto done;
258         }
259
260         rc = bdb_dn2id_children( be, ltid, &e->e_nname );
261         if( rc != DB_NOTFOUND ) {
262                 switch( rc ) {
263                 case DB_LOCK_DEADLOCK:
264                 case DB_LOCK_NOTGRANTED:
265                         goto retry;
266                 case 0:
267                         Debug(LDAP_DEBUG_ARGS,
268                                 "<=- bdb_delete: non-leaf %s\n",
269                                 dn->bv_val, 0, 0);
270                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
271                         text = "subtree delete not supported";
272                         break;
273                 default:
274                         Debug(LDAP_DEBUG_ARGS,
275                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
276                                 db_strerror(rc), rc, 0 );
277                         rc = LDAP_OTHER;
278                         text = "internal error";
279                 }
280                 goto return_results;
281         }
282
283         /* delete from dn2id */
284         rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
285         if ( rc != 0 ) {
286                 switch( rc ) {
287                 case DB_LOCK_DEADLOCK:
288                 case DB_LOCK_NOTGRANTED:
289                         goto retry;
290                 default:
291                         rc = LDAP_OTHER;
292                 }
293                 Debug(LDAP_DEBUG_ARGS,
294                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
295                         db_strerror(rc), rc, 0 );
296                 text = "DN index delete failed";
297                 goto return_results;
298         }
299
300         /* delete from id2entry */
301         rc = bdb_id2entry_delete( be, ltid, e );
302         if ( rc != 0 ) {
303                 switch( rc ) {
304                 case DB_LOCK_DEADLOCK:
305                 case DB_LOCK_NOTGRANTED:
306                         goto retry;
307                 default:
308                         rc = LDAP_OTHER;
309                 }
310                 Debug(LDAP_DEBUG_ARGS,
311                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
312                         db_strerror(rc), rc, 0 );
313                 text = "entry delete failed";
314                 goto return_results;
315         }
316
317         /* delete indices for old attributes */
318         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
319         if ( rc != LDAP_SUCCESS ) {
320                 switch( rc ) {
321                 case DB_LOCK_DEADLOCK:
322                 case DB_LOCK_NOTGRANTED:
323                         goto retry;
324                 default:
325                         rc = LDAP_OTHER;
326                 }
327                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
328                         0, 0, 0 );
329                 text = "entry index delete failed";
330                 goto return_results;
331         }
332
333 #if 0   /* Do we want to reclaim deleted IDs? */
334         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
335         if ( e->e_id == bdb->bi_lastid ) {
336                 bdb_last_id( be, ltid );
337         }
338         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
339 #endif
340
341         if( bdb->bi_txn ) {
342                 rc = txn_commit( ltid, 0 );
343         }
344         ltid = NULL;
345         op->o_private = NULL;
346
347         if( rc != 0 ) {
348                 Debug( LDAP_DEBUG_TRACE,
349                         "bdb_delete: txn_commit failed: %s (%d)\n",
350                         db_strerror(rc), rc, 0 );
351                 rc = LDAP_OTHER;
352                 text = "commit failed";
353
354         } else {
355                 Debug( LDAP_DEBUG_TRACE,
356                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
357                         e->e_id, e->e_dn, 0 );
358                 rc = LDAP_SUCCESS;
359                 text = NULL;
360         }
361
362 return_results:
363         send_ldap_result( conn, op, LDAP_SUCCESS,
364                 NULL, text, NULL, NULL );
365
366         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
367                 ldap_pvt_thread_yield();
368                 TXN_CHECKPOINT( bdb->bi_dbenv,
369                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
370         }
371
372 done:
373         /* free entry */
374         if( e != NULL ) {
375                 bdb_cache_return_entry_w(&bdb->bi_cache, e);
376         }
377
378         if( ltid != NULL ) {
379                 txn_abort( ltid );
380                 op->o_private = NULL;
381         }
382
383         return rc;
384 }