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