3 * Copyright 1998-2000 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 char *dn, *ndn = NULL, *newrdn;
50 /* Vars for LDAP v3 newSuperior support */
51 char *newSuperior = NULL;
52 char *nnewSuperior = NULL;
53 Backend *newSuperior_be = NULL;
60 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
61 "do_modrdn: begin\n" ));
63 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
68 * Parse the modrdn request. It looks like this:
70 * ModifyRDNRequest := SEQUENCE {
71 * entry DistinguishedName,
72 * newrdn RelativeDistinguishedName
73 * deleteoldrdn BOOLEAN,
74 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
78 if ( ber_scanf( op->o_ber, "{aab", &dn, &newrdn, &deloldrdn )
81 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
82 "do_modrdn: ber_scanf failed\n" ));
84 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
87 send_ldap_disconnect( conn, op,
88 LDAP_PROTOCOL_ERROR, "decoding error" );
89 return SLAPD_DISCONNECT;
92 /* Check for newSuperior parameter, if present scan it */
94 if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
95 if ( op->o_protocol < LDAP_VERSION3 ) {
96 /* Conection record indicates v2 but field
97 * newSuperior is present: report error.
100 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
101 "do_modrdn: (v2) invalid field newSuperior.\n" ));
103 Debug( LDAP_DEBUG_ANY,
104 "modrdn(v2): invalid field newSuperior!\n",
108 send_ldap_disconnect( conn, op,
109 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
110 rc = SLAPD_DISCONNECT;
114 if ( ber_scanf( op->o_ber, "a", &newSuperior )
118 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
119 "do_modrdn: ber_scanf(\"a\") failed\n" ));
121 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
125 send_ldap_disconnect( conn, op,
126 LDAP_PROTOCOL_ERROR, "decoding error" );
127 rc = SLAPD_DISCONNECT;
131 nnewSuperior = ch_strdup( newSuperior );
133 if( dn_normalize( nnewSuperior ) == NULL ) {
135 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
136 "do_modrdn: invalid new superior (%s)\n", newSuperior ));
138 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
142 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
143 "invalid new superior DN", NULL, NULL );
150 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
151 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
152 dn, newrdn, newSuperior != NULL ? newSuperior : "" ));
154 Debug( LDAP_DEBUG_ARGS,
155 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
157 newSuperior != NULL ? newSuperior : "" );
161 if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
163 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
164 "do_modrdn: ber_scanf failed\n" ));
166 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
169 send_ldap_disconnect( conn, op,
170 LDAP_PROTOCOL_ERROR, "decoding error" );
171 rc = SLAPD_DISCONNECT;
175 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
177 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
178 "do_modrdn: get_ctrls failed\n" ));
180 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
183 /* get_ctrls has sent results. Now clean up. */
187 ndn = ch_strdup( dn );
189 if( dn_normalize( ndn ) == NULL ) {
191 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
192 "do_modrdn: invalid dn (%s)\n", dn ));
194 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n", dn, 0, 0 );
197 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
198 "invalid DN", NULL, NULL );
202 if( !rdn_validate( newrdn ) ) {
204 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
205 "do_modrdn: invalid rdn (%s).\n", newrdn ));
207 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n", newrdn, 0, 0 );
210 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
211 "invalid RDN", NULL, NULL );
217 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
218 "do_modrdn: attempt to modify root DSE.\n" ));
220 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
223 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
224 NULL, "cannot rename the root DSE", NULL, NULL );
227 #ifdef SLAPD_SCHEMA_DN
228 } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
230 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
231 "do_modrdn: attempt to modify subschema subentry\n" ));
233 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry!\n", 0, 0, 0 );
236 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
237 NULL, "cannot rename subschema subentry", NULL, NULL );
242 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
243 op->o_connid, op->o_opid, dn, 0, 0 );
245 manageDSAit = get_manageDSAit( op );
248 * We could be serving multiple database backends. Select the
249 * appropriate one, or send a referral to our "referral server"
250 * if we don't hold it.
252 if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
253 struct berval **ref = referral_rewrite( default_referral,
254 NULL, dn, LDAP_SCOPE_DEFAULT );
256 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
257 NULL, NULL, ref ? ref : default_referral, NULL );
263 /* check restrictions */
264 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
265 if( rc != LDAP_SUCCESS ) {
266 send_ldap_result( conn, op, rc,
267 NULL, text, NULL, NULL );
271 /* check for referrals */
272 rc = backend_check_referrals( be, conn, op, dn, ndn );
273 if ( rc != LDAP_SUCCESS ) {
277 /* Make sure that the entry being changed and the newSuperior are in
278 * the same backend, otherwise we return an error.
280 if( newSuperior != NULL ) {
281 newSuperior_be = select_backend( nnewSuperior, 0 );
283 if ( newSuperior_be != be ) {
284 /* newSuperior is in same backend */
285 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
287 send_ldap_result( conn, op, rc,
288 NULL, "cannot rename between DSAa", NULL, NULL );
293 /* deref suffix alias if appropriate */
294 nnewSuperior = suffix_alias( be, nnewSuperior );
297 /* deref suffix alias if appropriate */
298 ndn = suffix_alias( be, ndn );
301 * do the add if 1 && (2 || 3)
302 * 1) there is an add function implemented in this backend;
303 * 2) this backend is master for what it holds;
304 * 3) it's a replica and the dn supplied is the update_ndn.
306 if ( be->be_modrdn ) {
307 /* do the update here */
308 #ifndef SLAPD_MULTIMASTER
309 if ( be->be_update_ndn == NULL ||
310 strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
313 if ( (*be->be_modrdn)( be, conn, op, dn, ndn, newrdn,
314 deloldrdn, newSuperior ) == 0
315 #ifdef SLAPD_MULTIMASTER
316 && ( be->be_update_ndn == NULL ||
317 strcmp( be->be_update_ndn, op->o_ndn ) )
320 struct replog_moddn moddn;
321 moddn.newrdn = newrdn;
322 moddn.deloldrdn = deloldrdn;
323 moddn.newsup = newSuperior;
325 replog( be, op, dn, &moddn );
327 #ifndef SLAPD_MULTIMASTER
329 struct berval **defref = be->be_update_refs
330 ? be->be_update_refs : default_referral;
331 struct berval **ref = referral_rewrite( defref,
332 NULL, dn, LDAP_SCOPE_DEFAULT );
334 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
335 ref ? ref : defref, NULL );
341 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
342 NULL, "operation not supported within namingContext", NULL, NULL );
347 if( ndn != NULL ) free( ndn );
349 if ( newSuperior != NULL )
351 if ( nnewSuperior != NULL )
352 free( nnewSuperior );