]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
Plug memory leak
[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 LDAP_CLIENT_UPDATE
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                 if( rc != 0 ) {
74                         rc = LDAP_OTHER;
75                         text = "internal error";
76                         goto return_results;
77                 }
78                 ldap_pvt_thread_yield();
79         }
80
81         /* begin transaction */
82         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
83                 bdb->bi_db_opflags );
84         text = NULL;
85         if( rc != 0 ) {
86 #ifdef NEW_LOGGING
87                 LDAP_LOG ( OPERATION, ERR, 
88                         "==> bdb_delete: txn_begin failed: %s (%d)\n",
89                         db_strerror(rc), rc, 0 );
90 #else
91                 Debug( LDAP_DEBUG_TRACE,
92                         "bdb_delete: txn_begin failed: %s (%d)\n",
93                         db_strerror(rc), rc, 0 );
94 #endif
95                 rc = LDAP_OTHER;
96                 text = "internal error";
97                 goto return_results;
98         }
99
100         locker = TXN_ID ( ltid );
101 #if 0
102         lockid = TXN_ID( ltid );
103 #endif
104
105         opinfo.boi_bdb = be;
106         opinfo.boi_txn = ltid;
107         opinfo.boi_err = 0;
108         op->o_private = &opinfo;
109
110         if ( !be_issuffix( be, ndn ) ) {
111                 dnParent( ndn, &pdn );
112         }
113
114         if( pdn.bv_len != 0 ) {
115 #if 0
116                 if ( ltid ) {
117                         DBT obj;
118                         obj.data = pdn.bv_val-1;
119                         obj.size = pdn.bv_len+1;
120                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
121                                 DB_LOCK_WRITE, &lock);
122                 }
123 #endif
124                 /* get parent */
125                 rc = bdb_dn2entry_r( be, ltid, &pdn, &p, NULL, 0, locker, &lock );
126
127                 switch( rc ) {
128                 case 0:
129                 case DB_NOTFOUND:
130                         break;
131                 case DB_LOCK_DEADLOCK:
132                 case DB_LOCK_NOTGRANTED:
133                         goto retry;
134                 case LDAP_BUSY:
135                         text = "ldap server busy";
136                         goto return_results;
137                 default:
138                         rc = LDAP_OTHER;
139                         text = "internal error";
140                         goto return_results;
141                 }
142
143                 if( p == NULL) {
144 #ifdef NEW_LOGGING
145                         LDAP_LOG ( OPERATION, DETAIL1, 
146                                 "<=- bdb_delete: parent does not exist\n", 0, 0, 0 );
147 #else
148                         Debug( LDAP_DEBUG_TRACE,
149                                 "<=- bdb_delete: parent does not exist\n",
150                                 0, 0, 0);
151 #endif
152                         rc = LDAP_OTHER;
153                         text = "could not locate parent of entry";
154                         goto return_results;
155                 }
156
157                 /* check parent for "children" acl */
158                 rc = access_allowed( be, conn, op, p,
159                         children, NULL, ACL_WRITE, NULL );
160
161                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
162                 p = NULL;
163
164                 switch( opinfo.boi_err ) {
165                 case DB_LOCK_DEADLOCK:
166                 case DB_LOCK_NOTGRANTED:
167                         goto retry;
168                 }
169
170                 if ( !rc  ) {
171 #ifdef NEW_LOGGING
172                         LDAP_LOG ( OPERATION, DETAIL1, 
173                                 "<=- bdb_delete: no write access to parent\n", 0, 0, 0 );
174 #else
175                         Debug( LDAP_DEBUG_TRACE,
176                                 "<=- bdb_delete: no write access to parent\n",
177                                 0, 0, 0 );
178 #endif
179                         rc = LDAP_INSUFFICIENT_ACCESS;
180                         text = "no write access to parent";
181                         goto return_results;
182                 }
183
184         } else {
185                 /* no parent, must be root to delete */
186                 if( ! be_isroot( be, &op->o_ndn ) ) {
187                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
188                                 || be_isupdate( be, &op->o_ndn ) ) {
189                                 p = (Entry *)&slap_entry_root;
190
191                                 /* check parent for "children" acl */
192                                 rc = access_allowed( be, conn, op, p,
193                                         children, NULL, ACL_WRITE, NULL );
194
195                                 p = NULL;
196
197                                 switch( opinfo.boi_err ) {
198                                 case DB_LOCK_DEADLOCK:
199                                 case DB_LOCK_NOTGRANTED:
200                                         goto retry;
201                                 }
202
203                                 if ( !rc  ) {
204 #ifdef NEW_LOGGING
205                                         LDAP_LOG ( OPERATION, DETAIL1, 
206                                                 "<=- bdb_delete: no access to parent\n", 0, 0, 0 );
207 #else
208                                         Debug( LDAP_DEBUG_TRACE,
209                                                 "<=- bdb_delete: no access "
210                                                 "to parent\n", 0, 0, 0 );
211 #endif
212                                         rc = LDAP_INSUFFICIENT_ACCESS;
213                                         text = "no write access to parent";
214                                         goto return_results;
215                                 }
216
217                         } else {
218 #ifdef NEW_LOGGING
219                                 LDAP_LOG ( OPERATION, DETAIL1, 
220                                         "<=- bdb_delete: no parent and not root\n", 0, 0, 0 );
221 #else
222                                 Debug( LDAP_DEBUG_TRACE,
223                                         "<=- bdb_delete: no parent "
224                                         "and not root\n", 0, 0, 0);
225 #endif
226                                 rc = LDAP_INSUFFICIENT_ACCESS;
227                                 goto return_results;
228                         }
229                 }
230
231 #if 0
232                 if ( ltid ) {
233                         DBT obj;
234                         obj.data = ",";
235                         obj.size = 1;
236                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
237                                 DB_LOCK_WRITE, &lock);
238                 }
239 #endif
240         }
241
242         /* get entry for read/modify/write */
243         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
244
245         switch( rc ) {
246         case 0:
247         case DB_NOTFOUND:
248                 break;
249         case DB_LOCK_DEADLOCK:
250         case DB_LOCK_NOTGRANTED:
251                 goto retry;
252         case LDAP_BUSY:
253                 text = "ldap server busy";
254                 goto return_results;
255         default:
256                 rc = LDAP_OTHER;
257                 text = "internal error";
258                 goto return_results;
259         }
260
261         if ( e == NULL ) {
262                 char *matched_dn = NULL;
263                 BerVarray refs;
264
265 #ifdef NEW_LOGGING
266                 LDAP_LOG ( OPERATION, ARGS, 
267                         "<=- bdb_delete: no such object %s\n", dn->bv_val, 0, 0);
268 #else
269                 Debug( LDAP_DEBUG_ARGS,
270                         "<=- bdb_delete: no such object %s\n",
271                         dn->bv_val, 0, 0);
272 #endif
273
274                 if ( matched != NULL ) {
275                         matched_dn = ch_strdup( matched->e_dn );
276                         refs = is_entry_referral( matched )
277                                 ? get_entry_referrals( be, conn, op, matched )
278                                 : NULL;
279                         bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, matched);
280                         matched = NULL;
281
282                 } else {
283                         refs = referral_rewrite( default_referral,
284                                 NULL, dn, LDAP_SCOPE_DEFAULT );
285                 }
286
287                 send_ldap_result( conn, op, LDAP_REFERRAL,
288                         matched_dn, NULL, refs, NULL );
289
290                 ber_bvarray_free( refs );
291                 free( matched_dn );
292
293                 rc = -1;
294                 goto done;
295         }
296
297         rc = access_allowed( be, conn, op, e,
298                 entry, NULL, ACL_WRITE, NULL );
299
300         switch( opinfo.boi_err ) {
301         case DB_LOCK_DEADLOCK:
302         case DB_LOCK_NOTGRANTED:
303                 goto retry;
304         }
305
306         if ( !rc  ) {
307 #ifdef NEW_LOGGING
308                 LDAP_LOG ( OPERATION, DETAIL1, 
309                         "<=- bdb_delete: no write access to entry\n", 0, 0, 0 );
310 #else
311                 Debug( LDAP_DEBUG_TRACE,
312                         "<=- bdb_delete: no write access to entry\n",
313                         0, 0, 0 );
314 #endif
315                 rc = LDAP_INSUFFICIENT_ACCESS;
316                 text = "no write access to entry";
317                 goto return_results;
318         }
319
320         if ( !manageDSAit && is_entry_referral( e ) ) {
321                 /* entry is a referral, don't allow delete */
322                 BerVarray refs = get_entry_referrals( be,
323                         conn, op, e );
324
325 #ifdef NEW_LOGGING
326                 LDAP_LOG ( OPERATION, DETAIL1, 
327                         "<=- bdb_delete: entry is referral\n", 0, 0, 0 );
328 #else
329                 Debug( LDAP_DEBUG_TRACE,
330                         "bdb_delete: entry is referral\n",
331                         0, 0, 0 );
332 #endif
333
334                 send_ldap_result( conn, op, LDAP_REFERRAL,
335                         e->e_dn, NULL, refs, NULL );
336
337                 ber_bvarray_free( refs );
338
339                 rc = 1;
340                 goto done;
341         }
342
343         rc = bdb_dn2id_children( be, ltid, &e->e_nname, 0 );
344         if( rc != DB_NOTFOUND ) {
345                 switch( rc ) {
346                 case DB_LOCK_DEADLOCK:
347                 case DB_LOCK_NOTGRANTED:
348                         goto retry;
349                 case 0:
350 #ifdef NEW_LOGGING
351                         LDAP_LOG ( OPERATION, DETAIL1, 
352                                 "<=- bdb_delete: non-leaf %s\n", dn->bv_val, 0, 0 );
353 #else
354                         Debug(LDAP_DEBUG_ARGS,
355                                 "<=- bdb_delete: non-leaf %s\n",
356                                 dn->bv_val, 0, 0);
357 #endif
358                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
359                         text = "subtree delete not supported";
360                         break;
361                 default:
362 #ifdef NEW_LOGGING
363                         LDAP_LOG ( OPERATION, ERR, 
364                                 "<=- bdb_delete: has_children failed %s (%d)\n",
365                                 db_strerror(rc), rc, 0 );
366 #else
367                         Debug(LDAP_DEBUG_ARGS,
368                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
369                                 db_strerror(rc), rc, 0 );
370 #endif
371                         rc = LDAP_OTHER;
372                         text = "internal error";
373                 }
374                 goto return_results;
375         }
376
377         /* delete from dn2id */
378         rc = bdb_dn2id_delete( be, ltid, pdn.bv_val, e );
379         if ( rc != 0 ) {
380                 switch( rc ) {
381                 case DB_LOCK_DEADLOCK:
382                 case DB_LOCK_NOTGRANTED:
383                         goto retry;
384                 default:
385                         rc = LDAP_OTHER;
386                 }
387 #ifdef NEW_LOGGING
388                 LDAP_LOG ( OPERATION, ERR, 
389                         "<=- bdb_delete: dn2id failed %s (%d)\n", db_strerror(rc), rc, 0 );
390 #else
391                 Debug(LDAP_DEBUG_ARGS,
392                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
393                         db_strerror(rc), rc, 0 );
394 #endif
395                 text = "DN index delete failed";
396                 goto return_results;
397         }
398
399         /* delete from id2entry */
400         rc = bdb_id2entry_delete( be, ltid, e );
401         if ( rc != 0 ) {
402                 switch( rc ) {
403                 case DB_LOCK_DEADLOCK:
404                 case DB_LOCK_NOTGRANTED:
405                         goto retry;
406                 default:
407                         rc = LDAP_OTHER;
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                 goto return_results;
420         }
421
422         /* delete indices for old attributes */
423         rc = bdb_index_entry_del( be, ltid, e, e->e_attrs );
424         if ( rc != LDAP_SUCCESS ) {
425                 switch( rc ) {
426                 case DB_LOCK_DEADLOCK:
427                 case DB_LOCK_NOTGRANTED:
428                         goto retry;
429                 default:
430                         rc = LDAP_OTHER;
431                 }
432 #ifdef NEW_LOGGING
433                 LDAP_LOG ( OPERATION, ERR, 
434                         "<=- bdb_delete: entry index delete failed!\n", 0, 0, 0 );
435 #else
436                 Debug( LDAP_DEBUG_ANY, "entry index delete failed!\n",
437                         0, 0, 0 );
438 #endif
439                 text = "entry index delete failed";
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 #ifdef LDAP_CLIENT_UPDATE
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, LCUP_PSEARCH_BY_DELETE );
500                 }
501         }
502 #endif /* LDAP_CLIENT_UPDATE */
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 }