]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
a1d0d25b3cdc8532c565ae26a4d3ea551c9843d0
[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
228         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
229             op->o_connid, op->o_opid, dn, 0, 0 );
230
231         manageDSAit = get_manageDSAit( op );
232
233         /*
234          * We could be serving multiple database backends.  Select the
235          * appropriate one, or send a referral to our "referral server"
236          * if we don't hold it.
237          */
238         if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
239                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
240                         NULL, NULL, default_referral, NULL );
241                 goto cleanup;
242         }
243
244         /* check restrictions */
245         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
246         if( rc != LDAP_SUCCESS ) {
247                 send_ldap_result( conn, op, rc,
248                         NULL, text, NULL, NULL );
249                 goto cleanup;
250         }
251
252         /* check for referrals */
253         rc = backend_check_referrals( be, conn, op, dn, ndn );
254         if ( rc != LDAP_SUCCESS ) {
255                 goto cleanup;
256         }
257
258         /* Make sure that the entry being changed and the newSuperior are in 
259          * the same backend, otherwise we return an error.
260          */
261         if( newSuperior != NULL ) {
262                 newSuperior_be = select_backend( nnewSuperior, 0 );
263
264                 if ( newSuperior_be != be ) {
265                         /* newSuperior is in same backend */
266                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
267
268                         send_ldap_result( conn, op, rc,
269                                 NULL, "cannot rename between DSAa", NULL, NULL );
270
271                         goto cleanup;
272                 }
273
274                 /* deref suffix alias if appropriate */
275                 nnewSuperior = suffix_alias( be, nnewSuperior );
276         }
277
278         /* deref suffix alias if appropriate */
279         ndn = suffix_alias( be, ndn );
280
281         /*
282          * do the add if 1 && (2 || 3)
283          * 1) there is an add function implemented in this backend;
284          * 2) this backend is master for what it holds;
285          * 3) it's a replica and the dn supplied is the update_ndn.
286          */
287         if ( be->be_modrdn ) {
288                 /* do the update here */
289 #ifndef SLAPD_MULTIMASTER
290                 if ( be->be_update_ndn == NULL ||
291                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
292 #endif
293                 {
294                         if ( (*be->be_modrdn)( be, conn, op, dn, ndn, newrdn,
295                             deloldrdn, newSuperior ) == 0
296 #ifdef SLAPD_MULTIMASTER
297                                 && ( be->be_update_ndn == NULL ||
298                                         strcmp( be->be_update_ndn, op->o_ndn ) )
299 #endif
300                         ) {
301                                 struct replog_moddn moddn;
302                                 moddn.newrdn = newrdn;
303                                 moddn.deloldrdn = deloldrdn;
304                                 moddn.newsup = newSuperior;
305
306                                 replog( be, op, dn, &moddn );
307                         }
308 #ifndef SLAPD_MULTIMASTER
309                 } else {
310                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
311                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
312 #endif
313                 }
314         } else {
315                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
316                         NULL, "operation not supported within namingContext", NULL, NULL );
317         }
318
319 cleanup:
320         free( dn );
321         if( ndn != NULL ) free( ndn );
322         free( newrdn ); 
323         if ( newSuperior != NULL )
324                 free( newSuperior );
325         if ( nnewSuperior != NULL )
326                 free( nnewSuperior );
327         return rc;
328 }