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