modrdn.c revision 1.1.1.7 1 /* $NetBSD: modrdn.c,v 1.1.1.7 2019/08/08 13:31:38 christos Exp $ */
2
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2019 The OpenLDAP Foundation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
18 * This software is not subject to any license of Silicon Graphics
19 * Inc. or Purdue University.
20 *
21 * Redistribution and use in source and binary forms are permitted
22 * without restriction or fee of any kind as long as this notice
23 * is preserved.
24 */
25 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms are permitted
29 * provided that this notice is preserved and that due credit is given
30 * to the University of Michigan at Ann Arbor. The name of the University
31 * may not be used to endorse or promote products derived from this
32 * software without specific prior written permission. This software
33 * is provided ``as is'' without express or implied warranty.
34 */
35
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: modrdn.c,v 1.1.1.7 2019/08/08 13:31:38 christos Exp $");
38
39 #include "portable.h"
40
41 #include <stdio.h>
42
43 #include <ac/socket.h>
44 #include <ac/string.h>
45
46 #include "slap.h"
47
48 int
49 do_modrdn(
50 Operation *op,
51 SlapReply *rs
52 )
53 {
54 struct berval dn = BER_BVNULL;
55 struct berval newrdn = BER_BVNULL;
56 struct berval newSuperior = BER_BVNULL;
57 ber_int_t deloldrdn;
58
59 struct berval pnewSuperior = BER_BVNULL;
60
61 struct berval nnewSuperior = BER_BVNULL;
62
63 ber_len_t length;
64
65 Debug( LDAP_DEBUG_TRACE, "%s do_modrdn\n",
66 op->o_log_prefix, 0, 0 );
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, "%s do_modrdn: ber_scanf failed\n",
82 op->o_log_prefix, 0, 0 );
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 /* Connection record indicates v2 but field
92 * newSuperior is present: report error.
93 */
94 Debug( LDAP_DEBUG_ANY,
95 "%s do_modrdn: newSuperior requires LDAPv3\n",
96 op->o_log_prefix, 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, "%s do_modrdn: ber_scanf(\"m\") failed\n",
108 op->o_log_prefix, 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, "%s do_modrdn: ber_scanf failed\n",
126 op->o_log_prefix, 0, 0 );
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, "%s do_modrdn: get_ctrls failed\n",
135 op->o_log_prefix, 0, 0 );
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, "%s do_modrdn: invalid dn (%s)\n",
143 op->o_log_prefix, dn.bv_val, 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, "%s do_modrdn: invalid newrdn (%s)\n",
153 op->o_log_prefix, newrdn.bv_val, 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, "%s do_modrdn: invalid rdn (%s)\n",
160 op->o_log_prefix, op->orr_newrdn.bv_val, 0 );
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 "%s do_modrdn: invalid newSuperior (%s)\n",
171 op->o_log_prefix, newSuperior.bv_val, 0 );
172 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid newSuperior" );
173 goto cleanup;
174 }
175 }
176
177 Statslog( LDAP_DEBUG_STATS, "%s MODRDN dn=\"%s\"\n",
178 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
179
180 op->orr_deleteoldrdn = deloldrdn;
181 op->orr_modlist = NULL;
182
183 /* prepare modlist of modifications from old/new RDN */
184 rs->sr_err = slap_modrdn2mods( op, rs );
185 if ( rs->sr_err != LDAP_SUCCESS ) {
186 send_ldap_result( op, rs );
187 goto cleanup;
188 }
189
190 op->o_bd = frontendDB;
191 rs->sr_err = frontendDB->be_modrdn( op, rs );
192
193 #ifdef LDAP_X_TXN
194 if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
195 /* skip cleanup */
196 }
197 #endif
198
199 cleanup:
200 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
201 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
202
203 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
204 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
205
206 if ( op->orr_modlist != NULL )
207 slap_mods_free( op->orr_modlist, 1 );
208
209 if ( !BER_BVISNULL( &pnewSuperior ) ) {
210 op->o_tmpfree( pnewSuperior.bv_val, op->o_tmpmemctx );
211 }
212 if ( !BER_BVISNULL( &nnewSuperior ) ) {
213 op->o_tmpfree( nnewSuperior.bv_val, op->o_tmpmemctx );
214 }
215
216 return rs->sr_err;
217 }
218
219 int
220 fe_op_modrdn( Operation *op, SlapReply *rs )
221 {
222 struct berval dest_ndn = BER_BVNULL, dest_pndn, pdn = BER_BVNULL;
223 BackendDB *op_be, *bd = op->o_bd;
224 ber_slen_t diff;
225
226 if( op->o_req_ndn.bv_len == 0 ) {
227 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: root dse!\n",
228 op->o_log_prefix, 0, 0 );
229 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
230 "cannot rename the root DSE" );
231 goto cleanup;
232
233 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
234 Debug( LDAP_DEBUG_ANY, "%s do_modrdn: subschema subentry: %s (%ld)\n",
235 op->o_log_prefix, frontendDB->be_schemandn.bv_val, (long)frontendDB->be_schemandn.bv_len );
236
237 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
238 "cannot rename subschema subentry" );
239 goto cleanup;
240 }
241
242 if( op->orr_nnewSup ) {
243 dest_pndn = *op->orr_nnewSup;
244 } else {
245 dnParent( &op->o_req_ndn, &dest_pndn );
246 }
247 build_new_dn( &dest_ndn, &dest_pndn, &op->orr_nnewrdn, op->o_tmpmemctx );
248
249 diff = (ber_slen_t) dest_ndn.bv_len - (ber_slen_t) op->o_req_ndn.bv_len;
250 if ( diff > 0 ? dnIsSuffix( &dest_ndn, &op->o_req_ndn )
251 : diff < 0 && dnIsSuffix( &op->o_req_ndn, &dest_ndn ) )
252 {
253 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
254 diff > 0 ? "cannot place an entry below itself"
255 : "cannot place an entry above itself" );
256 goto cleanup;
257 }
258
259 /*
260 * We could be serving multiple database backends. Select the
261 * appropriate one, or send a referral to our "referral server"
262 * if we don't hold it.
263 */
264 op->o_bd = select_backend( &op->o_req_ndn, 1 );
265 if ( op->o_bd == NULL ) {
266 op->o_bd = bd;
267 rs->sr_ref = referral_rewrite( default_referral,
268 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
269 if (!rs->sr_ref) rs->sr_ref = default_referral;
270
271 if ( rs->sr_ref != NULL ) {
272 rs->sr_err = LDAP_REFERRAL;
273 send_ldap_result( op, rs );
274
275 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
276 } else {
277 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
278 "no global superior knowledge" );
279 }
280 goto cleanup;
281 }
282
283 /* If we've got a glued backend, check the real backend */
284 op_be = op->o_bd;
285 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
286 op->o_bd = select_backend( &op->o_req_ndn, 0 );
287 }
288
289 /* check restrictions */
290 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
291 send_ldap_result( op, rs );
292 goto cleanup;
293 }
294
295 /* check for referrals */
296 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
297 goto cleanup;
298 }
299
300 /* check that destination DN is in the same backend as source DN */
301 if ( select_backend( &dest_ndn, 0 ) != op->o_bd ) {
302 send_ldap_error( op, rs, LDAP_AFFECTS_MULTIPLE_DSAS,
303 "cannot rename between DSAs" );
304 goto cleanup;
305 }
306
307 /*
308 * do the modrdn if 1 && (2 || 3)
309 * 1) there is a modrdn function implemented in this backend;
310 * 2) this backend is master for what it holds;
311 * 3) it's a replica and the dn supplied is the update_ndn.
312 */
313 if ( op->o_bd->be_modrdn ) {
314 /* do the update here */
315 int repl_user = be_isupdate( op );
316 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user )
317 {
318 op->o_bd = op_be;
319 op->o_bd->be_modrdn( op, rs );
320
321 if ( op->o_bd->be_delete ) {
322 struct berval org_req_dn = BER_BVNULL;
323 struct berval org_req_ndn = BER_BVNULL;
324 struct berval org_dn = BER_BVNULL;
325 struct berval org_ndn = BER_BVNULL;
326 int org_managedsait;
327
328 org_req_dn = op->o_req_dn;
329 org_req_ndn = op->o_req_ndn;
330 org_dn = op->o_dn;
331 org_ndn = op->o_ndn;
332 org_managedsait = get_manageDSAit( op );
333 op->o_dn = op->o_bd->be_rootdn;
334 op->o_ndn = op->o_bd->be_rootndn;
335 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
336
337 while ( rs->sr_err == LDAP_SUCCESS &&
338 op->o_delete_glue_parent ) {
339 op->o_delete_glue_parent = 0;
340 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
341 slap_callback cb = { NULL };
342 cb.sc_response = slap_null_cb;
343 dnParent( &op->o_req_ndn, &pdn );
344 op->o_req_dn = pdn;
345 op->o_req_ndn = pdn;
346 op->o_callback = &cb;
347 op->o_bd->be_delete( op, rs );
348 } else {
349 break;
350 }
351 }
352 op->o_managedsait = org_managedsait;
353 op->o_dn = org_dn;
354 op->o_ndn = org_ndn;
355 op->o_req_dn = org_req_dn;
356 op->o_req_ndn = org_req_ndn;
357 op->o_delete_glue_parent = 0;
358 }
359
360 } else {
361 BerVarray defref = op->o_bd->be_update_refs
362 ? op->o_bd->be_update_refs : default_referral;
363
364 if ( defref != NULL ) {
365 rs->sr_ref = referral_rewrite( defref,
366 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
367 if (!rs->sr_ref) rs->sr_ref = defref;
368
369 rs->sr_err = LDAP_REFERRAL;
370 send_ldap_result( op, rs );
371
372 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
373 } else {
374 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
375 "shadow context; no update referral" );
376 }
377 }
378 } else {
379 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
380 "operation not supported within namingContext" );
381 }
382
383 cleanup:;
384 if ( dest_ndn.bv_val != NULL )
385 ber_memfree_x( dest_ndn.bv_val, op->o_tmpmemctx );
386 op->o_bd = bd;
387 return rs->sr_err;
388 }
389
390 int
391 slap_modrdn2mods(
392 Operation *op,
393 SlapReply *rs )
394 {
395 int a_cnt, d_cnt;
396 LDAPRDN old_rdn = NULL;
397 LDAPRDN new_rdn = NULL;
398
399 assert( !BER_BVISEMPTY( &op->oq_modrdn.rs_newrdn ) );
400
401 /* if requestDN is empty, silently reset deleteOldRDN */
402 if ( BER_BVISEMPTY( &op->o_req_dn ) ) op->orr_deleteoldrdn = 0;
403
404 if ( ldap_bv2rdn_x( &op->oq_modrdn.rs_newrdn, &new_rdn,
405 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
406 Debug( LDAP_DEBUG_TRACE,
407 "%s slap_modrdn2mods: can't figure out "
408 "type(s)/value(s) of newrdn\n",
409 op->o_log_prefix, 0, 0 );
410 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
411 rs->sr_text = "unknown type(s)/value(s) used in RDN";
412 goto done;
413 }
414
415 if ( op->oq_modrdn.rs_deleteoldrdn ) {
416 if ( ldap_bv2rdn_x( &op->o_req_dn, &old_rdn,
417 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx ) ) {
418 Debug( LDAP_DEBUG_TRACE,
419 "%s slap_modrdn2mods: can't figure out "
420 "type(s)/value(s) of oldrdn\n",
421 op->o_log_prefix, 0, 0 );
422 rs->sr_err = LDAP_OTHER;
423 rs->sr_text = "cannot parse RDN from old DN";
424 goto done;
425 }
426 }
427 rs->sr_text = NULL;
428
429 /* Add new attribute values to the entry */
430 for ( a_cnt = 0; new_rdn[a_cnt]; a_cnt++ ) {
431 AttributeDescription *desc = NULL;
432 Modifications *mod_tmp;
433
434 rs->sr_err = slap_bv2ad( &new_rdn[a_cnt]->la_attr, &desc, &rs->sr_text );
435
436 if ( rs->sr_err != LDAP_SUCCESS ) {
437 Debug( LDAP_DEBUG_TRACE,
438 "%s slap_modrdn2mods: %s: %s (new)\n",
439 op->o_log_prefix,
440 rs->sr_text,
441 new_rdn[ a_cnt ]->la_attr.bv_val );
442 goto done;
443 }
444
445 if ( !desc->ad_type->sat_equality ) {
446 Debug( LDAP_DEBUG_TRACE,
447 "%s slap_modrdn2mods: %s: %s (new)\n",
448 op->o_log_prefix,
449 rs->sr_text,
450 new_rdn[ a_cnt ]->la_attr.bv_val );
451 rs->sr_text = "naming attribute has no equality matching rule";
452 rs->sr_err = LDAP_NAMING_VIOLATION;
453 goto done;
454 }
455
456 /* Apply modification */
457 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
458 mod_tmp->sml_desc = desc;
459 BER_BVZERO( &mod_tmp->sml_type );
460 mod_tmp->sml_numvals = 1;
461 mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
462 ber_dupbv( &mod_tmp->sml_values[0], &new_rdn[a_cnt]->la_value );
463 mod_tmp->sml_values[1].bv_val = NULL;
464 if( desc->ad_type->sat_equality->smr_normalize) {
465 mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
466 rs->sr_err = desc->ad_type->sat_equality->smr_normalize(
467 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
468 desc->ad_type->sat_syntax,
469 desc->ad_type->sat_equality,
470 &mod_tmp->sml_values[0],
471 &mod_tmp->sml_nvalues[0], NULL );
472 if (rs->sr_err != LDAP_SUCCESS) {
473 ch_free(mod_tmp->sml_nvalues);
474 ch_free(mod_tmp->sml_values[0].bv_val);
475 ch_free(mod_tmp->sml_values);
476 ch_free(mod_tmp);
477 goto done;
478 }
479 mod_tmp->sml_nvalues[1].bv_val = NULL;
480 } else {
481 mod_tmp->sml_nvalues = NULL;
482 }
483 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
484 mod_tmp->sml_flags = 0;
485 mod_tmp->sml_next = op->orr_modlist;
486 op->orr_modlist = mod_tmp;
487 }
488
489 /* Remove old rdn value if required */
490 if ( op->orr_deleteoldrdn ) {
491 for ( d_cnt = 0; old_rdn[d_cnt]; d_cnt++ ) {
492 AttributeDescription *desc = NULL;
493 Modifications *mod_tmp;
494
495 rs->sr_err = slap_bv2ad( &old_rdn[d_cnt]->la_attr, &desc, &rs->sr_text );
496 if ( rs->sr_err != LDAP_SUCCESS ) {
497 Debug( LDAP_DEBUG_TRACE,
498 "%s slap_modrdn2mods: %s: %s (old)\n",
499 op->o_log_prefix,
500 rs->sr_text,
501 old_rdn[d_cnt]->la_attr.bv_val );
502 goto done;
503 }
504
505 /* Apply modification */
506 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
507 mod_tmp->sml_desc = desc;
508 BER_BVZERO( &mod_tmp->sml_type );
509 mod_tmp->sml_numvals = 1;
510 mod_tmp->sml_values = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
511 ber_dupbv( &mod_tmp->sml_values[0], &old_rdn[d_cnt]->la_value );
512 mod_tmp->sml_values[1].bv_val = NULL;
513 if( desc->ad_type->sat_equality->smr_normalize) {
514 mod_tmp->sml_nvalues = ( BerVarray )ch_malloc( 2 * sizeof( struct berval ) );
515 (void) (*desc->ad_type->sat_equality->smr_normalize)(
516 SLAP_MR_EQUALITY|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
517 desc->ad_type->sat_syntax,
518 desc->ad_type->sat_equality,
519 &mod_tmp->sml_values[0],
520 &mod_tmp->sml_nvalues[0], NULL );
521 mod_tmp->sml_nvalues[1].bv_val = NULL;
522 } else {
523 mod_tmp->sml_nvalues = NULL;
524 }
525 mod_tmp->sml_op = LDAP_MOD_DELETE;
526 mod_tmp->sml_flags = 0;
527 mod_tmp->sml_next = op->orr_modlist;
528 op->orr_modlist = mod_tmp;
529 }
530 }
531
532 done:
533
534 /* LDAP v2 supporting correct attribute handling. */
535 if ( rs->sr_err != LDAP_SUCCESS && op->orr_modlist != NULL ) {
536 Modifications *tmp;
537
538 for ( ; op->orr_modlist != NULL; op->orr_modlist = tmp ) {
539 tmp = op->orr_modlist->sml_next;
540 ch_free( op->orr_modlist );
541 }
542 }
543
544 if ( new_rdn != NULL ) {
545 ldap_rdnfree_x( new_rdn, op->o_tmpmemctx );
546 }
547 if ( old_rdn != NULL ) {
548 ldap_rdnfree_x( old_rdn, op->o_tmpmemctx );
549 }
550
551 return rs->sr_err;
552 }
553
554