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