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