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