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