/* 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
/*
* (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 );
}
/*
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 );
}
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
+ * dnParent - dn's parent, in-place
*
- * 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()
+ * note: the incoming dn is assumed to be normalized/prettyfied,
+ * so that escaped rdn/ava separators are in '\'+hexpair form
*/
-char *
-dn_validate( char *dn )
+int
+dnParent(
+ struct berval *dn,
+ struct berval *pdn )
{
- struct berval val;
- struct berval *pretty = NULL;
- int rc;
+ char *p;
- if ( dn == NULL || dn[0] == '\0' ) {
- return dn;
- }
+ p = strchr( dn->bv_val, ',' );
- val.bv_val = dn;
- val.bv_len = strlen( dn );
-
- rc = dnPretty( NULL, &val, &pretty );
- if ( rc != LDAP_SUCCESS ) {
- return NULL;
+ /* one-level dn */
+ if ( p == NULL ) {
+ *pdn = slap_empty_bv;
+ return LDAP_SUCCESS;
}
- if ( val.bv_len < pretty->bv_len ) {
- ber_bvfree( pretty );
- return NULL;
- }
+ assert( DN_SEPARATOR( p[ 0 ] ) );
+ p++;
- AC_MEMCPY( dn, pretty->bv_val, pretty->bv_len + 1 );
- ber_bvfree( pretty );
+ assert( ATTR_LEADCHAR( p[ 0 ] ) );
+ pdn->bv_val = p;
+ pdn->bv_len = dn->bv_len - (p - dn->bv_val);
- return dn;
+ return LDAP_SUCCESS;
}
+#ifdef SLAP_DN_MIGRATION
+/*
+ * these routines are provided for migration purposes only!
+ * 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_normalize - put dn into a canonical form suitable for storing
* in a hash database. this involves normalizing the case as well as
return dn;
}
-/*
- * dnParent - dn's parent, in-place
- */
-int
-dnParent(
- const char *dn,
- const char **pdn )
-{
- const char *p;
- int rc;
-
- rc = ldap_str2rdn( dn, NULL, &p, LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
- if ( rc != LDAP_SUCCESS ) {
- return rc;
- }
-
- /* Parent is root */
- if (*p == '\0') {
- *pdn = "";
- return LDAP_SUCCESS;
- }
-
- 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()
Backend *be,
const char *dn )
{
- const char *pdn;
+ struct berval bv, pdn;
if ( dn == NULL ) {
return NULL;
return NULL;
}
- if ( be != NULL && be_issuffix( be, dn ) ) {
+ bv.bv_val = (char *)dn;
+ bv.bv_len = strlen(bv.bv_val);
+ if ( be != NULL && be_issuffix( be, &bv ) ) {
return NULL;
}
- if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
+ if ( dnParent( &bv, &pdn ) != LDAP_SUCCESS ) {
return NULL;
}
- return ( char * )pdn;
+ return pdn.bv_val;
}
+#endif /* SLAP_DN_MIGRATION */
+
int
dnExtractRdn(
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(
return 0;
}
- if ( be != NULL && be_issuffix( be, dn_in->bv_val ) ) {
+ if ( be != NULL && be_issuffix( be, dn_in ) ) {
return 0;
}
- rc = ldap_str2rdn( dn_in->bv_val, NULL, &p,
- LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
- if ( rc != LDAP_SUCCESS ) {
- return 0;
- }
+ p = strchr( dn_in->bv_val, ',' );
- 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
- */
-
-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;
}