]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
cleanup bind
[openldap] / servers / slapd / back-ldbm / delete.c
1 /* delete.c - ldbm backend delete routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 int
20 ldbm_back_delete(
21     Backend     *be,
22     Connection  *conn,
23     Operation   *op,
24     struct berval       *dn,
25     struct berval       *ndn
26 )
27 {
28         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
29         Entry   *matched;
30         struct berval   pdn;
31         Entry   *e, *p = NULL;
32         int     rc = -1;
33         int             manageDSAit = get_manageDSAit( op );
34         AttributeDescription *children = slap_schema.si_ad_children;
35         AttributeDescription *entry = slap_schema.si_ad_entry;
36
37 #ifdef NEW_LOGGING
38         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_delete: %s\n", dn->bv_val, 0, 0 );
39 #else
40         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_delete: %s\n", dn->bv_val, 0, 0);
41 #endif
42
43         /* grab giant lock for writing */
44         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
45
46         /* get entry with writer lock */
47         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
48                 char *matched_dn = NULL;
49                 BerVarray refs;
50
51 #ifdef NEW_LOGGING
52                 LDAP_LOG( BACK_LDBM, INFO, 
53                         "ldbm_back_delete: no such object %s\n", dn->bv_val, 0, 0 );
54 #else
55                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
56                         dn->bv_val, 0, 0);
57 #endif
58
59                 if ( matched != NULL ) {
60                         matched_dn = ch_strdup( matched->e_dn );
61                         refs = is_entry_referral( matched )
62                                 ? get_entry_referrals( be, conn, op, matched )
63                                 : NULL;
64                         cache_return_entry_r( &li->li_cache, matched );
65
66                 } else {
67                         refs = referral_rewrite( default_referral,
68                                 NULL, dn, LDAP_SCOPE_DEFAULT );
69                 }
70
71                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
72
73                 send_ldap_result( conn, op, LDAP_REFERRAL,
74                         matched_dn, NULL, refs, NULL );
75
76                 if ( refs ) ber_bvarray_free( refs );
77                 free( matched_dn );
78
79                 return( -1 );
80         }
81
82         /* check entry for "entry" acl */
83 #ifdef LDAP_CACHING
84         if( !op->o_caching_on ) {
85 #endif /* LDAP_CACHING */
86         if ( ! access_allowed( be, conn, op, e,
87                 entry, NULL, ACL_WRITE, NULL ) )
88         {
89 #ifdef NEW_LOGGING
90                 LDAP_LOG( BACK_LDBM, ERR, 
91                         "ldbm_back_delete: no write access to entry of (%s)\n", 
92                         dn->bv_val, 0, 0 );
93 #else
94                 Debug( LDAP_DEBUG_TRACE,
95                         "<=- ldbm_back_delete: no write access to entry\n", 0,
96                         0, 0 );
97 #endif
98
99                 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
100                         NULL, "no write access to entry", NULL, NULL );
101
102                 rc = 1;
103                 goto return_results;
104         }
105
106         if ( !manageDSAit && is_entry_referral( e ) ) {
107                 /* parent is a referral, don't allow add */
108                 /* parent is an alias, don't allow add */
109                 BerVarray refs = get_entry_referrals( be,
110                         conn, op, e );
111
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( BACK_LDBM, INFO, 
114                         "ldbm_back_delete: entry (%s) is a referral.\n", e->e_dn, 0, 0 );
115 #else
116                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
117                     0, 0 );
118 #endif
119
120                 send_ldap_result( conn, op, LDAP_REFERRAL,
121                     e->e_dn, NULL, refs, NULL );
122
123                 if ( refs ) ber_bvarray_free( refs );
124
125                 rc = 1;
126                 goto return_results;
127         }
128
129         if ( has_children( be, e ) ) {
130 #ifdef NEW_LOGGING
131                 LDAP_LOG( BACK_LDBM, ERR, 
132                            "ldbm_back_delete: (%s) is a non-leaf node.\n", dn->bv_val, 0,0);
133 #else
134                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
135                         dn->bv_val, 0, 0);
136 #endif
137
138                 send_ldap_result( conn, op, LDAP_NOT_ALLOWED_ON_NONLEAF,
139                         NULL, "subtree delete not supported", NULL, NULL );
140                 goto return_results;
141         }
142
143         /* delete from parent's id2children entry */
144         if( !be_issuffix( be, &e->e_nname ) && (dnParent( &e->e_nname, &pdn ),
145                 pdn.bv_len) ) {
146                 if( (p = dn2entry_w( be, &pdn, NULL )) == NULL) {
147 #ifdef NEW_LOGGING
148                         LDAP_LOG( BACK_LDBM, ERR, 
149                                 "ldbm_back_delete: parent of (%s) does not exist\n", dn, 0, 0 );
150 #else
151                         Debug( LDAP_DEBUG_TRACE,
152                                 "<=- ldbm_back_delete: parent does not exist\n",
153                                 0, 0, 0);
154 #endif
155
156                         send_ldap_result( conn, op, LDAP_OTHER,
157                                 NULL, "could not locate parent of entry", NULL, NULL );
158                         goto return_results;
159                 }
160
161                 /* check parent for "children" acl */
162                 if ( ! access_allowed( be, conn, op, p,
163                         children, NULL, ACL_WRITE, NULL ) )
164                 {
165 #ifdef NEW_LOGGING
166                         LDAP_LOG( BACK_LDBM, ERR, 
167                                 "ldbm_back_delete: no access to parent of (%s)\n", 
168                                 dn->bv_val, 0, 0 );
169 #else
170                         Debug( LDAP_DEBUG_TRACE,
171                                 "<=- ldbm_back_delete: no access to parent\n", 0,
172                                 0, 0 );
173 #endif
174
175                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
176                                 NULL, "no write access to parent", NULL, NULL );
177                         goto return_results;
178                 }
179
180         } else {
181                 /* no parent, must be root to delete */
182                 if( ! be_isroot( be, &op->o_ndn ) ) {
183                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) {
184                                 p = (Entry *)&slap_entry_root;
185                                 
186                                 rc = access_allowed( be, conn, op, p,
187                                         children, NULL, ACL_WRITE, NULL );
188                                 p = NULL;
189                                                                 
190                                 /* check parent for "children" acl */
191                                 if ( ! rc ) {
192 #ifdef NEW_LOGGING
193                                         LDAP_LOG( BACK_LDBM, ERR,
194                                                 "ldbm_back_delete: no access "
195                                                 "to parent of ("")\n", 0, 0, 0 );
196 #else
197                                         Debug( LDAP_DEBUG_TRACE,
198                                                 "<=- ldbm_back_delete: no "
199                                                 "access to parent\n", 0, 0, 0 );
200 #endif
201
202                                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
203                                                 NULL, "no write access to parent", NULL, NULL );
204                                         goto return_results;
205                                 }
206
207                         } else {
208 #ifdef NEW_LOGGING
209                                 LDAP_LOG( BACK_LDBM, ERR, 
210                                         "ldbm_back_delete: (%s) has no "
211                                         "parent & not a root.\n", dn, 0, 0 );
212 #else
213                                 Debug( LDAP_DEBUG_TRACE,
214                                         "<=- ldbm_back_delete: no parent & "
215                                         "not root\n", 0, 0, 0);
216 #endif
217
218                                 send_ldap_result( conn, op, 
219                                         LDAP_INSUFFICIENT_ACCESS,
220                                         NULL, NULL, NULL, NULL );
221                                 goto return_results;
222                         }
223                 }
224         }
225 #ifdef LDAP_CACHING
226         }
227 #endif /* LDAP_CACHING */
228
229         /* delete from dn2id mapping */
230         if ( dn2id_delete( be, &e->e_nname, e->e_id ) != 0 ) {
231 #ifdef NEW_LOGGING
232                 LDAP_LOG( BACK_LDBM, ERR, 
233                         "ldbm_back_delete: (%s) operations error\n", dn->bv_val, 0, 0 );
234 #else
235                 Debug(LDAP_DEBUG_ARGS,
236                         "<=- ldbm_back_delete: operations error %s\n",
237                         dn->bv_val, 0, 0);
238 #endif
239
240                 send_ldap_result( conn, op, LDAP_OTHER,
241                         NULL, "DN index delete failed", NULL, NULL );
242                 goto return_results;
243         }
244
245         /* delete from disk and cache */
246         if ( id2entry_delete( be, e ) != 0 ) {
247 #ifdef NEW_LOGGING
248                 LDAP_LOG( BACK_LDBM, ERR, 
249                         "ldbm_back_delete: (%s) operations error\n", dn->bv_val, 0, 0 );
250 #else
251                 Debug(LDAP_DEBUG_ARGS,
252                         "<=- ldbm_back_delete: operations error %s\n",
253                         dn->bv_val, 0, 0);
254 #endif
255
256                 send_ldap_result( conn, op, LDAP_OTHER,
257                         NULL, "entry delete failed", NULL, NULL );
258                 goto return_results;
259         }
260
261         /* delete attribute indices */
262         (void) index_entry_del( be, e, e->e_attrs );
263
264         send_ldap_result( conn, op, LDAP_SUCCESS,
265                 NULL, NULL, NULL, NULL );
266         rc = 0;
267
268 return_results:;
269         if( p != NULL ) {
270                 /* free parent and writer lock */
271                 cache_return_entry_w( &li->li_cache, p );
272         }
273
274         /* free entry and writer lock */
275         cache_return_entry_w( &li->li_cache, e );
276
277         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
278
279         return rc;
280 }