xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/value.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: value.c,v 1.1.1.4 2014/05/28 09:58:48 tron Exp $	*/
2 
3 /* value.c - routines for dealing with values */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2014 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /*
19  * Copyright (c) 1995 Regents of the University of Michigan.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms are permitted
23  * provided that this notice is preserved and that due credit is given
24  * to the University of Michigan at Ann Arbor. The name of the University
25  * may not be used to endorse or promote products derived from this
26  * software without specific prior written permission. This software
27  * is provided ``as is'' without express or implied warranty.
28  */
29 
30 #include "portable.h"
31 
32 #include <stdio.h>
33 
34 #include <ac/ctype.h>
35 #include <ac/socket.h>
36 #include <ac/string.h>
37 #include <ac/time.h>
38 
39 #include <sys/stat.h>
40 
41 #include "slap.h"
42 
43 int
44 value_add(
45     BerVarray	*vals,
46     BerVarray	addvals )
47 {
48 	int		n, nn = 0;
49 	BerVarray	v2;
50 
51 	if ( addvals != NULL ) {
52 		for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ )
53 			;	/* NULL */
54 	}
55 
56 	if ( *vals == NULL ) {
57 		*vals = (BerVarray) SLAP_MALLOC( (nn + 1)
58 		    * sizeof(struct berval) );
59 		if( *vals == NULL ) {
60 			Debug(LDAP_DEBUG_TRACE,
61 		      "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
62 			return LBER_ERROR_MEMORY;
63 		}
64 		n = 0;
65 
66 	} else {
67 		for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
68 			;	/* Empty */
69 		}
70 		*vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
71 		    (n + nn + 1) * sizeof(struct berval) );
72 		if( *vals == NULL ) {
73 			Debug(LDAP_DEBUG_TRACE,
74 		      "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 );
75 			return LBER_ERROR_MEMORY;
76 		}
77 	}
78 
79 	v2 = &(*vals)[n];
80 	for ( n = 0 ; n < nn; v2++, addvals++ ) {
81 		ber_dupbv( v2, addvals );
82 		if ( BER_BVISNULL( v2 ) ) break;
83 	}
84 	BER_BVZERO( v2 );
85 
86 	return LDAP_SUCCESS;
87 }
88 
89 int
90 value_add_one(
91     BerVarray		*vals,
92     struct berval	*addval )
93 {
94 	int		n;
95 	BerVarray	v2;
96 
97 	if ( *vals == NULL ) {
98 		*vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) );
99 		if( *vals == NULL ) {
100 			Debug(LDAP_DEBUG_TRACE,
101 		      "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
102 			return LBER_ERROR_MEMORY;
103 		}
104 		n = 0;
105 
106 	} else {
107 		for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) {
108 			;	/* Empty */
109 		}
110 		*vals = (BerVarray) SLAP_REALLOC( (char *) *vals,
111 		    (n + 2) * sizeof(struct berval) );
112 		if( *vals == NULL ) {
113 			Debug(LDAP_DEBUG_TRACE,
114 		      "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 );
115 			return LBER_ERROR_MEMORY;
116 		}
117 	}
118 
119 	v2 = &(*vals)[n];
120 	ber_dupbv(v2, addval);
121 
122 	v2++;
123 	BER_BVZERO( v2 );
124 
125 	return LDAP_SUCCESS;
126 }
127 
128 int asserted_value_validate_normalize(
129 	AttributeDescription *ad,
130 	MatchingRule *mr,
131 	unsigned usage,
132 	struct berval *in,
133 	struct berval *out,
134 	const char ** text,
135 	void *ctx )
136 {
137 	int rc;
138 	struct berval pval;
139 	pval.bv_val = NULL;
140 
141 	/* we expect the value to be in the assertion syntax */
142 	assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) );
143 
144 	if( mr == NULL ) {
145 		*text = "inappropriate matching request";
146 		return LDAP_INAPPROPRIATE_MATCHING;
147 	}
148 
149 	if( !mr->smr_match ) {
150 		*text = "requested matching rule not supported";
151 		return LDAP_INAPPROPRIATE_MATCHING;
152 	}
153 
154 	if( mr->smr_syntax->ssyn_pretty ) {
155 		rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx );
156 		in = &pval;
157 
158 	} else if ( mr->smr_syntax->ssyn_validate ) {
159 		rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
160 
161 	} else {
162 		*text = "inappropriate matching request";
163 		return LDAP_INAPPROPRIATE_MATCHING;
164 	}
165 
166 	if( rc != LDAP_SUCCESS ) {
167 		*text = "value does not conform to assertion syntax";
168 		return LDAP_INVALID_SYNTAX;
169 	}
170 
171 	if( mr->smr_normalize ) {
172 		rc = (mr->smr_normalize)(
173 			usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
174 			ad ? ad->ad_type->sat_syntax : NULL,
175 			mr, in, out, ctx );
176 
177 		if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );
178 
179 		if( rc != LDAP_SUCCESS ) {
180 			*text = "unable to normalize value for matching";
181 			return LDAP_INVALID_SYNTAX;
182 		}
183 
184 	} else if ( pval.bv_val != NULL ) {
185 		*out = pval;
186 
187 	} else {
188 		ber_dupbv_x( out, in, ctx );
189 	}
190 
191 	return LDAP_SUCCESS;
192 }
193 
194 int
195 value_match(
196 	int *match,
197 	AttributeDescription *ad,
198 	MatchingRule *mr,
199 	unsigned flags,
200 	struct berval *v1, /* stored value */
201 	void *v2, /* assertion */
202 	const char ** text )
203 {
204 	int rc;
205 
206 	assert( mr != NULL );
207 
208 	if( !mr->smr_match ) {
209 		return LDAP_INAPPROPRIATE_MATCHING;
210 	}
211 
212 	rc = (mr->smr_match)( match, flags,
213 		ad->ad_type->sat_syntax, mr, v1, v2 );
214 
215 	return rc;
216 }
217 
218 int value_find_ex(
219 	AttributeDescription *ad,
220 	unsigned flags,
221 	BerVarray vals,
222 	struct berval *val,
223 	void *ctx )
224 {
225 	int	i;
226 	int rc;
227 	struct berval nval = BER_BVNULL;
228 	MatchingRule *mr = ad->ad_type->sat_equality;
229 
230 	if( mr == NULL || !mr->smr_match ) {
231 		return LDAP_INAPPROPRIATE_MATCHING;
232 	}
233 
234 	assert( SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ) != 0 );
235 
236 	if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
237 		mr->smr_normalize )
238 	{
239 		rc = (mr->smr_normalize)(
240 			flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
241 			ad->ad_type->sat_syntax,
242 			mr, val, &nval, ctx );
243 
244 		if( rc != LDAP_SUCCESS ) {
245 			return LDAP_INVALID_SYNTAX;
246 		}
247 	}
248 
249 	for ( i = 0; vals[i].bv_val != NULL; i++ ) {
250 		int match;
251 		const char *text;
252 
253 		rc = value_match( &match, ad, mr, flags,
254 			&vals[i], nval.bv_val == NULL ? val : &nval, &text );
255 
256 		if( rc == LDAP_SUCCESS && match == 0 ) {
257 			slap_sl_free( nval.bv_val, ctx );
258 			return rc;
259 		}
260 	}
261 
262 	slap_sl_free( nval.bv_val, ctx );
263 	return LDAP_NO_SUCH_ATTRIBUTE;
264 }
265 
266 /* assign new indexes to an attribute's ordered values */
267 void
268 ordered_value_renumber( Attribute *a )
269 {
270 	char *ptr, ibuf[64];	/* many digits */
271 	struct berval ibv, tmp, vtmp;
272 	unsigned i;
273 
274 	ibv.bv_val = ibuf;
275 
276 	for (i=0; i<a->a_numvals; i++) {
277 		ibv.bv_len = sprintf(ibv.bv_val, "{%u}", i);
278 		vtmp = a->a_vals[i];
279 		if ( vtmp.bv_val[0] == '{' ) {
280 			ptr = ber_bvchr(&vtmp, '}');
281 			assert( ptr != NULL );
282 			++ptr;
283 			vtmp.bv_len -= ptr - vtmp.bv_val;
284 			vtmp.bv_val = ptr;
285 		}
286 		tmp.bv_len = ibv.bv_len + vtmp.bv_len;
287 		tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
288 		strcpy( tmp.bv_val, ibv.bv_val );
289 		AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
290 		tmp.bv_val[tmp.bv_len] = '\0';
291 		ch_free( a->a_vals[i].bv_val );
292 		a->a_vals[i] = tmp;
293 
294 		if ( a->a_nvals && a->a_nvals != a->a_vals ) {
295 			vtmp = a->a_nvals[i];
296 			if ( vtmp.bv_val[0] == '{' ) {
297 				ptr = ber_bvchr(&vtmp, '}');
298 				assert( ptr != NULL );
299 				++ptr;
300 				vtmp.bv_len -= ptr - vtmp.bv_val;
301 				vtmp.bv_val = ptr;
302 			}
303 			tmp.bv_len = ibv.bv_len + vtmp.bv_len;
304 			tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
305 			strcpy( tmp.bv_val, ibv.bv_val );
306 			AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
307 			tmp.bv_val[tmp.bv_len] = '\0';
308 			ch_free( a->a_nvals[i].bv_val );
309 			a->a_nvals[i] = tmp;
310 		}
311 	}
312 }
313 
314 /* Sort the values in an X-ORDERED VALUES attribute.
315  * If the values have no index, index them in their given order.
316  * If the values have indexes, sort them.
317  * If some are indexed and some are not, return Error.
318  */
319 int
320 ordered_value_sort( Attribute *a, int do_renumber )
321 {
322 	int i, vals;
323 	int index = 0, noindex = 0, renumber = 0, gotnvals = 0;
324 	struct berval tmp;
325 
326 	if ( a->a_nvals && a->a_nvals != a->a_vals )
327 		gotnvals = 1;
328 
329 	/* count attrs, look for index */
330 	for (i=0; a->a_vals[i].bv_val; i++) {
331 		if ( a->a_vals[i].bv_val[0] == '{' ) {
332 			char *ptr;
333 			index = 1;
334 			ptr = ber_bvchr( &a->a_vals[i], '}' );
335 			if ( !ptr )
336 				return LDAP_INVALID_SYNTAX;
337 			if ( noindex )
338 				return LDAP_INVALID_SYNTAX;
339 		} else {
340 			noindex = 1;
341 			if ( index )
342 				return LDAP_INVALID_SYNTAX;
343 		}
344 	}
345 	vals = i;
346 
347 	/* If values have indexes, sort the values */
348 	if ( index ) {
349 		int *indexes, j, idx;
350 		struct berval ntmp;
351 
352 #if 0
353 		/* Strip index from normalized values */
354 		if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
355 			a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
356 			BER_BVZERO(a->a_nvals+vals);
357 			for ( i=0; i<vals; i++ ) {
358 				char *ptr = ber_bvchr(&a->a_vals[i], '}') + 1;
359 				a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
360 					(ptr - a->a_vals[i].bv_val);
361 				a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
362 				strcpy(a->a_nvals[i].bv_val, ptr );
363 			}
364 		} else {
365 			for ( i=0; i<vals; i++ ) {
366 				char *ptr = ber_bvchr(&a->a_nvals[i], '}') + 1;
367 				a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
368 				strcpy(a->a_nvals[i].bv_val, ptr);
369 			}
370 		}
371 #endif
372 
373 		indexes = ch_malloc( vals * sizeof(int) );
374 		for ( i=0; i<vals; i++) {
375 			char *ptr;
376 			indexes[i] = strtol(a->a_vals[i].bv_val+1, &ptr, 0);
377 			if ( *ptr != '}' ) {
378 				ch_free( indexes );
379 				return LDAP_INVALID_SYNTAX;
380 			}
381 		}
382 
383 		/* Insertion sort */
384 		for ( i=1; i<vals; i++ ) {
385 			idx = indexes[i];
386 			tmp = a->a_vals[i];
387 			if ( gotnvals ) ntmp = a->a_nvals[i];
388 			j = i;
389 			while ((j > 0) && (indexes[j-1] > idx)) {
390 				indexes[j] = indexes[j-1];
391 				a->a_vals[j] = a->a_vals[j-1];
392 				if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
393 				j--;
394 			}
395 			indexes[j] = idx;
396 			a->a_vals[j] = tmp;
397 			if ( gotnvals ) a->a_nvals[j] = ntmp;
398 		}
399 
400 		/* If range is not contiguous, must renumber */
401 		if ( indexes[0] != 0 || indexes[vals-1] != vals-1 ) {
402 			renumber = 1;
403 		}
404 		ch_free( indexes );
405 	} else {
406 		renumber = 1;
407 	}
408 
409 	if ( do_renumber && renumber )
410 		ordered_value_renumber( a );
411 
412 	return 0;
413 }
414 
415 /*
416  * wrapper for validate function
417  * uses the validate function of the syntax after removing
418  * the index, if allowed and present
419  */
420 int
421 ordered_value_validate(
422 	AttributeDescription *ad,
423 	struct berval *in,
424 	int mop )
425 {
426 	struct berval	bv = *in;
427 
428 	assert( ad->ad_type->sat_syntax != NULL );
429 	assert( ad->ad_type->sat_syntax->ssyn_validate != NULL );
430 
431 	if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
432 
433 		/* Skip past the assertion index */
434 		if ( bv.bv_val[0] == '{' ) {
435 			char		*ptr;
436 
437 			ptr = ber_bvchr( &bv, '}' );
438 			if ( ptr != NULL ) {
439 				struct berval	ns;
440 
441 				ns.bv_val = bv.bv_val + 1;
442 				ns.bv_len = ptr - ns.bv_val;
443 
444 				if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) {
445 					ptr++;
446 					bv.bv_len -= ptr - bv.bv_val;
447 					bv.bv_val = ptr;
448 					in = &bv;
449 					/* If deleting by index, just succeed */
450 					if ( mop == LDAP_MOD_DELETE && BER_BVISEMPTY( &bv ) ) {
451 						return LDAP_SUCCESS;
452 					}
453 				}
454 			}
455 		}
456 	}
457 
458 	return ad->ad_type->sat_syntax->ssyn_validate( ad->ad_type->sat_syntax, in );
459 }
460 
461 /*
462  * wrapper for pretty function
463  * uses the pretty function of the syntax after removing
464  * the index, if allowed and present; in case, it's prepended
465  * to the pretty value
466  */
467 int
468 ordered_value_pretty(
469 	AttributeDescription *ad,
470 	struct berval *val,
471 	struct berval *out,
472 	void *ctx )
473 {
474 	struct berval	bv,
475 			idx = BER_BVNULL;
476 	int		rc;
477 
478 	assert( ad->ad_type->sat_syntax != NULL );
479 	assert( ad->ad_type->sat_syntax->ssyn_pretty != NULL );
480 	assert( val != NULL );
481 	assert( out != NULL );
482 
483 	bv = *val;
484 
485 	if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
486 
487 		/* Skip past the assertion index */
488 		if ( bv.bv_val[0] == '{' ) {
489 			char	*ptr;
490 
491 			ptr = ber_bvchr( &bv, '}' );
492 			if ( ptr != NULL ) {
493 				struct berval	ns;
494 
495 				ns.bv_val = bv.bv_val + 1;
496 				ns.bv_len = ptr - ns.bv_val;
497 
498 				if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) {
499 					ptr++;
500 
501 					idx = bv;
502 					idx.bv_len = ptr - bv.bv_val;
503 
504 					bv.bv_len -= idx.bv_len;
505 					bv.bv_val = ptr;
506 
507 					val = &bv;
508 				}
509 			}
510 		}
511 	}
512 
513 	rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx );
514 
515 	if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
516 		bv = *out;
517 
518 		out->bv_len = idx.bv_len + bv.bv_len;
519 		out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
520 
521 		AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len );
522 		AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
523 
524 		ber_memfree_x( bv.bv_val, ctx );
525 	}
526 
527 	return rc;
528 }
529 
530 /*
531  * wrapper for normalize function
532  * uses the normalize function of the attribute description equality rule
533  * after removing the index, if allowed and present; in case, it's
534  * prepended to the value
535  */
536 int
537 ordered_value_normalize(
538 	slap_mask_t usage,
539 	AttributeDescription *ad,
540 	MatchingRule *mr,
541 	struct berval *val,
542 	struct berval *normalized,
543 	void *ctx )
544 {
545 	struct berval	bv,
546 			idx = BER_BVNULL;
547 	int		rc;
548 
549 	assert( ad->ad_type->sat_equality != NULL );
550 	assert( ad->ad_type->sat_equality->smr_normalize != NULL );
551 	assert( val != NULL );
552 	assert( normalized != NULL );
553 
554 	bv = *val;
555 
556 	if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
557 
558 		/* Skip past the assertion index */
559 		if ( bv.bv_val[ 0 ] == '{' ) {
560 			char	*ptr;
561 
562 			ptr = ber_bvchr( &bv, '}' );
563 			if ( ptr != NULL ) {
564 				struct berval	ns;
565 
566 				ns.bv_val = bv.bv_val + 1;
567 				ns.bv_len = ptr - ns.bv_val;
568 
569 				if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) {
570 					ptr++;
571 
572 					idx = bv;
573 					idx.bv_len = ptr - bv.bv_val;
574 
575 					bv.bv_len -= idx.bv_len;
576 					bv.bv_val = ptr;
577 
578 					/* validator will already prevent this for Adds */
579 					if ( BER_BVISEMPTY( &bv )) {
580 						ber_dupbv_x( normalized, &idx, ctx );
581 						return LDAP_SUCCESS;
582 					}
583 					val = &bv;
584 				}
585 			}
586 		}
587 	}
588 
589 	rc = ad->ad_type->sat_equality->smr_normalize( usage,
590 		ad->ad_type->sat_syntax, mr, val, normalized, ctx );
591 
592 	if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
593 		bv = *normalized;
594 
595 		normalized->bv_len = idx.bv_len + bv.bv_len;
596 		normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx );
597 
598 		AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len );
599 		AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
600 
601 		ber_memfree_x( bv.bv_val, ctx );
602 	}
603 
604 	return rc;
605 }
606 
607 /* A wrapper for value match, handles Equality matches for attributes
608  * with ordered values.
609  */
610 int
611 ordered_value_match(
612 	int *match,
613 	AttributeDescription *ad,
614 	MatchingRule *mr,
615 	unsigned flags,
616 	struct berval *v1, /* stored value */
617 	struct berval *v2, /* assertion */
618 	const char ** text )
619 {
620 	struct berval bv1, bv2;
621 
622 	/* X-ORDERED VALUES equality matching:
623 	 * If (SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX) that means we are
624 	 * comparing two attribute values. In this case, we want to ignore
625 	 * the ordering index of both values, we just want to know if their
626 	 * main values are equal.
627 	 *
628 	 * If (SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX) then we are comparing
629 	 * an assertion against an attribute value.
630 	 *    If the assertion has no index, the index of the value is ignored.
631 	 *    If the assertion has only an index, the remainder of the value is
632 	 *      ignored.
633 	 *    If the assertion has index and value, both are compared.
634 	 */
635 	if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
636 		char *ptr;
637 		struct berval ns1 = BER_BVNULL, ns2 = BER_BVNULL;
638 
639 		bv1 = *v1;
640 		bv2 = *v2;
641 
642 		/* Skip past the assertion index */
643 		if ( bv2.bv_val[0] == '{' ) {
644 			ptr = ber_bvchr( &bv2, '}' );
645 			if ( ptr != NULL ) {
646 				ns2.bv_val = bv2.bv_val + 1;
647 				ns2.bv_len = ptr - ns2.bv_val;
648 
649 				if ( numericStringValidate( NULL, &ns2 ) == LDAP_SUCCESS ) {
650 					ptr++;
651 					bv2.bv_len -= ptr - bv2.bv_val;
652 					bv2.bv_val = ptr;
653 					v2 = &bv2;
654 				}
655 			}
656 		}
657 
658 		/* Skip past the attribute index */
659 		if ( bv1.bv_val[0] == '{' ) {
660 			ptr = ber_bvchr( &bv1, '}' );
661 			if ( ptr != NULL ) {
662 				ns1.bv_val = bv1.bv_val + 1;
663 				ns1.bv_len = ptr - ns1.bv_val;
664 
665 				if ( numericStringValidate( NULL, &ns1 ) == LDAP_SUCCESS ) {
666 					ptr++;
667 					bv1.bv_len -= ptr - bv1.bv_val;
668 					bv1.bv_val = ptr;
669 					v1 = &bv1;
670 				}
671 			}
672 		}
673 
674 		if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX( flags )) {
675 			if ( !BER_BVISNULL( &ns2 ) && !BER_BVISNULL( &ns1 ) ) {
676 				/* compare index values first */
677 				(void)octetStringOrderingMatch( match, 0, NULL, NULL, &ns1, &ns2 );
678 
679 				/* If not equal, or we're only comparing the index,
680 				 * return result now.
681 				 */
682 				if ( *match != 0 || BER_BVISEMPTY( &bv2 ) ) {
683 					return LDAP_SUCCESS;
684 				}
685 			}
686 		}
687 
688 	}
689 
690 	if ( !mr || !mr->smr_match ) {
691 		*match = ber_bvcmp( v1, v2 );
692 		return LDAP_SUCCESS;
693 	}
694 
695 	return value_match( match, ad, mr, flags, v1, v2, text );
696 }
697 
698 int
699 ordered_value_add(
700 	Entry *e,
701 	AttributeDescription *ad,
702 	Attribute *a,
703 	BerVarray vals,
704 	BerVarray nvals
705 )
706 {
707 	int i, j, k, anum, vnum;
708 	BerVarray new, nnew = NULL;
709 
710 	/* count new vals */
711 	for (i=0; !BER_BVISNULL( vals+i ); i++) ;
712 	vnum = i;
713 
714 	if ( a ) {
715 		ordered_value_sort( a, 0 );
716 	} else {
717 		Attribute **ap;
718 		for ( ap=&e->e_attrs; *ap; ap = &(*ap)->a_next ) ;
719 		a = attr_alloc( ad );
720 		*ap = a;
721 	}
722 	anum = a->a_numvals;
723 
724 	new = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
725 
726 	/* sanity check: if normalized modifications come in, either
727 	 * no values are present or normalized existing values differ
728 	 * from non-normalized; if no normalized modifications come in,
729 	 * either no values are present or normalized existing values
730 	 * don't differ from non-normalized */
731 	if ( nvals != NULL ) {
732 		assert( nvals != vals );
733 		assert( a->a_nvals == NULL || a->a_nvals != a->a_vals );
734 
735 	} else {
736 		assert( a->a_nvals == NULL || a->a_nvals == a->a_vals );
737 	}
738 
739 	if ( ( a->a_nvals && a->a_nvals != a->a_vals ) || nvals != NULL ) {
740 		nnew = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
741 		/* Shouldn't happen... */
742 		if ( !nvals ) nvals = vals;
743 	}
744 	if ( anum ) {
745 		AC_MEMCPY( new, a->a_vals, anum * sizeof(struct berval));
746 		if ( nnew && a->a_nvals )
747 			AC_MEMCPY( nnew, a->a_nvals, anum * sizeof(struct berval));
748 	}
749 
750 	for (i=0; i<vnum; i++) {
751 		char	*next;
752 
753 		k = -1;
754 		if ( vals[i].bv_val[0] == '{' ) {
755 			/* FIXME: strtol() could go past end... */
756 			k = strtol( vals[i].bv_val + 1, &next, 0 );
757 			if ( next == vals[i].bv_val + 1 ||
758 				next[ 0 ] != '}' ||
759 				(ber_len_t) (next - vals[i].bv_val) > vals[i].bv_len )
760 			{
761 				ch_free( nnew );
762 				ch_free( new );
763 				return -1;
764 			}
765 			if ( k > anum ) k = -1;
766 		}
767 		/* No index, or index is greater than current number of
768 		 * values, just tack onto the end
769 		 */
770 		if ( k < 0 ) {
771 			ber_dupbv( new+anum, vals+i );
772 			if ( nnew ) ber_dupbv( nnew+anum, nvals+i );
773 
774 		/* Indexed, push everything else down one and insert */
775 		} else {
776 			for (j=anum; j>k; j--) {
777 				new[j] = new[j-1];
778 				if ( nnew ) nnew[j] = nnew[j-1];
779 			}
780 			ber_dupbv( new+k, vals+i );
781 			if ( nnew ) ber_dupbv( nnew+k, nvals+i );
782 		}
783 		anum++;
784 	}
785 	BER_BVZERO( new+anum );
786 	ch_free( a->a_vals );
787 	a->a_vals = new;
788 	if ( nnew ) {
789 		BER_BVZERO( nnew+anum );
790 		ch_free( a->a_nvals );
791 		a->a_nvals = nnew;
792 	} else {
793 		a->a_nvals = a->a_vals;
794 	}
795 
796 	a->a_numvals = anum;
797 	ordered_value_renumber( a );
798 
799 	return 0;
800 }
801