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;
67 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
69 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
74 * Parse the modrdn request. It looks like this:
76 * ModifyRDNRequest := SEQUENCE {
77 * entry DistinguishedName,
78 * newrdn RelativeDistinguishedName
79 * deleteoldrdn BOOLEAN,
80 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
84 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
88 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
90 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
93 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
94 return SLAPD_DISCONNECT;
97 /* Check for newSuperior parameter, if present scan it */
99 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
100 if ( op->o_protocol < LDAP_VERSION3 ) {
101 /* Conection record indicates v2 but field
102 * newSuperior is present: report error.
105 LDAP_LOG( OPERATION, ERR,
106 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
108 Debug( LDAP_DEBUG_ANY,
109 "modrdn(v2): invalid field newSuperior!\n",
113 send_ldap_discon( op, rs,
114 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
115 rs->sr_err = SLAPD_DISCONNECT;
119 if ( ber_scanf( op->o_ber, "m", &newSuperior )
123 LDAP_LOG( OPERATION, ERR,
124 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
126 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
130 send_ldap_discon( op, rs,
131 LDAP_PROTOCOL_ERROR, "decoding error" );
132 rs->sr_err = SLAPD_DISCONNECT;
135 op->orr_newSup = &pnewSuperior;
136 op->orr_nnewSup = &nnewSuperior;
140 LDAP_LOG( OPERATION, ARGS,
141 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
142 dn.bv_val, newrdn.bv_val,
143 newSuperior.bv_len ? newSuperior.bv_val : "" );
145 Debug( LDAP_DEBUG_ARGS,
146 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
147 dn.bv_val, newrdn.bv_val,
148 newSuperior.bv_len ? newSuperior.bv_val : "" );
151 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
153 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
155 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
158 send_ldap_discon( op, rs,
159 LDAP_PROTOCOL_ERROR, "decoding error" );
160 rs->sr_err = SLAPD_DISCONNECT;
164 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
166 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
168 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
171 /* get_ctrls has sent results. Now clean up. */
175 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
176 if( rs->sr_err != LDAP_SUCCESS ) {
178 LDAP_LOG( OPERATION, INFO,
179 "do_modrdn: conn %d invalid dn (%s)\n",
180 op->o_connid, dn.bv_val, 0 );
182 Debug( LDAP_DEBUG_ANY,
183 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
185 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
189 if( op->o_req_ndn.bv_len == 0 ) {
191 LDAP_LOG( OPERATION, ERR,
192 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
194 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
197 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
198 "cannot rename the root DSE" );
201 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
203 LDAP_LOG( OPERATION, ERR,
204 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
205 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
207 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
208 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
211 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
212 "cannot rename subschema subentry" );
216 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
218 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
219 if( rs->sr_err != LDAP_SUCCESS ) {
221 LDAP_LOG( OPERATION, INFO,
222 "do_modrdn: conn %d invalid newrdn (%s)\n",
223 op->o_connid, newrdn.bv_val, 0 );
225 Debug( LDAP_DEBUG_ANY,
226 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
228 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
232 if( rdnValidate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
234 LDAP_LOG( OPERATION, ERR,
235 "do_modrdn: invalid rdn (%s).\n", op->orr_newrdn.bv_val, 0, 0 );
237 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
238 op->orr_newrdn.bv_val, 0, 0 );
241 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
245 if( op->orr_newSup ) {
246 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
247 &nnewSuperior, op->o_tmpmemctx );
248 if( rs->sr_err != LDAP_SUCCESS ) {
250 LDAP_LOG( OPERATION, INFO,
251 "do_modrdn: conn %d invalid newSuperior (%s)\n",
252 op->o_connid, newSuperior.bv_val, 0 );
254 Debug( LDAP_DEBUG_ANY,
255 "do_modrdn: invalid newSuperior (%s)\n",
256 newSuperior.bv_val, 0, 0 );
258 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
263 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
264 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
266 manageDSAit = get_manageDSAit( op );
269 * We could be serving multiple database backends. Select the
270 * appropriate one, or send a referral to our "referral server"
271 * if we don't hold it.
273 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
274 rs->sr_ref = referral_rewrite( default_referral,
275 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
276 if (!rs->sr_ref) rs->sr_ref = default_referral;
278 if ( rs->sr_ref != NULL ) {
279 rs->sr_err = LDAP_REFERRAL;
280 send_ldap_result( op, rs );
282 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
284 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
285 "referral missing" );
290 /* check restrictions */
291 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
292 send_ldap_result( op, rs );
296 /* check for referrals */
297 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
301 /* Make sure that the entry being changed and the newSuperior are in
302 * the same backend, otherwise we return an error.
304 if( op->orr_newSup ) {
305 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
307 if ( newSuperior_be != op->o_bd ) {
308 /* newSuperior is in different backend */
309 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
310 "cannot rename between DSAs" );
316 #if defined( LDAP_SLAPI )
319 slapi_int_pblock_set_operation( pb, op );
320 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
321 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
322 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
323 (void *)newSuperior.bv_val );
324 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
325 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
327 rs->sr_err = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
328 if ( rs->sr_err < 0 ) {
330 * A preoperation plugin failure will abort the
334 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
335 "failed\n", 0, 0, 0 );
337 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
338 "failed.\n", 0, 0, 0);
340 if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
341 rs->sr_err == LDAP_SUCCESS ) {
342 rs->sr_err = LDAP_OTHER;
347 #endif /* defined( LDAP_SLAPI ) */
350 * do the modrdn if 1 && (2 || 3)
351 * 1) there is a modrdn function implemented in this backend;
352 * 2) this backend is master for what it holds;
353 * 3) it's a replica and the dn supplied is the update_ndn.
355 if ( op->o_bd->be_modrdn ) {
356 /* do the update here */
357 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
358 #ifndef SLAPD_MULTIMASTER
359 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ) &&
360 ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
362 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ))
365 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
366 op->orr_deleteoldrdn = deloldrdn;
367 #ifdef SLAPD_MULTIMASTER
368 if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
371 cb.sc_next = op->o_callback;
372 op->o_callback = &cb;
374 op->o_bd->be_modrdn( op, rs );
376 #ifndef SLAPD_MULTIMASTER
378 BerVarray defref = NULL;
379 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
381 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
383 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
384 ber_bvarray_add( &defref, &tmpbv );
387 defref = op->o_bd->be_update_refs
388 ? op->o_bd->be_update_refs : default_referral;
390 if ( defref != NULL ) {
391 rs->sr_ref = referral_rewrite( defref,
392 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
393 if (!rs->sr_ref) rs->sr_ref = defref;
395 rs->sr_err = LDAP_REFERRAL;
396 send_ldap_result( op, rs );
398 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
400 send_ldap_error( op, rs,
401 LDAP_UNWILLING_TO_PERFORM,
402 "referral missing" );
407 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
408 "operation not supported within namingContext" );
411 #if defined( LDAP_SLAPI )
412 if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
414 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
415 "failed\n", 0, 0, 0 );
417 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
418 "failed.\n", 0, 0, 0);
421 #endif /* defined( LDAP_SLAPI ) */
425 slap_graduate_commit_csn( op );
427 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
428 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
430 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
431 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
433 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
434 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
446 Modifications **pmod )
448 Modifications *mod = NULL;
449 Modifications **modtail = &mod;
453 assert( new_rdn != NULL );
454 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
456 repl_user = be_isupdate( op->o_bd, &op->o_ndn );
458 /* Add new attribute values to the entry */
459 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
460 AttributeDescription *desc = NULL;
461 Modifications *mod_tmp;
463 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
465 if ( rs->sr_err != LDAP_SUCCESS ) {
467 LDAP_LOG ( OPERATION, ERR,
468 "slap_modrdn2modlist: %s: %s (new)\n",
470 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
472 Debug( LDAP_DEBUG_TRACE,
473 "slap_modrdn2modlist: %s: %s (new)\n",
475 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
480 /* ACL check of newly added attrs */
481 if ( op->o_bd && !access_allowed( op, e, desc,
482 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
484 LDAP_LOG ( OPERATION, ERR,
485 "slap_modrdn2modlist: access to attr \"%s\" "
486 "(new) not allowed\n",
487 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
489 Debug( LDAP_DEBUG_TRACE,
490 "slap_modrdn2modlist: access to attr \"%s\" "
491 "(new) not allowed\n",
492 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
494 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
498 /* Apply modification */
499 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
500 + 4 * sizeof( struct berval ) );
501 mod_tmp->sml_desc = desc;
502 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
503 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
504 mod_tmp->sml_values[1].bv_val = NULL;
505 if( desc->ad_type->sat_equality->smr_normalize) {
506 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
507 (void) (*desc->ad_type->sat_equality->smr_normalize)(
508 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
509 desc->ad_type->sat_syntax,
510 desc->ad_type->sat_equality,
511 &mod_tmp->sml_values[0],
512 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
513 mod_tmp->sml_nvalues[1].bv_val = NULL;
515 mod_tmp->sml_nvalues = NULL;
517 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
518 mod_tmp->sml_next = mod;
522 /* Remove old rdn value if required */
523 if ( op->orr_deleteoldrdn ) {
524 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
525 AttributeDescription *desc = NULL;
526 Modifications *mod_tmp;
528 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
529 if ( rs->sr_err != LDAP_SUCCESS ) {
531 LDAP_LOG ( OPERATION, ERR,
532 "slap_modrdn2modlist: %s: %s (old)\n",
534 old_rdn[d_cnt]->la_attr.bv_val,
537 Debug( LDAP_DEBUG_TRACE,
538 "slap_modrdn2modlist: %s: %s (old)\n",
540 old_rdn[d_cnt]->la_attr.bv_val,
546 /* ACL check of newly added attrs */
547 if ( op->o_bd && !access_allowed( op, e, desc,
548 &old_rdn[d_cnt]->la_value, ACL_WRITE,
551 LDAP_LOG ( OPERATION, ERR,
552 "slap_modrdn2modlist: access "
553 "to attr \"%s\" (old) not allowed\n",
554 old_rdn[ d_cnt ]->la_attr.bv_val,
557 Debug( LDAP_DEBUG_TRACE,
558 "slap_modrdn2modlist: access "
559 "to attr \"%s\" (old) not allowed\n",
560 old_rdn[ d_cnt ]->la_attr.bv_val,
563 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
567 /* Apply modification */
568 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
569 + 4 * sizeof ( struct berval ) );
570 mod_tmp->sml_desc = desc;
571 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
572 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
573 mod_tmp->sml_values[1].bv_val = NULL;
574 if( desc->ad_type->sat_equality->smr_normalize) {
575 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
576 (void) (*desc->ad_type->sat_equality->smr_normalize)(
577 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
578 desc->ad_type->sat_syntax,
579 desc->ad_type->sat_equality,
580 &mod_tmp->sml_values[0],
581 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
582 mod_tmp->sml_nvalues[1].bv_val = NULL;
584 mod_tmp->sml_nvalues = NULL;
586 mod_tmp->sml_op = LDAP_MOD_DELETE;
587 mod_tmp->sml_next = mod;
595 char textbuf[ SLAP_TEXT_BUFLEN ];
596 size_t textlen = sizeof textbuf;
600 modtail = &(*modtail)->sml_next )
605 rs->sr_err = slap_mods_opattrs( op, mod, modtail, &rs->sr_text, textbuf, textlen );
608 /* LDAP v2 supporting correct attribute handling. */
609 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
611 for ( ; mod; mod = tmp ) {