1 /* modrdn.c - ldbm backend modrdn routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
17 * This software is not subject to any license of Silicon Graphics
18 * Inc. or Purdue University.
20 * Redistribution and use in source and binary forms are permitted
21 * without restriction or fee of any kind as long as this notice
29 #include <ac/string.h>
30 #include <ac/socket.h>
33 #include "back-ldbm.h"
34 #include "proto-back-ldbm.h"
41 AttributeDescription *children = slap_schema.si_ad_children;
42 AttributeDescription *entry = slap_schema.si_ad_entry;
43 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
44 struct berval p_dn, p_ndn;
45 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
46 struct berval old_ndn = BER_BVNULL;
49 /* LDAP v2 supporting correct attribute handling. */
50 LDAPRDN new_rdn = NULL;
51 LDAPRDN old_rdn = NULL;
54 char textbuf[SLAP_TEXT_BUFLEN];
55 size_t textlen = sizeof textbuf;
56 /* Added to support newSuperior */
57 Entry *np = NULL; /* newSuperior Entry */
58 struct berval *np_ndn = NULL; /* newSuperior ndn */
59 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
60 /* Used to interface with ldbm_modify_internal() */
61 Modifications *mod = NULL; /* Used to delete old/add new rdn */
62 int manageDSAit = get_manageDSAit( op );
64 Debug( LDAP_DEBUG_TRACE,
65 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n",
66 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
67 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
68 ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
70 /* grab giant lock for writing */
71 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
73 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
75 /* get entry with writer lock */
76 /* FIXME: dn2entry() should return non-glue entry */
77 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
78 if ( matched != NULL ) {
79 rs->sr_matched = strdup( matched->e_dn );
80 rs->sr_ref = is_entry_referral( matched )
81 ? get_entry_referrals( op, matched )
83 cache_return_entry_r( &li->li_cache, matched );
85 rs->sr_ref = referral_rewrite( default_referral, NULL,
86 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
89 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
91 rs->sr_err = LDAP_REFERRAL;
92 send_ldap_result( op, rs );
94 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
95 free( (char *)rs->sr_matched );
97 rs->sr_matched = NULL;
101 /* check entry for "entry" acl */
102 if ( ! access_allowed( op, e, entry, NULL, ACL_WRITE, NULL ) )
104 Debug( LDAP_DEBUG_TRACE,
105 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
108 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
109 "no write access to entry" );
114 if (!manageDSAit && is_entry_referral( e ) ) {
115 /* parent is a referral, don't allow add */
116 /* parent is an alias, don't allow add */
117 rs->sr_ref = get_entry_referrals( op, e );
119 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
122 rs->sr_err = LDAP_REFERRAL;
123 rs->sr_matched = e->e_name.bv_val;
124 send_ldap_result( op, rs );
126 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
128 rs->sr_matched = NULL;
132 if ( has_children( op->o_bd, e ) ) {
133 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
136 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
137 "subtree rename not supported" );
141 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
142 p_ndn = slap_empty_bv ;
144 dnParent( &e->e_nname, &p_ndn );
147 if ( p_ndn.bv_len != 0 ) {
148 /* Make sure parent entry exist and we can write its
152 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
153 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
156 send_ldap_error( op, rs, LDAP_OTHER,
157 "parent entry does not exist" );
162 p = (Entry *)&slap_entry_root;
165 /* check parent for "children" acl */
166 if ( ! access_allowed( op, p, children, NULL,
167 op->oq_modrdn.rs_newSup != NULL ?
168 ACL_WDEL : ACL_WRITE,
171 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
174 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
179 if ( BER_BVISEMPTY( &p_ndn ))
182 Debug( LDAP_DEBUG_TRACE,
183 "ldbm_back_modrdn: wr to children of entry %s OK\n",
184 p_ndn.bv_val, 0, 0 );
186 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
187 p_dn = slap_empty_bv;
189 dnParent( &e->e_name, &p_dn );
192 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
195 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
197 if ( op->oq_modrdn.rs_newSup != NULL ) {
198 Debug( LDAP_DEBUG_TRACE,
199 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
200 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
202 np_ndn = op->oq_modrdn.rs_nnewSup;
204 /* newSuperior == oldParent? */
205 if ( dn_match( &p_ndn, np_ndn ) ) {
206 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
207 "new parent\"%s\" seems to be the same as the "
208 "old parent \"%s\"\n",
209 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
211 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
215 if ( op->oq_modrdn.rs_newSup != NULL ) {
216 /* newSuperior == entry being moved?, if so ==> ERROR */
217 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
219 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
220 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
221 Debug( LDAP_DEBUG_TRACE,
222 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
223 np_ndn->bv_val, 0, 0);
225 send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT,
226 "newSuperior not found" );
230 Debug( LDAP_DEBUG_TRACE,
231 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
232 (void *) np, np->e_id, 0 );
234 /* check newSuperior for "children" acl */
235 if ( !access_allowed( op, np, children, NULL,
238 Debug( LDAP_DEBUG_TRACE,
239 "ldbm_back_modrdn: no wr to newSup children\n",
242 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
246 if ( is_entry_alias( np ) ) {
247 /* parent is an alias, don't allow add */
248 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
251 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
252 "newSuperior is an alias" );
257 if ( is_entry_referral( np ) ) {
258 /* parent is a referral, don't allow add */
259 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
262 send_ldap_error( op, rs, LDAP_OTHER,
263 "newSuperior is a referral" );
269 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
270 || be_shadow_update( op ) ) {
272 np = (Entry *)&slap_entry_root;
274 can_access = access_allowed( op, np,
275 children, NULL, ACL_WADD, NULL );
278 /* check parent for "children" acl */
279 if ( ! can_access ) {
280 Debug( LDAP_DEBUG_TRACE,
281 "<=- ldbm_back_modrdn: no "
282 "access to new superior\n", 0, 0, 0 );
284 send_ldap_error( op, rs,
285 LDAP_INSUFFICIENT_ACCESS,
291 Debug( LDAP_DEBUG_TRACE,
292 "<=- ldbm_back_modrdn: \"\" "
293 "not allowed as new superior\n",
296 send_ldap_error( op, rs,
297 LDAP_INSUFFICIENT_ACCESS,
303 Debug( LDAP_DEBUG_TRACE,
304 "ldbm_back_modrdn: wr to new parent's children OK\n",
307 new_parent_dn = op->oq_modrdn.rs_newSup;
310 /* Build target dn and make sure target entry doesn't exist already. */
311 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL );
312 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
314 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
315 new_ndn.bv_val, 0, 0 );
317 /* check for abandon */
318 if ( op->o_abandon ) {
319 rs->sr_err = SLAPD_ABANDON;
323 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
324 /* if (rc_id) something bad happened to ldbm cache */
325 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
326 send_ldap_result( op, rs );
330 Debug( LDAP_DEBUG_TRACE,
331 "ldbm_back_modrdn: new ndn=%s does not exist\n",
332 new_ndn.bv_val, 0, 0 );
334 /* Get attribute type and attribute value of our new rdn, we will
335 * need to add that to our new entry
337 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
338 LDAP_DN_FORMAT_LDAP ) )
340 Debug( LDAP_DEBUG_TRACE,
341 "ldbm_back_modrdn: can't figure out "
342 "type(s)/values(s) of newrdn\n",
344 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
345 "unknown type(s) used in RDN" );
349 Debug( LDAP_DEBUG_TRACE,
350 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
351 "new_rdn_val=\"%s\"\n",
352 new_rdn[ 0 ]->la_attr.bv_val,
353 new_rdn[ 0 ]->la_value.bv_val, 0 );
355 if ( op->oq_modrdn.rs_deleteoldrdn ) {
356 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
357 LDAP_DN_FORMAT_LDAP ) )
359 Debug( LDAP_DEBUG_TRACE,
360 "ldbm_back_modrdn: can't figure out "
361 "the old_rdn type(s)/value(s)\n",
363 send_ldap_error( op, rs, LDAP_OTHER,
364 "cannot parse RDN from old DN" );
369 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
372 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
373 send_ldap_result( op, rs );
377 /* check for abandon */
378 if ( op->o_abandon ) {
379 rs->sr_err = SLAPD_ABANDON;
383 (void) cache_delete_entry( &li->li_cache, e );
386 old_ndn = e->e_nname;
388 e->e_nname = new_ndn;
389 new_dn.bv_val = NULL;
390 new_ndn.bv_val = NULL;
392 /* NOTE: after this you must not free new_dn or new_ndn!
393 * They are used by cache.
396 /* modify memory copy of entry */
397 rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
398 &rs->sr_text, textbuf, textlen );
399 switch ( rs->sr_err ) {
404 send_ldap_result( op, rs );
411 * NOTE: the backend MUST delete then add the entry,
412 * otherwise indexing may get hosed
413 * FIXME: if a new ID was used, the add could be done first.
414 * that would be safer.
418 if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
419 send_ldap_error( op, rs, LDAP_OTHER,
420 "DN index delete fail" );
425 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
426 /* try to repair old entry - probably hopeless */
427 if( dn2id_add( op->o_bd, &old_ndn, e->e_id) != 0 ) {
428 send_ldap_error( op, rs, LDAP_OTHER,
429 "DN index add and repair failed" );
431 send_ldap_error( op, rs, LDAP_OTHER,
432 "DN index add failed" );
438 if ( id2entry_add( op->o_bd, e ) != 0 ) {
441 rc = dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
442 rc |= dn2id_add( op->o_bd, &old_ndn, e->e_id );
444 send_ldap_error( op, rs, LDAP_OTHER,
445 "entry update and repair failed" );
447 send_ldap_error( op, rs, LDAP_OTHER,
448 "entry update failed" );
453 (void) cache_update_entry( &li->li_cache, e );
455 rs->sr_err = LDAP_SUCCESS;
457 send_ldap_result( op, rs );
458 cache_entry_commit( e );
461 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
462 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
463 if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
465 /* LDAP v2 supporting correct attribute handling. */
466 if ( new_rdn != NULL ) {
467 ldap_rdnfree( new_rdn );
469 if ( old_rdn != NULL ) {
470 ldap_rdnfree( old_rdn );
474 for (; mod; mod = tmp ) {
475 /* slap_modrdn2mods does things one way,
476 * slap_mods_opattrs does it differently
478 if ( mod->sml_op != SLAP_MOD_SOFTADD &&
479 mod->sml_op != LDAP_MOD_DELETE ) break;
480 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
484 slap_mods_free( mod, 1 );
487 /* LDAP v3 Support */
489 /* free new parent and writer lock */
490 cache_return_entry_w( &li->li_cache, np );
494 /* free parent and writer lock */
495 cache_return_entry_w( &li->li_cache, p );
498 /* free entry and writer lock */
499 cache_return_entry_w( &li->li_cache, e );
500 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
502 return( rs->sr_err );