1 /* modrdn.c - ldbm backend modrdn routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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 = { 0, NULL}, new_ndn = { 0, NULL };
48 /* LDAP v2 supporting correct attribute handling. */
49 LDAPRDN new_rdn = NULL;
50 LDAPRDN old_rdn = NULL;
52 #define CAN_ROLLBACK -1
53 #define MUST_DESTROY 1
54 int rc = CAN_ROLLBACK;
57 const char *text = NULL;
58 char textbuf[SLAP_TEXT_BUFLEN];
59 size_t textlen = sizeof textbuf;
60 /* Added to support newSuperior */
61 Entry *np = NULL; /* newSuperior Entry */
62 struct berval *np_ndn = NULL; /* newSuperior ndn */
63 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
64 /* Used to interface with ldbm_modify_internal() */
65 Modifications *mod = NULL; /* Used to delete old/add new rdn */
66 int manageDSAit = get_manageDSAit( op );
69 LDAP_LOG( BACK_LDBM, ENTRY,
70 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
71 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
72 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
74 Debug( LDAP_DEBUG_TRACE,
75 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n",
76 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
77 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
78 ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
81 /* grab giant lock for writing */
82 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
84 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
86 /* get entry with writer lock */
87 /* FIXME: dn2entry() should return non-glue entry */
88 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
89 if ( matched != NULL ) {
90 rs->sr_matched = strdup( matched->e_dn );
91 rs->sr_ref = is_entry_referral( matched )
92 ? get_entry_referrals( op, matched )
94 cache_return_entry_r( &li->li_cache, matched );
96 BerVarray deref = NULL;
97 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
99 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
101 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
102 ber_bvarray_add( &deref, &tmpbv );
105 deref = default_referral;
107 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
108 LDAP_SCOPE_DEFAULT );
111 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
113 rs->sr_err = LDAP_REFERRAL;
114 send_ldap_result( op, rs );
116 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
117 free( (char *)rs->sr_matched );
119 rs->sr_matched = NULL;
123 /* check entry for "entry" acl */
124 if ( ! access_allowed( op, e,
125 entry, NULL, ACL_WRITE, NULL ) )
128 LDAP_LOG( BACK_LDBM, ERR,
129 "ldbm_back_modrdn: no write access to entry of (%s)\n",
130 op->o_req_dn.bv_val, 0, 0 );
132 Debug( LDAP_DEBUG_TRACE,
133 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
137 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
138 "no write access to entry" );
143 if (!manageDSAit && is_entry_referral( e ) ) {
144 /* parent is a referral, don't allow add */
145 /* parent is an alias, don't allow add */
146 rs->sr_ref = get_entry_referrals( op, e );
149 LDAP_LOG( BACK_LDBM, INFO,
150 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
152 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
156 rs->sr_err = LDAP_REFERRAL;
157 rs->sr_matched = e->e_name.bv_val;
158 send_ldap_result( op, rs );
160 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
162 rs->sr_matched = NULL;
166 if ( has_children( op->o_bd, e ) ) {
168 LDAP_LOG( BACK_LDBM, INFO,
169 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
171 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
175 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
176 "subtree rename not supported" );
180 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
181 p_ndn = slap_empty_bv ;
183 dnParent( &e->e_nname, &p_ndn );
186 if ( p_ndn.bv_len != 0 ) {
187 /* Make sure parent entry exist and we can write its
191 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
193 LDAP_LOG( BACK_LDBM, INFO,
194 "ldbm_back_modrdn: parent of %s does not exist\n",
197 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
201 send_ldap_error( op, rs, LDAP_OTHER,
202 "parent entry does not exist" );
207 /* check parent for "children" acl */
208 if ( ! access_allowed( op, p,
209 children, NULL, ACL_WRITE, NULL ) )
212 LDAP_LOG( BACK_LDBM, INFO,
213 "ldbm_back_modrdn: no access to parent of (%s)\n",
216 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
220 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
226 LDAP_LOG( BACK_LDBM, DETAIL1,
227 "ldbm_back_modrdn: wr to children of entry %s OK\n",
228 p_ndn.bv_val, 0, 0 );
230 Debug( LDAP_DEBUG_TRACE,
231 "ldbm_back_modrdn: wr to children of entry %s OK\n",
232 p_ndn.bv_val, 0, 0 );
235 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
236 p_dn = slap_empty_bv;
238 dnParent( &e->e_name, &p_dn );
242 LDAP_LOG( BACK_LDBM, DETAIL1,
243 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
250 /* no parent, must be root to modify rdn */
251 isroot = be_isroot( op->o_bd, &op->o_ndn );
253 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
255 p = (Entry *)&slap_entry_root;
257 can_access = access_allowed( op, p,
258 children, NULL, ACL_WRITE, NULL );
261 /* check parent for "children" acl */
262 if ( ! can_access ) {
264 LDAP_LOG( BACK_LDBM, ERR,
265 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
267 Debug( LDAP_DEBUG_TRACE,
268 "<=- ldbm_back_modrdn: no "
269 "access to parent\n", 0, 0, 0 );
272 send_ldap_error( op, rs,
273 LDAP_INSUFFICIENT_ACCESS,
280 LDAP_LOG( BACK_LDBM, ERR,
281 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
284 Debug( LDAP_DEBUG_TRACE,
285 "<=- ldbm_back_modrdn: no parent & "
286 "not root\n", 0, 0, 0);
289 send_ldap_error( op, rs,
290 LDAP_INSUFFICIENT_ACCESS,
297 LDAP_LOG( BACK_LDBM, INFO,
298 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
300 Debug( LDAP_DEBUG_TRACE,
301 "ldbm_back_modrdn: no parent, locked root\n",
306 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
308 if ( op->oq_modrdn.rs_newSup != NULL ) {
310 LDAP_LOG( BACK_LDBM, DETAIL1,
311 "ldbm_back_modrdn: new parent \"%s\" requested\n",
312 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
314 Debug( LDAP_DEBUG_TRACE,
315 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
316 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
319 np_ndn = op->oq_modrdn.rs_nnewSup;
321 /* newSuperior == oldParent? */
322 if ( dn_match( &p_ndn, np_ndn ) ) {
324 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
325 "new parent\"%s\" seems to be the same as the "
326 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
328 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
329 "new parent\"%s\" seems to be the same as the "
330 "old parent \"%s\"\n",
331 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
334 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
338 if ( op->oq_modrdn.rs_newSup != NULL ) {
339 /* newSuperior == entry being moved?, if so ==> ERROR */
340 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
342 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
343 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
345 LDAP_LOG( BACK_LDBM, ERR,
346 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
347 np_ndn->bv_val, 0, 0 );
349 Debug( LDAP_DEBUG_TRACE,
350 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
351 np_ndn->bv_val, 0, 0);
354 send_ldap_error( op, rs, LDAP_OTHER,
355 "newSuperior not found" );
360 LDAP_LOG( BACK_LDBM, DETAIL1,
361 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
362 (void *) np, np->e_id, 0 );
364 Debug( LDAP_DEBUG_TRACE,
365 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
366 (void *) np, np->e_id, 0 );
369 /* check newSuperior for "children" acl */
370 if ( !access_allowed( op, np, children, NULL,
374 LDAP_LOG( BACK_LDBM, INFO,
375 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
377 Debug( LDAP_DEBUG_TRACE,
378 "ldbm_back_modrdn: no wr to newSup children\n",
382 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
386 if ( is_entry_alias( np ) ) {
387 /* parent is an alias, don't allow add */
389 LDAP_LOG( BACK_LDBM, INFO,
390 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
392 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
396 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
397 "newSuperior is an alias" );
402 if ( is_entry_referral( np ) ) {
403 /* parent is a referral, don't allow add */
405 LDAP_LOG( BACK_LDBM, INFO,
406 "ldbm_back_modrdn: entry (%s) is a referral\n",
409 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
413 send_ldap_error( op, rs, LDAP_OTHER,
414 "newSuperior is a referral" );
421 /* no parent, must be root to modify newSuperior */
422 if ( isroot == -1 ) {
423 isroot = be_isroot( op->o_bd, &op->o_ndn );
427 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
429 np = (Entry *)&slap_entry_root;
431 can_access = access_allowed( op, np,
432 children, NULL, ACL_WRITE, NULL );
435 /* check parent for "children" acl */
436 if ( ! can_access ) {
438 LDAP_LOG( BACK_LDBM, ERR,
439 "ldbm_back_modrdn: no access "
440 "to new superior \"\"\n", 0, 0, 0 );
442 Debug( LDAP_DEBUG_TRACE,
443 "<=- ldbm_back_modrdn: no "
444 "access to new superior\n", 0, 0, 0 );
447 send_ldap_error( op, rs,
448 LDAP_INSUFFICIENT_ACCESS,
455 LDAP_LOG( BACK_LDBM, ERR,
456 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
459 Debug( LDAP_DEBUG_TRACE,
460 "<=- ldbm_back_modrdn: \"\" "
461 "not allowed as new superior\n",
465 send_ldap_error( op, rs,
466 LDAP_INSUFFICIENT_ACCESS,
474 LDAP_LOG( BACK_LDBM, DETAIL1,
475 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
477 Debug( LDAP_DEBUG_TRACE,
478 "ldbm_back_modrdn: wr to new parent's children OK\n",
482 new_parent_dn = op->oq_modrdn.rs_newSup;
485 /* Build target dn and make sure target entry doesn't exist already. */
486 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL );
487 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
490 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
491 new_ndn.bv_val, 0, 0 );
493 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
494 new_ndn.bv_val, 0, 0 );
497 /* check for abandon */
498 if ( op->o_abandon ) {
502 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
503 /* if (rc_id) something bad happened to ldbm cache */
504 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
505 send_ldap_result( op, rs );
510 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
511 new_ndn.bv_val, 0, 0 );
513 Debug( LDAP_DEBUG_TRACE,
514 "ldbm_back_modrdn: new ndn=%s does not exist\n",
515 new_ndn.bv_val, 0, 0 );
518 /* Get attribute type and attribute value of our new rdn, we will
519 * need to add that to our new entry
521 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
522 LDAP_DN_FORMAT_LDAP ) )
525 LDAP_LOG ( OPERATION, ERR,
526 "ldbm_back_modrdn: can't figure out "
527 "type(s)/values(s) of newrdn\n",
530 Debug( LDAP_DEBUG_TRACE,
531 "ldbm_back_modrdn: can't figure out "
532 "type(s)/values(s) of newrdn\n",
539 LDAP_LOG ( OPERATION, RESULTS,
540 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
541 "new_rdn_val=\"%s\"\n",
542 new_rdn[ 0 ]->la_attr.bv_val,
543 new_rdn[ 0 ]->la_value.bv_val, 0 );
545 Debug( LDAP_DEBUG_TRACE,
546 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
547 "new_rdn_val=\"%s\"\n",
548 new_rdn[ 0 ]->la_attr.bv_val,
549 new_rdn[ 0 ]->la_value.bv_val, 0 );
552 if ( op->oq_modrdn.rs_deleteoldrdn ) {
553 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
554 LDAP_DN_FORMAT_LDAP ) )
557 LDAP_LOG ( OPERATION, ERR,
558 "ldbm_back_modrdn: can't figure out "
559 "type(s)/values(s) of old_rdn\n",
562 Debug( LDAP_DEBUG_TRACE,
563 "ldbm_back_modrdn: can't figure out "
564 "the old_rdn type(s)/value(s)\n",
572 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
574 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
578 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
583 /* check for abandon */
584 if ( op->o_abandon ) {
589 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
590 send_ldap_error( op, rs, LDAP_OTHER,
591 "DN index delete fail" );
595 (void) cache_delete_entry( &li->li_cache, e );
598 /* XXX: there is no going back! */
603 e->e_nname = new_ndn;
604 new_dn.bv_val = NULL;
605 new_ndn.bv_val = NULL;
607 /* NOTE: after this you must not free new_dn or new_ndn!
608 * They are used by cache.
612 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
613 send_ldap_error( op, rs, LDAP_OTHER,
614 "DN index add failed" );
618 /* modify memory copy of entry */
619 rc_id = ldbm_modify_internal( op, &mod[0], e,
620 &rs->sr_text, textbuf, textlen );
628 send_ldap_result( op, rs );
632 /* here we may try to delete the newly added dn */
633 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
634 /* we already are in trouble ... */
640 (void) cache_update_entry( &li->li_cache, e );
643 if ( id2entry_add( op->o_bd, e ) != 0 ) {
644 send_ldap_error( op, rs, LDAP_OTHER,
645 "entry update failed" );
649 rs->sr_err = LDAP_SUCCESS;
651 send_ldap_result( op, rs );
653 cache_entry_commit( e );
656 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
657 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
659 /* LDAP v2 supporting correct attribute handling. */
660 if ( new_rdn != NULL ) {
661 ldap_rdnfree( new_rdn );
663 if ( old_rdn != NULL ) {
664 ldap_rdnfree( old_rdn );
668 for (; mod; mod = tmp ) {
669 /* slap_modrdn2mods does things one way,
670 * slap_mods_opattrs does it differently
672 if ( mod->sml_op != SLAP_MOD_SOFTADD &&
673 mod->sml_op != LDAP_MOD_DELETE ) break;
674 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
678 slap_mods_free( mod );
681 /* LDAP v3 Support */
683 /* free new parent and writer lock */
684 cache_return_entry_w( &li->li_cache, np );
688 /* free parent and writer lock */
689 cache_return_entry_w( &li->li_cache, p );
692 /* free entry and writer lock */
693 cache_return_entry_w( &li->li_cache, e );
694 if ( rc == MUST_DESTROY ) {
695 /* if rc == MUST_DESTROY the entry is uncached
696 * and its private data is destroyed;
697 * the entry must be freed */
700 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);