2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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>
44 #include "slapi/slapi.h"
53 struct berval dn = { 0, NULL };
54 struct berval newrdn = { 0, NULL };
55 struct berval newSuperior = { 0, NULL };
58 struct berval pnewSuperior = { 0, NULL };
60 struct berval nnewSuperior = { 0, NULL };
62 Backend *newSuperior_be = NULL;
66 struct berval pdn = { 0, NULL };
67 struct berval org_req_dn = { 0, NULL };
68 struct berval org_req_ndn = { 0, NULL };
69 struct berval org_dn = { 0, NULL };
70 struct berval org_ndn = { 0, NULL };
74 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
76 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
81 * Parse the modrdn request. It looks like this:
83 * ModifyRDNRequest := SEQUENCE {
84 * entry DistinguishedName,
85 * newrdn RelativeDistinguishedName
86 * deleteoldrdn BOOLEAN,
87 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
91 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
95 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
97 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
100 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
101 return SLAPD_DISCONNECT;
104 /* Check for newSuperior parameter, if present scan it */
106 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
107 if ( op->o_protocol < LDAP_VERSION3 ) {
108 /* Conection record indicates v2 but field
109 * newSuperior is present: report error.
112 LDAP_LOG( OPERATION, ERR,
113 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
115 Debug( LDAP_DEBUG_ANY,
116 "modrdn(v2): invalid field newSuperior!\n",
120 send_ldap_discon( op, rs,
121 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
122 rs->sr_err = SLAPD_DISCONNECT;
126 if ( ber_scanf( op->o_ber, "m", &newSuperior )
130 LDAP_LOG( OPERATION, ERR,
131 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
133 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
137 send_ldap_discon( op, rs,
138 LDAP_PROTOCOL_ERROR, "decoding error" );
139 rs->sr_err = SLAPD_DISCONNECT;
142 op->orr_newSup = &pnewSuperior;
143 op->orr_nnewSup = &nnewSuperior;
147 LDAP_LOG( OPERATION, ARGS,
148 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
149 dn.bv_val, newrdn.bv_val,
150 newSuperior.bv_len ? newSuperior.bv_val : "" );
152 Debug( LDAP_DEBUG_ARGS,
153 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
154 dn.bv_val, newrdn.bv_val,
155 newSuperior.bv_len ? newSuperior.bv_val : "" );
158 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
160 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
162 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
165 send_ldap_discon( op, rs,
166 LDAP_PROTOCOL_ERROR, "decoding error" );
167 rs->sr_err = SLAPD_DISCONNECT;
171 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
173 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
175 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
178 /* get_ctrls has sent results. Now clean up. */
182 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
183 if( rs->sr_err != LDAP_SUCCESS ) {
185 LDAP_LOG( OPERATION, INFO,
186 "do_modrdn: conn %d invalid dn (%s)\n",
187 op->o_connid, dn.bv_val, 0 );
189 Debug( LDAP_DEBUG_ANY,
190 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
192 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
196 if( op->o_req_ndn.bv_len == 0 ) {
198 LDAP_LOG( OPERATION, ERR,
199 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
201 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
204 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
205 "cannot rename the root DSE" );
208 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
210 LDAP_LOG( OPERATION, ERR,
211 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
212 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
214 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
215 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
218 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
219 "cannot rename subschema subentry" );
223 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
225 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
226 if( rs->sr_err != LDAP_SUCCESS ) {
228 LDAP_LOG( OPERATION, INFO,
229 "do_modrdn: conn %d invalid newrdn (%s)\n",
230 op->o_connid, newrdn.bv_val, 0 );
232 Debug( LDAP_DEBUG_ANY,
233 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
235 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
239 if( rdnValidate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
241 LDAP_LOG( OPERATION, ERR,
242 "do_modrdn: invalid rdn (%s).\n", op->orr_newrdn.bv_val, 0, 0 );
244 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
245 op->orr_newrdn.bv_val, 0, 0 );
248 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
252 if( op->orr_newSup ) {
253 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
254 &nnewSuperior, op->o_tmpmemctx );
255 if( rs->sr_err != LDAP_SUCCESS ) {
257 LDAP_LOG( OPERATION, INFO,
258 "do_modrdn: conn %d invalid newSuperior (%s)\n",
259 op->o_connid, newSuperior.bv_val, 0 );
261 Debug( LDAP_DEBUG_ANY,
262 "do_modrdn: invalid newSuperior (%s)\n",
263 newSuperior.bv_val, 0, 0 );
265 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
270 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
271 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
273 manageDSAit = get_manageDSAit( op );
276 * We could be serving multiple database backends. Select the
277 * appropriate one, or send a referral to our "referral server"
278 * if we don't hold it.
280 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
281 rs->sr_ref = referral_rewrite( default_referral,
282 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
283 if (!rs->sr_ref) rs->sr_ref = default_referral;
285 if ( rs->sr_ref != NULL ) {
286 rs->sr_err = LDAP_REFERRAL;
287 send_ldap_result( op, rs );
289 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
291 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
292 "referral missing" );
297 /* check restrictions */
298 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
299 send_ldap_result( op, rs );
303 /* check for referrals */
304 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
308 /* Make sure that the entry being changed and the newSuperior are in
309 * the same backend, otherwise we return an error.
311 if( op->orr_newSup ) {
312 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
314 if ( newSuperior_be != op->o_bd ) {
315 /* newSuperior is in different backend */
316 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
317 "cannot rename between DSAs" );
323 #if defined( LDAP_SLAPI )
326 slapi_int_pblock_set_operation( pb, op );
327 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
328 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
329 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
330 (void *)newSuperior.bv_val );
331 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
332 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
334 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
335 if ( rs->sr_err < 0 ) {
337 * A preoperation plugin failure will abort the
341 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
342 "failed\n", 0, 0, 0 );
344 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
345 "failed.\n", 0, 0, 0);
347 if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
348 rs->sr_err == LDAP_SUCCESS ) {
349 rs->sr_err = LDAP_OTHER;
354 #endif /* defined( LDAP_SLAPI ) */
357 * do the modrdn if 1 && (2 || 3)
358 * 1) there is a modrdn function implemented in this backend;
359 * 2) this backend is master for what it holds;
360 * 3) it's a replica and the dn supplied is the update_ndn.
362 if ( op->o_bd->be_modrdn ) {
363 /* do the update here */
364 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
365 #ifndef SLAPD_MULTIMASTER
366 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ) &&
367 ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
369 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ))
372 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
373 op->orr_deleteoldrdn = deloldrdn;
374 #ifdef SLAPD_MULTIMASTER
375 if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
378 cb.sc_next = op->o_callback;
379 op->o_callback = &cb;
381 op->o_bd->be_modrdn( op, rs );
383 if ( op->o_bd->be_delete ) {
384 org_req_dn = op->o_req_dn;
385 org_req_ndn = op->o_req_ndn;
388 org_managedsait = get_manageDSAit( op );
389 op->o_dn = op->o_bd->be_rootdn;
390 op->o_ndn = op->o_bd->be_rootndn;
391 op->o_managedsait = 1;
393 while ( rs->sr_err == LDAP_SUCCESS &&
394 op->o_delete_glue_parent ) {
395 op->o_delete_glue_parent = 0;
396 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
397 slap_callback cb = { NULL };
398 cb.sc_response = slap_null_cb;
399 dnParent( &op->o_req_ndn, &pdn );
402 op->o_callback = &cb;
403 op->o_bd->be_delete( op, rs );
408 op->o_managedsait = org_managedsait;
411 op->o_req_dn = org_req_dn;
412 op->o_req_ndn = org_req_ndn;
413 op->o_delete_glue_parent = 0;
416 #ifndef SLAPD_MULTIMASTER
418 BerVarray defref = NULL;
419 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
421 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
423 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
424 ber_bvarray_add( &defref, &tmpbv );
427 defref = op->o_bd->be_update_refs
428 ? op->o_bd->be_update_refs : default_referral;
430 if ( defref != NULL ) {
431 rs->sr_ref = referral_rewrite( defref,
432 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
433 if (!rs->sr_ref) rs->sr_ref = defref;
435 rs->sr_err = LDAP_REFERRAL;
436 send_ldap_result( op, rs );
438 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
440 send_ldap_error( op, rs,
441 LDAP_UNWILLING_TO_PERFORM,
442 "referral missing" );
447 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
448 "operation not supported within namingContext" );
451 #if defined( LDAP_SLAPI )
452 if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
454 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
455 "failed\n", 0, 0, 0 );
457 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
458 "failed.\n", 0, 0, 0);
461 #endif /* defined( LDAP_SLAPI ) */
465 slap_graduate_commit_csn( op );
467 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
468 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
470 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
471 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
473 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
474 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
486 Modifications **pmod )
488 Modifications *mod = NULL;
489 Modifications **modtail = &mod;
493 assert( new_rdn != NULL );
494 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
496 repl_user = be_isupdate( op->o_bd, &op->o_ndn );
498 /* Add new attribute values to the entry */
499 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
500 AttributeDescription *desc = NULL;
501 Modifications *mod_tmp;
503 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
505 if ( rs->sr_err != LDAP_SUCCESS ) {
507 LDAP_LOG ( OPERATION, ERR,
508 "slap_modrdn2modlist: %s: %s (new)\n",
510 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
512 Debug( LDAP_DEBUG_TRACE,
513 "slap_modrdn2modlist: %s: %s (new)\n",
515 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
520 /* ACL check of newly added attrs */
521 if ( op->o_bd && !access_allowed( op, e, desc,
522 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
524 LDAP_LOG ( OPERATION, ERR,
525 "slap_modrdn2modlist: access to attr \"%s\" "
526 "(new) not allowed\n",
527 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
529 Debug( LDAP_DEBUG_TRACE,
530 "slap_modrdn2modlist: access to attr \"%s\" "
531 "(new) not allowed\n",
532 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
534 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
538 /* Apply modification */
539 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
540 + 4 * sizeof( struct berval ) );
541 mod_tmp->sml_desc = desc;
542 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
543 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
544 mod_tmp->sml_values[1].bv_val = NULL;
545 if( desc->ad_type->sat_equality->smr_normalize) {
546 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
547 (void) (*desc->ad_type->sat_equality->smr_normalize)(
548 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
549 desc->ad_type->sat_syntax,
550 desc->ad_type->sat_equality,
551 &mod_tmp->sml_values[0],
552 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
553 mod_tmp->sml_nvalues[1].bv_val = NULL;
555 mod_tmp->sml_nvalues = NULL;
557 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
558 mod_tmp->sml_next = mod;
562 /* Remove old rdn value if required */
563 if ( op->orr_deleteoldrdn ) {
564 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
565 AttributeDescription *desc = NULL;
566 Modifications *mod_tmp;
568 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
569 if ( rs->sr_err != LDAP_SUCCESS ) {
571 LDAP_LOG ( OPERATION, ERR,
572 "slap_modrdn2modlist: %s: %s (old)\n",
574 old_rdn[d_cnt]->la_attr.bv_val,
577 Debug( LDAP_DEBUG_TRACE,
578 "slap_modrdn2modlist: %s: %s (old)\n",
580 old_rdn[d_cnt]->la_attr.bv_val,
586 /* ACL check of newly added attrs */
587 if ( op->o_bd && !access_allowed( op, e, desc,
588 &old_rdn[d_cnt]->la_value, ACL_WRITE,
591 LDAP_LOG ( OPERATION, ERR,
592 "slap_modrdn2modlist: access "
593 "to attr \"%s\" (old) not allowed\n",
594 old_rdn[ d_cnt ]->la_attr.bv_val,
597 Debug( LDAP_DEBUG_TRACE,
598 "slap_modrdn2modlist: access "
599 "to attr \"%s\" (old) not allowed\n",
600 old_rdn[ d_cnt ]->la_attr.bv_val,
603 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
607 /* Apply modification */
608 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
609 + 4 * sizeof ( struct berval ) );
610 mod_tmp->sml_desc = desc;
611 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
612 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
613 mod_tmp->sml_values[1].bv_val = NULL;
614 if( desc->ad_type->sat_equality->smr_normalize) {
615 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
616 (void) (*desc->ad_type->sat_equality->smr_normalize)(
617 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
618 desc->ad_type->sat_syntax,
619 desc->ad_type->sat_equality,
620 &mod_tmp->sml_values[0],
621 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
622 mod_tmp->sml_nvalues[1].bv_val = NULL;
624 mod_tmp->sml_nvalues = NULL;
626 mod_tmp->sml_op = LDAP_MOD_DELETE;
627 mod_tmp->sml_next = mod;
635 char textbuf[ SLAP_TEXT_BUFLEN ];
636 size_t textlen = sizeof textbuf;
640 modtail = &(*modtail)->sml_next )
645 rs->sr_err = slap_mods_opattrs( op, mod, modtail, &rs->sr_text, textbuf, textlen );
648 /* LDAP v2 supporting correct attribute handling. */
649 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
651 for ( ; mod; mod = tmp ) {