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>
48 struct berval dn = { 0, NULL };
49 struct berval newrdn = { 0, NULL };
50 struct berval newSuperior = { 0, NULL };
53 struct berval pdn = { 0, NULL };
54 struct berval pnewrdn = { 0, NULL };
55 struct berval pnewSuperior = { 0, NULL }, *pnewS = NULL;
57 struct berval ndn = { 0, NULL };
58 struct berval nnewrdn = { 0, NULL };
59 struct berval nnewSuperior = { 0, NULL }, *nnewS = NULL;
62 Backend *newSuperior_be = NULL;
69 Slapi_PBlock *pb = op->o_pb;
73 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
75 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
80 * Parse the modrdn request. It looks like this:
82 * ModifyRDNRequest := SEQUENCE {
83 * entry DistinguishedName,
84 * newrdn RelativeDistinguishedName
85 * deleteoldrdn BOOLEAN,
86 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
90 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
94 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
99 send_ldap_disconnect( conn, op,
100 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_disconnect( conn, op,
121 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
122 rc = 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_disconnect( conn, op,
138 LDAP_PROTOCOL_ERROR, "decoding error" );
139 rc = SLAPD_DISCONNECT;
142 pnewS = &pnewSuperior;
143 nnewS = &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_disconnect( conn, op,
166 LDAP_PROTOCOL_ERROR, "decoding error" );
167 rc = SLAPD_DISCONNECT;
171 if( (rc = get_ctrls( conn, op, 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 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
183 if( rc != LDAP_SUCCESS ) {
185 LDAP_LOG( OPERATION, INFO,
186 "do_modrdn: conn %d invalid dn (%s)\n",
187 conn->c_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_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
193 "invalid DN", NULL, NULL );
197 if( ndn.bv_len == 0 ) {
199 LDAP_LOG( OPERATION, ERR,
200 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
202 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
205 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
206 NULL, "cannot rename the root DSE", NULL, NULL );
209 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
211 LDAP_LOG( OPERATION, ERR,
212 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
213 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
215 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
216 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
219 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
220 NULL, "cannot rename subschema subentry", NULL, NULL );
224 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
226 rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
227 if( rc != LDAP_SUCCESS ) {
229 LDAP_LOG( OPERATION, INFO,
230 "do_modrdn: conn %d invalid newrdn (%s)\n",
231 conn->c_connid, newrdn.bv_val, 0 );
233 Debug( LDAP_DEBUG_ANY,
234 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
236 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
237 "invalid new RDN", NULL, NULL );
241 if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
243 LDAP_LOG( OPERATION, ERR,
244 "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val, 0, 0 );
246 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
247 pnewrdn.bv_val, 0, 0 );
250 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
251 "invalid new RDN", NULL, NULL );
256 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
258 if( rc != LDAP_SUCCESS ) {
260 LDAP_LOG( OPERATION, INFO,
261 "do_modrdn: conn %d invalid newSuperior (%s)\n",
262 conn->c_connid, newSuperior.bv_val, 0 );
264 Debug( LDAP_DEBUG_ANY,
265 "do_modrdn: invalid newSuperior (%s)\n",
266 newSuperior.bv_val, 0, 0 );
268 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
269 "invalid newSuperior", NULL, NULL );
274 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
275 op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
277 manageDSAit = get_manageDSAit( op );
280 * We could be serving multiple database backends. Select the
281 * appropriate one, or send a referral to our "referral server"
282 * if we don't hold it.
284 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
285 BerVarray ref = referral_rewrite( default_referral,
286 NULL, &pdn, LDAP_SCOPE_DEFAULT );
288 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
289 NULL, NULL, ref ? ref : default_referral, NULL );
291 ber_bvarray_free( ref );
295 /* check restrictions */
296 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
297 if( rc != LDAP_SUCCESS ) {
298 send_ldap_result( conn, op, rc,
299 NULL, text, NULL, NULL );
303 /* check for referrals */
304 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
305 if ( rc != LDAP_SUCCESS ) {
309 /* Make sure that the entry being changed and the newSuperior are in
310 * the same backend, otherwise we return an error.
313 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
315 if ( newSuperior_be != be ) {
316 /* newSuperior is in same backend */
317 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
319 send_ldap_result( conn, op, rc,
320 NULL, "cannot rename between DSAa", NULL, NULL );
326 #if defined( LDAP_SLAPI )
327 slapi_x_backend_set_pb( pb, be );
328 slapi_x_connection_set_pb( pb, conn );
329 slapi_x_operation_set_pb( pb, op );
330 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
331 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
332 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
333 (void *)newSuperior.bv_val );
334 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
335 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
337 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
340 * A preoperation plugin failure will abort the
344 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
345 "failed\n", 0, 0, 0 );
347 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
348 "failed.\n", 0, 0, 0);
350 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0)
354 #endif /* defined( LDAP_SLAPI ) */
357 * do the add if 1 && (2 || 3)
358 * 1) there is an add function implemented in this backend;
359 * 2) this backend is master for what it holds;
360 * 3) it's a replica and the dn supplied is the update_ndn.
362 if ( be->be_modrdn ) {
363 /* do the update here */
364 int repl_user = be_isupdate( be, &op->o_ndn );
365 #ifndef SLAPD_MULTIMASTER
366 if ( !be->be_update_ndn.bv_len || repl_user )
369 if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
370 &pnewrdn, &nnewrdn, deloldrdn,
372 #ifdef SLAPD_MULTIMASTER
373 && ( !be->be_update_ndn.bv_len || !repl_user )
376 struct slap_replog_moddn moddn;
377 moddn.newrdn = &pnewrdn;
378 moddn.deloldrdn = deloldrdn;
379 moddn.newsup = &pnewSuperior;
381 replog( be, op, &pdn, &ndn, &moddn );
383 #ifndef SLAPD_MULTIMASTER
385 BerVarray defref = be->be_update_refs
386 ? be->be_update_refs : default_referral;
387 BerVarray ref = referral_rewrite( defref,
388 NULL, &pdn, LDAP_SCOPE_DEFAULT );
390 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
391 ref ? ref : defref, NULL );
393 ber_bvarray_free( ref );
397 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
398 NULL, "operation not supported within namingContext",
402 #if defined( LDAP_SLAPI )
403 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) != 0 ) {
405 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
406 "failed\n", 0, 0, 0 );
408 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
409 "failed.\n", 0, 0, 0);
412 #endif /* defined( LDAP_SLAPI ) */
418 free( pnewrdn.bv_val );
419 free( nnewrdn.bv_val );
421 if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
422 if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
436 Modifications **pmod )
438 int rc = LDAP_SUCCESS;
440 Modifications *mod = NULL;
443 assert( new_rdn != NULL );
444 assert( !deleteoldrdn || old_rdn != NULL );
446 /* Add new attribute values to the entry */
447 for ( a_cnt = 0; new_rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
448 AttributeDescription *desc = NULL;
449 Modifications *mod_tmp;
451 rc = slap_bv2ad( &new_rdn[ 0 ][ a_cnt ]->la_attr,
454 if ( rc != LDAP_SUCCESS ) {
456 LDAP_LOG ( OPERATION, ERR,
457 "slap_modrdn2modlist: %s: %s (new)\n",
459 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
461 Debug( LDAP_DEBUG_TRACE,
462 "slap_modrdn2modlist: %s: %s (new)\n",
464 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
469 /* ACL check of newly added attrs */
470 if ( be && !access_allowed( be, conn, op, e, desc,
471 &new_rdn[ 0 ][ 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[ 0 ][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[ 0 ][ a_cnt ]->la_attr.bv_val, 0, 0 );
483 rc = LDAP_INSUFFICIENT_ACCESS;
487 /* Apply modification */
488 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
489 + 2 * sizeof( struct berval ) );
490 mod_tmp->sml_desc = desc;
491 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
492 mod_tmp->sml_bvalues[ 0 ] = new_rdn[ 0 ][ a_cnt ]->la_value;
493 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
494 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
495 mod_tmp->sml_next = mod;
499 /* Remove old rdn value if required */
500 if ( deleteoldrdn ) {
501 for ( d_cnt = 0; old_rdn[ 0 ][ d_cnt ]; d_cnt++ ) {
502 AttributeDescription *desc = NULL;
503 Modifications *mod_tmp;
505 rc = slap_bv2ad( &old_rdn[ 0 ][ d_cnt ]->la_attr,
508 if ( rc != LDAP_SUCCESS ) {
510 LDAP_LOG ( OPERATION, ERR,
511 "slap_modrdn2modlist: %s: %s (old)\n",
513 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
516 Debug( LDAP_DEBUG_TRACE,
517 "slap_modrdn2modlist: %s: %s (old)\n",
519 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
525 /* ACL check of newly added attrs */
526 if ( be && !access_allowed( be, conn, op, e, desc,
527 &old_rdn[ 0 ][ d_cnt ]->la_value, ACL_WRITE,
530 LDAP_LOG ( OPERATION, ERR,
531 "slap_modrdn2modlist: access "
532 "to attr \"%s\" (old) not allowed\n",
533 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
536 Debug( LDAP_DEBUG_TRACE,
537 "slap_modrdn2modlist: access "
538 "to attr \"%s\" (old) not allowed\n",
539 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
542 rc = LDAP_INSUFFICIENT_ACCESS;
546 /* Apply modification */
547 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
548 + 2 * sizeof ( struct berval ) );
549 mod_tmp->sml_desc = desc;
550 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
551 mod_tmp->sml_bvalues[ 0 ]
552 = old_rdn[ 0 ][ d_cnt ]->la_value;
553 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
554 mod_tmp->sml_op = LDAP_MOD_DELETE;
555 mod_tmp->sml_next = mod;
561 /* LDAP v2 supporting correct attribute handling. */
562 if ( rc != LDAP_SUCCESS && mod != NULL ) {
564 for ( ; mod; mod = tmp ) {