1187767Sluigi/* $NetBSD$ */ 2187767Sluigi 3187767Sluigi/* value.c - routines for dealing with values */ 4187767Sluigi/* OpenLDAP: pkg/ldap/servers/slapd/value.c,v 1.96.2.9 2010/04/13 20:23:22 kurt Exp */ 5187767Sluigi/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6187767Sluigi * 7187767Sluigi * Copyright 1998-2010 The OpenLDAP Foundation. 8187767Sluigi * All rights reserved. 9187767Sluigi * 10187767Sluigi * Redistribution and use in source and binary forms, with or without 11187767Sluigi * modification, are permitted only as authorized by the OpenLDAP 12187767Sluigi * Public License. 13187767Sluigi * 14187767Sluigi * A copy of this license is available in the file LICENSE in the 15187767Sluigi * top-level directory of the distribution or, alternatively, at 16187767Sluigi * <http://www.OpenLDAP.org/license.html>. 17187767Sluigi */ 18187767Sluigi/* 19187767Sluigi * Copyright (c) 1995 Regents of the University of Michigan. 20187767Sluigi * All rights reserved. 21187767Sluigi * 22187767Sluigi * Redistribution and use in source and binary forms are permitted 23187767Sluigi * provided that this notice is preserved and that due credit is given 24187767Sluigi * to the University of Michigan at Ann Arbor. The name of the University 25187767Sluigi * may not be used to endorse or promote products derived from this 26187767Sluigi * software without specific prior written permission. This software 27187767Sluigi * is provided ``as is'' without express or implied warranty. 28187767Sluigi */ 29187767Sluigi 30187767Sluigi#include "portable.h" 31187767Sluigi 32187767Sluigi#include <stdio.h> 33187767Sluigi 34187767Sluigi#include <ac/ctype.h> 35187767Sluigi#include <ac/socket.h> 36187767Sluigi#include <ac/string.h> 37187767Sluigi#include <ac/time.h> 38187767Sluigi 39187767Sluigi#include <sys/stat.h> 40187767Sluigi 41187767Sluigi#include "slap.h" 42187767Sluigi 43187767Sluigiint 44187767Sluigivalue_add( 45187767Sluigi BerVarray *vals, 46187767Sluigi BerVarray addvals ) 47187767Sluigi{ 48187767Sluigi int n, nn = 0; 49187767Sluigi BerVarray v2; 50187767Sluigi 51187767Sluigi if ( addvals != NULL ) { 52187767Sluigi for ( ; !BER_BVISNULL( &addvals[nn] ); nn++ ) 53187767Sluigi ; /* NULL */ 54187767Sluigi } 55187767Sluigi 56187767Sluigi if ( *vals == NULL ) { 57187767Sluigi *vals = (BerVarray) SLAP_MALLOC( (nn + 1) 58187767Sluigi * sizeof(struct berval) ); 59187767Sluigi if( *vals == NULL ) { 60187767Sluigi Debug(LDAP_DEBUG_TRACE, 61187767Sluigi "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 ); 62187767Sluigi return LBER_ERROR_MEMORY; 63187767Sluigi } 64187767Sluigi n = 0; 65187767Sluigi 66187767Sluigi } else { 67187767Sluigi for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) { 68187767Sluigi ; /* Empty */ 69187767Sluigi } 70187767Sluigi *vals = (BerVarray) SLAP_REALLOC( (char *) *vals, 71187767Sluigi (n + nn + 1) * sizeof(struct berval) ); 72187767Sluigi if( *vals == NULL ) { 73187767Sluigi Debug(LDAP_DEBUG_TRACE, 74187769Sluigi "value_add: SLAP_MALLOC failed.\n", 0, 0, 0 ); 75187769Sluigi return LBER_ERROR_MEMORY; 76187769Sluigi } 77187769Sluigi } 78187769Sluigi 79187769Sluigi v2 = &(*vals)[n]; 80187769Sluigi for ( n = 0 ; n < nn; v2++, addvals++ ) { 81187769Sluigi ber_dupbv( v2, addvals ); 82187769Sluigi if ( BER_BVISNULL( v2 ) ) break; 83187769Sluigi } 84187769Sluigi BER_BVZERO( v2 ); 85187769Sluigi 86187769Sluigi return LDAP_SUCCESS; 87187769Sluigi} 88187769Sluigi 89187769Sluigiint 90187769Sluigivalue_add_one( 91187769Sluigi BerVarray *vals, 92187769Sluigi struct berval *addval ) 93187769Sluigi{ 94187769Sluigi int n; 95187769Sluigi BerVarray v2; 96187769Sluigi 97187769Sluigi if ( *vals == NULL ) { 98190633Spiso *vals = (BerVarray) SLAP_MALLOC( 2 * sizeof(struct berval) ); 99187769Sluigi if( *vals == NULL ) { 100187769Sluigi Debug(LDAP_DEBUG_TRACE, 101187769Sluigi "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 ); 102187769Sluigi return LBER_ERROR_MEMORY; 103187769Sluigi } 104187769Sluigi n = 0; 105187769Sluigi 106187769Sluigi } else { 107187769Sluigi for ( n = 0; !BER_BVISNULL( &(*vals)[n] ); n++ ) { 108187769Sluigi ; /* Empty */ 109187769Sluigi } 110187769Sluigi *vals = (BerVarray) SLAP_REALLOC( (char *) *vals, 111187769Sluigi (n + 2) * sizeof(struct berval) ); 112187769Sluigi if( *vals == NULL ) { 113187769Sluigi Debug(LDAP_DEBUG_TRACE, 114187769Sluigi "value_add_one: SLAP_MALLOC failed.\n", 0, 0, 0 ); 115187769Sluigi return LBER_ERROR_MEMORY; 116187769Sluigi } 117187769Sluigi } 118187769Sluigi 119187769Sluigi v2 = &(*vals)[n]; 120187769Sluigi ber_dupbv(v2, addval); 121187769Sluigi 122187769Sluigi v2++; 123187769Sluigi BER_BVZERO( v2 ); 124187769Sluigi 125187769Sluigi return LDAP_SUCCESS; 126187769Sluigi} 127187769Sluigi 128187769Sluigiint asserted_value_validate_normalize( 129187769Sluigi AttributeDescription *ad, 130187769Sluigi MatchingRule *mr, 131187769Sluigi unsigned usage, 132187769Sluigi struct berval *in, 133187769Sluigi struct berval *out, 134187769Sluigi const char ** text, 135187769Sluigi void *ctx ) 136187769Sluigi{ 137187769Sluigi int rc; 138187769Sluigi struct berval pval; 139187769Sluigi pval.bv_val = NULL; 140187769Sluigi 141187769Sluigi /* we expect the value to be in the assertion syntax */ 142187769Sluigi assert( !SLAP_MR_IS_VALUE_OF_ATTRIBUTE_SYNTAX(usage) ); 143187769Sluigi 144187769Sluigi if( mr == NULL ) { 145187769Sluigi *text = "inappropriate matching request"; 146187769Sluigi return LDAP_INAPPROPRIATE_MATCHING; 147187769Sluigi } 148187769Sluigi 149187769Sluigi if( !mr->smr_match ) { 150187769Sluigi *text = "requested matching rule not supported"; 151187769Sluigi return LDAP_INAPPROPRIATE_MATCHING; 152187769Sluigi } 153187769Sluigi 154187769Sluigi if( mr->smr_syntax->ssyn_pretty ) { 155187769Sluigi rc = (mr->smr_syntax->ssyn_pretty)( mr->smr_syntax, in, &pval, ctx ); 156190865Sluigi in = &pval; 157194930Soleg 158187769Sluigi } else if ( mr->smr_syntax->ssyn_validate ) { 159187769Sluigi rc = (mr->smr_syntax->ssyn_validate)( mr->smr_syntax, in ); 160187769Sluigi 161187769Sluigi } else { 162187769Sluigi *text = "inappropriate matching request"; 163187769Sluigi return LDAP_INAPPROPRIATE_MATCHING; 164187769Sluigi } 165187769Sluigi 166187769Sluigi if( rc != LDAP_SUCCESS ) { 167187769Sluigi *text = "value does not conform to assertion syntax"; 168187769Sluigi return LDAP_INVALID_SYNTAX; 169187769Sluigi } 170187769Sluigi 171187769Sluigi if( mr->smr_normalize ) { 172187769Sluigi rc = (mr->smr_normalize)( 173187769Sluigi usage|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, 174187769Sluigi ad ? ad->ad_type->sat_syntax : NULL, 175187769Sluigi mr, in, out, ctx ); 176187769Sluigi 177187769Sluigi if( pval.bv_val ) ber_memfree_x( pval.bv_val, ctx ); 178187769Sluigi 179187769Sluigi if( rc != LDAP_SUCCESS ) { 180187769Sluigi *text = "unable to normalize value for matching"; 181187769Sluigi return LDAP_INVALID_SYNTAX; 182187769Sluigi } 183187769Sluigi 184187769Sluigi } else if ( pval.bv_val != NULL ) { 185187769Sluigi *out = pval; 186187769Sluigi 187187769Sluigi } else { 188187769Sluigi ber_dupbv_x( out, in, ctx ); 189187769Sluigi } 190187767Sluigi 191187767Sluigi return LDAP_SUCCESS; 192187767Sluigi} 193187767Sluigi 194187767Sluigiint 195187767Sluigivalue_match( 196187787Sluigi int *match, 197187787Sluigi AttributeDescription *ad, 198187767Sluigi MatchingRule *mr, 199187767Sluigi unsigned flags, 200187767Sluigi struct berval *v1, /* stored value */ 201187767Sluigi void *v2, /* assertion */ 202187770Sluigi const char ** text ) 203187767Sluigi{ 204187769Sluigi int rc; 205187767Sluigi 206187770Sluigi assert( mr != NULL ); 207187769Sluigi 208187770Sluigi if( !mr->smr_match ) { 209187770Sluigi return LDAP_INAPPROPRIATE_MATCHING; 210187769Sluigi } 211187769Sluigi 212187769Sluigi rc = (mr->smr_match)( match, flags, 213187769Sluigi ad->ad_type->sat_syntax, mr, v1, v2 ); 214187770Sluigi 215187769Sluigi return rc; 216187819Sluigi} 217187819Sluigi 218187819Sluigiint value_find_ex( 219187819Sluigi AttributeDescription *ad, 220187819Sluigi unsigned flags, 221187819Sluigi BerVarray vals, 222187819Sluigi struct berval *val, 223187819Sluigi void *ctx ) 224187819Sluigi{ 225187983Sluigi int i; 226187819Sluigi int rc; 227187819Sluigi struct berval nval = BER_BVNULL; 228187819Sluigi MatchingRule *mr = ad->ad_type->sat_equality; 229187769Sluigi 230187767Sluigi if( mr == NULL || !mr->smr_match ) { 231187767Sluigi return LDAP_INAPPROPRIATE_MATCHING; 232187767Sluigi } 233187767Sluigi 234187767Sluigi assert( SLAP_IS_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH( flags ) != 0 ); 235187767Sluigi 236187767Sluigi if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) && 237187770Sluigi mr->smr_normalize ) 238187767Sluigi { 239187767Sluigi rc = (mr->smr_normalize)( 240187767Sluigi flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX), 241187767Sluigi ad->ad_type->sat_syntax, 242187767Sluigi mr, val, &nval, ctx ); 243187767Sluigi 244187767Sluigi if( rc != LDAP_SUCCESS ) { 245187767Sluigi return LDAP_INVALID_SYNTAX; 246187767Sluigi } 247187767Sluigi } 248187767Sluigi 249187767Sluigi for ( i = 0; vals[i].bv_val != NULL; i++ ) { 250187983Sluigi int match; 251187983Sluigi const char *text; 252187983Sluigi 253187983Sluigi rc = value_match( &match, ad, mr, flags, 254187983Sluigi &vals[i], nval.bv_val == NULL ? val : &nval, &text ); 255187983Sluigi 256187770Sluigi if( rc == LDAP_SUCCESS && match == 0 ) { 257187769Sluigi slap_sl_free( nval.bv_val, ctx ); 258187769Sluigi return rc; 259187769Sluigi } 260187770Sluigi } 261187770Sluigi 262187819Sluigi slap_sl_free( nval.bv_val, ctx ); 263187819Sluigi return LDAP_NO_SUCH_ATTRIBUTE; 264187819Sluigi} 265187819Sluigi 266187770Sluigi/* assign new indexes to an attribute's ordered values */ 267187819Sluigivoid 268187819Sluigiordered_value_renumber( Attribute *a ) 269187770Sluigi{ 270187819Sluigi char *ptr, ibuf[64]; /* many digits */ 271187770Sluigi struct berval ibv, tmp, vtmp; 272187819Sluigi unsigned i; 273187819Sluigi 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