]> git.sur5r.net Git - openldap/blob - servers/slapd/value.c
edeaff12e812694f9b5181beaffbf46901f3663d
[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-2007 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) ch_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) ch_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 if ( mr->smr_syntax->ssyn_validate ) {
157                 rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in );
158
159         } else {
160                 *text = "inappropriate matching request";
161                 return LDAP_INAPPROPRIATE_MATCHING;
162         }
163
164         if( rc != LDAP_SUCCESS ) {
165                 *text = "value does not conform to assertion syntax";
166                 return LDAP_INVALID_SYNTAX;
167         }
168
169         if( mr->smr_normalize ) {
170                 rc = (mr->smr_normalize)(
171                         usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
172                         ad ? ad->ad_type->sat_syntax : NULL,
173                         mr, in, out, ctx );
174
175                 if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx );
176
177                 if( rc != LDAP_SUCCESS ) {
178                         *text = "unable to normalize value for matching";
179                         return LDAP_INVALID_SYNTAX;
180                 }
181
182         } else if ( pval.bv_val != NULL ) {
183                 *out = pval;
184
185         } else {
186                 ber_dupbv_x( out, in, ctx );
187         }
188
189         return LDAP_SUCCESS;
190 }
191
192 int
193 value_match(
194         int *match,
195         AttributeDescription *ad,
196         MatchingRule *mr,
197         unsigned flags,
198         struct berval *v1, /* stored value */
199         void *v2, /* assertion */
200         const char ** text )
201 {
202         int rc;
203
204         assert( mr != NULL );
205
206         if( !mr->smr_match ) {
207                 return LDAP_INAPPROPRIATE_MATCHING;
208         }
209
210         rc = (mr->smr_match)( match, flags,
211                 ad->ad_type->sat_syntax, mr, v1, v2 );
212         
213         return rc;
214 }
215
216 int value_find_ex(
217         AttributeDescription *ad,
218         unsigned flags,
219         BerVarray vals,
220         struct berval *val,
221         void *ctx )
222 {
223         int     i;
224         int rc;
225         struct berval nval = BER_BVNULL;
226         MatchingRule *mr = ad->ad_type->sat_equality;
227
228         if( mr == NULL || !mr->smr_match ) {
229                 return LDAP_INAPPROPRIATE_MATCHING;
230         }
231
232         assert(SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ));
233
234         if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
235                 mr->smr_normalize )
236         {
237                 rc = (mr->smr_normalize)(
238                         flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
239                         ad ? ad->ad_type->sat_syntax : NULL,
240                         mr, val, &nval, ctx );
241
242                 if( rc != LDAP_SUCCESS ) {
243                         return LDAP_INVALID_SYNTAX;
244                 }
245         }
246
247         for ( i = 0; vals[i].bv_val != NULL; i++ ) {
248                 int match;
249                 const char *text;
250
251                 rc = value_match( &match, ad, mr, flags,
252                         &vals[i], nval.bv_val == NULL ? val : &nval, &text );
253
254                 if( rc == LDAP_SUCCESS && match == 0 ) {
255                         slap_sl_free( nval.bv_val, ctx );
256                         return rc;
257                 }
258         }
259
260         slap_sl_free( nval.bv_val, ctx );
261         return LDAP_NO_SUCH_ATTRIBUTE;
262 }
263
264 /* assign new indexes to an attribute's ordered values */
265 void
266 ordered_value_renumber( Attribute *a, int vals )
267 {
268         char *ptr, ibuf[64];    /* many digits */
269         struct berval ibv, tmp, vtmp;
270         int i;
271
272         ibv.bv_val = ibuf;
273
274         for (i=0; i<vals; i++) {
275                 ibv.bv_len = sprintf(ibv.bv_val, "{%d}", i);
276                 vtmp = a->a_vals[i];
277                 if ( vtmp.bv_val[0] == '{' ) {
278                         ptr = ber_bvchr(&vtmp, '}');
279                         assert( ptr != NULL );
280                         ++ptr;
281                         vtmp.bv_len -= ptr - vtmp.bv_val;
282                         vtmp.bv_val = ptr;
283                 }
284                 tmp.bv_len = ibv.bv_len + vtmp.bv_len;
285                 tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
286                 strcpy( tmp.bv_val, ibv.bv_val );
287                 AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
288                 tmp.bv_val[tmp.bv_len] = '\0';
289                 ch_free( a->a_vals[i].bv_val );
290                 a->a_vals[i] = tmp;
291
292                 if ( a->a_nvals && a->a_nvals != a->a_vals ) {
293                         vtmp = a->a_nvals[i];
294                         if ( vtmp.bv_val[0] == '{' ) {
295                                 ptr = ber_bvchr(&vtmp, '}');
296                                 assert( ptr != NULL );
297                                 ++ptr;
298                                 vtmp.bv_len -= ptr - vtmp.bv_val;
299                                 vtmp.bv_val = ptr;
300                         }
301                         tmp.bv_len = ibv.bv_len + vtmp.bv_len;
302                         tmp.bv_val = ch_malloc( tmp.bv_len + 1 );
303                         strcpy( tmp.bv_val, ibv.bv_val );
304                         AC_MEMCPY( tmp.bv_val + ibv.bv_len, vtmp.bv_val, vtmp.bv_len );
305                         tmp.bv_val[tmp.bv_len] = '\0';
306                         ch_free( a->a_nvals[i].bv_val );
307                         a->a_nvals[i] = tmp;
308                 }
309         }
310 }
311
312 /* Sort the values in an X-ORDERED VALUES attribute.
313  * If the values have no index, index them in their given order.
314  * If the values have indexes, sort them.
315  * If some are indexed and some are not, return Error.
316  */
317 int
318 ordered_value_sort( Attribute *a, int do_renumber )
319 {
320         int i, vals;
321         int index = 0, noindex = 0, renumber = 0, gotnvals = 0;
322         struct berval tmp;
323
324         if ( a->a_nvals && a->a_nvals != a->a_vals )
325                 gotnvals = 1;
326
327         /* count attrs, look for index */
328         for (i=0; a->a_vals[i].bv_val; i++) {
329                 if ( a->a_vals[i].bv_val[0] == '{' ) {
330                         char *ptr;
331                         index = 1;
332                         ptr = ber_bvchr( &a->a_vals[i], '}' );
333                         if ( !ptr )
334                                 return LDAP_INVALID_SYNTAX;
335                         if ( noindex )
336                                 return LDAP_INVALID_SYNTAX;
337                 } else {
338                         noindex = 1;
339                         if ( index )
340                                 return LDAP_INVALID_SYNTAX;
341                 }
342         }
343         vals = i;
344
345         /* If values have indexes, sort the values */
346         if ( index ) {
347                 int *indexes, j, idx;
348                 struct berval ntmp;
349
350 #if 0
351                 /* Strip index from normalized values */
352                 if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
353                         a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
354                         BER_BVZERO(a->a_nvals+vals);
355                         for ( i=0; i<vals; i++ ) {
356                                 char *ptr = ber_bvchr(&a->a_vals[i], '}') + 1;
357                                 a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
358                                         (ptr - a->a_vals[i].bv_val);
359                                 a->a_nvals[i].bv_val = ch_malloc( a->a_nvals[i].bv_len + 1);
360                                 strcpy(a->a_nvals[i].bv_val, ptr );
361                         }
362                 } else {
363                         for ( i=0; i<vals; i++ ) {
364                                 char *ptr = ber_bvchr(&a->a_nvals[i], '}') + 1;
365                                 a->a_nvals[i].bv_len -= ptr - a->a_nvals[i].bv_val;
366                                 strcpy(a->a_nvals[i].bv_val, ptr);
367                         }
368                 }
369 #endif
370                                 
371                 indexes = ch_malloc( vals * sizeof(int) );
372                 for ( i=0; i<vals; i++) {
373                         char *ptr;
374                         indexes[i] = strtol(a->a_vals[i].bv_val+1, &ptr, 0);
375                         if ( *ptr != '}' ) {
376                                 ch_free( indexes );
377                                 return LDAP_INVALID_SYNTAX;
378                         }
379                 }
380
381                 /* Insertion sort */
382                 for ( i=1; i<vals; i++ ) {
383                         idx = indexes[i];
384                         tmp = a->a_vals[i];
385                         if ( gotnvals ) ntmp = a->a_nvals[i];
386                         j = i;
387                         while ((j > 0) && (indexes[j-1] > idx)) {
388                                 indexes[j] = indexes[j-1];
389                                 a->a_vals[j] = a->a_vals[j-1];
390                                 if ( gotnvals ) a->a_nvals[j] = a->a_nvals[j-1];
391                                 j--;
392                         }
393                         indexes[j] = idx;
394                         a->a_vals[j] = tmp;
395                         if ( gotnvals ) a->a_nvals[j] = ntmp;
396                 }
397
398                 /* If range is not contiguous, must renumber */
399                 if ( indexes[0] != 0 || indexes[vals-1] != vals-1 ) {
400                         renumber = 1;
401                 }
402                 ch_free( indexes );
403         } else {
404                 renumber = 1;
405         }
406
407         if ( do_renumber && renumber )
408                 ordered_value_renumber( a, vals );
409
410         return 0;
411 }
412
413 /*
414  * wrapper for validate function
415  * uses the validate function of the syntax after removing
416  * the index, if allowed and present
417  */
418 int
419 ordered_value_validate(
420         AttributeDescription *ad,
421         struct berval *in,
422         int mop )
423 {
424         struct berval   bv = *in;
425
426         assert( ad->ad_type->sat_syntax != NULL );
427         assert( ad->ad_type->sat_syntax->ssyn_validate != NULL );
428
429         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
430
431                 /* Skip past the assertion index */
432                 if ( bv.bv_val[0] == '{' ) {
433                         char    *ptr;
434
435                         ptr = ber_bvchr( &bv, '}' );
436                         if ( ptr == NULL ) {
437                                 return LDAP_INVALID_SYNTAX;
438                         }
439                         ptr++;
440                         bv.bv_len -= ptr - bv.bv_val;
441                         bv.bv_val = ptr;
442                         in = &bv;
443                         /* If deleting by index, just succeed */
444                         if ( mop == LDAP_MOD_DELETE && BER_BVISEMPTY( &bv ))
445                                 return LDAP_SUCCESS;
446                 }
447         }
448
449         return ad->ad_type->sat_syntax->ssyn_validate( ad->ad_type->sat_syntax, in );
450 }
451
452 /*
453  * wrapper for pretty function
454  * uses the pretty function of the syntax after removing
455  * the index, if allowed and present; in case, it's prepended
456  * to the pretty value
457  */
458 int
459 ordered_value_pretty(
460         AttributeDescription *ad,
461         struct berval *val,
462         struct berval *out,
463         void *ctx )
464 {
465         struct berval   bv = *val,
466                         idx = BER_BVNULL;
467         int             rc;
468
469         assert( ad->ad_type->sat_syntax != NULL );
470         assert( ad->ad_type->sat_syntax->ssyn_pretty != NULL );
471         assert( val != NULL );
472         assert( out != NULL );
473
474         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
475
476                 /* Skip past the assertion index */
477                 if ( bv.bv_val[0] == '{' ) {
478                         char    *ptr;
479
480                         ptr = ber_bvchr( &bv, '}' );
481                         if ( ptr == NULL ) {
482                                 return LDAP_INVALID_SYNTAX;
483                         }
484                         ptr++;
485
486                         idx = bv;
487                         idx.bv_len = ptr - bv.bv_val;
488
489                         bv.bv_len -= idx.bv_len;
490                         bv.bv_val = ptr;
491
492                         val = &bv;
493                 }
494         }
495
496         rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx );
497
498         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
499                 bv = *out;
500
501                 out->bv_len = idx.bv_len + bv.bv_len;
502                 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
503                 
504                 AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len );
505                 AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
506
507                 ber_memfree_x( bv.bv_val, ctx );
508         }
509
510         return rc;
511 }
512
513 /*
514  * wrapper for normalize function
515  * uses the normalize function of the attribute description equality rule
516  * after removing the index, if allowed and present; in case, it's
517  * prepended to the value
518  */
519 int
520 ordered_value_normalize(
521         slap_mask_t usage,
522         AttributeDescription *ad,
523         MatchingRule *mr,
524         struct berval *val,
525         struct berval *normalized,
526         void *ctx )
527 {
528         struct berval   bv = *val,
529                         idx = BER_BVNULL;
530         int             rc;
531
532         assert( ad->ad_type->sat_equality != NULL );
533         assert( ad->ad_type->sat_equality->smr_normalize != NULL );
534         assert( val != NULL );
535         assert( normalized != NULL );
536
537         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
538
539                 /* Skip past the assertion index */
540                 if ( bv.bv_val[ 0 ] == '{' ) {
541                         char    *ptr;
542
543                         ptr = ber_bvchr( &bv, '}' );
544                         if ( ptr == NULL ) {
545                                 return LDAP_INVALID_SYNTAX;
546                         }
547                         ptr++;
548
549                         idx = bv;
550                         idx.bv_len = ptr - bv.bv_val;
551
552                         bv.bv_len -= idx.bv_len;
553                         bv.bv_val = ptr;
554
555                         /* validator will already prevent this for Adds */
556                         if ( BER_BVISEMPTY( &bv )) {
557                                 ber_dupbv_x( normalized, &idx, ctx );
558                                 return LDAP_SUCCESS;
559                         }
560                         val = &bv;
561                 }
562         }
563
564         rc = ad->ad_type->sat_equality->smr_normalize( usage,
565                 ad->ad_type->sat_syntax, mr, val, normalized, ctx );
566
567         if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) {
568                 bv = *normalized;
569
570                 normalized->bv_len = idx.bv_len + bv.bv_len;
571                 normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx );
572                 
573                 AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len );
574                 AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 );
575
576                 ber_memfree_x( bv.bv_val, ctx );
577         }
578
579         return rc;
580 }
581
582 /* A wrapper for value match, handles Equality matches for attributes
583  * with ordered values.
584  */
585 int
586 ordered_value_match(
587         int *match,
588         AttributeDescription *ad,
589         MatchingRule *mr,
590         unsigned flags,
591         struct berval *v1, /* stored value */
592         struct berval *v2, /* assertion */
593         const char ** text )
594 {
595         struct berval bv1, bv2;
596
597         /* X-ORDERED VALUES equality matching:
598          * If (SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX) that means we are
599          * comparing two attribute values. In this case, we want to ignore
600          * the ordering index of both values, we just want to know if their
601          * main values are equal.
602          *
603          * If (SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX) then we are comparing
604          * an assertion against an attribute value.
605          *    If the assertion has no index, the index of the value is ignored. 
606          *    If the assertion has only an index, the remainder of the value is
607          *      ignored.
608          *    If the assertion has index and value, both are compared.
609          */
610         if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) {
611                 char *ptr;
612                 struct berval iv;
613
614                 bv1 = *v1;
615                 bv2 = *v2;
616                 iv = bv2;
617
618                 /* Skip past the assertion index */
619                 if ( bv2.bv_val[0] == '{' ) {
620                         ptr = ber_bvchr( &bv2, '}' );
621                         if ( ptr == NULL ) {
622                                 return LDAP_INVALID_SYNTAX;
623                         }
624                         ptr++;
625                         bv2.bv_len -= ptr - bv2.bv_val;
626                         bv2.bv_val = ptr;
627                         v2 = &bv2;
628                 }
629
630                 if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX( flags )) {
631                         if ( iv.bv_val[0] == '{' && bv1.bv_val[0] == '{' ) {
632                         /* compare index values first */
633                                 long l1, l2, ret;
634
635                                 l1 = strtol( bv1.bv_val+1, NULL, 0 );
636                                 l2 = strtol( iv.bv_val+1, &ptr, 0 );
637
638                                 ret = l1 - l2;
639
640                                 /* If not equal, or we're only comparing the index,
641                                  * return result now.
642                                  */
643                                 if ( ret || ptr == iv.bv_val + iv.bv_len - 1 ) {
644                                         *match = ( ret < 0 ) ? -1 : (ret > 0 );
645                                         return LDAP_SUCCESS;
646                                 }
647                         }
648                 }
649                 /* Skip past the attribute index */
650                 if ( bv1.bv_val[0] == '{' ) {
651                         ptr = ber_bvchr( &bv1, '}' );
652                         if ( ptr == NULL ) {
653                                 return LDAP_INVALID_SYNTAX;
654                         }
655                         ptr++;
656                         bv1.bv_len -= ptr - bv1.bv_val;
657                         bv1.bv_val = ptr;
658                         v1 = &bv1;
659                 }
660         }
661
662         if ( !mr || !mr->smr_match ) {
663                 *match = ber_bvcmp( v1, v2 );
664                 return LDAP_SUCCESS;
665         }
666
667         return value_match( match, ad, mr, flags, v1, v2, text );
668 }
669
670 int
671 ordered_value_add(
672         Entry *e,
673         AttributeDescription *ad,
674         Attribute *a,
675         BerVarray vals,
676         BerVarray nvals
677 )
678 {
679         int i, j, k, anum, vnum;
680         BerVarray new, nnew = NULL;
681
682         /* count new vals */
683         for (i=0; !BER_BVISNULL( vals+i ); i++) ;
684         vnum = i;
685
686         if ( a ) {
687                 for (i=0; !BER_BVISNULL( a->a_vals+i ); i++) ;
688                 anum = i;
689                 ordered_value_sort( a, 0 );
690         } else {
691                 Attribute **ap;
692                 anum = 0;
693                 for ( ap=&e->e_attrs; *ap; ap = &(*ap)->a_next ) ;
694                 a = ch_calloc( 1, sizeof(Attribute) );
695                 a->a_desc = ad;
696                 *ap = a;
697         }
698
699         new = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
700
701         /* sanity check: if normalized modifications come in, either
702          * no values are present or normalized existing values differ
703          * from non-normalized; if no normalized modifications come in,
704          * either no values are present or normalized existing values
705          * don't differ from non-normalized */
706         if ( nvals != NULL ) {
707                 assert( nvals != vals );
708                 assert( a->a_nvals == NULL || a->a_nvals != a->a_vals );
709
710         } else {
711                 assert( a->a_nvals == NULL || a->a_nvals == a->a_vals );
712         }
713
714         if ( ( a->a_nvals && a->a_nvals != a->a_vals ) || nvals != NULL ) {
715                 nnew = ch_malloc( (anum+vnum+1) * sizeof(struct berval));
716                 /* Shouldn't happen... */
717                 if ( !nvals ) nvals = vals;
718         }
719         if ( anum ) {
720                 AC_MEMCPY( new, a->a_vals, anum * sizeof(struct berval));
721                 if ( nnew )
722                         AC_MEMCPY( nnew, a->a_nvals, anum * sizeof(struct berval));
723         }
724
725         for (i=0; i<vnum; i++) {
726                 char    *next;
727
728                 k = -1;
729                 if ( vals[i].bv_val[0] == '{' ) {
730                         k = strtol( vals[i].bv_val + 1, &next, 0 );
731                         if ( next == vals[i].bv_val + 1 ||
732                                 next[ 0 ] != '}' ||
733                                 next - vals[i].bv_val > vals[i].bv_len )
734                         {
735                                 return -1;
736                         }
737                         if ( k > anum ) k = -1;
738                 }
739                 /* No index, or index is greater than current number of
740                  * values, just tack onto the end
741                  */
742                 if ( k < 0 ) {
743                         ber_dupbv( new+anum, vals+i );
744                         if ( nnew ) ber_dupbv( nnew+anum, nvals+i );
745
746                 /* Indexed, push everything else down one and insert */
747                 } else {
748                         for (j=anum; j>k; j--) {
749                                 new[j] = new[j-1];
750                                 if ( nnew ) nnew[j] = nnew[j-1];
751                         }
752                         ber_dupbv( new+k, vals+i );
753                         if ( nnew ) ber_dupbv( nnew+k, nvals+i );
754                 }
755                 anum++;
756         }
757         BER_BVZERO( new+anum );
758         ch_free( a->a_vals );
759         a->a_vals = new;
760         if ( nnew ) {
761                 BER_BVZERO( nnew+anum );
762                 ch_free( a->a_nvals );
763                 a->a_nvals = nnew;
764         } else {
765                 a->a_nvals = a->a_vals;
766         }
767
768         ordered_value_renumber( a, anum );
769
770         return 0;
771 }