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