]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/delete.c
Move CSN invocations to backends
[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         /* allocate CSN */
48         if ( !SLAP_SHADOW( op->o_bd )) {
49                 struct berval csn;
50                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
51
52                 csn.bv_val = csnbuf;
53                 csn.bv_len = sizeof(csnbuf);
54                 slap_get_csn( op, &csn, 1 );
55         }
56
57         /* get entry with writer lock */
58         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
59
60         /* FIXME : dn2entry() should return non-glue entry */
61         if ( e == NULL || ( !manageDSAit && is_entry_glue( e ))) {
62                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: no such object %s\n",
63                         op->o_req_dn.bv_val, 0, 0);
64
65                 if ( matched != NULL ) {
66                         rs->sr_matched = ch_strdup( matched->e_dn );
67                         rs->sr_ref = is_entry_referral( matched )
68                                 ? get_entry_referrals( op, matched )
69                                 : NULL;
70                         cache_return_entry_r( &li->li_cache, matched );
71
72                 } else {
73                         rs->sr_ref = referral_rewrite( default_referral, NULL,
74                                                         &op->o_req_dn, LDAP_SCOPE_DEFAULT );
75                 }
76
77                 rs->sr_err = LDAP_REFERRAL;
78                 rs->sr_flags |= REP_MATCHED_MUST_BE_FREED | REP_REF_MUSTBEFREED;
79                 goto return_results;
80         }
81
82         /* check entry for "entry" acl */
83         if ( ! access_allowed( op, e, entry, NULL, ACL_WDEL, NULL ) )
84         {
85                 Debug( LDAP_DEBUG_TRACE,
86                         "<=- ldbm_back_delete: no write access to entry\n", 0,
87                         0, 0 );
88
89                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
90                 rs->sr_text = "no write access to entry";
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 = ch_strdup( e->e_name.bv_val );
104                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
105                 goto return_results;
106         }
107
108         if ( has_children( op->o_bd, e ) ) {
109                 Debug(LDAP_DEBUG_ARGS, "<=- ldbm_back_delete: non leaf %s\n",
110                         op->o_req_dn.bv_val, 0, 0);
111
112                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
113                 rs->sr_text = "subordinate objects must be deleted first";
114                 goto return_results;
115         }
116
117         /* delete from parent's id2children entry */
118         if( !be_issuffix( op->o_bd, &e->e_nname ) && (dnParent( &e->e_nname, &pdn ),
119                 pdn.bv_len) ) {
120                 if( (p = dn2entry_w( op->o_bd, &pdn, NULL )) == NULL) {
121                         Debug( LDAP_DEBUG_TRACE,
122                                 "<=- ldbm_back_delete: parent does not exist\n",
123                                 0, 0, 0);
124
125                         rs->sr_err = LDAP_OTHER;
126                         rs->sr_text = "could not locate parent of entry";
127                         goto return_results;
128                 }
129
130                 /* check parent for "children" acl */
131                 if ( ! access_allowed( op, p,
132                         children, NULL, ACL_WDEL, NULL ) )
133                 {
134                         Debug( LDAP_DEBUG_TRACE,
135                                 "<=- ldbm_back_delete: no access to parent\n", 0,
136                                 0, 0 );
137
138                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
139                         rs->sr_text = "no write access to parent";
140                         goto return_results;
141                 }
142
143         } else {
144                 /* no parent, must be root to delete */
145                 if( ! be_isroot( op ) ) {
146                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
147                                 || be_shadow_update( op ) ) {
148                                 p = (Entry *)&slap_entry_root;
149                                 
150                                 rc = access_allowed( op, p,
151                                         children, NULL, ACL_WDEL, NULL );
152                                 p = NULL;
153                                                                 
154                                 /* check parent for "children" acl */
155                                 if ( ! rc ) {
156                                         Debug( LDAP_DEBUG_TRACE,
157                                                 "<=- ldbm_back_delete: no "
158                                                 "access to parent\n", 0, 0, 0 );
159
160                                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
161                                         rs->sr_text = "no write access to parent";
162                                         goto return_results;
163                                 }
164
165                         } else {
166                                 Debug( LDAP_DEBUG_TRACE,
167                                         "<=- ldbm_back_delete: no parent & "
168                                         "not root\n", 0, 0, 0);
169
170                                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
171                                 goto return_results;
172                         }
173                 }
174         }
175
176         /* delete from dn2id mapping */
177         if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
178                 Debug(LDAP_DEBUG_ARGS,
179                         "<=- ldbm_back_delete: operations error %s\n",
180                         op->o_req_dn.bv_val, 0, 0);
181
182                 rs->sr_err = LDAP_OTHER;
183                 rs->sr_text = "DN index delete failed";
184                 goto return_results;
185         }
186
187         /* delete from disk and cache */
188         if ( id2entry_delete( op->o_bd, e ) != 0 ) {
189                 Debug(LDAP_DEBUG_ARGS,
190                         "<=- ldbm_back_delete: operations error %s\n",
191                         op->o_req_dn.bv_val, 0, 0);
192
193                 rs->sr_err = LDAP_OTHER;
194                 rs->sr_text = "entry delete failed";
195                 goto return_results;
196         }
197
198         /* delete attribute indices */
199         (void) index_entry_del( op, e );
200
201         rs->sr_err = LDAP_SUCCESS;
202
203 return_results:;
204         rc = rs->sr_err;
205
206         if( p != NULL ) {
207                 /* free parent and writer lock */
208                 cache_return_entry_w( &li->li_cache, p );
209         }
210
211         if ( e != NULL ) {
212                 /* free entry and writer lock */
213                 cache_return_entry_w( &li->li_cache, e );
214         }
215
216         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
217
218         send_ldap_result( op, rs );
219         if ( !SLAP_SHADOW( op->o_bd ))
220                 slap_graduate_commit_csn( op );
221
222         return rc;
223 }