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