]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/value.c
Tone down debug messages
[openldap] / servers / slapd / value.c
index 174a30579d2ffc2e464890bcf5187ad132d3b0cf..9ccdf5c0533558386f4f8eb8fe944a673bd15717 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2005 The OpenLDAP Foundation.
+ * Copyright 1998-2006 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -271,7 +271,9 @@ ordered_value_renumber( Attribute *a, int vals )
                ibv.bv_len = sprintf(ibv.bv_val, "{%d}", i);
                vtmp = a->a_vals[i];
                if ( vtmp.bv_val[0] == '{' ) {
-                       ptr = strchr(vtmp.bv_val, '}') + 1;
+                       ptr = ber_bvchr(&vtmp, '}');
+                       assert( ptr != NULL );
+                       ++ptr;
                        vtmp.bv_len -= ptr - vtmp.bv_val;
                        vtmp.bv_val = ptr;
                }
@@ -286,7 +288,9 @@ ordered_value_renumber( Attribute *a, int vals )
                if ( a->a_nvals && a->a_nvals != a->a_vals ) {
                        vtmp = a->a_nvals[i];
                        if ( vtmp.bv_val[0] == '{' ) {
-                               ptr = strchr(vtmp.bv_val, '}') + 1;
+                               ptr = ber_bvchr(&vtmp, '}');
+                               assert( ptr != NULL );
+                               ++ptr;
                                vtmp.bv_len -= ptr - vtmp.bv_val;
                                vtmp.bv_val = ptr;
                        }
@@ -321,8 +325,8 @@ ordered_value_sort( Attribute *a, int do_renumber )
                if ( a->a_vals[i].bv_val[0] == '{' ) {
                        char *ptr;
                        index = 1;
-                       ptr = strchr( a->a_vals[i].bv_val, '}' );
-                       if ( !ptr || !ptr[1] )
+                       ptr = ber_bvchr( &a->a_vals[i], '}' );
+                       if ( !ptr )
                                return LDAP_INVALID_SYNTAX;
                        if ( noindex )
                                return LDAP_INVALID_SYNTAX;
@@ -345,7 +349,7 @@ ordered_value_sort( Attribute *a, int do_renumber )
                        a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
                        BER_BVZERO(a->a_nvals+vals);
                        for ( i=0; i<vals; i++ ) {
-                               ptr = strchr(a->a_vals[i].bv_val, '}') + 1;
+                               char *ptr = ber_bvchr(&a->a_vals[i], '}') + 1;
                                a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
                                        (ptr - a->a_vals[i].bv_val);
                                a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
@@ -353,7 +357,7 @@ ordered_value_sort( Attribute *a, int do_renumber )
                        }
                } else {
                        for ( i=0; i<vals; i++ ) {
-                               ptr = strchr(a->a_nvals[i].bv_val, '}') + 1;
+                               char *ptr = ber_bvchr(&a->a_nvals[i], '}') + 1;
                                a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
                                strcpy(a->a_nvals[i].bv_val, ptr);
                        }
@@ -361,8 +365,14 @@ ordered_value_sort( Attribute *a, int do_renumber )
 #endif
                                
                indexes = ch_malloc( vals * sizeof(int) );
-               for ( i=0; i<vals; i++)
-                       indexes[i] = strtol(a->a_vals[i].bv_val+1, NULL, 0);
+               for ( i=0; i<vals; i++) {
+                       char *ptr;
+                       indexes[i] = strtol(a->a_vals[i].bv_val+1, &ptr, 0);
+                       if ( *ptr != '}' ) {
+                               ch_free( indexes );
+                               return LDAP_INVALID_SYNTAX;
+                       }
+               }
 
                /* Insertion sort */
                for ( i=1; i<vals; i++ ) {
@@ -385,6 +395,7 @@ ordered_value_sort( Attribute *a, int do_renumber )
                if ( indexes[0] != 0 || indexes[vals-1] != vals-1 ) {
                        renumber = 1;
                }
+               ch_free( indexes );
        } else {
                renumber = 1;
        }
@@ -395,6 +406,175 @@ ordered_value_sort( Attribute *a, int do_renumber )
        return 0;
 }
 
+/*
+ * wrapper for validate function
+ * uses the validate function of the syntax after removing
+ * the index, if allowed and present
+ */
+int
+ordered_value_validate(
+       AttributeDescription *ad,
+       struct berval *in,
+       int mop )
+{
+       struct berval   bv = *in;
+
+       assert( ad->ad_type->sat_syntax != NULL );
+       assert( ad->ad_type->sat_syntax->ssyn_validate != NULL );
+
+       if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
+
+               /* Skip past the assertion index */
+               if ( bv.bv_val[0] == '{' ) {
+                       char    *ptr;
+
+                       ptr = ber_bvchr( &bv, '}' );
+                       if ( ptr == NULL ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+                       ptr++;
+                       bv.bv_len -= ptr - bv.bv_val;
+                       bv.bv_val = ptr;
+                       in = &bv;
+                       /* If deleting by index, just succeed */
+                       if ( mop == LDAP_MOD_DELETE && BER_BVISEMPTY( &bv ))
+                               return LDAP_SUCCESS;
+               }
+       }
+
+       return ad->ad_type->sat_syntax->ssyn_validate( ad->ad_type->sat_syntax, in );
+}
+
+/*
+ * wrapper for pretty function
+ * uses the pretty function of the syntax after removing
+ * the index, if allowed and present; in case, it's prepended
+ * to the pretty value
+ */
+int
+ordered_value_pretty(
+       AttributeDescription *ad,
+       struct berval *val,
+       struct berval *out,
+       void *ctx )
+{
+       struct berval   bv = *val,
+                       idx = BER_BVNULL;
+       int             rc;
+
+       assert( ad->ad_type->sat_syntax != NULL );
+       assert( ad->ad_type->sat_syntax->ssyn_pretty != NULL );
+       assert( val != NULL );
+       assert( out != NULL );
+
+       if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
+
+               /* Skip past the assertion index */
+               if ( bv.bv_val[0] == '{' ) {
+                       char    *ptr;
+
+                       ptr = ber_bvchr( &bv, '}' );
+                       if ( ptr == NULL ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+                       ptr++;
+
+                       idx = bv;
+                       idx.bv_len = ptr - bv.bv_val;
+
+                       bv.bv_len -= idx.bv_len;
+                       bv.bv_val = ptr;
+
+                       val = &bv;
+               }
+       }
+
+       rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx );
+
+       if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
+               bv = *out;
+
+               out->bv_len = idx.bv_len + bv.bv_len;
+               out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
+               
+               AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len );
+               AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
+
+               ber_memfree_x( bv.bv_val, ctx );
+       }
+
+       return rc;
+}
+
+/*
+ * wrapper for normalize function
+ * uses the normalize function of the attribute description equality rule
+ * after removing the index, if allowed and present; in case, it's
+ * prepended to the value
+ */
+int
+ordered_value_normalize(
+       slap_mask_t usage,
+       AttributeDescription *ad,
+       MatchingRule *mr,
+       struct berval *val,
+       struct berval *normalized,
+       void *ctx )
+{
+       struct berval   bv = *val,
+                       idx = BER_BVNULL;
+       int             rc;
+
+       assert( ad->ad_type->sat_equality != NULL );
+       assert( ad->ad_type->sat_equality->smr_normalize != NULL );
+       assert( val != NULL );
+       assert( normalized != NULL );
+
+       if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
+
+               /* Skip past the assertion index */
+               if ( bv.bv_val[ 0 ] == '{' ) {
+                       char    *ptr;
+
+                       ptr = ber_bvchr( &bv, '}' );
+                       if ( ptr == NULL ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+                       ptr++;
+
+                       idx = bv;
+                       idx.bv_len = ptr - bv.bv_val;
+
+                       bv.bv_len -= idx.bv_len;
+                       bv.bv_val = ptr;
+
+                       /* validator will already prevent this for Adds */
+                       if ( BER_BVISEMPTY( &bv )) {
+                               ber_dupbv_x( normalized, &idx, ctx );
+                               return LDAP_SUCCESS;
+                       }
+                       val = &bv;
+               }
+       }
+
+       rc = ad->ad_type->sat_equality->smr_normalize( usage,
+               ad->ad_type->sat_syntax, mr, val, normalized, ctx );
+
+       if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
+               bv = *normalized;
+
+               normalized->bv_len = idx.bv_len + bv.bv_len;
+               normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx );
+               
+               AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len );
+               AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
+
+               ber_memfree_x( bv.bv_val, ctx );
+       }
+
+       return rc;
+}
+
 /* A wrapper for value match, handles Equality matches for attributes
  * with ordered values.
  */
@@ -433,7 +613,11 @@ ordered_value_match(
 
                /* Skip past the assertion index */
                if ( bv2.bv_val[0] == '{' ) {
-                       ptr = strchr( bv2.bv_val, '}' ) + 1;
+                       ptr = ber_bvchr( &bv2, '}' );
+                       if ( ptr == NULL ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+                       ptr++;
                        bv2.bv_len -= ptr - bv2.bv_val;
                        bv2.bv_val = ptr;
                        v2 = &bv2;
@@ -460,7 +644,11 @@ ordered_value_match(
                }
                /* Skip past the attribute index */
                if ( bv1.bv_val[0] == '{' ) {
-                       ptr = strchr( bv1.bv_val, '}' ) + 1;
+                       ptr = ber_bvchr( &bv1, '}' );
+                       if ( ptr == NULL ) {
+                               return LDAP_INVALID_SYNTAX;
+                       }
+                       ptr++;
                        bv1.bv_len -= ptr - bv1.bv_val;
                        bv1.bv_val = ptr;
                        v1 = &bv1;
@@ -517,9 +705,17 @@ ordered_value_add(
        }
 
        for (i=0; i<vnum; i++) {
+               char    *next;
+
                k = -1;
                if ( vals[i].bv_val[0] == '{' ) {
-                       k = strtol( vals[i].bv_val+1, NULL, 0 );
+                       k = strtol( vals[i].bv_val + 1, &next, 0 );
+                       if ( next == vals[i].bv_val + 1 ||
+                               next[ 0 ] != '}' ||
+                               next - vals[i].bv_val > vals[i].bv_len )
+                       {
+                               return -1;
+                       }
                        if ( k > anum ) k = -1;
                }
                /* No index, or index is greater than current number of