]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Use EntryInfo navigation for search scope checks
[openldap] / servers / slapd / back-bdb / delete.c
1 /* delete.c - bdb backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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( Operation *op, SlapReply *rs )
18 {
19         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
20         Entry   *matched;
21         struct berval   pdn = {0, NULL};
22         Entry   *e = NULL;
23         Entry   *p = NULL;
24         EntryInfo       *ei = NULL, *eip = NULL;
25         int             manageDSAit = get_manageDSAit( op );
26         AttributeDescription *children = slap_schema.si_ad_children;
27         AttributeDescription *entry = slap_schema.si_ad_entry;
28         DB_TXN          *ltid = NULL, *lt2;
29         struct bdb_op_info opinfo;
30
31         u_int32_t       locker = 0;
32         DB_LOCK         lock, plock;
33
34         int             noop = 0;
35
36 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
37         Operation* ps_list;
38 #endif
39
40 #ifdef NEW_LOGGING
41         LDAP_LOG ( OPERATION, ARGS,  "==> bdb_delete: %s\n", op->o_req_dn.bv_val, 0, 0 );
42 #else
43         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n",
44                 op->o_req_dn.bv_val, 0, 0 );
45 #endif
46
47         if( 0 ) {
48 retry:  /* transaction retry */
49                 if( e != NULL ) {
50                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
51                         e = NULL;
52                 }
53 #ifdef NEW_LOGGING
54                 LDAP_LOG ( OPERATION, DETAIL1, 
55                         "==> bdb_delete: retrying...\n", 0, 0, 0 );
56 #else
57                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n",
58                         0, 0, 0 );
59 #endif
60                 rs->sr_err = TXN_ABORT( ltid );
61                 ltid = NULL;
62                 op->o_private = NULL;
63                 op->o_do_not_cache = opinfo.boi_acl_cache;
64                 if( rs->sr_err != 0 ) {
65                         rs->sr_err = LDAP_OTHER;
66                         rs->sr_text = "internal error";
67                         goto return_results;
68                 }
69                 ldap_pvt_thread_yield();
70         }
71
72         /* begin transaction */
73         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
74                 bdb->bi_db_opflags );
75         rs->sr_text = NULL;
76         if( rs->sr_err != 0 ) {
77 #ifdef NEW_LOGGING
78                 LDAP_LOG ( OPERATION, ERR, 
79                         "==> bdb_delete: txn_begin failed: %s (%d)\n",
80                         db_strerror(rs->sr_err), rs->sr_err, 0 );
81 #else
82                 Debug( LDAP_DEBUG_TRACE,
83                         "bdb_delete: txn_begin failed: %s (%d)\n",
84                         db_strerror(rs->sr_err), rs->sr_err, 0 );
85 #endif
86                 rs->sr_err = LDAP_OTHER;
87                 rs->sr_text = "internal error";
88                 goto return_results;
89         }
90
91         locker = TXN_ID ( ltid );
92
93         opinfo.boi_bdb = op->o_bd;
94         opinfo.boi_txn = ltid;
95         opinfo.boi_locker = locker;
96         opinfo.boi_err = 0;
97         opinfo.boi_acl_cache = op->o_do_not_cache;
98         op->o_private = &opinfo;
99
100         if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
101                 dnParent( &op->o_req_ndn, &pdn );
102         }
103
104         /* get entry */
105         rs->sr_err = bdb_dn2entry( op->o_bd, ltid, &op->o_req_ndn, &ei, 1,
106                 locker, &lock, op->o_tmpmemctx );
107
108         switch( rs->sr_err ) {
109         case 0:
110         case DB_NOTFOUND:
111                 break;
112         case DB_LOCK_DEADLOCK:
113         case DB_LOCK_NOTGRANTED:
114                 goto retry;
115         case LDAP_BUSY:
116                 rs->sr_text = "ldap server busy";
117                 goto return_results;
118         default:
119                 rs->sr_err = LDAP_OTHER;
120                 rs->sr_text = "internal error";
121                 goto return_results;
122         }
123
124         if ( rs->sr_err == 0 ) {
125                 e = ei->bei_e;
126                 eip = ei->bei_parent;
127                 bdb_cache_find_entry_id( op->o_bd, ltid, eip->bei_id, &eip,
128                         0, locker, &plock, op->o_tmpmemctx );
129         }
130         if ( eip ) {
131                 p = eip->bei_e;
132         }
133
134         if ( pdn.bv_len != 0 ) {
135                 if( p == NULL || !bvmatch( &pdn, &p->e_nname )) {
136 #ifdef NEW_LOGGING
137                         LDAP_LOG ( OPERATION, DETAIL1, 
138                                 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
139 #else
140                         Debug( LDAP_DEBUG_TRACE,
141                                 "<=- bdb_delete: parent does not exist\n",
142                                 0, 0, 0);
143 #endif
144                         rs->sr_err = LDAP_OTHER;
145                         rs->sr_text = "could not locate parent of entry";
146                         goto return_results;
147                 }
148
149                 /* check parent for "children" acl */
150                 rs->sr_err = access_allowed( op, p,
151                         children, NULL, ACL_WRITE, NULL );
152
153                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
154                 p = NULL;
155
156                 if ( !rs->sr_err  ) {
157                         switch( opinfo.boi_err ) {
158                         case DB_LOCK_DEADLOCK:
159                         case DB_LOCK_NOTGRANTED:
160                                 goto retry;
161                         }
162
163 #ifdef NEW_LOGGING
164                         LDAP_LOG ( OPERATION, DETAIL1, 
165                                 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
166 #else
167                         Debug( LDAP_DEBUG_TRACE,
168                                 "<=- bdb_delete: no write access to parent\n",
169                                 0, 0, 0 );
170 #endif
171                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
172                         rs->sr_text = "no write access to parent";
173                         goto return_results;
174                 }
175
176         } else {
177                 /* no parent, must be root to delete */
178                 if( ! be_isroot( op->o_bd, &op->o_ndn ) ) {
179                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
180                                 || be_isupdate( op->o_bd, &op->o_ndn ) ) {
181                                 p = (Entry *)&slap_entry_root;
182
183                                 /* check parent for "children" acl */
184                                 rs->sr_err = access_allowed( op, p,
185                                         children, NULL, ACL_WRITE, NULL );
186
187                                 p = NULL;
188
189                                 if ( !rs->sr_err  ) {
190                                         switch( opinfo.boi_err ) {
191                                         case DB_LOCK_DEADLOCK:
192                                         case DB_LOCK_NOTGRANTED:
193                                                 goto retry;
194                                         }
195
196 #ifdef NEW_LOGGING
197                                         LDAP_LOG ( OPERATION, DETAIL1, 
198                                                 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
199 #else
200                                         Debug( LDAP_DEBUG_TRACE,
201                                                 "<=- bdb_delete: no access "
202                                                 "to parent\n", 0, 0, 0 );
203 #endif
204                                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
205                                         rs->sr_text = "no write access to parent";
206                                         goto return_results;
207                                 }
208
209                         } else {
210 #ifdef NEW_LOGGING
211                                 LDAP_LOG ( OPERATION, DETAIL1, 
212                                         "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
213 #else
214                                 Debug( LDAP_DEBUG_TRACE,
215                                         "<=- bdb_delete: no parent "
216                                         "and not root\n", 0, 0, 0);
217 #endif
218                                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
219                                 goto return_results;
220                         }
221                 }
222         }
223
224         if ( e == NULL ) {
225 #ifdef NEW_LOGGING
226                 LDAP_LOG ( OPERATION, ARGS, 
227                         "<=- bdb_delete: no such object %s\n", op->o_req_dn.bv_val, 0, 0);
228 #else
229                 Debug( LDAP_DEBUG_ARGS,
230                         "<=- bdb_delete: no such object %s\n",
231                         op->o_req_dn.bv_val, 0, 0);
232 #endif
233
234                 if ( matched != NULL ) {
235                         rs->sr_matched = ch_strdup( matched->e_dn );
236                         rs->sr_ref = is_entry_referral( matched )
237                                 ? get_entry_referrals( op, matched )
238                                 : NULL;
239                         bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
240                         matched = NULL;
241
242                 } else {
243                         rs->sr_ref = referral_rewrite( default_referral,
244                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
245                 }
246
247                 rs->sr_err = LDAP_REFERRAL;
248                 send_ldap_result( op, rs );
249
250                 ber_bvarray_free( rs->sr_ref );
251                 free( (char *)rs->sr_matched );
252                 rs->sr_ref = NULL;
253                 rs->sr_matched = NULL;
254
255                 rs->sr_err = -1;
256                 goto done;
257         }
258
259         rs->sr_err = access_allowed( op, e,
260                 entry, NULL, ACL_WRITE, NULL );
261
262         if ( !rs->sr_err  ) {
263                 switch( opinfo.boi_err ) {
264                 case DB_LOCK_DEADLOCK:
265                 case DB_LOCK_NOTGRANTED:
266                         goto retry;
267                 }
268
269 #ifdef NEW_LOGGING
270                 LDAP_LOG ( OPERATION, DETAIL1, 
271                         "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
272 #else
273                 Debug( LDAP_DEBUG_TRACE,
274                         "<=- bdb_delete: no write access to entry\n",
275                         0, 0, 0 );
276 #endif
277                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
278                 rs->sr_text = "no write access to entry";
279                 goto return_results;
280         }
281
282         if ( !manageDSAit && is_entry_referral( e ) ) {
283                 /* entry is a referral, don't allow delete */
284                 rs->sr_ref = get_entry_referrals( op, e );
285
286 #ifdef NEW_LOGGING
287                 LDAP_LOG ( OPERATION, DETAIL1, 
288                         "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
289 #else
290                 Debug( LDAP_DEBUG_TRACE,
291                         "bdb_delete: entry is referral\n",
292                         0, 0, 0 );
293 #endif
294
295                 rs->sr_err = LDAP_REFERRAL;
296                 rs->sr_matched = e->e_name.bv_val;
297                 send_ldap_result( op, rs );
298
299                 ber_bvarray_free( rs->sr_ref );
300                 rs->sr_ref = NULL;
301                 rs->sr_matched = NULL;
302
303                 rs->sr_err = 1;
304                 goto done;
305         }
306
307         /* nested transaction */
308         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
309                 bdb->bi_db_opflags );
310         rs->sr_text = NULL;
311         if( rs->sr_err != 0 ) {
312 #ifdef NEW_LOGGING
313                 LDAP_LOG ( OPERATION, ERR, 
314                         "bdb_delete: txn_begin(2) failed: %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
315 #else
316                 Debug( LDAP_DEBUG_TRACE,
317                         "bdb_delete: txn_begin(2) failed: %s (%d)\n",
318                         db_strerror(rs->sr_err), rs->sr_err, 0 );
319 #endif
320                 rs->sr_err = LDAP_OTHER;
321                 rs->sr_text = "internal error";
322                 goto return_results;
323         }
324
325         /* Can't do it if we have kids */
326         rs->sr_err = bdb_dn2id_children( op, lt2, e );
327         if( rs->sr_err != DB_NOTFOUND ) {
328                 switch( rs->sr_err ) {
329                 case DB_LOCK_DEADLOCK:
330                 case DB_LOCK_NOTGRANTED:
331                         goto retry;
332                 case 0:
333 #ifdef NEW_LOGGING
334                         LDAP_LOG ( OPERATION, DETAIL1, 
335                                 "<=- bdb_delete: non-leaf %s\n", op->o_req_dn.bv_val, 0, 0 );
336 #else
337                         Debug(LDAP_DEBUG_ARGS,
338                                 "<=- bdb_delete: non-leaf %s\n",
339                                 op->o_req_dn.bv_val, 0, 0);
340 #endif
341                         rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
342                         rs->sr_text = "subtree delete not supported";
343                         break;
344                 default:
345 #ifdef NEW_LOGGING
346                         LDAP_LOG ( OPERATION, ERR, 
347                                 "<=- bdb_delete: has_children failed %s (%d)\n",
348                                 db_strerror(rs->sr_err), rs->sr_err, 0 );
349 #else
350                         Debug(LDAP_DEBUG_ARGS,
351                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
352                                 db_strerror(rs->sr_err), rs->sr_err, 0 );
353 #endif
354                         rs->sr_err = LDAP_OTHER;
355                         rs->sr_text = "internal error";
356                 }
357                 goto return_results;
358         }
359
360         /* delete from dn2id */
361         rs->sr_err = bdb_dn2id_delete( op->o_bd, lt2, pdn.bv_val, e );
362         if ( rs->sr_err != 0 ) {
363                 switch( rs->sr_err ) {
364                 case DB_LOCK_DEADLOCK:
365                 case DB_LOCK_NOTGRANTED:
366                         goto retry;
367                 }
368 #ifdef NEW_LOGGING
369                 LDAP_LOG ( OPERATION, ERR, 
370                         "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
371 #else
372                 Debug(LDAP_DEBUG_ARGS,
373                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
374                         db_strerror(rs->sr_err), rs->sr_err, 0 );
375 #endif
376                 rs->sr_text = "DN index delete failed";
377                 rs->sr_err = LDAP_OTHER;
378                 goto return_results;
379         }
380
381         /* delete from id2entry */
382         rs->sr_err = bdb_id2entry_delete( op->o_bd, lt2, e );
383         if ( rs->sr_err != 0 ) {
384                 switch( rs->sr_err ) {
385                 case DB_LOCK_DEADLOCK:
386                 case DB_LOCK_NOTGRANTED:
387                         goto retry;
388                 }
389 #ifdef NEW_LOGGING
390                 LDAP_LOG ( OPERATION, ERR, 
391                         "<=- bdb_delete: id2entry failed: %s (%d)\n", 
392                         db_strerror(rs->sr_err), rs->sr_err, 0 );
393 #else
394                 Debug(LDAP_DEBUG_ARGS,
395                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
396                         db_strerror(rs->sr_err), rs->sr_err, 0 );
397 #endif
398                 rs->sr_text = "entry delete failed";
399                 rs->sr_err = LDAP_OTHER;
400                 goto return_results;
401         }
402
403         /* delete indices for old attributes */
404         rs->sr_err = bdb_index_entry_del( op, lt2, e );
405         if ( rs->sr_err != LDAP_SUCCESS ) {
406                 switch( rs->sr_err ) {
407                 case DB_LOCK_DEADLOCK:
408                 case DB_LOCK_NOTGRANTED:
409                         goto retry;
410                 }
411 #ifdef NEW_LOGGING
412                 LDAP_LOG ( OPERATION, ERR, 
413                         "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
414 #else
415                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
416                         0, 0, 0 );
417 #endif
418                 rs->sr_text = "entry index delete failed";
419                 rs->sr_err = LDAP_OTHER;
420                 goto return_results;
421         }
422         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
423                 rs->sr_err = LDAP_OTHER;
424                 rs->sr_text = "txn_commit(2) failed";
425                 goto return_results;
426         }
427
428 #if 0   /* Do we want to reclaim deleted IDs? */
429         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
430         if ( e->e_id == bdb->bi_lastid ) {
431                 bdb_last_id( op->o_bd, ltid );
432         }
433         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
434 #endif
435
436         if( op->o_noop ) {
437                 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
438                         rs->sr_text = "txn_abort (no-op) failed";
439                 } else {
440                         noop = 1;
441                         rs->sr_err = LDAP_SUCCESS;
442                 }
443         } else {
444                 bdb_cache_delete_entry( &bdb->bi_cache, e, bdb->bi_dbenv,
445                         locker, &lock );
446                 rs->sr_err = TXN_COMMIT( ltid, 0 );
447         }
448         ltid = NULL;
449         op->o_private = NULL;
450
451         if( rs->sr_err != 0 ) {
452 #ifdef NEW_LOGGING
453                 LDAP_LOG ( OPERATION, ERR, 
454                         "bdb_delete: txn_%s failed: %s (%d)\n",
455                         op->o_noop ? "abort (no-op)" : "commit", db_strerror(rs->sr_err), rs->sr_err );
456 #else
457                 Debug( LDAP_DEBUG_TRACE,
458                         "bdb_delete: txn_%s failed: %s (%d)\n",
459                         op->o_noop ? "abort (no-op)" : "commit",
460                         db_strerror(rs->sr_err), rs->sr_err );
461 #endif
462                 rs->sr_err = LDAP_OTHER;
463                 rs->sr_text = "commit failed";
464
465         } else {
466 #ifdef NEW_LOGGING
467                 LDAP_LOG ( OPERATION, RESULTS, 
468                         "bdb_delete: deleted%s id=%08lx db=\"%s\"\n",
469                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
470 #else
471                 Debug( LDAP_DEBUG_TRACE,
472                         "bdb_delete: deleted%s id=%08lx dn=\"%s\"\n",
473                         op->o_noop ? " (no-op)" : "",
474                         e->e_id, e->e_dn );
475 #endif
476                 rs->sr_err = LDAP_SUCCESS;
477                 rs->sr_text = NULL;
478         }
479
480 return_results:
481         send_ldap_result( op, rs );
482
483 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
484         if ( rs->sr_err == LDAP_SUCCESS && !noop ) {
485                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
486                         bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_DELETE );
487                 }
488         }
489 #endif
490
491         if(rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
492                 ldap_pvt_thread_yield();
493                 TXN_CHECKPOINT( bdb->bi_dbenv,
494                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
495         }
496
497 done:
498         /* free entry */
499         if( e != NULL ) {
500                 if ( rs->sr_err == LDAP_SUCCESS ) {
501                         bdb_entry_return( e );
502                 } else {
503                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
504                 }
505         }
506
507         if( ltid != NULL ) {
508                 TXN_ABORT( ltid );
509                 op->o_private = NULL;
510         }
511
512         return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );
513 }