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