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 = NULL;
96 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
98 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
100 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
101 ber_bvarray_add( &deref, &tmpbv );
104 deref = default_referral;
106 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
107 LDAP_SCOPE_DEFAULT );
110 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
112 rs->sr_err = LDAP_REFERRAL;
113 send_ldap_result( op, rs );
115 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
116 free( (char *)rs->sr_matched );
118 rs->sr_matched = NULL;
122 /* check entry for "entry" acl */
123 if ( ! access_allowed( op, e,
124 entry, NULL, ACL_WRITE, NULL ) )
127 LDAP_LOG( BACK_LDBM, ERR,
128 "ldbm_back_modrdn: no write access to entry of (%s)\n",
129 op->o_req_dn.bv_val, 0, 0 );
131 Debug( LDAP_DEBUG_TRACE,
132 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
136 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
137 "no write access to entry" );
142 if (!manageDSAit && is_entry_referral( e ) ) {
143 /* parent is a referral, don't allow add */
144 /* parent is an alias, don't allow add */
145 rs->sr_ref = get_entry_referrals( op, e );
148 LDAP_LOG( BACK_LDBM, INFO,
149 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
151 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
155 rs->sr_err = LDAP_REFERRAL;
156 rs->sr_matched = e->e_name.bv_val;
157 send_ldap_result( op, rs );
159 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
161 rs->sr_matched = NULL;
165 if ( has_children( op->o_bd, e ) ) {
167 LDAP_LOG( BACK_LDBM, INFO,
168 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
170 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
174 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
175 "subtree rename not supported" );
179 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
180 p_ndn = slap_empty_bv ;
182 dnParent( &e->e_nname, &p_ndn );
185 if ( p_ndn.bv_len != 0 ) {
186 /* Make sure parent entry exist and we can write its
190 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
192 LDAP_LOG( BACK_LDBM, INFO,
193 "ldbm_back_modrdn: parent of %s does not exist\n",
196 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
200 send_ldap_error( op, rs, LDAP_OTHER,
201 "parent entry does not exist" );
206 /* check parent for "children" acl */
207 if ( ! access_allowed( op, p,
208 children, NULL, ACL_WRITE, NULL ) )
211 LDAP_LOG( BACK_LDBM, INFO,
212 "ldbm_back_modrdn: no access to parent of (%s)\n",
215 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
219 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
225 LDAP_LOG( BACK_LDBM, DETAIL1,
226 "ldbm_back_modrdn: wr to children of entry %s OK\n",
227 p_ndn.bv_val, 0, 0 );
229 Debug( LDAP_DEBUG_TRACE,
230 "ldbm_back_modrdn: wr to children of entry %s OK\n",
231 p_ndn.bv_val, 0, 0 );
234 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
235 p_dn = slap_empty_bv;
237 dnParent( &e->e_name, &p_dn );
241 LDAP_LOG( BACK_LDBM, DETAIL1,
242 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
244 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
249 /* no parent, must be root to modify rdn */
250 isroot = be_isroot( op->o_bd, &op->o_ndn );
252 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
254 p = (Entry *)&slap_entry_root;
256 can_access = access_allowed( op, p,
257 children, NULL, ACL_WRITE, NULL );
260 /* check parent for "children" acl */
261 if ( ! can_access ) {
263 LDAP_LOG( BACK_LDBM, ERR,
264 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
266 Debug( LDAP_DEBUG_TRACE,
267 "<=- ldbm_back_modrdn: no "
268 "access to parent\n", 0, 0, 0 );
271 send_ldap_error( op, rs,
272 LDAP_INSUFFICIENT_ACCESS,
279 LDAP_LOG( BACK_LDBM, ERR,
280 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
283 Debug( LDAP_DEBUG_TRACE,
284 "<=- ldbm_back_modrdn: no parent & "
285 "not root\n", 0, 0, 0);
288 send_ldap_error( op, rs,
289 LDAP_INSUFFICIENT_ACCESS,
296 LDAP_LOG( BACK_LDBM, INFO,
297 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
299 Debug( LDAP_DEBUG_TRACE,
300 "ldbm_back_modrdn: no parent, locked root\n",
305 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
307 if ( op->oq_modrdn.rs_newSup != NULL ) {
309 LDAP_LOG( BACK_LDBM, DETAIL1,
310 "ldbm_back_modrdn: new parent \"%s\" requested\n",
311 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
313 Debug( LDAP_DEBUG_TRACE,
314 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
315 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
318 np_ndn = op->oq_modrdn.rs_nnewSup;
320 /* newSuperior == oldParent? */
321 if ( dn_match( &p_ndn, np_ndn ) ) {
323 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
324 "new parent\"%s\" seems to be the same as the "
325 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
327 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
328 "new parent\"%s\" seems to be the same as the "
329 "old parent \"%s\"\n",
330 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
333 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
337 if ( op->oq_modrdn.rs_newSup != NULL ) {
338 /* newSuperior == entry being moved?, if so ==> ERROR */
339 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
341 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
342 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
344 LDAP_LOG( BACK_LDBM, ERR,
345 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
346 np_ndn->bv_val, 0, 0 );
348 Debug( LDAP_DEBUG_TRACE,
349 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
350 np_ndn->bv_val, 0, 0);
353 send_ldap_error( op, rs, LDAP_OTHER,
354 "newSuperior not found" );
359 LDAP_LOG( BACK_LDBM, DETAIL1,
360 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
361 (void *) np, np->e_id, 0 );
363 Debug( LDAP_DEBUG_TRACE,
364 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
365 (void *) np, np->e_id, 0 );
368 /* check newSuperior for "children" acl */
369 if ( !access_allowed( op, np, children, NULL,
373 LDAP_LOG( BACK_LDBM, INFO,
374 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
376 Debug( LDAP_DEBUG_TRACE,
377 "ldbm_back_modrdn: no wr to newSup children\n",
381 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
385 if ( is_entry_alias( np ) ) {
386 /* parent is an alias, don't allow add */
388 LDAP_LOG( BACK_LDBM, INFO,
389 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
391 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
395 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
396 "newSuperior is an alias" );
401 if ( is_entry_referral( np ) ) {
402 /* parent is a referral, don't allow add */
404 LDAP_LOG( BACK_LDBM, INFO,
405 "ldbm_back_modrdn: entry (%s) is a referral\n",
408 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
412 send_ldap_error( op, rs, LDAP_OTHER,
413 "newSuperior is a referral" );
420 /* no parent, must be root to modify newSuperior */
421 if ( isroot == -1 ) {
422 isroot = be_isroot( op->o_bd, &op->o_ndn );
426 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
428 np = (Entry *)&slap_entry_root;
430 can_access = access_allowed( op, np,
431 children, NULL, ACL_WRITE, NULL );
434 /* check parent for "children" acl */
435 if ( ! can_access ) {
437 LDAP_LOG( BACK_LDBM, ERR,
438 "ldbm_back_modrdn: no access "
439 "to new superior \"\"\n", 0, 0, 0 );
441 Debug( LDAP_DEBUG_TRACE,
442 "<=- ldbm_back_modrdn: no "
443 "access to new superior\n", 0, 0, 0 );
446 send_ldap_error( op, rs,
447 LDAP_INSUFFICIENT_ACCESS,
454 LDAP_LOG( BACK_LDBM, ERR,
455 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
458 Debug( LDAP_DEBUG_TRACE,
459 "<=- ldbm_back_modrdn: \"\" "
460 "not allowed as new superior\n",
464 send_ldap_error( op, rs,
465 LDAP_INSUFFICIENT_ACCESS,
473 LDAP_LOG( BACK_LDBM, DETAIL1,
474 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
476 Debug( LDAP_DEBUG_TRACE,
477 "ldbm_back_modrdn: wr to new parent's children OK\n",
481 new_parent_dn = op->oq_modrdn.rs_newSup;
484 /* Build target dn and make sure target entry doesn't exist already. */
485 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL );
486 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
489 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
490 new_ndn.bv_val, 0, 0 );
492 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
493 new_ndn.bv_val, 0, 0 );
496 /* check for abandon */
497 if ( op->o_abandon ) {
501 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
502 /* if (rc_id) something bad happened to ldbm cache */
503 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
504 send_ldap_result( op, rs );
509 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
510 new_ndn.bv_val, 0, 0 );
512 Debug( LDAP_DEBUG_TRACE,
513 "ldbm_back_modrdn: new ndn=%s does not exist\n",
514 new_ndn.bv_val, 0, 0 );
517 /* Get attribute type and attribute value of our new rdn, we will
518 * need to add that to our new entry
520 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
521 LDAP_DN_FORMAT_LDAP ) )
524 LDAP_LOG ( OPERATION, ERR,
525 "ldbm_back_modrdn: can't figure out "
526 "type(s)/values(s) of newrdn\n",
529 Debug( LDAP_DEBUG_TRACE,
530 "ldbm_back_modrdn: can't figure out "
531 "type(s)/values(s) of newrdn\n",
538 LDAP_LOG ( OPERATION, RESULTS,
539 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
540 "new_rdn_val=\"%s\"\n",
541 new_rdn[ 0 ]->la_attr.bv_val,
542 new_rdn[ 0 ]->la_value.bv_val, 0 );
544 Debug( LDAP_DEBUG_TRACE,
545 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
546 "new_rdn_val=\"%s\"\n",
547 new_rdn[ 0 ]->la_attr.bv_val,
548 new_rdn[ 0 ]->la_value.bv_val, 0 );
551 if ( op->oq_modrdn.rs_deleteoldrdn ) {
552 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
553 LDAP_DN_FORMAT_LDAP ) )
556 LDAP_LOG ( OPERATION, ERR,
557 "ldbm_back_modrdn: can't figure out "
558 "type(s)/values(s) of old_rdn\n",
561 Debug( LDAP_DEBUG_TRACE,
562 "ldbm_back_modrdn: can't figure out "
563 "the old_rdn type(s)/value(s)\n",
571 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
573 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
577 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
582 /* check for abandon */
583 if ( op->o_abandon ) {
588 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
589 send_ldap_error( op, rs, LDAP_OTHER,
590 "DN index delete fail" );
594 (void) cache_delete_entry( &li->li_cache, e );
597 /* XXX: there is no going back! */
602 e->e_nname = new_ndn;
603 new_dn.bv_val = NULL;
604 new_ndn.bv_val = NULL;
606 /* NOTE: after this you must not free new_dn or new_ndn!
607 * They are used by cache.
611 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
612 send_ldap_error( op, rs, LDAP_OTHER,
613 "DN index add failed" );
617 /* modify memory copy of entry */
618 rc_id = ldbm_modify_internal( op, &mod[0], e,
619 &rs->sr_text, textbuf, textlen );
627 send_ldap_result( op, rs );
631 /* here we may try to delete the newly added dn */
632 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
633 /* we already are in trouble ... */
639 (void) cache_update_entry( &li->li_cache, e );
642 if ( id2entry_add( op->o_bd, e ) != 0 ) {
643 send_ldap_error( op, rs, LDAP_OTHER,
644 "entry update failed" );
648 rs->sr_err = LDAP_SUCCESS;
650 send_ldap_result( op, rs );
652 cache_entry_commit( e );
655 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
656 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
658 /* LDAP v2 supporting correct attribute handling. */
659 if ( new_rdn != NULL ) {
660 ldap_rdnfree( new_rdn );
662 if ( old_rdn != NULL ) {
663 ldap_rdnfree( old_rdn );
667 for (; mod; mod = tmp ) {
668 /* slap_modrdn2mods does things one way,
669 * slap_mods_opattrs does it differently
671 if ( mod->sml_op != SLAP_MOD_SOFTADD &&
672 mod->sml_op != LDAP_MOD_DELETE ) break;
673 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
677 slap_mods_free( mod );
680 /* LDAP v3 Support */
682 /* free new parent and writer lock */
683 cache_return_entry_w( &li->li_cache, np );
687 /* free parent and writer lock */
688 cache_return_entry_w( &li->li_cache, p );
691 /* free entry and writer lock */
692 cache_return_entry_w( &li->li_cache, e );
693 if ( rc == MUST_DESTROY ) {
694 /* if rc == MUST_DESTROY the entry is uncached
695 * and its private data is destroyed;
696 * the entry must be freed */
699 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);