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 Slapi_PBlock *pb = op->o_pb;
68 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
70 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
75 * Parse the modrdn request. It looks like this:
77 * ModifyRDNRequest := SEQUENCE {
78 * entry DistinguishedName,
79 * newrdn RelativeDistinguishedName
80 * deleteoldrdn BOOLEAN,
81 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
85 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
89 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
94 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
95 return SLAPD_DISCONNECT;
98 /* Check for newSuperior parameter, if present scan it */
100 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
101 if ( op->o_protocol < LDAP_VERSION3 ) {
102 /* Conection record indicates v2 but field
103 * newSuperior is present: report error.
106 LDAP_LOG( OPERATION, ERR,
107 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
109 Debug( LDAP_DEBUG_ANY,
110 "modrdn(v2): invalid field newSuperior!\n",
114 send_ldap_discon( op, rs,
115 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
116 rs->sr_err = SLAPD_DISCONNECT;
120 if ( ber_scanf( op->o_ber, "m", &newSuperior )
124 LDAP_LOG( OPERATION, ERR,
125 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
127 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
131 send_ldap_discon( op, rs,
132 LDAP_PROTOCOL_ERROR, "decoding error" );
133 rs->sr_err = SLAPD_DISCONNECT;
136 op->orr_newSup = &pnewSuperior;
137 op->orr_nnewSup = &nnewSuperior;
141 LDAP_LOG( OPERATION, ARGS,
142 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
143 dn.bv_val, newrdn.bv_val,
144 newSuperior.bv_len ? newSuperior.bv_val : "" );
146 Debug( LDAP_DEBUG_ARGS,
147 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
148 dn.bv_val, newrdn.bv_val,
149 newSuperior.bv_len ? newSuperior.bv_val : "" );
152 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
154 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
156 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
159 send_ldap_discon( op, rs,
160 LDAP_PROTOCOL_ERROR, "decoding error" );
161 rs->sr_err = SLAPD_DISCONNECT;
165 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
167 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
169 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
172 /* get_ctrls has sent results. Now clean up. */
176 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
177 if( rs->sr_err != LDAP_SUCCESS ) {
179 LDAP_LOG( OPERATION, INFO,
180 "do_modrdn: conn %d invalid dn (%s)\n",
181 op->o_connid, dn.bv_val, 0 );
183 Debug( LDAP_DEBUG_ANY,
184 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
186 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
190 if( op->o_req_ndn.bv_len == 0 ) {
192 LDAP_LOG( OPERATION, ERR,
193 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
195 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
198 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
199 "cannot rename the root DSE" );
202 } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
204 LDAP_LOG( OPERATION, ERR,
205 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
206 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
208 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
209 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
212 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
213 "cannot rename subschema subentry" );
217 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
219 rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
220 if( rs->sr_err != LDAP_SUCCESS ) {
222 LDAP_LOG( OPERATION, INFO,
223 "do_modrdn: conn %d invalid newrdn (%s)\n",
224 op->o_connid, newrdn.bv_val, 0 );
226 Debug( LDAP_DEBUG_ANY,
227 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
229 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
233 if( rdnValidate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
235 LDAP_LOG( OPERATION, ERR,
236 "do_modrdn: invalid rdn (%s).\n", op->orr_newrdn.bv_val, 0, 0 );
238 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
239 op->orr_newrdn.bv_val, 0, 0 );
242 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
246 if( op->orr_newSup ) {
247 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
248 &nnewSuperior, op->o_tmpmemctx );
249 if( rs->sr_err != LDAP_SUCCESS ) {
251 LDAP_LOG( OPERATION, INFO,
252 "do_modrdn: conn %d invalid newSuperior (%s)\n",
253 op->o_connid, newSuperior.bv_val, 0 );
255 Debug( LDAP_DEBUG_ANY,
256 "do_modrdn: invalid newSuperior (%s)\n",
257 newSuperior.bv_val, 0, 0 );
259 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
264 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
265 op->o_connid, op->o_opid, op->o_req_dn.bv_val, 0, 0 );
267 manageDSAit = get_manageDSAit( op );
270 * We could be serving multiple database backends. Select the
271 * appropriate one, or send a referral to our "referral server"
272 * if we don't hold it.
274 if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
275 rs->sr_ref = referral_rewrite( default_referral,
276 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
277 if (!rs->sr_ref) rs->sr_ref = default_referral;
279 if ( rs->sr_ref != NULL ) {
280 rs->sr_err = LDAP_REFERRAL;
281 send_ldap_result( op, rs );
283 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
285 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
286 "referral missing" );
291 /* check restrictions */
292 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
293 send_ldap_result( op, rs );
297 /* check for referrals */
298 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
302 /* Make sure that the entry being changed and the newSuperior are in
303 * the same backend, otherwise we return an error.
305 if( op->orr_newSup ) {
306 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
308 if ( newSuperior_be != op->o_bd ) {
309 /* newSuperior is in different backend */
310 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
311 "cannot rename between DSAs" );
317 #if defined( LDAP_SLAPI )
318 slapi_x_pblock_set_operation( pb, op );
319 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
320 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
321 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
322 (void *)newSuperior.bv_val );
323 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
324 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
326 rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
327 if ( rs->sr_err < 0 ) {
329 * A preoperation plugin failure will abort the
333 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
334 "failed\n", 0, 0, 0 );
336 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
337 "failed.\n", 0, 0, 0);
339 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0)
340 rs->sr_err = LDAP_OTHER;
343 #endif /* defined( LDAP_SLAPI ) */
346 * do the modrdn if 1 && (2 || 3)
347 * 1) there is a modrdn function implemented in this backend;
348 * 2) this backend is master for what it holds;
349 * 3) it's a replica and the dn supplied is the update_ndn.
351 if ( op->o_bd->be_modrdn ) {
352 /* do the update here */
353 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
354 #ifndef SLAPD_MULTIMASTER
355 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
358 op->orr_deleteoldrdn = deloldrdn;
359 if ( (op->o_bd->be_modrdn)( op, rs ) == 0
360 #ifdef SLAPD_MULTIMASTER
361 && ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
366 #ifndef SLAPD_MULTIMASTER
368 BerVarray defref = op->o_bd->be_update_refs
369 ? op->o_bd->be_update_refs : default_referral;
370 if ( defref != NULL ) {
371 rs->sr_ref = referral_rewrite( defref,
372 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
373 if (!rs->sr_ref) rs->sr_ref = defref;
375 rs->sr_err = LDAP_REFERRAL;
376 send_ldap_result( op, rs );
378 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
380 send_ldap_error( op, rs,
381 LDAP_UNWILLING_TO_PERFORM,
382 "referral missing" );
387 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
388 "operation not supported within namingContext" );
391 #if defined( LDAP_SLAPI )
392 if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) < 0 ) {
394 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
395 "failed\n", 0, 0, 0 );
397 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
398 "failed.\n", 0, 0, 0);
401 #endif /* defined( LDAP_SLAPI ) */
404 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
405 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
407 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
408 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
410 if ( pnewSuperior.bv_val ) op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
411 if ( nnewSuperior.bv_val ) op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
423 Modifications **pmod )
425 Modifications *mod = NULL;
428 assert( new_rdn != NULL );
429 assert( !op->orr_deleteoldrdn || old_rdn != NULL );
431 /* Add new attribute values to the entry */
432 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
433 AttributeDescription *desc = NULL;
434 Modifications *mod_tmp;
436 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
438 if ( rs->sr_err != LDAP_SUCCESS ) {
440 LDAP_LOG ( OPERATION, ERR,
441 "slap_modrdn2modlist: %s: %s (new)\n",
443 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
445 Debug( LDAP_DEBUG_TRACE,
446 "slap_modrdn2modlist: %s: %s (new)\n",
448 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
453 /* ACL check of newly added attrs */
454 if ( op->o_bd && !access_allowed( op, e, desc,
455 &new_rdn[a_cnt]->la_value, ACL_WRITE, NULL ) ) {
457 LDAP_LOG ( OPERATION, ERR,
458 "slap_modrdn2modlist: access to attr \"%s\" "
459 "(new) not allowed\n",
460 new_rdn[a_cnt]->la_attr.bv_val, 0, 0 );
462 Debug( LDAP_DEBUG_TRACE,
463 "slap_modrdn2modlist: access to attr \"%s\" "
464 "(new) not allowed\n",
465 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
467 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
471 /* Apply modification */
472 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
473 + 4 * sizeof( struct berval ) );
474 mod_tmp->sml_desc = desc;
475 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
476 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
477 mod_tmp->sml_values[1].bv_val = NULL;
478 if( desc->ad_type->sat_equality->smr_normalize) {
479 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
480 (void) (*desc->ad_type->sat_equality->smr_normalize)(
482 desc->ad_type->sat_syntax,
483 desc->ad_type->sat_equality,
484 &mod_tmp->sml_values[0],
485 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
486 mod_tmp->sml_nvalues[1].bv_val = NULL;
488 mod_tmp->sml_nvalues = NULL;
490 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
491 mod_tmp->sml_next = mod;
495 /* Remove old rdn value if required */
496 if ( op->orr_deleteoldrdn ) {
497 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
498 AttributeDescription *desc = NULL;
499 Modifications *mod_tmp;
501 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
502 if ( rs->sr_err != LDAP_SUCCESS ) {
504 LDAP_LOG ( OPERATION, ERR,
505 "slap_modrdn2modlist: %s: %s (old)\n",
507 old_rdn[d_cnt]->la_attr.bv_val,
510 Debug( LDAP_DEBUG_TRACE,
511 "slap_modrdn2modlist: %s: %s (old)\n",
513 old_rdn[d_cnt]->la_attr.bv_val,
519 /* ACL check of newly added attrs */
520 if ( op->o_bd && !access_allowed( op, e, desc,
521 &old_rdn[d_cnt]->la_value, ACL_WRITE,
524 LDAP_LOG ( OPERATION, ERR,
525 "slap_modrdn2modlist: access "
526 "to attr \"%s\" (old) not allowed\n",
527 old_rdn[ d_cnt ]->la_attr.bv_val,
530 Debug( LDAP_DEBUG_TRACE,
531 "slap_modrdn2modlist: access "
532 "to attr \"%s\" (old) not allowed\n",
533 old_rdn[ d_cnt ]->la_attr.bv_val,
536 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
540 /* Apply modification */
541 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
542 + 4 * sizeof ( struct berval ) );
543 mod_tmp->sml_desc = desc;
544 mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
545 mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
546 mod_tmp->sml_values[1].bv_val = NULL;
547 if( desc->ad_type->sat_equality->smr_normalize) {
548 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
549 (void) (*desc->ad_type->sat_equality->smr_normalize)(
551 desc->ad_type->sat_syntax,
552 desc->ad_type->sat_equality,
553 &mod_tmp->sml_values[0],
554 &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
555 mod_tmp->sml_nvalues[1].bv_val = NULL;
557 mod_tmp->sml_nvalues = NULL;
559 mod_tmp->sml_op = LDAP_MOD_DELETE;
560 mod_tmp->sml_next = mod;
566 /* LDAP v2 supporting correct attribute handling. */
567 if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
569 for ( ; mod; mod = tmp ) {