1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
37 #include <ac/socket.h>
38 #include <ac/string.h>
44 * Allocate in chunks, minimum of 1000 at a time.
46 #define CHUNK_SIZE 1000
47 typedef struct slap_list {
48 struct slap_list *next;
50 static slap_list *attr_chunks;
51 static Attribute *attr_list;
52 static ldap_pvt_thread_mutex_t attr_mutex;
55 attr_prealloc( int num )
62 s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Attribute));
63 s->next = attr_chunks;
66 a = (Attribute *)(s+1);
67 for ( ;num>1; num--) {
71 a->a_next = attr_list;
72 attr_list = (Attribute *)(s+1);
78 attr_alloc( AttributeDescription *ad )
82 ldap_pvt_thread_mutex_lock( &attr_mutex );
84 attr_prealloc( CHUNK_SIZE );
86 attr_list = a->a_next;
88 ldap_pvt_thread_mutex_unlock( &attr_mutex );
95 /* Return a list of num attrs */
97 attrs_alloc( int num )
99 Attribute *head = NULL;
102 ldap_pvt_thread_mutex_lock( &attr_mutex );
103 for ( a = &attr_list; *a && num > 0; a = &(*a)->a_next ) {
110 attr_prealloc( num > CHUNK_SIZE ? num : CHUNK_SIZE );
112 for ( ; *a && num > 0; a = &(*a)->a_next ) {
120 ldap_pvt_thread_mutex_unlock( &attr_mutex );
127 attr_clean( Attribute *a )
129 if ( a->a_nvals && a->a_nvals != a->a_vals &&
130 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
131 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
134 ber_bvarray_free( a->a_nvals );
137 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
138 * this is used as a placeholder for attributes that do not carry
139 * values, e.g. when proxying search entries with the "attrsonly"
141 if ( a->a_vals != &slap_dummy_bv &&
142 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
143 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
146 ber_bvarray_free( a->a_vals );
152 #ifdef LDAP_COMP_MATCH
153 a->a_comp_data = NULL;
160 attr_free( Attribute *a )
163 ldap_pvt_thread_mutex_lock( &attr_mutex );
164 a->a_next = attr_list;
166 ldap_pvt_thread_mutex_unlock( &attr_mutex );
169 #ifdef LDAP_COMP_MATCH
171 comp_tree_free( Attribute *a )
175 for( ; a != NULL ; a = next ) {
177 if ( component_destructor && a->a_comp_data ) {
178 if ( a->a_comp_data->cd_mem_op )
179 component_destructor( a->a_comp_data->cd_mem_op );
180 free ( a->a_comp_data );
187 attrs_free( Attribute *a )
190 Attribute *b = (Attribute *)0xBAD, *tail, *next;
202 ldap_pvt_thread_mutex_lock( &attr_mutex );
203 /* replace NULL with current attr list and let attr list
204 * start from last attribute returned to list */
205 tail->a_next = attr_list;
207 ldap_pvt_thread_mutex_unlock( &attr_mutex );
212 attr_dup2( Attribute *tmp, Attribute *a )
214 tmp->a_flags = a->a_flags & SLAP_ATTR_PERSISTENT_FLAGS;
215 if ( a->a_vals != NULL ) {
218 tmp->a_numvals = a->a_numvals;
219 tmp->a_vals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
220 for ( i = 0; i < tmp->a_numvals; i++ ) {
221 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
222 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
225 BER_BVZERO( &tmp->a_vals[i] );
227 /* a_nvals must be non null; it may be equal to a_vals */
228 assert( a->a_nvals != NULL );
230 if ( a->a_nvals != a->a_vals ) {
233 tmp->a_nvals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
234 for ( j = 0; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
236 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
237 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
241 BER_BVZERO( &tmp->a_nvals[j] );
244 tmp->a_nvals = tmp->a_vals;
250 attr_dup( Attribute *a )
254 if ( a == NULL) return NULL;
256 tmp = attr_alloc( a->a_desc );
262 attrs_dup( Attribute *a )
265 Attribute *tmp, *anew;
267 if( a == NULL ) return NULL;
270 for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
274 anew = attrs_alloc( i );
276 for( tmp=anew; a; a=a->a_next ) {
277 tmp->a_desc = a->a_desc;
293 struct berval nval = BER_BVNULL, *cval;
299 if ( flags & SLAP_MR_ORDERING )
300 mr = a->a_desc->ad_type->sat_ordering;
302 mr = a->a_desc->ad_type->sat_equality;
304 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
307 rc = (mr->smr_normalize)(
308 flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
309 a->a_desc->ad_type->sat_syntax,
310 mr, val, &nval, ctx );
312 if( rc != LDAP_SUCCESS ) {
313 return LDAP_INVALID_SYNTAX;
320 if ( a->a_flags & SLAP_ATTR_SORTED_VALS ) {
322 unsigned base = 0, n = a->a_numvals;
325 unsigned pivot = n >> 1;
327 if ( i >= a->a_numvals ) {
328 i = a->a_numvals - 1;
331 rc = value_match( &match, a->a_desc, mr, flags,
332 &a->a_nvals[i], cval, &text );
333 if ( rc == LDAP_SUCCESS && match == 0 )
343 for ( i = 0; i < a->a_numvals; i++ ) {
346 rc = ordered_value_match( &match, a->a_desc, mr, flags,
347 &a->a_nvals[i], cval, &text );
348 if ( rc == LDAP_SUCCESS && match == 0 )
355 rc = LDAP_NO_SUCH_ATTRIBUTE;
357 slap_sl_free( nval.bv_val, ctx );
372 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_vals,
373 (a->a_numvals + nn + 1) * sizeof(struct berval) );
375 Debug(LDAP_DEBUG_TRACE,
376 "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
377 return LBER_ERROR_MEMORY;
381 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_nvals,
382 (a->a_numvals + nn + 1) * sizeof(struct berval) );
384 Debug(LDAP_DEBUG_TRACE,
385 "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
386 return LBER_ERROR_MEMORY;
390 a->a_nvals = a->a_vals;
393 /* If sorted and old vals exist, must insert */
394 if (( a->a_flags & SLAP_ATTR_SORTED_VALS ) && a->a_numvals ) {
397 v2 = nvals ? nvals : vals;
398 for ( i = 0; i < nn; i++ ) {
399 rc = attr_valfind( a, SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX |
400 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
401 &v2[i], &slot, NULL );
402 if ( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
403 /* should never happen */
404 if ( rc == LDAP_SUCCESS )
405 rc = LDAP_TYPE_OR_VALUE_EXISTS;
408 for ( j = a->a_numvals; j >= slot; j-- ) {
409 a->a_vals[j+1] = a->a_vals[j];
411 a->a_nvals[j+1] = a->a_nvals[j];
413 ber_dupbv( &a->a_nvals[slot], &v2[i] );
415 ber_dupbv( &a->a_vals[slot], &vals[i] );
418 BER_BVZERO( &a->a_vals[a->a_numvals] );
419 if ( a->a_vals != a->a_nvals )
420 BER_BVZERO( &a->a_nvals[a->a_numvals] );
422 v2 = &a->a_vals[a->a_numvals];
423 for ( i = 0 ; i < nn; i++ ) {
424 ber_dupbv( &v2[i], &vals[i] );
425 if ( BER_BVISNULL( &v2[i] ) ) break;
427 BER_BVZERO( &v2[i] );
430 v2 = &a->a_nvals[a->a_numvals];
431 for ( i = 0 ; i < nn; i++ ) {
432 ber_dupbv( &v2[i], &nvals[i] );
433 if ( BER_BVISNULL( &v2[i] ) ) break;
435 BER_BVZERO( &v2[i] );
443 * attr_merge - merge the given type and value with the list of
444 * attributes in attrs.
446 * nvals must be NULL if the attribute has no normalizer.
447 * In this case, a->a_nvals will be set equal to a->a_vals.
449 * returns 0 everything went ok
456 AttributeDescription *desc,
464 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
465 if ( (*a)->a_desc == desc ) {
471 *a = attr_alloc( desc );
474 * FIXME: if the attribute already exists, the presence
475 * of nvals and the value of (*a)->a_nvals must be consistent
477 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
478 || ( nvals != NULL && (
479 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
480 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
483 if ( vals != NULL ) {
484 for ( ; !BER_BVISNULL( &vals[i] ); i++ ) ;
486 return attr_valadd( *a, vals, nvals, i );
490 * if a normalization function is defined for the equality matchingRule
491 * of desc, the value is normalized and stored in nval; otherwise nval
496 AttributeDescription *desc,
501 int rc = LDAP_SUCCESS;
502 BerVarray nvals = NULL;
506 if ( desc->ad_type->sat_equality &&
507 desc->ad_type->sat_equality->smr_normalize )
511 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
513 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
514 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
515 rc = desc->ad_type->sat_equality->smr_normalize(
516 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
517 desc->ad_type->sat_syntax,
518 desc->ad_type->sat_equality,
519 &vals[i], &nvals[i], memctx );
521 if ( rc != LDAP_SUCCESS ) {
522 BER_BVZERO( &nvals[i + 1] );
526 BER_BVZERO( &nvals[i] );
530 if ( rc != LDAP_SUCCESS && nvals != NULL ) {
531 ber_bvarray_free_x( nvals, memctx );
538 attr_merge_normalize(
540 AttributeDescription *desc,
544 BerVarray nvals = NULL;
547 rc = attr_normalize( desc, vals, &nvals, memctx );
548 if ( rc == LDAP_SUCCESS ) {
549 rc = attr_merge( e, desc, vals, nvals );
550 if ( nvals != NULL ) {
551 ber_bvarray_free_x( nvals, memctx );
561 AttributeDescription *desc,
563 struct berval *nval )
567 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
568 if ( (*a)->a_desc == desc ) {
574 *a = attr_alloc( desc );
577 return attr_valadd( *a, val, nval, 1 );
581 * if a normalization function is defined for the equality matchingRule
582 * of desc, the value is normalized and stored in nval; otherwise nval
587 AttributeDescription *desc,
592 int rc = LDAP_SUCCESS;
596 if ( desc->ad_type->sat_equality &&
597 desc->ad_type->sat_equality->smr_normalize )
599 rc = desc->ad_type->sat_equality->smr_normalize(
600 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
601 desc->ad_type->sat_syntax,
602 desc->ad_type->sat_equality,
605 if ( rc != LDAP_SUCCESS ) {
614 attr_merge_normalize_one(
616 AttributeDescription *desc,
620 struct berval nval = BER_BVNULL;
621 struct berval *nvalp = NULL;
624 rc = attr_normalize_one( desc, val, &nval, memctx );
625 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
629 rc = attr_merge_one( e, desc, val, nvalp );
630 if ( nvalp != NULL ) {
631 slap_sl_free( nval.bv_val, memctx );
637 * attrs_find - find attribute(s) by AttributeDescription
638 * returns next attribute which is subtype of provided description.
644 AttributeDescription *desc )
646 for ( ; a != NULL; a = a->a_next ) {
647 if ( is_ad_subtype( a->a_desc, desc ) ) {
656 * attr_find - find attribute by type
662 AttributeDescription *desc )
664 for ( ; a != NULL; a = a->a_next ) {
665 if ( a->a_desc == desc ) {
674 * attr_delete - delete the attribute type in list pointed to by attrs
675 * return 0 deleted ok
676 * 1 not found in list a
677 * -1 something bad happened
683 AttributeDescription *desc )
687 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
688 if ( (*a)->a_desc == desc ) {
689 Attribute *save = *a;
697 return LDAP_NO_SUCH_ATTRIBUTE;
703 ldap_pvt_thread_mutex_init( &attr_mutex );
712 for ( a=attr_chunks; a; a=attr_chunks ) {
713 attr_chunks = a->next;
716 ldap_pvt_thread_mutex_destroy( &attr_mutex );