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