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;
62 Backend *newSuperior_be = NULL;
66 struct berval pdn = BER_BVNULL;
67 struct berval org_req_dn = BER_BVNULL;
68 struct berval org_req_ndn = BER_BVNULL;
69 struct berval org_dn = BER_BVNULL;
70 struct berval org_ndn = BER_BVNULL;
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 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
281 if ( op->o_bd == NULL ) {
282 rs->sr_ref = referral_rewrite( default_referral,
283 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
284 if (!rs->sr_ref) rs->sr_ref = default_referral;
286 if ( rs->sr_ref != NULL ) {
287 rs->sr_err = LDAP_REFERRAL;
288 send_ldap_result( op, rs );
290 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
292 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
293 "no global superior knowledge" );
298 /* check restrictions */
299 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
300 send_ldap_result( op, rs );
304 /* check for referrals */
305 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
309 /* Make sure that the entry being changed and the newSuperior are in
310 * the same backend, otherwise we return an error.
312 if( op->orr_newSup ) {
313 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
315 if ( newSuperior_be != op->o_bd ) {
316 /* newSuperior is in different backend */
317 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
318 "cannot rename between DSAs" );
324 #if defined( LDAP_SLAPI )
327 slapi_int_pblock_set_operation( pb, op );
328 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
329 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
330 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
331 (void *)newSuperior.bv_val );
332 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
333 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
335 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
336 if ( rs->sr_err < 0 ) {
338 * A preoperation plugin failure will abort the
342 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
343 "failed\n", 0, 0, 0 );
345 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
346 "failed.\n", 0, 0, 0);
348 if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
349 rs->sr_err == LDAP_SUCCESS ) {
350 rs->sr_err = LDAP_OTHER;
355 #endif /* defined( LDAP_SLAPI ) */
358 * do the modrdn if 1 && (2 || 3)
359 * 1) there is a modrdn function implemented in this backend;
360 * 2) this backend is master for what it holds;
361 * 3) it's a replica and the dn supplied is the update_ndn.
363 if ( op->o_bd->be_modrdn ) {
364 /* do the update here */
365 int repl_user = be_isupdate( op );
366 #ifndef SLAPD_MULTIMASTER
367 if ( !SLAP_SHADOW(op->o_bd) || 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 = op->o_bd->be_update_refs
419 ? op->o_bd->be_update_refs : default_referral;
421 if ( defref != NULL ) {
422 rs->sr_ref = referral_rewrite( defref,
423 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
424 if (!rs->sr_ref) rs->sr_ref = defref;
426 rs->sr_err = LDAP_REFERRAL;
427 send_ldap_result( op, rs );
429 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
431 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
432 "shadow context; no update referral" );
437 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
438 "operation not supported within namingContext" );
441 #if defined( LDAP_SLAPI )
442 if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
444 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
445 "failed\n", 0, 0, 0 );
447 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
448 "failed.\n", 0, 0, 0);
451 #endif /* defined( LDAP_SLAPI ) */
455 slap_graduate_commit_csn( op );
457 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
458 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
460 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
461 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
463 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
464 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
476 Modifications **pmod )
478 Modifications *mod = NULL;
479 Modifications **modtail = &mod;
483 assert( new_rdn != NULL );
484 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
486 repl_user = be_isupdate( op );
488 /* Add new attribute values to the entry */
489 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
490 AttributeDescription *desc = NULL;
491 Modifications *mod_tmp;
493 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
495 if ( rs->sr_err != LDAP_SUCCESS ) {
497 LDAP_LOG ( OPERATION, ERR,
498 "slap_modrdn2modlist: %s: %s (new)\n",
500 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
502 Debug( LDAP_DEBUG_TRACE,
503 "slap_modrdn2modlist: %s: %s (new)\n",
505 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
510 /* ACL check of newly added attrs */
511 if ( op->o_bd && !access_allowed( op, e, desc,
512 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
514 LDAP_LOG ( OPERATION, ERR,
515 "slap_modrdn2modlist: access to attr \"%s\" "
516 "(new) not allowed\n",
517 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
519 Debug( LDAP_DEBUG_TRACE,
520 "slap_modrdn2modlist: access to attr \"%s\" "
521 "(new) not allowed\n",
522 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
524 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
528 /* Apply modification */
529 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
530 + 4 * sizeof( struct berval ) );
531 mod_tmp->sml_desc = desc;
532 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
533 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
534 mod_tmp->sml_values[1].bv_val = NULL;
535 if( desc->ad_type->sat_equality->smr_normalize) {
536 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
537 (void) (*desc->ad_type->sat_equality->smr_normalize)(
538 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
539 desc->ad_type->sat_syntax,
540 desc->ad_type->sat_equality,
541 &mod_tmp->sml_values[0],
542 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
543 mod_tmp->sml_nvalues[1].bv_val = NULL;
545 mod_tmp->sml_nvalues = NULL;
547 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
548 mod_tmp->sml_next = mod;
552 /* Remove old rdn value if required */
553 if ( op->orr_deleteoldrdn ) {
554 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
555 AttributeDescription *desc = NULL;
556 Modifications *mod_tmp;
558 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
559 if ( rs->sr_err != LDAP_SUCCESS ) {
561 LDAP_LOG ( OPERATION, ERR,
562 "slap_modrdn2modlist: %s: %s (old)\n",
564 old_rdn[d_cnt]->la_attr.bv_val,
567 Debug( LDAP_DEBUG_TRACE,
568 "slap_modrdn2modlist: %s: %s (old)\n",
570 old_rdn[d_cnt]->la_attr.bv_val,
576 /* ACL check of newly added attrs */
577 if ( op->o_bd && !access_allowed( op, e, desc,
578 &old_rdn[d_cnt]->la_value, ACL_WRITE,
581 LDAP_LOG ( OPERATION, ERR,
582 "slap_modrdn2modlist: access "
583 "to attr \"%s\" (old) not allowed\n",
584 old_rdn[ d_cnt ]->la_attr.bv_val,
587 Debug( LDAP_DEBUG_TRACE,
588 "slap_modrdn2modlist: access "
589 "to attr \"%s\" (old) not allowed\n",
590 old_rdn[ d_cnt ]->la_attr.bv_val,
593 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
597 /* Apply modification */
598 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
599 + 4 * sizeof ( struct berval ) );
600 mod_tmp->sml_desc = desc;
601 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
602 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
603 mod_tmp->sml_values[1].bv_val = NULL;
604 if( desc->ad_type->sat_equality->smr_normalize) {
605 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
606 (void) (*desc->ad_type->sat_equality->smr_normalize)(
607 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
608 desc->ad_type->sat_syntax,
609 desc->ad_type->sat_equality,
610 &mod_tmp->sml_values[0],
611 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
612 mod_tmp->sml_nvalues[1].bv_val = NULL;
614 mod_tmp->sml_nvalues = NULL;
616 mod_tmp->sml_op = LDAP_MOD_DELETE;
617 mod_tmp->sml_next = mod;
625 char textbuf[ SLAP_TEXT_BUFLEN ];
626 size_t textlen = sizeof textbuf;
630 modtail = &(*modtail)->sml_next )
635 rs->sr_err = slap_mods_opattrs( op, mod, modtail, &rs->sr_text, textbuf, textlen );
638 /* LDAP v2 supporting correct attribute handling. */
639 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
641 for ( ; mod; mod = tmp ) {