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