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