delete.c revision 1.1.1.6 1 1.1.1.6 christos /* $NetBSD: delete.c,v 1.1.1.6 2021/08/14 16:05:23 christos Exp $ */
2 1.1 tron
3 1.1 tron /* delete.c - mdb backend delete routine */
4 1.1 tron /* $OpenLDAP$ */
5 1.1 tron /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 1.1 tron *
7 1.1.1.6 christos * Copyright 2000-2021 The OpenLDAP Foundation.
8 1.1 tron * All rights reserved.
9 1.1 tron *
10 1.1 tron * Redistribution and use in source and binary forms, with or without
11 1.1 tron * modification, are permitted only as authorized by the OpenLDAP
12 1.1 tron * Public License.
13 1.1 tron *
14 1.1 tron * A copy of this license is available in the file LICENSE in the
15 1.1 tron * top-level directory of the distribution or, alternatively, at
16 1.1 tron * <http://www.OpenLDAP.org/license.html>.
17 1.1 tron */
18 1.1 tron
19 1.1.1.2 christos #include <sys/cdefs.h>
20 1.1.1.6 christos __RCSID("$NetBSD: delete.c,v 1.1.1.6 2021/08/14 16:05:23 christos Exp $");
21 1.1.1.2 christos
22 1.1 tron #include "portable.h"
23 1.1 tron
24 1.1 tron #include <stdio.h>
25 1.1 tron #include <ac/string.h>
26 1.1 tron
27 1.1 tron #include "lutil.h"
28 1.1 tron #include "back-mdb.h"
29 1.1 tron
30 1.1 tron int
31 1.1 tron mdb_delete( Operation *op, SlapReply *rs )
32 1.1 tron {
33 1.1 tron struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
34 1.1 tron struct berval pdn = {0, NULL};
35 1.1 tron Entry *e = NULL;
36 1.1 tron Entry *p = NULL;
37 1.1 tron int manageDSAit = get_manageDSAit( op );
38 1.1 tron AttributeDescription *children = slap_schema.si_ad_children;
39 1.1 tron AttributeDescription *entry = slap_schema.si_ad_entry;
40 1.1 tron MDB_txn *txn = NULL;
41 1.1 tron MDB_cursor *mc;
42 1.1 tron mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
43 1.1 tron
44 1.1 tron LDAPControl **preread_ctrl = NULL;
45 1.1 tron LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
46 1.1 tron int num_ctrls = 0;
47 1.1 tron
48 1.1 tron int parent_is_glue = 0;
49 1.1 tron int parent_is_leaf = 0;
50 1.1 tron
51 1.1 tron Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_delete) ": %s\n",
52 1.1.1.6 christos op->o_req_dn.bv_val );
53 1.1 tron
54 1.1 tron ctrls[num_ctrls] = 0;
55 1.1 tron
56 1.1 tron /* begin transaction */
57 1.1 tron rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
58 1.1 tron rs->sr_text = NULL;
59 1.1 tron if( rs->sr_err != 0 ) {
60 1.1 tron Debug( LDAP_DEBUG_TRACE,
61 1.1 tron LDAP_XSTRING(mdb_delete) ": txn_begin failed: "
62 1.1.1.6 christos "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err );
63 1.1 tron rs->sr_err = LDAP_OTHER;
64 1.1 tron rs->sr_text = "internal error";
65 1.1 tron goto return_results;
66 1.1 tron }
67 1.1 tron txn = moi->moi_txn;
68 1.1 tron
69 1.1 tron /* allocate CSN */
70 1.1 tron if ( BER_BVISNULL( &op->o_csn ) ) {
71 1.1 tron struct berval csn;
72 1.1 tron char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
73 1.1 tron
74 1.1 tron csn.bv_val = csnbuf;
75 1.1 tron csn.bv_len = sizeof(csnbuf);
76 1.1 tron slap_get_csn( op, &csn, 1 );
77 1.1 tron }
78 1.1 tron
79 1.1 tron rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mc );
80 1.1 tron if ( rs->sr_err ) {
81 1.1 tron rs->sr_err = LDAP_OTHER;
82 1.1 tron rs->sr_text = "internal error";
83 1.1 tron goto return_results;
84 1.1 tron }
85 1.1 tron
86 1.1.1.6 christos if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
87 1.1.1.6 christos dnParent( &op->o_req_ndn, &pdn );
88 1.1.1.6 christos
89 1.1.1.6 christos /* get parent */
90 1.1.1.6 christos rs->sr_err = mdb_dn2entry( op, txn, mc, &pdn, &p, NULL, 1 );
91 1.1.1.6 christos switch( rs->sr_err ) {
92 1.1.1.6 christos case 0:
93 1.1.1.6 christos case MDB_NOTFOUND:
94 1.1.1.6 christos break;
95 1.1.1.6 christos case LDAP_BUSY:
96 1.1.1.6 christos rs->sr_text = "ldap server busy";
97 1.1.1.6 christos goto return_results;
98 1.1.1.6 christos default:
99 1.1.1.6 christos rs->sr_err = LDAP_OTHER;
100 1.1.1.6 christos rs->sr_text = "internal error";
101 1.1.1.6 christos goto return_results;
102 1.1.1.6 christos }
103 1.1.1.6 christos if ( rs->sr_err == MDB_NOTFOUND ) {
104 1.1.1.6 christos Debug( LDAP_DEBUG_ARGS,
105 1.1.1.6 christos "<=- " LDAP_XSTRING(mdb_delete) ": no such object %s\n",
106 1.1.1.6 christos op->o_req_dn.bv_val );
107 1.1.1.6 christos
108 1.1.1.6 christos if ( p && !BER_BVISEMPTY( &p->e_name )) {
109 1.1.1.6 christos rs->sr_matched = ch_strdup( p->e_name.bv_val );
110 1.1.1.6 christos if ( is_entry_referral( p )) {
111 1.1.1.6 christos BerVarray ref = get_entry_referrals( op, p );
112 1.1.1.6 christos rs->sr_ref = referral_rewrite( ref, &p->e_name,
113 1.1.1.6 christos &op->o_req_dn, LDAP_SCOPE_DEFAULT );
114 1.1.1.6 christos ber_bvarray_free( ref );
115 1.1.1.6 christos } else {
116 1.1.1.6 christos rs->sr_ref = NULL;
117 1.1.1.6 christos }
118 1.1 tron } else {
119 1.1.1.6 christos rs->sr_ref = referral_rewrite( default_referral, NULL,
120 1.1.1.6 christos &op->o_req_dn, LDAP_SCOPE_DEFAULT );
121 1.1.1.6 christos }
122 1.1.1.6 christos if ( p ) {
123 1.1.1.6 christos mdb_entry_return( op, p );
124 1.1.1.6 christos p = NULL;
125 1.1 tron }
126 1.1 tron
127 1.1.1.6 christos rs->sr_err = LDAP_REFERRAL;
128 1.1.1.6 christos rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
129 1.1.1.6 christos goto return_results;
130 1.1.1.6 christos }
131 1.1 tron }
132 1.1 tron
133 1.1 tron /* get entry */
134 1.1 tron rs->sr_err = mdb_dn2entry( op, txn, mc, &op->o_req_ndn, &e, NULL, 0 );
135 1.1 tron switch( rs->sr_err ) {
136 1.1 tron case MDB_NOTFOUND:
137 1.1 tron e = p;
138 1.1 tron p = NULL;
139 1.1 tron case 0:
140 1.1 tron break;
141 1.1 tron case LDAP_BUSY:
142 1.1 tron rs->sr_text = "ldap server busy";
143 1.1 tron goto return_results;
144 1.1 tron default:
145 1.1 tron rs->sr_err = LDAP_OTHER;
146 1.1 tron rs->sr_text = "internal error";
147 1.1 tron goto return_results;
148 1.1 tron }
149 1.1 tron
150 1.1 tron /* FIXME : dn2entry() should return non-glue entry */
151 1.1 tron if ( rs->sr_err == MDB_NOTFOUND || ( !manageDSAit && is_entry_glue( e ))) {
152 1.1 tron Debug( LDAP_DEBUG_ARGS,
153 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": no such object %s\n",
154 1.1.1.6 christos op->o_req_dn.bv_val );
155 1.1 tron
156 1.1 tron rs->sr_matched = ch_strdup( e->e_dn );
157 1.1 tron if ( is_entry_referral( e )) {
158 1.1 tron BerVarray ref = get_entry_referrals( op, e );
159 1.1 tron rs->sr_ref = referral_rewrite( ref, &e->e_name,
160 1.1 tron &op->o_req_dn, LDAP_SCOPE_DEFAULT );
161 1.1 tron ber_bvarray_free( ref );
162 1.1 tron } else {
163 1.1 tron rs->sr_ref = NULL;
164 1.1 tron }
165 1.1 tron mdb_entry_return( op, e );
166 1.1 tron e = NULL;
167 1.1 tron
168 1.1 tron rs->sr_err = LDAP_REFERRAL;
169 1.1 tron rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
170 1.1 tron goto return_results;
171 1.1 tron }
172 1.1 tron
173 1.1 tron if ( pdn.bv_len != 0 ) {
174 1.1 tron /* check parent for "children" acl */
175 1.1 tron rs->sr_err = access_allowed( op, p,
176 1.1 tron children, NULL, ACL_WDEL, NULL );
177 1.1 tron
178 1.1 tron if ( !rs->sr_err ) {
179 1.1 tron Debug( LDAP_DEBUG_TRACE,
180 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": no write "
181 1.1.1.6 christos "access to parent\n" );
182 1.1 tron rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
183 1.1 tron rs->sr_text = "no write access to parent";
184 1.1 tron goto return_results;
185 1.1 tron }
186 1.1 tron
187 1.1 tron } else {
188 1.1 tron /* no parent, must be root to delete */
189 1.1 tron if( ! be_isroot( op ) ) {
190 1.1 tron if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
191 1.1 tron || be_shadow_update( op ) ) {
192 1.1 tron p = (Entry *)&slap_entry_root;
193 1.1 tron
194 1.1 tron /* check parent for "children" acl */
195 1.1 tron rs->sr_err = access_allowed( op, p,
196 1.1 tron children, NULL, ACL_WDEL, NULL );
197 1.1 tron
198 1.1 tron p = NULL;
199 1.1 tron
200 1.1 tron if ( !rs->sr_err ) {
201 1.1 tron Debug( LDAP_DEBUG_TRACE,
202 1.1 tron "<=- " LDAP_XSTRING(mdb_delete)
203 1.1.1.6 christos ": no access to parent\n" );
204 1.1 tron rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
205 1.1 tron rs->sr_text = "no write access to parent";
206 1.1 tron goto return_results;
207 1.1 tron }
208 1.1 tron
209 1.1 tron } else {
210 1.1 tron Debug( LDAP_DEBUG_TRACE,
211 1.1 tron "<=- " LDAP_XSTRING(mdb_delete)
212 1.1.1.6 christos ": no parent and not root\n" );
213 1.1 tron rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
214 1.1 tron goto return_results;
215 1.1 tron }
216 1.1 tron }
217 1.1 tron }
218 1.1 tron
219 1.1 tron if ( get_assert( op ) &&
220 1.1 tron ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
221 1.1 tron {
222 1.1 tron rs->sr_err = LDAP_ASSERTION_FAILED;
223 1.1 tron goto return_results;
224 1.1 tron }
225 1.1 tron
226 1.1 tron rs->sr_err = access_allowed( op, e,
227 1.1 tron entry, NULL, ACL_WDEL, NULL );
228 1.1 tron
229 1.1 tron if ( !rs->sr_err ) {
230 1.1 tron Debug( LDAP_DEBUG_TRACE,
231 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": no write access "
232 1.1.1.6 christos "to entry\n" );
233 1.1 tron rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
234 1.1 tron rs->sr_text = "no write access to entry";
235 1.1 tron goto return_results;
236 1.1 tron }
237 1.1 tron
238 1.1 tron if ( !manageDSAit && is_entry_referral( e ) ) {
239 1.1 tron /* entry is a referral, don't allow delete */
240 1.1 tron rs->sr_ref = get_entry_referrals( op, e );
241 1.1 tron
242 1.1 tron Debug( LDAP_DEBUG_TRACE,
243 1.1.1.6 christos LDAP_XSTRING(mdb_delete) ": entry is referral\n" );
244 1.1 tron
245 1.1 tron rs->sr_err = LDAP_REFERRAL;
246 1.1 tron rs->sr_matched = ch_strdup( e->e_name.bv_val );
247 1.1 tron rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
248 1.1 tron goto return_results;
249 1.1 tron }
250 1.1 tron
251 1.1 tron /* pre-read */
252 1.1 tron if( op->o_preread ) {
253 1.1 tron if( preread_ctrl == NULL ) {
254 1.1 tron preread_ctrl = &ctrls[num_ctrls++];
255 1.1 tron ctrls[num_ctrls] = NULL;
256 1.1 tron }
257 1.1 tron if( slap_read_controls( op, rs, e,
258 1.1 tron &slap_pre_read_bv, preread_ctrl ) )
259 1.1 tron {
260 1.1 tron Debug( LDAP_DEBUG_TRACE,
261 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": pre-read "
262 1.1.1.6 christos "failed!\n" );
263 1.1 tron if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
264 1.1 tron /* FIXME: is it correct to abort
265 1.1 tron * operation if control fails? */
266 1.1 tron goto return_results;
267 1.1 tron }
268 1.1 tron }
269 1.1 tron }
270 1.1 tron
271 1.1 tron rs->sr_text = NULL;
272 1.1 tron
273 1.1 tron /* Can't do it if we have kids */
274 1.1 tron rs->sr_err = mdb_dn2id_children( op, txn, e );
275 1.1 tron if( rs->sr_err != MDB_NOTFOUND ) {
276 1.1 tron switch( rs->sr_err ) {
277 1.1 tron case 0:
278 1.1 tron Debug(LDAP_DEBUG_ARGS,
279 1.1 tron "<=- " LDAP_XSTRING(mdb_delete)
280 1.1 tron ": non-leaf %s\n",
281 1.1.1.6 christos op->o_req_dn.bv_val );
282 1.1 tron rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
283 1.1 tron rs->sr_text = "subordinate objects must be deleted first";
284 1.1 tron break;
285 1.1 tron default:
286 1.1 tron Debug(LDAP_DEBUG_ARGS,
287 1.1 tron "<=- " LDAP_XSTRING(mdb_delete)
288 1.1 tron ": has_children failed: %s (%d)\n",
289 1.1.1.6 christos mdb_strerror(rs->sr_err), rs->sr_err );
290 1.1 tron rs->sr_err = LDAP_OTHER;
291 1.1 tron rs->sr_text = "internal error";
292 1.1 tron }
293 1.1 tron goto return_results;
294 1.1 tron }
295 1.1 tron
296 1.1 tron /* delete from dn2id */
297 1.1 tron rs->sr_err = mdb_dn2id_delete( op, mc, e->e_id, 1 );
298 1.1 tron mdb_cursor_close( mc );
299 1.1 tron if ( rs->sr_err != 0 ) {
300 1.1 tron Debug(LDAP_DEBUG_TRACE,
301 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": dn2id failed: "
302 1.1.1.6 christos "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err );
303 1.1 tron rs->sr_text = "DN index delete failed";
304 1.1 tron rs->sr_err = LDAP_OTHER;
305 1.1 tron goto return_results;
306 1.1 tron }
307 1.1 tron
308 1.1 tron /* delete indices for old attributes */
309 1.1 tron rs->sr_err = mdb_index_entry_del( op, txn, e );
310 1.1 tron if ( rs->sr_err != LDAP_SUCCESS ) {
311 1.1 tron Debug(LDAP_DEBUG_TRACE,
312 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": index failed: "
313 1.1.1.6 christos "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err );
314 1.1 tron rs->sr_text = "entry index delete failed";
315 1.1 tron rs->sr_err = LDAP_OTHER;
316 1.1 tron goto return_results;
317 1.1 tron }
318 1.1 tron
319 1.1 tron /* fixup delete CSN */
320 1.1 tron if ( !SLAP_SHADOW( op->o_bd )) {
321 1.1 tron struct berval vals[2];
322 1.1 tron
323 1.1 tron assert( !BER_BVISNULL( &op->o_csn ) );
324 1.1 tron vals[0] = op->o_csn;
325 1.1 tron BER_BVZERO( &vals[1] );
326 1.1 tron rs->sr_err = mdb_index_values( op, txn, slap_schema.si_ad_entryCSN,
327 1.1 tron vals, 0, SLAP_INDEX_ADD_OP );
328 1.1 tron if ( rs->sr_err != LDAP_SUCCESS ) {
329 1.1 tron rs->sr_text = "entryCSN index update failed";
330 1.1 tron rs->sr_err = LDAP_OTHER;
331 1.1 tron goto return_results;
332 1.1 tron }
333 1.1 tron }
334 1.1 tron
335 1.1 tron /* delete from id2entry */
336 1.1 tron rs->sr_err = mdb_id2entry_delete( op->o_bd, txn, e );
337 1.1 tron if ( rs->sr_err != 0 ) {
338 1.1 tron Debug( LDAP_DEBUG_TRACE,
339 1.1 tron "<=- " LDAP_XSTRING(mdb_delete) ": id2entry failed: "
340 1.1.1.6 christos "%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err );
341 1.1 tron rs->sr_text = "entry delete failed";
342 1.1 tron rs->sr_err = LDAP_OTHER;
343 1.1 tron goto return_results;
344 1.1 tron }
345 1.1 tron
346 1.1 tron if ( pdn.bv_len != 0 ) {
347 1.1 tron parent_is_glue = is_entry_glue(p);
348 1.1 tron rs->sr_err = mdb_dn2id_children( op, txn, p );
349 1.1 tron if ( rs->sr_err != MDB_NOTFOUND ) {
350 1.1 tron switch( rs->sr_err ) {
351 1.1 tron case 0:
352 1.1 tron break;
353 1.1 tron default:
354 1.1 tron Debug(LDAP_DEBUG_ARGS,
355 1.1 tron "<=- " LDAP_XSTRING(mdb_delete)
356 1.1 tron ": has_children failed: %s (%d)\n",
357 1.1.1.6 christos mdb_strerror(rs->sr_err), rs->sr_err );
358 1.1 tron rs->sr_err = LDAP_OTHER;
359 1.1 tron rs->sr_text = "internal error";
360 1.1 tron goto return_results;
361 1.1 tron }
362 1.1 tron parent_is_leaf = 1;
363 1.1 tron }
364 1.1 tron mdb_entry_return( op, p );
365 1.1 tron p = NULL;
366 1.1 tron }
367 1.1 tron
368 1.1 tron if( moi == &opinfo ) {
369 1.1 tron LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
370 1.1 tron opinfo.moi_oe.oe_key = NULL;
371 1.1 tron if( op->o_noop ) {
372 1.1 tron mdb_txn_abort( txn );
373 1.1 tron rs->sr_err = LDAP_X_NO_OPERATION;
374 1.1 tron txn = NULL;
375 1.1 tron goto return_results;
376 1.1 tron } else {
377 1.1 tron rs->sr_err = mdb_txn_commit( txn );
378 1.1 tron }
379 1.1 tron txn = NULL;
380 1.1 tron }
381 1.1 tron
382 1.1 tron if( rs->sr_err != 0 ) {
383 1.1 tron Debug( LDAP_DEBUG_ANY,
384 1.1 tron LDAP_XSTRING(mdb_delete) ": txn_%s failed: %s (%d)\n",
385 1.1 tron op->o_noop ? "abort (no-op)" : "commit",
386 1.1 tron mdb_strerror(rs->sr_err), rs->sr_err );
387 1.1 tron rs->sr_err = LDAP_OTHER;
388 1.1 tron rs->sr_text = "commit failed";
389 1.1 tron
390 1.1 tron goto return_results;
391 1.1 tron }
392 1.1 tron
393 1.1 tron Debug( LDAP_DEBUG_TRACE,
394 1.1 tron LDAP_XSTRING(mdb_delete) ": deleted%s id=%08lx dn=\"%s\"\n",
395 1.1 tron op->o_noop ? " (no-op)" : "",
396 1.1 tron e->e_id, op->o_req_dn.bv_val );
397 1.1 tron rs->sr_err = LDAP_SUCCESS;
398 1.1 tron rs->sr_text = NULL;
399 1.1 tron if( num_ctrls ) rs->sr_ctrls = ctrls;
400 1.1 tron
401 1.1 tron return_results:
402 1.1 tron if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
403 1.1 tron op->o_delete_glue_parent = 1;
404 1.1 tron }
405 1.1 tron
406 1.1 tron if ( p != NULL ) {
407 1.1 tron mdb_entry_return( op, p );
408 1.1 tron }
409 1.1 tron
410 1.1 tron /* free entry */
411 1.1 tron if( e != NULL ) {
412 1.1 tron mdb_entry_return( op, e );
413 1.1 tron }
414 1.1 tron
415 1.1 tron if( moi == &opinfo ) {
416 1.1 tron if( txn != NULL ) {
417 1.1 tron mdb_txn_abort( txn );
418 1.1 tron }
419 1.1 tron if ( opinfo.moi_oe.oe_key ) {
420 1.1 tron LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
421 1.1 tron }
422 1.1 tron } else {
423 1.1 tron moi->moi_ref--;
424 1.1 tron }
425 1.1 tron
426 1.1 tron send_ldap_result( op, rs );
427 1.1 tron slap_graduate_commit_csn( op );
428 1.1 tron
429 1.1 tron if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
430 1.1 tron slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
431 1.1 tron slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
432 1.1 tron }
433 1.1 tron
434 1.1 tron #if 0
435 1.1 tron if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
436 1.1 tron TXN_CHECKPOINT( mdb->bi_dbenv,
437 1.1 tron mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
438 1.1 tron }
439 1.1 tron #endif
440 1.1 tron return rs->sr_err;
441 1.1 tron }
442