]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
unifdef -UNEW_LOGGING
[openldap] / servers / slapd / back-ldbm / delete.c
1 /* delete.c - ldbm backend delete routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27
28 int
29 ldbm_back_delete(
30     Operation   *op,
31     SlapReply   *rs )
32 {
33         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
34         Entry   *matched;
35         struct berval   pdn;
36         Entry   *e, *p = NULL;
37         int     rc = -1;
38         int             manageDSAit = get_manageDSAit( op );
39         AttributeDescription *children = slap_schema.si_ad_children;
40         AttributeDescription *entry = slap_schema.si_ad_entry;
41
42         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_delete: %s\n", op->o_req_dn.bv_val, 0, 0);
43
44         /* grab giant lock for writing */
45         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
46
47         /* get entry with writer lock */
48         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
49
50         /* FIXME : dn2entry() should return non-glue entry */
51         if ( e == NULL || ( !manageDSAit && is_entry_glue( e ))) {
52                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
53                         op->o_req_dn.bv_val, 0, 0);
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                 Debug( LDAP_DEBUG_TRACE,
95                         "<=- ldbm_back_delete: no write access to entry\n", 0,
96                         0, 0 );
97
98                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
99                         "no write access to entry" );
100
101                 rc = LDAP_INSUFFICIENT_ACCESS;
102                 goto return_results;
103         }
104
105         if ( !manageDSAit && is_entry_referral( e ) ) {
106                 /* parent is a referral, don't allow add */
107                 /* parent is an alias, don't allow add */
108                 rs->sr_ref = get_entry_referrals( op, e );
109
110                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
111                     0, 0 );
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                 rs->sr_ref = NULL;
119                 rs->sr_matched = NULL;
120                 rc = LDAP_REFERRAL;
121                 goto return_results;
122         }
123
124         if ( has_children( op->o_bd, e ) ) {
125                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
126                         op->o_req_dn.bv_val, 0, 0);
127
128                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
129                         "subtree delete not supported" );
130                 goto return_results;
131         }
132
133         /* delete from parent's id2children entry */
134         if( !be_issuffix( op->o_bd, &e->e_nname ) && (dnParent( &e->e_nname, &pdn ),
135                 pdn.bv_len) ) {
136                 if( (p = dn2entry_w( op->o_bd, &pdn, NULL )) == NULL) {
137                         Debug( LDAP_DEBUG_TRACE,
138                                 "<=- ldbm_back_delete: parent does not exist\n",
139                                 0, 0, 0);
140
141                         send_ldap_error( op, rs, LDAP_OTHER,
142                                 "could not locate parent of entry" );
143                         goto return_results;
144                 }
145
146                 /* check parent for "children" acl */
147                 if ( ! access_allowed( op, p,
148                         children, NULL, ACL_WRITE, NULL ) )
149                 {
150                         Debug( LDAP_DEBUG_TRACE,
151                                 "<=- ldbm_back_delete: no access to parent\n", 0,
152                                 0, 0 );
153
154                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
155                                 "no write access to parent" );
156                         goto return_results;
157                 }
158
159         } else {
160                 /* no parent, must be root to delete */
161                 if( ! be_isroot( op ) ) {
162                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
163                                 || be_shadow_update( op ) ) {
164                                 p = (Entry *)&slap_entry_root;
165                                 
166                                 rc = access_allowed( op, p,
167                                         children, NULL, ACL_WRITE, NULL );
168                                 p = NULL;
169                                                                 
170                                 /* check parent for "children" acl */
171                                 if ( ! rc ) {
172                                         Debug( LDAP_DEBUG_TRACE,
173                                                 "<=- ldbm_back_delete: no "
174                                                 "access to parent\n", 0, 0, 0 );
175
176                                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
177                                                 "no write access to parent" );
178                                         goto return_results;
179                                 }
180
181                         } else {
182                                 Debug( LDAP_DEBUG_TRACE,
183                                         "<=- ldbm_back_delete: no parent & "
184                                         "not root\n", 0, 0, 0);
185
186                                 send_ldap_error( op, rs,
187                                         LDAP_INSUFFICIENT_ACCESS,
188                                         NULL );
189                                 goto return_results;
190                         }
191                 }
192         }
193
194         /* delete from dn2id mapping */
195         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
196                 Debug(LDAP_DEBUG_ARGS,
197                         "<=- ldbm_back_delete: operations error %s\n",
198                         op->o_req_dn.bv_val, 0, 0);
199
200                 send_ldap_error( op, rs, LDAP_OTHER,
201                         "DN index delete failed" );
202                 goto return_results;
203         }
204
205         /* delete from disk and cache */
206         if ( id2entry_delete( op->o_bd, e ) != 0 ) {
207                 Debug(LDAP_DEBUG_ARGS,
208                         "<=- ldbm_back_delete: operations error %s\n",
209                         op->o_req_dn.bv_val, 0, 0);
210
211                 send_ldap_error( op, rs, LDAP_OTHER,
212                         "entry delete failed" );
213                 goto return_results;
214         }
215
216         /* delete attribute indices */
217         (void) index_entry_del( op, e );
218
219         rs->sr_err = LDAP_SUCCESS;
220         send_ldap_result( op, rs );
221         rc = LDAP_SUCCESS;
222
223 return_results:;
224         if( p != NULL ) {
225                 /* free parent and writer lock */
226                 cache_return_entry_w( &li->li_cache, p );
227         }
228
229         /* free entry and writer lock */
230         cache_return_entry_w( &li->li_cache, e );
231
232         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
233
234         return rc;
235 }