From 472484725f862cc0ee27877c59e11ea4a54e7fcf Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Sat, 17 Aug 2002 19:42:59 +0000 Subject: [PATCH] Update dn2domain() to use str2dn() instead of explode_dn() --- libraries/libldap/dnssrv.c | 132 ++++++++++++++++--------------------- libraries/libldap/dntest.c | 9 +++ 2 files changed, 64 insertions(+), 77 deletions(-) diff --git a/libraries/libldap/dnssrv.c b/libraries/libldap/dnssrv.c index 0f7c863b2b..e2a9c0af29 100644 --- a/libraries/libldap/dnssrv.c +++ b/libraries/libldap/dnssrv.c @@ -37,106 +37,84 @@ int ldap_dn2domain( LDAP_CONST char *dn_in, char **domainp) { - int i; - char *domain = NULL; - char **dn; + int i, j; + char *ndomain; + LDAPDN *dn = NULL; + LDAPRDN *rdn = NULL; + LDAPAVA *ava = NULL; + struct berval domain = { 0, NULL }; + static struct berval DC = BER_BVC("DC"); + static struct berval DCOID = BER_BVC("0.9.2342.19200300.100.1.25"); assert( dn_in != NULL ); assert( domainp != NULL ); - dn = ldap_explode_dn( dn_in, 0 ); - - if( dn == NULL ) { + if ( ldap_str2dn( dn_in, &dn, LDAP_DN_FORMAT_LDAP ) != LDAP_SUCCESS ) { return -2; } - for( i=0; dn[i] != NULL; i++ ) { - char ** rdn = ldap_explode_rdn( dn[i], 0 ); - - if( rdn == NULL || *rdn == NULL ) { - LDAP_FREE( rdn ); - LDAP_FREE( domain ); - LDAP_VFREE( dn ); - return -3; - } - - - if( rdn[1] == NULL ) { - /* - * single-valued RDN - */ - char *dc; + if( dn ) for( i=0; (*dn)[i] != NULL; i++ ) { + rdn = (*dn)[i]; -#define LDAP_DC "dc=" -#define LDAP_DCOID "0.9.2342.19200300.100.1.25=" + for( j=0; (*rdn)[j] != NULL; j++ ) { + ava = (*rdn)[j]; - if( strncasecmp( rdn[0], - LDAP_DC, sizeof(LDAP_DC)-1 ) == 0 ) + if( (*dn)[i][j][1] == NULL && + !ava->la_flags && ava->la_value.bv_len && + ( ber_bvstrcasecmp( &ava->la_attr, &DC ) == 0 + || ber_bvstrcasecmp( &ava->la_attr, &DCOID ) == 0 ) ) { - dc = &rdn[0][sizeof(LDAP_DC)-1]; + if( domain.bv_len == 0 ) { + ndomain = LDAP_REALLOC( domain.bv_val, + ava->la_value.bv_len + 1); - } else if( strncmp( rdn[0], - LDAP_DCOID, sizeof(LDAP_DCOID)-1 ) == 0 ) - { - dc = &rdn[0][sizeof(LDAP_DCOID)-1]; + if( ndomain == NULL ) { + goto return_error; + } - } else { - dc = NULL; - } + domain.bv_val = ndomain; - if( dc != NULL ) { - char *ndomain; - - if( *dc == '\0' ) { - /* dc value is empty! */ - LDAP_FREE( rdn ); - LDAP_FREE( domain ); - LDAP_VFREE( dn ); - LDAP_VFREE( rdn ); - return -4; - } + AC_MEMCPY( domain.bv_val, ava->la_value.bv_val, + ava->la_value.bv_len ); - ndomain = LDAP_REALLOC( domain, - ( domain == NULL ? 0 : strlen(domain) ) - + strlen(dc) + sizeof(".") ); + domain.bv_len = ava->la_value.bv_len; + domain.bv_val[domain.bv_len] = '\0'; - if( ndomain == NULL ) { - LDAP_FREE( rdn ); - LDAP_FREE( domain ); - LDAP_VFREE( dn ); - LDAP_VFREE( rdn ); - return -5; - } - - if( domain == NULL ) { - ndomain[0] = '\0'; } else { - strcat( ndomain, "." ); + ndomain = LDAP_REALLOC( domain.bv_val, + ava->la_value.bv_len + sizeof(".") + domain.bv_len ); + + if( ndomain == NULL ) { + goto return_error; + } + + domain.bv_val = ndomain; + domain.bv_val[domain.bv_len++] = '.'; + AC_MEMCPY( &domain.bv_val[domain.bv_len], + ava->la_value.bv_val, ava->la_value.bv_len ); + domain.bv_len += ava->la_value.bv_len; + domain.bv_val[domain.bv_len] = '\0'; } - - strcat( ndomain, dc ); - - domain = ndomain; - continue; + } else { + domain.bv_len = 0; } - } - - /* - * multi-valued RDN or fall thru - */ + } + } - LDAP_VFREE( rdn ); - LDAP_FREE( domain ); - domain = NULL; - } - if( domain != NULL && *domain == '\0' ) { - LDAP_FREE( domain ); - domain = NULL; + if( domain.bv_len == 0 && domain.bv_val != NULL ) { + LDAP_FREE( domain.bv_val ); + domain.bv_val = NULL; } - *domainp = domain; + ldap_dnfree( dn ); + *domainp = domain.bv_val; return 0; + +return_error: + ldap_dnfree( dn ); + LDAP_FREE( domain.bv_val ); + return -1; } int ldap_domain2dn( diff --git a/libraries/libldap/dntest.c b/libraries/libldap/dntest.c index 97cc727880..e99b48b2ed 100644 --- a/libraries/libldap/dntest.c +++ b/libraries/libldap/dntest.c @@ -158,6 +158,15 @@ main( int argc, char *argv[] ) case LDAP_DN_FORMAT_LDAPV3: case LDAP_DN_FORMAT_LDAPV2: + n = ldap_dn2domain( strin, &tmp ); + if( n ) { + fprintf( stdout, "\nldap_dn2domain(\"%s\") FAILED\n", strin ); + } else { + fprintf( stdout, "\nldap_dn2domain(\"%s\")\n" + "\t= \"%s\"\n", strin, tmp ); + } + ldap_memfree( tmp ); + tmp = ldap_dn2ufn( strin ); fprintf( stdout, "\nldap_dn2ufn(\"%s\")\n" "\t= \"%s\"\n", strin, tmp ); -- 2.39.5