3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
19 * LDAP v3 newSuperior support.
21 * Copyright 1999, Juan C. Gomez, All rights reserved.
22 * This software is not subject to any license of Silicon Graphics
23 * Inc. or Purdue University.
25 * Redistribution and use in source and binary forms are permitted
26 * without restriction or fee of any kind as long as this notice
35 #include <ac/socket.h>
36 #include <ac/string.h>
50 struct berval dn = { 0, NULL };
51 struct berval newrdn = { 0, NULL };
52 struct berval newSuperior = { 0, NULL };
55 struct berval pnewSuperior = { 0, NULL };
57 struct berval nnewSuperior = { 0, NULL };
59 Backend *newSuperior_be = NULL;
64 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
66 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
71 * Parse the modrdn request. It looks like this:
73 * ModifyRDNRequest := SEQUENCE {
74 * entry DistinguishedName,
75 * newrdn RelativeDistinguishedName
76 * deleteoldrdn BOOLEAN,
77 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
81 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
85 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
87 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
90 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
91 return SLAPD_DISCONNECT;
94 /* Check for newSuperior parameter, if present scan it */
96 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
97 if ( op->o_protocol < LDAP_VERSION3 ) {
98 /* Conection record indicates v2 but field
99 * newSuperior is present: report error.
102 LDAP_LOG( OPERATION, ERR,
103 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
105 Debug( LDAP_DEBUG_ANY,
106 "modrdn(v2): invalid field newSuperior!\n",
110 send_ldap_discon( op, rs,
111 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
112 rs->sr_err = SLAPD_DISCONNECT;
116 if ( ber_scanf( op->o_ber, "m", &newSuperior )
120 LDAP_LOG( OPERATION, ERR,
121 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
123 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
127 send_ldap_discon( op, rs,
128 LDAP_PROTOCOL_ERROR, "decoding error" );
129 rs->sr_err = SLAPD_DISCONNECT;
132 op->orr_newSup = &pnewSuperior;
133 op->orr_nnewSup = &nnewSuperior;
137 LDAP_LOG( OPERATION, ARGS,
138 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
139 dn.bv_val, newrdn.bv_val,
140 newSuperior.bv_len ? newSuperior.bv_val : "" );
142 Debug( LDAP_DEBUG_ARGS,
143 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
144 dn.bv_val, newrdn.bv_val,
145 newSuperior.bv_len ? newSuperior.bv_val : "" );
148 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
150 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
152 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
155 send_ldap_discon( op, rs,
156 LDAP_PROTOCOL_ERROR, "decoding error" );
157 rs->sr_err = SLAPD_DISCONNECT;
161 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
163 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
165 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
168 /* get_ctrls has sent results. Now clean up. */
172 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
173 if( rs->sr_err != LDAP_SUCCESS ) {
175 LDAP_LOG( OPERATION, INFO,
176 "do_modrdn: conn %d invalid dn (%s)\n",
177 op->o_connid, dn.bv_val, 0 );
179 Debug( LDAP_DEBUG_ANY,
180 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
182 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
186 if( op->o_req_ndn.bv_len == 0 ) {
188 LDAP_LOG( OPERATION, ERR,
189 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
191 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
194 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
195 "cannot rename the root DSE" );
198 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
200 LDAP_LOG( OPERATION, ERR,
201 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
202 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
204 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
205 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
208 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
209 "cannot rename subschema subentry" );
213 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
215 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
216 if( rs->sr_err != LDAP_SUCCESS ) {
218 LDAP_LOG( OPERATION, INFO,
219 "do_modrdn: conn %d invalid newrdn (%s)\n",
220 op->o_connid, newrdn.bv_val, 0 );
222 Debug( LDAP_DEBUG_ANY,
223 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
225 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
229 if( rdnValidate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
231 LDAP_LOG( OPERATION, ERR,
232 "do_modrdn: invalid rdn (%s).\n", op->orr_newrdn.bv_val, 0, 0 );
234 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
235 op->orr_newrdn.bv_val, 0, 0 );
238 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
242 if( op->orr_newSup ) {
243 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
244 &nnewSuperior, op->o_tmpmemctx );
245 if( rs->sr_err != LDAP_SUCCESS ) {
247 LDAP_LOG( OPERATION, INFO,
248 "do_modrdn: conn %d invalid newSuperior (%s)\n",
249 op->o_connid, newSuperior.bv_val, 0 );
251 Debug( LDAP_DEBUG_ANY,
252 "do_modrdn: invalid newSuperior (%s)\n",
253 newSuperior.bv_val, 0, 0 );
255 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
260 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
261 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
263 manageDSAit = get_manageDSAit( op );
266 * We could be serving multiple database backends. Select the
267 * appropriate one, or send a referral to our "referral server"
268 * if we don't hold it.
270 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
271 rs->sr_ref = referral_rewrite( default_referral,
272 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
273 if (!rs->sr_ref) rs->sr_ref = default_referral;
275 if ( rs->sr_ref != NULL ) {
276 rs->sr_err = LDAP_REFERRAL;
277 send_ldap_result( op, rs );
279 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
281 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
282 "referral missing" );
287 /* check restrictions */
288 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
289 send_ldap_result( op, rs );
293 /* check for referrals */
294 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
298 /* Make sure that the entry being changed and the newSuperior are in
299 * the same backend, otherwise we return an error.
301 if( op->orr_newSup ) {
302 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
304 if ( newSuperior_be != op->o_bd ) {
305 /* newSuperior is in different backend */
306 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
307 "cannot rename between DSAs" );
313 #if defined( LDAP_SLAPI )
316 slapi_x_pblock_set_operation( pb, op );
317 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
318 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
319 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
320 (void *)newSuperior.bv_val );
321 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
322 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
324 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
325 if ( rs->sr_err < 0 ) {
327 * A preoperation plugin failure will abort the
331 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
332 "failed\n", 0, 0, 0 );
334 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
335 "failed.\n", 0, 0, 0);
337 if ( ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0 ) ||
338 rs->sr_err == LDAP_SUCCESS ) {
339 rs->sr_err = LDAP_OTHER;
344 #endif /* defined( LDAP_SLAPI ) */
347 * do the modrdn if 1 && (2 || 3)
348 * 1) there is a modrdn function implemented in this backend;
349 * 2) this backend is master for what it holds;
350 * 3) it's a replica and the dn supplied is the update_ndn.
352 if ( op->o_bd->be_modrdn ) {
353 /* do the update here */
354 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
355 #ifndef SLAPD_MULTIMASTER
356 if ( !op->o_bd->be_syncinfo &&
357 ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
359 if ( !op->o_bd->be_syncinfo )
362 op->orr_deleteoldrdn = deloldrdn;
363 if ( (op->o_bd->be_modrdn)( op, rs ) == 0
364 #ifdef SLAPD_MULTIMASTER
365 && ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
370 #ifndef SLAPD_MULTIMASTER
372 BerVarray defref = NULL;
373 if ( op->o_bd->be_syncinfo ) {
374 defref = op->o_bd->be_syncinfo->si_provideruri_bv;
376 defref = op->o_bd->be_update_refs
377 ? op->o_bd->be_update_refs : default_referral;
379 if ( defref != NULL ) {
380 rs->sr_ref = referral_rewrite( defref,
381 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
382 if (!rs->sr_ref) rs->sr_ref = defref;
384 rs->sr_err = LDAP_REFERRAL;
385 send_ldap_result( op, rs );
387 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
389 send_ldap_error( op, rs,
390 LDAP_UNWILLING_TO_PERFORM,
391 "referral missing" );
396 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
397 "operation not supported within namingContext" );
400 #if defined( LDAP_SLAPI )
401 if ( pb && doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
403 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
404 "failed\n", 0, 0, 0 );
406 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
407 "failed.\n", 0, 0, 0);
410 #endif /* defined( LDAP_SLAPI ) */
414 slap_graduate_commit_csn( op );
416 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
417 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
419 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
420 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
422 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
423 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
435 Modifications **pmod )
437 Modifications *mod = NULL;
438 Modifications **modtail = &mod;
442 assert( new_rdn != NULL );
443 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
445 repl_user = be_isupdate( op->o_bd, &op->o_ndn );
447 /* Add new attribute values to the entry */
448 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
449 AttributeDescription *desc = NULL;
450 Modifications *mod_tmp;
452 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
454 if ( rs->sr_err != LDAP_SUCCESS ) {
456 LDAP_LOG ( OPERATION, ERR,
457 "slap_modrdn2modlist: %s: %s (new)\n",
459 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
461 Debug( LDAP_DEBUG_TRACE,
462 "slap_modrdn2modlist: %s: %s (new)\n",
464 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
469 /* ACL check of newly added attrs */
470 if ( op->o_bd && !access_allowed( op, e, desc,
471 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
473 LDAP_LOG ( OPERATION, ERR,
474 "slap_modrdn2modlist: access to attr \"%s\" "
475 "(new) not allowed\n",
476 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
478 Debug( LDAP_DEBUG_TRACE,
479 "slap_modrdn2modlist: access to attr \"%s\" "
480 "(new) not allowed\n",
481 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
483 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
487 /* Apply modification */
488 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
489 + 4 * sizeof( struct berval ) );
490 mod_tmp->sml_desc = desc;
491 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
492 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
493 mod_tmp->sml_values[1].bv_val = NULL;
494 if( desc->ad_type->sat_equality->smr_normalize) {
495 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
496 (void) (*desc->ad_type->sat_equality->smr_normalize)(
497 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
498 desc->ad_type->sat_syntax,
499 desc->ad_type->sat_equality,
500 &mod_tmp->sml_values[0],
501 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
502 mod_tmp->sml_nvalues[1].bv_val = NULL;
504 mod_tmp->sml_nvalues = NULL;
506 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
507 mod_tmp->sml_next = mod;
511 /* Remove old rdn value if required */
512 if ( op->orr_deleteoldrdn ) {
513 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
514 AttributeDescription *desc = NULL;
515 Modifications *mod_tmp;
517 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
518 if ( rs->sr_err != LDAP_SUCCESS ) {
520 LDAP_LOG ( OPERATION, ERR,
521 "slap_modrdn2modlist: %s: %s (old)\n",
523 old_rdn[d_cnt]->la_attr.bv_val,
526 Debug( LDAP_DEBUG_TRACE,
527 "slap_modrdn2modlist: %s: %s (old)\n",
529 old_rdn[d_cnt]->la_attr.bv_val,
535 /* ACL check of newly added attrs */
536 if ( op->o_bd && !access_allowed( op, e, desc,
537 &old_rdn[d_cnt]->la_value, ACL_WRITE,
540 LDAP_LOG ( OPERATION, ERR,
541 "slap_modrdn2modlist: access "
542 "to attr \"%s\" (old) not allowed\n",
543 old_rdn[ d_cnt ]->la_attr.bv_val,
546 Debug( LDAP_DEBUG_TRACE,
547 "slap_modrdn2modlist: access "
548 "to attr \"%s\" (old) not allowed\n",
549 old_rdn[ d_cnt ]->la_attr.bv_val,
552 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
556 /* Apply modification */
557 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
558 + 4 * sizeof ( struct berval ) );
559 mod_tmp->sml_desc = desc;
560 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
561 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
562 mod_tmp->sml_values[1].bv_val = NULL;
563 if( desc->ad_type->sat_equality->smr_normalize) {
564 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
565 (void) (*desc->ad_type->sat_equality->smr_normalize)(
566 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
567 desc->ad_type->sat_syntax,
568 desc->ad_type->sat_equality,
569 &mod_tmp->sml_values[0],
570 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
571 mod_tmp->sml_nvalues[1].bv_val = NULL;
573 mod_tmp->sml_nvalues = NULL;
575 mod_tmp->sml_op = LDAP_MOD_DELETE;
576 mod_tmp->sml_next = mod;
584 char textbuf[ SLAP_TEXT_BUFLEN ];
585 size_t textlen = sizeof textbuf;
589 modtail = &(*modtail)->sml_next )
594 rs->sr_err = slap_mods_opattrs( op, mod, modtail, &rs->sr_text, textbuf, textlen );
597 /* LDAP v2 supporting correct attribute handling. */
598 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
600 for ( ; mod; mod = tmp ) {