modrdn.c 9.15 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
5
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * LDAP v3 newSuperior support.
 *
 * Copyright 1999, Juan C. Gomez, All rights reserved.
 * This software is not subject to any license of Silicon Graphics 
 * Inc. or Purdue University.
 *
 * Redistribution and use in source and binary forms are permitted
 * without restriction or fee of any kind as long as this notice
 * is preserved.
 *
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
32
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
33
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
34
35
36
37

#include <ac/socket.h>
#include <ac/string.h>

38
#include "ldap_pvt.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
40
#include "slap.h"

41
int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
42
43
44
45
46
do_modrdn(
    Connection	*conn,
    Operation	*op
)
{
47
48
49
	struct berval dn = { 0, NULL };
	struct berval newrdn = { 0, NULL };
	struct berval newSuperior = { 0, NULL };
50
	ber_int_t	deloldrdn;
51

52
	struct berval *ndn;
Gary Williams's avatar
Gary Williams committed
53
	char	*nnewSuperior = NULL;
54
55

	Backend	*be;
56
	Backend	*newSuperior_be = NULL;
57
	ber_len_t	length;
58
	int rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
59
	const char *text;
60
	int manageDSAit;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
61

62
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
63
	LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
64
		"do_modrdn: begin\n" ));
65
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66
	Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
67
68
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
69
70
71
72
73
74
75

	/*
	 * Parse the modrdn request.  It looks like this:
	 *
	 *	ModifyRDNRequest := SEQUENCE {
	 *		entry	DistinguishedName,
	 *		newrdn	RelativeDistinguishedName
76
77
	 *		deleteoldrdn	BOOLEAN,
	 *		newSuperior	[0] LDAPDN OPTIONAL (v3 Only!)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
78
79
80
	 *	}
	 */

81
82
83
	if ( ber_scanf( op->o_ber, "{oob", &dn, &newrdn, &deloldrdn )
	    == LBER_ERROR )
	{
84
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
85
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
86
			"do_modrdn: ber_scanf failed\n" ));
87
#else
Kurt Zeilenga's avatar
Kurt Zeilenga committed
88
		Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
89
90
#endif

91
92
		send_ldap_disconnect( conn, op,
			LDAP_PROTOCOL_ERROR, "decoding error" );
93
		return SLAPD_DISCONNECT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
94
95
	}

96
97
98
	/* Check for newSuperior parameter, if present scan it */

	if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
		if ( op->o_protocol < LDAP_VERSION3 ) {
100
101
102
			/* Conection record indicates v2 but field 
			 * newSuperior is present: report error.
			 */
103
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
104
			LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
105
				"do_modrdn: (v2) invalid field newSuperior.\n" ));
106
#else
107
			Debug( LDAP_DEBUG_ANY,
108
109
			    "modrdn(v2): invalid field newSuperior!\n",
			    0, 0, 0 );
110
111
#endif

112
113
			send_ldap_disconnect( conn, op,
				LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
114
			rc = SLAPD_DISCONNECT;
Howard Chu's avatar
Howard Chu committed
115
			goto cleanup;
116
		}
117

118
		if ( ber_scanf( op->o_ber, "o", &newSuperior ) 
119
120
		     == LBER_ERROR ) {

121
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
122
			LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
123
				"do_modrdn: ber_scanf(\"a\") failed\n" ));
124
#else
Howard Chu's avatar
Howard Chu committed
125
			Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
126
				0, 0, 0 );
127
128
#endif

129
130
			send_ldap_disconnect( conn, op,
				LDAP_PROTOCOL_ERROR, "decoding error" );
131
			rc = SLAPD_DISCONNECT;
Howard Chu's avatar
Howard Chu committed
132
			goto cleanup;
133
134
		}

135
		nnewSuperior = ch_strdup( newSuperior.bv_val );
136

137
		if( dn_normalize( nnewSuperior ) == NULL ) {
138
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
139
			LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
140
141
				"do_modrdn:  invalid new superior (%s)\n",
				newSuperior.bv_val ));
142
#else
143
			Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
144
				newSuperior.bv_val, 0, 0 );
145
146
#endif

147
			send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
148
				"invalid new superior DN", NULL, NULL );
Howard Chu's avatar
Howard Chu committed
149
			goto cleanup;
150
		}
151

152
	}
153

154
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
155
	LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
156
157
158
		"do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
		dn.bv_val, newrdn.bv_val,
		newSuperior.bv_len ? newSuperior.bv_val : "" ));
159
#else
160
161
	Debug( LDAP_DEBUG_ARGS,
	    "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
162
163
		dn.bv_val, newrdn.bv_val,
		newSuperior.bv_len ? newSuperior.bv_val : "" );
164
165
#endif

166

167
	if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
168
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
169
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
170
			"do_modrdn: ber_scanf failed\n" ));
171
#else
172
		Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
173
174
#endif

175
		send_ldap_disconnect( conn, op,
176
			LDAP_PROTOCOL_ERROR, "decoding error" );
177
		rc = SLAPD_DISCONNECT;
Howard Chu's avatar
Howard Chu committed
178
		goto cleanup;
179
180
	}

181
	if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
182
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
183
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
184
			"do_modrdn: get_ctrls failed\n" ));
185
#else
186
		Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
187
188
#endif

Gary Williams's avatar
Gary Williams committed
189
		/* get_ctrls has sent results.	Now clean up. */
Howard Chu's avatar
Howard Chu committed
190
		goto cleanup;
191
192
	} 

193
	rc = dnNormalize( NULL, &dn, &ndn );
194

195
	if( rc != LDAP_SUCCESS ) {
196
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
197
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
198
			"do_modrdn: invalid dn (%s)\n", dn.bv_val ));
199
#else
200
201
		Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n",
			dn.bv_val, 0, 0 );
202
203
#endif

204
205
206
207
208
		send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
		    "invalid DN", NULL, NULL );
		goto cleanup;
	}

209
	if( !rdn_validate( newrdn.bv_val ) ) {
210
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
211
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
212
			"do_modrdn: invalid rdn (%s).\n", newrdn.bv_val ));
213
#else
214
215
		Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
			newrdn.bv_val, 0, 0 );
216
217
#endif

218
219
220
221
222
		send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
		    "invalid RDN", NULL, NULL );
		goto cleanup;
	}

223
	if( ndn->bv_len == 0 ) {
224
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
225
226
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
			   "do_modrdn:  attempt to modify root DSE.\n" ));
227
#else
228
		Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
229
230
#endif

231
232
233
		send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
			NULL, "cannot rename the root DSE", NULL, NULL );
		goto cleanup;
234
235

#ifdef SLAPD_SCHEMA_DN
236
	} else if ( strcasecmp( ndn->bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
237
238
239
240
241
242
243
244
245
246
247
#ifdef NEW_LOGGING
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
			"do_modrdn: attempt to modify subschema subentry\n" ));
#else
		Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry!\n", 0, 0, 0 );
#endif

		send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
			NULL, "cannot rename subschema subentry", NULL, NULL );
		goto cleanup;
#endif
248
249
	}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
250
	Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
251
	    op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
252

253
254
	manageDSAit = get_manageDSAit( op );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
255
256
257
258
259
	/*
	 * We could be serving multiple database backends.  Select the
	 * appropriate one, or send a referral to our "referral server"
	 * if we don't hold it.
	 */
260
	if ( (be = select_backend( ndn->bv_val, manageDSAit, 0 )) == NULL ) {
261
		struct berval **ref = referral_rewrite( default_referral,
262
			NULL, dn.bv_val, LDAP_SCOPE_DEFAULT );
263

264
		send_ldap_result( conn, op, rc = LDAP_REFERRAL,
265
266
267
			NULL, NULL, ref ? ref : default_referral, NULL );

		ber_bvecfree( ref );
Howard Chu's avatar
Howard Chu committed
268
		goto cleanup;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
269
270
	}

271
272
	/* check restrictions */
	rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
273
274
	if( rc != LDAP_SUCCESS ) {
		send_ldap_result( conn, op, rc,
275
			NULL, text, NULL, NULL );
276
		goto cleanup;
277
278
	}

279
	/* check for referrals */
280
	rc = backend_check_referrals( be, conn, op, dn.bv_val, ndn->bv_val );
281
282
283
284
	if ( rc != LDAP_SUCCESS ) {
		goto cleanup;
	}

285
286
287
	/* Make sure that the entry being changed and the newSuperior are in 
	 * the same backend, otherwise we return an error.
	 */
288
	if( newSuperior.bv_len ) {
289
		newSuperior_be = select_backend( nnewSuperior, 0, 0 );
290

291
		if ( newSuperior_be != be ) {
292
293
			/* newSuperior is in same backend */
			rc = LDAP_AFFECTS_MULTIPLE_DSAS;
294

295
			send_ldap_result( conn, op, rc,
296
				NULL, "cannot rename between DSAa", NULL, NULL );
297

Howard Chu's avatar
Howard Chu committed
298
			goto cleanup;
299
		}
300
	}
301

Kurt Zeilenga's avatar
Kurt Zeilenga committed
302
	/* deref suffix alias if appropriate */
303
	suffix_alias( be, ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304

Kurt Zeilenga's avatar
Kurt Zeilenga committed
305
306
307
308
	/*
	 * do the add if 1 && (2 || 3)
	 * 1) there is an add function implemented in this backend;
	 * 2) this backend is master for what it holds;
309
	 * 3) it's a replica and the dn supplied is the update_ndn.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
310
	 */
311
	if ( be->be_modrdn ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
312
		/* do the update here */
313
		int repl_user = be_isupdate( be, op->o_ndn.bv_val );
314
#ifndef SLAPD_MULTIMASTER
Pierangelo Masarati's avatar
Pierangelo Masarati committed
315
		if ( be->be_update_ndn == NULL || repl_user )
316
#endif
317
		{
318
			if ( (*be->be_modrdn)( be, conn, op, dn.bv_val, ndn->bv_val,
319
				newrdn.bv_val, deloldrdn, newSuperior.bv_val ) == 0
320
#ifdef SLAPD_MULTIMASTER
Pierangelo Masarati's avatar
Pierangelo Masarati committed
321
				&& ( be->be_update_ndn == NULL || !repl_user )
322
323
#endif
			) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
324
				struct replog_moddn moddn;
325
				moddn.newrdn = newrdn.bv_val;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
326
				moddn.deloldrdn = deloldrdn;
327
				moddn.newsup = newSuperior.bv_val;
328

329
				replog( be, op, dn.bv_val, ndn->bv_val, &moddn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
330
			}
331
#ifndef SLAPD_MULTIMASTER
Kurt Zeilenga's avatar
Kurt Zeilenga committed
332
		} else {
333
334
335
			struct berval **defref = be->be_update_refs
				? be->be_update_refs : default_referral;
			struct berval **ref = referral_rewrite( defref,
336
				NULL, dn.bv_val, LDAP_SCOPE_DEFAULT );
337

338
			send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
339
340
341
				ref ? ref : defref, NULL );

			ber_bvecfree( ref );
342
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
343
344
		}
	} else {
345
		send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
346
347
			NULL, "operation not supported within namingContext",
			NULL, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
349
	}

Howard Chu's avatar
Howard Chu committed
350
cleanup:
351
352
353
354
	free( dn.bv_val );
	free( newrdn.bv_val );	
	free( newSuperior.bv_val );

355
	if( ndn != NULL ) ber_bvfree( ndn );
356
	if ( nnewSuperior != NULL ) free( nnewSuperior );
357
	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
358
}