1/* $NetBSD: value.c,v 1.1.1.3 2010/12/12 15:22:52 adam Exp $ */ 2 3/* value.c - routines for dealing with values */ 4/* OpenLDAP: pkg/ldap/servers/slapd/value.c,v 1.96.2.9 2010/04/13 20:23:22 kurt Exp */ 5/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2010 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 43int 44value_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 89int 90value_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 128int 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 194int 195value_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 218int 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 */ 267void 268ordered_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 */ 319int 320ordered_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 */ 420int 421ordered_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 */ 467int 468ordered_value_pretty( 469 AttributeDescription *ad, 470 struct berval *val, 471 struct berval *out, 472 void *ctx ) 473{ 474 struct berval bv = *val, 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 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) { 484 485 /* Skip past the assertion index */ 486 if ( bv.bv_val[0] == '{' ) { 487 char *ptr; 488 489 ptr = ber_bvchr( &bv, '}' ); 490 if ( ptr != NULL ) { 491 struct berval ns; 492 493 ns.bv_val = bv.bv_val + 1; 494 ns.bv_len = ptr - ns.bv_val; 495 496 if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) { 497 ptr++; 498 499 idx = bv; 500 idx.bv_len = ptr - bv.bv_val; 501 502 bv.bv_len -= idx.bv_len; 503 bv.bv_val = ptr; 504 505 val = &bv; 506 } 507 } 508 } 509 } 510 511 rc = ad->ad_type->sat_syntax->ssyn_pretty( ad->ad_type->sat_syntax, val, out, ctx ); 512 513 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) { 514 bv = *out; 515 516 out->bv_len = idx.bv_len + bv.bv_len; 517 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx ); 518 519 AC_MEMCPY( out->bv_val, idx.bv_val, idx.bv_len ); 520 AC_MEMCPY( &out->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 ); 521 522 ber_memfree_x( bv.bv_val, ctx ); 523 } 524 525 return rc; 526} 527 528/* 529 * wrapper for normalize function 530 * uses the normalize function of the attribute description equality rule 531 * after removing the index, if allowed and present; in case, it's 532 * prepended to the value 533 */ 534int 535ordered_value_normalize( 536 slap_mask_t usage, 537 AttributeDescription *ad, 538 MatchingRule *mr, 539 struct berval *val, 540 struct berval *normalized, 541 void *ctx ) 542{ 543 struct berval bv = *val, 544 idx = BER_BVNULL; 545 int rc; 546 547 assert( ad->ad_type->sat_equality != NULL ); 548 assert( ad->ad_type->sat_equality->smr_normalize != NULL ); 549 assert( val != NULL ); 550 assert( normalized != NULL ); 551 552 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) { 553 554 /* Skip past the assertion index */ 555 if ( bv.bv_val[ 0 ] == '{' ) { 556 char *ptr; 557 558 ptr = ber_bvchr( &bv, '}' ); 559 if ( ptr != NULL ) { 560 struct berval ns; 561 562 ns.bv_val = bv.bv_val + 1; 563 ns.bv_len = ptr - ns.bv_val; 564 565 if ( numericStringValidate( NULL, &ns ) == LDAP_SUCCESS ) { 566 ptr++; 567 568 idx = bv; 569 idx.bv_len = ptr - bv.bv_val; 570 571 bv.bv_len -= idx.bv_len; 572 bv.bv_val = ptr; 573 574 /* validator will already prevent this for Adds */ 575 if ( BER_BVISEMPTY( &bv )) { 576 ber_dupbv_x( normalized, &idx, ctx ); 577 return LDAP_SUCCESS; 578 } 579 val = &bv; 580 } 581 } 582 } 583 } 584 585 rc = ad->ad_type->sat_equality->smr_normalize( usage, 586 ad->ad_type->sat_syntax, mr, val, normalized, ctx ); 587 588 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &idx ) ) { 589 bv = *normalized; 590 591 normalized->bv_len = idx.bv_len + bv.bv_len; 592 normalized->bv_val = ber_memalloc_x( normalized->bv_len + 1, ctx ); 593 594 AC_MEMCPY( normalized->bv_val, idx.bv_val, idx.bv_len ); 595 AC_MEMCPY( &normalized->bv_val[ idx.bv_len ], bv.bv_val, bv.bv_len + 1 ); 596 597 ber_memfree_x( bv.bv_val, ctx ); 598 } 599 600 return rc; 601} 602 603/* A wrapper for value match, handles Equality matches for attributes 604 * with ordered values. 605 */ 606int 607ordered_value_match( 608 int *match, 609 AttributeDescription *ad, 610 MatchingRule *mr, 611 unsigned flags, 612 struct berval *v1, /* stored value */ 613 struct berval *v2, /* assertion */ 614 const char ** text ) 615{ 616 struct berval bv1, bv2; 617 618 /* X-ORDERED VALUES equality matching: 619 * If (SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX) that means we are 620 * comparing two attribute values. In this case, we want to ignore 621 * the ordering index of both values, we just want to know if their 622 * main values are equal. 623 * 624 * If (SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX) then we are comparing 625 * an assertion against an attribute value. 626 * If the assertion has no index, the index of the value is ignored. 627 * If the assertion has only an index, the remainder of the value is 628 * ignored. 629 * If the assertion has index and value, both are compared. 630 */ 631 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED ) { 632 char *ptr; 633 struct berval ns1 = BER_BVNULL, ns2 = BER_BVNULL; 634 635 bv1 = *v1; 636 bv2 = *v2; 637 638 /* Skip past the assertion index */ 639 if ( bv2.bv_val[0] == '{' ) { 640 ptr = ber_bvchr( &bv2, '}' ); 641 if ( ptr != NULL ) { 642 ns2.bv_val = bv2.bv_val + 1; 643 ns2.bv_len = ptr - ns2.bv_val; 644 645 if ( numericStringValidate( NULL, &ns2 ) == LDAP_SUCCESS ) { 646 ptr++; 647 bv2.bv_len -= ptr - bv2.bv_val; 648 bv2.bv_val = ptr; 649 v2 = &bv2; 650 } 651 } 652 } 653 654 /* Skip past the attribute index */ 655 if ( bv1.bv_val[0] == '{' ) { 656 ptr = ber_bvchr( &bv1, '}' ); 657 if ( ptr != NULL ) { 658 ns1.bv_val = bv1.bv_val + 1; 659 ns1.bv_len = ptr - ns1.bv_val; 660 661 if ( numericStringValidate( NULL, &ns1 ) == LDAP_SUCCESS ) { 662 ptr++; 663 bv1.bv_len -= ptr - bv1.bv_val; 664 bv1.bv_val = ptr; 665 v1 = &bv1; 666 } 667 } 668 } 669 670 if ( SLAP_MR_IS_VALUE_OF_ASSERTION_SYNTAX( flags )) { 671 if ( !BER_BVISNULL( &ns2 ) && !BER_BVISNULL( &ns1 ) ) { 672 /* compare index values first */ 673 (void)octetStringOrderingMatch( match, 0, NULL, NULL, &ns1, &ns2 ); 674 675 /* If not equal, or we're only comparing the index, 676 * return result now. 677 */ 678 if ( *match != 0 || BER_BVISEMPTY( &bv2 ) ) { 679 return LDAP_SUCCESS; 680 } 681 } 682 } 683 684 } 685 686 if ( !mr || !mr->smr_match ) { 687 *match = ber_bvcmp( v1, v2 ); 688 return LDAP_SUCCESS; 689 } 690 691 return value_match( match, ad, mr, flags, v1, v2, text ); 692} 693 694int 695ordered_value_add( 696 Entry *e, 697 AttributeDescription *ad, 698 Attribute *a, 699 BerVarray vals, 700 BerVarray nvals 701) 702{ 703 int i, j, k, anum, vnum; 704 BerVarray new, nnew = NULL; 705 706 /* count new vals */ 707 for (i=0; !BER_BVISNULL( vals+i ); i++) ; 708 vnum = i; 709 710 if ( a ) { 711 ordered_value_sort( a, 0 ); 712 } else { 713 Attribute **ap; 714 for ( ap=&e->e_attrs; *ap; ap = &(*ap)->a_next ) ; 715 a = attr_alloc( ad ); 716 *ap = a; 717 } 718 anum = a->a_numvals; 719 720 new = ch_malloc( (anum+vnum+1) * sizeof(struct berval)); 721 722 /* sanity check: if normalized modifications come in, either 723 * no values are present or normalized existing values differ 724 * from non-normalized; if no normalized modifications come in, 725 * either no values are present or normalized existing values 726 * don't differ from non-normalized */ 727 if ( nvals != NULL ) { 728 assert( nvals != vals ); 729 assert( a->a_nvals == NULL || a->a_nvals != a->a_vals ); 730 731 } else { 732 assert( a->a_nvals == NULL || a->a_nvals == a->a_vals ); 733 } 734 735 if ( ( a->a_nvals && a->a_nvals != a->a_vals ) || nvals != NULL ) { 736 nnew = ch_malloc( (anum+vnum+1) * sizeof(struct berval)); 737 /* Shouldn't happen... */ 738 if ( !nvals ) nvals = vals; 739 } 740 if ( anum ) { 741 AC_MEMCPY( new, a->a_vals, anum * sizeof(struct berval)); 742 if ( nnew && a->a_nvals ) 743 AC_MEMCPY( nnew, a->a_nvals, anum * sizeof(struct berval)); 744 } 745 746 for (i=0; i<vnum; i++) { 747 char *next; 748 749 k = -1; 750 if ( vals[i].bv_val[0] == '{' ) { 751 /* FIXME: strtol() could go past end... */ 752 k = strtol( vals[i].bv_val + 1, &next, 0 ); 753 if ( next == vals[i].bv_val + 1 || 754 next[ 0 ] != '}' || 755 (ber_len_t) (next - vals[i].bv_val) > vals[i].bv_len ) 756 { 757 ch_free( nnew ); 758 ch_free( new ); 759 return -1; 760 } 761 if ( k > anum ) k = -1; 762 } 763 /* No index, or index is greater than current number of 764 * values, just tack onto the end 765 */ 766 if ( k < 0 ) { 767 ber_dupbv( new+anum, vals+i ); 768 if ( nnew ) ber_dupbv( nnew+anum, nvals+i ); 769 770 /* Indexed, push everything else down one and insert */ 771 } else { 772 for (j=anum; j>k; j--) { 773 new[j] = new[j-1]; 774 if ( nnew ) nnew[j] = nnew[j-1]; 775 } 776 ber_dupbv( new+k, vals+i ); 777 if ( nnew ) ber_dupbv( nnew+k, nvals+i ); 778 } 779 anum++; 780 } 781 BER_BVZERO( new+anum ); 782 ch_free( a->a_vals ); 783 a->a_vals = new; 784 if ( nnew ) { 785 BER_BVZERO( nnew+anum ); 786 ch_free( a->a_nvals ); 787 a->a_nvals = nnew; 788 } else { 789 a->a_nvals = a->a_vals; 790 } 791 792 a->a_numvals = anum; 793 ordered_value_renumber( a ); 794 795 return 0; 796} 797