]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
ITS#4088 force cursors to use same locker
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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, "do_modrdn\n", 0, 0, 0 );
61
62
63         /*
64          * Parse the modrdn request.  It looks like this:
65          *
66          *      ModifyRDNRequest := SEQUENCE {
67          *              entry   DistinguishedName,
68          *              newrdn  RelativeDistinguishedName
69          *              deleteoldrdn    BOOLEAN,
70          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
71          *      }
72          */
73
74         if ( ber_scanf( op->o_ber, "{mmb", &dn, &newrdn, &deloldrdn )
75             == LBER_ERROR )
76         {
77                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
78
79                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
80                 return SLAPD_DISCONNECT;
81         }
82
83         /* Check for newSuperior parameter, if present scan it */
84
85         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
86                 if ( op->o_protocol < LDAP_VERSION3 ) {
87                         /* Conection record indicates v2 but field 
88                          * newSuperior is present: report error.
89                          */
90                         Debug( LDAP_DEBUG_ANY,
91                             "modrdn(v2): invalid field newSuperior!\n",
92                             0, 0, 0 );
93
94                         send_ldap_discon( op, rs,
95                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
96                         rs->sr_err = SLAPD_DISCONNECT;
97                         goto cleanup;
98                 }
99
100                 if ( ber_scanf( op->o_ber, "m", &newSuperior ) 
101                      == LBER_ERROR ) {
102
103                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"m\") failed\n",
104                                 0, 0, 0 );
105
106                         send_ldap_discon( op, rs,
107                                 LDAP_PROTOCOL_ERROR, "decoding error" );
108                         rs->sr_err = SLAPD_DISCONNECT;
109                         goto cleanup;
110                 }
111                 op->orr_newSup = &pnewSuperior;
112                 op->orr_nnewSup = &nnewSuperior;
113         }
114
115         Debug( LDAP_DEBUG_ARGS,
116             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
117                 dn.bv_val, newrdn.bv_val,
118                 newSuperior.bv_len ? newSuperior.bv_val : "" );
119
120         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
121                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
122
123                 send_ldap_discon( op, rs,
124                         LDAP_PROTOCOL_ERROR, "decoding error" );
125                 rs->sr_err = SLAPD_DISCONNECT;
126                 goto cleanup;
127         }
128
129         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
130                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
131
132                 /* get_ctrls has sent results.  Now clean up. */
133                 goto cleanup;
134         } 
135
136         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
137         if( rs->sr_err != LDAP_SUCCESS ) {
138                 Debug( LDAP_DEBUG_ANY,
139                         "do_modrdn: invalid dn (%s)\n", dn.bv_val, 0, 0 );
140                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
141                 goto cleanup;
142         }
143
144         /* FIXME: should have/use rdnPretty / rdnNormalize routines */
145
146         rs->sr_err = dnPrettyNormal( NULL, &newrdn, &op->orr_newrdn, &op->orr_nnewrdn, op->o_tmpmemctx );
147         if( rs->sr_err != LDAP_SUCCESS ) {
148                 Debug( LDAP_DEBUG_ANY,
149                         "do_modrdn: invalid newrdn (%s)\n", newrdn.bv_val, 0, 0 );
150                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
151                 goto cleanup;
152         }
153
154         if( rdn_validate( &op->orr_newrdn ) != LDAP_SUCCESS ) {
155                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n",
156                         op->orr_newrdn.bv_val, 0, 0 );
157
158                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid new RDN" );
159                 goto cleanup;
160         }
161
162         if( op->orr_newSup ) {
163                 rs->sr_err = dnPrettyNormal( NULL, &newSuperior, &pnewSuperior,
164                         &nnewSuperior, op->o_tmpmemctx );
165                 if( rs->sr_err != LDAP_SUCCESS ) {
166                         Debug( LDAP_DEBUG_ANY,
167                                 "do_modrdn: invalid newSuperior (%s)\n",
168                                 newSuperior.bv_val, 0, 0 );
169                         send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
170                         goto cleanup;
171                 }
172         }
173
174         /* FIXME: temporary? */
175         op->orr_deleteoldrdn = deloldrdn;
176
177         op->o_bd = frontendDB;
178         rs->sr_err = frontendDB->be_modrdn( op, rs );
179
180 cleanup:
181         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
182         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
183
184         op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );        
185         op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );       
186
187         if ( !BER_BVISNULL( &pnewSuperior ) ) 
188                 op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
189         if ( !BER_BVISNULL( &nnewSuperior ) )
190                 op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
191
192         return rs->sr_err;
193 }
194
195 int
196 fe_op_modrdn( Operation *op, SlapReply *rs )
197 {
198         Backend         *newSuperior_be = NULL;
199         int             manageDSAit;
200         struct berval   pdn = BER_BVNULL;
201         BackendDB *op_be;
202         
203         if( op->o_req_ndn.bv_len == 0 ) {
204                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
205
206                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
207                         "cannot rename the root DSE" );
208                 goto cleanup;
209
210         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
211                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
212                         frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len, 0 );
213
214                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
215                         "cannot rename subschema subentry" );
216                 goto cleanup;
217         }
218
219         Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
220             op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
221
222         manageDSAit = get_manageDSAit( op );
223
224         /*
225          * We could be serving multiple database backends.  Select the
226          * appropriate one, or send a referral to our "referral server"
227          * if we don't hold it.
228          */
229         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
230         if ( op->o_bd == NULL ) {
231                 rs->sr_ref = referral_rewrite( default_referral,
232                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
233                 if (!rs->sr_ref) rs->sr_ref = default_referral;
234
235                 if ( rs->sr_ref != NULL ) {
236                         rs->sr_err = LDAP_REFERRAL;
237                         op->o_bd = frontendDB;
238                         send_ldap_result( op, rs );
239                         op->o_bd = NULL;
240
241                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
242                 } else {
243                         op->o_bd = frontendDB;
244                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
245                                 "no global superior knowledge" );
246                         op->o_bd = NULL;
247                 }
248                 goto cleanup;
249         }
250
251         /* If we've got a glued backend, check the real backend */
252         op_be = op->o_bd;
253         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
254                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
255         }
256
257         /* check restrictions */
258         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
259                 send_ldap_result( op, rs );
260                 goto cleanup;
261         }
262
263         /* check for referrals */
264         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
265                 goto cleanup;
266         }
267
268         /* Make sure that the entry being changed and the newSuperior are in 
269          * the same backend, otherwise we return an error.
270          */
271         if( op->orr_newSup ) {
272                 newSuperior_be = select_backend( op->orr_nnewSup, 0, 0 );
273
274                 if ( newSuperior_be != op->o_bd ) {
275                         /* newSuperior is in different backend */
276                         send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
277                                 "cannot rename between DSAs" );
278
279                         goto cleanup;
280                 }
281         }
282
283         /*
284          * do the modrdn if 1 && (2 || 3)
285          * 1) there is a modrdn function implemented in this backend;
286          * 2) this backend is master for what it holds;
287          * 3) it's a replica and the dn supplied is the update_ndn.
288          */
289         if ( op->o_bd->be_modrdn ) {
290                 /* do the update here */
291                 int repl_user = be_isupdate( op );
292 #ifndef SLAPD_MULTIMASTER
293                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
294 #endif
295                 {
296                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
297
298                         op->o_bd = op_be;
299
300 #ifdef SLAPD_MULTIMASTER
301                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
302 #endif
303                         {
304                                 cb.sc_next = op->o_callback;
305                                 op->o_callback = &cb;
306                         }
307                         op->o_bd->be_modrdn( op, rs );
308
309                         if ( op->o_bd->be_delete ) {
310                                 struct berval   org_req_dn = BER_BVNULL;
311                                 struct berval   org_req_ndn = BER_BVNULL;
312                                 struct berval   org_dn = BER_BVNULL;
313                                 struct berval   org_ndn = BER_BVNULL;
314                                 int             org_managedsait;
315
316                                 org_req_dn = op->o_req_dn;
317                                 org_req_ndn = op->o_req_ndn;
318                                 org_dn = op->o_dn;
319                                 org_ndn = op->o_ndn;
320                                 org_managedsait = get_manageDSAit( op );
321                                 op->o_dn = op->o_bd->be_rootdn;
322                                 op->o_ndn = op->o_bd->be_rootndn;
323                                 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
324
325                                 while ( rs->sr_err == LDAP_SUCCESS &&
326                                                 op->o_delete_glue_parent ) {
327                                         op->o_delete_glue_parent = 0;
328                                         if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
329                                                 slap_callback cb = { NULL };
330                                                 cb.sc_response = slap_null_cb;
331                                                 dnParent( &op->o_req_ndn, &pdn );
332                                                 op->o_req_dn = pdn;
333                                                 op->o_req_ndn = pdn;
334                                                 op->o_callback = &cb;
335                                                 op->o_bd->be_delete( op, rs );
336                                         } else {
337                                                 break;
338                                         }
339                                 }
340                                 op->o_managedsait = org_managedsait;
341                                 op->o_dn = org_dn;
342                                 op->o_ndn = org_ndn;
343                                 op->o_req_dn = org_req_dn;
344                                 op->o_req_ndn = org_req_ndn;
345                                 op->o_delete_glue_parent = 0;
346                         }
347
348 #ifndef SLAPD_MULTIMASTER
349                 } else {
350                         BerVarray defref = op->o_bd->be_update_refs
351                                 ? op->o_bd->be_update_refs : default_referral;
352
353                         if ( defref != NULL ) {
354                                 rs->sr_ref = referral_rewrite( defref,
355                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
356                                 if (!rs->sr_ref) rs->sr_ref = defref;
357
358                                 rs->sr_err = LDAP_REFERRAL;
359                                 send_ldap_result( op, rs );
360
361                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
362                         } else {
363                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
364                                         "shadow context; no update referral" );
365                         }
366 #endif
367                 }
368         } else {
369                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
370                         "operation not supported within namingContext" );
371         }
372
373 cleanup:;
374         return rs->sr_err;
375 }
376
377 int
378 slap_modrdn2mods(
379         Operation       *op,
380         SlapReply       *rs,
381         Entry           *e,
382         LDAPRDN         old_rdn,
383         LDAPRDN         new_rdn,
384         Modifications   **pmod )
385 {
386         Modifications   *mod = NULL;
387         int             a_cnt, d_cnt;
388         int repl_user;
389
390         assert( new_rdn != NULL );
391         assert( !op->orr_deleteoldrdn || old_rdn != NULL );
392
393         repl_user = be_isupdate( op );
394
395         /* Add new attribute values to the entry */
396         for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
397                 AttributeDescription    *desc = NULL;
398                 Modifications           *mod_tmp;
399
400                 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
401
402                 if ( rs->sr_err != LDAP_SUCCESS ) {
403                         Debug( LDAP_DEBUG_TRACE,
404                                 "slap_modrdn2modlist: %s: %s (new)\n",
405                                 rs->sr_text, 
406                                 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
407                         goto done;              
408                 }
409
410                 /* ACL check of newly added attrs */
411                 if ( op->o_bd && !access_allowed( op, e, desc,
412                         &new_rdn[a_cnt]->la_value, ACL_WADD, NULL ) ) {
413                         Debug( LDAP_DEBUG_TRACE,
414                                 "slap_modrdn2modlist: access to attr \"%s\" "
415                                 "(new) not allowed\n", 
416                                 new_rdn[ a_cnt ]->la_attr.bv_val, 0, 0 );
417                         rs->sr_text = "access to naming attributes (new) not allowed";
418                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
419                         goto done;
420                 }
421
422                 /* Apply modification */
423                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
424                         + 4 * sizeof( struct berval ) );
425                 mod_tmp->sml_desc = desc;
426                 mod_tmp->sml_values = ( BerVarray )( mod_tmp + 1 );
427                 mod_tmp->sml_values[0] = new_rdn[a_cnt]->la_value;
428                 mod_tmp->sml_values[1].bv_val = NULL;
429                 if( desc->ad_type->sat_equality->smr_normalize) {
430                         mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
431                         (void) (*desc->ad_type->sat_equality->smr_normalize)(
432                                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
433                                 desc->ad_type->sat_syntax,
434                                 desc->ad_type->sat_equality,
435                                 &mod_tmp->sml_values[0],
436                                 &mod_tmp->sml_nvalues[0], NULL );
437                         mod_tmp->sml_nvalues[1].bv_val = NULL;
438                 } else {
439                         mod_tmp->sml_nvalues = NULL;
440                 }
441                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
442                 mod_tmp->sml_flags = SLAP_MOD_INTERNAL;
443                 mod_tmp->sml_next = mod;
444                 mod = mod_tmp;
445         }
446
447         /* Remove old rdn value if required */
448         if ( op->orr_deleteoldrdn ) {
449                 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
450                         AttributeDescription    *desc = NULL;
451                         Modifications           *mod_tmp;
452
453                         rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
454                         if ( rs->sr_err != LDAP_SUCCESS ) {
455                                 Debug( LDAP_DEBUG_TRACE,
456                                         "slap_modrdn2modlist: %s: %s (old)\n",
457                                         rs->sr_text, 
458                                         old_rdn[d_cnt]->la_attr.bv_val, 
459                                         0 );
460                                 goto done;              
461                         }
462
463                         /* ACL check of old rdn attrs removal */
464                         if ( op->o_bd && !access_allowed( op, e, desc,
465                                 &old_rdn[d_cnt]->la_value, ACL_WDEL, 
466                                 NULL ) ) {
467                                 Debug( LDAP_DEBUG_TRACE,
468                                         "slap_modrdn2modlist: access "
469                                         "to attr \"%s\" (old) not allowed\n", 
470                                         old_rdn[ d_cnt ]->la_attr.bv_val,
471                                         0, 0 );
472                                 rs->sr_text = "access to naming attributes (old) not allowed";
473                                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
474                                 goto done;
475                         }
476
477                         /* Apply modification */
478                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
479                                 + 4 * sizeof ( struct berval ) );
480                         mod_tmp->sml_desc = desc;
481                         mod_tmp->sml_values = ( BerVarray )(mod_tmp+1);
482                         mod_tmp->sml_values[0] = old_rdn[d_cnt]->la_value;
483                         mod_tmp->sml_values[1].bv_val = NULL;
484                         if( desc->ad_type->sat_equality->smr_normalize) {
485                                 mod_tmp->sml_nvalues = &mod_tmp->sml_values[2];
486                                 (void) (*desc->ad_type->sat_equality->smr_normalize)(
487                                         SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
488                                         desc->ad_type->sat_syntax,
489                                         desc->ad_type->sat_equality,
490                                         &mod_tmp->sml_values[0],
491                                         &mod_tmp->sml_nvalues[0], op->o_tmpmemctx );
492                                 mod_tmp->sml_nvalues[1].bv_val = NULL;
493                         } else {
494                                 mod_tmp->sml_nvalues = NULL;
495                         }
496                         mod_tmp->sml_op = LDAP_MOD_DELETE;
497                         mod_tmp->sml_flags = SLAP_MOD_INTERNAL;
498                         mod_tmp->sml_next = mod;
499                         mod = mod_tmp;
500                 }
501         }
502         
503 done:
504
505         if ( rs->sr_err == LDAP_SUCCESS && !repl_user ) {
506                 slap_mods_opattrs( op, mod, 1 );
507         }
508
509         /* LDAP v2 supporting correct attribute handling. */
510         if ( rs->sr_err != LDAP_SUCCESS && mod != NULL ) {
511                 Modifications *tmp;
512                 for ( ; mod; mod = tmp ) {
513                         tmp = mod->sml_next;
514                         ch_free( mod );
515                 }
516         }
517
518         *pmod = mod;
519
520         return rs->sr_err;
521 }