From: Howard Chu Date: Fri, 11 Apr 2003 01:02:08 +0000 (+0000) Subject: More memory context tweaks X-Git-Tag: OPENLDAP_REL_ENG_2_2_0ALPHA~405 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=1874658ae379b81683dfa70cdc8a531cf972b40f;p=openldap More memory context tweaks --- diff --git a/include/ldap.h b/include/ldap.h index d06f43e85f..f20383dfbb 100644 --- a/include/ldap.h +++ b/include/ldap.h @@ -1229,15 +1229,18 @@ typedef struct ldap_ava { struct berval la_attr; struct berval la_value; unsigned la_flags; -#define LDAP_AVA_STRING 0x0000U -#define LDAP_AVA_BINARY 0x0001U -#define LDAP_AVA_NONPRINTABLE 0x0002U +#define LDAP_AVA_NULL 0x0000U +#define LDAP_AVA_STRING 0x0001U +#define LDAP_AVA_BINARY 0x0002U +#define LDAP_AVA_NONPRINTABLE 0x0004U +#define LDAP_AVA_FREE_ATTR 0x0010U +#define LDAP_AVA_FREE_VALUE 0x0020U void *la_private; } LDAPAVA; typedef LDAPAVA** LDAPRDN; -typedef LDAPRDN** LDAPDN; +typedef LDAPRDN* LDAPDN; /* DN formats */ #define LDAP_DN_FORMAT_LDAP 0x0000U @@ -1256,57 +1259,75 @@ typedef LDAPRDN** LDAPDN; #define LDAP_DN_P_NOSPACEAFTERRDN 0x2000U #define LDAP_DN_PEDANTIC 0xF000U -LDAP_F( void ) ldap_avafree LDAP_P(( LDAPAVA *ava )); -LDAP_F( void ) ldap_rdnfree LDAP_P(( LDAPRDN *rdn )); -LDAP_F( void ) ldap_dnfree LDAP_P(( LDAPDN *dn )); +LDAP_F( void ) ldap_rdnfree LDAP_P(( LDAPRDN rdn )); +LDAP_F( void ) ldap_rdnfree_x LDAP_P(( LDAPRDN rdn, void *ctx )); +LDAP_F( void ) ldap_dnfree LDAP_P(( LDAPDN dn )); +LDAP_F( void ) ldap_dnfree_x LDAP_P(( LDAPDN dn, void *ctx )); LDAP_F( int ) ldap_bv2dn LDAP_P(( struct berval *bv, - LDAPDN **dn, + LDAPDN *dn, unsigned flags )); +LDAP_F( int ) +ldap_bv2dn_x LDAP_P(( + struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx )); + LDAP_F( int ) ldap_str2dn LDAP_P(( LDAP_CONST char *str, - LDAPDN **dn, + LDAPDN *dn, unsigned flags )); LDAP_F( int ) ldap_dn2bv LDAP_P(( - LDAPDN *dn, + LDAPDN dn, struct berval *bv, unsigned flags )); +LDAP_F( int ) +ldap_dn2bv_x LDAP_P(( + LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )); + LDAP_F( int ) ldap_dn2str LDAP_P(( - LDAPDN *dn, + LDAPDN dn, char **str, unsigned flags )); LDAP_F( int ) ldap_bv2rdn LDAP_P(( struct berval *bv, - LDAPRDN **rdn, + LDAPRDN *rdn, char **next, unsigned flags )); +LDAP_F( int ) +ldap_bv2rdn_x LDAP_P(( + struct berval *bv, LDAPRDN *rdn, char **next, + unsigned flags, void *ctx )); + LDAP_F( int ) ldap_str2rdn LDAP_P(( LDAP_CONST char *str, - LDAPRDN **rdn, + LDAPRDN *rdn, char **next, unsigned flags )); LDAP_F( int ) ldap_rdn2bv LDAP_P(( - LDAPRDN *rdn, + LDAPRDN rdn, struct berval *bv, unsigned flags )); +LDAP_F( int ) +ldap_rdn2bv_x LDAP_P(( + LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )); + LDAP_F( int ) ldap_rdn2str LDAP_P(( - LDAPRDN *rdn, + LDAPRDN rdn, char **str, unsigned flags )); @@ -1329,7 +1350,7 @@ ldap_explode_rdn LDAP_P(( /* deprecated */ LDAP_CONST char *rdn, int notypes )); -typedef int LDAPDN_rewrite_func LDAP_P(( LDAPDN *dn, unsigned flags )); +typedef int LDAPDN_rewrite_func LDAP_P(( LDAPDN dn, unsigned flags, void *ctx )); LDAP_F( int ) ldap_X509dn2bv LDAP_P(( void *x509_name, struct berval *dn, diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c index c76b1c07e2..d9bbcbe81e 100644 --- a/libraries/libldap/dnssrv.c +++ b/libraries/libldap/dnssrv.c @@ -39,8 +39,8 @@ int ldap_dn2domain( { int i, j; char *ndomain; - LDAPDN *dn = NULL; - LDAPRDN *rdn = NULL; + LDAPDN dn = NULL; + LDAPRDN rdn = NULL; LDAPAVA *ava = NULL; struct berval domain = { 0, NULL }; static const struct berval DC = BER_BVC("DC"); @@ -49,18 +49,21 @@ int ldap_dn2domain( assert( dn_in != NULL ); assert( domainp != NULL ); + *domainp = NULL; + if ( ldap_str2dn( dn_in, &dn, LDAP_DN_FORMAT_LDAP ) != LDAP_SUCCESS ) { return -2; } - if( dn ) for( i=0; (*dn)[i] != NULL; i++ ) { - rdn = (*dn)[i]; + if( dn ) for( i=0; dn[i] != NULL; i++ ) { + rdn = dn[i]; - for( j=0; (*rdn)[j] != NULL; j++ ) { - ava = (*rdn)[j]; + for( j=0; rdn[j] != NULL; j++ ) { + ava = rdn[j]; - if( (*dn)[i][j][1] == NULL && - !ava->la_flags && ava->la_value.bv_len && + if( rdn[j+1] == NULL && + (ava->la_flags & LDAP_AVA_STRING) && + ava->la_value.bv_len && ( ber_bvstrcasecmp( &ava->la_attr, &DC ) == 0 || ber_bvstrcasecmp( &ava->la_attr, &DCOID ) == 0 ) ) { diff --git a/libraries/libldap/dntest.c b/libraries/libldap/dntest.c index 31342a5bae..65acefa1e0 100644 --- a/libraries/libldap/dntest.c +++ b/libraries/libldap/dntest.c @@ -33,7 +33,7 @@ main( int argc, char *argv[] ) int rc, i, debug = 0, f2 = 0; unsigned flags[ 2 ] = { 0U, 0 }; char *strin, *str = NULL, buf[ 1024 ]; - LDAPDN *dn, *dn2 = NULL; + LDAPDN dn, dn2 = NULL; while ( 1 ) { int opt = getopt( argc, argv, "d:" ); @@ -124,8 +124,8 @@ main( int argc, char *argv[] ) if ( rc == LDAP_SUCCESS ) { int i; if ( dn ) { - for ( i = 0; dn[ 0 ][ i ]; i++ ) { - LDAPRDN *rdn = dn[ 0 ][ i ]; + for ( i = 0; dn[ i ]; i++ ) { + LDAPRDN rdn = dn[ i ]; char *rstr = NULL; if ( ldap_rdn2str( rdn, &rstr, flags[ f2 ] ) ) { @@ -241,15 +241,15 @@ main( int argc, char *argv[] ) if( dn != NULL && dn2 == NULL ) { fprintf( stdout, "dn mismatch\n" ); } else if (( dn != NULL ) && (dn2 != NULL)) - for ( iRDN = 0; dn[ 0 ][ iRDN ] && dn2[ 0 ][ iRDN ]; iRDN++ ) + for ( iRDN = 0; dn[ iRDN ] && dn2[ iRDN ]; iRDN++ ) { - LDAPRDN *r = dn[ 0 ][ iRDN ]; - LDAPRDN *r2 = dn2[ 0 ][ iRDN ]; + LDAPRDN r = dn[ iRDN ]; + LDAPRDN r2 = dn2[ iRDN ]; int iAVA; - for ( iAVA = 0; r[ 0 ][ iAVA ] && r2[ 0 ][ iAVA ]; iAVA++ ) { - LDAPAVA *a = r[ 0 ][ iAVA ]; - LDAPAVA *a2 = r2[ 0 ][ iAVA ]; + for ( iAVA = 0; r[ iAVA ] && r2[ iAVA ]; iAVA++ ) { + LDAPAVA *a = r[ iAVA ]; + LDAPAVA *a2 = r2[ iAVA ]; if ( a->la_attr.bv_len != a2->la_attr.bv_len ) { fprintf( stdout, "ava(%d), rdn(%d) attr len mismatch (%ld->%ld)\n", diff --git a/libraries/libldap/getdn.c b/libraries/libldap/getdn.c index ceb199427d..6b90e79993 100644 --- a/libraries/libldap/getdn.c +++ b/libraries/libldap/getdn.c @@ -29,15 +29,15 @@ /* 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, unsigned *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 @@ -180,7 +180,7 @@ 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; @@ -204,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 ) { @@ -212,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; @@ -224,7 +224,7 @@ 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; @@ -245,17 +245,17 @@ 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 ) { vl = 1 + 2 * ava->la_value.bv_len; @@ -381,7 +381,7 @@ 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 ); @@ -542,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) \ @@ -577,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 ); @@ -616,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_FREEX( ava, ctx ); +} - LDAP_FREE( ava ); +void +ldap_rdnfree( LDAPRDN rdn ) +{ + ldap_rdnfree_x( rdn, NULL ); } void -ldap_rdnfree( LDAPRDN *rdn ) +ldap_rdnfree_x( LDAPRDN rdn, void *ctx ) { int iAVA; @@ -634,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( LDAPDN dn ) +{ + ldap_dnfree_x( dn, NULL ); +} + +void +ldap_dnfree_x( LDAPDN dn, void *ctx ) { int iRDN; @@ -650,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 ); } /* @@ -681,7 +687,7 @@ 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; @@ -690,18 +696,24 @@ ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags ) 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; @@ -778,7 +790,7 @@ ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags ) 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; } @@ -820,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; @@ -831,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; @@ -846,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= 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 @@ -906,7 +915,7 @@ 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; @@ -917,12 +926,19 @@ ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn, 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; @@ -935,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; @@ -1238,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; } @@ -1251,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; @@ -1270,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; } @@ -1281,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; } @@ -1298,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; @@ -1316,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; @@ -1324,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; @@ -1363,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= 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 ) { @@ -1429,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, unsigned *retFlags, void *ctx ) { const char *p, *end, *startPos, *endPos = NULL; ber_len_t len, escapes; @@ -1532,17 +1542,17 @@ str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char * if ( escapes == 0 ) { if ( *retFlags == LDAP_AVA_NONPRINTABLE ) { - val->bv_val = LDAP_MALLOC( len + 1 ); + 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++; @@ -1575,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; @@ -1634,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 @@ -1660,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; @@ -1715,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++; @@ -1735,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; @@ -1805,12 +1815,12 @@ quotedIA52strval( const char *str, struct berval *val, const char **next, unsign 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; ) { @@ -1867,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; @@ -1949,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 ); } @@ -2461,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; @@ -2479,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; @@ -2516,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; @@ -2524,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; @@ -2551,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 ); @@ -2582,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; @@ -2591,15 +2601,15 @@ 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; @@ -2632,13 +2642,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; @@ -2683,7 +2693,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; @@ -2693,11 +2703,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 ) { @@ -2721,13 +2731,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++ ] = '#'; @@ -2746,7 +2756,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; @@ -2762,7 +2772,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; @@ -2772,8 +2782,8 @@ 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++; @@ -2807,13 +2817,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; @@ -2859,7 +2869,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; @@ -2870,13 +2880,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; @@ -2887,7 +2903,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 ); } @@ -2930,7 +2946,7 @@ ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags ) 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: @@ -2964,7 +2980,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; } @@ -2986,7 +3002,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; @@ -2997,12 +3013,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; @@ -3028,7 +3049,7 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags ) * FIXME: better raise an error? */ if ( dn == NULL ) { - bv->bv_val = LDAP_STRDUP( "" ); + bv->bv_val = LDAP_STRDUPX( "", ctx ); return( LDAP_SUCCESS ); } @@ -3043,29 +3064,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; } @@ -3124,17 +3142,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; } @@ -3144,7 +3161,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; } @@ -3152,13 +3169,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; } @@ -3177,11 +3193,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; } @@ -3189,7 +3204,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; } @@ -3203,29 +3218,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; } @@ -3255,18 +3267,17 @@ 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; } @@ -3275,11 +3286,10 @@ int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags ) if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) { 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; } @@ -3295,7 +3305,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; @@ -3303,11 +3313,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; } @@ -3364,8 +3373,8 @@ int ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func, unsigned flags ) { - LDAPDN *newDN; - LDAPRDN *newRDN; + LDAPDN newDN; + LDAPRDN newRDN; LDAPAVA *newAVA, *baseAVA; X509_NAME_ENTRY *ne; ASN1_OBJECT *obj; @@ -3393,21 +3402,20 @@ ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func, nrdns = ne->set + 1; /* Allocate the DN/RDN/AVA stuff as a single block */ - dnsize = sizeof(LDAPDN) + sizeof(LDAPRDN *) * (nrdns+1); - dnsize += sizeof(LDAPRDN) * nrdns + sizeof(LDAPAVA *) * (navas+nrdns); + dnsize = sizeof(LDAPRDN) * (nrdns+1); + dnsize += sizeof(LDAPAVA *) * (navas+nrdns); dnsize += sizeof(LDAPAVA) * navas; if (dnsize > sizeof(ptrs)) { - newDN = (LDAPDN *)LDAP_MALLOC( dnsize ); + newDN = (LDAPDN)LDAP_MALLOC( dnsize ); if ( newDN == NULL ) return LDAP_NO_MEMORY; } else { - newDN = (LDAPDN *)ptrs; + newDN = (LDAPDN)ptrs; } - newDN[0] = (LDAPRDN**)(newDN+1); - newDN[0][nrdns] = NULL; - newRDN = (LDAPRDN*)(newDN[0] + nrdns+1); - newAVA = (LDAPAVA*)(newRDN + navas + nrdns*2); + 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. */ @@ -3422,12 +3430,11 @@ ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func, * previous RDN and advance. */ if ( j > 0 ) { - newRDN[0][k] = NULL; - newRDN = (LDAPRDN*)(newRDN[0]+k+1); + newRDN[k] = NULL; + newRDN += k+1; } - newDN[0][j++] = newRDN; + newDN[j++] = newRDN; - newRDN[0] = (LDAPAVA**)(newRDN+1); k = 0; set = ne->set; } @@ -3444,6 +3451,7 @@ ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func, #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; @@ -3496,6 +3504,7 @@ get_oid: newAVA->la_attr.bv_val = oidptr; /* 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; @@ -3505,35 +3514,35 @@ to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value ); case V_ASN1_IA5STRING: case V_ASN1_PRINTABLESTRING: /* These are always 7-bit strings */ - ber_dupbv( &newAVA->la_value, &Val ); + newAVA->la_value = Val; default: ; } - newRDN[0][k] = newAVA; + newRDN[k] = newAVA; newAVA++; k++; } - newRDN[0][k] = NULL; + newRDN[k] = NULL; if ( func ) { - rc = func( newDN, flags ); + rc = func( newDN, flags, NULL ); if ( rc != LDAP_SUCCESS ) goto nomem; } - rc = ldap_dn2bv( newDN, bv, LDAP_DN_FORMAT_LDAPV3 ); + rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL ); nomem: for (;baseAVA < newAVA; baseAVA++) { - LDAP_FREE( baseAVA->la_value.bv_val ); -#ifdef HAVE_EBCDIC - if ( !func ) LDAP_FREE( baseAVA->la_attr.bv_val ); -#endif + 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 ) + if ( newDN != (LDAPDN) ptrs ) LDAP_FREE( newDN ); return rc; } diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index c6191e8aeb..9bb6a9b2ca 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -348,13 +348,21 @@ LDAP_F ( void ) ldap_int_initialize_global_options LDAP_P(( /* memory.c */ /* simple macros to realloc for now */ -#define LDAP_MALLOC(s) (LBER_MALLOC((s))) -#define LDAP_CALLOC(n,s) (LBER_CALLOC((n),(s))) -#define LDAP_REALLOC(p,s) (LBER_REALLOC((p),(s))) -#define LDAP_FREE(p) (LBER_FREE((p))) -#define LDAP_VFREE(v) (LBER_VFREE((void **)(v))) -#define LDAP_STRDUP(s) (LBER_STRDUP((s))) -#define LDAP_STRNDUP(s,l) (LBER_STRNDUP((s),(l))) +#define LDAP_MALLOC(s) (ber_memalloc_x((s),NULL)) +#define LDAP_CALLOC(n,s) (ber_memcalloc_x((n),(s),NULL)) +#define LDAP_REALLOC(p,s) (ber_memrealloc_x((p),(s),NULL)) +#define LDAP_FREE(p) (ber_memfree_x((p),NULL)) +#define LDAP_VFREE(v) (ber_memvfree_x((void **)(v),NULL)) +#define LDAP_STRDUP(s) (ber_strdup_x((s),NULL)) +#define LDAP_STRNDUP(s,l) (ber_strndup_x((s),(l),NULL)) + +#define LDAP_MALLOCX(s,x) (ber_memalloc_x((s),(x))) +#define LDAP_CALLOCX(n,s,x) (ber_memcalloc_x((n),(s),(x))) +#define LDAP_REALLOCX(p,s,x) (ber_memrealloc_x((p),(s),(x))) +#define LDAP_FREEX(p,x) (ber_memfree_x((p),(x))) +#define LDAP_VFREEX(v,x) (ber_memvfree_x((void **)(v),(x))) +#define LDAP_STRDUPX(s,x) (ber_strdup_x((s),(x))) +#define LDAP_STRNDUPX(s,l,x) (ber_strndup_x((s),(l),(x))) /* * in error.c diff --git a/libraries/libldap/tls.c b/libraries/libldap/tls.c index cf2604d2f5..5c0ca1a52a 100644 --- a/libraries/libldap/tls.c +++ b/libraries/libldap/tls.c @@ -112,6 +112,7 @@ ldap_pvt_tls_destroy( void ) tls_def_ctx = NULL; EVP_cleanup(); + ERR_remove_state(0); ERR_free_strings(); if ( tls_opt_certfile ) {