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