]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/dn.c
Fix previous commit
[openldap] / servers / slapd / dn.c
index c90dffaf6d867b132b3bb440bf4da57ae4cb1219..7d836a630e43c424e0275c1c161c27a1ab7de901 100644 (file)
@@ -18,6 +18,8 @@
 
 #include "slap.h"
 
+const struct berval slap_empty_bv = { 0, "" };
+
 #define SLAP_LDAPDN_PRETTY 0x1
 
 /*
@@ -139,6 +141,9 @@ dnValidate(
  * (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.
  */
@@ -192,8 +197,8 @@ AVA_Sort( LDAPRDN *rdn, int iAVA )
                        }
 
                        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 );
                }
 
                /*
@@ -553,52 +558,47 @@ dnMatch(
        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.
+ * note: the incoming dn is assumed to be normalized/prettyfied,
+ * so that escaped rdn/ava separators are in '\'+hexpair form
  */
-
-/*
- * 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 )
+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;
-       }
-
-       val.bv_val = dn;
-       val.bv_len = strlen( dn );
+       p = strchr( dn->bv_val, ',' );
 
-       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
@@ -635,41 +635,6 @@ dn_normalize( char *dn )
        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, (char **)&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()
@@ -679,7 +644,7 @@ dn_parent(
        Backend         *be,
        const char      *dn )
 {
-       const char      *pdn;
+       struct berval   bv, pdn;
 
        if ( dn == NULL ) {
                return NULL;
@@ -693,16 +658,20 @@ dn_parent(
                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( 
@@ -735,7 +704,7 @@ dnExtractRdn(
 }
 
 /*
- * FIXME: should be replaced by dnExtractRdn()
+ * We can assume the input is a prettied or normalized DN
  */
 int 
 dn_rdnlen(
@@ -755,17 +724,13 @@ 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, (char **)&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;
+       return p ? p - dn_in->bv_val : dn_in->bv_len;
 }
 
 
@@ -860,7 +825,6 @@ build_new_dn( struct berval * new_dn,
        strcpy( ptr, parent_dn->bv_val );
 }
 
-#endif /* SLAP_DN_MIGRATION */
 
 /*
  * dnIsSuffix - tells whether suffix is a suffix of dn.
@@ -887,8 +851,7 @@ dnIsSuffix(
        }
 
        /* 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;
        }