1 /* value.c - routines for dealing with values */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * Copyright (c) 1995 Regents of the University of Michigan.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the University
23 * may not be used to endorse or promote products derived from this
24 * software without specific prior written permission. This software
25 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
34 #include <ac/string.h>
49 if ( addvals != NULL ) {
50 for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ )
54 if ( *vals == NULL ) {
55 *vals = (BerVarray) SLAP_MALLOC( (nn + 1)
56 * sizeof(struct berval) );
58 Debug(LDAP_DEBUG_TRACE,
59 "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
60 return LBER_ERROR_MEMORY;
65 for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
68 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
69 (n + nn + 1) * sizeof(struct berval) );
71 Debug(LDAP_DEBUG_TRACE,
72 "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
73 return LBER_ERROR_MEMORY;
78 for ( ; !BER_BVISNULL( addvals ); v2++, addvals++ ) {
79 ber_dupbv( v2, addvals );
80 if ( BER_BVISNULL( v2 ) ) break;
90 struct berval *addval )
95 if ( *vals == NULL ) {
96 *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
98 Debug(LDAP_DEBUG_TRACE,
99 "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
100 return LBER_ERROR_MEMORY;
105 for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
108 *vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
109 (n + 2) * sizeof(struct berval) );
110 if( *vals == NULL ) {
111 Debug(LDAP_DEBUG_TRACE,
112 "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
113 return LBER_ERROR_MEMORY;
118 ber_dupbv(v2, addval);
126 int asserted_value_validate_normalize(
127 AttributeDescription *ad,
139 /* we expect the value to be in the assertion syntax */
140 assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );
143 *text = "inappropriate matching request";
144 return LDAP_INAPPROPRIATE_MATCHING;
147 if( !mr->smr_match ) {
148 *text = "requested matching rule not supported";
149 return LDAP_INAPPROPRIATE_MATCHING;
152 if( mr->smr_syntax->ssyn_pretty ) {
153 rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx );
157 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
160 if( rc != LDAP_SUCCESS ) {
161 *text = "value does not conform to assertion syntax";
162 return LDAP_INVALID_SYNTAX;
165 if( mr->smr_normalize ) {
166 rc = (mr->smr_normalize)(
167 usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
168 ad ? ad->ad_type->sat_syntax : NULL,
171 if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );
173 if( rc != LDAP_SUCCESS ) {
174 *text = "unable to normalize value for matching";
175 return LDAP_INVALID_SYNTAX;
178 } else if ( pval.bv_val != NULL ) {
182 ber_dupbv_x( out, in, ctx );
191 AttributeDescription *ad,
194 struct berval *v1, /* stored value */
195 void *v2, /* assertion */
200 assert( mr != NULL );
202 if( !mr->smr_match ) {
203 return LDAP_INAPPROPRIATE_MATCHING;
206 rc = (mr->smr_match)( match, flags,
207 ad->ad_type->sat_syntax, mr, v1, v2 );
213 AttributeDescription *ad,
221 struct berval nval = BER_BVNULL;
222 MatchingRule *mr = ad->ad_type->sat_equality;
224 if( mr == NULL || !mr->smr_match ) {
225 return LDAP_INAPPROPRIATE_MATCHING;
228 assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
230 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
233 rc = (mr->smr_normalize)(
234 flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
235 ad ? ad->ad_type->sat_syntax : NULL,
236 mr, val, &nval, ctx );
238 if( rc != LDAP_SUCCESS ) {
239 return LDAP_INVALID_SYNTAX;
243 for ( i = 0; vals[i].bv_val != NULL; i++ ) {
247 rc = value_match( &match, ad, mr, flags,
248 &vals[i], nval.bv_val == NULL ? val : &nval, &text );
250 if( rc == LDAP_SUCCESS && match == 0 ) {
251 slap_sl_free( nval.bv_val, ctx );
256 slap_sl_free( nval.bv_val, ctx );
257 return LDAP_NO_SUCH_ATTRIBUTE;
260 /* assign new indexes to an attribute's ordered values */
262 ordered_value_renumber( Attribute *a, int vals )
264 char *ptr, ibuf[64]; /* many digits */
265 struct berval ibv, tmp, vtmp;
270 for (i=0; i<vals; i++) {
271 ibv.bv_len = sprintf(ibv.bv_val, "{%d}", i);
273 if ( vtmp.bv_val[0] == '{' ) {
274 ptr = ber_bvchr(&vtmp, '}');
275 assert( ptr != NULL );
277 vtmp.bv_len -= ptr - vtmp.bv_val;
280 tmp.bv_len = ibv.bv_len + vtmp.bv_len;
281 tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
282 strcpy( tmp.bv_val, ibv.bv_val );
283 AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
284 tmp.bv_val[tmp.bv_len] = '\0';
285 ch_free( a->a_vals[i].bv_val );
288 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
289 vtmp = a->a_nvals[i];
290 if ( vtmp.bv_val[0] == '{' ) {
291 ptr = ber_bvchr(&vtmp, '}');
292 assert( ptr != NULL );
294 vtmp.bv_len -= ptr - vtmp.bv_val;
297 tmp.bv_len = ibv.bv_len + vtmp.bv_len;
298 tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
299 strcpy( tmp.bv_val, ibv.bv_val );
300 AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
301 tmp.bv_val[tmp.bv_len] = '\0';
302 ch_free( a->a_nvals[i].bv_val );
308 /* Sort the values in an X-ORDERED VALUES attribute.
309 * If the values have no index, index them in their given order.
310 * If the values have indexes, sort them.
311 * If some are indexed and some are not, return Error.
314 ordered_value_sort( Attribute *a, int do_renumber )
317 int index = 0, noindex = 0, renumber = 0, gotnvals = 0;
320 if ( a->a_nvals && a->a_nvals != a->a_vals )
323 /* count attrs, look for index */
324 for (i=0; a->a_vals[i].bv_val; i++) {
325 if ( a->a_vals[i].bv_val[0] == '{' ) {
328 ptr = ber_bvchr( &a->a_vals[i], '}' );
330 return LDAP_INVALID_SYNTAX;
332 return LDAP_INVALID_SYNTAX;
336 return LDAP_INVALID_SYNTAX;
341 /* If values have indexes, sort the values */
343 int *indexes, j, idx;
347 /* Strip index from normalized values */
348 if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
349 a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
350 BER_BVZERO(a->a_nvals+vals);
351 for ( i=0; i<vals; i++ ) {
352 char *ptr = ber_bvchr(&a->a_vals[i], '}') + 1;
353 a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
354 (ptr - a->a_vals[i].bv_val);
355 a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
356 strcpy(a->a_nvals[i].bv_val, ptr );
359 for ( i=0; i<vals; i++ ) {
360 char *ptr = ber_bvchr(&a->a_nvals[i], '}') + 1;
361 a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
362 strcpy(a->a_nvals[i].bv_val, ptr);
367 indexes = ch_malloc( vals * sizeof(int) );
368 for ( i=0; i<vals; i++) {
370 indexes[i] = strtol(a->a_vals[i].bv_val+1, &ptr, 0);
373 return LDAP_INVALID_SYNTAX;
378 for ( i=1; i<vals; i++ ) {
381 if ( gotnvals ) ntmp = a->a_nvals[i];
383 while ((j > 0) && (indexes[j-1] > idx)) {
384 indexes[j] = indexes[j-1];
385 a->a_vals[j] = a->a_vals[j-1];
386 if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
391 if ( gotnvals ) a->a_nvals[j] = ntmp;
394 /* If range is not contiguous, must renumber */
395 if ( indexes[0] != 0 || indexes[vals-1] != vals-1 ) {
403 if ( do_renumber && renumber )
404 ordered_value_renumber( a, vals );
410 * wrapper for validate function
411 * uses the validate function of the syntax after removing
412 * the index, if allowed an present
415 ordered_value_validate(
416 AttributeDescription *ad,
419 struct berval bv = *in;
421 assert( ad->ad_type->sat_syntax != NULL );
422 assert( ad->ad_type->sat_syntax->ssyn_validate != NULL );
424 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
426 /* Skip past the assertion index */
427 if ( bv.bv_val[0] == '{' ) {
430 ptr = ber_bvchr( &bv, '}' );
432 return LDAP_INVALID_SYNTAX;
435 bv.bv_len -= ptr - bv.bv_val;
441 return ad->ad_type->sat_syntax->ssyn_validate( ad->ad_type->sat_syntax, in );
445 * wrapper for pretty function
446 * uses the pretty function of the syntax after removing
447 * the index, if allowed and present; in case, it's prepended
448 * to the pretty value
451 ordered_value_pretty(
452 AttributeDescription *ad,
457 struct berval bv = *val,
461 assert( ad->ad_type->sat_syntax != NULL );
462 assert( ad->ad_type->sat_syntax->ssyn_pretty != NULL );
463 assert( val != NULL );
464 assert( out != NULL );
466 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
468 /* Skip past the assertion index */
469 if ( bv.bv_val[0] == '{' ) {
472 ptr = ber_bvchr( &bv, '}' );
474 return LDAP_INVALID_SYNTAX;
479 idx.bv_len = ptr - bv.bv_val;
481 bv.bv_len -= idx.bv_len;
488 rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx );
490 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
493 out->bv_len = idx.bv_len + bv.bv_len;
494 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
496 AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len );
497 AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
499 ber_memfree_x( bv.bv_val, ctx );
506 * wrapper for normalize function
507 * uses the normalize function of the attribute description equality rule
508 * after removing the index, if allowed and present; in case, it's
509 * prepended to the value
512 ordered_value_normalize(
514 AttributeDescription *ad,
517 struct berval *normalized,
520 struct berval bv = *val,
524 assert( ad->ad_type->sat_equality != NULL );
525 assert( ad->ad_type->sat_equality->smr_normalize != NULL );
526 assert( val != NULL );
527 assert( normalized != NULL );
529 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
531 /* Skip past the assertion index */
532 if ( bv.bv_val[ 0 ] == '{' ) {
535 ptr = ber_bvchr( &bv, '}' );
537 return LDAP_INVALID_SYNTAX;
542 idx.bv_len = ptr - bv.bv_val;
544 bv.bv_len -= idx.bv_len;
551 rc = ad->ad_type->sat_equality->smr_normalize( usage,
552 ad->ad_type->sat_syntax, mr, val, normalized, ctx );
554 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
557 normalized->bv_len = idx.bv_len + bv.bv_len;
558 normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx );
560 AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len );
561 AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
563 ber_memfree_x( bv.bv_val, ctx );
569 /* A wrapper for value match, handles Equality matches for attributes
570 * with ordered values.
575 AttributeDescription *ad,
578 struct berval *v1, /* stored value */
579 struct berval *v2, /* assertion */
582 struct berval bv1, bv2;
584 /* X-ORDERED VALUES equality matching:
585 * If (SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX) that means we are
586 * comparing two attribute values. In this case, we want to ignore
587 * the ordering index of both values, we just want to know if their
588 * main values are equal.
590 * If (SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX) then we are comparing
591 * an assertion against an attribute value.
592 * If the assertion has no index, the index of the value is ignored.
593 * If the assertion has only an index, the remainder of the value is
595 * If the assertion has index and value, both are compared.
597 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
605 /* Skip past the assertion index */
606 if ( bv2.bv_val[0] == '{' ) {
607 ptr = ber_bvchr( &bv2, '}' );
609 return LDAP_INVALID_SYNTAX;
612 bv2.bv_len -= ptr - bv2.bv_val;
617 if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX( flags )) {
618 if ( iv.bv_val[0] == '{' && bv1.bv_val[0] == '{' ) {
619 /* compare index values first */
622 l1 = strtol( bv1.bv_val+1, NULL, 0 );
623 l2 = strtol( iv.bv_val+1, &ptr, 0 );
627 /* If not equal, or we're only comparing the index,
630 if ( ret || ptr == iv.bv_val + iv.bv_len - 1 ) {
631 *match = ( ret < 0 ) ? -1 : (ret > 0 );
636 /* Skip past the attribute index */
637 if ( bv1.bv_val[0] == '{' ) {
638 ptr = ber_bvchr( &bv1, '}' );
640 return LDAP_INVALID_SYNTAX;
643 bv1.bv_len -= ptr - bv1.bv_val;
649 if ( !mr || !mr->smr_match ) {
650 *match = ber_bvcmp( v1, v2 );
654 return value_match( match, ad, mr, flags, v1, v2, text );
660 AttributeDescription *ad,
666 int i, j, k, anum, vnum;
667 BerVarray new, nnew = NULL;
670 for (i=0; !BER_BVISNULL( vals+i ); i++) ;
674 for (i=0; !BER_BVISNULL( a->a_vals+i ); i++) ;
676 ordered_value_sort( a, 0 );
680 for ( ap=&e->e_attrs; *ap; ap = &(*ap)->a_next ) ;
681 a = ch_calloc( 1, sizeof(Attribute) );
686 new = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
687 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
688 nnew = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
689 /* Shouldn't happen... */
690 if ( !nvals ) nvals = vals;
693 AC_MEMCPY( new, a->a_vals, anum * sizeof(struct berval));
695 AC_MEMCPY( nnew, a->a_nvals, anum * sizeof(struct berval));
698 for (i=0; i<vnum; i++) {
702 if ( vals[i].bv_val[0] == '{' ) {
703 k = strtol( vals[i].bv_val + 1, &next, 0 );
704 if ( next == vals[i].bv_val + 1 ||
706 next - vals[i].bv_val > vals[i].bv_len )
710 if ( k > anum ) k = -1;
712 /* No index, or index is greater than current number of
713 * values, just tack onto the end
716 ber_dupbv( new+anum, vals+i );
717 if ( nnew ) ber_dupbv( nnew+anum, nvals+i );
719 /* Indexed, push everything else down one and insert */
721 for (j=anum; j>k; j--) {
723 if ( nnew ) nnew[j] = nnew[j-1];
725 ber_dupbv( new+k, vals+i );
726 if ( nnew ) ber_dupbv( nnew+k, nvals+i );
730 BER_BVZERO( new+anum );
731 ch_free( a->a_vals );
734 BER_BVZERO( nnew+anum );
735 ch_free( a->a_nvals );
738 a->a_nvals = a->a_vals;
741 ordered_value_renumber( a, anum );