2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
16 * This software is not subject to any license of Silicon Graphics
17 * Inc. or Purdue University.
19 * Redistribution and use in source and binary forms are permitted
20 * without restriction or fee of any kind as long as this notice
23 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
24 * All rights reserved.
26 * Redistribution and use in source and binary forms are permitted
27 * provided that this notice is preserved and that due credit is given
28 * to the University of Michigan at Ann Arbor. The name of the University
29 * may not be used to endorse or promote products derived from this
30 * software without specific prior written permission. This software
31 * is provided ``as is'' without express or implied warranty.
38 #include <ac/socket.h>
39 #include <ac/string.h>
49 struct berval dn = BER_BVNULL;
50 struct berval newrdn = BER_BVNULL;
51 struct berval newSuperior = BER_BVNULL;
54 struct berval pnewSuperior = BER_BVNULL;
56 struct berval nnewSuperior = BER_BVNULL;
60 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
64 * Parse the modrdn request. It looks like this:
66 * ModifyRDNRequest := SEQUENCE {
67 * entry DistinguishedName,
68 * newrdn RelativeDistinguishedName
69 * deleteoldrdn BOOLEAN,
70 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
74 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
77 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
79 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
80 return SLAPD_DISCONNECT;
83 /* Check for newSuperior parameter, if present scan it */
85 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
86 if ( op->o_protocol < LDAP_VERSION3 ) {
87 /* Conection record indicates v2 but field
88 * newSuperior is present: report error.
90 Debug( LDAP_DEBUG_ANY,
91 "modrdn(v2): invalid field newSuperior!\n",
94 send_ldap_discon( op, rs,
95 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
96 rs->sr_err = SLAPD_DISCONNECT;
100 if ( ber_scanf( op->o_ber, "m", &newSuperior )
103 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
106 send_ldap_discon( op, rs,
107 LDAP_PROTOCOL_ERROR, "decoding error" );
108 rs->sr_err = SLAPD_DISCONNECT;
111 op->orr_newSup = &pnewSuperior;
112 op->orr_nnewSup = &nnewSuperior;
115 Debug( LDAP_DEBUG_ARGS,
116 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
117 dn.bv_val, newrdn.bv_val,
118 newSuperior.bv_len ? newSuperior.bv_val : "" );
120 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
121 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
123 send_ldap_discon( op, rs,
124 LDAP_PROTOCOL_ERROR, "decoding error" );
125 rs->sr_err = SLAPD_DISCONNECT;
129 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
130 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
132 /* get_ctrls has sent results. Now clean up. */
136 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
137 if( rs->sr_err != LDAP_SUCCESS ) {
138 Debug( LDAP_DEBUG_ANY,
139 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
140 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
144 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
146 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
147 if( rs->sr_err != LDAP_SUCCESS ) {
148 Debug( LDAP_DEBUG_ANY,
149 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
150 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
154 if( rdn_validate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
155 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
156 op->orr_newrdn.bv_val, 0, 0 );
158 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
162 if( op->orr_newSup ) {
163 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
164 &nnewSuperior, op->o_tmpmemctx );
165 if( rs->sr_err != LDAP_SUCCESS ) {
166 Debug( LDAP_DEBUG_ANY,
167 "do_modrdn: invalid newSuperior (%s)\n",
168 newSuperior.bv_val, 0, 0 );
169 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
174 op->orr_deleteoldrdn = deloldrdn;
175 op->orr_modlist = NULL;
177 /* prepare modlist of modifications from old/new RDN */
178 rs->sr_err = slap_modrdn2mods( op, rs );
179 if ( rs->sr_err != LDAP_SUCCESS ) {
180 send_ldap_result( op, rs );
184 op->o_bd = frontendDB;
185 rs->sr_err = frontendDB->be_modrdn( op, rs );
188 if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
194 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
195 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
197 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
198 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
200 if ( op->orr_modlist != NULL )
201 slap_mods_free( op->orr_modlist, 1 );
203 if ( !BER_BVISNULL( &pnewSuperior ) ) {
204 op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
206 if ( !BER_BVISNULL( &nnewSuperior ) ) {
207 op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
214 fe_op_modrdn( Operation *op, SlapReply *rs )
216 Backend *newSuperior_be = NULL;
217 struct berval pdn = BER_BVNULL;
218 BackendDB *op_be, *bd = op->o_bd;
220 if( op->o_req_ndn.bv_len == 0 ) {
221 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
223 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
224 "cannot rename the root DSE" );
227 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
228 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
229 frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len, 0 );
231 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
232 "cannot rename subschema subentry" );
236 Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
237 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
240 * We could be serving multiple database backends. Select the
241 * appropriate one, or send a referral to our "referral server"
242 * if we don't hold it.
244 op->o_bd = select_backend( &op->o_req_ndn, 1 );
245 if ( op->o_bd == NULL ) {
247 rs->sr_ref = referral_rewrite( default_referral,
248 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
249 if (!rs->sr_ref) rs->sr_ref = default_referral;
251 if ( rs->sr_ref != NULL ) {
252 rs->sr_err = LDAP_REFERRAL;
253 send_ldap_result( op, rs );
255 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
257 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
258 "no global superior knowledge" );
263 /* If we've got a glued backend, check the real backend */
265 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
266 op->o_bd = select_backend( &op->o_req_ndn, 0 );
269 /* check restrictions */
270 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
271 send_ldap_result( op, rs );
275 /* check for referrals */
276 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
280 /* Make sure that the entry being changed and the newSuperior are in
281 * the same backend, otherwise we return an error.
283 if( op->orr_newSup ) {
284 newSuperior_be = select_backend( op->orr_nnewSup, 0 );
286 if ( newSuperior_be != op->o_bd ) {
287 /* newSuperior is in different backend */
288 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
289 "cannot rename between DSAs" );
296 * do the modrdn if 1 && (2 || 3)
297 * 1) there is a modrdn function implemented in this backend;
298 * 2) this backend is master for what it holds;
299 * 3) it's a replica and the dn supplied is the update_ndn.
301 if ( op->o_bd->be_modrdn ) {
302 /* do the update here */
303 int repl_user = be_isupdate( op );
304 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user )
307 op->o_bd->be_modrdn( op, rs );
309 if ( op->o_bd->be_delete ) {
310 struct berval org_req_dn = BER_BVNULL;
311 struct berval org_req_ndn = BER_BVNULL;
312 struct berval org_dn = BER_BVNULL;
313 struct berval org_ndn = BER_BVNULL;
316 org_req_dn = op->o_req_dn;
317 org_req_ndn = op->o_req_ndn;
320 org_managedsait = get_manageDSAit( op );
321 op->o_dn = op->o_bd->be_rootdn;
322 op->o_ndn = op->o_bd->be_rootndn;
323 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
325 while ( rs->sr_err == LDAP_SUCCESS &&
326 op->o_delete_glue_parent ) {
327 op->o_delete_glue_parent = 0;
328 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
329 slap_callback cb = { NULL };
330 cb.sc_response = slap_null_cb;
331 dnParent( &op->o_req_ndn, &pdn );
334 op->o_callback = &cb;
335 op->o_bd->be_delete( op, rs );
340 op->o_managedsait = org_managedsait;
343 op->o_req_dn = org_req_dn;
344 op->o_req_ndn = org_req_ndn;
345 op->o_delete_glue_parent = 0;
349 BerVarray defref = op->o_bd->be_update_refs
350 ? op->o_bd->be_update_refs : default_referral;
352 if ( defref != NULL ) {
353 rs->sr_ref = referral_rewrite( defref,
354 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
355 if (!rs->sr_ref) rs->sr_ref = defref;
357 rs->sr_err = LDAP_REFERRAL;
358 send_ldap_result( op, rs );
360 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
362 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
363 "shadow context; no update referral" );
367 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
368 "operation not supported within namingContext" );
382 LDAPRDN old_rdn = NULL;
383 LDAPRDN new_rdn = NULL;
385 assert( !BER_BVISEMPTY( &op->oq_modrdn.rs_newrdn ) );
386 assert( !op->orr_deleteoldrdn || !BER_BVISEMPTY( &op->o_req_dn ) );
388 if ( ldap_bv2rdn_x( &op->oq_modrdn.rs_newrdn, &new_rdn,
389 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
390 Debug( LDAP_DEBUG_TRACE,
391 "slap_modrdn2mods: can't figure out "
392 "type(s)/value(s) of newrdn\n", 0, 0, 0 );
393 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
394 rs->sr_text = "unknown type(s) used in RDN";
398 if ( op->oq_modrdn.rs_deleteoldrdn ) {
399 if ( ldap_bv2rdn_x( &op->o_req_dn, &old_rdn,
400 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
401 Debug( LDAP_DEBUG_TRACE,
402 "slap_modrdn2mods: can't figure out "
403 "type(s)/value(s) of oldrdn\n", 0, 0, 0 );
404 rs->sr_err = LDAP_OTHER;
405 rs->sr_text = "cannot parse RDN from old DN";
411 /* Add new attribute values to the entry */
412 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
413 AttributeDescription *desc = NULL;
414 Modifications *mod_tmp;
416 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
418 if ( rs->sr_err != LDAP_SUCCESS ) {
419 Debug( LDAP_DEBUG_TRACE,
420 "slap_modrdn2mods: %s: %s (new)\n",
422 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
426 /* Apply modification */
427 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
428 mod_tmp->sml_desc = desc;
429 BER_BVZERO( &mod_tmp->sml_type );
430 mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
431 ber_dupbv( &mod_tmp->sml_values[0], &new_rdn[a_cnt]->la_value );
432 mod_tmp->sml_values[1].bv_val = NULL;
433 if( desc->ad_type->sat_equality->smr_normalize) {
434 mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
435 (void) (*desc->ad_type->sat_equality->smr_normalize)(
436 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
437 desc->ad_type->sat_syntax,
438 desc->ad_type->sat_equality,
439 &mod_tmp->sml_values[0],
440 &mod_tmp->sml_nvalues[0], NULL );
441 mod_tmp->sml_nvalues[1].bv_val = NULL;
443 mod_tmp->sml_nvalues = NULL;
445 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
446 mod_tmp->sml_flags = 0;
447 mod_tmp->sml_next = op->orr_modlist;
448 op->orr_modlist = mod_tmp;
451 /* Remove old rdn value if required */
452 if ( op->orr_deleteoldrdn ) {
453 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
454 AttributeDescription *desc = NULL;
455 Modifications *mod_tmp;
457 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
458 if ( rs->sr_err != LDAP_SUCCESS ) {
459 Debug( LDAP_DEBUG_TRACE,
460 "slap_modrdn2mods: %s: %s (old)\n",
462 old_rdn[d_cnt]->la_attr.bv_val,
467 /* Apply modification */
468 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
469 mod_tmp->sml_desc = desc;
470 BER_BVZERO( &mod_tmp->sml_type );
471 mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
472 ber_dupbv( &mod_tmp->sml_values[0], &old_rdn[d_cnt]->la_value );
473 mod_tmp->sml_values[1].bv_val = NULL;
474 if( desc->ad_type->sat_equality->smr_normalize) {
475 mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
476 (void) (*desc->ad_type->sat_equality->smr_normalize)(
477 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
478 desc->ad_type->sat_syntax,
479 desc->ad_type->sat_equality,
480 &mod_tmp->sml_values[0],
481 &mod_tmp->sml_nvalues[0], NULL );
482 mod_tmp->sml_nvalues[1].bv_val = NULL;
484 mod_tmp->sml_nvalues = NULL;
486 mod_tmp->sml_op = LDAP_MOD_DELETE;
487 mod_tmp->sml_flags = 0;
488 mod_tmp->sml_next = op->orr_modlist;
489 op->orr_modlist = mod_tmp;
495 /* LDAP v2 supporting correct attribute handling. */
496 if ( rs->sr_err != LDAP_SUCCESS && op->orr_modlist != NULL ) {
499 for ( ; op->orr_modlist != NULL; op->orr_modlist = tmp ) {
500 tmp = op->orr_modlist->sml_next;
501 ch_free( op->orr_modlist );
505 if ( new_rdn != NULL ) {
506 ldap_rdnfree_x( new_rdn, op->o_tmpmemctx );
508 if ( old_rdn != NULL ) {
509 ldap_rdnfree_x( old_rdn, op->o_tmpmemctx );