Debian Codegraph @

servers/slapd/back-wt/delete.c

raw
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2002-2024 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 /* ACKNOWLEDGEMENTS:
17 * This work was developed by HAMANO Tsukasa <[email protected]>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
20 */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "back-wt.h"
28 #include "slap-config.h"
29
30 int
31 wt_delete( Operation *op, SlapReply *rs )
32 {
33 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
34 struct berval pdn = {0, NULL};
35 Entry *e = NULL;
36 Entry *p = NULL;
37 int manageDSAit = get_manageDSAit( op );
38 AttributeDescription *children = slap_schema.si_ad_children;
39 AttributeDescription *entry = slap_schema.si_ad_entry;
40
41 LDAPControl **preread_ctrl = NULL;
42 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
43 int num_ctrls = 0;
44
45 wt_ctx *wc;
46 int rc;
47
48 int parent_is_glue = 0;
49 int parent_is_leaf = 0;
50
51 Debug( LDAP_DEBUG_ARGS, "==> wt_delete: %s\n",
52 op->o_req_dn.bv_val );
53
54 if( op->o_txnSpec && txn_preop( op, rs ))
55 return rs->sr_err;
56
57 ctrls[num_ctrls] = 0;
58 rs->sr_text = NULL;
59
60 wc = wt_ctx_get(op, wi);
61 if( !wc ){
62 Debug( LDAP_DEBUG_TRACE, "wt_delete: wt_ctx_get failed\n" );
63 rs->sr_err = LDAP_OTHER;
64 rs->sr_text = "internal error";
65 goto return_results;
66 }
67
68 /* allocate CSN */
69 if ( BER_BVISNULL( &op->o_csn ) ) {
70 struct berval csn;
71 char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
72
73 csn.bv_val = csnbuf;
74 csn.bv_len = sizeof(csnbuf);
75 slap_get_csn( op, &csn, 1 );
76 }
77
78 if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
79 dnParent( &op->o_req_ndn, &pdn );
80 }
81
82 /* get parent */
83 rc = wt_dn2entry(op->o_bd, wc, &pdn, &p);
84 switch( rc ) {
85 case 0:
86 case WT_NOTFOUND:
87 break;
88 default:
89 rs->sr_err = LDAP_OTHER;
90 rs->sr_text = "internal error";
91 Debug( LDAP_DEBUG_ANY,
92 "wt_delete: error at wt_dn2entry() rc=%d\n", rc );
93 goto return_results;
94 }
95
96 if ( rc == WT_NOTFOUND && pdn.bv_len != 0 ) {
97 Debug( LDAP_DEBUG_ARGS,
98 "<== wt_delete: parent not found %s\n", op->o_req_dn.bv_val );
99 rc = wt_dn2aentry(op->o_bd, wc, &op->o_req_ndn, &e);
100 Debug( LDAP_DEBUG_ARGS, "<== wt_delete: rc=%d\n", rc );
101
102 switch( rc ) {
103 case 0:
104 break;
105 case WT_NOTFOUND:
106 rs->sr_err = LDAP_NO_SUCH_OBJECT;
107 goto return_results;
108 default:
109 Debug( LDAP_DEBUG_ANY, "wt_delete: wt_dn2aentry failed (%d)\n", rc );
110 rs->sr_err = LDAP_OTHER;
111 rs->sr_text = "internal error";
112 goto return_results;
113 }
114
115 if ( e && !BER_BVISEMPTY( &e->e_name )) {
116 rs->sr_matched = ch_strdup( e->e_name.bv_val );
117 if ( is_entry_referral( e )) {
118 BerVarray ref = get_entry_referrals( op, e );
119 rs->sr_ref = referral_rewrite( ref, &e->e_name,
120 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
121 ber_bvarray_free( ref );
122 } else {
123 rs->sr_ref = NULL;
124 }
125 } else {
126 rs->sr_ref = referral_rewrite( default_referral, NULL,
127 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
128 }
129
130 rs->sr_err = LDAP_REFERRAL;
131 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
132 goto return_results;
133 }
134
135 /* get entry */
136 rc = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
137 switch( rc ) {
138 case 0:
139 break;
140 case WT_NOTFOUND:
141 break;
142 default:
143 rs->sr_err = LDAP_OTHER;
144 rs->sr_text = "internal error";
145 Debug( LDAP_DEBUG_ANY,
146 "wt_delete: error at wt_dn2entry() rc=%d\n", rc );
147 goto return_results;
148 }
149
150 /* FIXME : dn2entry() should return non-glue entry */
151 if (rc == WT_NOTFOUND ||
152 ( !manageDSAit && e && is_entry_glue( e ) )) {
153 if ( !e ) {
154 Debug( LDAP_DEBUG_ARGS,
155 "<== wt_delete: no such object %s\n",
156 op->o_req_dn.bv_val);
157 rc = wt_dn2aentry(op->o_bd, wc, &op->o_req_ndn, &e);
158 switch( rc ) {
159 case 0:
160 break;
161 case WT_NOTFOUND:
162 rs->sr_err = LDAP_NO_SUCH_OBJECT;
163 goto return_results;
164 default:
165 Debug( LDAP_DEBUG_ANY, "wt_delete: wt_dn2aentry failed (%d)\n", rc );
166 rs->sr_err = LDAP_OTHER;
167 rs->sr_text = "internal error";
168 goto return_results;
169 }
170 }
171
172 rs->sr_matched = ch_strdup( e->e_dn );
173 if ( is_entry_referral( e )) {
174 BerVarray ref = get_entry_referrals( op, e );
175 rs->sr_ref = referral_rewrite( ref, &e->e_name,
176 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
177 ber_bvarray_free( ref );
178 } else {
179 rs->sr_ref = NULL;
180 }
181
182 rs->sr_err = LDAP_REFERRAL;
183 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
184 goto return_results;
185 }
186
187 if ( pdn.bv_len != 0 ) {
188 /* check parent for "children" acl */
189 rs->sr_err = access_allowed( op, p,
190 children, NULL, ACL_WDEL, NULL );
191
192 if ( !rs->sr_err ) {
193 Debug( LDAP_DEBUG_TRACE,
194 "<== wt_delete: no write access to parent\n" );
195 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
196 rs->sr_text = "no write access to parent";
197 goto return_results;
198 }
199
200 } else {
201 /* no parent, must be root to delete */
202 if( ! be_isroot( op ) ) {
203 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
204 || be_shadow_update( op ) ) {
205 p = (Entry *)&slap_entry_root;
206
207 /* check parent for "children" acl */
208 rs->sr_err = access_allowed( op, p,
209 children, NULL, ACL_WDEL, NULL );
210
211 p = NULL;
212
213 if ( !rs->sr_err ) {
214 Debug( LDAP_DEBUG_TRACE,
215 "<== wt_delete: no access to parent\n" );
216 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
217 rs->sr_text = "no write access to parent";
218 goto return_results;
219 }
220
221 } else {
222 Debug( LDAP_DEBUG_TRACE,
223 "<== wt_delete: no parent and not root\n" );
224 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
225 goto return_results;
226 }
227 }
228 }
229
230 if ( get_assert( op ) &&
231 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
232 {
233 rs->sr_err = LDAP_ASSERTION_FAILED;
234 goto return_results;
235 }
236
237 rs->sr_err = access_allowed( op, e,
238 entry, NULL, ACL_WDEL, NULL );
239 if ( !rs->sr_err ) {
240 Debug( LDAP_DEBUG_TRACE,
241 "<== wt_delete: no write access to entry\n" );
242 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
243 rs->sr_text = "no write access to entry";
244 goto return_results;
245 }
246
247 if ( !manageDSAit && is_entry_referral( e ) ) {
248 /* entry is a referral, don't allow delete */
249 rs->sr_ref = get_entry_referrals( op, e );
250
251 Debug( LDAP_DEBUG_TRACE, "wt_delete: entry is referral\n" );
252
253 rs->sr_err = LDAP_REFERRAL;
254 rs->sr_matched = ch_strdup( e->e_name.bv_val );
255 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
256 goto return_results;
257 }
258
259 /* pre-read */
260 if( op->o_preread ) {
261 if( preread_ctrl == NULL ) {
262 preread_ctrl = &ctrls[num_ctrls++];
263 ctrls[num_ctrls] = NULL;
264 }
265 if( slap_read_controls( op, rs, e,
266 &slap_pre_read_bv, preread_ctrl ) )
267 {
268 Debug( LDAP_DEBUG_TRACE,
269 "<== wt_delete: pre-read failed!\n" );
270 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
271 /* FIXME: is it correct to abort
272 * operation if control fails? */
273 goto return_results;
274 }
275 }
276 }
277
278 /* Can't do it if we have kids */
279 rc = wt_dn2id_has_children( op, wc, e->e_id );
280 if( rc != WT_NOTFOUND ) {
281 switch( rc ) {
282 case 0:
283 Debug(LDAP_DEBUG_ARGS,
284 "<== wt_delete: non-leaf %s\n", op->o_req_dn.bv_val );
285 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
286 rs->sr_text = "subordinate objects must be deleted first";
287 break;
288 default:
289 Debug(LDAP_DEBUG_ARGS,
290 "<== wt_delete: has_children failed: %s (%d)\n",
291 wiredtiger_strerror(rc), rc );
292 rs->sr_err = LDAP_OTHER;
293 rs->sr_text = "internal error";
294 }
295 goto return_results;
296 }
297
298 /* begin transaction */
299 rc = wc->session->begin_transaction(wc->session, NULL);
300 if( rc ) {
301 Debug( LDAP_DEBUG_TRACE,
302 "wt_delete: begin_transaction failed: %s (%d)\n",
303 wiredtiger_strerror(rc), rc );
304 rs->sr_err = LDAP_OTHER;
305 rs->sr_text = "begin_transaction failed";
306 goto return_results;
307 }
308
309 /* delete from dn2id */
310 rc = wt_dn2id_delete( op, wc, &op->o_req_ndn);
311 if ( rc ) {
312 Debug(LDAP_DEBUG_TRACE,
313 "<== wt_delete: dn2id failed: %s (%d)\n",
314 wiredtiger_strerror(rc), rc );
315 rs->sr_err = LDAP_OTHER;
316 rs->sr_text = "dn2id delete failed";
317 wc->session->rollback_transaction(wc->session, NULL);
318 goto return_results;
319 }
320
321 /* delete indices for old attributes */
322 rc = wt_index_entry_del( op, wc, e );
323 if ( rc ) {
324 Debug(LDAP_DEBUG_TRACE,
325 "<== wt_delete: index delete failed: %s (%d)\n",
326 wiredtiger_strerror(rc), rc );
327 rs->sr_err = LDAP_OTHER;
328 rs->sr_text = "index delete failed";
329 wc->session->rollback_transaction(wc->session, NULL);
330 goto return_results;
331 }
332
333 /* fixup delete CSN */
334 if ( !SLAP_SHADOW( op->o_bd )) {
335 struct berval vals[2];
336
337 assert( !BER_BVISNULL( &op->o_csn ) );
338 vals[0] = op->o_csn;
339 BER_BVZERO( &vals[1] );
340 rs->sr_err = wt_index_values( op, wc, slap_schema.si_ad_entryCSN,
341 vals, 0, SLAP_INDEX_ADD_OP );
342 if ( rs->sr_err != LDAP_SUCCESS ) {
343 rs->sr_text = "entryCSN index update failed";
344 rs->sr_err = LDAP_OTHER;
345 wc->session->rollback_transaction(wc->session, NULL);
346 goto return_results;
347 }
348 }
349
350 /* delete from id2entry */
351 rc = wt_id2entry_delete( op, wc, e );
352 if ( rc ) {
353 Debug( LDAP_DEBUG_TRACE,
354 "<== wt_delete: id2entry failed: %s (%d)\n",
355 wiredtiger_strerror(rc), rc );
356 rs->sr_err = LDAP_OTHER;
357 rs->sr_text = "entry delete failed";
358 wc->session->rollback_transaction(wc->session, NULL);
359 goto return_results;
360 }
361
362 if ( pdn.bv_len != 0 ) {
363 // TODO: glue entry
364 }
365
366 rc = wc->session->commit_transaction(wc->session, NULL);
367 if( rc ) {
368 Debug( LDAP_DEBUG_TRACE,
369 "<== wt_delete: commit_transaction failed: %s (%d)\n",
370 wiredtiger_strerror(rc), rc );
371 rs->sr_err = LDAP_OTHER;
372 rs->sr_text = "commit_transaction failed";
373 goto return_results;
374 }
375
376 Debug( LDAP_DEBUG_TRACE,
377 "wt_delete: deleted%s id=%08lx dn=\"%s\"\n",
378 op->o_noop ? " (no-op)" : "", e->e_id, op->o_req_dn.bv_val );
379
380 rs->sr_err = LDAP_SUCCESS;
381 rs->sr_text = NULL;
382 if( num_ctrls ) {
383 rs->sr_ctrls = ctrls;
384 }
385
386 return_results:
387 if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
388 op->o_delete_glue_parent = 1;
389 }
390
391 if ( p != NULL ) {
392 wt_entry_return( p );
393 }
394
395 /* free entry */
396 if( e != NULL ) {
397 wt_entry_return( e );
398 }
399
400 send_ldap_result( op, rs );
401 slap_graduate_commit_csn( op );
402
403 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
404 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
405 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
406 }
407
408 /* TODO: checkpoint */
409
410 return rs->sr_err;
411 }
412
413 /*
414 * Local variables:
415 * indent-tabs-mode: t
416 * tab-width: 4
417 * c-basic-offset: 4
418 * End:
419 */