]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/delete.c
01e636f6e9ea4b3acaa2b1686621b20ea2d3b783
[openldap] / servers / slapd / back-bdb / delete.c
1 /* delete.c - ldbm backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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         const char      *dn,
22         const char      *ndn
23 )
24 {
25         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
26         Entry   *matched;
27         char    *pdn = NULL;
28         Entry   *e, *p = NULL;
29         int     rc;
30         const char *text = NULL;
31         int             manageDSAit = get_manageDSAit( op );
32         AttributeDescription *children = slap_schema.si_ad_children;
33         DB_TXN          *ltid = NULL;
34         struct bdb_op_info opinfo;
35
36         Debug( LDAP_DEBUG_ARGS, "==> bdb_delete: %s\n", dn, 0, 0 );
37
38         if (0) {
39 retry:  /* transaction retry */
40                 Debug( LDAP_DEBUG_TRACE, "==> bdb_delete: retrying...\n", 0, 0, 0 );
41                 rc = txn_abort( ltid );
42                 ltid = NULL;
43                 op->o_private = NULL;
44                 if( rc != 0 ) {
45                         rc = LDAP_OTHER;
46                         text = "internal error";
47                         goto return_results;
48                 }
49         }
50
51         /* begin transaction */
52         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
53         if( rc != 0 ) {
54                 Debug( LDAP_DEBUG_TRACE,
55                         "bdb_delete: txn_begin failed: %s (%d)\n",
56                         db_strerror(rc), rc, 0 );
57                 rc = LDAP_OTHER;
58                 text = "internal error";
59                 goto return_results;
60         }
61
62         opinfo.boi_bdb = be;
63         opinfo.boi_txn = ltid;
64         opinfo.boi_err = 0;
65         op->o_private = &opinfo;
66
67         /* get entry for read/modify/write */
68         rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, DB_RMW );
69
70         switch( rc ) {
71         case 0:
72         case DB_NOTFOUND:
73                 break;
74         case DB_LOCK_DEADLOCK:
75         case DB_LOCK_NOTGRANTED:
76                 goto retry;
77         default:
78                 rc = LDAP_OTHER;
79                 text = "internal error";
80                 goto return_results;
81         }
82
83         if ( e == NULL ) {
84                 char *matched_dn = NULL;
85                 struct berval **refs = NULL;
86
87                 Debug( LDAP_DEBUG_ARGS,
88                         "<=- bdb_delete: no such object %s\n",
89                         dn, 0, 0);
90
91                 if ( matched != NULL ) {
92                         matched_dn = ch_strdup( matched->e_dn );
93                         refs = is_entry_referral( matched )
94                                 ? get_entry_referrals( be, conn, op, matched )
95                                 : NULL;
96                         bdb_entry_return( be, matched );
97                 } else {
98                         refs = default_referral;
99                 }
100
101                 send_ldap_result( conn, op, LDAP_REFERRAL,
102                         matched_dn, NULL, refs, NULL );
103
104                 if ( matched != NULL ) {
105                         ber_bvecfree( refs );
106                         free( matched_dn );
107                 }
108
109                 rc = -1;
110                 goto done;
111         }
112
113         pdn = dn_parent( be, ndn );
114
115         if( pdn != NULL && *pdn != '\0' ) {
116                 /* get parent */
117                 rc = bdb_dn2entry( be, ltid, pdn, &p, NULL, 0 );
118
119                 ch_free( pdn );
120
121                 switch( rc ) {
122                 case 0:
123                 case DB_NOTFOUND:
124                         break;
125                 case DB_LOCK_DEADLOCK:
126                 case DB_LOCK_NOTGRANTED:
127                         goto retry;
128                 default:
129                         rc = LDAP_OTHER;
130                         text = "internal error";
131                         goto return_results;
132                 }
133
134                 if( p == NULL) {
135                         Debug( LDAP_DEBUG_TRACE,
136                                 "<=- bdb_delete: parent does not exist\n",
137                                 0, 0, 0);
138                         rc = LDAP_OTHER;
139                         text = "could not locate parent of entry";
140                         goto return_results;
141                 }
142
143                 /* check parent for "children" acl */
144                 rc = access_allowed( be, conn, op, p,
145                         children, NULL, ACL_WRITE );
146
147                 bdb_entry_return( be, p );
148
149                 if ( !rc  ) {
150                         Debug( LDAP_DEBUG_TRACE,
151                                 "<=- bdb_delete: no access to parent\n",
152                                 0, 0, 0 );
153                         rc = LDAP_INSUFFICIENT_ACCESS;
154                         goto return_results;
155                 }
156
157         } else {
158                 ch_free( pdn );
159
160                 /* no parent, must be root to delete */
161                 if( ! be_isroot( be, op->o_ndn ) ) {
162                         Debug( LDAP_DEBUG_TRACE,
163                                 "<=- bdb_delete: no parent and not root\n",
164                                 0, 0, 0);
165                         rc = LDAP_INSUFFICIENT_ACCESS;
166                         goto return_results;
167                 }
168         }
169
170         if ( !manageDSAit && is_entry_referral( e ) ) {
171                 /* parent is a referral, don't allow add */
172                 /* parent is an alias, don't allow add */
173                 struct berval **refs = get_entry_referrals( be,
174                         conn, op, e );
175
176                 Debug( LDAP_DEBUG_TRACE,
177                         "bdb_delete: entry is referral\n",
178                         0, 0, 0 );
179
180                 send_ldap_result( conn, op, LDAP_REFERRAL,
181                         e->e_dn, NULL, refs, NULL );
182
183                 ber_bvecfree( refs );
184
185                 rc = 1;
186                 goto done;
187         }
188
189         rc = bdb_dn2id_children( be, ltid, e->e_ndn );
190         if( rc != DB_NOTFOUND ) {
191                 switch( rc ) {
192                 case DB_LOCK_DEADLOCK:
193                 case DB_LOCK_NOTGRANTED:
194                         goto retry;
195                 case 0:
196                         Debug(LDAP_DEBUG_ARGS,
197                                 "<=- bdb_delete: non-leaf %s\n",
198                                 dn, 0, 0);
199                         rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
200                         text = "subtree delete not supported";
201                         break;
202                 default:
203                         Debug(LDAP_DEBUG_ARGS,
204                                 "<=- bdb_delete: has_children failed: %s (%d)\n",
205                                 db_strerror(rc), rc, 0 );
206                         rc = LDAP_OTHER;
207                         text = "internal error";
208                 }
209                 goto return_results;
210         }
211
212         /* delete from dn2id */
213         rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
214         if ( rc != 0 ) {
215                 switch( rc ) {
216                 case DB_LOCK_DEADLOCK:
217                 case DB_LOCK_NOTGRANTED:
218                         goto retry;
219                 default:
220                         rc = LDAP_OTHER;
221                 }
222                 Debug(LDAP_DEBUG_ARGS,
223                         "<=- bdb_delete: dn2id failed: %s (%d)\n",
224                         db_strerror(rc), rc, 0 );
225                 text = "DN index delete failed";
226                 goto return_results;
227         }
228
229         /* delete from id2entry */
230         if ( bdb_id2entry_delete( be, ltid, e->e_id ) != 0 ) {
231                 switch( rc ) {
232                 case DB_LOCK_DEADLOCK:
233                 case DB_LOCK_NOTGRANTED:
234                         goto retry;
235                 default:
236                         rc = LDAP_OTHER;
237                 }
238                 Debug(LDAP_DEBUG_ARGS,
239                         "<=- bdb_delete: id2entry failed: %s (%d)\n",
240                         db_strerror(rc), rc, 0 );
241                 text = "entry delete failed";
242                 goto return_results;
243         }
244
245         rc = txn_commit( ltid, 0 );
246         ltid = NULL;
247         op->o_private = NULL;
248
249         if( rc == 0 ) {
250                 Debug( LDAP_DEBUG_TRACE,
251                         "bdb_add: txn_commit failed: %s (%d)\n",
252                         db_strerror(rc), rc, 0 );
253                 rc = LDAP_OTHER;
254                 text = "commit failed";
255
256         } else {
257                 Debug( LDAP_DEBUG_TRACE,
258                         "bdb_add: added id=%08x dn=\"%s\"\n",
259                         e->e_id, e->e_dn, 0 );
260                 rc = LDAP_SUCCESS;
261                 text = NULL;
262         }
263
264 return_results:
265         send_ldap_result( conn, op, LDAP_SUCCESS,
266                 NULL, text, NULL, NULL );
267
268 done:
269         /* free entry */
270         if( e != NULL ) bdb_entry_return( be, e );
271
272         if( ltid != NULL ) {
273                 txn_abort( ltid );
274                 op->o_private = NULL;
275         }
276
277         return rc;
278 }