]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
Initial round of changes for 2.3 beta
[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-2005 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                         rs->sr_ref = referral_rewrite( default_referral, NULL,
64                                                         &op->o_req_dn, LDAP_SCOPE_DEFAULT );
65                 }
66
67                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
68
69                 rs->sr_err = LDAP_REFERRAL;
70                 send_ldap_result( op, rs );
71
72                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
73                 free( (char *)rs->sr_matched );
74                 rs->sr_ref = NULL;
75                 rs->sr_matched = NULL;
76                 return( -1 );
77         }
78
79         /* check entry for "entry" acl */
80         if ( ! access_allowed( op, e, entry, NULL, ACL_WDEL, NULL ) )
81         {
82                 Debug( LDAP_DEBUG_TRACE,
83                         "<=- ldbm_back_delete: no write access to entry\n", 0,
84                         0, 0 );
85
86                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
87                         "no write access to entry" );
88
89                 rc = LDAP_INSUFFICIENT_ACCESS;
90                 goto return_results;
91         }
92
93         if ( !manageDSAit && is_entry_referral( e ) ) {
94                 /* parent is a referral, don't allow add */
95                 /* parent is an alias, don't allow add */
96                 rs->sr_ref = get_entry_referrals( op, e );
97
98                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
99                     0, 0 );
100
101                 rs->sr_err = LDAP_REFERRAL;
102                 rs->sr_matched = e->e_name.bv_val;
103                 send_ldap_result( op, rs );
104
105                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
106                 rs->sr_ref = NULL;
107                 rs->sr_matched = NULL;
108                 rc = LDAP_REFERRAL;
109                 goto return_results;
110         }
111
112         if ( has_children( op->o_bd, e ) ) {
113                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
114                         op->o_req_dn.bv_val, 0, 0);
115
116                 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
117                         "subordinate objects must be deleted first");
118                 goto return_results;
119         }
120
121         /* delete from parent's id2children entry */
122         if( !be_issuffix( op->o_bd, &e->e_nname ) && (dnParent( &e->e_nname, &pdn ),
123                 pdn.bv_len) ) {
124                 if( (p = dn2entry_w( op->o_bd, &pdn, NULL )) == NULL) {
125                         Debug( LDAP_DEBUG_TRACE,
126                                 "<=- ldbm_back_delete: parent does not exist\n",
127                                 0, 0, 0);
128
129                         send_ldap_error( op, rs, LDAP_OTHER,
130                                 "could not locate parent of entry" );
131                         goto return_results;
132                 }
133
134                 /* check parent for "children" acl */
135                 if ( ! access_allowed( op, p,
136                         children, NULL, ACL_WDEL, NULL ) )
137                 {
138                         Debug( LDAP_DEBUG_TRACE,
139                                 "<=- ldbm_back_delete: no access to parent\n", 0,
140                                 0, 0 );
141
142                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
143                                 "no write access to parent" );
144                         goto return_results;
145                 }
146
147         } else {
148                 /* no parent, must be root to delete */
149                 if( ! be_isroot( op ) ) {
150                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
151                                 || be_shadow_update( op ) ) {
152                                 p = (Entry *)&slap_entry_root;
153                                 
154                                 rc = access_allowed( op, p,
155                                         children, NULL, ACL_WDEL, NULL );
156                                 p = NULL;
157                                                                 
158                                 /* check parent for "children" acl */
159                                 if ( ! rc ) {
160                                         Debug( LDAP_DEBUG_TRACE,
161                                                 "<=- ldbm_back_delete: no "
162                                                 "access to parent\n", 0, 0, 0 );
163
164                                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
165                                                 "no write access to parent" );
166                                         goto return_results;
167                                 }
168
169                         } else {
170                                 Debug( LDAP_DEBUG_TRACE,
171                                         "<=- ldbm_back_delete: no parent & "
172                                         "not root\n", 0, 0, 0);
173
174                                 send_ldap_error( op, rs,
175                                         LDAP_INSUFFICIENT_ACCESS,
176                                         NULL );
177                                 goto return_results;
178                         }
179                 }
180         }
181
182         /* delete from dn2id mapping */
183         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
184                 Debug(LDAP_DEBUG_ARGS,
185                         "<=- ldbm_back_delete: operations error %s\n",
186                         op->o_req_dn.bv_val, 0, 0);
187
188                 send_ldap_error( op, rs, LDAP_OTHER,
189                         "DN index delete failed" );
190                 goto return_results;
191         }
192
193         /* delete from disk and cache */
194         if ( id2entry_delete( op->o_bd, e ) != 0 ) {
195                 Debug(LDAP_DEBUG_ARGS,
196                         "<=- ldbm_back_delete: operations error %s\n",
197                         op->o_req_dn.bv_val, 0, 0);
198
199                 send_ldap_error( op, rs, LDAP_OTHER,
200                         "entry delete failed" );
201                 goto return_results;
202         }
203
204         /* delete attribute indices */
205         (void) index_entry_del( op, e );
206
207         rs->sr_err = LDAP_SUCCESS;
208         send_ldap_result( op, rs );
209         rc = LDAP_SUCCESS;
210
211 return_results:;
212         if( p != NULL ) {
213                 /* free parent and writer lock */
214                 cache_return_entry_w( &li->li_cache, p );
215         }
216
217         /* free entry and writer lock */
218         cache_return_entry_w( &li->li_cache, e );
219
220         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
221
222         return rc;
223 }