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