]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Cleanup for C++
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
16  * This software is not subject to any license of Silicon Graphics 
17  * Inc. or Purdue University.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * without restriction or fee of any kind as long as this notice
21  * is preserved.
22  */
23 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms are permitted
27  * provided that this notice is preserved and that due credit is given
28  * to the University of Michigan at Ann Arbor. The name of the University
29  * may not be used to endorse or promote products derived from this
30  * software without specific prior written permission. This software
31  * is provided ``as is'' without express or implied warranty.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/socket.h>
39 #include <ac/string.h>
40
41 #include "slap.h"
42
43 int
44 do_modrdn(
45     Operation   *op,
46     SlapReply   *rs
47 )
48 {
49         struct berval   dn = BER_BVNULL;
50         struct berval   newrdn = BER_BVNULL;
51         struct berval   newSuperior = BER_BVNULL;
52         ber_int_t       deloldrdn;
53
54         struct berval pnewSuperior = BER_BVNULL;
55
56         struct berval nnewSuperior = BER_BVNULL;
57
58         ber_len_t       length;
59
60         Debug( LDAP_DEBUG_TRACE, "%s do_modrdn\n",
61                         op->o_log_prefix, 0, 0 );
62         /*
63          * Parse the modrdn request.  It looks like this:
64          *
65          *      ModifyRDNRequest := SEQUENCE {
66          *              entry   DistinguishedName,
67          *              newrdn  RelativeDistinguishedName
68          *              deleteoldrdn    BOOLEAN,
69          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
70          *      }
71          */
72
73         if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
74             == LBER_ERROR )
75         {
76                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: ber_scanf failed\n",
77                         op->o_log_prefix, 0, 0 );
78                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
79                 return SLAPD_DISCONNECT;
80         }
81
82         /* Check for newSuperior parameter, if present scan it */
83
84         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
85                 if ( op->o_protocol < LDAP_VERSION3 ) {
86                         /* Connection record indicates v2 but field 
87                          * newSuperior is present: report error.
88                          */
89                         Debug( LDAP_DEBUG_ANY,
90                                 "%s do_modrdn: newSuperior requires LDAPv3\n",
91                                 op->o_log_prefix, 0, 0 );
92
93                         send_ldap_discon( op, rs,
94                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
95                         rs->sr_err = SLAPD_DISCONNECT;
96                         goto cleanup;
97                 }
98
99                 if ( ber_scanf( op->o_ber, "m", &newSuperior ) 
100                      == LBER_ERROR ) {
101
102                         Debug( LDAP_DEBUG_ANY, "%s do_modrdn: ber_scanf(\"m\") failed\n",
103                                 op->o_log_prefix, 0, 0 );
104
105                         send_ldap_discon( op, rs,
106                                 LDAP_PROTOCOL_ERROR, "decoding error" );
107                         rs->sr_err = SLAPD_DISCONNECT;
108                         goto cleanup;
109                 }
110                 op->orr_newSup = &pnewSuperior;
111                 op->orr_nnewSup = &nnewSuperior;
112         }
113
114         Debug( LDAP_DEBUG_ARGS,
115             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
116                 dn.bv_val, newrdn.bv_val,
117                 newSuperior.bv_len ? newSuperior.bv_val : "" );
118
119         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
120                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: ber_scanf failed\n",
121                         op->o_log_prefix, 0, 0 );
122                 send_ldap_discon( op, rs,
123                         LDAP_PROTOCOL_ERROR, "decoding error" );
124                 rs->sr_err = SLAPD_DISCONNECT;
125                 goto cleanup;
126         }
127
128         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
129                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: get_ctrls failed\n",
130                         op->o_log_prefix, 0, 0 );
131                 /* get_ctrls has sent results.  Now clean up. */
132                 goto cleanup;
133         } 
134
135         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
136         if( rs->sr_err != LDAP_SUCCESS ) {
137                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: invalid dn (%s)\n",
138                         op->o_log_prefix, dn.bv_val, 0 );
139                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
140                 goto cleanup;
141         }
142
143         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
144
145         rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
146         if( rs->sr_err != LDAP_SUCCESS ) {
147                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: invalid newrdn (%s)\n",
148                         op->o_log_prefix, newrdn.bv_val, 0 );
149                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
150                 goto cleanup;
151         }
152
153         if( rdn_validate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
154                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: invalid rdn (%s)\n",
155                         op->o_log_prefix, op->orr_newrdn.bv_val, 0 );
156                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
157                 goto cleanup;
158         }
159
160         if( op->orr_newSup ) {
161                 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
162                         &nnewSuperior, op->o_tmpmemctx );
163                 if( rs->sr_err != LDAP_SUCCESS ) {
164                         Debug( LDAP_DEBUG_ANY,
165                                 "%s do_modrdn: invalid newSuperior (%s)\n",
166                                 op->o_log_prefix, newSuperior.bv_val, 0 );
167                         send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
168                         goto cleanup;
169                 }
170         }
171
172         Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
173             op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
174
175         op->orr_deleteoldrdn = deloldrdn;
176         op->orr_modlist = NULL;
177
178         /* prepare modlist of modifications from old/new RDN */
179         rs->sr_err = slap_modrdn2mods( op, rs );
180         if ( rs->sr_err != LDAP_SUCCESS ) {
181                 send_ldap_result( op, rs );
182                 goto cleanup;
183         }
184
185         op->o_bd = frontendDB;
186         rs->sr_err = frontendDB->be_modrdn( op, rs );
187
188 #ifdef LDAP_X_TXN
189         if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
190                 /* skip cleanup */
191         }
192 #endif
193
194 cleanup:
195         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
196         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
197
198         op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );        
199         op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );       
200
201         if ( op->orr_modlist != NULL )
202                 slap_mods_free( op->orr_modlist, 1 );
203
204         if ( !BER_BVISNULL( &pnewSuperior ) ) {
205                 op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
206         }
207         if ( !BER_BVISNULL( &nnewSuperior ) ) {
208                 op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
209         }
210
211         return rs->sr_err;
212 }
213
214 int
215 fe_op_modrdn( Operation *op, SlapReply *rs )
216 {
217         Backend         *newSuperior_be = NULL;
218         struct berval   pdn = BER_BVNULL;
219         BackendDB       *op_be, *bd = op->o_bd;
220         
221         if( op->o_req_ndn.bv_len == 0 ) {
222                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: root dse!\n",
223                         op->o_log_prefix, 0, 0 );
224                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
225                         "cannot rename the root DSE" );
226                 goto cleanup;
227
228         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
229                 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: subschema subentry: %s (%ld)\n",
230                         op->o_log_prefix, frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len );
231
232                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
233                         "cannot rename subschema subentry" );
234                 goto cleanup;
235         }
236
237         /*
238          * We could be serving multiple database backends.  Select the
239          * appropriate one, or send a referral to our "referral server"
240          * if we don't hold it.
241          */
242         op->o_bd = select_backend( &op->o_req_ndn, 1 );
243         if ( op->o_bd == NULL ) {
244                 op->o_bd = bd;
245                 rs->sr_ref = referral_rewrite( default_referral,
246                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
247                 if (!rs->sr_ref) rs->sr_ref = default_referral;
248
249                 if ( rs->sr_ref != NULL ) {
250                         rs->sr_err = LDAP_REFERRAL;
251                         send_ldap_result( op, rs );
252
253                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
254                 } else {
255                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
256                                 "no global superior knowledge" );
257                 }
258                 goto cleanup;
259         }
260
261         /* If we've got a glued backend, check the real backend */
262         op_be = op->o_bd;
263         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
264                 op->o_bd = select_backend( &op->o_req_ndn, 0 );
265         }
266
267         /* check restrictions */
268         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
269                 send_ldap_result( op, rs );
270                 goto cleanup;
271         }
272
273         /* check for referrals */
274         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
275                 goto cleanup;
276         }
277
278         /* Make sure that the entry being changed and the newSuperior are in 
279          * the same backend, otherwise we return an error.
280          */
281         if( op->orr_newSup ) {
282                 newSuperior_be = select_backend( op->orr_nnewSup, 0 );
283
284                 if ( newSuperior_be != op->o_bd ) {
285                         /* newSuperior is in different backend */
286                         send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
287                                 "cannot rename between DSAs" );
288
289                         goto cleanup;
290                 }
291         }
292
293         /*
294          * do the modrdn if 1 && (2 || 3)
295          * 1) there is a modrdn function implemented in this backend;
296          * 2) this backend is master for what it holds;
297          * 3) it's a replica and the dn supplied is the update_ndn.
298          */
299         if ( op->o_bd->be_modrdn ) {
300                 /* do the update here */
301                 int repl_user = be_isupdate( op );
302                 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user )
303                 {
304                         op->o_bd = op_be;
305                         op->o_bd->be_modrdn( op, rs );
306
307                         if ( op->o_bd->be_delete ) {
308                                 struct berval   org_req_dn = BER_BVNULL;
309                                 struct berval   org_req_ndn = BER_BVNULL;
310                                 struct berval   org_dn = BER_BVNULL;
311                                 struct berval   org_ndn = BER_BVNULL;
312                                 int             org_managedsait;
313
314                                 org_req_dn = op->o_req_dn;
315                                 org_req_ndn = op->o_req_ndn;
316                                 org_dn = op->o_dn;
317                                 org_ndn = op->o_ndn;
318                                 org_managedsait = get_manageDSAit( op );
319                                 op->o_dn = op->o_bd->be_rootdn;
320                                 op->o_ndn = op->o_bd->be_rootndn;
321                                 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
322
323                                 while ( rs->sr_err == LDAP_SUCCESS &&
324                                                 op->o_delete_glue_parent ) {
325                                         op->o_delete_glue_parent = 0;
326                                         if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
327                                                 slap_callback cb = { NULL };
328                                                 cb.sc_response = slap_null_cb;
329                                                 dnParent( &op->o_req_ndn, &pdn );
330                                                 op->o_req_dn = pdn;
331                                                 op->o_req_ndn = pdn;
332                                                 op->o_callback = &cb;
333                                                 op->o_bd->be_delete( op, rs );
334                                         } else {
335                                                 break;
336                                         }
337                                 }
338                                 op->o_managedsait = org_managedsait;
339                                 op->o_dn = org_dn;
340                                 op->o_ndn = org_ndn;
341                                 op->o_req_dn = org_req_dn;
342                                 op->o_req_ndn = org_req_ndn;
343                                 op->o_delete_glue_parent = 0;
344                         }
345
346                 } else {
347                         BerVarray defref = op->o_bd->be_update_refs
348                                 ? op->o_bd->be_update_refs : default_referral;
349
350                         if ( defref != NULL ) {
351                                 rs->sr_ref = referral_rewrite( defref,
352                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
353                                 if (!rs->sr_ref) rs->sr_ref = defref;
354
355                                 rs->sr_err = LDAP_REFERRAL;
356                                 send_ldap_result( op, rs );
357
358                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
359                         } else {
360                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
361                                         "shadow context; no update referral" );
362                         }
363                 }
364         } else {
365                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
366                         "operation not supported within namingContext" );
367         }
368
369 cleanup:;
370         op->o_bd = bd;
371         return rs->sr_err;
372 }
373
374 int
375 slap_modrdn2mods(
376         Operation       *op,
377         SlapReply       *rs )
378 {
379         int             a_cnt, d_cnt;
380         LDAPRDN         old_rdn = NULL;
381         LDAPRDN         new_rdn = NULL;
382
383         assert( !BER_BVISEMPTY( &op->oq_modrdn.rs_newrdn ) );
384         assert( !op->orr_deleteoldrdn || !BER_BVISEMPTY( &op->o_req_dn ) );
385
386         if ( ldap_bv2rdn_x( &op->oq_modrdn.rs_newrdn, &new_rdn,
387                 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
388                 Debug( LDAP_DEBUG_TRACE,
389                         "%s slap_modrdn2mods: can't figure out "
390                         "type(s)/value(s) of newrdn\n",
391                         op->o_log_prefix, 0, 0 );
392                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
393                 rs->sr_text = "unknown type(s) used in RDN";
394                 goto done;
395         }
396
397         if ( op->oq_modrdn.rs_deleteoldrdn ) {
398                 if ( ldap_bv2rdn_x( &op->o_req_dn, &old_rdn,
399                         (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
400                         Debug( LDAP_DEBUG_TRACE,
401                                 "%s slap_modrdn2mods: can't figure out "
402                                 "type(s)/value(s) of oldrdn\n",
403                                 op->o_log_prefix, 0, 0 );
404                         rs->sr_err = LDAP_OTHER;
405                         rs->sr_text = "cannot parse RDN from old DN";
406                         goto done;
407                 }
408         }
409         rs->sr_text = NULL;
410
411         /* Add new attribute values to the entry */
412         for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
413                 AttributeDescription    *desc = NULL;
414                 Modifications           *mod_tmp;
415
416                 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
417
418                 if ( rs->sr_err != LDAP_SUCCESS ) {
419                         Debug( LDAP_DEBUG_TRACE,
420                                 "%s slap_modrdn2mods: %s: %s (new)\n",
421                                 op->o_log_prefix,
422                                 rs->sr_text, 
423                                 new_rdn[ a_cnt ]->la_attr.bv_val );
424                         goto done;              
425                 }
426
427                 /* Apply modification */
428                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
429                 mod_tmp->sml_desc = desc;
430                 BER_BVZERO( &mod_tmp->sml_type );
431                 mod_tmp->sml_numvals = 1;
432                 mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
433                 ber_dupbv( &mod_tmp->sml_values[0], &new_rdn[a_cnt]->la_value );
434                 mod_tmp->sml_values[1].bv_val = NULL;
435                 if( desc->ad_type->sat_equality->smr_normalize) {
436                         mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
437                         (void) (*desc->ad_type->sat_equality->smr_normalize)(
438                                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
439                                 desc->ad_type->sat_syntax,
440                                 desc->ad_type->sat_equality,
441                                 &mod_tmp->sml_values[0],
442                                 &mod_tmp->sml_nvalues[0], NULL );
443                         mod_tmp->sml_nvalues[1].bv_val = NULL;
444                 } else {
445                         mod_tmp->sml_nvalues = NULL;
446                 }
447                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
448                 mod_tmp->sml_flags = 0;
449                 mod_tmp->sml_next = op->orr_modlist;
450                 op->orr_modlist = mod_tmp;
451         }
452
453         /* Remove old rdn value if required */
454         if ( op->orr_deleteoldrdn ) {
455                 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
456                         AttributeDescription    *desc = NULL;
457                         Modifications           *mod_tmp;
458
459                         rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
460                         if ( rs->sr_err != LDAP_SUCCESS ) {
461                                 Debug( LDAP_DEBUG_TRACE,
462                                         "%s slap_modrdn2mods: %s: %s (old)\n",
463                                         op->o_log_prefix,
464                                         rs->sr_text, 
465                                         old_rdn[d_cnt]->la_attr.bv_val );
466                                 goto done;              
467                         }
468
469                         /* Apply modification */
470                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
471                         mod_tmp->sml_desc = desc;
472                         BER_BVZERO( &mod_tmp->sml_type );
473                         mod_tmp->sml_numvals = 1;
474                         mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
475                         ber_dupbv( &mod_tmp->sml_values[0], &old_rdn[d_cnt]->la_value );
476                         mod_tmp->sml_values[1].bv_val = NULL;
477                         if( desc->ad_type->sat_equality->smr_normalize) {
478                                 mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
479                                 (void) (*desc->ad_type->sat_equality->smr_normalize)(
480                                         SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
481                                         desc->ad_type->sat_syntax,
482                                         desc->ad_type->sat_equality,
483                                         &mod_tmp->sml_values[0],
484                                         &mod_tmp->sml_nvalues[0], NULL );
485                                 mod_tmp->sml_nvalues[1].bv_val = NULL;
486                         } else {
487                                 mod_tmp->sml_nvalues = NULL;
488                         }
489                         mod_tmp->sml_op = LDAP_MOD_DELETE;
490                         mod_tmp->sml_flags = 0;
491                         mod_tmp->sml_next = op->orr_modlist;
492                         op->orr_modlist = mod_tmp;
493                 }
494         }
495         
496 done:
497
498         /* LDAP v2 supporting correct attribute handling. */
499         if ( rs->sr_err != LDAP_SUCCESS && op->orr_modlist != NULL ) {
500                 Modifications *tmp;
501
502                 for ( ; op->orr_modlist != NULL; op->orr_modlist = tmp ) {
503                         tmp = op->orr_modlist->sml_next;
504                         ch_free( op->orr_modlist );
505                 }
506         }
507
508         if ( new_rdn != NULL ) {
509                 ldap_rdnfree_x( new_rdn, op->o_tmpmemctx );
510         }
511         if ( old_rdn != NULL ) {
512                 ldap_rdnfree_x( old_rdn, op->o_tmpmemctx );
513         }
514
515         return rs->sr_err;
516 }
517