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