]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
More dnPrettyNormal
[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         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 };
55
56         struct berval ndn = { 0, NULL };
57         struct berval nnewrdn = { 0, NULL };
58         struct berval nnewSuperior = { 0, 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, "{oob", &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, "o", &newSuperior ) 
124                      == LBER_ERROR ) {
125
126 #ifdef NEW_LOGGING
127                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
128                                 "do_modrdn: ber_scanf(\"a\") failed\n" ));
129 #else
130                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") 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         }
140
141 #ifdef NEW_LOGGING
142         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
143                 "do_modrdn: dn (%s) newrdn (%s) newsuperior(%s)\n",
144                 dn.bv_val, newrdn.bv_val,
145                 newSuperior.bv_len ? newSuperior.bv_val : "" ));
146 #else
147         Debug( LDAP_DEBUG_ARGS,
148             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
149                 dn.bv_val, newrdn.bv_val,
150                 newSuperior.bv_len ? newSuperior.bv_val : "" );
151 #endif
152
153         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
154 #ifdef NEW_LOGGING
155                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
156                         "do_modrdn: ber_scanf failed\n" ));
157 #else
158                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
159 #endif
160
161                 send_ldap_disconnect( conn, op,
162                         LDAP_PROTOCOL_ERROR, "decoding error" );
163                 rc = SLAPD_DISCONNECT;
164                 goto cleanup;
165         }
166
167         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
168 #ifdef NEW_LOGGING
169                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
170                         "do_modrdn: get_ctrls failed\n" ));
171 #else
172                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
173 #endif
174
175                 /* get_ctrls has sent results.  Now clean up. */
176                 goto cleanup;
177         } 
178
179         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
180         if( rc != LDAP_SUCCESS ) {
181 #ifdef NEW_LOGGING
182                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
183                         "do_modrdn: conn %d  invalid dn (%s)\n",
184                         conn->c_connid, dn.bv_val ));
185 #else
186                 Debug( LDAP_DEBUG_ANY,
187                         "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
188 #endif
189                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
190                     "invalid DN", NULL, NULL );
191                 goto cleanup;
192         }
193
194         if( ndn.bv_len == 0 ) {
195 #ifdef NEW_LOGGING
196                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
197                            "do_modrdn:  attempt to modify root DSE.\n" ));
198 #else
199                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
200 #endif
201
202                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
203                         NULL, "cannot rename the root DSE", NULL, NULL );
204                 goto cleanup;
205
206 #ifdef SLAPD_SCHEMA_DN
207         } else if ( strcasecmp( ndn.bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
208 #ifdef NEW_LOGGING
209                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
210                         "do_modrdn: attempt to modify subschema subentry\n" ));
211 #else
212                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry!\n", 0, 0, 0 );
213 #endif
214
215                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
216                         NULL, "cannot rename subschema subentry", NULL, NULL );
217                 goto cleanup;
218 #endif
219         }
220
221         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
222
223         rc = dnPrettyNormal( NULL, &newrdn, &pnewrdn, &nnewrdn );
224         if( rc != LDAP_SUCCESS ) {
225 #ifdef NEW_LOGGING
226                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
227                         "do_modrdn: conn %d  invalid newrdn (%s)\n",
228                         conn->c_connid, newrdn.bv_val ));
229 #else
230                 Debug( LDAP_DEBUG_ANY,
231                         "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
232 #endif
233                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
234                     "invalid new RDN", NULL, NULL );
235                 goto cleanup;
236         }
237
238         if( rdnValidate( &pnewrdn ) != LDAP_SUCCESS ) {
239 #ifdef NEW_LOGGING
240                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
241                         "do_modrdn: invalid rdn (%s).\n", pnewrdn.bv_val ));
242 #else
243                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
244                         pnewrdn.bv_val, 0, 0 );
245 #endif
246
247                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
248                     "invalid new RDN", NULL, NULL );
249                 goto cleanup;
250         }
251
252         if( newSuperior.bv_len ) {
253                 rc = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
254                         &nnewSuperior );
255                 if( rc != LDAP_SUCCESS ) {
256 #ifdef NEW_LOGGING
257                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
258                                 "do_modrdn: conn %d  invalid newSuperior (%s)\n",
259                                 conn->c_connid, newSuperior.bv_val ));
260 #else
261                         Debug( LDAP_DEBUG_ANY,
262                                 "do_modrdn: invalid newSuperior (%s)\n",
263                                 newSuperior.bv_val, 0, 0 );
264 #endif
265                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
266                                 "invalid newSuperior", NULL, NULL );
267                         goto cleanup;
268                 }
269         }
270
271         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
272             op->o_connid, op->o_opid, pdn.bv_val, 0, 0 );
273
274         manageDSAit = get_manageDSAit( op );
275
276         /*
277          * We could be serving multiple database backends.  Select the
278          * appropriate one, or send a referral to our "referral server"
279          * if we don't hold it.
280          */
281         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
282                 struct berval **ref = referral_rewrite( default_referral,
283                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
284
285                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
286                         NULL, NULL, ref ? ref : default_referral, NULL );
287
288                 ber_bvecfree( ref );
289                 goto cleanup;
290         }
291
292         /* check restrictions */
293         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
294         if( rc != LDAP_SUCCESS ) {
295                 send_ldap_result( conn, op, rc,
296                         NULL, text, NULL, NULL );
297                 goto cleanup;
298         }
299
300         /* check for referrals */
301         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
302         if ( rc != LDAP_SUCCESS ) {
303                 goto cleanup;
304         }
305
306         /* Make sure that the entry being changed and the newSuperior are in 
307          * the same backend, otherwise we return an error.
308          */
309         if( newSuperior.bv_len ) {
310                 newSuperior_be = select_backend( &nnewSuperior, 0, 0 );
311
312                 if ( newSuperior_be != be ) {
313                         /* newSuperior is in same backend */
314                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
315
316                         send_ldap_result( conn, op, rc,
317                                 NULL, "cannot rename between DSAa", NULL, NULL );
318
319                         goto cleanup;
320                 }
321
322                 /* deref suffix alias if appropriate */
323                 suffix_alias( be, &nnewSuperior );
324         }
325
326         /* deref suffix alias if appropriate */
327         suffix_alias( be, &ndn );
328
329         /*
330          * do the add if 1 && (2 || 3)
331          * 1) there is an add function implemented in this backend;
332          * 2) this backend is master for what it holds;
333          * 3) it's a replica and the dn supplied is the update_ndn.
334          */
335         if ( be->be_modrdn ) {
336                 /* do the update here */
337                 int repl_user = be_isupdate( be, &op->o_ndn );
338 #ifndef SLAPD_MULTIMASTER
339                 if ( !be->be_update_ndn.bv_len || repl_user )
340 #endif
341                 {
342                         if ( (*be->be_modrdn)( be, conn, op, &pdn, &ndn,
343                                 &pnewrdn, &nnewrdn, deloldrdn,
344                                 &pnewSuperior, &nnewSuperior ) == 0
345 #ifdef SLAPD_MULTIMASTER
346                                 && ( !be->be_update_ndn.bv_len || !repl_user )
347 #endif
348                         ) {
349                                 struct slap_replog_moddn moddn;
350                                 moddn.newrdn = &pnewrdn;
351                                 moddn.deloldrdn = deloldrdn;
352                                 moddn.newsup = &pnewSuperior;
353
354                                 replog( be, op, &pdn, &ndn, &moddn );
355                         }
356 #ifndef SLAPD_MULTIMASTER
357                 } else {
358                         struct berval **defref = be->be_update_refs
359                                 ? be->be_update_refs : default_referral;
360                         struct berval **ref = referral_rewrite( defref,
361                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
362
363                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
364                                 ref ? ref : defref, NULL );
365
366                         ber_bvecfree( ref );
367 #endif
368                 }
369         } else {
370                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
371                         NULL, "operation not supported within namingContext",
372                         NULL, NULL );
373         }
374
375 cleanup:
376         free( dn.bv_val );
377         free( pdn.bv_val );
378         free( ndn.bv_val );
379
380         free( newrdn.bv_val );  
381         free( pnewrdn.bv_val ); 
382         free( nnewrdn.bv_val ); 
383
384         free( newSuperior.bv_val );
385         free( pnewSuperior.bv_val );
386         free( nnewSuperior.bv_val );
387
388         return rc;
389 }