/* dn.c - routines for dealing with distinguished names */
/* $OpenLDAP$ */
/*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "slap.h"
+const struct berval slap_empty_bv = { 0, "" };
+
#define SLAP_LDAPDN_PRETTY 0x1
/*
/*
* Replace attr oid/name with the canonical name
*/
- free( ava->la_attr.bv_val );
- ber_dupbv( &ava->la_attr, &ad->ad_cname );
+ ava->la_attr = ad->ad_cname;
validate = ad->ad_type->sat_syntax->ssyn_validate;
* (use memcmp, which implies alphabetical order in case of IA5 value;
* this should guarantee the repeatability of the operation).
*
+ * Note: the sorting can be slightly improved by sorting first
+ * by attribute type length, then by alphabetical order.
+ *
* uses a linear search; should be fine since the number of AVAs in
* a RDN should be limited.
*/
}
ava = rdn[ 0 ][ i ];
- a = strcmp( ava_in->la_value.bv_val,
- ava->la_value.bv_val );
+ a = strcmp( ava_in->la_attr.bv_val,
+ ava->la_attr.bv_val );
}
/*
slap_syntax_transform_func *transf = NULL;
MatchingRule *mr;
struct berval bv = { 0, NULL };
+ int do_sort = 0;
assert( ava );
}
ava->la_private = ( void * )ad;
+ do_sort = 1;
}
/*
* Replace attr oid/name with the canonical name
*/
- free( ava->la_attr.bv_val );
- ber_dupbv( &ava->la_attr, &ad->ad_cname );
+ ava->la_attr = ad->ad_cname;
if( flags & SLAP_LDAPDN_PRETTY ) {
transf = ad->ad_type->sat_syntax->ssyn_pretty;
char *s = bv.bv_val;
ber_str2bv( UTF8normalize( bv.bv_val ? &bv
- : &ava->la_value, UTF8_CASEFOLD ),
+ : &ava->la_value, LDAP_UTF8_CASEFOLD ),
0, 0, &bv );
free( s );
}
ava->la_value = bv;
}
- AVA_Sort( rdn, iAVA );
+ if( do_sort ) AVA_Sort( rdn, iAVA );
}
}
return( LDAP_SUCCESS );
}
-#ifdef SLAP_DN_MIGRATION
-/*
- * these routines are provided for migration purposes only!
- * dn_validate is deprecated in favor of dnValidate
- * dn_normalize is deprecated in favor of dnNormalize
- * strcmp/strcasecmp for DNs is deprecated in favor of dnMatch
- *
- * other routines are likewise deprecated but may not yet have
- * replacement functions.
- */
-
-/*
- * dn_validate - validate and compress dn. the dn is
- * compressed in place are returned if valid.
- * Deprecated in favor of dnValidate()
- */
-char *
-dn_validate( char *dn )
-{
- struct berval val;
- struct berval *pretty = NULL;
- int rc;
-
- if ( dn == NULL || dn[0] == '\0' ) {
- return dn;
- }
-
- val.bv_val = dn;
- val.bv_len = strlen( dn );
-
- rc = dnPretty( NULL, &val, &pretty );
- if ( rc != LDAP_SUCCESS ) {
- return NULL;
- }
-
- if ( val.bv_len < pretty->bv_len ) {
- ber_bvfree( pretty );
- return NULL;
- }
-
- AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
- ber_bvfree( pretty );
-
- return dn;
-}
-
-/*
- * dn_normalize - put dn into a canonical form suitable for storing
- * in a hash database. this involves normalizing the case as well as
- * the format. the dn is normalized in place as well as returned if valid.
- * Deprecated in favor of dnNormalize()
- */
-char *
-dn_normalize( char *dn )
-{
- struct berval val;
- struct berval *normalized = NULL;
- int rc;
-
- if ( dn == NULL || dn[0] == '\0' ) {
- return dn;
- }
-
- val.bv_val = dn;
- val.bv_len = strlen( dn );
-
- rc = dnNormalize( NULL, &val, &normalized );
- if ( rc != LDAP_SUCCESS ) {
- return NULL;
- }
-
- if ( val.bv_len < normalized->bv_len ) {
- ber_bvfree( normalized );
- return NULL;
- }
-
- AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
- ber_bvfree( normalized );
-
- return dn;
-}
-
/*
* dnParent - dn's parent, in-place
+ *
+ * note: the incoming dn is assumed to be normalized/prettyfied,
+ * so that escaped rdn/ava separators are in '\'+hexpair form
*/
-int
+void
dnParent(
- const char *dn,
- const char **pdn )
+ struct berval *dn,
+ struct berval *pdn )
{
- const char *p;
- int rc;
+ char *p;
- rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
- if ( rc != LDAP_SUCCESS ) {
- return rc;
+ p = strchr( dn->bv_val, ',' );
+
+ /* one-level dn */
+ if ( p == NULL ) {
+ *pdn = slap_empty_bv;
+ return;
}
assert( DN_SEPARATOR( p[ 0 ] ) );
p++;
- while ( ASCII_SPACE( p[ 0 ] ) ) {
- p++;
- }
-
- *pdn = p;
-
- return LDAP_SUCCESS;
-}
-
-/*
- * dn_parent - return the dn's parent, in-place
- * FIXME: should be replaced by dnParent()
- */
-char *
-dn_parent(
- Backend *be,
- const char *dn )
-{
- const char *pdn;
-
- if ( dn == NULL ) {
- return NULL;
- }
-
- while ( dn[ 0 ] != '\0' && ASCII_SPACE( dn[ 0 ] ) ) {
- dn++;
- }
-
- if ( dn[ 0 ] == '\0' ) {
- return NULL;
- }
-
- if ( be != NULL && be_issuffix( be, dn ) ) {
- return NULL;
- }
+ assert( ATTR_LEADCHAR( p[ 0 ] ) );
+ pdn->bv_val = p;
+ pdn->bv_len = dn->bv_len - (p - dn->bv_val);
- if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
- return NULL;
- }
-
- return ( char * )pdn;
+ return;
}
int
return LDAP_OTHER;
}
- rc = ldap_str2rdn( dn->bv_val, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
+ rc = ldap_str2rdn( dn->bv_val, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
}
/*
- * FIXME: should be replaced by dnExtractRdn() (together with dn_rdn)
+ * We can assume the input is a prettied or normalized DN
*/
int
dn_rdnlen(
Backend *be,
struct berval *dn_in )
{
- int rc;
const char *p;
assert( dn_in );
return 0;
}
- if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
- return 0;
- }
-
- rc = ldap_str2rdn( dn_in->bv_val, NULL, &p,
- LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
- if ( rc != LDAP_SUCCESS ) {
+ if ( be != NULL && be_issuffix( be, dn_in ) ) {
return 0;
}
- return p - dn_in->bv_val;
-}
-
-/*
- * FIXME: should be replaced by dnExtractRdn() (together with dn_rdnlen)
- */
-char * dn_rdn(
- Backend *be,
- struct berval *dn_in )
-{
- struct berval rdn;
-
- assert( dn_in );
-
- if ( dn_in == NULL ) {
- return NULL;
- }
-
- if ( !dn_in->bv_len ) {
- return NULL;
- }
-
- if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
- return NULL;
- }
-
- if ( dnExtractRdn( dn_in, &rdn ) != LDAP_SUCCESS ) {
- return NULL;
- }
-
- return rdn.bv_val;
-}
-
-/*
- * dn_issuffix - tells whether suffix is a suffix of dn.
- * Both dn and suffix must be normalized.
- * deprecated in favor of dnIsSuffix()
- */
-int
-dn_issuffix(
- const char *dn,
- const char *suffix
-)
-{
- struct berval bvdn, bvsuffix;
-
- assert( dn );
- assert( suffix );
-
- bvdn.bv_val = (char *) dn;
- bvdn.bv_len = strlen( dn );
- bvsuffix.bv_val = (char *) suffix;
- bvsuffix.bv_len = strlen( suffix );
-
- return dnIsSuffix( &bvdn, &bvsuffix );
-}
-
-/* rdn_attr_type:
- *
- * Given a string (i.e. an rdn) of the form:
- * "attribute_type = attribute_value"
- * this function returns the type of an attribute, that is the
- * string "attribute_type" which is placed in newly allocated
- * memory. The returned string will be null-terminated.
- *
- * Deprecated
- */
-
-char * rdn_attr_type( const char * s )
-{
- char **attrs = NULL, **values = NULL, *retval;
-
- if ( rdn_attrs( s, &attrs, &values ) != LDAP_SUCCESS ) {
- return NULL;
- }
-
- retval = ch_strdup( attrs[ 0 ] );
-
- charray_free( attrs );
- charray_free( values );
-
- return retval;
-}
-
-
-/* rdn_attr_value:
- *
- * Given a string (i.e. an rdn) of the form:
- * "attribute_type = attribute_value"
- * this function returns "attribute_type" which is placed in newly allocated
- * memory. The returned string will be null-terminated and may contain
- * spaces (i.e. "John Doe\0").
- *
- * Deprecated
- */
+ p = strchr( dn_in->bv_val, ',' );
-char *
-rdn_attr_value( const char * rdn )
-{
- char **values = NULL, *retval;
-
- if ( rdn_attrs( rdn, NULL, &values ) != LDAP_SUCCESS ) {
- return NULL;
- }
-
- retval = ch_strdup( values[ 0 ] );
-
- charray_free( values );
-
- return retval;
-}
-
-
-/* rdn_attrs:
- *
- * Given a string (i.e. an rdn) of the form:
- * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
- * this function stores the types of the attributes in ptypes, that is the
- * array of strings "attribute_type" which is placed in newly allocated
- * memory, and the values of the attributes in pvalues, that is the
- * array of strings "attribute_value" which is placed in newly allocated
- * memory. Returns 0 on success, -1 on failure.
- *
- * note: got part of the code from dn_validate
- *
- * Deprecated; directly use LDAPRDN from ldap_str2rdn
- */
-int
-rdn_attrs( const char * rdn, char ***types, char ***values)
-{
- LDAPRDN *tmpRDN;
- const char *p;
- int iAVA;
- int rc;
-
- assert( rdn );
- assert( values );
- assert( *values == NULL );
- assert( types == NULL || *types == NULL );
-
- rc = ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP );
- if ( rc != LDAP_SUCCESS ) {
- return rc;
- }
-
-#if 0
- /*
- * FIXME: should we complain if the rdn is actually a dn?
- */
- if ( p[ 0 ] != '\0' ) {
- ldap_rdnfree( tmpRDN );
- return LDAP_INVALID_DN_SYNTAX;
- }
-#endif
-
- for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
- LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
-
- assert( ava );
- assert( ava->la_attr.bv_val );
- assert( ava->la_value.bv_val );
-
- if ( types ) {
- charray_add_n( types, ava->la_attr.bv_val,
- ava->la_attr.bv_len );
- }
- charray_add_n( values, ava->la_value.bv_val,
- ava->la_value.bv_len );
- }
-
- ldap_rdnfree( tmpRDN );
-
- return LDAP_SUCCESS;
+ return p ? p - dn_in->bv_val : dn_in->bv_len;
}
/*
* must be parsable
*/
- rc = ldap_str2rdn( rdn, &RDN, &p, LDAP_DN_FORMAT_LDAP );
+ rc = ldap_str2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
if ( rc != LDAP_SUCCESS ) {
return 0;
}
strcpy( ptr, parent_dn->bv_val );
}
-#endif /* SLAP_DN_MIGRATION */
/*
* dnIsSuffix - tells whether suffix is a suffix of dn.
}
/* no rdn separator or escaped rdn separator */
- if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
- || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
+ if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
return 0;
}