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 )
370 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
371 op->orr_deleteoldrdn = deloldrdn;
372 #ifdef SLAPD_MULTIMASTER
373 if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
376 cb.sc_next = op->o_callback;
377 op->o_callback = &cb;
379 op->o_bd->be_modrdn( op, rs );
381 if ( op->o_bd->be_delete ) {
382 org_req_dn = op->o_req_dn;
383 org_req_ndn = op->o_req_ndn;
386 org_managedsait = get_manageDSAit( op );
387 op->o_dn = op->o_bd->be_rootdn;
388 op->o_ndn = op->o_bd->be_rootndn;
389 op->o_managedsait = 1;
391 while ( rs->sr_err == LDAP_SUCCESS &&
392 op->o_delete_glue_parent ) {
393 op->o_delete_glue_parent = 0;
394 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
395 slap_callback cb = { NULL };
396 cb.sc_response = slap_null_cb;
397 dnParent( &op->o_req_ndn, &pdn );
400 op->o_callback = &cb;
401 op->o_bd->be_delete( op, rs );
406 op->o_managedsait = org_managedsait;
409 op->o_req_dn = org_req_dn;
410 op->o_req_ndn = org_req_ndn;
411 op->o_delete_glue_parent = 0;
414 #ifndef SLAPD_MULTIMASTER
416 BerVarray defref = op->o_bd->be_update_refs
417 ? op->o_bd->be_update_refs : default_referral;
419 if ( defref != NULL ) {
420 rs->sr_ref = referral_rewrite( defref,
421 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
422 if (!rs->sr_ref) rs->sr_ref = defref;
424 rs->sr_err = LDAP_REFERRAL;
425 send_ldap_result( op, rs );
427 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
429 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
430 "shadow context; no update referral" );
435 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
436 "operation not supported within namingContext" );
439 #if defined( LDAP_SLAPI )
440 if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
442 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
443 "failed\n", 0, 0, 0 );
445 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
446 "failed.\n", 0, 0, 0);
449 #endif /* defined( LDAP_SLAPI ) */
453 slap_graduate_commit_csn( op );
455 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
456 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
458 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
459 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
461 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
462 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
474 Modifications **pmod )
476 Modifications *mod = NULL;
477 Modifications **modtail = &mod;
481 assert( new_rdn != NULL );
482 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
484 repl_user = be_isupdate( op );
486 /* Add new attribute values to the entry */
487 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
488 AttributeDescription *desc = NULL;
489 Modifications *mod_tmp;
491 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
493 if ( rs->sr_err != LDAP_SUCCESS ) {
495 LDAP_LOG ( OPERATION, ERR,
496 "slap_modrdn2modlist: %s: %s (new)\n",
498 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
500 Debug( LDAP_DEBUG_TRACE,
501 "slap_modrdn2modlist: %s: %s (new)\n",
503 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
508 /* ACL check of newly added attrs */
509 if ( op->o_bd && !access_allowed( op, e, desc,
510 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
512 LDAP_LOG ( OPERATION, ERR,
513 "slap_modrdn2modlist: access to attr \"%s\" "
514 "(new) not allowed\n",
515 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
517 Debug( LDAP_DEBUG_TRACE,
518 "slap_modrdn2modlist: access to attr \"%s\" "
519 "(new) not allowed\n",
520 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
522 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
526 /* Apply modification */
527 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
528 + 4 * sizeof( struct berval ) );
529 mod_tmp->sml_desc = desc;
530 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
531 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
532 mod_tmp->sml_values[1].bv_val = NULL;
533 if( desc->ad_type->sat_equality->smr_normalize) {
534 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
535 (void) (*desc->ad_type->sat_equality->smr_normalize)(
536 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
537 desc->ad_type->sat_syntax,
538 desc->ad_type->sat_equality,
539 &mod_tmp->sml_values[0],
540 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
541 mod_tmp->sml_nvalues[1].bv_val = NULL;
543 mod_tmp->sml_nvalues = NULL;
545 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
546 mod_tmp->sml_next = mod;
550 /* Remove old rdn value if required */
551 if ( op->orr_deleteoldrdn ) {
552 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
553 AttributeDescription *desc = NULL;
554 Modifications *mod_tmp;
556 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
557 if ( rs->sr_err != LDAP_SUCCESS ) {
559 LDAP_LOG ( OPERATION, ERR,
560 "slap_modrdn2modlist: %s: %s (old)\n",
562 old_rdn[d_cnt]->la_attr.bv_val,
565 Debug( LDAP_DEBUG_TRACE,
566 "slap_modrdn2modlist: %s: %s (old)\n",
568 old_rdn[d_cnt]->la_attr.bv_val,
574 /* ACL check of newly added attrs */
575 if ( op->o_bd && !access_allowed( op, e, desc,
576 &old_rdn[d_cnt]->la_value, ACL_WRITE,
579 LDAP_LOG ( OPERATION, ERR,
580 "slap_modrdn2modlist: access "
581 "to attr \"%s\" (old) not allowed\n",
582 old_rdn[ d_cnt ]->la_attr.bv_val,
585 Debug( LDAP_DEBUG_TRACE,
586 "slap_modrdn2modlist: access "
587 "to attr \"%s\" (old) not allowed\n",
588 old_rdn[ d_cnt ]->la_attr.bv_val,
591 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
595 /* Apply modification */
596 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
597 + 4 * sizeof ( struct berval ) );
598 mod_tmp->sml_desc = desc;
599 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
600 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
601 mod_tmp->sml_values[1].bv_val = NULL;
602 if( desc->ad_type->sat_equality->smr_normalize) {
603 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
604 (void) (*desc->ad_type->sat_equality->smr_normalize)(
605 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
606 desc->ad_type->sat_syntax,
607 desc->ad_type->sat_equality,
608 &mod_tmp->sml_values[0],
609 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
610 mod_tmp->sml_nvalues[1].bv_val = NULL;
612 mod_tmp->sml_nvalues = NULL;
614 mod_tmp->sml_op = LDAP_MOD_DELETE;
615 mod_tmp->sml_next = mod;
623 char textbuf[ SLAP_TEXT_BUFLEN ];
624 size_t textlen = sizeof textbuf;
628 modtail = &(*modtail)->sml_next )
633 rs->sr_err = slap_mods_opattrs( op, mod, modtail, &rs->sr_text, textbuf, textlen );
636 /* LDAP v2 supporting correct attribute handling. */
637 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
639 for ( ; mod; mod = tmp ) {