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