]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/getdn.c
Fix typo in last commit
[openldap] / libraries / libldap / getdn.c
index 15980e2c2c41e1f5b27830005fb72fdccf02b44a..23bd41b640c0ab855f0d2e66b00a50679527bf6c 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /*  Portions
 
 /* parsing/printing routines */
 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val, 
-               const char **next, unsigned flags, unsigned *retFlags );
+               const char **next, unsigned flags, int *retFlags, void *ctx );
 static int DCE2strval( const char *str, struct berval *val, 
-               const char **next, unsigned flags );
+               const char **next, unsigned flags, void *ctx );
 static int IA52strval( const char *str, struct berval *val, 
-               const char **next, unsigned flags );
+               const char **next, unsigned flags, void *ctx );
 static int quotedIA52strval( const char *str, struct berval *val, 
-               const char **next, unsigned flags );
+               const char **next, unsigned flags, void *ctx );
 static int hexstr2binval( const char *str, struct berval *val, 
-               const char **next, unsigned flags );
+               const char **next, unsigned flags, void *ctx );
 static int hexstr2bin( const char *str, char *c );
 static int byte2hexpair( const char *val, char *pair );
 static int binval2hexstr( struct berval *val, char *str );
@@ -57,23 +57,23 @@ static int strval2ADstrlen( struct berval *val, unsigned flags,
                ber_len_t *len );
 static int strval2ADstr( struct berval *val, char *str, unsigned flags, 
                ber_len_t *len );
-static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
+static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
 
 /* AVA helpers */
 static LDAPAVA * ldapava_new(
-       const struct berval *attr, const struct berval *val, unsigned flags );
+       const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
 
 /* Higher level helpers */
-static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
+static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
                int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
-static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
+static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
                int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
-static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len  );
-static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
-static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
-static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
-static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
-static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
+static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len  );
+static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
+static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
+static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
+static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
+static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
 
 /*
  * RFC 1823 ldap_get_dn
@@ -84,12 +84,15 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
        char            *dn;
        BerElement      tmp;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
+#endif
 
-       if ( entry == NULL ) {
-               ld->ld_errno = LDAP_PARAM_ERROR;
-               return( NULL );
-       }
+       assert( ld != NULL );
+       assert( LDAP_VALID(ld) );
+       assert( entry != NULL );
 
        tmp = *entry->lm_ber;   /* struct copy */
        if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
@@ -100,6 +103,57 @@ ldap_get_dn( LDAP *ld, LDAPMessage *entry )
        return( dn );
 }
 
+int
+ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
+       BerValue *dn )
+{
+       BerElement      tmp, *ber;
+       ber_len_t       len = 0;
+       int rc = LDAP_SUCCESS;
+
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
+#else
+       Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
+#endif
+
+       assert( ld != NULL );
+       assert( LDAP_VALID(ld) );
+       assert( entry != NULL );
+       assert( dn != NULL );
+
+       dn->bv_val = NULL;
+       dn->bv_len = 0;
+
+       if ( berout ) {
+               *berout = NULL;
+               ber = ldap_alloc_ber_with_options( ld );
+               if( ber == NULL ) {
+                       return LDAP_NO_MEMORY;
+               }
+               *berout = ber;
+       } else {
+               ber = &tmp;
+       }
+               
+       *ber = *entry->lm_ber;  /* struct copy */
+       if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
+               rc = ld->ld_errno = LDAP_DECODING_ERROR;
+       }
+       if ( rc == LDAP_SUCCESS ) {
+               /* set the length to avoid overrun */
+               rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
+               if( rc != LBER_OPT_SUCCESS ) {
+                       rc = ld->ld_errno = LDAP_LOCAL_ERROR;
+               }
+       }
+       if ( rc != LDAP_SUCCESS && berout ) {
+               ber_free( ber, 0 );
+               *berout = NULL;
+       }
+       return rc;
+}
+
 /*
  * RFC 1823 ldap_dn2ufn
  */
@@ -108,7 +162,11 @@ ldap_dn2ufn( LDAP_CONST char *dn )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
+#endif
 
        ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP, 
                &out, LDAP_DN_FORMAT_UFN );
@@ -122,12 +180,16 @@ ldap_dn2ufn( LDAP_CONST char *dn )
 char **
 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
 {
-       LDAPDN  *tmpDN;
+       LDAPDN  tmpDN;
        char    **values = NULL;
        int     iRDN;
        unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
        
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
+#endif
 
        if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP ) 
                        != LDAP_SUCCESS ) {
@@ -142,7 +204,7 @@ ldap_explode_dn( LDAP_CONST char *dn, int notypes )
                return values;
        }
 
-       for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
+       for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
 
        values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
        if ( values == NULL ) {
@@ -150,8 +212,8 @@ ldap_explode_dn( LDAP_CONST char *dn, int notypes )
                return NULL;
        }
 
-       for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
-               ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
+       for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
+               ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
        }
        ldap_dnfree( tmpDN );
        values[ iRDN ] = NULL;
@@ -162,12 +224,16 @@ ldap_explode_dn( LDAP_CONST char *dn, int notypes )
 char **
 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
 {
-       LDAPRDN         *tmpRDN;
+       LDAPRDN         tmpRDN;
        char            **values = NULL;
        const char      *p;
        int             iAVA;
        
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
+#endif
 
        /*
         * we only parse the first rdn
@@ -179,19 +245,19 @@ ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
                return( NULL );
        }
 
-       for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
+       for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
        values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
        if ( values == NULL ) {
                ldap_rdnfree( tmpRDN );
                return( NULL );
        }
 
-       for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
+       for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
                ber_len_t       l = 0, vl, al = 0;
                char            *str;
-               LDAPAVA         *ava = tmpRDN[ 0 ][ iAVA ];
+               LDAPAVA         *ava = tmpRDN[ iAVA ];
                
-               if ( ava->la_flags == LDAP_AVA_BINARY ) {
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        vl = 1 + 2 * ava->la_value.bv_len;
 
                } else {
@@ -215,7 +281,7 @@ ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
                        str = LDAP_MALLOC( l + 1 );
                }
                
-               if ( ava->la_flags == LDAP_AVA_BINARY ) {
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ al++ ] = '#';
                        if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
                                goto error_return;
@@ -248,7 +314,11 @@ ldap_dn2dcedn( LDAP_CONST char *dn )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
+#endif
 
        ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP, 
                                   &out, LDAP_DN_FORMAT_DCE );
@@ -261,7 +331,11 @@ ldap_dcedn2dn( LDAP_CONST char *dce )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
+#endif
 
        ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
 
@@ -273,7 +347,11 @@ ldap_dn2ad_canonical( LDAP_CONST char *dn )
 {
        char    *out = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
+#endif
 
        ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP, 
                       &out, LDAP_DN_FORMAT_AD_CANONICAL );
@@ -303,9 +381,13 @@ ldap_dn_normalize( LDAP_CONST char *dnin,
        unsigned fin, char **dnout, unsigned fout )
 {
        int     rc;
-       LDAPDN  *tmpDN = NULL;
+       LDAPDN  tmpDN = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
+#endif
 
        assert( dnout );
 
@@ -460,13 +542,13 @@ ldap_dn_normalize( LDAP_CONST char *dnin,
 #define        LDAP_DC_ATTR    "dc"
 #define        LDAP_DC_ATTRU   "DC"
 #define LDAP_DN_IS_RDN_DC( r ) \
-       ( (r) && (r)[0][0] && !(r)[0][1] \
-         && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
-         && ((r)[0][0]->la_attr.bv_len == 2) \
-         && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
-               || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
-         && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
-               || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
+       ( (r) && (r)[0] && !(r)[1] \
+         && ((r)[0]->la_flags & LDAP_AVA_STRING) \
+         && ((r)[0]->la_attr.bv_len == 2) \
+         && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
+               || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
+         && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
+               || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
 
 /* Composite rules */
 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
@@ -495,35 +577,32 @@ ldap_dn_normalize( LDAP_CONST char *dnin,
  */
 LDAPAVA *
 ldapava_new( const struct berval *attr, const struct berval *val, 
-               unsigned flags )
+               unsigned flags, void *ctx )
 {
-       LDAPAVA *ava;
+       LDAPAVA *ava;
 
        assert( attr );
        assert( val );
 
-       ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
-       
-       /* should we test it? */
-       if ( ava == NULL ) {
-               return( NULL );
-       }
+       ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
 
-       ava->la_attr.bv_len = attr->bv_len;
-       ava->la_attr.bv_val = (char *)(ava+1);
-       AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
-       ava->la_attr.bv_val[attr->bv_len] = '\0';
+       if ( ava ) {
+               ava->la_attr.bv_len = attr->bv_len;
+               ava->la_attr.bv_val = (char *)(ava+1);
+               AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
+               ava->la_attr.bv_val[attr->bv_len] = '\0';
 
-       ava->la_value = *val;
-       ava->la_flags = flags;
+               ava->la_value = *val;
+               ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
 
-       ava->la_private = NULL;
+               ava->la_private = NULL;
+       }
 
        return( ava );
 }
 
 void
-ldap_avafree( LDAPAVA *ava )
+ldapava_free( LDAPAVA *ava, void *ctx )
 {
        assert( ava );
 
@@ -534,17 +613,20 @@ ldap_avafree( LDAPAVA *ava )
        assert( ava->la_private == NULL );
 #endif
 
-#if 0
-       /* la_attr is now contiguous with ava, not freed separately */
-       LDAP_FREE( ava->la_attr.bv_val );
-#endif
-       LDAP_FREE( ava->la_value.bv_val );
+       if (ava->la_flags & LDAP_AVA_FREE_VALUE)
+               LDAP_FREEX( ava->la_value.bv_val, ctx );
 
-       LDAP_FREE( ava );
+       LDAP_FREEX( ava, ctx );
 }
 
 void
-ldap_rdnfree( LDAPRDN *rdn )
+ldap_rdnfree( LDAPRDN rdn )
+{
+       ldap_rdnfree_x( rdn, NULL );
+}
+
+void
+ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
 {
        int iAVA;
        
@@ -552,15 +634,21 @@ ldap_rdnfree( LDAPRDN *rdn )
                return;
        }
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               ldap_avafree( rdn[ 0 ][ iAVA ] );
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               ldapava_free( rdn[ iAVA ], ctx );
        }
 
-       LDAP_FREE( rdn );
+       LDAP_FREEX( rdn, ctx );
+}
+
+void
+ldap_dnfree( LDAPDN dn )
+{
+       ldap_dnfree_x( dn, NULL );
 }
 
 void
-ldap_dnfree( LDAPDN *dn )
+ldap_dnfree_x( LDAPDN dn, void *ctx )
 {
        int iRDN;
        
@@ -568,11 +656,11 @@ ldap_dnfree( LDAPDN *dn )
                return;
        }
 
-       for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
-               ldap_rdnfree( dn[ 0 ][ iRDN ] );
+       for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
+               ldap_rdnfree_x( dn[ iRDN ], ctx );
        }
 
-       LDAP_FREE( dn );
+       LDAP_FREEX( dn, ctx );
 }
 
 /*
@@ -599,26 +687,33 @@ ldap_dnfree( LDAPDN *dn )
 #define        TMP_RDN_SLOTS   32
 
 int
-ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
+ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
 {
-       struct berval   bv = { 0, (char *)str };
+       struct berval   bv;
 
        assert( str );
 
        bv.bv_len = strlen( str );
+       bv.bv_val = (char *) str;
        
-       return ldap_bv2dn( &bv, dn, flags );
+       return ldap_bv2dn_x( &bv, dn, flags, NULL );
 }
 
 int
-ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
+ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
+{
+       return ldap_bv2dn_x( bv, dn, flags, NULL );
+}
+
+int
+ldap_bv2dn_x( struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx )
 {
        const char      *p;
        int             rc = LDAP_DECODING_ERROR;
        int             nrdns = 0;
 
-       LDAPDN          *newDN = NULL;
-       LDAPRDN         *newRDN = NULL, *tmpDN_[TMP_RDN_SLOTS], **tmpDN = tmpDN_;
+       LDAPDN          newDN = NULL;
+       LDAPRDN         newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
        int             num_slots = TMP_RDN_SLOTS;
        char            *str = bv->bv_val;
        char            *end = str + bv->bv_len;
@@ -627,7 +722,11 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
        assert( bv->bv_val );
        assert( dn );
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
+#else
        Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
+#endif
 
        *dn = NULL;
 
@@ -687,9 +786,11 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
 
        for ( ; p < end; p++ ) {
                int             err;
-               struct berval   tmpbv = { bv->bv_len - ( p - str ), (char *)p };
+               struct berval   tmpbv;
+               tmpbv.bv_len = bv->bv_len - ( p - str );
+               tmpbv.bv_val = (char *)p;
                
-               err = ldap_bv2rdn( &tmpbv, &newRDN, (char **) &p, flags );
+               err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
                if ( err != LDAP_SUCCESS ) {
                        goto parsing_error;
                }
@@ -731,10 +832,10 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
                 * make the static RDN array dynamically rescalable
                 */
                if ( nrdns == num_slots ) {
-                       LDAPRDN **tmp;
+                       LDAPRDN *tmp;
 
                        if ( tmpDN == tmpDN_ ) {
-                               tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPRDN * ) );
+                               tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
                                if ( tmp == NULL ) {
                                        rc = LDAP_NO_MEMORY;
                                        goto parsing_error;
@@ -742,7 +843,7 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
                                AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
 
                        } else {
-                               tmp = LDAP_REALLOC( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ) );
+                               tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
                                if ( tmp == NULL ) {
                                        rc = LDAP_NO_MEMORY;
                                        goto parsing_error;
@@ -757,25 +858,22 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
                        /* 
                         * the DN is over, phew
                         */
-                       newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
-                               sizeof(LDAPRDN *) * (nrdns+1));
+                       newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
                        if ( newDN == NULL ) {
                                rc = LDAP_NO_MEMORY;
                                goto parsing_error;
                        } else {
                                int i;
 
-                               newDN[0] = (LDAPRDN **)(newDN+1);
-
                                if ( LDAP_DN_DCE( flags ) ) {
                                        /* add in reversed order */
                                        for ( i=0; i<nrdns; i++ )
-                                               newDN[0][i] = tmpDN[nrdns-1-i];
+                                               newDN[i] = tmpDN[nrdns-1-i];
                                } else {
                                        for ( i=0; i<nrdns; i++ )
-                                               newDN[0][i] = tmpDN[i];
+                                               newDN[i] = tmpDN[i];
                                }
-                               newDN[0][nrdns] = NULL;
+                               newDN[nrdns] = NULL;
                                rc = LDAP_SUCCESS;
                        }
                        goto return_result;
@@ -784,20 +882,25 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
        
 parsing_error:;
        if ( newRDN ) {
-               ldap_rdnfree( newRDN );
+               ldap_rdnfree_x( newRDN, ctx );
        }
 
        for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
-               ldap_rdnfree( tmpDN[nrdns] );
+               ldap_rdnfree_x( tmpDN[nrdns], ctx );
        }
 
 return_result:;
 
        if ( tmpDN != tmpDN_ ) {
-               LDAP_FREE( tmpDN );
+               LDAP_FREEX( tmpDN, ctx );
        }
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n", 
+               str, flags, rc );
+#else
        Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
+#endif
        *dn = newDN;
        
        return( rc );
@@ -812,22 +915,30 @@ return_result:;
  * corresponds to the rdn separator or to '\0' in case the string is over.
  */
 int
-ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
+ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
        char **n_in, unsigned flags )
 {
-       struct berval   bv = { 0, (char *)str };
+       struct berval   bv;
 
        assert( str );
        assert( str[ 0 ] != '\0' );     /* FIXME: is this required? */
 
        bv.bv_len = strlen( str );
+       bv.bv_val = (char *) str;
 
-       return ldap_bv2rdn( &bv, rdn, n_in, flags );
+       return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
 }
 
 int
-ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
+ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
        char **n_in, unsigned flags )
+{
+       return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
+}
+
+int
+ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
+       char **n_in, unsigned flags, void *ctx )
 {
        const char      **n = (const char **) n_in;
        const char      *p;
@@ -840,7 +951,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
        struct berval   attrType = { 0, NULL };
        struct berval   attrValue = { 0, NULL };
 
-       LDAPRDN         *newRDN = NULL;
+       LDAPRDN         newRDN = NULL;
        LDAPAVA         *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
        int             num_slots = TMP_AVA_SLOTS;
 
@@ -1143,7 +1254,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                        break;
 
                case B4BINARYVALUE:
-                       if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
+                       if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
                                goto parsing_error;
                        }
 
@@ -1156,13 +1267,13 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                        case LDAP_DN_FORMAT_LDAPV3:
                                if ( str2strval( p, stoplen - ( p - str ),
                                                        &attrValue, &p, flags, 
-                                                       &attrValueEncoding ) ) {
+                                                       &attrValueEncoding, ctx ) ) {
                                        goto parsing_error;
                                }
                                break;
 
                        case LDAP_DN_FORMAT_DCE:
-                               if ( DCE2strval( p, &attrValue, &p, flags ) ) {
+                               if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
                                        goto parsing_error;
                                }
                                break;
@@ -1175,7 +1286,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                        break;
 
                case B4IA5VALUE:
-                       if ( IA52strval( p, &attrValue, &p, flags ) ) {
+                       if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
                                goto parsing_error;
                        }
 
@@ -1186,7 +1297,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
 
                        /* lead quote already stripped */
                        if ( quotedIA52strval( p, &attrValue, 
-                                               &p, flags ) ) {
+                                               &p, flags, ctx ) ) {
                                goto parsing_error;
                        }
 
@@ -1203,8 +1314,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                                 * we accept empty values
                                 */
                                ava = ldapava_new( &attrType, &attrValue, 
-                                               attrValueEncoding );
-                               
+                                               attrValueEncoding, ctx );
                                if ( ava == NULL ) {
                                        rc = LDAP_NO_MEMORY;
                                        goto parsing_error;
@@ -1221,7 +1331,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                                        LDAPAVA **tmp;
                                        
                                        if ( tmpRDN == tmpRDN_ ) {
-                                               tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPAVA * ) );
+                                               tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
                                                if ( tmp == NULL ) {
                                                        rc = LDAP_NO_MEMORY;
                                                        goto parsing_error;
@@ -1229,7 +1339,7 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                                                AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
 
                                        } else {
-                                               tmp = LDAP_REALLOC( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ) );
+                                               tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
                                                if ( tmp == NULL ) {
                                                        rc = LDAP_NO_MEMORY;
                                                        goto parsing_error;
@@ -1268,19 +1378,14 @@ ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
                                 */
                                *n = p;
                                if ( !( flags & LDAP_DN_SKIP ) ) {
-                                       newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
-                                               + sizeof(LDAPAVA *) * (navas+1) );
+                                       newRDN = (LDAPRDN)LDAP_MALLOCX( 
+                                               sizeof(LDAPAVA) * (navas+1), ctx );
                                        if ( newRDN == NULL ) {
                                                rc = LDAP_NO_MEMORY;
                                                goto parsing_error;
                                        } else {
-                                               int i;
-
-                                               newRDN[0] = (LDAPAVA**)(newRDN+1);
-
-                                               for (i=0; i<navas; i++)
-                                                       newRDN[0][i] = tmpRDN[i];
-                                               newRDN[0][i] = NULL;
+                                               AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
+                                               newRDN[navas] = NULL;
                                        }
 
                                }
@@ -1308,17 +1413,17 @@ parsing_error:;
        /* They are set to NULL after they're used in an AVA */
 
        if ( attrValue.bv_val ) {
-               free( attrValue.bv_val );
+               LDAP_FREEX( attrValue.bv_val, ctx );
        }
 
        for ( navas-- ; navas >= 0; navas-- ) {
-               ldap_avafree( tmpRDN[navas] );
+               ldapava_free( tmpRDN[navas], ctx );
        }
 
 return_result:;
 
        if ( tmpRDN != tmpRDN_ ) {
-               LDAP_FREE( tmpRDN );
+               LDAP_FREEX( tmpRDN, ctx );
        }
 
        if ( rdn ) {
@@ -1334,7 +1439,7 @@ return_result:;
  * '\' + HEXPAIR(p) -> unhex(p)
  */
 static int
-str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
+str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
 {
        const char      *p, *end, *startPos, *endPos = NULL;
        ber_len_t       len, escapes;
@@ -1386,6 +1491,9 @@ str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char *
                        return( 1 );
 
                } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
+                       if ( p[ 0 ] == '\0' ) {
+                               return( 1 );
+                       }
                        *retFlags = LDAP_AVA_NONPRINTABLE;
 
                } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) 
@@ -1433,18 +1541,18 @@ str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char *
        val->bv_len = len;
 
        if ( escapes == 0 ) {
-               if ( *retFlags == LDAP_AVA_NONPRINTABLE ) {
-                       val->bv_val = LDAP_MALLOC( len + 1 );
+               if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
+                       val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                        AC_MEMCPY( val->bv_val, startPos, len );
                        val->bv_val[ len ] = '\0';
                } else {
-                       val->bv_val = LDAP_STRNDUP( startPos, len );
+                       val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
                }
 
        } else {
                ber_len_t       s, d;
 
-               val->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                for ( s = 0, d = 0; d < len; ) {
                        if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
                                s++;
@@ -1477,7 +1585,7 @@ str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char *
 }
 
 static int
-DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
+DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len, escapes;
@@ -1536,12 +1644,12 @@ DCE2strval( const char *str, struct berval *val, const char **next, unsigned fla
        len = ( endPos ? endPos : p ) - startPos - escapes;
        val->bv_len = len;
        if ( escapes == 0 ){
-               val->bv_val = LDAP_STRNDUP( startPos, len );
+               val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
 
        } else {
                ber_len_t       s, d;
 
-               val->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                for ( s = 0, d = 0; d < len; ) {
                        /*
                         * This point is reached only if escapes 
@@ -1562,7 +1670,7 @@ DCE2strval( const char *str, struct berval *val, const char **next, unsigned fla
 }
 
 static int
-IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
+IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len, escapes;
@@ -1617,12 +1725,12 @@ IA52strval( const char *str, struct berval *val, const char **next, unsigned fla
        len = ( endPos ? endPos : p ) - startPos - escapes;
        val->bv_len = len;
        if ( escapes == 0 ) {
-               val->bv_val = LDAP_STRNDUP( startPos, len );
+               val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
 
        } else {
                ber_len_t       s, d;
                
-               val->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                for ( s = 0, d = 0; d < len; ) {
                        if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
                                s++;
@@ -1637,7 +1745,7 @@ IA52strval( const char *str, struct berval *val, const char **next, unsigned fla
 }
 
 static int
-quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
+quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len;
@@ -1704,15 +1812,15 @@ quotedIA52strval( const char *str, struct berval *val, const char **next, unsign
        }
 
        len = endPos - startPos - escapes;
-       assert( len >= 0 );
+       assert( endPos >= startPos + escapes );
        val->bv_len = len;
        if ( escapes == 0 ) {
-               val->bv_val = LDAP_STRNDUP( startPos, len );
+               val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
 
        } else {
                ber_len_t       s, d;
                
-               val->bv_val = LDAP_MALLOC( len + 1 );
+               val->bv_val = LDAP_MALLOCX( len + 1, ctx );
                val->bv_len = len;
 
                for ( s = d = 0; d < len; ) {
@@ -1769,7 +1877,7 @@ hexstr2bin( const char *str, char *c )
 }
 
 static int
-hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
+hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
 {
        const char      *p, *startPos, *endPos = NULL;
        ber_len_t       len;
@@ -1851,7 +1959,7 @@ end_of_value:;
        assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
 
        val->bv_len = len;
-       val->bv_val = LDAP_MALLOC( len + 1 );
+       val->bv_val = LDAP_MALLOCX( len + 1, ctx );
        if ( val->bv_val == NULL ) {
                return( LDAP_NO_MEMORY );
        }
@@ -2363,7 +2471,7 @@ strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
  * by Luke Howard, http://www.padl.com/~lukeh)
  */
 static int
-dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
+dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
 {
        int             i;
        int             domain = 0, first = 1;
@@ -2381,14 +2489,14 @@ dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
        str = bv->bv_val + pos;
 
        for ( i = *iRDN; i >= 0; i-- ) {
-               LDAPRDN         *rdn;
+               LDAPRDN         rdn;
                LDAPAVA         *ava;
 
-               assert( dn[ 0 ][ i ] );
-               rdn = dn[ 0 ][ i ];
+               assert( dn[ i ] );
+               rdn = dn[ i ];
 
-               assert( rdn[ 0 ][ 0 ] );
-               ava = rdn[ 0 ][ 0 ];
+               assert( rdn[ 0 ] );
+               ava = rdn[ 0 ];
 
                if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
                        break;
@@ -2418,7 +2526,7 @@ dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
 }
 
 static int
-rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
+rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
         int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
 {
        int             iAVA;
@@ -2426,8 +2534,8 @@ rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
 
        *len = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* len(type) + '=' + '+' | ',' */
                l += ava->la_attr.bv_len + 2;
@@ -2453,14 +2561,14 @@ rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
 }
 
 static int
-rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
+rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
        int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                AC_MEMCPY( &str[ l ], ava->la_attr.bv_val, 
                                ava->la_attr.bv_len );
@@ -2484,7 +2592,7 @@ rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
                        }
                        l += vl;
                }
-               str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
+               str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
        }
 
        *len = l;
@@ -2493,26 +2601,23 @@ rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
 }
 
 static int
-rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
+rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
        *len = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* len(type) + '=' + ',' | '/' */
                l += ava->la_attr.bv_len + 2;
 
-               switch ( ava->la_flags ) {
-               case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        /* octothorpe + twice the length */
                        l += 1 + 2 * ava->la_value.bv_len;
-                       break;
-
-               case LDAP_AVA_STRING: {
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
                        
@@ -2520,11 +2625,6 @@ rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-
-               default:
-                       return( -1 );
                }
        }
        
@@ -2534,13 +2634,13 @@ rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
+rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                if ( first ) {
                        first = 0;
@@ -2554,16 +2654,13 @@ rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
 
                str[ l++ ] = '=';
 
-               switch ( ava->la_flags ) {
-                       case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
                        if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
                                return( -1 );
                        }
                        l += 2 * ava->la_value.bv_len;
-                       break;
-
-               case LDAP_AVA_STRING: {
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
 
@@ -2571,11 +2668,6 @@ rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-                                     
-               default:
-                       return( -1 );
                }
        }
 
@@ -2585,7 +2677,7 @@ rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
 }
 
 static int
-rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
+rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
@@ -2595,11 +2687,11 @@ rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 
        *len = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* ' + ' | ', ' */
-               l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
+               l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
 
                /* FIXME: are binary values allowed in UFN? */
                if ( ava->la_flags & LDAP_AVA_BINARY ) {
@@ -2623,13 +2715,13 @@ rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
+rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
@@ -2648,7 +2740,7 @@ rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
                        l += vl;
                }
 
-               if ( rdn[ 0 ][ iAVA + 1 ]) {
+               if ( rdn[ iAVA + 1 ] ) {
                        AC_MEMCPY( &str[ l ], " + ", 3 );
                        l += 3;
 
@@ -2664,7 +2756,7 @@ rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
+rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
 {
        int             iAVA;
        ber_len_t       l = 0;
@@ -2674,20 +2766,17 @@ rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 
        *len = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                /* ',' | '/' */
                l++;
 
                /* FIXME: are binary values allowed in UFN? */
-               switch ( ava->la_flags ) {
-               case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        /* octothorpe + twice the value */
                        l += 1 + 2 * ava->la_value.bv_len;
-                       break;
-
-               case LDAP_AVA_STRING: {
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
 
@@ -2695,11 +2784,6 @@ rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-
-               default:
-                       return( -1 );
                }
        }
        
@@ -2709,13 +2793,13 @@ rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
 }
 
 static int
-rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
+rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
 {
        int             iAVA;
        ber_len_t       l = 0;
 
-       for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
-               LDAPAVA         *ava = rdn[ 0 ][ iAVA ];
+       for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
+               LDAPAVA         *ava = rdn[ iAVA ];
 
                if ( first ) {
                        first = 0;
@@ -2723,16 +2807,13 @@ rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
                        str[ l++ ] = ( iAVA ? ',' : '/' );
                }
 
-               switch ( ava->la_flags ) {
-               case LDAP_AVA_BINARY:
+               if ( ava->la_flags & LDAP_AVA_BINARY ) {
                        str[ l++ ] = '#';
                        if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
                                return( -1 );
                        }
                        l += 2 * ava->la_value.bv_len;
-                       break;
-                       
-               case LDAP_AVA_STRING: {
+               } else {
                        ber_len_t       vl;
                        unsigned        f = flags | ava->la_flags;
                        
@@ -2740,11 +2821,6 @@ rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
                                return( -1 );
                        }
                        l += vl;
-                       break;
-               }
-
-               default:
-                       return( -1 );
                }
        }
 
@@ -2761,7 +2837,7 @@ rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
  * this is wanted to reduce the allocation of temporary buffers.
  */
 int
-ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
+ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
 {
        struct berval bv;
        int rc;
@@ -2772,13 +2848,19 @@ ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
                return LDAP_PARAM_ERROR;
        }
 
-       rc = ldap_rdn2bv( rdn, &bv, flags );
+       rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
        *str = bv.bv_val;
        return rc;
 }
 
 int
-ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
+ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
+{
+       return ldap_rdn2bv_x( rdn, bv, flags, NULL );
+}
+
+int
+ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
 {
        int             rc, back;
        ber_len_t       l;
@@ -2789,7 +2871,7 @@ ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
        bv->bv_val = NULL;
 
        if ( rdn == NULL ) {
-               bv->bv_val = LDAP_STRDUP( "" );
+               bv->bv_val = LDAP_STRDUPX( "", ctx );
                return( LDAP_SUCCESS );
        }
 
@@ -2829,10 +2911,10 @@ ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
                break;
 
        default:
-               return( LDAP_PARAM_ERROR );
+               return LDAP_PARAM_ERROR;
        }
 
-       bv->bv_val = LDAP_MALLOC( l + 1 );
+       bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
 
        switch ( LDAP_DN_FORMAT( flags ) ) {
        case LDAP_DN_FORMAT_LDAPV3:
@@ -2866,7 +2948,7 @@ ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
        }
 
        if ( rc ) {
-               ldap_memfree( bv->bv_val );
+               LDAP_FREEX( bv->bv_val, ctx );
                return rc;
        }
 
@@ -2888,7 +2970,7 @@ ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
  *   c) what do we do when binary values must be converted in UTF/DCE/AD?
  *      use binary encoded BER
  */ 
-int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
+int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
 {
        struct berval bv;
        int rc;
@@ -2899,12 +2981,17 @@ int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
                return LDAP_PARAM_ERROR;
        }
        
-       rc = ldap_dn2bv( dn, &bv, flags );
+       rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
        *str = bv.bv_val;
        return rc;
 }
 
-int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
+int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
+{
+       return ldap_dn2bv_x( dn, bv, flags, NULL );
+}
+
+int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
 {
        int             iRDN;
        int             rc = LDAP_ENCODING_ERROR;
@@ -2918,14 +3005,19 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
        bv->bv_len = 0;
        bv->bv_val = NULL;
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s", 
+               flags, "", "" );
+#else
        Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
+#endif
 
        /* 
         * a null dn means an empty dn string 
         * FIXME: better raise an error?
         */
        if ( dn == NULL ) {
-               bv->bv_val = LDAP_STRDUP( "" );
+               bv->bv_val = LDAP_STRDUPX( "", ctx );
                return( LDAP_SUCCESS );
        }
 
@@ -2940,29 +3032,26 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
                        sv2s = strval2IA5str;
                }
 
-               for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
+               for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
-                       
-                       if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
+                       if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
                                goto return_results;
                        }
 
                        len += rdnl;
                }
 
-               if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
 
-               for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
+               for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                       if ( rdn2str( rdn, &bv->bv_val[ l ], flags, 
+                       if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags, 
                                        &rdnl, sv2s ) ) {
-                               LDAP_FREE( bv->bv_val );
+                               LDAP_FREEX( bv->bv_val, ctx );
                                bv->bv_val = NULL;
                                goto return_results;
                        }
@@ -3021,17 +3110,16 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
                int     last_iRDN = -1;
 #endif /* DC_IN_UFN */
 
-               for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
+               for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                       if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
+                       if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
                                goto return_results;
                        }
                        len += rdnl;
 
 #ifdef DC_IN_UFN
-                       if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
+                       if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
                                if ( leftmost_dc == -1 ) {
                                        leftmost_dc = iRDN;
                                }
@@ -3041,7 +3129,7 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
 #endif /* DC_IN_UFN */
                }
 
-               if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
@@ -3049,13 +3137,12 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
 #ifdef DC_IN_UFN
                if ( leftmost_dc == -1 ) {
 #endif /* DC_IN_UFN */
-                       for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
+                       for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
                                ber_len_t       vl;
-                               LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                               if ( rdn2UFNstr( rdn, &bv->bv_val[ l ], 
+                               if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &vl ) ) {
-                                       LDAP_FREE( bv->bv_val );
+                                       LDAP_FREEX( bv->bv_val, ctx );
                                        bv->bv_val = NULL;
                                        goto return_results;
                                }
@@ -3074,11 +3161,10 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
 
                        for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
                                ber_len_t       vl;
-                               LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                               if ( rdn2UFNstr( rdn, &bv->bv_val[ l ], 
+                               if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &vl ) ) {
-                                       LDAP_FREE( bv->bv_val );
+                                       LDAP_FREEX( bv->bv_val, ctx );
                                        bv->bv_val = NULL;
                                        goto return_results;
                                }
@@ -3086,7 +3172,7 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
                        }
 
                        if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
-                               LDAP_FREE( bv->bv_val );
+                               LDAP_FREEX( bv->bv_val, ctx );
                                bv->bv_val = NULL;
                                goto return_results;
                        }
@@ -3100,29 +3186,26 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
        } break;
 
        case LDAP_DN_FORMAT_DCE:
-               for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
+               for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
-                       
-                       if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
+                       if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
                                goto return_results;
                        }
 
                        len += rdnl;
                }
 
-               if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
 
                for ( l = 0; iRDN--; ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                       if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags, 
+                       if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags, 
                                        &rdnl, 0 ) ) {
-                               LDAP_FREE( bv->bv_val );
+                               LDAP_FREEX( bv->bv_val, ctx );
                                bv->bv_val = NULL;
                                goto return_results;
                        }
@@ -3152,31 +3235,29 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
                 * 
                 *      "microsoft.com/People/Bill,Gates"
                 */ 
-               for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
+               for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
                        ber_len_t       rdnl;
-                       LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                       if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
+                       if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
                                goto return_results;
                        }
 
                        len += rdnl;
                }
 
-               if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
+               if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
                        rc = LDAP_NO_MEMORY;
                        break;
                }
 
                iRDN--;
-               if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
+               if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
                        for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
                                ber_len_t       rdnl;
-                               LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                               if ( rdn2ADstr( rdn, &bv->bv_val[ l ], 
+                               if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &rdnl, 0 ) ) {
-                                       LDAP_FREE( bv->bv_val );
+                                       LDAP_FREEX( bv->bv_val, ctx );
                                        bv->bv_val = NULL;
                                        goto return_results;
                                }
@@ -3192,7 +3273,7 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
                         * i.e. terminated by a domain component
                         */
                        if ( flags & LDAP_DN_PEDANTIC ) {
-                               LDAP_FREE( bv->bv_val );
+                               LDAP_FREEX( bv->bv_val, ctx );
                                bv->bv_val = NULL;
                                rc = LDAP_ENCODING_ERROR;
                                break;
@@ -3200,11 +3281,10 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
 
                        for ( l = 0; iRDN >= 0 ; iRDN-- ) {
                                ber_len_t       rdnl;
-                               LDAPRDN         *rdn = dn[ 0 ][ iRDN ];
                        
-                               if ( rdn2ADstr( rdn, &bv->bv_val[ l ], 
+                               if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ], 
                                                flags, &rdnl, first ) ) {
-                                       LDAP_FREE( bv->bv_val );
+                                       LDAP_FREEX( bv->bv_val, ctx );
                                        bv->bv_val = NULL;
                                        goto return_results;
                                }
@@ -3225,10 +3305,214 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
                return LDAP_PARAM_ERROR;
        }
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n", 
+               bv->bv_val, flags, rc );
+#else
        Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
                bv->bv_val, flags, rc );
+#endif
 
 return_results:;
        return( rc );
 }
 
+#ifdef HAVE_TLS
+#include <openssl/x509.h>
+#include <openssl/err.h>
+
+/* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
+ * x509_name must be an (X509_NAME *). If func is non-NULL, the
+ * constructed DN will use numeric OIDs to identify attributeTypes,
+ * and the func() will be invoked to rewrite the DN with the given
+ * flags.
+ *
+ * Otherwise the DN will use shortNames as defined in the OpenSSL
+ * library.
+ *
+ * It's preferable to let slapd do the OID to attributeType mapping,
+ * because the OpenSSL tables are known to have many typos in versions
+ * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
+ * so we're forced to use OpenSSL's mapping there.
+ *  -- Howard Chu 2002-04-18
+ */
+
+int
+ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
+       unsigned flags )
+{
+       LDAPDN  newDN;
+       LDAPRDN newRDN;
+       LDAPAVA *newAVA, *baseAVA;
+       X509_NAME_ENTRY *ne;
+       ASN1_OBJECT *obj;
+       ASN1_STRING *str;
+       char oids[8192], *oidptr = oids, *oidbuf = NULL;
+       void *ptrs[2048];
+       int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
+       int set = -1;
+       size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
+       int csize;
+
+       struct berval   Val;
+
+       assert( bv );
+       bv->bv_len = 0;
+       bv->bv_val = NULL;
+
+       /* Get the number of AVAs. This is not necessarily the same as
+        * the number of RDNs.
+        */
+       navas = X509_NAME_entry_count( x509_name );
+
+       /* Get the last element, to see how many RDNs there are */
+       ne = X509_NAME_get_entry( x509_name, navas - 1 );
+       nrdns = ne->set + 1;
+
+       /* Allocate the DN/RDN/AVA stuff as a single block */    
+       dnsize = sizeof(LDAPRDN) * (nrdns+1);
+       dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
+       dnsize += sizeof(LDAPAVA) * navas;
+       if (dnsize > sizeof(ptrs)) {
+               newDN = (LDAPDN)LDAP_MALLOC( dnsize );
+               if ( newDN == NULL )
+                       return LDAP_NO_MEMORY;
+       } else {
+               newDN = (LDAPDN)ptrs;
+       }
+       
+       newDN[nrdns] = NULL;
+       newRDN = (LDAPRDN)(newDN + nrdns+1);
+       newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
+       baseAVA = newAVA;
+
+       /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
+       for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
+               ne = X509_NAME_get_entry( x509_name, i );
+               obj = X509_NAME_ENTRY_get_object( ne );
+               str = X509_NAME_ENTRY_get_data( ne );
+
+               /* If set changed, move to next RDN */
+               if ( set != ne->set ) {
+                       /* If this is not the first time, end the
+                        * previous RDN and advance.
+                        */
+                       if ( j > 0 ) {
+                               newRDN[k] = NULL;
+                               newRDN += k+1;
+                       }
+                       newDN[j++] = newRDN;
+
+                       k = 0;
+                       set = ne->set;
+               }
+               newAVA->la_private = NULL;
+               newAVA->la_flags = LDAP_AVA_STRING;
+
+               if ( !func ) {
+                       int n = OBJ_obj2nid( obj );
+
+                       if (n == NID_undef)
+                               goto get_oid;
+                       newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
+                       newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
+#ifdef HAVE_EBCDIC
+                       newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
+                       __etoa( newAVA->la_attr.bv_val );
+                       newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
+#endif
+               } else {
+get_oid:               newAVA->la_attr.bv_val = oidptr;
+                       newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
+#ifdef HAVE_EBCDIC
+                       __etoa( newAVA->la_attr.bv_val );
+#endif
+                       oidptr += newAVA->la_attr.bv_len + 1;
+                       oidrem -= newAVA->la_attr.bv_len + 1;
+
+                       /* Running out of OID buffer space? */
+                       if (oidrem < 128) {
+                               if ( oidsize == 0 ) {
+                                       oidsize = sizeof(oids) * 2;
+                                       oidrem = oidsize;
+                                       oidbuf = LDAP_MALLOC( oidsize );
+                                       if ( oidbuf == NULL ) goto nomem;
+                                       oidptr = oidbuf;
+                               } else {
+                                       char *old = oidbuf;
+                                       oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
+                                       if ( oidbuf == NULL ) goto nomem;
+                                       /* Buffer moved! Fix AVA pointers */
+                                       if ( old != oidbuf ) {
+                                               LDAPAVA *a;
+                                               long dif = oidbuf - old;
+
+                                               for (a=baseAVA; a<=newAVA; a++){
+                                                       if (a->la_attr.bv_val >= old &&
+                                                               a->la_attr.bv_val <= (old + oidsize))
+                                                               a->la_attr.bv_val += dif;
+                                               }
+                                       }
+                                       oidptr = oidbuf + oidsize - oidrem;
+                                       oidrem += oidsize;
+                                       oidsize *= 2;
+                               }
+                       }
+               }
+               Val.bv_val = (char *) str->data;
+               Val.bv_len = str->length;
+               switch( str->type ) {
+               case V_ASN1_UNIVERSALSTRING:
+                       /* This uses 32-bit ISO 10646-1 */
+                       csize = 4; goto to_utf8;
+               case V_ASN1_BMPSTRING:
+                       /* This uses 16-bit ISO 10646-1 */
+                       csize = 2; goto to_utf8;
+               case V_ASN1_T61STRING:
+                       /* This uses 8-bit, assume ISO 8859-1 */
+                       csize = 1;
+to_utf8:               rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
+                       newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
+                       if (rc != LDAP_SUCCESS) goto nomem;
+                       newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
+                       break;
+               case V_ASN1_UTF8STRING:
+                       newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
+                       /* This is already in UTF-8 encoding */
+               case V_ASN1_IA5STRING:
+               case V_ASN1_PRINTABLESTRING:
+                       /* These are always 7-bit strings */
+                       newAVA->la_value = Val;
+               default:
+                       ;
+               }
+               newRDN[k] = newAVA;
+               newAVA++;
+               k++;
+       }
+       newRDN[k] = NULL;
+
+       if ( func ) {
+               rc = func( newDN, flags, NULL );
+               if ( rc != LDAP_SUCCESS )
+                       goto nomem;
+       }
+
+       rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
+
+nomem:
+       for (;baseAVA < newAVA; baseAVA++) {
+               if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
+                       LDAP_FREE( baseAVA->la_attr.bv_val );
+               if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
+                       LDAP_FREE( baseAVA->la_value.bv_val );
+       }
+
+       if ( oidsize != 0 )
+               LDAP_FREE( oidbuf );
+       if ( newDN != (LDAPDN) ptrs )
+               LDAP_FREE( newDN );
+       return rc;
+}
+#endif /* HAVE_TLS */
+