]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Fix negative caching again, be sure it's really from zero entries returned
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 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         op->orr_deleteoldrdn = deloldrdn;
175         op->orr_modlist = NULL;
176
177         /* prepare modlist of modifications from old/new RDN */
178         rs->sr_err = slap_modrdn2mods( op, rs );
179         if ( rs->sr_err != LDAP_SUCCESS ) {
180                 send_ldap_result( op, rs );
181                 goto cleanup;
182         }
183
184         op->o_bd = frontendDB;
185         rs->sr_err = frontendDB->be_modrdn( op, rs );
186
187 cleanup:
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 ( op->orr_modlist != NULL )
195                 slap_mods_free( op->orr_modlist, 1 );
196
197         if ( !BER_BVISNULL( &pnewSuperior ) ) 
198                 op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
199         if ( !BER_BVISNULL( &nnewSuperior ) )
200                 op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
201
202         return rs->sr_err;
203 }
204
205 int
206 fe_op_modrdn( Operation *op, SlapReply *rs )
207 {
208         Backend         *newSuperior_be = NULL;
209         int             manageDSAit;
210         struct berval   pdn = BER_BVNULL;
211         BackendDB       *op_be, *bd = op->o_bd;
212         
213         if( op->o_req_ndn.bv_len == 0 ) {
214                 Debug( LDAP_DEBUG_ANY, "do_modrdn: root dse!\n", 0, 0, 0 );
215
216                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
217                         "cannot rename the root DSE" );
218                 goto cleanup;
219
220         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
221                 Debug( LDAP_DEBUG_ANY, "do_modrdn: subschema subentry: %s (%ld)\n",
222                         frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len, 0 );
223
224                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
225                         "cannot rename subschema subentry" );
226                 goto cleanup;
227         }
228
229         Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
230             op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
231
232         manageDSAit = get_manageDSAit( op );
233
234         /*
235          * We could be serving multiple database backends.  Select the
236          * appropriate one, or send a referral to our "referral server"
237          * if we don't hold it.
238          */
239         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
240         if ( op->o_bd == NULL ) {
241                 op->o_bd = bd;
242                 rs->sr_ref = referral_rewrite( default_referral,
243                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
244                 if (!rs->sr_ref) rs->sr_ref = default_referral;
245
246                 if ( rs->sr_ref != NULL ) {
247                         rs->sr_err = LDAP_REFERRAL;
248                         send_ldap_result( op, rs );
249
250                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
251                 } else {
252                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
253                                 "no global superior knowledge" );
254                 }
255                 goto cleanup;
256         }
257
258         /* If we've got a glued backend, check the real backend */
259         op_be = op->o_bd;
260         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
261                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
262         }
263
264         /* check restrictions */
265         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
266                 send_ldap_result( op, rs );
267                 goto cleanup;
268         }
269
270         /* check for referrals */
271         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
272                 goto cleanup;
273         }
274
275         /* Make sure that the entry being changed and the newSuperior are in 
276          * the same backend, otherwise we return an error.
277          */
278         if( op->orr_newSup ) {
279                 newSuperior_be = select_backend( op->orr_nnewSup, 0, 0 );
280
281                 if ( newSuperior_be != op->o_bd ) {
282                         /* newSuperior is in different backend */
283                         send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
284                                 "cannot rename between DSAs" );
285
286                         goto cleanup;
287                 }
288         }
289
290         /*
291          * do the modrdn if 1 && (2 || 3)
292          * 1) there is a modrdn function implemented in this backend;
293          * 2) this backend is master for what it holds;
294          * 3) it's a replica and the dn supplied is the update_ndn.
295          */
296         if ( op->o_bd->be_modrdn ) {
297                 /* do the update here */
298                 int repl_user = be_isupdate( op );
299 #ifndef SLAPD_MULTIMASTER
300                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
301 #endif /* ! SLAPD_MULTIMASTER */
302                 {
303                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
304
305                         op->o_bd = op_be;
306
307 #ifdef SLAPD_MULTIMASTER
308                         if ( !op->o_bd->be_update_ndn.bv_len || !repl_user )
309 #endif /* SLAPD_MULTIMASTER */
310                         {
311                                 cb.sc_next = op->o_callback;
312                                 op->o_callback = &cb;
313                         }
314                         op->o_bd->be_modrdn( op, rs );
315
316                         if ( op->o_bd->be_delete ) {
317                                 struct berval   org_req_dn = BER_BVNULL;
318                                 struct berval   org_req_ndn = BER_BVNULL;
319                                 struct berval   org_dn = BER_BVNULL;
320                                 struct berval   org_ndn = BER_BVNULL;
321                                 int             org_managedsait;
322
323                                 org_req_dn = op->o_req_dn;
324                                 org_req_ndn = op->o_req_ndn;
325                                 org_dn = op->o_dn;
326                                 org_ndn = op->o_ndn;
327                                 org_managedsait = get_manageDSAit( op );
328                                 op->o_dn = op->o_bd->be_rootdn;
329                                 op->o_ndn = op->o_bd->be_rootndn;
330                                 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
331
332                                 while ( rs->sr_err == LDAP_SUCCESS &&
333                                                 op->o_delete_glue_parent ) {
334                                         op->o_delete_glue_parent = 0;
335                                         if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
336                                                 slap_callback cb = { NULL };
337                                                 cb.sc_response = slap_null_cb;
338                                                 dnParent( &op->o_req_ndn, &pdn );
339                                                 op->o_req_dn = pdn;
340                                                 op->o_req_ndn = pdn;
341                                                 op->o_callback = &cb;
342                                                 op->o_bd->be_delete( op, rs );
343                                         } else {
344                                                 break;
345                                         }
346                                 }
347                                 op->o_managedsait = org_managedsait;
348                                 op->o_dn = org_dn;
349                                 op->o_ndn = org_ndn;
350                                 op->o_req_dn = org_req_dn;
351                                 op->o_req_ndn = org_req_ndn;
352                                 op->o_delete_glue_parent = 0;
353                         }
354
355 #ifndef SLAPD_MULTIMASTER
356                 } else {
357                         BerVarray defref = op->o_bd->be_update_refs
358                                 ? op->o_bd->be_update_refs : default_referral;
359
360                         if ( defref != NULL ) {
361                                 rs->sr_ref = referral_rewrite( defref,
362                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
363                                 if (!rs->sr_ref) rs->sr_ref = defref;
364
365                                 rs->sr_err = LDAP_REFERRAL;
366                                 send_ldap_result( op, rs );
367
368                                 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
369                         } else {
370                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
371                                         "shadow context; no update referral" );
372                         }
373 #endif /* ! SLAPD_MULTIMASTER */
374                 }
375         } else {
376                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
377                         "operation not supported within namingContext" );
378         }
379
380 cleanup:;
381         op->o_bd = bd;
382         return rs->sr_err;
383 }
384
385 int
386 slap_modrdn2mods(
387         Operation       *op,
388         SlapReply       *rs )
389 {
390         int             a_cnt, d_cnt;
391         LDAPRDN         old_rdn = NULL;
392         LDAPRDN         new_rdn = NULL;
393
394         assert( !BER_BVISEMPTY( &op->oq_modrdn.rs_newrdn ) );
395         assert( !op->orr_deleteoldrdn || !BER_BVISEMPTY( &op->o_req_dn ) );
396
397         if ( ldap_bv2rdn_x( &op->oq_modrdn.rs_newrdn, &new_rdn,
398                 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
399                 Debug( LDAP_DEBUG_TRACE,
400                         "slap_modrdn2mods: can't figure out "
401                         "type(s)/value(s) of newrdn\n", 0, 0, 0 );
402                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
403                 rs->sr_text = "unknown type(s) used in RDN";
404                 goto done;
405         }
406
407         if ( op->oq_modrdn.rs_deleteoldrdn ) {
408                 if ( ldap_bv2rdn_x( &op->o_req_dn, &old_rdn,
409                         (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
410                         Debug( LDAP_DEBUG_TRACE,
411                                 "slap_modrdn2mods: can't figure out "
412                                 "type(s)/value(s) of oldrdn\n", 0, 0, 0 );
413                         rs->sr_err = LDAP_OTHER;
414                         rs->sr_text = "cannot parse RDN from old DN";
415                         goto done;
416                 }
417         }
418
419         /* Add new attribute values to the entry */
420         for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
421                 AttributeDescription    *desc = NULL;
422                 Modifications           *mod_tmp;
423
424                 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
425
426                 if ( rs->sr_err != LDAP_SUCCESS ) {
427                         Debug( LDAP_DEBUG_TRACE,
428                                 "slap_modrdn2mods: %s: %s (new)\n",
429                                 rs->sr_text, 
430                                 new_rdn[ a_cnt ]->la_attr.bv_val, 0 );
431                         goto done;              
432                 }
433
434                 /* Apply modification */
435                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
436                 mod_tmp->sml_desc = desc;
437                 BER_BVZERO( &mod_tmp->sml_type );
438                 mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
439                 ber_dupbv( &mod_tmp->sml_values[0], &new_rdn[a_cnt]->la_value );
440                 mod_tmp->sml_values[1].bv_val = NULL;
441                 if( desc->ad_type->sat_equality->smr_normalize) {
442                         mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
443                         (void) (*desc->ad_type->sat_equality->smr_normalize)(
444                                 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
445                                 desc->ad_type->sat_syntax,
446                                 desc->ad_type->sat_equality,
447                                 &mod_tmp->sml_values[0],
448                                 &mod_tmp->sml_nvalues[0], NULL );
449                         mod_tmp->sml_nvalues[1].bv_val = NULL;
450                 } else {
451                         mod_tmp->sml_nvalues = NULL;
452                 }
453                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
454                 mod_tmp->sml_flags = 0;
455                 mod_tmp->sml_next = op->orr_modlist;
456                 op->orr_modlist = mod_tmp;
457         }
458
459         /* Remove old rdn value if required */
460         if ( op->orr_deleteoldrdn ) {
461                 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
462                         AttributeDescription    *desc = NULL;
463                         Modifications           *mod_tmp;
464
465                         rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
466                         if ( rs->sr_err != LDAP_SUCCESS ) {
467                                 Debug( LDAP_DEBUG_TRACE,
468                                         "slap_modrdn2mods: %s: %s (old)\n",
469                                         rs->sr_text, 
470                                         old_rdn[d_cnt]->la_attr.bv_val, 
471                                         0 );
472                                 goto done;              
473                         }
474
475                         /* Apply modification */
476                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
477                         mod_tmp->sml_desc = desc;
478                         BER_BVZERO( &mod_tmp->sml_type );
479                         mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
480                         ber_dupbv( &mod_tmp->sml_values[0], &old_rdn[d_cnt]->la_value );
481                         mod_tmp->sml_values[1].bv_val = NULL;
482                         if( desc->ad_type->sat_equality->smr_normalize) {
483                                 mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
484                                 (void) (*desc->ad_type->sat_equality->smr_normalize)(
485                                         SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
486                                         desc->ad_type->sat_syntax,
487                                         desc->ad_type->sat_equality,
488                                         &mod_tmp->sml_values[0],
489                                         &mod_tmp->sml_nvalues[0], NULL );
490                                 mod_tmp->sml_nvalues[1].bv_val = NULL;
491                         } else {
492                                 mod_tmp->sml_nvalues = NULL;
493                         }
494                         mod_tmp->sml_op = LDAP_MOD_DELETE;
495                         mod_tmp->sml_flags = 0;
496                         mod_tmp->sml_next = op->orr_modlist;
497                         op->orr_modlist = mod_tmp;
498                 }
499         }
500         
501 done:
502
503 #if 0
504         /* should be done by backend */
505         if ( rs->sr_err == LDAP_SUCCESS && !repl_user ) {
506                 slap_mods_opattrs( op, &op->orr_modlist, 1 );
507         }
508 #endif
509
510         /* LDAP v2 supporting correct attribute handling. */
511         if ( rs->sr_err != LDAP_SUCCESS && op->orr_modlist != NULL ) {
512                 Modifications *tmp;
513
514                 for ( ; op->orr_modlist != NULL; op->orr_modlist = tmp ) {
515                         tmp = op->orr_modlist->sml_next;
516                         ch_free( op->orr_modlist );
517                 }
518         }
519
520         if ( new_rdn != NULL ) {
521                 ldap_rdnfree_x( new_rdn, op->o_tmpmemctx );
522         }
523         if ( old_rdn != NULL ) {
524                 ldap_rdnfree_x( old_rdn, op->o_tmpmemctx );
525         }
526
527         return rs->sr_err;
528 }
529