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;
68 Slapi_PBlock *pb = op->o_pb;
71 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
73 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
78 * Parse the modrdn request. It looks like this:
80 * ModifyRDNRequest := SEQUENCE {
81 * entry DistinguishedName,
82 * newrdn RelativeDistinguishedName
83 * deleteoldrdn BOOLEAN,
84 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
88 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
92 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
97 send_ldap_disconnect( conn, op,
98 LDAP_PROTOCOL_ERROR, "decoding error" );
99 return SLAPD_DISCONNECT;
102 /* Check for newSuperior parameter, if present scan it */
104 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
105 if ( op->o_protocol < LDAP_VERSION3 ) {
106 /* Conection record indicates v2 but field
107 * newSuperior is present: report error.
110 LDAP_LOG( OPERATION, ERR,
111 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
113 Debug( LDAP_DEBUG_ANY,
114 "modrdn(v2): invalid field newSuperior!\n",
118 send_ldap_disconnect( conn, op,
119 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
120 rc = SLAPD_DISCONNECT;
124 if ( ber_scanf( op->o_ber, "m", &newSuperior )
128 LDAP_LOG( OPERATION, ERR,
129 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
131 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
135 send_ldap_disconnect( conn, op,
136 LDAP_PROTOCOL_ERROR, "decoding error" );
137 rc = SLAPD_DISCONNECT;
140 pnewS = &pnewSuperior;
141 nnewS = &nnewSuperior;
145 LDAP_LOG( OPERATION, 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 : "" );
150 Debug( LDAP_DEBUG_ARGS,
151 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
152 dn.bv_val, newrdn.bv_val,
153 newSuperior.bv_len ? newSuperior.bv_val : "" );
156 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
158 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
160 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
163 send_ldap_disconnect( conn, op,
164 LDAP_PROTOCOL_ERROR, "decoding error" );
165 rc = SLAPD_DISCONNECT;
169 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
171 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
173 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
176 /* get_ctrls has sent results. Now clean up. */
180 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
181 if( rc != LDAP_SUCCESS ) {
183 LDAP_LOG( OPERATION, INFO,
184 "do_modrdn: conn %d invalid dn (%s)\n",
185 conn->c_connid, dn.bv_val, 0 );
187 Debug( LDAP_DEBUG_ANY,
188 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
190 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
191 "invalid DN", NULL, NULL );
195 if( ndn.bv_len == 0 ) {
197 LDAP_LOG( OPERATION, ERR,
198 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
200 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
203 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
204 NULL, "cannot rename the root DSE", NULL, NULL );
207 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
209 LDAP_LOG( OPERATION, ERR,
210 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
211 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
213 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
214 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
217 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
218 NULL, "cannot rename subschema subentry", NULL, NULL );
222 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
224 rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
225 if( rc != LDAP_SUCCESS ) {
227 LDAP_LOG( OPERATION, INFO,
228 "do_modrdn: conn %d invalid newrdn (%s)\n",
229 conn->c_connid, newrdn.bv_val, 0 );
231 Debug( LDAP_DEBUG_ANY,
232 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
234 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
235 "invalid new RDN", NULL, NULL );
239 if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
241 LDAP_LOG( OPERATION, ERR,
242 "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val, 0, 0 );
244 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
245 pnewrdn.bv_val, 0, 0 );
248 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
249 "invalid new RDN", NULL, NULL );
254 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
256 if( rc != LDAP_SUCCESS ) {
258 LDAP_LOG( OPERATION, INFO,
259 "do_modrdn: conn %d invalid newSuperior (%s)\n",
260 conn->c_connid, newSuperior.bv_val, 0 );
262 Debug( LDAP_DEBUG_ANY,
263 "do_modrdn: invalid newSuperior (%s)\n",
264 newSuperior.bv_val, 0, 0 );
266 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
267 "invalid newSuperior", NULL, NULL );
272 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
273 op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
275 manageDSAit = get_manageDSAit( op );
278 * We could be serving multiple database backends. Select the
279 * appropriate one, or send a referral to our "referral server"
280 * if we don't hold it.
282 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
283 BerVarray ref = referral_rewrite( default_referral,
284 NULL, &pdn, LDAP_SCOPE_DEFAULT );
286 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
287 NULL, NULL, ref ? ref : default_referral, NULL );
289 ber_bvarray_free( ref );
293 /* check restrictions */
294 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
295 if( rc != LDAP_SUCCESS ) {
296 send_ldap_result( conn, op, rc,
297 NULL, text, NULL, NULL );
301 /* check for referrals */
302 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
303 if ( rc != LDAP_SUCCESS ) {
307 /* Make sure that the entry being changed and the newSuperior are in
308 * the same backend, otherwise we return an error.
311 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
313 if ( newSuperior_be != be ) {
314 /* newSuperior is in same backend */
315 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
317 send_ldap_result( conn, op, rc,
318 NULL, "cannot rename between DSAa", NULL, NULL );
323 /* deref suffix alias if appropriate */
324 suffix_alias( be, &nnewSuperior );
327 /* deref suffix alias if appropriate */
328 suffix_alias( be, &ndn );
330 #if defined( LDAP_SLAPI )
331 slapi_x_backend_set_pb( pb, be );
332 slapi_x_connection_set_pb( pb, conn );
333 slapi_x_operation_set_pb( pb, op );
334 slapi_pblock_set( pb, SLAPI_MODRDN_TARGET, (void *)dn.bv_val );
335 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
336 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
337 (void *)newSuperior.bv_val );
338 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
339 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
341 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
344 * A preoperation plugin failure will abort the
348 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preoperation plugin "
349 "failed\n", 0, 0, 0 );
351 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preoperation plugin "
352 "failed.\n", 0, 0, 0);
354 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0)
358 #endif /* defined( LDAP_SLAPI ) */
361 * do the add if 1 && (2 || 3)
362 * 1) there is an add function implemented in this backend;
363 * 2) this backend is master for what it holds;
364 * 3) it's a replica and the dn supplied is the update_ndn.
366 if ( be->be_modrdn ) {
367 /* do the update here */
368 int repl_user = be_isupdate( be, &op->o_ndn );
369 #ifndef SLAPD_MULTIMASTER
370 if ( !be->be_update_ndn.bv_len || repl_user )
373 if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
374 &pnewrdn, &nnewrdn, deloldrdn,
376 #ifdef SLAPD_MULTIMASTER
377 && ( !be->be_update_ndn.bv_len || !repl_user )
380 struct slap_replog_moddn moddn;
381 moddn.newrdn = &pnewrdn;
382 moddn.deloldrdn = deloldrdn;
383 moddn.newsup = &pnewSuperior;
385 replog( be, op, &pdn, &ndn, &moddn );
387 #ifndef SLAPD_MULTIMASTER
389 BerVarray defref = be->be_update_refs
390 ? be->be_update_refs : default_referral;
391 BerVarray ref = referral_rewrite( defref,
392 NULL, &pdn, LDAP_SCOPE_DEFAULT );
394 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
395 ref ? ref : defref, NULL );
397 ber_bvarray_free( ref );
401 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
402 NULL, "operation not supported within namingContext",
406 #if defined( LDAP_SLAPI )
407 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODRDN_FN, pb ) != 0 ) {
409 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postoperation plugins "
410 "failed\n", 0, 0, 0 );
412 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postoperation plugins "
413 "failed.\n", 0, 0, 0);
416 #endif /* defined( LDAP_SLAPI ) */
422 free( pnewrdn.bv_val );
423 free( nnewrdn.bv_val );
425 if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
426 if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
440 Modifications **pmod )
442 int rc = LDAP_SUCCESS;
444 Modifications *mod = NULL;
447 assert( new_rdn != NULL );
448 assert( !deleteoldrdn || old_rdn != NULL );
450 /* Add new attribute values to the entry */
451 for ( a_cnt = 0; new_rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
452 AttributeDescription *desc = NULL;
453 Modifications *mod_tmp;
455 rc = slap_bv2ad( &new_rdn[ 0 ][ a_cnt ]->la_attr,
458 if ( rc != LDAP_SUCCESS ) {
460 LDAP_LOG ( OPERATION, ERR,
461 "slap_modrdn2modlist: %s: %s (new)\n",
463 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
465 Debug( LDAP_DEBUG_TRACE,
466 "slap_modrdn2modlist: %s: %s (new)\n",
468 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
473 /* ACL check of newly added attrs */
474 if ( be && !access_allowed( be, conn, op, e, desc,
475 &new_rdn[ 0 ][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
477 LDAP_LOG ( OPERATION, ERR,
478 "slap_modrdn2modlist: access to attr \"%s\" "
479 "(new) not allowed\n",
480 new_rdn[ 0 ][a_cnt]->la_attr.bv_val, 0, 0 );
482 Debug( LDAP_DEBUG_TRACE,
483 "slap_modrdn2modlist: access to attr \"%s\" "
484 "(new) not allowed\n",
485 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0, 0 );
487 rc = LDAP_INSUFFICIENT_ACCESS;
491 /* Apply modification */
492 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
493 + 2 * sizeof( struct berval ) );
494 mod_tmp->sml_desc = desc;
495 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
496 mod_tmp->sml_bvalues[ 0 ] = new_rdn[ 0 ][ a_cnt ]->la_value;
497 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
498 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
499 mod_tmp->sml_next = mod;
503 /* Remove old rdn value if required */
504 if ( deleteoldrdn ) {
505 for ( d_cnt = 0; old_rdn[ 0 ][ d_cnt ]; d_cnt++ ) {
506 AttributeDescription *desc = NULL;
507 Modifications *mod_tmp;
509 rc = slap_bv2ad( &old_rdn[ 0 ][ d_cnt ]->la_attr,
512 if ( rc != LDAP_SUCCESS ) {
514 LDAP_LOG ( OPERATION, ERR,
515 "slap_modrdn2modlist: %s: %s (old)\n",
517 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
520 Debug( LDAP_DEBUG_TRACE,
521 "slap_modrdn2modlist: %s: %s (old)\n",
523 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
529 /* ACL check of newly added attrs */
530 if ( be && !access_allowed( be, conn, op, e, desc,
531 &old_rdn[ 0 ][ d_cnt ]->la_value, ACL_WRITE,
534 LDAP_LOG ( OPERATION, ERR,
535 "slap_modrdn2modlist: access "
536 "to attr \"%s\" (old) not allowed\n",
537 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
540 Debug( LDAP_DEBUG_TRACE,
541 "slap_modrdn2modlist: access "
542 "to attr \"%s\" (old) not allowed\n",
543 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
546 rc = LDAP_INSUFFICIENT_ACCESS;
550 /* Apply modification */
551 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
552 + 2 * sizeof ( struct berval ) );
553 mod_tmp->sml_desc = desc;
554 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
555 mod_tmp->sml_bvalues[ 0 ]
556 = old_rdn[ 0 ][ d_cnt ]->la_value;
557 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
558 mod_tmp->sml_op = LDAP_MOD_DELETE;
559 mod_tmp->sml_next = mod;
565 /* LDAP v2 supporting correct attribute handling. */
566 if ( rc != LDAP_SUCCESS && mod != NULL ) {
568 for ( ; mod; mod = tmp ) {