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