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
32 #include "slapi_common.h"
36 #include <ac/socket.h>
37 #include <ac/string.h>
49 struct berval dn = { 0, NULL };
50 struct berval newrdn = { 0, NULL };
51 struct berval newSuperior = { 0, NULL };
54 struct berval pdn = { 0, NULL };
55 struct berval pnewrdn = { 0, NULL };
56 struct berval pnewSuperior = { 0, NULL }, *pnewS = NULL;
58 struct berval ndn = { 0, NULL };
59 struct berval nnewrdn = { 0, NULL };
60 struct berval nnewSuperior = { 0, NULL }, *nnewS = NULL;
63 Backend *newSuperior_be = NULL;
69 Slapi_PBlock *pb = op->o_pb;
72 LDAP_LOG( OPERATION, ENTRY, "do_modrdn: begin\n", 0, 0, 0 );
74 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
79 * Parse the modrdn request. It looks like this:
81 * ModifyRDNRequest := SEQUENCE {
82 * entry DistinguishedName,
83 * newrdn RelativeDistinguishedName
84 * deleteoldrdn BOOLEAN,
85 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
89 if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
93 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
95 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
98 send_ldap_disconnect( conn, op,
99 LDAP_PROTOCOL_ERROR, "decoding error" );
100 return SLAPD_DISCONNECT;
103 /* Check for newSuperior parameter, if present scan it */
105 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
106 if ( op->o_protocol < LDAP_VERSION3 ) {
107 /* Conection record indicates v2 but field
108 * newSuperior is present: report error.
111 LDAP_LOG( OPERATION, ERR,
112 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
114 Debug( LDAP_DEBUG_ANY,
115 "modrdn(v2): invalid field newSuperior!\n",
119 send_ldap_disconnect( conn, op,
120 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
121 rc = SLAPD_DISCONNECT;
125 if ( ber_scanf( op->o_ber, "m", &newSuperior )
129 LDAP_LOG( OPERATION, ERR,
130 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
132 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
136 send_ldap_disconnect( conn, op,
137 LDAP_PROTOCOL_ERROR, "decoding error" );
138 rc = SLAPD_DISCONNECT;
141 pnewS = &pnewSuperior;
142 nnewS = &nnewSuperior;
146 LDAP_LOG( OPERATION, 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 : "" );
151 Debug( LDAP_DEBUG_ARGS,
152 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
153 dn.bv_val, newrdn.bv_val,
154 newSuperior.bv_len ? newSuperior.bv_val : "" );
157 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
159 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
161 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
164 send_ldap_disconnect( conn, op,
165 LDAP_PROTOCOL_ERROR, "decoding error" );
166 rc = SLAPD_DISCONNECT;
170 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
172 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
174 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
177 /* get_ctrls has sent results. Now clean up. */
181 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
182 if( rc != LDAP_SUCCESS ) {
184 LDAP_LOG( OPERATION, INFO,
185 "do_modrdn: conn %d invalid dn (%s)\n",
186 conn->c_connid, dn.bv_val, 0 );
188 Debug( LDAP_DEBUG_ANY,
189 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
191 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
192 "invalid DN", NULL, NULL );
196 if( 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_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
205 NULL, "cannot rename the root DSE", NULL, NULL );
208 } else if ( bvmatch( &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_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
219 NULL, "cannot rename subschema subentry", NULL, NULL );
223 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
225 rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
226 if( rc != LDAP_SUCCESS ) {
228 LDAP_LOG( OPERATION, INFO,
229 "do_modrdn: conn %d invalid newrdn (%s)\n",
230 conn->c_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_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
236 "invalid new RDN", NULL, NULL );
240 if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
242 LDAP_LOG( OPERATION, ERR,
243 "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val, 0, 0 );
245 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
246 pnewrdn.bv_val, 0, 0 );
249 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
250 "invalid new RDN", NULL, NULL );
255 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
257 if( rc != LDAP_SUCCESS ) {
259 LDAP_LOG( OPERATION, INFO,
260 "do_modrdn: conn %d invalid newSuperior (%s)\n",
261 conn->c_connid, newSuperior.bv_val, 0 );
263 Debug( LDAP_DEBUG_ANY,
264 "do_modrdn: invalid newSuperior (%s)\n",
265 newSuperior.bv_val, 0, 0 );
267 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
268 "invalid newSuperior", NULL, NULL );
273 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
274 op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
276 manageDSAit = get_manageDSAit( op );
279 * We could be serving multiple database backends. Select the
280 * appropriate one, or send a referral to our "referral server"
281 * if we don't hold it.
283 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
284 BerVarray ref = referral_rewrite( default_referral,
285 NULL, &pdn, LDAP_SCOPE_DEFAULT );
287 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
288 NULL, NULL, ref ? ref : default_referral, NULL );
290 ber_bvarray_free( ref );
294 /* check restrictions */
295 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
296 if( rc != LDAP_SUCCESS ) {
297 send_ldap_result( conn, op, rc,
298 NULL, text, NULL, NULL );
302 /* check for referrals */
303 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
304 if ( rc != LDAP_SUCCESS ) {
308 /* Make sure that the entry being changed and the newSuperior are in
309 * the same backend, otherwise we return an error.
312 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
314 if ( newSuperior_be != be ) {
315 /* newSuperior is in same backend */
316 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
318 send_ldap_result( conn, op, rc,
319 NULL, "cannot rename between DSAa", NULL, NULL );
324 /* deref suffix alias if appropriate */
325 suffix_alias( be, &nnewSuperior );
328 /* deref suffix alias if appropriate */
329 suffix_alias( be, &ndn );
331 #if defined( LDAP_SLAPI )
332 slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
333 slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
334 slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
335 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
336 slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN, (void *)newrdn.bv_val );
337 slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR,
338 (void *)newSuperior.bv_val );
339 slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN, (void *)deloldrdn );
340 slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
341 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(1) );
343 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODRDN_FN, pb );
344 if ( rc != 0 && rc != LDAP_OTHER ) {
346 * either there is no preOp (modrdn) plugins
347 * or a plugin failed. Just log it
349 * FIXME: is this correct?
352 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn preOps "
353 "failed\n", 0, 0, 0 );
355 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn preOps "
356 "failed.\n", 0, 0, 0);
359 #endif /* defined( LDAP_SLAPI ) */
362 * do the add if 1 && (2 || 3)
363 * 1) there is an add function implemented in this backend;
364 * 2) this backend is master for what it holds;
365 * 3) it's a replica and the dn supplied is the update_ndn.
367 if ( be->be_modrdn ) {
368 /* do the update here */
369 int repl_user = be_isupdate( be, &op->o_ndn );
370 #ifndef SLAPD_MULTIMASTER
371 if ( !be->be_update_ndn.bv_len || repl_user )
374 if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
375 &pnewrdn, &nnewrdn, deloldrdn,
377 #ifdef SLAPD_MULTIMASTER
378 && ( !be->be_update_ndn.bv_len || !repl_user )
381 struct slap_replog_moddn moddn;
382 moddn.newrdn = &pnewrdn;
383 moddn.deloldrdn = deloldrdn;
384 moddn.newsup = &pnewSuperior;
386 replog( be, op, &pdn, &ndn, &moddn );
388 #ifndef SLAPD_MULTIMASTER
390 BerVarray defref = be->be_update_refs
391 ? be->be_update_refs : default_referral;
392 BerVarray ref = referral_rewrite( defref,
393 NULL, &pdn, LDAP_SCOPE_DEFAULT );
395 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
396 ref ? ref : defref, NULL );
398 ber_bvarray_free( ref );
402 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
403 NULL, "operation not supported within namingContext",
407 #if defined( LDAP_SLAPI )
408 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_MODRDN_FN, pb );
409 if ( rc != 0 && rc != LDAP_OTHER ) {
411 * either there is no postOp (modrdn) plugins
412 * or a plugin failed. Just log it
414 * FIXME: is this correct?
417 LDAP_LOG( OPERATION, INFO, "do_modrdn: modrdn postOps "
418 "failed\n", 0, 0, 0 );
420 Debug(LDAP_DEBUG_TRACE, "do_modrdn: modrdn postOps "
421 "failed.\n", 0, 0, 0);
424 #endif /* defined( LDAP_SLAPI ) */
430 free( pnewrdn.bv_val );
431 free( nnewrdn.bv_val );
433 if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
434 if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
448 Modifications **pmod )
450 int rc = LDAP_SUCCESS;
452 Modifications *mod = NULL;
455 assert( new_rdn != NULL );
456 assert( !deleteoldrdn || old_rdn != NULL );
458 /* Add new attribute values to the entry */
459 for ( a_cnt = 0; new_rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
460 AttributeDescription *desc = NULL;
461 Modifications *mod_tmp;
463 rc = slap_bv2ad( &new_rdn[ 0 ][ a_cnt ]->la_attr,
466 if ( rc != LDAP_SUCCESS ) {
468 LDAP_LOG ( OPERATION, ERR,
469 "slap_modrdn2modlist: %s: %s (new)\n",
471 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
473 Debug( LDAP_DEBUG_TRACE,
474 "slap_modrdn2modlist: %s: %s (new)\n",
476 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
481 /* ACL check of newly added attrs */
482 if ( be && !access_allowed( be, conn, op, e, desc,
483 &new_rdn[ 0 ][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
485 LDAP_LOG ( OPERATION, ERR,
486 "slap_modrdn2modlist: access to attr \"%s\" "
487 "(new) not allowed\n",
488 new_rdn[ 0 ][a_cnt]->la_attr.bv_val, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE,
491 "slap_modrdn2modlist: access to attr \"%s\" "
492 "(new) not allowed\n",
493 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0, 0 );
495 rc = LDAP_INSUFFICIENT_ACCESS;
499 /* Apply modification */
500 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
501 + 2 * sizeof( struct berval ) );
502 mod_tmp->sml_desc = desc;
503 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
504 mod_tmp->sml_bvalues[ 0 ] = new_rdn[ 0 ][ a_cnt ]->la_value;
505 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
506 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
507 mod_tmp->sml_next = mod;
511 /* Remove old rdn value if required */
512 if ( deleteoldrdn ) {
513 for ( d_cnt = 0; old_rdn[ 0 ][ d_cnt ]; d_cnt++ ) {
514 AttributeDescription *desc = NULL;
515 Modifications *mod_tmp;
517 rc = slap_bv2ad( &old_rdn[ 0 ][ d_cnt ]->la_attr,
520 if ( rc != LDAP_SUCCESS ) {
522 LDAP_LOG ( OPERATION, ERR,
523 "slap_modrdn2modlist: %s: %s (old)\n",
525 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
528 Debug( LDAP_DEBUG_TRACE,
529 "slap_modrdn2modlist: %s: %s (old)\n",
531 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
537 /* ACL check of newly added attrs */
538 if ( be && !access_allowed( be, conn, op, e, desc,
539 &old_rdn[ 0 ][ d_cnt ]->la_value, ACL_WRITE,
542 LDAP_LOG ( OPERATION, ERR,
543 "slap_modrdn2modlist: access "
544 "to attr \"%s\" (old) not allowed\n",
545 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
548 Debug( LDAP_DEBUG_TRACE,
549 "slap_modrdn2modlist: access "
550 "to attr \"%s\" (old) not allowed\n",
551 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
554 rc = LDAP_INSUFFICIENT_ACCESS;
558 /* Apply modification */
559 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
560 + 2 * sizeof ( struct berval ) );
561 mod_tmp->sml_desc = desc;
562 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
563 mod_tmp->sml_bvalues[ 0 ]
564 = old_rdn[ 0 ][ d_cnt ]->la_value;
565 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
566 mod_tmp->sml_op = LDAP_MOD_DELETE;
567 mod_tmp->sml_next = mod;
573 /* LDAP v2 supporting correct attribute handling. */
574 if ( rc != LDAP_SUCCESS && mod != NULL ) {
576 for ( ; mod; mod = tmp ) {