]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/modrdn.c
add referral check to functions elaborated by overlays
[openldap] / servers / slapd / back-ldbm / modrdn.c
index de0d99b8868636fcc41b9b8f76a508a019f8d84c..eb463e3b96a8d4fd7cd9ebc919a392024c34b4f2 100644 (file)
@@ -1,24 +1,25 @@
 /* modrdn.c - ldbm backend modrdn routine */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
-
-/*
- * LDAP v3 newSuperior support. Add new rdn as an attribute.
- * (Full support for v2 also used software/ideas contributed
- * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
- * submission!.)
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2004 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
- * Copyright 1999, Juan C. Gomez, All rights reserved.
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright 1999, Juan C. Gomez, All rights reserved.
  * This software is not subject to any license of Silicon Graphics 
  * Inc. or Purdue University.
  *
  * Redistribution and use in source and binary forms are permitted
  * without restriction or fee of any kind as long as this notice
  * is preserved.
- *
  */
 
 #include "portable.h"
@@ -42,15 +43,13 @@ ldbm_back_modrdn(
        struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
        struct berval   p_dn, p_ndn;
        struct berval   new_dn = { 0, NULL}, new_ndn = { 0, NULL };
+       struct berval   old_ndn = { 0, NULL };
        Entry           *e, *p = NULL;
        Entry           *matched;
        /* LDAP v2 supporting correct attribute handling. */
-       LDAPRDN         *new_rdn = NULL;
-       LDAPRDN         *old_rdn = NULL;
+       LDAPRDN         new_rdn = NULL;
+       LDAPRDN         old_rdn = NULL;
        int             isroot = -1;
-#define CAN_ROLLBACK   -1
-#define MUST_DESTROY   1
-       int             rc = CAN_ROLLBACK;
        int             rc_id = 0;
        ID              id = NOID;
        const char      *text = NULL;
@@ -80,17 +79,31 @@ ldbm_back_modrdn(
        /* grab giant lock for writing */
        ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
 
+       e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
+
        /* get entry with writer lock */
-       if ( (e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
-               if( matched != NULL ) {
+       /* FIXME: dn2entry() should return non-glue entry */
+       if (( e == NULL  ) || ( !manageDSAit && e && is_entry_glue( e ))) {
+               if ( matched != NULL ) {
                        rs->sr_matched = strdup( matched->e_dn );
                        rs->sr_ref = is_entry_referral( matched )
                                ? get_entry_referrals( op, matched )
                                : NULL;
                        cache_return_entry_r( &li->li_cache, matched );
                } else {
-                       rs->sr_ref = referral_rewrite( default_referral,
-                               NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
+                       BerVarray deref = NULL;
+                       if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
+                               syncinfo_t *si;
+                               LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
+                                       struct berval tmpbv;
+                                       ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
+                                       ber_bvarray_add( &deref, &tmpbv );
+                               }
+                       } else {
+                               deref = default_referral;
+                       }
+                       rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
+                                                       LDAP_SCOPE_DEFAULT );
                }
 
                ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
@@ -100,8 +113,9 @@ ldbm_back_modrdn(
 
                if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
                free( (char *)rs->sr_matched );
-
-               return( -1 );
+               rs->sr_ref = NULL;
+               rs->sr_matched = NULL;
+               return rs->sr_err;
        }
 
        /* check entry for "entry" acl */
@@ -111,7 +125,7 @@ ldbm_back_modrdn(
 #ifdef NEW_LOGGING
                LDAP_LOG( BACK_LDBM, ERR, 
                        "ldbm_back_modrdn: no write access to entry of (%s)\n", 
-                       dn->bv_val, 0, 0 );
+                       op->o_req_dn.bv_val, 0, 0 );
 #else
                Debug( LDAP_DEBUG_TRACE,
                        "<=- ldbm_back_modrdn: no write access to entry\n", 0,
@@ -142,6 +156,8 @@ ldbm_back_modrdn(
                send_ldap_result( op, rs );
 
                if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
+               rs->sr_ref = NULL;
+               rs->sr_matched = NULL;
                goto return_results;
        }
 
@@ -261,7 +277,7 @@ ldbm_back_modrdn(
 #ifdef NEW_LOGGING
                                LDAP_LOG( BACK_LDBM, ERR, 
                                        "ldbm_back_modrdn: (%s) has no parent & not a root.\n", 
-                                       dn, 0, 0 );
+                                       op->o_ndn, 0, 0 );
 #else
                                Debug( LDAP_DEBUG_TRACE,
                                        "<=- ldbm_back_modrdn: no parent & "
@@ -341,11 +357,11 @@ ldbm_back_modrdn(
 #ifdef NEW_LOGGING
                        LDAP_LOG( BACK_LDBM, DETAIL1,
                                "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
-                               np, np->e_id, 0 );
+                               (void *) np, np->e_id, 0 );
 #else
                        Debug( LDAP_DEBUG_TRACE,
                                "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
-                               np, np->e_id, 0 );
+                               (void *) np, np->e_id, 0 );
 #endif
 
                        /* check newSuperior for "children" acl */
@@ -465,8 +481,8 @@ ldbm_back_modrdn(
        }
        
        /* Build target dn and make sure target entry doesn't exist already. */
-       build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn ); 
-       dnNormalize2( NULL, &new_dn, &new_ndn );
+       build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
+       dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
 
 #ifdef NEW_LOGGING
        LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n", 
@@ -514,6 +530,8 @@ ldbm_back_modrdn(
                        "type(s)/values(s) of newrdn\n", 
                        0, 0, 0 );
 #endif
+               send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX,
+                                   "unknown type(s) used in RDN" );
                goto return_results;            
        }
 
@@ -521,14 +539,14 @@ ldbm_back_modrdn(
        LDAP_LOG ( OPERATION, RESULTS, 
                "ldbm_back_modrdn: new_rdn_type=\"%s\", "
                "new_rdn_val=\"%s\"\n",
-               new_rdn[ 0 ][ 0 ]->la_attr.bv_val, 
-               new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
+               new_rdn[ 0 ]->la_attr.bv_val, 
+               new_rdn[ 0 ]->la_value.bv_val, 0 );
 #else
        Debug( LDAP_DEBUG_TRACE,
                "ldbm_back_modrdn: new_rdn_type=\"%s\", "
                "new_rdn_val=\"%s\"\n",
-               new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
-               new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
+               new_rdn[ 0 ]->la_attr.bv_val,
+               new_rdn[ 0 ]->la_value.bv_val, 0 );
 #endif
 
        if ( op->oq_modrdn.rs_deleteoldrdn ) {
@@ -546,6 +564,8 @@ ldbm_back_modrdn(
                                "the old_rdn type(s)/value(s)\n", 
                                0, 0, 0 );
 #endif
+                       send_ldap_error( op, rs, LDAP_OTHER,
+                                   "cannot parse RDN from old DN" );
                        goto return_results;            
                }
        }
@@ -558,29 +578,19 @@ ldbm_back_modrdn(
 #endif
        
        if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
+               send_ldap_result( op, rs );
                goto return_results;
        }
 
-
        /* check for abandon */
        if ( op->o_abandon ) {
                goto return_results;
        }
 
-       /* delete old one */
-       if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
-               send_ldap_error( op, rs, LDAP_OTHER,
-                       "DN index delete fail" );
-               goto return_results;
-       }
-
        (void) cache_delete_entry( &li->li_cache, e );
-       rc = MUST_DESTROY;
-
-       /* XXX: there is no going back! */
 
        free( e->e_dn );
-       free( e->e_ndn );
+       old_ndn = e->e_nname;
        e->e_name = new_dn;
        e->e_nname = new_ndn;
        new_dn.bv_val = NULL;
@@ -590,52 +600,56 @@ ldbm_back_modrdn(
         * They are used by cache.
         */
 
-       /* add new one */
-       if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
-               send_ldap_error( op, rs, LDAP_OTHER,
-                       "DN index add failed" );
-               goto return_results;
-       }
-
        /* modify memory copy of entry */
-       rc_id = ldbm_modify_internal( op, &mod[0], e,
+       rs->sr_err = ldbm_modify_internal( op, &mod[0], e,
                &rs->sr_text, textbuf, textlen );
-       switch ( rc_id ) {
+       switch ( rs->sr_err ) {
        case LDAP_SUCCESS:
                break;
 
-       case SLAPD_ABANDON:
-               /* too late ... */
-               rs->sr_err = rc_id;
-               send_ldap_result( op, rs );
-               goto return_results;
-       
        default:
-               /* here we may try to delete the newly added dn */
-               if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
-                       /* we already are in trouble ... */
-                       ;
-               }
+               send_ldap_result( op, rs );
+               /* FALLTHRU */
+       case SLAPD_ABANDON:
                goto return_results;
        }
        
-       (void) cache_update_entry( &li->li_cache, e );
+       /* add new one */
+       if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
+               send_ldap_error( op, rs, LDAP_OTHER,
+                       "DN index add failed" );
+               goto return_results;
+       }
+       /* delete old one */
+       if ( dn2id_delete( op->o_bd, &old_ndn, e->e_id ) != 0 ) {
+               /* undo add of new one */
+               dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
+               send_ldap_error( op, rs, LDAP_OTHER,
+                       "DN index delete fail" );
+               goto return_results;
+       }
 
        /* id2entry index */
        if ( id2entry_add( op->o_bd, e ) != 0 ) {
+               /* Try to undo */
+               dn2id_delete( op->o_bd, &e->e_nname, e->e_id );
+               dn2id_add( op->o_bd, &old_ndn, e->e_id );
                send_ldap_error( op, rs, LDAP_OTHER,
                        "entry update failed" );
                goto return_results;
        }
 
+       (void) cache_update_entry( &li->li_cache, e );
+
        rs->sr_err = LDAP_SUCCESS;
+       rs->sr_text = NULL;
        send_ldap_result( op, rs );
-       rc = 0;
        cache_entry_commit( e );
 
 return_results:
        if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
        if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
+       if( old_ndn.bv_val != NULL ) free( old_ndn.bv_val );
 
        /* LDAP v2 supporting correct attribute handling. */
        if ( new_rdn != NULL ) {
@@ -647,10 +661,16 @@ return_results:
        if ( mod != NULL ) {
                Modifications *tmp;
                for (; mod; mod = tmp ) {
+                       /* slap_modrdn2mods does things one way,
+                        * slap_mods_opattrs does it differently
+                        */
+                       if ( mod->sml_op != SLAP_MOD_SOFTADD &&
+                               mod->sml_op != LDAP_MOD_DELETE ) break;
                        if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
                        tmp = mod->sml_next;
                        free( mod );
                }
+               slap_mods_free( mod );
        }
 
        /* LDAP v3 Support */
@@ -666,12 +686,7 @@ return_results:
 
        /* free entry and writer lock */
        cache_return_entry_w( &li->li_cache, e );
-       if ( rc == MUST_DESTROY ) {
-               /* if rc == MUST_DESTROY the entry is uncached 
-                * and its private data is destroyed; 
-                * the entry must be freed */
-               entry_free( e );
-       }
        ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
-       return( rc );
+       rs->sr_text = NULL;
+       return( rs->sr_err );
 }