3 * Copyright 1998-2002 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>
47 struct berval dn = { 0, NULL };
48 struct berval newrdn = { 0, NULL };
49 struct berval newSuperior = { 0, NULL };
52 struct berval pdn = { 0, NULL };
53 struct berval pnewrdn = { 0, NULL };
54 struct berval pnewSuperior = { 0, NULL }, *pnewS = NULL;
56 struct berval ndn = { 0, NULL };
57 struct berval nnewrdn = { 0, NULL };
58 struct berval nnewSuperior = { 0, NULL }, *nnewS = NULL;
61 Backend *newSuperior_be = NULL;
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_disconnect( conn, op,
95 LDAP_PROTOCOL_ERROR, "decoding error" );
96 return SLAPD_DISCONNECT;
99 /* Check for newSuperior parameter, if present scan it */
101 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
102 if ( op->o_protocol < LDAP_VERSION3 ) {
103 /* Conection record indicates v2 but field
104 * newSuperior is present: report error.
107 LDAP_LOG( OPERATION, ERR,
108 "do_modrdn: (v2) invalid field newSuperior.\n", 0, 0, 0 );
110 Debug( LDAP_DEBUG_ANY,
111 "modrdn(v2): invalid field newSuperior!\n",
115 send_ldap_disconnect( conn, op,
116 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
117 rc = SLAPD_DISCONNECT;
121 if ( ber_scanf( op->o_ber, "m", &newSuperior )
125 LDAP_LOG( OPERATION, ERR,
126 "do_modrdn: ber_scanf(\"m\") failed\n", 0, 0, 0 );
128 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
132 send_ldap_disconnect( conn, op,
133 LDAP_PROTOCOL_ERROR, "decoding error" );
134 rc = SLAPD_DISCONNECT;
137 pnewS = &pnewSuperior;
138 nnewS = &nnewSuperior;
142 LDAP_LOG( OPERATION, 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 : "" );
147 Debug( LDAP_DEBUG_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 : "" );
153 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
155 LDAP_LOG( OPERATION, ERR, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
157 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
160 send_ldap_disconnect( conn, op,
161 LDAP_PROTOCOL_ERROR, "decoding error" );
162 rc = SLAPD_DISCONNECT;
166 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
168 LDAP_LOG( OPERATION, ERR, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
170 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
173 /* get_ctrls has sent results. Now clean up. */
177 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
178 if( rc != LDAP_SUCCESS ) {
180 LDAP_LOG( OPERATION, INFO,
181 "do_modrdn: conn %d invalid dn (%s)\n",
182 conn->c_connid, dn.bv_val, 0 );
184 Debug( LDAP_DEBUG_ANY,
185 "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
187 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
188 "invalid DN", NULL, NULL );
192 if( ndn.bv_len == 0 ) {
194 LDAP_LOG( OPERATION, ERR,
195 "do_modrdn: attempt to modify root DSE.\n", 0, 0, 0 );
197 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
200 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
201 NULL, "cannot rename the root DSE", NULL, NULL );
204 #ifdef SLAPD_SCHEMA_DN
205 } else if ( bvmatch( &ndn, &global_schemandn ) ) {
207 LDAP_LOG( OPERATION, ERR,
208 "do_modrdn: attempt to modify subschema subentry: %s (%ld)\n",
209 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
211 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
212 global_schemandn.bv_val, (long) global_schemandn.bv_len, 0 );
215 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
216 NULL, "cannot rename subschema subentry", NULL, NULL );
221 /* FIXME: should have/use rdnPretty / rdnNormalize routines */
223 rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
224 if( rc != LDAP_SUCCESS ) {
226 LDAP_LOG( OPERATION, INFO,
227 "do_modrdn: conn %d invalid newrdn (%s)\n",
228 conn->c_connid, newrdn.bv_val, 0 );
230 Debug( LDAP_DEBUG_ANY,
231 "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
233 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
234 "invalid new RDN", NULL, NULL );
238 if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
240 LDAP_LOG( OPERATION, ERR,
241 "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val, 0, 0 );
243 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
244 pnewrdn.bv_val, 0, 0 );
247 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
248 "invalid new RDN", NULL, NULL );
253 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
255 if( rc != LDAP_SUCCESS ) {
257 LDAP_LOG( OPERATION, INFO,
258 "do_modrdn: conn %d invalid newSuperior (%s)\n",
259 conn->c_connid, newSuperior.bv_val, 0 );
261 Debug( LDAP_DEBUG_ANY,
262 "do_modrdn: invalid newSuperior (%s)\n",
263 newSuperior.bv_val, 0, 0 );
265 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
266 "invalid newSuperior", NULL, NULL );
271 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MODRDN dn=\"%s\"\n",
272 op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
274 manageDSAit = get_manageDSAit( op );
277 * We could be serving multiple database backends. Select the
278 * appropriate one, or send a referral to our "referral server"
279 * if we don't hold it.
281 if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
282 BerVarray ref = referral_rewrite( default_referral,
283 NULL, &pdn, LDAP_SCOPE_DEFAULT );
285 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
286 NULL, NULL, ref ? ref : default_referral, NULL );
288 ber_bvarray_free( ref );
292 /* check restrictions */
293 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
294 if( rc != LDAP_SUCCESS ) {
295 send_ldap_result( conn, op, rc,
296 NULL, text, NULL, NULL );
300 /* check for referrals */
301 rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
302 if ( rc != LDAP_SUCCESS ) {
306 /* Make sure that the entry being changed and the newSuperior are in
307 * the same backend, otherwise we return an error.
310 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
312 if ( newSuperior_be != be ) {
313 /* newSuperior is in same backend */
314 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
316 send_ldap_result( conn, op, rc,
317 NULL, "cannot rename between DSAa", NULL, NULL );
322 /* deref suffix alias if appropriate */
323 suffix_alias( be, &nnewSuperior );
326 /* deref suffix alias if appropriate */
327 suffix_alias( be, &ndn );
330 * do the add if 1 && (2 || 3)
331 * 1) there is an add function implemented in this backend;
332 * 2) this backend is master for what it holds;
333 * 3) it's a replica and the dn supplied is the update_ndn.
335 if ( be->be_modrdn ) {
336 /* do the update here */
337 int repl_user = be_isupdate( be, &op->o_ndn );
338 #ifndef SLAPD_MULTIMASTER
339 if ( !be->be_update_ndn.bv_len || repl_user )
342 if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
343 &pnewrdn, &nnewrdn, deloldrdn,
345 #ifdef SLAPD_MULTIMASTER
346 && ( !be->be_update_ndn.bv_len || !repl_user )
349 struct slap_replog_moddn moddn;
350 moddn.newrdn = &pnewrdn;
351 moddn.deloldrdn = deloldrdn;
352 moddn.newsup = &pnewSuperior;
354 replog( be, op, &pdn, &ndn, &moddn );
356 #ifndef SLAPD_MULTIMASTER
358 BerVarray defref = be->be_update_refs
359 ? be->be_update_refs : default_referral;
360 BerVarray ref = referral_rewrite( defref,
361 NULL, &pdn, LDAP_SCOPE_DEFAULT );
363 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
364 ref ? ref : defref, NULL );
366 ber_bvarray_free( ref );
370 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
371 NULL, "operation not supported within namingContext",
379 free( pnewrdn.bv_val );
380 free( nnewrdn.bv_val );
382 if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
383 if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
397 Modifications **pmod )
399 int rc = LDAP_SUCCESS;
401 Modifications *mod = NULL;
404 assert( new_rdn != NULL );
405 assert( !deleteoldrdn || old_rdn != NULL );
407 /* Add new attribute values to the entry */
408 for ( a_cnt = 0; new_rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
409 AttributeDescription *desc = NULL;
410 Modifications *mod_tmp;
412 rc = slap_bv2ad( &new_rdn[ 0 ][ a_cnt ]->la_attr,
415 if ( rc != LDAP_SUCCESS ) {
417 LDAP_LOG ( OPERATION, ERR,
418 "slap_modrdn2modlist: %s: %s (new)\n",
420 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
422 Debug( LDAP_DEBUG_TRACE,
423 "slap_modrdn2modlist: %s: %s (new)\n",
425 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0 );
430 /* ACL check of newly added attrs */
431 if ( be && !access_allowed( be, conn, op, e, desc,
432 &new_rdn[ 0 ][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
434 LDAP_LOG ( OPERATION, ERR,
435 "slap_modrdn2modlist: access to attr \"%s\" "
436 "(new) not allowed\n",
437 new_rdn[ 0 ][a_cnt]->la_attr.bv_val, 0, 0 );
439 Debug( LDAP_DEBUG_TRACE,
440 "slap_modrdn2modlist: access to attr \"%s\" "
441 "(new) not allowed\n",
442 new_rdn[ 0 ][ a_cnt ]->la_attr.bv_val, 0, 0 );
444 rc = LDAP_INSUFFICIENT_ACCESS;
448 /* Apply modification */
449 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
450 + 2 * sizeof( struct berval ) );
451 mod_tmp->sml_desc = desc;
452 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
453 mod_tmp->sml_bvalues[ 0 ] = new_rdn[ 0 ][ a_cnt ]->la_value;
454 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
455 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
456 mod_tmp->sml_next = mod;
460 /* Remove old rdn value if required */
461 if ( deleteoldrdn ) {
462 for ( d_cnt = 0; old_rdn[ 0 ][ d_cnt ]; d_cnt++ ) {
463 AttributeDescription *desc = NULL;
464 Modifications *mod_tmp;
466 rc = slap_bv2ad( &old_rdn[ 0 ][ d_cnt ]->la_attr,
469 if ( rc != LDAP_SUCCESS ) {
471 LDAP_LOG ( OPERATION, ERR,
472 "slap_modrdn2modlist: %s: %s (old)\n",
474 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
477 Debug( LDAP_DEBUG_TRACE,
478 "slap_modrdn2modlist: %s: %s (old)\n",
480 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
486 /* ACL check of newly added attrs */
487 if ( be && !access_allowed( be, conn, op, e, desc,
488 &old_rdn[ 0 ][ d_cnt ]->la_value, ACL_WRITE,
491 LDAP_LOG ( OPERATION, ERR,
492 "slap_modrdn2modlist: access "
493 "to attr \"%s\" (old) not allowed\n",
494 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
497 Debug( LDAP_DEBUG_TRACE,
498 "slap_modrdn2modlist: access "
499 "to attr \"%s\" (old) not allowed\n",
500 old_rdn[ 0 ][ d_cnt ]->la_attr.bv_val,
503 rc = LDAP_INSUFFICIENT_ACCESS;
507 /* Apply modification */
508 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
509 + 2 * sizeof ( struct berval ) );
510 mod_tmp->sml_desc = desc;
511 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
512 mod_tmp->sml_bvalues[ 0 ]
513 = old_rdn[ 0 ][ d_cnt ]->la_value;
514 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
515 mod_tmp->sml_op = LDAP_MOD_DELETE;
516 mod_tmp->sml_next = mod;
522 /* LDAP v2 supporting correct attribute handling. */
523 if ( rc != LDAP_SUCCESS && mod != NULL ) {
525 for ( ; mod; mod = tmp ) {