1 /* attr.c - routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 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 );
91 if ( ad && ( ad->ad_type->sat_flags & SLAP_AT_SORTED_VAL ))
92 a->a_flags |= SLAP_ATTR_SORTED_VALS;
97 /* Return a list of num attrs */
99 attrs_alloc( int num )
101 Attribute *head = NULL;
104 ldap_pvt_thread_mutex_lock( &attr_mutex );
105 for ( a = &attr_list; *a && num > 0; a = &(*a)->a_next ) {
112 attr_prealloc( num > CHUNK_SIZE ? num : CHUNK_SIZE );
114 for ( ; *a && num > 0; a = &(*a)->a_next ) {
122 ldap_pvt_thread_mutex_unlock( &attr_mutex );
129 attr_clean( Attribute *a )
131 if ( a->a_nvals && a->a_nvals != a->a_vals &&
132 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
133 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
136 ber_bvarray_free( a->a_nvals );
139 /* a_vals may be equal to slap_dummy_bv, a static empty berval;
140 * this is used as a placeholder for attributes that do not carry
141 * values, e.g. when proxying search entries with the "attrsonly"
143 if ( a->a_vals != &slap_dummy_bv &&
144 !( a->a_flags & SLAP_ATTR_DONT_FREE_VALS )) {
145 if ( a->a_flags & SLAP_ATTR_DONT_FREE_DATA ) {
148 ber_bvarray_free( a->a_vals );
154 #ifdef LDAP_COMP_MATCH
155 a->a_comp_data = NULL;
162 attr_free( Attribute *a )
165 ldap_pvt_thread_mutex_lock( &attr_mutex );
166 a->a_next = attr_list;
168 ldap_pvt_thread_mutex_unlock( &attr_mutex );
171 #ifdef LDAP_COMP_MATCH
173 comp_tree_free( Attribute *a )
177 for( ; a != NULL ; a = next ) {
179 if ( component_destructor && a->a_comp_data ) {
180 if ( a->a_comp_data->cd_mem_op )
181 component_destructor( a->a_comp_data->cd_mem_op );
182 free ( a->a_comp_data );
189 attrs_free( Attribute *a )
192 Attribute *b = (Attribute *)0xBAD, *tail, *next;
204 ldap_pvt_thread_mutex_lock( &attr_mutex );
205 /* replace NULL with current attr list and let attr list
206 * start from last attribute returned to list */
207 tail->a_next = attr_list;
209 ldap_pvt_thread_mutex_unlock( &attr_mutex );
214 attr_dup2( Attribute *tmp, Attribute *a )
216 tmp->a_flags = a->a_flags & SLAP_ATTR_PERSISTENT_FLAGS;
217 if ( a->a_vals != NULL ) {
220 tmp->a_numvals = a->a_numvals;
221 tmp->a_vals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
222 for ( i = 0; i < tmp->a_numvals; i++ ) {
223 ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
224 if ( BER_BVISNULL( &tmp->a_vals[i] ) ) break;
227 BER_BVZERO( &tmp->a_vals[i] );
229 /* a_nvals must be non null; it may be equal to a_vals */
230 assert( a->a_nvals != NULL );
232 if ( a->a_nvals != a->a_vals ) {
234 tmp->a_nvals = ch_malloc( (tmp->a_numvals + 1) * sizeof(struct berval) );
237 for ( ; !BER_BVISNULL( &a->a_nvals[j] ); j++ ) {
239 ber_dupbv( &tmp->a_nvals[j], &a->a_nvals[j] );
240 if ( BER_BVISNULL( &tmp->a_nvals[j] ) ) break;
245 BER_BVZERO( &tmp->a_nvals[j] );
248 tmp->a_nvals = tmp->a_vals;
254 attr_dup( Attribute *a )
258 if ( a == NULL) return NULL;
260 tmp = attr_alloc( a->a_desc );
266 attrs_dup( Attribute *a )
269 Attribute *tmp, *anew;
271 if( a == NULL ) return NULL;
274 for( tmp=a,i=0; tmp; tmp=tmp->a_next ) {
278 anew = attrs_alloc( i );
280 for( tmp=anew; a; a=a->a_next ) {
281 tmp->a_desc = a->a_desc;
297 struct berval nval = BER_BVNULL, *cval;
303 if ( flags & SLAP_MR_ORDERING )
304 mr = a->a_desc->ad_type->sat_ordering;
306 mr = a->a_desc->ad_type->sat_equality;
308 if( !SLAP_IS_MR_ASSERTED_VALUE_NORMALIZED_MATCH( flags ) &&
311 rc = (mr->smr_normalize)(
312 flags & (SLAP_MR_TYPE_MASK|SLAP_MR_SUBTYPE_MASK|SLAP_MR_VALUE_OF_SYNTAX),
313 a->a_desc->ad_type->sat_syntax,
314 mr, val, &nval, ctx );
316 if( rc != LDAP_SUCCESS ) {
317 return LDAP_INVALID_SYNTAX;
325 if ( (a->a_flags & SLAP_ATTR_SORTED_VALS) && n ) {
330 unsigned pivot = n >> 1;
332 rc = value_match( &match, a->a_desc, mr, flags,
333 &a->a_nvals[i], cval, &text );
334 if ( rc == LDAP_SUCCESS && match == 0 )
347 for ( i = 0; i < n; i++ ) {
350 rc = ordered_value_match( &match, a->a_desc, mr, flags,
351 &a->a_nvals[i], cval, &text );
352 if ( rc == LDAP_SUCCESS && match == 0 )
357 rc = LDAP_NO_SUCH_ATTRIBUTE;
361 slap_sl_free( nval.bv_val, ctx );
376 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_vals,
377 (a->a_numvals + nn + 1) * sizeof(struct berval) );
379 Debug(LDAP_DEBUG_TRACE,
380 "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
381 return LBER_ERROR_MEMORY;
385 v2 = (BerVarray) SLAP_REALLOC( (char *) a->a_nvals,
386 (a->a_numvals + nn + 1) * sizeof(struct berval) );
388 Debug(LDAP_DEBUG_TRACE,
389 "attr_valadd: SLAP_REALLOC failed.\n", 0, 0, 0 );
390 return LBER_ERROR_MEMORY;
394 a->a_nvals = a->a_vals;
397 /* If sorted and old vals exist, must insert */
398 if (( a->a_flags & SLAP_ATTR_SORTED_VALS ) && a->a_numvals ) {
401 v2 = nvals ? nvals : vals;
402 for ( i = 0; i < nn; i++ ) {
403 rc = attr_valfind( a, SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX |
404 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
405 &v2[i], &slot, NULL );
406 if ( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
407 /* should never happen */
408 if ( rc == LDAP_SUCCESS )
409 rc = LDAP_TYPE_OR_VALUE_EXISTS;
412 for ( j = a->a_numvals; j >= (int)slot; j-- ) {
413 a->a_vals[j+1] = a->a_vals[j];
415 a->a_nvals[j+1] = a->a_nvals[j];
417 ber_dupbv( &a->a_nvals[slot], &v2[i] );
419 ber_dupbv( &a->a_vals[slot], &vals[i] );
422 BER_BVZERO( &a->a_vals[a->a_numvals] );
423 if ( a->a_vals != a->a_nvals )
424 BER_BVZERO( &a->a_nvals[a->a_numvals] );
426 v2 = &a->a_vals[a->a_numvals];
427 for ( i = 0 ; i < nn; i++ ) {
428 ber_dupbv( &v2[i], &vals[i] );
429 if ( BER_BVISNULL( &v2[i] ) ) break;
431 BER_BVZERO( &v2[i] );
434 v2 = &a->a_nvals[a->a_numvals];
435 for ( i = 0 ; i < nn; i++ ) {
436 ber_dupbv( &v2[i], &nvals[i] );
437 if ( BER_BVISNULL( &v2[i] ) ) break;
439 BER_BVZERO( &v2[i] );
447 * attr_merge - merge the given type and value with the list of
448 * attributes in attrs.
450 * nvals must be NULL if the attribute has no normalizer.
451 * In this case, a->a_nvals will be set equal to a->a_vals.
453 * returns 0 everything went ok
460 AttributeDescription *desc,
468 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
469 if ( (*a)->a_desc == desc ) {
475 *a = attr_alloc( desc );
478 * FIXME: if the attribute already exists, the presence
479 * of nvals and the value of (*a)->a_nvals must be consistent
481 assert( ( nvals == NULL && (*a)->a_nvals == (*a)->a_vals )
482 || ( nvals != NULL && (
483 ( (*a)->a_vals == NULL && (*a)->a_nvals == NULL )
484 || ( (*a)->a_nvals != (*a)->a_vals ) ) ) );
487 if ( vals != NULL ) {
488 for ( ; !BER_BVISNULL( &vals[i] ); i++ ) ;
490 return attr_valadd( *a, vals, nvals, i );
494 * if a normalization function is defined for the equality matchingRule
495 * of desc, the value is normalized and stored in nval; otherwise nval
500 AttributeDescription *desc,
505 int rc = LDAP_SUCCESS;
506 BerVarray nvals = NULL;
510 if ( desc->ad_type->sat_equality &&
511 desc->ad_type->sat_equality->smr_normalize )
515 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ );
517 nvals = slap_sl_calloc( sizeof(struct berval), i + 1, memctx );
518 for ( i = 0; !BER_BVISNULL( &vals[i] ); i++ ) {
519 rc = desc->ad_type->sat_equality->smr_normalize(
520 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
521 desc->ad_type->sat_syntax,
522 desc->ad_type->sat_equality,
523 &vals[i], &nvals[i], memctx );
525 if ( rc != LDAP_SUCCESS ) {
526 BER_BVZERO( &nvals[i + 1] );
530 BER_BVZERO( &nvals[i] );
534 if ( rc != LDAP_SUCCESS && nvals != NULL ) {
535 ber_bvarray_free_x( nvals, memctx );
542 attr_merge_normalize(
544 AttributeDescription *desc,
548 BerVarray nvals = NULL;
551 rc = attr_normalize( desc, vals, &nvals, memctx );
552 if ( rc == LDAP_SUCCESS ) {
553 rc = attr_merge( e, desc, vals, nvals );
554 if ( nvals != NULL ) {
555 ber_bvarray_free_x( nvals, memctx );
565 AttributeDescription *desc,
567 struct berval *nval )
571 for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
572 if ( (*a)->a_desc == desc ) {
578 *a = attr_alloc( desc );
581 return attr_valadd( *a, val, nval, 1 );
585 * if a normalization function is defined for the equality matchingRule
586 * of desc, the value is normalized and stored in nval; otherwise nval
591 AttributeDescription *desc,
596 int rc = LDAP_SUCCESS;
600 if ( desc->ad_type->sat_equality &&
601 desc->ad_type->sat_equality->smr_normalize )
603 rc = desc->ad_type->sat_equality->smr_normalize(
604 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
605 desc->ad_type->sat_syntax,
606 desc->ad_type->sat_equality,
609 if ( rc != LDAP_SUCCESS ) {
618 attr_merge_normalize_one(
620 AttributeDescription *desc,
624 struct berval nval = BER_BVNULL;
625 struct berval *nvalp = NULL;
628 rc = attr_normalize_one( desc, val, &nval, memctx );
629 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &nval ) ) {
633 rc = attr_merge_one( e, desc, val, nvalp );
634 if ( nvalp != NULL ) {
635 slap_sl_free( nval.bv_val, memctx );
641 * attrs_find - find attribute(s) by AttributeDescription
642 * returns next attribute which is subtype of provided description.
648 AttributeDescription *desc )
650 for ( ; a != NULL; a = a->a_next ) {
651 if ( is_ad_subtype( a->a_desc, desc ) ) {
660 * attr_find - find attribute by type
666 AttributeDescription *desc )
668 for ( ; a != NULL; a = a->a_next ) {
669 if ( a->a_desc == desc ) {
678 * attr_delete - delete the attribute type in list pointed to by attrs
679 * return 0 deleted ok
680 * 1 not found in list a
681 * -1 something bad happened
687 AttributeDescription *desc )
691 for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
692 if ( (*a)->a_desc == desc ) {
693 Attribute *save = *a;
701 return LDAP_NO_SUCH_ATTRIBUTE;
707 ldap_pvt_thread_mutex_init( &attr_mutex );
716 for ( a=attr_chunks; a; a=attr_chunks ) {
717 attr_chunks = a->next;
720 ldap_pvt_thread_mutex_destroy( &attr_mutex );