]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/map.c
Fix DEVEL dependency
[openldap] / servers / slapd / back-meta / map.c
index a76c5f0f856f92ddc1418dbaa69fd2937966ff12..1871ebbddf9521c2306f4548c1dd6dfbc023e378 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2003 The OpenLDAP Foundation.
+ * Copyright 1998-2007 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -70,7 +70,7 @@ mapping_cmp ( const void *c1, const void *c2 )
        struct ldapmapping *map2 = (struct ldapmapping *)c2;
        int rc = map1->src.bv_len - map2->src.bv_len;
        if (rc) return rc;
-       return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
+       return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) );
 }
 
 int
@@ -79,7 +79,7 @@ mapping_dup ( void *c1, void *c2 )
        struct ldapmapping *map1 = (struct ldapmapping *)c1;
        struct ldapmapping *map2 = (struct ldapmapping *)c2;
 
-       return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
+       return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 );
 }
 
 void
@@ -87,54 +87,78 @@ ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
 {
        struct ldapmapping *mapping;
 
-       assert( m );
+       assert( m != NULL );
 
        *m = NULL;
-       
+
        mapping = (struct ldapmapping *)ch_calloc( 2, 
                        sizeof( struct ldapmapping ) );
        if ( mapping == NULL ) {
                return;
        }
 
-       ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
-       ber_dupbv( &mapping->dst, &mapping->src );
-       mapping[1].src = mapping->src;
-       mapping[1].dst = mapping->dst;
+       ber_str2bv( "objectclass", STRLENOF("objectclass"), 1, &mapping[0].src);
+       ber_dupbv( &mapping[0].dst, &mapping[0].src );
+       mapping[1].src = mapping[0].src;
+       mapping[1].dst = mapping[0].dst;
 
-       avl_insert( &lm->map, (caddr_t)mapping
+       avl_insert( &lm->map, (caddr_t)&mapping[0]
                        mapping_cmp, mapping_dup );
        avl_insert( &lm->remap, (caddr_t)&mapping[1], 
                        mapping_cmp, mapping_dup );
        *m = mapping;
 }
 
-void
-ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
+int
+ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m,
        int remap )
 {
        Avlnode *tree;
-       struct ldapmapping *mapping, fmapping;
+       struct ldapmapping fmapping;
 
-       if (remap == BACKLDAP_REMAP)
+       assert( m != NULL );
+
+       if ( remap == BACKLDAP_REMAP ) {
                tree = map->remap;
-       else
+
+       } else {
                tree = map->map;
+       }
 
-       bv->bv_len = 0;
-       bv->bv_val = NULL;
        fmapping.src = *s;
-       mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
-       if (mapping != NULL) {
-               if ( mapping->dst.bv_val )
+       *m = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
+       if ( *m == NULL ) {
+               return map->drop_missing;
+       }
+
+       return 0;
+}
+
+void
+ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
+       int remap )
+{
+       struct ldapmapping *mapping;
+
+       /* map->map may be NULL when mapping is configured,
+        * but map->remap can't */
+       if ( map->remap == NULL ) {
+               *bv = *s;
+               return;
+       }
+
+       BER_BVZERO( bv );
+       ( void )ldap_back_mapping( map, s, &mapping, remap );
+       if ( mapping != NULL ) {
+               if ( !BER_BVISNULL( &mapping->dst ) ) {
                        *bv = mapping->dst;
+               }
                return;
        }
 
-       if (!map->drop_missing)
+       if ( !map->drop_missing ) {
                *bv = *s;
-
-       return;
+       }
 }
 
 int
@@ -149,28 +173,29 @@ ldap_back_map_attrs(
        char **na;
        struct berval mapped;
 
-       if (an == NULL) {
+       if ( an == NULL ) {
                *mapped_attrs = NULL;
                return LDAP_SUCCESS;
        }
 
-       for (i = 0; an[i].an_name.bv_val; i++) {
-               /*  */
-       }
+       for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
+               /*  */ ;
 
        na = (char **)ch_calloc( i + 1, sizeof(char *) );
-       if (na == NULL) {
+       if ( na == NULL ) {
                *mapped_attrs = NULL;
                return LDAP_NO_MEMORY;
        }
 
-       for (i = j = 0; an[i].an_name.bv_val; i++) {
-               ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
-               if (mapped.bv_val != NULL && mapped.bv_val != '\0')
+       for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
+               ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
+               if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
                        na[j++] = mapped.bv_val;
+               }
        }
-       if (j == 0 && i != 0)
+       if ( j == 0 && i != 0 ) {
                na[j++] = LDAP_NO_ATTRS;
+       }
        na[j] = NULL;
 
        *mapped_attrs = na;
@@ -189,15 +214,22 @@ map_attr_value(
        struct berval           vtmp;
        int                     freeval = 0;
 
-       ldap_back_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
-       if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
+       ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap );
+       if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
+#if 0
                /*
                 * FIXME: are we sure we need to search oc_map if at_map fails?
                 */
-               ldap_back_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
-               if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
+               ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap );
+               if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) {
                        *mapped_attr = ad->ad_cname;
                }
+#endif
+               if ( dc->target->mt_rwmap.rwm_at.drop_missing ) {
+                       return -1;
+               }
+
+               *mapped_attr = ad->ad_cname;
        }
 
        if ( value == NULL ) {
@@ -227,8 +259,8 @@ map_attr_value(
                }
 
        } else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
-               ldap_back_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
-               if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
+               ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap );
+               if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) {
                        vtmp = *value;
                }
                
@@ -254,13 +286,31 @@ ldap_back_int_filter_map_rewrite(
 {
        int             i;
        Filter          *p;
-       struct berval   atmp;
-       struct berval   vtmp;
+       struct berval   atmp,
+                       vtmp,
+                       *tmp;
+       static struct berval
+                       /* better than nothing... */
+                       ber_bvfalse = BER_BVC( "(!(objectClass=*))" ),
+                       ber_bvtf_false = BER_BVC( "(|)" ),
+                       /* better than nothing... */
+                       ber_bvtrue = BER_BVC( "(objectClass=*)" ),
+                       ber_bvtf_true = BER_BVC( "(&)" ),
+#if 0
+                       /* no longer needed; preserved for completeness */
+                       ber_bvundefined = BER_BVC( "(?=undefined)" ),
+#endif
+                       ber_bverror = BER_BVC( "(?=error)" ),
+                       ber_bvunknown = BER_BVC( "(?=unknown)" ),
+                       ber_bvnone = BER_BVC( "(?=none)" );
        ber_len_t       len;
 
+       assert( fstr != NULL );
+       BER_BVZERO( fstr );
+
        if ( f == NULL ) {
-               ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
-               return -1;
+               ber_dupbv( fstr, &ber_bvnone );
+               return LDAP_OTHER;
        }
 
        switch ( f->f_choice ) {
@@ -268,7 +318,7 @@ ldap_back_int_filter_map_rewrite(
                if ( map_attr_value( dc, f->f_av_desc, &atmp,
                                        &f->f_av_value, &vtmp, remap ) )
                {
-                       return -1;
+                       goto computed;
                }
 
                fstr->bv_len = atmp.bv_len + vtmp.bv_len
@@ -276,7 +326,7 @@ ldap_back_int_filter_map_rewrite(
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
-                       atmp.bv_val, vtmp.bv_val );
+                       atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
 
                ber_memfree( vtmp.bv_val );
                break;
@@ -285,7 +335,7 @@ ldap_back_int_filter_map_rewrite(
                if ( map_attr_value( dc, f->f_av_desc, &atmp,
                                        &f->f_av_value, &vtmp, remap ) )
                {
-                       return -1;
+                       goto computed;
                }
 
                fstr->bv_len = atmp.bv_len + vtmp.bv_len
@@ -293,7 +343,7 @@ ldap_back_int_filter_map_rewrite(
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
-                       atmp.bv_val, vtmp.bv_val );
+                       atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
 
                ber_memfree( vtmp.bv_val );
                break;
@@ -302,7 +352,7 @@ ldap_back_int_filter_map_rewrite(
                if ( map_attr_value( dc, f->f_av_desc, &atmp,
                                        &f->f_av_value, &vtmp, remap ) )
                {
-                       return -1;
+                       goto computed;
                }
 
                fstr->bv_len = atmp.bv_len + vtmp.bv_len
@@ -310,7 +360,7 @@ ldap_back_int_filter_map_rewrite(
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
-                       atmp.bv_val, vtmp.bv_val );
+                       atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
 
                ber_memfree( vtmp.bv_val );
                break;
@@ -319,7 +369,7 @@ ldap_back_int_filter_map_rewrite(
                if ( map_attr_value( dc, f->f_av_desc, &atmp,
                                        &f->f_av_value, &vtmp, remap ) )
                {
-                       return -1;
+                       goto computed;
                }
 
                fstr->bv_len = atmp.bv_len + vtmp.bv_len
@@ -327,7 +377,7 @@ ldap_back_int_filter_map_rewrite(
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
-                       atmp.bv_val, vtmp.bv_val );
+                       atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" );
 
                ber_memfree( vtmp.bv_val );
                break;
@@ -336,18 +386,18 @@ ldap_back_int_filter_map_rewrite(
                if ( map_attr_value( dc, f->f_sub_desc, &atmp,
                                        NULL, NULL, remap ) )
                {
-                       return -1;
+                       goto computed;
                }
 
                /* cannot be a DN ... */
 
-               fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
-               fstr->bv_val = malloc( fstr->bv_len + 128 );
+               fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
+               fstr->bv_val = malloc( fstr->bv_len + 128 ); /* FIXME: why 128 ? */
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
                        atmp.bv_val );
 
-               if ( f->f_sub_initial.bv_val != NULL ) {
+               if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
                        len = fstr->bv_len;
 
                        filter_escape_value( &f->f_sub_initial, &vtmp );
@@ -357,13 +407,13 @@ ldap_back_int_filter_map_rewrite(
 
                        snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3,
                                /* "(attr=" */ "%s*)",
-                               vtmp.bv_val );
+                               vtmp.bv_len ? vtmp.bv_val : "" );
 
                        ber_memfree( vtmp.bv_val );
                }
 
                if ( f->f_sub_any != NULL ) {
-                       for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
+                       for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
                                len = fstr->bv_len;
                                filter_escape_value( &f->f_sub_any[i], &vtmp );
 
@@ -372,12 +422,12 @@ ldap_back_int_filter_map_rewrite(
 
                                snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
                                        /* "(attr=[init]*[any*]" */ "%s*)",
-                                       vtmp.bv_val );
+                                       vtmp.bv_len ? vtmp.bv_val : "" );
                                ber_memfree( vtmp.bv_val );
                        }
                }
 
-               if ( f->f_sub_final.bv_val != NULL ) {
+               if ( !BER_BVISNULL( &f->f_sub_final ) ) {
                        len = fstr->bv_len;
 
                        filter_escape_value( &f->f_sub_final, &vtmp );
@@ -387,7 +437,7 @@ ldap_back_int_filter_map_rewrite(
 
                        snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3,
                                /* "(attr=[init*][any*]" */ "%s)",
-                               vtmp.bv_val );
+                               vtmp.bv_len ? vtmp.bv_val : "" );
 
                        ber_memfree( vtmp.bv_val );
                }
@@ -398,10 +448,10 @@ ldap_back_int_filter_map_rewrite(
                if ( map_attr_value( dc, f->f_desc, &atmp,
                                        NULL, NULL, remap ) )
                {
-                       return -1;
+                       goto computed;
                }
 
-               fstr->bv_len = atmp.bv_len + ( sizeof("(=*)") - 1 );
+               fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) );
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
@@ -411,82 +461,95 @@ ldap_back_int_filter_map_rewrite(
        case LDAP_FILTER_AND:
        case LDAP_FILTER_OR:
        case LDAP_FILTER_NOT:
-               fstr->bv_len = sizeof("(%)") - 1;
-               fstr->bv_val = malloc( fstr->bv_len + 128 );
+               fstr->bv_len = STRLENOF( "(%)" );
+               fstr->bv_val = malloc( fstr->bv_len + 128 );    /* FIXME: why 128? */
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
                        f->f_choice == LDAP_FILTER_AND ? '&' :
                        f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
 
                for ( p = f->f_list; p != NULL; p = p->f_next ) {
+                       int     rc;
+
                        len = fstr->bv_len;
 
-                       if ( ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap ) )
-                       {
-                               return -1;
+                       rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap );
+                       if ( rc != LDAP_SUCCESS ) {
+                               return rc;
                        }
                        
                        fstr->bv_len += vtmp.bv_len;
                        fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
 
                        snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, 
-                               /*"("*/ "%s)", vtmp.bv_val );
+                               /*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" );
 
                        ch_free( vtmp.bv_val );
                }
 
                break;
 
-       case LDAP_FILTER_EXT: {
+       case LDAP_FILTER_EXT:
                if ( f->f_mr_desc ) {
                        if ( map_attr_value( dc, f->f_mr_desc, &atmp,
                                                &f->f_mr_value, &vtmp, remap ) )
                        {
-                               return -1;
+                               goto computed;
                        }
 
                } else {
-                       atmp.bv_len = 0;
-                       atmp.bv_val = "";
-                       
+                       BER_BVSTR( &atmp, "" );
                        filter_escape_value( &f->f_mr_value, &vtmp );
                }
-                       
 
+               /* FIXME: cleanup (less ?: operators...) */
                fstr->bv_len = atmp.bv_len +
-                       ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
-                       ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
-                       vtmp.bv_len + ( sizeof("(:=)") - 1 );
+                       ( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) +
+                       ( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) +
+                       vtmp.bv_len + ( STRLENOF( "(:=)" ) );
                fstr->bv_val = malloc( fstr->bv_len + 1 );
 
                snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
                        atmp.bv_val,
                        f->f_mr_dnattrs ? ":dn" : "",
-                       f->f_mr_rule_text.bv_len ? ":" : "",
-                       f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
-                       vtmp.bv_val );
+                       !BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "",
+                       !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "",
+                       vtmp.bv_len ? vtmp.bv_val : "" );
                ber_memfree( vtmp.bv_val );
-               break;
+               break;
 
        case SLAPD_FILTER_COMPUTED:
                switch ( f->f_result ) {
                case LDAP_COMPARE_FALSE:
-                       ber_str2bv( "(?=false)", STRLENOF( "(?=false)" ), 1, fstr );
+               /* FIXME: treat UNDEFINED as FALSE */
+               case SLAPD_COMPARE_UNDEFINED:
+computed:;
+                       if ( META_BACK_TGT_T_F( dc->target ) ) {
+                               tmp = &ber_bvtf_false;
+                               break;
+                       }
+                       tmp = &ber_bvfalse;
                        break;
+
                case LDAP_COMPARE_TRUE:
-                       ber_str2bv( "(?=true)", STRLENOF( "(?=true)" ), 1, fstr );
-                       break;
-               case SLAPD_COMPARE_UNDEFINED:
-                       ber_str2bv( "(?=undefined)", STRLENOF( "(?=undefined)" ), 1, fstr );
+                       if ( META_BACK_TGT_T_F( dc->target ) ) {
+                               tmp = &ber_bvtf_true;
+                               break;
+                       }
+
+                       tmp = &ber_bvtrue;
                        break;
+
                default:
-                       ber_str2bv( "(?=error)", STRLENOF( "(?=error)" ), 1, fstr );
+                       tmp = &ber_bverror;
                        break;
                }
+
+               ber_dupbv( fstr, tmp );
                break;
 
        default:
-               ber_str2bv( "(?=unknown)", STRLENOF( "(?=unknown)" ), 1, fstr );
+               ber_dupbv( fstr, &ber_bvunknown );
                break;
        }
 
@@ -503,6 +566,7 @@ ldap_back_filter_map_rewrite(
        int             rc;
        dncookie        fdc;
        struct berval   ftmp;
+       static char     *dmy = "";
 
        rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap );
 
@@ -516,19 +580,21 @@ ldap_back_filter_map_rewrite(
 
        fdc.ctx = "searchFilter";
        
-       switch ( rewrite_session( fdc.rwmap->rwm_rw, fdc.ctx,
-                               ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : "" ),
+       switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx,
+                               ( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ),
                                fdc.conn, &fstr->bv_val ) )
        {
        case REWRITE_REGEXEC_OK:
                if ( !BER_BVISNULL( fstr ) ) {
                        fstr->bv_len = strlen( fstr->bv_val );
+
                } else {
                        *fstr = ftmp;
                }
                Debug( LDAP_DEBUG_ARGS,
                        "[rw] %s: \"%s\" -> \"%s\"\n",
-                       fdc.ctx, ftmp.bv_val, fstr->bv_val );           
+                       fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val,
+                       BER_BVISNULL( fstr ) ? "" : fstr->bv_val );             
                rc = LDAP_SUCCESS;
                break;
                
@@ -548,6 +614,10 @@ ldap_back_filter_map_rewrite(
                rc = LDAP_OTHER;
                break;
        }
+
+       if ( fstr->bv_val == dmy ) {
+               BER_BVZERO( fstr );
+       }
 #endif /* ENABLE_REWRITE */
 
        return rc;
@@ -561,15 +631,16 @@ ldap_back_referral_result_rewrite(
 {
        int             i, last;
 
-       assert( dc );
-       assert( a_vals );
+       assert( dc != NULL );
+       assert( a_vals != NULL );
 
        for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ )
                ;
        last--;
 
        for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) {
-               struct berval   dn, olddn;
+               struct berval   dn,
+                               olddn = BER_BVNULL;
                int             rc;
                LDAPURLDesc     *ludp;
 
@@ -579,6 +650,14 @@ ldap_back_referral_result_rewrite(
                        continue;
                }
 
+               /* FIXME: URLs like "ldap:///dc=suffix" if passed
+                * thru ldap_url_parse() and ldap_url_desc2str()
+                * get rewritten as "ldap:///dc=suffix??base";
+                * we don't want this to occur... */
+               if ( ludp->lud_scope == LDAP_SCOPE_BASE ) {
+                       ludp->lud_scope = LDAP_SCOPE_DEFAULT;
+               }
+
                ber_str2bv( ludp->lud_dn, 0, 0, &olddn );
                
                rc = ldap_back_dn_massage( dc, &olddn, &dn );
@@ -606,9 +685,11 @@ ldap_back_referral_result_rewrite(
 
                                ludp->lud_dn = dn.bv_val;
                                newurl = ldap_url_desc2str( ludp );
+                               free( dn.bv_val );
                                if ( newurl == NULL ) {
                                        /* FIXME: leave attr untouched
-                                        * even if ldap_url_desc2str failed... */
+                                        * even if ldap_url_desc2str failed...
+                                        */
                                        break;
                                }
 
@@ -642,10 +723,13 @@ ldap_dnattr_rewrite(
        struct berval   bv;
        int             i, last;
 
-       for ( last = 0; a_vals[last].bv_val != NULL; last++ );
+       assert( a_vals != NULL );
+
+       for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
+               ;
        last--;
 
-       for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
+       for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
                switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
                case LDAP_UNWILLING_TO_PERFORM:
                        /*
@@ -654,17 +738,16 @@ ldap_dnattr_rewrite(
                         * it should be when searching (e.g. ACLs).
                         */
                        ch_free( a_vals[i].bv_val );
-                       if (last > i ) {
+                       if ( last > i ) {
                                a_vals[i] = a_vals[last];
                        }
-                       a_vals[last].bv_len = 0;
-                       a_vals[last].bv_val = NULL;
+                       BER_BVZERO( &a_vals[last] );
                        last--;
                        break;
 
                default:
                        /* leave attr untouched if massage failed */
-                       if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
+                       if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) {
                                ch_free( a_vals[i].bv_val );
                                a_vals[i] = bv;
                        }
@@ -684,10 +767,13 @@ ldap_dnattr_result_rewrite(
        struct berval   bv;
        int             i, last;
 
-       for ( last = 0; a_vals[last].bv_val; last++ );
+       assert( a_vals != NULL );
+
+       for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ )
+               ;
        last--;
 
-       for ( i = 0; a_vals[i].bv_val; i++ ) {
+       for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) {
                switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
                case LDAP_UNWILLING_TO_PERFORM:
                        /*
@@ -705,7 +791,7 @@ ldap_dnattr_result_rewrite(
 
                default:
                        /* leave attr untouched if massage failed */
-                       if ( bv.bv_val && a_vals[i].bv_val != bv.bv_val ) {
+                       if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) {
                                LBER_FREE( a_vals[i].bv_val );
                                a_vals[i] = bv;
                        }