1 /* modrdn.c - ldbm backend modrdn routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 * LDAP v3 newSuperior support. Add new rdn as an attribute.
10 * (Full support for v2 also used software/ideas contributed
11 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
14 * Copyright 1999, Juan C. Gomez, All rights reserved.
15 * This software is not subject to any license of Silicon Graphics
16 * Inc. or Purdue University.
18 * Redistribution and use in source and binary forms are permitted
19 * without restriction or fee of any kind as long as this notice
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
40 AttributeDescription *children = slap_schema.si_ad_children;
41 AttributeDescription *entry = slap_schema.si_ad_entry;
42 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
43 struct berval p_dn, p_ndn;
44 struct berval new_dn = { 0, NULL}, new_ndn = { 0, NULL };
47 /* LDAP v2 supporting correct attribute handling. */
48 LDAPRDN new_rdn = NULL;
49 LDAPRDN old_rdn = NULL;
51 #define CAN_ROLLBACK -1
52 #define MUST_DESTROY 1
53 int rc = CAN_ROLLBACK;
56 const char *text = NULL;
57 char textbuf[SLAP_TEXT_BUFLEN];
58 size_t textlen = sizeof textbuf;
59 /* Added to support newSuperior */
60 Entry *np = NULL; /* newSuperior Entry */
61 struct berval *np_ndn = NULL; /* newSuperior ndn */
62 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 /* Used to interface with ldbm_modify_internal() */
64 Modifications *mod = NULL; /* Used to delete old/add new rdn */
65 int manageDSAit = get_manageDSAit( op );
68 LDAP_LOG( BACK_LDBM, ENTRY,
69 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
70 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
71 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
73 Debug( LDAP_DEBUG_TRACE,
74 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n",
75 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
76 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
77 ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
80 /* grab giant lock for writing */
81 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
83 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
85 /* get entry with writer lock */
86 /* FIXME: dn2entry() should return non-glue entry */
87 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
88 if ( matched != NULL ) {
89 rs->sr_matched = strdup( matched->e_dn );
90 rs->sr_ref = is_entry_referral( matched )
91 ? get_entry_referrals( op, matched )
93 cache_return_entry_r( &li->li_cache, matched );
95 BerVarray deref = op->o_bd->be_syncinfo ?
96 op->o_bd->be_syncinfo->si_provideruri_bv : default_referral;
97 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
101 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
103 rs->sr_err = LDAP_REFERRAL;
104 send_ldap_result( op, rs );
106 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
107 free( (char *)rs->sr_matched );
109 rs->sr_matched = NULL;
113 /* check entry for "entry" acl */
114 if ( ! access_allowed( op, e,
115 entry, NULL, ACL_WRITE, NULL ) )
118 LDAP_LOG( BACK_LDBM, ERR,
119 "ldbm_back_modrdn: no write access to entry of (%s)\n",
120 op->o_req_dn.bv_val, 0, 0 );
122 Debug( LDAP_DEBUG_TRACE,
123 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
127 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
128 "no write access to entry" );
133 if (!manageDSAit && is_entry_referral( e ) ) {
134 /* parent is a referral, don't allow add */
135 /* parent is an alias, don't allow add */
136 rs->sr_ref = get_entry_referrals( op, e );
139 LDAP_LOG( BACK_LDBM, INFO,
140 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
142 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
146 rs->sr_err = LDAP_REFERRAL;
147 rs->sr_matched = e->e_name.bv_val;
148 send_ldap_result( op, rs );
150 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
152 rs->sr_matched = NULL;
156 if ( has_children( op->o_bd, e ) ) {
158 LDAP_LOG( BACK_LDBM, INFO,
159 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
161 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
165 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
166 "subtree rename not supported" );
170 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
171 p_ndn = slap_empty_bv ;
173 dnParent( &e->e_nname, &p_ndn );
176 if ( p_ndn.bv_len != 0 ) {
177 /* Make sure parent entry exist and we can write its
181 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
183 LDAP_LOG( BACK_LDBM, INFO,
184 "ldbm_back_modrdn: parent of %s does not exist\n",
187 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
191 send_ldap_error( op, rs, LDAP_OTHER,
192 "parent entry does not exist" );
197 /* check parent for "children" acl */
198 if ( ! access_allowed( op, p,
199 children, NULL, ACL_WRITE, NULL ) )
202 LDAP_LOG( BACK_LDBM, INFO,
203 "ldbm_back_modrdn: no access to parent of (%s)\n",
206 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
210 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
216 LDAP_LOG( BACK_LDBM, DETAIL1,
217 "ldbm_back_modrdn: wr to children of entry %s OK\n",
218 p_ndn.bv_val, 0, 0 );
220 Debug( LDAP_DEBUG_TRACE,
221 "ldbm_back_modrdn: wr to children of entry %s OK\n",
222 p_ndn.bv_val, 0, 0 );
225 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
226 p_dn = slap_empty_bv;
228 dnParent( &e->e_name, &p_dn );
232 LDAP_LOG( BACK_LDBM, DETAIL1,
233 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
235 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
240 /* no parent, must be root to modify rdn */
241 isroot = be_isroot( op->o_bd, &op->o_ndn );
243 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
245 p = (Entry *)&slap_entry_root;
247 can_access = access_allowed( op, p,
248 children, NULL, ACL_WRITE, NULL );
251 /* check parent for "children" acl */
252 if ( ! can_access ) {
254 LDAP_LOG( BACK_LDBM, ERR,
255 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
257 Debug( LDAP_DEBUG_TRACE,
258 "<=- ldbm_back_modrdn: no "
259 "access to parent\n", 0, 0, 0 );
262 send_ldap_error( op, rs,
263 LDAP_INSUFFICIENT_ACCESS,
270 LDAP_LOG( BACK_LDBM, ERR,
271 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
274 Debug( LDAP_DEBUG_TRACE,
275 "<=- ldbm_back_modrdn: no parent & "
276 "not root\n", 0, 0, 0);
279 send_ldap_error( op, rs,
280 LDAP_INSUFFICIENT_ACCESS,
287 LDAP_LOG( BACK_LDBM, INFO,
288 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
290 Debug( LDAP_DEBUG_TRACE,
291 "ldbm_back_modrdn: no parent, locked root\n",
296 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
298 if ( op->oq_modrdn.rs_newSup != NULL ) {
300 LDAP_LOG( BACK_LDBM, DETAIL1,
301 "ldbm_back_modrdn: new parent \"%s\" requested\n",
302 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
304 Debug( LDAP_DEBUG_TRACE,
305 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
306 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
309 np_ndn = op->oq_modrdn.rs_nnewSup;
311 /* newSuperior == oldParent? */
312 if ( dn_match( &p_ndn, np_ndn ) ) {
314 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
315 "new parent\"%s\" seems to be the same as the "
316 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
318 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
319 "new parent\"%s\" seems to be the same as the "
320 "old parent \"%s\"\n",
321 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
324 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
328 if ( op->oq_modrdn.rs_newSup != NULL ) {
329 /* newSuperior == entry being moved?, if so ==> ERROR */
330 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
332 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
333 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
335 LDAP_LOG( BACK_LDBM, ERR,
336 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
337 np_ndn->bv_val, 0, 0 );
339 Debug( LDAP_DEBUG_TRACE,
340 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
341 np_ndn->bv_val, 0, 0);
344 send_ldap_error( op, rs, LDAP_OTHER,
345 "newSuperior not found" );
350 LDAP_LOG( BACK_LDBM, DETAIL1,
351 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
352 (void *) np, np->e_id, 0 );
354 Debug( LDAP_DEBUG_TRACE,
355 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
356 (void *) np, np->e_id, 0 );
359 /* check newSuperior for "children" acl */
360 if ( !access_allowed( op, np, children, NULL,
364 LDAP_LOG( BACK_LDBM, INFO,
365 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
367 Debug( LDAP_DEBUG_TRACE,
368 "ldbm_back_modrdn: no wr to newSup children\n",
372 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
376 if ( is_entry_alias( np ) ) {
377 /* parent is an alias, don't allow add */
379 LDAP_LOG( BACK_LDBM, INFO,
380 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
382 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
386 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
387 "newSuperior is an alias" );
392 if ( is_entry_referral( np ) ) {
393 /* parent is a referral, don't allow add */
395 LDAP_LOG( BACK_LDBM, INFO,
396 "ldbm_back_modrdn: entry (%s) is a referral\n",
399 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
403 send_ldap_error( op, rs, LDAP_OTHER,
404 "newSuperior is a referral" );
411 /* no parent, must be root to modify newSuperior */
412 if ( isroot == -1 ) {
413 isroot = be_isroot( op->o_bd, &op->o_ndn );
417 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
419 np = (Entry *)&slap_entry_root;
421 can_access = access_allowed( op, np,
422 children, NULL, ACL_WRITE, NULL );
425 /* check parent for "children" acl */
426 if ( ! can_access ) {
428 LDAP_LOG( BACK_LDBM, ERR,
429 "ldbm_back_modrdn: no access "
430 "to new superior \"\"\n", 0, 0, 0 );
432 Debug( LDAP_DEBUG_TRACE,
433 "<=- ldbm_back_modrdn: no "
434 "access to new superior\n", 0, 0, 0 );
437 send_ldap_error( op, rs,
438 LDAP_INSUFFICIENT_ACCESS,
445 LDAP_LOG( BACK_LDBM, ERR,
446 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
449 Debug( LDAP_DEBUG_TRACE,
450 "<=- ldbm_back_modrdn: \"\" "
451 "not allowed as new superior\n",
455 send_ldap_error( op, rs,
456 LDAP_INSUFFICIENT_ACCESS,
464 LDAP_LOG( BACK_LDBM, DETAIL1,
465 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
467 Debug( LDAP_DEBUG_TRACE,
468 "ldbm_back_modrdn: wr to new parent's children OK\n",
472 new_parent_dn = op->oq_modrdn.rs_newSup;
475 /* Build target dn and make sure target entry doesn't exist already. */
476 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL );
477 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
480 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
481 new_ndn.bv_val, 0, 0 );
483 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
484 new_ndn.bv_val, 0, 0 );
487 /* check for abandon */
488 if ( op->o_abandon ) {
492 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
493 /* if (rc_id) something bad happened to ldbm cache */
494 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
495 send_ldap_result( op, rs );
500 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
501 new_ndn.bv_val, 0, 0 );
503 Debug( LDAP_DEBUG_TRACE,
504 "ldbm_back_modrdn: new ndn=%s does not exist\n",
505 new_ndn.bv_val, 0, 0 );
508 /* Get attribute type and attribute value of our new rdn, we will
509 * need to add that to our new entry
511 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
512 LDAP_DN_FORMAT_LDAP ) )
515 LDAP_LOG ( OPERATION, ERR,
516 "ldbm_back_modrdn: can't figure out "
517 "type(s)/values(s) of newrdn\n",
520 Debug( LDAP_DEBUG_TRACE,
521 "ldbm_back_modrdn: can't figure out "
522 "type(s)/values(s) of newrdn\n",
529 LDAP_LOG ( OPERATION, RESULTS,
530 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
531 "new_rdn_val=\"%s\"\n",
532 new_rdn[ 0 ]->la_attr.bv_val,
533 new_rdn[ 0 ]->la_value.bv_val, 0 );
535 Debug( LDAP_DEBUG_TRACE,
536 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
537 "new_rdn_val=\"%s\"\n",
538 new_rdn[ 0 ]->la_attr.bv_val,
539 new_rdn[ 0 ]->la_value.bv_val, 0 );
542 if ( op->oq_modrdn.rs_deleteoldrdn ) {
543 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
544 LDAP_DN_FORMAT_LDAP ) )
547 LDAP_LOG ( OPERATION, ERR,
548 "ldbm_back_modrdn: can't figure out "
549 "type(s)/values(s) of old_rdn\n",
552 Debug( LDAP_DEBUG_TRACE,
553 "ldbm_back_modrdn: can't figure out "
554 "the old_rdn type(s)/value(s)\n",
562 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
564 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
568 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
573 /* check for abandon */
574 if ( op->o_abandon ) {
579 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
580 send_ldap_error( op, rs, LDAP_OTHER,
581 "DN index delete fail" );
585 (void) cache_delete_entry( &li->li_cache, e );
588 /* XXX: there is no going back! */
593 e->e_nname = new_ndn;
594 new_dn.bv_val = NULL;
595 new_ndn.bv_val = NULL;
597 /* NOTE: after this you must not free new_dn or new_ndn!
598 * They are used by cache.
602 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
603 send_ldap_error( op, rs, LDAP_OTHER,
604 "DN index add failed" );
608 /* modify memory copy of entry */
609 rc_id = ldbm_modify_internal( op, &mod[0], e,
610 &rs->sr_text, textbuf, textlen );
618 send_ldap_result( op, rs );
622 /* here we may try to delete the newly added dn */
623 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
624 /* we already are in trouble ... */
630 (void) cache_update_entry( &li->li_cache, e );
633 if ( id2entry_add( op->o_bd, e ) != 0 ) {
634 send_ldap_error( op, rs, LDAP_OTHER,
635 "entry update failed" );
639 rs->sr_err = LDAP_SUCCESS;
641 send_ldap_result( op, rs );
643 cache_entry_commit( e );
646 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
647 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
649 /* LDAP v2 supporting correct attribute handling. */
650 if ( new_rdn != NULL ) {
651 ldap_rdnfree( new_rdn );
653 if ( old_rdn != NULL ) {
654 ldap_rdnfree( old_rdn );
658 for (; mod; mod = tmp ) {
659 /* slap_modrdn2mods does things one way,
660 * slap_mods_opattrs does it differently
662 if ( mod->sml_op != SLAP_MOD_SOFTADD &&
663 mod->sml_op != LDAP_MOD_DELETE ) break;
664 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
668 slap_mods_free( mod );
671 /* LDAP v3 Support */
673 /* free new parent and writer lock */
674 cache_return_entry_w( &li->li_cache, np );
678 /* free parent and writer lock */
679 cache_return_entry_w( &li->li_cache, p );
682 /* free entry and writer lock */
683 cache_return_entry_w( &li->li_cache, e );
684 if ( rc == MUST_DESTROY ) {
685 /* if rc == MUST_DESTROY the entry is uncached
686 * and its private data is destroyed;
687 * the entry must be freed */
690 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);