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 = BER_BVNULL;
54 struct berval newrdn = BER_BVNULL;
55 struct berval newSuperior = BER_BVNULL;
58 struct berval pnewSuperior = BER_BVNULL;
60 struct berval nnewSuperior = BER_BVNULL;
65 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
67 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
72 * Parse the modrdn request. It looks like this:
74 * ModifyRDNRequest := SEQUENCE {
75 * entry DistinguishedName,
76 * newrdn RelativeDistinguishedName
77 * deleteoldrdn BOOLEAN,
78 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
82 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
86 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
88 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
91 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
92 return SLAPD_DISCONNECT;
95 /* Check for newSuperior parameter, if present scan it */
97 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
98 if ( op->o_protocol < LDAP_VERSION3 ) {
99 /* Conection record indicates v2 but field
100 * newSuperior is present: report error.
103 LDAP_LOG( OPERATION, ERR,
104 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
106 Debug( LDAP_DEBUG_ANY,
107 "modrdn(v2): invalid field newSuperior!\n",
111 send_ldap_discon( op, rs,
112 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
113 rs->sr_err = SLAPD_DISCONNECT;
117 if ( ber_scanf( op->o_ber, "m", &newSuperior )
121 LDAP_LOG( OPERATION, ERR,
122 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
124 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
128 send_ldap_discon( op, rs,
129 LDAP_PROTOCOL_ERROR, "decoding error" );
130 rs->sr_err = SLAPD_DISCONNECT;
133 op->orr_newSup = &pnewSuperior;
134 op->orr_nnewSup = &nnewSuperior;
138 LDAP_LOG( OPERATION, ARGS,
139 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
140 dn.bv_val, newrdn.bv_val,
141 newSuperior.bv_len ? newSuperior.bv_val : "" );
143 Debug( LDAP_DEBUG_ARGS,
144 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
145 dn.bv_val, newrdn.bv_val,
146 newSuperior.bv_len ? newSuperior.bv_val : "" );
149 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
151 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
153 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
156 send_ldap_discon( op, rs,
157 LDAP_PROTOCOL_ERROR, "decoding error" );
158 rs->sr_err = SLAPD_DISCONNECT;
162 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
164 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
166 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
169 /* get_ctrls has sent results. Now clean up. */
173 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
174 if( rs->sr_err != LDAP_SUCCESS ) {
176 LDAP_LOG( OPERATION, INFO,
177 "do_modrdn: conn %d invalid dn (%s)\n",
178 op->o_connid, dn.bv_val, 0 );
180 Debug( LDAP_DEBUG_ANY,
181 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
183 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
187 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
189 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
190 if( rs->sr_err != LDAP_SUCCESS ) {
192 LDAP_LOG( OPERATION, INFO,
193 "do_modrdn: conn %d invalid newrdn (%s)\n",
194 op->o_connid, newrdn.bv_val, 0 );
196 Debug( LDAP_DEBUG_ANY,
197 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
199 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
203 if( rdn_validate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
205 LDAP_LOG( OPERATION, ERR,
206 "do_modrdn: invalid rdn (%s).\n", op->orr_newrdn.bv_val, 0, 0 );
208 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
209 op->orr_newrdn.bv_val, 0, 0 );
212 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
216 if( op->orr_newSup ) {
217 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
218 &nnewSuperior, op->o_tmpmemctx );
219 if( rs->sr_err != LDAP_SUCCESS ) {
221 LDAP_LOG( OPERATION, INFO,
222 "do_modrdn: conn %d invalid newSuperior (%s)\n",
223 op->o_connid, newSuperior.bv_val, 0 );
225 Debug( LDAP_DEBUG_ANY,
226 "do_modrdn: invalid newSuperior (%s)\n",
227 newSuperior.bv_val, 0, 0 );
229 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
234 /* FIXME: temporary? */
235 op->orr_deleteoldrdn = deloldrdn;
237 op->o_bd = frontendDB;
238 rs->sr_err = frontendDB->be_modrdn( op, rs );
242 slap_graduate_commit_csn( op );
244 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
245 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
247 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
248 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
250 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
251 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
257 fe_op_modrdn( Operation *op, SlapReply *rs )
259 Backend *newSuperior_be = NULL;
261 struct berval pdn = BER_BVNULL;
263 if( op->o_req_ndn.bv_len == 0 ) {
265 LDAP_LOG( OPERATION, ERR,
266 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
268 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
271 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
272 "cannot rename the root DSE" );
275 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
277 LDAP_LOG( OPERATION, ERR,
278 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
279 frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len, 0 );
281 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
282 frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len, 0 );
285 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
286 "cannot rename subschema subentry" );
290 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
291 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
293 manageDSAit = get_manageDSAit( op );
296 * We could be serving multiple database backends. Select the
297 * appropriate one, or send a referral to our "referral server"
298 * if we don't hold it.
300 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
301 if ( op->o_bd == NULL ) {
302 rs->sr_ref = referral_rewrite( default_referral,
303 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
304 if (!rs->sr_ref) rs->sr_ref = default_referral;
306 if ( rs->sr_ref != NULL ) {
307 rs->sr_err = LDAP_REFERRAL;
308 send_ldap_result( op, rs );
310 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
312 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
313 "no global superior knowledge" );
318 /* check restrictions */
319 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
320 send_ldap_result( op, rs );
324 /* check for referrals */
325 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
329 /* Make sure that the entry being changed and the newSuperior are in
330 * the same backend, otherwise we return an error.
332 if( op->orr_newSup ) {
333 newSuperior_be = select_backend( op->orr_nnewSup, 0, 0 );
335 if ( newSuperior_be != op->o_bd ) {
336 /* newSuperior is in different backend */
337 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
338 "cannot rename between DSAs" );
344 #if defined( LDAP_SLAPI )
347 slapi_int_pblock_set_operation( pb, op );
348 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)op->o_req_dn.bv_val );
349 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)op->orr_newrdn.bv_val );
350 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
351 (void *)op->orr_newSup->bv_val );
352 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)op->orr_deleteoldrdn);
353 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
355 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
356 if ( rs->sr_err < 0 ) {
358 * A preoperation plugin failure will abort the
362 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
363 "failed\n", 0, 0, 0 );
365 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
366 "failed.\n", 0, 0, 0);
368 if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
369 rs->sr_err == LDAP_SUCCESS ) {
370 rs->sr_err = LDAP_OTHER;
375 #endif /* defined( LDAP_SLAPI ) */
378 * do the modrdn if 1 && (2 || 3)
379 * 1) there is a modrdn function implemented in this backend;
380 * 2) this backend is master for what it holds;
381 * 3) it's a replica and the dn supplied is the update_ndn.
383 if ( op->o_bd->be_modrdn ) {
384 /* do the update here */
385 int repl_user = be_isupdate( op );
386 #ifndef SLAPD_MULTIMASTER
387 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
390 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
391 #ifdef SLAPD_MULTIMASTER
392 if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
395 cb.sc_next = op->o_callback;
396 op->o_callback = &cb;
398 op->o_bd->be_modrdn( op, rs );
400 if ( op->o_bd->be_delete ) {
401 struct berval org_req_dn = BER_BVNULL;
402 struct berval org_req_ndn = BER_BVNULL;
403 struct berval org_dn = BER_BVNULL;
404 struct berval org_ndn = BER_BVNULL;
407 org_req_dn = op->o_req_dn;
408 org_req_ndn = op->o_req_ndn;
411 org_managedsait = get_manageDSAit( op );
412 op->o_dn = op->o_bd->be_rootdn;
413 op->o_ndn = op->o_bd->be_rootndn;
414 op->o_managedsait = 1;
416 while ( rs->sr_err == LDAP_SUCCESS &&
417 op->o_delete_glue_parent ) {
418 op->o_delete_glue_parent = 0;
419 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
420 slap_callback cb = { NULL };
421 cb.sc_response = slap_null_cb;
422 dnParent( &op->o_req_ndn, &pdn );
425 op->o_callback = &cb;
426 op->o_bd->be_delete( op, rs );
431 op->o_managedsait = org_managedsait;
434 op->o_req_dn = org_req_dn;
435 op->o_req_ndn = org_req_ndn;
436 op->o_delete_glue_parent = 0;
439 #ifndef SLAPD_MULTIMASTER
441 BerVarray defref = op->o_bd->be_update_refs
442 ? op->o_bd->be_update_refs : default_referral;
444 if ( defref != NULL ) {
445 rs->sr_ref = referral_rewrite( defref,
446 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
447 if (!rs->sr_ref) rs->sr_ref = defref;
449 rs->sr_err = LDAP_REFERRAL;
450 send_ldap_result( op, rs );
452 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
454 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
455 "shadow context; no update referral" );
460 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
461 "operation not supported within namingContext" );
464 #if defined( LDAP_SLAPI )
465 if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
467 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
468 "failed\n", 0, 0, 0 );
470 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
471 "failed.\n", 0, 0, 0);
474 #endif /* defined( LDAP_SLAPI ) */
487 Modifications **pmod )
489 Modifications *mod = NULL;
490 Modifications **modtail = &mod;
494 assert( new_rdn != NULL );
495 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
497 repl_user = be_isupdate( op );
499 /* Add new attribute values to the entry */
500 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
501 AttributeDescription *desc = NULL;
502 Modifications *mod_tmp;
504 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
506 if ( rs->sr_err != LDAP_SUCCESS ) {
508 LDAP_LOG ( OPERATION, ERR,
509 "slap_modrdn2modlist: %s: %s (new)\n",
511 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
513 Debug( LDAP_DEBUG_TRACE,
514 "slap_modrdn2modlist: %s: %s (new)\n",
516 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
521 /* ACL check of newly added attrs */
522 if ( op->o_bd && !access_allowed( op, e, desc,
523 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
525 LDAP_LOG ( OPERATION, ERR,
526 "slap_modrdn2modlist: access to attr \"%s\" "
527 "(new) not allowed\n",
528 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
530 Debug( LDAP_DEBUG_TRACE,
531 "slap_modrdn2modlist: access to attr \"%s\" "
532 "(new) not allowed\n",
533 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
535 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
539 /* Apply modification */
540 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
541 + 4 * sizeof( struct berval ) );
542 mod_tmp->sml_desc = desc;
543 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
544 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
545 mod_tmp->sml_values[1].bv_val = NULL;
546 if( desc->ad_type->sat_equality->smr_normalize) {
547 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
548 (void) (*desc->ad_type->sat_equality->smr_normalize)(
549 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
550 desc->ad_type->sat_syntax,
551 desc->ad_type->sat_equality,
552 &mod_tmp->sml_values[0],
553 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
554 mod_tmp->sml_nvalues[1].bv_val = NULL;
556 mod_tmp->sml_nvalues = NULL;
558 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
559 mod_tmp->sml_next = mod;
563 /* Remove old rdn value if required */
564 if ( op->orr_deleteoldrdn ) {
565 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
566 AttributeDescription *desc = NULL;
567 Modifications *mod_tmp;
569 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
570 if ( rs->sr_err != LDAP_SUCCESS ) {
572 LDAP_LOG ( OPERATION, ERR,
573 "slap_modrdn2modlist: %s: %s (old)\n",
575 old_rdn[d_cnt]->la_attr.bv_val,
578 Debug( LDAP_DEBUG_TRACE,
579 "slap_modrdn2modlist: %s: %s (old)\n",
581 old_rdn[d_cnt]->la_attr.bv_val,
587 /* ACL check of newly added attrs */
588 if ( op->o_bd && !access_allowed( op, e, desc,
589 &old_rdn[d_cnt]->la_value, ACL_WRITE,
592 LDAP_LOG ( OPERATION, ERR,
593 "slap_modrdn2modlist: access "
594 "to attr \"%s\" (old) not allowed\n",
595 old_rdn[ d_cnt ]->la_attr.bv_val,
598 Debug( LDAP_DEBUG_TRACE,
599 "slap_modrdn2modlist: access "
600 "to attr \"%s\" (old) not allowed\n",
601 old_rdn[ d_cnt ]->la_attr.bv_val,
604 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
608 /* Apply modification */
609 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
610 + 4 * sizeof ( struct berval ) );
611 mod_tmp->sml_desc = desc;
612 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
613 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
614 mod_tmp->sml_values[1].bv_val = NULL;
615 if( desc->ad_type->sat_equality->smr_normalize) {
616 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
617 (void) (*desc->ad_type->sat_equality->smr_normalize)(
618 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
619 desc->ad_type->sat_syntax,
620 desc->ad_type->sat_equality,
621 &mod_tmp->sml_values[0],
622 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
623 mod_tmp->sml_nvalues[1].bv_val = NULL;
625 mod_tmp->sml_nvalues = NULL;
627 mod_tmp->sml_op = LDAP_MOD_DELETE;
628 mod_tmp->sml_next = mod;
636 char textbuf[ SLAP_TEXT_BUFLEN ];
637 size_t textlen = sizeof textbuf;
641 modtail = &(*modtail)->sml_next )
646 rs->sr_err = slap_mods_opattrs( op, mod, modtail, &rs->sr_text, textbuf, textlen );
649 /* LDAP v2 supporting correct attribute handling. */
650 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
652 for ( ; mod; mod = tmp ) {