]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Missed a test in AttributeDescription commit
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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         char    *dn, *ndn = NULL, *newrdn;
48         ber_int_t       deloldrdn;
49         Backend *be;
50         /* Vars for LDAP v3 newSuperior support */
51         char    *newSuperior = NULL;
52         char    *nnewSuperior = NULL;
53         Backend *newSuperior_be = NULL;
54         ber_len_t       length;
55         int rc;
56         const char *text;
57         int manageDSAit;
58
59 #ifdef NEW_LOGGING
60         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
61                    "do_modrdn: begin\n" ));
62 #else
63         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
64 #endif
65
66
67         /*
68          * Parse the modrdn request.  It looks like this:
69          *
70          *      ModifyRDNRequest := SEQUENCE {
71          *              entry   DistinguishedName,
72          *              newrdn  RelativeDistinguishedName
73          *              deleteoldrdn    BOOLEAN,
74          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
75          *      }
76          */
77
78         if ( ber_scanf( op->o_ber, "{aab", &dn, &newrdn, &deloldrdn )
79             == LBER_ERROR ) {
80 #ifdef NEW_LOGGING
81                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
82                            "do_modrdn: ber_scanf failed\n" ));
83 #else
84                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
85 #endif
86
87                 send_ldap_disconnect( conn, op,
88                         LDAP_PROTOCOL_ERROR, "decoding error" );
89                 return SLAPD_DISCONNECT;
90         }
91
92         /* Check for newSuperior parameter, if present scan it */
93
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.
98                          */
99 #ifdef NEW_LOGGING
100                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
101                                    "do_modrdn: (v2) invalid field newSuperior.\n" ));
102 #else
103                         Debug( LDAP_DEBUG_ANY,
104                                "modrdn(v2): invalid field newSuperior!\n",
105                                0, 0, 0 );
106 #endif
107
108                         send_ldap_disconnect( conn, op,
109                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
110                         rc = SLAPD_DISCONNECT;
111                         goto cleanup;
112                 }
113
114                 if ( ber_scanf( op->o_ber, "a", &newSuperior ) 
115                      == LBER_ERROR ) {
116
117 #ifdef NEW_LOGGING
118                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
119                                    "do_modrdn: ber_scanf(\"a\") failed\n" ));
120 #else
121                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
122                            0, 0, 0 );
123 #endif
124
125                         send_ldap_disconnect( conn, op,
126                                 LDAP_PROTOCOL_ERROR, "decoding error" );
127                         rc = SLAPD_DISCONNECT;
128                         goto cleanup;
129                 }
130
131                 nnewSuperior = ch_strdup( newSuperior );
132
133                 if( dn_normalize( nnewSuperior ) == NULL ) {
134 #ifdef NEW_LOGGING
135                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
136                                    "do_modrdn:  invalid new superior (%s)\n", newSuperior ));
137 #else
138                         Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
139                                 newSuperior, 0, 0 );
140 #endif
141
142                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
143                                 "invalid new superior DN", NULL, NULL );
144                         goto cleanup;
145                 }
146
147         }
148
149 #ifdef NEW_LOGGING
150         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
151                    "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
152                    dn, newrdn, newSuperior != NULL ? newSuperior : "" ));
153 #else
154         Debug( LDAP_DEBUG_ARGS,
155             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
156                 dn, newrdn,
157                 newSuperior != NULL ? newSuperior : "" );
158 #endif
159
160
161         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
162 #ifdef NEW_LOGGING
163                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
164                            "do_modrdn: ber_scanf failed\n" ));
165 #else
166                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
167 #endif
168
169                 send_ldap_disconnect( conn, op,
170                                 LDAP_PROTOCOL_ERROR, "decoding error" );
171                 rc = SLAPD_DISCONNECT;
172                 goto cleanup;
173         }
174
175         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
176 #ifdef NEW_LOGGING
177                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
178                            "do_modrdn: get_ctrls failed\n" ));
179 #else
180                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
181 #endif
182
183                 /* get_ctrls has sent results.  Now clean up. */
184                 goto cleanup;
185         } 
186
187         ndn = ch_strdup( dn );
188
189         if( dn_normalize( ndn ) == NULL ) {
190 #ifdef NEW_LOGGING
191                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
192                            "do_modrdn: invalid dn (%s)\n", dn ));
193 #else
194                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n", dn, 0, 0 );
195 #endif
196
197                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
198                     "invalid DN", NULL, NULL );
199                 goto cleanup;
200         }
201
202         if( !rdn_validate( newrdn ) ) {
203 #ifdef NEW_LOGGING
204                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
205                            "do_modrdn: invalid rdn (%s).\n", newrdn ));
206 #else
207                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n", newrdn, 0, 0 );
208 #endif
209
210                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
211                     "invalid RDN", NULL, NULL );
212                 goto cleanup;
213         }
214
215         if( *ndn == '\0' ) {
216 #ifdef NEW_LOGGING
217                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
218                            "do_modrdn:  attempt to modify root DSE.\n" ));
219 #else
220                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
221 #endif
222
223                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
224                         NULL, "cannot rename the root DSE", NULL, NULL );
225                 goto cleanup;
226
227 #ifdef SLAPD_SCHEMA_DN
228         } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
229 #ifdef NEW_LOGGING
230                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
231                         "do_modrdn: attempt to modify subschema subentry\n" ));
232 #else
233                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry!\n", 0, 0, 0 );
234 #endif
235
236                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
237                         NULL, "cannot rename subschema subentry", NULL, NULL );
238                 goto cleanup;
239 #endif
240         }
241
242         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
243             op->o_connid, op->o_opid, dn, 0, 0 );
244
245         manageDSAit = get_manageDSAit( op );
246
247         /*
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.
251          */
252         if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
253                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
254                         NULL, NULL, default_referral, NULL );
255                 goto cleanup;
256         }
257
258         /* check restrictions */
259         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
260         if( rc != LDAP_SUCCESS ) {
261                 send_ldap_result( conn, op, rc,
262                         NULL, text, NULL, NULL );
263                 goto cleanup;
264         }
265
266         /* check for referrals */
267         rc = backend_check_referrals( be, conn, op, dn, ndn );
268         if ( rc != LDAP_SUCCESS ) {
269                 goto cleanup;
270         }
271
272         /* Make sure that the entry being changed and the newSuperior are in 
273          * the same backend, otherwise we return an error.
274          */
275         if( newSuperior != NULL ) {
276                 newSuperior_be = select_backend( nnewSuperior, 0 );
277
278                 if ( newSuperior_be != be ) {
279                         /* newSuperior is in same backend */
280                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
281
282                         send_ldap_result( conn, op, rc,
283                                 NULL, "cannot rename between DSAa", NULL, NULL );
284
285                         goto cleanup;
286                 }
287
288                 /* deref suffix alias if appropriate */
289                 nnewSuperior = suffix_alias( be, nnewSuperior );
290         }
291
292         /* deref suffix alias if appropriate */
293         ndn = suffix_alias( be, ndn );
294
295         /*
296          * do the add if 1 && (2 || 3)
297          * 1) there is an add function implemented in this backend;
298          * 2) this backend is master for what it holds;
299          * 3) it's a replica and the dn supplied is the update_ndn.
300          */
301         if ( be->be_modrdn ) {
302                 /* do the update here */
303 #ifndef SLAPD_MULTIMASTER
304                 if ( be->be_update_ndn == NULL ||
305                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
306 #endif
307                 {
308                         if ( (*be->be_modrdn)( be, conn, op, dn, ndn, newrdn,
309                             deloldrdn, newSuperior ) == 0
310 #ifdef SLAPD_MULTIMASTER
311                                 && ( be->be_update_ndn == NULL ||
312                                         strcmp( be->be_update_ndn, op->o_ndn ) )
313 #endif
314                         ) {
315                                 struct replog_moddn moddn;
316                                 moddn.newrdn = newrdn;
317                                 moddn.deloldrdn = deloldrdn;
318                                 moddn.newsup = newSuperior;
319
320                                 replog( be, op, dn, &moddn );
321                         }
322 #ifndef SLAPD_MULTIMASTER
323                 } else {
324                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
325                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
326 #endif
327                 }
328         } else {
329                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
330                         NULL, "operation not supported within namingContext", NULL, NULL );
331         }
332
333 cleanup:
334         free( dn );
335         if( ndn != NULL ) free( ndn );
336         free( newrdn ); 
337         if ( newSuperior != NULL )
338                 free( newSuperior );
339         if ( nnewSuperior != NULL )
340                 free( nnewSuperior );
341         return rc;
342 }