]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Fix SEGV when no newSuperior is specified
[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 = NULL;
53         struct berval *pnewrdn = NULL;
54         struct berval *pnewSuperior = NULL;
55
56         struct berval *ndn = NULL;
57         struct berval *nnewrdn = NULL;
58         struct berval *nnewSuperior = 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 = dnPretty( NULL, &dn, &pdn );
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         rc = dnNormalize( NULL, &dn, &ndn );
195         if( rc != LDAP_SUCCESS ) {
196 #ifdef NEW_LOGGING
197                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
198                         "do_modrdn: conn %d  invalid dn (%s)\n",
199                         conn->c_connid, pdn->bv_val ));
200 #else
201                 Debug( LDAP_DEBUG_ANY,
202                         "do_modrdn: invalid dn (%s)\n", pdn->bv_val, 0, 0 );
203 #endif
204                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
205                     "invalid DN", NULL, NULL );
206                 goto cleanup;
207         }
208
209         if( ndn->bv_len == 0 ) {
210 #ifdef NEW_LOGGING
211                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
212                            "do_modrdn:  attempt to modify root DSE.\n" ));
213 #else
214                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
215 #endif
216
217                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
218                         NULL, "cannot rename the root DSE", NULL, NULL );
219                 goto cleanup;
220
221 #ifdef SLAPD_SCHEMA_DN
222         } else if ( strcasecmp( ndn->bv_val, SLAPD_SCHEMA_DN ) == 0 ) {
223 #ifdef NEW_LOGGING
224                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
225                         "do_modrdn: attempt to modify subschema subentry\n" ));
226 #else
227                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry!\n", 0, 0, 0 );
228 #endif
229
230                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
231                         NULL, "cannot rename subschema subentry", NULL, NULL );
232                 goto cleanup;
233 #endif
234         }
235
236         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
237
238         rc = dnPretty( NULL, &newrdn, &pnewrdn );
239         if( rc != LDAP_SUCCESS ) {
240 #ifdef NEW_LOGGING
241                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
242                         "do_modrdn: conn %d  invalid newrdn (%s)\n",
243                         conn->c_connid, newrdn.bv_val ));
244 #else
245                 Debug( LDAP_DEBUG_ANY,
246                         "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
247 #endif
248                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
249                     "invalid new RDN", NULL, NULL );
250                 goto cleanup;
251         }
252
253         rc = dnNormalize( NULL, &newrdn, &nnewrdn );
254         if( rc != LDAP_SUCCESS ) {
255 #ifdef NEW_LOGGING
256                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
257                         "do_modrdn: conn %d  invalid newrdn (%s)\n",
258                         conn->c_connid, pnewrdn->bv_val ));
259 #else
260                 Debug( LDAP_DEBUG_ANY,
261                         "do_modrdn: invalid newrdn (%s)\n", pnewrdn->bv_val, 0, 0 );
262 #endif
263                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
264                     "invalid new RDN", NULL, NULL );
265                 goto cleanup;
266         }
267
268         if( !nnewrdn->bv_len || !rdn_validate( pnewrdn->bv_val ) ) {
269 #ifdef NEW_LOGGING
270                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
271                         "do_modrdn: invalid rdn (%s).\n", pnewrdn->bv_val ));
272 #else
273                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
274                         pnewrdn->bv_val, 0, 0 );
275 #endif
276
277                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
278                     "invalid new RDN", NULL, NULL );
279                 goto cleanup;
280         }
281
282         if( newSuperior.bv_len ) {
283                 rc = dnPretty( NULL, &newSuperior, &pnewSuperior );
284                 if( rc != LDAP_SUCCESS ) {
285 #ifdef NEW_LOGGING
286                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
287                                 "do_modrdn: conn %d  invalid newSuperior (%s)\n",
288                                 conn->c_connid, newSuperior.bv_val ));
289 #else
290                         Debug( LDAP_DEBUG_ANY,
291                                 "do_modrdn: invalid newSuperior (%s)\n",
292                                 newSuperior.bv_val, 0, 0 );
293 #endif
294                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
295                                 "invalid newSuperior", NULL, NULL );
296                         goto cleanup;
297                 }
298
299                 rc = dnNormalize( NULL, &newSuperior, &nnewSuperior );
300                 if( rc != LDAP_SUCCESS ) {
301 #ifdef NEW_LOGGING
302                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
303                                 "do_modrdn: conn %d  invalid newSuperior (%s)\n",
304                                 conn->c_connid, pnewSuperior->bv_val ));
305 #else
306                         Debug( LDAP_DEBUG_ANY,
307                                 "do_modrdn: invalid newSuperior (%s)\n",
308                                 pnewSuperior->bv_val, 0, 0 );
309 #endif
310                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
311                                 "invalid newSuperior", NULL, NULL );
312                         goto cleanup;
313                 }
314         }
315
316         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
317             op->o_connid, op->o_opid, pdn->bv_val, 0, 0 );
318
319         manageDSAit = get_manageDSAit( op );
320
321         /*
322          * We could be serving multiple database backends.  Select the
323          * appropriate one, or send a referral to our "referral server"
324          * if we don't hold it.
325          */
326         if ( (be = select_backend( ndn->bv_val, manageDSAit, 0 )) == NULL ) {
327                 struct berval **ref = referral_rewrite( default_referral,
328                         NULL, dn.bv_val, LDAP_SCOPE_DEFAULT );
329
330                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
331                         NULL, NULL, ref ? ref : default_referral, NULL );
332
333                 ber_bvecfree( ref );
334                 goto cleanup;
335         }
336
337         /* check restrictions */
338         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
339         if( rc != LDAP_SUCCESS ) {
340                 send_ldap_result( conn, op, rc,
341                         NULL, text, NULL, NULL );
342                 goto cleanup;
343         }
344
345         /* check for referrals */
346         rc = backend_check_referrals( be, conn, op, pdn->bv_val, ndn->bv_val );
347         if ( rc != LDAP_SUCCESS ) {
348                 goto cleanup;
349         }
350
351         /* Make sure that the entry being changed and the newSuperior are in 
352          * the same backend, otherwise we return an error.
353          */
354         if( newSuperior.bv_len ) {
355                 newSuperior_be = select_backend( nnewSuperior->bv_val, 0, 0 );
356
357                 if ( newSuperior_be != be ) {
358                         /* newSuperior is in same backend */
359                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
360
361                         send_ldap_result( conn, op, rc,
362                                 NULL, "cannot rename between DSAa", NULL, NULL );
363
364                         goto cleanup;
365                 }
366         }
367
368         /* deref suffix alias if appropriate */
369         suffix_alias( be, ndn );
370
371         /*
372          * do the add if 1 && (2 || 3)
373          * 1) there is an add function implemented in this backend;
374          * 2) this backend is master for what it holds;
375          * 3) it's a replica and the dn supplied is the update_ndn.
376          */
377         if ( be->be_modrdn ) {
378                 /* do the update here */
379                 int repl_user = be_isupdate( be, op->o_ndn.bv_val );
380 #ifndef SLAPD_MULTIMASTER
381                 if ( be->be_update_ndn == NULL || repl_user )
382 #endif
383                 {
384                         if ( (*be->be_modrdn)( be, conn, op, pdn->bv_val, ndn->bv_val,
385                                 pnewrdn->bv_val, deloldrdn, pnewSuperior ? pnewSuperior->bv_val : NULL ) == 0
386 #ifdef SLAPD_MULTIMASTER
387                                 && ( be->be_update_ndn == NULL || !repl_user )
388 #endif
389                         ) {
390                                 struct replog_moddn moddn;
391                                 moddn.newrdn = pnewrdn->bv_val;
392                                 moddn.deloldrdn = deloldrdn;
393                                 moddn.newsup = pnewSuperior ? pnewSuperior->bv_val : NULL;
394
395                                 replog( be, op, pdn->bv_val, ndn->bv_val, &moddn );
396                         }
397 #ifndef SLAPD_MULTIMASTER
398                 } else {
399                         struct berval **defref = be->be_update_refs
400                                 ? be->be_update_refs : default_referral;
401                         struct berval **ref = referral_rewrite( defref,
402                                 NULL, pdn->bv_val, LDAP_SCOPE_DEFAULT );
403
404                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
405                                 ref ? ref : defref, NULL );
406
407                         ber_bvecfree( ref );
408 #endif
409                 }
410         } else {
411                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
412                         NULL, "operation not supported within namingContext",
413                         NULL, NULL );
414         }
415
416 cleanup:
417         free( dn.bv_val );
418         if( pdn != NULL ) ber_bvfree( pdn );
419         if( ndn != NULL ) ber_bvfree( ndn );
420
421         free( newrdn.bv_val );  
422         if( pnewrdn != NULL ) ber_bvfree( pnewrdn );
423         if( nnewrdn != NULL ) ber_bvfree( nnewrdn );
424
425         free( newSuperior.bv_val );
426         if ( pnewSuperior != NULL ) ber_bvfree( pnewSuperior );
427         if ( nnewSuperior != NULL ) ber_bvfree( nnewSuperior );
428
429         return rc;
430 }