#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 );
}
/*
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, (char **)&p,
- LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
- if ( rc != LDAP_SUCCESS ) {
- return rc;
- }
+ p = strchr( dn->bv_val, ',' );
- /* Parent is root */
- if (*p == '\0') {
- *pdn = "";
- return LDAP_SUCCESS;
+ /* 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;
+ assert( ATTR_LEADCHAR( p[ 0 ] ) );
+ pdn->bv_val = p;
+ pdn->bv_len = dn->bv_len - (p - dn->bv_val);
- 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;
- }
-
- if ( dnParent( dn, &pdn ) != LDAP_SUCCESS ) {
- return NULL;
- }
-
- return ( char * )pdn;
+ return;
}
int
}
/*
- * FIXME: should be replaced by dnExtractRdn()
+ * 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 ) ) {
+ if ( be != NULL && be_issuffix( be, dn_in ) ) {
return 0;
}
- rc = ldap_str2rdn( dn_in->bv_val, NULL, (char **)&p,
- LDAP_DN_FORMAT_LDAP | LDAP_DN_SKIP );
- if ( rc != LDAP_SUCCESS ) {
- return 0;
- }
-
- return p - dn_in->bv_val;
-}
-
-/* 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;
-}
+ p = strchr( dn_in->bv_val, ',' );
-
-/* 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, (char **)&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;
}
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;
}