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