Debian Codegraph @

servers/slapd/back-wt/modrdn.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 "back-wt.h"
26 #include "slap-config.h"
27
28 int
29 wt_modrdn( Operation *op, SlapReply *rs )
30 {
31 struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
32 AttributeDescription *children = slap_schema.si_ad_children;
33 AttributeDescription *entry = slap_schema.si_ad_entry;
34 wt_ctx *wc = NULL;
35 Entry *e = NULL;
36 Entry *p = NULL;
37 Entry *ne = NULL;
38 Entry dummy = {0};
39
40 struct berval p_dn, p_ndn;
41 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
42
43 Entry *np = NULL; /* newSuperior Entry */
44 struct berval *np_dn = NULL; /* newSuperior dn */
45 struct berval *np_ndn = NULL; /* newSuperior ndn */
46 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
47
48 int manageDSAit = get_manageDSAit( op );
49 char textbuf[SLAP_TEXT_BUFLEN];
50 size_t textlen = sizeof textbuf;
51 LDAPControl **preread_ctrl = NULL;
52 LDAPControl **postread_ctrl = NULL;
53 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
54 int num_ctrls = 0;
55
56 int rc;
57
58 int parent_is_glue = 0;
59 int parent_is_leaf = 0;
60
61 Debug( LDAP_DEBUG_TRACE, "==> wt_modrdn(%s -> newrdn=%s - newsup=%s)\n",
62 op->o_req_dn.bv_val,
63 op->oq_modrdn.rs_newrdn.bv_val,
64 op->oq_modrdn.rs_newSup?op->oq_modrdn.rs_newSup->bv_val:"NULL" );
65
66 ctrls[num_ctrls] = NULL;
67
68 slap_mods_opattrs( op, &op->orr_modlist, 1 );
69
70 wc = wt_ctx_get(op, wi);
71 if( !wc ){
72 Debug( LDAP_DEBUG_ANY, "wt_modrdn: wt_ctx_get failed\n");
73 rs->sr_err = LDAP_OTHER;
74 rs->sr_text = "internal error";
75 send_ldap_result( op, rs );
76 return rs->sr_err;
77 }
78
79 /* get parent entry */
80 if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
81 rs->sr_err = LDAP_NAMING_VIOLATION;
82 rs->sr_text = "cannot rename suffix entry";
83 goto return_results;
84 } else {
85 dnParent( &op->o_req_ndn, &p_ndn );
86 }
87
88 rc = wt_dn2entry(op->o_bd, wc, &p_ndn, &p);
89 switch( rc ) {
90 case 0:
91 break;
92 case WT_NOTFOUND:
93 Debug( LDAP_DEBUG_ARGS,
94 "<== wt_modrdn: parent does not exist %s\n", p_ndn.bv_val);
95 rs->sr_err = LDAP_NO_SUCH_OBJECT;
96 goto return_results;
97 default:
98 Debug( LDAP_DEBUG_ANY,
99 "<== wt_modrdn: wt_dn2entry failed (%d)\n", rc );
100 rs->sr_err = LDAP_OTHER;
101 rs->sr_text = "internal error";
102 goto return_results;
103 }
104
105 /* check parent for "children" acl */
106 rc = access_allowed( op, p, children, NULL,
107 op->oq_modrdn.rs_newSup == NULL ?
108 ACL_WRITE : ACL_WDEL, NULL );
109
110 if ( !rc ) {
111 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
112 Debug( LDAP_DEBUG_TRACE,
113 "wt_modrdn: no access to parent\n");
114 rs->sr_text = "no write access to old parent's children";
115 goto return_results;
116 }
117
118 Debug( LDAP_DEBUG_TRACE,
119 "wt_modrdn: wr to children of entry %s OK\n", p_ndn.bv_val );
120
121 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
122 p_dn = slap_empty_bv;
123 } else {
124 dnParent( &op->o_req_dn, &p_dn );
125 }
126
127 Debug( LDAP_DEBUG_TRACE,
128 "wt_modrdn: parent dn=%s\n", p_dn.bv_val );
129
130 /* get entry */
131 rc = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
132 switch( rc ) {
133 case 0:
134 break;
135 case WT_NOTFOUND:
136 break;
137 default:
138 Debug( LDAP_DEBUG_ANY,
139 "<== wt_modrdn: wt_dn2entry failed (%d)\n", rc );
140 rs->sr_err = LDAP_OTHER;
141 rs->sr_text = "internal error";
142 goto return_results;
143 }
144
145 if ( rc == WT_NOTFOUND ||
146 ( !manageDSAit && e && is_entry_glue( e ) )) {
147
148 if ( !e ) {
149 Debug( LDAP_DEBUG_ARGS,
150 "<== wt_modrdn: no such object %s\n", op->o_req_dn.bv_val);
151 rc = wt_dn2aentry(op->o_bd, wc, &op->o_req_ndn, &e);
152 switch( rc ) {
153 case 0:
154 break;
155 case WT_NOTFOUND:
156 rs->sr_err = LDAP_NO_SUCH_OBJECT;
157 goto return_results;
158 default:
159 Debug( LDAP_DEBUG_ANY, "wt_modrdn: wt_dn2aentry failed (%d)\n", rc );
160 rs->sr_err = LDAP_OTHER;
161 rs->sr_text = "internal error";
162 goto return_results;
163 }
164 }
165
166 rs->sr_matched = ch_strdup( e->e_dn );
167
168 if ( is_entry_referral( e ) ) {
169 BerVarray ref = get_entry_referrals( op, e );
170 rs->sr_ref = referral_rewrite( ref, &e->e_name,
171 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
172 ber_bvarray_free( ref );
173 } else {
174 rs->sr_ref = NULL;
175 }
176 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
177 rs->sr_err = LDAP_REFERRAL;
178 send_ldap_result( op, rs );
179 goto done;
180 }
181
182 if ( get_assert( op ) &&
183 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
184 {
185 rs->sr_err = LDAP_ASSERTION_FAILED;
186 goto return_results;
187 }
188
189 /* check write on old entry */
190 rc = access_allowed( op, e, entry, NULL, ACL_WRITE, NULL );
191 if ( !rc ) {
192 Debug( LDAP_DEBUG_TRACE, "wt_modrdn: no access to entry\n");
193 rs->sr_text = "no write access to old entry";
194 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
195 goto return_results;
196 }
197
198 /* Can't do it if we have kids */
199 rc = wt_dn2id_has_children( op, wc, e->e_id );
200 if( rc != WT_NOTFOUND ) {
201 switch( rc ) {
202 case 0:
203 Debug(LDAP_DEBUG_ARGS, "<== wt_modrdn: non-leaf %s\n", op->o_req_dn.bv_val);
204 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
205 rs->sr_text = "subtree rename not supported";
206 break;
207 default:
208 Debug(LDAP_DEBUG_ARGS,
209 "<== wt_modrdn: has_children failed: %s (%d)\n",
210 wiredtiger_strerror(rc), rc );
211 rs->sr_err = LDAP_OTHER;
212 rs->sr_text = "internal error";
213 }
214 goto return_results;
215 }
216
217 if (!manageDSAit && is_entry_referral( e ) ) {
218 /* parent is a referral, don't allow add */
219 rs->sr_ref = get_entry_referrals( op, e );
220
221 Debug( LDAP_DEBUG_TRACE,
222 "wt_modrdn: entry %s is referral\n", e->e_dn );
223
224 rs->sr_err = LDAP_REFERRAL,
225 rs->sr_matched = e->e_name.bv_val;
226 send_ldap_result( op, rs );
227
228 ber_bvarray_free( rs->sr_ref );
229 rs->sr_ref = NULL;
230 rs->sr_matched = NULL;
231 goto done;
232 }
233
234 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
235 if ( op->oq_modrdn.rs_newSup != NULL ) {
236 Debug( LDAP_DEBUG_TRACE,
237 "wt_modrdn: new parent \"%s\" requested...\n",
238 op->oq_modrdn.rs_newSup->bv_val );
239
240 /* newSuperior == oldParent? */
241 if( dn_match( &p_ndn, op->oq_modrdn.rs_nnewSup ) ) {
242 Debug( LDAP_DEBUG_TRACE,
243 "wt_modrdn: new parent \"%s\" same as the old parent \"%s\"\n",
244 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val );
245 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
246 }
247 }
248
249 if ( op->oq_modrdn.rs_newSup != NULL ) {
250 if ( op->oq_modrdn.rs_newSup->bv_len ) {
251 np_dn = op->oq_modrdn.rs_newSup;
252 np_ndn = op->oq_modrdn.rs_nnewSup;
253
254 /* newSuperior == oldParent? - checked above */
255 /* newSuperior == entry being moved?, if so ==> ERROR */
256 if ( dnIsSuffix( np_ndn, &e->e_nname )) {
257 rs->sr_err = LDAP_NO_SUCH_OBJECT;
258 rs->sr_text = "new superior not found";
259 goto return_results;
260 }
261 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
262 rc = wt_dn2entry(op->o_bd, wc, np_ndn, &np);
263 switch( rc ) {
264 case 0:
265 break;
266 case WT_NOTFOUND:
267 Debug( LDAP_DEBUG_ANY,
268 "<== wt_modrdn: new superior not found: %s\n",
269 np_ndn->bv_val );
270 rs->sr_err = LDAP_NO_SUCH_OBJECT;
271 rs->sr_text = "new superior not found";
272 goto return_results;
273 default:
274 Debug( LDAP_DEBUG_ANY,
275 "<== wt_modrdn: wt_dn2entry failed %s (%d)\n",
276 wiredtiger_strerror(rc), rc );
277 rs->sr_err = LDAP_OTHER;
278 rs->sr_text = "internal error";
279 goto return_results;
280 }
281 Debug( LDAP_DEBUG_TRACE,
282 "wt_modrdn: wr to new parent OK np=%p, id=%ld\n",
283 (void *) np, (long) np->e_id );
284 rs->sr_err = access_allowed( op, np, children,
285 NULL, ACL_WADD, NULL );
286 if( ! rs->sr_err ) {
287 Debug( LDAP_DEBUG_TRACE,
288 "wt_modrdn: no wr to newSup children\n" );
289 rs->sr_text = "no write access to new superior's children";
290 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
291 goto return_results;
292 }
293 if ( is_entry_alias( np ) ) {
294 Debug( LDAP_DEBUG_TRACE,
295 "wt_modrdn: entry is alias\n" );
296 rs->sr_text = "new superior is an alias";
297 rs->sr_err = LDAP_ALIAS_PROBLEM;
298 goto return_results;
299 }
300 if ( is_entry_referral( np ) ) {
301 /* parent is a referral, don't allow add */
302 Debug( LDAP_DEBUG_TRACE,
303 "wt_modrdn: entry is referral\n" );
304 rs->sr_text = "new superior is a referral";
305 rs->sr_err = LDAP_OTHER;
306 goto return_results;
307 }
308 } else {
309 /* no parent, modrdn entry directly under root */
310 /* TODO: */
311 Debug( LDAP_DEBUG_TRACE,
312 "wt_modrdn: no parent, not implement yet\n" );
313 rs->sr_text = "not implement yet";
314 rs->sr_err = LDAP_OTHER;
315 goto return_results;
316 }
317
318 Debug( LDAP_DEBUG_TRACE,
319 "wt_modrdn: wr to new parent's children OK\n" );
320 new_parent_dn = np_dn;
321 }
322
323 /* Build target dn and make sure target entry doesn't exist already. */
324 if (!new_dn.bv_val) {
325 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL );
326 }
327
328 if (!new_ndn.bv_val) {
329 struct berval bv = {0, NULL};
330 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
331 ber_dupbv( &new_ndn, &bv );
332 /* FIXME: why not call dnNormalize() w/o ctx? */
333 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
334 }
335
336 Debug( LDAP_DEBUG_TRACE,
337 "wt_modrdn: new ndn=%s\n", new_ndn.bv_val );
338
339 /* check new entry */
340 rc = wt_dn2entry(op->o_bd, wc, &new_ndn, &ne);
341 switch( rc ) {
342 case 0:
343 /* Allow rename to same DN */
344 if(e->e_id == ne->e_id){
345 break;
346 }
347 rs->sr_err = LDAP_ALREADY_EXISTS;
348 goto return_results;
349 break;
350 case WT_NOTFOUND:
351 break;
352 default:
353 Debug( LDAP_DEBUG_ANY,
354 "<== wt_modrdn: wt_dn2entry failed %s (%d)\n",
355 wiredtiger_strerror(rc), rc );
356 rs->sr_err = LDAP_OTHER;
357 rs->sr_text = "internal error";
358 goto return_results;
359 }
360
361 assert( op->orr_modlist != NULL );
362
363 if( op->o_preread ) {
364 if( preread_ctrl == NULL ) {
365 preread_ctrl = &ctrls[num_ctrls++];
366 ctrls[num_ctrls] = NULL;
367 }
368 if( slap_read_controls( op, rs, e,
369 &slap_pre_read_bv, preread_ctrl ) )
370 {
371 Debug( LDAP_DEBUG_TRACE,
372 "<== wt_modrdn: pre-read failed!\n" );
373 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
374 /* FIXME: is it correct to abort
375 * operation if control fails? */
376 goto return_results;
377 }
378 }
379 }
380
381 /* begin transaction */
382 rc = wc->session->begin_transaction(wc->session, NULL);
383 if( rc ) {
384 Debug( LDAP_DEBUG_TRACE,
385 "wt_modrdn: begin_transaction failed: %s (%d)\n",
386 wiredtiger_strerror(rc), rc );
387 rs->sr_err = LDAP_OTHER;
388 rs->sr_text = "begin_transaction failed";
389 goto return_results;
390 }
391 wc->is_begin_transaction = 1;
392 Debug( LDAP_DEBUG_TRACE,
393 "wt_modrdn: session id: %p\n", wc->session );
394
395 /* delete old DN */
396 rc = wt_dn2id_delete( op, wc, &e->e_nname);
397 if ( rc ) {
398 Debug(LDAP_DEBUG_TRACE,
399 "<== wt_modrdn: delete failed: %s (%d)\n",
400 wiredtiger_strerror(rc), rc );
401 rs->sr_err = LDAP_OTHER;
402 rs->sr_text = "dn2id delete failed";
403 goto return_results;
404 }
405
406 /* copy the entry, then override some fields */
407 dummy = *e;
408 dummy.e_name = new_dn;
409 dummy.e_nname = new_ndn;
410 dummy.e_attrs = NULL;
411
412 /* add new DN */
413 rc = wt_dn2id_add( op, wc, np?np->e_id:p->e_id, &dummy );
414 if ( rc ) {
415 Debug(LDAP_DEBUG_TRACE,
416 "<== wt_modrdn: add failed: %s (%d)\n",
417 wiredtiger_strerror(rc), rc );
418 rs->sr_err = LDAP_OTHER;
419 rs->sr_text = "DN add failed";
420 goto return_results;
421 }
422 dummy.e_attrs = e->e_attrs;
423
424 rc = wt_modify_internal( op, wc, op->orm_modlist,
425 &dummy, &rs->sr_text, textbuf, textlen );
426 if( rc != LDAP_SUCCESS ) {
427 Debug(LDAP_DEBUG_TRACE,
428 "<== wt_modrdn: modify failed: %s (%d)\n",
429 wiredtiger_strerror(rc), rc );
430 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
431 goto return_results;
432 }
433
434 /* update entry */
435 rc = wt_id2entry_update( op, wc, &dummy );
436 if ( rc != 0 ) {
437 Debug( LDAP_DEBUG_TRACE,
438 "wt_modrdn: id2entry update failed(%d)\n", rc );
439 if ( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
440 rs->sr_text = "entry too big";
441 } else {
442 rs->sr_err = LDAP_OTHER;
443 rs->sr_text = "entry update failed";
444 }
445 goto return_results;
446 }
447
448 if ( p_ndn.bv_len != 0 ) {
449 parent_is_glue = is_entry_glue(p);
450 /* TODO: glue entry handling */
451 }
452
453 if( op->o_postread ) {
454 if( postread_ctrl == NULL ) {
455 postread_ctrl = &ctrls[num_ctrls++];
456 ctrls[num_ctrls] = NULL;
457 }
458 if( slap_read_controls( op, rs, &dummy,
459 &slap_post_read_bv, postread_ctrl ) )
460 {
461 Debug( LDAP_DEBUG_TRACE,
462 "<== wt_modrdn: post-read failed!\n" );
463 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
464 /* FIXME: is it correct to abort
465 * operation if control fails? */
466 goto return_results;
467 }
468 }
469 }
470
471 if( op->o_noop ) {
472 rs->sr_err = LDAP_X_NO_OPERATION;
473 goto return_results;
474 }
475
476 rc = wc->session->commit_transaction(wc->session, NULL);
477 wc->is_begin_transaction = 0;
478 if( rc ) {
479 Debug( LDAP_DEBUG_TRACE,
480 "<== wt_modrdn: commit failed: %s (%d)\n",
481 wiredtiger_strerror(rc), rc );
482 rs->sr_err = LDAP_OTHER;
483 rs->sr_text = "commit failed";
484 goto return_results;
485 }
486
487 Debug(LDAP_DEBUG_TRACE,
488 "wt_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
489 op->o_noop ? " (no-op)" : "",
490 dummy.e_id, op->o_req_dn.bv_val );
491
492 rs->sr_err = LDAP_SUCCESS;
493 rs->sr_text = NULL;
494 if( num_ctrls ) rs->sr_ctrls = ctrls;
495
496 return_results:
497 if ( dummy.e_attrs ) {
498 attrs_free( dummy.e_attrs );
499 }
500 send_ldap_result( op, rs );
501
502 if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
503 op->o_delete_glue_parent = 1;
504 }
505
506 done:
507 if( wc && wc->is_begin_transaction ){
508 Debug( LDAP_DEBUG_TRACE, "wt_modrdn: rollback transaction\n" );
509 wc->session->rollback_transaction(wc->session, NULL);
510 wc->is_begin_transaction = 0;
511 }
512
513 slap_graduate_commit_csn( op );
514
515 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
516 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
517
518 /* free entry */
519 if( e != NULL ) {
520 wt_entry_return( e );
521 }
522 /* free parent entry */
523 if( p != NULL ) {
524 wt_entry_return( p );
525 }
526 /* free new entry */
527 if( ne != NULL ) {
528 wt_entry_return( ne );
529 }
530 /* free new parent entry */
531 if( np != NULL ) {
532 wt_entry_return( np );
533 }
534
535 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
536 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
537 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
538 }
539 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
540 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
541 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
542 }
543 return rs->sr_err;
544 }
545
546 /*
547 * Local variables:
548 * indent-tabs-mode: t
549 * tab-width: 4
550 * c-basic-offset: 4
551 * End:
552 */