2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>
43 #include "slapi/slapi.h"
52 struct berval dn = BER_BVNULL;
53 struct berval newrdn = BER_BVNULL;
54 struct berval newSuperior = BER_BVNULL;
57 struct berval pnewSuperior = BER_BVNULL;
59 struct berval nnewSuperior = BER_BVNULL;
63 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
67 * Parse the modrdn request. It looks like this:
69 * ModifyRDNRequest := SEQUENCE {
70 * entry DistinguishedName,
71 * newrdn RelativeDistinguishedName
72 * deleteoldrdn BOOLEAN,
73 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
77 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
80 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
82 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
83 return SLAPD_DISCONNECT;
86 /* Check for newSuperior parameter, if present scan it */
88 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
89 if ( op->o_protocol < LDAP_VERSION3 ) {
90 /* Conection record indicates v2 but field
91 * newSuperior is present: report error.
93 Debug( LDAP_DEBUG_ANY,
94 "modrdn(v2): invalid field newSuperior!\n",
97 send_ldap_discon( op, rs,
98 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
99 rs->sr_err = SLAPD_DISCONNECT;
103 if ( ber_scanf( op->o_ber, "m", &newSuperior )
106 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
109 send_ldap_discon( op, rs,
110 LDAP_PROTOCOL_ERROR, "decoding error" );
111 rs->sr_err = SLAPD_DISCONNECT;
114 op->orr_newSup = &pnewSuperior;
115 op->orr_nnewSup = &nnewSuperior;
118 Debug( LDAP_DEBUG_ARGS,
119 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
120 dn.bv_val, newrdn.bv_val,
121 newSuperior.bv_len ? newSuperior.bv_val : "" );
123 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
124 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
126 send_ldap_discon( op, rs,
127 LDAP_PROTOCOL_ERROR, "decoding error" );
128 rs->sr_err = SLAPD_DISCONNECT;
132 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
133 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
135 /* get_ctrls has sent results. Now clean up. */
139 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
140 if( rs->sr_err != LDAP_SUCCESS ) {
141 Debug( LDAP_DEBUG_ANY,
142 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
143 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
147 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
149 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
150 if( rs->sr_err != LDAP_SUCCESS ) {
151 Debug( LDAP_DEBUG_ANY,
152 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
153 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
157 if( rdn_validate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
158 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
159 op->orr_newrdn.bv_val, 0, 0 );
161 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
165 if( op->orr_newSup ) {
166 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
167 &nnewSuperior, op->o_tmpmemctx );
168 if( rs->sr_err != LDAP_SUCCESS ) {
169 Debug( LDAP_DEBUG_ANY,
170 "do_modrdn: invalid newSuperior (%s)\n",
171 newSuperior.bv_val, 0, 0 );
172 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
177 /* FIXME: temporary? */
178 op->orr_deleteoldrdn = deloldrdn;
180 op->o_bd = frontendDB;
181 rs->sr_err = frontendDB->be_modrdn( op, rs );
184 slap_graduate_commit_csn( op );
186 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
187 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
189 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
190 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
192 if ( !BER_BVISNULL( &pnewSuperior ) )
193 op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
194 if ( !BER_BVISNULL( &nnewSuperior ) )
195 op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
201 fe_op_modrdn( Operation *op, SlapReply *rs )
203 Backend *newSuperior_be = NULL;
205 struct berval pdn = BER_BVNULL;
207 if( op->o_req_ndn.bv_len == 0 ) {
208 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
210 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
211 "cannot rename the root DSE" );
214 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
215 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
216 frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len, 0 );
218 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
219 "cannot rename subschema subentry" );
223 Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
224 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
226 manageDSAit = get_manageDSAit( op );
229 * We could be serving multiple database backends. Select the
230 * appropriate one, or send a referral to our "referral server"
231 * if we don't hold it.
233 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
234 if ( op->o_bd == NULL ) {
235 rs->sr_ref = referral_rewrite( default_referral,
236 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
237 if (!rs->sr_ref) rs->sr_ref = default_referral;
239 if ( rs->sr_ref != NULL ) {
240 rs->sr_err = LDAP_REFERRAL;
241 op->o_bd = frontendDB;
242 send_ldap_result( op, rs );
245 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
247 op->o_bd = frontendDB;
248 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
249 "no global superior knowledge" );
255 /* check restrictions */
256 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
257 send_ldap_result( op, rs );
261 /* check for referrals */
262 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
266 /* Make sure that the entry being changed and the newSuperior are in
267 * the same backend, otherwise we return an error.
269 if( op->orr_newSup ) {
270 newSuperior_be = select_backend( op->orr_nnewSup, 0, 0 );
272 if ( newSuperior_be != op->o_bd ) {
273 /* newSuperior is in different backend */
274 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
275 "cannot rename between DSAs" );
281 #if defined( LDAP_SLAPI )
284 slapi_int_pblock_set_operation( pb, op );
285 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)op->o_req_dn.bv_val );
286 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)op->orr_newrdn.bv_val );
287 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
288 (void *)op->orr_newSup->bv_val );
289 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)op->orr_deleteoldrdn);
290 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
292 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
293 if ( rs->sr_err < 0 ) {
295 * A preoperation plugin failure will abort the
298 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
299 "failed.\n", 0, 0, 0);
300 if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
301 rs->sr_err == LDAP_SUCCESS ) {
302 rs->sr_err = LDAP_OTHER;
307 #endif /* defined( LDAP_SLAPI ) */
310 * do the modrdn if 1 && (2 || 3)
311 * 1) there is a modrdn function implemented in this backend;
312 * 2) this backend is master for what it holds;
313 * 3) it's a replica and the dn supplied is the update_ndn.
315 if ( op->o_bd->be_modrdn ) {
316 /* do the update here */
317 int repl_user = be_isupdate( op );
318 #ifndef SLAPD_MULTIMASTER
319 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
322 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
323 #ifdef SLAPD_MULTIMASTER
324 if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
327 cb.sc_next = op->o_callback;
328 op->o_callback = &cb;
330 op->o_bd->be_modrdn( op, rs );
332 if ( op->o_bd->be_delete ) {
333 struct berval org_req_dn = BER_BVNULL;
334 struct berval org_req_ndn = BER_BVNULL;
335 struct berval org_dn = BER_BVNULL;
336 struct berval org_ndn = BER_BVNULL;
339 org_req_dn = op->o_req_dn;
340 org_req_ndn = op->o_req_ndn;
343 org_managedsait = get_manageDSAit( op );
344 op->o_dn = op->o_bd->be_rootdn;
345 op->o_ndn = op->o_bd->be_rootndn;
346 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
348 while ( rs->sr_err == LDAP_SUCCESS &&
349 op->o_delete_glue_parent ) {
350 op->o_delete_glue_parent = 0;
351 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
352 slap_callback cb = { NULL };
353 cb.sc_response = slap_null_cb;
354 dnParent( &op->o_req_ndn, &pdn );
357 op->o_callback = &cb;
358 op->o_bd->be_delete( op, rs );
363 op->o_managedsait = org_managedsait;
366 op->o_req_dn = org_req_dn;
367 op->o_req_ndn = org_req_ndn;
368 op->o_delete_glue_parent = 0;
371 #ifndef SLAPD_MULTIMASTER
373 BerVarray defref = op->o_bd->be_update_refs
374 ? op->o_bd->be_update_refs : default_referral;
376 if ( defref != NULL ) {
377 rs->sr_ref = referral_rewrite( defref,
378 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
379 if (!rs->sr_ref) rs->sr_ref = defref;
381 rs->sr_err = LDAP_REFERRAL;
382 send_ldap_result( op, rs );
384 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
386 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
387 "shadow context; no update referral" );
392 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
393 "operation not supported within namingContext" );
396 #if defined( LDAP_SLAPI )
397 if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
398 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
399 "failed.\n", 0, 0, 0);
401 #endif /* defined( LDAP_SLAPI ) */
414 Modifications **pmod )
416 Modifications *mod = NULL;
417 Modifications **modtail = &mod;
421 assert( new_rdn != NULL );
422 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
424 repl_user = be_isupdate( op );
426 /* Add new attribute values to the entry */
427 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
428 AttributeDescription *desc = NULL;
429 Modifications *mod_tmp;
431 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
433 if ( rs->sr_err != LDAP_SUCCESS ) {
434 Debug( LDAP_DEBUG_TRACE,
435 "slap_modrdn2modlist: %s: %s (new)\n",
437 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
441 /* ACL check of newly added attrs */
442 if ( op->o_bd && !access_allowed( op, e, desc,
443 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
444 Debug( LDAP_DEBUG_TRACE,
445 "slap_modrdn2modlist: access to attr \"%s\" "
446 "(new) not allowed\n",
447 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
448 rs->sr_text = "access to naming attributes (new) not allowed";
449 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
453 /* Apply modification */
454 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
455 + 4 * sizeof( struct berval ) );
456 mod_tmp->sml_desc = desc;
457 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
458 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
459 mod_tmp->sml_values[1].bv_val = NULL;
460 if( desc->ad_type->sat_equality->smr_normalize) {
461 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
462 (void) (*desc->ad_type->sat_equality->smr_normalize)(
463 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
464 desc->ad_type->sat_syntax,
465 desc->ad_type->sat_equality,
466 &mod_tmp->sml_values[0],
467 &mod_tmp->sml_nvalues[0], NULL );
468 mod_tmp->sml_nvalues[1].bv_val = NULL;
470 mod_tmp->sml_nvalues = NULL;
472 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
473 mod_tmp->sml_next = mod;
477 /* Remove old rdn value if required */
478 if ( op->orr_deleteoldrdn ) {
479 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
480 AttributeDescription *desc = NULL;
481 Modifications *mod_tmp;
483 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
484 if ( rs->sr_err != LDAP_SUCCESS ) {
485 Debug( LDAP_DEBUG_TRACE,
486 "slap_modrdn2modlist: %s: %s (old)\n",
488 old_rdn[d_cnt]->la_attr.bv_val,
493 /* ACL check of newly added attrs */
494 if ( op->o_bd && !access_allowed( op, e, desc,
495 &old_rdn[d_cnt]->la_value, ACL_WRITE,
497 Debug( LDAP_DEBUG_TRACE,
498 "slap_modrdn2modlist: access "
499 "to attr \"%s\" (old) not allowed\n",
500 old_rdn[ d_cnt ]->la_attr.bv_val,
502 rs->sr_text = "access to naming attributes (old) not allowed";
503 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
507 /* Apply modification */
508 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
509 + 4 * sizeof ( struct berval ) );
510 mod_tmp->sml_desc = desc;
511 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
512 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
513 mod_tmp->sml_values[1].bv_val = NULL;
514 if( desc->ad_type->sat_equality->smr_normalize) {
515 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
516 (void) (*desc->ad_type->sat_equality->smr_normalize)(
517 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
518 desc->ad_type->sat_syntax,
519 desc->ad_type->sat_equality,
520 &mod_tmp->sml_values[0],
521 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
522 mod_tmp->sml_nvalues[1].bv_val = NULL;
524 mod_tmp->sml_nvalues = NULL;
526 mod_tmp->sml_op = LDAP_MOD_DELETE;
527 mod_tmp->sml_next = mod;
534 if ( rs->sr_err == LDAP_SUCCESS && !repl_user ) {
535 char textbuf[ SLAP_TEXT_BUFLEN ];
536 size_t textlen = sizeof textbuf;
540 modtail = &(*modtail)->sml_next )
545 rs->sr_err = slap_mods_opattrs( op, mod, modtail,
546 &rs->sr_text, textbuf, textlen, 1 );
549 /* LDAP v2 supporting correct attribute handling. */
550 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
552 for ( ; mod; mod = tmp ) {