]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Added experimental support for DB_DIRTY_READ.
[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                 ch_free( pdn );
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                 ch_free( pdn );
166
167                 /* no parent, must be root to delete */
168                 if( ! be_isroot( be, op->o_ndn ) ) {
169                         if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
170                                 p = (Entry *)&slap_entry_root;
171
172                                 /* check parent for "children" acl */
173                                 rc = access_allowed( be, conn, op, p,
174                                         children, NULL, ACL_WRITE );
175                                 p = NULL;
176
177                                 if ( !rc  ) {
178                                         Debug( LDAP_DEBUG_TRACE,
179                                                 "<=- bdb_delete: no access "
180                                                 "to parent\n", 0, 0, 0 );
181                                         rc = LDAP_INSUFFICIENT_ACCESS;
182                                         goto return_results;
183                                 }
184
185                         } else {
186                                 Debug( LDAP_DEBUG_TRACE,
187                                         "<=- bdb_delete: no parent "
188                                         "and not root\n", 0, 0, 0);
189                                 rc = LDAP_INSUFFICIENT_ACCESS;
190                                 goto return_results;
191                         }
192                 }
193         }
194
195         if ( !manageDSAit && is_entry_referral( e ) ) {
196                 /* parent is a referral, don't allow add */
197                 /* parent is an alias, don't allow add */
198                 struct berval **refs = get_entry_referrals( be,
199                         conn, op, e, dn, LDAP_SCOPE_DEFAULT );
200
201                 Debug( LDAP_DEBUG_TRACE,
202                         "bdb_delete: entry is referral\n",
203                         0, 0, 0 );
204
205                 send_ldap_result( conn, op, LDAP_REFERRAL,
206                         e->e_dn, NULL, refs, NULL );
207
208                 ber_bvecfree( refs );
209
210                 rc = 1;
211                 goto done;
212         }
213
214         rc = bdb_dn2id_children( be, ltid, e->e_ndn );
215         if( rc != DB_NOTFOUND ) {
216                 switch( rc ) {
217                 case DB_LOCK_DEADLOCK:
218                 case DB_LOCK_NOTGRANTED:
219                         goto retry;
220                 case 0:
221                         Debug(LDAP_DEBUG_ARGS,
222                                 "<=- bdb_delete: non-leaf %s\n",
223                                 dn, 0, 0);
224                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
225                         text = "subtree delete not supported";
226                         break;
227                 default:
228                         Debug(LDAP_DEBUG_ARGS,
229                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
230                                 db_strerror(rc), rc, 0 );
231                         rc = LDAP_OTHER;
232                         text = "internal error";
233                 }
234                 goto return_results;
235         }
236
237         /* delete from dn2id */
238         rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
239         if ( rc != 0 ) {
240                 switch( rc ) {
241                 case DB_LOCK_DEADLOCK:
242                 case DB_LOCK_NOTGRANTED:
243                         goto retry;
244                 default:
245                         rc = LDAP_OTHER;
246                 }
247                 Debug(LDAP_DEBUG_ARGS,
248                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
249                         db_strerror(rc), rc, 0 );
250                 text = "DN index delete failed";
251                 goto return_results;
252         }
253
254         /* delete indices for old attributes */
255         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
256         if ( rc != LDAP_SUCCESS ) {
257                 switch( rc ) {
258                 case DB_LOCK_DEADLOCK:
259                 case DB_LOCK_NOTGRANTED:
260                         goto retry;
261                 default:
262                         rc = LDAP_OTHER;
263                 }
264                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
265                         0, 0, 0 );
266                 text = "entry index delete failed";
267                 goto return_results;
268         }
269
270         /* delete from id2entry */
271         rc = bdb_id2entry_delete( be, ltid, e->e_id );
272         if ( rc != 0 ) {
273                 switch( rc ) {
274                 case DB_LOCK_DEADLOCK:
275                 case DB_LOCK_NOTGRANTED:
276                         goto retry;
277                 default:
278                         rc = LDAP_OTHER;
279                 }
280                 Debug(LDAP_DEBUG_ARGS,
281                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
282                         db_strerror(rc), rc, 0 );
283                 text = "entry delete failed";
284                 goto return_results;
285         }
286
287         if( bdb->bi_txn ) {
288                 rc = txn_commit( ltid, 0 );
289         }
290         ltid = NULL;
291         op->o_private = NULL;
292
293         if( rc != 0 ) {
294                 Debug( LDAP_DEBUG_TRACE,
295                         "bdb_delete: txn_commit failed: %s (%d)\n",
296                         db_strerror(rc), rc, 0 );
297                 rc = LDAP_OTHER;
298                 text = "commit failed";
299
300         } else {
301                 Debug( LDAP_DEBUG_TRACE,
302                         "bdb_delete: deleted id=%08lx dn=\"%s\"\n",
303                         e->e_id, e->e_dn, 0 );
304                 rc = LDAP_SUCCESS;
305                 text = NULL;
306         }
307
308 return_results:
309         send_ldap_result( conn, op, LDAP_SUCCESS,
310                 NULL, text, NULL, NULL );
311
312         if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
313                 ldap_pvt_thread_yield();
314                 txn_checkpoint( bdb->bi_dbenv,
315                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
316         }
317
318 done:
319         /* free entry */
320         if( e != NULL ) {
321                 bdb_entry_return( be, e );
322         }
323
324         if( ltid != NULL ) {
325                 txn_abort( ltid );
326                 op->o_private = NULL;
327         }
328
329         return rc;
330 }