Home | History | Annotate | Line # | Download | only in slapd
modrdn.c revision 1.1
      1  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/modrdn.c,v 1.170.2.5 2008/02/11 23:26:44 kurt Exp $ */
      2  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1  lukem  *
      4  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      8  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
      9  1.1  lukem  * Public License.
     10  1.1  lukem  *
     11  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     12  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     13  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     14  1.1  lukem  */
     15  1.1  lukem /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
     16  1.1  lukem  * This software is not subject to any license of Silicon Graphics
     17  1.1  lukem  * Inc. or Purdue University.
     18  1.1  lukem  *
     19  1.1  lukem  * Redistribution and use in source and binary forms are permitted
     20  1.1  lukem  * without restriction or fee of any kind as long as this notice
     21  1.1  lukem  * is preserved.
     22  1.1  lukem  */
     23  1.1  lukem /* Portions Copyright (c) 1995 Regents of the University of Michigan.
     24  1.1  lukem  * All rights reserved.
     25  1.1  lukem  *
     26  1.1  lukem  * Redistribution and use in source and binary forms are permitted
     27  1.1  lukem  * provided that this notice is preserved and that due credit is given
     28  1.1  lukem  * to the University of Michigan at Ann Arbor. The name of the University
     29  1.1  lukem  * may not be used to endorse or promote products derived from this
     30  1.1  lukem  * software without specific prior written permission. This software
     31  1.1  lukem  * is provided ``as is'' without express or implied warranty.
     32  1.1  lukem  */
     33  1.1  lukem 
     34  1.1  lukem #include "portable.h"
     35  1.1  lukem 
     36  1.1  lukem #include <stdio.h>
     37  1.1  lukem 
     38  1.1  lukem #include <ac/socket.h>
     39  1.1  lukem #include <ac/string.h>
     40  1.1  lukem 
     41  1.1  lukem #include "slap.h"
     42  1.1  lukem 
     43  1.1  lukem int
     44  1.1  lukem do_modrdn(
     45  1.1  lukem     Operation	*op,
     46  1.1  lukem     SlapReply	*rs
     47  1.1  lukem )
     48  1.1  lukem {
     49  1.1  lukem 	struct berval	dn = BER_BVNULL;
     50  1.1  lukem 	struct berval	newrdn = BER_BVNULL;
     51  1.1  lukem 	struct berval	newSuperior = BER_BVNULL;
     52  1.1  lukem 	ber_int_t	deloldrdn;
     53  1.1  lukem 
     54  1.1  lukem 	struct berval pnewSuperior = BER_BVNULL;
     55  1.1  lukem 
     56  1.1  lukem 	struct berval nnewSuperior = BER_BVNULL;
     57  1.1  lukem 
     58  1.1  lukem 	ber_len_t	length;
     59  1.1  lukem 
     60  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "%s do_modrdn\n",
     61  1.1  lukem 			op->o_log_prefix, 0, 0 );
     62  1.1  lukem 	/*
     63  1.1  lukem 	 * Parse the modrdn request.  It looks like this:
     64  1.1  lukem 	 *
     65  1.1  lukem 	 *	ModifyRDNRequest := SEQUENCE {
     66  1.1  lukem 	 *		entry	DistinguishedName,
     67  1.1  lukem 	 *		newrdn	RelativeDistinguishedName
     68  1.1  lukem 	 *		deleteoldrdn	BOOLEAN,
     69  1.1  lukem 	 *		newSuperior	[0] LDAPDN OPTIONAL (v3 Only!)
     70  1.1  lukem 	 *	}
     71  1.1  lukem 	 */
     72  1.1  lukem 
     73  1.1  lukem 	if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
     74  1.1  lukem 	    == LBER_ERROR )
     75  1.1  lukem 	{
     76  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: ber_scanf failed\n",
     77  1.1  lukem 			op->o_log_prefix, 0, 0 );
     78  1.1  lukem 		send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
     79  1.1  lukem 		return SLAPD_DISCONNECT;
     80  1.1  lukem 	}
     81  1.1  lukem 
     82  1.1  lukem 	/* Check for newSuperior parameter, if present scan it */
     83  1.1  lukem 
     84  1.1  lukem 	if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
     85  1.1  lukem 		if ( op->o_protocol < LDAP_VERSION3 ) {
     86  1.1  lukem 			/* Connection record indicates v2 but field
     87  1.1  lukem 			 * newSuperior is present: report error.
     88  1.1  lukem 			 */
     89  1.1  lukem 			Debug( LDAP_DEBUG_ANY,
     90  1.1  lukem 				"%s do_modrdn: newSuperior requires LDAPv3\n",
     91  1.1  lukem 				op->o_log_prefix, 0, 0 );
     92  1.1  lukem 
     93  1.1  lukem 			send_ldap_discon( op, rs,
     94  1.1  lukem 				LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
     95  1.1  lukem 			rs->sr_err = SLAPD_DISCONNECT;
     96  1.1  lukem 			goto cleanup;
     97  1.1  lukem 		}
     98  1.1  lukem 
     99  1.1  lukem 		if ( ber_scanf( op->o_ber, "m", &newSuperior )
    100  1.1  lukem 		     == LBER_ERROR ) {
    101  1.1  lukem 
    102  1.1  lukem 			Debug( LDAP_DEBUG_ANY, "%s do_modrdn: ber_scanf(\"m\") failed\n",
    103  1.1  lukem 				op->o_log_prefix, 0, 0 );
    104  1.1  lukem 
    105  1.1  lukem 			send_ldap_discon( op, rs,
    106  1.1  lukem 				LDAP_PROTOCOL_ERROR, "decoding error" );
    107  1.1  lukem 			rs->sr_err = SLAPD_DISCONNECT;
    108  1.1  lukem 			goto cleanup;
    109  1.1  lukem 		}
    110  1.1  lukem 		op->orr_newSup = &pnewSuperior;
    111  1.1  lukem 		op->orr_nnewSup = &nnewSuperior;
    112  1.1  lukem 	}
    113  1.1  lukem 
    114  1.1  lukem 	Debug( LDAP_DEBUG_ARGS,
    115  1.1  lukem 	    "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
    116  1.1  lukem 		dn.bv_val, newrdn.bv_val,
    117  1.1  lukem 		newSuperior.bv_len ? newSuperior.bv_val : "" );
    118  1.1  lukem 
    119  1.1  lukem 	if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
    120  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: ber_scanf failed\n",
    121  1.1  lukem 			op->o_log_prefix, 0, 0 );
    122  1.1  lukem 		send_ldap_discon( op, rs,
    123  1.1  lukem 			LDAP_PROTOCOL_ERROR, "decoding error" );
    124  1.1  lukem 		rs->sr_err = SLAPD_DISCONNECT;
    125  1.1  lukem 		goto cleanup;
    126  1.1  lukem 	}
    127  1.1  lukem 
    128  1.1  lukem 	if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
    129  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: get_ctrls failed\n",
    130  1.1  lukem 			op->o_log_prefix, 0, 0 );
    131  1.1  lukem 		/* get_ctrls has sent results.	Now clean up. */
    132  1.1  lukem 		goto cleanup;
    133  1.1  lukem 	}
    134  1.1  lukem 
    135  1.1  lukem 	rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
    136  1.1  lukem 	if( rs->sr_err != LDAP_SUCCESS ) {
    137  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: invalid dn (%s)\n",
    138  1.1  lukem 			op->o_log_prefix, dn.bv_val, 0 );
    139  1.1  lukem 		send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
    140  1.1  lukem 		goto cleanup;
    141  1.1  lukem 	}
    142  1.1  lukem 
    143  1.1  lukem 	/* FIXME: should have/use rdnPretty / rdnNormalize routines */
    144  1.1  lukem 
    145  1.1  lukem 	rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
    146  1.1  lukem 	if( rs->sr_err != LDAP_SUCCESS ) {
    147  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: invalid newrdn (%s)\n",
    148  1.1  lukem 			op->o_log_prefix, newrdn.bv_val, 0 );
    149  1.1  lukem 		send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
    150  1.1  lukem 		goto cleanup;
    151  1.1  lukem 	}
    152  1.1  lukem 
    153  1.1  lukem 	if( rdn_validate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
    154  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: invalid rdn (%s)\n",
    155  1.1  lukem 			op->o_log_prefix, op->orr_newrdn.bv_val, 0 );
    156  1.1  lukem 		send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
    157  1.1  lukem 		goto cleanup;
    158  1.1  lukem 	}
    159  1.1  lukem 
    160  1.1  lukem 	if( op->orr_newSup ) {
    161  1.1  lukem 		rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
    162  1.1  lukem 			&nnewSuperior, op->o_tmpmemctx );
    163  1.1  lukem 		if( rs->sr_err != LDAP_SUCCESS ) {
    164  1.1  lukem 			Debug( LDAP_DEBUG_ANY,
    165  1.1  lukem 				"%s do_modrdn: invalid newSuperior (%s)\n",
    166  1.1  lukem 				op->o_log_prefix, newSuperior.bv_val, 0 );
    167  1.1  lukem 			send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
    168  1.1  lukem 			goto cleanup;
    169  1.1  lukem 		}
    170  1.1  lukem 	}
    171  1.1  lukem 
    172  1.1  lukem 	Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
    173  1.1  lukem 	    op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
    174  1.1  lukem 
    175  1.1  lukem 	op->orr_deleteoldrdn = deloldrdn;
    176  1.1  lukem 	op->orr_modlist = NULL;
    177  1.1  lukem 
    178  1.1  lukem 	/* prepare modlist of modifications from old/new RDN */
    179  1.1  lukem 	rs->sr_err = slap_modrdn2mods( op, rs );
    180  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS ) {
    181  1.1  lukem 		send_ldap_result( op, rs );
    182  1.1  lukem 		goto cleanup;
    183  1.1  lukem 	}
    184  1.1  lukem 
    185  1.1  lukem 	op->o_bd = frontendDB;
    186  1.1  lukem 	rs->sr_err = frontendDB->be_modrdn( op, rs );
    187  1.1  lukem 
    188  1.1  lukem #ifdef LDAP_X_TXN
    189  1.1  lukem 	if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
    190  1.1  lukem 		/* skip cleanup */
    191  1.1  lukem 	}
    192  1.1  lukem #endif
    193  1.1  lukem 
    194  1.1  lukem cleanup:
    195  1.1  lukem 	op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
    196  1.1  lukem 	op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
    197  1.1  lukem 
    198  1.1  lukem 	op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
    199  1.1  lukem 	op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
    200  1.1  lukem 
    201  1.1  lukem 	if ( op->orr_modlist != NULL )
    202  1.1  lukem 		slap_mods_free( op->orr_modlist, 1 );
    203  1.1  lukem 
    204  1.1  lukem 	if ( !BER_BVISNULL( &pnewSuperior ) ) {
    205  1.1  lukem 		op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
    206  1.1  lukem 	}
    207  1.1  lukem 	if ( !BER_BVISNULL( &nnewSuperior ) ) {
    208  1.1  lukem 		op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
    209  1.1  lukem 	}
    210  1.1  lukem 
    211  1.1  lukem 	return rs->sr_err;
    212  1.1  lukem }
    213  1.1  lukem 
    214  1.1  lukem int
    215  1.1  lukem fe_op_modrdn( Operation *op, SlapReply *rs )
    216  1.1  lukem {
    217  1.1  lukem 	struct berval	dest_ndn = BER_BVNULL, dest_pndn, pdn = BER_BVNULL;
    218  1.1  lukem 	BackendDB	*op_be, *bd = op->o_bd;
    219  1.1  lukem 	ber_slen_t	diff;
    220  1.1  lukem 
    221  1.1  lukem 	if( op->o_req_ndn.bv_len == 0 ) {
    222  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: root dse!\n",
    223  1.1  lukem 			op->o_log_prefix, 0, 0 );
    224  1.1  lukem 		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
    225  1.1  lukem 			"cannot rename the root DSE" );
    226  1.1  lukem 		goto cleanup;
    227  1.1  lukem 
    228  1.1  lukem 	} else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
    229  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "%s do_modrdn: subschema subentry: %s (%ld)\n",
    230  1.1  lukem 			op->o_log_prefix, frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len );
    231  1.1  lukem 
    232  1.1  lukem 		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
    233  1.1  lukem 			"cannot rename subschema subentry" );
    234  1.1  lukem 		goto cleanup;
    235  1.1  lukem 	}
    236  1.1  lukem 
    237  1.1  lukem 	if( op->orr_nnewSup ) {
    238  1.1  lukem 		dest_pndn = *op->orr_nnewSup;
    239  1.1  lukem 	} else {
    240  1.1  lukem 		dnParent( &op->o_req_ndn, &dest_pndn );
    241  1.1  lukem 	}
    242  1.1  lukem 	build_new_dn( &dest_ndn, &dest_pndn, &op->orr_nnewrdn, op->o_tmpmemctx );
    243  1.1  lukem 
    244  1.1  lukem 	diff = (ber_slen_t) dest_ndn.bv_len - (ber_slen_t) op->o_req_ndn.bv_len;
    245  1.1  lukem 	if ( diff > 0 ? dnIsSuffix( &dest_ndn, &op->o_req_ndn )
    246  1.1  lukem 		: diff < 0 && dnIsSuffix( &op->o_req_ndn, &dest_ndn ) )
    247  1.1  lukem 	{
    248  1.1  lukem 		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
    249  1.1  lukem 			diff > 0 ? "cannot place an entry below itself"
    250  1.1  lukem 			: "cannot place an entry above itself" );
    251  1.1  lukem 		goto cleanup;
    252  1.1  lukem 	}
    253  1.1  lukem 
    254  1.1  lukem 	/*
    255  1.1  lukem 	 * We could be serving multiple database backends.  Select the
    256  1.1  lukem 	 * appropriate one, or send a referral to our "referral server"
    257  1.1  lukem 	 * if we don't hold it.
    258  1.1  lukem 	 */
    259  1.1  lukem 	op->o_bd = select_backend( &op->o_req_ndn, 1 );
    260  1.1  lukem 	if ( op->o_bd == NULL ) {
    261  1.1  lukem 		op->o_bd = bd;
    262  1.1  lukem 		rs->sr_ref = referral_rewrite( default_referral,
    263  1.1  lukem 			NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
    264  1.1  lukem 		if (!rs->sr_ref) rs->sr_ref = default_referral;
    265  1.1  lukem 
    266  1.1  lukem 		if ( rs->sr_ref != NULL ) {
    267  1.1  lukem 			rs->sr_err = LDAP_REFERRAL;
    268  1.1  lukem 			send_ldap_result( op, rs );
    269  1.1  lukem 
    270  1.1  lukem 			if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
    271  1.1  lukem 		} else {
    272  1.1  lukem 			send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
    273  1.1  lukem 				"no global superior knowledge" );
    274  1.1  lukem 		}
    275  1.1  lukem 		goto cleanup;
    276  1.1  lukem 	}
    277  1.1  lukem 
    278  1.1  lukem 	/* If we've got a glued backend, check the real backend */
    279  1.1  lukem 	op_be = op->o_bd;
    280  1.1  lukem 	if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
    281  1.1  lukem 		op->o_bd = select_backend( &op->o_req_ndn, 0 );
    282  1.1  lukem 	}
    283  1.1  lukem 
    284  1.1  lukem 	/* check restrictions */
    285  1.1  lukem 	if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
    286  1.1  lukem 		send_ldap_result( op, rs );
    287  1.1  lukem 		goto cleanup;
    288  1.1  lukem 	}
    289  1.1  lukem 
    290  1.1  lukem 	/* check for referrals */
    291  1.1  lukem 	if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
    292  1.1  lukem 		goto cleanup;
    293  1.1  lukem 	}
    294  1.1  lukem 
    295  1.1  lukem 	/* check that destination DN is in the same backend as source DN */
    296  1.1  lukem 	if ( select_backend( &dest_ndn, 0 ) != op->o_bd ) {
    297  1.1  lukem 			send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
    298  1.1  lukem 				"cannot rename between DSAs" );
    299  1.1  lukem 			goto cleanup;
    300  1.1  lukem 	}
    301  1.1  lukem 
    302  1.1  lukem 	/*
    303  1.1  lukem 	 * do the modrdn if 1 && (2 || 3)
    304  1.1  lukem 	 * 1) there is a modrdn function implemented in this backend;
    305  1.1  lukem 	 * 2) this backend is master for what it holds;
    306  1.1  lukem 	 * 3) it's a replica and the dn supplied is the update_ndn.
    307  1.1  lukem 	 */
    308  1.1  lukem 	if ( op->o_bd->be_modrdn ) {
    309  1.1  lukem 		/* do the update here */
    310  1.1  lukem 		int repl_user = be_isupdate( op );
    311  1.1  lukem 		if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user )
    312  1.1  lukem 		{
    313  1.1  lukem 			op->o_bd = op_be;
    314  1.1  lukem 			op->o_bd->be_modrdn( op, rs );
    315  1.1  lukem 
    316  1.1  lukem 			if ( op->o_bd->be_delete ) {
    317  1.1  lukem 				struct berval	org_req_dn = BER_BVNULL;
    318  1.1  lukem 				struct berval	org_req_ndn = BER_BVNULL;
    319  1.1  lukem 				struct berval	org_dn = BER_BVNULL;
    320  1.1  lukem 				struct berval	org_ndn = BER_BVNULL;
    321  1.1  lukem 				int		org_managedsait;
    322  1.1  lukem 
    323  1.1  lukem 				org_req_dn = op->o_req_dn;
    324  1.1  lukem 				org_req_ndn = op->o_req_ndn;
    325  1.1  lukem 				org_dn = op->o_dn;
    326  1.1  lukem 				org_ndn = op->o_ndn;
    327  1.1  lukem 				org_managedsait = get_manageDSAit( op );
    328  1.1  lukem 				op->o_dn = op->o_bd->be_rootdn;
    329  1.1  lukem 				op->o_ndn = op->o_bd->be_rootndn;
    330  1.1  lukem 				op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
    331  1.1  lukem 
    332  1.1  lukem 				while ( rs->sr_err == LDAP_SUCCESS &&
    333  1.1  lukem 						op->o_delete_glue_parent ) {
    334  1.1  lukem 					op->o_delete_glue_parent = 0;
    335  1.1  lukem 					if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
    336  1.1  lukem 						slap_callback cb = { NULL };
    337  1.1  lukem 						cb.sc_response = slap_null_cb;
    338  1.1  lukem 						dnParent( &op->o_req_ndn, &pdn );
    339  1.1  lukem 						op->o_req_dn = pdn;
    340  1.1  lukem 						op->o_req_ndn = pdn;
    341  1.1  lukem 						op->o_callback = &cb;
    342  1.1  lukem 						op->o_bd->be_delete( op, rs );
    343  1.1  lukem 					} else {
    344  1.1  lukem 						break;
    345  1.1  lukem 					}
    346  1.1  lukem 				}
    347  1.1  lukem 				op->o_managedsait = org_managedsait;
    348  1.1  lukem 				op->o_dn = org_dn;
    349  1.1  lukem 				op->o_ndn = org_ndn;
    350  1.1  lukem 				op->o_req_dn = org_req_dn;
    351  1.1  lukem 				op->o_req_ndn = org_req_ndn;
    352  1.1  lukem 				op->o_delete_glue_parent = 0;
    353  1.1  lukem 			}
    354  1.1  lukem 
    355  1.1  lukem 		} else {
    356  1.1  lukem 			BerVarray defref = op->o_bd->be_update_refs
    357  1.1  lukem 				? op->o_bd->be_update_refs : default_referral;
    358  1.1  lukem 
    359  1.1  lukem 			if ( defref != NULL ) {
    360  1.1  lukem 				rs->sr_ref = referral_rewrite( defref,
    361  1.1  lukem 					NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
    362  1.1  lukem 				if (!rs->sr_ref) rs->sr_ref = defref;
    363  1.1  lukem 
    364  1.1  lukem 				rs->sr_err = LDAP_REFERRAL;
    365  1.1  lukem 				send_ldap_result( op, rs );
    366  1.1  lukem 
    367  1.1  lukem 				if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
    368  1.1  lukem 			} else {
    369  1.1  lukem 				send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
    370  1.1  lukem 					"shadow context; no update referral" );
    371  1.1  lukem 			}
    372  1.1  lukem 		}
    373  1.1  lukem 	} else {
    374  1.1  lukem 		send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
    375  1.1  lukem 			"operation not supported within namingContext" );
    376  1.1  lukem 	}
    377  1.1  lukem 
    378  1.1  lukem cleanup:;
    379  1.1  lukem 	if ( dest_ndn.bv_val != NULL )
    380  1.1  lukem 		ber_memfree_x( dest_ndn.bv_val, op->o_tmpmemctx );
    381  1.1  lukem 	op->o_bd = bd;
    382  1.1  lukem 	return rs->sr_err;
    383  1.1  lukem }
    384  1.1  lukem 
    385  1.1  lukem int
    386  1.1  lukem slap_modrdn2mods(
    387  1.1  lukem 	Operation	*op,
    388  1.1  lukem 	SlapReply	*rs )
    389  1.1  lukem {
    390  1.1  lukem 	int		a_cnt, d_cnt;
    391  1.1  lukem 	LDAPRDN		old_rdn = NULL;
    392  1.1  lukem 	LDAPRDN		new_rdn = NULL;
    393  1.1  lukem 
    394  1.1  lukem 	assert( !BER_BVISEMPTY( &op->oq_modrdn.rs_newrdn ) );
    395  1.1  lukem 	assert( !op->orr_deleteoldrdn || !BER_BVISEMPTY( &op->o_req_dn ) );
    396  1.1  lukem 
    397  1.1  lukem 	if ( ldap_bv2rdn_x( &op->oq_modrdn.rs_newrdn, &new_rdn,
    398  1.1  lukem 		(char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
    399  1.1  lukem 		Debug( LDAP_DEBUG_TRACE,
    400  1.1  lukem 			"%s slap_modrdn2mods: can't figure out "
    401  1.1  lukem 			"type(s)/value(s) of newrdn\n",
    402  1.1  lukem 			op->o_log_prefix, 0, 0 );
    403  1.1  lukem 		rs->sr_err = LDAP_INVALID_DN_SYNTAX;
    404  1.1  lukem 		rs->sr_text = "unknown type(s) used in RDN";
    405  1.1  lukem 		goto done;
    406  1.1  lukem 	}
    407  1.1  lukem 
    408  1.1  lukem 	if ( op->oq_modrdn.rs_deleteoldrdn ) {
    409  1.1  lukem 		if ( ldap_bv2rdn_x( &op->o_req_dn, &old_rdn,
    410  1.1  lukem 			(char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
    411  1.1  lukem 			Debug( LDAP_DEBUG_TRACE,
    412  1.1  lukem 				"%s slap_modrdn2mods: can't figure out "
    413  1.1  lukem 				"type(s)/value(s) of oldrdn\n",
    414  1.1  lukem 				op->o_log_prefix, 0, 0 );
    415  1.1  lukem 			rs->sr_err = LDAP_OTHER;
    416  1.1  lukem 			rs->sr_text = "cannot parse RDN from old DN";
    417  1.1  lukem 			goto done;
    418  1.1  lukem 		}
    419  1.1  lukem 	}
    420  1.1  lukem 	rs->sr_text = NULL;
    421  1.1  lukem 
    422  1.1  lukem 	/* Add new attribute values to the entry */
    423  1.1  lukem 	for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
    424  1.1  lukem 		AttributeDescription	*desc = NULL;
    425  1.1  lukem 		Modifications 		*mod_tmp;
    426  1.1  lukem 
    427  1.1  lukem 		rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
    428  1.1  lukem 
    429  1.1  lukem 		if ( rs->sr_err != LDAP_SUCCESS ) {
    430  1.1  lukem 			Debug( LDAP_DEBUG_TRACE,
    431  1.1  lukem 				"%s slap_modrdn2mods: %s: %s (new)\n",
    432  1.1  lukem 				op->o_log_prefix,
    433  1.1  lukem 				rs->sr_text,
    434  1.1  lukem 				new_rdn[ a_cnt ]->la_attr.bv_val );
    435  1.1  lukem 			goto done;
    436  1.1  lukem 		}
    437  1.1  lukem 
    438  1.1  lukem 		/* Apply modification */
    439  1.1  lukem 		mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
    440  1.1  lukem 		mod_tmp->sml_desc = desc;
    441  1.1  lukem 		BER_BVZERO( &mod_tmp->sml_type );
    442  1.1  lukem 		mod_tmp->sml_numvals = 1;
    443  1.1  lukem 		mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
    444  1.1  lukem 		ber_dupbv( &mod_tmp->sml_values[0], &new_rdn[a_cnt]->la_value );
    445  1.1  lukem 		mod_tmp->sml_values[1].bv_val = NULL;
    446  1.1  lukem 		if( desc->ad_type->sat_equality->smr_normalize) {
    447  1.1  lukem 			mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
    448  1.1  lukem 			(void) (*desc->ad_type->sat_equality->smr_normalize)(
    449  1.1  lukem 				SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
    450  1.1  lukem 				desc->ad_type->sat_syntax,
    451  1.1  lukem 				desc->ad_type->sat_equality,
    452  1.1  lukem 				&mod_tmp->sml_values[0],
    453  1.1  lukem 				&mod_tmp->sml_nvalues[0], NULL );
    454  1.1  lukem 			mod_tmp->sml_nvalues[1].bv_val = NULL;
    455  1.1  lukem 		} else {
    456  1.1  lukem 			mod_tmp->sml_nvalues = NULL;
    457  1.1  lukem 		}
    458  1.1  lukem 		mod_tmp->sml_op = SLAP_MOD_SOFTADD;
    459  1.1  lukem 		mod_tmp->sml_flags = 0;
    460  1.1  lukem 		mod_tmp->sml_next = op->orr_modlist;
    461  1.1  lukem 		op->orr_modlist = mod_tmp;
    462  1.1  lukem 	}
    463  1.1  lukem 
    464  1.1  lukem 	/* Remove old rdn value if required */
    465  1.1  lukem 	if ( op->orr_deleteoldrdn ) {
    466  1.1  lukem 		for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
    467  1.1  lukem 			AttributeDescription	*desc = NULL;
    468  1.1  lukem 			Modifications 		*mod_tmp;
    469  1.1  lukem 
    470  1.1  lukem 			rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
    471  1.1  lukem 			if ( rs->sr_err != LDAP_SUCCESS ) {
    472  1.1  lukem 				Debug( LDAP_DEBUG_TRACE,
    473  1.1  lukem 					"%s slap_modrdn2mods: %s: %s (old)\n",
    474  1.1  lukem 					op->o_log_prefix,
    475  1.1  lukem 					rs->sr_text,
    476  1.1  lukem 					old_rdn[d_cnt]->la_attr.bv_val );
    477  1.1  lukem 				goto done;
    478  1.1  lukem 			}
    479  1.1  lukem 
    480  1.1  lukem 			/* Apply modification */
    481  1.1  lukem 			mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
    482  1.1  lukem 			mod_tmp->sml_desc = desc;
    483  1.1  lukem 			BER_BVZERO( &mod_tmp->sml_type );
    484  1.1  lukem 			mod_tmp->sml_numvals = 1;
    485  1.1  lukem 			mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
    486  1.1  lukem 			ber_dupbv( &mod_tmp->sml_values[0], &old_rdn[d_cnt]->la_value );
    487  1.1  lukem 			mod_tmp->sml_values[1].bv_val = NULL;
    488  1.1  lukem 			if( desc->ad_type->sat_equality->smr_normalize) {
    489  1.1  lukem 				mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
    490  1.1  lukem 				(void) (*desc->ad_type->sat_equality->smr_normalize)(
    491  1.1  lukem 					SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
    492  1.1  lukem 					desc->ad_type->sat_syntax,
    493  1.1  lukem 					desc->ad_type->sat_equality,
    494  1.1  lukem 					&mod_tmp->sml_values[0],
    495  1.1  lukem 					&mod_tmp->sml_nvalues[0], NULL );
    496  1.1  lukem 				mod_tmp->sml_nvalues[1].bv_val = NULL;
    497  1.1  lukem 			} else {
    498  1.1  lukem 				mod_tmp->sml_nvalues = NULL;
    499  1.1  lukem 			}
    500  1.1  lukem 			mod_tmp->sml_op = LDAP_MOD_DELETE;
    501  1.1  lukem 			mod_tmp->sml_flags = 0;
    502  1.1  lukem 			mod_tmp->sml_next = op->orr_modlist;
    503  1.1  lukem 			op->orr_modlist = mod_tmp;
    504  1.1  lukem 		}
    505  1.1  lukem 	}
    506  1.1  lukem 
    507  1.1  lukem done:
    508  1.1  lukem 
    509  1.1  lukem 	/* LDAP v2 supporting correct attribute handling. */
    510  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS && op->orr_modlist != NULL ) {
    511  1.1  lukem 		Modifications *tmp;
    512  1.1  lukem 
    513  1.1  lukem 		for ( ; op->orr_modlist != NULL; op->orr_modlist = tmp ) {
    514  1.1  lukem 			tmp = op->orr_modlist->sml_next;
    515  1.1  lukem 			ch_free( op->orr_modlist );
    516  1.1  lukem 		}
    517  1.1  lukem 	}
    518  1.1  lukem 
    519  1.1  lukem 	if ( new_rdn != NULL ) {
    520  1.1  lukem 		ldap_rdnfree_x( new_rdn, op->o_tmpmemctx );
    521  1.1  lukem 	}
    522  1.1  lukem 	if ( old_rdn != NULL ) {
    523  1.1  lukem 		ldap_rdnfree_x( old_rdn, op->o_tmpmemctx );
    524  1.1  lukem 	}
    525  1.1  lukem 
    526  1.1  lukem 	return rs->sr_err;
    527  1.1  lukem }
    528  1.1  lukem 
    529