]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Round 2 of subentry changes
[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         char    *pdn = 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
36         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
37                 dn->bv_val, 0, 0 );
38
39         if( 0 ) {
40 retry:  /* transaction retry */
41                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
42                         0, 0, 0 );
43                 rc = txn_abort( ltid );
44                 ltid = NULL;
45                 op->o_private = NULL;
46                 if( rc != 0 ) {
47                         rc = LDAP_OTHER;
48                         text = "internal error";
49                         goto return_results;
50                 }
51         }
52
53         if( bdb->bi_txn ) {
54                 /* begin transaction */
55                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
56                         bdb->bi_db_opflags );
57                 text = NULL;
58                 if( rc != 0 ) {
59                         Debug( LDAP_DEBUG_TRACE,
60                                 "bdb_delete: txn_begin failed: %s (%d)\n",
61                                 db_strerror(rc), rc, 0 );
62                         rc = LDAP_OTHER;
63                         text = "internal error";
64                         goto return_results;
65                 }
66         }
67
68         opinfo.boi_bdb = be;
69         opinfo.boi_txn = ltid;
70         opinfo.boi_err = 0;
71         op->o_private = &opinfo;
72
73         /* get entry for read/modify/write */
74         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
75
76         switch( rc ) {
77         case 0:
78         case DB_NOTFOUND:
79                 break;
80         case DB_LOCK_DEADLOCK:
81         case DB_LOCK_NOTGRANTED:
82                 goto retry;
83         default:
84                 rc = LDAP_OTHER;
85                 text = "internal error";
86                 goto return_results;
87         }
88
89         if ( e == NULL ) {
90                 char *matched_dn = NULL;
91                 BVarray refs;
92
93                 Debug( LDAP_DEBUG_ARGS,
94                         "<=- bdb_delete: no such object %s\n",
95                         dn->bv_val, 0, 0);
96
97                 if ( matched != NULL ) {
98                         matched_dn = ch_strdup( matched->e_dn );
99                         refs = is_entry_referral( matched )
100                                 ? get_entry_referrals( be, conn, op, matched )
101                                 : NULL;
102                         bdb_entry_return( be, matched );
103                         matched = NULL;
104
105                 } else {
106                         refs = referral_rewrite( default_referral,
107                                 NULL, dn, LDAP_SCOPE_DEFAULT );
108                 }
109
110                 send_ldap_result( conn, op, LDAP_REFERRAL,
111                         matched_dn, NULL, refs, NULL );
112
113                 bvarray_free( refs );
114                 free( matched_dn );
115
116                 rc = -1;
117                 goto done;
118         }
119
120         pdn = dn_parent( be, ndn->bv_val );
121
122         if( pdn != NULL && *pdn != '\0' ) {
123                 struct berval pbv;
124
125                 pbv.bv_len = ndn->bv_len - (pdn - ndn->bv_val);
126                 pbv.bv_val = pdn;
127                 /* get parent */
128                 rc = bdb_dn2entry( be, ltid, &pbv, &p, NULL, 0 );
129
130                 switch( rc ) {
131                 case 0:
132                 case DB_NOTFOUND:
133                         break;
134                 case DB_LOCK_DEADLOCK:
135                 case DB_LOCK_NOTGRANTED:
136                         goto retry;
137                 default:
138                         rc = LDAP_OTHER;
139                         text = "internal error";
140                         goto return_results;
141                 }
142
143                 if( p == NULL) {
144                         Debug( LDAP_DEBUG_TRACE,
145                                 "<=- bdb_delete: parent does not exist\n",
146                                 0, 0, 0);
147                         rc = LDAP_OTHER;
148                         text = "could not locate parent of entry";
149                         goto return_results;
150                 }
151
152                 /* check parent for "children" acl */
153                 rc = access_allowed( be, conn, op, p,
154                         children, NULL, ACL_WRITE );
155
156                 bdb_entry_return( be, p );
157                 p = NULL;
158
159                 if ( !rc  ) {
160                         Debug( LDAP_DEBUG_TRACE,
161                                 "<=- bdb_delete: no access to parent\n",
162                                 0, 0, 0 );
163                         rc = LDAP_INSUFFICIENT_ACCESS;
164                         goto return_results;
165                 }
166
167         } else {
168                 /* no parent, must be root to delete */
169                 if( ! be_isroot( be, &op->o_ndn ) ) {
170                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
171                                 p = (Entry *)&slap_entry_root;
172
173                                 /* check parent for "children" acl */
174                                 rc = access_allowed( be, conn, op, p,
175                                         children, NULL, ACL_WRITE );
176                                 p = NULL;
177
178                                 if ( !rc  ) {
179                                         Debug( LDAP_DEBUG_TRACE,
180                                                 "<=- bdb_delete: no access "
181                                                 "to parent\n", 0, 0, 0 );
182                                         rc = LDAP_INSUFFICIENT_ACCESS;
183                                         goto return_results;
184                                 }
185
186                         } else {
187                                 Debug( LDAP_DEBUG_TRACE,
188                                         "<=- bdb_delete: no parent "
189                                         "and not root\n", 0, 0, 0);
190                                 rc = LDAP_INSUFFICIENT_ACCESS;
191                                 goto return_results;
192                         }
193                 }
194         }
195
196         if ( !manageDSAit && is_entry_referral( e ) ) {
197                 /* parent is a referral, don't allow add */
198                 /* parent is an alias, don't allow add */
199                 BVarray refs = get_entry_referrals( be,
200                         conn, op, e );
201
202                 Debug( LDAP_DEBUG_TRACE,
203                         "bdb_delete: entry is referral\n",
204                         0, 0, 0 );
205
206                 send_ldap_result( conn, op, LDAP_REFERRAL,
207                         e->e_dn, NULL, refs, NULL );
208
209                 bvarray_free( refs );
210
211                 rc = 1;
212                 goto done;
213         }
214
215         rc = bdb_dn2id_children( be, ltid, &e->e_nname );
216         if( rc != DB_NOTFOUND ) {
217                 switch( rc ) {
218                 case DB_LOCK_DEADLOCK:
219                 case DB_LOCK_NOTGRANTED:
220                         goto retry;
221                 case 0:
222                         Debug(LDAP_DEBUG_ARGS,
223                                 "<=- bdb_delete: non-leaf %s\n",
224                                 dn->bv_val, 0, 0);
225                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
226                         text = "subtree delete not supported";
227                         break;
228                 default:
229                         Debug(LDAP_DEBUG_ARGS,
230                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
231                                 db_strerror(rc), rc, 0 );
232                         rc = LDAP_OTHER;
233                         text = "internal error";
234                 }
235                 goto return_results;
236         }
237
238         /* delete from dn2id */
239         rc = bdb_dn2id_delete( be, ltid, pdn, e );
240         if ( rc != 0 ) {
241                 switch( rc ) {
242                 case DB_LOCK_DEADLOCK:
243                 case DB_LOCK_NOTGRANTED:
244                         goto retry;
245                 default:
246                         rc = LDAP_OTHER;
247                 }
248                 Debug(LDAP_DEBUG_ARGS,
249                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
250                         db_strerror(rc), rc, 0 );
251                 text = "DN index delete failed";
252                 goto return_results;
253         }
254
255         /* delete indices for old attributes */
256         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
257         if ( rc != LDAP_SUCCESS ) {
258                 switch( rc ) {
259                 case DB_LOCK_DEADLOCK:
260                 case DB_LOCK_NOTGRANTED:
261                         goto retry;
262                 default:
263                         rc = LDAP_OTHER;
264                 }
265                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
266                         0, 0, 0 );
267                 text = "entry index delete failed";
268                 goto return_results;
269         }
270
271         /* delete from id2entry */
272         rc = bdb_id2entry_delete( be, ltid, e->e_id );
273         if ( rc != 0 ) {
274                 switch( rc ) {
275                 case DB_LOCK_DEADLOCK:
276                 case DB_LOCK_NOTGRANTED:
277                         goto retry;
278                 default:
279                         rc = LDAP_OTHER;
280                 }
281                 Debug(LDAP_DEBUG_ARGS,
282                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
283                         db_strerror(rc), rc, 0 );
284                 text = "entry delete failed";
285                 goto return_results;
286         }
287
288
289 #if 0   /* Do we want to reclaim deleted IDs? */
290         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
291         if ( e->e_id == bdb->bi_lastid ) {
292                 bdb_last_id( be, ltid );
293         }
294         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
295 #endif
296
297         if( bdb->bi_txn ) {
298                 rc = txn_commit( ltid, 0 );
299         }
300         ltid = NULL;
301         op->o_private = NULL;
302
303         if( rc != 0 ) {
304                 Debug( LDAP_DEBUG_TRACE,
305                         "bdb_delete: txn_commit failed: %s (%d)\n",
306                         db_strerror(rc), rc, 0 );
307                 rc = LDAP_OTHER;
308                 text = "commit failed";
309
310         } else {
311                 Debug( LDAP_DEBUG_TRACE,
312                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
313                         e->e_id, e->e_dn, 0 );
314                 rc = LDAP_SUCCESS;
315                 text = NULL;
316         }
317
318 return_results:
319         send_ldap_result( conn, op, LDAP_SUCCESS,
320                 NULL, text, NULL, NULL );
321
322         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
323                 ldap_pvt_thread_yield();
324                 TXN_CHECKPOINT( bdb->bi_dbenv,
325                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
326         }
327
328 done:
329         /* free entry */
330         if( e != NULL ) {
331                 bdb_entry_return( be, e );
332         }
333
334         if( ltid != NULL ) {
335                 txn_abort( ltid );
336                 op->o_private = NULL;
337         }
338
339         return rc;
340 }