From a117c5eee7b566a469197ecc6ed454560a05ddac Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Wed, 13 Feb 2002 11:46:33 +0000 Subject: [PATCH] use ldap_bv2[r]dn and turn ldap_str2[r]dn into wrappers --- include/ldap.h | 7 +++ libraries/libldap/getdn.c | 80 ++++++++++++++++++++---------- servers/slapd/back-bdb/modrdn.c | 4 +- servers/slapd/back-ldbm/modrdn.c | 4 +- servers/slapd/back-monitor/conn.c | 2 +- servers/slapd/back-passwd/search.c | 4 +- servers/slapd/dn.c | 9 +--- 7 files changed, 70 insertions(+), 40 deletions(-) diff --git a/include/ldap.h b/include/ldap.h index 625f8f2c39..b1f1df6b1c 100644 --- a/include/ldap.h +++ b/include/ldap.h @@ -1227,6 +1227,13 @@ ldap_dn2str LDAP_P(( char **str, unsigned flags )); +LDAP_F( int ) +ldap_bv2rdn LDAP_P(( + struct berval *bv, + LDAPRDN **rdn, + char **next, + unsigned flags )); + LDAP_F( int ) ldap_str2rdn LDAP_P(( LDAP_CONST char *str, diff --git a/libraries/libldap/getdn.c b/libraries/libldap/getdn.c index f3c60465fd..a84a50be49 100644 --- a/libraries/libldap/getdn.c +++ b/libraries/libldap/getdn.c @@ -28,7 +28,7 @@ #define PRETTY_ESCAPE /* parsing/printing routines */ -static int str2strval( const char *str, struct berval *val, +static int str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, unsigned *retFlags ); static int DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags ); @@ -599,24 +599,19 @@ ldap_dnfree( LDAPDN *dn ) #define TMP_RDN_SLOTS 32 int -ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags ) +ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags ) { - assert( bv ); - assert( dn ); - - /* - * FIXME: ldap_bv2dn() and ldap_str2dn() will be swapped, - * i.e. ldap_str2dn() will become a wrapper for ldap_bv2dn() - */ - if ( bv->bv_len != strlen( bv->bv_val ) ) { - return LDAP_INVALID_DN_SYNTAX; - } + struct berval bv = { 0, (char *)str }; + + assert( str ); - return ldap_str2dn( bv->bv_val, dn, flags ); + bv.bv_len = strlen( str ); + + return ldap_bv2dn( &bv, dn, flags ); } int -ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags ) +ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags ) { const char *p; int rc = LDAP_DECODING_ERROR; @@ -625,8 +620,10 @@ ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags ) LDAPDN *newDN = NULL; LDAPRDN *newRDN = NULL, *tmpDN_[TMP_RDN_SLOTS], **tmpDN = tmpDN_; int num_slots = TMP_RDN_SLOTS; + char *str = bv->bv_val; - assert( str ); + assert( bv ); + assert( bv->bv_val ); assert( dn ); Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" ); @@ -684,8 +681,9 @@ ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags ) for ( ; p[ 0 ]; p++ ) { int err; + struct berval tmpbv = { bv->bv_len - ( p - str ), (char *)p }; - err = ldap_str2rdn( p, &newRDN, (char **) &p, flags ); + err = ldap_bv2rdn( &tmpbv, &newRDN, (char **) &p, flags ); if ( err != LDAP_SUCCESS ) { goto parsing_error; } @@ -793,7 +791,7 @@ return_result:; LDAP_FREE( tmpDN ); } - Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc ); + Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc ); *dn = newDN; return( rc ); @@ -811,7 +809,21 @@ int ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn, char **n_in, unsigned flags ) { - const char **n = (const char **) n_in; + struct berval bv = { 0, (char *)str }; + + assert( str ); + assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */ + + bv.bv_len = strlen( str ); + + return ldap_bv2rdn( &bv, rdn, n_in, flags ); +} + +int +ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn, + char **n_in, unsigned flags ) +{ + const char **n = (const char **) n_in; const char *p; int navas = 0; int state = B4AVA; @@ -825,15 +837,24 @@ ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn, LDAPRDN *newRDN = NULL; LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_; int num_slots = TMP_AVA_SLOTS; + + char *str; + ber_len_t stoplen; - assert( str ); + assert( bv ); + assert( bv->bv_len ); + assert( bv->bv_val ); assert( rdn || flags & LDAP_DN_SKIP ); assert( n ); #if 0 - Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" ); + Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2rdn(%s,%u)\n%s", + bv->bv_val, flags, "" ); #endif + str = bv->bv_val; + stoplen = bv->bv_len; + if ( rdn ) { *rdn = NULL; } @@ -1126,7 +1147,8 @@ ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn, switch ( LDAP_DN_FORMAT( flags ) ) { case LDAP_DN_FORMAT_LDAP: case LDAP_DN_FORMAT_LDAPV3: - if ( str2strval( p, &attrValue, &p, flags, + if ( str2strval( p, stoplen - ( p - str ), + &attrValue, &p, flags, &attrValueEncoding ) ) { goto parsing_error; } @@ -1310,9 +1332,9 @@ return_result:; * '\' + HEXPAIR(p) -> unhex(p) */ static int -str2strval( const char *str, 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 ) { - const char *p, *startPos, *endPos = NULL; + const char *p, *end, *startPos, *endPos = NULL; ber_len_t len, escapes; assert( str ); @@ -1320,8 +1342,8 @@ str2strval( const char *str, struct berval *val, const char **next, unsigned fla assert( next ); *next = NULL; - - for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) { + end = str + stoplen; + for ( startPos = p = str, escapes = 0; p < end; p++ ) { if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) { p++; if ( p[ 0 ] == '\0' ) { @@ -1409,7 +1431,13 @@ str2strval( const char *str, struct berval *val, const char **next, unsigned fla val->bv_len = len; if ( escapes == 0 ) { - val->bv_val = LDAP_STRNDUP( startPos, len ); + if ( *retFlags == LDAP_AVA_NONPRINTABLE ) { + val->bv_val = LDAP_MALLOC( len + 1 ); + AC_MEMCPY( val->bv_val, startPos, len ); + val->bv_val[ len ] = '\0'; + } else { + val->bv_val = LDAP_STRNDUP( startPos, len ); + } } else { ber_len_t s, d; diff --git a/servers/slapd/back-bdb/modrdn.c b/servers/slapd/back-bdb/modrdn.c index 4a7fdae141..4ff6577f9b 100644 --- a/servers/slapd/back-bdb/modrdn.c +++ b/servers/slapd/back-bdb/modrdn.c @@ -430,7 +430,7 @@ retry: /* transaction retry */ /* Get attribute type and attribute value of our new rdn, we will * need to add that to our new entry */ - if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text, + if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text, LDAP_DN_FORMAT_LDAP ) ) { Debug( LDAP_DEBUG_TRACE, @@ -445,7 +445,7 @@ retry: /* transaction retry */ "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n", new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 ); - if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text, + if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text, LDAP_DN_FORMAT_LDAP ) ) { Debug( LDAP_DEBUG_TRACE, diff --git a/servers/slapd/back-ldbm/modrdn.c b/servers/slapd/back-ldbm/modrdn.c index 2329b53f2d..709f3b6acf 100644 --- a/servers/slapd/back-ldbm/modrdn.c +++ b/servers/slapd/back-ldbm/modrdn.c @@ -497,7 +497,7 @@ ldbm_back_modrdn( /* Get attribute types and values of our new rdn, we will * need to add that to our new entry */ - if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text, + if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text, LDAP_DN_FORMAT_LDAP ) ) { #ifdef NEW_LOGGING @@ -525,7 +525,7 @@ ldbm_back_modrdn( #endif /* Retrieve the old rdn from the entry's dn */ - if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text, + if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text, LDAP_DN_FORMAT_LDAP ) ) { #ifdef NEW_LOGGING diff --git a/servers/slapd/back-monitor/conn.c b/servers/slapd/back-monitor/conn.c index 3b09f93c02..4d91d69384 100644 --- a/servers/slapd/back-monitor/conn.c +++ b/servers/slapd/back-monitor/conn.c @@ -386,7 +386,7 @@ monitor_subsys_conn_create( /* create exactly the required entry */ - if ( ldap_str2rdn( ndn->bv_val, &values, (char **)&text, + if ( ldap_bv2rdn( ndn, &values, (char **)&text, LDAP_DN_FORMAT_LDAP ) ) { return( -1 ); diff --git a/servers/slapd/back-passwd/search.c b/servers/slapd/back-passwd/search.c index bad2c17763..e88e05030b 100644 --- a/servers/slapd/back-passwd/search.c +++ b/servers/slapd/back-passwd/search.c @@ -90,7 +90,7 @@ passwd_back_search( /* Use the first attribute of the DN * as an attribute within the entry itself. */ - if( ldap_str2rdn( base->bv_val, &rdn, (char **)&text, + if( ldap_bv2rdn( base, &rdn, (char **)&text, LDAP_DN_FORMAT_LDAP ) ) { err = LDAP_INVALID_DN_SYNTAX; @@ -197,7 +197,7 @@ passwd_back_search( goto done; } - if ( ldap_str2rdn( base->bv_val, &rdn, (char **)&text, + if ( ldap_bv2rdn( base, &rdn, (char **)&text, LDAP_DN_FORMAT_LDAP )) { err = LDAP_OPERATIONS_ERROR; diff --git a/servers/slapd/dn.c b/servers/slapd/dn.c index 031c6c7fa8..a3c1b7e3e7 100644 --- a/servers/slapd/dn.c +++ b/servers/slapd/dn.c @@ -479,11 +479,6 @@ dnPrettyNormal( pretty->bv_len = 0; normal->bv_len = 0; - /* FIXME: str2dn should take a bv and handle this */ - if( strlen( val->bv_val ) != val->bv_len ) { - return LDAP_INVALID_SYNTAX; - } - /* FIXME: should be liberal in what we accept */ rc = ldap_bv2dn( val, &dn, LDAP_DN_FORMAT_LDAP ); if ( rc != LDAP_SUCCESS ) { @@ -619,7 +614,7 @@ dnExtractRdn( return LDAP_OTHER; } - rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP ); + rc = ldap_bv2rdn( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP ); if ( rc != LDAP_SUCCESS ) { return rc; } @@ -698,7 +693,7 @@ rdnValidate( struct berval *rdn ) /* * must be parsable */ - rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP ); + rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP ); if ( rc != LDAP_SUCCESS ) { return 0; } -- 2.39.5