modrdn.c 7.08 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
)
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
47
	char	*dn, *ndn = NULL, *newrdn;
48
	ber_int_t	deloldrdn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
49
	Backend	*be;
50
51
52
53
	/* Vars for LDAP v3 newSuperior support */
	char	*newSuperior = NULL;
	char    *nnewSuperior = NULL;
	Backend	*newSuperior_be = NULL;
54
	ber_len_t	length;
55
	int rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
56
	const char *text;
57
	struct berval **urls = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
58
59
60
61
62
63
64
65
66

	Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );

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

72
	if ( ber_scanf( op->o_ber, "{aab", &dn, &newrdn, &deloldrdn )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
73
74
	    == LBER_ERROR ) {
		Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
75
76
		send_ldap_disconnect( conn, op,
			LDAP_PROTOCOL_ERROR, "decoding error" );
77
		return SLAPD_DISCONNECT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
78
79
	}

80
81
82
	/* 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
83
		if ( op->o_protocol < LDAP_VERSION3 ) {
84
85
86
87
			/* Conection record indicates v2 but field 
			 * newSuperior is present: report error.
			 */
			Debug( LDAP_DEBUG_ANY,
88
			       "modrdn(v2): invalid field newSuperior!\n",
89
			       0, 0, 0 );
90
91
			send_ldap_disconnect( conn, op,
				LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
92
			rc = SLAPD_DISCONNECT;
Howard Chu's avatar
Howard Chu committed
93
			goto cleanup;
94
		}
95

96
		if ( ber_scanf( op->o_ber, "a", &newSuperior ) 
97
98
		     == LBER_ERROR ) {

Howard Chu's avatar
Howard Chu committed
99
			Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
100
			   0, 0, 0 );
101
102
			send_ldap_disconnect( conn, op,
				LDAP_PROTOCOL_ERROR, "decoding error" );
103
			rc = SLAPD_DISCONNECT;
Howard Chu's avatar
Howard Chu committed
104
			goto cleanup;
105
106
107
		}

		nnewSuperior = ch_strdup( newSuperior );
108

109
		if( dn_normalize( nnewSuperior ) == NULL ) {
110
111
112
			Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
				newSuperior, 0, 0 );
			send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
113
				"invalid new superior DN", NULL, NULL );
Howard Chu's avatar
Howard Chu committed
114
			goto cleanup;
115
		}
116

117
	}
118

119
120
	Debug( LDAP_DEBUG_ARGS,
	    "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
121
		dn, newrdn,
122
		newSuperior != NULL ? newSuperior : "" );
123

124
125
	if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
		Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
126
127
		send_ldap_disconnect( conn, op,
				LDAP_PROTOCOL_ERROR, "decoding error" );
128
		rc = SLAPD_DISCONNECT;
Howard Chu's avatar
Howard Chu committed
129
		goto cleanup;
130
131
	}

132
	if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
133
		Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
134
		/* get_ctrls has sent results.  Now clean up. */
Howard Chu's avatar
Howard Chu committed
135
		goto cleanup;
136
137
	} 

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	ndn = ch_strdup( dn );

	if( dn_normalize( ndn ) == NULL ) {
		Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n", dn, 0, 0 );
		send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
		    "invalid DN", NULL, NULL );
		goto cleanup;
	}

	if( !rdn_validate( newrdn ) ) {
		Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n", newrdn, 0, 0 );
		send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
		    "invalid RDN", NULL, NULL );
		goto cleanup;
	}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
154
	Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
155
	    op->o_connid, op->o_opid, dn, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156
157
158
159
160
161
162

	/*
	 * 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.
	 */

163
	if ( (be = select_backend( ndn )) == NULL ) {
164
165
		send_ldap_result( conn, op, rc = LDAP_REFERRAL,
			NULL, NULL, default_referral, NULL );
Howard Chu's avatar
Howard Chu committed
166
		goto cleanup;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
167
168
	}

169
	/* make sure this backend recongizes critical controls */
170
	rc = backend_check_controls( be, conn, op, &text ) ;
171
172
	if( rc != LDAP_SUCCESS ) {
		send_ldap_result( conn, op, rc,
173
			NULL, text, NULL, NULL );
174
		goto cleanup;
175
176
	}

177
178
179
180
181
182
183
184
185
	/* check for referrals */
	rc = backend_check_referrals( be, conn, op, &urls, &text );
	if ( rc != LDAP_SUCCESS ) {
		send_ldap_result( conn, op, rc,
			NULL, text, urls, NULL );
		ber_bvecfree( urls );
		goto cleanup;
	}

186
187
188
189
190
	if ( global_readonly || be->be_readonly ) {
		Debug( LDAP_DEBUG_ANY, "do_modrdn: database is read-only\n",
		       0, 0, 0 );
		send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
		                  NULL, "database is read-only", NULL, NULL );
Howard Chu's avatar
Howard Chu committed
191
		goto cleanup;
192
193
	}

194
195
196
	/* Make sure that the entry being changed and the newSuperior are in 
	 * the same backend, otherwise we return an error.
	 */
197
198
	if( newSuperior != NULL ) {
		newSuperior_be = select_backend( nnewSuperior );
199

200
		if ( newSuperior_be != be ) {
201
202
			/* newSuperior is in same backend */
			rc = LDAP_AFFECTS_MULTIPLE_DSAS;
203

204
			send_ldap_result( conn, op, rc,
205
				NULL, "cannot rename between DSAa", NULL, NULL );
206

Howard Chu's avatar
Howard Chu committed
207
			goto cleanup;
208
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
209
210
211

		/* deref suffix alias if appropriate */
		nnewSuperior = suffix_alias( be, nnewSuperior );
212
	}
213

Kurt Zeilenga's avatar
Kurt Zeilenga committed
214
215
216
	/* deref suffix alias if appropriate */
	ndn = suffix_alias( be, ndn );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
217
218
219
220
	/*
	 * 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;
221
	 * 3) it's a replica and the dn supplied is the update_ndn.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
222
	 */
223
	if ( be->be_modrdn ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
224
		/* do the update here */
225
#ifndef SLAPD_MULTIMASTER
226
227
		if ( be->be_update_ndn == NULL ||
			strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
228
#endif
229
		{
230
			if ( (*be->be_modrdn)( be, conn, op, dn, ndn, newrdn,
231
232
233
234
235
236
			    deloldrdn, newSuperior ) == 0
#ifdef SLAPD_MULTIMASTER
				&& ( be->be_update_ndn == NULL ||
					strcmp( be->be_update_ndn, op->o_ndn ) )
#endif
			) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
237
				struct replog_moddn moddn;
238
			   	moddn.newrdn = newrdn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
240
				moddn.deloldrdn = deloldrdn;
				moddn.newsup = newSuperior;
241

242
				replog( be, op, dn, &moddn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
243
			}
244
#ifndef SLAPD_MULTIMASTER
Kurt Zeilenga's avatar
Kurt Zeilenga committed
245
		} else {
246
247
			send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
				be->be_update_refs ? be->be_update_refs : default_referral, NULL );
248
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
249
250
		}
	} else {
251
		send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
252
			NULL, "operation not supported within namingContext", NULL, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
253
254
	}

Howard Chu's avatar
Howard Chu committed
255
cleanup:
256
	free( dn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
257
	if( ndn != NULL ) free( ndn );
258
	free( newrdn );	
Howard Chu's avatar
Howard Chu committed
259
260
261
262
	if ( newSuperior != NULL )
		free( newSuperior );
	if ( nnewSuperior != NULL )
		free( nnewSuperior );
263
	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
264
}