]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Suck in HEAD changes since 2.1alpha
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
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.
16  */
17
18 /*
19  * LDAP v3 newSuperior support.
20  *
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.
24  *
25  * Redistribution and use in source and binary forms are permitted
26  * without restriction or fee of any kind as long as this notice
27  * is preserved.
28  *
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/socket.h>
36 #include <ac/string.h>
37
38 #include "ldap_pvt.h"
39 #include "slap.h"
40
41 int
42 do_modrdn(
43     Connection  *conn,
44     Operation   *op
45 )
46 {
47         struct berval dn = { 0, NULL };
48         struct berval newrdn = { 0, NULL };
49         struct berval newSuperior = { 0, NULL };
50         ber_int_t       deloldrdn;
51
52         struct berval pdn = { 0, NULL };
53         struct berval pnewrdn = { 0, NULL };
54         struct berval pnewSuperior = { 0, NULL }, *pnewS = NULL;
55
56         struct berval ndn = { 0, NULL };
57         struct berval nnewrdn = { 0, NULL };
58         struct berval nnewSuperior = { 0, NULL }, *nnewS = NULL;
59
60         Backend *be;
61         Backend *newSuperior_be = NULL;
62         ber_len_t       length;
63         int rc;
64         const char *text;
65         int manageDSAit;
66
67 #ifdef NEW_LOGGING
68         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
69                 "do_modrdn: begin\n" ));
70 #else
71         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
72 #endif
73
74
75         /*
76          * Parse the modrdn request.  It looks like this:
77          *
78          *      ModifyRDNRequest := SEQUENCE {
79          *              entry   DistinguishedName,
80          *              newrdn  RelativeDistinguishedName
81          *              deleteoldrdn    BOOLEAN,
82          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
83          *      }
84          */
85
86         if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
87             == LBER_ERROR )
88         {
89 #ifdef NEW_LOGGING
90                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
91                         "do_modrdn: ber_scanf failed\n" ));
92 #else
93                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
94 #endif
95
96                 send_ldap_disconnect( conn, op,
97                         LDAP_PROTOCOL_ERROR, "decoding error" );
98                 return SLAPD_DISCONNECT;
99         }
100
101         /* Check for newSuperior parameter, if present scan it */
102
103         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
104                 if ( op->o_protocol < LDAP_VERSION3 ) {
105                         /* Conection record indicates v2 but field 
106                          * newSuperior is present: report error.
107                          */
108 #ifdef NEW_LOGGING
109                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
110                                 "do_modrdn: (v2) invalid field newSuperior.\n" ));
111 #else
112                         Debug( LDAP_DEBUG_ANY,
113                             "modrdn(v2): invalid field newSuperior!\n",
114                             0, 0, 0 );
115 #endif
116
117                         send_ldap_disconnect( conn, op,
118                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
119                         rc = SLAPD_DISCONNECT;
120                         goto cleanup;
121                 }
122
123                 if ( ber_scanf( op->o_ber, "m", &newSuperior ) 
124                      == LBER_ERROR ) {
125
126 #ifdef NEW_LOGGING
127                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
128                                 "do_modrdn: ber_scanf(\"m\") failed\n" ));
129 #else
130                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
131                                 0, 0, 0 );
132 #endif
133
134                         send_ldap_disconnect( conn, op,
135                                 LDAP_PROTOCOL_ERROR, "decoding error" );
136                         rc = SLAPD_DISCONNECT;
137                         goto cleanup;
138                 }
139                 pnewS = &pnewSuperior;
140                 nnewS = &nnewSuperior;
141         }
142
143 #ifdef NEW_LOGGING
144         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
145                 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
146                 dn.bv_val, newrdn.bv_val,
147                 newSuperior.bv_len ? newSuperior.bv_val : "" ));
148 #else
149         Debug( LDAP_DEBUG_ARGS,
150             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
151                 dn.bv_val, newrdn.bv_val,
152                 newSuperior.bv_len ? newSuperior.bv_val : "" );
153 #endif
154
155         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
158                         "do_modrdn: ber_scanf failed\n" ));
159 #else
160                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
161 #endif
162
163                 send_ldap_disconnect( conn, op,
164                         LDAP_PROTOCOL_ERROR, "decoding error" );
165                 rc = SLAPD_DISCONNECT;
166                 goto cleanup;
167         }
168
169         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
170 #ifdef NEW_LOGGING
171                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
172                         "do_modrdn: get_ctrls failed\n" ));
173 #else
174                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
175 #endif
176
177                 /* get_ctrls has sent results.  Now clean up. */
178                 goto cleanup;
179         } 
180
181         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
182         if( rc != LDAP_SUCCESS ) {
183 #ifdef NEW_LOGGING
184                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
185                         "do_modrdn: conn %d  invalid dn (%s)\n",
186                         conn->c_connid, dn.bv_val ));
187 #else
188                 Debug( LDAP_DEBUG_ANY,
189                         "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
190 #endif
191                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
192                     "invalid DN", NULL, NULL );
193                 goto cleanup;
194         }
195
196         if( ndn.bv_len == 0 ) {
197 #ifdef NEW_LOGGING
198                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
199                            "do_modrdn:  attempt to modify root DSE.\n" ));
200 #else
201                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
202 #endif
203
204                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
205                         NULL, "cannot rename the root DSE", NULL, NULL );
206                 goto cleanup;
207
208 #ifdef SLAPD_SCHEMA_DN
209         } else if ( strcasecmp( ndn.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
210 #ifdef NEW_LOGGING
211                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
212                         "do_modrdn: attempt to modify subschema subentry\n" ));
213 #else
214                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry!\n", 0, 0, 0 );
215 #endif
216
217                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
218                         NULL, "cannot rename subschema subentry", NULL, NULL );
219                 goto cleanup;
220 #endif
221         }
222
223         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
224
225         rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
226         if( rc != LDAP_SUCCESS ) {
227 #ifdef NEW_LOGGING
228                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
229                         "do_modrdn: conn %d  invalid newrdn (%s)\n",
230                         conn->c_connid, newrdn.bv_val ));
231 #else
232                 Debug( LDAP_DEBUG_ANY,
233                         "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
234 #endif
235                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
236                     "invalid new RDN", NULL, NULL );
237                 goto cleanup;
238         }
239
240         if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
241 #ifdef NEW_LOGGING
242                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
243                         "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val ));
244 #else
245                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
246                         pnewrdn.bv_val, 0, 0 );
247 #endif
248
249                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
250                     "invalid new RDN", NULL, NULL );
251                 goto cleanup;
252         }
253
254         if( pnewS ) {
255                 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
256                         &nnewSuperior );
257                 if( rc != LDAP_SUCCESS ) {
258 #ifdef NEW_LOGGING
259                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
260                                 "do_modrdn: conn %d  invalid newSuperior (%s)\n",
261                                 conn->c_connid, newSuperior.bv_val ));
262 #else
263                         Debug( LDAP_DEBUG_ANY,
264                                 "do_modrdn: invalid newSuperior (%s)\n",
265                                 newSuperior.bv_val, 0, 0 );
266 #endif
267                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
268                                 "invalid newSuperior", NULL, NULL );
269                         goto cleanup;
270                 }
271         }
272
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 );
275
276         manageDSAit = get_manageDSAit( op );
277
278         /*
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.
282          */
283         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
284                 BerVarray ref = referral_rewrite( default_referral,
285                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
286
287                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
288                         NULL, NULL, ref ? ref : default_referral, NULL );
289
290                 ber_bvarray_free( ref );
291                 goto cleanup;
292         }
293
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 );
299                 goto cleanup;
300         }
301
302         /* check for referrals */
303         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
304         if ( rc != LDAP_SUCCESS ) {
305                 goto cleanup;
306         }
307
308         /* Make sure that the entry being changed and the newSuperior are in 
309          * the same backend, otherwise we return an error.
310          */
311         if( pnewS ) {
312                 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
313
314                 if ( newSuperior_be != be ) {
315                         /* newSuperior is in same backend */
316                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
317
318                         send_ldap_result( conn, op, rc,
319                                 NULL, "cannot rename between DSAa", NULL, NULL );
320
321                         goto cleanup;
322                 }
323
324                 /* deref suffix alias if appropriate */
325                 suffix_alias( be, &nnewSuperior );
326         }
327
328         /* deref suffix alias if appropriate */
329         suffix_alias( be, &ndn );
330
331         /*
332          * do the add if 1 && (2 || 3)
333          * 1) there is an add function implemented in this backend;
334          * 2) this backend is master for what it holds;
335          * 3) it's a replica and the dn supplied is the update_ndn.
336          */
337         if ( be->be_modrdn ) {
338                 /* do the update here */
339                 int repl_user = be_isupdate( be, &op->o_ndn );
340 #ifndef SLAPD_MULTIMASTER
341                 if ( !be->be_update_ndn.bv_len || repl_user )
342 #endif
343                 {
344                         if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
345                                 &pnewrdn, &nnewrdn, deloldrdn,
346                                 pnewS, nnewS ) == 0
347 #ifdef SLAPD_MULTIMASTER
348                                 && ( !be->be_update_ndn.bv_len || !repl_user )
349 #endif
350                         ) {
351                                 struct slap_replog_moddn moddn;
352                                 moddn.newrdn = &pnewrdn;
353                                 moddn.deloldrdn = deloldrdn;
354                                 moddn.newsup = &pnewSuperior;
355
356                                 replog( be, op, &pdn, &ndn, &moddn );
357                         }
358 #ifndef SLAPD_MULTIMASTER
359                 } else {
360                         BerVarray defref = be->be_update_refs
361                                 ? be->be_update_refs : default_referral;
362                         BerVarray ref = referral_rewrite( defref,
363                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
364
365                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
366                                 ref ? ref : defref, NULL );
367
368                         ber_bvarray_free( ref );
369 #endif
370                 }
371         } else {
372                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
373                         NULL, "operation not supported within namingContext",
374                         NULL, NULL );
375         }
376
377 cleanup:
378         free( pdn.bv_val );
379         free( ndn.bv_val );
380
381         free( pnewrdn.bv_val ); 
382         free( nnewrdn.bv_val ); 
383
384         if ( pnewSuperior.bv_val ) free( pnewSuperior.bv_val );
385         if ( nnewSuperior.bv_val ) free( nnewSuperior.bv_val );
386
387         return rc;
388 }