Home | History | Annotate | Line # | Download | only in back-mdb
modrdn.c revision 1.1
      1  1.1  tron /*	$NetBSD: modrdn.c,v 1.1 2014/05/28 09:58:50 tron Exp $	*/
      2  1.1  tron 
      3  1.1  tron /* modrdn.c - mdb backend modrdn 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  tron  * Copyright 2000-2014 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  tron #include "portable.h"
     20  1.1  tron 
     21  1.1  tron #include <stdio.h>
     22  1.1  tron #include <ac/string.h>
     23  1.1  tron 
     24  1.1  tron #include "back-mdb.h"
     25  1.1  tron 
     26  1.1  tron int
     27  1.1  tron mdb_modrdn( Operation	*op, SlapReply *rs )
     28  1.1  tron {
     29  1.1  tron 	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
     30  1.1  tron 	AttributeDescription *children = slap_schema.si_ad_children;
     31  1.1  tron 	AttributeDescription *entry = slap_schema.si_ad_entry;
     32  1.1  tron 	struct berval	p_dn, p_ndn;
     33  1.1  tron 	struct berval	new_dn = {0, NULL}, new_ndn = {0, NULL};
     34  1.1  tron 	Entry		*e = NULL;
     35  1.1  tron 	Entry		*p = NULL;
     36  1.1  tron 	/* LDAP v2 supporting correct attribute handling. */
     37  1.1  tron 	char textbuf[SLAP_TEXT_BUFLEN];
     38  1.1  tron 	size_t textlen = sizeof textbuf;
     39  1.1  tron 	MDB_txn		*txn = NULL;
     40  1.1  tron 	MDB_cursor	*mc;
     41  1.1  tron 	struct mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
     42  1.1  tron 	Entry dummy = {0};
     43  1.1  tron 
     44  1.1  tron 	Entry		*np = NULL;			/* newSuperior Entry */
     45  1.1  tron 	struct berval	*np_dn = NULL;			/* newSuperior dn */
     46  1.1  tron 	struct berval	*np_ndn = NULL;			/* newSuperior ndn */
     47  1.1  tron 	struct berval	*new_parent_dn = NULL;	/* np_dn, p_dn, or NULL */
     48  1.1  tron 
     49  1.1  tron 	int		manageDSAit = get_manageDSAit( op );
     50  1.1  tron 
     51  1.1  tron 	ID nid, nsubs;
     52  1.1  tron 	LDAPControl **preread_ctrl = NULL;
     53  1.1  tron 	LDAPControl **postread_ctrl = NULL;
     54  1.1  tron 	LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
     55  1.1  tron 	int num_ctrls = 0;
     56  1.1  tron 
     57  1.1  tron 	int parent_is_glue = 0;
     58  1.1  tron 	int parent_is_leaf = 0;
     59  1.1  tron 
     60  1.1  tron #ifdef LDAP_X_TXN
     61  1.1  tron 	int settle = 0;
     62  1.1  tron #endif
     63  1.1  tron 
     64  1.1  tron 	Debug( LDAP_DEBUG_TRACE, "==>" LDAP_XSTRING(mdb_modrdn) "(%s,%s,%s)\n",
     65  1.1  tron 		op->o_req_dn.bv_val,op->oq_modrdn.rs_newrdn.bv_val,
     66  1.1  tron 		op->oq_modrdn.rs_newSup ? op->oq_modrdn.rs_newSup->bv_val : "NULL" );
     67  1.1  tron 
     68  1.1  tron #ifdef LDAP_X_TXN
     69  1.1  tron 	if( op->o_txnSpec ) {
     70  1.1  tron 		/* acquire connection lock */
     71  1.1  tron 		ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
     72  1.1  tron 		if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
     73  1.1  tron 			rs->sr_text = "invalid transaction identifier";
     74  1.1  tron 			rs->sr_err = LDAP_X_TXN_ID_INVALID;
     75  1.1  tron 			goto txnReturn;
     76  1.1  tron 		} else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
     77  1.1  tron 			settle=1;
     78  1.1  tron 			goto txnReturn;
     79  1.1  tron 		}
     80  1.1  tron 
     81  1.1  tron 		if( op->o_conn->c_txn_backend == NULL ) {
     82  1.1  tron 			op->o_conn->c_txn_backend = op->o_bd;
     83  1.1  tron 
     84  1.1  tron 		} else if( op->o_conn->c_txn_backend != op->o_bd ) {
     85  1.1  tron 			rs->sr_text = "transaction cannot span multiple database contexts";
     86  1.1  tron 			rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
     87  1.1  tron 			goto txnReturn;
     88  1.1  tron 		}
     89  1.1  tron 
     90  1.1  tron 		/* insert operation into transaction */
     91  1.1  tron 
     92  1.1  tron 		rs->sr_text = "transaction specified";
     93  1.1  tron 		rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
     94  1.1  tron 
     95  1.1  tron txnReturn:
     96  1.1  tron 		/* release connection lock */
     97  1.1  tron 		ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
     98  1.1  tron 
     99  1.1  tron 		if( !settle ) {
    100  1.1  tron 			send_ldap_result( op, rs );
    101  1.1  tron 			return rs->sr_err;
    102  1.1  tron 		}
    103  1.1  tron 	}
    104  1.1  tron #endif
    105  1.1  tron 
    106  1.1  tron 	ctrls[num_ctrls] = NULL;
    107  1.1  tron 
    108  1.1  tron 	/* begin transaction */
    109  1.1  tron 	rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
    110  1.1  tron 	rs->sr_text = NULL;
    111  1.1  tron 	if( rs->sr_err != 0 ) {
    112  1.1  tron 		Debug( LDAP_DEBUG_TRACE,
    113  1.1  tron 			LDAP_XSTRING(mdb_modrdn) ": txn_begin failed: "
    114  1.1  tron 			"%s (%d)\n", mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    115  1.1  tron 		rs->sr_err = LDAP_OTHER;
    116  1.1  tron 		rs->sr_text = "internal error";
    117  1.1  tron 		goto return_results;
    118  1.1  tron 	}
    119  1.1  tron 	txn = moi->moi_txn;
    120  1.1  tron 
    121  1.1  tron 	slap_mods_opattrs( op, &op->orr_modlist, 1 );
    122  1.1  tron 
    123  1.1  tron 	if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
    124  1.1  tron #ifdef MDB_MULTIPLE_SUFFIXES
    125  1.1  tron 		/* Allow renaming one suffix entry to another */
    126  1.1  tron 		p_ndn = slap_empty_bv;
    127  1.1  tron #else
    128  1.1  tron 		/* There can only be one suffix entry */
    129  1.1  tron 		rs->sr_err = LDAP_NAMING_VIOLATION;
    130  1.1  tron 		rs->sr_text = "cannot rename suffix entry";
    131  1.1  tron 		goto return_results;
    132  1.1  tron #endif
    133  1.1  tron 	} else {
    134  1.1  tron 		dnParent( &op->o_req_ndn, &p_ndn );
    135  1.1  tron 	}
    136  1.1  tron 	np_ndn = &p_ndn;
    137  1.1  tron 	/* Make sure parent entry exist and we can write its
    138  1.1  tron 	 * children.
    139  1.1  tron 	 */
    140  1.1  tron 	rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mc );
    141  1.1  tron 	if ( rs->sr_err != 0 ) {
    142  1.1  tron 		Debug(LDAP_DEBUG_TRACE,
    143  1.1  tron 			"<=- " LDAP_XSTRING(mdb_modrdn)
    144  1.1  tron 			": cursor_open failed: %s (%d)\n",
    145  1.1  tron 			mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    146  1.1  tron 		rs->sr_err = LDAP_OTHER;
    147  1.1  tron 		rs->sr_text = "DN cursor_open failed";
    148  1.1  tron 		goto return_results;
    149  1.1  tron 	}
    150  1.1  tron 	rs->sr_err = mdb_dn2entry( op, txn, mc, &p_ndn, &p, NULL, 0 );
    151  1.1  tron 	switch( rs->sr_err ) {
    152  1.1  tron 	case MDB_NOTFOUND:
    153  1.1  tron 		Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(mdb_modrdn)
    154  1.1  tron 			": parent does not exist\n", 0, 0, 0);
    155  1.1  tron 		rs->sr_ref = referral_rewrite( default_referral, NULL,
    156  1.1  tron 					&op->o_req_dn, LDAP_SCOPE_DEFAULT );
    157  1.1  tron 		rs->sr_err = LDAP_REFERRAL;
    158  1.1  tron 
    159  1.1  tron 		send_ldap_result( op, rs );
    160  1.1  tron 
    161  1.1  tron 		ber_bvarray_free( rs->sr_ref );
    162  1.1  tron 		goto done;
    163  1.1  tron 	case 0:
    164  1.1  tron 		break;
    165  1.1  tron 	case LDAP_BUSY:
    166  1.1  tron 		rs->sr_text = "ldap server busy";
    167  1.1  tron 		goto return_results;
    168  1.1  tron 	default:
    169  1.1  tron 		rs->sr_err = LDAP_OTHER;
    170  1.1  tron 		rs->sr_text = "internal error";
    171  1.1  tron 		goto return_results;
    172  1.1  tron 	}
    173  1.1  tron 
    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,
    177  1.1  tron 		op->oq_modrdn.rs_newSup == NULL ?
    178  1.1  tron 			ACL_WRITE : ACL_WDEL,
    179  1.1  tron 		NULL );
    180  1.1  tron 
    181  1.1  tron 	if ( ! rs->sr_err ) {
    182  1.1  tron 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    183  1.1  tron 		Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
    184  1.1  tron 			0, 0 );
    185  1.1  tron 		rs->sr_text = "no write access to parent's children";
    186  1.1  tron 		goto return_results;
    187  1.1  tron 	}
    188  1.1  tron 
    189  1.1  tron 	Debug( LDAP_DEBUG_TRACE,
    190  1.1  tron 		LDAP_XSTRING(mdb_modrdn) ": wr to children "
    191  1.1  tron 		"of entry %s OK\n", p_ndn.bv_val, 0, 0 );
    192  1.1  tron 
    193  1.1  tron 	if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
    194  1.1  tron 		p_dn = slap_empty_bv;
    195  1.1  tron 	} else {
    196  1.1  tron 		dnParent( &op->o_req_dn, &p_dn );
    197  1.1  tron 	}
    198  1.1  tron 
    199  1.1  tron 	Debug( LDAP_DEBUG_TRACE,
    200  1.1  tron 		LDAP_XSTRING(mdb_modrdn) ": parent dn=%s\n",
    201  1.1  tron 		p_dn.bv_val, 0, 0 );
    202  1.1  tron 
    203  1.1  tron 	/* get entry */
    204  1.1  tron 	rs->sr_err = mdb_dn2entry( op, txn, mc, &op->o_req_ndn, &e, &nsubs, 0 );
    205  1.1  tron 	switch( rs->sr_err ) {
    206  1.1  tron 	case MDB_NOTFOUND:
    207  1.1  tron 		e = p;
    208  1.1  tron 		p = NULL;
    209  1.1  tron 	case 0:
    210  1.1  tron 		break;
    211  1.1  tron 	case LDAP_BUSY:
    212  1.1  tron 		rs->sr_text = "ldap server busy";
    213  1.1  tron 		goto return_results;
    214  1.1  tron 	default:
    215  1.1  tron 		rs->sr_err = LDAP_OTHER;
    216  1.1  tron 		rs->sr_text = "internal error";
    217  1.1  tron 		goto return_results;
    218  1.1  tron 	}
    219  1.1  tron 
    220  1.1  tron 	/* FIXME: dn2entry() should return non-glue entry */
    221  1.1  tron 	if (( rs->sr_err == MDB_NOTFOUND ) ||
    222  1.1  tron 		( !manageDSAit && e && is_entry_glue( e )))
    223  1.1  tron 	{
    224  1.1  tron 		if( e != NULL ) {
    225  1.1  tron 			rs->sr_matched = ch_strdup( e->e_dn );
    226  1.1  tron 			if ( is_entry_referral( e )) {
    227  1.1  tron 				BerVarray ref = get_entry_referrals( op, e );
    228  1.1  tron 				rs->sr_ref = referral_rewrite( ref, &e->e_name,
    229  1.1  tron 					&op->o_req_dn, LDAP_SCOPE_DEFAULT );
    230  1.1  tron 				ber_bvarray_free( ref );
    231  1.1  tron 			} else {
    232  1.1  tron 				rs->sr_ref = NULL;
    233  1.1  tron 			}
    234  1.1  tron 			mdb_entry_return( op, e );
    235  1.1  tron 			e = NULL;
    236  1.1  tron 
    237  1.1  tron 		} else {
    238  1.1  tron 			rs->sr_ref = referral_rewrite( default_referral, NULL,
    239  1.1  tron 					&op->o_req_dn, LDAP_SCOPE_DEFAULT );
    240  1.1  tron 		}
    241  1.1  tron 
    242  1.1  tron 		rs->sr_err = LDAP_REFERRAL;
    243  1.1  tron 		send_ldap_result( op, rs );
    244  1.1  tron 
    245  1.1  tron 		ber_bvarray_free( rs->sr_ref );
    246  1.1  tron 		free( (char *)rs->sr_matched );
    247  1.1  tron 		rs->sr_ref = NULL;
    248  1.1  tron 		rs->sr_matched = NULL;
    249  1.1  tron 
    250  1.1  tron 		goto done;
    251  1.1  tron 	}
    252  1.1  tron 
    253  1.1  tron 	if ( get_assert( op ) &&
    254  1.1  tron 		( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
    255  1.1  tron 	{
    256  1.1  tron 		rs->sr_err = LDAP_ASSERTION_FAILED;
    257  1.1  tron 		goto return_results;
    258  1.1  tron 	}
    259  1.1  tron 
    260  1.1  tron 	/* check write on old entry */
    261  1.1  tron 	rs->sr_err = access_allowed( op, e, entry, NULL, ACL_WRITE, NULL );
    262  1.1  tron 	if ( ! rs->sr_err ) {
    263  1.1  tron 		Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
    264  1.1  tron 			0, 0 );
    265  1.1  tron 		rs->sr_text = "no write access to old entry";
    266  1.1  tron 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    267  1.1  tron 		goto return_results;
    268  1.1  tron 	}
    269  1.1  tron 
    270  1.1  tron 	if (!manageDSAit && is_entry_referral( e ) ) {
    271  1.1  tron 		/* entry is a referral, don't allow rename */
    272  1.1  tron 		rs->sr_ref = get_entry_referrals( op, e );
    273  1.1  tron 
    274  1.1  tron 		Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(mdb_modrdn)
    275  1.1  tron 			": entry %s is referral\n", e->e_dn, 0, 0 );
    276  1.1  tron 
    277  1.1  tron 		rs->sr_err = LDAP_REFERRAL,
    278  1.1  tron 		rs->sr_matched = e->e_name.bv_val;
    279  1.1  tron 		send_ldap_result( op, rs );
    280  1.1  tron 
    281  1.1  tron 		ber_bvarray_free( rs->sr_ref );
    282  1.1  tron 		rs->sr_ref = NULL;
    283  1.1  tron 		rs->sr_matched = NULL;
    284  1.1  tron 		goto done;
    285  1.1  tron 	}
    286  1.1  tron 
    287  1.1  tron 	new_parent_dn = &p_dn;	/* New Parent unless newSuperior given */
    288  1.1  tron 
    289  1.1  tron 	if ( op->oq_modrdn.rs_newSup != NULL ) {
    290  1.1  tron 		Debug( LDAP_DEBUG_TRACE,
    291  1.1  tron 			LDAP_XSTRING(mdb_modrdn)
    292  1.1  tron 			": new parent \"%s\" requested...\n",
    293  1.1  tron 			op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
    294  1.1  tron 
    295  1.1  tron 		/*  newSuperior == oldParent? */
    296  1.1  tron 		if( dn_match( &p_ndn, op->oq_modrdn.rs_nnewSup ) ) {
    297  1.1  tron 			Debug( LDAP_DEBUG_TRACE, "mdb_back_modrdn: "
    298  1.1  tron 				"new parent \"%s\" same as the old parent \"%s\"\n",
    299  1.1  tron 				op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
    300  1.1  tron 			op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
    301  1.1  tron 		}
    302  1.1  tron 	}
    303  1.1  tron 
    304  1.1  tron 	/* There's a MDB_MULTIPLE_SUFFIXES case here that this code doesn't
    305  1.1  tron 	 * support. E.g., two suffixes dc=foo,dc=com and dc=bar,dc=net.
    306  1.1  tron 	 * We do not allow modDN
    307  1.1  tron 	 *   dc=foo,dc=com
    308  1.1  tron 	 *    newrdn dc=bar
    309  1.1  tron 	 *    newsup dc=net
    310  1.1  tron 	 * and we probably should. But since MULTIPLE_SUFFIXES is deprecated
    311  1.1  tron 	 * I'm ignoring this problem for now.
    312  1.1  tron 	 */
    313  1.1  tron 	if ( op->oq_modrdn.rs_newSup != NULL ) {
    314  1.1  tron 		if ( op->oq_modrdn.rs_newSup->bv_len ) {
    315  1.1  tron 			np_dn = op->oq_modrdn.rs_newSup;
    316  1.1  tron 			np_ndn = op->oq_modrdn.rs_nnewSup;
    317  1.1  tron 
    318  1.1  tron 			/* newSuperior == oldParent? - checked above */
    319  1.1  tron 			/* newSuperior == entry being moved?, if so ==> ERROR */
    320  1.1  tron 			if ( dnIsSuffix( np_ndn, &e->e_nname )) {
    321  1.1  tron 				rs->sr_err = LDAP_NO_SUCH_OBJECT;
    322  1.1  tron 				rs->sr_text = "new superior not found";
    323  1.1  tron 				goto return_results;
    324  1.1  tron 			}
    325  1.1  tron 			/* Get Entry with dn=newSuperior. Does newSuperior exist? */
    326  1.1  tron 			rs->sr_err = mdb_dn2entry( op, txn, NULL, np_ndn, &np, NULL, 0 );
    327  1.1  tron 
    328  1.1  tron 			switch( rs->sr_err ) {
    329  1.1  tron 			case 0:
    330  1.1  tron 				break;
    331  1.1  tron 			case MDB_NOTFOUND:
    332  1.1  tron 				Debug( LDAP_DEBUG_TRACE,
    333  1.1  tron 					LDAP_XSTRING(mdb_modrdn)
    334  1.1  tron 					": newSup(ndn=%s) not here!\n",
    335  1.1  tron 					np_ndn->bv_val, 0, 0);
    336  1.1  tron 				rs->sr_text = "new superior not found";
    337  1.1  tron 				rs->sr_err = LDAP_NO_SUCH_OBJECT;
    338  1.1  tron 				goto return_results;
    339  1.1  tron 			case LDAP_BUSY:
    340  1.1  tron 				rs->sr_text = "ldap server busy";
    341  1.1  tron 				goto return_results;
    342  1.1  tron 			default:
    343  1.1  tron 				rs->sr_err = LDAP_OTHER;
    344  1.1  tron 				rs->sr_text = "internal error";
    345  1.1  tron 				goto return_results;
    346  1.1  tron 			}
    347  1.1  tron 
    348  1.1  tron 			/* check newSuperior for "children" acl */
    349  1.1  tron 			rs->sr_err = access_allowed( op, np, children,
    350  1.1  tron 				NULL, ACL_WADD, NULL );
    351  1.1  tron 
    352  1.1  tron 			if( ! rs->sr_err ) {
    353  1.1  tron 				Debug( LDAP_DEBUG_TRACE,
    354  1.1  tron 					LDAP_XSTRING(mdb_modrdn)
    355  1.1  tron 					": no wr to newSup children\n",
    356  1.1  tron 					0, 0, 0 );
    357  1.1  tron 				rs->sr_text = "no write access to new superior's children";
    358  1.1  tron 				rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    359  1.1  tron 				goto return_results;
    360  1.1  tron 			}
    361  1.1  tron 
    362  1.1  tron 			Debug( LDAP_DEBUG_TRACE,
    363  1.1  tron 				LDAP_XSTRING(mdb_modrdn)
    364  1.1  tron 				": wr to new parent OK np=%p, id=%ld\n",
    365  1.1  tron 				(void *) np, (long) np->e_id, 0 );
    366  1.1  tron 
    367  1.1  tron 			if ( is_entry_alias( np ) ) {
    368  1.1  tron 				/* parent is an alias, don't allow add */
    369  1.1  tron 				Debug( LDAP_DEBUG_TRACE,
    370  1.1  tron 					LDAP_XSTRING(mdb_modrdn)
    371  1.1  tron 					": entry is alias\n",
    372  1.1  tron 					0, 0, 0 );
    373  1.1  tron 				rs->sr_text = "new superior is an alias";
    374  1.1  tron 				rs->sr_err = LDAP_ALIAS_PROBLEM;
    375  1.1  tron 				goto return_results;
    376  1.1  tron 			}
    377  1.1  tron 
    378  1.1  tron 			if ( is_entry_referral( np ) ) {
    379  1.1  tron 				/* parent is a referral, don't allow add */
    380  1.1  tron 				Debug( LDAP_DEBUG_TRACE,
    381  1.1  tron 					LDAP_XSTRING(mdb_modrdn)
    382  1.1  tron 					": entry is referral\n",
    383  1.1  tron 					0, 0, 0 );
    384  1.1  tron 				rs->sr_text = "new superior is a referral";
    385  1.1  tron 				rs->sr_err = LDAP_OTHER;
    386  1.1  tron 				goto return_results;
    387  1.1  tron 			}
    388  1.1  tron 			new_parent_dn = &np->e_name;
    389  1.1  tron 
    390  1.1  tron 		} else {
    391  1.1  tron 			np_dn = NULL;
    392  1.1  tron 
    393  1.1  tron 			/* no parent, modrdn entry directly under root */
    394  1.1  tron 			if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
    395  1.1  tron 				|| be_isupdate( op ) ) {
    396  1.1  tron 				np = (Entry *)&slap_entry_root;
    397  1.1  tron 
    398  1.1  tron 				/* check parent for "children" acl */
    399  1.1  tron 				rs->sr_err = access_allowed( op, np,
    400  1.1  tron 					children, NULL, ACL_WADD, NULL );
    401  1.1  tron 
    402  1.1  tron 				np = NULL;
    403  1.1  tron 
    404  1.1  tron 				if ( ! rs->sr_err ) {
    405  1.1  tron 					rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    406  1.1  tron 					Debug( LDAP_DEBUG_TRACE,
    407  1.1  tron 						"no access to new superior\n",
    408  1.1  tron 						0, 0, 0 );
    409  1.1  tron 					rs->sr_text =
    410  1.1  tron 						"no write access to new superior's children";
    411  1.1  tron 					goto return_results;
    412  1.1  tron 				}
    413  1.1  tron 			}
    414  1.1  tron 		}
    415  1.1  tron 
    416  1.1  tron 		Debug( LDAP_DEBUG_TRACE,
    417  1.1  tron 			LDAP_XSTRING(mdb_modrdn)
    418  1.1  tron 			": wr to new parent's children OK\n",
    419  1.1  tron 			0, 0, 0 );
    420  1.1  tron 
    421  1.1  tron 		new_parent_dn = np_dn;
    422  1.1  tron 	}
    423  1.1  tron 
    424  1.1  tron 	/* Build target dn and make sure target entry doesn't exist already. */
    425  1.1  tron 	if (!new_dn.bv_val) {
    426  1.1  tron 		build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, op->o_tmpmemctx );
    427  1.1  tron 	}
    428  1.1  tron 
    429  1.1  tron 	if (!new_ndn.bv_val) {
    430  1.1  tron 		dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, op->o_tmpmemctx );
    431  1.1  tron 	}
    432  1.1  tron 
    433  1.1  tron 	Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(mdb_modrdn) ": new ndn=%s\n",
    434  1.1  tron 		new_ndn.bv_val, 0, 0 );
    435  1.1  tron 
    436  1.1  tron 	/* Shortcut the search */
    437  1.1  tron 	rs->sr_err = mdb_dn2id ( op, txn, NULL, &new_ndn, &nid, NULL, NULL, NULL );
    438  1.1  tron 	switch( rs->sr_err ) {
    439  1.1  tron 	case MDB_NOTFOUND:
    440  1.1  tron 		break;
    441  1.1  tron 	case 0:
    442  1.1  tron 		/* Allow rename to same DN */
    443  1.1  tron 		if ( nid == e->e_id )
    444  1.1  tron 			break;
    445  1.1  tron 		rs->sr_err = LDAP_ALREADY_EXISTS;
    446  1.1  tron 		goto return_results;
    447  1.1  tron 	default:
    448  1.1  tron 		rs->sr_err = LDAP_OTHER;
    449  1.1  tron 		rs->sr_text = "internal error";
    450  1.1  tron 		goto return_results;
    451  1.1  tron 	}
    452  1.1  tron 
    453  1.1  tron 	assert( op->orr_modlist != NULL );
    454  1.1  tron 
    455  1.1  tron 	if( op->o_preread ) {
    456  1.1  tron 		if( preread_ctrl == NULL ) {
    457  1.1  tron 			preread_ctrl = &ctrls[num_ctrls++];
    458  1.1  tron 			ctrls[num_ctrls] = NULL;
    459  1.1  tron 		}
    460  1.1  tron 		if( slap_read_controls( op, rs, e,
    461  1.1  tron 			&slap_pre_read_bv, preread_ctrl ) )
    462  1.1  tron 		{
    463  1.1  tron 			Debug( LDAP_DEBUG_TRACE,
    464  1.1  tron 				"<=- " LDAP_XSTRING(mdb_modrdn)
    465  1.1  tron 				": pre-read failed!\n", 0, 0, 0 );
    466  1.1  tron 			if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
    467  1.1  tron 				/* FIXME: is it correct to abort
    468  1.1  tron 				 * operation if control fails? */
    469  1.1  tron 				goto return_results;
    470  1.1  tron 			}
    471  1.1  tron 		}
    472  1.1  tron 	}
    473  1.1  tron 
    474  1.1  tron 	/* delete old DN
    475  1.1  tron 	 * If moving to a new parent, must delete current subtree count,
    476  1.1  tron 	 * otherwise leave it unchanged since we'll be adding it right back.
    477  1.1  tron 	 */
    478  1.1  tron 	rs->sr_err = mdb_dn2id_delete( op, mc, e->e_id, np ? nsubs : 0 );
    479  1.1  tron 	if ( rs->sr_err != 0 ) {
    480  1.1  tron 		Debug(LDAP_DEBUG_TRACE,
    481  1.1  tron 			"<=- " LDAP_XSTRING(mdb_modrdn)
    482  1.1  tron 			": dn2id del failed: %s (%d)\n",
    483  1.1  tron 			mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    484  1.1  tron 		rs->sr_err = LDAP_OTHER;
    485  1.1  tron 		rs->sr_text = "DN index delete fail";
    486  1.1  tron 		goto return_results;
    487  1.1  tron 	}
    488  1.1  tron 
    489  1.1  tron 	/* copy the entry, then override some fields */
    490  1.1  tron 	dummy = *e;
    491  1.1  tron 	dummy.e_name = new_dn;
    492  1.1  tron 	dummy.e_nname = new_ndn;
    493  1.1  tron 	dummy.e_attrs = NULL;
    494  1.1  tron 
    495  1.1  tron 	/* add new DN */
    496  1.1  tron 	rs->sr_err = mdb_dn2id_add( op, mc, mc, np ? np->e_id : p->e_id,
    497  1.1  tron 		nsubs, np != NULL, &dummy );
    498  1.1  tron 	if ( rs->sr_err != 0 ) {
    499  1.1  tron 		Debug(LDAP_DEBUG_TRACE,
    500  1.1  tron 			"<=- " LDAP_XSTRING(mdb_modrdn)
    501  1.1  tron 			": dn2id add failed: %s (%d)\n",
    502  1.1  tron 			mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    503  1.1  tron 		rs->sr_err = LDAP_OTHER;
    504  1.1  tron 		rs->sr_text = "DN index add failed";
    505  1.1  tron 		goto return_results;
    506  1.1  tron 	}
    507  1.1  tron 
    508  1.1  tron 	dummy.e_attrs = e->e_attrs;
    509  1.1  tron 
    510  1.1  tron 	/* modify entry */
    511  1.1  tron 	rs->sr_err = mdb_modify_internal( op, txn, op->orr_modlist, &dummy,
    512  1.1  tron 		&rs->sr_text, textbuf, textlen );
    513  1.1  tron 	if( rs->sr_err != LDAP_SUCCESS ) {
    514  1.1  tron 		Debug(LDAP_DEBUG_TRACE,
    515  1.1  tron 			"<=- " LDAP_XSTRING(mdb_modrdn)
    516  1.1  tron 			": modify failed: %s (%d)\n",
    517  1.1  tron 			mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    518  1.1  tron 		if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
    519  1.1  tron 		goto return_results;
    520  1.1  tron 	}
    521  1.1  tron 
    522  1.1  tron 	/* id2entry index */
    523  1.1  tron 	rs->sr_err = mdb_id2entry_update( op, txn, NULL, &dummy );
    524  1.1  tron 	if ( rs->sr_err != 0 ) {
    525  1.1  tron 		Debug(LDAP_DEBUG_TRACE,
    526  1.1  tron 			"<=- " LDAP_XSTRING(mdb_modrdn)
    527  1.1  tron 			": id2entry failed: %s (%d)\n",
    528  1.1  tron 			mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    529  1.1  tron 		rs->sr_err = LDAP_OTHER;
    530  1.1  tron 		rs->sr_text = "entry update failed";
    531  1.1  tron 		goto return_results;
    532  1.1  tron 	}
    533  1.1  tron 
    534  1.1  tron 	if ( p_ndn.bv_len != 0 ) {
    535  1.1  tron 		if ((parent_is_glue = is_entry_glue(p))) {
    536  1.1  tron 			rs->sr_err = mdb_dn2id_children( op, txn, p );
    537  1.1  tron 			if ( rs->sr_err != MDB_NOTFOUND ) {
    538  1.1  tron 				switch( rs->sr_err ) {
    539  1.1  tron 				case 0:
    540  1.1  tron 					break;
    541  1.1  tron 				default:
    542  1.1  tron 					Debug(LDAP_DEBUG_ARGS,
    543  1.1  tron 						"<=- " LDAP_XSTRING(mdb_modrdn)
    544  1.1  tron 						": has_children failed: %s (%d)\n",
    545  1.1  tron 						mdb_strerror(rs->sr_err), rs->sr_err, 0 );
    546  1.1  tron 					rs->sr_err = LDAP_OTHER;
    547  1.1  tron 					rs->sr_text = "internal error";
    548  1.1  tron 					goto return_results;
    549  1.1  tron 				}
    550  1.1  tron 			} else {
    551  1.1  tron 				parent_is_leaf = 1;
    552  1.1  tron 			}
    553  1.1  tron 		}
    554  1.1  tron 		mdb_entry_return( op, p );
    555  1.1  tron 		p = NULL;
    556  1.1  tron 	}
    557  1.1  tron 
    558  1.1  tron 	if( op->o_postread ) {
    559  1.1  tron 		if( postread_ctrl == NULL ) {
    560  1.1  tron 			postread_ctrl = &ctrls[num_ctrls++];
    561  1.1  tron 			ctrls[num_ctrls] = NULL;
    562  1.1  tron 		}
    563  1.1  tron 		if( slap_read_controls( op, rs, &dummy,
    564  1.1  tron 			&slap_post_read_bv, postread_ctrl ) )
    565  1.1  tron 		{
    566  1.1  tron 			Debug( LDAP_DEBUG_TRACE,
    567  1.1  tron 				"<=- " LDAP_XSTRING(mdb_modrdn)
    568  1.1  tron 				": post-read failed!\n", 0, 0, 0 );
    569  1.1  tron 			if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
    570  1.1  tron 				/* FIXME: is it correct to abort
    571  1.1  tron 				 * operation if control fails? */
    572  1.1  tron 				goto return_results;
    573  1.1  tron 			}
    574  1.1  tron 		}
    575  1.1  tron 	}
    576  1.1  tron 
    577  1.1  tron 	if( moi == &opinfo ) {
    578  1.1  tron 		LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
    579  1.1  tron 		opinfo.moi_oe.oe_key = NULL;
    580  1.1  tron 		if( op->o_noop ) {
    581  1.1  tron 			mdb_txn_abort( txn );
    582  1.1  tron 			rs->sr_err = LDAP_X_NO_OPERATION;
    583  1.1  tron 			txn = NULL;
    584  1.1  tron 			/* Only free attrs if they were dup'd.  */
    585  1.1  tron 			if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
    586  1.1  tron 			goto return_results;
    587  1.1  tron 
    588  1.1  tron 		} else {
    589  1.1  tron 			if(( rs->sr_err=mdb_txn_commit( txn )) != 0 ) {
    590  1.1  tron 				rs->sr_text = "txn_commit failed";
    591  1.1  tron 			} else {
    592  1.1  tron 				rs->sr_err = LDAP_SUCCESS;
    593  1.1  tron 			}
    594  1.1  tron 			txn = NULL;
    595  1.1  tron 		}
    596  1.1  tron 	}
    597  1.1  tron 
    598  1.1  tron 	if( rs->sr_err != LDAP_SUCCESS ) {
    599  1.1  tron 		Debug( LDAP_DEBUG_ANY,
    600  1.1  tron 			LDAP_XSTRING(mdb_modrdn) ": %s : %s (%d)\n",
    601  1.1  tron 			rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
    602  1.1  tron 		rs->sr_err = LDAP_OTHER;
    603  1.1  tron 
    604  1.1  tron 		goto return_results;
    605  1.1  tron 	}
    606  1.1  tron 
    607  1.1  tron 	Debug(LDAP_DEBUG_TRACE,
    608  1.1  tron 		LDAP_XSTRING(mdb_modrdn)
    609  1.1  tron 		": rdn modified%s id=%08lx dn=\"%s\"\n",
    610  1.1  tron 		op->o_noop ? " (no-op)" : "",
    611  1.1  tron 		dummy.e_id, op->o_req_dn.bv_val );
    612  1.1  tron 	rs->sr_text = NULL;
    613  1.1  tron 	if( num_ctrls ) rs->sr_ctrls = ctrls;
    614  1.1  tron 
    615  1.1  tron return_results:
    616  1.1  tron 	if ( dummy.e_attrs ) {
    617  1.1  tron 		attrs_free( dummy.e_attrs );
    618  1.1  tron 	}
    619  1.1  tron 	send_ldap_result( op, rs );
    620  1.1  tron 
    621  1.1  tron #if 0
    622  1.1  tron 	if( rs->sr_err == LDAP_SUCCESS && mdb->bi_txn_cp_kbyte ) {
    623  1.1  tron 		TXN_CHECKPOINT( mdb->bi_dbenv,
    624  1.1  tron 			mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
    625  1.1  tron 	}
    626  1.1  tron #endif
    627  1.1  tron 
    628  1.1  tron 	if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
    629  1.1  tron 		op->o_delete_glue_parent = 1;
    630  1.1  tron 	}
    631  1.1  tron 
    632  1.1  tron done:
    633  1.1  tron 	slap_graduate_commit_csn( op );
    634  1.1  tron 
    635  1.1  tron 	if( new_ndn.bv_val != NULL ) op->o_tmpfree( new_ndn.bv_val, op->o_tmpmemctx );
    636  1.1  tron 	if( new_dn.bv_val != NULL ) op->o_tmpfree( new_dn.bv_val, op->o_tmpmemctx );
    637  1.1  tron 
    638  1.1  tron 	/* LDAP v3 Support */
    639  1.1  tron 	if( np != NULL ) {
    640  1.1  tron 		/* free new parent */
    641  1.1  tron 		mdb_entry_return( op, np );
    642  1.1  tron 	}
    643  1.1  tron 
    644  1.1  tron 	if( p != NULL ) {
    645  1.1  tron 		/* free parent */
    646  1.1  tron 		mdb_entry_return( op, p );
    647  1.1  tron 	}
    648  1.1  tron 
    649  1.1  tron 	/* free entry */
    650  1.1  tron 	if( e != NULL ) {
    651  1.1  tron 		mdb_entry_return( op, e );
    652  1.1  tron 	}
    653  1.1  tron 
    654  1.1  tron 	if( moi == &opinfo ) {
    655  1.1  tron 		if( txn != NULL ) {
    656  1.1  tron 			mdb_txn_abort( txn );
    657  1.1  tron 		}
    658  1.1  tron 		if ( opinfo.moi_oe.oe_key ) {
    659  1.1  tron 			LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
    660  1.1  tron 		}
    661  1.1  tron 	} else {
    662  1.1  tron 		moi->moi_ref--;
    663  1.1  tron 	}
    664  1.1  tron 
    665  1.1  tron 	if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
    666  1.1  tron 		slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
    667  1.1  tron 		slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
    668  1.1  tron 	}
    669  1.1  tron 	if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
    670  1.1  tron 		slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
    671  1.1  tron 		slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
    672  1.1  tron 	}
    673  1.1  tron 	return rs->sr_err;
    674  1.1  tron }
    675