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