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